From b72db4ba6ff57fef5e3c583c147212f48b523e85 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Fri, 11 Feb 2022 15:22:43 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- smartagent/utils.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/smartagent/utils.py b/smartagent/utils.py index 534d89d..efc4be4 100644 --- a/smartagent/utils.py +++ b/smartagent/utils.py @@ -22,7 +22,6 @@ agents = pickle.load(open(AGENT_DATASET_LOCATION, 'rb')) except TypeError: raise Warning("User-Agent dataset cannot be found! Make sure that AGENT_DATASET_LOCATION is set.") - agents = [] def load_agents(): agents = pickle.load(open(SMART_AGENT_SETTINGS['AGENT_DATASET_LOCATION'], 'rb')) @@ -49,24 +48,21 @@ def detect_user_agent(user_agent_string): """ start = time.time() - candidates = [] - for agent in agents: - if agent['regex'].match(user_agent_string): - candidates.append(agent) + candidates = [ + agent for agent in agents if agent['regex'].match(user_agent_string) + ] start = time.time() candidates.sort(key=lambda x: len(x['name'])) start = time.time() - result = get_user_agent_characteristics(candidates[-1]) - return result + return get_user_agent_characteristics(candidates[-1]) def all_possible_matches(user_agent_string): - candidates = [] - for agent in agents: - if agent['regex'].match(user_agent_string): - candidates.append(agent) - + candidates = [ + agent for agent in agents if agent['regex'].match(user_agent_string) + ] + candidates.sort(key=lambda x: len(x['name'])) return [(item['name'], item['depth']) for item in candidates]