如何防止对象的类更改



我在带有对象的烧瓶应用程序中遇到问题。第一次运行函数时,它运行得很好。但是,第二次运行时,我收到一个错误,即"str"对象没有属性SubmitFeedResult。我可以通过重新启动应用程序并再次运行函数(然后运行良好)来解决此问题。运行函数后,对象正在更改为字符串,我想知道是否有办法防止对象的类更改为字符串(我倾向于否,因为它这样做是由于我正在使用的库中的源代码),或者解决这个问题的好方法是什么。为了使应用程序为其他用户成功运行,我不能有一个类str

的对象几个月前我问过这个问题,简短地解决了它,但现在问题又回来了。这是某些上下文的原始帖子的链接:"str"对象没有属性"SubmitFeedResult">

这是主要功能:

@app.route('/submission/', methods=['GET','POST'])
def feed_submission():
if request.method == 'POST':
file = request.files['file']
# Only XML files allowed
if not allowed_filetype(file.filename):
output = '<h2 style="color:red">Filetype must be XML! Please upload an XML file.</h2>'
return output
raise ValueError('Filetype Must Be XML.')
# Read file and encode it for transfer to Amazon
file_name = request.form['file_name']
f = file.read()
u = f.decode("utf-8-sig")
c = u.encode("utf-8")
feed_content = c
submit_feed_response = conn.submit_feed(
FeedType=feed_operation(file_name),
PurgeAndReplace=False,
MarketplaceIdList=[MARKETPLACE_ID],
content_type='text/xml',
FeedContent=feed_content)
feed_info = submit_feed_response.SubmitFeedResult.FeedSubmissionInfo.FeedSubmissionId
return redirect(url_for('feed_result', feed_id=feed_info))
else:
return render_template('submit_feed.html',access_key=MWS_ACCESS_KEY,secret_key=MWS_SECRET_KEY,
merchant_id=MERCHANT_ID,marketplace_id=MARKETPLACE_ID)

此函数运行后,它会重定向到此函数以返回请求的响应:

@app.route('/feed-result/<int:feed_id>')
def feed_result(feed_id):
response = MWSConnection._parse_response = lambda s,x,y,z: z
submitted_feed = conn.get_feed_submission_result(FeedSubmissionId=feed_id)
result = Response(submitted_feed, mimetype='text/xml')
return(result)

我在问题发生之前和之后记录了submit_feed_response的类型和值的信息,以查看导致错误的原因:

typeof1 = type(submit_feed_response)
val1 = submit_feed_response
typeof_conn1 = type(conn)
app.logger.warning("Type of submit_feed_response (original): " + str(typeof1))
app.logger.warning("Value of submit_feed_response: " + str(val1))

在日志中,当应用程序正常运行时,我看到:

Type of submit_feed_response (original): <class 'boto.mws.response.SubmitFeedResponse'>
Value of submit_feed_response: SubmitFeedResponse{u'xmlns': u'http://mws.amazonaws.com/doc/2009-01-01/'}(SubmitFeedResult: SubmitFeedResult{}(FeedSubmissionInfo: FeedSubmissionInfo{}...

失败后,我看到(如预期的那样)类型为字符串:

Type of submit_feed_response: <type 'str'>
Value of submit_feed_response: <?xml version="1.0"?>

......

以下是回溯:

Traceback (most recent call last):
File "C:Python27libsite-packagesflaskapp.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "C:Python27libsite-packagesflaskapp.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:Python27libsite-packagesflaskapp.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:Python27libsite-packagesflaskapp.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "C:Python27libsite-packagesflaskapp.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:Python27libsite-packagesflaskapp.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:Python27libsite-packagesflaskapp.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "C:Python27libsite-packagesflaskapp.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:UsersCTS 41DocumentsAmazon-appapplication.py", line 98, in feed_submission
feed_info = submit_feed_response.SubmitFeedResult.FeedSubmissionInfo.FeedSubmissionId
AttributeError: 'str' object has no attribute 'SubmitFeedResult'

似乎库导致对象在运行后返回字符串。有没有一个好的,蟒蛇的方法解决这个问题?即我是否能够以某种方式更改对象的类以确保它保持不变,或者将其从内存中完全删除?我已经尝试了除块之外的尝试,但返回相同的错误。

编辑

我已将问题缩小到:

response = MWSConnection._parse_response = lambda s,x,y,z: z在函数feed_result中。它在那里从 boto 返回 XML 响应(调用不会自然以 XML 格式返回,我不确定另一种以 XML 格式返回它的方法,请参阅如何从 boto 调用返回 XML?

我决定插入

MWSConnection._parse_response = None线上方

feed_info = feed.SubmitFeedResult.FeedSubmissionInfo.FeedSubmissionId,但是它仍然返回相同的错误。有没有办法在运行一次后从内存中清除此函数?我需要这个函数来正确提供响应,但也许有更好的方法?

我能够解决这个问题。如问题所述,该问题源于feed_result中的线路response = MWSConnection._parse_response = lambda s,x,y,z: z。此行是将响应解析为 XML 并为其提供服务所必需的,这会导致在调用此函数后将提交源的响应解析为 XML 字符串。为了解决这个问题,我检查了feed是否不是字符串,并像最初一样检索了 ID:

if type(feed) is not str:
feed_info = feed.SubmitFeedResult.FeedSubmissionInfo.FeedSubmissionId

如果它是一个字符串,则使用 ElementTree 将其解析为 XML 字符串以检索 ID:

else:
tree = et.fromstring(feed)
xmlns = {'response': '{http://mws.amazonaws.com/doc/2009-01-01/}'}
info = tree.find('.//{response}FeedSubmissionId'.format(**xmlns))
feed_info = info.text

现在无需重新启动应用程序即可正常工作。

相关内容

  • 没有找到相关文章

最新更新