Skip to content

Commit 790283b

Browse files
committed
Add help on magic numbers; fix AST funcs; misc
* Add help on magic numbers * Reorganise AST functions * Fix bugs in AST functions esp regarding negative numbers * Improve and refactor layout function * Add more doc strings * Fix minor error in quotation
1 parent c2bd89e commit 790283b

21 files changed

+539
-222
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![Example HTML output](https://github.com/grantps/superhelp/raw/master/superhelp_logo_padded_small.png)
44

5-
version number: 1.0.11
5+
version number: 1.0.12
66
author: Grant Paton-Simpson
77

88
## Overview

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from codecs import open
33
from os import path
44

5-
__version__ = '1.0.11'
5+
__version__ = '1.0.12'
66

77
here = path.abspath(path.dirname(__file__))
88

superhelp/ast_funcs/__init__.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Specialised functions for working with the AST. Copes with different ASTs
3+
created prior to Python 3.8. Handles odd things like positive numbers being
4+
under value but negative being under value/UnaryOp/operand/Constant etc.
5+
6+
When backward compatibility with 3.6 can be dropped use def __getattr__(name):
7+
https://stackoverflow.com/questions/2447353/getattr-on-a-module
8+
"""
9+
from .. import conf, gen_utils
10+
from . import versioned_gen as gen, versioned_nums as nums
11+
12+
python_version = gen_utils.get_python_version()
13+
14+
if python_version in (conf.PY3_6, conf.PY3_7):
15+
16+
val_dets = gen.val_dets_3_7
17+
18+
assigned_num_els_from_block = nums.assigned_num_els_from_block_3_7
19+
num_str_from_parent_el = nums.num_str_from_parent_el_3_7
20+
num_str_from_el = nums.num_str_from_el_3_7
21+
22+
assigned_str_els_from_block = gen.assigned_str_els_from_block_3_7
23+
str_from_el = gen.str_from_el_3_7
24+
str_els_from_block = gen.str_els_from_block_3_7
25+
26+
dict_key_from_subscript = gen.dict_key_from_subscript_3_7
27+
28+
_get_var_plus_equalled = gen._get_var_plus_equalled_3_7
29+
_get_var_equal_plussed = gen._get_var_equal_plussed_3_7
30+
get_danger_status = gen.get_danger_status_3_7
31+
32+
get_docstring_from_value = gen.get_docstring_from_value_3_7
33+
get_slice_dets = gen.get_slice_dets_3_7
34+
get_nt_lbl_flds = gen.get_nt_lbl_flds_3_7
35+
get_slice_n = gen.get_slice_n_3_7
36+
get_str_els_being_combined = gen.get_str_els_being_combined_3_7
37+
38+
elif python_version == conf.PY3_8:
39+
40+
val_dets = gen.val_dets_3_8
41+
42+
assigned_num_els_from_block = nums.assigned_num_els_from_block_3_8
43+
num_str_from_parent_el = nums.num_str_from_parent_el_3_8
44+
num_str_from_el = nums.num_str_from_el_3_8
45+
46+
assigned_str_els_from_block = gen.assigned_str_els_from_block_3_8
47+
str_from_el = gen.str_from_el_3_8
48+
str_els_from_block = gen.str_els_from_block_3_8
49+
50+
dict_key_from_subscript = gen.dict_key_from_subscript_3_8
51+
52+
_get_var_plus_equalled = gen._get_var_plus_equalled_3_8
53+
_get_var_equal_plussed = gen._get_var_equal_plussed_3_8
54+
get_danger_status = gen.get_danger_status_3_8
55+
get_docstring_from_value = gen.get_docstring_from_value_3_8
56+
get_slice_dets = gen.get_slice_dets_3_8
57+
get_nt_lbl_flds = gen.get_nt_lbl_flds_3_8
58+
get_slice_n = gen.get_slice_n_3_8
59+
get_str_els_being_combined = gen.get_str_els_being_combined_3_8
60+
61+
else:
62+
raise Exception(f"Unexpected Python version {python_version}")

superhelp/ast_funcs.py renamed to superhelp/ast_funcs/general.py

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import logging
22

3-
from . import conf
4-
from . import ast_versioned_funcs as avf
5-
from . import gen_utils
3+
from .. import conf
4+
from .. import gen_utils
65

76
def get_el_lines_dets(el, *, ignore_trailing_lines=False):
87
"""
@@ -99,57 +98,3 @@ def get_standardised_el_dict(el):
9998
std_el_dict = {key: value for key, value in el.items()
10099
if key not in conf.NON_STD_EL_KEYS}
101100
return std_el_dict
102-
103-
## when backward compatibility with 3.6 can be dropped use def __getattr__(name):
104-
## https://stackoverflow.com/questions/2447353/getattr-on-a-module
105-
python_version = gen_utils.get_python_version()
106-
107-
if python_version in (conf.PY3_6, conf.PY3_7):
108-
109-
val_dets = avf.val_dets_3_7
110-
111-
assigned_num_els_from_block = avf.assigned_num_els_from_block_3_7
112-
num_str_from_val = avf.num_str_from_val_3_7
113-
num_str_from_el = avf.num_str_from_el_3_7
114-
115-
assigned_str_els_from_block = avf.assigned_str_els_from_block_3_7
116-
str_from_el = avf.str_from_el_3_7
117-
str_els_from_block = avf.str_els_from_block_3_7
118-
119-
dict_key_from_subscript = avf.dict_key_from_subscript_3_7
120-
121-
_get_var_plus_equalled = avf._get_var_plus_equalled_3_7
122-
_get_var_equal_plussed = avf._get_var_equal_plussed_3_7
123-
get_danger_status = avf.get_danger_status_3_7
124-
125-
get_docstring_from_value = avf.get_docstring_from_value_3_7
126-
get_slice_dets = avf.get_slice_dets_3_7
127-
get_nt_lbl_flds = avf.get_nt_lbl_flds_3_7
128-
get_slice_n = avf.get_slice_n_3_7
129-
get_str_els_being_combined = avf.get_str_els_being_combined_3_7
130-
131-
elif python_version == conf.PY3_8:
132-
133-
val_dets = avf.val_dets_3_8
134-
135-
assigned_num_els_from_block = avf.assigned_num_els_from_block_3_8
136-
num_str_from_val = avf.num_str_from_val_3_8
137-
num_str_from_el = avf.num_str_from_el_3_8
138-
139-
assigned_str_els_from_block = avf.assigned_str_els_from_block_3_8
140-
str_from_el = avf.str_from_el_3_8
141-
str_els_from_block = avf.str_els_from_block_3_8
142-
143-
dict_key_from_subscript = avf.dict_key_from_subscript_3_8
144-
145-
_get_var_plus_equalled = avf._get_var_plus_equalled_3_8
146-
_get_var_equal_plussed = avf._get_var_equal_plussed_3_8
147-
get_danger_status = avf.get_danger_status_3_8
148-
get_docstring_from_value = avf.get_docstring_from_value_3_8
149-
get_slice_dets = avf.get_slice_dets_3_8
150-
get_nt_lbl_flds = avf.get_nt_lbl_flds_3_8
151-
get_slice_n = avf.get_slice_n_3_8
152-
get_str_els_being_combined = avf.get_str_els_being_combined_3_8
153-
154-
else:
155-
raise Exception(f"Unexpected Python version {python_version}")

superhelp/ast_versioned_funcs.py renamed to superhelp/ast_funcs/versioned_gen.py

Lines changed: 4 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
from . import versioned_nums as nums
3+
24
def val_dets_3_7(val_el):
35
"""
46
val_el is the element under value e.g. Constant (3.8+) or Str, Num (<3.8)
@@ -37,101 +39,6 @@ def val_dets_3_8(val_el):
3739
return None
3840
return val, needs_quoting
3941

40-
## nums ******************************
41-
42-
def assigned_num_els_from_block_3_7(block_el):
43-
num_els = block_el.xpath('descendant-or-self::Assign/value/Num')
44-
return num_els
45-
46-
def assigned_num_els_from_block_3_8(block_el):
47-
val_els = block_el.xpath('descendant-or-self::Assign/value')
48-
num_els = []
49-
for val_el in val_els:
50-
constant_els = val_el.xpath('Constant')
51-
if len(constant_els) != 1:
52-
continue
53-
constant_el = constant_els[0]
54-
if constant_el.get('type') in ('int', 'float'):
55-
num_els.append(constant_el)
56-
return num_els
57-
58-
def num_str_from_val_3_7(val_el):
59-
"""
60-
As for 3_8 version but Num / Str instead of Constant etc.
61-
"""
62-
positive_num_els = val_el.xpath('Num')
63-
if len(positive_num_els) == 1:
64-
num = positive_num_els[0].get('n')
65-
else:
66-
sub_els = val_el.xpath('UnaryOp/op/USub')
67-
if not sub_els:
68-
return None
69-
negative_num_els = val_el.xpath('UnaryOp/operand/Num')
70-
if len(negative_num_els) != 1:
71-
return None
72-
pos_num = negative_num_els[0].get('n')
73-
num = f'-{pos_num}'
74-
return num
75-
76-
def num_str_from_val_3_8(val_el):
77-
"""
78-
## a positive number
79-
<Assign lineno="1" col_offset="0">
80-
...
81-
<value>
82-
<Constant lineno="1" col_offset="4" type="int" value="0"/>
83-
</value>
84-
</Assign>
85-
86-
## a negative number
87-
<Assign lineno="1" col_offset="0">
88-
...
89-
<value>
90-
<UnaryOp lineno="1" col_offset="4">
91-
<op>
92-
<USub/>
93-
</op>
94-
<operand>
95-
<Constant lineno="1" col_offset="5" type="int" value="1"/>
96-
</operand>
97-
</UnaryOp>
98-
</value>
99-
</Assign>
100-
"""
101-
positive_num_els = val_el.xpath('Constant')
102-
if len(positive_num_els) == 1:
103-
positive_num_el = positive_num_els[0]
104-
val = positive_num_el.get('value')
105-
if positive_num_el.get('type') not in ('int', 'float'):
106-
return None
107-
num = val
108-
else:
109-
sub_els = val_el.xpath('UnaryOp/op/USub')
110-
if not sub_els:
111-
return None
112-
negative_num_els = val_el.xpath('UnaryOp/operand/Constant')
113-
if len(negative_num_els) != 1:
114-
return None
115-
negative_num_el = negative_num_els[0]
116-
val = negative_num_el.get('value')
117-
if negative_num_el.get('type') not in ('int', 'float'):
118-
return None
119-
num = f'-{val}'
120-
return num
121-
122-
def num_str_from_el_3_7(comparison_el):
123-
num = comparison_el.get('n')
124-
if not num:
125-
return None
126-
return num
127-
128-
def num_str_from_el_3_8(comparison_el):
129-
val = comparison_el.get('value')
130-
if comparison_el.get('type') in ('int', 'float'):
131-
num = val
132-
else:
133-
num = None
134-
return num
13542

13643
## strs ******************************
13744

@@ -405,7 +312,7 @@ def get_slice_dets_3_7(assign_subscript_el):
405312
if len(val_els) != 1:
406313
return None
407314
val_el = val_els[0]
408-
num_str = num_str_from_val_3_7(val_el)
315+
num_str = nums.num_str_from_parent_el_3_7(val_el)
409316
if num_str:
410317
slice_dets = slice_dets = f'[{num_str}]'
411318
else:
@@ -425,7 +332,7 @@ def get_slice_dets_3_8(assign_subscript_el):
425332
if len(val_els) != 1:
426333
return None
427334
val_el = val_els[0]
428-
num_str = num_str_from_val_3_8(val_el)
335+
num_str = nums.num_str_from_parent_el_3_8(val_el)
429336
if num_str:
430337
slice_dets = slice_dets = f'[{num_str}]'
431338
else:

0 commit comments

Comments
 (0)