gpt4 book ai didi

带有被访问键内存的 Python 字典?

转载 作者:太空狗 更新时间:2023-10-29 20:28:38 24 4
gpt4 key购买 nike

我想创建一个数据结构,其行为类似于字典,并具有一个附加功能,即跟踪哪些键已被“使用”。请注意,我不能只在值被重用时弹出它们。

该结构应支持这三种情况,即在访问时将 key 标记为已使用:

if key in d:
...
d[key]
d.get(key)

这是我写的:

class DictWithMemory(dict):

def __init__(self, *args, **kwargs):
self.memory = set()
return super(DictWithMemory, self).__init__(*args, **kwargs)

def __getitem__(self, key):
self.memory.add(key)
return super(DictWithMemory, self).__getitem__(key)

def __contains__(self, key):
self.memory.add(key)
return super(DictWithMemory, self).__contains__(key)

def get(self, key, d=None):
self.memory.add(key)
return super(DictWithMemory, self).get(key, d)

def unused_keys(self):
"""
Returns the list of unused keys.
"""
return set(self.keys()).difference(self.memory)

由于我对dict的内部不是很熟悉,有没有更好的方法来实现这个结果?

最佳答案

这是一个将所有内容抽象到元类中的解决方案。我不确定这是否真的更优雅,但如果您改变了对如何存储使用过的 key 的想法,它确实提供了一定程度的封装:

class KeyRememberer(type):

def __new__(meta, classname, bases, classDict):
cls = type.__new__(meta, classname, bases, classDict)

# Define init that creates the set of remembered keys
def __init__(self, *args, **kwargs):
self.memory = set()
return super(cls, self).__init__(*args, **kwargs)
cls.__init__ = __init__

# Decorator that stores a requested key in the cache
def remember(f):
def _(self, key, *args, **kwargs):
self.memory.add(key)
return f(self, key, *args, **kwargs)
return _

# Apply the decorator to each of the default implementations
for method_name in [ '__getitem__', '__contains__', 'get' ]:
m = getattr(cls, method_name)
setattr(cls, method_name, remember(m))

return cls


class DictWithMemory(dict):

# A metaclass that ensures the object
# has a set called 'memory' as an attribute,
# which is updated on each call to __getitem__,
# __contains__, or get.
__metaclass__ = KeyRememberer

def unused_keys(self):
"""
Returns the list of unused keys.
"""
print "Used", self.memory
return list(set(super(DictWithMemory,
self).keys()).difference(self.memory))

关于带有被访问键内存的 Python 字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10758095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com