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 __author__ = "Vratko Polak"
9 __copyright__ = "Copyright(c) 2015, Cisco Systems, Inc."
10 __license__ = "Eclipse Public License v1.0"
11 __email__ = "vrpolak@cisco.com"
13 import collections as _collections
16 class Hsfod(_collections.OrderedDict):
18 Hashable sorted (by key) frozen OrderedDict implementation stub.
20 Supports only __init__, __repr__ and __hash__ methods.
21 Other OrderedDict methods are available, but they may break contract.
24 def __init__(self, *args, **kwargs):
25 """Put arguments to OrderedDict, sort, pass to super, cache values."""
26 self_unsorted = _collections.OrderedDict(*args, **kwargs)
27 items_sorted = sorted(self_unsorted.items(), key=repr)
28 sup = super(Hsfod, self) # possibly something else than OrderedDict
29 sup.__init__(items_sorted)
30 # Repr string is used for sorting, keys are more important than values.
31 self.__repr = '{' + repr(self.keys()) + ':' + repr(self.values()) + '}'
32 self.__hash = hash(self.__repr)
35 """Return cached repr string."""
39 """Return cached hash."""