-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
93 lines (75 loc) · 2.45 KB
/
models.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""POD classes that are used by multiple files."""
from collections import OrderedDict
class Classifier:
def __init__(self, trad, simp, pinyin):
"""
:param str trad: traditional form
:param str simp: simplified form
:param str pinyin: pinyin, e.g. 'ge' (not 'ge5')
"""
self.trad = trad
self.simp = simp
self.pinyin = pinyin
def to_dict(self):
fields = ['trad', 'simp', 'pinyin']
rv = OrderedDict((field, getattr(self, field)) for field in fields if getattr(self, field))
if rv['simp'] == rv['trad']:
del rv['simp']
return rv
@classmethod
def from_dict(cls, d):
return cls(
trad=d['trad'],
simp=d.get('simp', d['trad']),
pinyin=d['pinyin'],
)
def __repr__(self):
return '{}(trad={}, simp={}, pinyin={})'.format(
self.__class__.__name__,
repr(self.trad),
repr(self.simp),
repr(self.pinyin),
)
def __eq__(self, other):
if not isinstance(other, Classifier):
raise TypeError('Cannot compare {} and {}'.format(self.__class__.__name__, other.__class__.__name__))
return self.trad == other.trad and self.simp == other.simp and self.pinyin == other.pinyin
class ExampleSentence:
def __init__(self, trad, simp, pinyin, eng):
"""
:param str trad|None: traditional form
:param str simp|None: simplified form
:param str pinyin: pinyin, e.g. 'nǐ hǎo' (not 'ni3 hao3')
:param str|None eng: English translation
"""
self.trad = trad
self.simp = simp
self.pinyin = pinyin
self.eng = eng
def __repr__(self):
return '{}(trad={}, simp={}, pinyin={}, eng={})'.format(
self.__class__.__name__,
repr(self.trad),
repr(self.simp),
repr(self.pinyin),
repr(self.eng),
)
def to_dict(self):
fields = ['trad', 'simp', 'pinyin', 'eng']
rv = OrderedDict((field, getattr(self, field)) for field in fields if getattr(self, field))
if rv.get('simp') == rv.get('trad'):
del rv['simp']
return rv
@classmethod
def from_dict(cls, d):
return cls(
trad=d.get('trad'),
simp=d.get('simp'),
pinyin=d.get('pinyin'),
eng=d.get('eng'),
)
def __eq__(self, other):
if not isinstance(other, ExampleSentence):
raise TypeError('Cannot compare {} and {}'.format(self.__class__.__name__, other.__class__.__name__))
return (
self.trad == other.trad and self.simp == other.simp and self.pinyin == other.pinyin and self.eng == other.eng)