Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
RestorableDict(): support creation of a new key
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Stinner committed Jun 22, 2010
1 parent 7a05acf commit 6394366
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions sandbox/restorable_dict.py
Expand Up @@ -2,10 +2,14 @@ class RestorableDict:
def __init__(self, dict):
self.dict = dict
self.original = {}
self.delete = set()

def __setitem__(self, key, value):
if key not in self.original:
self.original[key] = self.dict[key]
if (key not in self.original) and (key not in self.delete):
if key in self.dict:
self.original[key] = self.dict[key]
else:
self.delete.add(key)
self.dict[key] = value

def __delitem__(self, key):
Expand All @@ -15,6 +19,8 @@ def copy(self):
return self.dict.copy()

def restore(self):
for key in self.delete:
del self.dict[key]
self.dict.update(self.original)
self.original.clear()

0 comments on commit 6394366

Please sign in to comment.