Code documentation

compare_on_attr

class python3_utils.compare_on_attr(attr='id')

A decorator for comparing class attributes. By default, the decorator searches for an ‘id’ attribute on your class, but other attributes can be specified. The original use for this decorator is to compare Django models in Pandas DataFrames.

This decorator assumes the class you are decorating has the appropriate attribute, and will raise an error when comparing a class without the correct attribute.

Usage:

@compare_on_attr()
class MyModel(object):
    id = None
    def __init__(self, id):
        self.id = id

>>> m1 = MyModel(1)
>>> m2 = MyModel(2)
>>> m2 > m1
True

# or

@compare_on_attr(attr='count')
class MyClass(object):
    count = None