Saturday, 14 September 2013

Pointer-like behaviour in Python

Pointer-like behaviour in Python

I have two modules. One is the core of the website based on web.py (let's
name it code.py), and other is an add-on module (addon.py). Using web.py,
for each page that the website server there should be class definition in
a core, like that:
class Page:
def GET(self):
variable = "Hello!"
return render.page_template(variable) #Here, it returns the rendered
template to user
def POST(self):
post_variables = web.input()
pass #Doing something with those variables, maybe, writing in a database...
Now I really need to move that class definition from code.py to addon.py.
I can refer to the class definition as a addon.Page instead of simply
Page. The Page.GET function still works well... But there's one problem
with POST. It seems like at the each call of POST function in a module
web.input() is being set as a storage object storing all the variables.
And if my class definition is being stored in addon, the core simply calls
addon.Page.POST() (I see no way to change this behaviour). The POST()
tries to get web.input... And fails, of course - web is not imported in
addon.py, and even if it was, there wouldn't be any value web.py
web-server is getting - just empty dictionary, it would be just another
instance of the module. So i don't know...
One solution would be: putting some kind of function in addon.Page.POST().
This function would go one level down, to code.py and execute web.input()
there, and return it back, to addon.py, some kind of accessing parent
module namespace (like doing import __main__ and accessing
main.web.input() (which, as I know, is discouraged) ).
Or, for example, putting some kind of C-like pointer that would be shared
between the modules, like:
* in code.py there's definition that all the calls to
code.addon.web_input() get routed to code.web.input()
* in addon.py - there's simply need to call addon.web_input to get info
from code.web.input()
What do I do in this situation? There will be multiple addons, each with
the class definition stored in this addon, and I should be able to add new
modules, connect and disconnect existing modules easily, without any need
to modify code.py. I believe this is possible in Python... Maybe web.py
source needs modifying then?

No comments:

Post a Comment