多个模块具有数据库访问+通用应用程序设计 [英] Multiple modules with database access + general app design?

查看:57
本文介绍了多个模块具有数据库访问+通用应用程序设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿人们


我是一名经验丰富的PHP程序员,他现在已经花了几个星期的时间编写python。我正在编写一个相当大的应用程序,我已决定将b $ b分解为大量模块(替换PHP的include()

语句)。


我的问题是,在PHP中,如果你打开数据库连接,它在脚本的持续时间内总是在

范围内。即使您使用抽象层

($ db = DB :: connect(...)),您也可以`global $ db`并将其带入范围,

但是在Python中我很难将数据库保留在范围内。在

那一刻,我不得不推出来。将数据库放入模块中,但是我更愿意使用模块将数据库连接(拉)与其父模块连接起来。


例如:

导入模块

modules.foo.c = db.cursor()

modules.foo.Bar()


任何人都可以推荐任何清洁剂所有这些的解决方案?至于我b $ b可以看到它,Python并没有太多支持将大量的b $ b程序分解为可组织文件并相互引用。


另一个问题是我不得不在整个地方导入模块。一个实际的例子是,我有一个模块虚拟主机,一个模块用户,以及一个b&b模块普通模块。这些都是模块模块的子模块。 (坏的

命名我知道)。数据库连接在我的主模块的db

变量上实例化,该变量是yellowfish。 (一个全球模块),所以

得到以下情况:


(yellowfish.py)

导入模块
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中这种方法非常好,因为我可以包含我需要的所有文件,每个文件都需要包含一个类,我可以使用变量变量:


<?php

$ class = new $ module; //不记得这是否有效,有

//替代品虽然

$ class-> $ method($ args);

?>


和$ class-> $ method()只做global $ db; $ db->查询(...);" ;.


任何建议都将不胜感激!


干杯

-Robin Haswell

Hey people

I''m an experience PHP programmer who''s been writing python for a couple of
weeks now. I''m writing quite a large application which I''ve decided to
break down in to lots of modules (replacement for PHP''s include()
statement).

My problem is, in PHP if you open a database connection it''s always in
scope for the duration of the script. Even if you use an abstraction layer
($db = DB::connect(...)) you can `global $db` and bring it in to scope,
but in Python I''m having trouble keeping the the database in scope. At the
moment I''m having to "push" the database into the module, but I''d prefer
the module to bring the database connection in ("pull") from its parent.

Eg:
import modules
modules.foo.c = db.cursor()
modules.foo.Bar()

Can anyone recommend any "cleaner" solutions to all of this? As far as I
can see it, Python doesn''t have much support for breaking down large
programs in to organisable files and referencing each other.

Another problem is I keep having to import modules all over the place. A
real example is, I have a module "webhosting", a module "users", and a
module "common". These are all submodules of the module "modules" (bad
naming I know). The database connection is instantiated on the "db"
variable of my main module, which is "yellowfish" (a global module), so
get the situation where:

(yellowfish.py)
import modules
modules.webhosting.c = db.cursor()
modules.webhosting.Something()

webhosting needs methods in common and users:

from modules import common, users

However users also needs common:

from modules import common

And they all need access to the database

(users and common)
from yellowfish import db
c = db.cursor()

Can anyone give me advice on making this all a bit more transparent? I
guess I really would like a method to bring all these files in to the same
scope to make everything seem to be all one application, even though
everything 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 XML
format. Obviously I wanted to keep security in mind, so my application
goes something like this:

import modules
module, 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 I
need, each containing a class, and I can use variable variables:

<?php
$class = new $module; // can''t remember if this works, there are
// alternatves though
$class->$method($args);
?>

And $class->$method() just does "global $db; $db->query(...);".

Any advice would be greatly appreciated!

Cheers

-Robin Haswell

推荐答案

db = DB :: connect(...))你可以`global
db = DB::connect(...)) you can `global


db`并将其带入范围,

但是在Python中我很难将数据库保留在范围内。在

那一刻,我不得不推出来。将数据库放入模块中,但是我更愿意使用模块将数据库连接(拉)与其父模块连接起来。


例如:

导入模块

modules.foo.c = db.cursor()

modules.foo.Bar()


任何人都可以推荐任何清洁剂所有这些的解决方案?至于我b $ b可以看到它,Python并没有太多支持将大量的b $ b程序分解为可组织文件并相互引用。


另一个问题是我不得不在整个地方导入模块。一个实际的例子是,我有一个模块虚拟主机,一个模块用户,以及一个b&b模块普通模块。这些都是模块模块的子模块。 (坏的

命名我知道)。数据库连接在我的主模块的db

变量上实例化,该变量是yellowfish。 (一个全球模块),所以

得到以下情况:


(yellowfish.py)

导入模块
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中这种方法非常好,因为我可以包含我需要的所有文件,每个文件都需要包含一个类,我可以使用变量变量:


<?php
db` and bring it in to scope,
but in Python I''m having trouble keeping the the database in scope. At the
moment I''m having to "push" the database into the module, but I''d prefer
the module to bring the database connection in ("pull") from its parent.

Eg:
import modules
modules.foo.c = db.cursor()
modules.foo.Bar()

Can anyone recommend any "cleaner" solutions to all of this? As far as I
can see it, Python doesn''t have much support for breaking down large
programs in to organisable files and referencing each other.

Another problem is I keep having to import modules all over the place. A
real example is, I have a module "webhosting", a module "users", and a
module "common". These are all submodules of the module "modules" (bad
naming I know). The database connection is instantiated on the "db"
variable of my main module, which is "yellowfish" (a global module), so
get the situation where:

(yellowfish.py)
import modules
modules.webhosting.c = db.cursor()
modules.webhosting.Something()

webhosting needs methods in common and users:

from modules import common, users

However users also needs common:

from modules import common

And they all need access to the database

(users and common)
from yellowfish import db
c = db.cursor()

Can anyone give me advice on making this all a bit more transparent? I
guess I really would like a method to bring all these files in to the same
scope to make everything seem to be all one application, even though
everything 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 XML
format. Obviously I wanted to keep security in mind, so my application
goes something like this:

import modules
module, 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 I
need, each containing a class, and I can use variable variables:

<?php


class = new
class = new


这篇关于多个模块具有数据库访问+通用应用程序设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