I have the following input:
[['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '10', '10', '0', '0', '10', '10', '0', '0', '0', '10', '10', '10', '10', '10', '0', '0', '0'],
['0', '0', '10', '10', '0', '0', '10', '10', '0', '0', '0', '0', '0', '0', '0', '10', '10', '0', '0'],
['0', '0', '10', '10', '0', '0', '10', '10', '0', '0', '0', '0', '0', '0', '0', '10', '10', '0', '0'],
['0', '0', '10', '10', '10', '10', '10', '10', '0', '0', '0', '0', '10', '10', '10', '10', '0', '0', '0'],
['0', '0', '0', '10', '10', '10', '10', '10', '0', '0', '0', '10', '10', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '10', '10', '0', '0', '0', '10', '10', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '10', '10', '0', '0', '0', '10', '10', '10', '10', '10', '10', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']]
For the following function:
def assign_points(lists):
"""
Assigns points from a list of lists to Vec4 objects.
...
"""
z_max = float('-inf')
z_min = float('inf')
points = []
for y, l in enumerate(lists):
for x, point in enumerate(l):
try:
z, color = parse_point(point)
if color is None:
color = no_color(z)
points.append(Vec4(x, y, z, 0, color))
# Update z_max and z_min if necessary
if z > z_max:
z_max = z
if z < z_min:
z_min = z
except MyWarning as e:
exit(f"Error on line {str(y)}, item {str(x)}: {e}")
return points, z_max, z_min
However
My problem is that:
- I feel
assign_points
is too bloated and does/returns too much. - the input lists can get quite big and I think looping trough them more than once is wasteful
- I'm not sure global vars is best practice
I thought about doing a function that stored the values and (depending on a flag as argument) returned the maximum values once called outside assign_points
. But I'm pretty sure python doesn't have static vars (right?)
How would you go about this problem? Should I leave it as is or make global vars or do a 3rd option?