我想制作一个应用程序,它可以从带有多个工作表的 Google 电子表格中读出城市名称、道路名称以及它们之间的距离。
到目前为止,我的代码正常工作。它从电子表格中读出,还从想要找出两个城市之间的道路名称(如 Rode 60)以及它们之间的距离的用户那里接收输入。但是,当我运行该应用程序时,它非常慢。
我想我有一个服务器端用户端的问题,但是在阅读了大量的文档之后我很困惑。也许我应该考虑一种完全不同的方法。也许我需要用 gspread 读出所有电子表格并只在用户端工作。反正。现在它很慢,我希望稍后在我的电子表格中包含数千个城市,可能我会在那里放一些关于它们的更多数据,比如它是一条乡间小路还是一条高速公路、国道等。这需要很长时间才能它将使用我当前的代码返回结果。
如果您建议我应该用图表做这些事情,请帮助并请注意我是 python、wxPython、Google API 或 IGraph 的新手。今天我也为我的 Python 2.7 设置了 IGraph。也许这是我问题的关键?请至少给我正确的方法,正确的教程。我不指望任何人为我做肮脏的工作。先感谢您!!!
import gdata.spreadsheet.service
import gdata.service
import atom.service
import gdata.spreadsheet
import atom
import gspread
import wx
import gdata.docs
import gdata.docs.service
import re
import os
import csv
import math
class laci(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'distance calculator',size=(500,500))
panel=wx.Panel(self)
test=wx.TextEntryDialog(None,"Beginning point: ",'Name of beginning point','...')
if test.ShowModal()==wx.ID_OK:
all1=test.GetValue()
test2=wx.TextEntryDialog(None,"Finishing point: ",'Name of finish','...')
if test2.ShowModal()==wx.ID_OK:
all2=test2.GetValue()
c = gspread.Client(auth=('......@gmail.com','.............'))
c.login()
# open spreadsheet
sht=c.open_by_key('.................................')
worksheet = sht.get_worksheet(0)
print worksheet
i = 1
j = 1
What = None
first_col = worksheet.col_values(1)
print first_col
stopper = 0
n = 3
m = 3
while worksheet.cell(i,1).value != None and stopper != 1:
if worksheet.cell(i,1).value == all1:
print all1
stopper = 1
else:
i = i+1
print i
if worksheet.cell(i,1).value == None:
boxy=wx.MessageDialog(None,'Wrong start point. You wanna see correct start points list?','Mistake?',wx.YES_NO)
answer=boxy.ShowModal()
boxy.Destroy
if answer == 5103:
boxl=wx.SingleChoiceDialog(None,'Accepted Starting point names:','In case of mistake',
['City1','City2','City3','City4','City5','City6','City7','City8'])
if boxl.ShowModal()==wx.ID_OK:
all1=boxl.GetStringSelection()
stopper = 0 # figyelj
i = 1
print all1
boxl.Destroy
else:
print 'how unfortunate'
if stopper == 1:
sline = []
while worksheet.cell(i,n).value != None:
line = worksheet.cell(i,n).value
sline.append(line)
n = n + 1
print sline
slinestr = str(sline)
stopper2 = 0
print sline
while worksheet.cell(j,1).value != None and stopper2 != 1:
if worksheet.cell(j,1).value == all2:
print all2
stopper2 = 1
else:
j = j+1
print j
if worksheet.cell(j,1).value == None:
boxz=wx.MessageDialog(None,'Wrong Finish point? Wanna see correct choices?','Mistake?',wx.YES_NO)
answer2=boxz.ShowModal()
boxz.Destroy
if answer2 == 5103:
boxl2=wx.SingleChoiceDialog(None,'Accepted Finishing point names:','In case of mistake',
['City1','City2','City3','City4','City5','City6','City7','City8'])
if boxl2.ShowModal()==wx.ID_OK:
all2=boxl2.GetStringSelection()
print all2
boxl2.Destroy
else:
print 'how unfortunate'
if stopper2 == 1:
sline2 = []
while worksheet.cell(j,m).value != None:
line2 = worksheet.cell(j,m).value
sline2.append(line2)
m = m + 1
print sline2
slinestr2 = str(sline2)
print sline
print sline2
t = list(set(sline) & set(sline2))
print t
t = t[0]
t = str(t)
worksheet2 = sht.worksheet(t)
print worksheet2
print worksheet2.cell(2,2)
i = 2
j = 2
iszam = 1
iszam2 = 1
stopi = 0
stopi2 = 0
km = 0
while worksheet2.cell(i,2).value != None and stopi != 1:
if worksheet2.cell(i,2).value == all1:
iszam = i
print iszam
print worksheet2.cell(i,3)
stopi = 1
i = i + 1
print i
while worksheet2.cell(j,2).value != None and stopi2 != 1:
if worksheet2.cell(j,2).value == all2:
iszam2 = j
print iszam2
print worksheet2.cell(j,3)
stopi2 = 1
j = j + 1
print j
if iszam2 < iszam:
while iszam2 != iszam:
km = km + int(worksheet2.cell(iszam2+1,3).value)
iszam2 = iszam2 + 1
print km
elif iszam2 > iszam:
while iszam != iszam2:
km = km + int(worksheet2.cell(iszam+1,3).value)
iszam = iszam + 1
print km
else:
km = 0
print km
km = str(km)
wx.StaticText(panel, -1, all1, (20,30))
wx.StaticText(panel, -1, slinestr, (80,30))
wx.StaticText(panel, -1, all2, (20,60))
wx.StaticText(panel, -1, slinestr2, (80,60))
wx.StaticText(panel, -1, 'Path =', (20,90))
wx.StaticText(panel, -1, t, (80,90))
wx.StaticText(panel, -1, 'Distance =', (20,120))
wx.StaticText(panel, -1, km, (80,120))
if __name__=='__main__':
app=wx.PySimpleApp() #runs it
frame=laci(parent=None,id=-1) #face of programme
frame.Show()
app.MainLoop()