Browse Source

Do some linting

Phyks (Lucas Verney) 2 years ago
parent
commit
1baebffb87
6 changed files with 71 additions and 33 deletions
  1. 3
    3
      .ci/pylintrc
  2. 1
    1
      flatisfy/cmds.py
  3. 1
    2
      flatisfy/filters/__init__.py
  4. 43
    1
      flatisfy/filters/cache.py
  5. 1
    1
      flatisfy/models/flat.py
  6. 22
    25
      flatisfy/tools.py

+ 3
- 3
.ci/pylintrc View File

@@ -343,16 +343,16 @@ max-args=5
343 343
 ignored-argument-names=_.*
344 344
 
345 345
 # Maximum number of locals for function / method body
346
-max-locals=15
346
+max-locals=25
347 347
 
348 348
 # Maximum number of return / yield for function / method body
349 349
 max-returns=6
350 350
 
351 351
 # Maximum number of branch for function / method body
352
-max-branches=12
352
+max-branches=20
353 353
 
354 354
 # Maximum number of statements in function / method body
355
-max-statements=50
355
+max-statements=100
356 356
 
357 357
 # Maximum number of parents for a class (see R0901).
358 358
 max-parents=7

+ 1
- 1
flatisfy/cmds.py View File

@@ -81,7 +81,7 @@ def filter_flats_list(config, constraint_name, flats_list, fetch_details=True):
81 81
     # Do a third pass to deduplicate better
82 82
     if config["passes"] > 2:
83 83
         third_pass_result = flatisfy.filters.third_pass(
84
-            second_pass_result["new"], config
84
+            second_pass_result["new"]
85 85
         )
86 86
     else:
87 87
         third_pass_result["new"] = second_pass_result["new"]

+ 1
- 2
flatisfy/filters/__init__.py View File

@@ -231,7 +231,7 @@ def second_pass(flats_list, constraint, config):
231 231
     }
232 232
 
233 233
 @tools.timeit
234
-def third_pass(flats_list, config):
234
+def third_pass(flats_list):
235 235
     """
236 236
     Third filtering pass.
237 237
 
@@ -239,7 +239,6 @@ def third_pass(flats_list, config):
239 239
     flats.
240 240
 
241 241
     :param flats_list: A list of flats dict to filter.
242
-    :param config: A config dict.
243 242
     :return: A dict mapping flat status and list of flat objects.
244 243
     """
245 244
     LOGGER.info("Running third filtering pass.")

+ 43
- 1
flatisfy/filters/cache.py View File

@@ -8,14 +8,31 @@ from __future__ import absolute_import, print_function, unicode_literals
8 8
 
9 9
 import requests
10 10
 
11
+
11 12
 class MemoryCache(object):
13
+    """
14
+    A cache in memory.
15
+    """
12 16
     def __init__(self, on_miss):
17
+        """
18
+        Constructor
19
+
20
+        :param on_miss: Function to call to retrieve item when not already
21
+        cached.
22
+        """
13 23
         self.hits = 0
14 24
         self.misses = 0
15 25
         self.map = {}
16 26
         self.on_miss = on_miss
17 27
 
18 28
     def get(self, key):
29
+        """
30
+        Get an element from cache. Eventually call ``on_miss`` if the item is
31
+        not already cached.
32
+
33
+        :param key: Key of the element to retrieve.
34
+        :return: Requested element.
35
+        """
19 36
         cached = self.map.get(key, None)
20 37
         if cached is not None:
21 38
             self.hits += 1
@@ -26,20 +43,45 @@ class MemoryCache(object):
26 43
         return item
27 44
 
28 45
     def total(self):
46
+        """
47
+        Get the total number of calls (with hits to the cache, or miss and
48
+        fetching with ``on_miss``) to the cache.
49
+
50
+        :return: Total number of item accessing.
51
+        """
29 52
         return self.hits + self.misses
30 53
 
31 54
     def hit_rate(self):
55
+        """
56
+        Get the hit rate, that is the rate at which we requested an item which
57
+        was already in the cache.
58
+
59
+        :return: The hit rate, in percents.
60
+        """
32 61
         assert self.total() > 0
33 62
         return 100 * self.hits // self.total()
34 63
 
35 64
     def miss_rate(self):
65
+        """
66
+        Get the miss rate, that is the rate at which we requested an item which
67
+        was not already in the cache.
68
+
69
+        :return: The miss rate, in percents.
70
+        """
36 71
         assert self.total() > 0
37 72
         return 100 * self.misses // self.total()
38 73
 
74
+
39 75
 class ImageCache(MemoryCache):
