-1

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 web.input() in a core module 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?

4
  • 1
    Can you show what the imports are in your different modules, and what the global variables you're worried about are (and how they get initialized)? It's pretty hard to answer your question without a few more details.
    – Blckknght
    Sep 14, 2013 at 21:42
  • @Blckknght >what the imports are in your different modules Well, I don't quite understand your question. >what the global variables you're worried about are (and how they get initialized) in code.py I import web, create web.app() instance and use run() function of this instance to run web-server (after I've defined all the necessary classes for the pages, of course). Then, when web-server gets a GET or POST request, it puts all the GET or POST variables in a storage which can be accessed calling web.input() from the same main module where web.app() is running. That's all I know =( Sep 14, 2013 at 21:51
  • If web is a module, you should just be able to import it. All modules that import the same other module will get a reference to the same module object, so there should be no issues with values not showing up where they are supposed to. I'm not super clued in about web.py, so maybe there's something else going on, but your question doesn't really say what. Have you tried just importing web in addon?
    – Blckknght
    Sep 14, 2013 at 22:03
  • Oh, thank you very much! I really didn't know that reference thing, instead, I thought otherwise, as you can see. Thank you very much! Sep 14, 2013 at 22:13

1 Answer 1

1

I guess I'll turn my comment into an answer, since it seems to have solved your issue.

Modules that are imported are cached in Python. That means that when you import a module like web (the main web.py module) from multiple other modules, they'll all get the same module object, with the same contents.

So, probably all you need to do is import web at the top of your addon.py module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.