帮助创建良好的数据模型 [英] Help Create Good Data Model

查看:61
本文介绍了帮助创建良好的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在修改我写的一个小应用程序,以便从UI中分离数据

。作为一个开始,我想创建一个铁壳数据插座

,它将保留所有重要值,并经得起各种来源,可能同时查询

。很可能,应用程序

永远不需要任何强大的东西,但我想学习写它

无论如何,作为练习。所以这是我的代码。它真的很简单,而且我确定你可以看到我的Java背景。这里有什么问题吗?

有什么我想念或搞砸了吗?我欢迎任何和所有反馈,

特别是如果它包括*为什么'。*谢谢!


#!/ usr / bin / python

#author mwt

#Mar''06


导入副本,穿线


类FAHData(对象):

"""F @ H监视器的数据模型。""


def __init __(self):

self.data = {} #this dict将保存所有数据

self.mutex = threading.RLock()

def get_all_data(self):

"""返回整个数据字典的COPY。""

#not sure deepcopy()在这里真的是必要的

#但现在使用它

#might导致一些奇怪的同步问题...

尝试:

self.mutex.acquire()

返回copy.deepcopy(self.data)

终于:

self .mutex.release()


def get_data(self,key):

"""返回< key的COPY >数据元素。""

尝试:

self.mutex.acquire()

返回copy.deepcopy(self.data [关键])

终于:

self.mutex.release()


def set_value(self,key,value) :

"""设置< key>的值数据元素。""

尝试:

self.mutex.acquire()

self.data [key] = value

终于:

self.mutex.release()


def set_data(self,data):

"""设置整个数据字典。""

尝试:

self.mutex.acquire()

self.data = data

终于:

self.mutex.release()


def clear_data(self) :

"""清除整个数据字典。""

尝试:

self.mutex.acquire( )

self.data = {}

终于:

self.mutex.release()

Hi. I''m reworking a little app I wrote, in order to separate the data
from the UI. As a start, I wanted to create a iron-clad data recepticle
that will hold all the important values, and stand up to being queried
by various sources, perhaps concurrently. In all likelihood, the app
will never need anything that robust, but I want to learn to write it
anyway, as an exercise. So here is my code. It''s really simple, and I''m
sure you can see my Java background. Are there any problems here?
Something I''m missing or screwing up? I welcome any and alll feedback,
especially if it includes the *why''s.* Thanks!

#!/usr/bin/python
# author mwt
# Mar ''06

import copy, threading

class FAHData(object):
"""The data model for the F@H monitor."""

def __init__(self):
self.data = {}#this dict will hold all data
self.mutex = threading.RLock()

def get_all_data(self):
"""Returns a COPY of entire data dict."""
#not sure deepcopy() is really necessary here
#but using it for now
#might cause some weird synchronization problems...
try:
self.mutex.acquire()
return copy.deepcopy(self.data)
finally:
self.mutex.release()

def get_data(self, key):
"""Returns a COPY of <key> data element."""
try:
self.mutex.acquire()
return copy.deepcopy(self.data[key])
finally:
self.mutex.release()

def set_value(self, key, value):
"""Sets value of <key> data element."""
try:
self.mutex.acquire()
self.data[key] = value
finally:
self.mutex.release()

def set_data(self, data):
"""Sets entire data dictionary."""
try:
self.mutex.acquire()
self.data = data
finally:
self.mutex.release()

def clear_data(self):
"""Clears entire data dictionary."""
try:
self.mutex.acquire()
self.data = {}
finally:
self.mutex.release()

推荐答案

没有什么真正*破坏*跳出来对我。最后三个

方法(set_value,set_data和clear_data)可能不需要一个

互斥量,因为它们每个都有自己的帧,并且操作

是原子的。如果这没有意义,那么谷歌的Python GIL也是如此。 )。如果你只是从字典中返回一个值而不是使用副本,那么对于get方法可能会说同样的

- 这取决于你是否存储

