From 17ca8f8b3385946d4bafd26ac0dbeb44bcfe6e12 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Sat, 20 Apr 2019 23:23:30 +0200 Subject: [PATCH] linted --- .gitignore | 1 + tww.py | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 03779ec..c7b996b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ venv data/.cache.csv __pycache__ +.idea diff --git a/tww.py b/tww.py index 80c2dd4..dc151f6 100644 --- a/tww.py +++ b/tww.py @@ -3,10 +3,9 @@ Find time now, in the past or future in any timezone or location. """ -import os import argparse import logging -import sys +import os import dateparser from pytz.exceptions import UnknownTimeZoneError @@ -20,6 +19,7 @@ basepath = os.path.dirname(os.path.abspath(__file__)) logging.basicConfig() logger = logging.getLogger() + def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('query', nargs='*', default="now", help=" to ") @@ -36,19 +36,19 @@ def setup_logging_level(debug=False): logger.debug("Debugging enabled") - class Location: """ Represents a location with name, latitude and longitude """ - def __init__(self, name:str, latitude: float, longitude: float): + + def __init__(self, name: str, latitude: float, longitude: float): self.name = name self.latitude = latitude self.longitude = longitude - + def __lt__(self, other): return self.name < other.name - + def __str__(self): return "{} {} {}".format(self.name, self.latitude, self.longitude) @@ -59,9 +59,9 @@ def normalize_words_to_number(query): Assumes one-word numbers used """ from word2number import w2n - + normal_list = [] - + for word in query.split(): try: normal_list.append(str(w2n.word_to_num(word))) @@ -79,7 +79,7 @@ def timezone_to_normal(query): america/new_york -> America/New_York """ import re - + # The magic in the regex is that it splits by either / OR _ OR - # where the | are OR; and then the parens ( ) keep the splitting # entries in the list so that we can join later @@ -98,9 +98,9 @@ def create_if_not_exists(fname): def write_to_cache(query, location): import csv - + logger.debug("Writing location to cache") - with open(os.path.join(basepath, "data",".cache.csv"), 'a+') as wf: + with open(os.path.join(basepath, "data", ".cache.csv"), 'a+') as wf: cachewriter = csv.writer(wf) cachewriter.writerow([query, location.latitude, @@ -124,11 +124,11 @@ def resolve_location_local(query): from fuzzywuzzy import fuzz query = query.lower() - create_if_not_exists(os.path.join(basepath, "data",".cache.csv")) - + create_if_not_exists(os.path.join(basepath, "data", ".cache.csv")) + # location hypothesis heap heap = [] - + for fname in [".cache", "countries", "cities"]: with open(os.path.join(basepath, "data", "{}.csv".format(fname))) as f: cfile = csv.reader(f) @@ -196,9 +196,9 @@ def parse_query(query): logger.debug("raw human_dt: {}".format(human_dt)) logger.debug("raw human_tz_loc: {}".format(human_tz_loc)) - + human_dt = normalize_words_to_number(human_dt) - + return human_dt, human_tz_loc