Skip to content

Commit

Permalink
Merge pull request #7 from ryanmrubin/bug/fix-enummeta-comparisons
Browse files Browse the repository at this point in the history
Bug/fix enummeta comparisons
  • Loading branch information
David Eyk authored Nov 14, 2016
2 parents 4d8305a + fedd738 commit b108804
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ Cache keys are determined by hash key, range key, and a cache prefix
CHANGELOG
---------

0.3.1
^^^^^

Fixed bug whereby EnumMeta and subclasses were not comparing properly (re: at all) in Python 3.

0.3.0
^^^^^

Expand Down
20 changes: 17 additions & 3 deletions duo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""
from __future__ import unicode_literals
from six import with_metaclass, string_types, text_type, iteritems
from functools import total_ordering
import warnings
import collections
import datetime
Expand All @@ -50,6 +51,7 @@
# light on what they are and how they work.


@total_ordering
class EnumMeta(type):
"""Simple metaclass for enumerated types.
Expand Down Expand Up @@ -140,11 +142,23 @@ def __nonzero__(cls):
def __bool__(cls):
return bool(int(cls))

def __cmp__(self, other):
def __lt__(self, other):
if isinstance(other, string_types):
return cmp(str(self), other)
return str(self) < other
else:
return cmp(int(self), other)
return int(self) < int(other)

def __gt__(self, other):
if isinstance(other, string_types):
return str(self) > other
else:
return int(self) > int(other)

def __eq__(self, other):
if isinstance(other, string_types):
return str(self) == other
else:
return int(self) == int(other)

def __str__(cls):
try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
zip_safe = False,

version = "0.3.0",
version = "0.3.1",
description = "A powerful, dynamic, pythonic interface to AWS DynamoDB.",
long_description = open(README).read(),
author = "David Eyk",
Expand Down
24 changes: 24 additions & 0 deletions test_duo.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,27 @@ class TestItemSubclass(self.duo.Item):
self.assertIsInstance(item['place'], int)
self.assertEqual(item['place'], 1)
self.assertIs(item.place, Bar)

def test_enum_classes_should_compare(self):
class Placeholder(with_metaclass(self.duo.EnumMeta, object)): pass

class Foo(Placeholder): pass

class Bar(Placeholder): pass

class Baz(Placeholder): pass

self.assertEqual(Foo, 0)
self.assertEqual(Foo, 'Foo')
self.assertEqual(Foo, Foo)
self.assertLess(Foo, Bar)
self.assertEqual(Bar, 1)
self.assertEqual(Bar, 'Bar')
self.assertEqual(Bar, Bar)
self.assertGreater(Bar, Foo)
self.assertLess(Bar, Baz)
self.assertEqual(Baz, 2)
self.assertEqual(Baz, 'Baz')
self.assertEqual(Baz, Baz)
self.assertGreater(Baz, Bar)
self.assertLess(Baz, 3)

0 comments on commit b108804

Please sign in to comment.