-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassExample.py
87 lines (54 loc) · 2.77 KB
/
classExample.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
#-*- coding: utf-8 -*-#
from pyORM import orm
import uuid, json
import ast, itertools
from myutils import updateAfterEvent, updateObj
class dummyPokemonClass(object):
__tablename__ = "dummy"
__typesOrderArr__ = ['paper', 'scisor', 'rock', 'some different type that loses in front of all others']
__typesOrder__ = {'paper': [2, 3], 'scisor': [0, 3], 'rock': [1, 3], 'some different type that loses in front of all others': []}
def __init__(self, uuid_=None, name=None, typeArrayStr=None, hp=100):
# It must be specified whether it is a primary key or not, the other elements can be ignored/undefined.
# Order of the attributes must be same as args of __init__
self.id = [uuid_ if uuid_ else str(uuid.uuid4()), "VARCHAR(255)", {"PRIMARY KEY": True, "AUTOINCREMENT": False, "NOT NULL": True, "FOREIGN KEY {} REFERENCES ...etc": False}]
self.name = [name, "VARCHAR(255)", {"PRIMARY KEY": False, "AUTOINCREMENT": False, "NOT NULL": True}]
deSerial_typeArray = ast.literal_eval(typeArrayStr)
if not [xType in self.__typesOrderArr__ for xType in deSerial_typeArray].count(False):
self.type = [typeArrayStr, "TEXT", {"PRIMARY KEY": False, "AUTOINCREMENT": False, "NOT NULL": False}]
else:
raise "The type of pokemon doesn't exist"
self.hp = [hp, "INTEGER", {"PRIMARY KEY": False, "AUTOINCREMENT": False, "NOT NULL": True,\
"CHECK(hp BETWEEN 0 AND 100)": True}]
@updateAfterEvent
def attack(self, enemyObj):
deSerial_Self = ast.literal_eval(self.type[0])
deSerial_enemyObj = ast.literal_eval(enemyObj.type[0])
superAgainst = list(set(itertools.chain(*[self.__typesOrder__[t] for t in deSerial_Self])))
if len([self.__typesOrderArr__[i] for i in superAgainst if self.__typesOrderArr__[i] in deSerial_enemyObj]):
enemyObj.hp[0] = max(enemyObj.hp[0]-10, 0)
return enemyObj
if __name__ == "__main__":
pika = dummyPokemonClass(name="Pikachu", typeArrayStr=json.dumps(['paper', 'some different type that loses in front of all others']), hp=90)
crocodil = dummyPokemonClass(name="Crocordil", typeArrayStr=json.dumps(['rock']), hp=85)
dbSession = orm(pika)
dbSession.add()
dbSession = orm(crocodil)
if not dbSession.add():
print("{} Added successfully".format(crocodil.name[0]))
#Attack example
print(pika.hp[0], crocodil.hp[0])
crocodil = pika.attack(crocodil)
print(pika.hp[0], crocodil.hp[0])
'''
Extract pokemons attributes and instantiate pokemon object
'''
dbSession = orm()
pokSQL = dbSession.filterBy.get(tablename="dummy", name="Pikachu")[0]
print(pokSQL)
pika = dummyPokemonClass(*pokSQL)
pokSQL = dbSession.filterBy.get(tablename="dummy", name="Crocordil")[0]
crocodil = dummyPokemonClass(*pokSQL)
print(pika.name[0], crocodil.name[0])
print(pika.hp[0], crocodil.hp[0])
crocodil = pika.attack(crocodil)
print(pika.hp[0], crocodil.hp[0])