1 """This module contains single class, to store a sorted dict."""
2 # Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 # This program and the accompanying materials are made available under the
5 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 # and is available at http://www.eclipse.org/legal/epl-v10.html
8 import collections as _collections
11 __author__ = "Vratko Polak"
12 __copyright__ = "Copyright(c) 2015, Cisco Systems, Inc."
13 __license__ = "Eclipse Public License v1.0"
14 __email__ = "vrpolak@cisco.com"
17 class Hsfod(_collections.OrderedDict):
19 Hashable sorted (by key) frozen OrderedDict implementation stub.
21 Supports only __init__, __repr__ and __hash__ methods.
22 Other OrderedDict methods are available, but they may break contract.
25 def __init__(self, *args, **kwargs):
26 """Put arguments to OrderedDict, sort, pass to super, cache values."""
27 self_unsorted = _collections.OrderedDict(*args, **kwargs)
28 items_sorted = sorted(self_unsorted.items(), key=repr)
29 sup = super(Hsfod, self) # possibly something else than OrderedDict
30 sup.__init__(items_sorted)
31 # Repr string is used for sorting, keys are more important than values.
32 self.__repr = '{' + repr(self.keys()) + ':' + repr(self.values()) + '}'
33 self.__hash = hash(self.__repr)
36 """Return cached repr string."""
40 """Return cached hash."""