mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 06:56:13 +00:00
No __cmp__ or cmp in Python 3 (#429)
* No __cmp__ or cmp in Python 3 * Add rich comparisons * Simplify using total_ordering decorator
This commit is contained in:
committed by
GitHub
parent
b539e96697
commit
22a193dafb
@@ -33,12 +33,14 @@ __all__ = ["lookupFor"] # field classes added at the end of the module
|
|||||||
import datetime
|
import datetime
|
||||||
import struct
|
import struct
|
||||||
import sys
|
import sys
|
||||||
|
from functools import total_ordering
|
||||||
|
|
||||||
from . import utils
|
from . import utils
|
||||||
|
|
||||||
# abstract definitions
|
# abstract definitions
|
||||||
|
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
class DbfFieldDef:
|
class DbfFieldDef:
|
||||||
"""Abstract field definition.
|
"""Abstract field definition.
|
||||||
|
|
||||||
@@ -101,8 +103,14 @@ class DbfFieldDef:
|
|||||||
self.start = start
|
self.start = start
|
||||||
self.end = stop
|
self.end = stop
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.name, str(other).upper())
|
return repr(self) == repr(other)
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
return repr(self) != repr(other)
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
return repr(self) < repr(other)
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self.name)
|
return hash(self.name)
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""Tests for tablib.packages.dbfpy."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from tablib.packages.dbfpy import fields
|
||||||
|
|
||||||
|
|
||||||
|
class DbfFieldDefTestCompareCase(unittest.TestCase):
|
||||||
|
"""dbfpy.fields.DbfFieldDef comparison test cases, via child classes."""
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self.length = 10
|
||||||
|
self.a = fields.DbfCharacterFieldDef("abc", self.length)
|
||||||
|
self.z = fields.DbfCharacterFieldDef("xyz", self.length)
|
||||||
|
self.a2 = fields.DbfCharacterFieldDef("abc", self.length)
|
||||||
|
|
||||||
|
def test_compare__eq__(self):
|
||||||
|
# Act / Assert
|
||||||
|
self.assertEqual(self.a, self.a2)
|
||||||
|
|
||||||
|
def test_compare__ne__(self):
|
||||||
|
# Act / Assert
|
||||||
|
self.assertNotEqual(self.a, self.z)
|
||||||
|
|
||||||
|
def test_compare__lt__(self):
|
||||||
|
# Act / Assert
|
||||||
|
self.assertLess(self.a, self.z)
|
||||||
|
|
||||||
|
def test_compare__le__(self):
|
||||||
|
# Act / Assert
|
||||||
|
self.assertLessEqual(self.a, self.a2)
|
||||||
|
self.assertLessEqual(self.a, self.z)
|
||||||
|
|
||||||
|
def test_compare__gt__(self):
|
||||||
|
# Act / Assert
|
||||||
|
self.assertGreater(self.z, self.a)
|
||||||
|
|
||||||
|
def test_compare__ge__(self):
|
||||||
|
# Act / Assert
|
||||||
|
self.assertGreaterEqual(self.a2, self.a)
|
||||||
|
self.assertGreaterEqual(self.z, self.a)
|
||||||
Reference in New Issue
Block a user