-
Notifications
You must be signed in to change notification settings - Fork 0
/
bagua.py
238 lines (218 loc) · 6.64 KB
/
bagua.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
def rev_dict(d):
tmp = {}
for i in d:
tmp = tmp | {d[i]:i}
return tmp
阳 = 1
阴 = 0
卦表 = [
"坤",
"乾",
"兑",
"离",
"震",
"巽",
"坎",
"艮"
]
卦象 = { # 低位为下爻,高位为上爻,从下往上数
"乾":(1,1,1),
"兑":(1,1,0),
"离":(1,0,1),
"震":(1,0,0),
"巽":(0,1,1),
"坎":(0,1,0),
"艮":(0,0,1),
"坤":(0,0,0)
}
卦性 = {
"乾":("金", "天"),
"兑":("金", "泽"),
"离":("火", "火"),
"震":("木", "雷"),
"巽":("木", "风"),
"坎":("水", "水"),
"艮":("土", "山"),
"坤":("土", "地")
}
生 = {
"金":"水",
"水":"木",
"木":"火",
"火":"土",
"土":"金"
}
克 = {
"金":"木",
"木":"土",
"土":"水",
"水":"火",
"火":"金"
}
复卦名 = {
("乾", "乾"):"乾卦",
("乾", "兑"):"天泽履",
("乾", "离"):"天火同人",
("乾", "震"):"天雷无妄",
("乾", "巽"):"天风垢",
("乾", "坎"):"天水讼",
("乾", "艮"):"天山遁",
("乾", "坤"):"天地否",
("兑", "乾"):"泽天央",
("兑", "兑"):"泽卦",
("兑", "离"):"泽火革",
("兑", "震"):"泽雷随",
("兑", "巽"):"泽风大过",
("兑", "坎"):"泽水困",
("兑", "艮"):"泽山咸",
("兑", "坤"):"泽地萃",
("离", "乾"):"火天大有",
("离", "兑"):"火泽暌",
("离", "离"):"离卦",
("离", "震"):"火雷噬嗑",
("离", "巽"):"火风鼎",
("离", "坎"):"火水未济",
("离", "艮"):"火山旅",
("离", "坤"):"火地晋",
("震", "乾"):"雷天大壮",
("震", "兑"):"雷泽归妹",
("震", "离"):"雷火丰",
("震", "震"):"震卦",
("震", "巽"):"雷风恒",
("震", "坎"):"雷水解",
("震", "艮"):"雷山小过",
("震", "坤"):"雷地豫",
("巽", "乾"):"风天小畜",
("巽", "兑"):"风泽中孚",
("巽", "离"):"风火家人",
("巽", "震"):"风雷益",
("巽", "巽"):"巽卦",
("巽", "坎"):"风水涣",
("巽", "艮"):"风山渐",
("巽", "坤"):"风地观",
("坎", "乾"):"水天需",
("坎", "兑"):"水泽节",
("坎", "离"):"水火既济",
("坎", "震"):"水雷屯",
("坎", "巽"):"水风井",
("坎", "坎"):"坎卦",
("坎", "艮"):"水山蹇",
("坎", "坤"):"水地比",
("艮", "乾"):"山天大畜",
("艮", "兑"):"山泽损",
("艮", "离"):"山火贲",
("艮", "震"):"山雷颐",
("艮", "巽"):"山风蛊",
("艮", "坎"):"山水蒙",
("艮", "艮"):"艮卦",
("艮", "坤"):"山地剥",
("坤", "乾"):"地天泰",
("坤", "兑"):"地泽临",
("坤", "离"):"地火明夷",
("坤", "震"):"地雷复",
("坤", "巽"):"地风升",
("坤", "坎"):"地水师",
("坤", "艮"):"地山谦",
("坤", "坤"):"坤卦",
}
class 卦:
@property
def available(self):
if len(self.data) != 3:
return (False, "运行时错误:卦象格式错误")
for i in self.data:
if type(i) != type(0) and type(i) != type(False):
return (False, "运行时错误:卦象格式错误")
if int(i) not in (0, 1):
return (False, "运行时错误:卦象格式错误")
return (True, "运行状态良好")
def selftest(self):
if not self.available[0]:
raise Exception(self.available[1])
def __init__(self, data):
if type(data) == type(self):
self.data = data.data
elif type(data) == type(list()) or type(data) == type(tuple()):
self.data = tuple(data)
elif type(data) == type(0):
self.data = 卦象[卦表[data%8]]
elif type(data) == type("") and data in 卦表:
self.data = 卦象[data]
else:
raise Exception("初始化错误:不支持的卦初始化数据")
self.selftest()
@property
def name(self):
self.selftest()
return(rev_dict(卦象)[self.data])
@property
def elements(self):
self.selftest()
return(卦性[self.name])
def __eq__(self, value):
self.selftest()
return(self.data == value.data)
def __repr__(self):
self.selftest()
return("卦(%s)"%self.data.__repr__())
def __str__(self):
self.selftest()
return("卦(%s)"%self.data.__str__())
def __getitem__(self, index:int):
self.selftest()
return(self.data.__getitem__(index))
def __setitem__(self, index:int, value):
self.selftest()
return self.data.__setitem__(index, value)
class 复卦:
@property
def available(self):
if type(self.data) != type(list()):
return(False, "运行时错误:复卦格式错误")
if len(self.data) != 2:
return(False, "运行时错误:复卦格式错误")
if type(self.data[0]) != type(卦(0)) or type(self.data[1]) != type(卦(0)):
return(False, "运行时错误:复卦格式错误")
for i in self.change:
if type(i) != type(int):
return (False, "运行时错误:动爻表错误")
return (True, "运行状态良好")
def selftest(self):
if not self.available[0]:
raise Exception(self.available[1])
def __init__(self, gua1:卦, gua2:卦, change:list[int]):
if list(map(type, [gua1, gua2, change])) != list(map(type, [卦(0), 卦(0), []])):
raise Exception("初始化错误:复卦初始化失败")
self.data = [gua2, gua1]
self.change = change
self.selftest()
@property
def name(self):
self.selftest()
return(复卦名[(self.data[1].name, self.data[0].name)])
@property
def 互卦(self):
self.selftest()
return(复卦(卦([self[2], self[3], self[4]]), 卦([self[1], self[2], self[3]]), []))
def __repr__(self):
self.selftest()
return("复卦(%s, %s, %s)"%(self.data[0].__repr__(), self.data[1].__repr__(), self.change.__repr__()))
def __str__(self):
self.selftest()
return("复卦(%s, %s, %s)"%(self.data[0].__str__(), self.data[1].__str__(), self.change.__str__()))
def __getitem__(self, index:int):
self.selftest()
if index>=3:
return(self.data[1][index-3])
else:
return(self.data[0][index])
def __setitem__(self, index:int, value):
self.selftest()
if index>=3:
self.data[1][index-3] = value
else:
self.data[0][index] = value
if __name__ == "__main__":
a = 复卦(卦("巽"), 卦("乾"), [])
print(a.互卦.name)
print(a.互卦)