2

I'm writing a simple webapp2 script that takes in the parameters passed to it and spits out the result. Eventually I would like to pass those parameters to specific functions, depending on what kind of request is being made.

Code:

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)

As you can see, rather than make redundant requests for parameter values I might not need each time a request to the script is made, is there a way to collect all the parameters into a data structure (list/dictionary) and pass that to the functions?

EDIT

To clarify, what I mean is function create takes params:

name
partyImg
latitudeRestaurant
longitudeRestaurant
address

function retrieve takes params:

partyId

My earlier approach was doing this:

#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)
}

Instead of requesting individual params, I'd like to put all the params into a data structure and pass that to a function.

Phreakradio
  • 176
  • 1
  • 18

2 Answers2

1

I believe you are looking to do something with *args and **kwargs.

Here is information on it: What does ** (double star) and * (star) do for parameters?

Ultimately, with args and kwargs, they take an arbitrary collection of parameters and depending on who they are being passed to, you can validate inside that method for the params you are expecting.

So, for example, for one of your methods, e.g. create, you want parameters x and y, then your create might look like this:

def create(*args, **kwargs):
 ## check for parameters x and y is available in kwargs here and proceed
Community
  • 1
  • 1
idjaw
  • 25,487
  • 7
  • 64
  • 83
0

Looking at the webapp2 documentation for requests, I found out this works:

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

When those methods are called, it looks like this:

def create(self, args):
    username = args['username']
    ...
Phreakradio
  • 176
  • 1
  • 18