/
tut.py
210 lines (152 loc) · 6.46 KB
/
tut.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# vim: set et ts=4 sw=4 fdm=marker
"""
MIT License
Copyright (c) 2016 Jesse Hogan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from pdb import set_trace; B=set_trace
from tester import *
class test_entities(tester):
def it_appends(self):
class gods(entities):
pass
class god(entity):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
norse_pantheon = gods()
self.assertEq(norse_pantheon.count, 0)
thor = god('Thor')
odin = god('Odin')
norse_pantheon.append(thor)
norse_pantheon.append(odin)
self.assertEq(norse_pantheon.count, 2)
norse_pantheon.clear()
self.assertEq(norse_pantheon.count, 0)
self.assertTrue(norse_pantheon.isempty)
norse_pantheon += thor
norse_pantheon += odin
self.assertEq(norse_pantheon.count, 2)
norse_pantheon.clear()
norse_pantheon += thor + odin
self.assertEq(norse_pantheon.count, 2)
self.assertIs(norse_pantheon[0], thor)
self.assertIs(norse_pantheon[1], odin)
self.assertIs(norse_pantheon['Thor'], thor)
self.assertIs(norse_pantheon['Odin'], odin)
self.assertIs(norse_pantheon.first, thor)
self.assertIs(norse_pantheon.second, odin)
for g in norse_pantheon:
print(g.name)
print(norse_pantheon)
def it_calls_where(self):
class deities(entities):
pass
class deity(entity):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def __str__(self):
return self.name
pantheon = deities()
pantheon += deity('Aphrodite', 'female')
pantheon += deity('Apollo', 'male')
pantheon += deity('Ares', 'male')
pantheon += deity('Artemis', 'female')
pantheon += deity('Athena', 'female')
pantheon += deity('Demeter', 'female')
pantheon += deity('Hades', 'male')
pantheon += deity('Hephaestus', 'male')
pantheon += deity('Hera', 'female')
pantheon += deity('Hermes', 'male')
pantheon += deity('Hestia', 'female')
pantheon += deity('Poseidon', 'male')
pantheon += deity('Zeus', 'male')
gods = pantheon.where(lambda x: x.gender == 'male')
goddesses = pantheon.where(lambda x: x.gender == 'female')
assert isinstance(gods, entities)
assert isinstance(goddesses, entities)
assert gods.count == 7
assert goddesses.count == 6
gods = pantheon.where(lambda x: x.name[0] == 'A' and x.gender == 'male')
assert gods.count == 2
assert gods[0].name == 'Apollo'
assert gods[1].name == 'Ares'
def it_determine_validity(self):
class deities(entities):
pass
class deity(entity):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def __str__(self):
return self.name
pantheon = deities()
zeus = deity('Zeus', 'male')
assert pantheon.isvalid
assert pantheon.brokenrules.count == 0
assert zeus.isvalid
assert zeus.brokenrules.count == 0
class deity(entity):
def __init__(self, name, gender):
self.name = name
self.gender = gender
@property
def brokenrules(self):
brs = brokenrules()
if self.gender not in ('female', 'male'):
brs += '{} is an invalid gender.'.format(self.gender)
if type(self.name) != str:
brs += 'Deity names must be strings.'
return brs
def __str__(self):
return self.name
zeus = deity(42, None)
assert not zeus.isvalid
assert str(zeus.brokenrules) == 'None is an invalid gender.\nDeity names must be strings.'
pantheon = deities()
pantheon += zeus
assert not pantheon.isvalid
assert str(pantheon.brokenrules) == 'None is an invalid gender.\nDeity names must be strings.'
class deity(entity):
def __init__(self, name, gender):
self.name = name
self.gender = gender
self.children = deities()
@property
def brokenrules(self):
brs = brokenrules()
if self.gender not in ('female', 'male'):
brs += '{} is an invalid gender for "{}".'.format(self.gender, self.name)
if type(self.name) != str:
brs += 'Deity names must be strings.'
brs += self.children.brokenrules
return brs
def __str__(self):
return self.name
zeus = deity('Zeus', 'male')
zeus.children += deity('Aeacus', 'm')
zeus.children += deity('Angelos', 'female')
zeus.children += deity('Aphrodite', 'female')
zeus.children += deity('Apollo', 'male')
zeus.children += deity('Ares', 'male')
zeus.children += deity('Artemis', 'f')
zeus.children += deity('Athena', 'female')
assert not zeus.isvalid
assert str(zeus.brokenrules) == 'm is an invalid gender for "Aeacus".\nf is an invalid gender for "Artemis".'
print(testers())