How to fix: TypeError: _get_unique_zone() missing 1 required positional argument: 'shortcut_id_y'?

How to fix: TypeError: _get_unique_zone() missing 1 required positional argument: 'shortcut_id_y'?

我是 Python 的新手,正在尝试编写一个可以根据经度计算位置的实时时间的代码。

它已经工作了,但是现在,它需要参数 self fullfilled。我定义了 self,现在已经解决了,但是现在如果我尝试使用 offset(City, self=tf),它会给我这个错误:

  Traceback (most recent call last):

  File "C:\Users\Marvi\PycharmProjects\pythonProject1\main.py", line 55, in <module>
    GMTcor = time_in - offset(City, self=tf) * 60 + realTime
  File "C:\Users\Marvi\PycharmProjects\pythonProject1\main.py", line 45, in offset
    tz_target = timezone(tf.certain_timezone_at(self,lat=target['lat'], lng=target['lng']))
  File "C:\Users\Marvi\anaconda3\envs\pythonProject1\lib\site-packages\timezonefinder\timezonefinder.py", line 744, in certain_timezone_at
    timezone = self._get_unique_zone(shortcut_id_x, shortcut_id_y)
TypeError: _get_unique_zone() missing 1 required positional argument: 'shortcut_id_y'

而且我不知道如何修复它...请帮助我! 整个代码在这里:(我知道这很乱)

from geopy.geocoders import Nominatim
from datetime import timedelta
from pytz import timezone
import pytz
import datetime
from timezonefinder import TimezoneFinder

address = input("Insert place: ")
geolocator = Nominatim(user_agent="Time Correction Calculator")
location = geolocator.geocode(address)
print(location.address)

t = input("Insert time (hh:mm): ")
(h, m) = t.split(':') 
time_in = int(h) * 60 + int(m) 


date_input = input("Insert date (dd.mm.yyyy): ")

utc = pytz.utc
tf = TimezoneFinder

def offset(target, self):
    today = datetime.datetime.strptime(date_input, '%d.%m.%Y') # reading of date_in and Splitting it
    tz_target = timezone(tf.certain_timezone_at(self,lat=target['lat'], 
    lng = target['lng'])) # Searching timezone by coordinates
    today_target = tz_target.localize(today)
    today_utc = utc.localize(today)
    return (today_utc - today_target).total_seconds() / 3600 # Calculating the timezone in hours

City = dict({'lat' : location.latitude, 'lng' : location.longitude})

realTime : float = location.longitude * 4 # Longitude to minutes
realTime = round(realTime)

Correction = time_in - offset(City, self=tf) * 60 + realTime # Calculating the real time in minutes
print(str(timedelta(hours = GMTcor / 60))[:-3]) # printing the real Time

尝试编辑一行:25,同时按坐标搜索时区。

替换行:25

tz_target = timezone(tf.certain_timezone_at(self,lat=target['lat'], lng=target['lng'])) # Searching timezone by coordinates

有:

tz_target = timezone(tf().certain_timezone_at(lat=target['lat'], lng=target['lng'])) # Searching timezone by coordinates

您需要使用 TimezoneFinder 的对象调用 certain_timezone_at() 而不是从 class 引用调用它。

例如tf().certain_timezone_at(lat=target['lat'], lng=target['lng'])

此外,调用对象级方法时无需传递self。这将由 python 解释器隐式传递。

看看这个代码差异。