-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSortKey.py
78 lines (57 loc) · 4.15 KB
/
SortKey.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
"""
>>> forenames = ("Warisha", "Elysha", "Liona", "Kassandra", "Simone",
... "Halima", "Liona", "Zack", "Josiah", "Sam", "Braedon", "Eleni")
>>> surnames = ("Chandler", "Drennan", "Stead", "Doole", "Reneau", "Dent",
... "Sheckles", "Dent", "Reddihough", "Dodwell", "Conner", "Abson")
>>> people = []
>>> for forename, surname in zip(forenames, surnames):
... people.append(Person(forename, surname, forename.lower() +
... "." + surname.lower() + "@eg.com"))
>>> [x.email[:-7] for x in people]
['warisha.chandler', 'elysha.drennan', 'liona.stead', 'kassandra.doole', 'simone.reneau', 'halima.dent', 'liona.sheckles', 'zack.dent', 'josiah.reddihough', 'sam.dodwell', 'braedon.conner', 'eleni.abson']
>>> [x.email[:-7] for x in sorted(people, key=SortKey("surname"))]
['eleni.abson', 'warisha.chandler', 'braedon.conner', 'halima.dent', 'zack.dent', 'sam.dodwell', 'kassandra.doole', 'elysha.drennan', 'josiah.reddihough', 'simone.reneau', 'liona.sheckles', 'liona.stead']
>>> [x.email[:-7] for x in sorted(people, key=SortKey("surname", "forename"))]
['eleni.abson', 'warisha.chandler', 'braedon.conner', 'halima.dent', 'zack.dent', 'sam.dodwell', 'kassandra.doole', 'elysha.drennan', 'josiah.reddihough', 'simone.reneau', 'liona.sheckles', 'liona.stead']
>>> [x.email[:-7] for x in sorted(people, key=SortKey("forename"))]
['braedon.conner', 'eleni.abson', 'elysha.drennan', 'halima.dent', 'josiah.reddihough', 'kassandra.doole', 'liona.stead', 'liona.sheckles', 'sam.dodwell', 'simone.reneau', 'warisha.chandler', 'zack.dent']
>>> [x.email[:-7] for x in sorted(people, key=SortKey("forename", "surname"))]
['braedon.conner', 'eleni.abson', 'elysha.drennan', 'halima.dent', 'josiah.reddihough', 'kassandra.doole', 'liona.sheckles', 'liona.stead', 'sam.dodwell', 'simone.reneau', 'warisha.chandler', 'zack.dent']
>>> import operator
>>> [x.email[:-7] for x in sorted(people, key=operator.attrgetter("surname"))]
['eleni.abson', 'warisha.chandler', 'braedon.conner', 'halima.dent', 'zack.dent', 'sam.dodwell', 'kassandra.doole', 'elysha.drennan', 'josiah.reddihough', 'simone.reneau', 'liona.sheckles', 'liona.stead']
>>> [x.email[:-7] for x in sorted(people, key=operator.attrgetter("surname", "forename"))]
['eleni.abson', 'warisha.chandler', 'braedon.conner', 'halima.dent', 'zack.dent', 'sam.dodwell', 'kassandra.doole', 'elysha.drennan', 'josiah.reddihough', 'simone.reneau', 'liona.sheckles', 'liona.stead']
>>> [x.email[:-7] for x in sorted(people, key=operator.attrgetter("forename"))]
['braedon.conner', 'eleni.abson', 'elysha.drennan', 'halima.dent', 'josiah.reddihough', 'kassandra.doole', 'liona.stead', 'liona.sheckles', 'sam.dodwell', 'simone.reneau', 'warisha.chandler', 'zack.dent']
>>> [x.email[:-7] for x in sorted(people, key=operator.attrgetter("forename", "surname"))]
['braedon.conner', 'eleni.abson', 'elysha.drennan', 'halima.dent', 'josiah.reddihough', 'kassandra.doole', 'liona.sheckles', 'liona.stead', 'sam.dodwell', 'simone.reneau', 'warisha.chandler', 'zack.dent']
"""
class Person:
def __init__(self, forename, surname, email):
self.forename = forename
self.surname = surname
self.email = email
def __str__(self):
return "{0.forename} {0.surname} <{0.email}>".format(self)
class SortKey:
def __init__(self, *attribute_names):
self.attribute_names = attribute_names
def __call__(self, instance):
values = []
for attribute_name in self.attribute_names:
values.append(getattr(instance, attribute_name))
return values
if __name__ == "__main__":
import doctest
doctest.testmod()