从多个代码模块访问数据 [英] Data access from multiple code modules

查看:54
本文介绍了从多个代码模块访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个由3个文件组成的应用程序。一个main.py

文件,gui.py和一个处理持久数据存储的data.py.

假设data.py定义了一个类''MyDB'',它读取在来自

数据库的数据中,main.py创建此对象的实例。如何在gui.py中使用代码

访问此对象?这里是简化的伪代码:


MAIN.PY

import gui,data

DataObject = data.MyDB(blah)


如何在gui.py中编写可以访问DataObject的代码?这是否完全是错误的方法来处理这类问题?


因为GUI由一个主要的组成,所以问题更加复杂

GUI表单,以及在单独文件中定义为单独对象的面板。

各种面板将包含用于操作

DataObject中数据的控件,或者数据存储结束的位置祝你好运。

致以诚挚的问候,


Simon Hibbs

(有时会为了解决这个OOP问题而喋喋不休)。

Lets say that I have an application consisting of 3 files. A main.py
file, gui.py and a data.py which handles persistent data storage.
Suppose data.py defines a class ''MyDB'' which reads in data from a
database, and main.py creates an instance of this object. How does code
in gui.py access this object? Here''s simplified pseudocode:

MAIN.PY
import gui, data
DataObject = data.MyDB(blah)

How do I write code in gui.py that can access DataObject? Is this
entirely the wrong way to approach this sort of problem?

Actualy the problem is more complex because the GUI consists of a main
GUI form, and panels defined as seperate objects in seperate files.
Various panels will contain controlls for manipulating data in the
DataObject, or wherever data storage end up.
Best regards,

Simon Hibbs
(who strugles to get his head round this OOP stuff sometimes).

推荐答案


si ********* @ gmail.com 写道:

假设我有一个由3个文件组成的应用程序。一个main.py

文件,gui.py和一个处理持久数据存储的data.py.

假设data.py定义了一个类''MyDB'',它读取在来自

数据库的数据中,main.py创建此对象的实例。如何在gui.py中使用代码

访问此对象?这里是简化的伪代码:


MAIN.PY

import gui,data

DataObject = data.MyDB(blah)


如何在gui.py中编写可以访问DataObject的代码?这是否完全是错误的方法来处理这类问题?


因为GUI由一个主要的组成,所以问题更加复杂

GUI表单,以及在单独文件中定义为单独对象的面板。

各种面板将包含用于操作

DataObject中数据的控件,或者数据存储结束的位置起来。
Lets say that I have an application consisting of 3 files. A main.py
file, gui.py and a data.py which handles persistent data storage.
Suppose data.py defines a class ''MyDB'' which reads in data from a
database, and main.py creates an instance of this object. How does code
in gui.py access this object? Here''s simplified pseudocode:

MAIN.PY
import gui, data
DataObject = data.MyDB(blah)

How do I write code in gui.py that can access DataObject? Is this
entirely the wrong way to approach this sort of problem?

Actualy the problem is more complex because the GUI consists of a main
GUI form, and panels defined as seperate objects in seperate files.
Various panels will contain controlls for manipulating data in the
DataObject, or wherever data storage end up.



main.py有什么作用?你是在创建一个gui thingy的实例吗?

如果是这样的话,你可以将DataObject传递到你的gui thingy中,或者将

传递给构造函数或者创建一个setter它的实例。如果

gui在实例化时需要任何数据库内容,你可能需要将b $ b传递给构造函数。但是,一个main.py,gui.py和

db.py闻起来有点像你的标准MVC,在这种情况下,你会

让你的控制器通过在GUI需要它们的数据中。


- Jeremy M. Jones

What does main.py do? Are you creating an instance of the gui thingy?
If so, you could just pass DataObject into your gui thingy either into
the constructor or to a setter once you create an instance of it. If
the gui needs any database stuff at instantiation time, you probably
need to pass it into the constructor. However, a main.py, gui.py, and
db.py smells a little like your standard MVC, in which case, you would
get your controller to pass in the data pieces as the GUI needs them.

- Jeremy M. Jones


si ********* @ gmail.com 写道:

假设我有一个由3个文件组成的应用程序。一个main.py

文件,gui.py和一个处理持久数据存储的data.py.

假设data.py定义了一个类''MyDB'',它读取在来自

数据库的数据中,main.py创建此对象的实例。如何在gui.py中使用代码

访问此对象?
Lets say that I have an application consisting of 3 files. A main.py
file, gui.py and a data.py which handles persistent data storage.
Suppose data.py defines a class ''MyDB'' which reads in data from a
database, and main.py creates an instance of this object. How does code
in gui.py access this object?



