From: Kevin H. <kh...@pe...> - 2003-05-25 21:00:40
|
Hi, I've been moving to using Python from PHP and have been playing around with Spyce, which so far has been quite impressive. As an leaning experience I created a module which mimics PHP's extremenly useful GET/POST variable interperetation. ---------------------------- Variable interperetation ---------------------------- For example, the following fields: <input type="hidden" name="person[name]" value="Joe Smith"> <input type="hidden" name="person[email]" value="js...@ho..."> Would normally produce 2 request variables: >> print request.post() { 'person[name]': 'Joe Smith', 'person[email]': 'js...@ho...' } However, if you were to then import the module: [[.import name=xrequest]] The keys are interpereted as list references resulting in a variable named "person": >> print request.post1('person') { 'name':'Joe Smith', 'email': 'js...@ho...' } ---------------------------- Setting session variables ---------------------------- Another feature I think would be useful would be the auto-setting of session variables using a $ prefix for request field names: Regular field name, does not affect session: ?email=js...@ho... Prefixed field name. Sets the session variable "email" to "js...@ho..." ?$email=js...@ho... ---------------------------- Questions ---------------------------- I attemped to add the session prefix function, but am still a little confused by sessions. Could someone give me a simple example of how to simply get then set a single session variable? Comments? I think that these features would fit quite cleanly into the Spyce framework and add some powerful functionality. But then I've only used Spyce for a short time... The module is included below. Ideally if this were a feature to be added to Spyce, it would be incorporated into the Request module, where we would need only to parse the _get and _post variables. As mentioned, this was more of an learning exercise. ---------------------------- xrequest module ---------------------------- import re from types import DictType from spyceModule import spyceModulePlus class xrequest (spyceModulePlus): def init(self): # handles get/post variables self.setRequestVars() # session variables? would set variables prefixed with $ # self.setSessionVars() def setRequestVars(self): request = self._api.getModule('request') get = request.get1() self.setDictVars(request._get, get) self.setDictVars(request._get1, get) self.setDictVars(request._getL, get) self.setDictVars(request._getL1, get) post = request.post1() self.setDictVars(request._post, post) self.setDictVars(request._post1, post) self.setDictVars(request._postL, post) self.setDictVars(request._postL1, post) def setDictVars(self, dictdata, updates): reIsSpecific = re.compile('^\$') reGetVar = re.compile('^(\w+)(\[|$)') reIsNested = re.compile('^(\w+)\[') reGetNested = re.compile('\[(\w+)\]') for key, value in updates.items(): if reIsSpecific.match(key): key = key[1:] setvar = reGetVar.findall(key)[0][0] data = dictdata.get(setvar) if not reIsNested.match(key): data = value else: subVars = reGetNested.findall(key) if type(data) != DictType: data = {} count, var = 0, data for x in subVars: count += 1 if count == len(subVars): var[x] = value else: if not var.has_key(x) or var.has_key(x) and type(var[x]) != DictType: var[x] = {} var = var[x] dictdata[setvar] = data if not data and dictdata.has_key(setvar): del dictdata[setvar] |