Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 动态地向类提供变量_Python_Saratoga - Fatal编程技术网

Python 动态地向类提供变量

Python 动态地向类提供变量,python,saratoga,Python,Saratoga,我尝试了官方教程提供的示例,发现如下: 我对代码做了一些更改,如下所示: http://23.21.167.60:8094/v1/yearlength?name=earth 我的问题是,我需要在URL中提供帐户=211829,就像name=earth一样 我在下面写的东西是有效的,因为我已经向班级提供了帐号。如何动态地执行此操作 import json from saratoga.api import SaratogaAPI, DefaultServiceClass class Plane

我尝试了官方教程提供的示例,发现如下:

我对代码做了一些更改,如下所示:

http://23.21.167.60:8094/v1/yearlength?name=earth
我的问题是,我需要在URL中提供帐户=211829,就像name=earth一样

我在下面写的东西是有效的,因为我已经向班级提供了帐号。如何动态地执行此操作

import json
from saratoga.api import SaratogaAPI, DefaultServiceClass

class PlanetServiceClass(DefaultServiceClass):
    def __init__(self, myaccount):
        self.yearLength = {
            "earth": self.myquery(myaccount),
            "pluto": {"seconds": 7816176000}
        }

    def myquery(self, myaccount):
        import pandas as pd

        query = ('select * from mydata198 where account = %s  ')  % (myaccount)

        import sqlalchemy
        engine = sqlalchemy.create_engine('mysql+pymysql://dba:pass@1.2.3.4/test')
        conn = engine.raw_connection()

        df=pd.read_sql(query, conn)
        return df.to_json()

class PlanetAPI(object):
    class v1(object):
        def yearlength_GET(self, request, params):
            planetName = params["params"]["name"].lower()
            return self.yearLength.get(planetName)

APIDescription = json.load(open("planets.json"))
myAPI = SaratogaAPI(PlanetAPI, APIDescription, serviceClass=PlanetServiceClass('211829'))
myAPI.run(port=8094)

如何将
account\u num
变量从
PlanetAPI
类传递到
PlanetServiceClass

您需要将url更改为

http://23.21.167.60:8094/v1/yearlength?name=earth&account_num=12345
然后在代码中,您可以通过

account_num = params["params"]["account_num"]
编辑:

问题是您当前正在使用
account\u num
在服务器运行之前对其进行初始化。因此,您需要在它运行后传递它

class PlanetAPI(object):
    class v1(object):
        def yearlength_GET(self, request, params):
            planetName = params["params"]["name"].lower()
            account_num = params["params"]["account_num"]

            mysql_result_json = self.myquery(account_num)
            self.yearLength['earth'] = mysql_result_json  # assign to earth if you want, as in your question
            return self.yearLength.get(planetName)
然后将其余代码更改回教程中的代码:

myAPI = SaratogaAPI(PlanetAPI, APIDescription, serviceClass=PlanetServiceClass())


我应该把我的API改成这个吗?serviceClass=PlanetServiceClass(帐户号)ok更新了答案。您需要在“请求”时间而不是服务器初始化时间分配
帐户。\u num
。谢谢。成功了。但只有在两个类中都复制myquery函数时,它才起作用。为什么?因为函数不是全局的。您需要使用点符号来访问正确对象中的对象。
class PlanetServiceClass(DefaultServiceClass):
    def __init__(self):