modules.webhosting.c = db.cursor() modules.webhosting.Something() webhosting需要共同的方法和用户: 来自模块导入常用,用户 但是用户也需要共同点: 来自模块导入常见 他们都需要访问数据库 (用户和常用) 来自yellowfish导入数据库 c = db.cursor() 任何人都可以给我建议让这一切更透明吗?我想b $ b猜我真的想要一个方法将所有这些文件放到同一个 范围内,使一切似乎都是一个应用程序,即使 所有内容都分解为不同的文件。 这个特定应用程序增加了一个复杂功能: 我使用模块因为我''调用以某种XML 格式定义的任意方法。显然我想要牢记安全性,所以我的应用程序 就是这样的: 导入模块 模块,方法,args = getXmlAction() m = getattr(模块,模块) mc = db.cursor() f = getattr(m,方法) f(args) 在PHP中这种方法非常好,因为我可以包含我需要的所有文件,每个文件都需要包含一个类,我可以使用变量变量: <?phpdb` and bring it in to scope,but in Python I''m having trouble keeping the the database in scope. At themoment I''m having to "push" the database into the module, but I''d preferthe module to bring the database connection in ("pull") from its parent.Eg:import modulesmodules.foo.c = db.cursor()modules.foo.Bar()Can anyone recommend any "cleaner" solutions to all of this? As far as Ican see it, Python doesn''t have much support for breaking down largeprograms in to organisable files and referencing each other.Another problem is I keep having to import modules all over the place. Areal example is, I have a module "webhosting", a module "users", and amodule "common". These are all submodules of the module "modules" (badnaming I know). The database connection is instantiated on the "db"variable of my main module, which is "yellowfish" (a global module), soget the situation where:(yellowfish.py)import modulesmodules.webhosting.c = db.cursor()modules.webhosting.Something()webhosting needs methods in common and users:from modules import common, usersHowever users also needs common:from modules import commonAnd they all need access to the database(users and common)from yellowfish import dbc = db.cursor()Can anyone give me advice on making this all a bit more transparent? Iguess I really would like a method to bring all these files in to the samescope to make everything seem to be all one application, even thougheverything is broken up in to different files.One added complication in this particular application:I used modules because I''m calling arbitrary methods defined in some XMLformat. Obviously I wanted to keep security in mind, so my applicationgoes something like this:import modulesmodule, method, args = getXmlAction()m = getattr(modules, module)m.c = db.cursor()f = getattr(m, method)f(args)In PHP this method is excellent, because I can include all the files Ineed, each containing a class, and I can use variable variables:<?php class = newclass = new 这篇关于多个模块具有数据库访问+通用应用程序设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 18:54