1 from functools import total_ordering
2 from .base import MetaValue
6 class Person(MetaValue):
7 """Single person with last name and a list of first names."""
8 def __init__(self, last_name, *first_names):
9 self.last_name = last_name
10 self.first_names = first_names
13 def from_text(cls, text):
14 parts = [token.strip() for token in text.split(',')]
20 "Invalid person name. "
21 "There should be at most one comma: \"%s\"."
22 % text.encode('utf-8')
26 if len(parts[1]) == 0:
27 # there is no non-whitespace data after the comma
29 "Found a comma, but no names given: \"%s\" -> %r."
32 names = parts[1].split()
33 return cls(surname, *names)
36 return u" ".join(self.first_names + (self.last_name,))
38 def __eq__(self, right):
39 return (self.last_name == right.last_name
40 and self.first_names == right.first_names)
42 def __lt__(self, other):
43 return ((self.last_name, self.first_names)
44 < (other.last_name, other.first_names))
47 return hash((self.last_name, self.first_names))
50 if len(self.first_names) > 0:
51 return '%s, %s' % (self.last_name, ' '.join(self.first_names))
56 return 'Person(last_name=%r, first_names=*%r)' % (
57 self.last_name, self.first_names