如何将所有参数放入webapp2中的数据结构(list/dict/etc)
How to get all parameters into a data structure (list/dict/etc) in webapp2
我正在编写一个简单的 webapp2 脚本,它接收传递给它的参数并输出结果。最后,我想将这些参数传递给特定的函数,具体取决于发出的请求类型。
代码:
class partyHandler(webapp2.RequestHandler):
def get(self, requestType):
PROCESS = partyRequests();
self.response.headers['Content-Type'] = 'text/plain'
#Here's where I would normally request the paramters,
#There's too many to list
#EX:
# name = cgi.escape(self.request.get('name'))
#
options = {
'create': PROCESS.createParty,
'retrieve': PROCESS.retrieveParty,
'update': PROCESS.updateParty,
'delete': PROCESS.deleteParty,
'join': PROCESS.joinParty,
'broadcast':PROCESS.broadcastParty,
'leave': PROCESS.leaveParty,
}
#THE PARAMETERS WOULD BE PASSED TO ANY FUNCTION ABOVE DEPENDING ON requestType.
return self.response.write(options[requestType](<PARAMS>))
app = webapp2.WSGIApplication([
('/party/(create|retrieve|update|delete|broadcast|join|leave)/', partyHandler)
], debug=True)
如您所见,有没有办法将所有参数收集到数据结构中(list/dictionary) 并将其传递给函数?
编辑
澄清一下,我的意思是
函数创建采用参数:
name
partyImg
latitudeRestaurant
longitudeRestaurant
address
函数检索采用参数:
partyId
我之前的做法是这样做的:
#IF I'M CALLING retrieve, THESE ARE POINTLESS
name = cgi.escape(self.request.get('name'))
partyImg = cgi.escape(self.request.get('partyImg'))
latitudeRestaurant = cgi.escape(self.request.get('latitudeRestaurant'))
longitudeRestaurant = cgi.escape(self.request.get('longitudeRestaurant'))
address = cgi.escape(self.request.get('address'))
#IF I'M CALLING create, THESE ARE POINTLESS
partyId = cgi.escape(self.request.get('partyId'))
options = {
'create': PROCESS.createParty(name, partyImg,latitudeRestaurant,longitudeRestaurant,address),
'retrieve': PROCESS.retrieveParty(partyId)
}
我不想请求单个参数,而是想将所有参数放入一个数据结构并将其传递给一个函数。
我相信你想用 *args 和 **kwargs 做点什么。
这是有关它的信息:
What does ** (double star) and * (star) do for parameters?
最终,对于 args 和 kwargs,它们采用任意参数集合,并且根据传递给谁,您可以在该方法中验证您期望的参数。
因此,例如,对于您的一种方法,例如创建,你需要参数 x 和 y,那么你的创建可能如下所示:
def create(*args, **kwargs):
## check for parameters x and y is available in kwargs here and proceed
查看请求的 webapp2 文档,我发现这个有效:
class partyHandler(webapp2.RequestHandler):
def get(self, requestType):
PROCESS = partyRequests();
self.response.headers['Content-Type'] = 'text/plain'
options = {
'create': PROCESS.createParty,
'retrieve': PROCESS.retrieveParty,
'update': PROCESS.updateParty,
'delete': PROCESS.deleteParty,
'join': PROCESS.joinParty,
'broadcast':PROCESS.broadcastParty,
'leave': PROCESS.leaveParty
}
self.response.write(options[requestType](self.request.GET)) <--THIS WORKS
调用这些方法时,它看起来像这样:
def create(self, args):
username = args['username']
...
我正在编写一个简单的 webapp2 脚本,它接收传递给它的参数并输出结果。最后,我想将这些参数传递给特定的函数,具体取决于发出的请求类型。
代码:
class partyHandler(webapp2.RequestHandler):
def get(self, requestType):
PROCESS = partyRequests();
self.response.headers['Content-Type'] = 'text/plain'
#Here's where I would normally request the paramters,
#There's too many to list
#EX:
# name = cgi.escape(self.request.get('name'))
#
options = {
'create': PROCESS.createParty,
'retrieve': PROCESS.retrieveParty,
'update': PROCESS.updateParty,
'delete': PROCESS.deleteParty,
'join': PROCESS.joinParty,
'broadcast':PROCESS.broadcastParty,
'leave': PROCESS.leaveParty,
}
#THE PARAMETERS WOULD BE PASSED TO ANY FUNCTION ABOVE DEPENDING ON requestType.
return self.response.write(options[requestType](<PARAMS>))
app = webapp2.WSGIApplication([
('/party/(create|retrieve|update|delete|broadcast|join|leave)/', partyHandler)
], debug=True)
如您所见,有没有办法将所有参数收集到数据结构中(list/dictionary) 并将其传递给函数?
编辑
澄清一下,我的意思是 函数创建采用参数:
name
partyImg
latitudeRestaurant
longitudeRestaurant
address
函数检索采用参数:
partyId
我之前的做法是这样做的:
#IF I'M CALLING retrieve, THESE ARE POINTLESS
name = cgi.escape(self.request.get('name'))
partyImg = cgi.escape(self.request.get('partyImg'))
latitudeRestaurant = cgi.escape(self.request.get('latitudeRestaurant'))
longitudeRestaurant = cgi.escape(self.request.get('longitudeRestaurant'))
address = cgi.escape(self.request.get('address'))
#IF I'M CALLING create, THESE ARE POINTLESS
partyId = cgi.escape(self.request.get('partyId'))
options = {
'create': PROCESS.createParty(name, partyImg,latitudeRestaurant,longitudeRestaurant,address),
'retrieve': PROCESS.retrieveParty(partyId)
}
我不想请求单个参数,而是想将所有参数放入一个数据结构并将其传递给一个函数。
我相信你想用 *args 和 **kwargs 做点什么。
这是有关它的信息: What does ** (double star) and * (star) do for parameters?
最终,对于 args 和 kwargs,它们采用任意参数集合,并且根据传递给谁,您可以在该方法中验证您期望的参数。
因此,例如,对于您的一种方法,例如创建,你需要参数 x 和 y,那么你的创建可能如下所示:
def create(*args, **kwargs):
## check for parameters x and y is available in kwargs here and proceed
查看请求的 webapp2 文档,我发现这个有效:
class partyHandler(webapp2.RequestHandler):
def get(self, requestType):
PROCESS = partyRequests();
self.response.headers['Content-Type'] = 'text/plain'
options = {
'create': PROCESS.createParty,
'retrieve': PROCESS.retrieveParty,
'update': PROCESS.updateParty,
'delete': PROCESS.deleteParty,
'join': PROCESS.joinParty,
'broadcast':PROCESS.broadcastParty,
'leave': PROCESS.leaveParty
}
self.response.write(options[requestType](self.request.GET)) <--THIS WORKS
调用这些方法时,它看起来像这样:
def create(self, args):
username = args['username']
...