Updated code to match new rules
[integration/test.git] / csit / libraries / HsfJson / hsfod.py
1 """This module contains single class, to store a sorted dict."""
2 # Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3 #
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
7
8 import collections as _collections
9
10
11 __author__ = "Vratko Polak"
12 __copyright__ = "Copyright(c) 2015, Cisco Systems, Inc."
13 __license__ = "Eclipse Public License v1.0"
14 __email__ = "vrpolak@cisco.com"
15
16
17 class Hsfod(_collections.OrderedDict):
18     """
19     Hashable sorted (by key) frozen OrderedDict implementation stub.
20
21     Supports only __init__, __repr__ and __hash__ methods.
22     Other OrderedDict methods are available, but they may break contract.
23     """
24
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)
34
35     def __repr__(self):
36         """Return cached repr string."""
37         return self.__repr
38
39     def __hash__(self):
40         """Return cached hash."""
41         return self.__hash