From 009bd230667bb722988e6aec69972ada6121d57d Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Tue, 31 Jul 2012 17:13:49 -0400 Subject: [PATCH] save LazyLoadingDict in case we do want it later --- common/lib/xmodule/lazy_dict.py | 58 +++++++++++++++++++++++ common/lib/xmodule/xmodule/xml_module.py | 60 ------------------------ 2 files changed, 58 insertions(+), 60 deletions(-) create mode 100644 common/lib/xmodule/lazy_dict.py diff --git a/common/lib/xmodule/lazy_dict.py b/common/lib/xmodule/lazy_dict.py new file mode 100644 index 0000000000..fa614843ec --- /dev/null +++ b/common/lib/xmodule/lazy_dict.py @@ -0,0 +1,58 @@ +from collections import MutableMapping + +class LazyLoadingDict(MutableMapping): + """ + A dictionary object that lazily loads its contents from a provided + function on reads (of members that haven't already been set). + """ + + def __init__(self, loader): + ''' + On the first read from this dictionary, it will call loader() to + populate its contents. loader() must return something dict-like. Any + elements set before the first read will be preserved. + ''' + self._contents = {} + self._loaded = False + self._loader = loader + self._deleted = set() + + def __getitem__(self, name): + if not (self._loaded or name in self._contents or name in self._deleted): + self.load() + + return self._contents[name] + + def __setitem__(self, name, value): + self._contents[name] = value + self._deleted.discard(name) + + def __delitem__(self, name): + del self._contents[name] + self._deleted.add(name) + + def __contains__(self, name): + self.load() + return name in self._contents + + def __len__(self): + self.load() + return len(self._contents) + + def __iter__(self): + self.load() + return iter(self._contents) + + def __repr__(self): + self.load() + return repr(self._contents) + + def load(self): + if self._loaded: + return + + loaded_contents = self._loader() + loaded_contents.update(self._contents) + self._contents = loaded_contents + self._loaded = True + diff --git a/common/lib/xmodule/xmodule/xml_module.py b/common/lib/xmodule/xmodule/xml_module.py index de62bc5d38..89f1dc4ed2 100644 --- a/common/lib/xmodule/xmodule/xml_module.py +++ b/common/lib/xmodule/xmodule/xml_module.py @@ -11,66 +11,6 @@ import os log = logging.getLogger(__name__) -# # TODO (cpennington): This was implemented in an attempt to improve performance, -# # but the actual improvement wasn't measured (and it was implemented late at night). -# # We should check if it hurts, and whether there's a better way of doing lazy loading - -# class LazyLoadingDict(MutableMapping): -# """ -# A dictionary object that lazily loads its contents from a provided -# function on reads (of members that haven't already been set). -# """ - -# def __init__(self, loader): -# ''' -# On the first read from this dictionary, it will call loader() to -# populate its contents. loader() must return something dict-like. Any -# elements set before the first read will be preserved. -# ''' -# self._contents = {} -# self._loaded = False -# self._loader = loader -# self._deleted = set() - -# def __getitem__(self, name): -# if not (self._loaded or name in self._contents or name in self._deleted): -# self.load() - -# return self._contents[name] - -# def __setitem__(self, name, value): -# self._contents[name] = value -# self._deleted.discard(name) - -# def __delitem__(self, name): -# del self._contents[name] -# self._deleted.add(name) - -# def __contains__(self, name): -# self.load() -# return name in self._contents - -# def __len__(self): -# self.load() -# return len(self._contents) - -# def __iter__(self): -# self.load() -# return iter(self._contents) - -# def __repr__(self): -# self.load() -# return repr(self._contents) - -# def load(self): -# if self._loaded: -# return - -# loaded_contents = self._loader() -# loaded_contents.update(self._contents) -# self._contents = loaded_contents -# self._loaded = True - _AttrMapBase = namedtuple('_AttrMap', 'metadata_key to_metadata from_metadata')