如何在修改时创建一个标记自己的字典? [英] How can I make a dictionary that marks itself when it's modified?

查看:135
本文介绍了如何在修改时创建一个标记自己的字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重要的是,我可以在没有

将其标记为已修改的情况下阅读dict的内容,但我希望它在我添加的时刻设置标志

a新元素或改变现有元素(dict中的值是

mutable),这就是困难的原因。因为这些值是可变的,所以我不认为你可以分辨一下读取和写入
之间的区别,而不会在它们周围形成某种包装。


不过,我很想听听你们会怎么做。


谢谢,

-Sandra

It''s important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don''t think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I''d love to hear how you guys would do it.

Thanks,
-Sandra

推荐答案

sa ***********@yahoo.com 写道:
重要的是我可以阅读dict的内容而不用标记它修改后,但是我希望它在我添加新元素或改变现有元素的时刻设置标志(dict中的值是可变的),这就是困难的原因。因为这些值是可变的,所以我不认为你可以分辨读和写之间的区别,而不用在它们周围做一些包装。

仍然,我我很想听听你们会怎么做。
It''s important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don''t think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I''d love to hear how you guys would do it.




如果字典很小且速度不重要,你可以把它包起来

在一个类中捕捉__getitem__和__setitem__并测试

如果repr(self)发生变化。

-

------- -------------------------------------------------- -

| Radovan Garab?* k http://kassiopeia.juls.savba.sk/~ garabik / |

| __..-- ^^^ --..__ garabik @ kassiopeia.juls.savba.sk |

------------------- ----------------------------------------

防病毒警报:文件.signature被签名病毒感染。

嗨!我是一个签名病毒!将我复制到您的签名文件中以帮助我传播!



if the dictionary is small and speed not important, you can wrap it
in a class catching __getitem__ and __setitem__ and testing
if repr(self) changes.
--
-----------------------------------------------------------
| Radovan Garab?*k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I''m a signature virus! Copy me into your signature file to help me spread!


ga ****************** @ kassiopeia.juls.savba.sk 写道:
sa***********@yahoo.com 写道:
sa***********@yahoo.com wrote:
重要的是我可以读取字典的内容而不将其标记为已修改,但我希望它在我添加的时刻设置标记
仍然,我我很想知道你们会怎么做。
It''s important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don''t think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I''d love to hear how you guys would do it.



如果字典很小且速度不重要,你可以将它包装在一个类__getitem__和__setitem__中和测试
如果repr(自我)改变。


if the dictionary is small and speed not important, you can wrap it
in a class catching __getitem__ and __setitem__ and testing
if repr(self) changes.



d = {1:[a,b],

2:[ b,c]}


d [1] [0] = 3

这怎么样? __getitem __()将被调用一次,以获得对列表的

引用,因此没有机会在''和'之前比较

'' '之后''repr()值。


问候

Steve

-

史蒂夫Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www。 holdenweb.com

PyCon TX 2006 www.python .org / pycon /


d = {1: [a, b],
2: [b, c]}

d[1][0] = 3

How would this work? __getitem__() will be called once, to get a
reference to the list, and so there''s no opportunity to compare the
''before'' and ''after'' repr() values.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


sa *********** @ yahoo.com 写道:
重要的是我可以在没有
仍然,我我很想听听你们会怎么做。

谢谢,
-Sandra
It''s important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don''t think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I''d love to hear how you guys would do it.

Thanks,
-Sandra




Sandra


您似乎想要:


D = {" a":obj1," b":obj2}

当obj1或obj2发生变化时,
通知D


我认为在一般情况下很难做到(即对于任何obj值)。你需要设计/修改obj1和obj2或将它们包装起来才能实现这个目标。如果

对象不是同质的,我会放弃;-)


如果你可以为此目的设计obj它不应该是努力,使用

观察者模式:


这里是草图:


类Observable(对象) ):

"""<""""""""""""""""

def __init __(self):

self._observers = []

def add_observer(self,observer):

self._observers.append(观察者)

def _notify (self,attrname,newval):

为self._observers中的观察者:

observer.notify(self,attrname,newval)

def __setattr __(self,attrname,val):

object .__ setattr __(self,attrname,val)

self._notify(attrname,val)

class DictObserver(dict):

"""观察Observable实例的dict""

def __setitem __(self,key ,价值):

assert isinstance(value,Observable)

dict .__ setitem __(self,key,value)

value.add_observer(self)

def notify( self,obj,attr,newval):

print"%s。%s =%s" %(obj,attr,newval)



Sandra

You appear to want:

D = {"a":obj1, "b":obj2}

when obj1 or obj2 changes, to notify D

I think that''s hard to do in the general case (i.e., for any value of obj). You
will have to design/modify obj1 and obj2 or wrap them it to achieve this. If
the objects are not homogeneous, I''d give up ;-)

If you can design obj for the purpose it shouldn''t be do hard, using the
observer pattern:

Here''s a sketch:

class Observable(object):
"""An object that notifies observers when it is changed"""
def __init__(self):
self._observers = []
def add_observer(self, observer):
self._observers.append(observer)
def _notify(self, attrname, newval):
for observer in self._observers:
observer.notify(self, attrname, newval)
def __setattr__(self, attrname, val):
object.__setattr__(self, attrname, val)
self._notify(attrname, val)

class DictObserver(dict):
"""A dict that observes Observable instances"""
def __setitem__(self, key, value):
assert isinstance(value, Observable)
dict.__setitem__(self, key, value)
value.add_observer(self)
def notify(self, obj, attr, newval):
print "%s.%s = %s" % (obj, attr, newval)

a = Observable()
b = Observable()
D = DictObserver()
D [" a] = a
D [" b"] = b
a.new_attribute = 4
< observer_dict.Observable object at 0x01AE9210> .new_attribute = 4 b.new_attribute = 6
< observer_dict.Observable object at 0x01AE98F0> .new_attribute = 6 a.new_attribute = 5
< observer_dict.Observable object at 0x01AE9210> ; .new_attribute = 5
a = Observable()
b = Observable()
D = DictObserver()
D["a"] = a
D["b"] = b
a.new_attribute = 4 <observer_dict.Observable object at 0x01AE9210>.new_attribute = 4 b.new_attribute = 6 <observer_dict.Observable object at 0x01AE98F0>.new_attribute = 6 a.new_attribute = 5 <observer_dict.Observable object at 0x01AE9210>.new_attribute = 5



Michael


Michael


这篇关于如何在修改时创建一个标记自己的字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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