Created
July 9, 2009 15:33
-
-
Save willb/143743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ord_dict(dict): | |
""" ord_dict is a dict that remembers the order in which its keys were originally inserted""" | |
def __init__(self): | |
self.ord_keys = [] | |
def keys(self): | |
return list(self.ord_keys) | |
def __setitem__(self, key, value): | |
if not self.has_key(key): | |
self.ord_keys.append(key) | |
super(ord_dict, self).__setitem__(key, value) | |
def __iter__(self): | |
def od_iterator(ls, od): | |
for val in ls: | |
yield ((val, od[val])) | |
return od_iterator(self.ord_keys, self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment