使用 Python 中的 Schedule Module 从不同文件调用类方法

发布于 2025-01-11 17:03:46 字数 1655 浏览 1 评论 0原文

我正在尝试从不同文件中的类调用方法。这就是我的代码的样子:

ma​​in.py

###Imports
import funct as f
import schedule
import time
###More Imports

###Grabbing GPS data from user and assigning values

bboxsize = f.find(userLat, userLng, userRad)

if __name__ == '__main__':
    r = f.find.radar(bboxsize) ### <---- Line 14
    schedule.every(5).seconds.do(r)
    while True:
        schedule.run_pending()
        time.sleep(1)


funct.py

###Imports

class find:

    def __init__(self, userLat, userLng, userRad):
        self.userLat = userLat
        self.userLng = userLng
        self.userRad = userRad

    def bboxFormula(self):
    ### Lots of code that is not important to this
    return bboxsize

    def radar(self, bboxsize):
        api = OpenSkyApi(username='###', password='###')
        states = api.get_states(bbox=bboxsize)
        print(f"vvvvvv| {time.ctime(time.time())} |vvvvvv")
        print("------------------------------------------")
        for s in states.states:
            print("Callsign: %r, Latitude: %r, Longitude: %r, Altitude (meters): %r, Velocity %r)" % (s.callsign, s.latitude, s.longitude, s.geo_altitude, s.velocity))
        print("------------------------------------------")

运行此代码时,我得到:

Traceback (most recent call last):
  File "/##/##/##/##/main.py", line 14, in <module>
    r = f.find.radar(bboxsize)
TypeError: find.radar() missing 1 required positional argument: 'bboxsize'

我可以在一个文件中运行所有这些而不需要类,所以我知道它可以工作。我已经搞乱这个问题有一段时间了,我所做的每一个更改都会出现各种各样的错误。

我是否遗漏了某些内容,或者我无法使用计划模块来执行此操作?

I'm trying to call a method from a class that is in a different file. This is how my code looks:

main.py

###Imports
import funct as f
import schedule
import time
###More Imports

###Grabbing GPS data from user and assigning values

bboxsize = f.find(userLat, userLng, userRad)

if __name__ == '__main__':
    r = f.find.radar(bboxsize) ### <---- Line 14
    schedule.every(5).seconds.do(r)
    while True:
        schedule.run_pending()
        time.sleep(1)


funct.py

###Imports

class find:

    def __init__(self, userLat, userLng, userRad):
        self.userLat = userLat
        self.userLng = userLng
        self.userRad = userRad

    def bboxFormula(self):
    ### Lots of code that is not important to this
    return bboxsize

    def radar(self, bboxsize):
        api = OpenSkyApi(username='###', password='###')
        states = api.get_states(bbox=bboxsize)
        print(f"vvvvvv| {time.ctime(time.time())} |vvvvvv")
        print("------------------------------------------")
        for s in states.states:
            print("Callsign: %r, Latitude: %r, Longitude: %r, Altitude (meters): %r, Velocity %r)" % (s.callsign, s.latitude, s.longitude, s.geo_altitude, s.velocity))
        print("------------------------------------------")

When running this I get:

Traceback (most recent call last):
  File "/##/##/##/##/main.py", line 14, in <module>
    r = f.find.radar(bboxsize)
TypeError: find.radar() missing 1 required positional argument: 'bboxsize'

I can run all of this in one file without classes, so I know it works. I have been messing with this for a while getting all sorts of errors with every change I make.

Is there something that I'm missing or is there no way for me to do this with the Schedule Module?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨敛清荷 2025-01-18 17:03:46

我只是在这里猜测,但我认为当它都是一个文件时你有不同的代码。

现在有两个文件,我想你是想写这个:

find = f.find(userLat, userLng, userRad)
boxsize = find.bboxFormula()

if __name__ == '__main__':
    r = lambda :find.radar(bboxsize) ### <---- Line 14
    schedule.every(5).seconds.do(r)
    while True:
        schedule.run_pending()
        time.sleep(1)

I’m just guessing here, but I think you had different code when it was all one file.

Now with two files I think you meant to write this:

find = f.find(userLat, userLng, userRad)
boxsize = find.bboxFormula()

if __name__ == '__main__':
    r = lambda :find.radar(bboxsize) ### <---- Line 14
    schedule.every(5).seconds.do(r)
    while True:
        schedule.run_pending()
        time.sleep(1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文