Skip to content

Commit a81ab61

Browse files
committed
Refactor helper function and imports
1 parent 8212605 commit a81ab61

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

helper.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from itertools import tee
2-
32
from moves import adjacent_octile
43

54

@@ -35,19 +34,16 @@ def is_valid(point):
3534
return selected_nbors
3635

3736

38-
def get_x_and_y_from_store(store):
39-
x, y = [], []
40-
for key in store.keys():
41-
x.append(key.x)
42-
y.append(key.y)
43-
44-
return x, y
45-
37+
def get_x_and_y_from_itr(store):
38+
"""
39+
Extracts x and y coordinates into separate lists. Store can
40+
be list or dictionary or any other iterable that returns points
4641
47-
def get_x_and_y_from_path(path):
42+
store (iterable): iterable returns points
43+
"""
4844
x, y = [], []
49-
for key in path:
50-
x.append(key.x)
51-
y.append(key.y)
45+
for pos in store:
46+
x.append(pos.x)
47+
y.append(pos.y)
5248

5349
return x, y

mapworks/visualization.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from helper import get_x_and_y_from_store
2-
from helper import get_x_and_y_from_path
3-
from mpl_toolkits import mplot3d
1+
from helper import get_x_and_y_from_itr
42

53
import matplotlib.pyplot as plt
64
import matplotlib.cm as cm
@@ -35,14 +33,14 @@ def view_map(map_data, stores=None, paths=None, points=None, grid=True):
3533
if stores:
3634
colors = cm.autumn(np.linspace(0, 1, len(stores)))
3735
for c, store in zip(colors, stores):
38-
x, y = get_x_and_y_from_store(store)
36+
x, y = get_x_and_y_from_itr(store)
3937
plt.scatter(x, y, color=c)
4038

4139
if paths:
4240
colors = cm.winter(np.linspace(0, 1, len(paths)))
4341
for c, path in zip(colors, paths):
4442
start, end = path[0], path[-1]
45-
x, y = get_x_and_y_from_path(path)
43+
x, y = get_x_and_y_from_itr(path)
4644
plt.scatter(x, y, color=c)
4745
plt.plot(start.x, start.y, marker='x')
4846
plt.plot(end.x, end.y, marker='x')
@@ -73,13 +71,13 @@ def view_path(map_data, path, store=None, grid=True, markers=False):
7371
"""
7472
# plot all the points in the store
7573
if store:
76-
x, y = get_x_and_y_from_store(store)
74+
x, y = get_x_and_y_from_itr(store)
7775
plt.scatter(x, y, color='orange')
7876
# plot all the points in the path
7977
if path:
8078
start = path[0]
8179
end = path[-1]
82-
x, y = get_x_and_y_from_path(path)
80+
x, y = get_x_and_y_from_itr(path)
8381
plt.plot(x, y, color='blue')
8482
if markers:
8583
plt.scatter(x, y, color='green')

0 commit comments

Comments
 (0)