self.data中的可变对象。


当你完成练习并希望坚持这些值时

某处,试试Dejavu: http://projects.amor.org/dejavu/

Robert Brewer

系统架构师

Amor Ministries
fu ****** @ amor.org

There''s nothing really *broken* jumping out at me. The last three
methods (set_value, set_data, and clear_data) probably don''t need a
mutex, since they will each have their own frame, and the operations
are atomic. If that makes no sense, Google for "Python GIL" ;). If you
just returned a value from the dict instead of using copy, the same
might be said for the get methods--it depends on whether you''re storing
mutable objects in self.data or not.

When you''re done with the exercise and want to persist those values
somewhere, give Dejavu a try: http://projects.amor.org/dejavu/
Robert Brewer
System Architect
Amor Ministries
fu******@amor.org


mwt启发我们:
我正在修改我写的一个小应用程序,以便将数据从UI中分离出来。


好​​主意。

首先,我想创建一个包含所有重要值的铁壳数据插座,以及经得起各种来源的质疑,可能会同时进行。
I''m reworking a little app I wrote, in order to separate the data
from the UI.
Good idea.
As a start, I wanted to create a iron-clad data recepticle that will
hold all the important values, and stand up to being queried by
various sources, perhaps concurrently.




如果你能拥有SQLite数据库,为什么要自己这样做呢? SQLite甚至可以支持内存数据库。无需重新发明轮子。


Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa



Why do that yourself, if you can have SQLite databases? SQLite is even
capable of in-memory databases. No need to re-invent the wheel.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


fumanchu:有趣。我正在努力理解原子性。另外,由于

我希望这个类使用Observer模式工作,我已经很复杂了

