X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=csit%2Flibraries%2FHsfJson%2Fhsfod.py;fp=csit%2Flibraries%2FHsfJson%2Fhsfod.py;h=8b19a9d3af0cbd77e8e8ba0c4d8543990191697e;hb=59e81c38620fa1b61e15771191e35771450b9499;hp=0000000000000000000000000000000000000000;hpb=072f6e3a8d1bdf8f4c663843589c22d93ba07791;p=integration%2Ftest.git diff --git a/csit/libraries/HsfJson/hsfod.py b/csit/libraries/HsfJson/hsfod.py new file mode 100644 index 0000000000..8b19a9d3af --- /dev/null +++ b/csit/libraries/HsfJson/hsfod.py @@ -0,0 +1,40 @@ +"""This module contains single class, to store a sorted dict.""" +# Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License v1.0 which accompanies this distribution, +# and is available at http://www.eclipse.org/legal/epl-v10.html + +__author__ = "Vratko Polak" +__copyright__ = "Copyright(c) 2015, Cisco Systems, Inc." +__license__ = "Eclipse Public License v1.0" +__email__ = "vrpolak@cisco.com" + +import collections as _collections + + +class Hsfod(_collections.OrderedDict): + """ + Hashable sorted (by key) frozen OrderedDict implementation stub. + + Supports only __init__, __repr__ and __hash__ methods. + Other OrderedDict methods are available, but they may break contract. + """ + + def __init__(self, *args, **kwargs): + """Put arguments to OrderedDict, sort, pass to super, cache values.""" + self_unsorted = _collections.OrderedDict(*args, **kwargs) + items_sorted = sorted(self_unsorted.items(), key=repr) + sup = super(Hsfod, self) # possibly something else than OrderedDict + sup.__init__(items_sorted) + # Repr string is used for sorting, keys are more important than values. + self.__repr = '{' + repr(self.keys()) + ':' + repr(self.values()) + '}' + self.__hash = hash(self.__repr) + + def __repr__(self): + """Return cached repr string.""" + return self.__repr + + def __hash__(self): + """Return cached hash.""" + return self.__hash