|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -# Copyright (C) 2012-2014 Rocky Bernstein <rocky@gnu.org> |
| 2 | +# Copyright (C) 2012-2014, 2020 Rocky Bernstein <rocky@gnu.org> |
3 | 3 | # |
4 | 4 | # This program is free software: you can redistribute it and/or modify |
5 | 5 | # it under the terms of the GNU General Public License as published by |
|
13 | 13 | # |
14 | 14 | # You should have received a copy of the GNU General Public License |
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | | -'''Things related to eval/exec''' |
| 16 | +"""Things related to eval/exec""" |
17 | 17 |
|
18 | 18 | # extract the "expression" part of a line of source code. |
19 | 19 | # |
20 | 20 | import re |
21 | 21 |
|
22 | 22 |
|
23 | 23 | def extract_expression(text): |
24 | | - if re.search('^\s*(?:if|elif)\s+', text): |
25 | | - text = re.sub('^\s*(?:if|elif)\s+', '', text) |
26 | | - text = re.sub(':(?:\s+.*$|$)', '', text) |
27 | | - elif re.search('^\s*assert\s+.*', text): |
| 24 | + if re.search("^\s*(?:if|elif)\s+", text): |
| 25 | + text = re.sub("^\s*(?:if|elif)\s+", "", text) |
| 26 | + text = re.sub(":(?:\s+.*$|$)", "", text) |
| 27 | + elif re.search("^\s*assert\s+.*", text): |
28 | 28 | # EXPR in : assert EXPRESSION: |
29 | | - text = re.sub('^\s*assert\s+', '', text) |
30 | | - elif re.search('^\s*(?:while)\s+', text): |
31 | | - text = re.sub('^\s*(?:while)\s+', '', text) |
32 | | - text = re.sub(':(?:\s+.*$|$)', '', text) |
33 | | - elif re.search('^\s*return\s+', text): |
| 29 | + text = re.sub("^\s*assert\s+", "", text) |
| 30 | + elif re.search("^\s*(?:while)\s+", text): |
| 31 | + text = re.sub("^\s*(?:while)\s+", "", text) |
| 32 | + text = re.sub(":(?:\s+.*$|$)", "", text) |
| 33 | + elif re.search("^\s*return\s+", text): |
34 | 34 | # EXPRESION in: return EXPRESSION |
35 | | - text = re.sub('^\s*return\s+', '', text) |
36 | | - elif re.search('^\s*for\s+.+\s+in\s+.*:', text): |
| 35 | + text = re.sub("^\s*return\s+", "", text) |
| 36 | + elif re.search("^\s*for\s+.+\s+in\s+.*:", text): |
37 | 37 | # EXPRESION in: for VAR in EXPRESSION: |
38 | | - text = re.sub('^\s*for\s+.+\s+in\s+', '', text) |
39 | | - text = re.sub(':.*$', '', text) |
40 | | - elif re.search('\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]', text): |
| 38 | + text = re.sub("^\s*for\s+.+\s+in\s+", "", text) |
| 39 | + text = re.sub(":.*$", "", text) |
| 40 | + elif re.search("^\s*and\s+.*", text): |
| 41 | + # EXPRESION in: and EXPRESSION |
| 42 | + text = re.sub("^\s*and\s+", "", text) |
| 43 | + elif re.search("^\s*or\s+.*", text): |
| 44 | + # EXPRESION in: and EXPRESSION |
| 45 | + text = re.sub("^\s*or\s+", "", text) |
| 46 | + elif re.search("\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]", text): |
41 | 47 | # RHS of an assignment statement. |
42 | | - text = re.sub('^\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=\s*', '', text) |
| 48 | + text = re.sub("^\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=\s*", "", text) |
43 | 49 | pass |
44 | 50 | return text |
45 | 51 |
|
46 | 52 |
|
47 | 53 | # Demo it |
48 | | -if __name__=='__main__': |
| 54 | +if __name__ == "__main__": |
49 | 55 | for stmt in ( |
50 | | - 'if condition(x):', |
51 | | - 'elif _is_magic(name):', |
52 | | - 'while expression:', |
53 | | - 'for i in range(3):', |
54 | | - 'abc = 123', |
55 | | - 'return return_value', |
56 | | - 'nothing_to_be.done'): |
| 56 | + "if condition(x):", |
| 57 | + "elif _is_magic(name):", |
| 58 | + "while expression:", |
| 59 | + "for i in range(3):", |
| 60 | + "and x > 3", |
| 61 | + "or y < 3", |
| 62 | + "abc = 123", |
| 63 | + "return return_value", |
| 64 | + "nothing_to_be.done", |
| 65 | + ): |
57 | 66 | print(extract_expression(stmt)) |
58 | 67 | pass |
59 | 68 | pass |
0 commit comments