76
+    """
77
+    A cache for images, stored in memory.
78
+    """
40 79
     @staticmethod
41 80
     def retrieve_photo(url):
81
+        """
82
+        Helper to actually retrieve photos if not already cached.
83
+        """
42 84
         return requests.get(url)
43 85
 
44 86
     def __init__(self):
45
-        super(self.__class__, self).__init__(on_miss=ImageCache.retrieve_photo)
87
+        super(ImageCache, self).__init__(on_miss=ImageCache.retrieve_photo)

+ 1
- 1
flatisfy/models/flat.py View File

@@ -5,10 +5,10 @@ This modules defines an SQLAlchemy ORM model for a flat.
5 5
 # pylint: disable=locally-disabled,invalid-name,too-few-public-methods
6 6
 from __future__ import absolute_import, print_function, unicode_literals
7 7
 
8
+import enum
8 9
 import logging
9 10
 
10 11
 import arrow
11
-import enum
12 12
 
13 13
 from sqlalchemy import (
14 14
     Column, DateTime, Enum, Float, SmallInteger, String, Text

+ 22
- 25
flatisfy/tools.py View File

@@ -18,18 +18,6 @@ import time
18 18
 import requests
19 19
 import unidecode
20 20
 
21
-try:
22
-    from functools import wraps
23
-except ImportError:
24
-    try:
25
-        from functools32 import wraps
26
-    except ImportError:
27
-        def wraps(func):
28
-            """
29
-            Identity implementation of ``wraps`` for fallback.
30
-            """
31
-            return lambda func: func
32
-
33 21
 
34 22
 LOGGER = logging.getLogger(__name__)
35 23
 
@@ -52,14 +40,20 @@ def hash_dict(func):
52 40
         def __hash__(self):
53 41
             return hash(json.dumps(self))
54 42
 
55
-    @wraps(func)
56 43
     def wrapped(*args, **kwargs):
44
+        """
45
+        The wrapped function
46
+        """
57 47
         args = tuple(
58
-            [HDict(arg) if isinstance(arg, dict) else arg
59
-             for arg in args
60
-            ])
61
-        kwargs = {k: HDict(v) if isinstance(v, dict) else v
62
-                  for k, v in kwargs.items()}
48
+            [
49
+                HDict(arg) if isinstance(arg, dict) else arg
50
+                for arg in args
51
+            ]
52
+        )
53
+        kwargs = {
54
+            k: HDict(v) if isinstance(v, dict) else v
55
+            for k, v in kwargs.items()
56
+        }
63 57
         return func(*args, **kwargs)
64 58
     return wrapped
65 59
 
@@ -281,7 +275,7 @@ def get_travel_time_between(latlng_from, latlng_to, config):
281 275
     .. note :: Uses the Navitia API. Requires a ``navitia_api_key`` field to be
282 276
     filled-in in the ``config``.
283 277
     """
284
-    time = None
278
+    travel_time = None
285 279
 
286 280
     # Check that Navitia API key is available
287 281
     if config["navitia_api_key"]:
@@ -300,7 +294,7 @@ def get_travel_time_between(latlng_from, latlng_to, config):
300 294
             req.raise_for_status()
301 295
 
302 296
             journeys = req.json()["journeys"][0]
303
-            time = journeys["durations"]["total"]
297
+            travel_time = journeys["durations"]["total"]
304 298
             sections = []
305 299
             for section in journeys["sections"]:
306 300
                 if section["type"] == "public_transport":
@@ -333,22 +327,25 @@ def get_travel_time_between(latlng_from, latlng_to, config):
333 327
             "No API key available for travel time lookup. Please provide "
334 328
             "a Navitia API key. Skipping travel time lookup."
335 329
         )
336
-    if time:
330
+    if travel_time:
337 331
         return {
338
-            "time": time,
332
+            "time": travel_time,
339 333
             "sections": sections
340 334
         }
341 335
     return None
342 336
 
343 337
 
344
-def timeit(f):
338
+def timeit(func):
345 339
     """
346 340
     A decorator that logs how much time was spent in the function.
347 341
     """
348 342
     def wrapped(*args, **kwargs):
343
+        """
344
+        The wrapped function
345
+        """
349 346
         before = time.time()
350
-        res = f(*args, **kwargs)
347
+        res = func(*args, **kwargs)
351 348
         runtime = time.time() - before
352
-        LOGGER.info("%s -- Execution took %s seconds.", f.__name__, runtime)
349
+        LOGGER.info("%s -- Execution took %s seconds.", func.__name__, runtime)
353 350
         return res
354 351
     return wrapped