Skip to content

Commit

Permalink
pythonlib: Enable ambiguous variable name warning (#1538)
Browse files Browse the repository at this point in the history
Enable Flake8 warning about ambiguous variables name l in grass pkg.
Fix most of the cases (e.g., l is line).
Use per-file ignores for the rest (e.g., lexer in temporal).
  • Loading branch information
wenzeslaus committed Apr 19, 2021
1 parent 2312a5d commit 235c855
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 62 deletions.
7 changes: 6 additions & 1 deletion python/grass/.flake8
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ ignore =
E203, # whitespace before ':' (Black)
W503, # line break before binary operator (Black)
E722, # do not use bare 'except'
E741, # ambiguous variable name 'l'
F403, # 'from ctypes import *' used; unable to detect undefined names
F405, # 'RasterRow' may be undefined, or defined from star imports: ctypes, grass.pygrass.raster, grass.pygrass.vector

Expand All @@ -15,6 +14,7 @@ per-file-ignores =
# Files and directories which need fixes or specific exceptions
# E501 line too long
# W605 invalid escape sequence
# E741 ambiguous variable name 'l'
gunittest/*.py: E501 # These are mainly just todo comments
gunittest/gmodules.py: E501, W605
pygrass/vector/geometry.py: W605
Expand All @@ -25,13 +25,18 @@ per-file-ignores =
pygrass/modules/interface/parameter.py: E501, W605
pygrass/modules/grid/*.py: E501, F401
pygrass/raster/*.py: E501
pygrass/raster/rowio.py: E741
pygrass/rpc/__init__.py: E501, F401
pygrass/utils.py: E402, E501
script/db.py: E501
script/task.py: W605
script/vector.py: E501 # Long doctest lines which need review anyway
temporal/*.py: E501, F841
temporal/abstract_space_time_dataset.py: W605, E501, F841
temporal/temporal_algebra.py: E741, E501, F841
temporal/temporal_raster_algebra.py: E741
temporal/temporal_raster3d_algebra.py: E741
temporal/temporal_vector_algebra.py: E741, E501, F841
# Current benchmarks/tests are changing sys.path before import.
# Possibly, a different approach should be taken there anyway.
pygrass/tests/benchmark.py: E501, E402, F401, F821
Expand Down
24 changes: 12 additions & 12 deletions python/grass/gunittest/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def unify_projection(dic):
# possible name for a projection system
lookup = [["Universal Transverse Mercator", "Universe Transverse Mercator"]]
dic = dict(dic)
for l in lookup:
for item in lookup:
for n in range(len(dic["name"])):
if dic["name"][n] in l:
dic["name"][n] = l[0]
if dic["name"][n] in item:
dic["name"][n] = item[0]
return dic


Expand Down Expand Up @@ -87,21 +87,21 @@ def unify_units(dic):
["Kilometers", "Kilometres"],
]
dic = dict(dic)
for l in lookup:
for item in lookup:
if not isinstance(dic["unit"], str):
for n in range(len(dic["unit"])):
if dic["unit"][n] in l:
dic["unit"][n] = l[0]
if dic["unit"][n] in item:
dic["unit"][n] = item[0]
else:
if dic["unit"] in l:
dic["unit"] = l[0]
if dic["unit"] in item:
dic["unit"] = item[0]
if not isinstance(dic["units"], str):
for n in range(len(dic["units"])):
if dic["units"][n] in l:
dic["units"][n] = l[0]
if dic["units"][n] in item:
dic["units"][n] = item[0]
else:
if dic["units"] in l:
dic["units"] = l[0]
if dic["units"] in item:
dic["units"] = item[0]
return dic


Expand Down
2 changes: 1 addition & 1 deletion python/grass/pygrass/gis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def read(self):
with open(self.spath, "ab+") as f:
lines = f.readlines()
if lines:
return [decode(l.strip()) for l in lines]
return [decode(line.strip()) for line in lines]
lns = [
"PERMANENT",
]
Expand Down
40 changes: 20 additions & 20 deletions python/grass/pygrass/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,29 +538,29 @@ def create_test_stream_network_map(map_name="streams"):
with VectorTopo(map_name, mode="w", tab_name=map_name, tab_cols=cols) as streams:

# First flow graph
l = Line([(0, 2), (0.22, 1.75), (0.55, 1.5), (1, 1)])
streams.write(l, cat=1, attrs=(1,))
l = Line([(2, 2), (1, 1)])
streams.write(l, cat=2, attrs=(2,))
l = Line([(1, 1), (0.85, 0.5), (1, 0)])
streams.write(l, cat=3, attrs=(3,))
l = Line([(2, 1), (1, 0)])
streams.write(l, cat=4, attrs=(4,))
l = Line([(0, 1), (1, 0)])
streams.write(l, cat=5, attrs=(5,))
l = Line([(1, 0), (1, -1)])
streams.write(l, cat=6, attrs=(6,))
line = Line([(0, 2), (0.22, 1.75), (0.55, 1.5), (1, 1)])
streams.write(line, cat=1, attrs=(1,))
line = Line([(2, 2), (1, 1)])
streams.write(line, cat=2, attrs=(2,))
line = Line([(1, 1), (0.85, 0.5), (1, 0)])
streams.write(line, cat=3, attrs=(3,))
line = Line([(2, 1), (1, 0)])
streams.write(line, cat=4, attrs=(4,))
line = Line([(0, 1), (1, 0)])
streams.write(line, cat=5, attrs=(5,))
line = Line([(1, 0), (1, -1)])
streams.write(line, cat=6, attrs=(6,))
# Reverse line 3
l = Line([(1, 0), (1.15, 0.5), (1, 1)])
streams.write(l, cat=7, attrs=(7,))
line = Line([(1, 0), (1.15, 0.5), (1, 1)])
streams.write(line, cat=7, attrs=(7,))

# second flow graph
l = Line([(0, -1), (1, -2)])
streams.write(l, cat=8, attrs=(8,))
l = Line([(2, -1), (1, -2)])
streams.write(l, cat=9, attrs=(9,))
l = Line([(1, -2), (1, -3)])
streams.write(l, cat=10, attrs=(10,))
line = Line([(0, -1), (1, -2)])
streams.write(line, cat=8, attrs=(8,))
line = Line([(2, -1), (1, -2)])
streams.write(line, cat=9, attrs=(9,))
line = Line([(1, -2), (1, -3)])
streams.write(line, cat=10, attrs=(10,))

streams.organization = "Thuenen Institut"
streams.person = "Soeren Gebbert"
Expand Down
16 changes: 8 additions & 8 deletions python/grass/pygrass/vector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,9 @@ def table_to_dict(self, where=None):
self.table.filters.select(",".join(self.table.columns.names()))
# Execute the query and fetch the result
cur = self.table.execute()
l = cur.fetchall()
entries = cur.fetchall()
# Generate the dictionary
for entry in l:
for entry in entries:
table_dict[entry[cat_index]] = list(entry)

return table_dict
Expand Down Expand Up @@ -842,7 +842,7 @@ def features_to_wkb_list(self, bbox=None, feature_type="point", field=1):

if bboxlist is not None and len(bboxlist) > 0:

l = []
wkb_list = []
line_p = libvect.line_pnts()
line_c = libvect.line_cats()
size = ctypes.c_size_t()
Expand Down Expand Up @@ -875,10 +875,10 @@ def features_to_wkb_list(self, bbox=None, feature_type="point", field=1):
else:
pcat = cat.value

l.append((f_id, pcat, ctypes.string_at(barray, size.value)))
wkb_list.append((f_id, pcat, ctypes.string_at(barray, size.value)))
libgis.G_free(barray)

return l
return wkb_list
return None

@must_be_open
Expand Down Expand Up @@ -942,7 +942,7 @@ def areas_to_wkb_list(self, bbox=None, field=1):

if bboxlist is not None and len(bboxlist) > 0:

l = []
wkb_list = []
line_c = libvect.line_cats()
size = ctypes.c_size_t()
cat = ctypes.c_int()
Expand All @@ -966,10 +966,10 @@ def areas_to_wkb_list(self, bbox=None, field=1):
if ok > 0:
pcat = cat.value

l.append((a_id, pcat, ctypes.string_at(barray, size.value)))
wkb_list.append((a_id, pcat, ctypes.string_at(barray, size.value)))
libgis.G_free(barray)

return l
return wkb_list
return None


Expand Down
10 changes: 5 additions & 5 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,13 +1047,13 @@ def _compare_units(dic):
["kilometer", "kilometre"],
["kilometers", "kilometres"],
]
for l in lookup:
for item in lookup:
for n in range(len(dic["unit"])):
if dic["unit"][n].lower() in l:
dic["unit"][n] = l[0]
if dic["unit"][n].lower() in item:
dic["unit"][n] = item[0]
for n in range(len(dic["units"])):
if dic["units"][n].lower() in l:
dic["units"][n] = l[0]
if dic["units"][n].lower() in item:
dic["units"][n] = item[0]
return dic


Expand Down
4 changes: 2 additions & 2 deletions python/grass/script/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def db_describe(table, env=None, **args):

cols = []
result = {}
for l in s.splitlines():
f = l.split(":")
for line in s.splitlines():
f = line.split(":")
key = f[0]
f[1] = f[1].lstrip(" ")
if key.startswith("Column "):
Expand Down
16 changes: 8 additions & 8 deletions python/grass/script/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,21 @@ def split(s):
# source:
# http://stackoverflow.com/questions/4836710/
# does-python-have-a-built-in-function-for-string-natural-sort/4836734#4836734
def natural_sort(l):
def natural_sort(items):
"""Returns sorted list using natural sort
(deprecated, use naturally_sorted)
"""
return naturally_sorted(l)
return naturally_sorted(items)


def naturally_sorted(l, key=None):
def naturally_sorted(items, key=None):
"""Returns sorted list using natural sort"""
copy_l = l[:]
naturally_sort(copy_l, key)
return copy_l
copy_items = items[:]
naturally_sort(copy_items, key)
return copy_items


def naturally_sort(l, key=None):
def naturally_sort(items, key=None):
"""Sorts lists using natural sort"""

def convert(text):
Expand All @@ -361,7 +361,7 @@ def alphanum_key(actual_key):
sort_key = actual_key
return [convert(c) for c in re.split("([0-9]+)", sort_key)]

l.sort(key=alphanum_key)
items.sort(key=alphanum_key)


def get_lib_path(modname, libname=None):
Expand Down
6 changes: 3 additions & 3 deletions python/grass/script/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def vector_db(map, env=None, **kwargs):
)
result = {}

for l in s.splitlines():
f = l.split(";")
for line in s.splitlines():
f = line.split(";")
if len(f) != 5:
continue

Expand Down Expand Up @@ -379,7 +379,7 @@ def vector_what(

if layer:
if isinstance(layer, (tuple, list)):
layer_list = [str(l) for l in layer]
layer_list = [str(item) for item in layer]
else:
layer_list = [str(layer)]
if len(layer_list) != len(map_list):
Expand Down
4 changes: 2 additions & 2 deletions python/grass/temporal/abstract_space_time_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,13 +1142,13 @@ def get_registered_maps_as_objects_by_granularity(self, gran=None, dbif=None):
else:
end = end + gran

l = AbstractSpaceTimeDataset.resample_maplist_by_granularity(
maplist = AbstractSpaceTimeDataset.resample_maplist_by_granularity(
maps, start, end, gran
)
if connection_state_changed:
dbif.close()

return l
return maplist

@staticmethod
def resample_maplist_by_granularity(maps, start, end, gran):
Expand Down

0 comments on commit 235c855

Please sign in to comment.