[英]Python(Flask RESTful API) Modifying Objects
I am having an issue with an object (configuration object) that I want both the python program and external API users to modify. 我在同时希望python程序和外部API用户修改的对象(配置对象)上遇到问题。 If I use the API and PUT new data, the data is saved. 如果我使用API并输入新数据,则会保存数据。 If I have the Python program try modifying the object, I cannot observe that change when doing a GET.As you can see, I change config.encoding to "test", but when doing a GET, I do not observe the change. 如果我有Python程序尝试修改该对象,则在执行GET时无法观察到该更改。如您所见,我将config.encoding更改为“ test”,但是在执行GET时,我没有观察到该更改。 I still see encoding set to "ASCII". 我仍然看到编码设置为“ ASCII”。 I am relatively new to Python and may be overlooking something simple. 我对Python比较陌生,可能忽略了一些简单的知识。
Thanks. 谢谢。
config = util.Config()
class api_Config(Resource):
def get(self, id):
return [{"id": config.id,
"capture": config.capture,
"controller": config.controller,
"gateway_ip_address": config.gateway_ip_address,
"port": config.port,
"interval_rate": config.interval_rate,
"encoding": config.encoding}], 200
def put(self, id):
parser = reqparse.RequestParser()
parser.add_argument("capture")
parser.add_argument("controller")
parser.add_argument("gateway_ip_address")
parser.add_argument("port")
parser.add_argument("interval_rate")
parser.add_argument("encoding")
args = parser.parse_args()
config.capture = args["capture"]
config.controller = args["controller"]
config.gateway_ip_address = args["gateway_ip_address"]
config.port = args["port"]
config.interval_rate = args["interval_rate"]
config.encoding = args["encoding"]
return [{"id": config.id,
"capture": config.capture,
"controller": config.controller,
"gateway_ip_address": config.gateway_ip_address,
"port": config.port,
"interval_rate": config.interval_rate,
"encoding": config.encoding}], 200
if __name__== "__main__":
app = Flask(__name__)
api = Api(app)
config = util.Config()
api.add_resource(api_Config, '/<string:id>')
app.run(debug=True)
print config.encoding
config.encoding = "test"
There are two places at which you are creating your config
object. 您可以在两个地方创建config
对象。 One is at the very first line, where you say config = util.Config()
which looks like either util.Config
is a class and then util.Config()
returns an instance of that class or it is a function and whatever that function returns will be assigned to config
. 一个在第一行,您说config = util.Config()
看起来像util.Config
是一个类,然后util.Config()
返回该类的实例或它是一个函数以及该函数的任何内容返回值将分配给config
。 Either way, that will be the config
object used by your api_Config.get
method. 无论哪种方式,这都是api_Config.get
方法使用的config
对象。
The second place where you create an instance of this config object is after your if __name__== "__main__":
block, which will be an entirely new object and will not be used by those api handlers. 创建此配置对象实例的第二个位置是if __name__== "__main__":
块之后,它将是一个全新的对象,并且不会被这些api处理程序使用。
It might be enough for you to remove that second definition and use 删除第二个定义并使用可能就足够了
if __name__== "__main__":
app = Flask(__name__)
api = Api(app)
api.add_resource(api_Config, '/<string:id>')
app.run(debug=True)
print config.encoding
config.encoding = "test"
without modifying the rest. 不修改其余部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.