Skip to content

Commit 4d16d2b

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Make 'FormattableColumn' comparable"
2 parents a5bdcc6 + c1c9910 commit 4d16d2b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

cliff/columns.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def __eq__(self, other):
2626
self.__class__ == other.__class__ and self._value == other._value
2727
)
2828

29+
def __lt__(self, other):
30+
return (
31+
self.__class__ == other.__class__ and self._value < other._value
32+
)
33+
2934
@abc.abstractmethod
3035
def human_readable(self):
3136
"""Return a basic human readable version of the data."""

cliff/tests/test_columns.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ def test_faux_column_human(self):
3333
u"I made this string myself: ['list', 'of', 'values']",
3434
c.human_readable(),
3535
)
36+
37+
def test_sorting(self):
38+
cols = [
39+
FauxColumn('foo'),
40+
FauxColumn('bar'),
41+
FauxColumn('baz'),
42+
FauxColumn('foo'),
43+
]
44+
cols.sort()
45+
self.assertEqual(
46+
['bar', 'baz', 'foo', 'foo'],
47+
[c.machine_readable() for c in cols],
48+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
Instances of ``cliff.columns.FormattableColumn`` are now comparable. This
5+
allows implementations of ``FormattableColumn`` storing primitive data
6+
types or containers with primitive data types to be sorted using the
7+
``--sort-column`` option. Implementations of ``FormattableColumn`` that
8+
store other types of data will still need to implement their own rich
9+
comparison magic methods.

0 commit comments

Comments
 (0)