Repository: martinfleis/momepy
Pull request #61 (2019-08-14 11:11:22)

CLN: code cleanup

codecov[bot]: # [Codecov](https://codecov.io/gh/martinfleis/momepy/pull/61?src=pr&el=h1) Report
> Merging [#61](https://codecov.io/gh/martinfleis/momepy/pull/61?src=pr&el=desc) into [master](https://codecov.io/gh/martinfleis/momepy/commit/b3f0e7cddd92bd7db82581995cc290f4839baa7a?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `93.33%`.

[![Impacted file tree graph](https://codecov.io/gh/martinfleis/momepy/pull/61/graphs/tree.svg?width=650&token=i04999UxkK&height=150&src=pr)](https://codecov.io/gh/martinfleis/momepy/pull/61?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master      #61   +/-   ##
=======================================
  Coverage   92.88%   92.88%           
=======================================
  Files           9        9           
  Lines        2150     2150           
=======================================
  Hits         1997     1997           
  Misses        153      153
```


| [Impacted Files](https://codecov.io/gh/martinfleis/momepy/pull/61?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [momepy/utils.py](https://codecov.io/gh/martinfleis/momepy/pull/61/diff?src=pr&el=tree#diff-bW9tZXB5L3V0aWxzLnB5) | `78.26% <100%> (ø)` | :arrow_up: |
| [momepy/diversity.py](https://codecov.io/gh/martinfleis/momepy/pull/61/diff?src=pr&el=tree#diff-bW9tZXB5L2RpdmVyc2l0eS5weQ==) | `90.59% <100%> (ø)` | :arrow_up: |
| [momepy/shape.py](https://codecov.io/gh/martinfleis/momepy/pull/61/diff?src=pr&el=tree#diff-bW9tZXB5L3NoYXBlLnB5) | `95.18% <100%> (ø)` | :arrow_up: |
| [momepy/elements.py](https://codecov.io/gh/martinfleis/momepy/pull/61/diff?src=pr&el=tree#diff-bW9tZXB5L2VsZW1lbnRzLnB5) | `91.44% <85.71%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/martinfleis/momepy/pull/61?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/martinfleis/momepy/pull/61?src=pr&el=footer). Last update [b3f0e7c...6b1236a](https://codecov.io/gh/martinfleis/momepy/pull/61?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/momepy/diversity.py b/momepy/diversity.py
index 04b0412..684d6ae 100644
--- a/momepy/diversity.py
+++ b/momepy/diversity.py
@@ -207,7 +207,7 @@ def simpson(gdf, values, spatial_weights, unique_id, binning='HeadTailBreaks', *
     100%|██████████| 144/144 [00:00<00:00, 455.83it/s]
     Simpson's diversity index calculated.
     """
-    def simpson_di(data):
+    def _simpson_di(data):
 
         """ Given a hash { 'species': count } , returns the Simpson Diversity Index
 
@@ -221,8 +221,7 @@ def simpson(gdf, values, spatial_weights, unique_id, binning='HeadTailBreaks', *
             """ Relative abundance """
             if n == 0:
                 return 0
-            else:
-                return float(n) / N
+            return float(n) / N
 
         N = sum(data.values())
 
@@ -267,7 +266,7 @@ def simpson(gdf, values, spatial_weights, unique_id, binning='HeadTailBreaks', *
 
         sample_bins = classifiers.UserDefined(values_list, bins)
         counts = dict(zip(bins, sample_bins.counts))
-        results_list.append(simpson_di(counts))
+        results_list.append(_simpson_di(counts))
 
     series = pd.Series(results_list, index=gdf.index)
 
diff --git a/momepy/elements.py b/momepy/elements.py
index 49e32e7..47daf4f 100644
--- a/momepy/elements.py
+++ b/momepy/elements.py
@@ -88,13 +88,13 @@ def _point_array(objects, unique_id):
                 for line in poly_ext:
                     point_coords = line.coords
                     row_array = np.array(point_coords).tolist()
-                    for i in range(len(row_array)):
+                    for i, a in enumerate(row_array):
                         points.append(row_array[i])
                         ids.append(row[unique_id])
             elif poly_ext.type == 'LineString':
                 point_coords = poly_ext.coords
                 row_array = np.array(point_coords).tolist()
-                for i in range(len(row_array)):
+                for i, a in enumerate(row_array):
                     points.append(row_array[i])
                     ids.append(row[unique_id])
             else:
@@ -187,7 +187,7 @@ def _cut(tessellation, limit, unique_id):
         intersection = row.geometry.intersection(limit)
         if intersection.type == 'MultiPolygon':
             areas = {}
-            for p in range(len(intersection)):
+            for p, i in enumerate(intersection):
                 area = intersection[p].area
                 areas[p] = area
             maximal = max(areas.items(), key=operator.itemgetter(1))[0]
@@ -246,7 +246,7 @@ def _queen_corners(tessellation, sensitivity, sindex):
                 corners.append(point)
 
         if len(corners) > 2:
-            for c in range(len(corners)):
+            for c, it in enumerate(corners):
                 next_c = c + 1
                 if c == (len(corners) - 1):
                     next_c = 0
@@ -276,7 +276,7 @@ def _queen_corners(tessellation, sensitivity, sindex):
                 moves[coords.index(x)] = changes[x]
         keys = list(moves.keys())
         delete_points = []
-        for move in range(len(keys)):
+        for move, k in enumerate(keys):
             if move < len(keys) - 1:
                 if moves[keys[move]][1] == moves[keys[move + 1]][1] and keys[move + 1] - keys[move] < 5:
                     delete_points = delete_points + (coords[keys[move]:keys[move + 1]])
@@ -294,7 +294,7 @@ def _queen_corners(tessellation, sensitivity, sindex):
                 if len(list(shapely.ops.polygonize(mls))) > 1:
                     newgeom = MultiPolygon(shapely.ops.polygonize(mls))
                     geoms = []
-                    for g in range(len(newgeom)):
+                    for g, n in enumerate(newgeom):
                         geoms.append(newgeom[g].area)
                     newgeom = newgeom[geoms.index(max(geoms))]
                 else:
@@ -378,7 +378,7 @@ def tessellation(gdf, unique_id, limit, shrink=0.4, segment=0.5, queen_corners=F
     hull = series.geometry[0].convex_hull.buffer(300)
     hull = _densify(hull, 20)
     hull_array = np.array(hull.boundary.coords).tolist()
-    for i in range(len(hull_array)):
+    for i, a in enumerate(hull_array):
         points.append(hull_array[i])
         ids.append(-1)
 
diff --git a/momepy/shape.py b/momepy/shape.py
index ff071a4..4bb1c54 100644
--- a/momepy/shape.py
+++ b/momepy/shape.py
@@ -280,9 +280,9 @@ def _make_circle_two_points(points, p, q):
     # Select which circle to return
     if left is None and right is None:
         return circ
-    elif left is None:
+    if left is None:
         return right
-    elif right is None:
+    if right is None:
         return left
     if (left[2] <= right[2]):
         return left
@@ -717,7 +717,7 @@ def corners(gdf):
 
         if np.degrees(angle) <= 170:
             return True
-        elif np.degrees(angle) >= 190:
+        if np.degrees(angle) >= 190:
             return True
         return False
 
@@ -1038,7 +1038,7 @@ def centroid_corners(gdf):
 
         if np.degrees(angle) <= 170:
             return True
-        elif np.degrees(angle) >= 190:
+        if np.degrees(angle) >= 190:
             return True
         return False
 
diff --git a/momepy/utils.py b/momepy/utils.py
index 72cd937..bd92363 100644
--- a/momepy/utils.py
+++ b/momepy/utils.py
@@ -78,7 +78,7 @@ def sw_high(k, gdf=None, weights=None, ids=None, contiguity='queen'):
         else:
             raise ValueError('{} is not supported. Use \'queen\' or \'rook\'.'.format(contiguity))
     else:
-        raise Warning('GeoDataFrame of spatial weights must be given.')
+        raise AttributeError('GeoDataFrame or spatial weights must be given.')
 
     joined = first_order
     for i in list(range(2, k + 1)):
@@ -175,7 +175,7 @@ def nx_to_gdf(net, nodes=True, edges=True, spatial_weights=False, nodeID='nodeID
         if spatial_weights is True:
             return gdf_nodes, gdf_edges, W
         return gdf_nodes, gdf_edges
-    elif nodes is True and edges is False:
+    if nodes is True and edges is False:
         if spatial_weights is True:
             return gdf_nodes, W
         return gdf_nodes


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index b008723..e8204de 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -30,7 +30,7 @@ class TestUtils:
         assert from_df.neighbors[0] == check
         assert rook.neighbors[0] == check
 
-        with pytest.raises(Warning):
+        with pytest.raises(AttributeError):
             mm.sw_high(2, gdf=None, weights=None)
 
         with pytest.raises(ValueError):