的东西,如下所示。我会调查Dejavu的持久性(虽然

大部分基本值都存在于其他地方,所以这个应用程序将

主要只需要内存值和配置内容(对此我认为
认为ConfigParser可能就足够了。)


Sybren:这是一个很酷的选择,但我不认为对于我想要做的事情来说,这是正确的。比我现在做的更大规模的矫枉过正。

另外,我正在尝试写这个东西,以便我可以把它*任何东西*

(一个值,一个列表,另一个字典,整个对象等),这可能是

艰难有一个DB。


这就是我在这一点上做的事情(很大程度上来自Bruce

Eckel,以及融入了fumanchu如果你得到一个

的机会,请告诉我你的想法。

#!/ usr / bin / python

#author mwt

#Mar''06

进口警察y,线程,可观察

类FAHData(可观察):

"""F @ H监视器的数据模型。 ;"


def __init __(自我):

Observable .__ init __(self)

self.data = {}#这个dict将保存所有数据

self.mutex = threading.RLock()


def get_all_data(self):

" ;""返回整个数据字典的COPY。"

#不确定deepcopy()在这里真的是必要的

#but使用它现在

#might导致一些奇怪的同步问题...

试试:

self.mutex.acquire()

返回copy.deepcopy(self.data)

终于:

self.mutex.release()


def get_data (self,key):

"""返回< key>的COPY数据元素。""

尝试:

self.mutex.acquire()

返回copy.deepcopy(self.data [关键])

终于:

self.mutex.release()


#thethe这三种方法都不需要一个互斥体,因为它们是原子的(我想b $ b):

#---------------------- ---------------------->

def set_value(self,key,value):

"""设置< key>的值数据元素。"""

self.data [key] = value

Observable.notifyObservers(self,arg =''ELEMENT_CHANGED'')


def set_data(self,data):

"""设置整个数据字典。"""

self.data = data

Observable.notifyObservers(self,arg =''DATA_CHANGED'')

def clear_data(self):

"""清除整个数据字典。""

self.data = {}

Observable.notifyObservers(self,arg = ''DATA_CHANGED'')

#< --------------------------------- ------------

#!/ usr / bin / python

#author mwt

#Mar' '06

导入线程


类观察者(对象):

def更新(可观察,arg):

""" OVERRIDE ME""

pass

class Observable(object):

def __init__ (个体经营):

self.o bs = []

self.mutex = threading.RLock()


def addObserver(self,observer):

self .mutex.aquire()

尝试:

如果观察者不在self.obs中:

self.obs.append(observer)

最后:

self.mutex.release()


def notifyObservers(self,arg = None):

self.mutex.aquire()

尝试:

localArray = self.obs [:]

终于:

self.mutex.release()

for localArray中的观察者:

observer.update(self,arg)


#these方法不需要互斥,因为它们是原子的(我想b $ b):

#------------ -------------------------------->


def deleteObserver(self ,观察员):

self.obs.remove(观察员)


def deleteObservers(self):

self.obs = []


def countObservers(个体经营):

返回len(self.obs)


#< ------------------------------ ---------------


mwt

fumanchu: Interesting. I''m trying to understand atomicity. Also, since
I want this class to work using the Observer pattern, I''ve complicated
things, as shown below. I''ll look into Dejavu for persistence (although
most of the basic values are persisted elsewhere, so this app will
mainly need only in-memory values and configuration stuff (for which I
think a ConfigParser will probably be enough).

Sybren: That''s a cool choice, but I don''t think it''s right for what I''m
trying to do. Even more massive overkill than what I''m already doing.
Plus, I''m trying to write this thing so that I can hand it *anything*
(a value, a list, another dict, whole objects, etc.), which might be
tough with a DB.

Here''s what I''ve got cooking at this point (adapted heavily from Bruce
Eckel, as well as incorporating fumanchu''s corrections). If you get a
chance, please let me know what you think.
#!/usr/bin/python
# author mwt
# Mar ''06
import copy, threading, observable

class FAHData(Observable):
"""The data model for the F@H monitor."""

def __init__(self):
Observable.__init__(self)
self.data = {}#this dict will hold all data
self.mutex = threading.RLock()

def get_all_data(self):
"""Returns a COPY of entire data dict."""
#not sure deepcopy() is really necessary here
#but using it for now
#might cause some weird synchronization problems...
try:
self.mutex.acquire()
return copy.deepcopy(self.data)
finally:
self.mutex.release()

def get_data(self, key):
"""Returns a COPY of <key> data element."""
try:
self.mutex.acquire()
return copy.deepcopy(self.data[key])
finally:
self.mutex.release()

#these three methods don''t need a mutex because they are atomic (I
think):
#-------------------------------------------->
def set_value(self, key, value):
"""Sets value of <key> data element."""
self.data[key] = value
Observable.notifyObservers(self, arg = ''ELEMENT_CHANGED'')

def set_data(self, data):
"""Sets entire data dictionary."""
self.data = data
Observable.notifyObservers(self, arg = ''DATA_CHANGED'')

def clear_data(self):
"""Clears entire data dictionary."""
self.data = {}
Observable.notifyObservers(self, arg = ''DATA_CHANGED'')
#<---------------------------------------------
#!/usr/bin/python
# author mwt
# Mar ''06
import threading

class Observer(object):
def update(observable, arg):
"""OVERRIDE ME"""
pass
class Observable(object):
def __init__(self):
self.obs = []
self.mutex = threading.RLock()

def addObserver(self, observer):
self.mutex.aquire()
try:
if observer not in self.obs:
self.obs.append(observer)
finally:
self.mutex.release()

def notifyObservers(self, arg = None):
self.mutex.aquire()
try:
localArray = self.obs[:]
finally:
self.mutex.release()
for observer in localArray:
observer.update(self, arg)

#these methods don''t need a mutex because they are atomic (I
think):
#-------------------------------------------->

def deleteObserver(self, observer):
self.obs.remove(observer)

def deleteObservers(self):
self.obs = []

def countObservers(self):
return len(self.obs)

#<---------------------------------------------

mwt


这篇关于帮助创建良好的数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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