1# -*- coding: utf-8 -*-2# Copyright 2011 Takeshi KOMIYA3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.151617def cmp_to_key(mycmp):18 """Convert a cmp= function into a key= function"""19 class K(object):20 __slots__ = ['obj']2122 def __init__(self, obj, *args):23 self.obj = obj2425 def __lt__(self, other):26 return mycmp(self.obj, other.obj) < 02728 def __gt__(self, other):29 return mycmp(self.obj, other.obj) > 03031 def __eq__(self, other):32 return mycmp(self.obj, other.obj) == 03334 def __le__(self, other):35 return mycmp(self.obj, other.obj) <= 03637 def __ge__(self, other):38 return mycmp(self.obj, other.obj) >= 03940 def __ne__(self, other):41 return mycmp(self.obj, other.obj) != 04243 def __hash__(self):44 raise TypeError('hash not implemented')4546 return K