summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2021-02-10 12:12:30 +0000
committerGerrit Code Review <review@openstack.org>2021-02-10 12:12:30 +0000
commit4d16d2bf6997fbbb5d160261eec3dc39425ff3bb (patch)
tree1cbfa9065e8939bff2c7ed1d644313067c6cffb5
parenta5bdcc6e789adb0182df0721909d7b657f9a50b5 (diff)
parentc1c991045cece85dd55494f9d4670429e370e131 (diff)
downloadcliff-4d16d2bf6997fbbb5d160261eec3dc39425ff3bb.tar.gz
Merge "Make 'FormattableColumn' comparable"
-rw-r--r--cliff/columns.py5
-rw-r--r--cliff/tests/test_columns.py13
-rw-r--r--releasenotes/notes/comparable-FormattableColumn-31c0030ced70b7fb.yaml9
3 files changed, 27 insertions, 0 deletions
diff --git a/cliff/columns.py b/cliff/columns.py
index 6ecef64..b9cac5e 100644
--- a/cliff/columns.py
+++ b/cliff/columns.py
@@ -26,6 +26,11 @@ class FormattableColumn(object, metaclass=abc.ABCMeta):
self.__class__ == other.__class__ and self._value == other._value
)
+ def __lt__(self, other):
+ return (
+ self.__class__ == other.__class__ and self._value < other._value
+ )
+
@abc.abstractmethod
def human_readable(self):
"""Return a basic human readable version of the data."""
diff --git a/cliff/tests/test_columns.py b/cliff/tests/test_columns.py
index 564620d..6bce767 100644
--- a/cliff/tests/test_columns.py
+++ b/cliff/tests/test_columns.py
@@ -33,3 +33,16 @@ class TestColumns(unittest.TestCase):
u"I made this string myself: ['list', 'of', 'values']",
c.human_readable(),
)
+
+ def test_sorting(self):
+ cols = [
+ FauxColumn('foo'),
+ FauxColumn('bar'),
+ FauxColumn('baz'),
+ FauxColumn('foo'),
+ ]
+ cols.sort()
+ self.assertEqual(
+ ['bar', 'baz', 'foo', 'foo'],
+ [c.machine_readable() for c in cols],
+ )
diff --git a/releasenotes/notes/comparable-FormattableColumn-31c0030ced70b7fb.yaml b/releasenotes/notes/comparable-FormattableColumn-31c0030ced70b7fb.yaml
new file mode 100644
index 0000000..f32322e
--- /dev/null
+++ b/releasenotes/notes/comparable-FormattableColumn-31c0030ced70b7fb.yaml
@@ -0,0 +1,9 @@
+---
+features:
+ - |
+ Instances of ``cliff.columns.FormattableColumn`` are now comparable. This
+ allows implementations of ``FormattableColumn`` storing primitive data
+ types or containers with primitive data types to be sorted using the
+ ``--sort-column`` option. Implementations of ``FormattableColumn`` that
+ store other types of data will still need to implement their own rich
+ comparison magic methods.