显然,您需要传递与此代码相关的任何内容...

Obviously, you need to pass whatever relevant to this code...


这里''简化的伪代码:


MAIN.PY

import gui,data

DataObject = data.MyDB(blah)


如何在gui.py中编写可以访问DataObject的代码?
Here''s simplified pseudocode:

MAIN.PY
import gui, data
DataObject = data.MyDB(blah)

How do I write code in gui.py that can access DataObject?



将DataObject传递给guy.py中的代码似乎是一个非常直接的

解决方案...

Passing DataObject to code in guy.py seems like a very straightforward
solution...


这是

完全是解决这类问题的错误方法吗?
Is this
entirely the wrong way to approach this sort of problem?


Actualy问题更复杂,因为GUI包含一个主要的
GUI表单,以及定义为单独对象的面板在单独的文件中。

各种面板将包含用于操作

DataObject中数据的控件,或者数据存储最终的位置。
Actualy the problem is more complex because the GUI consists of a main
GUI form, and panels defined as seperate objects in seperate files.
Various panels will contain controlls for manipulating data in the
DataObject, or wherever data storage end up.



嗯...很难说没看到代码(我已经很长时间没有完成GUI

程序员) ,但是在GUI中放置应用程序逻辑

通常是一个坏主意IME。最好将应用程序逻辑放在一个单独的控制器(它有一个模型的引用 - 你的

DataObject)中,并在控制器上调用GUI部分。

Hmmm... Hard to tell without seeing the code (and I''ve not done GUI
programmer for a long time), but putting application logic in the GUI is
usually a bad idea IME. Better to put the application logic in a
separate controler (that has a reference to the model -here your
DataObject), and have the GUI part call on the controler.


>

祝你好运,

Simon Hibbs

(有时候他会为了这个OOP的东西而喋喋不休)。
>
Best regards,

Simon Hibbs
(who strugles to get his head round this OOP stuff sometimes).



嗯,实际上可以在没有OO的情况下进行MVC。这更像是分离问题的问题。现在我同意,并不总是很明显知道

确定哪一部分应该对什么负责......


-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for w in p .split(''。'')])

p in''o **** @ xiludom.gro''。split(''''')])"

Well, actually one can do MVC without OO. It''s more a matter of
separating concerns. Now I agree that it''s not always obvious to know
for sure which part should be responsible for what...

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"




Jeremy Jones写道:

Jeremy Jones wrote:

main.py有什么作用?你是在创建一个gui thingy的实例吗?

如果是这样的话,你可以将DataObject传递到你的gui thingy中,或者将

传递给构造函数或者创建一个setter它的实例。
What does main.py do? Are you creating an instance of the gui thingy?
If so, you could just pass DataObject into your gui thingy either into
the constructor or to a setter once you create an instance of it.



这是一个wxPython应用程序。我首先使用wxGlade创建了GUI,

给了我一个小的myapp.py脚本,一个包含主应用程序的文件

frame(包含一个listbook controll)和几个包含的文件

小组课程。每个面板类包含用于对数据集执行各种操作的控件(添加记录,删除它们,

运行各种转换)。我不能说我明白了这一切

在很深的层次上都有效,尽管到目前为止我已经成功地将它完成了很多次。
。 >

大概如果我将DataObject传递给Frame对象,并从

传递到Panel对象,那么可能这将解决

万阿英,蒋达清?我想它会通过引用传递所以所有的面板

都会处理相同的数据对象?


Doh!多么简单为什么我没想到这个?我已经习惯了程序性的b $ b脚本,你只需将所有东西放在全局数据结构中。我知道这很糟糕,但很难摆脱这种心态。


非常感谢,


Simon Hibbs


PS Oreillynet上您的博客的常规读者。

It''s a wxPython app. I created the GUI initialy using wxGlade which
gave me a small myapp.py script, a file containing the main application
frame (containing a listbook controll) and several files containing
panel classes. Each panel class contains controlls for performing
various operations on the data set (adding records, deleting them,
running various transformations). I can''t say I understand how it all
works at a deep level, although I''ve been hacking it about quite
successfuly so far.

Presumably if I pass DataObject through to the Frame object, and from
there through to the Panel objects then presumably this will solve the
probelm? I guess it would be passed by reference so all the panels
would be working on the same data object?

Doh! How simple. Why didn''t I think of that? I''m too used to procedural
scripts where you''d just put everything in a global data structure. I
know this is bad, but it''s hard to get out of that mentality.

Many thanks,

Simon Hibbs

P.S. Regular reader of your blog on Oreillynet.


这篇关于从多个代码模块访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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