-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyse.py
72 lines (54 loc) · 2.17 KB
/
analyse.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
#!/usr/bin/env python
#coding=utf-8
import mongoengine
import plistlib
from DBModel import TrainRecord
from DBModel import TrainConnectionRecord
import timeit
from config import config
mongoengine.connect(db='train', host=config['db_host'])
stations = plistlib.readPlist('StationList.plist')['stations']
def main():
for fromStation in stations:
for toStation in stations:
if fromStation['code'] == toStation['code']:
continue
if (TrainRecord.objects(__raw__={'fromStationCode':fromStation['code'], 'toStationCode':toStation['code']}).count()):
print('direct: '+fromStation['code']+'->'+toStation['code'])
continue
print('connection analysing: '+fromStation['code']+'->'+toStation['code'])
connection_between(fromStation['code'], toStation['code'])
def connection_between(fromStationCode, toStationCode):
if (TrainRecord.objects(__raw__={'fromStationCode':fromStationCode, 'toStationCode':toStationCode}).count()):
print(getStationByCode(fromStationCode)+'->'+getStationByCode(toStationCode)+' no need to connect')
return
cursor1 = TrainRecord.objects(__raw__={'fromStationCode':fromStationCode})
cursor2 = TrainRecord.objects(__raw__={'toStationCode':toStationCode})
tcr = TrainConnectionRecord()
tcr.fromStationCode = fromStationCode
tcr.toStationCode = toStationCode
for train1 in cursor1:
for train2 in cursor2:
if train1['toStationCode'] == train2['fromStationCode']:
# print(getStationByCode(train1['fromStationCode'])+'->'+
# getStationByCode(train1['toStationCode'])+'->'+
# getStationByCode(train2['toStationCode']))
has = False
for path in tcr.paths:
if path[0] == train1['toStationCode']:
has = True
break
if not has:
tcr.paths.append([train1['toStationCode']])
if len(tcr.paths) > 0:
tcr.put()
def getStationByCode(stationCode):
for station in stations:
if station['code'] == stationCode:
# print(station['name'].encode('utf-8'))
return station['name'].encode('utf-8')
if __name__ == '__main__':
# connection_between('AAX', 'BJP')
# main()
# getStationByCode('BJP')
print(timeit.timeit('TrainRecord.objects(fromStationCode="SHH")', setup='from __main__ import TrainRecord', number=1000))