From 1baebffb8780fb1e0eb2d5e2fc80c086a77ed6e0 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Sun, 29 Oct 2017 21:04:09 +0100 Subject: [PATCH] Do some linting --- .ci/pylintrc | 6 ++--- flatisfy/cmds.py | 2 +- flatisfy/filters/__init__.py | 3 +-- flatisfy/filters/cache.py | 44 ++++++++++++++++++++++++++++++++- flatisfy/models/flat.py | 2 +- flatisfy/tools.py | 47 +++++++++++++++++------------------- 6 files changed, 71 insertions(+), 33 deletions(-) diff --git a/.ci/pylintrc b/.ci/pylintrc index 790b882..627d210 100644 --- a/.ci/pylintrc +++ b/.ci/pylintrc @@ -343,16 +343,16 @@ max-args=5 ignored-argument-names=_.* # Maximum number of locals for function / method body -max-locals=15 +max-locals=25 # Maximum number of return / yield for function / method body max-returns=6 # Maximum number of branch for function / method body -max-branches=12 +max-branches=20 # Maximum number of statements in function / method body -max-statements=50 +max-statements=100 # Maximum number of parents for a class (see R0901). max-parents=7 diff --git a/flatisfy/cmds.py b/flatisfy/cmds.py index d158f69..eae3971 100644 --- a/flatisfy/cmds.py +++ b/flatisfy/cmds.py @@ -81,7 +81,7 @@ def filter_flats_list(config, constraint_name, flats_list, fetch_details=True): # Do a third pass to deduplicate better if config["passes"] > 2: third_pass_result = flatisfy.filters.third_pass( - second_pass_result["new"], config + second_pass_result["new"] ) else: third_pass_result["new"] = second_pass_result["new"] diff --git a/flatisfy/filters/__init__.py b/flatisfy/filters/__init__.py index 6e86040..5cf9104 100644 --- a/flatisfy/filters/__init__.py +++ b/flatisfy/filters/__init__.py @@ -231,7 +231,7 @@ def second_pass(flats_list, constraint, config): } @tools.timeit -def third_pass(flats_list, config): +def third_pass(flats_list): """ Third filtering pass. @@ -239,7 +239,6 @@ def third_pass(flats_list, config): flats. :param flats_list: A list of flats dict to filter. - :param config: A config dict. :return: A dict mapping flat status and list of flat objects. """ LOGGER.info("Running third filtering pass.") diff --git a/flatisfy/filters/cache.py b/flatisfy/filters/cache.py index d53950f..b0a73c4 100644 --- a/flatisfy/filters/cache.py +++ b/flatisfy/filters/cache.py @@ -8,14 +8,31 @@ from __future__ import absolute_import, print_function, unicode_literals import requests + class MemoryCache(object): + """ + A cache in memory. + """ def __init__(self, on_miss): + """ + Constructor + + :param on_miss: Function to call to retrieve item when not already + cached. + """ self.hits = 0 self.misses = 0 self.map = {} self.on_miss = on_miss def get(self, key): + """ + Get an element from cache. Eventually call ``on_miss`` if the item is + not already cached. + + :param key: Key of the element to retrieve. + :return: Requested element. + """ cached = self.map.get(key, None) if cached is not None: self.hits += 1 @@ -26,20 +43,45 @@ class MemoryCache(object): return item def total(self): + """ + Get the total number of calls (with hits to the cache, or miss and + fetching with ``on_miss``) to the cache. + + :return: Total number of item accessing. + """ return self.hits + self.misses def hit_rate(self): + """ + Get the hit rate, that is the rate at which we requested an item which + was already in the cache. + + :return: The hit rate, in percents. + """ assert self.total() > 0 return 100 * self.hits // self.total() def miss_rate(self): + """ + Get the miss rate, that is the rate at which we requested an item which + was not already in the cache. + + :return: The miss rate, in percents. + """ assert self.total() > 0 return 100 * self.misses // self.total() + class ImageCache(MemoryCache): + """ + A cache for images, stored in memory. + """ @staticmethod def retrieve_photo(url): + """ + Helper to actually retrieve photos if not already cached. + """ return requests.get(url) def __init__(self): - super(self.__class__, self).__init__(on_miss=ImageCache.retrieve_photo) + super(ImageCache, self).__init__(on_miss=ImageCache.retrieve_photo) diff --git a/flatisfy/models/flat.py b/flatisfy/models/flat.py index 87c0fd7..907838b 100644 --- a/flatisfy/models/flat.py +++ b/flatisfy/models/flat.py @@ -5,10 +5,10 @@ This modules defines an SQLAlchemy ORM model for a flat. # pylint: disable=locally-disabled,invalid-name,too-few-public-methods from __future__ import absolute_import, print_function, unicode_literals +import enum import logging import arrow -import enum from sqlalchemy import ( Column, DateTime, Enum, Float, SmallInteger, String, Text diff --git a/flatisfy/tools.py b/flatisfy/tools.py index d5b5da1..8de5df9 100644 --- a/flatisfy/tools.py +++ b/flatisfy/tools.py @@ -18,18 +18,6 @@ import time import requests import unidecode -try: - from functools import wraps -except ImportError: - try: - from functools32 import wraps - except ImportError: - def wraps(func): - """ - Identity implementation of ``wraps`` for fallback. - """ - return lambda func: func - LOGGER = logging.getLogger(__name__) @@ -52,14 +40,20 @@ def hash_dict(func): def __hash__(self): return hash(json.dumps(self)) - @wraps(func) def wrapped(*args, **kwargs): + """ + The wrapped function + """ args = tuple( - [HDict(arg) if isinstance(arg, dict) else arg - for arg in args - ]) - kwargs = {k: HDict(v) if isinstance(v, dict) else v - for k, v in kwargs.items()} + [ + HDict(arg) if isinstance(arg, dict) else arg + for arg in args + ] + ) + kwargs = { + k: HDict(v) if isinstance(v, dict) else v + for k, v in kwargs.items() + } return func(*args, **kwargs) return wrapped @@ -281,7 +275,7 @@ def get_travel_time_between(latlng_from, latlng_to, config): .. note :: Uses the Navitia API. Requires a ``navitia_api_key`` field to be filled-in in the ``config``. """ - time = None + travel_time = None # Check that Navitia API key is available if config["navitia_api_key"]: @@ -300,7 +294,7 @@ def get_travel_time_between(latlng_from, latlng_to, config): req.raise_for_status() journeys = req.json()["journeys"][0] - time = journeys["durations"]["total"] + travel_time = journeys["durations"]["total"] sections = [] for section in journeys["sections"]: if section["type"] == "public_transport": @@ -333,22 +327,25 @@ def get_travel_time_between(latlng_from, latlng_to, config): "No API key available for travel time lookup. Please provide " "a Navitia API key. Skipping travel time lookup." ) - if time: + if travel_time: return { - "time": time, + "time": travel_time, "sections": sections } return None -def timeit(f): +def timeit(func): """ A decorator that logs how much time was spent in the function. """ def wrapped(*args, **kwargs): + """ + The wrapped function + """ before = time.time() - res = f(*args, **kwargs) + res = func(*args, **kwargs) runtime = time.time() - before - LOGGER.info("%s -- Execution took %s seconds.", f.__name__, runtime) + LOGGER.info("%s -- Execution took %s seconds.", func.__name__, runtime) return res return wrapped