def __setitem__(self, key, value): """ stores a NewsitemFilter, and raises DuplicateFilterError if the key exists. """ if self.has_key(key): raise DuplicateFilterError(key) SortedDict.__setitem__(self, key, value)
def get_context_data(self, **kwargs): the_profile = kwargs userinfo = [model_to_dict(value, exclude=['id', 'slug', 'date_added_to_db']) for value in the_profile.values()][0] for key, info in userinfo.items(): # if there's no data associated (value empty), do not display empty string. if not userinfo[key]: userinfo.__delitem__(key) # Keys cannot be changed. Add a new key with the modified value then remove the old one, or create a new dict with a dict comprehension or the like. # just becuase django is NOT using sorted dict when handling models... and we want as less logic as possible in template # it must go ordered this way: 1.first 2.last 3.birthdate 4.country 6.contacts 5.bio sorted_fields = SortedDict({ 'first_name' : userinfo['first_name'], 'last_name' : userinfo['last_name'], }) # lot of calculations(going over and over keys) here - for large dbs that's ineffective if 'date_of_birth' in userinfo.keys(): sorted_fields.__setitem__("birth_date", userinfo['date_of_birth']) if 'country' in userinfo.keys(): sorted_fields.__setitem__("country", the_profile['object'].get_country_display()) # did not work in template as this is not the object but dictionary we iterate on if 'contacts' in userinfo.keys(): sorted_fields.__setitem__("contacts", userinfo['contacts']) if 'biography' in userinfo.keys(): sorted_fields.__setitem__("biography", userinfo['biography']) # sorted_fields.keyOrder.reverse() - here we might have an answer for upcoming task, let's inspect that later return { 'context' : sorted_fields }
class Node(object): """ A base class representing a navigation or page node and providing some dictionary-like behavior for navigating the tree. """ def __init__(self): super(Node, self).__init__() self.children = SortedDict() def __getitem__(self, key): return self.children.__getitem__(key) def __iter__(self): return self.children.__iter__() def __setitem__(self, key, value): return self.children.__setitem__(key, value) def __unicode__(self): return self.title def keys(self): return self.children.keys() def values(self): return self.children.values() @property def is_leaf(self): return not bool(self.children)