diff --git a/pyhamtools/callinfo.py b/pyhamtools/callinfo.py index bde9959..aebb135 100644 --- a/pyhamtools/callinfo.py +++ b/pyhamtools/callinfo.py @@ -1,16 +1,12 @@ import re import logging -from datetime import datetime +from datetime import datetime, timezone import sys -import pytz - from pyhamtools.consts import LookupConventions as const from pyhamtools.callsign_exceptions import callsign_exceptions -UTC = pytz.UTC - if sys.version_info < (2, 7, ): class NullHandler(logging.Handler): def emit(self, record): @@ -81,10 +77,10 @@ class Callinfo(object): """truncate call until it corresponds to a Prefix in the database""" prefix = callsign if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) if re.search('(VK|AX|VI)9[A-Z]{3}', callsign): #special rule for VK9 calls - if timestamp > datetime(2006,1,1, tzinfo=UTC): + if timestamp > datetime(2006,1,1, tzinfo=timezone.utc): prefix = callsign[0:3]+callsign[4:5] while len(prefix) > 0: @@ -115,7 +111,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Raises: KeyError: Callsign could not be identified @@ -124,7 +120,7 @@ class Callinfo(object): """ entire_callsign = callsign.upper() if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) if re.search('[/A-Z0-9\\-]{3,15}', entire_callsign): # make sure the call has at least 3 characters @@ -230,7 +226,7 @@ class Callinfo(object): def _lookup_callsign(self, callsign, timestamp=None): if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) # Check if operation is invalid invalid = False @@ -278,7 +274,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: dict: Dictionary containing the callsign specific data @@ -315,7 +311,7 @@ class Callinfo(object): callsign = callsign.upper() if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) callsign_data = self._lookup_callsign(callsign, timestamp) @@ -332,7 +328,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: bool: True / False @@ -348,7 +344,7 @@ class Callinfo(object): """ if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) try: if self.get_all(callsign, timestamp): @@ -361,7 +357,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: dict: Containing Latitude and Longitude @@ -388,7 +384,7 @@ class Callinfo(object): """ if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) callsign_data = self.get_all(callsign, timestamp=timestamp) return { @@ -401,7 +397,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: int: containing the callsign's CQ Zone @@ -411,7 +407,7 @@ class Callinfo(object): """ if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) return self.get_all(callsign, timestamp)[const.CQZ] @@ -420,7 +416,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: int: containing the callsign's CQ Zone @@ -433,7 +429,7 @@ class Callinfo(object): """ if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) return self.get_all(callsign, timestamp)[const.ITUZ] @@ -442,7 +438,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: str: name of the Country @@ -460,7 +456,7 @@ class Callinfo(object): """ if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) return self.get_all(callsign, timestamp)[const.COUNTRY] @@ -469,7 +465,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: int: containing the country ADIF id @@ -479,7 +475,7 @@ class Callinfo(object): """ if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) return self.get_all(callsign, timestamp)[const.ADIF] @@ -488,7 +484,7 @@ class Callinfo(object): Args: callsign (str): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: str: continent identified @@ -508,6 +504,6 @@ class Callinfo(object): - AN: Antarctica """ if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) return self.get_all(callsign, timestamp)[const.CONTINENT] diff --git a/pyhamtools/dxcluster.py b/pyhamtools/dxcluster.py index c148c76..f427a76 100644 --- a/pyhamtools/dxcluster.py +++ b/pyhamtools/dxcluster.py @@ -1,18 +1,11 @@ __author__ = 'dh1tw' -from datetime import datetime +from datetime import datetime, timezone from time import strptime, mktime import re -import pytz - from pyhamtools.consts import LookupConventions as const - -UTC = pytz.UTC - - - def decode_char_spot(raw_string): """Chop Line from DX-Cluster into pieces and return a dict with the spot data""" @@ -31,7 +24,7 @@ def decode_char_spot(raw_string): data[const.DX] = re.sub(r'[^A-Za-z0-9\/]+', '', raw_string[26:38]) data[const.COMMENT] = re.sub(r'[^\sA-Za-z0-9\.,;\#\+\-!\?\$\(\)@\/]+', ' ', raw_string[39:69]).strip() - data[const.TIME] = datetime.now().replace(tzinfo=UTC) + data[const.TIME] = datetime.now(timezone.utc) return data diff --git a/pyhamtools/locator.py b/pyhamtools/locator.py index 707534b..2f0872a 100644 --- a/pyhamtools/locator.py +++ b/pyhamtools/locator.py @@ -1,11 +1,8 @@ from math import pi, sin, cos, atan2, sqrt, radians, log, tan, degrees -from datetime import datetime +from datetime import datetime, timezone -import pytz import ephem -UTC = pytz.UTC - def latlong_to_locator (latitude, longitude): """converts WGS84 coordinates into the corresponding Maidenhead Locator @@ -282,16 +279,14 @@ def calculate_sunrise_sunset(locator, calc_date=None): The following calculates the next sunrise & sunset for JN48QM on the 1./Jan/2014 >>> from pyhamtools.locator import calculate_sunrise_sunset - >>> from datetime import datetime - >>> import pytz - >>> UTC = pytz.UTC - >>> myDate = datetime(year=2014, month=1, day=1, tzinfo=UTC) + >>> from datetime import datetime, timezone + >>> myDate = datetime(year=2014, month=1, day=1, tzinfo=timezone.utc) >>> calculate_sunrise_sunset("JN48QM", myDate) { - 'morning_dawn': datetime.datetime(2014, 1, 1, 6, 36, 51, 710524, tzinfo=), - 'sunset': datetime.datetime(2014, 1, 1, 16, 15, 23, 31016, tzinfo=), - 'evening_dawn': datetime.datetime(2014, 1, 1, 15, 38, 8, 355315, tzinfo=), - 'sunrise': datetime.datetime(2014, 1, 1, 7, 14, 6, 162063, tzinfo=) + 'morning_dawn': datetime.datetime(2014, 1, 1, 6, 36, 51, 710524, tzinfo=datetime.timezone.utc), + 'sunset': datetime.datetime(2014, 1, 1, 16, 15, 23, 31016, tzinfo=datetime.timezone.utc), + 'evening_dawn': datetime.datetime(2014, 1, 1, 15, 38, 8, 355315, tzinfo=datetime.timezone.utc), + 'sunrise': datetime.datetime(2014, 1, 1, 7, 14, 6, 162063, tzinfo=datetime.timezone.utc) } """ @@ -303,7 +298,7 @@ def calculate_sunrise_sunset(locator, calc_date=None): latitude, longitude = locator_to_latlong(locator) if calc_date is None: - calc_date = datetime.utcnow() + calc_date = datetime.now(timezone.utc) if type(calc_date) != datetime: raise ValueError @@ -349,11 +344,11 @@ def calculate_sunrise_sunset(locator, calc_date=None): result['sunset'] = sunset if morning_dawn: - result['morning_dawn'] = morning_dawn.replace(tzinfo=UTC) + result['morning_dawn'] = morning_dawn.replace(tzinfo=timezone.utc) if sunrise: - result['sunrise'] = sunrise.replace(tzinfo=UTC) + result['sunrise'] = sunrise.replace(tzinfo=timezone.utc) if evening_dawn: - result['evening_dawn'] = evening_dawn.replace(tzinfo=UTC) + result['evening_dawn'] = evening_dawn.replace(tzinfo=timezone.utc) if sunset: - result['sunset'] = sunset.replace(tzinfo=UTC) + result['sunset'] = sunset.replace(tzinfo=timezone.utc) return result diff --git a/pyhamtools/lookuplib.py b/pyhamtools/lookuplib.py index 6193611..0e7b8bf 100644 --- a/pyhamtools/lookuplib.py +++ b/pyhamtools/lookuplib.py @@ -3,7 +3,7 @@ import logging import logging.config import re import random, string -from datetime import datetime +from datetime import datetime, timezone import xml.etree.ElementTree as ET import urllib import json @@ -14,14 +14,11 @@ import unicodedata import requests from requests.exceptions import ConnectionError, HTTPError, Timeout from bs4 import BeautifulSoup -import pytz from . import version from .consts import LookupConventions as const from .exceptions import APIKeyMissingError -UTC = pytz.UTC - REDIS_LUA_DEL_SCRIPT = "local keys = redis.call('keys', ARGV[1]) \n for i=1,#keys,20000 do \n redis.call('del', unpack(keys, i, math.min(i+19999, #keys))) \n end \n return keys" if sys.version_info < (2, 7,): @@ -333,7 +330,7 @@ class LookupLib(object): Args: callsign (string): Amateur radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: dict: Dictionary containing the country specific data of the callsign @@ -346,10 +343,9 @@ class LookupLib(object): The following code queries the the online Clublog API for the callsign "VK9XO" on a specific date. >>> from pyhamtools import LookupLib - >>> from datetime import datetime - >>> import pytz + >>> from datetime import datetime, timezone >>> my_lookuplib = LookupLib(lookuptype="clublogapi", apikey="myapikey") - >>> timestamp = datetime(year=1962, month=7, day=7, tzinfo=pytz.UTC) + >>> timestamp = datetime(year=1962, month=7, day=7, tzinfo=timezone.utc) >>> print my_lookuplib.lookup_callsign("VK9XO", timestamp) { 'country': u'CHRISTMAS ISLAND', @@ -373,7 +369,7 @@ class LookupLib(object): """ callsign = callsign.strip().upper() if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) if self._lookuptype == "clublogapi": callsign_data = self._lookup_clublogAPI(callsign=callsign, timestamp=timestamp, apikey=self._apikey) @@ -498,7 +494,7 @@ class LookupLib(object): Args: prefix (string): Prefix of a Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: dict: Dictionary containing the country specific data of the Prefix @@ -535,7 +531,7 @@ class LookupLib(object): prefix = prefix.strip().upper() if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) if self._lookuptype == "clublogxml" or self._lookuptype == "countryfile": @@ -555,7 +551,7 @@ class LookupLib(object): Args: callsign (string): Amateur Radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: bool: True if a record exists for this callsign (at the given time) @@ -568,13 +564,12 @@ class LookupLib(object): The following code checks the Clublog XML database if the operation is valid for two dates. >>> from pyhamtools import LookupLib - >>> from datetime import datetime - >>> import pytz + >>> from datetime import datetime, timezone >>> my_lookuplib = LookupLib(lookuptype="clublogxml", apikey="myapikey") >>> print my_lookuplib.is_invalid_operation("5W1CFN") True >>> try: - >>> timestamp = datetime(year=2012, month=1, day=31).replace(tzinfo=pytz.UTC) + >>> timestamp = datetime(year=2012, month=1, day=31, tzinfo=timezone.utc) >>> my_lookuplib.is_invalid_operation("5W1CFN", timestamp) >>> except KeyError: >>> print "Seems to be invalid operation before 31.1.2012" @@ -590,7 +585,7 @@ class LookupLib(object): callsign = callsign.strip().upper() if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) if self._lookuptype == "clublogxml": @@ -644,7 +639,7 @@ class LookupLib(object): Args: callsign (string): Amateur radio callsign - timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC) + timestamp (datetime, optional): datetime in UTC (tzinfo=timezone.utc) Returns: int: Value of the the CQ Zone exception which exists for this callsign (at the given time) @@ -674,7 +669,7 @@ class LookupLib(object): callsign = callsign.strip().upper() if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) if self._lookuptype == "clublogxml": @@ -703,7 +698,7 @@ class LookupLib(object): } if timestamp is None: - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) if sys.version_info.major == 3: encodeurl = url + "?" + urllib.parse.urlencode(params) @@ -889,12 +884,12 @@ class LookupLib(object): lookup[const.LAND] = root.Callsign.land.text if root.Callsign.efdate: try: - lookup[const.EFDATE] = datetime.strptime(root.Callsign.efdate.text, '%Y-%m-%d').replace(tzinfo=UTC) + lookup[const.EFDATE] = datetime.strptime(root.Callsign.efdate.text, '%Y-%m-%d').replace(tzinfo=timezone.utc) except ValueError: self._logger.debug("[QRZ.com] efdate: Invalid DateTime; " + callsign + " " + root.Callsign.efdate.text) if root.Callsign.expdate: try: - lookup[const.EXPDATE] = datetime.strptime(root.Callsign.expdate.text, '%Y-%m-%d').replace(tzinfo=UTC) + lookup[const.EXPDATE] = datetime.strptime(root.Callsign.expdate.text, '%Y-%m-%d').replace(tzinfo=timezone.utc) except ValueError: self._logger.debug("[QRZ.com] expdate: Invalid DateTime; " + callsign + " " + root.Callsign.expdate.text) if root.Callsign.p_call: @@ -915,7 +910,7 @@ class LookupLib(object): lookup[const.BIO] = root.Callsign.bio.text if root.Callsign.biodate: try: - lookup[const.BIODATE] = datetime.strptime(root.Callsign.biodate.text, '%Y-%m-%d %H:%M:%S').replace(tzinfo=UTC) + lookup[const.BIODATE] = datetime.strptime(root.Callsign.biodate.text, '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc) except ValueError: self._logger.warning("[QRZ.com] biodate: Invalid DateTime; " + callsign) if root.Callsign.image: @@ -926,7 +921,7 @@ class LookupLib(object): lookup[const.SERIAL] = long(root.Callsign.serial.text) if root.Callsign.moddate: try: - lookup[const.MODDATE] = datetime.strptime(root.Callsign.moddate.text, '%Y-%m-%d %H:%M:%S').replace(tzinfo=UTC) + lookup[const.MODDATE] = datetime.strptime(root.Callsign.moddate.text, '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc) except ValueError: self._logger.warning("[QRZ.com] moddate: Invalid DateTime; " + callsign) if root.Callsign.MSA: @@ -1134,7 +1129,7 @@ class LookupLib(object): if cty_date: cty_date = cty_date.group(0).replace("date=", "").replace("'", "") cty_date = datetime.strptime(cty_date[:19], '%Y-%m-%dT%H:%M:%S') - cty_date.replace(tzinfo=UTC) + cty_date.replace(tzinfo=timezone.utc) cty_header["Date"] = cty_date cty_ns = re.search("xmlns='.+[']", raw_header) @@ -1233,10 +1228,10 @@ class LookupLib(object): entity[const.LATITUDE] = float(item.text) elif item.tag == "start": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - entity[const.START] = dt.replace(tzinfo=UTC) + entity[const.START] = dt.replace(tzinfo=timezone.utc) elif item.tag == "end": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - entity[const.END] = dt.replace(tzinfo=UTC) + entity[const.END] = dt.replace(tzinfo=timezone.utc) elif item.tag == "whitelist": if item.text == "TRUE": entity[const.WHITELIST] = True @@ -1244,10 +1239,10 @@ class LookupLib(object): entity[const.WHITELIST] = False elif item.tag == "whitelist_start": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - entity[const.WHITELIST_START] = dt.replace(tzinfo=UTC) + entity[const.WHITELIST_START] = dt.replace(tzinfo=timezone.utc) elif item.tag == "whitelist_end": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - entity[const.WHITELIST_END] = dt.replace(tzinfo=UTC) + entity[const.WHITELIST_END] = dt.replace(tzinfo=timezone.utc) except AttributeError: self._logger.error("Error while processing: ") entities[int(cty_entity[0].text)] = entity @@ -1281,10 +1276,10 @@ class LookupLib(object): call_exception[const.LATITUDE] = float(item.text) elif item.tag == "start": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - call_exception[const.START] = dt.replace(tzinfo=UTC) + call_exception[const.START] = dt.replace(tzinfo=timezone.utc) elif item.tag == "end": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - call_exception[const.END] = dt.replace(tzinfo=UTC) + call_exception[const.END] = dt.replace(tzinfo=timezone.utc) call_exceptions[int(cty_exception.attrib["record"])] = call_exception self._logger.debug(str(len(call_exceptions))+" Exceptions added") @@ -1322,10 +1317,10 @@ class LookupLib(object): prefix[const.LATITUDE] = float(item.text) elif item.tag == "start": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - prefix[const.START] = dt.replace(tzinfo=UTC) + prefix[const.START] = dt.replace(tzinfo=timezone.utc) elif item.tag == "end": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - prefix[const.END] = dt.replace(tzinfo=UTC) + prefix[const.END] = dt.replace(tzinfo=timezone.utc) prefixes[int(cty_prefix.attrib["record"])] = prefix self._logger.debug(str(len(prefixes))+" Prefixes added") @@ -1348,10 +1343,10 @@ class LookupLib(object): elif item.tag == "start": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - invalid_operation[const.START] = dt.replace(tzinfo=UTC) + invalid_operation[const.START] = dt.replace(tzinfo=timezone.utc) elif item.tag == "end": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - invalid_operation[const.END] = dt.replace(tzinfo=UTC) + invalid_operation[const.END] = dt.replace(tzinfo=timezone.utc) invalid_operations[int(cty_inv_operation.attrib["record"])] = invalid_operation self._logger.debug(str(len(invalid_operations))+" Invalid Operations added") @@ -1377,10 +1372,10 @@ class LookupLib(object): zoneException[const.CQZ] = int(item.text) elif item.tag == "start": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - zoneException[const.START] = dt.replace(tzinfo=UTC) + zoneException[const.START] = dt.replace(tzinfo=timezone.utc) elif item.tag == "end": dt = datetime.strptime(item.text[:19], '%Y-%m-%dT%H:%M:%S') - zoneException[const.END] = dt.replace(tzinfo=UTC) + zoneException[const.END] = dt.replace(tzinfo=timezone.utc) zone_exceptions[int(cty_zone_exception.attrib["record"])] = zoneException self._logger.debug(str(len(zone_exceptions))+" Zone Exceptions added") @@ -1533,13 +1528,13 @@ class LookupLib(object): elif item == const.LONGITUDE: my_dict[item] = float(my_dict[item]) elif item == const.START: - my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=UTC) + my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=timezone.utc) elif item == const.END: - my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=UTC) + my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=timezone.utc) elif item == const.WHITELIST_START: - my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=UTC) + my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=timezone.utc) elif item == const.WHITELIST_END: - my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=UTC) + my_dict[item] = datetime.strptime(my_dict[item], '%Y-%m-%d%H:%M:%S').replace(tzinfo=timezone.utc) elif item == const.WHITELIST: my_dict[item] = self._str_to_bool(my_dict[item]) else: diff --git a/setup.py b/setup.py index e8a2284..b26f642 100755 --- a/setup.py +++ b/setup.py @@ -16,7 +16,6 @@ setup(name='pyhamtools', package_data={'': ['countryfilemapping.json']}, packages=['pyhamtools'], install_requires=[ - "pytz>=2019.1", "requests>=2.21.0", "ephem>=4.1.3", "beautifulsoup4>=4.7.1", diff --git a/test/test_callinfo.py b/test/test_callinfo.py index e7331a1..2d20541 100644 --- a/test/test_callinfo.py +++ b/test/test_callinfo.py @@ -1,14 +1,10 @@ # -*- coding: utf-8 -*- -from datetime import datetime +from datetime import datetime, timezone import pytest -import pytz from pyhamtools.consts import LookupConventions as const -UTC = pytz.UTC - - response_prefix_DH_clublog = { 'country': 'FEDERAL REPUBLIC OF GERMANY', 'adif': 230, @@ -389,7 +385,7 @@ class Test_callinfo_methods: if fix_callinfo._lookuplib._lookuptype == "clublogxml" or fix_callinfo._lookuplib._lookuptype == "clublogapi": assert fix_callinfo.get_all("DH1TW") == response_prefix_DH_clublog assert fix_callinfo.get_all("ci8aw") == response_zone_exception_ci8aw - timestamp = datetime(year=2016, month=1, day=20, tzinfo=UTC) + timestamp = datetime(year=2016, month=1, day=20, tzinfo=timezone.utc) assert fix_callinfo.get_all("VP8STI", timestamp) == response_Exception_VP8STI_with_start_and_stop_date elif fix_callinfo._lookuplib._lookuptype == "countryfile": diff --git a/test/test_dxcluster.py b/test/test_dxcluster.py index 4092978..afb6dcb 100644 --- a/test/test_dxcluster.py +++ b/test/test_dxcluster.py @@ -1,15 +1,9 @@ import pytest -from datetime import datetime - - -import pytz - +from datetime import datetime, timezone from pyhamtools.consts import LookupConventions as const from pyhamtools.dxcluster import decode_char_spot, decode_pc11_message, decode_pc61_message -UTC = pytz.UTC - fix_spot1 = "DX de CT3FW: 21004.8 HC2AO 599 TKS(CW)QSL READ,QRZ.COM 2132Z" fix_spot1_broken_spotter_call = "DX de $QRM: 21004.8 HC2AO 599 TKS(CW)QSL READ,QRZ.COM 2132Z" @@ -34,7 +28,7 @@ response_spot1 = { const.BAND: 15, const.MODE: "CW", const.COMMENT: "599 TKS(CW)QSL READ,QRZ.COM", - const.TIME: datetime.utcnow().replace( hour=21, minute=32, second=0, microsecond = 0, tzinfo=UTC) + const.TIME: datetime.now(timezone.utc).replace(hour=21, minute=32, second=0, microsecond = 0) } diff --git a/test/test_locator_sunrise_sunset.py b/test/test_locator_sunrise_sunset.py index a11b6b8..caf73ad 100644 --- a/test/test_locator_sunrise_sunset.py +++ b/test/test_locator_sunrise_sunset.py @@ -1,12 +1,9 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone import pytest -import pytz from pyhamtools.locator import calculate_sunrise_sunset -UTC = pytz.UTC - class Test_calculate_sunrise_sunset_normal_case(): def test_calculate_sunrise_sunset(self): @@ -14,11 +11,11 @@ class Test_calculate_sunrise_sunset_normal_case(): time_margin = timedelta(minutes=1) locator = "JN48QM" - test_time = datetime(year=2014, month=1, day=1, tzinfo=UTC) - result_JN48QM_1_1_2014_evening_dawn = datetime(2014, 1, 1, 15, 38, tzinfo=UTC) - result_JN48QM_1_1_2014_morning_dawn = datetime(2014, 1, 1, 6, 36, tzinfo=UTC) - result_JN48QM_1_1_2014_sunrise = datetime(2014, 1, 1, 7, 14, tzinfo=UTC) - result_JN48QM_1_1_2014_sunset = datetime(2014, 1, 1, 16, 15, 23, 31016, tzinfo=UTC) + test_time = datetime(year=2014, month=1, day=1, tzinfo=timezone.utc) + result_JN48QM_1_1_2014_evening_dawn = datetime(2014, 1, 1, 15, 38, tzinfo=timezone.utc) + result_JN48QM_1_1_2014_morning_dawn = datetime(2014, 1, 1, 6, 36, tzinfo=timezone.utc) + result_JN48QM_1_1_2014_sunrise = datetime(2014, 1, 1, 7, 14, tzinfo=timezone.utc) + result_JN48QM_1_1_2014_sunset = datetime(2014, 1, 1, 16, 15, 23, 31016, tzinfo=timezone.utc) assert calculate_sunrise_sunset(locator, test_time)['morning_dawn'] - result_JN48QM_1_1_2014_morning_dawn < time_margin assert calculate_sunrise_sunset(locator, test_time)['evening_dawn'] - result_JN48QM_1_1_2014_evening_dawn < time_margin @@ -33,7 +30,7 @@ class Test_calculate_sunrise_sunset_normal_case(): # The sun never rises in winter time close to the north pole (e.g. at Jan Mayen) # Therefore we expect no sunrise or sunset. - test_time = datetime(year=2021, month=12, day=15, tzinfo=UTC) + test_time = datetime(year=2021, month=12, day=15, tzinfo=timezone.utc) assert calculate_sunrise_sunset(locator, test_time)['morning_dawn'] == None assert calculate_sunrise_sunset(locator, test_time)['evening_dawn'] == None @@ -48,7 +45,7 @@ class Test_calculate_sunrise_sunset_normal_case(): # The sun never sets at the south pole during arctic summer # Therefore we expect no sunrise or sunset. - test_time = datetime(year=2014, month=1, day=1, tzinfo=UTC) + test_time = datetime(year=2014, month=1, day=1, tzinfo=timezone.utc) assert calculate_sunrise_sunset(locator, test_time)['morning_dawn'] == None assert calculate_sunrise_sunset(locator, test_time)['evening_dawn'] == None diff --git a/test/test_lookuplib_clublogapi.py b/test/test_lookuplib_clublogapi.py index 1af3673..f30bd67 100644 --- a/test/test_lookuplib_clublogapi.py +++ b/test/test_lookuplib_clublogapi.py @@ -1,5 +1,5 @@ import pytest -from datetime import datetime +from datetime import datetime, timezone from pyhamtools.lookuplib import LookupLib @@ -84,7 +84,7 @@ class TestclublogApi_Getters: def test_lookup_callsign(self, fixClublogApi): assert fixClublogApi.lookup_callsign("DH1TW") == response_Exception_DH1TW assert fixClublogApi.lookup_callsign("VU9KV") == response_Exception_VU9KV - d = datetime.utcnow().replace(year=1971, month=4, day=14) + d = datetime.now(timezone.utc).replace(year=1971, month=4, day=14) assert fixClublogApi.lookup_callsign("VU9KV", d) == response_Exception_VU9KV_with_Date assert fixClublogApi.lookup_callsign("DH1TW/MM") == response_Exception_DH1TW_MM assert fixClublogApi.lookup_callsign("DH1TW/AM") == response_Exception_DH1TW_AM diff --git a/test/test_lookuplib_clublogxml.py b/test/test_lookuplib_clublogxml.py index c975cd2..5241729 100644 --- a/test/test_lookuplib_clublogxml.py +++ b/test/test_lookuplib_clublogxml.py @@ -1,13 +1,10 @@ import pytest -from datetime import datetime -import pytz +from datetime import datetime, timezone import os from pyhamtools.lookuplib import LookupLib from pyhamtools.exceptions import APIKeyMissingError -UTC = pytz.UTC - #Fixtures #=========================================================== @@ -142,40 +139,40 @@ class TestclublogXML_Getters: #=============================== def test_lookup_callsign_same_callsign_different_exceptions(self, fixClublogXML): - timestamp = datetime(year=1990, month=10, day=12, tzinfo=UTC) + timestamp = datetime(year=1990, month=10, day=12, tzinfo=timezone.utc) assert fixClublogXML.lookup_callsign("kc6mm", timestamp) == response_Exception_KC6MM_1990 - timestamp = datetime(year=1992, month=3, day=8, tzinfo=UTC) + timestamp = datetime(year=1992, month=3, day=8, tzinfo=timezone.utc) assert fixClublogXML.lookup_callsign("kc6mm", timestamp) == response_Exception_KC6MM_1992 def test_lookup_callsign_exception_only_with_start_date(self, fixClublogXML): #timestamp > startdate - timestamp = datetime(year=1962, month=7, day=7, tzinfo=UTC) + timestamp = datetime(year=1962, month=7, day=7, tzinfo=timezone.utc) assert fixClublogXML.lookup_callsign("vk9xo", timestamp) == response_Exception_VK9XO_with_start_date assert fixClublogXML.lookup_callsign("vk9xo") == response_Exception_VK9XO_with_start_date #timestamp < startdate - timestamp = datetime(year=1962, month=7, day=5, tzinfo=UTC) + timestamp = datetime(year=1962, month=7, day=5, tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.lookup_callsign("vk9xo", timestamp) def test_lookup_callsign_exception_only_with_end_date(self, fixClublogXML): #timestamp < enddate - timestamp = datetime(year=1975, month=9, day=14, tzinfo=UTC) + timestamp = datetime(year=1975, month=9, day=14, tzinfo=timezone.utc) assert fixClublogXML.lookup_callsign("vk9xx", timestamp) == response_Exception_VK9XX_with_end_date # timestamp > enddate with pytest.raises(KeyError): fixClublogXML.lookup_callsign("vk9xx") - timestamp = datetime(year=1975, month=9, day=16, tzinfo=UTC) + timestamp = datetime(year=1975, month=9, day=16, tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.lookup_callsign("vk9xx", timestamp) def test_lookup_callsign_exception_no_start_nor_end_date(self, fixClublogXML): - timestamp = datetime(year=1975, month=9, day=14, tzinfo=UTC) + timestamp = datetime(year=1975, month=9, day=14, tzinfo=timezone.utc) assert fixClublogXML.lookup_callsign("ax9nyg", timestamp) == response_Exception_AX9NYG assert fixClublogXML.lookup_callsign("ax9nyg" ) == response_Exception_AX9NYG @@ -196,29 +193,29 @@ class TestclublogXML_Getters: def test_lookup_prefix_with_changing_entities(self, fixClublogXML): #return old entity (PAPUA TERR) - timestamp = datetime(year=1975, month=9, day=14).replace(tzinfo=UTC) + timestamp = datetime(year=1975, month=9, day=14, tzinfo=timezone.utc) assert fixClublogXML.lookup_prefix("VK9", timestamp) == response_Prefix_VK9_until_1975 #return empty dict - Prefix was not assigned at that time - timestamp = datetime(year=1975, month=9, day=16).replace(tzinfo=UTC) + timestamp = datetime(year=1975, month=9, day=16, tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.lookup_prefix("VK9", timestamp) #return new entity (Norfolk Island) - timestamp = datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.now(timezone.utc) assert fixClublogXML.lookup_prefix("VK9", timestamp ) == response_Prefix_VK9_starting_1976 def test_lookup_prefix_with_entities_having_start_and_stop(self, fixClublogXML): - timestamp_before = datetime(year=1964, month=11, day=1).replace(tzinfo=UTC) + timestamp_before = datetime(year=1964, month=11, day=1, tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.lookup_prefix("ZD5", timestamp_before) - timestamp_valid = datetime(year=1964, month=12, day=2).replace(tzinfo=UTC) + timestamp_valid = datetime(year=1964, month=12, day=2, tzinfo=timezone.utc) assert fixClublogXML.lookup_prefix("ZD5", timestamp_valid) == response_Prefix_ZD5_1964_to_1971 - timestamp_after = datetime(year=1971, month=8, day=1).replace(tzinfo=UTC) + timestamp_after = datetime(year=1971, month=8, day=1, tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.lookup_prefix("ZD5", timestamp_after) @@ -234,8 +231,8 @@ class TestclublogXML_Getters: fixClublogXML.is_invalid_operation("dh1tw") #Invalid Operation with start and end date - timestamp_before = datetime(year=1993, month=12, day=30).replace(tzinfo=UTC) - timestamp = datetime(year=1994, month=12, day=30).replace(tzinfo=UTC) + timestamp_before = datetime(year=1993, month=12, day=30, tzinfo=timezone.utc) + timestamp = datetime(year=1994, month=12, day=30, tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.is_invalid_operation("vk0mc") @@ -246,7 +243,7 @@ class TestclublogXML_Getters: #Invalid Operation with start date assert fixClublogXML.is_invalid_operation("5W1CFN") - timestamp_before = datetime(year=2012, month=1, day=31).replace(tzinfo=UTC) + timestamp_before = datetime(year=2012, month=1, day=31, tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.is_invalid_operation("5W1CFN", timestamp_before) @@ -264,9 +261,9 @@ class TestclublogXML_Getters: assert fixClublogXML.lookup_zone_exception("dp0gvn") == 38 #zone exception with start and end date - timestamp = datetime(year=1992, month=10, day=2).replace(tzinfo=UTC) - timestamp_before = datetime(year=1992, month=9, day=30).replace(tzinfo=UTC) - timestamp_after = datetime(year=1993, month=3, day=1).replace(tzinfo=UTC) + timestamp = datetime(year=1992, month=10, day=2, tzinfo=timezone.utc) + timestamp_before = datetime(year=1992, month=9, day=30, tzinfo=timezone.utc) + timestamp_after = datetime(year=1993, month=3, day=1, tzinfo=timezone.utc) assert fixClublogXML.lookup_zone_exception("dl1kvc/p", timestamp) == 38 with pytest.raises(KeyError): @@ -276,6 +273,6 @@ class TestclublogXML_Getters: fixClublogXML.lookup_zone_exception("dl1kvc/p", timestamp_after) #zone exception with start date - timestamp_before = datetime(year=2013, month=12, day=26).replace(tzinfo=UTC) + timestamp_before = datetime(year=2013, month=12, day=26,tzinfo=timezone.utc) with pytest.raises(KeyError): fixClublogXML.lookup_zone_exception("dh1hb/p", timestamp_before) diff --git a/test/test_lookuplib_qrz.py b/test/test_lookuplib_qrz.py index 8a4476e..b63cc4b 100644 --- a/test/test_lookuplib_qrz.py +++ b/test/test_lookuplib_qrz.py @@ -1,16 +1,12 @@ import os import pytest -from datetime import datetime +from datetime import datetime, timezone from pyhamtools.lookuplib import LookupLib from pyhamtools.exceptions import APIKeyMissingError from pyhamtools.consts import LookupConventions as const -import pytz -UTC = pytz.UTC - - try: QRZ_USERNAME = str(os.environ['QRZ_USERNAME']) QRZ_PWD = str(os.environ['QRZ_PWD']) @@ -21,10 +17,10 @@ except Exception: #=========================================================== response_1A1AB = { - u'biodate': datetime(2018, 9, 7, 21, 17, 7, tzinfo=UTC), + u'biodate': datetime(2018, 9, 7, 21, 17, 7, tzinfo=timezone.utc), u'bio': u'0', u'license_class': u'C', - u'moddate': datetime(2008, 11, 2, 15, 0, 38, tzinfo=UTC), + u'moddate': datetime(2008, 11, 2, 15, 0, 38, tzinfo=timezone.utc), u'locator': u'JN61fw', u'callsign': u'1A1AB', u'addr2': u'00187 Rome', diff --git a/test/test_lookuplib_redis.py b/test/test_lookuplib_redis.py index 2ccb0dc..b244c3c 100644 --- a/test/test_lookuplib_redis.py +++ b/test/test_lookuplib_redis.py @@ -1,15 +1,11 @@ import pytest import json -from datetime import datetime +from datetime import datetime, timezone -import pytz import redis from pyhamtools import LookupLib, Callinfo - -UTC = pytz.UTC - r = redis.Redis() @@ -44,7 +40,7 @@ class TestStoreDataInRedis: with pytest.raises(KeyError): fix_redis.is_invalid_operation("VK0MC") - timestamp = datetime(year=1994, month=12, day=30).replace(tzinfo=UTC) + timestamp = datetime(year=1994, month=12, day=30, tzinfo=timezone.utc) assert fix_redis.is_invalid_operation("VK0MC", timestamp) with pytest.raises(KeyError): @@ -61,7 +57,7 @@ class TestStoreDataInRedis: assert lib.lookup_prefix("DH") == fixCountryFile.lookup_prefix("DH") def test_redis_lookup(self, fixClublogXML, fix_redis): - timestamp = datetime(year=2016, month=1, day=20, tzinfo=UTC) + timestamp = datetime(year=2016, month=1, day=20, tzinfo=timezone.utc) ci = Callinfo(fix_redis) assert ci.get_all("VP8STI", timestamp) == response_Exception_VP8STI_with_start_and_stop_date assert ci.get_all("tu5pct") == response_TU5PCT