Robot Framework Integrated Development Environment (RIDE)
sortable.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
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 at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # 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 and
14 # limitations under the License.
15 
16 from operator import eq, lt, le, gt, ge
17 
18 from .robottypes import type_name
19 
20 
21 
22 class Sortable():
23 
24 
27  _sort_key = NotImplemented
28 
29  def __test(self, operator, other, require_sortable=True):
30  if isinstance(other, Sortable):
31  return operator(self._sort_key_sort_key, other._sort_key)
32  if not require_sortable:
33  return False
34  raise TypeError("Cannot sort '%s' and '%s'."
35  % (type_name(self), type_name(other)))
36 
37  def __eq__(self, other):
38  return self.__test__test(eq, other, require_sortable=False)
39 
40  def __ne__(self, other):
41  return not self == other
42 
43  def __lt__(self, other):
44  return self.__test__test(lt, other)
45 
46  def __le__(self, other):
47  return self.__test__test(le, other)
48 
49  def __gt__(self, other):
50  return self.__test__test(gt, other)
51 
52  def __ge__(self, other):
53  return self.__test__test(ge, other)
54 
55  def __hash__(self):
56  return hash(self._sort_key_sort_key)
Base class for sorting based self._sort_key.
Definition: sortable.py:22
def __test(self, operator, other, require_sortable=True)
Definition: sortable.py:29