Correct len and iter operators for LazyDict

pull/4426/head
Jimmy Brisson 2017-06-16 15:28:10 -05:00
parent 27c55d5c8d
commit 7513d4f897
1 changed files with 9 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from time import time, sleep
from types import ListType
from shutil import copyfile
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir, normcase
from itertools import chain
from inspect import getmro
from copy import deepcopy
from tools.config import Config
@ -48,6 +49,8 @@ class LazyDict(dict):
self.lazy = {}
def add_lazy(self, key, thunk):
if key in self.eager:
del self.eager[key]
self.lazy[key] = thunk
def __getitem__(self, key):
@ -63,6 +66,12 @@ class LazyDict(dict):
def __contains__(self, key):
return key in self.eager or key in self.lazy
def __iter__(self):
return chain(iter(self.eager), iter(self.lazy))
def __len__(self):
return len(self.eager) + len(self.lazy)
def update(self, other):
if isinstance(other, LazyDict):
self.eager.update(other.eager)