[Edu-sig] another look at sorting generic objects by attribute...

kirby urner kirby.urner at gmail.com
Tue Apr 12 18:29:54 EDT 2016


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 12 14:38:28 2016

@author: Kirby Urner, 4Dsolutions.net

Tips and Tricks:
Note use of **self.__dict__ in the __repr__, and print(*sorted(L)).
Also, remember sep= and end= are overridable keyword args to print().

Related: data-only objects
http://stackoverflow.com/questions/19988043/are-data-only-objects-ok-in-an-oop-design

See also:

Data Transfer Object, Passive Data Object, Bunch Class
E.g.: http://pydanny.blogspot.com/2011/11/loving-bunch-class.html


"""
from collections import namedtuple

# first, with an ordinary class (not quite a 'Bunch class', but could be)
class Element:

    def __init__(self, dim0, dim1, dim2, dim3):
        self.axis0 = dim0
        self.axis1 = dim1
        self.axis2 = dim2
        self.axis3 = dim3

    def __repr__(self):
        return 'Element({axis0},{axis1},{axis2},{axis3})'.\
                format(**self.__dict__)

the_list = [Element('A', 'D', 0, 1),   # goal:  to sort on different axes
            Element('X', 'R', 2, 3),
            Element('R', 'B', 9, 8)]

# sort on any attribute of Element
print(sorted(the_list, key=lambda elem: elem.axis0))  # lambda extracts the
key
print(sorted(the_list, key=lambda elem: elem.axis1))
print(sorted(the_list, key=lambda elem: elem.axis2))
print(sorted(the_list, key=lambda elem: elem.axis3))


# one might use a named tuple to the same end
Elem = namedtuple("Elem", "axis0 axis1 axis2, axis3")

the_list = [Elem('A', 'D', 0, 1),
            Elem('X', 'R', 2, 3),
            Elem('R', 'B', 9, 8)]

# almost identical syntax but line-wrapping added because namedtuple
__repr__
# is more verbose...
print("+++")
print(*sorted(the_list, key=lambda elem: elem.axis0), sep="\n",
end="\n--\n")
print(*sorted(the_list, key=lambda elem: elem.axis1), sep="\n",
end="\n--\n")
print(*sorted(the_list, key=lambda elem: elem.axis2), sep="\n",
end="\n--\n")
print(*sorted(the_list, key=lambda elem: elem.axis3), sep="\n",
end="\n--\n")

expected_output = \
"""
OUTPUT:

[Element(A,D,0,1), Element(R,B,9,8), Element(X,R,2,3)]
[Element(R,B,9,8), Element(A,D,0,1), Element(X,R,2,3)]
[Element(A,D,0,1), Element(X,R,2,3), Element(R,B,9,8)]
[Element(A,D,0,1), Element(X,R,2,3), Element(R,B,9,8)]
+++
Elem(axis0='A', axis1='D', axis2=0, axis3=1)
Elem(axis0='R', axis1='B', axis2=9, axis3=8)
Elem(axis0='X', axis1='R', axis2=2, axis3=3)
--
Elem(axis0='R', axis1='B', axis2=9, axis3=8)
Elem(axis0='A', axis1='D', axis2=0, axis3=1)
Elem(axis0='X', axis1='R', axis2=2, axis3=3)
--
Elem(axis0='A', axis1='D', axis2=0, axis3=1)
Elem(axis0='X', axis1='R', axis2=2, axis3=3)
Elem(axis0='R', axis1='B', axis2=9, axis3=8)
--
Elem(axis0='A', axis1='D', axis2=0, axis3=1)
Elem(axis0='X', axis1='R', axis2=2, axis3=3)
Elem(axis0='R', axis1='B', axis2=9, axis3=8)
"""
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20160412/1ff26109/attachment.html>


More information about the Edu-sig mailing list