sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:commit_check; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:commands; 7, string:""; 8, default_parameter; 8, 9; 8, 10; 9, identifier:req_format; 10, string:"text"; 11, block; 11, 12; 11, 14; 11, 23; 11, 27; 11, 41; 11, 47; 11, 61; 11, 62; 11, 63; 11, 83; 11, 84; 11, 90; 11, 102; 11, 106; 11, 107; 11, 108; 11, 189; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 17; 15, not_operator; 15, 16; 16, identifier:commands; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:InvalidCommandError; 21, argument_list; 21, 22; 22, string:'No commands specified'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:clean_cmds; 26, list:[]; 27, for_statement; 27, 28; 27, 29; 27, 33; 28, identifier:cmd; 29, call; 29, 30; 29, 31; 30, identifier:clean_lines; 31, argument_list; 31, 32; 32, identifier:commands; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:clean_cmds; 38, identifier:append; 39, argument_list; 39, 40; 40, identifier:cmd; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:lock; 46, argument_list; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_session; 53, identifier:load_configuration; 54, argument_list; 54, 55; 54, 58; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:action; 57, string:'set'; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:config; 60, identifier:clean_cmds; 61, comment; 62, comment; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:results; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:ET; 69, identifier:fromstring; 70, argument_list; 70, 71; 71, attribute; 71, 72; 71, 82; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:_session; 77, identifier:validate; 78, argument_list; 78, 79; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:source; 81, string:'candidate'; 82, identifier:tostring; 83, comment; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:unlock; 89, argument_list; 90, if_statement; 90, 91; 90, 94; 91, comparison_operator:==; 91, 92; 91, 93; 92, identifier:req_format; 93, string:"xml"; 94, block; 94, 95; 95, return_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:ET; 99, identifier:tostring; 100, argument_list; 100, 101; 101, identifier:results; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:out; 105, string:""; 106, comment; 107, comment; 108, for_statement; 108, 109; 108, 110; 108, 115; 108, 116; 108, 117; 109, identifier:i; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:results; 113, identifier:iter; 114, argument_list; 115, comment; 116, comment; 117, block; 117, 118; 118, if_statement; 118, 119; 118, 124; 118, 129; 118, 130; 118, 131; 118, 163; 118, 164; 118, 165; 119, comparison_operator:==; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:i; 122, identifier:tag; 123, string:'commit-check-success'; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, augmented_assignment:+=; 126, 127; 126, 128; 127, identifier:out; 128, string:'configuration check succeeds\n'; 129, comment; 130, comment; 131, elif_clause; 131, 132; 131, 137; 132, comparison_operator:is; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:i; 135, identifier:text; 136, None; 137, block; 137, 138; 138, if_statement; 138, 139; 138, 150; 139, comparison_operator:!=; 139, 140; 139, 149; 140, binary_operator:+; 140, 141; 140, 148; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:i; 145, identifier:text; 146, identifier:strip; 147, argument_list; 148, string:'\n'; 149, string:'\n'; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, augmented_assignment:+=; 152, 153; 152, 154; 153, identifier:out; 154, binary_operator:+; 154, 155; 154, 162; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:i; 159, identifier:text; 160, identifier:strip; 161, argument_list; 162, string:'\n'; 163, comment; 164, comment; 165, elif_clause; 165, 166; 165, 171; 166, comparison_operator:is; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:i; 169, identifier:text; 170, None; 171, block; 171, 172; 172, if_statement; 172, 173; 172, 180; 173, comparison_operator:!=; 173, 174; 173, 179; 174, binary_operator:+; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:i; 177, identifier:tag; 178, string:'\n'; 179, string:'\n'; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, augmented_assignment:+=; 182, 183; 182, 184; 183, identifier:out; 184, binary_operator:+; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:i; 187, identifier:tag; 188, string:'\n'; 189, return_statement; 189, 190; 190, identifier:out
def commit_check(self, commands="", req_format="text"): """ Execute a commit check operation. Purpose: This method will take in string of multiple commands, | and perform and 'commit check' on the device to ensure | the commands are syntactically correct. The response can | be formatted as text or as xml. @param commands: A string, filepath, or list of multiple commands | that the device will compare with. @type commands: str or list @param req_format: The desired format of the response, defaults to | 'text', but also accepts 'xml' @type req_format: str @returns: The reply from the device. @rtype: str """ if not commands: raise InvalidCommandError('No commands specified') clean_cmds = [] for cmd in clean_lines(commands): clean_cmds.append(cmd) self.lock() self._session.load_configuration(action='set', config=clean_cmds) # conn.validate() DOES NOT return a parse-able xml tree, so we # convert it to an ElementTree xml tree. results = ET.fromstring(self._session.validate( source='candidate').tostring) # release the candidate configuration self.unlock() if req_format == "xml": return ET.tostring(results) out = "" # we have to parse the elementTree object, and get the text # from the xml. for i in results.iter(): # the success message is just a tag, so we need to get it # specifically. if i.tag == 'commit-check-success': out += 'configuration check succeeds\n' # this is for normal output with a tag and inner text, it will # strip the inner text and add it to the output. elif i.text is not None: if i.text.strip() + '\n' != '\n': out += i.text.strip() + '\n' # this is for elements that don't have inner text, it will add the # tag to the output. elif i.text is None: if i.tag + '\n' != '\n': out += i.tag + '\n' return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:connect; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 95; 5, 387; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, comparison_operator:==; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:conn_type; 13, string:'paramiko'; 14, block; 14, 15; 14, 25; 14, 26; 14, 27; 14, 40; 14, 49; 14, 62; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_session; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:paramiko; 23, identifier:SSHClient; 24, argument_list; 25, comment; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:logger; 30, call; 30, 31; 30, 38; 31, attribute; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:logging; 35, identifier:Logger; 36, identifier:manager; 37, identifier:getLogger; 38, argument_list; 38, 39; 39, string:'paramiko.transport'; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:logger; 44, identifier:setLevel; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:logging; 48, identifier:CRITICAL; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:_session; 55, identifier:set_missing_host_key_policy; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:paramiko; 60, identifier:AutoAddPolicy; 61, argument_list; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:_session; 68, identifier:connect; 69, argument_list; 69, 70; 69, 75; 69, 80; 69, 85; 69, 90; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:hostname; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:host; 75, keyword_argument; 75, 76; 75, 77; 76, identifier:username; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:username; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:password; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:password; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:port; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:port; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:timeout; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:connect_timeout; 95, if_statement; 95, 96; 95, 101; 95, 195; 95, 246; 95, 337; 96, comparison_operator:==; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:conn_type; 100, string:'scp'; 101, block; 101, 102; 101, 112; 101, 125; 101, 134; 101, 147; 101, 180; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:_scp_session; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:paramiko; 110, identifier:SSHClient; 111, argument_list; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:logger; 115, call; 115, 116; 115, 123; 116, attribute; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:logging; 120, identifier:Logger; 121, identifier:manager; 122, identifier:getLogger; 123, argument_list; 123, 124; 124, string:'paramiko.transport'; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:logger; 129, identifier:setLevel; 130, argument_list; 130, 131; 131, attribute; 131, 132; 131, 133; 132, identifier:logging; 133, identifier:CRITICAL; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:_scp_session; 140, identifier:set_missing_host_key_policy; 141, argument_list; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:paramiko; 145, identifier:AutoAddPolicy; 146, argument_list; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:_scp_session; 153, identifier:connect; 154, argument_list; 154, 155; 154, 160; 154, 165; 154, 170; 154, 175; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:hostname; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:host; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:username; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:username; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:password; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:password; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:port; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:port; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:timeout; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:connect_timeout; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:self; 184, identifier:_scp; 185, call; 185, 186; 185, 187; 186, identifier:SCPClient; 187, argument_list; 187, 188; 188, call; 188, 189; 188, 194; 189, attribute; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:_scp_session; 193, identifier:get_transport; 194, argument_list; 195, elif_clause; 195, 196; 195, 201; 196, comparison_operator:==; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:conn_type; 200, string:"ncclient"; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:_session; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:manager; 210, identifier:connect; 211, argument_list; 211, 212; 211, 217; 211, 222; 211, 227; 211, 232; 211, 237; 211, 243; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:host; 214, attribute; 214, 215; 214, 216; 215, identifier:self; 216, identifier:host; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:port; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:port; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:username; 224, attribute; 224, 225; 224, 226; 225, identifier:self; 226, identifier:username; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:password; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:password; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:timeout; 234, attribute; 234, 235; 234, 236; 235, identifier:self; 236, identifier:connect_timeout; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:device_params; 239, dictionary; 239, 240; 240, pair; 240, 241; 240, 242; 241, string:'name'; 242, string:'junos'; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:hostkey_verify; 245, False; 246, elif_clause; 246, 247; 246, 252; 247, comparison_operator:==; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:self; 250, identifier:conn_type; 251, string:'shell'; 252, block; 252, 253; 252, 277; 252, 320; 253, if_statement; 253, 254; 253, 258; 254, not_operator; 254, 255; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:_session; 258, block; 258, 259; 258, 265; 258, 271; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:conn_type; 264, string:'paramiko'; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:connect; 270, argument_list; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:conn_type; 276, string:'shell'; 277, if_statement; 277, 278; 277, 282; 278, not_operator; 278, 279; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:_shell; 282, block; 282, 283; 282, 295; 282, 302; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:_shell; 288, call; 288, 289; 288, 294; 289, attribute; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:_session; 293, identifier:invoke_shell; 294, argument_list; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:time; 299, identifier:sleep; 300, argument_list; 300, 301; 301, integer:2; 302, if_statement; 302, 303; 302, 313; 303, boolean_operator:and; 303, 304; 303, 309; 304, comparison_operator:!=; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:self; 307, identifier:username; 308, string:'root'; 309, not_operator; 309, 310; 310, attribute; 310, 311; 310, 312; 311, identifier:self; 312, identifier:_in_cli; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:self; 318, identifier:_in_cli; 319, True; 320, if_statement; 320, 321; 320, 327; 321, not_operator; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:self; 325, identifier:cli_to_shell; 326, argument_list; 327, block; 327, 328; 328, expression_statement; 328, 329; 329, call; 329, 330; 329, 335; 330, attribute; 330, 331; 330, 334; 331, attribute; 331, 332; 331, 333; 332, identifier:self; 333, identifier:_shell; 334, identifier:recv; 335, argument_list; 335, 336; 336, integer:9999; 337, elif_clause; 337, 338; 337, 343; 337, 344; 338, comparison_operator:==; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:self; 341, identifier:conn_type; 342, string:'root'; 343, comment; 344, block; 344, 345; 344, 370; 345, if_statement; 345, 346; 345, 350; 346, not_operator; 346, 347; 347, attribute; 347, 348; 347, 349; 348, identifier:self; 349, identifier:_shell; 350, block; 350, 351; 350, 363; 351, expression_statement; 351, 352; 352, assignment; 352, 353; 352, 356; 353, attribute; 353, 354; 353, 355; 354, identifier:self; 355, identifier:_shell; 356, call; 356, 357; 356, 362; 357, attribute; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:self; 360, identifier:_session; 361, identifier:invoke_shell; 362, argument_list; 363, expression_statement; 363, 364; 364, call; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:time; 367, identifier:sleep; 368, argument_list; 368, 369; 369, integer:2; 370, if_statement; 370, 371; 370, 377; 371, not_operator; 371, 372; 372, call; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:self; 375, identifier:shell_to_cli; 376, argument_list; 377, block; 377, 378; 378, expression_statement; 378, 379; 379, call; 379, 380; 379, 385; 380, attribute; 380, 381; 380, 384; 381, attribute; 381, 382; 381, 383; 382, identifier:self; 383, identifier:_shell; 384, identifier:recv; 385, argument_list; 385, 386; 386, integer:9999; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:self; 391, identifier:_update_timeout; 392, argument_list; 392, 393; 393, attribute; 393, 394; 393, 395; 394, identifier:self; 395, identifier:session_timeout
def connect(self): """ Establish a connection to the device. Purpose: This method is used to make a connection to the junos | device. The internal property conn_type is what | determines the type of connection we make to the device. | - 'paramiko' is used for operational commands (to allow | pipes in commands) | - 'scp' is used for copying files | - 'shell' is used for to send shell commands | - 'root' is used when logging into the device as root, and | wanting to send operational commands | - 'ncclient' is used for the rest (commit, compare_config, | commit_check) @returns: None @rtype: None """ if self.conn_type == 'paramiko': self._session = paramiko.SSHClient() # These two lines set the paramiko logging to Critical to # remove extra messages from being sent to the user output. logger = logging.Logger.manager.getLogger('paramiko.transport') logger.setLevel(logging.CRITICAL) self._session.set_missing_host_key_policy( paramiko.AutoAddPolicy()) self._session.connect(hostname=self.host, username=self.username, password=self.password, port=self.port, timeout=self.connect_timeout) if self.conn_type == 'scp': self._scp_session = paramiko.SSHClient() logger = logging.Logger.manager.getLogger('paramiko.transport') logger.setLevel(logging.CRITICAL) self._scp_session.set_missing_host_key_policy( paramiko.AutoAddPolicy()) self._scp_session.connect(hostname=self.host, username=self.username, password=self.password, port=self.port, timeout=self.connect_timeout) self._scp = SCPClient(self._scp_session.get_transport()) elif self.conn_type == "ncclient": self._session = manager.connect( host=self.host, port=self.port, username=self.username, password=self.password, timeout=self.connect_timeout, device_params={'name': 'junos'}, hostkey_verify=False ) elif self.conn_type == 'shell': if not self._session: self.conn_type = 'paramiko' self.connect() self.conn_type = 'shell' if not self._shell: self._shell = self._session.invoke_shell() time.sleep(2) if self.username != 'root' and not self._in_cli: self._in_cli = True if not self.cli_to_shell(): self._shell.recv(9999) elif self.conn_type == 'root': # open the shell if necessary, and move into CLI if not self._shell: self._shell = self._session.invoke_shell() time.sleep(2) if not self.shell_to_cli(): self._shell.recv(9999) self._update_timeout(self.session_timeout)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:script; 3, parameters; 3, 4; 3, 5; 4, identifier:inbox; 5, identifier:cfg; 6, block; 6, 7; 6, 9; 6, 15; 6, 47; 6, 48; 6, 49; 6, 53; 6, 64; 6, 70; 6, 106; 6, 107; 6, 132; 6, 133; 6, 139; 6, 143; 6, 296; 6, 297; 6, 319; 6, 345; 6, 346; 6, 347; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:script_name; 12, subscript; 12, 13; 12, 14; 13, identifier:cfg; 14, string:"id"; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:script_id; 18, subscript; 18, 19; 18, 43; 19, call; 19, 20; 19, 21; 20, identifier:str; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:abs; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:hash; 27, argument_list; 27, 28; 28, binary_operator:+; 28, 29; 28, 33; 29, tuple; 29, 30; 30, subscript; 30, 31; 30, 32; 31, identifier:cfg; 32, string:"id"; 33, call; 33, 34; 33, 35; 34, identifier:tuple; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:inbox; 40, integer:0; 41, identifier:values; 42, argument_list; 43, slice; 43, 44; 43, 45; 43, 46; 44, integer:0; 45, colon; 46, integer:8; 47, comment; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:args; 52, dictionary; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:args; 57, string:"params"; 58, call; 58, 59; 58, 60; 59, identifier:dict; 60, argument_list; 60, 61; 61, subscript; 61, 62; 61, 63; 62, identifier:cfg; 63, string:"params"; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:args; 68, string:"in"; 69, dictionary; 70, for_statement; 70, 71; 70, 72; 70, 75; 71, identifier:in_port; 72, subscript; 72, 73; 72, 74; 73, identifier:cfg; 74, string:"in"; 75, block; 75, 76; 76, for_statement; 76, 77; 76, 78; 76, 79; 77, identifier:inin_ports; 78, identifier:inbox; 79, block; 79, 80; 79, 90; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:in_path; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:inin_ports; 86, identifier:get; 87, argument_list; 87, 88; 87, 89; 88, identifier:in_port; 89, None; 90, if_statement; 90, 91; 90, 95; 90, 96; 91, parenthesized_expression; 91, 92; 92, comparison_operator:is; 92, 93; 92, 94; 93, identifier:in_path; 94, None; 95, comment; 96, block; 96, 97; 96, 105; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 104; 99, subscript; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:args; 102, string:"in"; 103, identifier:in_port; 104, identifier:in_path; 105, break_statement; 106, comment; 107, if_statement; 107, 108; 107, 121; 108, comparison_operator:<; 108, 109; 108, 115; 109, call; 109, 110; 109, 111; 110, identifier:len; 111, argument_list; 111, 112; 112, subscript; 112, 113; 112, 114; 113, identifier:args; 114, string:"in"; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, subscript; 118, 119; 118, 120; 119, identifier:cfg; 120, string:"in"; 121, block; 121, 122; 122, raise_statement; 122, 123; 123, call; 123, 124; 123, 125; 124, identifier:Exception; 125, argument_list; 125, 126; 126, binary_operator:%; 126, 127; 126, 128; 127, string:"not all in_ports connected, got: %s"; 128, tuple; 128, 129; 129, subscript; 129, 130; 129, 131; 130, identifier:args; 131, string:"in"; 132, comment; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:args; 137, string:"out"; 138, dictionary; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:out; 142, dictionary; 143, for_statement; 143, 144; 143, 149; 143, 155; 144, pattern_list; 144, 145; 144, 146; 145, identifier:i; 146, tuple_pattern; 146, 147; 146, 148; 147, identifier:out_port; 148, identifier:out_ext; 149, call; 149, 150; 149, 151; 150, identifier:enumerate; 151, argument_list; 151, 152; 152, subscript; 152, 153; 152, 154; 153, identifier:cfg; 154, string:"out"; 155, block; 155, 156; 155, 249; 155, 282; 155, 290; 156, if_statement; 156, 157; 156, 172; 156, 208; 157, comparison_operator:==; 157, 158; 157, 161; 158, subscript; 158, 159; 158, 160; 159, identifier:cfg; 160, string:"in"; 161, call; 161, 162; 161, 163; 162, identifier:tuple; 163, generator_expression; 163, 164; 163, 165; 164, identifier:out_port_; 165, for_in_clause; 165, 166; 165, 169; 166, pattern_list; 166, 167; 166, 168; 167, identifier:out_port_; 168, identifier:_; 169, subscript; 169, 170; 169, 171; 170, identifier:cfg; 171, string:"out"; 172, block; 172, 173; 172, 202; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:pfx; 176, binary_operator:+; 176, 177; 176, 201; 177, subscript; 177, 178; 177, 200; 178, call; 178, 179; 178, 198; 179, attribute; 179, 180; 179, 197; 180, subscript; 180, 181; 180, 195; 181, call; 181, 182; 181, 193; 182, attribute; 182, 183; 182, 192; 183, subscript; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:args; 186, string:"in"; 187, subscript; 187, 188; 187, 191; 188, subscript; 188, 189; 188, 190; 189, identifier:cfg; 190, string:"in"; 191, identifier:i; 192, identifier:split; 193, argument_list; 193, 194; 194, string:"/"; 195, unary_operator:-; 195, 196; 196, integer:1; 197, identifier:split; 198, argument_list; 198, 199; 199, string:"."; 200, integer:0; 201, string:"_"; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:base; 205, subscript; 205, 206; 205, 207; 206, identifier:cfg; 207, string:"id"; 208, else_clause; 208, 209; 209, block; 209, 210; 209, 239; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:pfx; 213, binary_operator:+; 213, 214; 213, 238; 214, subscript; 214, 215; 214, 237; 215, call; 215, 216; 215, 235; 216, attribute; 216, 217; 216, 234; 217, subscript; 217, 218; 217, 232; 218, call; 218, 219; 218, 230; 219, attribute; 219, 220; 219, 229; 220, subscript; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:args; 223, string:"in"; 224, subscript; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:cfg; 227, string:"in"; 228, integer:0; 229, identifier:split; 230, argument_list; 230, 231; 231, string:"/"; 232, unary_operator:-; 232, 233; 233, integer:1; 234, identifier:split; 235, argument_list; 235, 236; 236, string:"."; 237, integer:0; 238, string:"_"; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:base; 242, binary_operator:+; 242, 243; 242, 248; 243, binary_operator:+; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:cfg; 246, string:"id"; 247, string:"-"; 248, identifier:out_port; 249, if_statement; 249, 250; 249, 251; 249, 268; 250, identifier:out_ext; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:out_path; 255, binary_operator:+; 255, 256; 255, 267; 256, binary_operator:+; 256, 257; 256, 266; 257, binary_operator:+; 257, 258; 257, 265; 258, binary_operator:+; 258, 259; 258, 264; 259, binary_operator:+; 259, 260; 259, 263; 260, subscript; 260, 261; 260, 262; 261, identifier:cfg; 262, string:"dir"; 263, string:"/"; 264, identifier:pfx; 265, identifier:base; 266, string:"."; 267, identifier:out_ext; 268, else_clause; 268, 269; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:out_path; 273, binary_operator:+; 273, 274; 273, 281; 274, binary_operator:+; 274, 275; 274, 280; 275, binary_operator:+; 275, 276; 275, 279; 276, subscript; 276, 277; 276, 278; 277, identifier:cfg; 278, string:"dir"; 279, string:"/"; 280, identifier:pfx; 281, identifier:base; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 289; 284, subscript; 284, 285; 284, 288; 285, subscript; 285, 286; 285, 287; 286, identifier:args; 287, string:"out"; 288, identifier:out_port; 289, identifier:out_path; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 295; 292, subscript; 292, 293; 292, 294; 293, identifier:out; 294, identifier:out_port; 295, identifier:out_path; 296, comment; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 300; 299, identifier:ret; 300, call; 300, 301; 300, 302; 301, identifier:_eval_script; 302, argument_list; 302, 303; 302, 306; 302, 309; 302, 312; 302, 315; 302, 318; 303, subscript; 303, 304; 303, 305; 304, identifier:cfg; 305, string:"evaluator"; 306, subscript; 306, 307; 306, 308; 307, identifier:cfg; 308, string:"preamble"; 309, subscript; 309, 310; 309, 311; 310, identifier:cfg; 311, string:"dir"; 312, subscript; 312, 313; 312, 314; 313, identifier:cfg; 314, string:"executable"; 315, subscript; 315, 316; 315, 317; 316, identifier:cfg; 317, string:"script"; 318, identifier:args; 319, if_statement; 319, 320; 319, 325; 319, 326; 319, 327; 319, 328; 320, comparison_operator:!=; 320, 321; 320, 324; 321, subscript; 321, 322; 321, 323; 322, identifier:ret; 323, integer:0; 324, integer:0; 325, comment; 326, comment; 327, comment; 328, block; 328, 329; 329, raise_statement; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:Exception; 332, argument_list; 332, 333; 332, 336; 332, 339; 332, 342; 333, subscript; 333, 334; 333, 335; 334, identifier:ret; 335, integer:0; 336, subscript; 336, 337; 336, 338; 337, identifier:cfg; 338, string:"script"; 339, subscript; 339, 340; 339, 341; 340, identifier:ret; 341, integer:1; 342, subscript; 342, 343; 342, 344; 343, identifier:ret; 344, integer:2; 345, comment; 346, comment; 347, return_statement; 347, 348; 348, identifier:out
def script(inbox, cfg): """ Execute arbitrary scripts. Arguments: - cfg(``dict``) script configuartion dictionary """ script_name = cfg["id"] script_id = str(abs(hash((cfg["id"],) + tuple(inbox[0].values()))))[0:8] # LOG.log(mp.DEFAULT, "@papy;script %s:%s started" % (script_name, script_id)) # LOG.log(mp.SUBDEFAULT, "@papy;%s:%s received: %s" % (script_name, script_id, inbox)) args = {} args["params"] = dict(cfg["params"]) args["in"] = {} for in_port in cfg["in"]: for inin_ports in inbox: in_path = inin_ports.get(in_port, None) if (in_path is not None): # first matching input-output (including type) port is linked remaining ignored args["in"][in_port] = in_path break # check that all input ports are connected if len(args["in"]) < len(cfg["in"]): raise Exception("not all in_ports connected, got: %s" % (args["in"],)) # create output file for out_ports args["out"] = {} out = {} for i, (out_port, out_ext) in enumerate(cfg["out"]): if cfg["in"] == tuple(out_port_ for out_port_, _ in cfg["out"]): pfx = args["in"][cfg["in"][i]].split("/")[-1].split(".")[0] + "_" base = cfg["id"] else: pfx = args["in"][cfg["in"][0]].split("/")[-1].split(".")[0] + "_" base = cfg["id"] + "-" + out_port if out_ext: out_path = cfg["dir"] + "/" + pfx + base + "." + out_ext else: out_path = cfg["dir"] + "/" + pfx + base args["out"][out_port] = out_path out[out_port] = out_path # evaluate and check for errors ret = _eval_script(cfg["evaluator"], cfg["preamble"], cfg["dir"], cfg["executable"], cfg["script"], args) if ret[0] != 0: # LOG.error("@papy;%s:%s %s:%s:%s" % (script_name, script_id, ret[0], # ret[1].replace("\n", "<br>"), # ret[2].replace("\n", "<br>"))) raise Exception(ret[0], cfg["script"], ret[1], ret[2]) #LOG.log(mp.SUBDEFAULT, "@papy;%s:%s produced:%s" % (script_name, script_id, out)) #LOG.log(mp.DEFAULT, "@papy;script %s:%s finished" % (script_name, script_id)) return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_create_update_tracking_related_event; 3, parameters; 3, 4; 4, identifier:instance; 5, block; 5, 6; 5, 8; 5, 12; 5, 13; 5, 104; 5, 105; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:events; 11, dictionary; 12, comment; 13, for_statement; 13, 14; 13, 17; 13, 24; 14, pattern_list; 14, 15; 14, 16; 15, identifier:field; 16, identifier:related_fields; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:instance; 21, identifier:_tracked_related_fields; 22, identifier:items; 23, argument_list; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 39; 26, not_operator; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 38; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:instance; 34, identifier:_meta; 35, identifier:get_field; 36, argument_list; 36, 37; 37, identifier:field; 38, identifier:ManyToManyField; 39, block; 39, 40; 39, 78; 40, if_statement; 40, 41; 40, 53; 40, 54; 40, 68; 41, call; 41, 42; 41, 43; 42, identifier:isinstance; 43, argument_list; 43, 44; 43, 52; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:instance; 48, identifier:_meta; 49, identifier:get_field; 50, argument_list; 50, 51; 51, identifier:field; 52, identifier:ForeignKey; 53, comment; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:value; 58, call; 58, 59; 58, 60; 59, identifier:getattr; 60, argument_list; 60, 61; 60, 62; 61, identifier:instance; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, string:'{0}_id'; 65, identifier:format; 66, argument_list; 66, 67; 67, identifier:field; 68, else_clause; 68, 69; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:value; 73, call; 73, 74; 73, 75; 74, identifier:getattr; 75, argument_list; 75, 76; 75, 77; 76, identifier:instance; 77, identifier:field; 78, if_statement; 78, 79; 78, 86; 79, comparison_operator:!=; 79, 80; 79, 85; 80, subscript; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:instance; 83, identifier:_original_fields; 84, identifier:field; 85, identifier:value; 86, block; 86, 87; 87, for_statement; 87, 88; 87, 89; 87, 90; 88, identifier:related_field; 89, identifier:related_fields; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 102; 93, attribute; 93, 94; 93, 101; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:events; 97, identifier:setdefault; 98, argument_list; 98, 99; 98, 100; 99, identifier:related_field; 100, list:[]; 101, identifier:append; 102, argument_list; 102, 103; 103, identifier:field; 104, comment; 105, for_statement; 105, 106; 105, 109; 105, 114; 106, pattern_list; 106, 107; 106, 108; 107, identifier:related_field; 108, identifier:fields; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:events; 112, identifier:items; 113, argument_list; 114, block; 114, 115; 114, 131; 114, 132; 114, 154; 115, try_statement; 115, 116; 115, 127; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:related_instances; 120, call; 120, 121; 120, 122; 121, identifier:getattr; 122, argument_list; 122, 123; 122, 124; 123, identifier:instance; 124, subscript; 124, 125; 124, 126; 125, identifier:related_field; 126, integer:1; 127, except_clause; 127, 128; 127, 129; 128, identifier:ObjectDoesNotExist; 129, block; 129, 130; 130, continue_statement; 131, comment; 132, if_statement; 132, 133; 132, 138; 132, 147; 133, call; 133, 134; 133, 135; 134, identifier:hasattr; 135, argument_list; 135, 136; 135, 137; 136, identifier:related_instances; 137, string:'all'; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:related_instances; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:related_instances; 145, identifier:all; 146, argument_list; 147, else_clause; 147, 148; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:related_instances; 152, list:[related_instances]; 152, 153; 153, identifier:related_instances; 154, for_statement; 154, 155; 154, 156; 154, 157; 155, identifier:related_instance; 156, identifier:related_instances; 157, block; 157, 158; 157, 166; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:event; 161, call; 161, 162; 161, 163; 162, identifier:_create_event; 163, argument_list; 163, 164; 163, 165; 164, identifier:related_instance; 165, identifier:UPDATE; 166, for_statement; 166, 167; 166, 168; 166, 169; 167, identifier:field; 168, identifier:fields; 169, block; 169, 170; 169, 182; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:fieldname; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, string:'{0}__{1}'; 176, identifier:format; 177, argument_list; 177, 178; 177, 181; 178, subscript; 178, 179; 178, 180; 179, identifier:related_field; 180, integer:0; 181, identifier:field; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:_create_tracked_field; 185, argument_list; 185, 186; 185, 187; 185, 188; 185, 189; 186, identifier:event; 187, identifier:instance; 188, identifier:field; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:fieldname; 191, identifier:fieldname
def _create_update_tracking_related_event(instance): """ Create a TrackingEvent and TrackedFieldModification for an UPDATE event for each related model. """ events = {} # Create a dict mapping related model field to modified fields for field, related_fields in instance._tracked_related_fields.items(): if not isinstance(instance._meta.get_field(field), ManyToManyField): if isinstance(instance._meta.get_field(field), ForeignKey): # Compare pk value = getattr(instance, '{0}_id'.format(field)) else: value = getattr(instance, field) if instance._original_fields[field] != value: for related_field in related_fields: events.setdefault(related_field, []).append(field) # Create the events from the events dict for related_field, fields in events.items(): try: related_instances = getattr(instance, related_field[1]) except ObjectDoesNotExist: continue # FIXME: isinstance(related_instances, RelatedManager ?) if hasattr(related_instances, 'all'): related_instances = related_instances.all() else: related_instances = [related_instances] for related_instance in related_instances: event = _create_event(related_instance, UPDATE) for field in fields: fieldname = '{0}__{1}'.format(related_field[0], field) _create_tracked_field( event, instance, field, fieldname=fieldname )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 21; 2, function_name:list; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filter; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:type; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:limit; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:page; 19, None; 20, comment; 21, block; 21, 22; 21, 24; 21, 38; 21, 56; 21, 76; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:schema; 27, call; 27, 28; 27, 29; 28, identifier:PackageSchema; 29, argument_list; 29, 30; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:exclude; 32, tuple; 32, 33; 32, 34; 32, 35; 32, 36; 32, 37; 33, string:'testlist'; 34, string:'extra_cli_args'; 35, string:'agent_id'; 36, string:'options'; 37, string:'note'; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:resp; 41, call; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:service; 46, identifier:list; 47, argument_list; 47, 48; 47, 51; 47, 52; 47, 53; 47, 54; 47, 55; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:base; 51, identifier:filter; 52, identifier:type; 53, identifier:sort; 54, identifier:limit; 55, identifier:page; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, pattern_list; 58, 59; 58, 60; 59, identifier:ps; 60, identifier:l; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:service; 66, identifier:decode; 67, argument_list; 67, 68; 67, 69; 67, 70; 67, 73; 68, identifier:schema; 69, identifier:resp; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:many; 72, True; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:links; 75, True; 76, return_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:Page; 79, argument_list; 79, 80; 79, 81; 80, identifier:ps; 81, identifier:l
def list(self, filter=None, type=None, sort=None, limit=None, page=None): # pylint: disable=redefined-builtin """Get a list of packages. :param filter: (optional) Filters to apply as a string list. :param type: (optional) `union` or `inter` as string. :param sort: (optional) Sort fields to apply as string list. :param limit: (optional) Limit returned list length. :param page: (optional) Page to return. :return: :class:`packages.Page <packages.Page>` object """ schema = PackageSchema(exclude=('testlist', 'extra_cli_args', 'agent_id', 'options', 'note')) resp = self.service.list(self.base, filter, type, sort, limit, page) ps, l = self.service.decode(schema, resp, many=True, links=True) return Page(ps, l)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:clean_lines; 3, parameters; 3, 4; 4, identifier:commands; 5, block; 5, 6; 5, 8; 5, 100; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 8, 15; 8, 85; 8, 93; 9, call; 9, 10; 9, 11; 10, identifier:isinstance; 11, argument_list; 11, 12; 11, 13; 12, identifier:commands; 13, identifier:basestring; 14, comment; 15, block; 15, 16; 16, if_statement; 16, 17; 16, 23; 16, 32; 16, 33; 16, 55; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:path; 20, identifier:isfile; 21, argument_list; 21, 22; 22, identifier:commands; 23, block; 23, 24; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:commands; 27, call; 27, 28; 27, 29; 28, identifier:open; 29, argument_list; 29, 30; 29, 31; 30, identifier:commands; 31, string:'rb'; 32, comment; 33, elif_clause; 33, 34; 33, 45; 34, comparison_operator:>; 34, 35; 34, 44; 35, call; 35, 36; 35, 37; 36, identifier:len; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:commands; 41, identifier:split; 42, argument_list; 42, 43; 43, string:','; 44, integer:1; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:commands; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:commands; 52, identifier:split; 53, argument_list; 53, 54; 54, string:','; 55, else_clause; 55, 56; 55, 57; 56, comment; 57, block; 57, 58; 58, try_statement; 58, 59; 58, 81; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 70; 61, comparison_operator:!=; 61, 62; 61, 69; 62, subscript; 62, 63; 62, 68; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:commands; 66, identifier:strip; 67, argument_list; 68, integer:0; 69, string:"#"; 70, block; 70, 71; 70, 80; 71, expression_statement; 71, 72; 72, yield; 72, 73; 73, binary_operator:+; 73, 74; 73, 79; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:commands; 77, identifier:strip; 78, argument_list; 79, string:'\n'; 80, return_statement; 81, except_clause; 81, 82; 81, 83; 82, identifier:IndexError; 83, block; 83, 84; 84, pass_statement; 85, elif_clause; 85, 86; 85, 91; 86, call; 86, 87; 86, 88; 87, identifier:isinstance; 88, argument_list; 88, 89; 88, 90; 89, identifier:commands; 90, identifier:list; 91, block; 91, 92; 92, pass_statement; 93, else_clause; 93, 94; 94, block; 94, 95; 95, raise_statement; 95, 96; 96, call; 96, 97; 96, 98; 97, identifier:TypeError; 98, argument_list; 98, 99; 99, string:'clean_lines() accepts a \'str\' or \'list\''; 100, for_statement; 100, 101; 100, 102; 100, 103; 100, 104; 101, identifier:cmd; 102, identifier:commands; 103, comment; 104, block; 104, 105; 105, try_statement; 105, 106; 105, 127; 106, block; 106, 107; 107, if_statement; 107, 108; 107, 117; 108, comparison_operator:!=; 108, 109; 108, 116; 109, subscript; 109, 110; 109, 115; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:cmd; 113, identifier:strip; 114, argument_list; 115, integer:0; 116, string:"#"; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, yield; 119, 120; 120, binary_operator:+; 120, 121; 120, 126; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:cmd; 124, identifier:strip; 125, argument_list; 126, string:'\n'; 127, except_clause; 127, 128; 127, 129; 128, identifier:IndexError; 129, block; 129, 130; 130, pass_statement
def clean_lines(commands): """ Generate strings that are not comments or lines with only whitespace. Purpose: This function is a generator that will read in either a | plain text file of strings(IP list, command list, etc), a | comma separated string of strings, or a list of strings. It | will crop out any comments or blank lines, and yield | individual strings. | | Only strings that do not start with a comment '#', or are not | entirely whitespace will be yielded. This allows a file with | comments and blank lines for formatting neatness to be used | without a problem. @param commands: This can be either a string that is a file | location, a comma separated string of strings | ('x,y,z,1,2,3'), or a python list of strings. @type commands: str or list @returns: Yields each command in order @rtype: iterable of str """ if isinstance(commands, basestring): # if the command argument is a filename, we need to open it. if path.isfile(commands): commands = open(commands, 'rb') # if the command string is a comma separated list, break it up. elif len(commands.split(',')) > 1: commands = commands.split(',') else: # if a single command, need to just be returned. try: if commands.strip()[0] != "#": yield commands.strip() + '\n' return except IndexError: pass elif isinstance(commands, list): pass else: raise TypeError('clean_lines() accepts a \'str\' or \'list\'') for cmd in commands: # exclude commented lines, and skip blank lines (index error) try: if cmd.strip()[0] != "#": yield cmd.strip() + '\n' except IndexError: pass
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:xpath; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:source_xml; 5, identifier:xpath_expr; 6, default_parameter; 6, 7; 6, 8; 7, identifier:req_format; 8, string:'string'; 9, block; 9, 10; 9, 12; 9, 16; 9, 35; 9, 36; 9, 37; 9, 85; 9, 86; 9, 96; 9, 105; 9, 106; 9, 113; 9, 133; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:tree; 15, identifier:source_xml; 16, if_statement; 16, 17; 16, 25; 17, not_operator; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:isinstance; 20, argument_list; 20, 21; 20, 22; 21, identifier:source_xml; 22, attribute; 22, 23; 22, 24; 23, identifier:ET; 24, identifier:Element; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:tree; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:objectify; 32, identifier:fromstring; 33, argument_list; 33, 34; 34, identifier:source_xml; 35, comment; 36, comment; 37, for_statement; 37, 38; 37, 39; 37, 44; 37, 45; 38, identifier:elem; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:tree; 42, identifier:getiterator; 43, argument_list; 44, comment; 45, block; 45, 46; 46, if_statement; 46, 47; 46, 54; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:elem; 52, identifier:tag; 53, identifier:basestring; 54, block; 54, 55; 54, 66; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:i; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:elem; 62, identifier:tag; 63, identifier:find; 64, argument_list; 64, 65; 65, string:'}'; 66, if_statement; 66, 67; 66, 70; 67, comparison_operator:>=; 67, 68; 67, 69; 68, identifier:i; 69, integer:0; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:elem; 75, identifier:tag; 76, subscript; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:elem; 79, identifier:tag; 80, slice; 80, 81; 80, 84; 81, binary_operator:+; 81, 82; 81, 83; 82, identifier:i; 83, integer:1; 84, colon; 85, comment; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:objectify; 90, identifier:deannotate; 91, argument_list; 91, 92; 91, 93; 92, identifier:tree; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:cleanup_namespaces; 95, True; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:filtered_list; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:tree; 102, identifier:xpath; 103, argument_list; 103, 104; 104, identifier:xpath_expr; 105, comment; 106, if_statement; 106, 107; 106, 110; 107, comparison_operator:==; 107, 108; 107, 109; 108, identifier:req_format; 109, string:'xml'; 110, block; 110, 111; 111, return_statement; 111, 112; 112, identifier:filtered_list; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:matches; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, string:''; 119, identifier:join; 120, generator_expression; 120, 121; 120, 130; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:etree; 124, identifier:tostring; 125, argument_list; 125, 126; 125, 127; 126, identifier:element; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:pretty_print; 129, True; 130, for_in_clause; 130, 131; 130, 132; 131, identifier:element; 132, identifier:filtered_list; 133, return_statement; 133, 134; 134, conditional_expression:if; 134, 135; 134, 136; 134, 137; 135, identifier:matches; 136, identifier:matches; 137, string:""
def xpath(source_xml, xpath_expr, req_format='string'): """ Filter xml based on an xpath expression. Purpose: This function applies an Xpath expression to the XML | supplied by source_xml. Returns a string subtree or | subtrees that match the Xpath expression. It can also return | an xml object if desired. @param source_xml: Plain text XML that will be filtered @type source_xml: str or lxml.etree.ElementTree.Element object @param xpath_expr: Xpath expression that we will filter the XML by. @type xpath_expr: str @param req_format: the desired format of the response, accepts string or | xml. @type req_format: str @returns: The filtered XML if filtering was successful. Otherwise, | an empty string. @rtype: str or ElementTree """ tree = source_xml if not isinstance(source_xml, ET.Element): tree = objectify.fromstring(source_xml) # clean up the namespace in the tags, as namespaces appear to confuse # xpath method for elem in tree.getiterator(): # beware of factory functions such as Comment if isinstance(elem.tag, basestring): i = elem.tag.find('}') if i >= 0: elem.tag = elem.tag[i+1:] # remove unused namespaces objectify.deannotate(tree, cleanup_namespaces=True) filtered_list = tree.xpath(xpath_expr) # Return string from the list of Elements or pure xml if req_format == 'xml': return filtered_list matches = ''.join(etree.tostring( element, pretty_print=True) for element in filtered_list) return matches if matches else ""
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:model_to_dict; 3, parameters; 3, 4; 3, 5; 4, identifier:instance; 5, dictionary_splat_pattern; 5, 6; 6, identifier:options; 7, block; 7, 8; 7, 10; 7, 17; 7, 21; 7, 53; 7, 54; 7, 55; 7, 256; 7, 257; 7, 272; 8, expression_statement; 8, 9; 9, string:"Takes a model instance and converts it into a dict."; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:options; 13, call; 13, 14; 13, 15; 14, identifier:_defaults; 15, argument_list; 15, 16; 16, identifier:options; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:attrs; 20, dictionary; 21, if_statement; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:options; 24, string:'prehook'; 25, block; 25, 26; 26, if_statement; 26, 27; 26, 36; 27, call; 27, 28; 27, 29; 28, identifier:isinstance; 29, argument_list; 29, 30; 29, 33; 30, subscript; 30, 31; 30, 32; 31, identifier:options; 32, string:'prehook'; 33, attribute; 33, 34; 33, 35; 34, identifier:collections; 35, identifier:Callable; 36, block; 36, 37; 36, 46; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:instance; 40, call; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:options; 43, string:'prehook'; 44, argument_list; 44, 45; 45, identifier:instance; 46, if_statement; 46, 47; 46, 50; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:instance; 49, None; 50, block; 50, 51; 51, return_statement; 51, 52; 52, identifier:attrs; 53, comment; 54, comment; 55, for_statement; 55, 56; 55, 57; 55, 60; 55, 61; 56, identifier:alias; 57, subscript; 57, 58; 57, 59; 58, identifier:options; 59, string:'fields'; 60, comment; 61, block; 61, 62; 61, 74; 61, 75; 61, 83; 61, 84; 61, 96; 61, 97; 61, 98; 61, 99; 61, 112; 61, 113; 61, 250; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:accessor; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:options; 69, string:'aliases'; 70, identifier:get; 71, argument_list; 71, 72; 71, 73; 72, identifier:alias; 73, identifier:alias; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:key; 78, binary_operator:+; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:options; 81, string:'prefix'; 82, identifier:alias; 83, comment; 84, if_statement; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:options; 87, string:'camelcase'; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:key; 92, call; 92, 93; 92, 94; 93, identifier:convert_to_camel; 94, argument_list; 94, 95; 95, identifier:key; 96, comment; 97, comment; 98, comment; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:value; 102, call; 102, 103; 102, 104; 103, identifier:get_field_value; 104, argument_list; 104, 105; 104, 106; 104, 107; 105, identifier:instance; 106, identifier:accessor; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:allow_missing; 109, subscript; 109, 110; 109, 111; 110, identifier:options; 111, string:'allow_missing'; 112, comment; 113, if_statement; 113, 114; 113, 123; 114, call; 114, 115; 114, 116; 115, identifier:isinstance; 116, argument_list; 116, 117; 116, 118; 117, identifier:value; 118, tuple; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:models; 121, identifier:Model; 122, identifier:QuerySet; 123, block; 123, 124; 123, 139; 123, 140; 123, 141; 123, 161; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:_options; 127, call; 127, 128; 127, 129; 128, identifier:_defaults; 129, argument_list; 129, 130; 130, call; 130, 131; 130, 136; 131, attribute; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:options; 134, string:'related'; 135, identifier:get; 136, argument_list; 136, 137; 136, 138; 137, identifier:accessor; 138, dictionary; 139, comment; 140, comment; 141, if_statement; 141, 142; 141, 147; 142, comparison_operator:in; 142, 143; 142, 144; 143, string:'%(accessor)s'; 144, subscript; 144, 145; 144, 146; 145, identifier:_options; 146, string:'prefix'; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:_options; 152, string:'prefix'; 153, binary_operator:%; 153, 154; 153, 157; 154, subscript; 154, 155; 154, 156; 155, identifier:_options; 156, string:'prefix'; 157, dictionary; 157, 158; 158, pair; 158, 159; 158, 160; 159, string:'accessor'; 160, identifier:alias; 161, if_statement; 161, 162; 161, 169; 161, 239; 162, call; 162, 163; 162, 164; 163, identifier:isinstance; 164, argument_list; 164, 165; 164, 166; 165, identifier:value; 166, attribute; 166, 167; 166, 168; 167, identifier:models; 168, identifier:Model; 169, block; 169, 170; 170, if_statement; 170, 171; 170, 189; 170, 208; 171, boolean_operator:and; 171, 172; 171, 184; 171, 185; 172, boolean_operator:and; 172, 173; 172, 181; 173, comparison_operator:==; 173, 174; 173, 180; 174, call; 174, 175; 174, 176; 175, identifier:len; 176, argument_list; 176, 177; 177, subscript; 177, 178; 177, 179; 178, identifier:_options; 179, string:'fields'; 180, integer:1; 181, subscript; 181, 182; 181, 183; 182, identifier:_options; 183, string:'flat'; 184, line_continuation:\; 185, not_operator; 185, 186; 186, subscript; 186, 187; 186, 188; 187, identifier:_options; 188, string:'merge'; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:value; 193, subscript; 193, 194; 193, 207; 194, call; 194, 195; 194, 196; 195, identifier:list; 196, argument_list; 196, 197; 197, call; 197, 198; 197, 206; 198, attribute; 198, 199; 198, 205; 199, call; 199, 200; 199, 201; 200, identifier:serialize; 201, argument_list; 201, 202; 201, 203; 202, identifier:value; 203, dictionary_splat; 203, 204; 204, identifier:_options; 205, identifier:values; 206, argument_list; 207, integer:0; 208, else_clause; 208, 209; 208, 210; 209, comment; 210, block; 210, 211; 210, 220; 210, 221; 210, 222; 210, 235; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:_attrs; 214, call; 214, 215; 214, 216; 215, identifier:serialize; 216, argument_list; 216, 217; 216, 218; 217, identifier:value; 218, dictionary_splat; 218, 219; 219, identifier:_options; 220, comment; 221, comment; 222, if_statement; 222, 223; 222, 226; 223, subscript; 223, 224; 223, 225; 224, identifier:_options; 225, string:'merge'; 226, block; 226, 227; 226, 234; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:attrs; 231, identifier:update; 232, argument_list; 232, 233; 233, identifier:_attrs; 234, continue_statement; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:value; 238, identifier:_attrs; 239, else_clause; 239, 240; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:value; 244, call; 244, 245; 244, 246; 245, identifier:serialize; 246, argument_list; 246, 247; 246, 248; 247, identifier:value; 248, dictionary_splat; 248, 249; 249, identifier:_options; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 255; 252, subscript; 252, 253; 252, 254; 253, identifier:attrs; 254, identifier:key; 255, identifier:value; 256, comment; 257, if_statement; 257, 258; 257, 261; 258, subscript; 258, 259; 258, 260; 259, identifier:options; 260, string:'posthook'; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:attrs; 265, call; 265, 266; 265, 269; 266, subscript; 266, 267; 266, 268; 267, identifier:options; 268, string:'posthook'; 269, argument_list; 269, 270; 269, 271; 270, identifier:instance; 271, identifier:attrs; 272, return_statement; 272, 273; 273, identifier:attrs
def model_to_dict(instance, **options): "Takes a model instance and converts it into a dict." options = _defaults(options) attrs = {} if options['prehook']: if isinstance(options['prehook'], collections.Callable): instance = options['prehook'](instance) if instance is None: return attrs # Items in the `fields` list are the output aliases, not the raw # accessors (field, method, property names) for alias in options['fields']: # Get the accessor for the object accessor = options['aliases'].get(alias, alias) # Create the key that will be used in the output dict key = options['prefix'] + alias # Optionally camelcase the key if options['camelcase']: key = convert_to_camel(key) # Get the field value. Use the mapped value to the actually property or # method name. `value` may be a number of things, so the various types # are checked below. value = get_field_value(instance, accessor, allow_missing=options['allow_missing']) # Related objects, perform some checks on their options if isinstance(value, (models.Model, QuerySet)): _options = _defaults(options['related'].get(accessor, {})) # If the `prefix` follows the below template, generate the # `prefix` for the related object if '%(accessor)s' in _options['prefix']: _options['prefix'] = _options['prefix'] % {'accessor': alias} if isinstance(value, models.Model): if len(_options['fields']) == 1 and _options['flat'] \ and not _options['merge']: value = list(serialize(value, **_options).values())[0] else: # Recurse, get the dict representation _attrs = serialize(value, **_options) # Check if this object should be merged into the parent, # otherwise nest it under the accessor name if _options['merge']: attrs.update(_attrs) continue value = _attrs else: value = serialize(value, **_options) attrs[key] = value # Apply post-hook to serialized attributes if options['posthook']: attrs = options['posthook'](instance, attrs) return attrs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 1, 22; 2, function_name:list; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:self; 5, identifier:id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:filter; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:type; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:sort; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:limit; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:page; 20, None; 21, comment; 22, block; 22, 23; 22, 25; 22, 35; 22, 56; 22, 76; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:schema; 28, call; 28, 29; 28, 30; 29, identifier:AttachmentSchema; 30, argument_list; 30, 31; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:exclude; 33, parenthesized_expression; 33, 34; 34, string:'path'; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:resp; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:service; 43, identifier:list; 44, argument_list; 44, 45; 44, 51; 44, 52; 44, 53; 44, 54; 44, 55; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:_base; 49, argument_list; 49, 50; 50, identifier:id; 51, identifier:filter; 52, identifier:type; 53, identifier:sort; 54, identifier:limit; 55, identifier:page; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, pattern_list; 58, 59; 58, 60; 59, identifier:at; 60, identifier:l; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:service; 66, identifier:decode; 67, argument_list; 67, 68; 67, 69; 67, 70; 67, 73; 68, identifier:schema; 69, identifier:resp; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:many; 72, True; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:links; 75, True; 76, return_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:Page; 79, argument_list; 79, 80; 79, 81; 80, identifier:at; 81, identifier:l
def list(self, id, filter=None, type=None, sort=None, limit=None, page=None): # pylint: disable=invalid-name,redefined-builtin """Get a list of a device's attachments. :param id: Device ID as an int. :param filter: (optional) Filters to apply as a string list. :param type: (optional) `union` or `inter` as string. :param sort: (optional) Sort fields to apply as string list. :param limit: (optional) Limit returned list length. :param page: (optional) Page to return. :return: :class:`attachments.Page <attachments.Page>` object """ schema = AttachmentSchema(exclude=('path')) resp = self.service.list(self._base(id), filter, type, sort, limit, page) at, l = self.service.decode(schema, resp, many=True, links=True) return Page(at, l)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:load_item; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:inbox; 5, default_parameter; 5, 6; 5, 7; 6, identifier:type; 7, string:"string"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:remove; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:buffer; 13, None; 14, block; 14, 15; 14, 17; 14, 27; 14, 33; 14, 48; 14, 108; 14, 124; 14, 138; 14, 139; 14, 143; 14, 264; 14, 265; 14, 437; 14, 438; 14, 479; 14, 480; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 23; 19, pattern_list; 19, 20; 19, 21; 19, 22; 20, identifier:is_file; 21, identifier:is_fifo; 22, identifier:is_socket; 23, expression_list; 23, 24; 23, 25; 23, 26; 24, False; 25, False; 26, False; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:file; 30, subscript; 30, 31; 30, 32; 31, identifier:inbox; 32, integer:0; 33, try_statement; 33, 34; 33, 41; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:file_type; 38, subscript; 38, 39; 38, 40; 39, identifier:file; 40, integer:0; 41, except_clause; 41, 42; 42, block; 42, 43; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:ValueError; 46, argument_list; 46, 47; 47, string:"invalid inbox item"; 48, if_statement; 48, 49; 48, 52; 48, 66; 48, 89; 48, 99; 49, comparison_operator:==; 49, 50; 49, 51; 50, identifier:file_type; 51, string:"file"; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:is_file; 56, call; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:os; 60, identifier:path; 61, identifier:exists; 62, argument_list; 62, 63; 63, subscript; 63, 64; 63, 65; 64, identifier:file; 65, integer:1; 66, elif_clause; 66, 67; 66, 70; 67, comparison_operator:==; 67, 68; 67, 69; 68, identifier:file_type; 69, string:"fifo"; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:is_fifo; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:stat; 77, identifier:S_ISFIFO; 78, argument_list; 78, 79; 79, attribute; 79, 80; 79, 88; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:os; 83, identifier:stat; 84, argument_list; 84, 85; 85, subscript; 85, 86; 85, 87; 86, identifier:file; 87, integer:1; 88, identifier:st_mode; 89, elif_clause; 89, 90; 89, 93; 89, 94; 90, comparison_operator:==; 90, 91; 90, 92; 91, identifier:file_type; 92, string:"socket"; 93, comment; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:is_socket; 98, True; 99, else_clause; 99, 100; 100, block; 100, 101; 101, raise_statement; 101, 102; 102, call; 102, 103; 102, 104; 103, identifier:ValueError; 104, argument_list; 104, 105; 105, binary_operator:%; 105, 106; 105, 107; 106, string:"type: %s not undertood"; 107, identifier:file_type; 108, if_statement; 108, 109; 108, 118; 109, boolean_operator:and; 109, 110; 109, 114; 110, parenthesized_expression; 110, 111; 111, boolean_operator:or; 111, 112; 111, 113; 112, identifier:is_fifo; 113, identifier:is_socket; 114, parenthesized_expression; 114, 115; 115, comparison_operator:==; 115, 116; 115, 117; 116, identifier:type; 117, string:'mmap'; 118, block; 118, 119; 119, raise_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:ValueError; 122, argument_list; 122, 123; 123, string:"mmap is not supported for FIFOs and sockets"; 124, if_statement; 124, 125; 124, 132; 125, boolean_operator:and; 125, 126; 125, 130; 126, parenthesized_expression; 126, 127; 127, boolean_operator:or; 127, 128; 127, 129; 128, identifier:is_fifo; 129, identifier:is_socket; 130, not_operator; 130, 131; 131, identifier:remove; 132, block; 132, 133; 133, raise_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:ValueError; 136, argument_list; 136, 137; 137, string:"FIFOs and sockets have to be removed"; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:start; 142, integer:0; 143, if_statement; 143, 144; 143, 147; 143, 186; 143, 251; 144, boolean_operator:or; 144, 145; 144, 146; 145, identifier:is_fifo; 146, identifier:is_file; 147, block; 147, 148; 147, 163; 147, 177; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:stop; 151, binary_operator:-; 151, 152; 151, 162; 152, attribute; 152, 153; 152, 161; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:os; 156, identifier:stat; 157, argument_list; 157, 158; 158, subscript; 158, 159; 158, 160; 159, identifier:file; 160, integer:1; 161, identifier:st_size; 162, integer:1; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:fd; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:os; 169, identifier:open; 170, argument_list; 170, 171; 170, 174; 171, subscript; 171, 172; 171, 173; 172, identifier:file; 173, integer:1; 174, attribute; 174, 175; 174, 176; 175, identifier:os; 176, identifier:O_RDONLY; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:BUFFER; 180, parenthesized_expression; 180, 181; 181, boolean_operator:or; 181, 182; 181, 183; 182, identifier:buffer; 183, subscript; 183, 184; 183, 185; 184, identifier:PAPY_DEFAULTS; 185, string:'PIPE_BUF'; 186, elif_clause; 186, 187; 186, 188; 187, identifier:is_socket; 188, block; 188, 189; 188, 206; 188, 220; 188, 229; 188, 234; 188, 242; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 194; 191, pattern_list; 191, 192; 191, 193; 192, identifier:host; 193, identifier:port; 194, expression_list; 194, 195; 194, 203; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:socket; 198, identifier:gethostbyname; 199, argument_list; 199, 200; 200, subscript; 200, 201; 200, 202; 201, identifier:file; 202, integer:1; 203, subscript; 203, 204; 203, 205; 204, identifier:file; 205, integer:2; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:sock; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:socket; 212, identifier:socket; 213, argument_list; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:socket; 216, identifier:AF_INET; 217, attribute; 217, 218; 217, 219; 218, identifier:socket; 219, identifier:SOCK_STREAM; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:sock; 224, identifier:connect; 225, argument_list; 225, 226; 226, tuple; 226, 227; 226, 228; 227, identifier:host; 228, identifier:port; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:stop; 232, unary_operator:-; 232, 233; 233, integer:1; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:fd; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:sock; 240, identifier:fileno; 241, argument_list; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 245; 244, identifier:BUFFER; 245, parenthesized_expression; 245, 246; 246, boolean_operator:or; 246, 247; 246, 248; 247, identifier:buffer; 248, subscript; 248, 249; 248, 250; 249, identifier:PAPY_DEFAULTS; 250, string:'TCP_RCVBUF'; 251, else_clause; 251, 252; 252, block; 252, 253; 253, raise_statement; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:ValueError; 256, argument_list; 256, 257; 257, binary_operator:%; 257, 258; 257, 259; 258, string:"got unknown inbox: %s"; 259, parenthesized_expression; 259, 260; 260, call; 260, 261; 260, 262; 261, identifier:repr; 262, argument_list; 262, 263; 263, identifier:inbox; 264, comment; 265, if_statement; 265, 266; 265, 269; 265, 352; 265, 428; 266, comparison_operator:==; 266, 267; 266, 268; 267, identifier:type; 268, string:'mmap'; 269, block; 269, 270; 269, 292; 269, 298; 269, 306; 269, 345; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:offset; 273, binary_operator:-; 273, 274; 273, 275; 274, identifier:start; 275, parenthesized_expression; 275, 276; 276, binary_operator:%; 276, 277; 276, 278; 277, identifier:start; 278, parenthesized_expression; 278, 279; 279, boolean_operator:or; 279, 280; 279, 286; 279, 287; 280, call; 280, 281; 280, 282; 281, identifier:getattr; 282, argument_list; 282, 283; 282, 284; 282, 285; 283, identifier:mmap; 284, string:'ALLOCATIONGRANULARITY'; 285, None; 286, line_continuation:\; 287, call; 287, 288; 287, 289; 288, identifier:getattr; 289, argument_list; 289, 290; 289, 291; 290, identifier:mmap; 291, string:'PAGESIZE'; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:start; 295, binary_operator:-; 295, 296; 295, 297; 296, identifier:start; 297, identifier:offset; 298, expression_statement; 298, 299; 299, assignment; 299, 300; 299, 301; 300, identifier:stop; 301, binary_operator:+; 301, 302; 301, 305; 302, binary_operator:-; 302, 303; 302, 304; 303, identifier:stop; 304, identifier:offset; 305, integer:1; 306, try_statement; 306, 307; 306, 326; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:data; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:mmap; 314, identifier:mmap; 315, argument_list; 315, 316; 315, 317; 315, 318; 315, 323; 316, identifier:fd; 317, identifier:stop; 318, keyword_argument; 318, 319; 318, 320; 319, identifier:access; 320, attribute; 320, 321; 320, 322; 321, identifier:mmap; 322, identifier:ACCESS_READ; 323, keyword_argument; 323, 324; 323, 325; 324, identifier:offset; 325, identifier:offset; 326, except_clause; 326, 327; 326, 328; 326, 329; 327, identifier:TypeError; 328, comment; 329, block; 329, 330; 330, expression_statement; 330, 331; 331, assignment; 331, 332; 331, 333; 332, identifier:data; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:mmap; 336, identifier:mmap; 337, argument_list; 337, 338; 337, 339; 337, 340; 338, identifier:fd; 339, identifier:stop; 340, keyword_argument; 340, 341; 340, 342; 341, identifier:access; 342, attribute; 342, 343; 342, 344; 343, identifier:mmap; 344, identifier:ACCESS_READ; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:data; 349, identifier:seek; 350, argument_list; 350, 351; 351, identifier:start; 352, elif_clause; 352, 353; 352, 356; 353, comparison_operator:==; 353, 354; 353, 355; 354, identifier:type; 355, string:'string'; 356, block; 356, 357; 356, 361; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 360; 359, identifier:data; 360, list:[]; 361, if_statement; 361, 362; 361, 366; 361, 403; 362, comparison_operator:==; 362, 363; 362, 364; 363, identifier:stop; 364, unary_operator:-; 364, 365; 365, integer:1; 366, block; 366, 367; 366, 392; 366, 401; 366, 402; 367, while_statement; 367, 368; 367, 369; 368, True; 369, block; 369, 370; 369, 380; 369, 385; 370, expression_statement; 370, 371; 371, assignment; 371, 372; 371, 373; 372, identifier:buffer_; 373, call; 373, 374; 373, 377; 374, attribute; 374, 375; 374, 376; 375, identifier:os; 376, identifier:read; 377, argument_list; 377, 378; 377, 379; 378, identifier:fd; 379, identifier:BUFFER; 380, if_statement; 380, 381; 380, 383; 381, not_operator; 381, 382; 382, identifier:buffer_; 383, block; 383, 384; 384, break_statement; 385, expression_statement; 385, 386; 386, call; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:data; 389, identifier:append; 390, argument_list; 390, 391; 391, identifier:buffer_; 392, expression_statement; 392, 393; 393, assignment; 393, 394; 393, 395; 394, identifier:data; 395, call; 395, 396; 395, 399; 396, attribute; 396, 397; 396, 398; 397, string:""; 398, identifier:join; 399, argument_list; 399, 400; 400, identifier:data; 401, comment; 402, comment; 403, else_clause; 403, 404; 404, block; 404, 405; 404, 414; 405, expression_statement; 405, 406; 406, call; 406, 407; 406, 410; 407, attribute; 407, 408; 407, 409; 408, identifier:os; 409, identifier:lseek; 410, argument_list; 410, 411; 410, 412; 410, 413; 411, identifier:fd; 412, identifier:start; 413, integer:0; 414, expression_statement; 414, 415; 415, assignment; 415, 416; 415, 417; 416, identifier:data; 417, call; 417, 418; 417, 421; 418, attribute; 418, 419; 418, 420; 419, identifier:os; 420, identifier:read; 421, argument_list; 421, 422; 421, 423; 422, identifier:fd; 423, binary_operator:+; 423, 424; 423, 427; 424, binary_operator:-; 424, 425; 424, 426; 425, identifier:stop; 426, identifier:start; 427, integer:1; 428, else_clause; 428, 429; 429, block; 429, 430; 430, raise_statement; 430, 431; 431, call; 431, 432; 431, 433; 432, identifier:ValueError; 433, argument_list; 433, 434; 434, binary_operator:%; 434, 435; 434, 436; 435, string:'type: %s not understood.'; 436, identifier:type; 437, comment; 438, if_statement; 438, 439; 438, 440; 438, 470; 439, identifier:remove; 440, block; 440, 441; 441, if_statement; 441, 442; 441, 443; 441, 444; 441, 451; 442, identifier:is_socket; 443, comment; 444, block; 444, 445; 445, expression_statement; 445, 446; 446, call; 446, 447; 446, 450; 447, attribute; 447, 448; 447, 449; 448, identifier:sock; 449, identifier:close; 450, argument_list; 451, else_clause; 451, 452; 451, 453; 452, comment; 453, block; 453, 454; 453, 461; 454, expression_statement; 454, 455; 455, call; 455, 456; 455, 459; 456, attribute; 456, 457; 456, 458; 457, identifier:os; 458, identifier:close; 459, argument_list; 459, 460; 460, identifier:fd; 461, expression_statement; 461, 462; 462, call; 462, 463; 462, 466; 463, attribute; 463, 464; 463, 465; 464, identifier:os; 465, identifier:unlink; 466, argument_list; 466, 467; 467, subscript; 467, 468; 467, 469; 468, identifier:file; 469, integer:1; 470, else_clause; 470, 471; 471, block; 471, 472; 472, expression_statement; 472, 473; 473, call; 473, 474; 473, 477; 474, attribute; 474, 475; 474, 476; 475, identifier:os; 476, identifier:close; 477, argument_list; 477, 478; 478, identifier:fd; 479, comment; 480, return_statement; 480, 481; 481, identifier:data
def load_item(inbox, type="string", remove=True, buffer=None): """ Loads data from a file. Determines the file type automatically ``"file"``, ``"fifo"``, ``"socket"``, but allows to specify the representation type ``"string"`` or ``"mmap"`` for memory mapped access to the file. Returns the loaded item as a ``str`` or ``mmap`` object. Internally creates an item from a ``file``. Arguments: - type(``"string"`` or ``"mmap"``) [default: ``"string"``] Determines the type of ``object`` the worker returns i.e. the ``file`` is read as a string or a memmory map. FIFOs cannot be memory mapped. - remove(``bool``) [default: ``True``] Should the file be removed from the filesystem? This is mandatory for FIFOs and sockets. Only Files can be used to store data persistantly. """ is_file, is_fifo, is_socket = False, False, False file = inbox[0] try: file_type = file[0] except: raise ValueError("invalid inbox item") if file_type == "file": is_file = os.path.exists(file[1]) elif file_type == "fifo": is_fifo = stat.S_ISFIFO(os.stat(file[1]).st_mode) elif file_type == "socket": # how to test is valid socket? is_socket = True else: raise ValueError("type: %s not undertood" % file_type) if (is_fifo or is_socket) and (type == 'mmap'): raise ValueError("mmap is not supported for FIFOs and sockets") if (is_fifo or is_socket) and not remove: raise ValueError("FIFOs and sockets have to be removed") # get a fd and start/stop start = 0 if is_fifo or is_file: stop = os.stat(file[1]).st_size - 1 fd = os.open(file[1], os.O_RDONLY) BUFFER = (buffer or PAPY_DEFAULTS['PIPE_BUF']) elif is_socket: host, port = socket.gethostbyname(file[1]), file[2] sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) stop = -1 fd = sock.fileno() BUFFER = (buffer or PAPY_DEFAULTS['TCP_RCVBUF']) else: raise ValueError("got unknown inbox: %s" % (repr(inbox))) # get the data if type == 'mmap': offset = start - (start % (getattr(mmap, 'ALLOCATIONGRANULARITY', None)\ or getattr(mmap, 'PAGESIZE'))) start = start - offset stop = stop - offset + 1 try: data = mmap.mmap(fd, stop, access=mmap.ACCESS_READ, offset=offset) except TypeError: # we're on Python 2.5 data = mmap.mmap(fd, stop, access=mmap.ACCESS_READ) data.seek(start) elif type == 'string': data = [] if stop == -1: while True: buffer_ = os.read(fd, BUFFER) if not buffer_: break data.append(buffer_) data = "".join(data) # data = sock.recv(socket.MSG_WAITALL) # this would read all the data from a socket else: os.lseek(fd, start, 0) data = os.read(fd, stop - start + 1) else: raise ValueError('type: %s not understood.' % type) # remove the file or close the socket if remove: if is_socket: # closes client socket sock.close() else: # pipes and files are just removed os.close(fd) os.unlink(file[1]) else: os.close(fd) # returns a string or mmap return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:write_validate; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:ctx; 5, identifier:param; 6, identifier:value; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 16; 10, 86; 11, comparison_operator:!=; 11, 12; 11, 13; 12, identifier:value; 13, tuple; 13, 14; 13, 15; 14, string:"default"; 15, string:"default"; 16, block; 16, 17; 16, 45; 16, 70; 16, 71; 16, 72; 17, try_statement; 17, 18; 17, 31; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 24; 21, pattern_list; 21, 22; 21, 23; 22, identifier:mode; 23, identifier:dest_file; 24, tuple; 24, 25; 24, 28; 25, subscript; 25, 26; 25, 27; 26, identifier:value; 27, integer:0; 28, subscript; 28, 29; 28, 30; 29, identifier:value; 30, integer:1; 31, except_clause; 31, 32; 31, 33; 32, identifier:IndexError; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:click; 38, identifier:BadParameter; 39, argument_list; 39, 40; 40, concatenated_string; 40, 41; 40, 42; 40, 43; 40, 44; 41, string:'Expecting two arguments, one for how to '; 42, string:'output (s, single, m, multiple), and '; 43, string:'the second is a filepath where to put'; 44, string:' the output.'; 45, if_statement; 45, 46; 45, 57; 46, comparison_operator:not; 46, 47; 46, 52; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:mode; 50, identifier:lower; 51, argument_list; 52, list:['s', 'single', 'm', 'multiple']; 52, 53; 52, 54; 52, 55; 52, 56; 53, string:'s'; 54, string:'single'; 55, string:'m'; 56, string:'multiple'; 57, block; 57, 58; 58, raise_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:click; 62, identifier:BadParameter; 63, argument_list; 63, 64; 64, concatenated_string; 64, 65; 64, 66; 64, 67; 64, 68; 64, 69; 65, string:'The first argument of the -w/--write '; 66, string:'option must specifies whether to write'; 67, string:' to one file per device, or all device'; 68, string:' output to a single file. Valid options'; 69, string:' are "s", "single", "m", and "multiple"'; 70, comment; 71, comment; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 79; 74, subscript; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:ctx; 77, identifier:obj; 78, string:'out'; 79, tuple; 79, 80; 79, 85; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:mode; 83, identifier:lower; 84, argument_list; 85, identifier:dest_file; 86, else_clause; 86, 87; 86, 88; 87, comment; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 96; 91, subscript; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:ctx; 94, identifier:obj; 95, string:'out'; 96, None
def write_validate(ctx, param, value): """ Validate the -w option. Purpose: Validates the `-w`|`--write` option. Two arguments are expected. | The first is the mode, which must be in ['s', 'single', 'm', | 'multiple']. The mode determins if we're writing to one file for | all device output, or to a separate file for each device being | handled. | | The second expected argument is the filepath of the desired | output file. This will automatically be prepended with the IP or | hostname of the device if we're writing to multiple files. @param ctx: The click context paramter, for receiving the object dictionary | being manipulated by other previous functions. Needed by any | function with the @click.pass_context decorator. Callback | functions such as this one receive this automatically. @type ctx: click.Context @param param: param is passed into a validation callback function by click. | We do not use it. @type param: None @param value: The value that the user supplied for the write option. @type value: str @returns: The value that the user supplied, if it passed validation. | Otherwise, raises click.BadParameter @rtype: str """ if value != ("default", "default"): try: mode, dest_file = (value[0], value[1]) except IndexError: raise click.BadParameter('Expecting two arguments, one for how to ' 'output (s, single, m, multiple), and ' 'the second is a filepath where to put' ' the output.') if mode.lower() not in ['s', 'single', 'm', 'multiple']: raise click.BadParameter('The first argument of the -w/--write ' 'option must specifies whether to write' ' to one file per device, or all device' ' output to a single file. Valid options' ' are "s", "single", "m", and "multiple"') # we've passed the checks, so set the 'out' context variable to our # tuple of the mode, and the destination file. ctx.obj['out'] = (mode.lower(), dest_file) else: # they didn't use -w, so set the context variable accordingly. ctx.obj['out'] = None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:write_out; 3, parameters; 3, 4; 4, identifier:input; 5, block; 5, 6; 5, 8; 5, 9; 5, 15; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 14; 11, pattern_list; 11, 12; 11, 13; 12, identifier:to_file; 13, identifier:output; 14, identifier:input; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:!=; 16, 17; 16, 18; 17, identifier:to_file; 18, string:"quiet"; 19, block; 19, 20; 20, try_statement; 20, 21; 20, 22; 20, 29; 20, 41; 21, comment; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, pattern_list; 25, 26; 25, 27; 26, identifier:mode; 27, identifier:dest_file; 28, identifier:to_file; 29, except_clause; 29, 30; 29, 31; 29, 32; 29, 33; 30, identifier:TypeError; 31, comment; 32, comment; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:click; 38, identifier:echo; 39, argument_list; 39, 40; 40, identifier:output; 41, else_clause; 41, 42; 42, block; 42, 43; 42, 65; 42, 102; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:ip; 46, call; 46, 47; 46, 64; 47, attribute; 47, 48; 47, 63; 48, subscript; 48, 49; 48, 62; 49, call; 49, 50; 49, 60; 50, attribute; 50, 51; 50, 59; 51, subscript; 51, 52; 51, 58; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:output; 55, identifier:split; 56, argument_list; 56, 57; 57, string:'device: '; 58, integer:1; 59, identifier:split; 60, argument_list; 60, 61; 61, string:'\n'; 62, integer:0; 63, identifier:strip; 64, argument_list; 65, if_statement; 65, 66; 65, 71; 65, 72; 65, 73; 66, comparison_operator:in; 66, 67; 66, 68; 67, identifier:mode; 68, list:['m', 'multiple']; 68, 69; 68, 70; 69, string:'m'; 70, string:'multiple'; 71, comment; 72, comment; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:dest_file; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:path; 80, identifier:join; 81, argument_list; 81, 82; 81, 90; 82, subscript; 82, 83; 82, 89; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:path; 86, identifier:split; 87, argument_list; 87, 88; 88, identifier:dest_file; 89, integer:0; 90, binary_operator:+; 90, 91; 90, 94; 91, binary_operator:+; 91, 92; 91, 93; 92, identifier:ip; 93, string:"_"; 94, subscript; 94, 95; 94, 101; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:path; 98, identifier:split; 99, argument_list; 99, 100; 100, identifier:dest_file; 101, integer:1; 102, try_statement; 102, 103; 102, 112; 102, 147; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:out_file; 107, call; 107, 108; 107, 109; 108, identifier:open; 109, argument_list; 109, 110; 109, 111; 110, identifier:dest_file; 111, string:'a+b'; 112, except_clause; 112, 113; 112, 117; 113, as_pattern; 113, 114; 113, 115; 114, identifier:IOError; 115, as_pattern_target; 115, 116; 116, identifier:e; 117, block; 117, 118; 117, 133; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:print; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:color; 124, argument_list; 124, 125; 124, 132; 125, binary_operator:%; 125, 126; 125, 129; 126, concatenated_string; 126, 127; 126, 128; 127, string:"Could not open output file '%s' for writing. "; 128, string:"Output would have been:\n%s"; 129, tuple; 129, 130; 129, 131; 130, identifier:dest_file; 131, identifier:output; 132, string:'red'; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:print; 136, argument_list; 136, 137; 137, call; 137, 138; 137, 139; 138, identifier:color; 139, argument_list; 139, 140; 139, 146; 140, binary_operator:+; 140, 141; 140, 142; 141, string:'Here is the error for opening the output file:'; 142, call; 142, 143; 142, 144; 143, identifier:str; 144, argument_list; 144, 145; 145, identifier:e; 146, string:'red'; 147, else_clause; 147, 148; 148, block; 148, 149; 148, 162; 148, 174; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:click; 153, identifier:echo; 154, argument_list; 154, 155; 154, 156; 154, 159; 155, identifier:output; 156, keyword_argument; 156, 157; 156, 158; 157, identifier:nl; 158, False; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:file; 161, identifier:out_file; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:print; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:color; 168, argument_list; 168, 169; 169, binary_operator:%; 169, 170; 169, 171; 170, string:'%s output appended to: %s'; 171, tuple; 171, 172; 171, 173; 172, identifier:ip; 173, identifier:dest_file; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:out_file; 178, identifier:close; 179, argument_list
def write_out(input): """ Callback function to write the output from the script. @param input: A tuple containing two things: | 1. None or Tuple of file mode and destination filepath | 2. The output of the jaide command that will be either | written to sys.stdout or to a file, depending on the | first index in the tuple. | | If the first index of the tuple *is not* another tuple, | the output will be written to sys.stdout. If the first | index *is* a tuple, that tuple is further broken down | into the mode ('single' for single file or 'multiple' | for one file for each IP), and the destination filepath. @type input: tuple @returns: None """ # peel off the to_file metadata from the output. to_file, output = input if to_file != "quiet": try: # split the to_file metadata into it's separate parts. mode, dest_file = to_file except TypeError: # just dump the output if we had an internal problem with getting # the metadata. click.echo(output) else: ip = output.split('device: ')[1].split('\n')[0].strip() if mode in ['m', 'multiple']: # put the IP in front of the filename if we're writing each # device to its own file. dest_file = path.join(path.split(dest_file)[0], ip + "_" + path.split(dest_file)[1]) try: out_file = open(dest_file, 'a+b') except IOError as e: print(color("Could not open output file '%s' for writing. " "Output would have been:\n%s" % (dest_file, output), 'red')) print(color('Here is the error for opening the output file:' + str(e), 'red')) else: click.echo(output, nl=False, file=out_file) print(color('%s output appended to: %s' % (ip, dest_file))) out_file.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:endpoint; 3, parameters; 3, 4; 4, identifier:request; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 11; 5, 20; 5, 21; 5, 62; 5, 63; 5, 92; 5, 123; 5, 124; 5, 125; 5, 149; 5, 150; 5, 173; 5, 174; 5, 175; 5, 176; 5, 177; 5, 188; 5, 197; 5, 222; 5, 223; 5, 250; 5, 251; 5, 268; 5, 269; 5, 315; 5, 345; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, comment; 11, if_statement; 11, 12; 11, 17; 12, comparison_operator:!=; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:request; 15, identifier:method; 16, string:'POST'; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, identifier:Http404; 20, comment; 21, if_statement; 21, 22; 21, 27; 21, 28; 22, call; 22, 23; 22, 24; 23, identifier:hasattr; 24, argument_list; 24, 25; 24, 26; 25, identifier:settings; 26, string:'BOUNCY_TOPIC_ARN'; 27, comment; 28, block; 28, 29; 28, 41; 28, 42; 28, 43; 28, 44; 29, if_statement; 29, 30; 29, 35; 30, comparison_operator:not; 30, 31; 30, 32; 31, string:'HTTP_X_AMZ_SNS_TOPIC_ARN'; 32, attribute; 32, 33; 32, 34; 33, identifier:request; 34, identifier:META; 35, block; 35, 36; 36, return_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:HttpResponseBadRequest; 39, argument_list; 39, 40; 40, string:'No TopicArn Header'; 41, comment; 42, comment; 43, comment; 44, if_statement; 44, 45; 44, 56; 45, parenthesized_expression; 45, 46; 46, not_operator; 46, 47; 47, comparison_operator:in; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:request; 51, identifier:META; 52, string:'HTTP_X_AMZ_SNS_TOPIC_ARN'; 53, attribute; 53, 54; 53, 55; 54, identifier:settings; 55, identifier:BOUNCY_TOPIC_ARN; 56, block; 56, 57; 57, return_statement; 57, 58; 58, call; 58, 59; 58, 60; 59, identifier:HttpResponseBadRequest; 60, argument_list; 60, 61; 61, string:'Bad Topic'; 62, comment; 63, if_statement; 63, 64; 63, 71; 63, 72; 63, 79; 64, call; 64, 65; 64, 66; 65, identifier:isinstance; 66, argument_list; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:request; 69, identifier:body; 70, identifier:str; 71, comment; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:request_body; 76, attribute; 76, 77; 76, 78; 77, identifier:request; 78, identifier:body; 79, else_clause; 79, 80; 79, 81; 80, comment; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:request_body; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:request; 89, identifier:body; 90, identifier:decode; 91, argument_list; 92, try_statement; 92, 93; 92, 103; 93, block; 93, 94; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:data; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:json; 100, identifier:loads; 101, argument_list; 101, 102; 102, identifier:request_body; 103, except_clause; 103, 104; 103, 105; 104, identifier:ValueError; 105, block; 105, 106; 105, 118; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:logger; 110, identifier:warning; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, string:'Notification Not Valid JSON: {}'; 115, identifier:format; 116, argument_list; 116, 117; 117, identifier:request_body; 118, return_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:HttpResponseBadRequest; 121, argument_list; 121, 122; 122, string:'Not Valid JSON'; 123, comment; 124, comment; 125, if_statement; 125, 126; 125, 136; 126, not_operator; 126, 127; 127, comparison_operator:<=; 127, 128; 127, 132; 128, call; 128, 129; 128, 130; 129, identifier:set; 130, argument_list; 130, 131; 131, identifier:VITAL_NOTIFICATION_FIELDS; 132, call; 132, 133; 132, 134; 133, identifier:set; 134, argument_list; 134, 135; 135, identifier:data; 136, block; 136, 137; 136, 144; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:logger; 141, identifier:warning; 142, argument_list; 142, 143; 143, string:'Request Missing Necessary Keys'; 144, return_statement; 144, 145; 145, call; 145, 146; 145, 147; 146, identifier:HttpResponseBadRequest; 147, argument_list; 147, 148; 148, string:'Request Missing Necessary Keys'; 149, comment; 150, if_statement; 150, 151; 150, 157; 151, not_operator; 151, 152; 152, comparison_operator:in; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:data; 155, string:'Type'; 156, identifier:ALLOWED_TYPES; 157, block; 157, 158; 157, 168; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:logger; 162, identifier:info; 163, argument_list; 163, 164; 163, 165; 164, string:'Notification Type Not Known %s'; 165, subscript; 165, 166; 165, 167; 166, identifier:data; 167, string:'Type'; 168, return_statement; 168, 169; 169, call; 169, 170; 169, 171; 170, identifier:HttpResponseBadRequest; 171, argument_list; 171, 172; 172, string:'Unknown Notification Type'; 173, comment; 174, comment; 175, comment; 176, comment; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:domain; 180, attribute; 180, 181; 180, 187; 181, call; 181, 182; 181, 183; 182, identifier:urlparse; 183, argument_list; 183, 184; 184, subscript; 184, 185; 184, 186; 185, identifier:data; 186, string:'SigningCertURL'; 187, identifier:netloc; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:pattern; 191, call; 191, 192; 191, 193; 192, identifier:getattr; 193, argument_list; 193, 194; 193, 195; 193, 196; 194, identifier:settings; 195, string:'BOUNCY_CERT_DOMAIN_REGEX'; 196, string:r"sns.[a-z0-9\-]+.amazonaws.com$"; 197, if_statement; 197, 198; 197, 206; 198, not_operator; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:re; 202, identifier:search; 203, argument_list; 203, 204; 203, 205; 204, identifier:pattern; 205, identifier:domain; 206, block; 206, 207; 206, 217; 207, expression_statement; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:logger; 211, identifier:warning; 212, argument_list; 212, 213; 212, 214; 213, string:'Improper Certificate Location %s'; 214, subscript; 214, 215; 214, 216; 215, identifier:data; 216, string:'SigningCertURL'; 217, return_statement; 217, 218; 218, call; 218, 219; 218, 220; 219, identifier:HttpResponseBadRequest; 220, argument_list; 220, 221; 221, string:'Improper Certificate Location'; 222, comment; 223, if_statement; 223, 224; 223, 237; 224, parenthesized_expression; 224, 225; 225, boolean_operator:and; 225, 226; 225, 232; 226, call; 226, 227; 226, 228; 227, identifier:getattr; 228, argument_list; 228, 229; 228, 230; 228, 231; 229, identifier:settings; 230, string:'BOUNCY_VERIFY_CERTIFICATE'; 231, True; 232, not_operator; 232, 233; 233, call; 233, 234; 233, 235; 234, identifier:verify_notification; 235, argument_list; 235, 236; 236, identifier:data; 237, block; 237, 238; 237, 245; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:logger; 242, identifier:error; 243, argument_list; 243, 244; 244, string:'Verification Failure %s'; 245, return_statement; 245, 246; 246, call; 246, 247; 246, 248; 247, identifier:HttpResponseBadRequest; 248, argument_list; 248, 249; 249, string:'Improper Signature'; 250, comment; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 258; 253, attribute; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:signals; 256, identifier:notification; 257, identifier:send; 258, argument_list; 258, 259; 258, 262; 258, 265; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:sender; 261, string:'bouncy_endpoint'; 262, keyword_argument; 262, 263; 262, 264; 263, identifier:notification; 264, identifier:data; 265, keyword_argument; 265, 266; 265, 267; 266, identifier:request; 267, identifier:request; 268, comment; 269, if_statement; 269, 270; 269, 275; 269, 276; 269, 293; 270, comparison_operator:==; 270, 271; 270, 274; 271, subscript; 271, 272; 271, 273; 272, identifier:data; 273, string:'Type'; 274, string:'SubscriptionConfirmation'; 275, comment; 276, block; 276, 277; 276, 288; 277, if_statement; 277, 278; 277, 285; 278, not_operator; 278, 279; 279, call; 279, 280; 279, 281; 280, identifier:getattr; 281, argument_list; 281, 282; 281, 283; 281, 284; 282, identifier:settings; 283, string:'BOUNCY_AUTO_SUBSCRIBE'; 284, True; 285, block; 285, 286; 286, raise_statement; 286, 287; 287, identifier:Http404; 288, return_statement; 288, 289; 289, call; 289, 290; 289, 291; 290, identifier:approve_subscription; 291, argument_list; 291, 292; 292, identifier:data; 293, elif_clause; 293, 294; 293, 299; 293, 300; 293, 301; 293, 302; 294, comparison_operator:==; 294, 295; 294, 298; 295, subscript; 295, 296; 295, 297; 296, identifier:data; 297, string:'Type'; 298, string:'UnsubscribeConfirmation'; 299, comment; 300, comment; 301, comment; 302, block; 302, 303; 302, 310; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:logger; 307, identifier:info; 308, argument_list; 308, 309; 309, string:'UnsubscribeConfirmation Not Handled'; 310, return_statement; 310, 311; 311, call; 311, 312; 311, 313; 312, identifier:HttpResponse; 313, argument_list; 313, 314; 314, string:'UnsubscribeConfirmation Not Handled'; 315, try_statement; 315, 316; 315, 328; 316, block; 316, 317; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:message; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:json; 323, identifier:loads; 324, argument_list; 324, 325; 325, subscript; 325, 326; 325, 327; 326, identifier:data; 327, string:'Message'; 328, except_clause; 328, 329; 328, 330; 328, 331; 328, 332; 329, identifier:ValueError; 330, comment; 331, comment; 332, block; 332, 333; 332, 340; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:logger; 337, identifier:info; 338, argument_list; 338, 339; 339, string:'Non-Valid JSON Message Received'; 340, return_statement; 340, 341; 341, call; 341, 342; 341, 343; 342, identifier:HttpResponse; 343, argument_list; 343, 344; 344, string:'Message is not valid JSON'; 345, return_statement; 345, 346; 346, call; 346, 347; 346, 348; 347, identifier:process_message; 348, argument_list; 348, 349; 348, 350; 349, identifier:message; 350, identifier:data
def endpoint(request): """Endpoint that SNS accesses. Includes logic verifying request""" # pylint: disable=too-many-return-statements,too-many-branches # In order to 'hide' the endpoint, all non-POST requests should return # the site's default HTTP404 if request.method != 'POST': raise Http404 # If necessary, check that the topic is correct if hasattr(settings, 'BOUNCY_TOPIC_ARN'): # Confirm that the proper topic header was sent if 'HTTP_X_AMZ_SNS_TOPIC_ARN' not in request.META: return HttpResponseBadRequest('No TopicArn Header') # Check to see if the topic is in the settings # Because you can have bounces and complaints coming from multiple # topics, BOUNCY_TOPIC_ARN is a list if (not request.META['HTTP_X_AMZ_SNS_TOPIC_ARN'] in settings.BOUNCY_TOPIC_ARN): return HttpResponseBadRequest('Bad Topic') # Load the JSON POST Body if isinstance(request.body, str): # requests return str in python 2.7 request_body = request.body else: # and return bytes in python 3.4 request_body = request.body.decode() try: data = json.loads(request_body) except ValueError: logger.warning('Notification Not Valid JSON: {}'.format(request_body)) return HttpResponseBadRequest('Not Valid JSON') # Ensure that the JSON we're provided contains all the keys we expect # Comparison code from http://stackoverflow.com/questions/1285911/ if not set(VITAL_NOTIFICATION_FIELDS) <= set(data): logger.warning('Request Missing Necessary Keys') return HttpResponseBadRequest('Request Missing Necessary Keys') # Ensure that the type of notification is one we'll accept if not data['Type'] in ALLOWED_TYPES: logger.info('Notification Type Not Known %s', data['Type']) return HttpResponseBadRequest('Unknown Notification Type') # Confirm that the signing certificate is hosted on a correct domain # AWS by default uses sns.{region}.amazonaws.com # On the off chance you need this to be a different domain, allow the # regex to be overridden in settings domain = urlparse(data['SigningCertURL']).netloc pattern = getattr( settings, 'BOUNCY_CERT_DOMAIN_REGEX', r"sns.[a-z0-9\-]+.amazonaws.com$" ) if not re.search(pattern, domain): logger.warning( 'Improper Certificate Location %s', data['SigningCertURL']) return HttpResponseBadRequest('Improper Certificate Location') # Verify that the notification is signed by Amazon if (getattr(settings, 'BOUNCY_VERIFY_CERTIFICATE', True) and not verify_notification(data)): logger.error('Verification Failure %s', ) return HttpResponseBadRequest('Improper Signature') # Send a signal to say a valid notification has been received signals.notification.send( sender='bouncy_endpoint', notification=data, request=request) # Handle subscription-based messages. if data['Type'] == 'SubscriptionConfirmation': # Allow the disabling of the auto-subscription feature if not getattr(settings, 'BOUNCY_AUTO_SUBSCRIBE', True): raise Http404 return approve_subscription(data) elif data['Type'] == 'UnsubscribeConfirmation': # We won't handle unsubscribe requests here. Return a 200 status code # so Amazon won't redeliver the request. If you want to remove this # endpoint, remove it either via the API or the AWS Console logger.info('UnsubscribeConfirmation Not Handled') return HttpResponse('UnsubscribeConfirmation Not Handled') try: message = json.loads(data['Message']) except ValueError: # This message is not JSON. But we need to return a 200 status code # so that Amazon doesn't attempt to deliver the message again logger.info('Non-Valid JSON Message Received') return HttpResponse('Message is not valid JSON') return process_message(message, data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_process_value; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:value; 6, identifier:type; 7, block; 7, 8; 7, 10; 7, 32; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 22; 11, not_operator; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:isinstance; 14, argument_list; 14, 15; 14, 16; 15, identifier:value; 16, binary_operator:+; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:six; 19, identifier:string_types; 20, tuple; 20, 21; 21, identifier:list; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:value; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:json; 29, identifier:dumps; 30, argument_list; 30, 31; 31, identifier:value; 32, return_statement; 32, 33; 33, identifier:value
def _process_value(self, value, type): """ Process a value that will be sent to backend :param value: the value to return :param type: hint for what sort of value this is :type type: str """ if not isinstance(value, six.string_types + (list,)): value = json.dumps(value) return value
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:representatives; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:count; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sorting; 10, False; 11, block; 11, 12; 11, 14; 11, 18; 11, 35; 11, 50; 11, 60; 11, 71; 11, 90; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:payload; 17, dictionary; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:count; 21, None; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 28; 25, subscript; 25, 26; 25, 27; 26, identifier:payload; 27, string:'count'; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_process_value; 32, argument_list; 32, 33; 32, 34; 33, identifier:count; 34, string:'int'; 35, if_statement; 35, 36; 35, 37; 36, identifier:sorting; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:payload; 42, string:'sorting'; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:_process_value; 47, argument_list; 47, 48; 47, 49; 48, identifier:sorting; 49, string:'strbool'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:resp; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:call; 57, argument_list; 57, 58; 57, 59; 58, string:'representatives'; 59, identifier:payload; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:representatives; 63, boolean_operator:or; 63, 64; 63, 70; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:resp; 67, identifier:get; 68, argument_list; 68, 69; 69, string:'representatives'; 70, dictionary; 71, for_statement; 71, 72; 71, 75; 71, 80; 72, pattern_list; 72, 73; 72, 74; 73, identifier:k; 74, identifier:v; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:representatives; 78, identifier:items; 79, argument_list; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:representatives; 85, identifier:k; 86, call; 86, 87; 86, 88; 87, identifier:int; 88, argument_list; 88, 89; 89, identifier:v; 90, return_statement; 90, 91; 91, identifier:representatives
def representatives(self, count=None, sorting=False): """ Returns a list of pairs of representative and its voting weight :param count: Max amount of representatives to return :type count: int :param sorting: If true, sorts by weight :type sorting: bool :raises: :py:exc:`nano.rpc.RPCException` >>> rpc.representatives() { "xrb_1111111111111111111111111111111111111111111111111117353trpda": 3822372327060170000000000000000000000, "xrb_1111111111111111111111111111111111111111111111111awsq94gtecn": 30999999999999999999999999000000, "xrb_114nk4rwjctu6n6tr6g6ps61g1w3hdpjxfas4xj1tq6i8jyomc5d858xr1xi": 0 } """ payload = {} if count is not None: payload['count'] = self._process_value(count, 'int') if sorting: payload['sorting'] = self._process_value(sorting, 'strbool') resp = self.call('representatives', payload) representatives = resp.get('representatives') or {} for k, v in representatives.items(): representatives[k] = int(v) return representatives
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:iterative_overlap_assembly; 3, parameters; 3, 4; 3, 5; 4, identifier:variant_sequences; 5, default_parameter; 5, 6; 5, 7; 6, identifier:min_overlap_size; 7, identifier:MIN_VARIANT_SEQUENCE_ASSEMBLY_OVERLAP_SIZE; 8, block; 8, 9; 8, 11; 8, 23; 8, 24; 8, 25; 8, 32; 8, 39; 8, 46; 8, 55; 8, 63; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 18; 11, 19; 11, 20; 12, comparison_operator:<=; 12, 13; 12, 17; 13, call; 13, 14; 13, 15; 14, identifier:len; 15, argument_list; 15, 16; 16, identifier:variant_sequences; 17, integer:1; 18, comment; 19, comment; 20, block; 20, 21; 21, return_statement; 21, 22; 22, identifier:variant_sequences; 23, comment; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:n_before_collapse; 28, call; 28, 29; 28, 30; 29, identifier:len; 30, argument_list; 30, 31; 31, identifier:variant_sequences; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:variant_sequences; 35, call; 35, 36; 35, 37; 36, identifier:collapse_substrings; 37, argument_list; 37, 38; 38, identifier:variant_sequences; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:n_after_collapse; 42, call; 42, 43; 42, 44; 43, identifier:len; 44, argument_list; 44, 45; 45, identifier:variant_sequences; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:logger; 50, identifier:info; 51, argument_list; 51, 52; 51, 53; 51, 54; 52, string:"Collapsed %d -> %d sequences"; 53, identifier:n_before_collapse; 54, identifier:n_after_collapse; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:merged_variant_sequences; 58, call; 58, 59; 58, 60; 59, identifier:greedy_merge; 60, argument_list; 60, 61; 60, 62; 61, identifier:variant_sequences; 62, identifier:min_overlap_size; 63, return_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:list; 66, argument_list; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:sorted; 69, argument_list; 69, 70; 69, 71; 70, identifier:merged_variant_sequences; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:key; 73, lambda; 73, 74; 73, 76; 74, lambda_parameters; 74, 75; 75, identifier:seq; 76, unary_operator:-; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:len; 79, argument_list; 79, 80; 80, attribute; 80, 81; 80, 82; 81, identifier:seq; 82, identifier:reads
def iterative_overlap_assembly( variant_sequences, min_overlap_size=MIN_VARIANT_SEQUENCE_ASSEMBLY_OVERLAP_SIZE): """ Assembles longer sequences from reads centered on a variant by between merging all pairs of overlapping sequences and collapsing shorter sequences onto every longer sequence which contains them. Returns a list of variant sequences, sorted by decreasing read support. """ if len(variant_sequences) <= 1: # if we don't have at least two sequences to start with then # skip the whole mess below return variant_sequences # reduce the number of inputs to the merge algorithm by first collapsing # shorter sequences onto the longer sequences which contain them n_before_collapse = len(variant_sequences) variant_sequences = collapse_substrings(variant_sequences) n_after_collapse = len(variant_sequences) logger.info( "Collapsed %d -> %d sequences", n_before_collapse, n_after_collapse) merged_variant_sequences = greedy_merge(variant_sequences, min_overlap_size) return list(sorted( merged_variant_sequences, key=lambda seq: -len(seq.reads)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:ortho_basis; 3, parameters; 3, 4; 3, 5; 4, identifier:normal; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ref_vec; 7, None; 8, block; 8, 9; 8, 11; 8, 12; 8, 17; 8, 24; 8, 31; 8, 38; 8, 45; 8, 46; 8, 47; 8, 51; 8, 52; 8, 68; 8, 69; 8, 84; 8, 85; 8, 86; 8, 97; 8, 98; 8, 232; 8, 233; 8, 234; 8, 244; 8, 255; 8, 256; 8, 257; 8, 267; 8, 278; 8, 279; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, import_statement; 12, 13; 13, aliased_import; 13, 14; 13, 16; 14, dotted_name; 14, 15; 15, identifier:numpy; 16, identifier:np; 17, import_from_statement; 17, 18; 17, 20; 18, dotted_name; 18, 19; 19, identifier:scipy; 20, aliased_import; 20, 21; 20, 23; 21, dotted_name; 21, 22; 22, identifier:linalg; 23, identifier:spla; 24, import_from_statement; 24, 25; 24, 27; 25, dotted_name; 25, 26; 26, identifier:scipy; 27, aliased_import; 27, 28; 27, 30; 28, dotted_name; 28, 29; 29, identifier:random; 30, identifier:sprnd; 31, import_from_statement; 31, 32; 31, 36; 32, relative_import; 32, 33; 32, 34; 33, import_prefix; 34, dotted_name; 34, 35; 35, identifier:const; 36, dotted_name; 36, 37; 37, identifier:PRM; 38, import_from_statement; 38, 39; 38, 43; 39, relative_import; 39, 40; 39, 41; 40, import_prefix; 41, dotted_name; 41, 42; 42, identifier:error; 43, dotted_name; 43, 44; 44, identifier:VectorError; 45, comment; 46, comment; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:RAND_MAG; 50, float:0.25; 51, comment; 52, if_statement; 52, 53; 52, 62; 53, not_operator; 53, 54; 54, comparison_operator:==; 54, 55; 54, 61; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, attribute; 58, 59; 58, 60; 59, identifier:normal; 60, identifier:shape; 61, integer:1; 62, block; 62, 63; 63, raise_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:ValueError; 66, argument_list; 66, 67; 67, string:"'normal' is not a vector"; 68, comment; 69, if_statement; 69, 70; 69, 78; 70, not_operator; 70, 71; 71, comparison_operator:==; 71, 72; 71, 77; 72, subscript; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:normal; 75, identifier:shape; 76, integer:0; 77, integer:3; 78, block; 78, 79; 79, raise_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:ValueError; 82, argument_list; 82, 83; 83, string:"Length of 'normal' is not three"; 84, comment; 85, comment; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:nv; 89, binary_operator:/; 89, 90; 89, 91; 90, identifier:normal; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:spla; 94, identifier:norm; 95, argument_list; 95, 96; 96, identifier:normal; 97, comment; 98, if_statement; 98, 99; 98, 102; 98, 103; 98, 104; 98, 105; 98, 106; 98, 107; 98, 163; 99, comparison_operator:is; 99, 100; 99, 101; 100, identifier:ref_vec; 101, None; 102, comment; 103, comment; 104, comment; 105, comment; 106, comment; 107, block; 107, 108; 107, 112; 107, 141; 107, 142; 107, 143; 107, 144; 107, 152; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:rv; 111, identifier:nv; 112, while_statement; 112, 113; 112, 118; 113, call; 113, 114; 113, 115; 114, identifier:parallel_check; 115, argument_list; 115, 116; 115, 117; 116, identifier:nv; 117, identifier:rv; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:rv; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:np; 125, identifier:float64; 126, argument_list; 126, 127; 127, binary_operator:+; 127, 128; 127, 131; 128, binary_operator:-; 128, 129; 128, 130; 129, float:1.0; 130, identifier:RAND_MAG; 131, binary_operator:*; 131, 132; 131, 135; 132, binary_operator:*; 132, 133; 132, 134; 133, integer:2; 134, identifier:RAND_MAG; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:sprnd; 138, identifier:rand; 139, argument_list; 139, 140; 140, integer:3; 141, comment; 142, comment; 143, comment; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:rv; 147, call; 147, 148; 147, 149; 148, identifier:rej; 149, argument_list; 149, 150; 149, 151; 150, identifier:rv; 151, identifier:nv; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:rv; 155, binary_operator:/; 155, 156; 155, 157; 156, identifier:rv; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:spla; 160, identifier:norm; 161, argument_list; 161, 162; 162, identifier:rv; 163, else_clause; 163, 164; 163, 165; 164, comment; 165, block; 165, 166; 165, 182; 165, 183; 165, 198; 165, 199; 165, 200; 165, 211; 165, 212; 165, 230; 165, 231; 166, if_statement; 166, 167; 166, 176; 167, not_operator; 167, 168; 168, comparison_operator:==; 168, 169; 168, 175; 169, call; 169, 170; 169, 171; 170, identifier:len; 171, argument_list; 171, 172; 172, attribute; 172, 173; 172, 174; 173, identifier:ref_vec; 174, identifier:shape; 175, integer:1; 176, block; 176, 177; 177, raise_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:ValueError; 180, argument_list; 180, 181; 181, string:"ref_vec is not a vector"; 182, comment; 183, if_statement; 183, 184; 183, 192; 184, not_operator; 184, 185; 185, comparison_operator:==; 185, 186; 185, 191; 186, subscript; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:ref_vec; 189, identifier:shape; 190, integer:0; 191, integer:3; 192, block; 192, 193; 193, raise_statement; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:ValueError; 196, argument_list; 196, 197; 197, string:"ref_vec length is not three"; 198, comment; 199, comment; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:rv; 203, binary_operator:/; 203, 204; 203, 205; 204, identifier:ref_vec; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:spla; 208, identifier:norm; 209, argument_list; 209, 210; 210, identifier:ref_vec; 211, comment; 212, if_statement; 212, 213; 212, 218; 212, 219; 212, 220; 213, call; 213, 214; 213, 215; 214, identifier:parallel_check; 215, argument_list; 215, 216; 215, 217; 216, identifier:nv; 217, identifier:rv; 218, comment; 219, comment; 220, block; 220, 221; 221, raise_statement; 221, 222; 222, call; 222, 223; 222, 224; 223, identifier:VectorError; 224, argument_list; 224, 225; 224, 228; 224, 229; 225, attribute; 225, 226; 225, 227; 226, identifier:VectorError; 227, identifier:NONPRL; 228, string:"'normal' and 'ref_vec' are too nearly parallel."; 229, string:""; 230, comment; 231, comment; 232, comment; 233, comment; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:on2; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:np; 240, identifier:cross; 241, argument_list; 241, 242; 241, 243; 242, identifier:nv; 243, identifier:rv; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:on2; 247, binary_operator:/; 247, 248; 247, 249; 248, identifier:on2; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:spla; 252, identifier:norm; 253, argument_list; 253, 254; 254, identifier:on2; 255, comment; 256, comment; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:on1; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:np; 263, identifier:cross; 264, argument_list; 264, 265; 264, 266; 265, identifier:on2; 266, identifier:nv; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:on1; 270, binary_operator:/; 270, 271; 270, 272; 271, identifier:on1; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:spla; 275, identifier:norm; 276, argument_list; 276, 277; 277, identifier:on1; 278, comment; 279, return_statement; 279, 280; 280, expression_list; 280, 281; 280, 282; 281, identifier:on1; 282, identifier:on2
def ortho_basis(normal, ref_vec=None): """Generates an orthonormal basis in the plane perpendicular to `normal` The orthonormal basis generated spans the plane defined with `normal` as its normal vector. The handedness of `on1` and `on2` in the returned basis is such that: .. math:: \\mathsf{on1} \\times \\mathsf{on2} = {\\mathsf{normal} \\over \\left\\| \\mathsf{normal}\\right\\|} `normal` must be expressible as a one-dimensional |nparray| of length 3. Parameters ---------- normal length-3 |npfloat_| -- The orthonormal basis output will span the plane perpendicular to `normal`. ref_vec length-3 |npfloat_|, optional -- If specified, `on1` will be the normalized projection of `ref_vec` onto the plane perpendicular to `normal`. Default is |None|. Returns ------- on1 length-3 |npfloat_| -- First vector defining the orthonormal basis in the plane normal to `normal` on2 length-3 |npfloat_| -- Second vector defining the orthonormal basis in the plane normal to `normal` Raises ------ ~exceptions.ValueError If `normal` or `ref_vec` is not expressible as a 1-D vector with 3 elements ~opan.error.VectorError (typecode :attr:`~opan.error.VectorError.NONPRL`) If `ref_vec` is specified and it is insufficiently non- parallel with respect to `normal` """ # Imports for library functions import numpy as np from scipy import linalg as spla from scipy import random as sprnd from ..const import PRM from ..error import VectorError # Internal parameters # Magnitude of the perturbation from 'normal' in constructing a random rv RAND_MAG = 0.25 # Test 'normal' for shape and length if not len(normal.shape) == 1: raise ValueError("'normal' is not a vector") ## end if if not normal.shape[0] == 3: raise ValueError("Length of 'normal' is not three") ## end if # Normalize to concise variable 'nv' nv = normal / spla.norm(normal) # Test for specification of ref_vec in the function call if ref_vec is None: # ref_vec not specified. # # Generate reference vector by generation of a random perturbation # vector suitably non-parallel to norm_vec # Generate suitable randomizer, looping as needed rv = nv while parallel_check(nv, rv): rv = np.float64(1.0 - RAND_MAG + 2 * RAND_MAG * sprnd.rand(3)) ## do loop # Calculate rejection of perturbed vector on the normal, then # normalize rv = rej(rv, nv) rv = rv / spla.norm(rv) else: # ref_vec specified, go ahead and use. Start with validity check. if not len(ref_vec.shape) == 1: raise ValueError("ref_vec is not a vector") ## end if if not ref_vec.shape[0] == 3: raise ValueError("ref_vec length is not three") ## end if # Normalize ref_vec to 'rv' rv = ref_vec / spla.norm(ref_vec) # Check for collinearity of nv and rv; raise error if too close if parallel_check(nv, rv): # Essentially equal or opposite vectors, making them too nearly # parallel. raise VectorError(VectorError.NONPRL, "'normal' and 'ref_vec' are too nearly parallel.", "") ## end if # rv is ok to use from here ## end try # on2 is the unit vector parallel to nv x rv on2 = np.cross(nv, rv) on2 = on2 / spla.norm(on2) # on1 is on2 x nv (normalization should not be necessary here, but is # performed just in case) on1 = np.cross(on2, nv) on1 = on1 / spla.norm(on1) # Return the spanning vectors return on1, on2
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:orthonorm_check; 3, parameters; 3, 4; 3, 5; 3, 10; 4, identifier:a; 5, default_parameter; 5, 6; 5, 7; 6, identifier:tol; 7, attribute; 7, 8; 7, 9; 8, identifier:_DEF; 9, identifier:ORTHONORM_TOL; 10, default_parameter; 10, 11; 10, 12; 11, identifier:report; 12, False; 13, block; 13, 14; 13, 16; 13, 17; 13, 22; 13, 29; 13, 30; 13, 31; 13, 32; 13, 33; 13, 37; 13, 41; 13, 45; 13, 46; 13, 47; 13, 48; 13, 49; 13, 91; 13, 92; 13, 106; 13, 107; 13, 192; 13, 193; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, import_statement; 17, 18; 18, aliased_import; 18, 19; 18, 21; 19, dotted_name; 19, 20; 20, identifier:numpy; 21, identifier:np; 22, import_from_statement; 22, 23; 22, 27; 23, relative_import; 23, 24; 23, 25; 24, import_prefix; 25, dotted_name; 25, 26; 26, identifier:base; 27, dotted_name; 27, 28; 28, identifier:delta_fxn; 29, comment; 30, comment; 31, comment; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:orth; 36, True; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:n_fail; 40, list:[]; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:o_fail; 44, list:[]; 45, comment; 46, comment; 47, comment; 48, comment; 49, if_statement; 49, 50; 49, 58; 49, 75; 50, comparison_operator:==; 50, 51; 50, 57; 51, call; 51, 52; 51, 53; 52, identifier:len; 53, argument_list; 53, 54; 54, attribute; 54, 55; 54, 56; 55, identifier:a; 56, identifier:shape; 57, integer:1; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:a_mx; 62, attribute; 62, 63; 62, 74; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:np; 66, identifier:matrix; 67, argument_list; 67, 68; 67, 69; 68, identifier:a; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:dtype; 71, attribute; 71, 72; 71, 73; 72, identifier:np; 73, identifier:float_; 74, identifier:T; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:a_mx; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:np; 83, identifier:matrix; 84, argument_list; 84, 85; 84, 86; 85, identifier:a; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:dtype; 88, attribute; 88, 89; 88, 90; 89, identifier:np; 90, identifier:float_; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:a_split; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:np; 98, identifier:hsplit; 99, argument_list; 99, 100; 99, 101; 100, identifier:a_mx; 101, subscript; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:a_mx; 104, identifier:shape; 105, integer:1; 106, comment; 107, for_statement; 107, 108; 107, 109; 107, 117; 108, identifier:iter1; 109, call; 109, 110; 109, 111; 110, identifier:range; 111, argument_list; 111, 112; 112, subscript; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:a_mx; 115, identifier:shape; 116, integer:1; 117, block; 117, 118; 118, for_statement; 118, 119; 118, 120; 118, 129; 119, identifier:iter2; 120, call; 120, 121; 120, 122; 121, identifier:range; 122, argument_list; 122, 123; 122, 124; 123, identifier:iter1; 124, subscript; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:a_mx; 127, identifier:shape; 128, integer:1; 129, block; 129, 130; 130, if_statement; 130, 131; 130, 161; 131, not_operator; 131, 132; 132, comparison_operator:<=; 132, 133; 132, 160; 133, call; 133, 134; 133, 135; 134, identifier:abs; 135, argument_list; 135, 136; 136, binary_operator:-; 136, 137; 136, 150; 137, subscript; 137, 138; 137, 148; 137, 149; 138, parenthesized_expression; 138, 139; 139, binary_operator:*; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, subscript; 141, 142; 141, 143; 142, identifier:a_split; 143, identifier:iter1; 144, identifier:T; 145, subscript; 145, 146; 145, 147; 146, identifier:a_split; 147, identifier:iter2; 148, integer:0; 149, integer:0; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:np; 153, identifier:float_; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:delta_fxn; 157, argument_list; 157, 158; 157, 159; 158, identifier:iter1; 159, identifier:iter2; 160, identifier:tol; 161, block; 161, 162; 161, 166; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:orth; 165, False; 166, if_statement; 166, 167; 166, 168; 167, identifier:report; 168, block; 168, 169; 169, if_statement; 169, 170; 169, 173; 169, 181; 170, comparison_operator:==; 170, 171; 170, 172; 171, identifier:iter1; 172, identifier:iter2; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:n_fail; 178, identifier:append; 179, argument_list; 179, 180; 180, identifier:iter1; 181, else_clause; 181, 182; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:o_fail; 187, identifier:append; 188, argument_list; 188, 189; 189, tuple; 189, 190; 189, 191; 190, identifier:iter1; 191, identifier:iter2; 192, comment; 193, if_statement; 193, 194; 193, 195; 193, 201; 194, identifier:report; 195, block; 195, 196; 196, return_statement; 196, 197; 197, expression_list; 197, 198; 197, 199; 197, 200; 198, identifier:orth; 199, identifier:n_fail; 200, identifier:o_fail; 201, else_clause; 201, 202; 202, block; 202, 203; 203, return_statement; 203, 204; 204, expression_list; 204, 205; 204, 206; 204, 207; 205, identifier:orth; 206, None; 207, None
def orthonorm_check(a, tol=_DEF.ORTHONORM_TOL, report=False): """Checks orthonormality of the column vectors of a matrix. If a one-dimensional |nparray| is passed to `a`, it is treated as a single column vector, rather than a row matrix of length-one column vectors. The matrix `a` does not need to be square, though it must have at least as many rows as columns, since orthonormality is only possible in N-space with a set of no more than N vectors. (This condition is not directly checked.) Parameters ---------- a R x S |npfloat_| -- 2-D array of column vectors to be checked for orthonormality. tol |npfloat_|, optional -- Tolerance for deviation of dot products from one or zero. Default value is :data:`opan.const.DEF.ORTHONORM_TOL`. report |bool|, optional -- Whether to record and return vectors / vector pairs failing the orthonormality condition. Default is |False|. Returns ------- o |bool| -- Indicates whether column vectors of `a` are orthonormal to within tolerance `tol`. n_fail |list| of |int|, or |None| -- If `report` == |True|: A list of indices of column vectors failing the normality condition, or an empty list if all vectors are normalized. If `report` == |False|: |None| o_fail |list| of 2-tuples of |int|, or |None| -- If `report` == |True|: A list of 2-tuples of indices of column vectors failing the orthogonality condition, or an empty list if all vectors are orthogonal. If `report` == |False|: |None| """ # Imports import numpy as np from .base import delta_fxn #!TODO? orthonorm_check Must add traps to ensure a is a single array, # that it is 2D, that it's all real? To enforce the limits stated # in the docstring? # Initialize return variables orth = True n_fail = [] o_fail = [] # Coerce to float_ matrix. Must treat 1-D vector as column vector. # Should raise an exception for any objects with more than # two dimensions; real and all-numeric are still not yet checked, but # will probably be run-time caught if too bad an object is passed. if len(a.shape) == 1: a_mx = np.matrix(a, dtype=np.float_).T else: a_mx = np.matrix(a, dtype=np.float_) # Split matrix into separate vectors for convenient indexing. a_split = np.hsplit(a_mx, a_mx.shape[1]) # Loop over vectors and check orthonormality. for iter1 in range(a_mx.shape[1]): for iter2 in range(iter1,a_mx.shape[1]): if not abs((a_split[iter1].T * a_split[iter2])[0,0] - np.float_(delta_fxn(iter1, iter2))) <= tol: orth = False if report: if iter1 == iter2: n_fail.append(iter1) else: o_fail.append((iter1, iter2)) # Return results if report: return orth, n_fail, o_fail else: return orth, None, None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parallel_check; 3, parameters; 3, 4; 3, 5; 4, identifier:vec1; 5, identifier:vec2; 6, block; 6, 7; 6, 9; 6, 10; 6, 17; 6, 22; 6, 23; 6, 27; 6, 28; 6, 61; 6, 62; 6, 81; 6, 82; 6, 83; 6, 91; 6, 115; 6, 116; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, import_from_statement; 10, 11; 10, 15; 11, relative_import; 11, 12; 11, 13; 12, import_prefix; 13, dotted_name; 13, 14; 14, identifier:const; 15, dotted_name; 15, 16; 16, identifier:PRM; 17, import_statement; 17, 18; 18, aliased_import; 18, 19; 18, 21; 19, dotted_name; 19, 20; 20, identifier:numpy; 21, identifier:np; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:par; 26, False; 27, comment; 28, for_statement; 28, 29; 28, 32; 28, 38; 29, pattern_list; 29, 30; 29, 31; 30, identifier:n; 31, identifier:v; 32, call; 32, 33; 32, 34; 33, identifier:enumerate; 34, argument_list; 34, 35; 35, list:[vec1, vec2]; 35, 36; 35, 37; 36, identifier:vec1; 37, identifier:vec2; 38, block; 38, 39; 38, 60; 39, if_statement; 39, 40; 39, 49; 40, not_operator; 40, 41; 41, comparison_operator:==; 41, 42; 41, 48; 42, call; 42, 43; 42, 44; 43, identifier:len; 44, argument_list; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:v; 47, identifier:shape; 48, integer:1; 49, block; 49, 50; 50, raise_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:ValueError; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, string:"Bad shape for vector #{0}"; 57, identifier:format; 58, argument_list; 58, 59; 59, identifier:n; 60, comment; 61, comment; 62, if_statement; 62, 63; 62, 75; 63, not_operator; 63, 64; 64, comparison_operator:==; 64, 65; 64, 70; 65, subscript; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:vec1; 68, identifier:shape; 69, integer:0; 70, subscript; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:vec2; 73, identifier:shape; 74, integer:0; 75, block; 75, 76; 76, raise_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:ValueError; 79, argument_list; 79, 80; 80, string:"Vector length mismatch"; 81, comment; 82, comment; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:angle; 86, call; 86, 87; 86, 88; 87, identifier:vec_angle; 88, argument_list; 88, 89; 88, 90; 89, identifier:vec1; 90, identifier:vec2; 91, if_statement; 91, 92; 91, 110; 92, comparison_operator:<; 92, 93; 92, 107; 93, call; 93, 94; 93, 95; 94, identifier:min; 95, argument_list; 95, 96; 96, list:[abs(angle), abs(angle - 180.)]; 96, 97; 96, 101; 97, call; 97, 98; 97, 99; 98, identifier:abs; 99, argument_list; 99, 100; 100, identifier:angle; 101, call; 101, 102; 101, 103; 102, identifier:abs; 103, argument_list; 103, 104; 104, binary_operator:-; 104, 105; 104, 106; 105, identifier:angle; 106, float:180.; 107, attribute; 107, 108; 107, 109; 108, identifier:PRM; 109, identifier:NON_PARALLEL_TOL; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:par; 114, True; 115, comment; 116, return_statement; 116, 117; 117, identifier:par
def parallel_check(vec1, vec2): """Checks whether two vectors are parallel OR anti-parallel. Vectors must be of the same dimension. Parameters ---------- vec1 length-R |npfloat_| -- First vector to compare vec2 length-R |npfloat_| -- Second vector to compare Returns ------- par |bool| -- |True| if (anti-)parallel to within :data:`opan.const.PRM.NON_PARALLEL_TOL` degrees. |False| otherwise. """ # Imports from ..const import PRM import numpy as np # Initialize False par = False # Shape check for n,v in enumerate([vec1, vec2]): if not len(v.shape) == 1: raise ValueError("Bad shape for vector #{0}".format(n)) ## end if ## next v,n if not vec1.shape[0] == vec2.shape[0]: raise ValueError("Vector length mismatch") ## end if # Check for (anti-)parallel character and return angle = vec_angle(vec1, vec2) if min([abs(angle), abs(angle - 180.)]) < PRM.NON_PARALLEL_TOL: par = True ## end if return par
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:vec_angle; 3, parameters; 3, 4; 3, 5; 4, identifier:vec1; 5, identifier:vec2; 6, block; 6, 7; 6, 9; 6, 10; 6, 15; 6, 22; 6, 29; 6, 30; 6, 45; 6, 46; 6, 61; 6, 62; 6, 80; 6, 81; 6, 82; 6, 99; 6, 100; 6, 117; 6, 118; 6, 119; 6, 120; 6, 144; 6, 181; 6, 182; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, import_statement; 10, 11; 11, aliased_import; 11, 12; 11, 14; 12, dotted_name; 12, 13; 13, identifier:numpy; 14, identifier:np; 15, import_from_statement; 15, 16; 15, 18; 16, dotted_name; 16, 17; 17, identifier:scipy; 18, aliased_import; 18, 19; 18, 21; 19, dotted_name; 19, 20; 20, identifier:linalg; 21, identifier:spla; 22, import_from_statement; 22, 23; 22, 27; 23, relative_import; 23, 24; 23, 25; 24, import_prefix; 25, dotted_name; 25, 26; 26, identifier:const; 27, dotted_name; 27, 28; 28, identifier:PRM; 29, comment; 30, if_statement; 30, 31; 30, 39; 31, comparison_operator:!=; 31, 32; 31, 38; 32, call; 32, 33; 32, 34; 33, identifier:len; 34, argument_list; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:vec1; 37, identifier:shape; 38, integer:1; 39, block; 39, 40; 40, raise_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:ValueError; 43, argument_list; 43, 44; 44, string:"'vec1' is not a vector"; 45, comment; 46, if_statement; 46, 47; 46, 55; 47, comparison_operator:!=; 47, 48; 47, 54; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:vec2; 53, identifier:shape; 54, integer:1; 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:"'vec2' is not a vector"; 61, comment; 62, if_statement; 62, 63; 62, 74; 63, comparison_operator:!=; 63, 64; 63, 69; 64, subscript; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:vec1; 67, identifier:shape; 68, integer:0; 69, subscript; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:vec2; 72, identifier:shape; 73, integer:0; 74, block; 74, 75; 75, raise_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:ValueError; 78, argument_list; 78, 79; 79, string:"Vector lengths are not equal"; 80, comment; 81, comment; 82, if_statement; 82, 83; 82, 93; 83, comparison_operator:<; 83, 84; 83, 90; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:spla; 87, identifier:norm; 88, argument_list; 88, 89; 89, identifier:vec1; 90, attribute; 90, 91; 90, 92; 91, identifier:PRM; 92, identifier:ZERO_VEC_TOL; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:ValueError; 97, argument_list; 97, 98; 98, string:"'vec1' norm is too small"; 99, comment; 100, if_statement; 100, 101; 100, 111; 101, comparison_operator:<; 101, 102; 101, 108; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:spla; 105, identifier:norm; 106, argument_list; 106, 107; 107, identifier:vec2; 108, attribute; 108, 109; 108, 110; 109, identifier:PRM; 110, identifier:ZERO_VEC_TOL; 111, block; 111, 112; 112, raise_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:ValueError; 115, argument_list; 115, 116; 116, string:"'vec2' norm is too small"; 117, comment; 118, comment; 119, comment; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:dotp; 123, binary_operator:/; 123, 124; 123, 138; 124, binary_operator:/; 124, 125; 124, 132; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:np; 128, identifier:dot; 129, argument_list; 129, 130; 129, 131; 130, identifier:vec1; 131, identifier:vec2; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:spla; 135, identifier:norm; 136, argument_list; 136, 137; 137, identifier:vec1; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:spla; 141, identifier:norm; 142, argument_list; 142, 143; 143, identifier:vec2; 144, if_statement; 144, 145; 144, 148; 144, 154; 144, 165; 145, comparison_operator:>; 145, 146; 145, 147; 146, identifier:dotp; 147, integer:1; 148, block; 148, 149; 148, 153; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:angle; 152, float:0.; 153, comment; 154, elif_clause; 154, 155; 154, 159; 155, comparison_operator:<; 155, 156; 155, 157; 156, identifier:dotp; 157, unary_operator:-; 157, 158; 158, integer:1; 159, block; 159, 160; 159, 164; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:angle; 163, float:180.; 164, comment; 165, else_clause; 165, 166; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:angle; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:np; 173, identifier:degrees; 174, argument_list; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:np; 178, identifier:arccos; 179, argument_list; 179, 180; 180, identifier:dotp; 181, comment; 182, return_statement; 182, 183; 183, identifier:angle
def vec_angle(vec1, vec2): """ Angle between two R-dimensional vectors. Angle calculated as: .. math:: \\arccos\\left[ \\frac{\\mathsf{vec1}\cdot\\mathsf{vec2}} {\\left\\|\\mathsf{vec1}\\right\\| \\left\\|\\mathsf{vec2}\\right\\|} \\right] Parameters ---------- vec1 length-R |npfloat_| -- First vector vec2 length-R |npfloat_| -- Second vector Returns ------- angle |npfloat_| -- Angle between the two vectors in degrees """ # Imports import numpy as np from scipy import linalg as spla from ..const import PRM # Check shape and equal length if len(vec1.shape) != 1: raise ValueError("'vec1' is not a vector") ## end if if len(vec2.shape) != 1: raise ValueError("'vec2' is not a vector") ## end if if vec1.shape[0] != vec2.shape[0]: raise ValueError("Vector lengths are not equal") ## end if # Check magnitudes if spla.norm(vec1) < PRM.ZERO_VEC_TOL: raise ValueError("'vec1' norm is too small") ## end if if spla.norm(vec2) < PRM.ZERO_VEC_TOL: raise ValueError("'vec2' norm is too small") ## end if # Calculate the angle and return. Do in multiple steps to test for # possible >1 or <-1 values from numerical precision errors. dotp = np.dot(vec1, vec2) / spla.norm(vec1) / spla.norm(vec2) if dotp > 1: angle = 0. # pragma: no cover elif dotp < -1: angle = 180. # pragma: no cover else: angle = np.degrees(np.arccos(dotp)) ## end if return angle
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:ctr_mass; 3, parameters; 3, 4; 3, 5; 4, identifier:geom; 5, identifier:masses; 6, block; 6, 7; 6, 9; 6, 10; 6, 15; 6, 24; 6, 25; 6, 40; 6, 41; 6, 56; 6, 57; 6, 74; 6, 75; 6, 107; 6, 108; 6, 109; 6, 133; 6, 134; 6, 135; 6, 136; 6, 137; 6, 138; 6, 182; 6, 183; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, import_statement; 10, 11; 11, aliased_import; 11, 12; 11, 14; 12, dotted_name; 12, 13; 13, identifier:numpy; 14, identifier:np; 15, import_from_statement; 15, 16; 15, 20; 16, relative_import; 16, 17; 16, 18; 17, import_prefix; 18, dotted_name; 18, 19; 19, identifier:base; 20, aliased_import; 20, 21; 20, 23; 21, dotted_name; 21, 22; 22, identifier:safe_cast; 23, identifier:scast; 24, comment; 25, if_statement; 25, 26; 25, 34; 26, comparison_operator:!=; 26, 27; 26, 33; 27, call; 27, 28; 27, 29; 28, identifier:len; 29, argument_list; 29, 30; 30, attribute; 30, 31; 30, 32; 31, identifier:geom; 32, identifier:shape; 33, integer:1; 34, block; 34, 35; 35, raise_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:ValueError; 38, argument_list; 38, 39; 39, string:"Geometry is not a vector"; 40, comment; 41, if_statement; 41, 42; 41, 50; 42, comparison_operator:!=; 42, 43; 42, 49; 43, call; 43, 44; 43, 45; 44, identifier:len; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:masses; 48, identifier:shape; 49, integer:1; 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:"Masses cannot be parsed as a vector"; 56, comment; 57, if_statement; 57, 58; 57, 68; 58, not_operator; 58, 59; 59, comparison_operator:==; 59, 60; 59, 67; 60, binary_operator:%; 60, 61; 60, 66; 61, subscript; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:geom; 64, identifier:shape; 65, integer:0; 66, integer:3; 67, integer:0; 68, block; 68, 69; 69, raise_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:ValueError; 72, argument_list; 72, 73; 73, string:"Geometry is not length-3N"; 74, comment; 75, if_statement; 75, 76; 75, 101; 76, boolean_operator:and; 76, 77; 76, 90; 77, comparison_operator:!=; 77, 78; 77, 83; 78, subscript; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:geom; 81, identifier:shape; 82, integer:0; 83, binary_operator:*; 83, 84; 83, 85; 84, integer:3; 85, subscript; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:masses; 88, identifier:shape; 89, integer:0; 90, comparison_operator:!=; 90, 91; 90, 96; 91, subscript; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:geom; 94, identifier:shape; 95, integer:0; 96, subscript; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:masses; 99, identifier:shape; 100, integer:0; 101, block; 101, 102; 102, raise_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:ValueError; 105, argument_list; 105, 106; 106, string:"Inconsistent geometry and masses vector lengths"; 107, comment; 108, comment; 109, if_statement; 109, 110; 109, 123; 110, comparison_operator:==; 110, 111; 110, 116; 111, subscript; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:geom; 114, identifier:shape; 115, integer:0; 116, binary_operator:*; 116, 117; 116, 118; 117, integer:3; 118, subscript; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:masses; 121, identifier:shape; 122, integer:0; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:masses; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:masses; 130, identifier:repeat; 131, argument_list; 131, 132; 132, integer:3; 133, comment; 134, comment; 135, comment; 136, comment; 137, comment; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:ctr; 141, binary_operator:/; 141, 142; 141, 174; 142, call; 142, 143; 142, 173; 143, attribute; 143, 144; 143, 172; 144, call; 144, 145; 144, 168; 145, attribute; 145, 146; 145, 166; 145, 167; 146, call; 146, 147; 146, 156; 147, attribute; 147, 148; 147, 155; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:np; 151, identifier:multiply; 152, argument_list; 152, 153; 152, 154; 153, identifier:geom; 154, identifier:masses; 155, identifier:reshape; 156, argument_list; 156, 157; 157, tuple; 157, 158; 157, 165; 158, binary_operator://; 158, 159; 158, 164; 159, subscript; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:geom; 162, identifier:shape; 163, integer:0; 164, integer:3; 165, integer:3; 166, line_continuation:\; 167, identifier:sum; 168, argument_list; 168, 169; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:axis; 171, integer:0; 172, identifier:squeeze; 173, argument_list; 174, parenthesized_expression; 174, 175; 175, binary_operator:/; 175, 176; 175, 181; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:masses; 179, identifier:sum; 180, argument_list; 181, integer:3; 182, comment; 183, return_statement; 183, 184; 184, identifier:ctr
def ctr_mass(geom, masses): """Calculate the center of mass of the indicated geometry. Take a geometry and atom masses and compute the location of the center of mass. Parameters ---------- geom length-3N |npfloat_| -- Coordinates of the atoms masses length-N OR length-3N |npfloat_| -- Atomic masses of the atoms. Length-3N option is to allow calculation of a per-coordinate perturbed value. Returns ------- ctr length-3 |npfloat_| -- Vector location of center of mass Raises ------ ~exceptions.ValueError If `geom` & `masses` shapes are inconsistent """ # Imports import numpy as np from .base import safe_cast as scast # Shape check if len(geom.shape) != 1: raise ValueError("Geometry is not a vector") ## end if if len(masses.shape) != 1: raise ValueError("Masses cannot be parsed as a vector") ## end if if not geom.shape[0] % 3 == 0: raise ValueError("Geometry is not length-3N") ## end if if geom.shape[0] != 3*masses.shape[0] and geom.shape[0] != masses.shape[0]: raise ValueError("Inconsistent geometry and masses vector lengths") ## end if # If N masses are provided, expand to 3N; if 3N, retain. if geom.shape[0] == 3*masses.shape[0]: masses = masses.repeat(3) ## end if # Calculate the mass-weighted coordinates, reshape to group by coordinate # column-wise, sum each column, then divide by the sum of masses, which # must further be divided by three because there are three replicates # (possibly perturbed) of the mass of each atom. ctr = np.multiply(geom, masses).reshape((geom.shape[0]//3, 3)) \ .sum(axis=0).squeeze() / (masses.sum() / 3) # Return the vector return ctr
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:inertia_tensor; 3, parameters; 3, 4; 3, 5; 4, identifier:geom; 5, identifier:masses; 6, block; 6, 7; 6, 9; 6, 10; 6, 15; 6, 16; 6, 17; 6, 25; 6, 26; 6, 27; 6, 51; 6, 52; 6, 53; 6, 64; 6, 65; 6, 263; 6, 264; 6, 265; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, import_statement; 10, 11; 11, aliased_import; 11, 12; 11, 14; 12, dotted_name; 12, 13; 13, identifier:numpy; 14, identifier:np; 15, comment; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:geom; 20, call; 20, 21; 20, 22; 21, identifier:ctr_geom; 22, argument_list; 22, 23; 22, 24; 23, identifier:geom; 24, identifier:masses; 25, comment; 26, comment; 27, if_statement; 27, 28; 27, 41; 28, comparison_operator:==; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:geom; 32, identifier:shape; 33, integer:0; 34, binary_operator:*; 34, 35; 34, 36; 35, integer:3; 36, subscript; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:masses; 39, identifier:shape; 40, integer:0; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:masses; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:masses; 48, identifier:repeat; 49, argument_list; 49, 50; 50, integer:3; 51, comment; 52, comment; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:tensor; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:np; 59, identifier:zeros; 60, argument_list; 60, 61; 61, tuple; 61, 62; 61, 63; 62, integer:3; 63, integer:3; 64, comment; 65, for_statement; 65, 66; 65, 67; 65, 71; 66, identifier:i; 67, call; 67, 68; 67, 69; 68, identifier:range; 69, argument_list; 69, 70; 70, integer:3; 71, block; 71, 72; 71, 262; 72, for_statement; 72, 73; 72, 74; 72, 79; 73, identifier:j; 74, call; 74, 75; 74, 76; 75, identifier:range; 76, argument_list; 76, 77; 76, 78; 77, identifier:i; 78, integer:3; 79, block; 79, 80; 79, 261; 80, if_statement; 80, 81; 80, 84; 80, 85; 80, 162; 81, comparison_operator:==; 81, 82; 81, 83; 82, identifier:i; 83, identifier:j; 84, comment; 85, block; 85, 86; 85, 135; 85, 136; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:ind; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:np; 92, identifier:concatenate; 93, argument_list; 93, 94; 94, list_comprehension; 94, 95; 94, 123; 95, binary_operator:+; 95, 96; 95, 122; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:np; 99, identifier:array; 100, argument_list; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:list; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:map; 106, argument_list; 106, 107; 106, 113; 107, lambda; 107, 108; 107, 110; 108, lambda_parameters; 108, 109; 109, identifier:v; 110, binary_operator:%; 110, 111; 110, 112; 111, identifier:v; 112, integer:3; 113, call; 113, 114; 113, 115; 114, identifier:range; 115, argument_list; 115, 116; 115, 119; 116, binary_operator:+; 116, 117; 116, 118; 117, identifier:i; 118, integer:1; 119, binary_operator:+; 119, 120; 119, 121; 120, identifier:i; 121, integer:3; 122, identifier:o; 123, for_in_clause; 123, 124; 123, 125; 124, identifier:o; 125, call; 125, 126; 125, 127; 126, identifier:range; 127, argument_list; 127, 128; 127, 129; 127, 134; 128, integer:0; 129, subscript; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:geom; 132, identifier:shape; 133, integer:0; 134, integer:3; 135, comment; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 142; 138, subscript; 138, 139; 138, 140; 138, 141; 139, identifier:tensor; 140, identifier:i; 141, identifier:i; 142, call; 142, 143; 142, 161; 143, attribute; 143, 144; 143, 160; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:np; 147, identifier:multiply; 148, argument_list; 148, 149; 148, 157; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:np; 152, identifier:square; 153, argument_list; 153, 154; 154, subscript; 154, 155; 154, 156; 155, identifier:geom; 156, identifier:ind; 157, subscript; 157, 158; 157, 159; 158, identifier:masses; 159, identifier:ind; 160, identifier:sum; 161, argument_list; 162, else_clause; 162, 163; 162, 164; 163, comment; 164, block; 164, 165; 164, 185; 164, 205; 164, 206; 164, 251; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:ind_i; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:np; 171, identifier:array; 172, argument_list; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:range; 175, argument_list; 175, 176; 175, 177; 175, 184; 176, identifier:i; 177, binary_operator:+; 177, 178; 177, 183; 178, subscript; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:geom; 181, identifier:shape; 182, integer:0; 183, identifier:i; 184, integer:3; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:ind_j; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:np; 191, identifier:array; 192, argument_list; 192, 193; 193, call; 193, 194; 193, 195; 194, identifier:range; 195, argument_list; 195, 196; 195, 197; 195, 204; 196, identifier:j; 197, binary_operator:+; 197, 198; 197, 203; 198, subscript; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:geom; 201, identifier:shape; 202, integer:0; 203, identifier:j; 204, integer:3; 205, comment; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 212; 208, subscript; 208, 209; 208, 210; 208, 211; 209, identifier:tensor; 210, identifier:i; 211, identifier:j; 212, binary_operator:*; 212, 213; 212, 249; 213, call; 213, 214; 213, 248; 214, attribute; 214, 215; 214, 247; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:np; 218, identifier:multiply; 219, argument_list; 219, 220; 219, 236; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:np; 223, identifier:sqrt; 224, argument_list; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:np; 228, identifier:multiply; 229, argument_list; 229, 230; 229, 233; 230, subscript; 230, 231; 230, 232; 231, identifier:masses; 232, identifier:ind_i; 233, subscript; 233, 234; 233, 235; 234, identifier:masses; 235, identifier:ind_j; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:np; 239, identifier:multiply; 240, argument_list; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:geom; 243, identifier:ind_i; 244, subscript; 244, 245; 244, 246; 245, identifier:geom; 246, identifier:ind_j; 247, identifier:sum; 248, argument_list; 249, unary_operator:-; 249, 250; 250, integer:1; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 257; 253, subscript; 253, 254; 253, 255; 253, 256; 254, identifier:tensor; 255, identifier:j; 256, identifier:i; 257, subscript; 257, 258; 257, 259; 257, 260; 258, identifier:tensor; 259, identifier:i; 260, identifier:j; 261, comment; 262, comment; 263, comment; 264, comment; 265, return_statement; 265, 266; 266, identifier:tensor
def inertia_tensor(geom, masses): """Generate the 3x3 moment-of-inertia tensor. Compute the 3x3 moment-of-inertia tensor for the provided geometry and atomic masses. Always recenters the geometry to the center of mass as the first step. Reference for inertia tensor: [Kro92]_, Eq. (2.26) .. todo:: Replace cite eventually with link to exposition in user guide. Parameters ---------- geom length-3N |npfloat_| -- Coordinates of the atoms masses length-N OR length-3N |npfloat_| -- Atomic masses of the atoms. Length-3N option is to allow calculation of a per-coordinate perturbed value. Returns ------- tensor 3 x 3 |npfloat_| -- Moment of inertia tensor for the system Raises ------ ~exceptions.ValueError If shapes of `geom` & `masses` are inconsistent """ # Imports import numpy as np # Center the geometry. Takes care of any improper shapes of geom or # masses via the internal call to 'ctr_mass' within the call to 'ctr_geom' geom = ctr_geom(geom, masses) # Expand the masses if required. Shape should only ever be (N,) or (3N,), # else would raise an exception within the above 'ctr_geom' call if geom.shape[0] == 3*masses.shape[0]: masses = masses.repeat(3) ## end if # Initialize the tensor matrix tensor = np.zeros((3,3)) # Fill the matrix for i in range(3): for j in range(i,3): if i == j: # On-diagonal element; calculate indices to include ind = np.concatenate([np.array(list(map(lambda v: v % 3, range(i+1, i+3)))) + o for o in range(0,geom.shape[0],3)]) # Calculate the tensor element tensor[i,i] = np.multiply(np.square(geom[ind]), masses[ind]).sum() else: # Off-diagonal element; calculate the indices ind_i = np.array(range(i,geom.shape[0]+i,3)) ind_j = np.array(range(j,geom.shape[0]+j,3)) # Calculate the tensor element and its symmetric partner tensor[i,j] = np.multiply( np.sqrt(np.multiply(masses[ind_i], masses[ind_j])) , np.multiply(geom[ind_i], geom[ind_j]) ).sum() * -1 tensor[j,i] = tensor[i,j] ## end if ## next j ## next i # Return the tensor return tensor
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:rot_consts; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 11; 4, identifier:geom; 5, identifier:masses; 6, default_parameter; 6, 7; 6, 8; 7, identifier:units; 8, attribute; 8, 9; 8, 10; 9, identifier:_EURC; 10, identifier:INV_INERTIA; 11, default_parameter; 11, 12; 11, 13; 12, identifier:on_tol; 13, attribute; 13, 14; 13, 15; 14, identifier:_DEF; 15, identifier:ORTHONORM_TOL; 16, block; 16, 17; 16, 19; 16, 20; 16, 25; 16, 42; 16, 43; 16, 59; 16, 60; 16, 61; 16, 62; 16, 74; 16, 75; 16, 111; 16, 112; 16, 113; 16, 357; 16, 358; 16, 359; 17, expression_statement; 17, 18; 18, comment; 19, comment; 20, import_statement; 20, 21; 21, aliased_import; 21, 22; 21, 24; 22, dotted_name; 22, 23; 23, identifier:numpy; 24, identifier:np; 25, import_from_statement; 25, 26; 25, 30; 25, 34; 25, 38; 25, 40; 26, relative_import; 26, 27; 26, 28; 27, import_prefix; 28, dotted_name; 28, 29; 29, identifier:const; 30, aliased_import; 30, 31; 30, 33; 31, dotted_name; 31, 32; 32, identifier:EnumTopType; 33, identifier:ETT; 34, aliased_import; 34, 35; 34, 37; 35, dotted_name; 35, 36; 36, identifier:EnumUnitsRotConst; 37, identifier:EURC; 38, dotted_name; 38, 39; 39, identifier:PRM; 40, dotted_name; 40, 41; 41, identifier:PHYS; 42, comment; 43, if_statement; 43, 44; 43, 48; 44, not_operator; 44, 45; 45, comparison_operator:in; 45, 46; 45, 47; 46, identifier:units; 47, identifier:EURC; 48, block; 48, 49; 49, raise_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:ValueError; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, string:"'{0}' is not a valid units value"; 56, identifier:format; 57, argument_list; 57, 58; 58, identifier:units; 59, comment; 60, comment; 61, comment; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 68; 64, pattern_list; 64, 65; 64, 66; 64, 67; 65, identifier:mom; 66, identifier:ax; 67, identifier:top; 68, call; 68, 69; 68, 70; 69, identifier:principals; 70, argument_list; 70, 71; 70, 72; 70, 73; 71, identifier:geom; 72, identifier:masses; 73, identifier:on_tol; 74, comment; 75, if_statement; 75, 76; 75, 81; 75, 82; 75, 95; 76, comparison_operator:==; 76, 77; 76, 78; 77, identifier:top; 78, attribute; 78, 79; 78, 80; 79, identifier:ETT; 80, identifier:ATOM; 81, comment; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:mom; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:np; 89, identifier:repeat; 90, argument_list; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:PRM; 93, identifier:ZERO_MOMENT_TOL; 94, integer:3; 95, elif_clause; 95, 96; 95, 101; 95, 102; 96, comparison_operator:==; 96, 97; 96, 98; 97, identifier:top; 98, attribute; 98, 99; 98, 100; 99, identifier:ETT; 100, identifier:LINEAR; 101, comment; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:mom; 107, integer:0; 108, attribute; 108, 109; 108, 110; 109, identifier:PRM; 110, identifier:ZERO_MOMENT_TOL; 111, comment; 112, comment; 113, if_statement; 113, 114; 113, 119; 113, 120; 113, 130; 113, 153; 113, 180; 113, 207; 113, 239; 113, 273; 113, 308; 113, 349; 114, comparison_operator:==; 114, 115; 114, 116; 115, identifier:units; 116, attribute; 116, 117; 116, 118; 117, identifier:EURC; 118, identifier:INV_INERTIA; 119, comment; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:rc; 124, binary_operator:/; 124, 125; 124, 126; 125, float:1.0; 126, parenthesized_expression; 126, 127; 127, binary_operator:*; 127, 128; 127, 129; 128, float:2.0; 129, identifier:mom; 130, elif_clause; 130, 131; 130, 136; 130, 137; 131, comparison_operator:==; 131, 132; 131, 133; 132, identifier:units; 133, attribute; 133, 134; 133, 135; 134, identifier:EURC; 135, identifier:ANGFREQ_ATOMIC; 136, comment; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:rc; 141, binary_operator:/; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:PHYS; 144, identifier:PLANCK_BAR; 145, parenthesized_expression; 145, 146; 146, binary_operator:*; 146, 147; 146, 150; 147, binary_operator:*; 147, 148; 147, 149; 148, float:2.0; 149, identifier:mom; 150, attribute; 150, 151; 150, 152; 151, identifier:PHYS; 152, identifier:ME_PER_AMU; 153, elif_clause; 153, 154; 153, 159; 153, 160; 154, comparison_operator:==; 154, 155; 154, 156; 155, identifier:units; 156, attribute; 156, 157; 156, 158; 157, identifier:EURC; 158, identifier:ANGFREQ_SECS; 159, comment; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:rc; 164, binary_operator:/; 164, 165; 164, 177; 165, binary_operator:/; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:PHYS; 168, identifier:PLANCK_BAR; 169, parenthesized_expression; 169, 170; 170, binary_operator:*; 170, 171; 170, 174; 171, binary_operator:*; 171, 172; 171, 173; 172, float:2.0; 173, identifier:mom; 174, attribute; 174, 175; 174, 176; 175, identifier:PHYS; 176, identifier:ME_PER_AMU; 177, attribute; 177, 178; 177, 179; 178, identifier:PHYS; 179, identifier:SEC_PER_TA; 180, elif_clause; 180, 181; 180, 186; 180, 187; 181, comparison_operator:==; 181, 182; 181, 183; 182, identifier:units; 183, attribute; 183, 184; 183, 185; 184, identifier:EURC; 185, identifier:CYCFREQ_ATOMIC; 186, comment; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:rc; 191, binary_operator:/; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:PHYS; 194, identifier:PLANCK_BAR; 195, parenthesized_expression; 195, 196; 196, binary_operator:*; 196, 197; 196, 204; 197, binary_operator:*; 197, 198; 197, 203; 198, binary_operator:*; 198, 199; 198, 200; 199, float:4.0; 200, attribute; 200, 201; 200, 202; 201, identifier:np; 202, identifier:pi; 203, identifier:mom; 204, attribute; 204, 205; 204, 206; 205, identifier:PHYS; 206, identifier:ME_PER_AMU; 207, elif_clause; 207, 208; 207, 213; 207, 214; 208, comparison_operator:==; 208, 209; 208, 210; 209, identifier:units; 210, attribute; 210, 211; 210, 212; 211, identifier:EURC; 212, identifier:CYCFREQ_HZ; 213, comment; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:rc; 218, binary_operator:/; 218, 219; 218, 235; 218, 236; 219, binary_operator:/; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:PHYS; 222, identifier:PLANCK_BAR; 223, parenthesized_expression; 223, 224; 224, binary_operator:*; 224, 225; 224, 232; 225, binary_operator:*; 225, 226; 225, 231; 226, binary_operator:*; 226, 227; 226, 228; 227, float:4.0; 228, attribute; 228, 229; 228, 230; 229, identifier:np; 230, identifier:pi; 231, identifier:mom; 232, attribute; 232, 233; 232, 234; 233, identifier:PHYS; 234, identifier:ME_PER_AMU; 235, line_continuation:\; 236, attribute; 236, 237; 236, 238; 237, identifier:PHYS; 238, identifier:SEC_PER_TA; 239, elif_clause; 239, 240; 239, 245; 239, 246; 240, comparison_operator:==; 240, 241; 240, 242; 241, identifier:units; 242, attribute; 242, 243; 242, 244; 243, identifier:EURC; 244, identifier:CYCFREQ_MHZ; 245, comment; 246, block; 246, 247; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:rc; 250, binary_operator:/; 250, 251; 250, 272; 251, binary_operator:/; 251, 252; 251, 268; 251, 269; 252, binary_operator:/; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:PHYS; 255, identifier:PLANCK_BAR; 256, parenthesized_expression; 256, 257; 257, binary_operator:*; 257, 258; 257, 265; 258, binary_operator:*; 258, 259; 258, 264; 259, binary_operator:*; 259, 260; 259, 261; 260, float:4.0; 261, attribute; 261, 262; 261, 263; 262, identifier:np; 263, identifier:pi; 264, identifier:mom; 265, attribute; 265, 266; 265, 267; 266, identifier:PHYS; 267, identifier:ME_PER_AMU; 268, line_continuation:\; 269, attribute; 269, 270; 269, 271; 270, identifier:PHYS; 271, identifier:SEC_PER_TA; 272, float:1.0e6; 273, elif_clause; 273, 274; 273, 279; 273, 280; 274, comparison_operator:==; 274, 275; 274, 276; 275, identifier:units; 276, attribute; 276, 277; 276, 278; 277, identifier:EURC; 278, identifier:WAVENUM_ATOMIC; 279, comment; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:rc; 284, binary_operator:/; 284, 285; 284, 295; 284, 296; 285, binary_operator:/; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:PHYS; 288, identifier:PLANCK; 289, parenthesized_expression; 289, 290; 290, binary_operator:*; 290, 291; 290, 292; 291, identifier:mom; 292, attribute; 292, 293; 292, 294; 293, identifier:PHYS; 294, identifier:ME_PER_AMU; 295, line_continuation:\; 296, parenthesized_expression; 296, 297; 297, binary_operator:*; 297, 298; 297, 305; 298, binary_operator:*; 298, 299; 298, 300; 299, float:8.0; 300, binary_operator:**; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:np; 303, identifier:pi; 304, float:2.0; 305, attribute; 305, 306; 305, 307; 306, identifier:PHYS; 307, identifier:LIGHT_SPEED; 308, elif_clause; 308, 309; 308, 314; 308, 315; 309, comparison_operator:==; 309, 310; 309, 311; 310, identifier:units; 311, attribute; 311, 312; 311, 313; 312, identifier:EURC; 313, identifier:WAVENUM_CM; 314, comment; 315, block; 315, 316; 316, expression_statement; 316, 317; 317, assignment; 317, 318; 317, 319; 318, identifier:rc; 319, binary_operator:*; 319, 320; 319, 348; 320, binary_operator:/; 320, 321; 320, 331; 320, 332; 321, binary_operator:/; 321, 322; 321, 325; 322, attribute; 322, 323; 322, 324; 323, identifier:PHYS; 324, identifier:PLANCK; 325, parenthesized_expression; 325, 326; 326, binary_operator:*; 326, 327; 326, 328; 327, identifier:mom; 328, attribute; 328, 329; 328, 330; 329, identifier:PHYS; 330, identifier:ME_PER_AMU; 331, line_continuation:\; 332, parenthesized_expression; 332, 333; 333, binary_operator:*; 333, 334; 333, 345; 334, binary_operator:*; 334, 335; 334, 342; 335, binary_operator:*; 335, 336; 335, 337; 336, float:8.0; 337, binary_operator:**; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:np; 340, identifier:pi; 341, float:2.0; 342, attribute; 342, 343; 342, 344; 343, identifier:PHYS; 344, identifier:LIGHT_SPEED; 345, attribute; 345, 346; 345, 347; 346, identifier:PHYS; 347, identifier:ANG_PER_BOHR; 348, float:1.0e8; 349, else_clause; 349, 350; 349, 351; 350, comment; 351, block; 351, 352; 352, raise_statement; 352, 353; 353, call; 353, 354; 353, 355; 354, identifier:NotImplementedError; 355, argument_list; 355, 356; 356, string:"Units conversion not yet implemented."; 357, comment; 358, comment; 359, return_statement; 359, 360; 360, identifier:rc
def rot_consts(geom, masses, units=_EURC.INV_INERTIA, on_tol=_DEF.ORTHONORM_TOL): """Rotational constants for a given molecular system. Calculates the rotational constants for the provided system with numerical value given in the units provided in `units`. The orthnormality tolerance `on_tol` is required in order to be passed through to the :func:`principals` function. If the system is linear or a single atom, the effectively-zero principal moments of inertia will be assigned values of :data:`opan.const.PRM.ZERO_MOMENT_TOL` before transformation into the appropriate rotational constant units. The moments of inertia are always sorted in increasing order as :math:`0 \\leq I_A \\leq I_B \\leq I_C`; the rotational constants calculated from these will thus always be in **decreasing** order as :math:`B_A \\geq B_B \\geq B_C`, retaining the ordering and association with the three principal ``axes[:,i]`` generated by :func:`principals`. Parameters ---------- geom length-3N |npfloat_| -- Coordinates of the atoms masses length-N OR length-3N |npfloat_| -- Atomic masses of the atoms. Length-3N option is to allow calculation of a per-coordinate perturbed value. units :class:`~opan.const.EnumUnitsRotConst`, optional -- Enum value indicating the desired units of the output rotational constants. Default is :data:`~opan.const.EnumUnitsRotConst.INV_INERTIA` :math:`\\left(1\\over \\mathrm{uB^2}\\right)` on_tol |npfloat_|, optional -- Tolerance for deviation from unity/zero for principal axis dot products, within which axes are considered orthonormal. Default is :data:`opan.const.DEF.ORTHONORM_TOL` Returns ------- rc length-3 |npfloat_| -- Vector of rotational constants in the indicated units """ # Imports import numpy as np from ..const import EnumTopType as ETT, EnumUnitsRotConst as EURC, PRM, PHYS # Ensure units are valid if not units in EURC: raise ValueError("'{0}' is not a valid units value".format(units)) ## end if # Retrieve the moments, axes and top type. Geom and masses are proofed # internally in this call. mom, ax, top = principals(geom, masses, on_tol) # Check for special cases if top == ETT.ATOM: # All moments are zero; set to zero-moment threshold mom = np.repeat(PRM.ZERO_MOMENT_TOL, 3) elif top == ETT.LINEAR: # First moment is zero; set to zero-moment threshold mom[0] = PRM.ZERO_MOMENT_TOL ## end if # Calculate the values in the indicated units if units == EURC.INV_INERTIA: # 1/(amu*B^2) rc = 1.0 / (2.0 * mom) elif units == EURC.ANGFREQ_ATOMIC: # 1/Ta rc = PHYS.PLANCK_BAR / (2.0 * mom * PHYS.ME_PER_AMU) elif units == EURC.ANGFREQ_SECS: # 1/s rc = PHYS.PLANCK_BAR / (2.0 * mom * PHYS.ME_PER_AMU) / PHYS.SEC_PER_TA elif units == EURC.CYCFREQ_ATOMIC: # cyc/Ta rc = PHYS.PLANCK_BAR / (4.0 * np.pi * mom * PHYS.ME_PER_AMU) elif units == EURC.CYCFREQ_HZ: # cyc/s rc = PHYS.PLANCK_BAR / (4.0 * np.pi * mom * PHYS.ME_PER_AMU) / \ PHYS.SEC_PER_TA elif units == EURC.CYCFREQ_MHZ: # Mcyc/s rc = PHYS.PLANCK_BAR / (4.0 * np.pi * mom * PHYS.ME_PER_AMU) / \ PHYS.SEC_PER_TA / 1.0e6 elif units == EURC.WAVENUM_ATOMIC: # cyc/B rc = PHYS.PLANCK / (mom * PHYS.ME_PER_AMU) / \ (8.0 * np.pi**2.0 * PHYS.LIGHT_SPEED) elif units == EURC.WAVENUM_CM: # cyc/cm rc = PHYS.PLANCK / (mom * PHYS.ME_PER_AMU) / \ (8.0 * np.pi**2.0 * PHYS.LIGHT_SPEED * PHYS.ANG_PER_BOHR) * 1.0e8 else: # pragma: no cover -- Valid units; not implemented raise NotImplementedError("Units conversion not yet implemented.") ## end if # Return the result return rc
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_fadn_orth; 3, parameters; 3, 4; 3, 5; 4, identifier:vec; 5, identifier:geom; 6, block; 6, 7; 6, 9; 6, 10; 6, 15; 6, 22; 6, 29; 6, 36; 6, 45; 6, 46; 6, 73; 6, 74; 6, 88; 6, 89; 6, 90; 6, 107; 6, 108; 6, 109; 6, 120; 6, 121; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, import_statement; 10, 11; 11, aliased_import; 11, 12; 11, 14; 12, dotted_name; 12, 13; 13, identifier:numpy; 14, identifier:np; 15, import_from_statement; 15, 16; 15, 18; 16, dotted_name; 16, 17; 17, identifier:scipy; 18, aliased_import; 18, 19; 18, 21; 19, dotted_name; 19, 20; 20, identifier:linalg; 21, identifier:spla; 22, import_from_statement; 22, 23; 22, 27; 23, relative_import; 23, 24; 23, 25; 24, import_prefix; 25, dotted_name; 25, 26; 26, identifier:const; 27, dotted_name; 27, 28; 28, identifier:PRM; 29, import_from_statement; 29, 30; 29, 34; 30, relative_import; 30, 31; 30, 32; 31, import_prefix; 32, dotted_name; 32, 33; 33, identifier:error; 34, dotted_name; 34, 35; 35, identifier:InertiaError; 36, import_from_statement; 36, 37; 36, 41; 37, relative_import; 37, 38; 37, 39; 38, import_prefix; 39, dotted_name; 39, 40; 40, identifier:vector; 41, aliased_import; 41, 42; 41, 44; 42, dotted_name; 42, 43; 43, identifier:orthonorm_check; 44, identifier:onchk; 45, comment; 46, if_statement; 46, 47; 46, 67; 47, not_operator; 47, 48; 48, parenthesized_expression; 48, 49; 49, boolean_operator:and; 49, 50; 49, 58; 50, comparison_operator:==; 50, 51; 50, 57; 51, call; 51, 52; 51, 53; 52, identifier:len; 53, argument_list; 53, 54; 54, attribute; 54, 55; 54, 56; 55, identifier:geom; 56, identifier:shape; 57, integer:1; 58, comparison_operator:==; 58, 59; 58, 66; 59, binary_operator:%; 59, 60; 59, 65; 60, subscript; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:geom; 63, identifier:shape; 64, integer:0; 65, integer:3; 66, integer:0; 67, block; 67, 68; 68, raise_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:ValueError; 71, argument_list; 71, 72; 72, string:"Geometry is not length 3N"; 73, comment; 74, if_statement; 74, 75; 74, 82; 75, not_operator; 75, 76; 76, comparison_operator:==; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:vec; 79, identifier:shape; 80, tuple; 80, 81; 81, integer:3; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:ValueError; 86, argument_list; 86, 87; 87, string:"Reference vector is not length 3"; 88, comment; 89, comment; 90, if_statement; 90, 91; 90, 101; 91, comparison_operator:<; 91, 92; 91, 98; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:spla; 95, identifier:norm; 96, argument_list; 96, 97; 97, identifier:vec; 98, attribute; 98, 99; 98, 100; 99, identifier:PRM; 100, identifier:ZERO_VEC_TOL; 101, block; 101, 102; 102, raise_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:ValueError; 105, argument_list; 105, 106; 106, string:"Reference vector norm is too small"; 107, comment; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:vec; 112, binary_operator:/; 112, 113; 112, 114; 113, identifier:vec; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:spla; 117, identifier:norm; 118, argument_list; 118, 119; 119, identifier:vec; 120, comment; 121, for_statement; 121, 122; 121, 123; 121, 137; 121, 138; 121, 139; 121, 197; 121, 198; 122, identifier:disp; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:geom; 126, identifier:reshape; 127, argument_list; 127, 128; 128, tuple; 128, 129; 128, 136; 129, binary_operator://; 129, 130; 129, 135; 130, subscript; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:geom; 133, identifier:shape; 134, integer:0; 135, integer:3; 136, integer:3; 137, comment; 138, comment; 139, block; 139, 140; 139, 196; 140, if_statement; 140, 141; 140, 180; 140, 181; 141, boolean_operator:and; 141, 142; 141, 152; 142, comparison_operator:>=; 142, 143; 142, 149; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:spla; 146, identifier:norm; 147, argument_list; 147, 148; 148, identifier:disp; 149, attribute; 149, 150; 149, 151; 150, identifier:PRM; 151, identifier:ZERO_VEC_TOL; 152, not_operator; 152, 153; 153, subscript; 153, 154; 153, 179; 154, call; 154, 155; 154, 156; 155, identifier:onchk; 156, argument_list; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:np; 160, identifier:column_stack; 161, argument_list; 161, 162; 162, tuple; 162, 163; 162, 171; 163, binary_operator:/; 163, 164; 163, 165; 164, identifier:disp; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:spla; 168, identifier:norm; 169, argument_list; 169, 170; 170, identifier:disp; 171, binary_operator:/; 171, 172; 171, 173; 172, identifier:vec; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:spla; 176, identifier:norm; 177, argument_list; 177, 178; 178, identifier:vec; 179, integer:0; 180, comment; 181, block; 181, 182; 181, 193; 181, 195; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:out_vec; 185, binary_operator:/; 185, 186; 185, 187; 186, identifier:disp; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:spla; 190, identifier:norm; 191, argument_list; 191, 192; 192, identifier:disp; 193, return_statement; 193, 194; 194, identifier:out_vec; 195, comment; 196, comment; 197, comment; 198, else_clause; 198, 199; 198, 200; 199, comment; 200, block; 200, 201; 201, raise_statement; 201, 202; 202, call; 202, 203; 202, 204; 203, identifier:InertiaError; 204, argument_list; 204, 205; 204, 208; 204, 209; 205, attribute; 205, 206; 205, 207; 206, identifier:InertiaError; 207, identifier:BAD_GEOM; 208, string:"No suitable atomic displacement found"; 209, string:""
def _fadn_orth(vec, geom): """First non-zero Atomic Displacement Non-Orthogonal to Vec Utility function to identify the first atomic displacement in a geometry that is (a) not the zero vector; and (b) not normal to the reference vector. Parameters ---------- vec length-3 |npfloat_| -- Reference vector. Does not need to be normalized. geom length-3N |npfloat_| -- *CENTERED* molecular geometry Returns ------- out_vec length-3 |npfloat_| -- Normalized non-zero atomic displacement not orthogonal to vec """ # Imports import numpy as np from scipy import linalg as spla from ..const import PRM from ..error import InertiaError from .vector import orthonorm_check as onchk # Geom and vec must both be the right shape if not (len(geom.shape) == 1 and geom.shape[0] % 3 == 0): raise ValueError("Geometry is not length 3N") ## end if if not vec.shape == (3,): raise ValueError("Reference vector is not length 3") ## end if # vec must not be the zero vector if spla.norm(vec) < PRM.ZERO_VEC_TOL: raise ValueError("Reference vector norm is too small") ## end if # Normalize the ref vec vec = vec / spla.norm(vec) # Iterate over reshaped geometry for disp in geom.reshape((geom.shape[0]//3, 3)): # See if the displacement is nonzero and not orthonormal. Trailing # [0] index is to retrieve only the success/fail bool. if spla.norm(disp) >= PRM.ZERO_VEC_TOL and not onchk( np.column_stack((disp / spla.norm(disp), vec / spla.norm(vec))))[0]: # This is the displacement you are looking for out_vec = disp / spla.norm(disp) return out_vec ## end if ## end if ## next disp else: # Nothing fit the bill - must be atom, linear, or planar raise InertiaError(InertiaError.BAD_GEOM, "No suitable atomic displacement found", "")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_fadn_par; 3, parameters; 3, 4; 3, 5; 4, identifier:vec; 5, identifier:geom; 6, block; 6, 7; 6, 9; 6, 10; 6, 15; 6, 22; 6, 29; 6, 36; 6, 45; 6, 46; 6, 73; 6, 74; 6, 88; 6, 89; 6, 90; 6, 107; 6, 108; 6, 109; 6, 120; 6, 121; 6, 192; 6, 193; 6, 194; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, import_statement; 10, 11; 11, aliased_import; 11, 12; 11, 14; 12, dotted_name; 12, 13; 13, identifier:numpy; 14, identifier:np; 15, import_from_statement; 15, 16; 15, 18; 16, dotted_name; 16, 17; 17, identifier:scipy; 18, aliased_import; 18, 19; 18, 21; 19, dotted_name; 19, 20; 20, identifier:linalg; 21, identifier:spla; 22, import_from_statement; 22, 23; 22, 27; 23, relative_import; 23, 24; 23, 25; 24, import_prefix; 25, dotted_name; 25, 26; 26, identifier:const; 27, dotted_name; 27, 28; 28, identifier:PRM; 29, import_from_statement; 29, 30; 29, 34; 30, relative_import; 30, 31; 30, 32; 31, import_prefix; 32, dotted_name; 32, 33; 33, identifier:error; 34, dotted_name; 34, 35; 35, identifier:InertiaError; 36, import_from_statement; 36, 37; 36, 41; 37, relative_import; 37, 38; 37, 39; 38, import_prefix; 39, dotted_name; 39, 40; 40, identifier:vector; 41, aliased_import; 41, 42; 41, 44; 42, dotted_name; 42, 43; 43, identifier:parallel_check; 44, identifier:parchk; 45, comment; 46, if_statement; 46, 47; 46, 67; 47, not_operator; 47, 48; 48, parenthesized_expression; 48, 49; 49, boolean_operator:and; 49, 50; 49, 58; 50, comparison_operator:==; 50, 51; 50, 57; 51, call; 51, 52; 51, 53; 52, identifier:len; 53, argument_list; 53, 54; 54, attribute; 54, 55; 54, 56; 55, identifier:geom; 56, identifier:shape; 57, integer:1; 58, comparison_operator:==; 58, 59; 58, 66; 59, binary_operator:%; 59, 60; 59, 65; 60, subscript; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:geom; 63, identifier:shape; 64, integer:0; 65, integer:3; 66, integer:0; 67, block; 67, 68; 68, raise_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:ValueError; 71, argument_list; 71, 72; 72, string:"Geometry is not length 3N"; 73, comment; 74, if_statement; 74, 75; 74, 82; 75, not_operator; 75, 76; 76, comparison_operator:==; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:vec; 79, identifier:shape; 80, tuple; 80, 81; 81, integer:3; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:ValueError; 86, argument_list; 86, 87; 87, string:"Reference vector is not length 3"; 88, comment; 89, comment; 90, if_statement; 90, 91; 90, 101; 91, comparison_operator:<; 91, 92; 91, 98; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:spla; 95, identifier:norm; 96, argument_list; 96, 97; 97, identifier:vec; 98, attribute; 98, 99; 98, 100; 99, identifier:PRM; 100, identifier:ZERO_VEC_TOL; 101, block; 101, 102; 102, raise_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:ValueError; 105, argument_list; 105, 106; 106, string:"Reference vector norm is too small"; 107, comment; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:vec; 112, binary_operator:/; 112, 113; 112, 114; 113, identifier:vec; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:spla; 117, identifier:norm; 118, argument_list; 118, 119; 119, identifier:vec; 120, comment; 121, for_statement; 121, 122; 121, 123; 121, 137; 121, 138; 121, 179; 121, 180; 122, identifier:disp; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:geom; 126, identifier:reshape; 127, argument_list; 127, 128; 128, tuple; 128, 129; 128, 136; 129, binary_operator://; 129, 130; 129, 135; 130, subscript; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:geom; 133, identifier:shape; 134, integer:0; 135, integer:3; 136, integer:3; 137, comment; 138, block; 138, 139; 138, 178; 139, if_statement; 139, 140; 139, 163; 139, 164; 140, boolean_operator:and; 140, 141; 140, 151; 140, 152; 141, comparison_operator:>=; 141, 142; 141, 148; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:spla; 145, identifier:norm; 146, argument_list; 146, 147; 147, identifier:disp; 148, attribute; 148, 149; 148, 150; 149, identifier:PRM; 150, identifier:ZERO_VEC_TOL; 151, line_continuation:\; 152, not_operator; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:parchk; 155, argument_list; 155, 156; 155, 162; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:disp; 159, identifier:reshape; 160, argument_list; 160, 161; 161, integer:3; 162, identifier:vec; 163, comment; 164, block; 164, 165; 164, 176; 164, 177; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:out_vec; 168, binary_operator:/; 168, 169; 168, 170; 169, identifier:disp; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:spla; 173, identifier:norm; 174, argument_list; 174, 175; 175, identifier:disp; 176, break_statement; 177, comment; 178, comment; 179, comment; 180, else_clause; 180, 181; 180, 182; 181, comment; 182, block; 182, 183; 183, raise_statement; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:InertiaError; 186, argument_list; 186, 187; 186, 190; 186, 191; 187, attribute; 187, 188; 187, 189; 188, identifier:InertiaError; 189, identifier:BAD_GEOM; 190, string:"Linear molecule, no non-parallel displacement"; 191, string:""; 192, comment; 193, comment; 194, return_statement; 194, 195; 195, identifier:out_vec
def _fadn_par(vec, geom): """First non-zero Atomic Displacement that is Non-Parallel with Vec Utility function to identify the first atomic displacement in a geometry that is both (a) not the zero vector and (b) non-(anti-)parallel with a reference vector. Parameters ---------- vec length-3 |npfloat_| -- Reference vector. Does not need to be normalized. geom length-3N |npfloat_| -- *CENTERED* molecular geometry. Returns ------- out_vec length-3 |npfloat_| -- Normalized non-zero atomic displacement not (anti-)parallel to vec. """ # Imports import numpy as np from scipy import linalg as spla from ..const import PRM from ..error import InertiaError from .vector import parallel_check as parchk # Geom and vec must both be the right shape if not (len(geom.shape) == 1 and geom.shape[0] % 3 == 0): raise ValueError("Geometry is not length 3N") ## end if if not vec.shape == (3,): raise ValueError("Reference vector is not length 3") ## end if # vec must not be the zero vector if spla.norm(vec) < PRM.ZERO_VEC_TOL: raise ValueError("Reference vector norm is too small") ## end if # Normalize the ref vec vec = vec / spla.norm(vec) # Iterate over reshaped geometry for disp in geom.reshape((geom.shape[0]//3, 3)): # See if the displacement is nonzero and nonparallel to the ref vec if spla.norm(disp) >= PRM.ZERO_VEC_TOL and \ not parchk(disp.reshape(3), vec): # This is the displacement you are looking for out_vec = disp / spla.norm(disp) break ## end if ## end if ## next disp else: # Nothing fit the bill - must be a linear molecule? raise InertiaError(InertiaError.BAD_GEOM, "Linear molecule, no non-parallel displacement", "") ## end for disp # Return the resulting vector return out_vec
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:reference_contexts_for_variants; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:variants; 5, identifier:context_size; 6, default_parameter; 6, 7; 6, 8; 7, identifier:transcript_id_whitelist; 8, None; 9, block; 9, 10; 9, 12; 9, 18; 9, 39; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:result; 15, call; 15, 16; 15, 17; 16, identifier:OrderedDict; 17, argument_list; 18, for_statement; 18, 19; 18, 20; 18, 21; 19, identifier:variant; 20, identifier:variants; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 27; 24, subscript; 24, 25; 24, 26; 25, identifier:result; 26, identifier:variant; 27, call; 27, 28; 27, 29; 28, identifier:reference_contexts_for_variant; 29, argument_list; 29, 30; 29, 33; 29, 36; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:variant; 32, identifier:variant; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:context_size; 35, identifier:context_size; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:transcript_id_whitelist; 38, identifier:transcript_id_whitelist; 39, return_statement; 39, 40; 40, identifier:result
def reference_contexts_for_variants( variants, context_size, transcript_id_whitelist=None): """ Extract a set of reference contexts for each variant in the collection. Parameters ---------- variants : varcode.VariantCollection context_size : int Max of nucleotides to include to the left and right of the variant in the context sequence. transcript_id_whitelist : set, optional If given, then only consider transcripts whose IDs are in this set. Returns a dictionary from variants to lists of ReferenceContext objects, sorted by max coding sequence length of any transcript. """ result = OrderedDict() for variant in variants: result[variant] = reference_contexts_for_variant( variant=variant, context_size=context_size, transcript_id_whitelist=transcript_id_whitelist) return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:pack_tups; 3, parameters; 3, 4; 4, list_splat_pattern; 4, 5; 5, identifier:args; 6, block; 6, 7; 6, 9; 6, 10; 6, 15; 6, 16; 6, 20; 6, 21; 6, 26; 6, 27; 6, 32; 6, 33; 6, 47; 6, 48; 6, 66; 6, 67; 6, 68; 6, 87; 6, 88; 6, 89; 6, 107; 6, 114; 6, 115; 6, 138; 6, 139; 6, 140; 6, 141; 6, 149; 6, 150; 6, 151; 6, 152; 6, 185; 6, 186; 6, 200; 6, 201; 6, 202; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, import_statement; 10, 11; 11, aliased_import; 11, 12; 11, 14; 12, dotted_name; 12, 13; 13, identifier:numpy; 14, identifier:np; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:_DEBUG; 19, False; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:NOT_ITER; 24, unary_operator:-; 24, 25; 25, integer:1; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:UNINIT_VAL; 30, unary_operator:-; 30, 31; 31, integer:1; 32, comment; 33, if_statement; 33, 34; 33, 35; 33, 36; 34, identifier:_DEBUG; 35, comment; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:print; 40, argument_list; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, string:"args = {0}"; 44, identifier:format; 45, argument_list; 45, 46; 46, identifier:args; 47, comment; 48, class_definition; 48, 49; 48, 50; 48, 52; 49, identifier:StrNoIter; 50, argument_list; 50, 51; 51, identifier:str; 52, block; 52, 53; 52, 55; 52, 65; 53, expression_statement; 53, 54; 54, comment; 55, function_definition; 55, 56; 55, 57; 55, 59; 56, function_name:__iter__; 57, parameters; 57, 58; 58, identifier:self; 59, block; 59, 60; 60, raise_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:NotImplementedError; 63, argument_list; 63, 64; 64, string:"Non-iterable string"; 65, comment; 66, comment; 67, comment; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:mod_args; 71, list_comprehension; 71, 72; 71, 84; 72, parenthesized_expression; 72, 73; 73, conditional_expression:if; 73, 74; 73, 78; 73, 83; 74, call; 74, 75; 74, 76; 75, identifier:StrNoIter; 76, argument_list; 76, 77; 77, identifier:a; 78, call; 78, 79; 78, 80; 79, identifier:isinstance; 80, argument_list; 80, 81; 80, 82; 81, identifier:a; 82, identifier:str; 83, identifier:a; 84, for_in_clause; 84, 85; 84, 86; 85, identifier:a; 86, identifier:args; 87, comment; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:iterlens; 92, list_comprehension; 92, 93; 92, 104; 93, parenthesized_expression; 93, 94; 94, conditional_expression:if; 94, 95; 94, 99; 94, 103; 95, call; 95, 96; 95, 97; 96, identifier:len; 97, argument_list; 97, 98; 98, identifier:a; 99, call; 99, 100; 99, 101; 100, identifier:iterable; 101, argument_list; 101, 102; 102, identifier:a; 103, identifier:NOT_ITER; 104, for_in_clause; 104, 105; 104, 106; 105, identifier:a; 106, identifier:mod_args; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:maxiter; 110, call; 110, 111; 110, 112; 111, identifier:max; 112, argument_list; 112, 113; 113, identifier:iterlens; 114, comment; 115, if_statement; 115, 116; 115, 132; 116, not_operator; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:all; 119, argument_list; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:map; 122, argument_list; 122, 123; 122, 131; 123, lambda; 123, 124; 123, 126; 124, lambda_parameters; 124, 125; 125, identifier:v; 126, comparison_operator:in; 126, 127; 126, 128; 127, identifier:v; 128, tuple; 128, 129; 128, 130; 129, identifier:NOT_ITER; 130, identifier:maxiter; 131, identifier:iterlens; 132, block; 132, 133; 133, raise_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:ValueError; 136, argument_list; 136, 137; 137, string:"All iterable items must be of equal length"; 138, comment; 139, comment; 140, comment; 141, if_statement; 141, 142; 141, 145; 142, comparison_operator:==; 142, 143; 142, 144; 143, identifier:maxiter; 144, identifier:NOT_ITER; 145, block; 145, 146; 146, return_statement; 146, 147; 147, list:[args]; 147, 148; 148, identifier:args; 149, comment; 150, comment; 151, comment; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:tups; 155, call; 155, 156; 155, 157; 156, identifier:list; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:zip; 160, argument_list; 160, 161; 161, list_splat; 161, 162; 162, list_comprehension; 162, 163; 162, 176; 163, parenthesized_expression; 163, 164; 164, conditional_expression:if; 164, 165; 164, 172; 164, 175; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:np; 168, identifier:repeat; 169, argument_list; 169, 170; 169, 171; 170, identifier:a; 171, identifier:maxiter; 172, comparison_operator:==; 172, 173; 172, 174; 173, identifier:l; 174, identifier:NOT_ITER; 175, identifier:a; 176, for_in_clause; 176, 177; 176, 180; 177, tuple_pattern; 177, 178; 177, 179; 178, identifier:a; 179, identifier:l; 180, call; 180, 181; 180, 182; 181, identifier:zip; 182, argument_list; 182, 183; 182, 184; 183, identifier:mod_args; 184, identifier:iterlens; 185, comment; 186, if_statement; 186, 187; 186, 188; 186, 189; 187, identifier:_DEBUG; 188, comment; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:print; 193, argument_list; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, string:"tups = {0}"; 197, identifier:format; 198, argument_list; 198, 199; 199, identifier:tups; 200, comment; 201, comment; 202, return_statement; 202, 203; 203, identifier:tups
def pack_tups(*args): """Pack an arbitrary set of iterables and non-iterables into tuples. Function packs a set of inputs with arbitrary iterability into tuples. Iterability is tested with :func:`iterable`. Non-iterable inputs are repeated in each output tuple. Iterable inputs are expanded uniformly across the output tuples. For consistency, all iterables must be the same length. The input arguments are parsed such that bare strings are treated as **NON-ITERABLE**, through the use of a local subclass of |str| that cripples the ``__iter__()`` method. Any strings passed are returned in the packed tuples as standard, **ITERABLE** instances of |str|, however. The order of the input arguments is retained within each output tuple. No structural conversion is attempted on the arguments. If all inputs are non-iterable, a list containing a single |tuple| will be returned. Parameters ---------- \*args Arbitrary number of arbitrary mix of iterable and non-iterable objects to be packed into tuples. Returns ------- tups |list| of |tuple| -- Number of tuples returned is equal to the length of the iterables passed in `*args` Raises ------ ~exceptions.ValueError If any iterable objects are of different lengths """ # Imports import numpy as np # Debug flag _DEBUG = False # Marker value for non-iterable items NOT_ITER = -1 # Uninitialized test value UNINIT_VAL = -1 # Print the input if in debug mode if _DEBUG: # pragma: no cover print("args = {0}".format(args)) # Non-iterable subclass of str class StrNoIter(str): """ Non-iterable subclass of |str|. """ def __iter__(self): raise NotImplementedError("Non-iterable string") ## end def __iter__ ## end class StrNoIter # Re-wrap input arguments with non-iterable strings if required mod_args = [(StrNoIter(a) if isinstance(a, str) else a) for a in args] # Determine the length or non-iterable status of each item and store # the maximum value (depends on NOT_ITER < 0) iterlens = [(len(a) if iterable(a) else NOT_ITER) for a in mod_args] maxiter = max(iterlens) # Check to ensure all iterables are the same length if not all(map(lambda v: v in (NOT_ITER, maxiter), iterlens)): raise ValueError("All iterable items must be of equal length") ## end if # If everything is non-iterable, just return the args tuple wrapped in # a list (as above, depends on NOT_ITER < 0) if maxiter == NOT_ITER: return [args] ## end if # Swap any non-iterables for a suitable length repeat, and zip to # tuples for return tups = list(zip(*[(np.repeat(a, maxiter) if l == NOT_ITER else a) for (a,l) in zip(mod_args, iterlens)])) # Dump the resulting tuples, if in debug mode if _DEBUG: # pragma: no cover print("tups = {0}".format(tups)) ## end if # Return the tuples return tups
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:check_geom; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:c1; 5, identifier:a1; 6, identifier:c2; 7, identifier:a2; 8, default_parameter; 8, 9; 8, 10; 9, identifier:tol; 10, attribute; 10, 11; 10, 12; 11, identifier:_DEF; 12, identifier:XYZ_COORD_MATCH_TOL; 13, block; 13, 14; 13, 16; 13, 17; 13, 24; 13, 29; 13, 38; 13, 39; 13, 43; 13, 44; 13, 62; 13, 63; 13, 81; 13, 82; 13, 83; 13, 84; 13, 102; 13, 103; 13, 121; 13, 122; 13, 123; 13, 144; 13, 145; 13, 166; 13, 167; 13, 168; 13, 169; 13, 198; 13, 199; 13, 200; 13, 221; 13, 250; 13, 251; 13, 252; 13, 253; 13, 285; 13, 286; 13, 318; 13, 319; 13, 329; 13, 330; 13, 359; 13, 360; 14, expression_statement; 14, 15; 15, comment; 16, comment; 17, import_from_statement; 17, 18; 17, 22; 18, relative_import; 18, 19; 18, 20; 19, import_prefix; 20, dotted_name; 20, 21; 21, identifier:const; 22, dotted_name; 22, 23; 23, identifier:atom_num; 24, import_statement; 24, 25; 25, aliased_import; 25, 26; 25, 28; 26, dotted_name; 26, 27; 27, identifier:numpy; 28, identifier:np; 29, import_from_statement; 29, 30; 29, 34; 30, relative_import; 30, 31; 30, 32; 31, import_prefix; 32, dotted_name; 32, 33; 33, identifier:const; 34, aliased_import; 34, 35; 34, 37; 35, dotted_name; 35, 36; 36, identifier:EnumCheckGeomMismatch; 37, identifier:ECGM; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:match; 42, True; 43, comment; 44, if_statement; 44, 45; 44, 54; 44, 55; 45, not_operator; 45, 46; 46, comparison_operator:==; 46, 47; 46, 53; 47, call; 47, 48; 47, 49; 48, identifier:len; 49, argument_list; 49, 50; 50, attribute; 50, 51; 50, 52; 51, identifier:c1; 52, identifier:shape; 53, integer:1; 54, comment; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:ValueError; 59, argument_list; 59, 60; 60, parenthesized_expression; 60, 61; 61, string:"'c1' is not a vector."; 62, comment; 63, if_statement; 63, 64; 63, 73; 63, 74; 64, not_operator; 64, 65; 65, comparison_operator:==; 65, 66; 65, 72; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, attribute; 69, 70; 69, 71; 70, identifier:c2; 71, identifier:shape; 72, integer:1; 73, comment; 74, block; 74, 75; 75, raise_statement; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:ValueError; 78, argument_list; 78, 79; 79, parenthesized_expression; 79, 80; 80, string:"'c2' is not a vector."; 81, comment; 82, comment; 83, comment; 84, if_statement; 84, 85; 84, 94; 84, 95; 85, not_operator; 85, 86; 86, comparison_operator:==; 86, 87; 86, 93; 87, call; 87, 88; 87, 89; 88, identifier:len; 89, argument_list; 89, 90; 90, attribute; 90, 91; 90, 92; 91, identifier:a1; 92, identifier:shape; 93, integer:1; 94, comment; 95, block; 95, 96; 96, raise_statement; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:ValueError; 99, argument_list; 99, 100; 100, parenthesized_expression; 100, 101; 101, string:"'a1' is not a simple list."; 102, comment; 103, if_statement; 103, 104; 103, 113; 103, 114; 104, not_operator; 104, 105; 105, comparison_operator:==; 105, 106; 105, 112; 106, call; 106, 107; 106, 108; 107, identifier:len; 108, argument_list; 108, 109; 109, attribute; 109, 110; 109, 111; 110, identifier:a2; 111, identifier:shape; 112, integer:1; 113, comment; 114, block; 114, 115; 115, raise_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:ValueError; 118, argument_list; 118, 119; 119, parenthesized_expression; 119, 120; 120, string:"'a2' is not a simple list."; 121, comment; 122, comment; 123, if_statement; 123, 124; 123, 138; 124, not_operator; 124, 125; 125, comparison_operator:==; 125, 126; 125, 131; 126, subscript; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:c1; 129, identifier:shape; 130, integer:0; 131, binary_operator:*; 131, 132; 131, 133; 132, integer:3; 133, subscript; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:a1; 136, identifier:shape; 137, integer:0; 138, block; 138, 139; 139, raise_statement; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:ValueError; 142, argument_list; 142, 143; 143, string:"len(c1) != 3*len(a1)"; 144, comment; 145, if_statement; 145, 146; 145, 160; 146, not_operator; 146, 147; 147, comparison_operator:==; 147, 148; 147, 153; 148, subscript; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:c2; 151, identifier:shape; 152, integer:0; 153, binary_operator:*; 153, 154; 153, 155; 154, integer:3; 155, subscript; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:a2; 158, identifier:shape; 159, integer:0; 160, block; 160, 161; 161, raise_statement; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:ValueError; 164, argument_list; 164, 165; 165, string:"len(c2) != 3*len(a2)"; 166, comment; 167, comment; 168, comment; 169, if_statement; 169, 170; 169, 182; 170, not_operator; 170, 171; 171, comparison_operator:==; 171, 172; 171, 177; 172, subscript; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:c1; 175, identifier:shape; 176, integer:0; 177, subscript; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:c2; 180, identifier:shape; 181, integer:0; 182, block; 182, 183; 182, 187; 182, 193; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:match; 186, False; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:fail_type; 190, attribute; 190, 191; 190, 192; 191, identifier:ECGM; 192, identifier:DIMENSION; 193, return_statement; 193, 194; 194, expression_list; 194, 195; 194, 196; 194, 197; 195, identifier:match; 196, identifier:fail_type; 197, None; 198, comment; 199, comment; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:fail_loc; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:np; 206, identifier:less_equal; 207, argument_list; 207, 208; 207, 220; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:np; 211, identifier:abs; 212, argument_list; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:np; 216, identifier:subtract; 217, argument_list; 217, 218; 217, 219; 218, identifier:c1; 219, identifier:c2; 220, identifier:tol; 221, if_statement; 221, 222; 221, 232; 221, 233; 221, 234; 222, comparison_operator:!=; 222, 223; 222, 227; 223, call; 223, 224; 223, 225; 224, identifier:sum; 225, argument_list; 225, 226; 226, identifier:fail_loc; 227, subscript; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:c2; 230, identifier:shape; 231, integer:0; 232, comment; 233, comment; 234, block; 234, 235; 234, 239; 234, 245; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:match; 238, False; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:fail_type; 242, attribute; 242, 243; 242, 244; 243, identifier:ECGM; 244, identifier:COORDS; 245, return_statement; 245, 246; 246, expression_list; 246, 247; 246, 248; 246, 249; 247, identifier:match; 248, identifier:fail_type; 249, identifier:fail_loc; 250, comment; 251, comment; 252, comment; 253, if_statement; 253, 254; 253, 268; 253, 269; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:np; 257, identifier:issubdtype; 258, argument_list; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:a1; 261, identifier:dtype; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:np; 265, identifier:dtype; 266, argument_list; 266, 267; 267, string:'str'; 268, comment; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, assignment; 271, 272; 271, 273; 272, identifier:a1; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:np; 276, identifier:array; 277, argument_list; 277, 278; 278, list_comprehension; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:atom_num; 281, identifier:e; 282, for_in_clause; 282, 283; 282, 284; 283, identifier:e; 284, identifier:a1; 285, comment; 286, if_statement; 286, 287; 286, 301; 286, 302; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:np; 290, identifier:issubdtype; 291, argument_list; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:a2; 294, identifier:dtype; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:np; 298, identifier:dtype; 299, argument_list; 299, 300; 300, string:'str'; 301, comment; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:a2; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:np; 309, identifier:array; 310, argument_list; 310, 311; 311, list_comprehension; 311, 312; 311, 315; 312, subscript; 312, 313; 312, 314; 313, identifier:atom_num; 314, identifier:e; 315, for_in_clause; 315, 316; 315, 317; 316, identifier:e; 317, identifier:a2; 318, comment; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:fail_loc; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:np; 325, identifier:equal; 326, argument_list; 326, 327; 326, 328; 327, identifier:a1; 328, identifier:a2; 329, comment; 330, if_statement; 330, 331; 330, 341; 330, 342; 330, 343; 331, comparison_operator:!=; 331, 332; 331, 336; 332, call; 332, 333; 332, 334; 333, identifier:sum; 334, argument_list; 334, 335; 335, identifier:fail_loc; 336, subscript; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:a2; 339, identifier:shape; 340, integer:0; 341, comment; 342, comment; 343, block; 343, 344; 343, 348; 343, 354; 344, expression_statement; 344, 345; 345, assignment; 345, 346; 345, 347; 346, identifier:match; 347, False; 348, expression_statement; 348, 349; 349, assignment; 349, 350; 349, 351; 350, identifier:fail_type; 351, attribute; 351, 352; 351, 353; 352, identifier:ECGM; 353, identifier:ATOMS; 354, return_statement; 354, 355; 355, expression_list; 355, 356; 355, 357; 355, 358; 356, identifier:match; 357, identifier:fail_type; 358, identifier:fail_loc; 359, comment; 360, return_statement; 360, 361; 361, expression_list; 361, 362; 361, 363; 361, 364; 362, identifier:match; 363, None; 364, None
def check_geom(c1, a1, c2, a2, tol=_DEF.XYZ_COORD_MATCH_TOL): """ Check for consistency of two geometries and atom symbol lists Cartesian coordinates are considered consistent with the input coords if each component matches to within `tol`. If coords or atoms vectors are passed that are of mismatched lengths, a |False| value is returned. Both coords vectors must be three times the length of the atoms vectors or a :exc:`~exceptions.ValueError` is raised. Parameters ---------- c1 length-3N |npfloat_| -- Vector of first set of stacked 'lab-frame' Cartesian coordinates a1 length-N |str| or |int| -- Vector of first set of atom symbols or atomic numbers c2 length-3N |npfloat_| -- Vector of second set of stacked 'lab-frame' Cartesian coordinates a2 length-N |str| or |int| -- Vector of second set of atom symbols or atomic numbers tol |float|, optional -- Tolerance for acceptable deviation of each geometry coordinate from that in the reference instance to still be considered matching. Default value is specified by :attr:`opan.const.DEF.XYZ_COORD_MATCH_TOL`) Returns ------- match |bool| -- Whether input coords and atoms match (|True|) or not (|False|) fail_type :class:`~opan.const.EnumCheckGeomMismatch` or |None| -- Type of check failure If `match` == |True|: Returns as |None| If `match` == |False|: An :class:`~opan.const.EnumCheckGeomMismatch` value indicating the reason for the failed match: :attr:`~opan.const.EnumCheckGeomMismatch.DIMENSION` -- Mismatch in geometry size (number of atoms) :attr:`~opan.const.EnumCheckGeomMismatch.COORDS` -- Mismatch in one or more coordinates :attr:`~opan.const.EnumCheckGeomMismatch.ATOMS` -- Mismatch in one or more atoms fail_loc length-3N |bool| or length-N |bool| or |None| -- Mismatched elements If `match` == |True|: Returns as |None| If `match` == |False|: For "array-level" problems such as a dimension mismatch, a |None| value is returned. For "element-level" problems, a vector is returned indicating positions of mismatch in either `coords` or `atoms`, depending on the value of `fail_type`. |True| elements indicate **MATCHING** values |False| elements mark **MISMATCHES** Raises ------ ~exceptions.ValueError If a pair of coords & atoms array lengths is inconsistent: .. code-block:: python if len(c1) != 3 * len(a1) or len(c2) != 3 * len(a2): raise ValueError(...) """ # Import(s) from ..const import atom_num import numpy as np from ..const import EnumCheckGeomMismatch as ECGM # Initialize return value to success condition match = True #** Check coords for suitable shape. Assume 1-D np.arrays. if not len(c1.shape) == 1: # Cannot coerce to vector; complain. raise ValueError(("'c1' is not a vector.")) ## end if if not len(c2.shape) == 1: # Cannot coerce to vector; complain. raise ValueError(("'c2' is not a vector.")) ## end if #** Check atoms for suitable shape. Assume lists of strings, so # convert to np.array to check. if not len(a1.shape) == 1: # Not a vector; complain raise ValueError(("'a1' is not a simple list.")) ## end if if not len(a2.shape) == 1: # Not a vector; complain. raise ValueError(("'a2' is not a simple list.")) ## end if #** Confirm proper lengths of coords vs atoms if not c1.shape[0] == 3 * a1.shape[0]: raise ValueError("len(c1) != 3*len(a1)") ## end if if not c2.shape[0] == 3 * a2.shape[0]: raise ValueError("len(c2) != 3*len(a2)") ## end if #** Confirm matching lengths of coords and atoms w/corresponding # objects among the two geometries if not c1.shape[0] == c2.shape[0]: match = False fail_type = ECGM.DIMENSION return match, fail_type, None ## end if #** Element-wise check for geometry match to within 'tol' fail_loc = np.less_equal(np.abs(np.subtract(c1,c2)), tol) if sum(fail_loc) != c2.shape[0]: # Count of matching coordinates should equal the number of # coordinates. If not, complain with 'coord_mismatch' fail type. match = False fail_type = ECGM.COORDS return match, fail_type, fail_loc ## end if #** Element-wise check for atoms match. Quietly convert both input and # instance atom arrays to atom_nums to allow np.equals comparison. if np.issubdtype(a1.dtype, np.dtype('str')): # Presume atomic symbol data and attempt conversion a1 = np.array([atom_num[e] for e in a1]) ## end if if np.issubdtype(a2.dtype, np.dtype('str')): # Presume atomic symbol data and attempt conversion a2 = np.array([atom_num[e] for e in a2]) ## end if fail_loc = np.equal(a1, a2) #** Perform the test to ensure all atoms match. if sum(fail_loc) != a2.shape[0]: # Count of matching atoms should equal number of atoms. If not, # complain with the 'atom_mismatch' fail type. match = False fail_type = ECGM.ATOMS return match, fail_type, fail_loc #** If reached here, all tests passed; return success. return match, None, None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:assert_npfloatarray; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:obj; 5, identifier:varname; 6, identifier:desc; 7, identifier:exc; 8, identifier:tc; 9, identifier:errsrc; 10, block; 10, 11; 10, 13; 10, 14; 10, 19; 10, 20; 10, 59; 10, 60; 10, 61; 10, 104; 10, 105; 10, 106; 11, expression_statement; 11, 12; 12, comment; 13, comment; 14, import_statement; 14, 15; 15, aliased_import; 15, 16; 15, 18; 16, dotted_name; 16, 17; 17, identifier:numpy; 18, identifier:np; 19, comment; 20, if_statement; 20, 21; 20, 24; 20, 29; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:varname; 23, None; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:var; 28, identifier:obj; 29, else_clause; 29, 30; 29, 31; 30, comment; 31, block; 31, 32; 31, 58; 32, try_statement; 32, 33; 32, 42; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:var; 37, call; 37, 38; 37, 39; 38, identifier:getattr; 39, argument_list; 39, 40; 39, 41; 40, identifier:obj; 41, identifier:varname; 42, except_clause; 42, 43; 42, 44; 43, identifier:AttributeError; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:exc; 48, argument_list; 48, 49; 48, 50; 48, 57; 49, identifier:tc; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, string:"Attribute '{0}' not defined in '{1}'"; 53, identifier:format; 54, argument_list; 54, 55; 54, 56; 55, identifier:varname; 56, identifier:obj; 57, identifier:errsrc; 58, comment; 59, comment; 60, comment; 61, try_statement; 61, 62; 61, 69; 61, 84; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:dt; 66, attribute; 66, 67; 66, 68; 67, identifier:var; 68, identifier:dtype; 69, except_clause; 69, 70; 69, 71; 70, identifier:AttributeError; 71, block; 71, 72; 72, raise_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:exc; 75, argument_list; 75, 76; 75, 77; 75, 83; 76, identifier:tc; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, string:"'{0}' is not an np.array (lacks a 'dtype' member)"; 80, identifier:format; 81, argument_list; 81, 82; 82, identifier:desc; 83, identifier:errsrc; 84, else_clause; 84, 85; 85, block; 85, 86; 86, if_statement; 86, 87; 86, 91; 87, not_operator; 87, 88; 88, attribute; 88, 89; 88, 90; 89, identifier:var; 90, identifier:shape; 91, block; 91, 92; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:exc; 95, argument_list; 95, 96; 95, 97; 95, 103; 96, identifier:tc; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, string:"'{0}' is not an np.array ('len(shape)' < 1)"; 100, identifier:format; 101, argument_list; 101, 102; 102, identifier:desc; 103, identifier:errsrc; 104, comment; 105, comment; 106, if_statement; 106, 107; 106, 117; 107, not_operator; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:np; 111, identifier:issubdtype; 112, argument_list; 112, 113; 112, 114; 113, identifier:dt; 114, attribute; 114, 115; 114, 116; 115, identifier:np; 116, identifier:float; 117, block; 117, 118; 118, raise_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:exc; 121, argument_list; 121, 122; 121, 123; 121, 129; 122, identifier:tc; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, string:"'{0}' is not an np.array of np.float"; 126, identifier:format; 127, argument_list; 127, 128; 128, identifier:desc; 129, identifier:errsrc
def assert_npfloatarray(obj, varname, desc, exc, tc, errsrc): """ Assert a value is an |nparray| of NumPy floats. Pass |None| to `varname` if `obj` itself is to be checked. Otherwise, `varname` is the string name of the attribute of `obj` to check. In either case, `desc` is a string description of the object to be checked, for use in raising of exceptions. Raises the exception `exc` with typecode `tc` if the indicated object is determined not to be an |nparray|, with a NumPy float dtype. Intended primarily to serve as an early check for proper implementation of subclasses of :class:`~opan.grad.SuperOpanGrad` and :class:`~opan.hess.SuperOpanHess`. Early type-checking of key attributes will hopefully avoid confusing bugs downstream. Parameters ---------- obj (arbitrary) -- Object to be checked, or object with attribute to be checked. varname |str| or |None| -- Name of the attribute of `obj` to be type-checked. |None| indicates to check `obj` itself. desc |str| -- Description of the object being checked to be used in any raised exceptions. exc Subclass of :class:`~opan.error.OpanError` to be raised on a failed typecheck. tc Typecode of `exc` to be raised on a failed typecheck. errsrc |str| -- String description of the source of the data leading to a failed typecheck. """ # Imports import numpy as np # Check for whether member or object is to be checked if varname is None: var = obj else: # Try to get the variable to be typechecked try: var = getattr(obj, varname) except AttributeError: raise exc(tc, "Attribute '{0}' not defined in '{1}'" .format(varname, obj), errsrc) ## end try ## end if # Try to pull the np dtype off of it try: dt = var.dtype except AttributeError: raise exc(tc, "'{0}' is not an np.array (lacks a 'dtype' member)" .format(desc), errsrc) else: if not var.shape: raise exc(tc, "'{0}' is not an np.array ('len(shape)' < 1)" .format(desc), errsrc) ## end try # Confirm dtype inherits from np.float if not np.issubdtype(dt, np.float): raise exc(tc, "'{0}' is not an np.array of np.float".format(desc), errsrc)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_advance_pattern_generators; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:p; 6, block; 6, 7; 6, 9; 6, 13; 6, 83; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:valid_generators; 12, list:[]; 13, for_statement; 13, 14; 13, 15; 13, 18; 14, identifier:g; 15, attribute; 15, 16; 15, 17; 16, identifier:p; 17, identifier:generators; 18, block; 18, 19; 19, for_statement; 19, 20; 19, 21; 19, 27; 19, 28; 19, 70; 20, identifier:trial; 21, call; 21, 22; 21, 23; 22, identifier:range; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:max_trials; 27, comment; 28, block; 28, 29; 28, 56; 28, 63; 29, if_statement; 29, 30; 29, 47; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:np; 33, identifier:alltrue; 34, argument_list; 34, 35; 35, list_comprehension; 35, 36; 35, 44; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:__distance_valid; 40, argument_list; 40, 41; 40, 42; 40, 43; 41, identifier:g; 42, identifier:v; 43, identifier:p; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:v; 46, identifier:valid_generators; 47, block; 47, 48; 47, 55; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:valid_generators; 52, identifier:append; 53, argument_list; 53, 54; 54, identifier:g; 55, break_statement; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:g; 60, identifier:force_new_dynamic_value; 61, argument_list; 61, 62; 62, string:'x'; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:g; 67, identifier:force_new_dynamic_value; 68, argument_list; 68, 69; 69, string:'y'; 70, else_clause; 70, 71; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:warning; 77, argument_list; 77, 78; 78, binary_operator:%; 78, 79; 78, 80; 79, string:"Unable to place pattern %s subject to given constraints"; 80, attribute; 80, 81; 80, 82; 81, identifier:g; 82, identifier:name; 83, return_statement; 83, 84; 84, identifier:valid_generators
def _advance_pattern_generators(self,p): """ Advance the parameters for each generator for this presentation. Picks a position for each generator that is accepted by __distance_valid for all combinations. Returns a new list of the generators, with some potentially omitted due to failure to meet the constraints. """ valid_generators = [] for g in p.generators: for trial in range(self.max_trials): # Generate a new position and add generator if it's ok if np.alltrue([self.__distance_valid(g,v,p) for v in valid_generators]): valid_generators.append(g) break g.force_new_dynamic_value('x') g.force_new_dynamic_value('y') else: self.warning("Unable to place pattern %s subject to given constraints" % g.name) return valid_generators
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:register; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 10; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:settings_class; 7, identifier:NoSwitcher; 8, list_splat_pattern; 8, 9; 9, identifier:simple_checks; 10, dictionary_splat_pattern; 10, 11; 11, identifier:conditions; 12, block; 12, 13; 12, 15; 12, 40; 12, 50; 12, 70; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:settings_class; 18, identifier:NoSwitcher; 19, block; 19, 20; 19, 38; 20, function_definition; 20, 21; 20, 22; 20, 24; 21, function_name:decorator; 22, parameters; 22, 23; 23, identifier:cls; 24, block; 24, 25; 24, 36; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:register; 30, argument_list; 30, 31; 30, 32; 30, 34; 31, identifier:cls; 32, list_splat; 32, 33; 33, identifier:simple_checks; 34, dictionary_splat; 34, 35; 35, identifier:conditions; 36, return_statement; 36, 37; 37, identifier:cls; 38, return_statement; 38, 39; 39, identifier:decorator; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:available_checks; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:checks; 48, identifier:keys; 49, argument_list; 50, for_statement; 50, 51; 50, 52; 50, 57; 51, identifier:condition; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:conditions; 55, identifier:keys; 56, argument_list; 57, block; 57, 58; 58, if_statement; 58, 59; 58, 62; 59, comparison_operator:not; 59, 60; 59, 61; 60, identifier:condition; 61, identifier:available_checks; 62, block; 62, 63; 63, raise_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:InvalidCondition; 66, argument_list; 66, 67; 67, binary_operator:%; 67, 68; 67, 69; 68, string:'There is no check for the condition "%s"'; 69, identifier:condition; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 77; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:_registry; 76, identifier:append; 77, argument_list; 77, 78; 78, tuple; 78, 79; 78, 80; 78, 81; 79, identifier:settings_class; 80, identifier:simple_checks; 81, identifier:conditions
def register(self, settings_class=NoSwitcher, *simple_checks, **conditions): """ Register a settings class with the switcher. Can be passed the settings class to register or be used as a decorator. :param settings_class: The class to register with the provided conditions. :param *simple_checks: A list of conditions for using the settings class. If any of the values are falsy, the class will not be used. If any of the values are callable, they will be called before evaluating. :param **conditions: Values to check. The key specifies which of the check functions (registered with ``add_check``) to use; the value is passed to the check function. """ if settings_class is NoSwitcher: def decorator(cls): self.register(cls, *simple_checks, **conditions) return cls return decorator available_checks = self.checks.keys() for condition in conditions.keys(): if condition not in available_checks: raise InvalidCondition( 'There is no check for the condition "%s"' % condition) self._registry.append((settings_class, simple_checks, conditions))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:_load_data; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:atom_syms; 6, identifier:coords; 7, default_parameter; 7, 8; 7, 9; 8, identifier:bohrs; 9, True; 10, block; 10, 11; 10, 13; 10, 14; 10, 19; 10, 28; 10, 35; 10, 36; 10, 53; 10, 54; 10, 55; 10, 71; 10, 72; 10, 88; 10, 89; 10, 110; 10, 111; 10, 112; 10, 178; 10, 179; 10, 180; 10, 198; 10, 199; 10, 200; 10, 201; 10, 211; 10, 217; 10, 225; 10, 239; 10, 247; 10, 248; 10, 266; 10, 267; 11, expression_statement; 11, 12; 12, comment; 13, comment; 14, import_statement; 14, 15; 15, aliased_import; 15, 16; 15, 18; 16, dotted_name; 16, 17; 17, identifier:numpy; 18, identifier:np; 19, import_from_statement; 19, 20; 19, 24; 19, 26; 20, relative_import; 20, 21; 20, 22; 21, import_prefix; 22, dotted_name; 22, 23; 23, identifier:const; 24, dotted_name; 24, 25; 25, identifier:atom_num; 26, dotted_name; 26, 27; 27, identifier:PHYS; 28, import_from_statement; 28, 29; 28, 33; 29, relative_import; 29, 30; 29, 31; 30, import_prefix; 31, dotted_name; 31, 32; 32, identifier:error; 33, dotted_name; 33, 34; 34, identifier:XYZError; 35, comment; 36, if_statement; 36, 37; 36, 43; 37, comparison_operator:in; 37, 38; 37, 39; 38, string:'geoms'; 39, call; 39, 40; 39, 41; 40, identifier:dir; 41, argument_list; 41, 42; 42, identifier:self; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:XYZError; 47, argument_list; 47, 48; 47, 51; 47, 52; 48, attribute; 48, 49; 48, 50; 49, identifier:XYZError; 50, identifier:OVERWRITE; 51, string:"Cannot overwrite contents of existing OpanXYZ"; 52, string:""; 53, comment; 54, comment; 55, if_statement; 55, 56; 55, 65; 56, not_operator; 56, 57; 57, comparison_operator:==; 57, 58; 57, 64; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, attribute; 61, 62; 61, 63; 62, identifier:coords; 63, identifier:shape; 64, integer:1; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:ValueError; 69, argument_list; 69, 70; 70, string:"Coordinates are not a vector"; 71, comment; 72, if_statement; 72, 73; 72, 82; 73, not_operator; 73, 74; 74, comparison_operator:==; 74, 75; 74, 81; 75, call; 75, 76; 75, 77; 76, identifier:len; 77, argument_list; 77, 78; 78, attribute; 78, 79; 78, 80; 79, identifier:atom_syms; 80, identifier:shape; 81, integer:1; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:ValueError; 86, argument_list; 86, 87; 87, string:"Atom symbols are not a simple list"; 88, comment; 89, if_statement; 89, 90; 89, 104; 90, not_operator; 90, 91; 91, comparison_operator:==; 91, 92; 91, 97; 92, subscript; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:coords; 95, identifier:shape; 96, integer:0; 97, binary_operator:*; 97, 98; 97, 99; 98, integer:3; 99, subscript; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:atom_syms; 102, identifier:shape; 103, integer:0; 104, block; 104, 105; 105, raise_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:ValueError; 108, argument_list; 108, 109; 109, string:"len(coords) != 3 * len(atom_syms)"; 110, comment; 111, comment; 112, if_statement; 112, 113; 112, 137; 112, 138; 113, not_operator; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:all; 116, generator_expression; 116, 117; 116, 127; 117, parenthesized_expression; 117, 118; 118, comparison_operator:in; 118, 119; 118, 126; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:atom_syms; 123, identifier:i; 124, identifier:upper; 125, argument_list; 126, identifier:atom_num; 127, for_in_clause; 127, 128; 127, 129; 128, identifier:i; 129, call; 129, 130; 129, 131; 130, identifier:range; 131, argument_list; 131, 132; 132, subscript; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:atom_syms; 135, identifier:shape; 136, integer:0; 137, comment; 138, block; 138, 139; 139, raise_statement; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:ValueError; 142, argument_list; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, string:"Invalid atoms specified: {0}"; 146, identifier:format; 147, argument_list; 147, 148; 148, list_comprehension; 148, 149; 148, 154; 149, tuple; 149, 150; 149, 151; 150, identifier:j; 151, subscript; 151, 152; 151, 153; 152, identifier:atom_syms; 153, identifier:j; 154, for_in_clause; 154, 155; 154, 156; 155, identifier:j; 156, generator_expression; 156, 157; 156, 158; 156, 175; 157, identifier:i; 158, for_in_clause; 158, 159; 158, 162; 159, tuple_pattern; 159, 160; 159, 161; 160, identifier:i; 161, identifier:valid; 162, call; 162, 163; 162, 164; 163, identifier:enumerate; 164, argument_list; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:map; 167, argument_list; 167, 168; 167, 174; 168, lambda; 168, 169; 168, 171; 169, lambda_parameters; 169, 170; 170, identifier:k; 171, comparison_operator:in; 171, 172; 171, 173; 172, identifier:k; 173, identifier:atom_num; 174, identifier:atom_syms; 175, if_clause; 175, 176; 176, not_operator; 176, 177; 177, identifier:valid; 178, comment; 179, comment; 180, if_statement; 180, 181; 180, 192; 181, not_operator; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:all; 184, argument_list; 184, 185; 185, call; 185, 186; 185, 187; 186, identifier:map; 187, argument_list; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:np; 190, identifier:isreal; 191, identifier:coords; 192, block; 192, 193; 193, raise_statement; 193, 194; 194, call; 194, 195; 194, 196; 195, identifier:ValueError; 196, argument_list; 196, 197; 197, string:"All coordinates must be real numeric"; 198, comment; 199, comment; 200, comment; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:num_atoms; 206, subscript; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:atom_syms; 209, identifier:shape; 210, integer:0; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:num_geoms; 216, integer:1; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:in_str; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:LOAD_DATA_FLAG; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:descs; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:np; 233, identifier:array; 234, argument_list; 234, 235; 235, list:[self.LOAD_DATA_FLAG]; 235, 236; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:LOAD_DATA_FLAG; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:XYZ_path; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:LOAD_DATA_FLAG; 247, comment; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:self; 252, identifier:atom_syms; 253, call; 253, 254; 253, 255; 254, identifier:list; 255, argument_list; 255, 256; 256, call; 256, 257; 256, 258; 257, identifier:map; 258, argument_list; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:str; 261, identifier:upper; 262, call; 262, 263; 262, 264; 263, identifier:list; 264, argument_list; 264, 265; 265, identifier:atom_syms; 266, comment; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:self; 271, identifier:geoms; 272, list:[coords / (1.0 if bohrs else PHYS.ANG_PER_BOHR)]; 272, 273; 273, binary_operator:/; 273, 274; 273, 275; 274, identifier:coords; 275, parenthesized_expression; 275, 276; 276, conditional_expression:if; 276, 277; 276, 278; 276, 279; 277, float:1.0; 278, identifier:bohrs; 279, attribute; 279, 280; 279, 281; 280, identifier:PHYS; 281, identifier:ANG_PER_BOHR
def _load_data(self, atom_syms, coords, bohrs=True): """ Internal function for making XYZ object from explicit geom data. Parameters ---------- atom_syms Squeezes to array of N |str| -- Element symbols for the XYZ. Must be valid elements as defined in the keys of :data:`const.atom_num <opan.const.atom_num>`. coords Squeezes to array of 3N |npfloat_| castables -- Coordinates for the geometry. bohrs |bool|, optional -- Units of coordinates (default |True|) Raises ------ ~opan.XYZError (typecode :attr:`~opan.error.XYZError.OVERWRITE`) If :class:`ORCA_XYZ` object has already been initialized. ~exceptions.ValueError If atom_syms & coords dimensions are incompatible ~exceptions.ValueError If type of `atom_syms` and/or `coords` is invalid """ # Imports import numpy as np from .const import atom_num, PHYS from .error import XYZError # Gripe if already initialized if 'geoms' in dir(self): raise XYZError(XYZError.OVERWRITE, "Cannot overwrite contents of existing OpanXYZ", "") ## end if # Check and store dimensions if not len(coords.shape) == 1: raise ValueError("Coordinates are not a vector") ## end if if not len(atom_syms.shape) == 1: raise ValueError("Atom symbols are not a simple list") ## end if if not coords.shape[0] == 3 * atom_syms.shape[0]: raise ValueError("len(coords) != 3 * len(atom_syms)") ## end if # Proof the atoms list if not all( (atom_syms[i].upper() in atom_num) for i in range(atom_syms.shape[0]) ): # Invalid atoms specified raise ValueError("Invalid atoms specified: {0}".format( [(j, atom_syms[j]) for j in (i for (i, valid) in enumerate(map(lambda k: k in atom_num, atom_syms)) if not valid ) ] )) ## end if # Ensure the geometry is all numeric if not all(map(np.isreal, coords)): raise ValueError("All coordinates must be real numeric") ## end if # Store the number of atoms. Only one geometry. Standard string # content for things only relevant to file load. self.num_atoms = atom_syms.shape[0] self.num_geoms = 1 self.in_str = self.LOAD_DATA_FLAG self.descs = np.array([self.LOAD_DATA_FLAG]) self.XYZ_path = self.LOAD_DATA_FLAG # Store the atoms as vector self.atom_syms = list(map(str.upper, list(atom_syms))) # Store the single geometry by bracketing with an array self.geoms = [coords / (1.0 if bohrs else PHYS.ANG_PER_BOHR)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:dist_single; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:g_num; 6, identifier:at_1; 7, identifier:at_2; 8, block; 8, 9; 8, 11; 8, 12; 8, 17; 8, 24; 8, 33; 8, 34; 8, 35; 8, 36; 8, 37; 8, 60; 8, 61; 8, 84; 8, 85; 8, 86; 8, 87; 8, 88; 8, 103; 8, 118; 8, 119; 8, 120; 8, 153; 8, 154; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, import_statement; 12, 13; 13, aliased_import; 13, 14; 13, 16; 14, dotted_name; 14, 15; 15, identifier:numpy; 16, identifier:np; 17, import_from_statement; 17, 18; 17, 20; 18, dotted_name; 18, 19; 19, identifier:scipy; 20, aliased_import; 20, 21; 20, 23; 21, dotted_name; 21, 22; 22, identifier:linalg; 23, identifier:spla; 24, import_from_statement; 24, 25; 24, 29; 25, relative_import; 25, 26; 25, 27; 26, import_prefix; 27, dotted_name; 27, 28; 28, identifier:utils; 29, aliased_import; 29, 30; 29, 32; 30, dotted_name; 30, 31; 31, identifier:safe_cast; 32, identifier:scast; 33, comment; 34, comment; 35, comment; 36, comment; 37, if_statement; 37, 38; 37, 49; 38, not_operator; 38, 39; 39, parenthesized_expression; 39, 40; 40, comparison_operator:<=; 40, 41; 40, 45; 40, 46; 41, unary_operator:-; 41, 42; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:num_atoms; 45, identifier:at_1; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:num_atoms; 49, block; 49, 50; 50, raise_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:IndexError; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, string:"Invalid index for 'at_1' ({0})"; 57, identifier:format; 58, argument_list; 58, 59; 59, identifier:at_1; 60, comment; 61, if_statement; 61, 62; 61, 73; 62, not_operator; 62, 63; 63, parenthesized_expression; 63, 64; 64, comparison_operator:<=; 64, 65; 64, 69; 64, 70; 65, unary_operator:-; 65, 66; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:num_atoms; 69, identifier:at_2; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:num_atoms; 73, block; 73, 74; 74, raise_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:IndexError; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, string:"Invalid index for 'at_2' ({0})"; 81, identifier:format; 82, argument_list; 82, 83; 83, identifier:at_2; 84, comment; 85, comment; 86, comment; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:at_1; 91, call; 91, 92; 91, 93; 92, identifier:scast; 93, argument_list; 93, 94; 93, 100; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:np; 97, identifier:floor; 98, argument_list; 98, 99; 99, identifier:at_1; 100, attribute; 100, 101; 100, 102; 101, identifier:np; 102, identifier:int_; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:at_2; 106, call; 106, 107; 106, 108; 107, identifier:scast; 108, argument_list; 108, 109; 108, 115; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:np; 112, identifier:floor; 113, argument_list; 113, 114; 114, identifier:at_2; 115, attribute; 115, 116; 115, 117; 116, identifier:np; 117, identifier:int_; 118, comment; 119, comment; 120, if_statement; 120, 121; 120, 124; 120, 129; 121, comparison_operator:==; 121, 122; 121, 123; 122, identifier:at_1; 123, identifier:at_2; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:dist; 128, float:0.0; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:dist; 134, call; 134, 135; 134, 136; 135, identifier:scast; 136, argument_list; 136, 137; 136, 150; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:spla; 140, identifier:norm; 141, argument_list; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:displ_single; 146, argument_list; 146, 147; 146, 148; 146, 149; 147, identifier:g_num; 148, identifier:at_1; 149, identifier:at_2; 150, attribute; 150, 151; 150, 152; 151, identifier:np; 152, identifier:float_; 153, comment; 154, return_statement; 154, 155; 155, identifier:dist
def dist_single(self, g_num, at_1, at_2): """ Distance between two atoms. Parameters ---------- g_num |int| -- Index of the desired geometry at_1 |int| -- Index of the first atom at_2 |int| -- Index of the second atom Returns ------- dist |npfloat_| -- Distance in Bohrs between `at_1` and `at_2` from geometry `g_num` Raises ------ ~exceptions.IndexError If an invalid (out-of-range) `g_num` or `at_#` is provided """ # Import used math library function(s) import numpy as np from scipy import linalg as spla from .utils import safe_cast as scast # The below errors are explicitly thrown since values are multiplied by # three when they are used as an index and thus give non-intuitive # errors in subsequent code. # Complain if at_1 is invalid if not (-self.num_atoms <= at_1 < self.num_atoms): raise IndexError("Invalid index for 'at_1' ({0})".format(at_1)) # Complain if at_2 is invalid if not (-self.num_atoms <= at_2 < self.num_atoms): raise IndexError("Invalid index for 'at_2' ({0})".format(at_2)) # Should never be necessary (save for badly erroneous calling code), # but coerce at_1 and at_2 to their floor() values. This is again # needed since they are multiplied by three in the index expresssions # below, and can cause funny behavior when truncated by the indexing at_1 = scast(np.floor(at_1), np.int_) at_2 = scast(np.floor(at_2), np.int_) # Calculate the interatomic distance and return. Return identically # zero if the indices are equal if at_1 == at_2: dist = 0.0 else: dist = scast( spla.norm(self.displ_single(g_num, at_1, at_2)), np.float_) ## end if return dist
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:dist_iter; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:g_nums; 6, identifier:ats_1; 7, identifier:ats_2; 8, default_parameter; 8, 9; 8, 10; 9, identifier:invalid_error; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 20; 11, 27; 11, 28; 11, 62; 11, 63; 11, 64; 11, 75; 11, 76; 11, 84; 11, 85; 11, 94; 11, 95; 11, 96; 11, 97; 11, 98; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, import_statement; 15, 16; 16, aliased_import; 16, 17; 16, 19; 17, dotted_name; 17, 18; 18, identifier:numpy; 19, identifier:np; 20, import_from_statement; 20, 21; 20, 25; 21, relative_import; 21, 22; 21, 23; 22, import_prefix; 23, dotted_name; 23, 24; 24, identifier:utils; 25, dotted_name; 25, 26; 26, identifier:pack_tups; 27, comment; 28, if_statement; 28, 29; 28, 30; 28, 31; 29, identifier:_DEBUG; 30, comment; 31, block; 31, 32; 31, 42; 31, 52; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:print; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, string:"g_nums = {0}"; 39, identifier:format; 40, argument_list; 40, 41; 41, identifier:g_nums; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:print; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, string:"ats_1 = {0}"; 49, identifier:format; 50, argument_list; 50, 51; 51, identifier:ats_1; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:print; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:"ats_2 = {0}"; 59, identifier:format; 60, argument_list; 60, 61; 61, identifier:ats_2; 62, comment; 63, comment; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:arglist; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_none_subst; 71, argument_list; 71, 72; 71, 73; 71, 74; 72, identifier:g_nums; 73, identifier:ats_1; 74, identifier:ats_2; 75, comment; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:tups; 79, call; 79, 80; 79, 81; 80, identifier:pack_tups; 81, argument_list; 81, 82; 82, list_splat; 82, 83; 83, identifier:arglist; 84, comment; 85, if_statement; 85, 86; 85, 87; 85, 88; 86, identifier:_DEBUG; 87, comment; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:print; 92, argument_list; 92, 93; 93, identifier:tups; 94, comment; 95, comment; 96, comment; 97, comment; 98, for_statement; 98, 99; 98, 100; 98, 101; 99, identifier:tup; 100, identifier:tups; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, yield; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:_iter_return; 108, argument_list; 108, 109; 108, 110; 108, 113; 109, identifier:tup; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:dist_single; 113, identifier:invalid_error
def dist_iter(self, g_nums, ats_1, ats_2, invalid_error=False): """ Iterator over selected interatomic distances. Distances are in Bohrs as with :meth:`dist_single`. See `above <toc-generators_>`_ for more information on calling options. Parameters ---------- g_nums |int| or length-R iterable |int| or |None| -- Index/indices of the desired geometry/geometries ats_1 |int| or iterable |int| or |None| -- Index/indices of the first atom(s) ats_2 |int| or iterable |int| or |None| -- Index/indices of the second atom(s) invalid_error |bool|, optional -- If |False| (the default), |None| values are returned for results corresponding to invalid indices. If |True|, exceptions are raised per normal. Yields ------ dist |npfloat_| -- Interatomic distance in Bohrs between each atom pair of `ats_1` and `ats_2` from the corresponding geometries of `g_nums`. Raises ------ ~exceptions.IndexError If an invalid (out-of-range) `g_num` or `at_#` is provided. ~exceptions.ValueError If all iterable objects are not the same length. """ # Imports import numpy as np from .utils import pack_tups # Print the function inputs if debug mode is on if _DEBUG: # pragma: no cover print("g_nums = {0}".format(g_nums)) print("ats_1 = {0}".format(ats_1)) print("ats_2 = {0}".format(ats_2)) ## end if # Perform the None substitution arglist = self._none_subst(g_nums, ats_1, ats_2) # Expand/pack the tuples from the inputs tups = pack_tups(*arglist) # Dump the results if debug mode is on if _DEBUG: # pragma: no cover print(tups) ## end if # Construct the generator using the packed tuples. If 'None' expansion # was used, return None for any invalid indices instead of raising # an exception. for tup in tups: yield self._iter_return(tup, self.dist_single, invalid_error)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:angle_single; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:g_num; 6, identifier:at_1; 7, identifier:at_2; 8, identifier:at_3; 9, block; 9, 10; 9, 12; 9, 13; 9, 18; 9, 27; 9, 35; 9, 36; 9, 37; 9, 38; 9, 39; 9, 62; 9, 63; 9, 86; 9, 87; 9, 110; 9, 111; 9, 112; 9, 113; 9, 114; 9, 129; 9, 144; 9, 159; 9, 160; 9, 161; 9, 181; 9, 201; 9, 202; 9, 220; 9, 221; 9, 222; 9, 223; 9, 234; 9, 245; 9, 246; 9, 247; 9, 255; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, import_statement; 13, 14; 14, aliased_import; 14, 15; 14, 17; 15, dotted_name; 15, 16; 16, identifier:numpy; 17, identifier:np; 18, import_from_statement; 18, 19; 18, 23; 19, relative_import; 19, 20; 19, 21; 20, import_prefix; 21, dotted_name; 21, 22; 22, identifier:utils; 23, aliased_import; 23, 24; 23, 26; 24, dotted_name; 24, 25; 25, identifier:safe_cast; 26, identifier:scast; 27, import_from_statement; 27, 28; 27, 33; 28, relative_import; 28, 29; 28, 30; 29, import_prefix; 30, dotted_name; 30, 31; 30, 32; 31, identifier:utils; 32, identifier:vector; 33, dotted_name; 33, 34; 34, identifier:vec_angle; 35, comment; 36, comment; 37, comment; 38, comment; 39, if_statement; 39, 40; 39, 51; 40, not_operator; 40, 41; 41, parenthesized_expression; 41, 42; 42, comparison_operator:<=; 42, 43; 42, 47; 42, 48; 43, unary_operator:-; 43, 44; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:num_atoms; 47, identifier:at_1; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:num_atoms; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:IndexError; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:"Invalid index for 'at_1' ({0})"; 59, identifier:format; 60, argument_list; 60, 61; 61, identifier:at_1; 62, comment; 63, if_statement; 63, 64; 63, 75; 64, not_operator; 64, 65; 65, parenthesized_expression; 65, 66; 66, comparison_operator:<=; 66, 67; 66, 71; 66, 72; 67, unary_operator:-; 67, 68; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:num_atoms; 71, identifier:at_2; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:num_atoms; 75, block; 75, 76; 76, raise_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:IndexError; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:"Invalid index for 'at_2' ({0})"; 83, identifier:format; 84, argument_list; 84, 85; 85, identifier:at_2; 86, comment; 87, if_statement; 87, 88; 87, 99; 88, not_operator; 88, 89; 89, parenthesized_expression; 89, 90; 90, comparison_operator:<=; 90, 91; 90, 95; 90, 96; 91, unary_operator:-; 91, 92; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:num_atoms; 95, identifier:at_3; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:num_atoms; 99, block; 99, 100; 100, raise_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:IndexError; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:"Invalid index for 'at_3' ({0})"; 107, identifier:format; 108, argument_list; 108, 109; 109, identifier:at_3; 110, comment; 111, comment; 112, comment; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:at_1; 117, call; 117, 118; 117, 119; 118, identifier:scast; 119, argument_list; 119, 120; 119, 126; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:np; 123, identifier:floor; 124, argument_list; 124, 125; 125, identifier:at_1; 126, attribute; 126, 127; 126, 128; 127, identifier:np; 128, identifier:int_; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:at_2; 132, call; 132, 133; 132, 134; 133, identifier:scast; 134, argument_list; 134, 135; 134, 141; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:np; 138, identifier:floor; 139, argument_list; 139, 140; 140, identifier:at_2; 141, attribute; 141, 142; 141, 143; 142, identifier:np; 143, identifier:int_; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:at_3; 147, call; 147, 148; 147, 149; 148, identifier:scast; 149, argument_list; 149, 150; 149, 156; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:np; 153, identifier:floor; 154, argument_list; 154, 155; 155, identifier:at_3; 156, attribute; 156, 157; 156, 158; 157, identifier:np; 158, identifier:int_; 159, comment; 160, comment; 161, if_statement; 161, 162; 161, 175; 162, comparison_operator:==; 162, 163; 162, 169; 163, parenthesized_expression; 163, 164; 164, binary_operator:%; 164, 165; 164, 166; 165, identifier:at_2; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:num_atoms; 169, parenthesized_expression; 169, 170; 170, binary_operator:%; 170, 171; 170, 172; 171, identifier:at_1; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:num_atoms; 175, block; 175, 176; 176, raise_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:ValueError; 179, argument_list; 179, 180; 180, string:"'at_1' and 'at_2' must be different"; 181, if_statement; 181, 182; 181, 195; 182, comparison_operator:==; 182, 183; 182, 189; 183, parenthesized_expression; 183, 184; 184, binary_operator:%; 184, 185; 184, 186; 185, identifier:at_2; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:num_atoms; 189, parenthesized_expression; 189, 190; 190, binary_operator:%; 190, 191; 190, 192; 191, identifier:at_3; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:num_atoms; 195, block; 195, 196; 196, raise_statement; 196, 197; 197, call; 197, 198; 197, 199; 198, identifier:ValueError; 199, argument_list; 199, 200; 200, string:"'at_2' and 'at_3' must be different"; 201, comment; 202, if_statement; 202, 203; 202, 216; 202, 217; 203, comparison_operator:==; 203, 204; 203, 210; 204, parenthesized_expression; 204, 205; 205, binary_operator:%; 205, 206; 205, 207; 206, identifier:at_1; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:num_atoms; 210, parenthesized_expression; 210, 211; 211, binary_operator:%; 211, 212; 211, 213; 212, identifier:at_3; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:num_atoms; 216, comment; 217, block; 217, 218; 218, return_statement; 218, 219; 219, float:0.0; 220, comment; 221, comment; 222, comment; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:vec_2_1; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:displ_single; 230, argument_list; 230, 231; 230, 232; 230, 233; 231, identifier:g_num; 232, identifier:at_2; 233, identifier:at_1; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:vec_2_3; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:displ_single; 241, argument_list; 241, 242; 241, 243; 241, 244; 242, identifier:g_num; 243, identifier:at_2; 244, identifier:at_3; 245, comment; 246, comment; 247, expression_statement; 247, 248; 248, assignment; 248, 249; 248, 250; 249, identifier:angle; 250, call; 250, 251; 250, 252; 251, identifier:vec_angle; 252, argument_list; 252, 253; 252, 254; 253, identifier:vec_2_1; 254, identifier:vec_2_3; 255, return_statement; 255, 256; 256, identifier:angle
def angle_single(self, g_num, at_1, at_2, at_3): """ Spanning angle among three atoms. The indices `at_1` and `at_3` can be the same (yielding a trivial zero angle), but `at_2` must be different from both `at_1` and `at_3`. Parameters ---------- g_num |int| -- Index of the desired geometry at_1 |int| -- Index of the first atom at_2 |int| -- Index of the second atom at_3 |int| -- Index of the third atom Returns ------- angle |npfloat_| -- Spanning angle in degrees between `at_1`-`at_2`-`at_3`, from geometry `g_num` Raises ------ ~exceptions.IndexError If an invalid (out-of-range) `g_num` or `at_#` is provided ~exceptions.ValueError If `at_2` is equal to either `at_1` or `at_3` """ # Imports import numpy as np from .utils import safe_cast as scast from .utils.vector import vec_angle # The below errors are explicitly thrown since they are multiplied by # three when they are used as an index and thus give non-intuitive # errors in later code. # Complain if at_1 is invalid if not(-self.num_atoms <= at_1 < self.num_atoms): raise IndexError("Invalid index for 'at_1' ({0})".format(at_1)) # Complain if at_2 is invalid if not(-self.num_atoms <= at_2 < self.num_atoms): raise IndexError("Invalid index for 'at_2' ({0})".format(at_2)) # Complain if at_3 is invalid if not(-self.num_atoms <= at_3 < self.num_atoms): raise IndexError("Invalid index for 'at_3' ({0})".format(at_3)) # Should never be necessary (save for badly erroneous calling code), # but coerce the at_x to their floor() values. This is again # needed since they are multiplied by three in the index expresssions # below, and can cause funny behavior when truncated by the indexing at_1 = scast(np.floor(at_1), np.int_) at_2 = scast(np.floor(at_2), np.int_) at_3 = scast(np.floor(at_3), np.int_) # Complain if at_2 is equal to either at_1 or at_3. Must factor in # the possibility of negative indexing via modulo arithmetic. if (at_2 % self.num_atoms) == (at_1 % self.num_atoms): raise ValueError("'at_1' and 'at_2' must be different") if (at_2 % self.num_atoms) == (at_3 % self.num_atoms): raise ValueError("'at_2' and 'at_3' must be different") # Trivial return if at_1 and at_3 are the same if (at_1 % self.num_atoms) == (at_3 % self.num_atoms): # Angle is identically zero in this case return 0.0 ## end if # Store the displacement vectors from at_2 to at_1 and to at_3 # The np.float64 type should be retained through the displ_single call. vec_2_1 = self.displ_single(g_num, at_2, at_1) vec_2_3 = self.displ_single(g_num, at_2, at_3) # Compute and return the calculated angle, in degrees # v1 {dot} v2 == |v1||v2| * cos(theta) angle = vec_angle(vec_2_1, vec_2_3) return angle
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:angle_iter; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:self; 5, identifier:g_nums; 6, identifier:ats_1; 7, identifier:ats_2; 8, identifier:ats_3; 9, default_parameter; 9, 10; 9, 11; 10, identifier:invalid_error; 11, False; 12, block; 12, 13; 12, 15; 12, 16; 12, 17; 12, 18; 12, 25; 12, 26; 12, 70; 12, 71; 12, 72; 12, 84; 12, 85; 12, 93; 12, 94; 12, 103; 12, 104; 12, 105; 13, expression_statement; 13, 14; 14, comment; 15, comment; 16, comment; 17, comment; 18, import_from_statement; 18, 19; 18, 23; 19, relative_import; 19, 20; 19, 21; 20, import_prefix; 21, dotted_name; 21, 22; 22, identifier:utils; 23, dotted_name; 23, 24; 24, identifier:pack_tups; 25, comment; 26, if_statement; 26, 27; 26, 28; 26, 29; 27, identifier:_DEBUG; 28, comment; 29, block; 29, 30; 29, 40; 29, 50; 29, 60; 30, expression_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:print; 33, argument_list; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, string:"g_nums = {0}"; 37, identifier:format; 38, argument_list; 38, 39; 39, identifier:g_nums; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:print; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:"ats_1 = {0}"; 47, identifier:format; 48, argument_list; 48, 49; 49, identifier:ats_1; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:print; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, string:"ats_2 = {0}"; 57, identifier:format; 58, argument_list; 58, 59; 59, identifier:ats_2; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:print; 63, argument_list; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, string:"ats_3 = {0}"; 67, identifier:format; 68, argument_list; 68, 69; 69, identifier:ats_3; 70, comment; 71, comment; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:arglist; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:_none_subst; 79, argument_list; 79, 80; 79, 81; 79, 82; 79, 83; 80, identifier:g_nums; 81, identifier:ats_1; 82, identifier:ats_2; 83, identifier:ats_3; 84, comment; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:tups; 88, call; 88, 89; 88, 90; 89, identifier:pack_tups; 90, argument_list; 90, 91; 91, list_splat; 91, 92; 92, identifier:arglist; 93, comment; 94, if_statement; 94, 95; 94, 96; 94, 97; 95, identifier:_DEBUG; 96, comment; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:print; 101, argument_list; 101, 102; 102, identifier:tups; 103, comment; 104, comment; 105, for_statement; 105, 106; 105, 107; 105, 108; 106, identifier:tup; 107, identifier:tups; 108, block; 108, 109; 108, 118; 108, 119; 109, if_statement; 109, 110; 109, 111; 109, 112; 110, identifier:_DEBUG; 111, comment; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:print; 116, argument_list; 116, 117; 117, identifier:tup; 118, comment; 119, expression_statement; 119, 120; 120, yield; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:_iter_return; 125, argument_list; 125, 126; 125, 127; 125, 130; 126, identifier:tup; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:angle_single; 130, identifier:invalid_error
def angle_iter(self, g_nums, ats_1, ats_2, ats_3, invalid_error=False): """ Iterator over selected atomic angles. Angles are in degrees as with :meth:`angle_single`. See `above <toc-generators_>`_ for more information on calling options. Parameters ---------- g_nums |int| or iterable |int| or |None| -- Index of the desired geometry ats_1 |int| or iterable |int| or |None| -- Index of the first atom ats_2 |int| or iterable |int| or |None| -- Index of the second atom ats_3 |int| or iterable |int| or |None| -- Index of the third atom invalid_error |bool|, optional -- If |False| (the default), |None| values are returned for results corresponding to invalid indices. If |True|, exceptions are raised per normal. Yields ------ angle |npfloat_| -- Spanning angles in degrees between corresponding |br| `ats_1`-`ats_2`-`ats_3`, from geometry/geometries `g_nums` Raises ------ ~exceptions.IndexError If an invalid (out-of-range) `g_num` or `at_#` is provided. ~exceptions.ValueError If all iterable objects are not the same length. ~exceptions.ValueError If any `ats_2` element is equal to either the corresponding `ats_1` or `ats_3` element. """ # Suitability of ats_n indices will be checked within the # self.angle_single() calls and thus no check is needed here. # Import the tuple-generating function from .utils import pack_tups # Print the function inputs if debug mode is on if _DEBUG: # pragma: no cover print("g_nums = {0}".format(g_nums)) print("ats_1 = {0}".format(ats_1)) print("ats_2 = {0}".format(ats_2)) print("ats_3 = {0}".format(ats_3)) ## end if # Perform the None substitution arglist = self._none_subst(g_nums, ats_1, ats_2, ats_3) # Expand/pack the tuples from the inputs tups = pack_tups(*arglist) # Dump the results if debug mode is on if _DEBUG: # pragma: no cover print(tups) ## end if # Construct the generator using the packed tuples. for tup in tups: if _DEBUG: # pragma: no cover print(tup) ## end if yield self._iter_return(tup, self.angle_single, invalid_error)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:dihed_iter; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, identifier:self; 5, identifier:g_nums; 6, identifier:ats_1; 7, identifier:ats_2; 8, identifier:ats_3; 9, identifier:ats_4; 10, line_continuation:\; 11, default_parameter; 11, 12; 11, 13; 12, identifier:invalid_error; 13, False; 14, block; 14, 15; 14, 17; 14, 18; 14, 19; 14, 20; 14, 27; 14, 28; 14, 82; 14, 83; 14, 84; 14, 97; 14, 98; 14, 106; 14, 107; 14, 116; 14, 117; 14, 118; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, comment; 19, comment; 20, import_from_statement; 20, 21; 20, 25; 21, relative_import; 21, 22; 21, 23; 22, import_prefix; 23, dotted_name; 23, 24; 24, identifier:utils; 25, dotted_name; 25, 26; 26, identifier:pack_tups; 27, comment; 28, if_statement; 28, 29; 28, 30; 28, 31; 29, identifier:_DEBUG; 30, comment; 31, block; 31, 32; 31, 42; 31, 52; 31, 62; 31, 72; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:print; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, string:"g_nums = {0}"; 39, identifier:format; 40, argument_list; 40, 41; 41, identifier:g_nums; 42, expression_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:print; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, string:"ats_1 = {0}"; 49, identifier:format; 50, argument_list; 50, 51; 51, identifier:ats_1; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:print; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:"ats_2 = {0}"; 59, identifier:format; 60, argument_list; 60, 61; 61, identifier:ats_2; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:print; 65, argument_list; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:"ats_3 = {0}"; 69, identifier:format; 70, argument_list; 70, 71; 71, identifier:ats_3; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:print; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, string:"ats_4 = {0}"; 79, identifier:format; 80, argument_list; 80, 81; 81, identifier:ats_4; 82, comment; 83, comment; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:arglist; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:self; 90, identifier:_none_subst; 91, argument_list; 91, 92; 91, 93; 91, 94; 91, 95; 91, 96; 92, identifier:g_nums; 93, identifier:ats_1; 94, identifier:ats_2; 95, identifier:ats_3; 96, identifier:ats_4; 97, comment; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:tups; 101, call; 101, 102; 101, 103; 102, identifier:pack_tups; 103, argument_list; 103, 104; 104, list_splat; 104, 105; 105, identifier:arglist; 106, comment; 107, if_statement; 107, 108; 107, 109; 107, 110; 108, identifier:_DEBUG; 109, comment; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:print; 114, argument_list; 114, 115; 115, identifier:tups; 116, comment; 117, comment; 118, for_statement; 118, 119; 118, 120; 118, 121; 119, identifier:tup; 120, identifier:tups; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, yield; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:_iter_return; 128, argument_list; 128, 129; 128, 130; 128, 133; 129, identifier:tup; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:dihed_single; 133, identifier:invalid_error
def dihed_iter(self, g_nums, ats_1, ats_2, ats_3, ats_4, \ invalid_error=False): """ Iterator over selected dihedral angles. Angles are in degrees as with :meth:`dihed_single`. See `above <toc-generators_>`_ for more information on calling options. Parameters ---------- g_nums |int| or iterable |int| or |None| -- Indices of the desired geometry ats_1 |int| or iterable |int| or |None| -- Indices of the first atoms ats_2 |int| or iterable |int| or |None| -- Indices of the second atoms ats_3 |int| or iterable |int| or |None| -- Indices of the third atoms ats_4 |int| or iterable |int| or |None| -- Indices of the fourth atoms invalid_error |bool|, optional -- If |False| (the default), |None| values are returned for results corresponding to invalid indices. If |True|, exceptions are raised per normal. Yields ------ dihed |npfloat_| -- Out-of-plane/dihedral angles in degrees for the indicated atom sets `ats_1`-`ats_2`-`ats_3`-`ats_4`, drawn from the respective `g_nums`. Raises ------ ~exceptions.IndexError If an invalid (out-of-range) `g_num` or `at_#` is provided. ~exceptions.ValueError If all iterable objects are not the same length. ~exceptions.ValueError If any corresponding `ats_#` indices are equal. ~opan.error.XYZError (typecode :data:`~opan.error.XYZError.DIHED`) If either of the atom trios (1-2-3 or 2-3-4) is too close to linearity for any group of `ats_#` """ # Suitability of ats_n indices will be checked within the # self.dihed_single() calls and thus no check is needed here. # Import the tuple-generating function from .utils import pack_tups # Print the function inputs if debug mode is on if _DEBUG: # pragma: no cover print("g_nums = {0}".format(g_nums)) print("ats_1 = {0}".format(ats_1)) print("ats_2 = {0}".format(ats_2)) print("ats_3 = {0}".format(ats_3)) print("ats_4 = {0}".format(ats_4)) ## end if # Perform the None substitution arglist = self._none_subst(g_nums, ats_1, ats_2, ats_3, ats_4) # Expand/pack the tuples from the inputs tups = pack_tups(*arglist) # Dump the results if debug mode is on if _DEBUG: # pragma: no cover print(tups) ## end if # Construct the generator using the packed tuples. for tup in tups: yield self._iter_return(tup, self.dihed_single, invalid_error)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:displ_single; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:g_num; 6, identifier:at_1; 7, identifier:at_2; 8, block; 8, 9; 8, 11; 8, 12; 8, 17; 8, 26; 8, 27; 8, 28; 8, 29; 8, 30; 8, 53; 8, 54; 8, 77; 8, 78; 8, 79; 8, 80; 8, 81; 8, 96; 8, 111; 8, 112; 8, 137; 8, 138; 8, 139; 8, 148; 8, 149; 8, 179; 8, 180; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, import_statement; 12, 13; 13, aliased_import; 13, 14; 13, 16; 14, dotted_name; 14, 15; 15, identifier:numpy; 16, identifier:np; 17, import_from_statement; 17, 18; 17, 22; 18, relative_import; 18, 19; 18, 20; 19, import_prefix; 20, dotted_name; 20, 21; 21, identifier:utils; 22, aliased_import; 22, 23; 22, 25; 23, dotted_name; 23, 24; 24, identifier:safe_cast; 25, identifier:scast; 26, comment; 27, comment; 28, comment; 29, comment; 30, if_statement; 30, 31; 30, 42; 31, not_operator; 31, 32; 32, parenthesized_expression; 32, 33; 33, comparison_operator:<=; 33, 34; 33, 38; 33, 39; 34, unary_operator:-; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:num_atoms; 38, identifier:at_1; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:num_atoms; 42, block; 42, 43; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:IndexError; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, string:"Invalid index for 'at_1' ({0})"; 50, identifier:format; 51, argument_list; 51, 52; 52, identifier:at_1; 53, comment; 54, if_statement; 54, 55; 54, 66; 55, not_operator; 55, 56; 56, parenthesized_expression; 56, 57; 57, comparison_operator:<=; 57, 58; 57, 62; 57, 63; 58, unary_operator:-; 58, 59; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:num_atoms; 62, identifier:at_2; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:num_atoms; 66, block; 66, 67; 67, raise_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:IndexError; 70, argument_list; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, string:"Invalid index for 'at_2' ({0})"; 74, identifier:format; 75, argument_list; 75, 76; 76, identifier:at_2; 77, comment; 78, comment; 79, comment; 80, comment; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:at_1; 84, call; 84, 85; 84, 86; 85, identifier:scast; 86, argument_list; 86, 87; 86, 93; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:np; 90, identifier:floor; 91, argument_list; 91, 92; 92, identifier:at_1; 93, attribute; 93, 94; 93, 95; 94, identifier:np; 95, identifier:int_; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:at_2; 99, call; 99, 100; 99, 101; 100, identifier:scast; 101, argument_list; 101, 102; 101, 108; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:np; 105, identifier:floor; 106, argument_list; 106, 107; 107, identifier:at_2; 108, attribute; 108, 109; 108, 110; 109, identifier:np; 110, identifier:int_; 111, comment; 112, if_statement; 112, 113; 112, 126; 113, comparison_operator:==; 113, 114; 113, 120; 114, parenthesized_expression; 114, 115; 115, binary_operator:%; 115, 116; 115, 117; 116, identifier:at_1; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:num_atoms; 120, parenthesized_expression; 120, 121; 121, binary_operator:%; 121, 122; 121, 123; 122, identifier:at_2; 123, attribute; 123, 124; 123, 125; 124, identifier:self; 125, identifier:num_atoms; 126, block; 126, 127; 127, return_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:np; 131, identifier:array; 132, argument_list; 132, 133; 133, list:[0.0, 0.0, 0.0]; 133, 134; 133, 135; 133, 136; 134, float:0.0; 135, float:0.0; 136, float:0.0; 137, comment; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:g; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:geom_single; 146, argument_list; 146, 147; 147, identifier:g_num; 148, comment; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:displ; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:np; 155, identifier:array; 156, argument_list; 156, 157; 157, list_comprehension; 157, 158; 157, 173; 158, binary_operator:-; 158, 159; 158, 166; 159, subscript; 159, 160; 159, 161; 160, identifier:g; 161, binary_operator:+; 161, 162; 161, 163; 162, identifier:i; 163, binary_operator:*; 163, 164; 163, 165; 164, integer:3; 165, identifier:at_2; 166, subscript; 166, 167; 166, 168; 167, identifier:g; 168, binary_operator:+; 168, 169; 168, 170; 169, identifier:i; 170, binary_operator:*; 170, 171; 170, 172; 171, integer:3; 172, identifier:at_1; 173, for_in_clause; 173, 174; 173, 175; 174, identifier:i; 175, call; 175, 176; 175, 177; 176, identifier:range; 177, argument_list; 177, 178; 178, integer:3; 179, comment; 180, return_statement; 180, 181; 181, identifier:displ
def displ_single(self, g_num, at_1, at_2): """ Displacement vector between two atoms. Returns the displacement vector pointing from `at_1` toward `at_2` from geometry `g_num`. If `at_1` == `at_2` a strict zero vector is returned. Displacement vector is returned in units of Bohrs. Parameters ---------- g_num |int| -- Index of the desired geometry at_1 |int| -- Index of the first atom at_2 |int| -- Index of the second atom Returns ------- displ length-3 |npfloat_| -- Displacement vector from `at_1` to `at_2` Raises ------ ~exceptions.IndexError If an invalid (out-of-range) `g_num` or `at_#` is provided """ # Library imports import numpy as np from .utils import safe_cast as scast # The below errors are explicitly thrown since they are multiplied by # three when they are used as an index and thus give non-intuitive # errors. # Complain if at_1 is invalid if not (-self.num_atoms <= at_1 < self.num_atoms): raise IndexError("Invalid index for 'at_1' ({0})".format(at_1)) # Complain if at_2 is invalid if not (-self.num_atoms <= at_2 < self.num_atoms): raise IndexError("Invalid index for 'at_2' ({0})".format(at_2)) # Should never be necessary (save for badly erroneous calling code), # but coerce at_1 and at_2 to their floor() values. This is again # needed since they are multiplied by three in the index expresssions # below, and can cause funny behavior when truncated by the indexing at_1 = scast(np.floor(at_1), np.int_) at_2 = scast(np.floor(at_2), np.int_) # If the atom indices are the same, return trivial zero vector if (at_1 % self.num_atoms) == (at_2 % self.num_atoms): return np.array([0.0, 0.0, 0.0]) ## end if # Retrieve the geometry; np.float_ type should be retained g = self.geom_single(g_num) # Calculate the displacement vector and return displ = np.array([ g[i + 3*at_2] - g[i + 3*at_1] for i in range(3) ]) # Return the displacement vector return displ
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:displ_iter; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:g_nums; 6, identifier:ats_1; 7, identifier:ats_2; 8, default_parameter; 8, 9; 8, 10; 9, identifier:invalid_error; 10, False; 11, block; 11, 12; 11, 14; 11, 15; 11, 22; 11, 23; 11, 57; 11, 58; 11, 59; 11, 70; 11, 71; 11, 79; 11, 80; 11, 89; 11, 90; 11, 91; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, import_from_statement; 15, 16; 15, 20; 16, relative_import; 16, 17; 16, 18; 17, import_prefix; 18, dotted_name; 18, 19; 19, identifier:utils; 20, dotted_name; 20, 21; 21, identifier:pack_tups; 22, comment; 23, if_statement; 23, 24; 23, 25; 23, 26; 24, identifier:_DEBUG; 25, comment; 26, block; 26, 27; 26, 37; 26, 47; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:print; 30, argument_list; 30, 31; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, string:"g_nums = {0}"; 34, identifier:format; 35, argument_list; 35, 36; 36, identifier:g_nums; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:print; 40, argument_list; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, string:"ats_1 = {0}"; 44, identifier:format; 45, argument_list; 45, 46; 46, identifier:ats_1; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:print; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, string:"ats_2 = {0}"; 54, identifier:format; 55, argument_list; 55, 56; 56, identifier:ats_2; 57, comment; 58, comment; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:arglist; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:_none_subst; 66, argument_list; 66, 67; 66, 68; 66, 69; 67, identifier:g_nums; 68, identifier:ats_1; 69, identifier:ats_2; 70, comment; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:tups; 74, call; 74, 75; 74, 76; 75, identifier:pack_tups; 76, argument_list; 76, 77; 77, list_splat; 77, 78; 78, identifier:arglist; 79, comment; 80, if_statement; 80, 81; 80, 82; 80, 83; 81, identifier:_DEBUG; 82, comment; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:print; 87, argument_list; 87, 88; 88, identifier:tups; 89, comment; 90, comment; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:tup; 93, identifier:tups; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, yield; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:_iter_return; 101, argument_list; 101, 102; 101, 103; 101, 106; 102, identifier:tup; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:displ_single; 106, identifier:invalid_error
def displ_iter(self, g_nums, ats_1, ats_2, invalid_error=False): """ Iterator over indicated displacement vectors. Displacements are in Bohrs as with :meth:`displ_single`. See `above <toc-generators_>`_ for more information on calling options. Parameters ---------- g_nums |int| or length-R iterable |int| or |None| -- Index/indices of the desired geometry/geometries ats_1 |int| or length-R iterable |int| or |None| -- Index/indices of the first atom(s) ats_2 |int| or length-R iterable |int| or |None| -- Index/indices of the second atom(s) invalid_error |bool|, optional -- If |False| (the default), |None| values are returned for results corresponding to invalid indices. If |True|, exceptions are raised per normal. Yields ------ displ |npfloat_| -- Displacement vector in Bohrs between each atom pair of |br| `ats_1` :math:`\\rightarrow` `ats_2` from the corresponding geometries of `g_nums`. Raises ------ ~exceptions.IndexError If an invalid (out-of-range) `g_num` or `at_#` is provided. ~exceptions.ValueError If all iterable objects are not the same length. """ # Import the tuple-generating function from .utils import pack_tups # Print the function inputs if debug mode is on if _DEBUG: # pragma: no cover print("g_nums = {0}".format(g_nums)) print("ats_1 = {0}".format(ats_1)) print("ats_2 = {0}".format(ats_2)) ## end if # Perform the None substitution arglist = self._none_subst(g_nums, ats_1, ats_2) # Expand/pack the tuples from the inputs tups = pack_tups(*arglist) # Dump the results if debug mode is on if _DEBUG: # pragma: no cover print(tups) ## end if # Construct the generator using the packed tuples. for tup in tups: yield self._iter_return(tup, self.displ_single, invalid_error)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_none_subst; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, block; 7, 8; 7, 10; 7, 11; 7, 16; 7, 17; 7, 25; 7, 29; 7, 30; 7, 51; 7, 52; 7, 53; 7, 175; 7, 176; 7, 177; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, import_statement; 11, 12; 12, aliased_import; 12, 13; 12, 15; 13, dotted_name; 13, 14; 14, identifier:numpy; 15, identifier:np; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:arglist; 20, list_comprehension; 20, 21; 20, 22; 21, identifier:a; 22, for_in_clause; 22, 23; 22, 24; 23, identifier:a; 24, identifier:args; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:none_found; 28, False; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:none_vals; 33, call; 33, 34; 33, 35; 34, identifier:list; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:map; 38, argument_list; 38, 39; 38, 50; 39, lambda; 39, 40; 39, 42; 40, lambda_parameters; 40, 41; 41, identifier:e; 42, call; 42, 43; 42, 44; 43, identifier:isinstance; 44, argument_list; 44, 45; 44, 46; 45, identifier:e; 46, call; 46, 47; 46, 48; 47, identifier:type; 48, argument_list; 48, 49; 49, None; 50, identifier:arglist; 51, comment; 52, comment; 53, if_statement; 53, 54; 53, 62; 53, 83; 54, comparison_operator:>; 54, 55; 54, 61; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:np; 58, identifier:count_nonzero; 59, argument_list; 59, 60; 60, identifier:none_vals; 61, integer:1; 62, block; 62, 63; 63, raise_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:ValueError; 66, argument_list; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, string:"Multiple 'None' values [indices {0}] not supported"; 70, identifier:format; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:tuple; 74, argument_list; 74, 75; 75, subscript; 75, 76; 75, 82; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:np; 79, identifier:nonzero; 80, argument_list; 80, 81; 81, identifier:none_vals; 82, integer:0; 83, elif_clause; 83, 84; 83, 92; 83, 93; 83, 94; 83, 95; 84, comparison_operator:==; 84, 85; 84, 91; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:np; 88, identifier:count_nonzero; 89, argument_list; 89, 90; 90, identifier:none_vals; 91, integer:1; 92, comment; 93, comment; 94, comment; 95, block; 95, 96; 95, 137; 95, 138; 95, 139; 95, 143; 95, 156; 96, if_statement; 96, 97; 96, 131; 97, not_operator; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:all; 100, argument_list; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:np; 104, identifier:equal; 105, argument_list; 105, 106; 105, 116; 106, call; 106, 107; 106, 108; 107, identifier:list; 108, argument_list; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:map; 111, argument_list; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:np; 114, identifier:iterable; 115, identifier:arglist; 116, call; 116, 117; 116, 118; 117, identifier:list; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:map; 121, argument_list; 121, 122; 121, 130; 122, lambda; 122, 123; 122, 125; 123, lambda_parameters; 123, 124; 124, identifier:e; 125, call; 125, 126; 125, 127; 126, identifier:isinstance; 127, argument_list; 127, 128; 127, 129; 128, identifier:e; 129, identifier:str; 130, identifier:arglist; 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:"'None' as parameter invalid with non-str iterables"; 137, comment; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:none_found; 142, True; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:none_loc; 146, subscript; 146, 147; 146, 155; 147, subscript; 147, 148; 147, 154; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:np; 151, identifier:nonzero; 152, argument_list; 152, 153; 153, identifier:none_vals; 154, integer:0; 155, integer:0; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 161; 157, 162; 158, subscript; 158, 159; 158, 160; 159, identifier:arglist; 160, identifier:none_loc; 161, line_continuation:\; 162, call; 162, 163; 162, 164; 163, identifier:range; 164, argument_list; 164, 165; 165, conditional_expression:if; 165, 166; 165, 169; 165, 172; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:num_geoms; 169, comparison_operator:==; 169, 170; 169, 171; 170, identifier:none_loc; 171, integer:0; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:num_atoms; 175, comment; 176, comment; 177, return_statement; 177, 178; 178, identifier:arglist
def _none_subst(self, *args): """ Helper function to insert full ranges for |None| for X_iter methods. Custom method, specifically tailored, taking in the arguments from an X_iter method and performing the replacement of |None| after error-checking the arguments for a max of one |None| value, and ensuring that if a |None| is present, no other non-|str| iterables are present. Parameters ---------- args : 3-5 arguments of |int| or iterable |int|, or |None| First argument is always the indices for the geometries; all following are for the atoms in sequence as required for the particular :samp:`{x}_iter` method Returns ------- arglist : 3-5 arguments, matching input params Argument list, with |None| substituted if validly present Raises ------ ~exceptions.ValueError : If more than one |None| argument is present ~exceptions.ValueError : If an arg is non-|str| iterable when one |None| is present """ # Imports import numpy as np # Initialize argument list return value, and as None not found arglist = [a for a in args] none_found = False # Check for None values none_vals = list(map(lambda e: isinstance(e, type(None)), arglist)) # Error if more than one None; handle if exactly one; pass through if # none. if np.count_nonzero(none_vals) > 1: raise ValueError( "Multiple 'None' values [indices {0}] not supported" .format(tuple(np.nonzero(none_vals)[0]))) elif np.count_nonzero(none_vals) == 1: # Must be no iterables that are not strings. Thus, an element-wise # test for iterability and an element-wise test for stringiness # must give matching arrays if not all(np.equal(list(map(np.iterable, arglist)), list(map(lambda e: isinstance(e, str), arglist)))): raise ValueError( "'None' as parameter invalid with non-str iterables") ## end if # Parameters okay; replace the None with the appropriate range() none_found = True none_loc = np.nonzero(none_vals)[0][0] arglist[none_loc] = \ range(self.num_geoms if none_loc == 0 else self.num_atoms) ## end if # Return the arguments list and the none-found value return arglist
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:new_from_files; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 3, 13; 3, 18; 3, 19; 3, 22; 4, identifier:self; 5, identifier:basepath; 6, identifier:basename; 7, identifier:repo; 8, line_continuation:\; 9, default_parameter; 9, 10; 9, 11; 10, identifier:bohrs; 11, False; 12, line_continuation:\; 13, default_parameter; 13, 14; 13, 15; 14, identifier:software; 15, attribute; 15, 16; 15, 17; 16, identifier:_E_SW; 17, identifier:ORCA; 18, line_continuation:\; 19, default_parameter; 19, 20; 19, 21; 20, identifier:repo_clobber; 21, False; 22, dictionary_splat_pattern; 22, 23; 23, identifier:kwargs; 24, block; 24, 25; 24, 27; 24, 28; 24, 31; 24, 38; 24, 47; 24, 56; 24, 65; 24, 74; 24, 87; 24, 96; 24, 103; 24, 112; 24, 113; 24, 114; 24, 115; 24, 132; 24, 133; 24, 150; 24, 151; 24, 168; 24, 169; 24, 186; 24, 187; 24, 188; 24, 189; 24, 211; 24, 239; 24, 267; 24, 268; 24, 283; 24, 284; 24, 285; 24, 286; 24, 287; 24, 317; 24, 318; 24, 319; 24, 334; 24, 335; 24, 336; 24, 367; 24, 368; 24, 369; 25, expression_statement; 25, 26; 26, comment; 27, comment; 28, import_statement; 28, 29; 29, dotted_name; 29, 30; 30, identifier:os; 31, import_from_statement; 31, 32; 31, 34; 32, dotted_name; 32, 33; 33, identifier:os; 34, aliased_import; 34, 35; 34, 37; 35, dotted_name; 35, 36; 36, identifier:path; 37, identifier:osp; 38, import_from_statement; 38, 39; 38, 43; 39, relative_import; 39, 40; 39, 41; 40, import_prefix; 41, dotted_name; 41, 42; 42, identifier:xyz; 43, aliased_import; 43, 44; 43, 46; 44, dotted_name; 44, 45; 45, identifier:OpanXYZ; 46, identifier:OX; 47, import_from_statement; 47, 48; 47, 52; 48, relative_import; 48, 49; 48, 50; 49, import_prefix; 50, dotted_name; 50, 51; 51, identifier:grad; 52, aliased_import; 52, 53; 52, 55; 53, dotted_name; 53, 54; 54, identifier:OrcaEngrad; 55, identifier:OE; 56, import_from_statement; 56, 57; 56, 61; 57, relative_import; 57, 58; 57, 59; 58, import_prefix; 59, dotted_name; 59, 60; 60, identifier:hess; 61, aliased_import; 61, 62; 61, 64; 62, dotted_name; 62, 63; 63, identifier:OrcaHess; 64, identifier:OH; 65, import_from_statement; 65, 66; 65, 70; 66, relative_import; 66, 67; 66, 68; 67, import_prefix; 68, dotted_name; 68, 69; 69, identifier:repo; 70, aliased_import; 70, 71; 70, 73; 71, dotted_name; 71, 72; 72, identifier:OpanAnharmRepo; 73, identifier:OR; 74, import_from_statement; 74, 75; 74, 79; 74, 83; 75, relative_import; 75, 76; 75, 77; 76, import_prefix; 77, dotted_name; 77, 78; 78, identifier:const; 79, aliased_import; 79, 80; 79, 82; 80, dotted_name; 80, 81; 81, identifier:EnumDispDirection; 82, identifier:E_DDir; 83, aliased_import; 83, 84; 83, 86; 84, dotted_name; 84, 85; 85, identifier:EnumFileType; 86, identifier:E_FT; 87, import_from_statement; 87, 88; 87, 92; 88, relative_import; 88, 89; 88, 90; 89, import_prefix; 90, dotted_name; 90, 91; 91, identifier:const; 92, aliased_import; 92, 93; 92, 95; 93, dotted_name; 93, 94; 94, identifier:EnumSoftware; 95, identifier:E_SW; 96, import_from_statement; 96, 97; 96, 101; 97, relative_import; 97, 98; 97, 99; 98, import_prefix; 99, dotted_name; 99, 100; 100, identifier:const; 101, dotted_name; 101, 102; 102, identifier:DEF; 103, import_from_statement; 103, 104; 103, 108; 104, relative_import; 104, 105; 104, 106; 105, import_prefix; 106, dotted_name; 106, 107; 107, identifier:error; 108, aliased_import; 108, 109; 108, 111; 109, dotted_name; 109, 110; 110, identifier:AnharmError; 111, identifier:ANHErr; 112, comment; 113, comment; 114, comment; 115, if_statement; 115, 116; 115, 122; 116, not_operator; 116, 117; 117, comparison_operator:==; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:w_xyz; 121, None; 122, block; 122, 123; 123, raise_statement; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:ANHErr; 126, argument_list; 126, 127; 126, 130; 126, 131; 127, attribute; 127, 128; 127, 129; 128, identifier:ANHErr; 129, identifier:STATUS; 130, string:"XYZ object is already bound"; 131, string:""; 132, comment; 133, if_statement; 133, 134; 133, 140; 134, not_operator; 134, 135; 135, comparison_operator:==; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:w_grad; 139, None; 140, block; 140, 141; 141, raise_statement; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:ANHErr; 144, argument_list; 144, 145; 144, 148; 144, 149; 145, attribute; 145, 146; 145, 147; 146, identifier:ANHErr; 147, identifier:STATUS; 148, string:"GRAD object is already bound"; 149, string:""; 150, comment; 151, if_statement; 151, 152; 151, 158; 152, not_operator; 152, 153; 153, comparison_operator:==; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:w_hess; 157, None; 158, block; 158, 159; 159, raise_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:ANHErr; 162, argument_list; 162, 163; 162, 166; 162, 167; 163, attribute; 163, 164; 163, 165; 164, identifier:ANHErr; 165, identifier:STATUS; 166, string:"HESS object is already bound"; 167, string:""; 168, comment; 169, if_statement; 169, 170; 169, 176; 170, not_operator; 170, 171; 171, comparison_operator:==; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:repo; 175, None; 176, block; 176, 177; 177, raise_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:ANHErr; 180, argument_list; 180, 181; 180, 184; 180, 185; 181, attribute; 181, 182; 181, 183; 182, identifier:ANHErr; 183, identifier:STATUS; 184, string:"Repository object is already bound"; 185, string:""; 186, comment; 187, comment; 188, comment; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:w_xyz; 194, call; 194, 195; 194, 196; 195, identifier:OX; 196, argument_list; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:osp; 200, identifier:join; 201, argument_list; 201, 202; 201, 203; 201, 204; 202, identifier:basepath; 203, line_continuation:\; 204, binary_operator:+; 204, 205; 204, 210; 205, binary_operator:+; 205, 206; 205, 207; 206, identifier:basename; 207, attribute; 207, 208; 207, 209; 208, identifier:osp; 209, identifier:extsep; 210, identifier:xyz_ext; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:w_grad; 216, call; 216, 217; 216, 218; 217, identifier:OE; 218, argument_list; 218, 219; 218, 233; 218, 234; 218, 235; 218, 238; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:osp; 222, identifier:join; 223, argument_list; 223, 224; 223, 225; 223, 226; 224, identifier:basepath; 225, line_continuation:\; 226, binary_operator:+; 226, 227; 226, 232; 227, binary_operator:+; 227, 228; 227, 229; 228, identifier:basename; 229, attribute; 229, 230; 229, 231; 230, identifier:osp; 231, identifier:extsep; 232, identifier:engrad_ext; 233, line_continuation:\; 234, integer:0; 235, attribute; 235, 236; 235, 237; 236, identifier:E_DDir; 237, identifier:NO_DISP; 238, float:0.0; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:self; 243, identifier:w_hess; 244, call; 244, 245; 244, 246; 245, identifier:OH; 246, argument_list; 246, 247; 246, 261; 246, 262; 246, 263; 246, 266; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:osp; 250, identifier:join; 251, argument_list; 251, 252; 251, 253; 251, 254; 252, identifier:basepath; 253, line_continuation:\; 254, binary_operator:+; 254, 255; 254, 260; 255, binary_operator:+; 255, 256; 255, 257; 256, identifier:basename; 257, attribute; 257, 258; 257, 259; 258, identifier:osp; 259, identifier:extsep; 260, identifier:hess_ext; 261, line_continuation:\; 262, integer:0; 263, attribute; 263, 264; 263, 265; 264, identifier:E_DDir; 265, identifier:NO_DISP; 266, float:0.0; 267, comment; 268, if_statement; 268, 269; 268, 275; 269, not_operator; 269, 270; 270, call; 270, 271; 270, 272; 271, identifier:isinstance; 272, argument_list; 272, 273; 272, 274; 273, identifier:repo; 274, identifier:str; 275, block; 275, 276; 276, raise_statement; 276, 277; 277, call; 277, 278; 277, 279; 278, identifier:TypeError; 279, argument_list; 279, 280; 280, binary_operator:+; 280, 281; 280, 282; 281, string:"Must create new repository when loading "; 282, string:"a new dataset."; 283, comment; 284, comment; 285, comment; 286, comment; 287, if_statement; 287, 288; 287, 306; 288, boolean_operator:and; 288, 289; 288, 299; 289, comparison_operator:>; 289, 290; 289, 298; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:osp; 293, identifier:split; 294, argument_list; 294, 295; 295, subscript; 295, 296; 295, 297; 296, identifier:repo; 297, integer:0; 298, integer:0; 299, not_operator; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:osp; 303, identifier:isabs; 304, argument_list; 304, 305; 305, identifier:repo; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 310; 309, identifier:repo; 310, call; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:osp; 313, identifier:join; 314, argument_list; 314, 315; 314, 316; 315, identifier:basepath; 316, identifier:repo; 317, comment; 318, comment; 319, if_statement; 319, 320; 319, 326; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:osp; 323, identifier:isdir; 324, argument_list; 324, 325; 325, identifier:repo; 326, block; 326, 327; 327, raise_statement; 327, 328; 328, call; 328, 329; 328, 330; 329, identifier:IOError; 330, argument_list; 330, 331; 331, binary_operator:+; 331, 332; 331, 333; 332, string:"Cannot bind repository -- specified "; 333, string:"location is a directory"; 334, comment; 335, comment; 336, if_statement; 336, 337; 336, 343; 336, 344; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:osp; 340, identifier:isfile; 341, argument_list; 341, 342; 342, identifier:repo; 343, comment; 344, block; 344, 345; 344, 366; 345, if_statement; 345, 346; 345, 347; 345, 348; 345, 356; 346, identifier:repo_clobber; 347, comment; 348, block; 348, 349; 349, expression_statement; 349, 350; 350, call; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:os; 353, identifier:remove; 354, argument_list; 354, 355; 355, identifier:repo; 356, else_clause; 356, 357; 356, 358; 357, comment; 358, block; 358, 359; 359, raise_statement; 359, 360; 360, call; 360, 361; 360, 362; 361, identifier:IOError; 362, argument_list; 362, 363; 363, binary_operator:+; 363, 364; 363, 365; 364, string:"Target repository file exists and "; 365, string:"clobber is disabled."; 366, comment; 367, comment; 368, comment; 369, expression_statement; 369, 370; 370, assignment; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:self; 373, identifier:repo; 374, call; 374, 375; 374, 376; 375, identifier:OR; 376, argument_list; 376, 377; 377, identifier:repo
def new_from_files(self, basepath, basename, repo, \ bohrs=False, \ software=_E_SW.ORCA, \ repo_clobber=False, **kwargs): """ Initialize with data from files. """ # Imports import os from os import path as osp from ..xyz import OpanXYZ as OX from ..grad import OrcaEngrad as OE from ..hess import OrcaHess as OH from .repo import OpanAnharmRepo as OR from ..const import EnumDispDirection as E_DDir, EnumFileType as E_FT from ..const import EnumSoftware as E_SW from ..const import DEF from ..error import AnharmError as ANHErr ## # Store working directory for restore? ## prev_dir = os.getcwd() # Complain if anything is already bound if not self.w_xyz == None: raise ANHErr(ANHErr.STATUS, "XYZ object is already bound", "") ## end if if not self.w_grad == None: raise ANHErr(ANHErr.STATUS, "GRAD object is already bound", "") ## end if if not self.w_hess == None: raise ANHErr(ANHErr.STATUS, "HESS object is already bound", "") ## end if if not self.repo == None: raise ANHErr(ANHErr.STATUS, "Repository object is already bound", "") ## end if # RESUME: vpt2--factor for loading from different software pkgs # Load the three data files self.w_xyz = OX( osp.join(basepath, \ basename + osp.extsep + xyz_ext) ) self.w_grad = OE( osp.join(basepath, \ basename + osp.extsep + engrad_ext), \ 0, E_DDir.NO_DISP, 0.0 ) self.w_hess = OH( osp.join(basepath, \ basename + osp.extsep + hess_ext), \ 0, E_DDir.NO_DISP, 0.0 ) # Only accept new repos for now if not isinstance(repo, str): raise TypeError("Must create new repository when loading " + "a new dataset.") ## end if # Repo is string, treat as filename and try to load # Check if it's a complete path # If it's a relative path, prepend the basepath if osp.split(repo[0]) > 0 and not osp.isabs(repo): repo = osp.join(basepath, repo) ## end if # Complain if it's a directory if osp.isdir(repo): raise IOError("Cannot bind repository -- specified " + "location is a directory") ## end if # If file exists ... if osp.isfile(repo): # Depending on clobber, either delete existing or raise error if repo_clobber: # Clobber old repo os.remove(repo) else: # Raise error raise IOError("Target repository file exists and " + "clobber is disabled.") ## end if ## end if # Should be good to create the repo self.repo = OR(repo)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:from_locus_read; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:locus_read; 6, identifier:n_ref; 7, block; 7, 8; 7, 10; 7, 16; 7, 22; 7, 23; 7, 24; 7, 30; 7, 36; 7, 37; 7, 38; 7, 44; 7, 59; 7, 65; 7, 80; 7, 172; 7, 183; 7, 193; 7, 201; 7, 211; 7, 221; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:sequence; 13, attribute; 13, 14; 13, 15; 14, identifier:locus_read; 15, identifier:sequence; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:reference_positions; 19, attribute; 19, 20; 19, 21; 20, identifier:locus_read; 21, identifier:reference_positions; 22, comment; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:read_pos_before; 27, attribute; 27, 28; 27, 29; 28, identifier:locus_read; 29, identifier:base0_read_position_before_variant; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:read_pos_after; 33, attribute; 33, 34; 33, 35; 34, identifier:locus_read; 35, identifier:base0_read_position_after_variant; 36, comment; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:ref_pos_before; 41, subscript; 41, 42; 41, 43; 42, identifier:reference_positions; 43, identifier:read_pos_before; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:ref_pos_before; 47, None; 48, block; 48, 49; 48, 57; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:logger; 53, identifier:warn; 54, argument_list; 54, 55; 54, 56; 55, string:"Missing reference pos for nucleotide before variant on read: %s"; 56, identifier:locus_read; 57, return_statement; 57, 58; 58, None; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:ref_pos_after; 62, subscript; 62, 63; 62, 64; 63, identifier:reference_positions; 64, identifier:read_pos_after; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:is; 66, 67; 66, 68; 67, identifier:ref_pos_after; 68, None; 69, block; 69, 70; 69, 78; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:logger; 74, identifier:warn; 75, argument_list; 75, 76; 75, 77; 76, string:"Missing reference pos for nucleotide after variant on read: %s"; 77, identifier:locus_read; 78, return_statement; 78, 79; 79, None; 80, if_statement; 80, 81; 80, 84; 80, 142; 81, comparison_operator:==; 81, 82; 81, 83; 82, identifier:n_ref; 83, integer:0; 84, block; 84, 85; 84, 107; 84, 108; 84, 109; 84, 120; 85, if_statement; 85, 86; 85, 91; 85, 92; 85, 93; 85, 94; 86, comparison_operator:!=; 86, 87; 86, 90; 87, binary_operator:-; 87, 88; 87, 89; 88, identifier:ref_pos_after; 89, identifier:ref_pos_before; 90, integer:1; 91, comment; 92, comment; 93, comment; 94, block; 94, 95; 94, 105; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:logger; 99, identifier:debug; 100, argument_list; 100, 101; 100, 102; 100, 103; 100, 104; 101, string:"Positions before (%d) and after (%d) variant should be adjacent on read %s"; 102, identifier:ref_pos_before; 103, identifier:ref_pos_after; 104, identifier:locus_read; 105, return_statement; 105, 106; 106, None; 107, comment; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:ref_positions_for_inserted; 112, subscript; 112, 113; 112, 114; 113, identifier:reference_positions; 114, slice; 114, 115; 114, 118; 114, 119; 115, binary_operator:+; 115, 116; 115, 117; 116, identifier:read_pos_before; 117, integer:1; 118, colon; 119, identifier:read_pos_after; 120, if_statement; 120, 121; 120, 130; 120, 131; 120, 132; 121, call; 121, 122; 121, 123; 122, identifier:any; 123, generator_expression; 123, 124; 123, 127; 124, comparison_operator:is; 124, 125; 124, 126; 125, identifier:insert_pos; 126, None; 127, for_in_clause; 127, 128; 127, 129; 128, identifier:insert_pos; 129, identifier:ref_positions_for_inserted; 130, comment; 131, comment; 132, block; 132, 133; 132, 140; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:logger; 137, identifier:debug; 138, argument_list; 138, 139; 139, string:"Skipping read, inserted nucleotides shouldn't map to reference"; 140, return_statement; 140, 141; 141, None; 142, else_clause; 142, 143; 142, 144; 143, comment; 144, block; 144, 145; 145, if_statement; 145, 146; 145, 153; 145, 154; 145, 155; 145, 156; 146, comparison_operator:!=; 146, 147; 146, 150; 147, binary_operator:-; 147, 148; 147, 149; 148, identifier:ref_pos_after; 149, identifier:ref_pos_before; 150, binary_operator:+; 150, 151; 150, 152; 151, identifier:n_ref; 152, integer:1; 153, comment; 154, comment; 155, comment; 156, block; 156, 157; 156, 170; 157, expression_statement; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:logger; 161, identifier:debug; 162, argument_list; 162, 163; 162, 167; 162, 168; 162, 169; 163, parenthesized_expression; 163, 164; 164, concatenated_string; 164, 165; 164, 166; 165, string:"Positions before (%d) and after (%d) variant should be "; 166, string:"adjacent on read %s"; 167, identifier:ref_pos_before; 168, identifier:ref_pos_after; 169, identifier:locus_read; 170, return_statement; 170, 171; 171, None; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:nucleotides_at_variant_locus; 175, subscript; 175, 176; 175, 177; 176, identifier:sequence; 177, slice; 177, 178; 177, 181; 177, 182; 178, binary_operator:+; 178, 179; 178, 180; 179, identifier:read_pos_before; 180, integer:1; 181, colon; 182, identifier:read_pos_after; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:prefix; 186, subscript; 186, 187; 186, 188; 187, identifier:sequence; 188, slice; 188, 189; 188, 190; 189, colon; 190, binary_operator:+; 190, 191; 190, 192; 191, identifier:read_pos_before; 192, integer:1; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:suffix; 196, subscript; 196, 197; 196, 198; 197, identifier:sequence; 198, slice; 198, 199; 198, 200; 199, identifier:read_pos_after; 200, colon; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 206; 203, pattern_list; 203, 204; 203, 205; 204, identifier:prefix; 205, identifier:suffix; 206, call; 206, 207; 206, 208; 207, identifier:convert_from_bytes_if_necessary; 208, argument_list; 208, 209; 208, 210; 209, identifier:prefix; 210, identifier:suffix; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 216; 213, pattern_list; 213, 214; 213, 215; 214, identifier:prefix; 215, identifier:suffix; 216, call; 216, 217; 216, 218; 217, identifier:trim_N_nucleotides; 218, argument_list; 218, 219; 218, 220; 219, identifier:prefix; 220, identifier:suffix; 221, return_statement; 221, 222; 222, call; 222, 223; 222, 224; 223, identifier:cls; 224, argument_list; 224, 225; 224, 226; 224, 227; 224, 228; 225, identifier:prefix; 226, identifier:nucleotides_at_variant_locus; 227, identifier:suffix; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:name; 230, attribute; 230, 231; 230, 232; 231, identifier:locus_read; 232, identifier:name
def from_locus_read(cls, locus_read, n_ref): """ Given a single LocusRead object, return either an AlleleRead or None Parameters ---------- locus_read : LocusRead Read which overlaps a variant locus but doesn't necessarily contain the alternate nucleotides n_ref : int Number of reference positions we are expecting to be modified or deleted (for insertions this should be 0) """ sequence = locus_read.sequence reference_positions = locus_read.reference_positions # positions of the nucleotides before and after the variant within # the read sequence read_pos_before = locus_read.base0_read_position_before_variant read_pos_after = locus_read.base0_read_position_after_variant # positions of the nucleotides before and after the variant on the # reference genome ref_pos_before = reference_positions[read_pos_before] if ref_pos_before is None: logger.warn( "Missing reference pos for nucleotide before variant on read: %s", locus_read) return None ref_pos_after = reference_positions[read_pos_after] if ref_pos_after is None: logger.warn( "Missing reference pos for nucleotide after variant on read: %s", locus_read) return None if n_ref == 0: if ref_pos_after - ref_pos_before != 1: # if the number of nucleotides skipped isn't the same # as the number of reference nucleotides in the variant then # don't use this read logger.debug( "Positions before (%d) and after (%d) variant should be adjacent on read %s", ref_pos_before, ref_pos_after, locus_read) return None # insertions require a sequence of non-aligned bases # followed by the subsequence reference position ref_positions_for_inserted = reference_positions[ read_pos_before + 1:read_pos_after] if any(insert_pos is not None for insert_pos in ref_positions_for_inserted): # all these inserted nucleotides should *not* align to the # reference logger.debug( "Skipping read, inserted nucleotides shouldn't map to reference") return None else: # substitutions and deletions if ref_pos_after - ref_pos_before != n_ref + 1: # if the number of nucleotides skipped isn't the same # as the number of reference nucleotides in the variant then # don't use this read logger.debug( ("Positions before (%d) and after (%d) variant should be " "adjacent on read %s"), ref_pos_before, ref_pos_after, locus_read) return None nucleotides_at_variant_locus = sequence[read_pos_before + 1:read_pos_after] prefix = sequence[:read_pos_before + 1] suffix = sequence[read_pos_after:] prefix, suffix = convert_from_bytes_if_necessary(prefix, suffix) prefix, suffix = trim_N_nucleotides(prefix, suffix) return cls( prefix, nucleotides_at_variant_locus, suffix, name=locus_read.name)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:mtx_refl; 3, parameters; 3, 4; 3, 5; 4, identifier:nv; 5, default_parameter; 5, 6; 5, 7; 6, identifier:reps; 7, integer:1; 8, block; 8, 9; 8, 11; 8, 12; 8, 17; 8, 24; 8, 31; 8, 32; 8, 49; 8, 50; 8, 51; 8, 69; 8, 70; 8, 84; 8, 85; 8, 103; 8, 104; 8, 115; 8, 116; 8, 117; 8, 135; 8, 136; 8, 196; 8, 197; 8, 198; 8, 215; 8, 216; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, import_statement; 12, 13; 13, aliased_import; 13, 14; 13, 16; 14, dotted_name; 14, 15; 15, identifier:numpy; 16, identifier:np; 17, import_from_statement; 17, 18; 17, 20; 18, dotted_name; 18, 19; 19, identifier:scipy; 20, aliased_import; 20, 21; 20, 23; 21, dotted_name; 21, 22; 22, identifier:linalg; 23, identifier:spla; 24, import_from_statement; 24, 25; 24, 29; 25, relative_import; 25, 26; 25, 27; 26, import_prefix; 27, dotted_name; 27, 28; 28, identifier:const; 29, dotted_name; 29, 30; 30, identifier:PRM; 31, comment; 32, if_statement; 32, 33; 32, 43; 33, comparison_operator:<; 33, 34; 33, 40; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:spla; 37, identifier:norm; 38, argument_list; 38, 39; 39, identifier:nv; 40, attribute; 40, 41; 40, 42; 41, identifier:PRM; 42, identifier:ZERO_VEC_TOL; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:ValueError; 47, argument_list; 47, 48; 48, string:"Norm of 'nv' is too small."; 49, comment; 50, comment; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:nv; 54, call; 54, 55; 54, 56; 55, identifier:make_nd_vec; 56, argument_list; 56, 57; 56, 58; 56, 61; 56, 66; 57, identifier:nv; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:nd; 60, integer:3; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:t; 63, attribute; 63, 64; 63, 65; 64, identifier:np; 65, identifier:float64; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:norm; 68, True; 69, comment; 70, if_statement; 70, 71; 70, 78; 71, not_operator; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:np; 75, identifier:isscalar; 76, argument_list; 76, 77; 77, identifier:reps; 78, block; 78, 79; 79, raise_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:ValueError; 82, argument_list; 82, 83; 83, string:"'reps' must be scalar."; 84, comment; 85, if_statement; 85, 86; 85, 97; 86, not_operator; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:np; 90, identifier:issubdtype; 91, argument_list; 91, 92; 91, 96; 92, call; 92, 93; 92, 94; 93, identifier:type; 94, argument_list; 94, 95; 95, identifier:reps; 96, identifier:int; 97, block; 97, 98; 98, raise_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:ValueError; 101, argument_list; 101, 102; 102, string:"'reps' must be an integer."; 103, comment; 104, if_statement; 104, 105; 104, 109; 105, not_operator; 105, 106; 106, comparison_operator:>; 106, 107; 106, 108; 107, identifier:reps; 108, integer:0; 109, block; 109, 110; 110, raise_statement; 110, 111; 111, call; 111, 112; 111, 113; 112, identifier:ValueError; 113, argument_list; 113, 114; 114, string:"'reps' must be a positive integer."; 115, comment; 116, comment; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:base_mtx; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:np; 123, identifier:zeros; 124, argument_list; 124, 125; 124, 130; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:shape; 127, tuple; 127, 128; 127, 129; 128, integer:3; 129, integer:3; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:dtype; 132, attribute; 132, 133; 132, 134; 133, identifier:np; 134, identifier:float64; 135, comment; 136, for_statement; 136, 137; 136, 138; 136, 142; 137, identifier:i; 138, call; 138, 139; 138, 140; 139, identifier:range; 140, argument_list; 140, 141; 141, integer:3; 142, block; 142, 143; 142, 195; 143, for_statement; 143, 144; 143, 145; 143, 150; 144, identifier:j; 145, call; 145, 146; 145, 147; 146, identifier:range; 147, argument_list; 147, 148; 147, 149; 148, identifier:i; 149, integer:3; 150, block; 150, 151; 150, 194; 151, if_statement; 151, 152; 151, 155; 151, 171; 152, comparison_operator:==; 152, 153; 152, 154; 153, identifier:i; 154, identifier:j; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 162; 158, subscript; 158, 159; 158, 160; 158, 161; 159, identifier:base_mtx; 160, identifier:i; 161, identifier:j; 162, binary_operator:-; 162, 163; 162, 164; 163, integer:1; 164, binary_operator:*; 164, 165; 164, 166; 165, integer:2; 166, binary_operator:**; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:nv; 169, identifier:i; 170, integer:2; 171, else_clause; 171, 172; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 179; 175, subscript; 175, 176; 175, 177; 175, 178; 176, identifier:base_mtx; 177, identifier:i; 178, identifier:j; 179, assignment; 179, 180; 179, 184; 180, subscript; 180, 181; 180, 182; 180, 183; 181, identifier:base_mtx; 182, identifier:j; 183, identifier:i; 184, binary_operator:*; 184, 185; 184, 191; 185, binary_operator:*; 185, 186; 185, 188; 186, unary_operator:-; 186, 187; 187, integer:2; 188, subscript; 188, 189; 188, 190; 189, identifier:nv; 190, identifier:i; 191, subscript; 191, 192; 191, 193; 192, identifier:nv; 193, identifier:j; 194, comment; 195, comment; 196, comment; 197, comment; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:refl_mtx; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:spla; 204, identifier:block_diag; 205, argument_list; 205, 206; 206, list_splat; 206, 207; 207, list_comprehension; 207, 208; 207, 209; 208, identifier:base_mtx; 209, for_in_clause; 209, 210; 209, 211; 210, identifier:i; 211, call; 211, 212; 211, 213; 212, identifier:range; 213, argument_list; 213, 214; 214, identifier:reps; 215, comment; 216, return_statement; 216, 217; 217, identifier:refl_mtx
def mtx_refl(nv, reps=1): """ Generate block-diagonal reflection matrix about nv. reps must be >=1 and indicates the number of times the reflection matrix should be repeated along the block diagonal. Typically this will be the number of atoms in a geometry. .. todo:: Complete mtx_refl docstring """ # Imports import numpy as np from scipy import linalg as spla from ..const import PRM # Ensure |nv| is large enough for confident directionality if spla.norm(nv) < PRM.ZERO_VEC_TOL: raise ValueError("Norm of 'nv' is too small.") ## end if # Ensure nv is a normalized np.float64 3-vector nv = make_nd_vec(nv, nd=3, t=np.float64, norm=True) # Ensure reps is a positive scalar integer if not np.isscalar(reps): raise ValueError("'reps' must be scalar.") ## end if if not np.issubdtype(type(reps), int): raise ValueError("'reps' must be an integer.") ## end if if not reps > 0: raise ValueError("'reps' must be a positive integer.") ## end if # Initialize the single-point reflection transform matrix base_mtx = np.zeros(shape=(3,3), dtype=np.float64) # Construct the single-point transform matrix for i in range(3): for j in range(i,3): if i==j: base_mtx[i,j] = 1 - 2*nv[i]**2 else: base_mtx[i,j] = base_mtx[j,i] = -2*nv[i]*nv[j] ## end if ## next j ## next i # Construct the block-diagonal replicated reflection matrix refl_mtx= spla.block_diag(*[base_mtx for i in range(reps)]) # Return the result return refl_mtx
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:mtx_rot; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:ax; 5, identifier:theta; 6, default_parameter; 6, 7; 6, 8; 7, identifier:reps; 8, integer:1; 9, block; 9, 10; 9, 12; 9, 13; 9, 18; 9, 25; 9, 32; 9, 33; 9, 50; 9, 51; 9, 52; 9, 70; 9, 71; 9, 85; 9, 86; 9, 104; 9, 105; 9, 116; 9, 117; 9, 118; 9, 132; 9, 133; 9, 134; 9, 175; 9, 176; 9, 200; 9, 201; 9, 202; 9, 247; 9, 248; 9, 265; 9, 266; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, import_statement; 13, 14; 14, aliased_import; 14, 15; 14, 17; 15, dotted_name; 15, 16; 16, identifier:numpy; 17, identifier:np; 18, import_from_statement; 18, 19; 18, 21; 19, dotted_name; 19, 20; 20, identifier:scipy; 21, aliased_import; 21, 22; 21, 24; 22, dotted_name; 22, 23; 23, identifier:linalg; 24, identifier:spla; 25, import_from_statement; 25, 26; 25, 30; 26, relative_import; 26, 27; 26, 28; 27, import_prefix; 28, dotted_name; 28, 29; 29, identifier:const; 30, dotted_name; 30, 31; 31, identifier:PRM; 32, comment; 33, if_statement; 33, 34; 33, 44; 34, comparison_operator:<; 34, 35; 34, 41; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:spla; 38, identifier:norm; 39, argument_list; 39, 40; 40, identifier:ax; 41, attribute; 41, 42; 41, 43; 42, identifier:PRM; 43, identifier:ZERO_VEC_TOL; 44, block; 44, 45; 45, raise_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:ValueError; 48, argument_list; 48, 49; 49, string:"Norm of 'ax' is too small."; 50, comment; 51, comment; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:ax; 55, call; 55, 56; 55, 57; 56, identifier:make_nd_vec; 57, argument_list; 57, 58; 57, 59; 57, 62; 57, 67; 58, identifier:ax; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:nd; 61, integer:3; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:t; 64, attribute; 64, 65; 64, 66; 65, identifier:np; 66, identifier:float64; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:norm; 69, True; 70, comment; 71, if_statement; 71, 72; 71, 79; 72, not_operator; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:np; 76, identifier:isscalar; 77, argument_list; 77, 78; 78, identifier:reps; 79, block; 79, 80; 80, raise_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:ValueError; 83, argument_list; 83, 84; 84, string:"'reps' must be scalar."; 85, comment; 86, if_statement; 86, 87; 86, 98; 87, not_operator; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:np; 91, identifier:issubdtype; 92, argument_list; 92, 93; 92, 97; 93, call; 93, 94; 93, 95; 94, identifier:type; 95, argument_list; 95, 96; 96, identifier:reps; 97, identifier:int; 98, block; 98, 99; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:ValueError; 102, argument_list; 102, 103; 103, string:"'reps' must be an integer."; 104, comment; 105, if_statement; 105, 106; 105, 110; 106, not_operator; 106, 107; 107, comparison_operator:>; 107, 108; 107, 109; 108, identifier:reps; 109, integer:0; 110, block; 110, 111; 111, raise_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:ValueError; 114, argument_list; 114, 115; 115, string:"'reps' must be a positive integer."; 116, comment; 117, comment; 118, if_statement; 118, 119; 118, 126; 119, not_operator; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:np; 123, identifier:isscalar; 124, argument_list; 124, 125; 125, identifier:theta; 126, block; 126, 127; 127, raise_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:ValueError; 130, argument_list; 130, 131; 131, string:"'theta' must be scalar."; 132, comment; 133, comment; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:mod_lc; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:np; 140, identifier:array; 141, argument_list; 141, 142; 141, 170; 142, list:[ [0, -ax[2], ax[1]], [ax[2], 0, -ax[0]], [-ax[1], ax[0], 0] ]; 142, 143; 142, 152; 142, 161; 143, list:[0, -ax[2], ax[1]]; 143, 144; 143, 145; 143, 149; 144, integer:0; 145, unary_operator:-; 145, 146; 146, subscript; 146, 147; 146, 148; 147, identifier:ax; 148, integer:2; 149, subscript; 149, 150; 149, 151; 150, identifier:ax; 151, integer:1; 152, list:[ax[2], 0, -ax[0]]; 152, 153; 152, 156; 152, 157; 153, subscript; 153, 154; 153, 155; 154, identifier:ax; 155, integer:2; 156, integer:0; 157, unary_operator:-; 157, 158; 158, subscript; 158, 159; 158, 160; 159, identifier:ax; 160, integer:0; 161, list:[-ax[1], ax[0], 0]; 161, 162; 161, 166; 161, 169; 162, unary_operator:-; 162, 163; 163, subscript; 163, 164; 163, 165; 164, identifier:ax; 165, integer:1; 166, subscript; 166, 167; 166, 168; 167, identifier:ax; 168, integer:0; 169, integer:0; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:dtype; 172, attribute; 172, 173; 172, 174; 173, identifier:np; 174, identifier:float64; 175, comment; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:ax_oprod; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:np; 182, identifier:dot; 183, argument_list; 183, 184; 183, 192; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:ax; 187, identifier:reshape; 188, argument_list; 188, 189; 189, tuple; 189, 190; 189, 191; 190, integer:3; 191, integer:1; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:ax; 195, identifier:reshape; 196, argument_list; 196, 197; 197, tuple; 197, 198; 197, 199; 198, integer:1; 199, integer:3; 200, comment; 201, comment; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:base_mtx; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:np; 208, identifier:add; 209, argument_list; 209, 210; 209, 239; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:np; 213, identifier:add; 214, argument_list; 214, 215; 214, 226; 215, binary_operator:*; 215, 216; 215, 225; 216, parenthesized_expression; 216, 217; 217, binary_operator:-; 217, 218; 217, 219; 218, float:1.0; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:np; 222, identifier:cos; 223, argument_list; 223, 224; 224, identifier:theta; 225, identifier:ax_oprod; 226, binary_operator:*; 226, 227; 226, 233; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:np; 230, identifier:cos; 231, argument_list; 231, 232; 232, identifier:theta; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:np; 236, identifier:eye; 237, argument_list; 237, 238; 238, integer:3; 239, binary_operator:*; 239, 240; 239, 246; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:np; 243, identifier:sin; 244, argument_list; 244, 245; 245, identifier:theta; 246, identifier:mod_lc; 247, comment; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:rot_mtx; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:spla; 254, identifier:block_diag; 255, argument_list; 255, 256; 256, list_splat; 256, 257; 257, list_comprehension; 257, 258; 257, 259; 258, identifier:base_mtx; 259, for_in_clause; 259, 260; 259, 261; 260, identifier:i; 261, call; 261, 262; 261, 263; 262, identifier:range; 263, argument_list; 263, 264; 264, identifier:reps; 265, comment; 266, return_statement; 266, 267; 267, identifier:rot_mtx
def mtx_rot(ax, theta, reps=1): """ Generate block-diagonal rotation matrix about ax. [copy handedness from somewhere] .. todo:: Complete mtx_rot docstring """ # Imports import numpy as np from scipy import linalg as spla from ..const import PRM # Ensure |ax| is large enough for confident directionality if spla.norm(ax) < PRM.ZERO_VEC_TOL: raise ValueError("Norm of 'ax' is too small.") ## end if # Ensure ax is a normalized np.float64 3-vector ax = make_nd_vec(ax, nd=3, t=np.float64, norm=True) # Ensure reps is a positive scalar integer if not np.isscalar(reps): raise ValueError("'reps' must be scalar.") ## end if if not np.issubdtype(type(reps), int): raise ValueError("'reps' must be an integer.") ## end if if not reps > 0: raise ValueError("'reps' must be a positive integer.") ## end if # Ensure theta is scalar if not np.isscalar(theta): raise ValueError("'theta' must be scalar.") ## end if # Assemble the modified Levi-Civita matrix mod_lc = np.array([ [0, -ax[2], ax[1]], [ax[2], 0, -ax[0]], [-ax[1], ax[0], 0] ], dtype=np.float64) # Compute the outer product of the axis vector ax_oprod = np.dot(ax.reshape((3,1)), ax.reshape((1,3))) # Construct the base matrix # Will need to refer to external math to explain this. base_mtx = np.add( np.add( (1.0 - np.cos(theta)) * ax_oprod, np.cos(theta) * np.eye(3) ), np.sin(theta) * mod_lc ) # Construct the block-diagonal replicated reflection matrix rot_mtx= spla.block_diag(*[base_mtx for i in range(reps)]) # Return the result return rot_mtx
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:ffd; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:items; 5, identifier:targets; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 24; 8, 42; 8, 55; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:sizes; 14, call; 14, 15; 14, 16; 15, identifier:zip; 16, argument_list; 16, 17; 16, 18; 17, identifier:items; 18, call; 18, 19; 18, 20; 19, identifier:weight; 20, argument_list; 20, 21; 20, 22; 21, identifier:items; 22, dictionary_splat; 22, 23; 23, identifier:kwargs; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:sizes; 27, call; 27, 28; 27, 29; 28, identifier:sorted; 29, argument_list; 29, 30; 29, 31; 29, 39; 30, identifier:sizes; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:key; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:operator; 36, identifier:itemgetter; 37, argument_list; 37, 38; 38, integer:1; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:reverse; 41, True; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:items; 45, call; 45, 46; 45, 47; 46, identifier:map; 47, argument_list; 47, 48; 47, 54; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:operator; 51, identifier:itemgetter; 52, argument_list; 52, 53; 53, integer:0; 54, identifier:sizes; 55, return_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:ff; 58, argument_list; 58, 59; 58, 60; 59, identifier:items; 60, identifier:targets
def ffd(items, targets, **kwargs): """First-Fit Decreasing This is perhaps the simplest packing heuristic; it simply packs items in the next available bin. This algorithm differs only from Next-Fit Decreasing in having a 'sort'; that is, the items are pre-sorted (largest to smallest). Complexity O(n^2) """ sizes = zip(items, weight(items, **kwargs)) sizes = sorted(sizes, key=operator.itemgetter(1), reverse=True) items = map(operator.itemgetter(0), sizes) return ff(items, targets)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:match_variant_sequence_to_reference_context; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:variant_sequence; 5, identifier:reference_context; 6, identifier:min_transcript_prefix_length; 7, identifier:max_transcript_mismatches; 8, default_parameter; 8, 9; 8, 10; 9, identifier:include_mismatches_after_variant; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:max_trimming_attempts; 13, integer:2; 14, block; 14, 15; 14, 17; 14, 21; 14, 22; 14, 23; 14, 210; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:variant_sequence_in_reading_frame; 20, None; 21, comment; 22, comment; 23, for_statement; 23, 24; 23, 25; 23, 31; 23, 32; 23, 33; 23, 34; 24, identifier:i; 25, call; 25, 26; 25, 27; 26, identifier:range; 27, argument_list; 27, 28; 28, binary_operator:+; 28, 29; 28, 30; 29, identifier:max_trimming_attempts; 30, integer:1; 31, comment; 32, comment; 33, comment; 34, block; 34, 35; 34, 70; 34, 87; 34, 102; 34, 109; 34, 116; 34, 123; 34, 139; 34, 143; 34, 150; 34, 159; 34, 178; 34, 179; 34, 180; 34, 181; 34, 189; 34, 199; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:variant_sequence_too_short; 38, parenthesized_expression; 38, 39; 39, boolean_operator:or; 39, 40; 39, 55; 40, parenthesized_expression; 40, 41; 41, boolean_operator:and; 41, 42; 41, 47; 42, comparison_operator:==; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:reference_context; 45, identifier:strand; 46, string:"+"; 47, comparison_operator:<; 47, 48; 47, 54; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:variant_sequence; 53, identifier:prefix; 54, identifier:min_transcript_prefix_length; 55, parenthesized_expression; 55, 56; 56, boolean_operator:and; 56, 57; 56, 62; 57, comparison_operator:==; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:reference_context; 60, identifier:strand; 61, string:"-"; 62, comparison_operator:<; 62, 63; 62, 69; 63, call; 63, 64; 63, 65; 64, identifier:len; 65, argument_list; 65, 66; 66, attribute; 66, 67; 66, 68; 67, identifier:variant_sequence; 68, identifier:suffix; 69, identifier:min_transcript_prefix_length; 70, if_statement; 70, 71; 70, 72; 71, identifier:variant_sequence_too_short; 72, block; 72, 73; 72, 85; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:logger; 77, identifier:info; 78, argument_list; 78, 79; 78, 80; 78, 81; 78, 82; 79, string:"Variant sequence %s shorter than min allowed %d (iter=%d)"; 80, identifier:variant_sequence; 81, identifier:min_transcript_prefix_length; 82, binary_operator:+; 82, 83; 82, 84; 83, identifier:i; 84, integer:1; 85, return_statement; 85, 86; 86, None; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 88, 91; 89, identifier:variant_sequence_in_reading_frame; 90, line_continuation:\; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:VariantSequenceInReadingFrame; 94, identifier:from_variant_sequence_and_reference_context; 95, argument_list; 95, 96; 95, 99; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:variant_sequence; 98, identifier:variant_sequence; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:reference_context; 101, identifier:reference_context; 102, if_statement; 102, 103; 102, 106; 103, comparison_operator:is; 103, 104; 103, 105; 104, identifier:variant_sequence_in_reading_frame; 105, None; 106, block; 106, 107; 107, return_statement; 107, 108; 108, None; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:n_mismatch_before_variant; 112, parenthesized_expression; 112, 113; 113, attribute; 113, 114; 113, 115; 114, identifier:variant_sequence_in_reading_frame; 115, identifier:number_mismatches_before_variant; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:n_mismatch_after_variant; 119, parenthesized_expression; 119, 120; 120, attribute; 120, 121; 120, 122; 121, identifier:variant_sequence_in_reading_frame; 122, identifier:number_mismatches_after_variant; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:logger; 127, identifier:info; 128, argument_list; 128, 129; 129, binary_operator:%; 129, 130; 129, 131; 130, string:"Iter #%d/%d: %s"; 131, tuple; 131, 132; 131, 135; 131, 138; 132, binary_operator:+; 132, 133; 132, 134; 133, identifier:i; 134, integer:1; 135, binary_operator:+; 135, 136; 135, 137; 136, identifier:max_trimming_attempts; 137, integer:1; 138, identifier:variant_sequence_in_reading_frame; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:total_mismatches; 142, identifier:n_mismatch_before_variant; 143, if_statement; 143, 144; 143, 145; 144, identifier:include_mismatches_after_variant; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, augmented_assignment:+=; 147, 148; 147, 149; 148, identifier:total_mismatches; 149, identifier:n_mismatch_after_variant; 150, if_statement; 150, 151; 150, 154; 150, 155; 150, 156; 151, comparison_operator:<=; 151, 152; 151, 153; 152, identifier:total_mismatches; 153, identifier:max_transcript_mismatches; 154, comment; 155, comment; 156, block; 156, 157; 157, return_statement; 157, 158; 158, identifier:variant_sequence_in_reading_frame; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:logger; 163, identifier:info; 164, argument_list; 164, 165; 164, 169; 164, 170; 164, 171; 164, 172; 164, 175; 165, parenthesized_expression; 165, 166; 166, concatenated_string; 166, 167; 166, 168; 167, string:"Too many mismatches (%d) between variant sequence %s and "; 168, string:"reference context %s (attempt=%d/%d)"; 169, identifier:n_mismatch_before_variant; 170, identifier:variant_sequence; 171, identifier:reference_context; 172, binary_operator:+; 172, 173; 172, 174; 173, identifier:i; 174, integer:1; 175, binary_operator:+; 175, 176; 175, 177; 176, identifier:max_trimming_attempts; 177, integer:1; 178, comment; 179, comment; 180, comment; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:current_min_coverage; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:variant_sequence; 187, identifier:min_coverage; 188, argument_list; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:logger; 193, identifier:info; 194, argument_list; 194, 195; 194, 196; 195, string:"Trimming to subsequence covered by at least %d reads"; 196, binary_operator:+; 196, 197; 196, 198; 197, identifier:current_min_coverage; 198, integer:1; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:variant_sequence; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:variant_sequence; 205, identifier:trim_by_coverage; 206, argument_list; 206, 207; 207, binary_operator:+; 207, 208; 207, 209; 208, identifier:current_min_coverage; 209, integer:1; 210, return_statement; 210, 211; 211, None
def match_variant_sequence_to_reference_context( variant_sequence, reference_context, min_transcript_prefix_length, max_transcript_mismatches, include_mismatches_after_variant=False, max_trimming_attempts=2): """ Iteratively trim low-coverage subsequences of a variant sequence until it either matches the given reference context or there are too few nucleotides left in the variant sequence. Parameters ---------- variant_sequence : VariantSequence Assembled sequence from RNA reads, will need to be to be reverse complemented if matching against a reference transcript on the negative strand. reference_context : ReferenceContext Sequence of reference transcript before the variant and associated metadata. min_transcript_prefix_length : int Minimum number of nucleotides we try to match against a reference transcript. max_transcript_mismatches : int Maximum number of nucleotide differences between reference transcript sequence and the variant sequence. include_mismatches_after_variant : bool Set to true if the number of mismatches after the variant locus should count toward the total max_transcript_mismatches, which by default only counts mismatches before the variant locus. max_trimming_attempts : int How many times do we try trimming the VariantSequence to higher levels of coverage before giving up? Returns VariantSequenceInReadingFrame or None """ variant_sequence_in_reading_frame = None # if we can't get the variant sequence to match this reference # context then keep trimming it by coverage until either for i in range(max_trimming_attempts + 1): # check the reverse-complemented prefix if the reference context is # on the negative strand since variant sequence is aligned to # genomic DNA (positive strand) variant_sequence_too_short = ( (reference_context.strand == "+" and len(variant_sequence.prefix) < min_transcript_prefix_length) or (reference_context.strand == "-" and len(variant_sequence.suffix) < min_transcript_prefix_length) ) if variant_sequence_too_short: logger.info( "Variant sequence %s shorter than min allowed %d (iter=%d)", variant_sequence, min_transcript_prefix_length, i + 1) return None variant_sequence_in_reading_frame = \ VariantSequenceInReadingFrame.from_variant_sequence_and_reference_context( variant_sequence=variant_sequence, reference_context=reference_context) if variant_sequence_in_reading_frame is None: return None n_mismatch_before_variant = ( variant_sequence_in_reading_frame.number_mismatches_before_variant) n_mismatch_after_variant = ( variant_sequence_in_reading_frame.number_mismatches_after_variant) logger.info("Iter #%d/%d: %s" % ( i + 1, max_trimming_attempts + 1, variant_sequence_in_reading_frame)) total_mismatches = n_mismatch_before_variant if include_mismatches_after_variant: total_mismatches += n_mismatch_after_variant if total_mismatches <= max_transcript_mismatches: # if we got a variant sequence + reading frame with sufficiently # few mismatches then call it a day return variant_sequence_in_reading_frame logger.info( ("Too many mismatches (%d) between variant sequence %s and " "reference context %s (attempt=%d/%d)"), n_mismatch_before_variant, variant_sequence, reference_context, i + 1, max_trimming_attempts + 1) # if portions of the sequence are supported by only 1 read # then try trimming to 2 to see if the better supported # subsequence can be better matched against the reference current_min_coverage = variant_sequence.min_coverage() logger.info( "Trimming to subsequence covered by at least %d reads", current_min_coverage + 1) variant_sequence = variant_sequence.trim_by_coverage( current_min_coverage + 1) return None
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:predicted_effects_for_variant; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:variant; 5, default_parameter; 5, 6; 5, 7; 6, identifier:transcript_id_whitelist; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:only_coding_changes; 10, True; 11, block; 11, 12; 11, 14; 11, 18; 11, 81; 11, 88; 11, 95; 11, 106; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:effects; 17, list:[]; 18, for_statement; 18, 19; 18, 20; 18, 23; 19, identifier:transcript; 20, attribute; 20, 21; 20, 22; 21, identifier:variant; 22, identifier:transcripts; 23, block; 23, 24; 23, 44; 23, 69; 24, if_statement; 24, 25; 24, 31; 25, boolean_operator:and; 25, 26; 25, 27; 26, identifier:only_coding_changes; 27, not_operator; 27, 28; 28, attribute; 28, 29; 28, 30; 29, identifier:transcript; 30, identifier:complete; 31, block; 31, 32; 31, 43; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:logger; 36, identifier:info; 37, argument_list; 37, 38; 37, 39; 37, 42; 38, string:"Skipping transcript %s for variant %s because it's incomplete"; 39, attribute; 39, 40; 39, 41; 40, identifier:transcript; 41, identifier:name; 42, identifier:variant; 43, continue_statement; 44, if_statement; 44, 45; 44, 52; 45, boolean_operator:and; 45, 46; 45, 47; 46, identifier:transcript_id_whitelist; 47, comparison_operator:not; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:transcript; 50, identifier:id; 51, identifier:transcript_id_whitelist; 52, block; 52, 53; 52, 68; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:logger; 57, identifier:info; 58, argument_list; 58, 59; 58, 60; 58, 63; 58, 64; 59, string:"Skipping transcript %s for variant %s because it's not one of %d allowed"; 60, attribute; 60, 61; 60, 62; 61, identifier:transcript; 62, identifier:name; 63, identifier:variant; 64, call; 64, 65; 64, 66; 65, identifier:len; 66, argument_list; 66, 67; 67, identifier:transcript_id_whitelist; 68, continue_statement; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:effects; 73, identifier:append; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:variant; 78, identifier:effect_on_transcript; 79, argument_list; 79, 80; 80, identifier:transcript; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:effects; 84, call; 84, 85; 84, 86; 85, identifier:EffectCollection; 86, argument_list; 86, 87; 87, identifier:effects; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:n_total_effects; 91, call; 91, 92; 91, 93; 92, identifier:len; 93, argument_list; 93, 94; 94, identifier:effects; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:logger; 99, identifier:info; 100, argument_list; 100, 101; 101, binary_operator:%; 101, 102; 101, 103; 102, string:"Predicted total %d effects for variant %s"; 103, tuple; 103, 104; 103, 105; 104, identifier:n_total_effects; 105, identifier:variant; 106, if_statement; 106, 107; 106, 109; 106, 112; 107, not_operator; 107, 108; 108, identifier:only_coding_changes; 109, block; 109, 110; 110, return_statement; 110, 111; 111, identifier:effects; 112, else_clause; 112, 113; 113, block; 113, 114; 113, 122; 113, 136; 113, 150; 113, 163; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:nonsynonymous_coding_effects; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:effects; 120, identifier:drop_silent_and_noncoding; 121, argument_list; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:logger; 126, identifier:info; 127, argument_list; 127, 128; 127, 129; 127, 133; 127, 134; 127, 135; 128, string:"Keeping %d/%d effects which affect protein coding sequence for %s: %s"; 129, call; 129, 130; 129, 131; 130, identifier:len; 131, argument_list; 131, 132; 132, identifier:nonsynonymous_coding_effects; 133, identifier:n_total_effects; 134, identifier:variant; 135, identifier:nonsynonymous_coding_effects; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:usable_effects; 139, list_comprehension; 139, 140; 139, 141; 139, 144; 140, identifier:effect; 141, for_in_clause; 141, 142; 141, 143; 142, identifier:effect; 143, identifier:nonsynonymous_coding_effects; 144, if_clause; 144, 145; 145, comparison_operator:is; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:effect; 148, identifier:mutant_protein_sequence; 149, None; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:logger; 154, identifier:info; 155, argument_list; 155, 156; 155, 157; 155, 161; 155, 162; 156, string:"Keeping %d effects with predictable AA sequences for %s: %s"; 157, call; 157, 158; 157, 159; 158, identifier:len; 159, argument_list; 159, 160; 160, identifier:usable_effects; 161, identifier:variant; 162, identifier:usable_effects; 163, return_statement; 163, 164; 164, identifier:usable_effects
def predicted_effects_for_variant( variant, transcript_id_whitelist=None, only_coding_changes=True): """ For a given variant, return its set of predicted effects. Optionally filter to transcripts where this variant results in a non-synonymous change to the protein sequence. Parameters ---------- variant : varcode.Variant transcript_id_whitelist : set Filter effect predictions to only include these transcripts Returns a varcode.EffectCollection object """ effects = [] for transcript in variant.transcripts: if only_coding_changes and not transcript.complete: logger.info( "Skipping transcript %s for variant %s because it's incomplete", transcript.name, variant) continue if transcript_id_whitelist and transcript.id not in transcript_id_whitelist: logger.info( "Skipping transcript %s for variant %s because it's not one of %d allowed", transcript.name, variant, len(transcript_id_whitelist)) continue effects.append(variant.effect_on_transcript(transcript)) effects = EffectCollection(effects) n_total_effects = len(effects) logger.info("Predicted total %d effects for variant %s" % ( n_total_effects, variant)) if not only_coding_changes: return effects else: nonsynonymous_coding_effects = effects.drop_silent_and_noncoding() logger.info( "Keeping %d/%d effects which affect protein coding sequence for %s: %s", len(nonsynonymous_coding_effects), n_total_effects, variant, nonsynonymous_coding_effects) usable_effects = [ effect for effect in nonsynonymous_coding_effects if effect.mutant_protein_sequence is not None ] logger.info( "Keeping %d effects with predictable AA sequences for %s: %s", len(usable_effects), variant, usable_effects) return usable_effects
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_protein_sequences; 3, parameters; 3, 4; 4, identifier:protein_sequences; 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:list; 11, argument_list; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:sorted; 14, argument_list; 14, 15; 14, 16; 14, 21; 15, identifier:protein_sequences; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:key; 18, attribute; 18, 19; 18, 20; 19, identifier:ProteinSequence; 20, identifier:ascending_sort_key; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:reverse; 23, True
def sort_protein_sequences(protein_sequences): """ Sort protein sequences in decreasing order of priority """ return list( sorted( protein_sequences, key=ProteinSequence.ascending_sort_key, reverse=True))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:reads_generator_to_protein_sequences_generator; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:variant_and_overlapping_reads_generator; 5, default_parameter; 5, 6; 5, 7; 6, identifier:transcript_id_whitelist; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:protein_sequence_length; 10, identifier:PROTEIN_SEQUENCE_LENGTH; 11, default_parameter; 11, 12; 11, 13; 12, identifier:min_alt_rna_reads; 13, identifier:MIN_ALT_RNA_READS; 14, default_parameter; 14, 15; 14, 16; 15, identifier:min_variant_sequence_coverage; 16, identifier:MIN_VARIANT_SEQUENCE_COVERAGE; 17, default_parameter; 17, 18; 17, 19; 18, identifier:min_transcript_prefix_length; 19, identifier:MIN_TRANSCRIPT_PREFIX_LENGTH; 20, default_parameter; 20, 21; 20, 22; 21, identifier:max_transcript_mismatches; 22, identifier:MAX_REFERENCE_TRANSCRIPT_MISMATCHES; 23, default_parameter; 23, 24; 23, 25; 24, identifier:include_mismatches_after_variant; 25, identifier:INCLUDE_MISMATCHES_AFTER_VARIANT; 26, default_parameter; 26, 27; 26, 28; 27, identifier:max_protein_sequences_per_variant; 28, identifier:MAX_PROTEIN_SEQUENCES_PER_VARIANT; 29, default_parameter; 29, 30; 29, 31; 30, identifier:variant_sequence_assembly; 31, identifier:VARIANT_SEQUENCE_ASSEMBLY; 32, block; 32, 33; 32, 35; 33, expression_statement; 33, 34; 34, comment; 35, for_statement; 35, 36; 35, 39; 35, 40; 36, tuple_pattern; 36, 37; 36, 38; 37, identifier:variant; 38, identifier:overlapping_reads; 39, identifier:variant_and_overlapping_reads_generator; 40, block; 40, 41; 40, 57; 40, 67; 40, 74; 40, 81; 40, 91; 40, 101; 40, 137; 40, 141; 40, 245; 40, 246; 40, 253; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:overlapping_transcript_ids; 44, list_comprehension; 44, 45; 44, 48; 44, 53; 45, attribute; 45, 46; 45, 47; 46, identifier:t; 47, identifier:id; 48, for_in_clause; 48, 49; 48, 50; 49, identifier:t; 50, attribute; 50, 51; 50, 52; 51, identifier:variant; 52, identifier:transcripts; 53, if_clause; 53, 54; 54, attribute; 54, 55; 54, 56; 55, identifier:t; 56, identifier:is_protein_coding; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 63; 59, pattern_list; 59, 60; 59, 61; 59, 62; 60, identifier:_; 61, identifier:ref; 62, identifier:alt; 63, call; 63, 64; 63, 65; 64, identifier:trim_variant; 65, argument_list; 65, 66; 66, identifier:variant; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:overlapping_reads; 70, call; 70, 71; 70, 72; 71, identifier:list; 72, argument_list; 72, 73; 73, identifier:overlapping_reads; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:reads_grouped_by_allele; 77, call; 77, 78; 77, 79; 78, identifier:group_reads_by_allele; 79, argument_list; 79, 80; 80, identifier:overlapping_reads; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:ref_reads; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:reads_grouped_by_allele; 87, identifier:get; 88, argument_list; 88, 89; 88, 90; 89, identifier:ref; 90, list:[]; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:alt_reads; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:reads_grouped_by_allele; 97, identifier:get; 98, argument_list; 98, 99; 98, 100; 99, identifier:alt; 100, list:[]; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:translations; 104, call; 104, 105; 104, 106; 105, identifier:translate_variant_reads; 106, argument_list; 106, 107; 106, 110; 106, 113; 106, 116; 106, 119; 106, 122; 106, 125; 106, 128; 106, 131; 106, 134; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:variant; 109, identifier:variant; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:variant_reads; 112, identifier:alt_reads; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:transcript_id_whitelist; 115, identifier:transcript_id_whitelist; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:protein_sequence_length; 118, identifier:protein_sequence_length; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:min_alt_rna_reads; 121, identifier:min_alt_rna_reads; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:min_variant_sequence_coverage; 124, identifier:min_variant_sequence_coverage; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:min_transcript_prefix_length; 127, identifier:min_transcript_prefix_length; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:max_transcript_mismatches; 130, identifier:max_transcript_mismatches; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:include_mismatches_after_variant; 133, identifier:include_mismatches_after_variant; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:variant_sequence_assembly; 136, identifier:variant_sequence_assembly; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:protein_sequences; 140, list:[]; 141, for_statement; 141, 142; 141, 145; 141, 158; 141, 159; 141, 160; 142, tuple_pattern; 142, 143; 142, 144; 143, identifier:key; 144, identifier:equivalent_translations; 145, call; 145, 146; 145, 157; 146, attribute; 146, 147; 146, 156; 147, call; 147, 148; 147, 149; 148, identifier:groupby; 149, argument_list; 149, 150; 149, 151; 150, identifier:translations; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:key_fn; 153, attribute; 153, 154; 153, 155; 154, identifier:Translation; 155, identifier:as_translation_key; 156, identifier:items; 157, argument_list; 158, comment; 159, comment; 160, block; 160, 161; 160, 174; 160, 187; 160, 225; 160, 238; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 167; 162, 168; 163, pattern_list; 163, 164; 163, 165; 163, 166; 164, identifier:alt_reads_supporting_protein_sequence; 165, identifier:group_transcript_ids; 166, identifier:group_gene_names; 167, line_continuation:\; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:ProteinSequence; 171, identifier:_summarize_translations; 172, argument_list; 172, 173; 173, identifier:equivalent_translations; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:logger; 178, identifier:info; 179, argument_list; 179, 180; 179, 181; 179, 182; 179, 186; 180, string:"%s: %s alt reads supporting protein sequence (gene names = %s)"; 181, identifier:key; 182, call; 182, 183; 182, 184; 183, identifier:len; 184, argument_list; 184, 185; 185, identifier:alt_reads_supporting_protein_sequence; 186, identifier:group_gene_names; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:protein_sequence; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:ProteinSequence; 193, identifier:from_translation_key; 194, argument_list; 194, 195; 194, 198; 194, 201; 194, 204; 194, 207; 194, 210; 194, 213; 194, 216; 194, 219; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:translation_key; 197, identifier:key; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:translations; 200, identifier:equivalent_translations; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:overlapping_reads; 203, identifier:overlapping_reads; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:alt_reads; 206, identifier:alt_reads; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:ref_reads; 209, identifier:ref_reads; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:alt_reads_supporting_protein_sequence; 212, identifier:alt_reads_supporting_protein_sequence; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:transcripts_supporting_protein_sequence; 215, identifier:group_transcript_ids; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:transcripts_overlapping_variant; 218, identifier:overlapping_transcript_ids; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:gene; 221, call; 221, 222; 221, 223; 222, identifier:list; 223, argument_list; 223, 224; 224, identifier:group_gene_names; 225, expression_statement; 225, 226; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:logger; 229, identifier:info; 230, argument_list; 230, 231; 231, binary_operator:%; 231, 232; 231, 233; 232, string:"%s: protein sequence = %s"; 233, tuple; 233, 234; 233, 235; 234, identifier:key; 235, attribute; 235, 236; 235, 237; 236, identifier:protein_sequence; 237, identifier:amino_acids; 238, expression_statement; 238, 239; 239, call; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:protein_sequences; 242, identifier:append; 243, argument_list; 243, 244; 244, identifier:protein_sequence; 245, comment; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:protein_sequences; 249, call; 249, 250; 249, 251; 250, identifier:sort_protein_sequences; 251, argument_list; 251, 252; 252, identifier:protein_sequences; 253, expression_statement; 253, 254; 254, yield; 254, 255; 255, expression_list; 255, 256; 255, 257; 256, identifier:variant; 257, subscript; 257, 258; 257, 259; 258, identifier:protein_sequences; 259, slice; 259, 260; 259, 261; 260, colon; 261, identifier:max_protein_sequences_per_variant
def reads_generator_to_protein_sequences_generator( variant_and_overlapping_reads_generator, transcript_id_whitelist=None, protein_sequence_length=PROTEIN_SEQUENCE_LENGTH, min_alt_rna_reads=MIN_ALT_RNA_READS, min_variant_sequence_coverage=MIN_VARIANT_SEQUENCE_COVERAGE, min_transcript_prefix_length=MIN_TRANSCRIPT_PREFIX_LENGTH, max_transcript_mismatches=MAX_REFERENCE_TRANSCRIPT_MISMATCHES, include_mismatches_after_variant=INCLUDE_MISMATCHES_AFTER_VARIANT, max_protein_sequences_per_variant=MAX_PROTEIN_SEQUENCES_PER_VARIANT, variant_sequence_assembly=VARIANT_SEQUENCE_ASSEMBLY): """" Translates each coding variant in a collection to one or more Translation objects, which are then aggregated into equivalent ProteinSequence objects. Parameters ---------- variant_and_overlapping_reads_generator : generator Yields sequence of varcode.Variant objects paired with sequences of AlleleRead objects that support that variant. transcript_id_whitelist : set, optional If given, expected to be a set of transcript IDs which we should use for determining the reading frame around a variant. If omitted, then try to use all overlapping reference transcripts. protein_sequence_length : int Try to translate protein sequences of this length, though sometimes we'll have to return something shorter (depending on the RNAseq data, and presence of stop codons). min_alt_rna_reads : int Drop variant sequences at loci with fewer than this number of reads supporting the alt allele. min_variant_sequence_coverage : int Trim variant sequences to positions supported by at least this number of RNA reads. min_transcript_prefix_length : int Minimum number of bases we need to try matching between the reference context and variant sequence. max_transcript_mismatches : int Don't try to determine the reading frame for a transcript if more than this number of bases differ. include_mismatches_after_variant : bool Include mismatches after the variant locus in the count compared against max_transcript_mismatches. max_protein_sequences_per_variant : int Number of protein sequences to return for each ProteinSequence variant_cdna_sequence_assembly : bool If True, then assemble variant cDNA sequences based on overlap of RNA reads. If False, then variant cDNA sequences must be fully spanned and contained within RNA reads. Yields pairs of a Variant and a list of ProteinSequence objects """ for (variant, overlapping_reads) in variant_and_overlapping_reads_generator: overlapping_transcript_ids = [ t.id for t in variant.transcripts if t.is_protein_coding ] _, ref, alt = trim_variant(variant) overlapping_reads = list(overlapping_reads) reads_grouped_by_allele = group_reads_by_allele(overlapping_reads) ref_reads = reads_grouped_by_allele.get(ref, []) alt_reads = reads_grouped_by_allele.get(alt, []) translations = translate_variant_reads( variant=variant, variant_reads=alt_reads, transcript_id_whitelist=transcript_id_whitelist, protein_sequence_length=protein_sequence_length, min_alt_rna_reads=min_alt_rna_reads, min_variant_sequence_coverage=min_variant_sequence_coverage, min_transcript_prefix_length=min_transcript_prefix_length, max_transcript_mismatches=max_transcript_mismatches, include_mismatches_after_variant=include_mismatches_after_variant, variant_sequence_assembly=variant_sequence_assembly) protein_sequences = [] for (key, equivalent_translations) in groupby( translations, key_fn=Translation.as_translation_key).items(): # get the variant read names, transcript IDs and gene names for # protein sequence we're about to construct alt_reads_supporting_protein_sequence, group_transcript_ids, group_gene_names = \ ProteinSequence._summarize_translations(equivalent_translations) logger.info( "%s: %s alt reads supporting protein sequence (gene names = %s)", key, len(alt_reads_supporting_protein_sequence), group_gene_names) protein_sequence = ProteinSequence.from_translation_key( translation_key=key, translations=equivalent_translations, overlapping_reads=overlapping_reads, alt_reads=alt_reads, ref_reads=ref_reads, alt_reads_supporting_protein_sequence=alt_reads_supporting_protein_sequence, transcripts_supporting_protein_sequence=group_transcript_ids, transcripts_overlapping_variant=overlapping_transcript_ids, gene=list(group_gene_names)) logger.info("%s: protein sequence = %s" % (key, protein_sequence.amino_acids)) protein_sequences.append(protein_sequence) # sort protein sequences before returning the top results protein_sequences = sort_protein_sequences(protein_sequences) yield variant, protein_sequences[:max_protein_sequences_per_variant]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:from_variant_and_transcript; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:cls; 5, identifier:variant; 6, identifier:transcript; 7, identifier:context_size; 8, block; 8, 9; 8, 11; 8, 30; 8, 49; 8, 68; 8, 85; 8, 103; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 16; 12, not_operator; 12, 13; 13, attribute; 13, 14; 13, 15; 14, identifier:transcript; 15, identifier:contains_start_codon; 16, block; 16, 17; 16, 28; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:logger; 21, identifier:info; 22, argument_list; 22, 23; 22, 24; 22, 27; 23, string:"Expected transcript %s for variant %s to have start codon"; 24, attribute; 24, 25; 24, 26; 25, identifier:transcript; 26, identifier:name; 27, identifier:variant; 28, return_statement; 28, 29; 29, None; 30, if_statement; 30, 31; 30, 35; 31, not_operator; 31, 32; 32, attribute; 32, 33; 32, 34; 33, identifier:transcript; 34, identifier:contains_stop_codon; 35, block; 35, 36; 35, 47; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:logger; 40, identifier:info; 41, argument_list; 41, 42; 41, 43; 41, 46; 42, string:"Expected transcript %s for variant %s to have stop codon"; 43, attribute; 43, 44; 43, 45; 44, identifier:transcript; 45, identifier:name; 46, identifier:variant; 47, return_statement; 47, 48; 48, None; 49, if_statement; 49, 50; 49, 54; 50, not_operator; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:transcript; 53, identifier:protein_sequence; 54, block; 54, 55; 54, 66; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:logger; 59, identifier:info; 60, argument_list; 60, 61; 60, 62; 60, 65; 61, string:"Expected transript %s for variant %s to have protein sequence"; 62, attribute; 62, 63; 62, 64; 63, identifier:transcript; 64, identifier:name; 65, identifier:variant; 66, return_statement; 66, 67; 67, None; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:sequence_key; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:ReferenceSequenceKey; 74, identifier:from_variant_and_transcript; 75, argument_list; 75, 76; 75, 79; 75, 82; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:variant; 78, identifier:variant; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:transcript; 81, identifier:transcript; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:context_size; 84, identifier:context_size; 85, if_statement; 85, 86; 85, 89; 86, comparison_operator:is; 86, 87; 86, 88; 87, identifier:sequence_key; 88, None; 89, block; 89, 90; 89, 101; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:logger; 94, identifier:info; 95, argument_list; 95, 96; 95, 97; 95, 98; 96, string:"No sequence key for variant %s on transcript %s"; 97, identifier:variant; 98, attribute; 98, 99; 98, 100; 99, identifier:transcript; 100, identifier:name; 101, return_statement; 101, 102; 102, None; 103, return_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:cls; 107, identifier:from_variant_and_transcript_and_sequence_key; 108, argument_list; 108, 109; 108, 112; 108, 115; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:variant; 111, identifier:variant; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:transcript; 114, identifier:transcript; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:sequence_key; 117, identifier:sequence_key
def from_variant_and_transcript( cls, variant, transcript, context_size): """ Extracts the reference sequence around a variant locus on a particular transcript and determines the reading frame at the start of that sequence context. Parameters ---------- variant : varcode.Variant transcript : pyensembl.Transcript context_size : int Returns SequenceKeyWithReadingFrame object or None if Transcript lacks coding sequence, protein sequence or annotated start/stop codons. """ if not transcript.contains_start_codon: logger.info( "Expected transcript %s for variant %s to have start codon", transcript.name, variant) return None if not transcript.contains_stop_codon: logger.info( "Expected transcript %s for variant %s to have stop codon", transcript.name, variant) return None if not transcript.protein_sequence: logger.info( "Expected transript %s for variant %s to have protein sequence", transcript.name, variant) return None sequence_key = ReferenceSequenceKey.from_variant_and_transcript( variant=variant, transcript=transcript, context_size=context_size) if sequence_key is None: logger.info( "No sequence key for variant %s on transcript %s", variant, transcript.name) return None return cls.from_variant_and_transcript_and_sequence_key( variant=variant, transcript=transcript, sequence_key=sequence_key)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:info; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:package; 5, identifier:long_description; 6, identifier:classifiers; 7, identifier:license; 8, block; 8, 9; 8, 11; 8, 19; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:client; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:requests; 17, identifier:Session; 18, argument_list; 19, for_statement; 19, 20; 19, 21; 19, 22; 20, identifier:name_or_url; 21, identifier:package; 22, block; 22, 23; 22, 31; 22, 56; 22, 57; 22, 93; 22, 98; 22, 110; 22, 111; 22, 115; 22, 129; 22, 130; 22, 144; 22, 145; 22, 149; 22, 154; 22, 155; 22, 159; 22, 171; 22, 187; 22, 203; 22, 204; 22, 216; 22, 225; 22, 241; 22, 257; 22, 258; 22, 262; 22, 276; 22, 295; 22, 314; 22, 315; 22, 344; 22, 397; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:package; 26, call; 26, 27; 26, 28; 27, identifier:get_package; 28, argument_list; 28, 29; 28, 30; 29, identifier:name_or_url; 30, identifier:client; 31, if_statement; 31, 32; 31, 34; 32, not_operator; 32, 33; 33, identifier:package; 34, block; 34, 35; 34, 55; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:secho; 38, argument_list; 38, 39; 38, 47; 38, 50; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, string:u'Invalid name or URL: "{name}"'; 42, identifier:format; 43, argument_list; 43, 44; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:name; 46, identifier:name_or_url; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:fg; 49, string:'red'; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:file; 52, attribute; 52, 53; 52, 54; 53, identifier:sys; 54, identifier:stderr; 55, continue_statement; 56, comment; 57, try_statement; 57, 58; 57, 67; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:info; 62, subscript; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:package; 65, identifier:data; 66, string:'info'; 67, except_clause; 67, 68; 67, 69; 68, identifier:NotFoundError; 69, block; 69, 70; 69, 92; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:secho; 73, argument_list; 73, 74; 73, 84; 73, 87; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, concatenated_string; 76, 77; 76, 78; 77, string:u'No versions found for "{0}". '; 78, string:u'Skipping. . .'; 79, identifier:format; 80, argument_list; 80, 81; 81, attribute; 81, 82; 81, 83; 82, identifier:package; 83, identifier:name; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:fg; 86, string:'red'; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:file; 89, attribute; 89, 90; 89, 91; 90, identifier:sys; 91, identifier:stderr; 92, continue_statement; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:echo_header; 96, argument_list; 96, 97; 97, identifier:name_or_url; 98, if_statement; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:package; 101, identifier:summary; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:echo; 106, argument_list; 106, 107; 107, attribute; 107, 108; 107, 109; 108, identifier:package; 109, identifier:summary; 110, comment; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 114; 113, identifier:echo; 114, argument_list; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:echo; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, string:'Latest release: {version:12}'; 122, identifier:format; 123, argument_list; 123, 124; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:version; 126, subscript; 126, 127; 126, 128; 127, identifier:info; 128, string:'version'; 129, comment; 130, if_statement; 130, 131; 130, 132; 131, identifier:long_description; 132, block; 132, 133; 132, 137; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:echo; 136, argument_list; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:echo; 140, argument_list; 140, 141; 141, attribute; 141, 142; 141, 143; 142, identifier:package; 143, identifier:description; 144, comment; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:echo; 148, argument_list; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 152; 151, identifier:echo_download_summary; 152, argument_list; 152, 153; 153, identifier:package; 154, comment; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 158; 157, identifier:echo; 158, argument_list; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, pattern_list; 161, 162; 161, 163; 162, identifier:author; 163, identifier:author_email; 164, expression_list; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:package; 167, identifier:author; 168, attribute; 168, 169; 168, 170; 169, identifier:package; 170, identifier:author_email; 171, if_statement; 171, 172; 171, 173; 172, identifier:author; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 177; 176, identifier:echo; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, string:u'Author: {author:12}'; 181, identifier:format; 182, argument_list; 182, 183; 183, dictionary_splat; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:locals; 186, argument_list; 187, if_statement; 187, 188; 187, 189; 188, identifier:author_email; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:echo; 193, argument_list; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, string:u'Author email: {author_email:12}'; 197, identifier:format; 198, argument_list; 198, 199; 199, dictionary_splat; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:locals; 202, argument_list; 203, comment; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 209; 206, pattern_list; 206, 207; 206, 208; 207, identifier:maintainer; 208, identifier:maintainer_email; 209, tuple; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:package; 212, identifier:maintainer; 213, attribute; 213, 214; 213, 215; 214, identifier:package; 215, identifier:maintainer_email; 216, if_statement; 216, 217; 216, 220; 217, boolean_operator:or; 217, 218; 217, 219; 218, identifier:maintainer; 219, identifier:maintainer_email; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 224; 223, identifier:echo; 224, argument_list; 225, if_statement; 225, 226; 225, 227; 226, identifier:maintainer; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 231; 230, identifier:echo; 231, argument_list; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, string:u'Maintainer: {maintainer:12}'; 235, identifier:format; 236, argument_list; 236, 237; 237, dictionary_splat; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:locals; 240, argument_list; 241, if_statement; 241, 242; 241, 243; 242, identifier:maintainer_email; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 247; 246, identifier:echo; 247, argument_list; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, string:u'Maintainer email: {maintainer_email:12}'; 251, identifier:format; 252, argument_list; 252, 253; 253, dictionary_splat; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:locals; 256, argument_list; 257, comment; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 261; 260, identifier:echo; 261, argument_list; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 265; 264, identifier:echo; 265, argument_list; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, string:u'PyPI URL: {pypi_url:12}'; 269, identifier:format; 270, argument_list; 270, 271; 271, keyword_argument; 271, 272; 271, 273; 272, identifier:pypi_url; 273, attribute; 273, 274; 273, 275; 274, identifier:package; 275, identifier:package_url; 276, if_statement; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:package; 279, identifier:home_page; 280, block; 280, 281; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 284; 283, identifier:echo; 284, argument_list; 284, 285; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, string:u'Home Page: {home_page:12}'; 288, identifier:format; 289, argument_list; 289, 290; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:home_page; 292, attribute; 292, 293; 292, 294; 293, identifier:package; 294, identifier:home_page; 295, if_statement; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:package; 298, identifier:docs_url; 299, block; 299, 300; 300, expression_statement; 300, 301; 301, call; 301, 302; 301, 303; 302, identifier:echo; 303, argument_list; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, string:u'Documentation: {docs_url:12}'; 307, identifier:format; 308, argument_list; 308, 309; 309, keyword_argument; 309, 310; 309, 311; 310, identifier:docs_url; 311, attribute; 311, 312; 311, 313; 312, identifier:package; 313, identifier:docs_url; 314, comment; 315, if_statement; 315, 316; 315, 317; 316, identifier:classifiers; 317, block; 317, 318; 317, 322; 317, 327; 318, expression_statement; 318, 319; 319, call; 319, 320; 319, 321; 320, identifier:echo; 321, argument_list; 322, expression_statement; 322, 323; 323, call; 323, 324; 323, 325; 324, identifier:echo; 325, argument_list; 325, 326; 326, string:u'Classifiers: '; 327, for_statement; 327, 328; 327, 329; 327, 336; 328, identifier:each; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:info; 332, identifier:get; 333, argument_list; 333, 334; 333, 335; 334, string:'classifiers'; 335, list:[]; 336, block; 336, 337; 337, expression_statement; 337, 338; 338, call; 338, 339; 338, 340; 339, identifier:echo; 340, argument_list; 340, 341; 341, binary_operator:+; 341, 342; 341, 343; 342, string:'\t'; 343, identifier:each; 344, if_statement; 344, 345; 344, 350; 345, boolean_operator:and; 345, 346; 345, 347; 346, identifier:license; 347, attribute; 347, 348; 347, 349; 348, identifier:package; 349, identifier:license; 350, block; 350, 351; 350, 355; 350, 363; 350, 364; 350, 365; 350, 390; 351, expression_statement; 351, 352; 352, call; 352, 353; 352, 354; 353, identifier:echo; 354, argument_list; 355, expression_statement; 355, 356; 356, call; 356, 357; 356, 358; 357, identifier:echo; 358, argument_list; 358, 359; 358, 360; 359, string:u'License: '; 360, keyword_argument; 360, 361; 360, 362; 361, identifier:nl; 362, False; 363, comment; 364, comment; 365, if_statement; 365, 366; 365, 385; 366, boolean_operator:or; 366, 367; 366, 377; 367, comparison_operator:>=; 367, 368; 367, 376; 368, call; 368, 369; 368, 374; 369, attribute; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:package; 372, identifier:license; 373, identifier:find; 374, argument_list; 374, 375; 375, string:'\n'; 376, integer:0; 377, comparison_operator:>; 377, 378; 377, 384; 378, call; 378, 379; 378, 380; 379, identifier:len; 380, argument_list; 380, 381; 381, attribute; 381, 382; 381, 383; 382, identifier:package; 383, identifier:license; 384, integer:80; 385, block; 385, 386; 386, expression_statement; 386, 387; 387, call; 387, 388; 387, 389; 388, identifier:echo; 389, argument_list; 390, expression_statement; 390, 391; 391, call; 391, 392; 391, 393; 392, identifier:echo; 393, argument_list; 393, 394; 394, attribute; 394, 395; 394, 396; 395, identifier:package; 396, identifier:license; 397, expression_statement; 397, 398; 398, call; 398, 399; 398, 400; 399, identifier:echo; 400, argument_list
def info(package, long_description, classifiers, license): """Get info about a package or packages. """ client = requests.Session() for name_or_url in package: package = get_package(name_or_url, client) if not package: secho(u'Invalid name or URL: "{name}"'.format(name=name_or_url), fg='red', file=sys.stderr) continue # Name and summary try: info = package.data['info'] except NotFoundError: secho(u'No versions found for "{0}". ' u'Skipping. . .'.format(package.name), fg='red', file=sys.stderr) continue echo_header(name_or_url) if package.summary: echo(package.summary) # Version info echo() echo('Latest release: {version:12}'.format(version=info['version'])) # Long description if long_description: echo() echo(package.description) # Download info echo() echo_download_summary(package) # Author info echo() author, author_email = package.author, package.author_email if author: echo(u'Author: {author:12}'.format(**locals())) if author_email: echo(u'Author email: {author_email:12}'.format(**locals())) # Maintainer info maintainer, maintainer_email = (package.maintainer, package.maintainer_email) if maintainer or maintainer_email: echo() if maintainer: echo(u'Maintainer: {maintainer:12}'.format(**locals())) if maintainer_email: echo(u'Maintainer email: {maintainer_email:12}'.format(**locals())) # URLS echo() echo(u'PyPI URL: {pypi_url:12}'.format(pypi_url=package.package_url)) if package.home_page: echo(u'Home Page: {home_page:12}'.format( home_page=package.home_page)) if package.docs_url: echo(u'Documentation: {docs_url:12}'.format( docs_url=package.docs_url)) # Classifiers if classifiers: echo() echo(u'Classifiers: ') for each in info.get('classifiers', []): echo('\t' + each) if license and package.license: echo() echo(u'License: ', nl=False) # license may be just a name, e.g. 'BSD' or the full license text # If a new line is found in the text, print a new line if package.license.find('\n') >= 0 or len(package.license) > 80: echo() echo(package.license) echo()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:install_packages; 3, parameters; 3, 4; 3, 5; 4, identifier:packages; 5, default_parameter; 5, 6; 5, 7; 6, identifier:what_for; 7, string:'for a complete setup to work properly'; 8, block; 8, 9; 8, 11; 8, 15; 8, 22; 8, 31; 8, 196; 9, expression_statement; 9, 10; 10, string:'''Try to install .deb packages given by list. Return True, if packages could be installed or are installed already, or if they cannot be installed but the user gives feedback to continue. Else return False. '''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:res; 14, True; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:non_installed_packages; 18, call; 18, 19; 18, 20; 19, identifier:_non_installed; 20, argument_list; 20, 21; 21, identifier:packages; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:packages_str; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, string:' '; 28, identifier:join; 29, argument_list; 29, 30; 30, identifier:non_installed_packages; 31, if_statement; 31, 32; 31, 33; 32, identifier:non_installed_packages; 33, block; 33, 34; 33, 47; 33, 51; 33, 55; 33, 59; 33, 164; 33, 175; 34, with_statement; 34, 35; 34, 40; 35, with_clause; 35, 36; 36, with_item; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:quiet; 39, argument_list; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:dpkg; 44, call; 44, 45; 44, 46; 45, identifier:_has_dpkg; 46, argument_list; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:hint; 50, string:' (You may have to install them manually)'; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:do_install; 54, False; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:go_on; 58, True; 59, if_statement; 59, 60; 59, 61; 59, 120; 60, identifier:dpkg; 61, block; 61, 62; 62, if_statement; 62, 63; 62, 67; 62, 72; 63, call; 63, 64; 63, 65; 64, identifier:_is_sudoer; 65, argument_list; 65, 66; 66, string:'Want to install dpkg packages'; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:do_install; 71, True; 72, else_clause; 72, 73; 73, block; 73, 74; 73, 78; 73, 79; 73, 102; 73, 106; 74, expression_statement; 74, 75; 75, comparison_operator:is; 75, 76; 75, 77; 76, identifier:do_install; 77, False; 78, comment; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:info; 82, call; 82, 83; 82, 84; 83, identifier:yellow; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, string:' '; 88, identifier:join; 89, argument_list; 89, 90; 90, list:[ 'This deb packages are missing to be installed', flo("{what_for}: "), ', '.join(non_installed_packages), ]; 90, 91; 90, 92; 90, 96; 91, string:'This deb packages are missing to be installed'; 92, call; 92, 93; 92, 94; 93, identifier:flo; 94, argument_list; 94, 95; 95, string:"{what_for}: "; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, string:', '; 99, identifier:join; 100, argument_list; 100, 101; 101, identifier:non_installed_packages; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:question; 105, string:' Continue anyway?'; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:go_on; 109, call; 109, 110; 109, 111; 110, identifier:query_yes_no; 111, argument_list; 111, 112; 111, 117; 112, binary_operator:+; 112, 113; 112, 116; 113, binary_operator:+; 113, 114; 113, 115; 114, identifier:info; 115, identifier:hint; 116, identifier:question; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:default; 119, string:'no'; 120, else_clause; 120, 121; 120, 122; 121, comment; 122, block; 122, 123; 122, 127; 122, 128; 122, 150; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:do_install; 126, False; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:info; 131, call; 131, 132; 131, 133; 132, identifier:yellow; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, string:' '; 137, identifier:join; 138, argument_list; 138, 139; 139, list:[ flo('Required {what_for}: '), ', '.join(non_installed_packages), ]; 139, 140; 139, 144; 140, call; 140, 141; 140, 142; 141, identifier:flo; 142, argument_list; 142, 143; 143, string:'Required {what_for}: '; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, string:', '; 147, identifier:join; 148, argument_list; 148, 149; 149, identifier:non_installed_packages; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:go_on; 153, call; 153, 154; 153, 155; 154, identifier:query_yes_no; 155, argument_list; 155, 156; 155, 161; 156, binary_operator:+; 156, 157; 156, 160; 157, binary_operator:+; 157, 158; 157, 159; 158, identifier:info; 159, identifier:hint; 160, string:' Continue?'; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:default; 163, string:'yes'; 164, if_statement; 164, 165; 164, 167; 165, not_operator; 165, 166; 166, identifier:go_on; 167, block; 167, 168; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:sys; 172, identifier:exit; 173, argument_list; 173, 174; 174, string:'Abort'; 175, if_statement; 175, 176; 175, 177; 176, identifier:do_install; 177, block; 177, 178; 177, 185; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:command; 181, call; 181, 182; 181, 183; 182, identifier:flo; 183, argument_list; 183, 184; 184, string:'sudo apt-get install {packages_str}'; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:res; 188, comparison_operator:==; 188, 189; 188, 195; 189, attribute; 189, 190; 189, 194; 190, call; 190, 191; 190, 192; 191, identifier:run; 192, argument_list; 192, 193; 193, identifier:command; 194, identifier:return_code; 195, integer:0; 196, return_statement; 196, 197; 197, identifier:res
def install_packages(packages, what_for='for a complete setup to work properly'): '''Try to install .deb packages given by list. Return True, if packages could be installed or are installed already, or if they cannot be installed but the user gives feedback to continue. Else return False. ''' res = True non_installed_packages = _non_installed(packages) packages_str = ' '.join(non_installed_packages) if non_installed_packages: with quiet(): dpkg = _has_dpkg() hint = ' (You may have to install them manually)' do_install = False go_on = True if dpkg: if _is_sudoer('Want to install dpkg packages'): do_install = True else: do_install is False # cannot install anything info = yellow(' '.join([ 'This deb packages are missing to be installed', flo("{what_for}: "), ', '.join(non_installed_packages), ])) question = ' Continue anyway?' go_on = query_yes_no(info + hint + question, default='no') else: # dpkg == False, unable to determine if packages are installed do_install = False # cannot install anything info = yellow(' '.join([ flo('Required {what_for}: '), ', '.join(non_installed_packages), ])) go_on = query_yes_no(info + hint + ' Continue?', default='yes') if not go_on: sys.exit('Abort') if do_install: command = flo('sudo apt-get install {packages_str}') res = run(command).return_code == 0 return res
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:scan; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:host; 5, default_parameter; 5, 6; 5, 7; 6, identifier:port; 7, integer:80; 8, default_parameter; 8, 9; 8, 10; 9, identifier:url; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:https; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:timeout; 16, integer:1; 17, default_parameter; 17, 18; 17, 19; 18, identifier:max_size; 19, integer:65535; 20, block; 20, 21; 20, 23; 20, 29; 20, 35; 20, 42; 20, 62; 20, 75; 20, 91; 20, 92; 20, 134; 20, 135; 20, 147; 20, 161; 20, 168; 20, 179; 20, 191; 20, 192; 20, 243; 20, 244; 20, 371; 20, 377; 20, 378; 20, 390; 20, 421; 20, 432; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:starts; 26, call; 26, 27; 26, 28; 27, identifier:OrderedDict; 28, argument_list; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:ends; 32, call; 32, 33; 32, 34; 33, identifier:OrderedDict; 34, argument_list; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:port; 38, call; 38, 39; 38, 40; 39, identifier:int; 40, argument_list; 40, 41; 41, identifier:port; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:result; 45, call; 45, 46; 45, 47; 46, identifier:dict; 47, argument_list; 47, 48; 47, 51; 47, 54; 47, 57; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:host; 50, identifier:host; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:port; 53, identifier:port; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:state; 56, string:'closed'; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:durations; 59, call; 59, 60; 59, 61; 60, identifier:OrderedDict; 61, argument_list; 62, if_statement; 62, 63; 62, 64; 63, identifier:url; 64, block; 64, 65; 64, 69; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:timeout; 68, integer:1; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:result; 73, string:'code'; 74, None; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:starts; 79, string:'all'; 80, assignment; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:starts; 83, string:'dns'; 84, call; 84, 85; 84, 90; 85, attribute; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:datetime; 88, identifier:datetime; 89, identifier:now; 90, argument_list; 91, comment; 92, try_statement; 92, 93; 92, 121; 93, block; 93, 94; 93, 103; 93, 109; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:hostip; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:socket; 100, identifier:gethostbyname; 101, argument_list; 101, 102; 102, identifier:host; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:result; 107, string:'ip'; 108, identifier:hostip; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:ends; 113, string:'dns'; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:datetime; 118, identifier:datetime; 119, identifier:now; 120, argument_list; 121, except_clause; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:socket; 124, identifier:gaierror; 125, block; 125, 126; 126, raise_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:ScanFailed; 129, argument_list; 129, 130; 129, 131; 130, string:'DNS Lookup failed'; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:result; 133, identifier:result; 134, comment; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, subscript; 137, 138; 137, 139; 138, identifier:starts; 139, string:'connect'; 140, call; 140, 141; 140, 146; 141, attribute; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:datetime; 144, identifier:datetime; 145, identifier:now; 146, argument_list; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:network_socket; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:socket; 153, identifier:socket; 154, argument_list; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:socket; 157, identifier:AF_INET; 158, attribute; 158, 159; 158, 160; 159, identifier:socket; 160, identifier:SOCK_STREAM; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:network_socket; 165, identifier:settimeout; 166, argument_list; 166, 167; 167, identifier:timeout; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:result_connection; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:network_socket; 174, identifier:connect_ex; 175, argument_list; 175, 176; 176, tuple; 176, 177; 176, 178; 177, identifier:hostip; 178, identifier:port; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 184; 181, subscript; 181, 182; 181, 183; 182, identifier:ends; 183, string:'connect'; 184, call; 184, 185; 184, 190; 185, attribute; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:datetime; 188, identifier:datetime; 189, identifier:now; 190, argument_list; 191, comment; 192, if_statement; 192, 193; 192, 194; 193, identifier:https; 194, block; 194, 195; 194, 207; 194, 231; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 200; 197, subscript; 197, 198; 197, 199; 198, identifier:starts; 199, string:'ssl'; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:datetime; 204, identifier:datetime; 205, identifier:now; 206, argument_list; 207, try_statement; 207, 208; 207, 218; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:network_socket; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:ssl; 215, identifier:wrap_socket; 216, argument_list; 216, 217; 217, identifier:network_socket; 218, except_clause; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:socket; 221, identifier:timeout; 222, block; 222, 223; 223, raise_statement; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:ScanFailed; 226, argument_list; 226, 227; 226, 228; 227, string:'SSL socket timeout'; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:result; 230, identifier:result; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 236; 233, subscript; 233, 234; 233, 235; 234, identifier:ends; 235, string:'ssl'; 236, call; 236, 237; 236, 242; 237, attribute; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:datetime; 240, identifier:datetime; 241, identifier:now; 242, argument_list; 243, comment; 244, if_statement; 244, 245; 244, 250; 245, boolean_operator:and; 245, 246; 245, 249; 246, comparison_operator:==; 246, 247; 246, 248; 247, identifier:result_connection; 248, integer:0; 249, identifier:url; 250, block; 250, 251; 250, 263; 250, 281; 250, 303; 250, 312; 250, 324; 250, 331; 250, 359; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 256; 253, subscript; 253, 254; 253, 255; 254, identifier:starts; 255, string:'request'; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:datetime; 260, identifier:datetime; 261, identifier:now; 262, argument_list; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:network_socket; 267, identifier:send; 268, argument_list; 268, 269; 269, call; 269, 270; 269, 279; 270, attribute; 270, 271; 270, 278; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, string:"GET {0} HTTP/1.0\r\nHost: {1}\r\n\r\n"; 274, identifier:format; 275, argument_list; 275, 276; 275, 277; 276, identifier:url; 277, identifier:host; 278, identifier:encode; 279, argument_list; 279, 280; 280, string:'ascii'; 281, if_statement; 281, 282; 281, 283; 281, 293; 282, identifier:max_size; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:data; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:network_socket; 290, identifier:recv; 291, argument_list; 291, 292; 292, identifier:max_size; 293, else_clause; 293, 294; 294, block; 294, 295; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:data; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:network_socket; 301, identifier:recv; 302, argument_list; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 308; 305, subscript; 305, 306; 305, 307; 306, identifier:result; 307, string:'length'; 308, call; 308, 309; 308, 310; 309, identifier:len; 310, argument_list; 310, 311; 311, identifier:data; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:data; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:data; 318, identifier:decode; 319, argument_list; 319, 320; 319, 321; 320, string:'ascii'; 321, keyword_argument; 321, 322; 321, 323; 322, identifier:errors; 323, string:'ignore'; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 329; 326, subscript; 326, 327; 326, 328; 327, identifier:result; 328, string:'response'; 329, parenthesized_expression; 329, 330; 330, identifier:data; 331, try_statement; 331, 332; 331, 355; 332, block; 332, 333; 333, expression_statement; 333, 334; 334, assignment; 334, 335; 334, 338; 335, subscript; 335, 336; 335, 337; 336, identifier:result; 337, string:'code'; 338, call; 338, 339; 338, 340; 339, identifier:int; 340, argument_list; 340, 341; 341, subscript; 341, 342; 341, 354; 342, call; 342, 343; 342, 353; 343, attribute; 343, 344; 343, 352; 344, subscript; 344, 345; 344, 351; 345, call; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:data; 348, identifier:split; 349, argument_list; 349, 350; 350, string:'\n'; 351, integer:0; 352, identifier:split; 353, argument_list; 354, integer:1; 355, except_clause; 355, 356; 355, 357; 356, identifier:IndexError; 357, block; 357, 358; 358, pass_statement; 359, expression_statement; 359, 360; 360, assignment; 360, 361; 360, 364; 361, subscript; 361, 362; 361, 363; 362, identifier:ends; 363, string:'request'; 364, call; 364, 365; 364, 370; 365, attribute; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:datetime; 368, identifier:datetime; 369, identifier:now; 370, argument_list; 371, expression_statement; 371, 372; 372, call; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:network_socket; 375, identifier:close; 376, argument_list; 377, comment; 378, expression_statement; 378, 379; 379, assignment; 379, 380; 379, 383; 380, subscript; 380, 381; 380, 382; 381, identifier:ends; 382, string:'all'; 383, call; 383, 384; 383, 389; 384, attribute; 384, 385; 384, 388; 385, attribute; 385, 386; 385, 387; 386, identifier:datetime; 387, identifier:datetime; 388, identifier:now; 389, argument_list; 390, for_statement; 390, 391; 390, 392; 390, 397; 391, identifier:duration; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:starts; 395, identifier:keys; 396, argument_list; 397, block; 397, 398; 398, if_statement; 398, 399; 398, 406; 399, comparison_operator:in; 399, 400; 399, 401; 400, identifier:duration; 401, call; 401, 402; 401, 405; 402, attribute; 402, 403; 402, 404; 403, identifier:ends; 404, identifier:keys; 405, argument_list; 406, block; 406, 407; 407, expression_statement; 407, 408; 408, assignment; 408, 409; 408, 414; 409, subscript; 409, 410; 409, 413; 410, subscript; 410, 411; 410, 412; 411, identifier:result; 412, string:'durations'; 413, identifier:duration; 414, binary_operator:-; 414, 415; 414, 418; 415, subscript; 415, 416; 415, 417; 416, identifier:ends; 417, identifier:duration; 418, subscript; 418, 419; 418, 420; 419, identifier:starts; 420, identifier:duration; 421, if_statement; 421, 422; 421, 425; 422, comparison_operator:==; 422, 423; 422, 424; 423, identifier:result_connection; 424, integer:0; 425, block; 425, 426; 426, expression_statement; 426, 427; 427, assignment; 427, 428; 427, 431; 428, subscript; 428, 429; 428, 430; 429, identifier:result; 430, string:'state'; 431, string:'open'; 432, return_statement; 432, 433; 433, identifier:result
def scan(host, port=80, url=None, https=False, timeout=1, max_size=65535): """ Scan a network port Parameters ---------- host : str Host or ip address to scan port : int, optional Port to scan, default=80 url : str, optional URL to perform get request to on the host and port specified https : bool, optional Perform ssl connection on the socket, default=False timeout : float Timeout for network operations, default=1 Returns ------- dict Result dictionary that contains the following keys: host - The host or IP address that was scanned port - The port number that was scanned state - The state of the port, will be either "open" or "closed" durations - An ordered dictionary with floating point value of the time elapsed for each connection operation Raises ------ ScanFailed - The scan operation failed """ starts = OrderedDict() ends = OrderedDict() port = int(port) result = dict( host=host, port=port, state='closed', durations=OrderedDict() ) if url: timeout = 1 result['code'] = None starts['all'] = starts['dns'] = datetime.datetime.now() # DNS Lookup try: hostip = socket.gethostbyname(host) result['ip'] = hostip ends['dns'] = datetime.datetime.now() except socket.gaierror: raise ScanFailed('DNS Lookup failed', result=result) # TCP Connect starts['connect'] = datetime.datetime.now() network_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) network_socket.settimeout(timeout) result_connection = network_socket.connect_ex((hostip, port)) ends['connect'] = datetime.datetime.now() # SSL if https: starts['ssl'] = datetime.datetime.now() try: network_socket = ssl.wrap_socket(network_socket) except socket.timeout: raise ScanFailed('SSL socket timeout', result=result) ends['ssl'] = datetime.datetime.now() # Get request if result_connection == 0 and url: starts['request'] = datetime.datetime.now() network_socket.send( "GET {0} HTTP/1.0\r\nHost: {1}\r\n\r\n".format( url, host ).encode('ascii')) if max_size: data = network_socket.recv(max_size) else: data = network_socket.recv() result['length'] = len(data) data = data.decode('ascii', errors='ignore') result['response'] = (data) try: result['code'] = int(data.split('\n')[0].split()[1]) except IndexError: pass ends['request'] = datetime.datetime.now() network_socket.close() # Calculate durations ends['all'] = datetime.datetime.now() for duration in starts.keys(): if duration in ends.keys(): result['durations'][duration] = ends[duration] - starts[duration] if result_connection == 0: result['state'] = 'open' return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 45; 2, function_name:redirect; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 3, 36; 3, 39; 3, 42; 4, identifier:pattern; 5, identifier:to; 6, default_parameter; 6, 7; 6, 8; 7, identifier:permanent; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:locale_prefix; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:anchor; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:name; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:query; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:vary; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:cache_timeout; 26, integer:12; 27, default_parameter; 27, 28; 27, 29; 28, identifier:decorators; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:re_flags; 32, None; 33, default_parameter; 33, 34; 33, 35; 34, identifier:to_args; 35, None; 36, default_parameter; 36, 37; 36, 38; 37, identifier:to_kwargs; 38, None; 39, default_parameter; 39, 40; 39, 41; 40, identifier:prepend_locale; 41, True; 42, default_parameter; 42, 43; 42, 44; 43, identifier:merge_query; 44, False; 45, block; 45, 46; 45, 48; 45, 61; 45, 79; 45, 93; 45, 97; 45, 112; 45, 138; 45, 163; 45, 444; 45, 445; 45, 476; 46, expression_statement; 46, 47; 47, comment; 48, if_statement; 48, 49; 48, 50; 48, 55; 49, identifier:permanent; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:redirect_class; 54, identifier:HttpResponsePermanentRedirect; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:redirect_class; 60, identifier:HttpResponseRedirect; 61, if_statement; 61, 62; 61, 63; 62, identifier:locale_prefix; 63, block; 63, 64; 63, 73; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:pattern; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:pattern; 70, identifier:lstrip; 71, argument_list; 71, 72; 72, string:'^/'; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:pattern; 76, binary_operator:+; 76, 77; 76, 78; 77, identifier:LOCALE_RE; 78, identifier:pattern; 79, if_statement; 79, 80; 79, 81; 80, identifier:re_flags; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:pattern; 85, binary_operator:+; 85, 86; 85, 92; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, string:'(?{})'; 89, identifier:format; 90, argument_list; 90, 91; 91, identifier:re_flags; 92, identifier:pattern; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:view_decorators; 96, list:[]; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:is; 98, 99; 98, 100; 99, identifier:cache_timeout; 100, None; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:view_decorators; 106, identifier:append; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 110; 109, identifier:cache_control_expires; 110, argument_list; 110, 111; 111, identifier:cache_timeout; 112, if_statement; 112, 113; 112, 114; 113, identifier:vary; 114, block; 114, 115; 114, 127; 115, if_statement; 115, 116; 115, 121; 116, call; 116, 117; 116, 118; 117, identifier:isinstance; 118, argument_list; 118, 119; 118, 120; 119, identifier:vary; 120, identifier:basestring; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:vary; 125, list:[vary]; 125, 126; 126, identifier:vary; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:view_decorators; 131, identifier:append; 132, argument_list; 132, 133; 133, call; 133, 134; 133, 135; 134, identifier:vary_on_headers; 135, argument_list; 135, 136; 136, list_splat; 136, 137; 137, identifier:vary; 138, if_statement; 138, 139; 138, 140; 139, identifier:decorators; 140, block; 140, 141; 141, if_statement; 141, 142; 141, 146; 141, 154; 142, call; 142, 143; 142, 144; 143, identifier:callable; 144, argument_list; 144, 145; 145, identifier:decorators; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:view_decorators; 151, identifier:append; 152, argument_list; 152, 153; 153, identifier:decorators; 154, else_clause; 154, 155; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:view_decorators; 160, identifier:extend; 161, argument_list; 161, 162; 162, identifier:decorators; 163, function_definition; 163, 164; 163, 165; 163, 171; 163, 172; 164, function_name:_view; 165, parameters; 165, 166; 165, 167; 165, 169; 166, identifier:request; 167, list_splat_pattern; 167, 168; 168, identifier:args; 169, dictionary_splat_pattern; 169, 170; 170, identifier:kwargs; 171, comment; 172, block; 172, 173; 172, 191; 172, 201; 172, 202; 172, 225; 172, 269; 172, 297; 172, 298; 172, 321; 172, 392; 172, 406; 172, 420; 172, 439; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:kwargs; 176, dictionary_comprehension; 176, 177; 176, 182; 177, pair; 177, 178; 177, 179; 178, identifier:k; 179, boolean_operator:or; 179, 180; 179, 181; 180, identifier:v; 181, string:''; 182, for_in_clause; 182, 183; 182, 186; 183, pattern_list; 183, 184; 183, 185; 184, identifier:k; 185, identifier:v; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:kwargs; 189, identifier:items; 190, argument_list; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:args; 194, list_comprehension; 194, 195; 194, 198; 195, boolean_operator:or; 195, 196; 195, 197; 196, identifier:x; 197, string:''; 198, for_in_clause; 198, 199; 198, 200; 199, identifier:x; 200, identifier:args; 201, comment; 202, if_statement; 202, 203; 202, 207; 202, 219; 203, call; 203, 204; 203, 205; 204, identifier:callable; 205, argument_list; 205, 206; 206, identifier:to; 207, block; 207, 208; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:to_value; 211, call; 211, 212; 211, 213; 212, identifier:to; 213, argument_list; 213, 214; 213, 215; 213, 217; 214, identifier:request; 215, list_splat; 215, 216; 216, identifier:args; 217, dictionary_splat; 217, 218; 218, identifier:kwargs; 219, else_clause; 219, 220; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:to_value; 224, identifier:to; 225, if_statement; 225, 226; 225, 239; 225, 244; 226, boolean_operator:or; 226, 227; 226, 233; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:to_value; 230, identifier:startswith; 231, argument_list; 231, 232; 232, string:'/'; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:HTTP_RE; 236, identifier:match; 237, argument_list; 237, 238; 238, identifier:to_value; 239, block; 239, 240; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:redirect_url; 243, identifier:to_value; 244, else_clause; 244, 245; 245, block; 245, 246; 246, try_statement; 246, 247; 246, 261; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:redirect_url; 251, call; 251, 252; 251, 253; 252, identifier:reverse; 253, argument_list; 253, 254; 253, 255; 253, 258; 254, identifier:to_value; 255, keyword_argument; 255, 256; 255, 257; 256, identifier:args; 257, identifier:to_args; 258, keyword_argument; 258, 259; 258, 260; 259, identifier:kwargs; 260, identifier:to_kwargs; 261, except_clause; 261, 262; 261, 263; 261, 264; 262, identifier:NoReverseMatch; 263, comment; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:redirect_url; 268, identifier:to_value; 269, if_statement; 269, 270; 269, 285; 270, boolean_operator:and; 270, 271; 270, 279; 271, boolean_operator:and; 271, 272; 271, 273; 272, identifier:prepend_locale; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:redirect_url; 276, identifier:startswith; 277, argument_list; 277, 278; 278, string:'/'; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:kwargs; 282, identifier:get; 283, argument_list; 283, 284; 284, string:'locale'; 285, block; 285, 286; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:redirect_url; 289, binary_operator:+; 289, 290; 289, 291; 290, string:'/{locale}'; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:redirect_url; 294, identifier:lstrip; 295, argument_list; 295, 296; 296, string:'/'; 297, comment; 298, if_statement; 298, 299; 298, 302; 299, boolean_operator:or; 299, 300; 299, 301; 300, identifier:args; 301, identifier:kwargs; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:redirect_url; 306, call; 306, 307; 306, 308; 307, identifier:strip_tags; 308, argument_list; 308, 309; 309, call; 309, 310; 309, 316; 310, attribute; 310, 311; 310, 315; 311, call; 311, 312; 311, 313; 312, identifier:force_text; 313, argument_list; 313, 314; 314, identifier:redirect_url; 315, identifier:format; 316, argument_list; 316, 317; 316, 319; 317, list_splat; 317, 318; 318, identifier:args; 319, dictionary_splat; 319, 320; 320, identifier:kwargs; 321, if_statement; 321, 322; 321, 323; 321, 370; 321, 386; 322, identifier:query; 323, block; 323, 324; 324, if_statement; 324, 325; 324, 326; 324, 358; 325, identifier:merge_query; 326, block; 326, 327; 326, 341; 326, 348; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:req_query; 330, call; 330, 331; 330, 332; 331, identifier:parse_qs; 332, argument_list; 332, 333; 333, call; 333, 334; 333, 339; 334, attribute; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:request; 337, identifier:META; 338, identifier:get; 339, argument_list; 339, 340; 340, string:'QUERY_STRING'; 341, expression_statement; 341, 342; 342, call; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:req_query; 345, identifier:update; 346, argument_list; 346, 347; 347, identifier:query; 348, expression_statement; 348, 349; 349, assignment; 349, 350; 349, 351; 350, identifier:querystring; 351, call; 351, 352; 351, 353; 352, identifier:urlencode; 353, argument_list; 353, 354; 353, 355; 354, identifier:req_query; 355, keyword_argument; 355, 356; 355, 357; 356, identifier:doseq; 357, True; 358, else_clause; 358, 359; 359, block; 359, 360; 360, expression_statement; 360, 361; 361, assignment; 361, 362; 361, 363; 362, identifier:querystring; 363, call; 363, 364; 363, 365; 364, identifier:urlencode; 365, argument_list; 365, 366; 365, 367; 366, identifier:query; 367, keyword_argument; 367, 368; 367, 369; 368, identifier:doseq; 369, True; 370, elif_clause; 370, 371; 370, 374; 371, comparison_operator:is; 371, 372; 371, 373; 372, identifier:query; 373, None; 374, block; 374, 375; 375, expression_statement; 375, 376; 376, assignment; 376, 377; 376, 378; 377, identifier:querystring; 378, call; 378, 379; 378, 384; 379, attribute; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:request; 382, identifier:META; 383, identifier:get; 384, argument_list; 384, 385; 385, string:'QUERY_STRING'; 386, else_clause; 386, 387; 387, block; 387, 388; 388, expression_statement; 388, 389; 389, assignment; 389, 390; 389, 391; 390, identifier:querystring; 391, string:''; 392, if_statement; 392, 393; 392, 394; 393, identifier:querystring; 394, block; 394, 395; 395, expression_statement; 395, 396; 396, assignment; 396, 397; 396, 398; 397, identifier:redirect_url; 398, call; 398, 399; 398, 402; 399, attribute; 399, 400; 399, 401; 400, string:'?'; 401, identifier:join; 402, argument_list; 402, 403; 403, list:[redirect_url, querystring]; 403, 404; 403, 405; 404, identifier:redirect_url; 405, identifier:querystring; 406, if_statement; 406, 407; 406, 408; 407, identifier:anchor; 408, block; 408, 409; 409, expression_statement; 409, 410; 410, assignment; 410, 411; 410, 412; 411, identifier:redirect_url; 412, call; 412, 413; 412, 416; 413, attribute; 413, 414; 413, 415; 414, string:'#'; 415, identifier:join; 416, argument_list; 416, 417; 417, list:[redirect_url, anchor]; 417, 418; 417, 419; 418, identifier:redirect_url; 419, identifier:anchor; 420, if_statement; 420, 421; 420, 427; 421, call; 421, 422; 421, 425; 422, attribute; 422, 423; 422, 424; 423, identifier:PROTOCOL_RELATIVE_RE; 424, identifier:match; 425, argument_list; 425, 426; 426, identifier:redirect_url; 427, block; 427, 428; 428, expression_statement; 428, 429; 429, assignment; 429, 430; 429, 431; 430, identifier:redirect_url; 431, binary_operator:+; 431, 432; 431, 433; 432, string:'/'; 433, call; 433, 434; 433, 437; 434, attribute; 434, 435; 434, 436; 435, identifier:redirect_url; 436, identifier:lstrip; 437, argument_list; 437, 438; 438, string:'/'; 439, return_statement; 439, 440; 440, call; 440, 441; 440, 442; 441, identifier:redirect_class; 442, argument_list; 442, 443; 443, identifier:redirect_url; 444, comment; 445, try_statement; 445, 446; 445, 447; 445, 448; 445, 449; 445, 464; 446, comment; 447, comment; 448, comment; 449, block; 449, 450; 450, for_statement; 450, 451; 450, 452; 450, 456; 451, identifier:decorator; 452, call; 452, 453; 452, 454; 453, identifier:reversed; 454, argument_list; 454, 455; 455, identifier:view_decorators; 456, block; 456, 457; 457, expression_statement; 457, 458; 458, assignment; 458, 459; 458, 460; 459, identifier:_view; 460, call; 460, 461; 460, 462; 461, identifier:decorator; 462, argument_list; 462, 463; 463, identifier:_view; 464, except_clause; 464, 465; 464, 466; 465, identifier:TypeError; 466, block; 466, 467; 467, expression_statement; 467, 468; 468, call; 468, 469; 468, 472; 469, attribute; 469, 470; 469, 471; 470, identifier:log; 471, identifier:exception; 472, argument_list; 472, 473; 473, concatenated_string; 473, 474; 473, 475; 474, string:'decorators not iterable or does not contain '; 475, string:'callable items'; 476, return_statement; 476, 477; 477, call; 477, 478; 477, 479; 478, identifier:url; 479, argument_list; 479, 480; 479, 481; 479, 482; 480, identifier:pattern; 481, identifier:_view; 482, keyword_argument; 482, 483; 482, 484; 483, identifier:name; 484, identifier:name
def redirect(pattern, to, permanent=True, locale_prefix=True, anchor=None, name=None, query=None, vary=None, cache_timeout=12, decorators=None, re_flags=None, to_args=None, to_kwargs=None, prepend_locale=True, merge_query=False): """ Return a url matcher suited for urlpatterns. pattern: the regex against which to match the requested URL. to: either a url name that `reverse` will find, a url that will simply be returned, or a function that will be given the request and url captures, and return the destination. permanent: boolean whether to send a 301 or 302 response. locale_prefix: automatically prepend `pattern` with a regex for an optional locale in the url. This locale (or None) will show up in captured kwargs as 'locale'. anchor: if set it will be appended to the destination url after a '#'. name: if used in a `urls.py` the redirect URL will be available as the name for use in calls to `reverse()`. Does _NOT_ work if used in a `redirects.py` file. query: a dict of query params to add to the destination url. vary: if you used an HTTP header to decide where to send users you should include that header's name in the `vary` arg. cache_timeout: number of hours to cache this redirect. just sets the proper `cache-control` and `expires` headers. decorators: a callable (or list of callables) that will wrap the view used to redirect the user. equivalent to adding a decorator to any other view. re_flags: a string of any of the characters: "iLmsux". Will modify the `pattern` regex based on the documented meaning of the flags (see python re module docs). to_args: a tuple or list of args to pass to reverse if `to` is a url name. to_kwargs: a dict of keyword args to pass to reverse if `to` is a url name. prepend_locale: if true the redirect URL will be prepended with the locale from the requested URL. merge_query: merge the requested query params from the `query` arg with any query params from the request. Usage: urlpatterns = [ redirect(r'projects/$', 'mozorg.product'), redirect(r'^projects/seamonkey$', 'mozorg.product', locale_prefix=False), redirect(r'apps/$', 'https://marketplace.firefox.com'), redirect(r'firefox/$', 'firefox.new', name='firefox'), redirect(r'the/dude$', 'abides', query={'aggression': 'not_stand'}), ] """ if permanent: redirect_class = HttpResponsePermanentRedirect else: redirect_class = HttpResponseRedirect if locale_prefix: pattern = pattern.lstrip('^/') pattern = LOCALE_RE + pattern if re_flags: pattern = '(?{})'.format(re_flags) + pattern view_decorators = [] if cache_timeout is not None: view_decorators.append(cache_control_expires(cache_timeout)) if vary: if isinstance(vary, basestring): vary = [vary] view_decorators.append(vary_on_headers(*vary)) if decorators: if callable(decorators): view_decorators.append(decorators) else: view_decorators.extend(decorators) def _view(request, *args, **kwargs): # don't want to have 'None' in substitutions kwargs = {k: v or '' for k, v in kwargs.items()} args = [x or '' for x in args] # If it's a callable, call it and get the url out. if callable(to): to_value = to(request, *args, **kwargs) else: to_value = to if to_value.startswith('/') or HTTP_RE.match(to_value): redirect_url = to_value else: try: redirect_url = reverse(to_value, args=to_args, kwargs=to_kwargs) except NoReverseMatch: # Assume it's a URL redirect_url = to_value if prepend_locale and redirect_url.startswith('/') and kwargs.get('locale'): redirect_url = '/{locale}' + redirect_url.lstrip('/') # use info from url captures. if args or kwargs: redirect_url = strip_tags(force_text(redirect_url).format(*args, **kwargs)) if query: if merge_query: req_query = parse_qs(request.META.get('QUERY_STRING')) req_query.update(query) querystring = urlencode(req_query, doseq=True) else: querystring = urlencode(query, doseq=True) elif query is None: querystring = request.META.get('QUERY_STRING') else: querystring = '' if querystring: redirect_url = '?'.join([redirect_url, querystring]) if anchor: redirect_url = '#'.join([redirect_url, anchor]) if PROTOCOL_RELATIVE_RE.match(redirect_url): redirect_url = '/' + redirect_url.lstrip('/') return redirect_class(redirect_url) # Apply decorators try: # Decorators should be applied in reverse order so that input # can be sent in the order your would write nested decorators # e.g. dec1(dec2(_view)) -> [dec1, dec2] for decorator in reversed(view_decorators): _view = decorator(_view) except TypeError: log.exception('decorators not iterable or does not contain ' 'callable items') return url(pattern, _view, name=name)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:FD; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 19; 5, 20; 5, 21; 5, 22; 5, 23; 5, 24; 5, 50; 5, 51; 5, 52; 5, 81; 5, 117; 5, 118; 5, 156; 5, 157; 5, 276; 5, 277; 5, 301; 5, 302; 5, 303; 5, 304; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:Verbose; 12, block; 12, 13; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:print; 16, argument_list; 16, 17; 17, string:"Finite Difference Solution Technique"; 18, comment; 19, comment; 20, comment; 21, comment; 22, comment; 23, comment; 24, try_statement; 24, 25; 24, 30; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:qs; 30, except_clause; 30, 31; 31, block; 31, 32; 31, 44; 31, 45; 31, 46; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:qs; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:q0; 42, identifier:copy; 43, argument_list; 44, comment; 45, comment; 46, delete_statement; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:q0; 50, comment; 51, comment; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:x; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:np; 60, identifier:arange; 61, argument_list; 61, 62; 61, 67; 61, 78; 62, binary_operator:/; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:dx; 66, float:2.; 67, binary_operator:*; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:dx; 71, subscript; 71, 72; 71, 77; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:qs; 76, identifier:shape; 77, integer:0; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:dx; 81, if_statement; 81, 82; 81, 87; 82, comparison_operator:==; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:dimension; 86, integer:2; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:y; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:np; 96, identifier:arange; 97, argument_list; 97, 98; 97, 103; 97, 114; 98, binary_operator:/; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:dy; 102, float:2.; 103, binary_operator:*; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:dy; 107, subscript; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:qs; 112, identifier:shape; 113, integer:1; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:dy; 117, comment; 118, try_statement; 118, 119; 118, 125; 119, block; 119, 120; 119, 124; 120, expression_statement; 120, 121; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:Solver; 124, comment; 125, except_clause; 125, 126; 125, 127; 125, 128; 126, comment; 127, comment; 128, block; 128, 129; 129, if_statement; 129, 130; 129, 133; 129, 147; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:filename; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:Solver; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:configGet; 143, argument_list; 143, 144; 143, 145; 143, 146; 144, string:"string"; 145, string:"numerical"; 146, string:"Solver"; 147, else_clause; 147, 148; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:sys; 153, identifier:exit; 154, argument_list; 154, 155; 155, string:"No solver defined!"; 156, comment; 157, if_statement; 157, 158; 157, 161; 157, 162; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:filename; 161, comment; 162, block; 162, 163; 162, 176; 162, 177; 162, 250; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:iterative_ConvergenceTolerance; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:configGet; 172, argument_list; 172, 173; 172, 174; 172, 175; 173, string:"float"; 174, string:"numerical"; 175, string:"ConvergenceTolerance"; 176, comment; 177, try_statement; 177, 178; 177, 228; 178, block; 178, 179; 178, 195; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:self; 183, identifier:Te; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:configGet; 188, argument_list; 188, 189; 188, 190; 188, 191; 188, 192; 189, string:"float"; 190, string:"input"; 191, string:"ElasticThickness"; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:optional; 194, False; 195, if_statement; 195, 196; 195, 201; 195, 222; 196, comparison_operator:is; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:Te; 200, None; 201, block; 201, 202; 201, 216; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:Tepath; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:configGet; 209, argument_list; 209, 210; 209, 211; 209, 212; 209, 213; 210, string:"string"; 211, string:"input"; 212, string:"ElasticThickness"; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:optional; 215, False; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:self; 220, identifier:Te; 221, identifier:Tepath; 222, else_clause; 222, 223; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:Tepath; 227, None; 228, except_clause; 228, 229; 229, block; 229, 230; 229, 244; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:Tepath; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:self; 236, identifier:configGet; 237, argument_list; 237, 238; 237, 239; 237, 240; 237, 241; 238, string:"string"; 239, string:"input"; 240, string:"ElasticThickness"; 241, keyword_argument; 241, 242; 241, 243; 242, identifier:optional; 243, False; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:Te; 249, identifier:Tepath; 250, if_statement; 250, 251; 250, 256; 251, comparison_operator:is; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:Te; 255, None; 256, block; 256, 257; 257, if_statement; 257, 258; 257, 263; 257, 265; 258, comparison_operator:is; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:coeff_matrix; 262, None; 263, block; 263, 264; 264, pass_statement; 265, else_clause; 265, 266; 265, 267; 265, 268; 266, comment; 267, comment; 268, block; 268, 269; 269, expression_statement; 269, 270; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:sys; 273, identifier:exit; 274, argument_list; 274, 275; 275, string:"No input elastic thickness or coefficient matrix supplied."; 276, comment; 277, if_statement; 277, 278; 277, 286; 277, 287; 277, 294; 278, comparison_operator:==; 278, 279; 278, 285; 279, call; 279, 280; 279, 281; 280, identifier:type; 281, argument_list; 281, 282; 282, attribute; 282, 283; 282, 284; 283, identifier:self; 284, identifier:Te; 285, identifier:str; 286, comment; 287, block; 287, 288; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:Tepath; 291, attribute; 291, 292; 291, 293; 292, identifier:self; 293, identifier:Te; 294, else_clause; 294, 295; 295, block; 295, 296; 295, 300; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 299; 298, identifier:Tepath; 299, None; 300, comment; 301, comment; 302, comment; 303, comment; 304, if_statement; 304, 305; 304, 306; 305, identifier:Tepath; 306, block; 306, 307; 306, 323; 306, 375; 306, 376; 306, 377; 307, expression_statement; 307, 308; 308, assignment; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:self; 311, identifier:Te; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:self; 315, identifier:loadFile; 316, argument_list; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:self; 319, identifier:Te; 320, keyword_argument; 320, 321; 320, 322; 321, identifier:close_on_fail; 322, False; 323, if_statement; 323, 324; 323, 329; 324, comparison_operator:is; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:self; 327, identifier:Te; 328, None; 329, block; 329, 330; 329, 335; 329, 340; 329, 345; 330, expression_statement; 330, 331; 331, call; 331, 332; 331, 333; 332, identifier:print; 333, argument_list; 333, 334; 334, string:"Requested Te file is provided but cannot be located."; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 338; 337, identifier:print; 338, argument_list; 338, 339; 339, string:"No scalar elastic thickness is provided in configuration file"; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 343; 342, identifier:print; 343, argument_list; 343, 344; 344, string:"(Typo in path to input Te grid?)"; 345, if_statement; 345, 346; 345, 351; 345, 362; 346, comparison_operator:is; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:self; 349, identifier:coeff_matrix; 350, None; 351, block; 351, 352; 351, 357; 352, expression_statement; 352, 353; 353, call; 353, 354; 353, 355; 354, identifier:print; 355, argument_list; 355, 356; 356, string:"But a coefficient matrix has been found."; 357, expression_statement; 357, 358; 358, call; 358, 359; 358, 360; 359, identifier:print; 360, argument_list; 360, 361; 361, string:"Calculations will be carried forward using it."; 362, else_clause; 362, 363; 363, block; 363, 364; 363, 369; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 367; 366, identifier:print; 367, argument_list; 367, 368; 368, string:"Exiting."; 369, expression_statement; 369, 370; 370, call; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:sys; 373, identifier:exit; 374, argument_list; 375, comment; 376, comment; 377, if_statement; 377, 378; 377, 385; 378, call; 378, 379; 378, 384; 379, attribute; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:self; 382, identifier:Te; 383, identifier:any; 384, argument_list; 385, block; 385, 386; 386, expression_statement; 386, 387; 387, call; 387, 388; 387, 391; 388, attribute; 388, 389; 388, 390; 389, identifier:self; 390, identifier:TeArraySizeCheck; 391, argument_list
def FD(self): """ Set-up for the finite difference solution method """ if self.Verbose: print("Finite Difference Solution Technique") # Used to check for coeff_matrix here, but now doing so in self.bc_check() # called by f1d and f2d at the start # # Define a stress-based qs = q0 # But only if the latter has not already been defined # (e.g., by the getters and setters) try: self.qs except: self.qs = self.q0.copy() # Remove self.q0 to avoid issues with multiply-defined inputs # q0 is the parsable input to either a qs grid or contains (x,(y),q) del self.q0 # Give it x and y dimensions for help with plotting tools # (not implemented internally, but a help with external methods) self.x = np.arange(self.dx/2., self.dx * self.qs.shape[0], self.dx) if self.dimension == 2: self.y = np.arange(self.dy/2., self.dy * self.qs.shape[1], self.dy) # Is there a solver defined try: self.Solver # See if it exists already except: # Well, will fail if it doesn't see this, maybe not the most reasonable # error message. if self.filename: self.Solver = self.configGet("string", "numerical", "Solver") else: sys.exit("No solver defined!") # Check consistency of size if coeff array was loaded if self.filename: # In the case that it is iterative, find the convergence criterion self.iterative_ConvergenceTolerance = self.configGet("float", "numerical", "ConvergenceTolerance") # Try to import Te grid or scalar for the finite difference solution try: self.Te = self.configGet("float", "input", "ElasticThickness", optional=False) if self.Te is None: Tepath = self.configGet("string", "input", "ElasticThickness", optional=False) self.Te = Tepath else: Tepath = None except: Tepath = self.configGet("string", "input", "ElasticThickness", optional=False) self.Te = Tepath if self.Te is None: if self.coeff_matrix is not None: pass else: # Have to bring this out here in case it was discovered in the # try statement that there is no value given sys.exit("No input elastic thickness or coefficient matrix supplied.") # or if getter/setter if type(self.Te) == str: # Try to import Te grid or scalar for the finite difference solution Tepath = self.Te else: Tepath = None # in case no self.filename present (like for GRASS GIS) # If there is a Tepath, import Te # Assume that even if a coeff_matrix is defined # That the user wants Te if they gave the path if Tepath: self.Te = self.loadFile(self.Te, close_on_fail = False) if self.Te is None: print("Requested Te file is provided but cannot be located.") print("No scalar elastic thickness is provided in configuration file") print("(Typo in path to input Te grid?)") if self.coeff_matrix is not None: print("But a coefficient matrix has been found.") print("Calculations will be carried forward using it.") else: print("Exiting.") sys.exit() # Check that Te is the proper size if it was loaded # Will be array if it was loaded if self.Te.any(): self.TeArraySizeCheck()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:SAS_NG; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 77; 5, 78; 5, 237; 5, 238; 5, 239; 5, 240; 5, 241; 5, 249; 5, 264; 5, 265; 5, 266; 5, 270; 5, 271; 5, 272; 5, 273; 5, 320; 5, 321; 5, 344; 5, 478; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 8, 13; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:filename; 12, comment; 13, block; 13, 14; 13, 27; 13, 28; 13, 29; 13, 30; 13, 31; 13, 32; 13, 48; 13, 64; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:Te; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:configGet; 23, argument_list; 23, 24; 23, 25; 23, 26; 24, string:"float"; 25, string:"input"; 26, string:"ElasticThickness"; 27, comment; 28, comment; 29, comment; 30, comment; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:latlon; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:configGet; 41, argument_list; 41, 42; 41, 43; 41, 44; 41, 45; 42, string:"string"; 43, string:"numerical2D"; 44, string:"latlon"; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:optional; 47, True; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:PlanetaryRadius; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:configGet; 57, argument_list; 57, 58; 57, 59; 57, 60; 57, 61; 58, string:"float"; 59, string:"numerical2D"; 60, string:"PlanetaryRadius"; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:optional; 63, True; 64, if_statement; 64, 65; 64, 70; 65, comparison_operator:==; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:dimension; 69, integer:2; 70, block; 70, 71; 71, import_from_statement; 71, 72; 71, 75; 72, dotted_name; 72, 73; 72, 74; 73, identifier:scipy; 74, identifier:special; 75, dotted_name; 75, 76; 76, identifier:kei; 77, comment; 78, if_statement; 78, 79; 78, 84; 78, 152; 79, comparison_operator:==; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:dimension; 83, integer:1; 84, block; 84, 85; 85, try_statement; 85, 86; 85, 87; 85, 96; 86, comment; 87, block; 87, 88; 87, 92; 88, expression_statement; 88, 89; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:x; 92, expression_statement; 92, 93; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:q; 96, except_clause; 96, 97; 96, 98; 97, comment; 98, block; 98, 99; 99, if_statement; 99, 100; 99, 109; 99, 134; 100, comparison_operator:==; 100, 101; 100, 108; 101, subscript; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:q0; 106, identifier:shape; 107, integer:1; 108, integer:2; 109, block; 109, 110; 109, 122; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:x; 115, subscript; 115, 116; 115, 119; 115, 121; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:q0; 119, slice; 119, 120; 120, colon; 121, integer:0; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:q; 127, subscript; 127, 128; 127, 131; 127, 133; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:q0; 131, slice; 131, 132; 132, colon; 133, integer:1; 134, else_clause; 134, 135; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:sys; 140, identifier:exit; 141, argument_list; 141, 142; 142, binary_operator:+; 142, 143; 142, 144; 143, string:"For 1D (ungridded) SAS_NG configuration file, need [x,w] array. Your dimensions are: "; 144, call; 144, 145; 144, 146; 145, identifier:str; 146, argument_list; 146, 147; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:q0; 151, identifier:shape; 152, else_clause; 152, 153; 153, block; 153, 154; 154, try_statement; 154, 155; 154, 156; 154, 169; 155, comment; 156, block; 156, 157; 156, 161; 156, 165; 157, expression_statement; 157, 158; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:x; 161, expression_statement; 161, 162; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:u; 165, expression_statement; 165, 166; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:q; 169, except_clause; 169, 170; 169, 171; 170, comment; 171, block; 171, 172; 172, if_statement; 172, 173; 172, 182; 172, 219; 173, comparison_operator:==; 173, 174; 173, 181; 174, subscript; 174, 175; 174, 180; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:self; 178, identifier:q0; 179, identifier:shape; 180, integer:1; 181, integer:3; 182, block; 182, 183; 182, 195; 182, 207; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:x; 188, subscript; 188, 189; 188, 192; 188, 194; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:q0; 192, slice; 192, 193; 193, colon; 194, integer:0; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:y; 200, subscript; 200, 201; 200, 204; 200, 206; 201, attribute; 201, 202; 201, 203; 202, identifier:self; 203, identifier:q0; 204, slice; 204, 205; 205, colon; 206, integer:1; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:q; 212, subscript; 212, 213; 212, 216; 212, 218; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:q0; 216, slice; 216, 217; 217, colon; 218, integer:2; 219, else_clause; 219, 220; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:sys; 225, identifier:exit; 226, argument_list; 226, 227; 227, binary_operator:+; 227, 228; 227, 229; 228, string:"For 2D (ungridded) SAS_NG configuration file, need [x,y,w] array. Your dimensions are: "; 229, call; 229, 230; 229, 231; 230, identifier:str; 231, argument_list; 231, 232; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:q0; 236, identifier:shape; 237, comment; 238, comment; 239, comment; 240, comment; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:x; 246, attribute; 246, 247; 246, 248; 247, identifier:self; 248, identifier:x; 249, if_statement; 249, 250; 249, 255; 250, comparison_operator:==; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:dimension; 254, integer:2; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:y; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:y; 264, comment; 265, comment; 266, delete_statement; 266, 267; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:q0; 270, comment; 271, comment; 272, comment; 273, try_statement; 273, 274; 273, 279; 274, block; 274, 275; 275, expression_statement; 275, 276; 276, attribute; 276, 277; 276, 278; 277, identifier:self; 278, identifier:xw; 279, except_clause; 279, 280; 280, block; 280, 281; 281, try_statement; 281, 282; 281, 312; 282, block; 282, 283; 282, 299; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:xw; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:self; 291, identifier:configGet; 292, argument_list; 292, 293; 292, 294; 292, 295; 292, 296; 293, string:'string'; 294, string:"input"; 295, string:"xw"; 296, keyword_argument; 296, 297; 296, 298; 297, identifier:optional; 298, True; 299, if_statement; 299, 300; 299, 305; 300, comparison_operator:==; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:xw; 304, string:''; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:xw; 311, None; 312, except_clause; 312, 313; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:self; 318, identifier:xw; 319, None; 320, comment; 321, if_statement; 321, 322; 321, 330; 322, comparison_operator:==; 322, 323; 322, 329; 323, call; 323, 324; 323, 325; 324, identifier:type; 325, argument_list; 325, 326; 326, attribute; 326, 327; 326, 328; 327, identifier:self; 328, identifier:xw; 329, identifier:str; 330, block; 330, 331; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:self; 335, identifier:xw; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:self; 339, identifier:loadFile; 340, argument_list; 340, 341; 341, attribute; 341, 342; 341, 343; 342, identifier:self; 343, identifier:xw; 344, if_statement; 344, 345; 344, 350; 345, comparison_operator:==; 345, 346; 345, 349; 346, attribute; 346, 347; 346, 348; 347, identifier:self; 348, identifier:dimension; 349, integer:2; 350, block; 350, 351; 350, 399; 350, 400; 350, 435; 350, 436; 351, try_statement; 351, 352; 351, 353; 351, 358; 352, comment; 353, block; 353, 354; 354, expression_statement; 354, 355; 355, attribute; 355, 356; 355, 357; 356, identifier:self; 357, identifier:yw; 358, except_clause; 358, 359; 359, block; 359, 360; 360, try_statement; 360, 361; 360, 391; 361, block; 361, 362; 361, 378; 362, expression_statement; 362, 363; 363, assignment; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:self; 366, identifier:yw; 367, call; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:self; 370, identifier:configGet; 371, argument_list; 371, 372; 371, 373; 371, 374; 371, 375; 372, string:'string'; 373, string:"input"; 374, string:"yw"; 375, keyword_argument; 375, 376; 375, 377; 376, identifier:optional; 377, True; 378, if_statement; 378, 379; 378, 384; 379, comparison_operator:==; 379, 380; 379, 383; 380, attribute; 380, 381; 380, 382; 381, identifier:self; 382, identifier:yw; 383, string:''; 384, block; 384, 385; 385, expression_statement; 385, 386; 386, assignment; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:self; 389, identifier:yw; 390, None; 391, except_clause; 391, 392; 392, block; 392, 393; 393, expression_statement; 393, 394; 394, assignment; 394, 395; 394, 398; 395, attribute; 395, 396; 395, 397; 396, identifier:self; 397, identifier:yw; 398, None; 399, comment; 400, if_statement; 400, 401; 400, 427; 401, boolean_operator:or; 401, 402; 401, 414; 401, 415; 402, parenthesized_expression; 402, 403; 403, boolean_operator:and; 403, 404; 403, 409; 404, comparison_operator:is; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, identifier:self; 407, identifier:xw; 408, None; 409, comparison_operator:is; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, identifier:self; 412, identifier:yw; 413, None; 414, line_continuation:\; 415, parenthesized_expression; 415, 416; 416, boolean_operator:and; 416, 417; 416, 422; 417, comparison_operator:is; 417, 418; 417, 421; 418, attribute; 418, 419; 418, 420; 419, identifier:self; 420, identifier:xw; 421, None; 422, comparison_operator:is; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:self; 425, identifier:yw; 426, None; 427, block; 427, 428; 428, expression_statement; 428, 429; 429, call; 429, 430; 429, 433; 430, attribute; 430, 431; 430, 432; 431, identifier:sys; 432, identifier:exit; 433, argument_list; 433, 434; 434, string:"SAS_NG output at specified points requires both xw and yw to be defined"; 435, comment; 436, if_statement; 436, 437; 436, 445; 436, 459; 437, comparison_operator:==; 437, 438; 437, 444; 438, call; 438, 439; 438, 440; 439, identifier:type; 440, argument_list; 440, 441; 441, attribute; 441, 442; 441, 443; 442, identifier:self; 443, identifier:yw; 444, identifier:str; 445, block; 445, 446; 446, expression_statement; 446, 447; 447, assignment; 447, 448; 447, 451; 448, attribute; 448, 449; 448, 450; 449, identifier:self; 450, identifier:yw; 451, call; 451, 452; 451, 455; 452, attribute; 452, 453; 452, 454; 453, identifier:self; 454, identifier:loadFile; 455, argument_list; 455, 456; 456, attribute; 456, 457; 456, 458; 457, identifier:self; 458, identifier:yw; 459, elif_clause; 459, 460; 459, 465; 460, comparison_operator:is; 460, 461; 460, 464; 461, attribute; 461, 462; 461, 463; 462, identifier:self; 463, identifier:yw; 464, None; 465, block; 465, 466; 466, expression_statement; 466, 467; 467, assignment; 467, 468; 467, 471; 468, attribute; 468, 469; 468, 470; 469, identifier:self; 470, identifier:yw; 471, call; 471, 472; 471, 477; 472, attribute; 472, 473; 472, 476; 473, attribute; 473, 474; 473, 475; 474, identifier:self; 475, identifier:y; 476, identifier:copy; 477, argument_list; 478, if_statement; 478, 479; 478, 484; 479, comparison_operator:is; 479, 480; 479, 483; 480, attribute; 480, 481; 480, 482; 481, identifier:self; 482, identifier:xw; 483, None; 484, block; 484, 485; 485, expression_statement; 485, 486; 486, assignment; 486, 487; 486, 490; 487, attribute; 487, 488; 487, 489; 488, identifier:self; 489, identifier:xw; 490, call; 490, 491; 490, 496; 491, attribute; 491, 492; 491, 495; 492, attribute; 492, 493; 492, 494; 493, identifier:self; 494, identifier:x; 495, identifier:copy; 496, argument_list
def SAS_NG(self): """ Set-up for the ungridded superposition of analytical solutions method for solving flexure """ if self.filename: # Define the (scalar) elastic thickness self.Te = self.configGet("float", "input", "ElasticThickness") # See if it wants to be run in lat/lon # Could put under in 2D if-statement, but could imagine an eventual desire # to change this and have 1D lat/lon profiles as well. # So while the options will be under "numerical2D", this place here will # remain held for an eventual future. self.latlon = self.configGet("string", "numerical2D", "latlon", optional=True) self.PlanetaryRadius = self.configGet("float", "numerical2D", "PlanetaryRadius", optional=True) if self.dimension == 2: from scipy.special import kei # Parse out input q0 into variables of imoprtance for solution if self.dimension == 1: try: # If these have already been set, e.g., by getters/setters, great! self.x self.q except: # Using [x, y, w] configuration file if self.q0.shape[1] == 2: self.x = self.q0[:,0] self.q = self.q0[:,1] else: sys.exit("For 1D (ungridded) SAS_NG configuration file, need [x,w] array. Your dimensions are: "+str(self.q0.shape)) else: try: # If these have already been set, e.g., by getters/setters, great! self.x self.u self.q except: # Using [x, y, w] configuration file if self.q0.shape[1] == 3: self.x = self.q0[:,0] self.y = self.q0[:,1] self.q = self.q0[:,2] else: sys.exit("For 2D (ungridded) SAS_NG configuration file, need [x,y,w] array. Your dimensions are: "+str(self.q0.shape)) # x, y are in absolute coordinates. Create a local grid reference to # these. This local grid, which starts at (0,0), is defined just so that # we have a way of running the model without defined real-world # coordinates self.x = self.x if self.dimension == 2: self.y = self.y # Remove self.q0 to avoid issues with multiply-defined inputs # q0 is the parsable input to either a qs grid or contains (x,(y),q) del self.q0 # Check if a seperate output set of x,y points has been defined # otherwise, set those values to None # First, try to load the arrays try: self.xw except: try: self.xw = self.configGet('string', "input", "xw", optional=True) if self.xw == '': self.xw = None except: self.xw = None # If strings, load arrays if type(self.xw) == str: self.xw = self.loadFile(self.xw) if self.dimension == 2: try: # already set by setter? self.yw except: try: self.yw = self.configGet('string', "input", "yw", optional=True ) if self.yw == '': self.yw = None except: self.yw = None # At this point, can check if we have both None or both defined if (self.xw is not None and self.yw is None) \ or (self.xw is None and self.yw is not None): sys.exit("SAS_NG output at specified points requires both xw and yw to be defined") # All right, now just finish defining if type(self.yw) == str: self.yw = self.loadFile(self.yw) elif self.yw is None: self.yw = self.y.copy() if self.xw is None: self.xw = self.x.copy()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_c3_mro; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, default_parameter; 5, 6; 5, 7; 6, identifier:abcs; 7, None; 8, block; 8, 9; 8, 11; 8, 51; 8, 61; 8, 74; 8, 78; 8, 91; 8, 126; 8, 137; 8, 151; 8, 165; 8, 179; 9, expression_statement; 9, 10; 10, comment; 11, for_statement; 11, 12; 11, 15; 11, 24; 11, 45; 12, pattern_list; 12, 13; 12, 14; 13, identifier:i; 14, identifier:base; 15, call; 15, 16; 15, 17; 16, identifier:enumerate; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:reversed; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:cls; 23, identifier:__bases__; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 31; 26, call; 26, 27; 26, 28; 27, identifier:hasattr; 28, argument_list; 28, 29; 28, 30; 29, identifier:base; 30, string:'__abstractmethods__'; 31, block; 31, 32; 31, 43; 31, 44; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:boundary; 35, binary_operator:-; 35, 36; 35, 42; 36, call; 36, 37; 36, 38; 37, identifier:len; 38, argument_list; 38, 39; 39, attribute; 39, 40; 39, 41; 40, identifier:cls; 41, identifier:__bases__; 42, identifier:i; 43, break_statement; 44, comment; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:boundary; 50, integer:0; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:abcs; 54, conditional_expression:if; 54, 55; 54, 59; 54, 60; 55, call; 55, 56; 55, 57; 56, identifier:list; 57, argument_list; 57, 58; 58, identifier:abcs; 59, identifier:abcs; 60, list:[]; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:explicit_bases; 64, call; 64, 65; 64, 66; 65, identifier:list; 66, argument_list; 66, 67; 67, subscript; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:cls; 70, identifier:__bases__; 71, slice; 71, 72; 71, 73; 72, colon; 73, identifier:boundary; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:abstract_bases; 77, list:[]; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:other_bases; 81, call; 81, 82; 81, 83; 82, identifier:list; 83, argument_list; 83, 84; 84, subscript; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:cls; 87, identifier:__bases__; 88, slice; 88, 89; 88, 90; 89, identifier:boundary; 90, colon; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:base; 93, identifier:abcs; 94, block; 94, 95; 95, if_statement; 95, 96; 95, 116; 95, 117; 95, 118; 96, boolean_operator:and; 96, 97; 96, 102; 97, call; 97, 98; 97, 99; 98, identifier:issubclass; 99, argument_list; 99, 100; 99, 101; 100, identifier:cls; 101, identifier:base; 102, not_operator; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:any; 105, generator_expression; 105, 106; 105, 111; 106, call; 106, 107; 106, 108; 107, identifier:issubclass; 108, argument_list; 108, 109; 108, 110; 109, identifier:b; 110, identifier:base; 111, for_in_clause; 111, 112; 111, 113; 112, identifier:b; 113, attribute; 113, 114; 113, 115; 114, identifier:cls; 115, identifier:__bases__; 116, comment; 117, comment; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:abstract_bases; 123, identifier:append; 124, argument_list; 124, 125; 125, identifier:base; 126, for_statement; 126, 127; 126, 128; 126, 129; 127, identifier:base; 128, identifier:abstract_bases; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:abcs; 134, identifier:remove; 135, argument_list; 135, 136; 136, identifier:base; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:explicit_c3_mros; 140, list_comprehension; 140, 141; 140, 148; 141, call; 141, 142; 141, 143; 142, identifier:_c3_mro; 143, argument_list; 143, 144; 143, 145; 144, identifier:base; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:abcs; 147, identifier:abcs; 148, for_in_clause; 148, 149; 148, 150; 149, identifier:base; 150, identifier:explicit_bases; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:abstract_c3_mros; 154, list_comprehension; 154, 155; 154, 162; 155, call; 155, 156; 155, 157; 156, identifier:_c3_mro; 157, argument_list; 157, 158; 157, 159; 158, identifier:base; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:abcs; 161, identifier:abcs; 162, for_in_clause; 162, 163; 162, 164; 163, identifier:base; 164, identifier:abstract_bases; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:other_c3_mros; 168, list_comprehension; 168, 169; 168, 176; 169, call; 169, 170; 169, 171; 170, identifier:_c3_mro; 171, argument_list; 171, 172; 171, 173; 172, identifier:base; 173, keyword_argument; 173, 174; 173, 175; 174, identifier:abcs; 175, identifier:abcs; 176, for_in_clause; 176, 177; 176, 178; 177, identifier:base; 178, identifier:other_bases; 179, return_statement; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:_c3_merge; 182, argument_list; 182, 183; 183, binary_operator:+; 183, 184; 183, 199; 184, binary_operator:+; 184, 185; 184, 197; 185, binary_operator:+; 185, 186; 185, 195; 186, binary_operator:+; 186, 187; 186, 194; 187, binary_operator:+; 187, 188; 187, 193; 188, binary_operator:+; 188, 189; 188, 192; 189, list:[[cls]]; 189, 190; 190, list:[cls]; 190, 191; 191, identifier:cls; 192, identifier:explicit_c3_mros; 193, identifier:abstract_c3_mros; 194, identifier:other_c3_mros; 195, list:[explicit_bases]; 195, 196; 196, identifier:explicit_bases; 197, list:[abstract_bases]; 197, 198; 198, identifier:abstract_bases; 199, list:[ other_bases]; 199, 200; 200, identifier:other_bases
def _c3_mro(cls, abcs=None): """Computes the method resolution order using extended C3 linearization. If no *abcs* are given, the algorithm works exactly like the built-in C3 linearization used for method resolution. If given, *abcs* is a list of abstract base classes that should be inserted into the resulting MRO. Unrelated ABCs are ignored and don't end up in the result. The algorithm inserts ABCs where their functionality is introduced, i.e. issubclass(cls, abc) returns True for the class itself but returns False for all its direct base classes. Implicit ABCs for a given class (either registered or inferred from the presence of a special method like __len__) are inserted directly after the last ABC explicitly listed in the MRO of said class. If two implicit ABCs end up next to each other in the resulting MRO, their ordering depends on the order of types in *abcs*. """ for i, base in enumerate(reversed(cls.__bases__)): if hasattr(base, '__abstractmethods__'): boundary = len(cls.__bases__) - i break # Bases up to the last explicit ABC are considered first. else: boundary = 0 abcs = list(abcs) if abcs else [] explicit_bases = list(cls.__bases__[:boundary]) abstract_bases = [] other_bases = list(cls.__bases__[boundary:]) for base in abcs: if issubclass(cls, base) and not any(issubclass(b, base) for b in cls.__bases__): # If *cls* is the class that introduces behaviour described by # an ABC *base*, insert said ABC to its MRO. abstract_bases.append(base) for base in abstract_bases: abcs.remove(base) explicit_c3_mros = [_c3_mro(base, abcs=abcs) for base in explicit_bases] abstract_c3_mros = [_c3_mro(base, abcs=abcs) for base in abstract_bases] other_c3_mros = [_c3_mro(base, abcs=abcs) for base in other_bases] return _c3_merge( [[cls]] + explicit_c3_mros + abstract_c3_mros + other_c3_mros + [explicit_bases] + [abstract_bases] + [ other_bases])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 6; 2, function_name:singledispatch; 3, parameters; 3, 4; 4, identifier:function; 5, comment; 6, block; 6, 7; 6, 9; 6, 13; 6, 19; 6, 24; 6, 30; 6, 31; 6, 108; 6, 109; 6, 168; 6, 191; 6, 197; 6, 203; 6, 209; 6, 218; 6, 226; 6, 232; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:registry; 12, dictionary; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:dispatch_cache; 16, call; 16, 17; 16, 18; 17, identifier:WeakKeyDictionary; 18, argument_list; 19, function_definition; 19, 20; 19, 21; 19, 22; 20, function_name:ns; 21, parameters; 22, block; 22, 23; 23, pass_statement; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:ns; 28, identifier:cache_token; 29, None; 30, comment; 31, function_definition; 31, 32; 31, 33; 31, 35; 32, function_name:dispatch; 33, parameters; 33, 34; 34, identifier:cls; 35, block; 35, 36; 35, 38; 35, 70; 35, 106; 36, expression_statement; 36, 37; 37, comment; 38, if_statement; 38, 39; 38, 44; 39, comparison_operator:is; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:ns; 42, identifier:cache_token; 43, None; 44, block; 44, 45; 44, 51; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:current_token; 48, call; 48, 49; 48, 50; 49, identifier:get_cache_token; 50, argument_list; 51, if_statement; 51, 52; 51, 57; 52, comparison_operator:!=; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:ns; 55, identifier:cache_token; 56, identifier:current_token; 57, block; 57, 58; 57, 64; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:dispatch_cache; 62, identifier:clear; 63, argument_list; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:ns; 68, identifier:cache_token; 69, identifier:current_token; 70, try_statement; 70, 71; 70, 78; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:impl; 75, subscript; 75, 76; 75, 77; 76, identifier:dispatch_cache; 77, identifier:cls; 78, except_clause; 78, 79; 78, 80; 79, identifier:KeyError; 80, block; 80, 81; 80, 100; 81, try_statement; 81, 82; 81, 89; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:impl; 86, subscript; 86, 87; 86, 88; 87, identifier:registry; 88, identifier:cls; 89, except_clause; 89, 90; 89, 91; 90, identifier:KeyError; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:impl; 95, call; 95, 96; 95, 97; 96, identifier:_find_impl; 97, argument_list; 97, 98; 97, 99; 98, identifier:cls; 99, identifier:registry; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:dispatch_cache; 104, identifier:cls; 105, identifier:impl; 106, return_statement; 106, 107; 107, identifier:impl; 108, comment; 109, function_definition; 109, 110; 109, 111; 109, 116; 110, function_name:register; 111, parameters; 111, 112; 111, 113; 112, identifier:cls; 113, default_parameter; 113, 114; 113, 115; 114, identifier:func; 115, None; 116, block; 116, 117; 116, 119; 116, 133; 116, 139; 116, 160; 116, 166; 117, expression_statement; 117, 118; 118, comment; 119, if_statement; 119, 120; 119, 123; 120, comparison_operator:is; 120, 121; 120, 122; 121, identifier:func; 122, None; 123, block; 123, 124; 124, return_statement; 124, 125; 125, lambda; 125, 126; 125, 128; 126, lambda_parameters; 126, 127; 127, identifier:f; 128, call; 128, 129; 128, 130; 129, identifier:register; 130, argument_list; 130, 131; 130, 132; 131, identifier:cls; 132, identifier:f; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:registry; 137, identifier:cls; 138, identifier:func; 139, if_statement; 139, 140; 139, 151; 140, boolean_operator:and; 140, 141; 140, 146; 141, comparison_operator:is; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:ns; 144, identifier:cache_token; 145, None; 146, call; 146, 147; 146, 148; 147, identifier:hasattr; 148, argument_list; 148, 149; 148, 150; 149, identifier:cls; 150, string:'__abstractmethods__'; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:ns; 156, identifier:cache_token; 157, call; 157, 158; 157, 159; 158, identifier:get_cache_token; 159, argument_list; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:dispatch_cache; 164, identifier:clear; 165, argument_list; 166, return_statement; 166, 167; 167, identifier:func; 168, function_definition; 168, 169; 168, 170; 168, 175; 169, function_name:wrapper; 170, parameters; 170, 171; 170, 173; 171, list_splat_pattern; 171, 172; 172, identifier:args; 173, dictionary_splat_pattern; 173, 174; 174, identifier:kw; 175, block; 175, 176; 176, return_statement; 176, 177; 177, call; 177, 178; 177, 186; 178, call; 178, 179; 178, 180; 179, identifier:dispatch; 180, argument_list; 180, 181; 181, attribute; 181, 182; 181, 185; 182, subscript; 182, 183; 182, 184; 183, identifier:args; 184, integer:0; 185, identifier:__class__; 186, argument_list; 186, 187; 186, 189; 187, list_splat; 187, 188; 188, identifier:args; 189, dictionary_splat; 189, 190; 190, identifier:kw; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:registry; 195, identifier:object; 196, identifier:function; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:wrapper; 201, identifier:register; 202, identifier:register; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:wrapper; 207, identifier:dispatch; 208, identifier:dispatch; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:wrapper; 213, identifier:registry; 214, call; 214, 215; 214, 216; 215, identifier:MappingProxyType; 216, argument_list; 216, 217; 217, identifier:registry; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:wrapper; 222, identifier:_clear_cache; 223, attribute; 223, 224; 223, 225; 224, identifier:dispatch_cache; 225, identifier:clear; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:update_wrapper; 229, argument_list; 229, 230; 229, 231; 230, identifier:wrapper; 231, identifier:function; 232, return_statement; 232, 233; 233, identifier:wrapper
def singledispatch(function): # noqa """Single-dispatch generic function decorator. Transforms a function into a generic function, which can have different behaviours depending upon the type of its first argument. The decorated function acts as the default implementation, and additional implementations can be registered using the register() attribute of the generic function. """ registry = {} dispatch_cache = WeakKeyDictionary() def ns(): pass ns.cache_token = None # noinspection PyIncorrectDocstring def dispatch(cls): """generic_func.dispatch(cls) -> <function implementation> Runs the dispatch algorithm to return the best available implementation for the given *cls* registered on *generic_func*. """ if ns.cache_token is not None: current_token = get_cache_token() if ns.cache_token != current_token: dispatch_cache.clear() ns.cache_token = current_token try: impl = dispatch_cache[cls] except KeyError: try: impl = registry[cls] except KeyError: impl = _find_impl(cls, registry) dispatch_cache[cls] = impl return impl # noinspection PyIncorrectDocstring def register(cls, func=None): """generic_func.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_func*. """ if func is None: return lambda f: register(cls, f) registry[cls] = func if ns.cache_token is None and hasattr(cls, '__abstractmethods__'): ns.cache_token = get_cache_token() dispatch_cache.clear() return func def wrapper(*args, **kw): return dispatch(args[0].__class__)(*args, **kw) registry[object] = function wrapper.register = register wrapper.dispatch = dispatch wrapper.registry = MappingProxyType(registry) wrapper._clear_cache = dispatch_cache.clear update_wrapper(wrapper, function) return wrapper
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 40; 2, function_name:createResource; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 3, 28; 3, 31; 3, 34; 3, 37; 4, identifier:self; 5, identifier:resource_type; 6, identifier:title; 7, default_parameter; 7, 8; 7, 9; 8, identifier:resource_file; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:resource_filename; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:abstract; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:keywords; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:edit_users; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:view_users; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:edit_groups; 27, None; 28, default_parameter; 28, 29; 28, 30; 29, identifier:view_groups; 30, None; 31, default_parameter; 31, 32; 31, 33; 32, identifier:metadata; 33, None; 34, default_parameter; 34, 35; 34, 36; 35, identifier:extra_metadata; 36, None; 37, default_parameter; 37, 38; 37, 39; 38, identifier:progress_callback; 39, None; 40, block; 40, 41; 40, 43; 40, 56; 40, 60; 40, 89; 40, 90; 40, 100; 40, 109; 40, 139; 40, 148; 40, 157; 40, 166; 40, 175; 40, 184; 40, 193; 40, 207; 40, 214; 40, 223; 40, 231; 40, 252; 40, 269; 40, 303; 40, 311; 40, 317; 41, expression_statement; 41, 42; 42, comment; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:url; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, string:"{url_base}/resource/"; 49, identifier:format; 50, argument_list; 50, 51; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:url_base; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:url_base; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:close_fd; 59, False; 60, if_statement; 60, 61; 60, 66; 61, comparison_operator:not; 61, 62; 61, 63; 62, identifier:resource_type; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:resource_types; 66, block; 66, 67; 67, raise_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:HydroShareArgumentException; 70, argument_list; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, string:"Resource type {0} is not among known resources: {1}"; 74, identifier:format; 75, argument_list; 75, 76; 75, 77; 76, identifier:resource_type; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, string:", "; 80, identifier:join; 81, argument_list; 81, 82; 82, list_comprehension; 82, 83; 82, 84; 83, identifier:r; 84, for_in_clause; 84, 85; 84, 86; 85, identifier:r; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:resource_types; 89, comment; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:params; 93, dictionary; 93, 94; 93, 97; 94, pair; 94, 95; 94, 96; 95, string:'resource_type'; 96, identifier:resource_type; 97, pair; 97, 98; 97, 99; 98, string:'title'; 99, identifier:title; 100, if_statement; 100, 101; 100, 102; 101, identifier:abstract; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:params; 107, string:'abstract'; 108, identifier:abstract; 109, if_statement; 109, 110; 109, 111; 109, 112; 110, identifier:keywords; 111, comment; 112, block; 112, 113; 113, for_statement; 113, 114; 113, 117; 113, 121; 114, tuple_pattern; 114, 115; 114, 116; 115, identifier:i; 116, identifier:kw; 117, call; 117, 118; 117, 119; 118, identifier:enumerate; 119, argument_list; 119, 120; 120, identifier:keywords; 121, block; 121, 122; 121, 133; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:key; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, string:"keywords[{index}]"; 128, identifier:format; 129, argument_list; 129, 130; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:index; 132, identifier:i; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:params; 137, identifier:key; 138, identifier:kw; 139, if_statement; 139, 140; 139, 141; 140, identifier:edit_users; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, subscript; 144, 145; 144, 146; 145, identifier:params; 146, string:'edit_users'; 147, identifier:edit_users; 148, if_statement; 148, 149; 148, 150; 149, identifier:view_users; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 156; 153, subscript; 153, 154; 153, 155; 154, identifier:params; 155, string:'view_users'; 156, identifier:view_users; 157, if_statement; 157, 158; 157, 159; 158, identifier:edit_groups; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:params; 164, string:'edit_groups'; 165, identifier:edit_groups; 166, if_statement; 166, 167; 166, 168; 167, identifier:view_groups; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 174; 171, subscript; 171, 172; 171, 173; 172, identifier:params; 173, string:'view_groups'; 174, identifier:view_groups; 175, if_statement; 175, 176; 175, 177; 176, identifier:metadata; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:params; 182, string:'metadata'; 183, identifier:metadata; 184, if_statement; 184, 185; 184, 186; 185, identifier:extra_metadata; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, subscript; 189, 190; 189, 191; 190, identifier:params; 191, string:'extra_metadata'; 192, identifier:extra_metadata; 193, if_statement; 193, 194; 193, 195; 194, identifier:resource_file; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:close_fd; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:_prepareFileForUpload; 203, argument_list; 203, 204; 203, 205; 203, 206; 204, identifier:params; 205, identifier:resource_file; 206, identifier:resource_filename; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:encoder; 210, call; 210, 211; 210, 212; 211, identifier:MultipartEncoder; 212, argument_list; 212, 213; 213, identifier:params; 214, if_statement; 214, 215; 214, 218; 215, comparison_operator:is; 215, 216; 215, 217; 216, identifier:progress_callback; 217, None; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:progress_callback; 222, identifier:default_progress_callback; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:monitor; 226, call; 226, 227; 226, 228; 227, identifier:MultipartEncoderMonitor; 228, argument_list; 228, 229; 228, 230; 229, identifier:encoder; 230, identifier:progress_callback; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:r; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:_request; 238, argument_list; 238, 239; 238, 240; 238, 241; 238, 244; 239, string:'POST'; 240, identifier:url; 241, keyword_argument; 241, 242; 241, 243; 242, identifier:data; 243, identifier:monitor; 244, keyword_argument; 244, 245; 244, 246; 245, identifier:headers; 246, dictionary; 246, 247; 247, pair; 247, 248; 247, 249; 248, string:'Content-Type'; 249, attribute; 249, 250; 249, 251; 250, identifier:monitor; 251, identifier:content_type; 252, if_statement; 252, 253; 252, 254; 253, identifier:close_fd; 254, block; 254, 255; 254, 263; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:fd; 258, subscript; 258, 259; 258, 262; 259, subscript; 259, 260; 259, 261; 260, identifier:params; 261, string:'file'; 262, integer:1; 263, expression_statement; 263, 264; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:fd; 267, identifier:close; 268, argument_list; 269, if_statement; 269, 270; 269, 275; 270, comparison_operator:!=; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:r; 273, identifier:status_code; 274, integer:201; 275, block; 275, 276; 276, if_statement; 276, 277; 276, 282; 276, 290; 277, comparison_operator:==; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:r; 280, identifier:status_code; 281, integer:403; 282, block; 282, 283; 283, raise_statement; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:HydroShareNotAuthorized; 286, argument_list; 286, 287; 287, tuple; 287, 288; 287, 289; 288, string:'POST'; 289, identifier:url; 290, else_clause; 290, 291; 291, block; 291, 292; 292, raise_statement; 292, 293; 293, call; 293, 294; 293, 295; 294, identifier:HydroShareHTTPException; 295, argument_list; 295, 296; 296, tuple; 296, 297; 296, 298; 296, 299; 296, 302; 297, identifier:url; 298, string:'POST'; 299, attribute; 299, 300; 299, 301; 300, identifier:r; 301, identifier:status_code; 302, identifier:params; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:response; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:r; 309, identifier:json; 310, argument_list; 311, expression_statement; 311, 312; 312, assignment; 312, 313; 312, 314; 313, identifier:new_resource_id; 314, subscript; 314, 315; 314, 316; 315, identifier:response; 316, string:'resource_id'; 317, return_statement; 317, 318; 318, identifier:new_resource_id
def createResource(self, resource_type, title, resource_file=None, resource_filename=None, abstract=None, keywords=None, edit_users=None, view_users=None, edit_groups=None, view_groups=None, metadata=None, extra_metadata=None, progress_callback=None): """ Create a new resource. :param resource_type: string representing the a HydroShare resource type recognized by this server. :param title: string representing the title of the new resource :param resource_file: a read-only binary file-like object (i.e. opened with the flag 'rb') or a string representing path to file to be uploaded as part of the new resource :param resource_filename: string representing the filename of the resource file. Must be specified if resource_file is a file-like object. If resource_file is a string representing a valid file path, and resource_filename is not specified, resource_filename will be equal to os.path.basename(resource_file). is a string :param abstract: string representing abstract of resource :param keywords: list of strings representing keywords to associate with the resource :param edit_users: list of HydroShare usernames who will be given edit permissions :param view_users: list of HydroShare usernames who will be given view permissions :param edit_groups: list of HydroShare group names that will be given edit permissions :param view_groups: list of HydroShare group names that will be given view permissions :param metadata: json string data for each of the metadata elements :param extra_metadata: json string data for key/value pair metadata elements defined by user :param progress_callback: user-defined function to provide feedback to the user about the progress of the upload of resource_file. For more information, see: http://toolbelt.readthedocs.org/en/latest/uploading-data.html#monitoring-your-streaming-multipart-upload :return: string representing ID of newly created resource. :raises: HydroShareArgumentException if any parameters are invalid. :raises: HydroShareNotAuthorized if user is not authorized to perform action. :raises: HydroShareHTTPException if an unexpected HTTP response code is encountered. """ url = "{url_base}/resource/".format(url_base=self.url_base) close_fd = False if resource_type not in self.resource_types: raise HydroShareArgumentException("Resource type {0} is not among known resources: {1}".format(resource_type, ", ".join([r for r in self.resource_types]))) # Prepare request params = {'resource_type': resource_type, 'title': title} if abstract: params['abstract'] = abstract if keywords: # Put keywords in a format that django-rest's serializer will understand for (i, kw) in enumerate(keywords): key = "keywords[{index}]".format(index=i) params[key] = kw if edit_users: params['edit_users'] = edit_users if view_users: params['view_users'] = view_users if edit_groups: params['edit_groups'] = edit_groups if view_groups: params['view_groups'] = view_groups if metadata: params['metadata'] = metadata if extra_metadata: params['extra_metadata'] = extra_metadata if resource_file: close_fd = self._prepareFileForUpload(params, resource_file, resource_filename) encoder = MultipartEncoder(params) if progress_callback is None: progress_callback = default_progress_callback monitor = MultipartEncoderMonitor(encoder, progress_callback) r = self._request('POST', url, data=monitor, headers={'Content-Type': monitor.content_type}) if close_fd: fd = params['file'][1] fd.close() if r.status_code != 201: if r.status_code == 403: raise HydroShareNotAuthorized(('POST', url)) else: raise HydroShareHTTPException((url, 'POST', r.status_code, params)) response = r.json() new_resource_id = response['resource_id'] return new_resource_id
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:getResourceFile; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:pid; 6, identifier:filename; 7, default_parameter; 7, 8; 7, 9; 8, identifier:destination; 9, None; 10, block; 10, 11; 10, 13; 10, 32; 10, 78; 10, 91; 10, 138; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:url; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, string:"{url_base}/resource/{pid}/files/{filename}"; 19, identifier:format; 20, argument_list; 20, 21; 20, 26; 20, 29; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:url_base; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:url_base; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:pid; 28, identifier:pid; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:filename; 31, identifier:filename; 32, if_statement; 32, 33; 32, 34; 33, identifier:destination; 34, block; 34, 35; 34, 56; 35, if_statement; 35, 36; 35, 45; 36, not_operator; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:os; 41, identifier:path; 42, identifier:isdir; 43, argument_list; 43, 44; 44, identifier:destination; 45, block; 45, 46; 46, raise_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:HydroShareArgumentException; 49, argument_list; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, string:"{0} is not a directory."; 53, identifier:format; 54, argument_list; 54, 55; 55, identifier:destination; 56, if_statement; 56, 57; 56, 67; 57, not_operator; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:os; 61, identifier:access; 62, argument_list; 62, 63; 62, 64; 63, identifier:destination; 64, attribute; 64, 65; 64, 66; 65, identifier:os; 66, identifier:W_OK; 67, block; 67, 68; 68, raise_statement; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:HydroShareArgumentException; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, string:"You do not have write permissions to directory '{0}'."; 75, identifier:format; 76, argument_list; 76, 77; 77, identifier:destination; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:r; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:_request; 85, argument_list; 85, 86; 85, 87; 85, 88; 86, string:'GET'; 87, identifier:url; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:stream; 90, True; 91, if_statement; 91, 92; 91, 97; 92, comparison_operator:!=; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:r; 95, identifier:status_code; 96, integer:200; 97, block; 97, 98; 98, if_statement; 98, 99; 98, 104; 98, 112; 98, 126; 99, comparison_operator:==; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:r; 102, identifier:status_code; 103, integer:403; 104, block; 104, 105; 105, raise_statement; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:HydroShareNotAuthorized; 108, argument_list; 108, 109; 109, tuple; 109, 110; 109, 111; 110, string:'GET'; 111, identifier:url; 112, elif_clause; 112, 113; 112, 118; 113, comparison_operator:==; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:r; 116, identifier:status_code; 117, integer:404; 118, block; 118, 119; 119, raise_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:HydroShareNotFound; 122, argument_list; 122, 123; 123, tuple; 123, 124; 123, 125; 124, identifier:pid; 125, identifier:filename; 126, else_clause; 126, 127; 127, block; 127, 128; 128, raise_statement; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:HydroShareHTTPException; 131, argument_list; 131, 132; 132, tuple; 132, 133; 132, 134; 132, 135; 133, identifier:url; 134, string:'GET'; 135, attribute; 135, 136; 135, 137; 136, identifier:r; 137, identifier:status_code; 138, if_statement; 138, 139; 138, 142; 138, 150; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:destination; 141, None; 142, block; 142, 143; 143, return_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:r; 147, identifier:iter_content; 148, argument_list; 148, 149; 149, identifier:STREAM_CHUNK_SIZE; 150, else_clause; 150, 151; 151, block; 151, 152; 151, 164; 151, 192; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:filepath; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:os; 159, identifier:path; 160, identifier:join; 161, argument_list; 161, 162; 161, 163; 162, identifier:destination; 163, identifier:filename; 164, with_statement; 164, 165; 164, 175; 165, with_clause; 165, 166; 166, with_item; 166, 167; 167, as_pattern; 167, 168; 167, 173; 168, call; 168, 169; 168, 170; 169, identifier:open; 170, argument_list; 170, 171; 170, 172; 171, identifier:filepath; 172, string:'wb'; 173, as_pattern_target; 173, 174; 174, identifier:fd; 175, block; 175, 176; 176, for_statement; 176, 177; 176, 178; 176, 184; 177, identifier:chunk; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:r; 181, identifier:iter_content; 182, argument_list; 182, 183; 183, identifier:STREAM_CHUNK_SIZE; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:fd; 189, identifier:write; 190, argument_list; 190, 191; 191, identifier:chunk; 192, return_statement; 192, 193; 193, identifier:filepath
def getResourceFile(self, pid, filename, destination=None): """ Get a file within a resource. :param pid: The HydroShare ID of the resource :param filename: String representing the name of the resource file to get. :param destination: String representing the directory to save the resource file to. If None, a stream to the resource file will be returned instead. :return: The path of the downloaded file (if destination was specified), or a stream to the resource file. :raises: HydroShareArgumentException if any parameters are invalid. :raises: HydroShareNotAuthorized if user is not authorized to perform action. :raises: HydroShareNotFound if the resource was not found. :raises: HydroShareHTTPException if an unexpected HTTP response code is encountered. """ url = "{url_base}/resource/{pid}/files/{filename}".format(url_base=self.url_base, pid=pid, filename=filename) if destination: if not os.path.isdir(destination): raise HydroShareArgumentException("{0} is not a directory.".format(destination)) if not os.access(destination, os.W_OK): raise HydroShareArgumentException("You do not have write permissions to directory '{0}'.".format(destination)) r = self._request('GET', url, stream=True) if r.status_code != 200: if r.status_code == 403: raise HydroShareNotAuthorized(('GET', url)) elif r.status_code == 404: raise HydroShareNotFound((pid, filename)) else: raise HydroShareHTTPException((url, 'GET', r.status_code)) if destination is None: return r.iter_content(STREAM_CHUNK_SIZE) else: filepath = os.path.join(destination, filename) with open(filepath, 'wb') as fd: for chunk in r.iter_content(STREAM_CHUNK_SIZE): fd.write(chunk) return filepath
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:upsert; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 18; 5, 31; 5, 451; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:required_parameters; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:_stackParameters; 17, list:[]; 18, try_statement; 18, 19; 18, 26; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_initialize_upsert; 25, argument_list; 26, except_clause; 26, 27; 26, 28; 27, identifier:Exception; 28, block; 28, 29; 29, return_statement; 29, 30; 30, False; 31, try_statement; 31, 32; 31, 416; 32, block; 32, 33; 32, 43; 32, 65; 32, 77; 32, 89; 32, 93; 32, 157; 32, 172; 32, 223; 32, 245; 32, 263; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:available_parameters; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:_parameters; 41, identifier:keys; 42, argument_list; 43, for_statement; 43, 44; 43, 45; 43, 54; 44, identifier:parameter_name; 45, call; 45, 46; 45, 51; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:_template; 50, identifier:get; 51, argument_list; 51, 52; 51, 53; 52, string:'Parameters'; 53, dictionary; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:required_parameters; 59, identifier:append; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:str; 63, argument_list; 63, 64; 64, identifier:parameter_name; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:logging; 69, identifier:info; 70, argument_list; 70, 71; 71, binary_operator:+; 71, 72; 71, 73; 72, string:' required parameters: '; 73, call; 73, 74; 73, 75; 74, identifier:str; 75, argument_list; 75, 76; 76, identifier:required_parameters; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:logging; 81, identifier:info; 82, argument_list; 82, 83; 83, binary_operator:+; 83, 84; 83, 85; 84, string:'available parameters: '; 85, call; 85, 86; 85, 87; 86, identifier:str; 87, argument_list; 87, 88; 88, identifier:available_parameters; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:parameters; 92, list:[]; 93, for_statement; 93, 94; 93, 95; 93, 96; 94, identifier:required_parameter; 95, identifier:required_parameters; 96, block; 96, 97; 96, 101; 96, 110; 96, 117; 96, 150; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:parameter; 100, dictionary; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:parameter; 105, string:'ParameterKey'; 106, call; 106, 107; 106, 108; 107, identifier:str; 108, argument_list; 108, 109; 109, identifier:required_parameter; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:required_parameter; 113, call; 113, 114; 113, 115; 114, identifier:str; 115, argument_list; 115, 116; 116, identifier:required_parameter; 117, if_statement; 117, 118; 117, 123; 117, 134; 118, comparison_operator:in; 118, 119; 118, 120; 119, identifier:required_parameter; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:_parameters; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:parameter; 128, string:'ParameterValue'; 129, subscript; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:_parameters; 133, identifier:required_parameter; 134, else_clause; 134, 135; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, subscript; 138, 139; 138, 140; 139, identifier:parameter; 140, string:'ParameterValue'; 141, subscript; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:_parameters; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:required_parameter; 148, identifier:lower; 149, argument_list; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:parameters; 154, identifier:append; 155, argument_list; 155, 156; 156, identifier:parameter; 157, if_statement; 157, 158; 157, 164; 158, not_operator; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:self; 162, identifier:_analyze_stuff; 163, argument_list; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:sys; 169, identifier:exit; 170, argument_list; 170, 171; 171, integer:1; 172, if_statement; 172, 173; 172, 182; 173, call; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:_config; 178, identifier:get; 179, argument_list; 179, 180; 179, 181; 180, string:'dryrun'; 181, False; 182, block; 182, 183; 182, 190; 182, 199; 182, 209; 182, 216; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:logging; 187, identifier:info; 188, argument_list; 188, 189; 189, string:'Generating change set'; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:set_id; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:_generate_change_set; 197, argument_list; 197, 198; 198, identifier:parameters; 199, if_statement; 199, 200; 199, 201; 200, identifier:set_id; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:_describe_change_set; 207, argument_list; 207, 208; 208, identifier:set_id; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:logging; 213, identifier:info; 214, argument_list; 214, 215; 215, string:'This was a dryrun'; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:sys; 220, identifier:exit; 221, argument_list; 221, 222; 222, integer:0; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 230; 225, attribute; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:self; 228, identifier:_tags; 229, identifier:append; 230, argument_list; 230, 231; 231, dictionary; 231, 232; 231, 235; 232, pair; 232, 233; 232, 234; 233, string:"Key"; 234, string:"CODE_VERSION_SD"; 235, pair; 235, 236; 235, 237; 236, string:"Value"; 237, call; 237, 238; 237, 243; 238, attribute; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:self; 241, identifier:_config; 242, identifier:get; 243, argument_list; 243, 244; 244, string:'codeVersion'; 245, expression_statement; 245, 246; 246, call; 246, 247; 246, 252; 247, attribute; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:self; 250, identifier:_tags; 251, identifier:append; 252, argument_list; 252, 253; 253, dictionary; 253, 254; 253, 257; 254, pair; 254, 255; 254, 256; 255, string:"Key"; 256, string:"ANSWER"; 257, pair; 257, 258; 257, 259; 258, string:"Value"; 259, call; 259, 260; 259, 261; 260, identifier:str; 261, argument_list; 261, 262; 262, integer:42; 263, if_statement; 263, 264; 263, 267; 263, 341; 264, attribute; 264, 265; 264, 266; 265, identifier:self; 266, identifier:_updateStack; 267, block; 267, 268; 267, 323; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:stack; 271, call; 271, 272; 271, 277; 272, attribute; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:_cloudFormation; 276, identifier:update_stack; 277, argument_list; 277, 278; 277, 295; 277, 300; 277, 303; 277, 308; 277, 313; 278, keyword_argument; 278, 279; 278, 280; 279, identifier:StackName; 280, call; 280, 281; 280, 292; 281, attribute; 281, 282; 281, 291; 282, call; 282, 283; 282, 288; 283, attribute; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:_config; 287, identifier:get; 288, argument_list; 288, 289; 288, 290; 289, string:'environment'; 290, dictionary; 291, identifier:get; 292, argument_list; 292, 293; 292, 294; 293, string:'stack_name'; 294, None; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:TemplateURL; 297, attribute; 297, 298; 297, 299; 298, identifier:self; 299, identifier:_templateUrl; 300, keyword_argument; 300, 301; 300, 302; 301, identifier:Parameters; 302, identifier:parameters; 303, keyword_argument; 303, 304; 303, 305; 304, identifier:Capabilities; 305, list:['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM']; 305, 306; 305, 307; 306, string:'CAPABILITY_IAM'; 307, string:'CAPABILITY_NAMED_IAM'; 308, keyword_argument; 308, 309; 308, 310; 309, identifier:Tags; 310, attribute; 310, 311; 310, 312; 311, identifier:self; 312, identifier:_tags; 313, keyword_argument; 313, 314; 313, 315; 314, identifier:ClientRequestToken; 315, call; 315, 316; 315, 317; 316, identifier:str; 317, argument_list; 317, 318; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:uuid; 321, identifier:uuid4; 322, argument_list; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:logging; 327, identifier:info; 328, argument_list; 328, 329; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, string:'existing stack ID: {}'; 332, identifier:format; 333, argument_list; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:stack; 337, identifier:get; 338, argument_list; 338, 339; 338, 340; 339, string:'StackId'; 340, string:'unknown'; 341, else_clause; 341, 342; 342, block; 342, 343; 342, 398; 343, expression_statement; 343, 344; 344, assignment; 344, 345; 344, 346; 345, identifier:stack; 346, call; 346, 347; 346, 352; 347, attribute; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:self; 350, identifier:_cloudFormation; 351, identifier:create_stack; 352, argument_list; 352, 353; 352, 370; 352, 375; 352, 378; 352, 383; 352, 388; 353, keyword_argument; 353, 354; 353, 355; 354, identifier:StackName; 355, call; 355, 356; 355, 367; 356, attribute; 356, 357; 356, 366; 357, call; 357, 358; 357, 363; 358, attribute; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:self; 361, identifier:_config; 362, identifier:get; 363, argument_list; 363, 364; 363, 365; 364, string:'environment'; 365, dictionary; 366, identifier:get; 367, argument_list; 367, 368; 367, 369; 368, string:'stack_name'; 369, None; 370, keyword_argument; 370, 371; 370, 372; 371, identifier:TemplateURL; 372, attribute; 372, 373; 372, 374; 373, identifier:self; 374, identifier:_templateUrl; 375, keyword_argument; 375, 376; 375, 377; 376, identifier:Parameters; 377, identifier:parameters; 378, keyword_argument; 378, 379; 378, 380; 379, identifier:Capabilities; 380, list:['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM']; 380, 381; 380, 382; 381, string:'CAPABILITY_IAM'; 382, string:'CAPABILITY_NAMED_IAM'; 383, keyword_argument; 383, 384; 383, 385; 384, identifier:Tags; 385, attribute; 385, 386; 385, 387; 386, identifier:self; 387, identifier:_tags; 388, keyword_argument; 388, 389; 388, 390; 389, identifier:ClientRequestToken; 390, call; 390, 391; 390, 392; 391, identifier:str; 392, argument_list; 392, 393; 393, call; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:uuid; 396, identifier:uuid4; 397, argument_list; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:logging; 402, identifier:info; 403, argument_list; 403, 404; 404, call; 404, 405; 404, 408; 405, attribute; 405, 406; 405, 407; 406, string:'new stack ID: {}'; 407, identifier:format; 408, argument_list; 408, 409; 409, call; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, identifier:stack; 412, identifier:get; 413, argument_list; 413, 414; 413, 415; 414, string:'StackId'; 415, string:'unknown'; 416, except_clause; 416, 417; 416, 421; 417, as_pattern; 417, 418; 417, 419; 418, identifier:Exception; 419, as_pattern_target; 419, 420; 420, identifier:x; 421, block; 421, 422; 421, 449; 422, if_statement; 422, 423; 422, 426; 422, 437; 423, attribute; 423, 424; 423, 425; 424, identifier:self; 425, identifier:_verbose; 426, block; 426, 427; 427, expression_statement; 427, 428; 428, call; 428, 429; 428, 432; 429, attribute; 429, 430; 429, 431; 430, identifier:logging; 431, identifier:error; 432, argument_list; 432, 433; 432, 434; 433, identifier:x; 434, keyword_argument; 434, 435; 434, 436; 435, identifier:exc_info; 436, True; 437, else_clause; 437, 438; 438, block; 438, 439; 439, expression_statement; 439, 440; 440, call; 440, 441; 440, 444; 441, attribute; 441, 442; 441, 443; 442, identifier:logging; 443, identifier:error; 444, argument_list; 444, 445; 444, 446; 445, identifier:x; 446, keyword_argument; 446, 447; 446, 448; 447, identifier:exc_info; 448, False; 449, return_statement; 449, 450; 450, False; 451, return_statement; 451, 452; 452, True
def upsert(self): """ The main event of the utility. Create or update a Cloud Formation stack. Injecting properties where needed Args: None Returns: True if the stack create/update is started successfully else False if the start goes off in the weeds. Exits: If the user asked for a dryrun exit(with a code 0) the thing here. There is no point continuing after that point. """ required_parameters = [] self._stackParameters = [] try: self._initialize_upsert() except Exception: return False try: available_parameters = self._parameters.keys() for parameter_name in self._template.get('Parameters', {}): required_parameters.append(str(parameter_name)) logging.info(' required parameters: ' + str(required_parameters)) logging.info('available parameters: ' + str(available_parameters)) parameters = [] for required_parameter in required_parameters: parameter = {} parameter['ParameterKey'] = str(required_parameter) required_parameter = str(required_parameter) if required_parameter in self._parameters: parameter['ParameterValue'] = self._parameters[required_parameter] else: parameter['ParameterValue'] = self._parameters[required_parameter.lower()] parameters.append(parameter) if not self._analyze_stuff(): sys.exit(1) if self._config.get('dryrun', False): logging.info('Generating change set') set_id = self._generate_change_set(parameters) if set_id: self._describe_change_set(set_id) logging.info('This was a dryrun') sys.exit(0) self._tags.append({"Key": "CODE_VERSION_SD", "Value": self._config.get('codeVersion')}) self._tags.append({"Key": "ANSWER", "Value": str(42)}) if self._updateStack: stack = self._cloudFormation.update_stack( StackName=self._config.get('environment', {}).get('stack_name', None), TemplateURL=self._templateUrl, Parameters=parameters, Capabilities=['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'], Tags=self._tags, ClientRequestToken=str(uuid.uuid4()) ) logging.info('existing stack ID: {}'.format(stack.get('StackId', 'unknown'))) else: stack = self._cloudFormation.create_stack( StackName=self._config.get('environment', {}).get('stack_name', None), TemplateURL=self._templateUrl, Parameters=parameters, Capabilities=['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'], Tags=self._tags, ClientRequestToken=str(uuid.uuid4()) ) logging.info('new stack ID: {}'.format(stack.get('StackId', 'unknown'))) except Exception as x: if self._verbose: logging.error(x, exc_info=True) else: logging.error(x, exc_info=False) return False return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:dump; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:obj; 5, default_parameter; 5, 6; 5, 7; 6, identifier:fp; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:indent; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort_keys; 13, False; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kw; 16, block; 16, 17; 16, 19; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 21; 19, 52; 20, identifier:fp; 21, block; 21, 22; 21, 41; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:iterable; 25, call; 25, 26; 25, 39; 26, attribute; 26, 27; 26, 38; 27, call; 27, 28; 27, 29; 28, identifier:YAMLEncoder; 29, argument_list; 29, 30; 29, 33; 29, 36; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:indent; 32, identifier:indent; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:sort_keys; 35, identifier:sort_keys; 36, dictionary_splat; 36, 37; 37, identifier:kw; 38, identifier:iterencode; 39, argument_list; 39, 40; 40, identifier:obj; 41, for_statement; 41, 42; 41, 43; 41, 44; 42, identifier:chunk; 43, identifier:iterable; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:fp; 49, identifier:write; 50, argument_list; 50, 51; 51, identifier:chunk; 52, else_clause; 52, 53; 53, block; 53, 54; 54, return_statement; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:dumps; 57, argument_list; 57, 58; 57, 59; 57, 62; 57, 65; 58, identifier:obj; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:indent; 61, identifier:indent; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:sort_keys; 64, identifier:sort_keys; 65, dictionary_splat; 65, 66; 66, identifier:kw
def dump(obj, fp=None, indent=None, sort_keys=False, **kw): """ Dump object to a file like object or string. :param obj: :param fp: Open file like object :param int indent: Indent size, default 2 :param bool sort_keys: Optionally sort dictionary keys. :return: Yaml serialized data. """ if fp: iterable = YAMLEncoder(indent=indent, sort_keys=sort_keys, **kw).iterencode(obj) for chunk in iterable: fp.write(chunk) else: return dumps(obj, indent=indent, sort_keys=sort_keys, **kw)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_reference; 3, parameters; 3, 4; 4, identifier:root; 5, block; 5, 6; 5, 8; 5, 12; 5, 21; 5, 31; 5, 32; 5, 42; 5, 52; 5, 323; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:reference; 11, dictionary; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:elem; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:root; 18, identifier:find; 19, argument_list; 19, 20; 20, string:'bibliographyLink'; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:elem; 24, None; 25, block; 25, 26; 26, raise_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:MissingElementError; 29, argument_list; 29, 30; 30, string:'bibliographyLink'; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:ref_doi; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:elem; 38, identifier:get; 39, argument_list; 39, 40; 39, 41; 40, string:'doi'; 41, None; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:ref_key; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:elem; 48, identifier:get; 49, argument_list; 49, 50; 49, 51; 50, string:'preferredKey'; 51, None; 52, if_statement; 52, 53; 52, 56; 52, 280; 52, 314; 53, comparison_operator:is; 53, 54; 53, 55; 54, identifier:ref_doi; 55, None; 56, block; 56, 57; 57, try_statement; 57, 58; 57, 72; 57, 121; 58, block; 58, 59; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:ref; 62, subscript; 62, 63; 62, 71; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:crossref_api; 66, identifier:works; 67, argument_list; 67, 68; 68, keyword_argument; 68, 69; 68, 70; 69, identifier:ids; 70, identifier:ref_doi; 71, string:'message'; 72, except_clause; 72, 73; 72, 79; 73, tuple; 73, 74; 73, 75; 73, 78; 74, identifier:HTTPError; 75, attribute; 75, 76; 75, 77; 76, identifier:habanero; 77, identifier:RequestError; 78, identifier:ConnectionError; 79, block; 79, 80; 80, if_statement; 80, 81; 80, 84; 80, 90; 81, comparison_operator:is; 81, 82; 81, 83; 82, identifier:ref_key; 83, None; 84, block; 84, 85; 85, raise_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:KeywordError; 88, argument_list; 88, 89; 89, string:'DOI not found and preferredKey attribute not set'; 90, else_clause; 90, 91; 91, block; 91, 92; 91, 99; 91, 105; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:warn; 95, argument_list; 95, 96; 96, concatenated_string; 96, 97; 96, 98; 97, string:'Missing doi attribute in bibliographyLink or lookup failed. '; 98, string:'Setting "detail" key as a fallback; please update to the appropriate fields.'; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:reference; 103, string:'detail'; 104, identifier:ref_key; 105, if_statement; 105, 106; 105, 114; 106, comparison_operator:!=; 106, 107; 106, 113; 107, subscript; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:reference; 110, string:'detail'; 111, unary_operator:-; 111, 112; 112, integer:1; 113, string:'.'; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, augmented_assignment:+=; 116, 117; 116, 120; 117, subscript; 117, 118; 117, 119; 118, identifier:reference; 119, string:'detail'; 120, string:'.'; 121, else_clause; 121, 122; 122, block; 122, 123; 122, 133; 122, 143; 122, 144; 122, 145; 122, 158; 122, 174; 122, 189; 122, 203; 122, 214; 122, 220; 123, if_statement; 123, 124; 123, 127; 124, comparison_operator:is; 124, 125; 124, 126; 125, identifier:ref_key; 126, None; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:warn; 131, argument_list; 131, 132; 132, string:'Using DOI to obtain reference information, rather than preferredKey.'; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:reference; 137, string:'doi'; 138, subscript; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:elem; 141, identifier:attrib; 142, string:'doi'; 143, comment; 144, comment; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 150; 147, subscript; 147, 148; 147, 149; 148, identifier:reference; 149, string:'journal'; 150, subscript; 150, 151; 150, 157; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:ref; 154, identifier:get; 155, argument_list; 155, 156; 156, string:'container-title'; 157, integer:0; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:ref_year; 161, boolean_operator:or; 161, 162; 161, 168; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:ref; 165, identifier:get; 166, argument_list; 166, 167; 167, string:'published-print'; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:ref; 171, identifier:get; 172, argument_list; 172, 173; 173, string:'published-online'; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 179; 176, subscript; 176, 177; 176, 178; 177, identifier:reference; 178, string:'year'; 179, call; 179, 180; 179, 181; 180, identifier:int; 181, argument_list; 181, 182; 182, subscript; 182, 183; 182, 188; 183, subscript; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:ref_year; 186, string:'date-parts'; 187, integer:0; 188, integer:0; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:reference; 193, string:'volume'; 194, call; 194, 195; 194, 196; 195, identifier:int; 196, argument_list; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:ref; 200, identifier:get; 201, argument_list; 201, 202; 202, string:'volume'; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 208; 205, subscript; 205, 206; 205, 207; 206, identifier:reference; 207, string:'pages'; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:ref; 211, identifier:get; 212, argument_list; 212, 213; 213, string:'page'; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 219; 216, subscript; 216, 217; 216, 218; 217, identifier:reference; 218, string:'authors'; 219, list:[]; 220, for_statement; 220, 221; 220, 222; 220, 225; 221, identifier:author; 222, subscript; 222, 223; 222, 224; 223, identifier:ref; 224, string:'author'; 225, block; 225, 226; 225, 230; 225, 247; 225, 248; 225, 257; 225, 271; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:auth; 229, dictionary; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:auth; 234, string:'name'; 235, call; 235, 236; 235, 239; 236, attribute; 236, 237; 236, 238; 237, string:' '; 238, identifier:join; 239, argument_list; 239, 240; 240, list:[author['given'], author['family']]; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:author; 243, string:'given'; 244, subscript; 244, 245; 244, 246; 245, identifier:author; 246, string:'family'; 247, comment; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:orcid; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:author; 254, identifier:get; 255, argument_list; 255, 256; 256, string:'ORCID'; 257, if_statement; 257, 258; 257, 259; 258, identifier:orcid; 259, block; 259, 260; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 265; 262, subscript; 262, 263; 262, 264; 263, identifier:auth; 264, string:'ORCID'; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:orcid; 268, identifier:lstrip; 269, argument_list; 269, 270; 270, string:'http://orcid.org/'; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 278; 273, attribute; 273, 274; 273, 277; 274, subscript; 274, 275; 274, 276; 275, identifier:reference; 276, string:'authors'; 277, identifier:append; 278, argument_list; 278, 279; 279, identifier:auth; 280, elif_clause; 280, 281; 280, 284; 281, comparison_operator:is; 281, 282; 281, 283; 282, identifier:ref_key; 283, None; 284, block; 284, 285; 284, 292; 284, 298; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 288; 287, identifier:warn; 288, argument_list; 288, 289; 289, concatenated_string; 289, 290; 289, 291; 290, string:'Missing doi attribute in bibliographyLink. '; 291, string:'Setting "detail" key as a fallback; please update to the appropriate fields.'; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 297; 294, subscript; 294, 295; 294, 296; 295, identifier:reference; 296, string:'detail'; 297, identifier:ref_key; 298, if_statement; 298, 299; 298, 307; 299, comparison_operator:!=; 299, 300; 299, 306; 300, subscript; 300, 301; 300, 304; 301, subscript; 301, 302; 301, 303; 302, identifier:reference; 303, string:'detail'; 304, unary_operator:-; 304, 305; 305, integer:1; 306, string:'.'; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, augmented_assignment:+=; 309, 310; 309, 313; 310, subscript; 310, 311; 310, 312; 311, identifier:reference; 312, string:'detail'; 313, string:'.'; 314, else_clause; 314, 315; 314, 316; 315, comment; 316, block; 316, 317; 317, raise_statement; 317, 318; 318, call; 318, 319; 318, 320; 319, identifier:MissingAttributeError; 320, argument_list; 320, 321; 320, 322; 321, string:'preferredKey'; 322, string:'bibliographyLink'; 323, return_statement; 323, 324; 324, identifier:reference
def get_reference(root): """Read reference info from root of ReSpecTh XML file. Args: root (`~xml.etree.ElementTree.Element`): Root of ReSpecTh XML file Returns: properties (`dict`): Dictionary with reference information """ reference = {} elem = root.find('bibliographyLink') if elem is None: raise MissingElementError('bibliographyLink') # Try to get reference info via DOI, fall back on preferredKey if necessary. ref_doi = elem.get('doi', None) ref_key = elem.get('preferredKey', None) if ref_doi is not None: try: ref = crossref_api.works(ids=ref_doi)['message'] except (HTTPError, habanero.RequestError, ConnectionError): if ref_key is None: raise KeywordError('DOI not found and preferredKey attribute not set') else: warn('Missing doi attribute in bibliographyLink or lookup failed. ' 'Setting "detail" key as a fallback; please update to the appropriate fields.' ) reference['detail'] = ref_key if reference['detail'][-1] != '.': reference['detail'] += '.' else: if ref_key is not None: warn('Using DOI to obtain reference information, rather than preferredKey.') reference['doi'] = elem.attrib['doi'] # Now get elements of the reference data # Assume that the reference returned by the DOI lookup always has a container-title reference['journal'] = ref.get('container-title')[0] ref_year = ref.get('published-print') or ref.get('published-online') reference['year'] = int(ref_year['date-parts'][0][0]) reference['volume'] = int(ref.get('volume')) reference['pages'] = ref.get('page') reference['authors'] = [] for author in ref['author']: auth = {} auth['name'] = ' '.join([author['given'], author['family']]) # Add ORCID if available orcid = author.get('ORCID') if orcid: auth['ORCID'] = orcid.lstrip('http://orcid.org/') reference['authors'].append(auth) elif ref_key is not None: warn('Missing doi attribute in bibliographyLink. ' 'Setting "detail" key as a fallback; please update to the appropriate fields.' ) reference['detail'] = ref_key if reference['detail'][-1] != '.': reference['detail'] += '.' else: # Need one of DOI or preferredKey raise MissingAttributeError('preferredKey', 'bibliographyLink') return reference
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:ReSpecTh_to_ChemKED; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 12; 4, identifier:filename_xml; 5, default_parameter; 5, 6; 5, 7; 6, identifier:file_author; 7, string:''; 8, default_parameter; 8, 9; 8, 10; 9, identifier:file_author_orcid; 10, string:''; 11, keyword_separator; 12, default_parameter; 12, 13; 12, 14; 13, identifier:validate; 14, False; 15, block; 15, 16; 15, 18; 15, 19; 15, 28; 15, 36; 15, 37; 15, 44; 15, 45; 15, 54; 15, 55; 15, 83; 15, 84; 15, 94; 15, 95; 15, 104; 15, 105; 15, 116; 15, 117; 15, 126; 15, 127; 15, 151; 15, 167; 15, 197; 15, 213; 15, 214; 15, 225; 15, 253; 15, 254; 15, 286; 15, 298; 16, expression_statement; 16, 17; 17, comment; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:tree; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:etree; 25, identifier:parse; 26, argument_list; 26, 27; 27, identifier:filename_xml; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:root; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:tree; 34, identifier:getroot; 35, argument_list; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:properties; 40, call; 40, 41; 40, 42; 41, identifier:get_file_metadata; 42, argument_list; 42, 43; 43, identifier:root; 44, comment; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:properties; 49, string:'reference'; 50, call; 50, 51; 50, 52; 51, identifier:get_reference; 52, argument_list; 52, 53; 53, identifier:root; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 62; 57, subscript; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:properties; 60, string:'reference'; 61, string:'detail'; 62, parenthesized_expression; 62, 63; 63, binary_operator:+; 63, 64; 63, 75; 64, binary_operator:+; 64, 65; 64, 74; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:properties; 69, string:'reference'; 70, identifier:get; 71, argument_list; 71, 72; 71, 73; 72, string:'detail'; 73, string:''; 74, string:'Converted from ReSpecTh XML file '; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:os; 79, identifier:path; 80, identifier:basename; 81, argument_list; 81, 82; 82, identifier:filename_xml; 83, comment; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:properties; 88, identifier:update; 89, argument_list; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:get_experiment_kind; 92, argument_list; 92, 93; 93, identifier:root; 94, comment; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:properties; 99, string:'common-properties'; 100, call; 100, 101; 100, 102; 101, identifier:get_common_properties; 102, argument_list; 102, 103; 103, identifier:root; 104, comment; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 112; 107, subscript; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:properties; 110, string:'common-properties'; 111, string:'ignition-type'; 112, call; 112, 113; 112, 114; 113, identifier:get_ignition_type; 114, argument_list; 114, 115; 115, identifier:root; 116, comment; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 122; 119, subscript; 119, 120; 119, 121; 120, identifier:properties; 121, string:'datapoints'; 122, call; 122, 123; 122, 124; 123, identifier:get_datapoints; 124, argument_list; 124, 125; 125, identifier:root; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:has_pres_rise; 130, parenthesized_expression; 130, 131; 131, boolean_operator:or; 131, 132; 131, 137; 132, comparison_operator:in; 132, 133; 132, 134; 133, string:'pressure-rise'; 134, subscript; 134, 135; 134, 136; 135, identifier:properties; 136, string:'common-properties'; 137, call; 137, 138; 137, 139; 138, identifier:any; 139, argument_list; 139, 140; 140, list_comprehension; 140, 141; 140, 142; 140, 147; 141, True; 142, for_in_clause; 142, 143; 142, 144; 143, identifier:dp; 144, subscript; 144, 145; 144, 146; 145, identifier:properties; 146, string:'datapoints'; 147, if_clause; 147, 148; 148, comparison_operator:in; 148, 149; 148, 150; 149, string:'pressure-rise'; 150, identifier:dp; 151, if_statement; 151, 152; 151, 161; 152, boolean_operator:and; 152, 153; 152, 154; 153, identifier:has_pres_rise; 154, comparison_operator:==; 154, 155; 154, 160; 155, subscript; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:properties; 158, string:'apparatus'; 159, string:'kind'; 160, string:'rapid compression machine'; 161, block; 161, 162; 162, raise_statement; 162, 163; 163, call; 163, 164; 163, 165; 164, identifier:KeywordError; 165, argument_list; 165, 166; 166, string:'Pressure rise cannot be defined for RCM.'; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:has_vol_hist; 170, call; 170, 171; 170, 172; 171, identifier:any; 172, argument_list; 172, 173; 173, list_comprehension; 173, 174; 173, 182; 173, 187; 174, comparison_operator:==; 174, 175; 174, 181; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:t; 178, identifier:get; 179, argument_list; 179, 180; 180, string:'type'; 181, string:'volume'; 182, for_in_clause; 182, 183; 182, 184; 183, identifier:dp; 184, subscript; 184, 185; 184, 186; 185, identifier:properties; 186, string:'datapoints'; 187, for_in_clause; 187, 188; 187, 189; 188, identifier:t; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:dp; 192, identifier:get; 193, argument_list; 193, 194; 193, 195; 194, string:'time-histories'; 195, list:[{}]; 195, 196; 196, dictionary; 197, if_statement; 197, 198; 197, 207; 198, boolean_operator:and; 198, 199; 198, 200; 199, identifier:has_vol_hist; 200, comparison_operator:==; 200, 201; 200, 206; 201, subscript; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:properties; 204, string:'apparatus'; 205, string:'kind'; 206, string:'shock tube'; 207, block; 207, 208; 208, raise_statement; 208, 209; 209, call; 209, 210; 209, 211; 210, identifier:KeywordError; 211, argument_list; 211, 212; 212, string:'Volume history cannot be defined for shock tube.'; 213, comment; 214, if_statement; 214, 215; 214, 219; 215, boolean_operator:and; 215, 216; 215, 217; 216, identifier:file_author_orcid; 217, not_operator; 217, 218; 218, identifier:file_author; 219, block; 219, 220; 220, raise_statement; 220, 221; 221, call; 221, 222; 221, 223; 222, identifier:KeywordError; 223, argument_list; 223, 224; 224, string:'If file_author_orcid is specified, file_author must be as well'; 225, if_statement; 225, 226; 225, 227; 226, identifier:file_author; 227, block; 227, 228; 227, 235; 227, 244; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:temp_author; 231, dictionary; 231, 232; 232, pair; 232, 233; 232, 234; 233, string:'name'; 234, identifier:file_author; 235, if_statement; 235, 236; 235, 237; 236, identifier:file_author_orcid; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 243; 240, subscript; 240, 241; 240, 242; 241, identifier:temp_author; 242, string:'ORCID'; 243, identifier:file_author_orcid; 244, expression_statement; 244, 245; 245, call; 245, 246; 245, 251; 246, attribute; 246, 247; 246, 250; 247, subscript; 247, 248; 247, 249; 248, identifier:properties; 249, string:'file-authors'; 250, identifier:append; 251, argument_list; 251, 252; 252, identifier:temp_author; 253, comment; 254, for_statement; 254, 255; 254, 256; 254, 265; 255, identifier:idx; 256, call; 256, 257; 256, 258; 257, identifier:range; 258, argument_list; 258, 259; 259, call; 259, 260; 259, 261; 260, identifier:len; 261, argument_list; 261, 262; 262, subscript; 262, 263; 262, 264; 263, identifier:properties; 264, string:'datapoints'; 265, block; 265, 266; 266, for_statement; 266, 267; 266, 268; 266, 271; 267, identifier:prop; 268, subscript; 268, 269; 268, 270; 269, identifier:properties; 270, string:'common-properties'; 271, block; 271, 272; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 281; 274, subscript; 274, 275; 274, 280; 275, subscript; 275, 276; 275, 279; 276, subscript; 276, 277; 276, 278; 277, identifier:properties; 278, string:'datapoints'; 279, identifier:idx; 280, identifier:prop; 281, subscript; 281, 282; 281, 285; 282, subscript; 282, 283; 282, 284; 283, identifier:properties; 284, string:'common-properties'; 285, identifier:prop; 286, if_statement; 286, 287; 286, 288; 287, identifier:validate; 288, block; 288, 289; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:chemked; 293, identifier:ChemKED; 294, argument_list; 294, 295; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:dict_input; 297, identifier:properties; 298, return_statement; 298, 299; 299, identifier:properties
def ReSpecTh_to_ChemKED(filename_xml, file_author='', file_author_orcid='', *, validate=False): """Convert ReSpecTh XML file to ChemKED-compliant dictionary. Args: filename_xml (`str`): Name of ReSpecTh XML file to be converted. file_author (`str`, optional): Name to override original file author file_author_orcid (`str`, optional): ORCID of file author validate (`bool`, optional, keyword-only): Set to `True` to validate the resulting property dictionary with `ChemKED`. Set to `False` if the file is being loaded and will be validated at some other point before use. """ # get all information from XML file tree = etree.parse(filename_xml) root = tree.getroot() # get file metadata properties = get_file_metadata(root) # get reference info properties['reference'] = get_reference(root) # Save name of original data filename properties['reference']['detail'] = (properties['reference'].get('detail', '') + 'Converted from ReSpecTh XML file ' + os.path.basename(filename_xml) ) # Ensure ignition delay, and get which kind of experiment properties.update(get_experiment_kind(root)) # Get properties shared across the file properties['common-properties'] = get_common_properties(root) # Determine definition of ignition delay properties['common-properties']['ignition-type'] = get_ignition_type(root) # Now parse ignition delay datapoints properties['datapoints'] = get_datapoints(root) # Ensure inclusion of pressure rise or volume history matches apparatus. has_pres_rise = ('pressure-rise' in properties['common-properties'] or any([True for dp in properties['datapoints'] if 'pressure-rise' in dp]) ) if has_pres_rise and properties['apparatus']['kind'] == 'rapid compression machine': raise KeywordError('Pressure rise cannot be defined for RCM.') has_vol_hist = any( [t.get('type') == 'volume' for dp in properties['datapoints'] for t in dp.get('time-histories', [{}])] ) if has_vol_hist and properties['apparatus']['kind'] == 'shock tube': raise KeywordError('Volume history cannot be defined for shock tube.') # add any additional file authors if file_author_orcid and not file_author: raise KeywordError('If file_author_orcid is specified, file_author must be as well') if file_author: temp_author = {'name': file_author} if file_author_orcid: temp_author['ORCID'] = file_author_orcid properties['file-authors'].append(temp_author) # Now go through datapoints and apply common properties for idx in range(len(properties['datapoints'])): for prop in properties['common-properties']: properties['datapoints'][idx][prop] = properties['common-properties'][prop] if validate: chemked.ChemKED(dict_input=properties) return properties
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_cantera_composition_string; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:species_conversion; 7, None; 8, block; 8, 9; 8, 11; 8, 49; 8, 235; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 11, 24; 11, 35; 12, comparison_operator:in; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:composition_type; 16, list:['mole fraction', 'mass fraction']; 16, 17; 16, 18; 17, string:'mole fraction'; 18, string:'mass fraction'; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:factor; 23, float:1.0; 24, elif_clause; 24, 25; 24, 30; 25, comparison_operator:==; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:composition_type; 29, string:'mole percent'; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:factor; 34, float:100.0; 35, else_clause; 35, 36; 36, block; 36, 37; 37, raise_statement; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:ValueError; 40, argument_list; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, string:'Unknown composition type: {}'; 44, identifier:format; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:composition_type; 49, if_statement; 49, 50; 49, 53; 49, 82; 50, comparison_operator:is; 50, 51; 50, 52; 51, identifier:species_conversion; 52, None; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:comps; 57, list_comprehension; 57, 58; 57, 73; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, string:'{!s}:{:.4e}'; 61, identifier:format; 62, argument_list; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:c; 65, identifier:species_name; 66, binary_operator:/; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:c; 70, identifier:amount; 71, identifier:magnitude; 72, identifier:factor; 73, for_in_clause; 73, 74; 73, 75; 74, identifier:c; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:composition; 80, identifier:values; 81, argument_list; 82, else_clause; 82, 83; 83, block; 83, 84; 83, 88; 83, 217; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:comps; 87, list:[]; 88, for_statement; 88, 89; 88, 90; 88, 97; 89, identifier:c; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:composition; 95, identifier:values; 96, argument_list; 97, block; 97, 98; 97, 108; 97, 124; 97, 134; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:amount; 101, binary_operator:/; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:c; 105, identifier:amount; 106, identifier:magnitude; 107, identifier:factor; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:idents; 111, list_comprehension; 111, 112; 111, 118; 112, call; 112, 113; 112, 114; 113, identifier:getattr; 114, argument_list; 114, 115; 114, 116; 114, 117; 115, identifier:c; 116, identifier:s; 117, False; 118, for_in_clause; 118, 119; 118, 120; 119, identifier:s; 120, list:['species_name', 'InChI', 'SMILES']; 120, 121; 120, 122; 120, 123; 121, string:'species_name'; 122, string:'InChI'; 123, string:'SMILES'; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:present; 127, list_comprehension; 127, 128; 127, 131; 128, comparison_operator:in; 128, 129; 128, 130; 129, identifier:i; 130, identifier:species_conversion; 131, for_in_clause; 131, 132; 131, 133; 132, identifier:i; 133, identifier:idents; 134, if_statement; 134, 135; 134, 140; 134, 156; 135, not_operator; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:any; 138, argument_list; 138, 139; 139, identifier:present; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:comps; 145, identifier:append; 146, argument_list; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, string:'{!s}:{:.4e}'; 150, identifier:format; 151, argument_list; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:c; 154, identifier:species_name; 155, identifier:amount; 156, else_clause; 156, 157; 157, block; 157, 158; 157, 184; 157, 195; 157, 204; 158, if_statement; 158, 159; 158, 171; 159, comparison_operator:>; 159, 160; 159, 170; 160, call; 160, 161; 160, 162; 161, identifier:len; 162, argument_list; 162, 163; 163, list_comprehension; 163, 164; 163, 165; 163, 168; 164, identifier:i; 165, for_in_clause; 165, 166; 165, 167; 166, identifier:i; 167, identifier:present; 168, if_clause; 168, 169; 169, identifier:i; 170, integer:1; 171, block; 171, 172; 172, raise_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:ValueError; 175, argument_list; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, string:'More than one conversion present for species {}'; 179, identifier:format; 180, argument_list; 180, 181; 181, attribute; 181, 182; 181, 183; 182, identifier:c; 183, identifier:species_name; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:ident; 187, subscript; 187, 188; 187, 189; 188, identifier:idents; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:present; 192, identifier:index; 193, argument_list; 193, 194; 194, True; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:species_replacement_name; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:species_conversion; 201, identifier:pop; 202, argument_list; 202, 203; 203, identifier:ident; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:comps; 208, identifier:append; 209, argument_list; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, string:'{!s}:{:.4e}'; 213, identifier:format; 214, argument_list; 214, 215; 214, 216; 215, identifier:species_replacement_name; 216, identifier:amount; 217, if_statement; 217, 218; 217, 224; 218, comparison_operator:>; 218, 219; 218, 223; 219, call; 219, 220; 219, 221; 220, identifier:len; 221, argument_list; 221, 222; 222, identifier:species_conversion; 223, integer:0; 224, block; 224, 225; 225, raise_statement; 225, 226; 226, call; 226, 227; 226, 228; 227, identifier:ValueError; 228, argument_list; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, string:'Unknown species in conversion: {}'; 232, identifier:format; 233, argument_list; 233, 234; 234, identifier:species_conversion; 235, return_statement; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, string:', '; 239, identifier:join; 240, argument_list; 240, 241; 241, identifier:comps
def get_cantera_composition_string(self, species_conversion=None): """Get the composition in a string format suitable for input to Cantera. Returns a formatted string no matter the type of composition. As such, this method is not recommended for end users; instead, prefer the `get_cantera_mole_fraction` or `get_cantera_mass_fraction` methods. Arguments: species_conversion (`dict`, optional): Mapping of species identifier to a species name. This argument should be supplied when the name of the species in the ChemKED YAML file does not match the name of the same species in a chemical kinetic mechanism. The species identifier (the key of the mapping) can be the name, InChI, or SMILES provided in the ChemKED file, while the value associated with a key should be the desired name in the Cantera format output string. Returns: `str`: String in the ``SPEC:AMT, SPEC:AMT`` format Raises: `ValueError`: If the composition type of the `DataPoint` is not one of ``'mass fraction'``, ``'mole fraction'``, or ``'mole percent'`` """ if self.composition_type in ['mole fraction', 'mass fraction']: factor = 1.0 elif self.composition_type == 'mole percent': factor = 100.0 else: raise ValueError('Unknown composition type: {}'.format(self.composition_type)) if species_conversion is None: comps = ['{!s}:{:.4e}'.format(c.species_name, c.amount.magnitude/factor) for c in self.composition.values()] else: comps = [] for c in self.composition.values(): amount = c.amount.magnitude/factor idents = [getattr(c, s, False) for s in ['species_name', 'InChI', 'SMILES']] present = [i in species_conversion for i in idents] if not any(present): comps.append('{!s}:{:.4e}'.format(c.species_name, amount)) else: if len([i for i in present if i]) > 1: raise ValueError('More than one conversion present for species {}'.format( c.species_name)) ident = idents[present.index(True)] species_replacement_name = species_conversion.pop(ident) comps.append('{!s}:{:.4e}'.format(species_replacement_name, amount)) if len(species_conversion) > 0: raise ValueError('Unknown species in conversion: {}'.format(species_conversion)) return ', '.join(comps)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:compare_name; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:given_name; 5, identifier:family_name; 6, identifier:question_name; 7, block; 7, 8; 7, 10; 7, 11; 7, 19; 7, 27; 7, 35; 7, 36; 7, 69; 7, 70; 7, 80; 7, 90; 7, 100; 7, 101; 7, 118; 7, 138; 7, 139; 7, 156; 7, 163; 7, 185; 7, 276; 7, 277; 7, 316; 7, 317; 7, 356; 7, 357; 7, 408; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:given_name; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:given_name; 17, identifier:lower; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:family_name; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:family_name; 25, identifier:lower; 26, argument_list; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:question_name; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:question_name; 33, identifier:lower; 34, argument_list; 35, comment; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:in; 37, 38; 37, 39; 38, string:','; 39, identifier:question_name; 40, block; 40, 41; 40, 50; 40, 56; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:name_split; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:question_name; 47, identifier:split; 48, argument_list; 48, 49; 49, string:','; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:name_split; 54, identifier:reverse; 55, argument_list; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:question_name; 59, call; 59, 60; 59, 68; 60, attribute; 60, 61; 60, 67; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:' '; 64, identifier:join; 65, argument_list; 65, 66; 66, identifier:name_split; 67, identifier:strip; 68, argument_list; 69, comment; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:question_name; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:question_name; 76, identifier:replace; 77, argument_list; 77, 78; 77, 79; 78, string:'.'; 79, string:''; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:given_name; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:given_name; 86, identifier:replace; 87, argument_list; 87, 88; 87, 89; 88, string:'.'; 89, string:''; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:family_name; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:family_name; 96, identifier:replace; 97, argument_list; 97, 98; 97, 99; 98, string:'.'; 99, string:''; 100, comment; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:given_name; 104, call; 104, 105; 104, 106; 105, identifier:list; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:filter; 109, argument_list; 109, 110; 109, 111; 110, None; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:re; 114, identifier:split; 115, argument_list; 115, 116; 115, 117; 116, string:r"[, \-.]+"; 117, identifier:given_name; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:num_family_names; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:list; 126, argument_list; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:filter; 129, argument_list; 129, 130; 129, 131; 130, None; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:re; 134, identifier:split; 135, argument_list; 135, 136; 135, 137; 136, string:"[, .]+"; 137, identifier:family_name; 138, comment; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:name_split; 142, call; 142, 143; 142, 144; 143, identifier:list; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 147; 146, identifier:filter; 147, argument_list; 147, 148; 147, 149; 148, None; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:re; 152, identifier:split; 153, argument_list; 153, 154; 153, 155; 154, string:r"[, \-.]+"; 155, identifier:question_name; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:first_name; 159, list:[name_split[0]]; 159, 160; 160, subscript; 160, 161; 160, 162; 161, identifier:name_split; 162, integer:0; 163, if_statement; 163, 164; 163, 170; 164, comparison_operator:>; 164, 165; 164, 169; 165, call; 165, 166; 165, 167; 166, identifier:len; 167, argument_list; 167, 168; 168, identifier:name_split; 169, integer:2; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, augmented_assignment:+=; 172, 173; 172, 174; 173, identifier:first_name; 174, list_comprehension; 174, 175; 174, 176; 175, identifier:n; 176, for_in_clause; 176, 177; 176, 178; 177, identifier:n; 178, subscript; 178, 179; 178, 180; 179, identifier:name_split; 180, slice; 180, 181; 180, 182; 180, 183; 181, integer:1; 182, colon; 183, unary_operator:-; 183, 184; 184, identifier:num_family_names; 185, if_statement; 185, 186; 185, 202; 185, 203; 185, 235; 186, boolean_operator:and; 186, 187; 186, 193; 187, comparison_operator:>; 187, 188; 187, 192; 188, call; 188, 189; 188, 190; 189, identifier:len; 190, argument_list; 190, 191; 191, identifier:first_name; 192, integer:1; 193, comparison_operator:==; 193, 194; 193, 198; 194, call; 194, 195; 194, 196; 195, identifier:len; 196, argument_list; 196, 197; 197, identifier:given_name; 198, call; 198, 199; 198, 200; 199, identifier:len; 200, argument_list; 200, 201; 201, identifier:first_name; 202, comment; 203, block; 203, 204; 204, for_statement; 204, 205; 204, 206; 204, 214; 205, identifier:i; 206, call; 206, 207; 206, 208; 207, identifier:range; 208, argument_list; 208, 209; 208, 210; 209, integer:1; 210, call; 210, 211; 210, 212; 211, identifier:len; 212, argument_list; 212, 213; 213, identifier:first_name; 214, block; 214, 215; 214, 225; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 220; 217, subscript; 217, 218; 217, 219; 218, identifier:first_name; 219, identifier:i; 220, subscript; 220, 221; 220, 224; 221, subscript; 221, 222; 221, 223; 222, identifier:first_name; 223, identifier:i; 224, integer:0; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, subscript; 227, 228; 227, 229; 228, identifier:given_name; 229, identifier:i; 230, subscript; 230, 231; 230, 234; 231, subscript; 231, 232; 231, 233; 232, identifier:given_name; 233, identifier:i; 234, integer:0; 235, elif_clause; 235, 236; 235, 245; 236, comparison_operator:!=; 236, 237; 236, 241; 237, call; 237, 238; 237, 239; 238, identifier:len; 239, argument_list; 239, 240; 240, identifier:given_name; 241, call; 241, 242; 241, 243; 242, identifier:len; 243, argument_list; 243, 244; 244, identifier:first_name; 245, block; 245, 246; 245, 260; 245, 268; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:min_names; 249, call; 249, 250; 249, 251; 250, identifier:min; 251, argument_list; 251, 252; 251, 256; 252, call; 252, 253; 252, 254; 253, identifier:len; 254, argument_list; 254, 255; 255, identifier:given_name; 256, call; 256, 257; 256, 258; 257, identifier:len; 258, argument_list; 258, 259; 259, identifier:first_name; 260, expression_statement; 260, 261; 261, assignment; 261, 262; 261, 263; 262, identifier:first_name; 263, subscript; 263, 264; 263, 265; 264, identifier:first_name; 265, slice; 265, 266; 265, 267; 266, colon; 267, identifier:min_names; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:given_name; 271, subscript; 271, 272; 271, 273; 272, identifier:given_name; 273, slice; 273, 274; 273, 275; 274, colon; 275, identifier:min_names; 276, comment; 277, if_statement; 277, 278; 277, 295; 278, boolean_operator:or; 278, 279; 278, 287; 279, comparison_operator:==; 279, 280; 279, 286; 280, call; 280, 281; 280, 282; 281, identifier:len; 282, argument_list; 282, 283; 283, subscript; 283, 284; 283, 285; 284, identifier:first_name; 285, integer:0; 286, integer:1; 287, comparison_operator:==; 287, 288; 287, 294; 288, call; 288, 289; 288, 290; 289, identifier:len; 290, argument_list; 290, 291; 291, subscript; 291, 292; 291, 293; 292, identifier:given_name; 293, integer:0; 294, integer:1; 295, block; 295, 296; 295, 306; 296, expression_statement; 296, 297; 297, assignment; 297, 298; 297, 301; 298, subscript; 298, 299; 298, 300; 299, identifier:given_name; 300, integer:0; 301, subscript; 301, 302; 301, 305; 302, subscript; 302, 303; 302, 304; 303, identifier:given_name; 304, integer:0; 305, integer:0; 306, expression_statement; 306, 307; 307, assignment; 307, 308; 307, 311; 308, subscript; 308, 309; 308, 310; 309, identifier:first_name; 310, integer:0; 311, subscript; 311, 312; 311, 315; 312, subscript; 312, 313; 312, 314; 313, identifier:first_name; 314, integer:0; 315, integer:0; 316, comment; 317, if_statement; 317, 318; 317, 335; 318, boolean_operator:or; 318, 319; 318, 327; 319, comparison_operator:>; 319, 320; 319, 326; 320, call; 320, 321; 320, 322; 321, identifier:len; 322, argument_list; 322, 323; 323, subscript; 323, 324; 323, 325; 324, identifier:first_name; 325, integer:0; 326, integer:1; 327, comparison_operator:>; 327, 328; 327, 334; 328, call; 328, 329; 328, 330; 329, identifier:len; 330, argument_list; 330, 331; 331, subscript; 331, 332; 331, 333; 332, identifier:given_name; 333, integer:0; 334, integer:1; 335, block; 335, 336; 335, 346; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 341; 338, subscript; 338, 339; 338, 340; 339, identifier:given_name; 340, integer:0; 341, subscript; 341, 342; 341, 345; 342, subscript; 342, 343; 342, 344; 343, identifier:given_name; 344, integer:0; 345, integer:0; 346, expression_statement; 346, 347; 347, assignment; 347, 348; 347, 351; 348, subscript; 348, 349; 348, 350; 349, identifier:first_name; 350, integer:0; 351, subscript; 351, 352; 351, 355; 352, subscript; 352, 353; 352, 354; 353, identifier:name_split; 354, integer:0; 355, integer:0; 356, comment; 357, if_statement; 357, 358; 357, 365; 357, 392; 358, boolean_operator:and; 358, 359; 358, 362; 359, comparison_operator:==; 359, 360; 359, 361; 360, identifier:num_family_names; 361, integer:1; 362, comparison_operator:in; 362, 363; 362, 364; 363, string:'-'; 364, identifier:family_name; 365, block; 365, 366; 365, 375; 366, expression_statement; 366, 367; 367, assignment; 367, 368; 367, 369; 368, identifier:num_hyphen; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:family_name; 372, identifier:count; 373, argument_list; 373, 374; 374, string:'-'; 375, expression_statement; 375, 376; 376, assignment; 376, 377; 376, 378; 377, identifier:family_name_compare; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, string:'-'; 381, identifier:join; 382, argument_list; 382, 383; 383, subscript; 383, 384; 383, 385; 384, identifier:name_split; 385, slice; 385, 386; 385, 391; 386, unary_operator:-; 386, 387; 387, parenthesized_expression; 387, 388; 388, binary_operator:+; 388, 389; 388, 390; 389, identifier:num_hyphen; 390, integer:1; 391, colon; 392, else_clause; 392, 393; 393, block; 393, 394; 394, expression_statement; 394, 395; 395, assignment; 395, 396; 395, 397; 396, identifier:family_name_compare; 397, call; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, string:' '; 400, identifier:join; 401, argument_list; 401, 402; 402, subscript; 402, 403; 402, 404; 403, identifier:name_split; 404, slice; 404, 405; 404, 407; 405, unary_operator:-; 405, 406; 406, identifier:num_family_names; 407, colon; 408, return_statement; 408, 409; 409, boolean_operator:and; 409, 410; 409, 413; 410, comparison_operator:==; 410, 411; 410, 412; 411, identifier:given_name; 412, identifier:first_name; 413, comparison_operator:==; 413, 414; 413, 415; 414, identifier:family_name; 415, identifier:family_name_compare
def compare_name(given_name, family_name, question_name): """Compares a name in question to a specified name separated into given and family. The name in question ``question_name`` can be of varying format, including "Kyle E. Niemeyer", "Kyle Niemeyer", "K. E. Niemeyer", "KE Niemeyer", and "K Niemeyer". Other possibilities include names with hyphens such as "Chih-Jen Sung", "C. J. Sung", "C-J Sung". Examples: >>> compare_name('Kyle', 'Niemeyer', 'Kyle E Niemeyer') True >>> compare_name('Chih-Jen', 'Sung', 'C-J Sung') True Args: given_name (`str`): Given (or first) name to be checked against. family_name (`str`): Family (or last) name to be checked against. question_name (`str`): The whole name in question. Returns: `bool`: The return value. True for successful comparison, False otherwise. """ # lowercase everything given_name = given_name.lower() family_name = family_name.lower() question_name = question_name.lower() # rearrange names given as "last, first middle" if ',' in question_name: name_split = question_name.split(',') name_split.reverse() question_name = ' '.join(name_split).strip() # remove periods question_name = question_name.replace('.', '') given_name = given_name.replace('.', '') family_name = family_name.replace('.', '') # split names by , <space> - . given_name = list(filter(None, re.split(r"[, \-.]+", given_name))) num_family_names = len(list(filter(None, re.split("[, .]+", family_name)))) # split name in question by , <space> - . name_split = list(filter(None, re.split(r"[, \-.]+", question_name))) first_name = [name_split[0]] if len(name_split) > 2: first_name += [n for n in name_split[1:-num_family_names]] if len(first_name) > 1 and len(given_name) == len(first_name): # both have same number of first and middle names/initials for i in range(1, len(first_name)): first_name[i] = first_name[i][0] given_name[i] = given_name[i][0] elif len(given_name) != len(first_name): min_names = min(len(given_name), len(first_name)) first_name = first_name[:min_names] given_name = given_name[:min_names] # first initial if len(first_name[0]) == 1 or len(given_name[0]) == 1: given_name[0] = given_name[0][0] first_name[0] = first_name[0][0] # first and middle initials combined if len(first_name[0]) > 1 or len(given_name[0]) > 1: given_name[0] = given_name[0][0] first_name[0] = name_split[0][0] # Hyphenated last name may need to be reconnected if num_family_names == 1 and '-' in family_name: num_hyphen = family_name.count('-') family_name_compare = '-'.join(name_split[-(num_hyphen + 1):]) else: family_name_compare = ' '.join(name_split[-num_family_names:]) return given_name == first_name and family_name == family_name_compare
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sorted_options; 3, parameters; 3, 4; 4, identifier:sort_options; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, list_comprehension; 9, 10; 9, 36; 10, dictionary; 10, 11; 10, 16; 11, pair; 11, 12; 11, 13; 12, string:'title'; 13, subscript; 13, 14; 13, 15; 14, identifier:v; 15, string:'title'; 16, pair; 16, 17; 16, 18; 17, string:'value'; 18, parenthesized_expression; 18, 19; 19, conditional_expression:if; 19, 20; 19, 26; 19, 35; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, string:'-{0}'; 23, identifier:format; 24, argument_list; 24, 25; 25, identifier:k; 26, comparison_operator:==; 26, 27; 26, 34; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:v; 30, identifier:get; 31, argument_list; 31, 32; 31, 33; 32, string:'default_order'; 33, string:'asc'; 34, string:'desc'; 35, identifier:k; 36, for_in_clause; 36, 37; 36, 40; 37, pattern_list; 37, 38; 37, 39; 38, identifier:k; 39, identifier:v; 40, call; 40, 41; 40, 42; 41, identifier:sorted; 42, argument_list; 42, 43; 42, 48; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:sort_options; 46, identifier:items; 47, argument_list; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:key; 50, lambda; 50, 51; 50, 53; 51, lambda_parameters; 51, 52; 52, identifier:x; 53, call; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:x; 57, integer:1; 58, identifier:get; 59, argument_list; 59, 60; 59, 61; 60, string:'order'; 61, integer:0
def sorted_options(sort_options): """Sort sort options for display. :param sort_options: A dictionary containing the field name as key and asc/desc as value. :returns: A dictionary with sorting options for Invenio-Search-JS. """ return [ { 'title': v['title'], 'value': ('-{0}'.format(k) if v.get('default_order', 'asc') == 'desc' else k), } for k, v in sorted(sort_options.items(), key=lambda x: x[1].get('order', 0)) ]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:insert_files; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 4, identifier:self; 5, identifier:rootpath; 6, default_parameter; 6, 7; 6, 8; 7, identifier:directoryInFilter; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:directoryExFilter; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:compileInFilter; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:compileExFilter; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:contentInFilter; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:contentExFilter; 23, None; 24, block; 24, 25; 24, 27; 24, 28; 24, 39; 24, 50; 24, 61; 24, 72; 24, 83; 24, 94; 24, 137; 25, expression_statement; 25, 26; 26, comment; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:directoryInFilter; 31, conditional_expression:if; 31, 32; 31, 35; 31, 38; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:DirectoryInFilter; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:directoryInFilter; 37, None; 38, identifier:directoryInFilter; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:directoryExFilter; 42, conditional_expression:if; 42, 43; 42, 46; 42, 49; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:DirectoryExFilter; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:directoryExFilter; 48, None; 49, identifier:directoryExFilter; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:compileInFilter; 53, conditional_expression:if; 53, 54; 53, 57; 53, 60; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:CompileInFilter; 57, comparison_operator:is; 57, 58; 57, 59; 58, identifier:compileInFilter; 59, None; 60, identifier:compileInFilter; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:compileExFilter; 64, conditional_expression:if; 64, 65; 64, 68; 64, 71; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:CompileExFilter; 68, comparison_operator:is; 68, 69; 68, 70; 69, identifier:compileExFilter; 70, None; 71, identifier:compileExFilter; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:contentInFilter; 75, conditional_expression:if; 75, 76; 75, 79; 75, 82; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:ContentInFilter; 79, comparison_operator:is; 79, 80; 79, 81; 80, identifier:contentInFilter; 81, None; 82, identifier:contentInFilter; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:contentExFilter; 86, conditional_expression:if; 86, 87; 86, 90; 86, 93; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:ContentExFilter; 90, comparison_operator:is; 90, 91; 90, 92; 91, identifier:contentExFilter; 92, None; 93, identifier:contentExFilter; 94, function_definition; 94, 95; 94, 96; 94, 100; 95, function_name:filter; 96, parameters; 96, 97; 96, 98; 96, 99; 97, identifier:text; 98, identifier:filters; 99, identifier:explicit; 100, block; 100, 101; 100, 103; 100, 120; 101, expression_statement; 101, 102; 102, comment; 103, if_statement; 103, 104; 103, 105; 104, identifier:explicit; 105, block; 105, 106; 106, return_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:any; 109, generator_expression; 109, 110; 109, 117; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:fnmatch; 113, identifier:fnmatch; 114, argument_list; 114, 115; 114, 116; 115, identifier:text; 116, identifier:f; 117, for_in_clause; 117, 118; 117, 119; 118, identifier:f; 119, identifier:filters; 120, return_statement; 120, 121; 121, boolean_operator:or; 121, 122; 121, 124; 122, not_operator; 122, 123; 123, identifier:filters; 124, call; 124, 125; 124, 126; 125, identifier:any; 126, generator_expression; 126, 127; 126, 134; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:fnmatch; 130, identifier:fnmatch; 131, argument_list; 131, 132; 131, 133; 132, identifier:text; 133, identifier:f; 134, for_in_clause; 134, 135; 134, 136; 135, identifier:f; 136, identifier:filters; 137, for_statement; 137, 138; 137, 142; 137, 148; 138, pattern_list; 138, 139; 138, 140; 138, 141; 139, identifier:root; 140, identifier:dirnames; 141, identifier:filenames; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:os; 145, identifier:walk; 146, argument_list; 146, 147; 147, identifier:rootpath; 148, block; 148, 149; 148, 167; 148, 168; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:searchdir; 152, call; 152, 153; 152, 158; 153, attribute; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:os; 156, identifier:path; 157, identifier:normpath; 158, argument_list; 158, 159; 159, call; 159, 160; 159, 165; 160, attribute; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:os; 163, identifier:path; 164, identifier:normcase; 165, argument_list; 165, 166; 166, identifier:root; 167, comment; 168, if_statement; 168, 169; 168, 175; 168, 183; 169, call; 169, 170; 169, 171; 170, identifier:filter; 171, argument_list; 171, 172; 171, 173; 171, 174; 172, identifier:searchdir; 173, identifier:directoryExFilter; 174, True; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 182; 178, subscript; 178, 179; 178, 180; 179, identifier:dirnames; 180, slice; 180, 181; 181, colon; 182, list:[]; 183, elif_clause; 183, 184; 183, 190; 184, call; 184, 185; 184, 186; 185, identifier:filter; 186, argument_list; 186, 187; 186, 188; 186, 189; 187, identifier:searchdir; 188, identifier:directoryInFilter; 189, False; 190, block; 190, 191; 191, for_statement; 191, 192; 191, 193; 191, 206; 192, identifier:filepath; 193, list_comprehension; 193, 194; 193, 203; 194, call; 194, 195; 194, 200; 195, attribute; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:os; 198, identifier:path; 199, identifier:join; 200, argument_list; 200, 201; 200, 202; 201, identifier:root; 202, identifier:filename; 203, for_in_clause; 203, 204; 203, 205; 204, identifier:filename; 205, identifier:filenames; 206, block; 206, 207; 207, if_statement; 207, 208; 207, 222; 207, 232; 208, boolean_operator:and; 208, 209; 208, 215; 209, call; 209, 210; 209, 211; 210, identifier:filter; 211, argument_list; 211, 212; 211, 213; 211, 214; 212, identifier:filepath; 213, identifier:compileInFilter; 214, False; 215, not_operator; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:filter; 218, argument_list; 218, 219; 218, 220; 218, 221; 219, identifier:filepath; 220, identifier:compileExFilter; 221, True; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 230; 225, attribute; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:self; 228, identifier:CompileFiles; 229, identifier:append; 230, argument_list; 230, 231; 231, identifier:filepath; 232, elif_clause; 232, 233; 232, 247; 233, boolean_operator:and; 233, 234; 233, 240; 234, call; 234, 235; 234, 236; 235, identifier:filter; 236, argument_list; 236, 237; 236, 238; 236, 239; 237, identifier:filepath; 238, identifier:contentInFilter; 239, False; 240, not_operator; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:filter; 243, argument_list; 243, 244; 243, 245; 243, 246; 244, identifier:filepath; 245, identifier:contentExFilter; 246, True; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 255; 250, attribute; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:ContentFiles; 254, identifier:append; 255, argument_list; 255, 256; 256, identifier:filepath
def insert_files(self, rootpath, directoryInFilter=None, directoryExFilter=None, compileInFilter=None, compileExFilter=None, contentInFilter=None, contentExFilter=None): """ Inserts files by recursive traversing the rootpath and inserting files according the addition filter parameters. :param str rootpath: The absolute path to the root directory. :param list directoryInFilter: A list of fnmatch expressions to match directories to be included. A `None` value will default to :attr:`DirectoryInFilter`. :param list directoryExFilter: A list of fnmatch expressions to match directories to be excluded. A `None` value will default to :attr:`DirectoryExFilter`. :param list compileInFilter: A list of fnmatch expressions to match compile files to be included. A `None` value will default to :attr:`CompileInFilter`. :param list compileExFilter: A list of fnmatch expressions to match compile files to be excludes. A `None` value will default to :attr:`CompileExFilter`. :param list contentInFilter: A list of fnmatch expressions to match content files to be includes. A `None` value will default to :attr:`ContentInFilter`. :param list contentExFilter: A list of fnmatch expressions to match content files to be excludes. A `None` value will default to :attr:`ContentExFilter`. """ # Overrides directoryInFilter = self.DirectoryInFilter if directoryInFilter is None else directoryInFilter directoryExFilter = self.DirectoryExFilter if directoryExFilter is None else directoryExFilter compileInFilter = self.CompileInFilter if compileInFilter is None else compileInFilter compileExFilter = self.CompileExFilter if compileExFilter is None else compileExFilter contentInFilter = self.ContentInFilter if contentInFilter is None else contentInFilter contentExFilter = self.ContentExFilter if contentExFilter is None else contentExFilter def filter(text, filters, explicit): """ Convience filter function :param text text: The target text. :param list filters: The collection of fnmatch expressions :param bool explicit: Flag denoting an the empty filter collection return match failure. """ if explicit: return any(fnmatch.fnmatch(text, f) for f in filters) return not filters or any(fnmatch.fnmatch(text, f) for f in filters) for root, dirnames, filenames in os.walk(rootpath): searchdir = os.path.normpath(os.path.normcase(root)) # If the root dir matches an excluded directory, stop any further searches if filter(searchdir, directoryExFilter, True): dirnames[:] = [] elif filter(searchdir, directoryInFilter, False): for filepath in [os.path.join(root, filename) for filename in filenames]: if filter(filepath, compileInFilter, False) and not filter(filepath, compileExFilter, True): self.CompileFiles.append(filepath) elif filter(filepath, contentInFilter, False) and not filter(filepath, contentExFilter, True): self.ContentFiles.append(filepath)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:make_variant; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:cls; 5, identifier:converters; 6, default_parameter; 6, 7; 6, 8; 7, identifier:re_opts; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:compiled; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:strict; 14, True; 15, block; 15, 16; 15, 18; 15, 19; 15, 20; 15, 23; 15, 35; 15, 46; 15, 61; 15, 69; 15, 76; 15, 89; 15, 115; 15, 121; 15, 130; 15, 131; 15, 137; 16, expression_statement; 16, 17; 17, comment; 18, comment; 19, comment; 20, assert_statement; 20, 21; 20, 22; 21, identifier:converters; 22, string:"REQUIRE: Non-empty list."; 23, if_statement; 23, 24; 23, 30; 24, comparison_operator:==; 24, 25; 24, 29; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, identifier:converters; 29, integer:1; 30, block; 30, 31; 31, return_statement; 31, 32; 32, subscript; 32, 33; 32, 34; 33, identifier:converters; 34, integer:0; 35, if_statement; 35, 36; 35, 39; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:re_opts; 38, None; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:re_opts; 43, attribute; 43, 44; 43, 45; 44, identifier:cls; 45, identifier:default_re_opts; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:pattern; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, string:r")|("; 52, identifier:join; 53, argument_list; 53, 54; 54, list_comprehension; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:tc; 57, identifier:pattern; 58, for_in_clause; 58, 59; 58, 60; 59, identifier:tc; 60, identifier:converters; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:pattern; 64, binary_operator:+; 64, 65; 64, 68; 65, binary_operator:+; 65, 66; 65, 67; 66, string:r"("; 67, identifier:pattern; 68, string:")"; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:group_count; 72, call; 72, 73; 72, 74; 73, identifier:len; 74, argument_list; 74, 75; 75, identifier:converters; 76, for_statement; 76, 77; 76, 78; 76, 79; 77, identifier:converter; 78, identifier:converters; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, augmented_assignment:+=; 81, 82; 81, 83; 82, identifier:group_count; 83, call; 83, 84; 83, 85; 84, identifier:pattern_group_count; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:converter; 88, identifier:pattern; 89, if_statement; 89, 90; 89, 91; 89, 103; 90, identifier:compiled; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:convert_variant; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:cls; 98, identifier:__create_convert_variant_compiled; 99, argument_list; 99, 100; 99, 101; 99, 102; 100, identifier:converters; 101, identifier:re_opts; 102, identifier:strict; 103, else_clause; 103, 104; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:convert_variant; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:cls; 111, identifier:__create_convert_variant; 112, argument_list; 112, 113; 112, 114; 113, identifier:re_opts; 114, identifier:strict; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:convert_variant; 119, identifier:pattern; 120, identifier:pattern; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:convert_variant; 125, identifier:converters; 126, call; 126, 127; 126, 128; 127, identifier:tuple; 128, argument_list; 128, 129; 129, identifier:converters; 130, comment; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:convert_variant; 135, identifier:regex_group_count; 136, identifier:group_count; 137, return_statement; 137, 138; 138, identifier:convert_variant
def make_variant(cls, converters, re_opts=None, compiled=False, strict=True): """ Creates a type converter for a number of type converter alternatives. The first matching type converter is used. REQUIRES: type_converter.pattern attribute :param converters: List of type converters as alternatives. :param re_opts: Regular expression options zu use (=default_re_opts). :param compiled: Use compiled regexp matcher, if true (=False). :param strict: Enable assertion checks. :return: Type converter function object. .. note:: Works only with named fields in :class:`parse.Parser`. Parser needs group_index delta for unnamed/fixed fields. This is not supported for user-defined types. Otherwise, you need to use :class:`parse_type.parse.Parser` (patched version of the :mod:`parse` module). """ # -- NOTE: Uses double-dispatch with regex pattern rematch because # match is not passed through to primary type converter. assert converters, "REQUIRE: Non-empty list." if len(converters) == 1: return converters[0] if re_opts is None: re_opts = cls.default_re_opts pattern = r")|(".join([tc.pattern for tc in converters]) pattern = r"("+ pattern + ")" group_count = len(converters) for converter in converters: group_count += pattern_group_count(converter.pattern) if compiled: convert_variant = cls.__create_convert_variant_compiled(converters, re_opts, strict) else: convert_variant = cls.__create_convert_variant(re_opts, strict) convert_variant.pattern = pattern convert_variant.converters = tuple(converters) # OLD: convert_variant.group_count = group_count convert_variant.regex_group_count = group_count return convert_variant
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:info; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, string:''' Returns information on all the registered checkers. Sorted by namespace and then name :returns a list of CheckerInfo '''; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 11; 10, identifier:sorted; 11, argument_list; 11, 12; 11, 19; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:_checkers; 17, identifier:values; 18, argument_list; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:key; 21, lambda; 21, 22; 21, 24; 22, lambda_parameters; 22, 23; 23, identifier:x; 24, tuple; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:x; 27, identifier:ns; 28, attribute; 28, 29; 28, 30; 29, identifier:x; 30, identifier:name
def info(self): ''' Returns information on all the registered checkers. Sorted by namespace and then name :returns a list of CheckerInfo ''' return sorted(self._checkers.values(), key=lambda x: (x.ns, x.name))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:extract_from_text; 3, parameters; 3, 4; 4, identifier:text; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:tools; 12, identifier:remove_duplicates; 13, argument_list; 13, 14; 14, list_comprehension; 14, 15; 14, 18; 14, 26; 15, subscript; 15, 16; 15, 17; 16, identifier:i; 17, integer:0; 18, for_in_clause; 18, 19; 18, 20; 19, identifier:i; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:REGEX; 23, identifier:findall; 24, argument_list; 24, 25; 25, identifier:text; 26, if_clause; 26, 27; 27, comparison_operator:!=; 27, 28; 27, 29; 28, identifier:i; 29, string:''
def extract_from_text(text): """ Extract HAL ids from a text. :param text: The text to extract HAL ids from. :returns: A list of matching HAL ids. >>> sorted(extract_from_text("hal-01258754 hal-01258754v2 foobar")) ['hal-01258754', 'hal-01258754v2'] """ return tools.remove_duplicates([i[0] for i in REGEX.findall(text) if i != ''])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:add_caveat; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:cav; 6, default_parameter; 6, 7; 6, 8; 7, identifier:key; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:loc; 11, None; 12, block; 12, 13; 12, 15; 12, 41; 12, 51; 12, 60; 12, 120; 12, 129; 12, 130; 12, 155; 12, 170; 12, 205; 13, expression_statement; 13, 14; 14, string:'''Add a caveat to the macaroon. It encrypts it using the given key pair and by looking up the location using the given locator. As a special case, if the caveat's Location field has the prefix "local " the caveat is added as a client self-discharge caveat using the public key base64-encoded in the rest of the location. In this case, the Condition field must be empty. The resulting third-party caveat will encode the condition "true" encrypted with that public key. @param cav the checkers.Caveat to be added. @param key the public key to encrypt third party caveat. @param loc locator to find information on third parties when adding third party caveats. It is expected to have a third_party_info method that will be called with a location string and should return a ThirdPartyInfo instance holding the requested information. '''; 15, if_statement; 15, 16; 15, 21; 16, comparison_operator:is; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:cav; 19, identifier:location; 20, None; 21, block; 21, 22; 21, 40; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:_macaroon; 28, identifier:add_first_party_caveat; 29, argument_list; 29, 30; 30, attribute; 30, 31; 30, 39; 31, call; 31, 32; 31, 37; 32, attribute; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:namespace; 36, identifier:resolve_caveat; 37, argument_list; 37, 38; 38, identifier:cav; 39, identifier:condition; 40, return_statement; 41, if_statement; 41, 42; 41, 45; 42, comparison_operator:is; 42, 43; 42, 44; 43, identifier:key; 44, None; 45, block; 45, 46; 46, raise_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:ValueError; 49, argument_list; 49, 50; 50, string:'no private key to encrypt third party caveat'; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:local_info; 54, call; 54, 55; 54, 56; 55, identifier:_parse_local_location; 56, argument_list; 56, 57; 57, attribute; 57, 58; 57, 59; 58, identifier:cav; 59, identifier:location; 60, if_statement; 60, 61; 60, 64; 60, 97; 61, comparison_operator:is; 61, 62; 61, 63; 62, identifier:local_info; 63, None; 64, block; 64, 65; 64, 69; 64, 83; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:info; 68, identifier:local_info; 69, if_statement; 69, 70; 69, 75; 70, comparison_operator:is; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:cav; 73, identifier:condition; 74, string:''; 75, block; 75, 76; 76, raise_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:ValueError; 79, argument_list; 79, 80; 80, concatenated_string; 80, 81; 80, 82; 81, string:'cannot specify caveat condition in '; 82, string:'local third-party caveat'; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:cav; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:checkers; 89, identifier:Caveat; 90, argument_list; 90, 91; 90, 94; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:location; 93, string:'local'; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:condition; 96, string:'true'; 97, else_clause; 97, 98; 98, block; 98, 99; 98, 109; 99, if_statement; 99, 100; 99, 103; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:loc; 102, None; 103, block; 103, 104; 104, raise_statement; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:ValueError; 107, argument_list; 107, 108; 108, string:'no locator when adding third party caveat'; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:info; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:loc; 115, identifier:third_party_info; 116, argument_list; 116, 117; 117, attribute; 117, 118; 117, 119; 118, identifier:cav; 119, identifier:location; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:root_key; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:os; 126, identifier:urandom; 127, argument_list; 127, 128; 128, integer:24; 129, comment; 130, if_statement; 130, 131; 130, 138; 131, comparison_operator:<; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:_version; 135, attribute; 135, 136; 135, 137; 136, identifier:info; 137, identifier:version; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:info; 142, call; 142, 143; 142, 144; 143, identifier:ThirdPartyInfo; 144, argument_list; 144, 145; 144, 150; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:version; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:_version; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:public_key; 152, attribute; 152, 153; 152, 154; 153, identifier:info; 154, identifier:public_key; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:caveat_info; 158, call; 158, 159; 158, 160; 159, identifier:encode_caveat; 160, argument_list; 160, 161; 160, 164; 160, 165; 160, 166; 160, 167; 161, attribute; 161, 162; 161, 163; 162, identifier:cav; 163, identifier:condition; 164, identifier:root_key; 165, identifier:info; 166, identifier:key; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:_namespace; 170, if_statement; 170, 171; 170, 176; 170, 177; 170, 178; 170, 179; 170, 184; 171, comparison_operator:<; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:info; 174, identifier:version; 175, identifier:VERSION_3; 176, comment; 177, comment; 178, comment; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:id; 183, identifier:caveat_info; 184, else_clause; 184, 185; 185, block; 185, 186; 185, 197; 186, expression_statement; 186, 187; 187, assignment; 187, 188; 187, 189; 188, identifier:id; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:self; 192, identifier:_new_caveat_id; 193, argument_list; 193, 194; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:_caveat_id_prefix; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 204; 199, subscript; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:_caveat_data; 203, identifier:id; 204, identifier:caveat_info; 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:_macaroon; 211, identifier:add_third_party_caveat; 212, argument_list; 212, 213; 212, 216; 212, 217; 213, attribute; 213, 214; 213, 215; 214, identifier:cav; 215, identifier:location; 216, identifier:root_key; 217, identifier:id
def add_caveat(self, cav, key=None, loc=None): '''Add a caveat to the macaroon. It encrypts it using the given key pair and by looking up the location using the given locator. As a special case, if the caveat's Location field has the prefix "local " the caveat is added as a client self-discharge caveat using the public key base64-encoded in the rest of the location. In this case, the Condition field must be empty. The resulting third-party caveat will encode the condition "true" encrypted with that public key. @param cav the checkers.Caveat to be added. @param key the public key to encrypt third party caveat. @param loc locator to find information on third parties when adding third party caveats. It is expected to have a third_party_info method that will be called with a location string and should return a ThirdPartyInfo instance holding the requested information. ''' if cav.location is None: self._macaroon.add_first_party_caveat( self.namespace.resolve_caveat(cav).condition) return if key is None: raise ValueError( 'no private key to encrypt third party caveat') local_info = _parse_local_location(cav.location) if local_info is not None: info = local_info if cav.condition is not '': raise ValueError( 'cannot specify caveat condition in ' 'local third-party caveat') cav = checkers.Caveat(location='local', condition='true') else: if loc is None: raise ValueError( 'no locator when adding third party caveat') info = loc.third_party_info(cav.location) root_key = os.urandom(24) # Use the least supported version to encode the caveat. if self._version < info.version: info = ThirdPartyInfo( version=self._version, public_key=info.public_key, ) caveat_info = encode_caveat( cav.condition, root_key, info, key, self._namespace) if info.version < VERSION_3: # We're encoding for an earlier client or third party which does # not understand bundled caveat info, so use the encoded # caveat information as the caveat id. id = caveat_info else: id = self._new_caveat_id(self._caveat_id_prefix) self._caveat_data[id] = caveat_info self._macaroon.add_third_party_caveat(cav.location, root_key, id)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_new_caveat_id; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:base; 6, block; 6, 7; 6, 9; 6, 15; 6, 46; 6, 47; 6, 48; 6, 49; 6, 58; 6, 66; 7, expression_statement; 7, 8; 8, string:'''Return a third party caveat id This does not duplicate any third party caveat ids already inside macaroon. If base is non-empty, it is used as the id prefix. @param base bytes @return bytes '''; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:id; 12, call; 12, 13; 12, 14; 13, identifier:bytearray; 14, argument_list; 15, if_statement; 15, 16; 15, 22; 15, 30; 16, comparison_operator:>; 16, 17; 16, 21; 17, call; 17, 18; 17, 19; 18, identifier:len; 19, argument_list; 19, 20; 20, identifier:base; 21, integer:0; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:id; 27, identifier:extend; 28, argument_list; 28, 29; 29, identifier:base; 30, else_clause; 30, 31; 30, 32; 30, 33; 30, 34; 30, 35; 30, 36; 30, 37; 30, 38; 31, comment; 32, comment; 33, comment; 34, comment; 35, comment; 36, comment; 37, comment; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:id; 43, identifier:append; 44, argument_list; 44, 45; 45, identifier:VERSION_3; 46, comment; 47, comment; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:i; 52, call; 52, 53; 52, 54; 53, identifier:len; 54, argument_list; 54, 55; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_caveat_data; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:caveats; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:_macaroon; 65, identifier:caveats; 66, while_statement; 66, 67; 66, 68; 66, 69; 66, 70; 66, 71; 66, 72; 66, 73; 67, True; 68, comment; 69, comment; 70, comment; 71, comment; 72, comment; 73, block; 73, 74; 73, 81; 73, 87; 73, 91; 73, 114; 73, 123; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:temp; 77, subscript; 77, 78; 77, 79; 78, identifier:id; 79, slice; 79, 80; 80, colon; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:encode_uvarint; 84, argument_list; 84, 85; 84, 86; 85, identifier:i; 86, identifier:temp; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:found; 90, False; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:cav; 93, identifier:caveats; 94, block; 94, 95; 95, if_statement; 95, 96; 95, 108; 96, parenthesized_expression; 96, 97; 97, boolean_operator:and; 97, 98; 97, 103; 98, comparison_operator:is; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:cav; 101, identifier:verification_key_id; 102, None; 103, comparison_operator:==; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:cav; 106, identifier:caveat_id; 107, identifier:temp; 108, block; 108, 109; 108, 113; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:found; 112, True; 113, break_statement; 114, if_statement; 114, 115; 114, 117; 115, not_operator; 115, 116; 116, identifier:found; 117, block; 117, 118; 118, return_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:bytes; 121, argument_list; 121, 122; 122, identifier:temp; 123, expression_statement; 123, 124; 124, augmented_assignment:+=; 124, 125; 124, 126; 125, identifier:i; 126, integer:1
def _new_caveat_id(self, base): '''Return a third party caveat id This does not duplicate any third party caveat ids already inside macaroon. If base is non-empty, it is used as the id prefix. @param base bytes @return bytes ''' id = bytearray() if len(base) > 0: id.extend(base) else: # Add a version byte to the caveat id. Technically # this is unnecessary as the caveat-decoding logic # that looks at versions should never see this id, # but if the caveat payload isn't provided with the # payload, having this version gives a strong indication # that the payload has been omitted so we can produce # a better error for the user. id.append(VERSION_3) # Iterate through integers looking for one that isn't already used, # starting from n so that if everyone is using this same algorithm, # we'll only perform one iteration. i = len(self._caveat_data) caveats = self._macaroon.caveats while True: # We append a varint to the end of the id and assume that # any client that's created the id that we're using as a base # is using similar conventions - in the worst case they might # end up with a duplicate third party caveat id and thus create # a macaroon that cannot be discharged. temp = id[:] encode_uvarint(i, temp) found = False for cav in caveats: if (cav.verification_key_id is not None and cav.caveat_id == temp): found = True break if not found: return bytes(temp) i += 1
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:extract_macaroons; 3, parameters; 3, 4; 4, identifier:headers_or_request; 5, block; 5, 6; 5, 8; 5, 37; 5, 41; 5, 95; 5, 102; 5, 147; 5, 148; 5, 149; 5, 150; 5, 157; 5, 176; 6, expression_statement; 6, 7; 7, string:''' Returns an array of any macaroons found in the given slice of cookies. If the argument implements a get_header method, that will be used instead of the get method to retrieve headers. @param headers_or_request: dict of headers or a urllib.request.Request-like object. @return: A list of list of mpy macaroons '''; 8, function_definition; 8, 9; 8, 10; 8, 15; 9, function_name:get_header; 10, parameters; 10, 11; 10, 12; 11, identifier:key; 12, default_parameter; 12, 13; 12, 14; 13, identifier:default; 14, None; 15, block; 15, 16; 16, try_statement; 16, 17; 16, 26; 17, block; 17, 18; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:headers_or_request; 22, identifier:get_header; 23, argument_list; 23, 24; 23, 25; 24, identifier:key; 25, identifier:default; 26, except_clause; 26, 27; 26, 28; 27, identifier:AttributeError; 28, block; 28, 29; 29, return_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:headers_or_request; 33, identifier:get; 34, argument_list; 34, 35; 34, 36; 35, identifier:key; 36, identifier:default; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:mss; 40, list:[]; 41, function_definition; 41, 42; 41, 43; 41, 45; 42, function_name:add_macaroon; 43, parameters; 43, 44; 44, identifier:data; 45, block; 45, 46; 45, 75; 45, 88; 46, try_statement; 46, 47; 46, 71; 47, block; 47, 48; 47, 57; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:data; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:utils; 54, identifier:b64decode; 55, argument_list; 55, 56; 56, identifier:data; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:data_as_objs; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:json; 63, identifier:loads; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:data; 68, identifier:decode; 69, argument_list; 69, 70; 70, string:'utf-8'; 71, except_clause; 71, 72; 71, 73; 72, identifier:ValueError; 73, block; 73, 74; 74, return_statement; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:ms; 78, list_comprehension; 78, 79; 78, 85; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:utils; 82, identifier:macaroon_from_dict; 83, argument_list; 83, 84; 84, identifier:x; 85, for_in_clause; 85, 86; 85, 87; 86, identifier:x; 87, identifier:data_as_objs; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:mss; 92, identifier:append; 93, argument_list; 93, 94; 94, identifier:ms; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:cookie_header; 98, call; 98, 99; 98, 100; 99, identifier:get_header; 100, argument_list; 100, 101; 101, string:'Cookie'; 102, if_statement; 102, 103; 102, 106; 103, comparison_operator:is; 103, 104; 103, 105; 104, identifier:cookie_header; 105, None; 106, block; 106, 107; 106, 113; 106, 114; 106, 115; 106, 116; 106, 126; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:cs; 110, call; 110, 111; 110, 112; 111, identifier:SimpleCookie; 112, argument_list; 113, comment; 114, comment; 115, comment; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:cs; 120, identifier:load; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:str; 124, argument_list; 124, 125; 125, identifier:cookie_header; 126, for_statement; 126, 127; 126, 128; 126, 129; 127, identifier:c; 128, identifier:cs; 129, block; 129, 130; 130, if_statement; 130, 131; 130, 137; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:c; 134, identifier:startswith; 135, argument_list; 135, 136; 136, string:'macaroon-'; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 141; 140, identifier:add_macaroon; 141, argument_list; 141, 142; 142, attribute; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:cs; 145, identifier:c; 146, identifier:value; 147, comment; 148, comment; 149, comment; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:macaroon_header; 153, call; 153, 154; 153, 155; 154, identifier:get_header; 155, argument_list; 155, 156; 156, string:'Macaroons'; 157, if_statement; 157, 158; 157, 161; 158, comparison_operator:is; 158, 159; 158, 160; 159, identifier:macaroon_header; 160, None; 161, block; 161, 162; 162, for_statement; 162, 163; 162, 164; 162, 170; 163, identifier:h; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:macaroon_header; 167, identifier:split; 168, argument_list; 168, 169; 169, string:','; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 174; 173, identifier:add_macaroon; 174, argument_list; 174, 175; 175, identifier:h; 176, return_statement; 176, 177; 177, identifier:mss
def extract_macaroons(headers_or_request): ''' Returns an array of any macaroons found in the given slice of cookies. If the argument implements a get_header method, that will be used instead of the get method to retrieve headers. @param headers_or_request: dict of headers or a urllib.request.Request-like object. @return: A list of list of mpy macaroons ''' def get_header(key, default=None): try: return headers_or_request.get_header(key, default) except AttributeError: return headers_or_request.get(key, default) mss = [] def add_macaroon(data): try: data = utils.b64decode(data) data_as_objs = json.loads(data.decode('utf-8')) except ValueError: return ms = [utils.macaroon_from_dict(x) for x in data_as_objs] mss.append(ms) cookie_header = get_header('Cookie') if cookie_header is not None: cs = SimpleCookie() # The cookie might be a unicode object, so convert it # to ASCII. This may cause an exception under Python 2. # TODO is that a problem? cs.load(str(cookie_header)) for c in cs: if c.startswith('macaroon-'): add_macaroon(cs[c].value) # Python doesn't make it easy to have multiple values for a # key, so split the header instead, which is necessary # for HTTP1.1 compatibility anyway (see RFC 7230, section 3.2.2) macaroon_header = get_header('Macaroons') if macaroon_header is not None: for h in macaroon_header.split(','): add_macaroon(h) return mss
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:http_error_handler; 3, parameters; 3, 4; 4, identifier:f; 5, block; 5, 6; 5, 8; 5, 52; 5, 89; 5, 308; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 12; 9, function_name:hrefs_to_resources; 10, parameters; 10, 11; 11, identifier:hrefs; 12, block; 12, 13; 13, for_statement; 13, 14; 13, 15; 13, 26; 14, identifier:href; 15, call; 15, 16; 15, 25; 16, attribute; 16, 17; 16, 24; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:hrefs; 20, identifier:replace; 21, argument_list; 21, 22; 21, 23; 22, string:','; 23, string:''; 24, identifier:split; 25, argument_list; 26, block; 26, 27; 26, 43; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, pattern_list; 29, 30; 29, 31; 30, identifier:type; 31, identifier:uuid; 32, subscript; 32, 33; 32, 39; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:href; 36, identifier:split; 37, argument_list; 37, 38; 38, string:'/'; 39, slice; 39, 40; 39, 42; 40, unary_operator:-; 40, 41; 41, integer:2; 42, colon; 43, expression_statement; 43, 44; 44, yield; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:Resource; 47, argument_list; 47, 48; 47, 49; 48, identifier:type; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:uuid; 51, identifier:uuid; 52, function_definition; 52, 53; 52, 54; 52, 56; 53, function_name:hrefs_list_to_resources; 54, parameters; 54, 55; 55, identifier:hrefs_list; 56, block; 56, 57; 57, for_statement; 57, 58; 57, 59; 57, 63; 58, identifier:href; 59, call; 59, 60; 59, 61; 60, identifier:eval; 61, argument_list; 61, 62; 62, identifier:hrefs_list; 63, block; 63, 64; 63, 80; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, pattern_list; 66, 67; 66, 68; 67, identifier:type; 68, identifier:uuid; 69, subscript; 69, 70; 69, 76; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:href; 73, identifier:split; 74, argument_list; 74, 75; 75, string:'/'; 76, slice; 76, 77; 76, 79; 77, unary_operator:-; 77, 78; 78, integer:2; 79, colon; 80, expression_statement; 80, 81; 81, yield; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:Resource; 84, argument_list; 84, 85; 84, 86; 85, identifier:type; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:uuid; 88, identifier:uuid; 89, decorated_definition; 89, 90; 89, 95; 90, decorator; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:wraps; 93, argument_list; 93, 94; 94, identifier:f; 95, function_definition; 95, 96; 95, 97; 95, 103; 96, function_name:wrapper; 97, parameters; 97, 98; 97, 99; 97, 101; 98, identifier:self; 99, list_splat_pattern; 99, 100; 100, identifier:args; 101, dictionary_splat_pattern; 101, 102; 102, identifier:kwargs; 103, block; 103, 104; 104, try_statement; 104, 105; 104, 115; 105, block; 105, 106; 106, return_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:f; 109, argument_list; 109, 110; 109, 111; 109, 113; 110, identifier:self; 111, list_splat; 111, 112; 112, identifier:args; 113, dictionary_splat; 113, 114; 114, identifier:kwargs; 115, except_clause; 115, 116; 115, 120; 116, as_pattern; 116, 117; 116, 118; 117, identifier:HttpError; 118, as_pattern_target; 118, 119; 119, identifier:e; 120, block; 120, 121; 120, 307; 121, if_statement; 121, 122; 121, 127; 121, 128; 121, 129; 121, 166; 122, comparison_operator:==; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:e; 125, identifier:http_status; 126, integer:404; 127, comment; 128, comment; 129, block; 129, 130; 129, 138; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:self; 134, identifier:emit; 135, argument_list; 135, 136; 135, 137; 136, string:'deleted'; 137, identifier:self; 138, if_statement; 138, 139; 138, 144; 138, 152; 139, call; 139, 140; 139, 141; 140, identifier:isinstance; 141, argument_list; 141, 142; 141, 143; 142, identifier:self; 143, identifier:Resource; 144, block; 144, 145; 145, raise_statement; 145, 146; 146, call; 146, 147; 146, 148; 147, identifier:ResourceNotFound; 148, argument_list; 148, 149; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:resource; 151, identifier:self; 152, elif_clause; 152, 153; 152, 158; 153, call; 153, 154; 153, 155; 154, identifier:isinstance; 155, argument_list; 155, 156; 155, 157; 156, identifier:self; 157, identifier:Collection; 158, block; 158, 159; 159, raise_statement; 159, 160; 160, call; 160, 161; 160, 162; 161, identifier:CollectionNotFound; 162, argument_list; 162, 163; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:collection; 165, identifier:self; 166, elif_clause; 166, 167; 166, 172; 166, 173; 167, comparison_operator:==; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:e; 170, identifier:http_status; 171, integer:409; 172, comment; 173, block; 173, 174; 173, 186; 173, 207; 173, 219; 173, 240; 173, 241; 173, 253; 173, 274; 173, 286; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:matches; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:re; 180, identifier:match; 181, argument_list; 181, 182; 181, 183; 182, string:r'^Delete when children still present: (\[[^]]*\])'; 183, attribute; 183, 184; 183, 185; 184, identifier:e; 185, identifier:message; 186, if_statement; 186, 187; 186, 188; 187, identifier:matches; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 192; 191, identifier:ChildrenExists; 192, argument_list; 192, 193; 193, keyword_argument; 193, 194; 193, 195; 194, identifier:resources; 195, call; 195, 196; 195, 197; 196, identifier:list; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 200; 199, identifier:hrefs_list_to_resources; 200, argument_list; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:matches; 204, identifier:group; 205, argument_list; 205, 206; 206, integer:1; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:matches; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:re; 213, identifier:match; 214, argument_list; 214, 215; 214, 216; 215, string:r'^Delete when resource still referred: (\[[^]]*\])'; 216, attribute; 216, 217; 216, 218; 217, identifier:e; 218, identifier:message; 219, if_statement; 219, 220; 219, 221; 220, identifier:matches; 221, block; 221, 222; 222, raise_statement; 222, 223; 223, call; 223, 224; 223, 225; 224, identifier:BackRefsExists; 225, argument_list; 225, 226; 226, keyword_argument; 226, 227; 226, 228; 227, identifier:resources; 228, call; 228, 229; 228, 230; 229, identifier:list; 230, argument_list; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:hrefs_list_to_resources; 233, argument_list; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:matches; 237, identifier:group; 238, argument_list; 238, 239; 239, integer:1; 240, comment; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:matches; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:re; 247, identifier:match; 248, argument_list; 248, 249; 248, 250; 249, string:r'^Children (.*) still exist$'; 250, attribute; 250, 251; 250, 252; 251, identifier:e; 252, identifier:message; 253, if_statement; 253, 254; 253, 255; 254, identifier:matches; 255, block; 255, 256; 256, raise_statement; 256, 257; 257, call; 257, 258; 257, 259; 258, identifier:ChildrenExists; 259, argument_list; 259, 260; 260, keyword_argument; 260, 261; 260, 262; 261, identifier:resources; 262, call; 262, 263; 262, 264; 263, identifier:list; 264, argument_list; 264, 265; 265, call; 265, 266; 265, 267; 266, identifier:hrefs_to_resources; 267, argument_list; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:matches; 271, identifier:group; 272, argument_list; 272, 273; 273, integer:1; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:matches; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:re; 280, identifier:match; 281, argument_list; 281, 282; 281, 283; 282, string:r'^Back-References from (.*) still exist$'; 283, attribute; 283, 284; 283, 285; 284, identifier:e; 285, identifier:message; 286, if_statement; 286, 287; 286, 288; 287, identifier:matches; 288, block; 288, 289; 289, raise_statement; 289, 290; 290, call; 290, 291; 290, 292; 291, identifier:BackRefsExists; 292, argument_list; 292, 293; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:resources; 295, call; 295, 296; 295, 297; 296, identifier:list; 297, argument_list; 297, 298; 298, call; 298, 299; 298, 300; 299, identifier:hrefs_to_resources; 300, argument_list; 300, 301; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:matches; 304, identifier:group; 305, argument_list; 305, 306; 306, integer:1; 307, raise_statement; 308, return_statement; 308, 309; 309, identifier:wrapper
def http_error_handler(f): """Handle 404 errors returned by the API server """ def hrefs_to_resources(hrefs): for href in hrefs.replace(',', '').split(): type, uuid = href.split('/')[-2:] yield Resource(type, uuid=uuid) def hrefs_list_to_resources(hrefs_list): for href in eval(hrefs_list): type, uuid = href.split('/')[-2:] yield Resource(type, uuid=uuid) @wraps(f) def wrapper(self, *args, **kwargs): try: return f(self, *args, **kwargs) except HttpError as e: if e.http_status == 404: # remove previously created resource # from the cache self.emit('deleted', self) if isinstance(self, Resource): raise ResourceNotFound(resource=self) elif isinstance(self, Collection): raise CollectionNotFound(collection=self) elif e.http_status == 409: # contrail 3.2 matches = re.match(r'^Delete when children still present: (\[[^]]*\])', e.message) if matches: raise ChildrenExists( resources=list(hrefs_list_to_resources(matches.group(1)))) matches = re.match(r'^Delete when resource still referred: (\[[^]]*\])', e.message) if matches: raise BackRefsExists( resources=list(hrefs_list_to_resources(matches.group(1)))) # contrail 2.21 matches = re.match(r'^Children (.*) still exist$', e.message) if matches: raise ChildrenExists( resources=list(hrefs_to_resources(matches.group(1)))) matches = re.match(r'^Back-References from (.*) still exist$', e.message) if matches: raise BackRefsExists( resources=list(hrefs_to_resources(matches.group(1)))) raise return wrapper
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_handle_windows; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:event; 6, block; 6, 7; 6, 9; 6, 22; 6, 72; 6, 83; 6, 87; 6, 151; 6, 173; 6, 188; 6, 328; 6, 469; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:vkey; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_keyname; 16, argument_list; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:event; 20, identifier:GetKey; 21, argument_list; 22, if_statement; 22, 23; 22, 34; 23, comparison_operator:in; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:event; 26, identifier:Message; 27, binary_operator:+; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:KEYS_UP; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:KEYS_DOWN; 34, block; 34, 35; 35, if_statement; 35, 36; 35, 41; 36, comparison_operator:in; 36, 37; 36, 38; 37, identifier:vkey; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:MODIFIERNAMES; 41, block; 41, 42; 41, 56; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 49; 44, subscript; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:_realmodifiers; 48, identifier:vkey; 49, comparison_operator:in; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:event; 52, identifier:Message; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:KEYS_DOWN; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 67; 58, subscript; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_modifiers; 62, subscript; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:MODIFIERNAMES; 66, identifier:vkey; 67, subscript; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_realmodifiers; 71, identifier:vkey; 72, if_statement; 72, 73; 72, 80; 73, comparison_operator:not; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:event; 76, identifier:Message; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:KEYS_DOWN; 80, block; 80, 81; 81, return_statement; 81, 82; 82, True; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:is_altgr; 86, False; 87, if_statement; 87, 88; 87, 99; 87, 108; 87, 125; 88, comparison_operator:in; 88, 89; 88, 96; 89, tuple; 89, 90; 89, 91; 90, identifier:vkey; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:event; 94, identifier:IsExtended; 95, argument_list; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:NUMPAD_SPECIALS; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:key; 103, assignment; 103, 104; 103, 105; 104, identifier:vkey; 105, binary_operator:+; 105, 106; 105, 107; 106, string:"Numpad-"; 107, identifier:vkey; 108, elif_clause; 108, 109; 108, 120; 109, boolean_operator:or; 109, 110; 109, 114; 110, not_operator; 110, 111; 111, attribute; 111, 112; 111, 113; 112, identifier:event; 113, identifier:Ascii; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:vkey; 117, identifier:startswith; 118, argument_list; 118, 119; 119, string:"Numpad"; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:key; 124, identifier:vkey; 125, else_clause; 125, 126; 126, block; 126, 127; 126, 137; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:is_altgr; 130, comparison_operator:in; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:event; 133, identifier:Ascii; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:ALT_GRS; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:key; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:_keyname; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 147; 146, identifier:unichr; 147, argument_list; 147, 148; 148, attribute; 148, 149; 148, 150; 149, identifier:event; 150, identifier:Ascii; 151, if_statement; 151, 152; 151, 153; 152, identifier:DEBUG; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:print; 157, argument_list; 157, 158; 158, binary_operator:%; 158, 159; 158, 160; 159, string:"Adding key %s (real %s)"; 160, tuple; 160, 161; 160, 167; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:key; 164, identifier:encode; 165, argument_list; 165, 166; 166, string:"utf-8"; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:vkey; 170, identifier:encode; 171, argument_list; 171, 172; 172, string:"utf-8"; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:self; 177, identifier:_output; 178, argument_list; 178, 179; 178, 182; 178, 185; 179, keyword_argument; 179, 180; 179, 181; 180, identifier:type; 181, string:"keys"; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:key; 184, identifier:key; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:realkey; 187, identifier:vkey; 188, if_statement; 188, 189; 188, 197; 189, boolean_operator:and; 189, 190; 189, 195; 190, comparison_operator:not; 190, 191; 190, 192; 191, identifier:vkey; 192, attribute; 192, 193; 192, 194; 193, identifier:self; 194, identifier:MODIFIERNAMES; 195, not_operator; 195, 196; 196, identifier:is_altgr; 197, block; 197, 198; 197, 220; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:modifier; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, string:"-"; 204, identifier:join; 205, generator_expression; 205, 206; 205, 207; 205, 214; 206, identifier:k; 207, for_in_clause; 207, 208; 207, 209; 208, identifier:k; 209, list:["Ctrl", "Alt", "Shift", "Win"]; 209, 210; 209, 211; 209, 212; 209, 213; 210, string:"Ctrl"; 211, string:"Alt"; 212, string:"Shift"; 213, string:"Win"; 214, if_clause; 214, 215; 215, subscript; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:_modifiers; 219, identifier:k; 220, if_statement; 220, 221; 220, 226; 220, 227; 221, boolean_operator:and; 221, 222; 221, 223; 222, identifier:modifier; 223, comparison_operator:!=; 223, 224; 223, 225; 224, identifier:modifier; 225, string:"Shift"; 226, comment; 227, block; 227, 228; 227, 253; 227, 275; 227, 283; 227, 291; 227, 313; 228, if_statement; 228, 229; 228, 238; 229, boolean_operator:and; 229, 230; 229, 235; 230, subscript; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:_modifiers; 234, string:"Ctrl"; 235, attribute; 235, 236; 235, 237; 236, identifier:event; 237, identifier:Ascii; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:key; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:_keyname; 246, argument_list; 246, 247; 247, call; 247, 248; 247, 249; 248, identifier:unichr; 249, argument_list; 249, 250; 250, attribute; 250, 251; 250, 252; 251, identifier:event; 252, identifier:KeyID; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:realmodifier; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, string:"-"; 259, identifier:join; 260, generator_expression; 260, 261; 260, 262; 260, 273; 261, identifier:k; 262, for_in_clause; 262, 263; 262, 266; 263, pattern_list; 263, 264; 263, 265; 264, identifier:k; 265, identifier:v; 266, call; 266, 267; 266, 272; 267, attribute; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:_realmodifiers; 271, identifier:items; 272, argument_list; 273, if_clause; 273, 274; 274, identifier:v; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:realkey; 278, binary_operator:%; 278, 279; 278, 280; 279, string:"%s-%s"; 280, tuple; 280, 281; 280, 282; 281, identifier:realmodifier; 282, identifier:key; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:key; 286, binary_operator:%; 286, 287; 286, 288; 287, string:"%s-%s"; 288, tuple; 288, 289; 288, 290; 289, identifier:modifier; 290, identifier:key; 291, if_statement; 291, 292; 291, 293; 292, identifier:DEBUG; 293, block; 293, 294; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 297; 296, identifier:print; 297, argument_list; 297, 298; 298, binary_operator:%; 298, 299; 298, 300; 299, string:"Adding combo %s (real %s)"; 300, tuple; 300, 301; 300, 307; 301, call; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:key; 304, identifier:encode; 305, argument_list; 305, 306; 306, string:"utf-8"; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:realkey; 310, identifier:encode; 311, argument_list; 311, 312; 312, string:"utf-8"; 313, expression_statement; 313, 314; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:self; 317, identifier:_output; 318, argument_list; 318, 319; 318, 322; 318, 325; 319, keyword_argument; 319, 320; 319, 321; 320, identifier:type; 321, string:"combos"; 322, keyword_argument; 322, 323; 322, 324; 323, identifier:key; 324, identifier:key; 325, keyword_argument; 325, 326; 325, 327; 326, identifier:realkey; 327, identifier:realkey; 328, if_statement; 328, 329; 328, 330; 329, identifier:DEBUG; 330, block; 330, 331; 330, 338; 330, 352; 330, 353; 330, 367; 330, 368; 330, 382; 330, 383; 330, 397; 330, 398; 330, 412; 330, 413; 330, 425; 330, 426; 330, 438; 330, 439; 330, 451; 330, 452; 330, 464; 330, 465; 331, expression_statement; 331, 332; 332, call; 332, 333; 332, 334; 333, identifier:print; 334, argument_list; 334, 335; 335, binary_operator:%; 335, 336; 335, 337; 336, string:"CHARACTER: %r"; 337, identifier:key; 338, expression_statement; 338, 339; 339, call; 339, 340; 339, 341; 340, identifier:print; 341, argument_list; 341, 342; 342, call; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, string:'GetKey: {0}'; 345, identifier:format; 346, argument_list; 346, 347; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:event; 350, identifier:GetKey; 351, argument_list; 352, comment; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 356; 355, identifier:print; 356, argument_list; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, string:'IsAlt: {0}'; 360, identifier:format; 361, argument_list; 361, 362; 362, call; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:event; 365, identifier:IsAlt; 366, argument_list; 367, comment; 368, expression_statement; 368, 369; 369, call; 369, 370; 369, 371; 370, identifier:print; 371, argument_list; 371, 372; 372, call; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, string:'IsExtended: {0}'; 375, identifier:format; 376, argument_list; 376, 377; 377, call; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, identifier:event; 380, identifier:IsExtended; 381, argument_list; 382, comment; 383, expression_statement; 383, 384; 384, call; 384, 385; 384, 386; 385, identifier:print; 386, argument_list; 386, 387; 387, call; 387, 388; 387, 391; 388, attribute; 388, 389; 388, 390; 389, string:'IsInjected: {0}'; 390, identifier:format; 391, argument_list; 391, 392; 392, call; 392, 393; 392, 396; 393, attribute; 393, 394; 393, 395; 394, identifier:event; 395, identifier:IsInjected; 396, argument_list; 397, comment; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 401; 400, identifier:print; 401, argument_list; 401, 402; 402, call; 402, 403; 402, 406; 403, attribute; 403, 404; 403, 405; 404, string:'IsTransition: {0}'; 405, identifier:format; 406, argument_list; 406, 407; 407, call; 407, 408; 407, 411; 408, attribute; 408, 409; 408, 410; 409, identifier:event; 410, identifier:IsTransition; 411, argument_list; 412, comment; 413, expression_statement; 413, 414; 414, call; 414, 415; 414, 416; 415, identifier:print; 416, argument_list; 416, 417; 417, call; 417, 418; 417, 421; 418, attribute; 418, 419; 418, 420; 419, string:'ASCII: {0}'; 420, identifier:format; 421, argument_list; 421, 422; 422, attribute; 422, 423; 422, 424; 423, identifier:event; 424, identifier:Ascii; 425, comment; 426, expression_statement; 426, 427; 427, call; 427, 428; 427, 429; 428, identifier:print; 429, argument_list; 429, 430; 430, call; 430, 431; 430, 434; 431, attribute; 431, 432; 431, 433; 432, string:'KeyID: {0}'; 433, identifier:format; 434, argument_list; 434, 435; 435, attribute; 435, 436; 435, 437; 436, identifier:event; 437, identifier:KeyID; 438, comment; 439, expression_statement; 439, 440; 440, call; 440, 441; 440, 442; 441, identifier:print; 442, argument_list; 442, 443; 443, call; 443, 444; 443, 447; 444, attribute; 444, 445; 444, 446; 445, string:'ScanCode: {0}'; 446, identifier:format; 447, argument_list; 447, 448; 448, attribute; 448, 449; 448, 450; 449, identifier:event; 450, identifier:ScanCode; 451, comment; 452, expression_statement; 452, 453; 453, call; 453, 454; 453, 455; 454, identifier:print; 455, argument_list; 455, 456; 456, call; 456, 457; 456, 460; 457, attribute; 457, 458; 457, 459; 458, string:'Message: {0}'; 459, identifier:format; 460, argument_list; 460, 461; 461, attribute; 461, 462; 461, 463; 462, identifier:event; 463, identifier:Message; 464, comment; 465, expression_statement; 465, 466; 466, call; 466, 467; 466, 468; 467, identifier:print; 468, argument_list; 469, return_statement; 469, 470; 470, True
def _handle_windows(self, event): """Windows key event handler.""" vkey = self._keyname(event.GetKey()) if event.Message in self.KEYS_UP + self.KEYS_DOWN: if vkey in self.MODIFIERNAMES: self._realmodifiers[vkey] = event.Message in self.KEYS_DOWN self._modifiers[self.MODIFIERNAMES[vkey]] = self._realmodifiers[vkey] if event.Message not in self.KEYS_DOWN: return True is_altgr = False if (vkey, event.IsExtended()) in self.NUMPAD_SPECIALS: key = vkey = "Numpad-" + vkey elif not event.Ascii or vkey.startswith("Numpad"): key = vkey else: is_altgr = event.Ascii in self.ALT_GRS key = self._keyname(unichr(event.Ascii)) if DEBUG: print("Adding key %s (real %s)" % (key.encode("utf-8"), vkey.encode("utf-8"))) self._output(type="keys", key=key, realkey=vkey) if vkey not in self.MODIFIERNAMES and not is_altgr: modifier = "-".join(k for k in ["Ctrl", "Alt", "Shift", "Win"] if self._modifiers[k]) if modifier and modifier != "Shift": # Shift-X is not a combo if self._modifiers["Ctrl"] and event.Ascii: key = self._keyname(unichr(event.KeyID)) realmodifier = "-".join(k for k, v in self._realmodifiers.items() if v) realkey = "%s-%s" % (realmodifier, key) key = "%s-%s" % (modifier, key) if DEBUG: print("Adding combo %s (real %s)" % (key.encode("utf-8"), realkey.encode("utf-8"))) self._output(type="combos", key=key, realkey=realkey) if DEBUG: print("CHARACTER: %r" % key) print('GetKey: {0}'.format(event.GetKey())) # Name of the virtual keycode, str print('IsAlt: {0}'.format(event.IsAlt())) # Was the alt key depressed?, bool print('IsExtended: {0}'.format(event.IsExtended())) # Is this an extended key?, bool print('IsInjected: {0}'.format(event.IsInjected())) # Was this event generated programmatically?, bool print('IsTransition: {0}'.format(event.IsTransition())) #Is this a transition from up to down or vice versa?, bool print('ASCII: {0}'.format(event.Ascii)) # ASCII value, if one exists, str print('KeyID: {0}'.format(event.KeyID)) # Virtual key code, int print('ScanCode: {0}'.format(event.ScanCode)) # Scan code, int print('Message: {0}'.format(event.Message)) # Name of the virtual keycode, str print() return True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_handle_linux; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:keycode; 6, identifier:character; 7, identifier:press; 8, block; 8, 9; 8, 11; 8, 17; 8, 27; 8, 54; 8, 72; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:character; 14, None; 15, block; 15, 16; 16, return_statement; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:key; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_keyname; 24, argument_list; 24, 25; 24, 26; 25, identifier:character; 26, identifier:keycode; 27, if_statement; 27, 28; 27, 33; 28, comparison_operator:in; 28, 29; 28, 30; 29, identifier:key; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:MODIFIERNAMES; 33, block; 33, 34; 33, 46; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 45; 36, subscript; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_modifiers; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:MODIFIERNAMES; 44, identifier:key; 45, identifier:press; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:_realmodifiers; 52, identifier:key; 53, identifier:press; 54, if_statement; 54, 55; 54, 56; 55, identifier:press; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_output; 62, argument_list; 62, 63; 62, 66; 62, 69; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:type; 65, string:"keys"; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:key; 68, identifier:key; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:realkey; 71, identifier:key; 72, if_statement; 72, 73; 72, 80; 73, boolean_operator:and; 73, 74; 73, 75; 74, identifier:press; 75, comparison_operator:not; 75, 76; 75, 77; 76, identifier:key; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:MODIFIERNAMES; 80, block; 80, 81; 80, 103; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:modifier; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, string:"-"; 87, identifier:join; 88, generator_expression; 88, 89; 88, 90; 88, 97; 89, identifier:k; 90, for_in_clause; 90, 91; 90, 92; 91, identifier:k; 92, list:["Ctrl", "Alt", "Shift", "Win"]; 92, 93; 92, 94; 92, 95; 92, 96; 93, string:"Ctrl"; 94, string:"Alt"; 95, string:"Shift"; 96, string:"Win"; 97, if_clause; 97, 98; 98, subscript; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:_modifiers; 102, identifier:k; 103, if_statement; 103, 104; 103, 109; 103, 110; 104, boolean_operator:and; 104, 105; 104, 106; 105, identifier:modifier; 106, comparison_operator:!=; 106, 107; 106, 108; 107, identifier:modifier; 108, string:"Shift"; 109, comment; 110, block; 110, 111; 110, 133; 110, 141; 110, 149; 110, 171; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:realmodifier; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, string:"-"; 117, identifier:join; 118, generator_expression; 118, 119; 118, 120; 118, 131; 119, identifier:k; 120, for_in_clause; 120, 121; 120, 124; 121, pattern_list; 121, 122; 121, 123; 122, identifier:k; 123, identifier:v; 124, call; 124, 125; 124, 130; 125, attribute; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:_realmodifiers; 129, identifier:items; 130, argument_list; 131, if_clause; 131, 132; 132, identifier:v; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:realkey; 136, binary_operator:%; 136, 137; 136, 138; 137, string:"%s-%s"; 138, tuple; 138, 139; 138, 140; 139, identifier:realmodifier; 140, identifier:key; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:key; 144, binary_operator:%; 144, 145; 144, 146; 145, string:"%s-%s"; 146, tuple; 146, 147; 146, 148; 147, identifier:modifier; 148, identifier:key; 149, if_statement; 149, 150; 149, 151; 150, identifier:DEBUG; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:print; 155, argument_list; 155, 156; 156, binary_operator:%; 156, 157; 156, 158; 157, string:"Adding combo %s (real %s)"; 158, tuple; 158, 159; 158, 165; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:key; 162, identifier:encode; 163, argument_list; 163, 164; 164, string:"utf-8"; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:realkey; 168, identifier:encode; 169, argument_list; 169, 170; 170, string:"utf-8"; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:_output; 176, argument_list; 176, 177; 176, 180; 176, 183; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:type; 179, string:"combos"; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:key; 182, identifier:key; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:realkey; 185, identifier:realkey
def _handle_linux(self, keycode, character, press): """Linux key event handler.""" if character is None: return key = self._keyname(character, keycode) if key in self.MODIFIERNAMES: self._modifiers[self.MODIFIERNAMES[key]] = press self._realmodifiers[key] = press if press: self._output(type="keys", key=key, realkey=key) if press and key not in self.MODIFIERNAMES: modifier = "-".join(k for k in ["Ctrl", "Alt", "Shift", "Win"] if self._modifiers[k]) if modifier and modifier != "Shift": # Shift-X is not a combo realmodifier = "-".join(k for k, v in self._realmodifiers.items() if v) realkey = "%s-%s" % (realmodifier, key) key = "%s-%s" % (modifier, key) if DEBUG: print("Adding combo %s (real %s)" % (key.encode("utf-8"), realkey.encode("utf-8"))) self._output(type="combos", key=key, realkey=realkey)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_store_documentation; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:path; 6, identifier:html; 7, identifier:overwrite; 8, identifier:quiet; 9, block; 9, 10; 9, 12; 9, 17; 9, 28; 9, 46; 9, 68; 9, 93; 9, 118; 9, 151; 9, 155; 9, 215; 9, 220; 9, 233; 9, 240; 9, 244; 9, 265; 9, 277; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:echo; 15, argument_list; 15, 16; 16, string:"Storing groundwork application documents\n"; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:echo; 20, argument_list; 20, 21; 21, binary_operator:%; 21, 22; 21, 23; 22, string:"Application: %s"; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:app; 27, identifier:name; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:echo; 31, argument_list; 31, 32; 32, binary_operator:%; 32, 33; 32, 34; 33, string:"Number of documents: %s\n"; 34, call; 34, 35; 34, 36; 35, identifier:len; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 45; 38, attribute; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:app; 43, identifier:documents; 44, identifier:get; 45, argument_list; 46, if_statement; 46, 47; 46, 56; 47, not_operator; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:os; 52, identifier:path; 53, identifier:isabs; 54, argument_list; 54, 55; 55, identifier:path; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:path; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:os; 64, identifier:path; 65, identifier:abspath; 66, argument_list; 66, 67; 67, identifier:path; 68, if_statement; 68, 69; 68, 78; 69, not_operator; 69, 70; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:os; 74, identifier:path; 75, identifier:isdir; 76, argument_list; 76, 77; 77, identifier:path; 78, block; 78, 79; 78, 86; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:echo; 82, argument_list; 82, 83; 83, binary_operator:%; 83, 84; 83, 85; 84, string:"Path %s is not a directory!"; 85, identifier:path; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:sys; 90, identifier:exit; 91, argument_list; 91, 92; 92, integer:1; 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:path; 103, block; 103, 104; 103, 111; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 107; 106, identifier:echo; 107, argument_list; 107, 108; 108, binary_operator:%; 108, 109; 108, 110; 109, string:"Path %s does not exist"; 110, identifier:path; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:sys; 115, identifier:exit; 116, argument_list; 116, 117; 117, integer:1; 118, for_statement; 118, 119; 118, 123; 118, 129; 119, pattern_list; 119, 120; 119, 121; 119, 122; 120, identifier:dirpath; 121, identifier:dirnames; 122, identifier:files; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:os; 126, identifier:walk; 127, argument_list; 127, 128; 128, identifier:path; 129, block; 129, 130; 130, if_statement; 130, 131; 130, 132; 131, identifier:files; 132, block; 132, 133; 132, 140; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:echo; 136, argument_list; 136, 137; 137, binary_operator:%; 137, 138; 137, 139; 138, string:"Path %s is not empty!\n"; 139, identifier:path; 140, if_statement; 140, 141; 140, 143; 141, not_operator; 141, 142; 142, identifier:overwrite; 143, block; 143, 144; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:sys; 148, identifier:exit; 149, argument_list; 149, 150; 150, integer:1; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:documents; 154, list:[]; 155, for_statement; 155, 156; 155, 159; 155, 172; 156, pattern_list; 156, 157; 156, 158; 157, identifier:key; 158, identifier:document; 159, call; 159, 160; 159, 171; 160, attribute; 160, 161; 160, 170; 161, call; 161, 162; 161, 169; 162, attribute; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:app; 167, identifier:documents; 168, identifier:get; 169, argument_list; 170, identifier:items; 171, argument_list; 172, block; 172, 173; 172, 180; 172, 181; 172, 193; 172, 202; 172, 206; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:file_extension; 176, conditional_expression:if; 176, 177; 176, 178; 176, 179; 177, string:".html"; 178, identifier:html; 179, string:".rst"; 180, comment; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:file_name_parts; 184, call; 184, 185; 184, 192; 185, attribute; 185, 186; 185, 191; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:key; 189, identifier:lower; 190, argument_list; 191, identifier:split; 192, argument_list; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:file_name; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, string:""; 199, identifier:join; 200, argument_list; 200, 201; 201, identifier:file_name_parts; 202, expression_statement; 202, 203; 203, augmented_assignment:+=; 203, 204; 203, 205; 204, identifier:file_name; 205, identifier:file_extension; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:documents; 210, identifier:append; 211, argument_list; 211, 212; 212, tuple; 212, 213; 212, 214; 213, identifier:file_name; 214, identifier:document; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:echo; 218, argument_list; 218, 219; 219, string:"Going to write to following files:"; 220, for_statement; 220, 221; 220, 222; 220, 223; 221, identifier:document; 222, identifier:documents; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:echo; 227, argument_list; 227, 228; 228, binary_operator:%; 228, 229; 228, 230; 229, string:" %s"; 230, subscript; 230, 231; 230, 232; 231, identifier:document; 232, integer:0; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 236; 235, identifier:echo; 236, argument_list; 236, 237; 237, binary_operator:%; 237, 238; 237, 239; 238, string:"\nTarget directory: %s"; 239, identifier:path; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:answer; 243, None; 244, while_statement; 244, 245; 244, 253; 245, boolean_operator:and; 245, 246; 245, 251; 246, comparison_operator:not; 246, 247; 246, 248; 247, identifier:answer; 248, list:["N", "Y"]; 248, 249; 248, 250; 249, string:"N"; 250, string:"Y"; 251, not_operator; 251, 252; 252, identifier:quiet; 253, block; 253, 254; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:answer; 257, call; 257, 258; 257, 264; 258, attribute; 258, 259; 258, 263; 259, call; 259, 260; 259, 261; 260, identifier:prompt; 261, argument_list; 261, 262; 262, string:"Shall we go on? [Y]es, [N]o: "; 263, identifier:upper; 264, argument_list; 265, if_statement; 265, 266; 265, 269; 266, comparison_operator:==; 266, 267; 266, 268; 267, identifier:answer; 268, string:"N"; 269, block; 269, 270; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:sys; 274, identifier:exit; 275, argument_list; 275, 276; 276, integer:0; 277, for_statement; 277, 278; 277, 279; 277, 280; 278, identifier:document; 279, identifier:documents; 280, block; 280, 281; 281, try_statement; 281, 282; 281, 364; 281, 381; 282, block; 282, 283; 283, with_statement; 283, 284; 283, 304; 284, with_clause; 284, 285; 285, with_item; 285, 286; 286, as_pattern; 286, 287; 286, 302; 287, call; 287, 288; 287, 289; 288, identifier:open; 289, argument_list; 289, 290; 289, 301; 290, call; 290, 291; 290, 296; 291, attribute; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:os; 294, identifier:path; 295, identifier:join; 296, argument_list; 296, 297; 296, 298; 297, identifier:path; 298, subscript; 298, 299; 298, 300; 299, identifier:document; 300, integer:0; 301, string:"w"; 302, as_pattern_target; 302, 303; 303, identifier:doc_file; 304, block; 304, 305; 304, 336; 304, 357; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:doc_rendered; 308, call; 308, 309; 308, 323; 309, attribute; 309, 310; 309, 322; 310, call; 310, 311; 310, 316; 311, attribute; 311, 312; 311, 315; 312, call; 312, 313; 312, 314; 313, identifier:Environment; 314, argument_list; 315, identifier:from_string; 316, argument_list; 316, 317; 317, attribute; 317, 318; 317, 321; 318, subscript; 318, 319; 318, 320; 319, identifier:document; 320, integer:1; 321, identifier:content; 322, identifier:render; 323, argument_list; 323, 324; 323, 329; 324, keyword_argument; 324, 325; 324, 326; 325, identifier:app; 326, attribute; 326, 327; 326, 328; 327, identifier:self; 328, identifier:app; 329, keyword_argument; 329, 330; 329, 331; 330, identifier:plugin; 331, attribute; 331, 332; 331, 335; 332, subscript; 332, 333; 332, 334; 333, identifier:document; 334, integer:1; 335, identifier:plugin; 336, if_statement; 336, 337; 336, 338; 336, 351; 337, identifier:html; 338, block; 338, 339; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:output; 342, subscript; 342, 343; 342, 350; 343, call; 343, 344; 343, 345; 344, identifier:publish_parts; 345, argument_list; 345, 346; 345, 347; 346, identifier:doc_rendered; 347, keyword_argument; 347, 348; 347, 349; 348, identifier:writer_name; 349, string:"html"; 350, string:'whole'; 351, else_clause; 351, 352; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 356; 355, identifier:output; 356, identifier:doc_rendered; 357, expression_statement; 357, 358; 358, call; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:doc_file; 361, identifier:write; 362, argument_list; 362, 363; 363, identifier:output; 364, except_clause; 364, 365; 364, 369; 365, as_pattern; 365, 366; 365, 367; 366, identifier:Exception; 367, as_pattern_target; 367, 368; 368, identifier:e; 369, block; 369, 370; 370, expression_statement; 370, 371; 371, call; 371, 372; 371, 373; 372, identifier:echo; 373, argument_list; 373, 374; 374, binary_operator:%; 374, 375; 374, 376; 375, string:"%s error occurred: %s"; 376, tuple; 376, 377; 376, 380; 377, subscript; 377, 378; 377, 379; 378, identifier:document; 379, integer:0; 380, identifier:e; 381, else_clause; 381, 382; 382, block; 382, 383; 383, expression_statement; 383, 384; 384, call; 384, 385; 384, 386; 385, identifier:echo; 386, argument_list; 386, 387; 387, binary_operator:%; 387, 388; 387, 389; 388, string:"%s stored."; 389, subscript; 389, 390; 389, 391; 390, identifier:document; 391, integer:0
def _store_documentation(self, path, html, overwrite, quiet): """ Stores all documents on the file system. Target location is **path**. File name is the lowercase name of the document + .rst. """ echo("Storing groundwork application documents\n") echo("Application: %s" % self.app.name) echo("Number of documents: %s\n" % len(self.app.documents.get())) if not os.path.isabs(path): path = os.path.abspath(path) if not os.path.isdir(path): echo("Path %s is not a directory!" % path) sys.exit(1) if not os.path.exists(path): echo("Path %s does not exist" % path) sys.exit(1) for dirpath, dirnames, files in os.walk(path): if files: echo("Path %s is not empty!\n" % path) if not overwrite: sys.exit(1) documents = [] for key, document in self.app.documents.get().items(): file_extension = ".html" if html else ".rst" # lowers the name, removes all whitespaces and adds the file extension file_name_parts = key.lower().split() file_name = "".join(file_name_parts) file_name += file_extension documents.append((file_name, document)) echo("Going to write to following files:") for document in documents: echo(" %s" % document[0]) echo("\nTarget directory: %s" % path) answer = None while answer not in ["N", "Y"] and not quiet: answer = prompt("Shall we go on? [Y]es, [N]o: ").upper() if answer == "N": sys.exit(0) for document in documents: try: with open(os.path.join(path, document[0]), "w") as doc_file: doc_rendered = Environment().from_string(document[1].content).render(app=self.app, plugin=document[1].plugin) if html: output = publish_parts(doc_rendered, writer_name="html")['whole'] else: output = doc_rendered doc_file.write(output) except Exception as e: echo("%s error occurred: %s" % (document[0], e)) else: echo("%s stored." % document[0])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_show_documentation; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 44; 5, 59; 5, 72; 5, 89; 5, 93; 5, 97; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:documents; 11, list:[]; 12, for_statement; 12, 13; 12, 16; 12, 29; 13, pattern_list; 13, 14; 13, 15; 14, identifier:key; 15, identifier:document; 16, call; 16, 17; 16, 28; 17, attribute; 17, 18; 17, 27; 18, call; 18, 19; 18, 26; 19, attribute; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:app; 24, identifier:documents; 25, identifier:get; 26, argument_list; 27, identifier:items; 28, argument_list; 29, block; 29, 30; 30, if_statement; 30, 31; 30, 34; 31, comparison_operator:!=; 31, 32; 31, 33; 32, identifier:key; 33, string:"main"; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:documents; 39, identifier:append; 40, argument_list; 40, 41; 41, tuple; 41, 42; 41, 43; 42, identifier:key; 43, identifier:document; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:documents; 47, call; 47, 48; 47, 49; 48, identifier:sorted; 49, argument_list; 49, 50; 49, 51; 50, identifier:documents; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:key; 53, lambda; 53, 54; 53, 56; 54, lambda_parameters; 54, 55; 55, identifier:x; 56, subscript; 56, 57; 56, 58; 57, identifier:x; 58, integer:0; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:main; 62, call; 62, 63; 62, 70; 63, attribute; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:app; 68, identifier:documents; 69, identifier:get; 70, argument_list; 70, 71; 71, string:"main"; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:main; 75, None; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:documents; 81, identifier:insert; 82, argument_list; 82, 83; 82, 84; 83, integer:0; 84, tuple; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:main; 87, identifier:name; 88, identifier:main; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:user_answer; 92, string:""; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:index; 96, integer:0; 97, while_statement; 97, 98; 97, 101; 98, comparison_operator:!=; 98, 99; 98, 100; 99, identifier:user_answer; 100, string:"X"; 101, block; 101, 102; 101, 111; 101, 130; 101, 138; 101, 152; 101, 180; 101, 194; 101, 204; 101, 209; 101, 219; 101, 223; 101, 237; 101, 246; 101, 250; 101, 255; 101, 284; 101, 298; 102, if_statement; 102, 103; 102, 106; 103, comparison_operator:<; 103, 104; 103, 105; 104, identifier:index; 105, integer:0; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:index; 110, integer:0; 111, if_statement; 111, 112; 111, 120; 112, comparison_operator:>; 112, 113; 112, 114; 113, identifier:index; 114, binary_operator:-; 114, 115; 114, 119; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, identifier:documents; 119, integer:1; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:index; 124, binary_operator:-; 124, 125; 124, 129; 125, call; 125, 126; 125, 127; 126, identifier:len; 127, argument_list; 127, 128; 128, identifier:documents; 129, integer:1; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:document; 133, subscript; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:documents; 136, identifier:index; 137, integer:1; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:os; 142, identifier:system; 143, argument_list; 143, 144; 144, conditional_expression:if; 144, 145; 144, 146; 144, 151; 145, string:'cls'; 146, comparison_operator:==; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:os; 149, identifier:name; 150, string:'nt'; 151, string:'clear'; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 155; 154, identifier:echo; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 169; 157, attribute; 157, 158; 157, 168; 158, call; 158, 159; 158, 164; 159, attribute; 159, 160; 159, 163; 160, call; 160, 161; 160, 162; 161, identifier:Environment; 162, argument_list; 163, identifier:from_string; 164, argument_list; 164, 165; 165, attribute; 165, 166; 165, 167; 166, identifier:document; 167, identifier:content; 168, identifier:render; 169, argument_list; 169, 170; 169, 175; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:app; 172, attribute; 172, 173; 172, 174; 173, identifier:self; 174, identifier:app; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:plugin; 177, attribute; 177, 178; 177, 179; 178, identifier:document; 179, identifier:plugin; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:source; 183, binary_operator:%; 183, 184; 183, 185; 184, string:"This document is registered by '%s' under the name '%s'"; 185, tuple; 185, 186; 185, 191; 186, attribute; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:document; 189, identifier:plugin; 190, identifier:name; 191, attribute; 191, 192; 191, 193; 192, identifier:document; 193, identifier:name; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:echo; 197, argument_list; 197, 198; 198, binary_operator:*; 198, 199; 198, 200; 199, string:"-"; 200, call; 200, 201; 200, 202; 201, identifier:len; 202, argument_list; 202, 203; 203, identifier:source; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 207; 206, identifier:echo; 207, argument_list; 207, 208; 208, identifier:source; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 212; 211, identifier:echo; 212, argument_list; 212, 213; 213, binary_operator:*; 213, 214; 213, 215; 214, string:"-"; 215, call; 215, 216; 215, 217; 216, identifier:len; 217, argument_list; 217, 218; 218, identifier:source; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:commands; 222, string:"Actions: "; 223, if_statement; 223, 224; 223, 232; 224, comparison_operator:<; 224, 225; 224, 226; 225, identifier:index; 226, binary_operator:-; 226, 227; 226, 231; 227, call; 227, 228; 227, 229; 228, identifier:len; 229, argument_list; 229, 230; 230, identifier:documents; 231, integer:1; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, augmented_assignment:+=; 234, 235; 234, 236; 235, identifier:commands; 236, string:"[N]ext, "; 237, if_statement; 237, 238; 237, 241; 238, comparison_operator:>; 238, 239; 238, 240; 239, identifier:index; 240, integer:0; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, augmented_assignment:+=; 243, 244; 243, 245; 244, identifier:commands; 245, string:"[P]revious, "; 246, expression_statement; 246, 247; 247, augmented_assignment:+=; 247, 248; 247, 249; 248, identifier:commands; 249, string:"E[x]it"; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 253; 252, identifier:echo; 253, argument_list; 253, 254; 254, identifier:commands; 255, if_statement; 255, 256; 255, 264; 255, 269; 255, 278; 256, comparison_operator:<; 256, 257; 256, 258; 257, identifier:index; 258, binary_operator:-; 258, 259; 258, 263; 259, call; 259, 260; 259, 261; 260, identifier:len; 261, argument_list; 261, 262; 262, identifier:documents; 263, integer:1; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:default; 268, string:"N"; 269, elif_clause; 269, 270; 269, 273; 270, comparison_operator:>; 270, 271; 270, 272; 271, identifier:index; 272, integer:0; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:default; 277, string:"P"; 278, else_clause; 278, 279; 279, block; 279, 280; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 282, identifier:default; 283, string:"X"; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:user_answer; 287, call; 287, 288; 287, 297; 288, attribute; 288, 289; 288, 296; 289, call; 289, 290; 289, 291; 290, identifier:prompt; 291, argument_list; 291, 292; 291, 293; 292, string:"Select your action"; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:default; 295, identifier:default; 296, identifier:upper; 297, argument_list; 298, if_statement; 298, 299; 298, 302; 298, 307; 299, comparison_operator:==; 299, 300; 299, 301; 300, identifier:user_answer; 301, string:"N"; 302, block; 302, 303; 303, expression_statement; 303, 304; 304, augmented_assignment:+=; 304, 305; 304, 306; 305, identifier:index; 306, integer:1; 307, elif_clause; 307, 308; 307, 311; 308, comparison_operator:==; 308, 309; 308, 310; 309, identifier:user_answer; 310, string:"P"; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, augmented_assignment:-=; 313, 314; 313, 315; 314, identifier:index; 315, integer:1
def _show_documentation(self): """ Shows all documents of the current groundwork app in the console. Documents are sorted bei its names, except "main", which gets set to the beginning. """ documents = [] for key, document in self.app.documents.get().items(): if key != "main": documents.append((key, document)) documents = sorted(documents, key=lambda x: x[0]) main = self.app.documents.get("main") if main is not None: documents.insert(0, (main.name, main)) user_answer = "" index = 0 while user_answer != "X": if index < 0: index = 0 if index > len(documents) - 1: index = len(documents) - 1 document = documents[index][1] os.system('cls' if os.name == 'nt' else 'clear') echo(Environment().from_string(document.content).render(app=self.app, plugin=document.plugin)) source = "This document is registered by '%s' under the name '%s'" % (document.plugin.name, document.name) echo("-" * len(source)) echo(source) echo("-" * len(source)) commands = "Actions: " if index < len(documents) - 1: commands += "[N]ext, " if index > 0: commands += "[P]revious, " commands += "E[x]it" echo(commands) if index < len(documents) - 1: default = "N" elif index > 0: default = "P" else: default = "X" user_answer = prompt("Select your action", default=default).upper() if user_answer == "N": index += 1 elif user_answer == "P": index -= 1
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:infer_declared_from_conditions; 3, parameters; 3, 4; 3, 5; 4, identifier:conds; 5, default_parameter; 5, 6; 5, 7; 6, identifier:namespace; 7, None; 8, block; 8, 9; 8, 11; 8, 15; 8, 16; 8, 17; 8, 18; 8, 29; 8, 38; 8, 47; 8, 53; 8, 57; 8, 152; 8, 163; 9, expression_statement; 9, 10; 10, string:''' like infer_declared except that it is passed a set of first party caveat conditions as a list of string rather than a set of macaroons. '''; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:conflicts; 14, list:[]; 15, comment; 16, comment; 17, comment; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:namespace; 21, None; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:namespace; 26, call; 26, 27; 26, 28; 27, identifier:Namespace; 28, argument_list; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:prefix; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:namespace; 35, identifier:resolve; 36, argument_list; 36, 37; 37, identifier:STD_NAMESPACE; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:prefix; 41, None; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:prefix; 46, string:''; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:declared_cond; 50, binary_operator:+; 50, 51; 50, 52; 51, identifier:prefix; 52, identifier:COND_DECLARED; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:info; 56, dictionary; 57, for_statement; 57, 58; 57, 59; 57, 60; 58, identifier:cond; 59, identifier:conds; 60, block; 60, 61; 60, 83; 60, 89; 60, 99; 60, 108; 60, 120; 60, 129; 60, 146; 61, try_statement; 61, 62; 61, 72; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 68; 65, pattern_list; 65, 66; 65, 67; 66, identifier:name; 67, identifier:rest; 68, call; 68, 69; 68, 70; 69, identifier:parse_caveat; 70, argument_list; 70, 71; 71, identifier:cond; 72, except_clause; 72, 73; 72, 74; 73, identifier:ValueError; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 80; 77, pattern_list; 77, 78; 77, 79; 78, identifier:name; 79, identifier:rest; 80, expression_list; 80, 81; 80, 82; 81, string:''; 82, string:''; 83, if_statement; 83, 84; 83, 87; 84, comparison_operator:!=; 84, 85; 84, 86; 85, identifier:name; 86, identifier:declared_cond; 87, block; 87, 88; 88, continue_statement; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:parts; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:rest; 95, identifier:split; 96, argument_list; 96, 97; 96, 98; 97, string:' '; 98, integer:1; 99, if_statement; 99, 100; 99, 106; 100, comparison_operator:!=; 100, 101; 100, 105; 101, call; 101, 102; 101, 103; 102, identifier:len; 103, argument_list; 103, 104; 104, identifier:parts; 105, integer:2; 106, block; 106, 107; 107, continue_statement; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 113; 110, pattern_list; 110, 111; 110, 112; 111, identifier:key; 112, identifier:val; 113, expression_list; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:parts; 116, integer:0; 117, subscript; 117, 118; 117, 119; 118, identifier:parts; 119, integer:1; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:old_val; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:info; 126, identifier:get; 127, argument_list; 127, 128; 128, identifier:key; 129, if_statement; 129, 130; 129, 137; 130, boolean_operator:and; 130, 131; 130, 134; 131, comparison_operator:is; 131, 132; 131, 133; 132, identifier:old_val; 133, None; 134, comparison_operator:!=; 134, 135; 134, 136; 135, identifier:old_val; 136, identifier:val; 137, block; 137, 138; 137, 145; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:conflicts; 142, identifier:append; 143, argument_list; 143, 144; 144, identifier:key; 145, continue_statement; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, subscript; 148, 149; 148, 150; 149, identifier:info; 150, identifier:key; 151, identifier:val; 152, for_statement; 152, 153; 152, 154; 152, 158; 153, identifier:key; 154, call; 154, 155; 154, 156; 155, identifier:set; 156, argument_list; 156, 157; 157, identifier:conflicts; 158, block; 158, 159; 159, delete_statement; 159, 160; 160, subscript; 160, 161; 160, 162; 161, identifier:info; 162, identifier:key; 163, return_statement; 163, 164; 164, identifier:info
def infer_declared_from_conditions(conds, namespace=None): ''' like infer_declared except that it is passed a set of first party caveat conditions as a list of string rather than a set of macaroons. ''' conflicts = [] # If we can't resolve that standard namespace, then we'll look for # just bare "declared" caveats which will work OK for legacy # macaroons with no namespace. if namespace is None: namespace = Namespace() prefix = namespace.resolve(STD_NAMESPACE) if prefix is None: prefix = '' declared_cond = prefix + COND_DECLARED info = {} for cond in conds: try: name, rest = parse_caveat(cond) except ValueError: name, rest = '', '' if name != declared_cond: continue parts = rest.split(' ', 1) if len(parts) != 2: continue key, val = parts[0], parts[1] old_val = info.get(key) if old_val is not None and old_val != val: conflicts.append(key) continue info[key] = val for key in set(conflicts): del info[key] return info
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_cited_dois; 3, parameters; 3, 4; 4, identifier:file; 5, block; 5, 6; 5, 8; 5, 9; 5, 33; 5, 37; 5, 41; 5, 42; 5, 133; 5, 134; 5, 220; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, if_statement; 9, 10; 9, 16; 9, 17; 9, 18; 9, 26; 10, not_operator; 10, 11; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:file; 15, identifier:list; 16, comment; 17, comment; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:plaintext_citations; 22, call; 22, 23; 22, 24; 23, identifier:get_plaintext_citations; 24, argument_list; 24, 25; 25, identifier:file; 26, else_clause; 26, 27; 26, 28; 27, comment; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:plaintext_citations; 32, identifier:file; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:dois; 36, dictionary; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:crossref_queue; 40, list:[]; 41, comment; 42, for_statement; 42, 43; 42, 44; 42, 48; 42, 49; 43, identifier:citation; 44, subscript; 44, 45; 44, 46; 45, identifier:plaintext_citations; 46, slice; 46, 47; 47, colon; 48, comment; 49, block; 49, 50; 49, 59; 49, 81; 49, 82; 49, 91; 49, 118; 49, 119; 49, 120; 49, 121; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:matched_dois; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:doi; 56, identifier:extract_from_text; 57, argument_list; 57, 58; 58, identifier:citation; 59, if_statement; 59, 60; 59, 66; 59, 67; 60, comparison_operator:>; 60, 61; 60, 65; 61, call; 61, 62; 61, 63; 62, identifier:len; 63, argument_list; 63, 64; 64, identifier:matched_dois; 65, integer:0; 66, comment; 67, block; 67, 68; 67, 80; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, subscript; 70, 71; 70, 72; 71, identifier:dois; 72, identifier:citation; 73, call; 73, 74; 73, 75; 74, identifier:next; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 78; 77, identifier:iter; 78, argument_list; 78, 79; 79, identifier:matched_dois; 80, continue_statement; 81, comment; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:matched_arxiv; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:arxiv; 88, identifier:extract_from_text; 89, argument_list; 89, 90; 90, identifier:citation; 91, if_statement; 91, 92; 91, 98; 91, 99; 92, comparison_operator:>; 92, 93; 92, 97; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:matched_arxiv; 97, integer:0; 98, comment; 99, block; 99, 100; 99, 117; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, subscript; 102, 103; 102, 104; 103, identifier:dois; 104, identifier:citation; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:arxiv; 108, identifier:to_doi; 109, argument_list; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:next; 112, argument_list; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:iter; 115, argument_list; 115, 116; 116, identifier:matched_arxiv; 117, continue_statement; 118, comment; 119, comment; 120, comment; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:crossref_queue; 125, identifier:append; 126, argument_list; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:tools; 130, identifier:remove_urls; 131, argument_list; 131, 132; 132, identifier:citation; 133, comment; 134, for_statement; 134, 135; 134, 136; 134, 143; 135, identifier:batch; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:tools; 139, identifier:batch; 140, argument_list; 140, 141; 140, 142; 141, identifier:crossref_queue; 142, identifier:CROSSREF_MAX_BATCH_SIZE; 143, block; 143, 144; 143, 152; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:batch; 147, list_comprehension; 147, 148; 147, 149; 148, identifier:i; 149, for_in_clause; 149, 150; 149, 151; 150, identifier:i; 151, identifier:batch; 152, try_statement; 152, 153; 152, 154; 152, 202; 153, comment; 154, block; 154, 155; 154, 167; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:request; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:requests; 161, identifier:post; 162, argument_list; 162, 163; 162, 164; 163, identifier:CROSSREF_LINKS_API_URL; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:json; 166, identifier:batch; 167, for_statement; 167, 168; 167, 169; 167, 176; 167, 177; 168, identifier:result; 169, subscript; 169, 170; 169, 175; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:request; 173, identifier:json; 174, argument_list; 175, string:"results"; 176, comment; 177, block; 177, 178; 178, try_statement; 178, 179; 178, 190; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 187; 182, subscript; 182, 183; 182, 184; 183, identifier:dois; 184, subscript; 184, 185; 184, 186; 185, identifier:result; 186, string:"text"; 187, subscript; 187, 188; 187, 189; 188, identifier:result; 189, string:"doi"; 190, except_clause; 190, 191; 190, 192; 190, 193; 191, identifier:KeyError; 192, comment; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 201; 196, subscript; 196, 197; 196, 198; 197, identifier:dois; 198, subscript; 198, 199; 198, 200; 199, identifier:result; 200, string:"text"; 201, None; 202, except_clause; 202, 203; 202, 207; 202, 208; 202, 209; 203, tuple; 203, 204; 203, 205; 203, 206; 204, identifier:RequestException; 205, identifier:ValueError; 206, identifier:KeyError; 207, comment; 208, comment; 209, block; 209, 210; 210, for_statement; 210, 211; 210, 212; 210, 213; 211, identifier:i; 212, identifier:batch; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 219; 216, subscript; 216, 217; 216, 218; 217, identifier:dois; 218, identifier:i; 219, None; 220, return_statement; 220, 221; 221, identifier:dois
def get_cited_dois(file): """ Get the DOIs of the papers cited in a plaintext file. The file should \ have one citation per line. .. note:: This function is also used as a backend tool by most of the others \ citations processors, to factorize the code. :param file: Either the path to the plaintext file or the content of a \ plaintext file. It can also be a parsed list of plaintext \ citations and, in this case, no preprocessing is done. :returns: A dict of cleaned plaintext citations and their associated DOI. """ # If file is not a pre-processed list of plaintext citations if not isinstance(file, list): # It is either a path to a plaintext file or the content of a plaintext # file, we need some pre-processing to get a list of citations. plaintext_citations = get_plaintext_citations(file) else: # Else, we passed a list of plaintext citations. plaintext_citations = file dois = {} crossref_queue = [] # Try to get the DOI directly from the citation for citation in plaintext_citations[:]: # Some citations already contain a DOI so try to match it directly matched_dois = doi.extract_from_text(citation) if len(matched_dois) > 0: # Add the DOI and go on dois[citation] = next(iter(matched_dois)) continue # Same thing for arXiv id matched_arxiv = arxiv.extract_from_text(citation) if len(matched_arxiv) > 0: # Add the associated DOI and go on dois[citation] = arxiv.to_doi(next(iter(matched_arxiv))) continue # If no match found, stack it for next step # Note to remove URLs in the citation as the plaintext citations can # contain URLs and they are bad for the CrossRef API. crossref_queue.append(tools.remove_urls(citation)) # Do batch with remaining papers, to prevent from the timeout of CrossRef for batch in tools.batch(crossref_queue, CROSSREF_MAX_BATCH_SIZE): batch = [i for i in batch] try: # Fetch results from CrossRef request = requests.post(CROSSREF_LINKS_API_URL, json=batch) for result in request.json()["results"]: # Try to get a DOI try: dois[result["text"]] = result["doi"] except KeyError: # Or set it to None dois[result["text"]] = None except (RequestException, ValueError, KeyError): # If an exception occurred, set all the DOIs to None for the # current batch for i in batch: dois[i] = None return dois
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:used_args; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 90; 5, 99; 5, 100; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:values; 12, list:[]; 13, for_statement; 13, 14; 13, 17; 13, 27; 14, pattern_list; 14, 15; 14, 16; 15, identifier:idx; 16, identifier:c; 17, call; 17, 18; 17, 19; 18, identifier:enumerate; 19, argument_list; 19, 20; 20, subscript; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:words; 24, slice; 24, 25; 24, 26; 25, integer:1; 26, colon; 27, block; 27, 28; 27, 37; 27, 51; 27, 60; 28, if_statement; 28, 29; 28, 35; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:c; 32, identifier:startswith; 33, argument_list; 33, 34; 34, string:'-'; 35, block; 35, 36; 36, continue_statement; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:option_str; 40, subscript; 40, 41; 40, 48; 41, subscript; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:words; 45, slice; 45, 46; 45, 47; 46, integer:1; 47, colon; 48, binary_operator:-; 48, 49; 48, 50; 49, identifier:idx; 50, integer:1; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:option; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:get_option; 58, argument_list; 58, 59; 59, identifier:option_str; 60, if_statement; 60, 61; 60, 69; 61, boolean_operator:or; 61, 62; 61, 65; 62, comparison_operator:is; 62, 63; 62, 64; 63, identifier:option; 64, None; 65, not_operator; 65, 66; 66, attribute; 66, 67; 66, 68; 67, identifier:option; 68, identifier:need_value; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:values; 74, identifier:append; 75, argument_list; 75, 76; 76, tuple; 76, 77; 76, 78; 77, identifier:c; 78, comparison_operator:==; 78, 79; 78, 80; 79, identifier:c; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:document; 85, identifier:get_word_before_cursor; 86, argument_list; 86, 87; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:WORD; 89, True; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:logger; 94, identifier:debug; 95, argument_list; 95, 96; 96, binary_operator:%; 96, 97; 96, 98; 97, string:"Found args values %s"; 98, identifier:values; 99, comment; 100, for_statement; 100, 101; 100, 102; 100, 111; 101, identifier:arg; 102, call; 102, 103; 102, 110; 103, attribute; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:self; 107, identifier:cmd; 108, identifier:args; 109, identifier:values; 110, argument_list; 111, block; 111, 112; 111, 118; 112, if_statement; 112, 113; 112, 115; 113, not_operator; 113, 114; 114, identifier:values; 115, block; 115, 116; 116, raise_statement; 116, 117; 117, identifier:StopIteration; 118, if_statement; 118, 119; 118, 122; 118, 130; 119, attribute; 119, 120; 119, 121; 120, identifier:arg; 121, identifier:is_multiple; 122, block; 122, 123; 122, 127; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:values; 126, list:[]; 127, expression_statement; 127, 128; 128, yield; 128, 129; 129, identifier:arg; 130, elif_clause; 130, 131; 130, 139; 131, comparison_operator:is; 131, 132; 131, 138; 132, call; 132, 133; 132, 134; 133, identifier:type; 134, argument_list; 134, 135; 135, attribute; 135, 136; 135, 137; 136, identifier:arg; 137, identifier:nargs; 138, identifier:int; 139, block; 139, 140; 140, for_statement; 140, 141; 140, 142; 140, 148; 141, identifier:_; 142, call; 142, 143; 142, 144; 143, identifier:range; 144, argument_list; 144, 145; 145, attribute; 145, 146; 145, 147; 146, identifier:arg; 147, identifier:nargs; 148, block; 148, 149; 148, 158; 148, 159; 148, 169; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:value; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:values; 155, identifier:pop; 156, argument_list; 156, 157; 157, integer:0; 158, comment; 159, if_statement; 159, 160; 159, 165; 160, comparison_operator:is; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:value; 163, integer:1; 164, False; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, yield; 167, 168; 168, identifier:arg; 169, if_statement; 169, 170; 169, 172; 170, not_operator; 170, 171; 171, identifier:values; 172, block; 172, 173; 173, raise_statement; 173, 174; 174, identifier:StopIteration
def used_args(self): """Return args already used in the command line rtype: command.Arg generator """ # get all arguments values from the command line values = [] for idx, c in enumerate(self.words[1:]): if c.startswith('-'): continue option_str = self.words[1:][idx - 1] option = self.get_option(option_str) if option is None or not option.need_value: values.append((c, c == self.document.get_word_before_cursor(WORD=True))) logger.debug("Found args values %s" % values) # consume values for arg in self.cmd.args.values(): if not values: raise StopIteration if arg.is_multiple: values = [] yield arg elif type(arg.nargs) is int: for _ in range(arg.nargs): value = values.pop(0) # not the current argument if value[1] is False: yield arg if not values: raise StopIteration
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_preprocess; 3, parameters; 3, 4; 4, identifier:inp; 5, block; 5, 6; 5, 8; 5, 19; 5, 30; 5, 41; 5, 52; 5, 63; 5, 74; 5, 85; 5, 96; 5, 107; 5, 118; 5, 129; 5, 140; 5, 151; 5, 337; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:inp; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:re; 14, identifier:sub; 15, argument_list; 15, 16; 15, 17; 15, 18; 16, string:r'(\b)a(\b)'; 17, string:r'\g<1>one\g<2>'; 18, identifier:inp; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:inp; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:re; 25, identifier:sub; 26, argument_list; 26, 27; 26, 28; 26, 29; 27, string:r'to the (.*) power'; 28, string:r'to \g<1>'; 29, identifier:inp; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:inp; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:re; 36, identifier:sub; 37, argument_list; 37, 38; 37, 39; 37, 40; 38, string:r'to the (.*?)(\b)'; 39, string:r'to \g<1>\g<2>'; 40, identifier:inp; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:inp; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:re; 47, identifier:sub; 48, argument_list; 48, 49; 48, 50; 48, 51; 49, string:r'log of'; 50, string:r'log'; 51, identifier:inp; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:inp; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:re; 58, identifier:sub; 59, argument_list; 59, 60; 59, 61; 59, 62; 60, string:r'(square )?root( of)?'; 61, string:r'sqrt'; 62, identifier:inp; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:inp; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:re; 69, identifier:sub; 70, argument_list; 70, 71; 70, 72; 70, 73; 71, string:r'squared'; 72, string:r'to two'; 73, identifier:inp; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:inp; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:re; 80, identifier:sub; 81, argument_list; 81, 82; 81, 83; 81, 84; 82, string:r'cubed'; 83, string:r'to three'; 84, identifier:inp; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:inp; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:re; 91, identifier:sub; 92, argument_list; 92, 93; 92, 94; 92, 95; 93, string:r'divided?( by)?'; 94, string:r'divide'; 95, identifier:inp; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:inp; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:re; 102, identifier:sub; 103, argument_list; 103, 104; 103, 105; 103, 106; 104, string:r'(\b)over(\b)'; 105, string:r'\g<1>divide\g<2>'; 106, identifier:inp; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:inp; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:re; 113, identifier:sub; 114, argument_list; 114, 115; 114, 116; 114, 117; 115, string:r'(\b)EE(\b)'; 116, string:r'\g<1>e\g<2>'; 117, identifier:inp; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:inp; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:re; 124, identifier:sub; 125, argument_list; 125, 126; 125, 127; 125, 128; 126, string:r'(\b)E(\b)'; 127, string:r'\g<1>e\g<2>'; 128, identifier:inp; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:inp; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:re; 135, identifier:sub; 136, argument_list; 136, 137; 136, 138; 136, 139; 137, string:r'(\b)pie(\b)'; 138, string:r'\g<1>pi\g<2>'; 139, identifier:inp; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:inp; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:re; 146, identifier:sub; 147, argument_list; 147, 148; 147, 149; 147, 150; 148, string:r'(\b)PI(\b)'; 149, string:r'\g<1>pi\g<2>'; 150, identifier:inp; 151, function_definition; 151, 152; 151, 153; 151, 155; 152, function_name:findImplicitMultiplications; 153, parameters; 153, 154; 154, identifier:inp; 155, block; 155, 156; 155, 158; 155, 236; 155, 329; 156, expression_statement; 156, 157; 157, comment; 158, function_definition; 158, 159; 158, 160; 158, 162; 159, function_name:findConstantMultiplications; 160, parameters; 160, 161; 161, identifier:inp; 162, block; 162, 163; 162, 172; 162, 176; 162, 182; 162, 234; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:split; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:inp; 169, identifier:split; 170, argument_list; 170, 171; 171, string:' '; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:revision; 175, string:""; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:converter; 179, call; 179, 180; 179, 181; 180, identifier:NumberService; 181, argument_list; 182, for_statement; 182, 183; 182, 186; 182, 190; 183, pattern_list; 183, 184; 183, 185; 184, identifier:i; 185, identifier:w; 186, call; 186, 187; 186, 188; 187, identifier:enumerate; 188, argument_list; 188, 189; 189, identifier:split; 190, block; 190, 191; 190, 218; 191, if_statement; 191, 192; 191, 201; 192, boolean_operator:and; 192, 193; 192, 196; 193, comparison_operator:>; 193, 194; 193, 195; 194, identifier:i; 195, integer:0; 196, comparison_operator:in; 196, 197; 196, 198; 197, identifier:w; 198, attribute; 198, 199; 198, 200; 199, identifier:MathService; 200, identifier:__constants__; 201, block; 201, 202; 202, if_statement; 202, 203; 202, 213; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:converter; 206, identifier:isValid; 207, argument_list; 207, 208; 208, subscript; 208, 209; 208, 210; 209, identifier:split; 210, binary_operator:-; 210, 211; 210, 212; 211, identifier:i; 212, integer:1; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, augmented_assignment:+=; 215, 216; 215, 217; 216, identifier:revision; 217, string:" times"; 218, if_statement; 218, 219; 218, 221; 218, 226; 219, not_operator; 219, 220; 220, identifier:revision; 221, block; 221, 222; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:revision; 225, identifier:w; 226, else_clause; 226, 227; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, augmented_assignment:+=; 229, 230; 229, 231; 230, identifier:revision; 231, binary_operator:+; 231, 232; 231, 233; 232, string:" "; 233, identifier:w; 234, return_statement; 234, 235; 235, identifier:revision; 236, function_definition; 236, 237; 236, 238; 236, 240; 237, function_name:findUnaryMultiplications; 238, parameters; 238, 239; 239, identifier:inp; 240, block; 240, 241; 240, 250; 240, 254; 240, 327; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:split; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:inp; 247, identifier:split; 248, argument_list; 248, 249; 249, string:' '; 250, expression_statement; 250, 251; 251, assignment; 251, 252; 251, 253; 252, identifier:revision; 253, string:""; 254, for_statement; 254, 255; 254, 258; 254, 262; 255, pattern_list; 255, 256; 255, 257; 256, identifier:i; 257, identifier:w; 258, call; 258, 259; 258, 260; 259, identifier:enumerate; 260, argument_list; 260, 261; 261, identifier:split; 262, block; 262, 263; 262, 311; 263, if_statement; 263, 264; 263, 273; 264, boolean_operator:and; 264, 265; 264, 268; 265, comparison_operator:>; 265, 266; 265, 267; 266, identifier:i; 267, integer:0; 268, comparison_operator:in; 268, 269; 268, 270; 269, identifier:w; 270, attribute; 270, 271; 270, 272; 271, identifier:MathService; 272, identifier:__unaryOperators__; 273, block; 273, 274; 273, 282; 273, 290; 273, 298; 274, expression_statement; 274, 275; 275, assignment; 275, 276; 275, 277; 276, identifier:last_op; 277, subscript; 277, 278; 277, 279; 278, identifier:split; 279, binary_operator:-; 279, 280; 279, 281; 280, identifier:i; 281, integer:1; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:binary; 285, comparison_operator:in; 285, 286; 285, 287; 286, identifier:last_op; 287, attribute; 287, 288; 287, 289; 288, identifier:MathService; 289, identifier:__binaryOperators__; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:unary; 293, comparison_operator:in; 293, 294; 293, 295; 294, identifier:last_op; 295, attribute; 295, 296; 295, 297; 296, identifier:MathService; 297, identifier:__unaryOperators__; 298, if_statement; 298, 299; 298, 306; 299, boolean_operator:and; 299, 300; 299, 301; 300, identifier:last_op; 301, not_operator; 301, 302; 302, parenthesized_expression; 302, 303; 303, boolean_operator:or; 303, 304; 303, 305; 304, identifier:binary; 305, identifier:unary; 306, block; 306, 307; 307, expression_statement; 307, 308; 308, augmented_assignment:+=; 308, 309; 308, 310; 309, identifier:revision; 310, string:" times"; 311, if_statement; 311, 312; 311, 314; 311, 319; 312, not_operator; 312, 313; 313, identifier:revision; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, assignment; 316, 317; 316, 318; 317, identifier:revision; 318, identifier:w; 319, else_clause; 319, 320; 320, block; 320, 321; 321, expression_statement; 321, 322; 322, augmented_assignment:+=; 322, 323; 322, 324; 323, identifier:revision; 324, binary_operator:+; 324, 325; 324, 326; 325, string:" "; 326, identifier:w; 327, return_statement; 327, 328; 328, identifier:revision; 329, return_statement; 329, 330; 330, call; 330, 331; 330, 332; 331, identifier:findUnaryMultiplications; 332, argument_list; 332, 333; 333, call; 333, 334; 333, 335; 334, identifier:findConstantMultiplications; 335, argument_list; 335, 336; 336, identifier:inp; 337, return_statement; 337, 338; 338, call; 338, 339; 338, 340; 339, identifier:findImplicitMultiplications; 340, argument_list; 340, 341; 341, identifier:inp
def _preprocess(inp): """Revise wording to match canonical and expected forms.""" inp = re.sub(r'(\b)a(\b)', r'\g<1>one\g<2>', inp) inp = re.sub(r'to the (.*) power', r'to \g<1>', inp) inp = re.sub(r'to the (.*?)(\b)', r'to \g<1>\g<2>', inp) inp = re.sub(r'log of', r'log', inp) inp = re.sub(r'(square )?root( of)?', r'sqrt', inp) inp = re.sub(r'squared', r'to two', inp) inp = re.sub(r'cubed', r'to three', inp) inp = re.sub(r'divided?( by)?', r'divide', inp) inp = re.sub(r'(\b)over(\b)', r'\g<1>divide\g<2>', inp) inp = re.sub(r'(\b)EE(\b)', r'\g<1>e\g<2>', inp) inp = re.sub(r'(\b)E(\b)', r'\g<1>e\g<2>', inp) inp = re.sub(r'(\b)pie(\b)', r'\g<1>pi\g<2>', inp) inp = re.sub(r'(\b)PI(\b)', r'\g<1>pi\g<2>', inp) def findImplicitMultiplications(inp): """Replace omitted 'times' references.""" def findConstantMultiplications(inp): split = inp.split(' ') revision = "" converter = NumberService() for i, w in enumerate(split): if i > 0 and w in MathService.__constants__: if converter.isValid(split[i - 1]): revision += " times" if not revision: revision = w else: revision += " " + w return revision def findUnaryMultiplications(inp): split = inp.split(' ') revision = "" for i, w in enumerate(split): if i > 0 and w in MathService.__unaryOperators__: last_op = split[i - 1] binary = last_op in MathService.__binaryOperators__ unary = last_op in MathService.__unaryOperators__ if last_op and not (binary or unary): revision += " times" if not revision: revision = w else: revision += " " + w return revision return findUnaryMultiplications(findConstantMultiplications(inp)) return findImplicitMultiplications(inp)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parseEquation; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:inp; 6, block; 6, 7; 6, 9; 6, 18; 6, 27; 6, 28; 6, 111; 6, 240; 6, 249; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:inp; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:MathService; 15, identifier:_preprocess; 16, argument_list; 16, 17; 17, identifier:inp; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:split; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:inp; 24, identifier:split; 25, argument_list; 25, 26; 26, string:' '; 27, comment; 28, for_statement; 28, 29; 28, 32; 28, 36; 29, pattern_list; 29, 30; 29, 31; 30, identifier:i; 31, identifier:w; 32, call; 32, 33; 32, 34; 33, identifier:enumerate; 34, argument_list; 34, 35; 35, identifier:split; 36, block; 36, 37; 37, if_statement; 37, 38; 37, 43; 38, comparison_operator:in; 38, 39; 38, 40; 39, identifier:w; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:__unaryOperators__; 43, block; 43, 44; 43, 52; 43, 53; 43, 66; 43, 81; 43, 82; 43, 97; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:op; 47, subscript; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:__unaryOperators__; 51, identifier:w; 52, comment; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:eq1; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, string:' '; 59, identifier:join; 60, argument_list; 60, 61; 61, subscript; 61, 62; 61, 63; 62, identifier:split; 63, slice; 63, 64; 63, 65; 64, colon; 65, identifier:i; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:eq2; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, string:' '; 72, identifier:join; 73, argument_list; 73, 74; 74, subscript; 74, 75; 74, 76; 75, identifier:split; 76, slice; 76, 77; 76, 80; 77, binary_operator:+; 77, 78; 77, 79; 78, identifier:i; 79, integer:1; 80, colon; 81, comment; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:result; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:MathService; 88, identifier:_applyUnary; 89, argument_list; 89, 90; 89, 96; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:parseEquation; 94, argument_list; 94, 95; 95, identifier:eq2; 96, identifier:op; 97, return_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:parseEquation; 102, argument_list; 102, 103; 103, binary_operator:+; 103, 104; 103, 107; 104, binary_operator:+; 104, 105; 104, 106; 105, identifier:eq1; 106, string:" "; 107, call; 107, 108; 107, 109; 108, identifier:str; 109, argument_list; 109, 110; 110, identifier:result; 111, function_definition; 111, 112; 111, 113; 111, 115; 112, function_name:extractNumbersAndSymbols; 113, parameters; 113, 114; 114, identifier:inp; 115, block; 115, 116; 115, 120; 115, 124; 115, 125; 115, 129; 115, 183; 115, 193; 115, 194; 115, 225; 115, 236; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:numbers; 119, list:[]; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:symbols; 123, list:[]; 124, comment; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:next_number; 128, string:""; 129, for_statement; 129, 130; 129, 131; 129, 137; 130, identifier:w; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:inp; 134, identifier:split; 135, argument_list; 135, 136; 136, string:' '; 137, block; 137, 138; 138, if_statement; 138, 139; 138, 144; 138, 170; 139, comparison_operator:in; 139, 140; 139, 141; 140, identifier:w; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:__binaryOperators__; 144, block; 144, 145; 144, 156; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:symbols; 149, identifier:append; 150, argument_list; 150, 151; 151, subscript; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:__binaryOperators__; 155, identifier:w; 156, if_statement; 156, 157; 156, 158; 157, identifier:next_number; 158, block; 158, 159; 158, 166; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:numbers; 163, identifier:append; 164, argument_list; 164, 165; 165, identifier:next_number; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:next_number; 169, string:""; 170, else_clause; 170, 171; 171, block; 171, 172; 171, 179; 172, if_statement; 172, 173; 172, 174; 173, identifier:next_number; 174, block; 174, 175; 175, expression_statement; 175, 176; 176, augmented_assignment:+=; 176, 177; 176, 178; 177, identifier:next_number; 178, string:" "; 179, expression_statement; 179, 180; 180, augmented_assignment:+=; 180, 181; 180, 182; 181, identifier:next_number; 182, identifier:w; 183, if_statement; 183, 184; 183, 185; 184, identifier:next_number; 185, block; 185, 186; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:numbers; 190, identifier:append; 191, argument_list; 191, 192; 192, identifier:next_number; 193, comment; 194, function_definition; 194, 195; 194, 196; 194, 198; 195, function_name:convert; 196, parameters; 196, 197; 197, identifier:n; 198, block; 198, 199; 198, 212; 198, 218; 199, if_statement; 199, 200; 199, 205; 200, comparison_operator:in; 200, 201; 200, 202; 201, identifier:n; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:__constants__; 205, block; 205, 206; 206, return_statement; 206, 207; 207, subscript; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:__constants__; 211, identifier:n; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:converter; 215, call; 215, 216; 215, 217; 216, identifier:NumberService; 217, argument_list; 218, return_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:converter; 222, identifier:parse; 223, argument_list; 223, 224; 224, identifier:n; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:numbers; 228, list_comprehension; 228, 229; 228, 233; 229, call; 229, 230; 229, 231; 230, identifier:convert; 231, argument_list; 231, 232; 232, identifier:n; 233, for_in_clause; 233, 234; 233, 235; 234, identifier:n; 235, identifier:numbers; 236, return_statement; 236, 237; 237, expression_list; 237, 238; 237, 239; 238, identifier:numbers; 239, identifier:symbols; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 245; 242, pattern_list; 242, 243; 242, 244; 243, identifier:numbers; 244, identifier:symbols; 245, call; 245, 246; 245, 247; 246, identifier:extractNumbersAndSymbols; 247, argument_list; 247, 248; 248, identifier:inp; 249, return_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:MathService; 253, identifier:_calculate; 254, argument_list; 254, 255; 254, 256; 255, identifier:numbers; 256, identifier:symbols
def parseEquation(self, inp): """Solves the equation specified by the input string. Args: inp (str): An equation, specified in words, containing some combination of numbers, binary, and unary operations. Returns: The floating-point result of carrying out the computation. """ inp = MathService._preprocess(inp) split = inp.split(' ') # Recursive call on unary operators for i, w in enumerate(split): if w in self.__unaryOperators__: op = self.__unaryOperators__[w] # Split equation into halves eq1 = ' '.join(split[:i]) eq2 = ' '.join(split[i + 1:]) # Calculate second half result = MathService._applyUnary(self.parseEquation(eq2), op) return self.parseEquation(eq1 + " " + str(result)) def extractNumbersAndSymbols(inp): numbers = [] symbols = [] # Divide into values (numbers), operators (symbols) next_number = "" for w in inp.split(' '): if w in self.__binaryOperators__: symbols.append(self.__binaryOperators__[w]) if next_number: numbers.append(next_number) next_number = "" else: if next_number: next_number += " " next_number += w if next_number: numbers.append(next_number) # Cast numbers from words to integers def convert(n): if n in self.__constants__: return self.__constants__[n] converter = NumberService() return converter.parse(n) numbers = [convert(n) for n in numbers] return numbers, symbols numbers, symbols = extractNumbersAndSymbols(inp) return MathService._calculate(numbers, symbols)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:activate; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:plugins; 7, list:[]; 8, block; 8, 9; 8, 11; 8, 20; 8, 38; 8, 54; 8, 58; 8, 288; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:_log; 17, identifier:debug; 18, argument_list; 18, 19; 19, string:"Plugins Activation started"; 20, if_statement; 20, 21; 20, 27; 21, not_operator; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:isinstance; 24, argument_list; 24, 25; 24, 26; 25, identifier:plugins; 26, identifier:list; 27, block; 27, 28; 28, raise_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:AttributeError; 31, argument_list; 31, 32; 32, binary_operator:%; 32, 33; 32, 34; 33, string:"plugins must be a list, not %s"; 34, call; 34, 35; 34, 36; 35, identifier:type; 36, argument_list; 36, 37; 37, identifier:plugins; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:_log; 44, identifier:debug; 45, argument_list; 45, 46; 46, binary_operator:%; 46, 47; 46, 48; 47, string:"Plugins to activate: %s"; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, string:", "; 51, identifier:join; 52, argument_list; 52, 53; 53, identifier:plugins; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:plugins_activated; 57, list:[]; 58, for_statement; 58, 59; 58, 60; 58, 61; 59, identifier:plugin_name; 60, identifier:plugins; 61, block; 61, 62; 61, 80; 61, 181; 62, if_statement; 62, 63; 62, 69; 63, not_operator; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:isinstance; 66, argument_list; 66, 67; 66, 68; 67, identifier:plugin_name; 68, identifier:str; 69, block; 69, 70; 70, raise_statement; 70, 71; 71, call; 71, 72; 71, 73; 72, identifier:AttributeError; 73, argument_list; 73, 74; 74, binary_operator:%; 74, 75; 74, 76; 75, string:"plugin name must be a str, not %s"; 76, call; 76, 77; 76, 78; 77, identifier:type; 78, argument_list; 78, 79; 79, identifier:plugin_name; 80, if_statement; 80, 81; 80, 102; 81, boolean_operator:and; 81, 82; 81, 91; 82, comparison_operator:not; 82, 83; 82, 84; 83, identifier:plugin_name; 84, call; 84, 85; 84, 90; 85, attribute; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:_plugins; 89, identifier:keys; 90, argument_list; 91, comparison_operator:in; 91, 92; 91, 93; 92, identifier:plugin_name; 93, call; 93, 94; 93, 101; 94, attribute; 94, 95; 94, 100; 95, attribute; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:classes; 99, identifier:_classes; 100, identifier:keys; 101, argument_list; 102, block; 102, 103; 102, 112; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 110; 105, attribute; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:_log; 109, identifier:debug; 110, argument_list; 110, 111; 111, string:"Initialisation needed before activation."; 112, try_statement; 112, 113; 112, 122; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:initialise_by_names; 119, argument_list; 119, 120; 120, list:[plugin_name]; 120, 121; 121, identifier:plugin_name; 122, except_clause; 122, 123; 122, 127; 123, as_pattern; 123, 124; 123, 125; 124, identifier:Exception; 125, as_pattern_target; 125, 126; 126, identifier:e; 127, block; 127, 128; 127, 141; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_log; 134, identifier:error; 135, argument_list; 135, 136; 136, binary_operator:%; 136, 137; 136, 138; 137, string:"Couldn't initialise plugin %s. Reason %s"; 138, tuple; 138, 139; 138, 140; 139, identifier:plugin_name; 140, identifier:e; 141, if_statement; 141, 142; 141, 147; 141, 178; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:_app; 146, identifier:strict; 147, block; 147, 148; 147, 154; 147, 169; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:error; 151, binary_operator:%; 151, 152; 151, 153; 152, string:"Couldn't initialise plugin %s"; 153, identifier:plugin_name; 154, if_statement; 154, 155; 154, 162; 155, comparison_operator:<; 155, 156; 155, 161; 156, subscript; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:sys; 159, identifier:version_info; 160, integer:0; 161, integer:3; 162, block; 162, 163; 163, expression_statement; 163, 164; 164, augmented_assignment:+=; 164, 165; 164, 166; 165, identifier:error; 166, binary_operator:%; 166, 167; 166, 168; 167, string:"Reason: %s"; 168, identifier:e; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 172; 171, identifier:raise_from; 172, argument_list; 172, 173; 172, 177; 173, call; 173, 174; 173, 175; 174, identifier:Exception; 175, argument_list; 175, 176; 176, identifier:error; 177, identifier:e; 178, else_clause; 178, 179; 179, block; 179, 180; 180, continue_statement; 181, if_statement; 181, 182; 181, 191; 182, comparison_operator:in; 182, 183; 182, 184; 183, identifier:plugin_name; 184, call; 184, 185; 184, 190; 185, attribute; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:_plugins; 189, identifier:keys; 190, argument_list; 191, block; 191, 192; 191, 203; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 199; 194, attribute; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:_log; 198, identifier:debug; 199, argument_list; 199, 200; 200, binary_operator:%; 200, 201; 200, 202; 201, string:"Activating plugin %s"; 202, identifier:plugin_name; 203, if_statement; 203, 204; 203, 212; 203, 264; 204, not_operator; 204, 205; 205, attribute; 205, 206; 205, 211; 206, subscript; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:_plugins; 210, identifier:plugin_name; 211, identifier:active; 212, block; 212, 213; 213, try_statement; 213, 214; 213, 225; 213, 244; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, call; 216, 217; 216, 224; 217, attribute; 217, 218; 217, 223; 218, subscript; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:_plugins; 222, identifier:plugin_name; 223, identifier:activate; 224, argument_list; 225, except_clause; 225, 226; 225, 230; 226, as_pattern; 226, 227; 226, 228; 227, identifier:Exception; 228, as_pattern_target; 228, 229; 229, identifier:e; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 234; 233, identifier:raise_from; 234, argument_list; 234, 235; 234, 243; 235, call; 235, 236; 235, 237; 236, identifier:PluginNotActivatableException; 237, argument_list; 237, 238; 238, binary_operator:%; 238, 239; 238, 240; 239, string:"Plugin %s could not be activated: %s"; 240, tuple; 240, 241; 240, 242; 241, identifier:plugin_name; 242, identifier:e; 243, identifier:e; 244, else_clause; 244, 245; 245, block; 245, 246; 245, 257; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 253; 248, attribute; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:_log; 252, identifier:debug; 253, argument_list; 253, 254; 254, binary_operator:%; 254, 255; 254, 256; 255, string:"Plugin %s activated"; 256, identifier:plugin_name; 257, expression_statement; 257, 258; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:plugins_activated; 261, identifier:append; 262, argument_list; 262, 263; 263, identifier:plugin_name; 264, else_clause; 264, 265; 265, block; 265, 266; 265, 277; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 273; 268, attribute; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:self; 271, identifier:_log; 272, identifier:warning; 273, argument_list; 273, 274; 274, binary_operator:%; 274, 275; 274, 276; 275, string:"Plugin %s got already activated."; 276, identifier:plugin_name; 277, if_statement; 277, 278; 277, 283; 278, attribute; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:_app; 282, identifier:strict; 283, block; 283, 284; 284, raise_statement; 284, 285; 285, call; 285, 286; 285, 287; 286, identifier:PluginNotInitialisableException; 287, argument_list; 288, expression_statement; 288, 289; 289, call; 289, 290; 289, 295; 290, attribute; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:self; 293, identifier:_log; 294, identifier:info; 295, argument_list; 295, 296; 296, binary_operator:%; 296, 297; 296, 298; 297, string:"Plugins activated: %s"; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, string:", "; 301, identifier:join; 302, argument_list; 302, 303; 303, identifier:plugins_activated
def activate(self, plugins=[]): """ Activates given plugins. This calls mainly plugin.activate() and plugins register needed resources like commands, signals or documents. If given plugins have not been initialised, this is also done via :func:`_load`. :param plugins: List of plugin names :type plugins: list of strings """ self._log.debug("Plugins Activation started") if not isinstance(plugins, list): raise AttributeError("plugins must be a list, not %s" % type(plugins)) self._log.debug("Plugins to activate: %s" % ", ".join(plugins)) plugins_activated = [] for plugin_name in plugins: if not isinstance(plugin_name, str): raise AttributeError("plugin name must be a str, not %s" % type(plugin_name)) if plugin_name not in self._plugins.keys() and plugin_name in self.classes._classes.keys(): self._log.debug("Initialisation needed before activation.") try: self.initialise_by_names([plugin_name]) except Exception as e: self._log.error("Couldn't initialise plugin %s. Reason %s" % (plugin_name, e)) if self._app.strict: error = "Couldn't initialise plugin %s" % plugin_name if sys.version_info[0] < 3: error += "Reason: %s" % e raise_from(Exception(error), e) else: continue if plugin_name in self._plugins.keys(): self._log.debug("Activating plugin %s" % plugin_name) if not self._plugins[plugin_name].active: try: self._plugins[plugin_name].activate() except Exception as e: raise_from( PluginNotActivatableException("Plugin %s could not be activated: %s" % (plugin_name, e)), e) else: self._log.debug("Plugin %s activated" % plugin_name) plugins_activated.append(plugin_name) else: self._log.warning("Plugin %s got already activated." % plugin_name) if self._app.strict: raise PluginNotInitialisableException() self._log.info("Plugins activated: %s" % ", ".join(plugins_activated))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:dispersion_ranking_NN; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:nnm; 6, default_parameter; 6, 7; 6, 8; 7, identifier:num_norm_avg; 8, integer:50; 9, block; 9, 10; 9, 12; 9, 19; 9, 29; 9, 43; 9, 54; 9, 72; 9, 86; 9, 104; 9, 112; 9, 130; 9, 140; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:knn_avg; 17, argument_list; 17, 18; 18, identifier:nnm; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:D_avg; 22, subscript; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:adata; 27, identifier:layers; 28, string:'X_knn_avg'; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, pattern_list; 31, 32; 31, 33; 32, identifier:mu; 33, identifier:var; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:sf; 37, identifier:mean_variance_axis; 38, argument_list; 38, 39; 38, 40; 39, identifier:D_avg; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:axis; 42, integer:0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:dispersions; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:np; 49, identifier:zeros; 50, argument_list; 50, 51; 51, attribute; 51, 52; 51, 53; 52, identifier:var; 53, identifier:size; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 61; 56, subscript; 56, 57; 56, 58; 57, identifier:dispersions; 58, comparison_operator:>; 58, 59; 58, 60; 59, identifier:mu; 60, integer:0; 61, binary_operator:/; 61, 62; 61, 67; 62, subscript; 62, 63; 62, 64; 63, identifier:var; 64, comparison_operator:>; 64, 65; 64, 66; 65, identifier:mu; 66, integer:0; 67, subscript; 67, 68; 67, 69; 68, identifier:mu; 69, comparison_operator:>; 69, 70; 69, 71; 70, identifier:mu; 71, integer:0; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 81; 74, subscript; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:adata; 79, identifier:var; 80, string:'spatial_dispersions'; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:dispersions; 84, identifier:copy; 85, argument_list; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:ma; 89, call; 89, 90; 89, 103; 90, attribute; 90, 91; 90, 102; 91, subscript; 91, 92; 91, 98; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:np; 95, identifier:sort; 96, argument_list; 96, 97; 97, identifier:dispersions; 98, slice; 98, 99; 98, 101; 99, unary_operator:-; 99, 100; 100, identifier:num_norm_avg; 101, colon; 102, identifier:mean; 103, argument_list; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 111; 106, subscript; 106, 107; 106, 108; 107, identifier:dispersions; 108, comparison_operator:>=; 108, 109; 108, 110; 109, identifier:dispersions; 110, identifier:ma; 111, identifier:ma; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:weights; 115, call; 115, 116; 115, 129; 116, attribute; 116, 117; 116, 128; 117, parenthesized_expression; 117, 118; 118, binary_operator:**; 118, 119; 118, 127; 119, parenthesized_expression; 119, 120; 120, binary_operator:/; 120, 121; 120, 122; 121, identifier:dispersions; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:dispersions; 125, identifier:max; 126, argument_list; 127, float:0.5; 128, identifier:flatten; 129, argument_list; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 139; 132, subscript; 132, 133; 132, 138; 133, attribute; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:adata; 137, identifier:var; 138, string:'weights'; 139, identifier:weights; 140, return_statement; 140, 141; 141, identifier:weights
def dispersion_ranking_NN(self, nnm, num_norm_avg=50): """Computes the spatial dispersion factors for each gene. Parameters ---------- nnm - scipy.sparse, float Square cell-to-cell nearest-neighbor matrix. num_norm_avg - int, optional, default 50 The top 'num_norm_avg' dispersions are averaged to determine the normalization factor when calculating the weights. This ensures that outlier genes do not significantly skew the weight distribution. Returns: ------- indices - ndarray, int The indices corresponding to the gene weights sorted in decreasing order. weights - ndarray, float The vector of gene weights. """ self.knn_avg(nnm) D_avg = self.adata.layers['X_knn_avg'] mu, var = sf.mean_variance_axis(D_avg, axis=0) dispersions = np.zeros(var.size) dispersions[mu > 0] = var[mu > 0] / mu[mu > 0] self.adata.var['spatial_dispersions'] = dispersions.copy() ma = np.sort(dispersions)[-num_norm_avg:].mean() dispersions[dispersions >= ma] = ma weights = ((dispersions / dispersions.max())**0.5).flatten() self.adata.var['weights'] = weights return weights
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 31; 2, function_name:scatter; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:projection; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:c; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:cmap; 13, string:'rainbow'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:linewidth; 16, float:0.0; 17, default_parameter; 17, 18; 17, 19; 18, identifier:edgecolor; 19, string:'k'; 20, default_parameter; 20, 21; 20, 22; 21, identifier:axes; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:colorbar; 25, True; 26, default_parameter; 26, 27; 26, 28; 27, identifier:s; 28, integer:10; 29, dictionary_splat_pattern; 29, 30; 30, identifier:kwargs; 31, block; 31, 32; 31, 34; 32, expression_statement; 32, 33; 33, comment; 34, if_statement; 34, 35; 34, 38; 34, 44; 35, parenthesized_expression; 35, 36; 36, not_operator; 36, 37; 37, identifier:PLOTTING; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:print; 42, argument_list; 42, 43; 43, string:"matplotlib not installed!"; 44, else_clause; 44, 45; 45, block; 45, 46; 45, 126; 45, 146; 46, if_statement; 46, 47; 46, 53; 46, 76; 46, 120; 47, parenthesized_expression; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:isinstance; 50, argument_list; 50, 51; 50, 52; 51, identifier:projection; 52, identifier:str; 53, block; 53, 54; 54, try_statement; 54, 55; 54, 66; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:dt; 59, subscript; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:adata; 64, identifier:obsm; 65, identifier:projection; 66, except_clause; 66, 67; 66, 68; 67, identifier:KeyError; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:print; 72, argument_list; 72, 73; 73, concatenated_string; 73, 74; 73, 75; 74, string:'Please create a projection first using run_umap or'; 75, string:'run_tsne'; 76, elif_clause; 76, 77; 76, 81; 77, parenthesized_expression; 77, 78; 78, comparison_operator:is; 78, 79; 78, 80; 79, identifier:projection; 80, None; 81, block; 81, 82; 82, try_statement; 82, 83; 82, 94; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:dt; 87, subscript; 87, 88; 87, 93; 88, attribute; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:adata; 92, identifier:obsm; 93, string:'X_umap'; 94, except_clause; 94, 95; 94, 96; 95, identifier:KeyError; 96, block; 96, 97; 97, try_statement; 97, 98; 97, 109; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:dt; 102, subscript; 102, 103; 102, 108; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:adata; 107, identifier:obsm; 108, string:'X_tsne'; 109, except_clause; 109, 110; 109, 111; 110, identifier:KeyError; 111, block; 111, 112; 111, 119; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:print; 115, argument_list; 115, 116; 116, concatenated_string; 116, 117; 116, 118; 117, string:"Please create either a t-SNE or UMAP projection"; 118, string:"first."; 119, return_statement; 120, else_clause; 120, 121; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:dt; 125, identifier:projection; 126, if_statement; 126, 127; 126, 131; 127, parenthesized_expression; 127, 128; 128, comparison_operator:is; 128, 129; 128, 130; 129, identifier:axes; 130, None; 131, block; 131, 132; 131, 138; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:plt; 136, identifier:figure; 137, argument_list; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:axes; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:plt; 144, identifier:gca; 145, argument_list; 146, if_statement; 146, 147; 146, 151; 146, 179; 147, parenthesized_expression; 147, 148; 148, comparison_operator:is; 148, 149; 148, 150; 149, identifier:c; 150, None; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:plt; 156, identifier:scatter; 157, argument_list; 157, 158; 157, 163; 157, 168; 157, 171; 157, 174; 157, 177; 158, subscript; 158, 159; 158, 160; 158, 162; 159, identifier:dt; 160, slice; 160, 161; 161, colon; 162, integer:0; 163, subscript; 163, 164; 163, 165; 163, 167; 164, identifier:dt; 165, slice; 165, 166; 166, colon; 167, integer:1; 168, keyword_argument; 168, 169; 168, 170; 169, identifier:s; 170, identifier:s; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:linewidth; 173, identifier:linewidth; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:edgecolor; 176, identifier:edgecolor; 177, dictionary_splat; 177, 178; 178, identifier:kwargs; 179, else_clause; 179, 180; 180, block; 180, 181; 180, 210; 181, if_statement; 181, 182; 181, 187; 182, call; 182, 183; 182, 184; 183, identifier:isinstance; 184, argument_list; 184, 185; 184, 186; 185, identifier:c; 186, identifier:str; 187, block; 187, 188; 188, try_statement; 188, 189; 188, 204; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:c; 193, call; 193, 194; 193, 203; 194, attribute; 194, 195; 194, 202; 195, subscript; 195, 196; 195, 201; 196, attribute; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:self; 199, identifier:adata; 200, identifier:obs; 201, identifier:c; 202, identifier:get_values; 203, argument_list; 204, except_clause; 204, 205; 204, 206; 205, identifier:KeyError; 206, block; 206, 207; 206, 209; 207, expression_statement; 207, 208; 208, integer:0; 209, comment; 210, if_statement; 210, 211; 210, 245; 210, 334; 211, parenthesized_expression; 211, 212; 212, boolean_operator:and; 212, 213; 212, 231; 213, parenthesized_expression; 213, 214; 214, boolean_operator:or; 214, 215; 214, 222; 215, call; 215, 216; 215, 217; 216, identifier:isinstance; 217, argument_list; 217, 218; 217, 221; 218, subscript; 218, 219; 218, 220; 219, identifier:c; 220, integer:0; 221, identifier:str; 222, call; 222, 223; 222, 224; 223, identifier:isinstance; 224, argument_list; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:c; 227, integer:0; 228, attribute; 228, 229; 228, 230; 229, identifier:np; 230, identifier:str_; 231, parenthesized_expression; 231, 232; 232, boolean_operator:or; 232, 233; 232, 240; 233, call; 233, 234; 233, 235; 234, identifier:isinstance; 235, argument_list; 235, 236; 235, 237; 236, identifier:c; 237, attribute; 237, 238; 237, 239; 238, identifier:np; 239, identifier:ndarray; 240, call; 240, 241; 240, 242; 241, identifier:isinstance; 242, argument_list; 242, 243; 242, 244; 243, identifier:c; 244, identifier:list; 245, block; 245, 246; 245, 255; 245, 269; 245, 304; 246, expression_statement; 246, 247; 247, assignment; 247, 248; 247, 249; 248, identifier:i; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:ut; 252, identifier:convert_annotations; 253, argument_list; 253, 254; 254, identifier:c; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 260; 257, pattern_list; 257, 258; 257, 259; 258, identifier:ui; 259, identifier:ai; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:np; 263, identifier:unique; 264, argument_list; 264, 265; 264, 266; 265, identifier:i; 266, keyword_argument; 266, 267; 266, 268; 267, identifier:return_index; 268, True; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 271, identifier:cax; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:axes; 275, identifier:scatter; 276, argument_list; 276, 277; 276, 282; 276, 287; 276, 290; 276, 293; 276, 296; 276, 299; 276, 302; 277, subscript; 277, 278; 277, 279; 277, 281; 278, identifier:dt; 279, slice; 279, 280; 280, colon; 281, integer:0; 282, subscript; 282, 283; 282, 284; 282, 286; 283, identifier:dt; 284, slice; 284, 285; 285, colon; 286, integer:1; 287, keyword_argument; 287, 288; 287, 289; 288, identifier:c; 289, identifier:i; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:cmap; 292, identifier:cmap; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:s; 295, identifier:s; 296, keyword_argument; 296, 297; 296, 298; 297, identifier:linewidth; 298, identifier:linewidth; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:edgecolor; 301, identifier:edgecolor; 302, dictionary_splat; 302, 303; 303, identifier:kwargs; 304, if_statement; 304, 305; 304, 307; 305, parenthesized_expression; 305, 306; 306, identifier:colorbar; 307, block; 307, 308; 307, 323; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:cbar; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:plt; 314, identifier:colorbar; 315, argument_list; 315, 316; 315, 317; 315, 320; 316, identifier:cax; 317, keyword_argument; 317, 318; 317, 319; 318, identifier:ax; 319, identifier:axes; 320, keyword_argument; 320, 321; 320, 322; 321, identifier:ticks; 322, identifier:ui; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:cbar; 328, identifier:ax; 329, identifier:set_yticklabels; 330, argument_list; 330, 331; 331, subscript; 331, 332; 331, 333; 332, identifier:c; 333, identifier:ai; 334, else_clause; 334, 335; 335, block; 335, 336; 335, 357; 335, 361; 335, 396; 336, if_statement; 336, 337; 336, 352; 337, not_operator; 337, 338; 338, parenthesized_expression; 338, 339; 339, boolean_operator:or; 339, 340; 339, 347; 340, call; 340, 341; 340, 342; 341, identifier:isinstance; 342, argument_list; 342, 343; 342, 344; 343, identifier:c; 344, attribute; 344, 345; 344, 346; 345, identifier:np; 346, identifier:ndarray; 347, call; 347, 348; 347, 349; 348, identifier:isinstance; 349, argument_list; 349, 350; 349, 351; 350, identifier:c; 351, identifier:list; 352, block; 352, 353; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 356; 355, identifier:colorbar; 356, False; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 360; 359, identifier:i; 360, identifier:c; 361, expression_statement; 361, 362; 362, assignment; 362, 363; 362, 364; 363, identifier:cax; 364, call; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:axes; 367, identifier:scatter; 368, argument_list; 368, 369; 368, 374; 368, 379; 368, 382; 368, 385; 368, 388; 368, 391; 368, 394; 369, subscript; 369, 370; 369, 371; 369, 373; 370, identifier:dt; 371, slice; 371, 372; 372, colon; 373, integer:0; 374, subscript; 374, 375; 374, 376; 374, 378; 375, identifier:dt; 376, slice; 376, 377; 377, colon; 378, integer:1; 379, keyword_argument; 379, 380; 379, 381; 380, identifier:c; 381, identifier:i; 382, keyword_argument; 382, 383; 382, 384; 383, identifier:cmap; 384, identifier:cmap; 385, keyword_argument; 385, 386; 385, 387; 386, identifier:s; 387, identifier:s; 388, keyword_argument; 388, 389; 388, 390; 389, identifier:linewidth; 390, identifier:linewidth; 391, keyword_argument; 391, 392; 391, 393; 392, identifier:edgecolor; 393, identifier:edgecolor; 394, dictionary_splat; 394, 395; 395, identifier:kwargs; 396, if_statement; 396, 397; 396, 399; 397, parenthesized_expression; 397, 398; 398, identifier:colorbar; 399, block; 399, 400; 400, expression_statement; 400, 401; 401, call; 401, 402; 401, 405; 402, attribute; 402, 403; 402, 404; 403, identifier:plt; 404, identifier:colorbar; 405, argument_list; 405, 406; 405, 407; 406, identifier:cax; 407, keyword_argument; 407, 408; 407, 409; 408, identifier:ax; 409, identifier:axes
def scatter(self, projection=None, c=None, cmap='rainbow', linewidth=0.0, edgecolor='k', axes=None, colorbar=True, s=10, **kwargs): """Display a scatter plot. Displays a scatter plot using the SAM projection or another input projection with or without annotations. Parameters ---------- projection - ndarray of floats, optional, default None An N x 2 matrix, where N is the number of data points. If None, use an existing SAM projection (default t-SNE). Can take on values 'umap' or 'tsne' to specify either the SAM UMAP embedding or SAM t-SNE embedding. c - ndarray or str, optional, default None Colors for each cell in the scatter plot. Can be a vector of floats or strings for cell annotations. Can also be a key for sam.adata.obs (i.e. 'louvain_clusters'). axes - matplotlib axis, optional, default None Plot output to the specified, existing axes. If None, create new figure window. cmap - string, optional, default 'rainbow' The colormap to use for the input color values. colorbar - bool, optional default True If True, display a colorbar indicating which values / annotations correspond to which color in the scatter plot. Keyword arguments - All other keyword arguments that can be passed into matplotlib.pyplot.scatter can be used. """ if (not PLOTTING): print("matplotlib not installed!") else: if(isinstance(projection, str)): try: dt = self.adata.obsm[projection] except KeyError: print('Please create a projection first using run_umap or' 'run_tsne') elif(projection is None): try: dt = self.adata.obsm['X_umap'] except KeyError: try: dt = self.adata.obsm['X_tsne'] except KeyError: print("Please create either a t-SNE or UMAP projection" "first.") return else: dt = projection if(axes is None): plt.figure() axes = plt.gca() if(c is None): plt.scatter(dt[:, 0], dt[:, 1], s=s, linewidth=linewidth, edgecolor=edgecolor, **kwargs) else: if isinstance(c, str): try: c = self.adata.obs[c].get_values() except KeyError: 0 # do nothing if((isinstance(c[0], str) or isinstance(c[0], np.str_)) and (isinstance(c, np.ndarray) or isinstance(c, list))): i = ut.convert_annotations(c) ui, ai = np.unique(i, return_index=True) cax = axes.scatter(dt[:,0], dt[:,1], c=i, cmap=cmap, s=s, linewidth=linewidth, edgecolor=edgecolor, **kwargs) if(colorbar): cbar = plt.colorbar(cax, ax=axes, ticks=ui) cbar.ax.set_yticklabels(c[ai]) else: if not (isinstance(c, np.ndarray) or isinstance(c, list)): colorbar = False i = c cax = axes.scatter(dt[:,0], dt[:,1], c=i, cmap=cmap, s=s, linewidth=linewidth, edgecolor=edgecolor, **kwargs) if(colorbar): plt.colorbar(cax, ax=axes)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:parseFloat; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:words; 6, block; 6, 7; 6, 9; 6, 88; 6, 243; 6, 244; 6, 251; 6, 256; 6, 257; 6, 264; 6, 269; 6, 270; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:pointFloat; 11, parameters; 11, 12; 12, identifier:words; 13, block; 13, 14; 13, 24; 13, 86; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:m; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:re; 20, identifier:search; 21, argument_list; 21, 22; 21, 23; 22, string:r'(.*) point (.*)'; 23, identifier:words; 24, if_statement; 24, 25; 24, 26; 25, identifier:m; 26, block; 26, 27; 26, 36; 26, 45; 26, 49; 26, 53; 26, 77; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:whole; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:m; 33, identifier:group; 34, argument_list; 34, 35; 35, integer:1; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:frac; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:m; 42, identifier:group; 43, argument_list; 43, 44; 44, integer:2; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:total; 48, float:0.0; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:coeff; 52, float:0.10; 53, for_statement; 53, 54; 53, 55; 53, 61; 54, identifier:digit; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:frac; 58, identifier:split; 59, argument_list; 59, 60; 60, string:' '; 61, block; 61, 62; 61, 73; 62, expression_statement; 62, 63; 63, augmented_assignment:+=; 63, 64; 63, 65; 64, identifier:total; 65, binary_operator:*; 65, 66; 65, 67; 66, identifier:coeff; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:parse; 71, argument_list; 71, 72; 72, identifier:digit; 73, expression_statement; 73, 74; 74, augmented_assignment:/=; 74, 75; 74, 76; 75, identifier:coeff; 76, float:10.0; 77, return_statement; 77, 78; 78, binary_operator:+; 78, 79; 78, 85; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:parseInt; 83, argument_list; 83, 84; 84, identifier:whole; 85, identifier:total; 86, return_statement; 86, 87; 87, None; 88, function_definition; 88, 89; 88, 90; 88, 92; 89, function_name:fractionFloat; 90, parameters; 90, 91; 91, identifier:words; 92, block; 92, 93; 92, 103; 92, 241; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:m; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:re; 99, identifier:search; 100, argument_list; 100, 101; 100, 102; 101, string:r'(.*) and (.*)'; 102, identifier:words; 103, if_statement; 103, 104; 103, 105; 104, identifier:m; 105, block; 105, 106; 105, 120; 105, 129; 105, 130; 105, 141; 105, 142; 105, 153; 105, 162; 105, 163; 105, 171; 105, 179; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:whole; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:parseInt; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:m; 117, identifier:group; 118, argument_list; 118, 119; 119, integer:1; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:frac; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:m; 126, identifier:group; 127, argument_list; 127, 128; 128, integer:2; 129, comment; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:frac; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:re; 136, identifier:sub; 137, argument_list; 137, 138; 137, 139; 137, 140; 138, string:r'(\w+)s(\b)'; 139, string:'\g<1>\g<2>'; 140, identifier:frac; 141, comment; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:frac; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:re; 148, identifier:sub; 149, argument_list; 149, 150; 149, 151; 149, 152; 150, string:r'(\b)a(\b)'; 151, string:'\g<1>one\g<2>'; 152, identifier:frac; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:split; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:frac; 159, identifier:split; 160, argument_list; 160, 161; 161, string:' '; 162, comment; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:num; 166, subscript; 166, 167; 166, 168; 167, identifier:split; 168, slice; 168, 169; 168, 170; 169, colon; 170, integer:1; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:denom; 174, subscript; 174, 175; 174, 176; 175, identifier:split; 176, slice; 176, 177; 176, 178; 177, integer:1; 178, colon; 179, while_statement; 179, 180; 179, 181; 180, identifier:denom; 181, block; 181, 182; 182, try_statement; 182, 183; 182, 184; 182, 222; 183, comment; 184, block; 184, 185; 184, 199; 184, 213; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:num_value; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:self; 191, identifier:parse; 192, argument_list; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, string:' '; 196, identifier:join; 197, argument_list; 197, 198; 198, identifier:num; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:denom_value; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:parse; 206, argument_list; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, string:' '; 210, identifier:join; 211, argument_list; 211, 212; 212, identifier:denom; 213, return_statement; 213, 214; 214, binary_operator:+; 214, 215; 214, 216; 215, identifier:whole; 216, binary_operator:/; 216, 217; 216, 221; 217, call; 217, 218; 217, 219; 218, identifier:float; 219, argument_list; 219, 220; 220, identifier:num_value; 221, identifier:denom_value; 222, except_clause; 222, 223; 222, 224; 223, comment; 224, block; 224, 225; 224, 233; 225, expression_statement; 225, 226; 226, augmented_assignment:+=; 226, 227; 226, 228; 227, identifier:num; 228, subscript; 228, 229; 228, 230; 229, identifier:denom; 230, slice; 230, 231; 230, 232; 231, colon; 232, integer:1; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:denom; 236, subscript; 236, 237; 236, 238; 237, identifier:denom; 238, slice; 238, 239; 238, 240; 239, integer:1; 240, colon; 241, return_statement; 241, 242; 242, None; 243, comment; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:result; 247, call; 247, 248; 247, 249; 248, identifier:pointFloat; 249, argument_list; 249, 250; 250, identifier:words; 251, if_statement; 251, 252; 251, 253; 252, identifier:result; 253, block; 253, 254; 254, return_statement; 254, 255; 255, identifier:result; 256, comment; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:result; 260, call; 260, 261; 260, 262; 261, identifier:fractionFloat; 262, argument_list; 262, 263; 263, identifier:words; 264, if_statement; 264, 265; 264, 266; 265, identifier:result; 266, block; 266, 267; 267, return_statement; 267, 268; 268, identifier:result; 269, comment; 270, return_statement; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:parseInt; 275, argument_list; 275, 276; 276, identifier:words
def parseFloat(self, words): """Convert a floating-point number described in words to a double. Supports two kinds of descriptions: those with a 'point' (e.g., "one point two five") and those with a fraction (e.g., "one and a quarter"). Args: words (str): Description of the floating-point number. Returns: A double representation of the words. """ def pointFloat(words): m = re.search(r'(.*) point (.*)', words) if m: whole = m.group(1) frac = m.group(2) total = 0.0 coeff = 0.10 for digit in frac.split(' '): total += coeff * self.parse(digit) coeff /= 10.0 return self.parseInt(whole) + total return None def fractionFloat(words): m = re.search(r'(.*) and (.*)', words) if m: whole = self.parseInt(m.group(1)) frac = m.group(2) # Replace plurals frac = re.sub(r'(\w+)s(\b)', '\g<1>\g<2>', frac) # Convert 'a' to 'one' (e.g., 'a third' to 'one third') frac = re.sub(r'(\b)a(\b)', '\g<1>one\g<2>', frac) split = frac.split(' ') # Split fraction into num (regular integer), denom (ordinal) num = split[:1] denom = split[1:] while denom: try: # Test for valid num, denom num_value = self.parse(' '.join(num)) denom_value = self.parse(' '.join(denom)) return whole + float(num_value) / denom_value except: # Add another word to num num += denom[:1] denom = denom[1:] return None # Extract "one point two five"-type float result = pointFloat(words) if result: return result # Extract "one and a quarter"-type float result = fractionFloat(words) if result: return result # Parse as integer return self.parseInt(words)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:run; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:cmd; 6, default_parameter; 6, 7; 6, 8; 7, identifier:stdin; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:marshal_output; 11, True; 12, dictionary_splat_pattern; 12, 13; 13, identifier:kwargs; 14, block; 14, 15; 14, 17; 14, 21; 14, 36; 14, 52; 14, 62; 14, 76; 14, 80; 14, 89; 14, 93; 14, 116; 14, 145; 14, 162; 14, 314; 14, 325; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:records; 20, list:[]; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:args; 24, list:[self._executable, "-u", self._user, "-p", self._port]; 24, 25; 24, 28; 24, 29; 24, 32; 24, 33; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:_executable; 28, string:"-u"; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_user; 32, string:"-p"; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:_port; 36, if_statement; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_client; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, augmented_assignment:+=; 42, 43; 42, 44; 43, identifier:args; 44, list:["-c", str(self._client)]; 44, 45; 44, 46; 45, string:"-c"; 46, call; 46, 47; 46, 48; 47, identifier:str; 48, argument_list; 48, 49; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:_client; 52, if_statement; 52, 53; 52, 54; 53, identifier:marshal_output; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:args; 59, identifier:append; 60, argument_list; 60, 61; 61, string:'-G'; 62, if_statement; 62, 63; 62, 70; 63, call; 63, 64; 63, 65; 64, identifier:isinstance; 65, argument_list; 65, 66; 65, 67; 66, identifier:cmd; 67, attribute; 67, 68; 67, 69; 68, identifier:six; 69, identifier:string_types; 70, block; 70, 71; 71, raise_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:ValueError; 74, argument_list; 74, 75; 75, string:'String commands are not supported, please use a list'; 76, expression_statement; 76, 77; 77, augmented_assignment:+=; 77, 78; 77, 79; 78, identifier:args; 79, identifier:cmd; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:command; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, string:' '; 86, identifier:join; 87, argument_list; 87, 88; 88, identifier:args; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:startupinfo; 92, None; 93, if_statement; 93, 94; 93, 99; 94, comparison_operator:==; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:os; 97, identifier:name; 98, string:'nt'; 99, block; 99, 100; 99, 108; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:startupinfo; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:subprocess; 106, identifier:STARTUPINFO; 107, argument_list; 108, expression_statement; 108, 109; 109, augmented_assignment:|=; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:startupinfo; 112, identifier:dwFlags; 113, attribute; 113, 114; 113, 115; 114, identifier:subprocess; 115, identifier:STARTF_USESHOWWINDOW; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:proc; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:subprocess; 122, identifier:Popen; 123, argument_list; 123, 124; 123, 125; 123, 130; 123, 135; 123, 140; 123, 143; 124, identifier:args; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:stdin; 127, attribute; 127, 128; 127, 129; 128, identifier:subprocess; 129, identifier:PIPE; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:stdout; 132, attribute; 132, 133; 132, 134; 133, identifier:subprocess; 134, identifier:PIPE; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:stderr; 137, attribute; 137, 138; 137, 139; 138, identifier:subprocess; 139, identifier:PIPE; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:startupinfo; 142, identifier:startupinfo; 143, dictionary_splat; 143, 144; 144, identifier:kwargs; 145, if_statement; 145, 146; 145, 147; 146, identifier:stdin; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 155; 150, attribute; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:proc; 153, identifier:stdin; 154, identifier:write; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:six; 159, identifier:b; 160, argument_list; 160, 161; 161, identifier:stdin; 162, if_statement; 162, 163; 162, 164; 162, 302; 163, identifier:marshal_output; 164, block; 164, 165; 164, 292; 165, try_statement; 165, 166; 165, 288; 166, block; 166, 167; 167, while_statement; 167, 168; 167, 169; 168, True; 169, block; 169, 170; 169, 181; 169, 227; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:record; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:marshal; 176, identifier:load; 177, argument_list; 177, 178; 178, attribute; 178, 179; 178, 180; 179, identifier:proc; 180, identifier:stdout; 181, if_statement; 181, 182; 181, 199; 182, boolean_operator:and; 182, 183; 182, 192; 183, comparison_operator:==; 183, 184; 183, 191; 184, call; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:record; 187, identifier:get; 188, argument_list; 188, 189; 188, 190; 189, string:b'code'; 190, string:''; 191, string:b'error'; 192, comparison_operator:>=; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:record; 195, string:b'severity'; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_level; 199, block; 199, 200; 199, 208; 199, 216; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 207; 202, attribute; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:proc; 205, identifier:stdin; 206, identifier:close; 207, argument_list; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 215; 210, attribute; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:proc; 213, identifier:stdout; 214, identifier:close; 215, argument_list; 216, raise_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:errors; 220, identifier:CommandError; 221, argument_list; 221, 222; 221, 225; 221, 226; 222, subscript; 222, 223; 222, 224; 223, identifier:record; 224, string:b'data'; 225, identifier:record; 226, identifier:command; 227, if_statement; 227, 228; 227, 233; 228, call; 228, 229; 228, 230; 229, identifier:isinstance; 230, argument_list; 230, 231; 230, 232; 231, identifier:record; 232, identifier:dict; 233, block; 233, 234; 234, if_statement; 234, 235; 234, 238; 234, 246; 235, attribute; 235, 236; 235, 237; 236, identifier:six; 237, identifier:PY2; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:records; 243, identifier:append; 244, argument_list; 244, 245; 245, identifier:record; 246, else_clause; 246, 247; 247, block; 247, 248; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:records; 252, identifier:append; 253, argument_list; 253, 254; 254, dictionary_comprehension; 254, 255; 254, 279; 255, pair; 255, 256; 255, 261; 256, call; 256, 257; 256, 258; 257, identifier:str; 258, argument_list; 258, 259; 258, 260; 259, identifier:k; 260, string:'utf8'; 261, conditional_expression:if; 261, 262; 261, 266; 261, 271; 262, call; 262, 263; 262, 264; 263, identifier:str; 264, argument_list; 264, 265; 265, identifier:v; 266, call; 266, 267; 266, 268; 267, identifier:isinstance; 268, argument_list; 268, 269; 268, 270; 269, identifier:v; 270, identifier:int; 271, call; 271, 272; 271, 273; 272, identifier:str; 273, argument_list; 273, 274; 273, 275; 273, 276; 274, identifier:v; 275, string:'utf8'; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:errors; 278, string:'ignore'; 279, for_in_clause; 279, 280; 279, 283; 280, pattern_list; 280, 281; 280, 282; 281, identifier:k; 282, identifier:v; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:record; 286, identifier:items; 287, argument_list; 288, except_clause; 288, 289; 288, 290; 289, identifier:EOFError; 290, block; 290, 291; 291, pass_statement; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 297; 294, pattern_list; 294, 295; 294, 296; 295, identifier:stdout; 296, identifier:stderr; 297, call; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:proc; 300, identifier:communicate; 301, argument_list; 302, else_clause; 302, 303; 303, block; 303, 304; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 309; 306, pattern_list; 306, 307; 306, 308; 307, identifier:records; 308, identifier:stderr; 309, call; 309, 310; 309, 313; 310, attribute; 310, 311; 310, 312; 311, identifier:proc; 312, identifier:communicate; 313, argument_list; 314, if_statement; 314, 315; 314, 316; 315, identifier:stderr; 316, block; 316, 317; 317, raise_statement; 317, 318; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:errors; 321, identifier:CommandError; 322, argument_list; 322, 323; 322, 324; 323, identifier:stderr; 324, identifier:command; 325, return_statement; 325, 326; 326, identifier:records
def run(self, cmd, stdin=None, marshal_output=True, **kwargs): """Runs a p4 command and returns a list of dictionary objects :param cmd: Command to run :type cmd: list :param stdin: Standard Input to send to the process :type stdin: str :param marshal_output: Whether or not to marshal the output from the command :type marshal_output: bool :param kwargs: Passes any other keyword arguments to subprocess :raises: :class:`.error.CommandError` :returns: list, records of results """ records = [] args = [self._executable, "-u", self._user, "-p", self._port] if self._client: args += ["-c", str(self._client)] if marshal_output: args.append('-G') if isinstance(cmd, six.string_types): raise ValueError('String commands are not supported, please use a list') args += cmd command = ' '.join(args) startupinfo = None if os.name == 'nt': startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW proc = subprocess.Popen( args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo, **kwargs ) if stdin: proc.stdin.write(six.b(stdin)) if marshal_output: try: while True: record = marshal.load(proc.stdout) if record.get(b'code', '') == b'error' and record[b'severity'] >= self._level: proc.stdin.close() proc.stdout.close() raise errors.CommandError(record[b'data'], record, command) if isinstance(record, dict): if six.PY2: records.append(record) else: records.append({str(k, 'utf8'): str(v) if isinstance(v, int) else str(v, 'utf8', errors='ignore') for k, v in record.items()}) except EOFError: pass stdout, stderr = proc.communicate() else: records, stderr = proc.communicate() if stderr: raise errors.CommandError(stderr, command) return records
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:canonical_ops; 3, parameters; 3, 4; 4, identifier:ops; 5, block; 5, 6; 5, 8; 5, 30; 6, expression_statement; 6, 7; 7, string:''' Returns the given operations array sorted with duplicates removed. @param ops checker.Ops @return: checker.Ops '''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:new_ops; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 13, 18; 14, call; 14, 15; 14, 16; 15, identifier:set; 16, argument_list; 16, 17; 17, identifier:ops; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:key; 20, lambda; 20, 21; 20, 23; 21, lambda_parameters; 21, 22; 22, identifier:x; 23, tuple; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:x; 26, identifier:entity; 27, attribute; 27, 28; 27, 29; 28, identifier:x; 29, identifier:action; 30, return_statement; 30, 31; 31, identifier:new_ops
def canonical_ops(ops): ''' Returns the given operations array sorted with duplicates removed. @param ops checker.Ops @return: checker.Ops ''' new_ops = sorted(set(ops), key=lambda x: (x.entity, x.action)) return new_ops
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:extract_from_text; 3, parameters; 3, 4; 4, identifier:text; 5, block; 5, 6; 5, 8; 5, 9; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, return_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:tools; 13, identifier:remove_duplicates; 14, argument_list; 14, 15; 15, list_comprehension; 15, 16; 15, 31; 15, 39; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:re; 19, identifier:sub; 20, argument_list; 20, 21; 20, 22; 20, 23; 20, 26; 21, string:"arxiv:"; 22, string:""; 23, subscript; 23, 24; 23, 25; 24, identifier:i; 25, integer:0; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:flags; 28, attribute; 28, 29; 28, 30; 29, identifier:re; 30, identifier:IGNORECASE; 31, for_in_clause; 31, 32; 31, 33; 32, identifier:i; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:REGEX; 36, identifier:findall; 37, argument_list; 37, 38; 38, identifier:text; 39, if_clause; 39, 40; 40, comparison_operator:!=; 40, 41; 40, 44; 41, subscript; 41, 42; 41, 43; 42, identifier:i; 43, integer:0; 44, string:''
def extract_from_text(text): """ Extract arXiv IDs from a text. :param text: The text to extract arXiv IDs from. :returns: A list of matching arXiv IDs, in canonical form. >>> sorted(extract_from_text('1506.06690 1506.06690v1 arXiv:1506.06690 arXiv:1506.06690v1 arxiv:1506.06690 arxiv:1506.06690v1 math.GT/0309136 abcdf bar1506.06690foo mare.GG/0309136')) ['1506.06690', '1506.06690v1', 'math.GT/0309136'] """ # Remove the leading "arxiv:". return tools.remove_duplicates([re.sub("arxiv:", "", i[0], flags=re.IGNORECASE) for i in REGEX.findall(text) if i[0] != ''])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:extractTimes; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:inp; 6, block; 6, 7; 6, 9; 6, 314; 6, 323; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:handleMatch; 11, parameters; 11, 12; 12, identifier:time; 13, block; 13, 14; 13, 18; 13, 270; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:relative; 17, False; 18, if_statement; 18, 19; 18, 21; 18, 24; 18, 25; 18, 43; 18, 61; 18, 79; 18, 208; 19, not_operator; 19, 20; 20, identifier:time; 21, block; 21, 22; 22, return_statement; 22, 23; 23, None; 24, comment; 25, elif_clause; 25, 26; 25, 34; 26, comparison_operator:==; 26, 27; 26, 33; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:time; 30, identifier:group; 31, argument_list; 31, 32; 32, integer:1; 33, string:'morning'; 34, block; 34, 35; 34, 39; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:h; 38, integer:8; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:m; 42, integer:0; 43, elif_clause; 43, 44; 43, 52; 44, comparison_operator:==; 44, 45; 44, 51; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:time; 48, identifier:group; 49, argument_list; 49, 50; 50, integer:1; 51, string:'afternoon'; 52, block; 52, 53; 52, 57; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:h; 56, integer:12; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:m; 60, integer:0; 61, elif_clause; 61, 62; 61, 70; 62, comparison_operator:==; 62, 63; 62, 69; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:time; 66, identifier:group; 67, argument_list; 67, 68; 68, integer:1; 69, string:'evening'; 70, block; 70, 71; 70, 75; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:h; 74, integer:19; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:m; 78, integer:0; 79, elif_clause; 79, 80; 79, 93; 80, boolean_operator:and; 80, 81; 80, 87; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:time; 84, identifier:group; 85, argument_list; 85, 86; 86, integer:4; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:time; 90, identifier:group; 91, argument_list; 91, 92; 92, integer:5; 93, block; 93, 94; 93, 102; 93, 103; 93, 109; 93, 129; 93, 149; 93, 150; 93, 204; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 99; 96, pattern_list; 96, 97; 96, 98; 97, identifier:h; 98, identifier:m; 99, expression_list; 99, 100; 99, 101; 100, integer:0; 101, integer:0; 102, comment; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:converter; 106, call; 106, 107; 106, 108; 107, identifier:NumberService; 108, argument_list; 109, try_statement; 109, 110; 109, 125; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:diff; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:converter; 117, identifier:parse; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:time; 122, identifier:group; 123, argument_list; 123, 124; 124, integer:4; 125, except_clause; 125, 126; 126, block; 126, 127; 127, return_statement; 127, 128; 128, None; 129, if_statement; 129, 130; 129, 138; 129, 143; 130, comparison_operator:==; 130, 131; 130, 137; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:time; 134, identifier:group; 135, argument_list; 135, 136; 136, integer:5; 137, string:'hours'; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, augmented_assignment:+=; 140, 141; 140, 142; 141, identifier:h; 142, identifier:diff; 143, else_clause; 143, 144; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, augmented_assignment:+=; 146, 147; 146, 148; 147, identifier:m; 148, identifier:diff; 149, comment; 150, if_statement; 150, 151; 150, 157; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:time; 154, identifier:group; 155, argument_list; 155, 156; 156, integer:6; 157, block; 157, 158; 157, 164; 157, 184; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:converter; 161, call; 161, 162; 161, 163; 162, identifier:NumberService; 163, argument_list; 164, try_statement; 164, 165; 164, 180; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:diff; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:converter; 172, identifier:parse; 173, argument_list; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:time; 177, identifier:group; 178, argument_list; 178, 179; 179, integer:7; 180, except_clause; 180, 181; 181, block; 181, 182; 182, return_statement; 182, 183; 183, None; 184, if_statement; 184, 185; 184, 193; 184, 198; 185, comparison_operator:==; 185, 186; 185, 192; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:time; 189, identifier:group; 190, argument_list; 190, 191; 191, integer:8; 192, string:'hours'; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, augmented_assignment:+=; 195, 196; 195, 197; 196, identifier:h; 197, identifier:diff; 198, else_clause; 198, 199; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, augmented_assignment:+=; 201, 202; 201, 203; 202, identifier:m; 203, identifier:diff; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:relative; 207, True; 208, else_clause; 208, 209; 208, 210; 209, comment; 210, block; 210, 211; 210, 220; 210, 250; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:t; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:time; 217, identifier:group; 218, argument_list; 218, 219; 219, integer:2; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 225; 222, pattern_list; 222, 223; 222, 224; 223, identifier:h; 224, identifier:m; 225, expression_list; 225, 226; 225, 239; 226, binary_operator:%; 226, 227; 226, 238; 227, call; 227, 228; 227, 229; 228, identifier:int; 229, argument_list; 229, 230; 230, subscript; 230, 231; 230, 237; 231, call; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:t; 234, identifier:split; 235, argument_list; 235, 236; 236, string:':'; 237, integer:0; 238, integer:12; 239, call; 239, 240; 239, 241; 240, identifier:int; 241, argument_list; 241, 242; 242, subscript; 242, 243; 242, 249; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:t; 246, identifier:split; 247, argument_list; 247, 248; 248, string:':'; 249, integer:1; 250, try_statement; 250, 251; 250, 266; 251, block; 251, 252; 252, if_statement; 252, 253; 252, 261; 253, comparison_operator:==; 253, 254; 253, 260; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:time; 257, identifier:group; 258, argument_list; 258, 259; 259, integer:3; 260, string:'pm'; 261, block; 261, 262; 262, expression_statement; 262, 263; 263, augmented_assignment:+=; 263, 264; 263, 265; 264, identifier:h; 265, integer:12; 266, except_clause; 266, 267; 266, 268; 267, identifier:IndexError; 268, block; 268, 269; 269, pass_statement; 270, if_statement; 270, 271; 270, 272; 270, 289; 271, identifier:relative; 272, block; 272, 273; 273, return_statement; 273, 274; 274, binary_operator:+; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:self; 277, identifier:now; 278, call; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:datetime; 281, identifier:timedelta; 282, argument_list; 282, 283; 282, 286; 283, keyword_argument; 283, 284; 283, 285; 284, identifier:hours; 285, identifier:h; 286, keyword_argument; 286, 287; 286, 288; 287, identifier:minutes; 288, identifier:m; 289, else_clause; 289, 290; 290, block; 290, 291; 291, return_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:datetime; 295, identifier:datetime; 296, argument_list; 296, 297; 296, 302; 296, 307; 296, 312; 296, 313; 297, attribute; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:self; 300, identifier:now; 301, identifier:year; 302, attribute; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:self; 305, identifier:now; 306, identifier:month; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:now; 311, identifier:day; 312, identifier:h; 313, identifier:m; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:inp; 317, call; 317, 318; 317, 321; 318, attribute; 318, 319; 318, 320; 319, identifier:self; 320, identifier:_preprocess; 321, argument_list; 321, 322; 322, identifier:inp; 323, return_statement; 323, 324; 324, list_comprehension; 324, 325; 324, 329; 325, call; 325, 326; 325, 327; 326, identifier:handleMatch; 327, argument_list; 327, 328; 328, identifier:time; 329, for_in_clause; 329, 330; 329, 331; 330, identifier:time; 331, call; 331, 332; 331, 337; 332, attribute; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:self; 335, identifier:_timeRegex; 336, identifier:finditer; 337, argument_list; 337, 338; 338, identifier:inp
def extractTimes(self, inp): """Extracts time-related information from an input string. Ignores any information related to the specific date, focusing on the time-of-day. Args: inp (str): Input string to be parsed. Returns: A list of datetime objects containing the extracted times from the input snippet, or an empty list if none found. """ def handleMatch(time): relative = False if not time: return None # Default times: 8am, 12pm, 7pm elif time.group(1) == 'morning': h = 8 m = 0 elif time.group(1) == 'afternoon': h = 12 m = 0 elif time.group(1) == 'evening': h = 19 m = 0 elif time.group(4) and time.group(5): h, m = 0, 0 # Extract hours difference converter = NumberService() try: diff = converter.parse(time.group(4)) except: return None if time.group(5) == 'hours': h += diff else: m += diff # Extract minutes difference if time.group(6): converter = NumberService() try: diff = converter.parse(time.group(7)) except: return None if time.group(8) == 'hours': h += diff else: m += diff relative = True else: # Convert from "HH:MM pm" format t = time.group(2) h, m = int(t.split(':')[0]) % 12, int(t.split(':')[1]) try: if time.group(3) == 'pm': h += 12 except IndexError: pass if relative: return self.now + datetime.timedelta(hours=h, minutes=m) else: return datetime.datetime( self.now.year, self.now.month, self.now.day, h, m ) inp = self._preprocess(inp) return [handleMatch(time) for time in self._timeRegex.finditer(inp)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:serialize_text; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 26; 5, 44; 5, 48; 5, 65; 6, expression_statement; 6, 7; 7, string:'''Returns a serialized form of the Namepace. All the elements in the namespace are sorted by URI, joined to the associated prefix with a colon and separated with spaces. :return: bytes '''; 8, if_statement; 8, 9; 8, 23; 9, boolean_operator:or; 9, 10; 9, 15; 10, comparison_operator:is; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_uri_to_prefix; 14, None; 15, comparison_operator:==; 15, 16; 15, 22; 16, call; 16, 17; 16, 18; 17, identifier:len; 18, argument_list; 18, 19; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_uri_to_prefix; 22, integer:0; 23, block; 23, 24; 24, return_statement; 24, 25; 25, string:b''; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:od; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:collections; 32, identifier:OrderedDict; 33, argument_list; 33, 34; 34, call; 34, 35; 34, 36; 35, identifier:sorted; 36, argument_list; 36, 37; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:_uri_to_prefix; 42, identifier:items; 43, argument_list; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:data; 47, list:[]; 48, for_statement; 48, 49; 48, 50; 48, 51; 49, identifier:uri; 50, identifier:od; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:data; 56, identifier:append; 57, argument_list; 57, 58; 58, binary_operator:+; 58, 59; 58, 62; 59, binary_operator:+; 59, 60; 59, 61; 60, identifier:uri; 61, string:':'; 62, subscript; 62, 63; 62, 64; 63, identifier:od; 64, identifier:uri; 65, return_statement; 65, 66; 66, call; 66, 67; 66, 75; 67, attribute; 67, 68; 67, 74; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, string:' '; 71, identifier:join; 72, argument_list; 72, 73; 73, identifier:data; 74, identifier:encode; 75, argument_list; 75, 76; 76, string:'utf-8'
def serialize_text(self): '''Returns a serialized form of the Namepace. All the elements in the namespace are sorted by URI, joined to the associated prefix with a colon and separated with spaces. :return: bytes ''' if self._uri_to_prefix is None or len(self._uri_to_prefix) == 0: return b'' od = collections.OrderedDict(sorted(self._uri_to_prefix.items())) data = [] for uri in od: data.append(uri + ':' + od[uri]) return ' '.join(data).encode('utf-8')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:with_zero_or_one; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:converter; 6, default_parameter; 6, 7; 6, 8; 7, identifier:pattern; 8, None; 9, block; 9, 10; 9, 12; 9, 18; 9, 33; 9, 42; 9, 51; 9, 81; 9, 87; 9, 88; 9, 94; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:cardinality; 15, attribute; 15, 16; 15, 17; 16, identifier:Cardinality; 17, identifier:zero_or_one; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:pattern; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:pattern; 25, call; 25, 26; 25, 27; 26, identifier:getattr; 27, argument_list; 27, 28; 27, 29; 27, 30; 28, identifier:converter; 29, string:"pattern"; 30, attribute; 30, 31; 30, 32; 31, identifier:cls; 32, identifier:default_pattern; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:optional_pattern; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:cardinality; 39, identifier:make_pattern; 40, argument_list; 40, 41; 41, identifier:pattern; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:group_count; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:cardinality; 48, identifier:compute_group_count; 49, argument_list; 49, 50; 50, identifier:pattern; 51, function_definition; 51, 52; 51, 53; 51, 58; 52, function_name:convert_optional; 53, parameters; 53, 54; 53, 55; 54, identifier:text; 55, default_parameter; 55, 56; 55, 57; 56, identifier:m; 57, None; 58, block; 58, 59; 58, 70; 58, 76; 59, if_statement; 59, 60; 59, 61; 60, identifier:text; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:text; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:text; 68, identifier:strip; 69, argument_list; 70, if_statement; 70, 71; 70, 73; 71, not_operator; 71, 72; 72, identifier:text; 73, block; 73, 74; 74, return_statement; 74, 75; 75, None; 76, return_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:converter; 79, argument_list; 79, 80; 80, identifier:text; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:convert_optional; 85, identifier:pattern; 86, identifier:optional_pattern; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:convert_optional; 92, identifier:regex_group_count; 93, identifier:group_count; 94, return_statement; 94, 95; 95, identifier:convert_optional
def with_zero_or_one(cls, converter, pattern=None): """Creates a type converter for a T with 0..1 times by using the type converter for one item of T. :param converter: Type converter (function) for data type T. :param pattern: Regexp pattern for an item (=converter.pattern). :return: type-converter for optional<T> (T or None). """ cardinality = Cardinality.zero_or_one if not pattern: pattern = getattr(converter, "pattern", cls.default_pattern) optional_pattern = cardinality.make_pattern(pattern) group_count = cardinality.compute_group_count(pattern) def convert_optional(text, m=None): if text: text = text.strip() if not text: return None return converter(text) convert_optional.pattern = optional_pattern # OLD: convert_optional.group_count = group_count convert_optional.regex_group_count = group_count return convert_optional
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:stats_keyboard; 3, parameters; 3, 4; 3, 5; 4, identifier:events; 5, identifier:table; 6, block; 6, 7; 6, 9; 6, 21; 6, 29; 6, 37; 6, 50; 6, 65; 6, 74; 6, 75; 6, 203; 6, 232; 6, 390; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 16; 10, comparison_operator:<; 10, 11; 10, 15; 11, call; 11, 12; 11, 13; 12, identifier:len; 13, argument_list; 13, 14; 14, identifier:events; 15, integer:2; 16, block; 16, 17; 17, return_statement; 17, 18; 18, expression_list; 18, 19; 18, 20; 19, list:[]; 20, list:[]; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 26; 23, pattern_list; 23, 24; 23, 25; 24, identifier:deltas; 25, identifier:prev_dt; 26, expression_list; 26, 27; 26, 28; 27, list:[]; 28, None; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, pattern_list; 31, 32; 31, 33; 32, identifier:sessions; 33, identifier:session; 34, expression_list; 34, 35; 34, 36; 35, list:[]; 36, None; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:UNBROKEN_DELTA; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:datetime; 43, identifier:timedelta; 44, argument_list; 44, 45; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:seconds; 47, attribute; 47, 48; 47, 49; 48, identifier:conf; 49, identifier:KeyboardSessionMaxDelta; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:blank; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:collections; 56, identifier:defaultdict; 57, argument_list; 57, 58; 58, lambda; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:collections; 62, identifier:defaultdict; 63, argument_list; 63, 64; 64, identifier:int; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:collated; 68, list:[blank.copy()]; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:blank; 72, identifier:copy; 73, argument_list; 74, comment; 75, for_statement; 75, 76; 75, 77; 75, 78; 76, identifier:e; 77, identifier:events; 78, block; 78, 79; 78, 173; 78, 184; 78, 197; 79, if_statement; 79, 80; 79, 81; 80, identifier:prev_dt; 81, block; 81, 82; 81, 125; 81, 133; 81, 140; 82, if_statement; 82, 83; 82, 113; 83, parenthesized_expression; 83, 84; 84, boolean_operator:or; 84, 85; 84, 104; 85, boolean_operator:or; 85, 86; 85, 95; 86, comparison_operator:!=; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:prev_dt; 89, identifier:second; 90, attribute; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:e; 93, string:"dt"; 94, identifier:second; 95, comparison_operator:!=; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:prev_dt; 98, identifier:minute; 99, attribute; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:e; 102, string:"dt"; 103, identifier:minute; 104, comparison_operator:!=; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:prev_dt; 107, identifier:hour; 108, attribute; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:e; 111, string:"dt"; 112, identifier:hour; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:collated; 118, identifier:append; 119, argument_list; 119, 120; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:blank; 123, identifier:copy; 124, argument_list; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:delta; 128, binary_operator:-; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:e; 131, string:"dt"; 132, identifier:prev_dt; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:deltas; 137, identifier:append; 138, argument_list; 138, 139; 139, identifier:delta; 140, if_statement; 140, 141; 140, 144; 140, 149; 141, comparison_operator:>; 141, 142; 141, 143; 142, identifier:delta; 143, identifier:UNBROKEN_DELTA; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:session; 148, None; 149, else_clause; 149, 150; 150, block; 150, 151; 150, 166; 151, if_statement; 151, 152; 151, 154; 152, not_operator; 152, 153; 153, identifier:session; 154, block; 154, 155; 154, 159; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:session; 158, list:[]; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:sessions; 163, identifier:append; 164, argument_list; 164, 165; 165, identifier:session; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:session; 170, identifier:append; 171, argument_list; 171, 172; 172, identifier:delta; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 181; 175, subscript; 175, 176; 175, 180; 176, subscript; 176, 177; 176, 178; 177, identifier:collated; 178, unary_operator:-; 178, 179; 179, integer:1; 180, string:"dt"; 181, subscript; 181, 182; 181, 183; 182, identifier:e; 183, string:"dt"; 184, expression_statement; 184, 185; 185, augmented_assignment:+=; 185, 186; 185, 196; 186, subscript; 186, 187; 186, 193; 187, subscript; 187, 188; 187, 192; 188, subscript; 188, 189; 188, 190; 189, identifier:collated; 190, unary_operator:-; 190, 191; 191, integer:1; 192, string:"keys"; 193, subscript; 193, 194; 193, 195; 194, identifier:e; 195, string:"realkey"; 196, integer:1; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:prev_dt; 200, subscript; 200, 201; 200, 202; 201, identifier:e; 202, string:"dt"; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:longest_session; 206, call; 206, 207; 206, 208; 207, identifier:max; 208, argument_list; 208, 209; 208, 218; 209, binary_operator:+; 209, 210; 209, 211; 210, identifier:sessions; 211, list:[[datetime.timedelta()]]; 211, 212; 212, list:[datetime.timedelta()]; 212, 213; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:datetime; 216, identifier:timedelta; 217, argument_list; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:key; 220, lambda; 220, 221; 220, 223; 221, lambda_parameters; 221, 222; 222, identifier:x; 223, call; 223, 224; 223, 225; 224, identifier:sum; 225, argument_list; 225, 226; 225, 227; 226, identifier:x; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:datetime; 230, identifier:timedelta; 231, argument_list; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:stats; 235, conditional_expression:if; 235, 236; 235, 253; 235, 256; 236, list:[ ("Average interval between combos", sum(deltas, datetime.timedelta()) / len(deltas)), ]; 236, 237; 237, tuple; 237, 238; 237, 239; 238, string:"Average interval between combos"; 239, binary_operator:/; 239, 240; 239, 249; 240, call; 240, 241; 240, 242; 241, identifier:sum; 242, argument_list; 242, 243; 242, 244; 243, identifier:deltas; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:datetime; 247, identifier:timedelta; 248, argument_list; 249, call; 249, 250; 249, 251; 250, identifier:len; 251, argument_list; 251, 252; 252, identifier:deltas; 253, comparison_operator:==; 253, 254; 253, 255; 254, string:"combos"; 255, identifier:table; 256, list:[ ("Keys per hour", int(3600 * len(events) / timedelta_seconds(events[-1]["dt"] - events[0]["dt"]))), ("Average interval between keys", sum(deltas, datetime.timedelta()) / len(deltas)), ("Typing sessions (key interval < %ss)" % UNBROKEN_DELTA.seconds, len(sessions)), ("Average keys in session", sum(len(x) + 1 for x in sessions) / len(sessions)), ("Average session duration", sum((sum(x, datetime.timedelta()) for x in sessions), datetime.timedelta()) / len(sessions)), ("Longest session duration", sum(longest_session, datetime.timedelta())), ("Keys in longest session", len(longest_session) + 1), ("Most keys in session", max(len(x) + 1 for x in sessions)), ]; 256, 257; 256, 284; 256, 300; 256, 310; 256, 329; 256, 357; 256, 368; 256, 376; 257, tuple; 257, 258; 257, 259; 258, string:"Keys per hour"; 259, call; 259, 260; 259, 261; 260, identifier:int; 261, argument_list; 261, 262; 262, binary_operator:/; 262, 263; 262, 269; 263, binary_operator:*; 263, 264; 263, 265; 264, integer:3600; 265, call; 265, 266; 265, 267; 266, identifier:len; 267, argument_list; 267, 268; 268, identifier:events; 269, call; 269, 270; 269, 271; 270, identifier:timedelta_seconds; 271, argument_list; 271, 272; 272, binary_operator:-; 272, 273; 272, 279; 273, subscript; 273, 274; 273, 278; 274, subscript; 274, 275; 274, 276; 275, identifier:events; 276, unary_operator:-; 276, 277; 277, integer:1; 278, string:"dt"; 279, subscript; 279, 280; 279, 283; 280, subscript; 280, 281; 280, 282; 281, identifier:events; 282, integer:0; 283, string:"dt"; 284, tuple; 284, 285; 284, 286; 285, string:"Average interval between keys"; 286, binary_operator:/; 286, 287; 286, 296; 287, call; 287, 288; 287, 289; 288, identifier:sum; 289, argument_list; 289, 290; 289, 291; 290, identifier:deltas; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:datetime; 294, identifier:timedelta; 295, argument_list; 296, call; 296, 297; 296, 298; 297, identifier:len; 298, argument_list; 298, 299; 299, identifier:deltas; 300, tuple; 300, 301; 300, 306; 301, binary_operator:%; 301, 302; 301, 303; 302, string:"Typing sessions (key interval < %ss)"; 303, attribute; 303, 304; 303, 305; 304, identifier:UNBROKEN_DELTA; 305, identifier:seconds; 306, call; 306, 307; 306, 308; 307, identifier:len; 308, argument_list; 308, 309; 309, identifier:sessions; 310, tuple; 310, 311; 310, 312; 311, string:"Average keys in session"; 312, binary_operator:/; 312, 313; 312, 325; 313, call; 313, 314; 313, 315; 314, identifier:sum; 315, generator_expression; 315, 316; 315, 322; 316, binary_operator:+; 316, 317; 316, 321; 317, call; 317, 318; 317, 319; 318, identifier:len; 319, argument_list; 319, 320; 320, identifier:x; 321, integer:1; 322, for_in_clause; 322, 323; 322, 324; 323, identifier:x; 324, identifier:sessions; 325, call; 325, 326; 325, 327; 326, identifier:len; 327, argument_list; 327, 328; 328, identifier:sessions; 329, tuple; 329, 330; 329, 331; 330, string:"Average session duration"; 331, binary_operator:/; 331, 332; 331, 353; 332, call; 332, 333; 332, 334; 333, identifier:sum; 334, argument_list; 334, 335; 334, 348; 335, generator_expression; 335, 336; 335, 345; 336, call; 336, 337; 336, 338; 337, identifier:sum; 338, argument_list; 338, 339; 338, 340; 339, identifier:x; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:datetime; 343, identifier:timedelta; 344, argument_list; 345, for_in_clause; 345, 346; 345, 347; 346, identifier:x; 347, identifier:sessions; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:datetime; 351, identifier:timedelta; 352, argument_list; 353, call; 353, 354; 353, 355; 354, identifier:len; 355, argument_list; 355, 356; 356, identifier:sessions; 357, tuple; 357, 358; 357, 359; 358, string:"Longest session duration"; 359, call; 359, 360; 359, 361; 360, identifier:sum; 361, argument_list; 361, 362; 361, 363; 362, identifier:longest_session; 363, call; 363, 364; 363, 367; 364, attribute; 364, 365; 364, 366; 365, identifier:datetime; 366, identifier:timedelta; 367, argument_list; 368, tuple; 368, 369; 368, 370; 369, string:"Keys in longest session"; 370, binary_operator:+; 370, 371; 370, 375; 371, call; 371, 372; 371, 373; 372, identifier:len; 373, argument_list; 373, 374; 374, identifier:longest_session; 375, integer:1; 376, tuple; 376, 377; 376, 378; 377, string:"Most keys in session"; 378, call; 378, 379; 378, 380; 379, identifier:max; 380, generator_expression; 380, 381; 380, 387; 381, binary_operator:+; 381, 382; 381, 386; 382, call; 382, 383; 382, 384; 383, identifier:len; 384, argument_list; 384, 385; 385, identifier:x; 386, integer:1; 387, for_in_clause; 387, 388; 387, 389; 388, identifier:x; 389, identifier:sessions; 390, return_statement; 390, 391; 391, expression_list; 391, 392; 391, 393; 392, identifier:stats; 393, identifier:collated
def stats_keyboard(events, table): """Return statistics and collated events for keyboard events.""" if len(events) < 2: return [], [] deltas, prev_dt = [], None sessions, session = [], None UNBROKEN_DELTA = datetime.timedelta(seconds=conf.KeyboardSessionMaxDelta) blank = collections.defaultdict(lambda: collections.defaultdict(int)) collated = [blank.copy()] # [{dt, keys: {key: count}}] for e in events: if prev_dt: if (prev_dt.second != e["dt"].second or prev_dt.minute != e["dt"].minute or prev_dt.hour != e["dt"].hour): collated.append(blank.copy()) delta = e["dt"] - prev_dt deltas.append(delta) if delta > UNBROKEN_DELTA: session = None else: if not session: session = [] sessions.append(session) session.append(delta) collated[-1]["dt"] = e["dt"] collated[-1]["keys"][e["realkey"]] += 1 prev_dt = e["dt"] longest_session = max(sessions + [[datetime.timedelta()]], key=lambda x: sum(x, datetime.timedelta())) stats = [ ("Average interval between combos", sum(deltas, datetime.timedelta()) / len(deltas)), ] if "combos" == table else [ ("Keys per hour", int(3600 * len(events) / timedelta_seconds(events[-1]["dt"] - events[0]["dt"]))), ("Average interval between keys", sum(deltas, datetime.timedelta()) / len(deltas)), ("Typing sessions (key interval < %ss)" % UNBROKEN_DELTA.seconds, len(sessions)), ("Average keys in session", sum(len(x) + 1 for x in sessions) / len(sessions)), ("Average session duration", sum((sum(x, datetime.timedelta()) for x in sessions), datetime.timedelta()) / len(sessions)), ("Longest session duration", sum(longest_session, datetime.timedelta())), ("Keys in longest session", len(longest_session) + 1), ("Most keys in session", max(len(x) + 1 for x in sessions)), ] return stats, collated
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:to_perseus; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:df; 5, identifier:path_or_file; 6, default_parameter; 6, 7; 6, 8; 7, identifier:main_columns; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:separator; 11, identifier:separator; 12, default_parameter; 12, 13; 12, 14; 13, identifier:convert_bool_to_category; 14, True; 15, default_parameter; 15, 16; 15, 17; 16, identifier:numerical_annotation_rows; 17, call; 17, 18; 17, 19; 18, identifier:set; 19, argument_list; 19, 20; 20, list:[]; 21, block; 21, 22; 21, 24; 21, 32; 21, 48; 21, 59; 21, 63; 21, 75; 21, 102; 21, 103; 21, 184; 21, 237; 21, 251; 21, 283; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:_df; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:df; 30, identifier:copy; 31, argument_list; 32, if_statement; 32, 33; 32, 39; 33, not_operator; 33, 34; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:_df; 37, identifier:columns; 38, identifier:name; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:_df; 45, identifier:columns; 46, identifier:name; 47, string:'Column Name'; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:column_names; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:_df; 55, identifier:columns; 56, identifier:get_level_values; 57, argument_list; 57, 58; 58, string:'Column Name'; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:annotations; 62, dictionary; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:main_columns; 66, conditional_expression:if; 66, 67; 66, 71; 66, 74; 67, call; 67, 68; 67, 69; 68, identifier:_infer_main_columns; 69, argument_list; 69, 70; 70, identifier:_df; 71, comparison_operator:is; 71, 72; 71, 73; 72, identifier:main_columns; 73, None; 74, identifier:main_columns; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:annotations; 79, string:'Type'; 80, list_comprehension; 80, 81; 80, 92; 81, conditional_expression:if; 81, 82; 81, 83; 81, 88; 82, string:'E'; 83, comparison_operator:in; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:column_names; 86, identifier:i; 87, identifier:main_columns; 88, call; 88, 89; 88, 90; 89, identifier:dtype_to_perseus; 90, argument_list; 90, 91; 91, identifier:dtype; 92, for_in_clause; 92, 93; 92, 96; 93, pattern_list; 93, 94; 93, 95; 94, identifier:i; 95, identifier:dtype; 96, call; 96, 97; 96, 98; 97, identifier:enumerate; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 101; 100, identifier:_df; 101, identifier:dtypes; 102, comment; 103, for_statement; 103, 104; 103, 107; 103, 113; 104, pattern_list; 104, 105; 104, 106; 105, identifier:i; 106, identifier:column; 107, call; 107, 108; 107, 109; 108, identifier:enumerate; 109, argument_list; 109, 110; 110, attribute; 110, 111; 110, 112; 111, identifier:_df; 112, identifier:columns; 113, block; 113, 114; 113, 128; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:valid_values; 117, list_comprehension; 117, 118; 117, 119; 117, 124; 118, identifier:value; 119, for_in_clause; 119, 120; 119, 121; 120, identifier:value; 121, subscript; 121, 122; 121, 123; 122, identifier:_df; 123, identifier:column; 124, if_clause; 124, 125; 125, comparison_operator:is; 125, 126; 125, 127; 126, identifier:value; 127, None; 128, if_statement; 128, 129; 128, 148; 129, boolean_operator:and; 129, 130; 129, 136; 130, comparison_operator:>; 130, 131; 130, 135; 131, call; 131, 132; 131, 133; 132, identifier:len; 133, argument_list; 133, 134; 134, identifier:valid_values; 135, integer:0; 136, call; 136, 137; 136, 138; 137, identifier:all; 138, generator_expression; 138, 139; 138, 145; 139, comparison_operator:is; 139, 140; 139, 144; 140, call; 140, 141; 140, 142; 141, identifier:type; 142, argument_list; 142, 143; 143, identifier:value; 144, identifier:list; 145, for_in_clause; 145, 146; 145, 147; 146, identifier:value; 147, identifier:valid_values; 148, block; 148, 149; 148, 157; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 156; 151, subscript; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:annotations; 154, string:'Type'; 155, identifier:i; 156, string:'M'; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:_df; 161, identifier:column; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:_df; 166, identifier:column; 167, identifier:apply; 168, argument_list; 168, 169; 169, lambda; 169, 170; 169, 172; 170, lambda_parameters; 170, 171; 171, identifier:xs; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, string:';'; 175, identifier:join; 176, generator_expression; 176, 177; 176, 181; 177, call; 177, 178; 177, 179; 178, identifier:str; 179, argument_list; 179, 180; 180, identifier:x; 181, for_in_clause; 181, 182; 181, 183; 182, identifier:x; 183, identifier:xs; 184, if_statement; 184, 185; 184, 186; 185, identifier:convert_bool_to_category; 186, block; 186, 187; 187, for_statement; 187, 188; 187, 191; 187, 197; 188, pattern_list; 188, 189; 188, 190; 189, identifier:i; 190, identifier:column; 191, call; 191, 192; 191, 193; 192, identifier:enumerate; 193, argument_list; 193, 194; 194, attribute; 194, 195; 194, 196; 195, identifier:_df; 196, identifier:columns; 197, block; 197, 198; 198, if_statement; 198, 199; 198, 211; 199, comparison_operator:is; 199, 200; 199, 205; 200, subscript; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:_df; 203, identifier:dtypes; 204, identifier:i; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:np; 208, identifier:dtype; 209, argument_list; 209, 210; 210, string:'bool'; 211, block; 211, 212; 211, 220; 211, 228; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:values; 215, attribute; 215, 216; 215, 219; 216, subscript; 216, 217; 216, 218; 217, identifier:_df; 218, identifier:column; 219, identifier:values; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 227; 222, subscript; 222, 223; 222, 226; 223, subscript; 223, 224; 223, 225; 224, identifier:_df; 225, identifier:column; 226, identifier:values; 227, string:'+'; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 236; 230, subscript; 230, 231; 230, 234; 231, subscript; 231, 232; 231, 233; 232, identifier:_df; 233, identifier:column; 234, unary_operator:~; 234, 235; 235, identifier:values; 236, string:''; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:annotation_row_names; 240, binary_operator:-; 240, 241; 240, 249; 241, call; 241, 242; 241, 243; 242, identifier:set; 243, argument_list; 243, 244; 244, attribute; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:_df; 247, identifier:columns; 248, identifier:names; 249, set; 249, 250; 250, string:'Column Name'; 251, for_statement; 251, 252; 251, 253; 251, 254; 252, identifier:name; 253, identifier:annotation_row_names; 254, block; 254, 255; 254, 264; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:annotation_type; 258, conditional_expression:if; 258, 259; 258, 260; 258, 263; 259, string:'N'; 260, comparison_operator:in; 260, 261; 260, 262; 261, identifier:name; 262, identifier:numerical_annotation_rows; 263, string:'C'; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 275; 266, subscript; 266, 267; 266, 268; 267, identifier:annotations; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, string:'{}:{}'; 271, identifier:format; 272, argument_list; 272, 273; 272, 274; 273, identifier:annotation_type; 274, identifier:name; 275, call; 275, 276; 275, 281; 276, attribute; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:_df; 279, identifier:columns; 280, identifier:get_level_values; 281, argument_list; 281, 282; 282, identifier:name; 283, with_statement; 283, 284; 283, 294; 284, with_clause; 284, 285; 285, with_item; 285, 286; 286, as_pattern; 286, 287; 286, 292; 287, call; 287, 288; 287, 289; 288, identifier:PathOrFile; 289, argument_list; 289, 290; 289, 291; 290, identifier:path_or_file; 291, string:'w'; 292, as_pattern_target; 292, 293; 293, identifier:f; 294, block; 294, 295; 294, 309; 294, 348; 295, expression_statement; 295, 296; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:f; 299, identifier:write; 300, argument_list; 300, 301; 301, binary_operator:+; 301, 302; 301, 308; 302, call; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:separator; 305, identifier:join; 306, argument_list; 306, 307; 307, identifier:column_names; 308, string:'\n'; 309, for_statement; 309, 310; 309, 313; 309, 318; 310, pattern_list; 310, 311; 310, 312; 311, identifier:name; 312, identifier:values; 313, call; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:annotations; 316, identifier:items; 317, argument_list; 318, block; 318, 319; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:f; 323, identifier:write; 324, argument_list; 324, 325; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, string:'#!{{{name}}}{values}\n'; 328, identifier:format; 329, argument_list; 329, 330; 329, 333; 330, keyword_argument; 330, 331; 330, 332; 331, identifier:name; 332, identifier:name; 333, keyword_argument; 333, 334; 333, 335; 334, identifier:values; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:separator; 338, identifier:join; 339, argument_list; 339, 340; 340, list_comprehension; 340, 341; 340, 345; 341, call; 341, 342; 341, 343; 342, identifier:str; 343, argument_list; 343, 344; 344, identifier:x; 345, for_in_clause; 345, 346; 345, 347; 346, identifier:x; 347, identifier:values; 348, expression_statement; 348, 349; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:_df; 352, identifier:to_csv; 353, argument_list; 353, 354; 353, 355; 353, 358; 353, 361; 354, identifier:f; 355, keyword_argument; 355, 356; 355, 357; 356, identifier:header; 357, None; 358, keyword_argument; 358, 359; 358, 360; 359, identifier:index; 360, False; 361, keyword_argument; 361, 362; 361, 363; 362, identifier:sep; 363, identifier:separator
def to_perseus(df, path_or_file, main_columns=None, separator=separator, convert_bool_to_category=True, numerical_annotation_rows = set([])): """ Save pd.DataFrame to Perseus text format. :param df: pd.DataFrame. :param path_or_file: File name or file-like object. :param main_columns: Main columns. Will be infered if set to None. All numeric columns up-until the first non-numeric column are considered main columns. :param separator: For separating fields, default='\t'. :param covert_bool_to_category: Convert bool columns of True/False to category columns '+'/'', default=True. :param numerical_annotation_rows: Set of column names to be interpreted as numerical annotation rows, default=set([]). """ _df = df.copy() if not _df.columns.name: _df.columns.name = 'Column Name' column_names = _df.columns.get_level_values('Column Name') annotations = {} main_columns = _infer_main_columns(_df) if main_columns is None else main_columns annotations['Type'] = ['E' if column_names[i] in main_columns else dtype_to_perseus(dtype) for i, dtype in enumerate(_df.dtypes)] # detect multi-numeric columns for i, column in enumerate(_df.columns): valid_values = [value for value in _df[column] if value is not None] if len(valid_values) > 0 and all(type(value) is list for value in valid_values): annotations['Type'][i] = 'M' _df[column] = _df[column].apply(lambda xs: ';'.join(str(x) for x in xs)) if convert_bool_to_category: for i, column in enumerate(_df.columns): if _df.dtypes[i] is np.dtype('bool'): values = _df[column].values _df[column][values] = '+' _df[column][~values] = '' annotation_row_names = set(_df.columns.names) - {'Column Name'} for name in annotation_row_names: annotation_type = 'N' if name in numerical_annotation_rows else 'C' annotations['{}:{}'.format(annotation_type, name)] = _df.columns.get_level_values(name) with PathOrFile(path_or_file, 'w') as f: f.write(separator.join(column_names) + '\n') for name, values in annotations.items(): f.write('#!{{{name}}}{values}\n'.format(name=name, values=separator.join([str(x) for x in values]))) _df.to_csv(f, header=None, index=False, sep=separator)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:do_your_job; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 18; 5, 19; 5, 69; 5, 75; 5, 76; 5, 85; 5, 86; 5, 159; 5, 369; 5, 384; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, pattern_list; 10, 11; 10, 12; 11, identifier:y; 12, identifier:x; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:get_intended_direction; 17, argument_list; 18, comment; 19, if_statement; 19, 20; 19, 35; 19, 36; 20, boolean_operator:and; 20, 21; 20, 28; 21, comparison_operator:==; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:target_x; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:current_x; 28, comparison_operator:==; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:target_y; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:current_y; 35, comment; 36, block; 36, 37; 36, 68; 37, if_statement; 37, 38; 37, 46; 38, comparison_operator:==; 38, 39; 38, 45; 39, call; 39, 40; 39, 41; 40, identifier:len; 41, argument_list; 41, 42; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:results; 45, integer:0; 46, block; 46, 47; 46, 56; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:results; 53, identifier:append; 54, argument_list; 54, 55; 55, string:"TARGET ACQUIRED"; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:lg_mv; 61, argument_list; 61, 62; 61, 63; 62, integer:2; 63, binary_operator:+; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:name; 67, string:": TARGET ACQUIRED"; 68, return_statement; 69, expression_statement; 69, 70; 70, augmented_assignment:+=; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:num_steps; 74, integer:1; 75, comment; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:accessible; 79, list:['\\', '-', '|', '/', '.']; 79, 80; 79, 81; 79, 82; 79, 83; 79, 84; 80, string:'\\'; 81, string:'-'; 82, string:'|'; 83, string:'/'; 84, string:'.'; 85, comment; 86, if_statement; 86, 87; 86, 102; 87, boolean_operator:and; 87, 88; 87, 95; 88, boolean_operator:and; 88, 89; 88, 92; 89, comparison_operator:!=; 89, 90; 89, 91; 90, identifier:y; 91, integer:0; 92, comparison_operator:!=; 92, 93; 92, 94; 93, identifier:x; 94, integer:0; 95, comparison_operator:==; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:backtrack; 99, list:[0,0]; 99, 100; 99, 101; 100, integer:0; 101, integer:0; 102, block; 102, 103; 103, if_statement; 103, 104; 103, 113; 104, comparison_operator:>; 104, 105; 104, 112; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:random; 108, identifier:randint; 109, argument_list; 109, 110; 109, 111; 110, integer:1; 111, integer:10; 112, integer:6; 113, block; 113, 114; 114, if_statement; 114, 115; 114, 132; 115, comparison_operator:in; 115, 116; 115, 131; 116, call; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:self; 120, identifier:grd; 121, identifier:get_tile; 122, argument_list; 122, 123; 122, 128; 123, binary_operator:+; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:self; 126, identifier:current_y; 127, identifier:y; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:current_x; 131, identifier:accessible; 132, block; 132, 133; 132, 139; 132, 158; 133, expression_statement; 133, 134; 134, augmented_assignment:+=; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:current_y; 138, identifier:y; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:lg_mv; 144, argument_list; 144, 145; 144, 146; 145, integer:3; 146, binary_operator:+; 146, 147; 146, 152; 147, binary_operator:+; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:self; 150, identifier:name; 151, string:": randomly moving Y axis "; 152, call; 152, 153; 152, 154; 153, identifier:str; 154, argument_list; 154, 155; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:num_steps; 158, return_statement; 159, if_statement; 159, 160; 159, 163; 159, 211; 159, 264; 159, 316; 160, comparison_operator:==; 160, 161; 160, 162; 161, identifier:x; 162, integer:1; 163, block; 163, 164; 164, if_statement; 164, 165; 164, 182; 165, comparison_operator:in; 165, 166; 165, 181; 166, call; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:grd; 171, identifier:get_tile; 172, argument_list; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:self; 175, identifier:current_y; 176, binary_operator:+; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:self; 179, identifier:current_x; 180, integer:1; 181, identifier:accessible; 182, block; 182, 183; 182, 189; 182, 210; 183, expression_statement; 183, 184; 184, augmented_assignment:+=; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:current_x; 188, integer:1; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:lg_mv; 194, argument_list; 194, 195; 194, 196; 195, integer:3; 196, binary_operator:+; 196, 197; 196, 209; 197, binary_operator:+; 197, 198; 197, 203; 198, binary_operator:+; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:name; 202, string:": move# "; 203, call; 203, 204; 203, 205; 204, identifier:str; 205, argument_list; 205, 206; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:num_steps; 209, string:" - moving West"; 210, return_statement; 211, elif_clause; 211, 212; 211, 216; 212, comparison_operator:==; 212, 213; 212, 214; 213, identifier:x; 214, unary_operator:-; 214, 215; 215, integer:1; 216, block; 216, 217; 217, if_statement; 217, 218; 217, 235; 218, comparison_operator:in; 218, 219; 218, 234; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:grd; 224, identifier:get_tile; 225, argument_list; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:self; 228, identifier:current_y; 229, binary_operator:-; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:self; 232, identifier:current_x; 233, integer:1; 234, identifier:accessible; 235, block; 235, 236; 235, 242; 235, 263; 236, expression_statement; 236, 237; 237, augmented_assignment:-=; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:self; 240, identifier:current_x; 241, integer:1; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:lg_mv; 247, argument_list; 247, 248; 247, 249; 248, integer:3; 249, binary_operator:+; 249, 250; 249, 262; 250, binary_operator:+; 250, 251; 250, 256; 251, binary_operator:+; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:name; 255, string:": move# "; 256, call; 256, 257; 256, 258; 257, identifier:str; 258, argument_list; 258, 259; 259, attribute; 259, 260; 259, 261; 260, identifier:self; 261, identifier:num_steps; 262, string:" - moving East"; 263, return_statement; 264, elif_clause; 264, 265; 264, 268; 265, comparison_operator:==; 265, 266; 265, 267; 266, identifier:y; 267, integer:1; 268, block; 268, 269; 269, if_statement; 269, 270; 269, 287; 270, comparison_operator:in; 270, 271; 270, 286; 271, call; 271, 272; 271, 277; 272, attribute; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:self; 275, identifier:grd; 276, identifier:get_tile; 277, argument_list; 277, 278; 277, 283; 278, binary_operator:+; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:current_y; 282, integer:1; 283, attribute; 283, 284; 283, 285; 284, identifier:self; 285, identifier:current_x; 286, identifier:accessible; 287, block; 287, 288; 287, 294; 287, 315; 288, expression_statement; 288, 289; 289, augmented_assignment:+=; 289, 290; 289, 293; 290, attribute; 290, 291; 290, 292; 291, identifier:self; 292, identifier:current_y; 293, integer:1; 294, expression_statement; 294, 295; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:self; 298, identifier:lg_mv; 299, argument_list; 299, 300; 299, 301; 300, integer:3; 301, binary_operator:+; 301, 302; 301, 314; 302, binary_operator:+; 302, 303; 302, 308; 303, binary_operator:+; 303, 304; 303, 307; 304, attribute; 304, 305; 304, 306; 305, identifier:self; 306, identifier:name; 307, string:": move# "; 308, call; 308, 309; 308, 310; 309, identifier:str; 310, argument_list; 310, 311; 311, attribute; 311, 312; 311, 313; 312, identifier:self; 313, identifier:num_steps; 314, string:" - moving South"; 315, return_statement; 316, elif_clause; 316, 317; 316, 321; 317, comparison_operator:==; 317, 318; 317, 319; 318, identifier:y; 319, unary_operator:-; 319, 320; 320, integer:1; 321, block; 321, 322; 322, if_statement; 322, 323; 322, 340; 323, comparison_operator:in; 323, 324; 323, 339; 324, call; 324, 325; 324, 330; 325, attribute; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:self; 328, identifier:grd; 329, identifier:get_tile; 330, argument_list; 330, 331; 330, 336; 331, binary_operator:-; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:self; 334, identifier:current_y; 335, integer:1; 336, attribute; 336, 337; 336, 338; 337, identifier:self; 338, identifier:current_x; 339, identifier:accessible; 340, block; 340, 341; 340, 347; 340, 368; 341, expression_statement; 341, 342; 342, augmented_assignment:-=; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:self; 345, identifier:current_y; 346, integer:1; 347, expression_statement; 347, 348; 348, call; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:self; 351, identifier:lg_mv; 352, argument_list; 352, 353; 352, 354; 353, integer:3; 354, binary_operator:+; 354, 355; 354, 367; 355, binary_operator:+; 355, 356; 355, 361; 356, binary_operator:+; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:self; 359, identifier:name; 360, string:": move# "; 361, call; 361, 362; 361, 363; 362, identifier:str; 363, argument_list; 363, 364; 364, attribute; 364, 365; 364, 366; 365, identifier:self; 366, identifier:num_steps; 367, string:" - moving North"; 368, return_statement; 369, expression_statement; 369, 370; 370, call; 370, 371; 370, 376; 371, attribute; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:self; 374, identifier:grd; 375, identifier:set_tile; 376, argument_list; 376, 377; 376, 380; 376, 383; 377, attribute; 377, 378; 377, 379; 378, identifier:self; 379, identifier:start_y; 380, attribute; 380, 381; 380, 382; 381, identifier:self; 382, identifier:start_x; 383, string:'A'; 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:grd; 390, identifier:save; 391, argument_list; 391, 392; 392, call; 392, 393; 392, 398; 393, attribute; 393, 394; 393, 397; 394, attribute; 394, 395; 394, 396; 395, identifier:os; 396, identifier:path; 397, identifier:join; 398, argument_list; 398, 399; 398, 404; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:os; 402, identifier:getcwd; 403, argument_list; 404, string:'agent.txt'
def do_your_job(self): """ the goal of the explore agent is to move to the target while avoiding blockages on the grid. This function is messy and needs to be looked at. It currently has a bug in that the backtrack oscillates so need a new method of doing this - probably checking if previously backtracked in that direction for those coords, ie keep track of cells visited and number of times visited? """ y,x = self.get_intended_direction() # first find out where we should go if self.target_x == self.current_x and self.target_y == self.current_y: #print(self.name + " : TARGET ACQUIRED") if len(self.results) == 0: self.results.append("TARGET ACQUIRED") self.lg_mv(2, self.name + ": TARGET ACQUIRED" ) return self.num_steps += 1 # first try is to move on the x axis in a simple greedy search accessible = ['\\', '-', '|', '/', '.'] # randomly move in Y direction instead of X if all paths clear if y != 0 and x != 0 and self.backtrack == [0,0]: if random.randint(1,10) > 6: if self.grd.get_tile(self.current_y + y, self.current_x) in accessible: self.current_y += y self.lg_mv(3, self.name + ": randomly moving Y axis " + str(self.num_steps) ) return if x == 1: if self.grd.get_tile(self.current_y, self.current_x + 1) in accessible: self.current_x += 1 self.lg_mv(3, self.name + ": move# " + str(self.num_steps) + " - moving West" ) return elif x == -1: if self.grd.get_tile(self.current_y, self.current_x - 1) in accessible: self.current_x -= 1 self.lg_mv(3, self.name + ": move# " + str(self.num_steps) + " - moving East" ) return elif y == 1: if self.grd.get_tile(self.current_y + 1, self.current_x) in accessible: self.current_y += 1 self.lg_mv(3, self.name + ": move# " + str(self.num_steps) + " - moving South" ) return elif y == -1: if self.grd.get_tile(self.current_y - 1, self.current_x) in accessible: self.current_y -= 1 self.lg_mv(3, self.name + ": move# " + str(self.num_steps) + " - moving North") return self.grd.set_tile(self.start_y, self.start_x, 'A') self.grd.save(os.path.join(os.getcwd(), 'agent.txt'))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:save_filelist; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:opFile; 6, identifier:opFormat; 7, default_parameter; 7, 8; 7, 9; 8, identifier:delim; 9, string:','; 10, default_parameter; 10, 11; 10, 12; 11, identifier:qu; 12, string:'"'; 13, block; 13, 14; 13, 16; 13, 27; 13, 51; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:op_folder; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:os; 23, identifier:path; 24, identifier:dirname; 25, argument_list; 25, 26; 26, identifier:opFile; 27, if_statement; 27, 28; 27, 31; 27, 32; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:op_folder; 30, None; 31, comment; 32, block; 32, 33; 33, if_statement; 33, 34; 33, 43; 34, not_operator; 34, 35; 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:exists; 41, argument_list; 41, 42; 42, identifier:op_folder; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:os; 48, identifier:makedirs; 49, argument_list; 49, 50; 50, identifier:op_folder; 51, with_statement; 51, 52; 51, 62; 52, with_clause; 52, 53; 53, with_item; 53, 54; 54, as_pattern; 54, 55; 54, 60; 55, call; 55, 56; 55, 57; 56, identifier:open; 57, argument_list; 57, 58; 57, 59; 58, identifier:opFile; 59, string:'w'; 60, as_pattern_target; 60, 61; 61, identifier:fout; 62, block; 62, 63; 62, 72; 62, 85; 62, 92; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:fout; 67, identifier:write; 68, argument_list; 68, 69; 69, binary_operator:+; 69, 70; 69, 71; 70, string:"fullFilename"; 71, identifier:delim; 72, for_statement; 72, 73; 72, 74; 72, 75; 73, identifier:colHeading; 74, identifier:opFormat; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:fout; 80, identifier:write; 81, argument_list; 81, 82; 82, binary_operator:+; 82, 83; 82, 84; 83, identifier:colHeading; 84, identifier:delim; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:fout; 89, identifier:write; 90, argument_list; 90, 91; 91, string:'\n'; 92, for_statement; 92, 93; 92, 94; 92, 97; 93, identifier:f; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:filelist; 97, block; 97, 98; 97, 108; 97, 219; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:line; 101, binary_operator:+; 101, 102; 101, 107; 102, binary_operator:+; 102, 103; 102, 106; 103, binary_operator:+; 103, 104; 103, 105; 104, identifier:qu; 105, identifier:f; 106, identifier:qu; 107, identifier:delim; 108, try_statement; 108, 109; 108, 211; 109, block; 109, 110; 110, for_statement; 110, 111; 110, 112; 110, 113; 111, identifier:fld; 112, identifier:opFormat; 113, block; 113, 114; 113, 138; 113, 160; 113, 187; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:==; 115, 116; 115, 117; 116, identifier:fld; 117, string:"name"; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:line; 122, binary_operator:+; 122, 123; 122, 137; 123, binary_operator:+; 123, 124; 123, 136; 124, binary_operator:+; 124, 125; 124, 128; 125, binary_operator:+; 125, 126; 125, 127; 126, identifier:line; 127, identifier:qu; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:os; 132, identifier:path; 133, identifier:basename; 134, argument_list; 134, 135; 135, identifier:f; 136, identifier:qu; 137, identifier:delim; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:==; 139, 140; 139, 141; 140, identifier:fld; 141, string:"date"; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:line; 146, binary_operator:+; 146, 147; 146, 159; 147, binary_operator:+; 147, 148; 147, 158; 148, binary_operator:+; 148, 149; 148, 152; 149, binary_operator:+; 149, 150; 149, 151; 150, identifier:line; 151, identifier:qu; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:GetDateAsString; 156, argument_list; 156, 157; 157, identifier:f; 158, identifier:qu; 159, identifier:delim; 160, if_statement; 160, 161; 160, 164; 161, comparison_operator:==; 161, 162; 161, 163; 162, identifier:fld; 163, string:"size"; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:line; 168, binary_operator:+; 168, 169; 168, 186; 169, binary_operator:+; 169, 170; 169, 185; 170, binary_operator:+; 170, 171; 170, 174; 171, binary_operator:+; 171, 172; 171, 173; 172, identifier:line; 173, identifier:qu; 174, call; 174, 175; 174, 176; 175, identifier:str; 176, argument_list; 176, 177; 177, call; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:os; 181, identifier:path; 182, identifier:getsize; 183, argument_list; 183, 184; 184, identifier:f; 185, identifier:qu; 186, identifier:delim; 187, if_statement; 187, 188; 187, 191; 188, comparison_operator:==; 188, 189; 188, 190; 189, identifier:fld; 190, string:"path"; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:line; 195, binary_operator:+; 195, 196; 195, 210; 196, binary_operator:+; 196, 197; 196, 209; 197, binary_operator:+; 197, 198; 197, 201; 198, binary_operator:+; 198, 199; 198, 200; 199, identifier:line; 200, identifier:qu; 201, call; 201, 202; 201, 207; 202, attribute; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:os; 205, identifier:path; 206, identifier:dirname; 207, argument_list; 207, 208; 208, identifier:f; 209, identifier:qu; 210, identifier:delim; 211, except_clause; 211, 212; 211, 213; 212, identifier:IOError; 213, block; 213, 214; 213, 218; 214, expression_statement; 214, 215; 215, augmented_assignment:+=; 215, 216; 215, 217; 216, identifier:line; 217, string:'\n'; 218, comment; 219, try_statement; 219, 220; 219, 249; 220, block; 220, 221; 220, 242; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:fout; 225, identifier:write; 226, argument_list; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:str; 229, argument_list; 229, 230; 230, call; 230, 231; 230, 240; 231, attribute; 231, 232; 231, 239; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:line; 235, identifier:encode; 236, argument_list; 236, 237; 236, 238; 237, string:'ascii'; 238, string:'ignore'; 239, identifier:decode; 240, argument_list; 240, 241; 241, string:'utf-8'; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:fout; 246, identifier:write; 247, argument_list; 247, 248; 248, string:'\n'; 249, except_clause; 249, 250; 249, 251; 249, 252; 250, identifier:IOError; 251, comment; 252, block; 252, 253; 253, pass_statement
def save_filelist(self, opFile, opFormat, delim=',', qu='"'): """ uses a List of files and collects meta data on them and saves to an text file as a list or with metadata depending on opFormat. """ op_folder = os.path.dirname(opFile) if op_folder is not None: # short filename passed if not os.path.exists(op_folder): os.makedirs(op_folder) with open(opFile,'w') as fout: fout.write("fullFilename" + delim) for colHeading in opFormat: fout.write(colHeading + delim) fout.write('\n') for f in self.filelist: line = qu + f + qu + delim try: for fld in opFormat: if fld == "name": line = line + qu + os.path.basename(f) + qu + delim if fld == "date": line = line + qu + self.GetDateAsString(f) + qu + delim if fld == "size": line = line + qu + str(os.path.getsize(f)) + qu + delim if fld == "path": line = line + qu + os.path.dirname(f) + qu + delim except IOError: line += '\n' # no metadata try: fout.write (str(line.encode('ascii', 'ignore').decode('utf-8'))) fout.write ('\n') except IOError: #print("Cant print line - cls_filelist line 304") pass
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:type; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:variant_probe_coverages; 6, default_parameter; 6, 7; 6, 8; 7, identifier:variant; 8, None; 9, block; 9, 10; 9, 12; 9, 25; 9, 29; 9, 46; 9, 63; 9, 80; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 19; 13, not_operator; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:isinstance; 16, argument_list; 16, 17; 16, 18; 17, identifier:variant_probe_coverages; 18, identifier:list; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:variant_probe_coverages; 23, list:[variant_probe_coverages]; 23, 24; 24, identifier:variant_probe_coverages; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:calls; 28, list:[]; 29, for_statement; 29, 30; 29, 31; 29, 32; 30, identifier:variant_probe_coverage; 31, identifier:variant_probe_coverages; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:calls; 37, identifier:append; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:_type_variant_probe_coverages; 43, argument_list; 43, 44; 43, 45; 44, identifier:variant_probe_coverage; 45, identifier:variant; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:hom_alt_calls; 49, list_comprehension; 49, 50; 49, 51; 49, 54; 50, identifier:c; 51, for_in_clause; 51, 52; 51, 53; 52, identifier:c; 53, identifier:calls; 54, if_clause; 54, 55; 55, comparison_operator:>; 55, 56; 55, 62; 56, call; 56, 57; 56, 58; 57, identifier:sum; 58, argument_list; 58, 59; 59, subscript; 59, 60; 59, 61; 60, identifier:c; 61, string:"genotype"; 62, integer:1; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:het_calls; 66, list_comprehension; 66, 67; 66, 68; 66, 71; 67, identifier:c; 68, for_in_clause; 68, 69; 68, 70; 69, identifier:c; 70, identifier:calls; 71, if_clause; 71, 72; 72, comparison_operator:==; 72, 73; 72, 79; 73, call; 73, 74; 73, 75; 74, identifier:sum; 75, argument_list; 75, 76; 76, subscript; 76, 77; 76, 78; 77, identifier:c; 78, string:"genotype"; 79, integer:1; 80, if_statement; 80, 81; 80, 82; 80, 106; 80, 132; 81, identifier:hom_alt_calls; 82, block; 82, 83; 82, 102; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:hom_alt_calls; 87, identifier:sort; 88, argument_list; 88, 89; 88, 99; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:key; 91, lambda; 91, 92; 91, 94; 92, lambda_parameters; 92, 93; 93, identifier:x; 94, subscript; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:x; 97, string:"info"; 98, string:"conf"; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:reverse; 101, True; 102, return_statement; 102, 103; 103, subscript; 103, 104; 103, 105; 104, identifier:hom_alt_calls; 105, integer:0; 106, elif_clause; 106, 107; 106, 108; 107, identifier:het_calls; 108, block; 108, 109; 108, 128; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:het_calls; 113, identifier:sort; 114, argument_list; 114, 115; 114, 125; 115, keyword_argument; 115, 116; 115, 117; 116, identifier:key; 117, lambda; 117, 118; 117, 120; 118, lambda_parameters; 118, 119; 119, identifier:x; 120, subscript; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:x; 123, string:"info"; 124, string:"conf"; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:reverse; 127, True; 128, return_statement; 128, 129; 129, subscript; 129, 130; 129, 131; 130, identifier:het_calls; 131, integer:0; 132, else_clause; 132, 133; 133, block; 133, 134; 133, 153; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:calls; 138, identifier:sort; 139, argument_list; 139, 140; 139, 150; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:key; 142, lambda; 142, 143; 142, 145; 143, lambda_parameters; 143, 144; 144, identifier:x; 145, subscript; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:x; 148, string:"info"; 149, string:"conf"; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:reverse; 152, True; 153, return_statement; 153, 154; 154, subscript; 154, 155; 154, 156; 155, identifier:calls; 156, integer:0
def type(self, variant_probe_coverages, variant=None): """ Takes a list of VariantProbeCoverages and returns a Call for the Variant. Note, in the simplest case the list will be of length one. However, we may be typing the Variant on multiple backgrouds leading to multiple VariantProbes for a single Variant. """ if not isinstance(variant_probe_coverages, list): variant_probe_coverages = [variant_probe_coverages] calls = [] for variant_probe_coverage in variant_probe_coverages: calls.append( self._type_variant_probe_coverages( variant_probe_coverage, variant)) hom_alt_calls = [c for c in calls if sum(c["genotype"]) > 1] het_calls = [c for c in calls if sum(c["genotype"]) == 1] if hom_alt_calls: hom_alt_calls.sort(key=lambda x: x["info"]["conf"], reverse=True) return hom_alt_calls[0] elif het_calls: het_calls.sort(key=lambda x: x["info"]["conf"], reverse=True) return het_calls[0] else: calls.sort(key=lambda x: x["info"]["conf"], reverse=True) return calls[0]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:process; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:versions; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, for_statement; 9, 10; 9, 11; 9, 19; 10, identifier:tool_name; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:versions; 17, identifier:keys; 18, argument_list; 19, block; 19, 20; 19, 26; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:version; 23, subscript; 23, 24; 23, 25; 24, identifier:versions; 25, identifier:tool_name; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_log; 31, argument_list; 31, 32; 32, binary_operator:%; 32, 33; 32, 34; 33, string:"Using tool '%s', %s"; 34, tuple; 34, 35; 34, 36; 35, identifier:tool_name; 36, identifier:version
def process(self, versions): """Logging version sorted ascending by tool name.""" for tool_name in sorted(versions.keys()): version = versions[tool_name] self._log("Using tool '%s', %s" % (tool_name, version))