太慢的wxPython应用程序从Google Spreadsheet和用户输入中获取数据需要加速解决方案 [英] Way too slow wxPython application getting data from Google Spreadsheet and User input needs speed up solution

查看:112
本文介绍了太慢的wxPython应用程序从Google Spreadsheet和用户输入中获取数据需要加速解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个应用程序,通过Google Spreadsheet和几张工作表读出城市名称,道路名称和它们之间的距离。



到目前为止,我拥有代码波纹管正常工作。它从电子表格中读出,也接收来自想要找出两个城市之间的道路名称(例如60号道路)的用户的输入以及它们之间的距离。但是,当我运行该应用程序时,它的速度非常慢。



我认为我有一个服务器端用户端问题,但在阅读大量文档后,我感到非常困惑。也许我应该考虑一种完全不同的方法。也许我需要用gspread读出所有的电子表格,并且只能在用户端工作。无论如何。现在速度很慢,我想在后面的电子表格中显示成千上万的城市,我可能会在那里放置更多关于它们的数据,比如它是乡村公路,还是高速公路,国道等等,它会返回结果与我目前的代码。



请帮助和请注意,我是新的python,wxPython,谷歌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
import csv
import math
$ b $ 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 (无,开始点:,开始点名称,...)
if test.ShowModal()== wx.ID_OK:
all1 = test.GetValue()

test2 = wx.TextEntryDialog(None,Finishing point:,'Name of finish','...')
如果test2.ShowModal()== wx.ID_OK:
all2 = test2.GetValue()



c = gspread.Client(auth =('...... @ gmail.com','.. ...........'))
c.login()
#打开电子表格
sht = c.open_by_key('........................... ......')
worksheet = sht.get_worksheet(0)

打印工作表

i = 1
j = 1

什么=无



first_col = worksheet.col_values(1)
print first_col
stopper = 0
n = 3
m = 3

而worksheet.cell(i,1).value!= None且塞尔!= 1:
如果工作表.cell(i,1).value = = all1:
print all1
stopper = 1
else:
i = i + 1
如果worksheet.cell(i,1)打印我
。值==无:
boxy = wx.MessageDialog(None,'错误的起点。你想看到正确的开始点列表?','错误?',wx.YES_NO)
答案= boxy.ShowModal()
boxy.Destroy
如果答案== 5103:
boxl = wx.SingleChoiceDialog(None,'Accepted起始点名称','如有错误',
['City1','City2','City3','City4','City5',' City)'))
if boxl.ShowModal()== wx.ID_OK:
all1 = boxl.GetStringSelection()
stop = 0#figyelj
i = 1
打印所有1
boxl.Destroy
else:
打印'不幸'
如果塞子== 1:
sline = []
而worksheet.cell(i,n).value!=无:
line = worksheet.cell(i,n)。价值
sline.append(行)

n = n + 1
print sline
slinestr = str(sline)

$ b $ (j,1).value!= None和stopper2!= 1:如果工作表.cell(j,1).value = 1,
打印sline
而工作表。 = all2:
print all2
stop2 = 1
else:
j = j + 1
打印j
如果工作表.cell(j,1)。 value == None:
boxz = wx.MessageDialog(None,'Wrong Finish point? wx.YES_NO)
answer2 = boxz.ShowModal()
boxz.Destroy $ b $如果answer2 == 5103:
boxl2 = wx.SingleChoiceDialog(None,'已接受整理点名称','如有错误',
['City1','City2','City3','City4','City5','City6' 'City7','City8'])
if boxl2.ShowModal()== wx.ID_OK:
all2 = boxl2.GetStringSelection()
打印所有2
boxl2.Destroy
else:
print'how unfortunate'
如果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 works2
打印worksheet2.cell(2,2)
i = 2
j = 2
iszam = 1
iszam2 = 1
stopi = 0
stopi2 = 0
km = 0
worksheet2.cell(i,2).value!= None和stopi!= 1:
如果worksheet2.cell(i,2).value == all1:
iszam =
print iszam
print worksheet2.cell(i,3)
stopi = 1
i = i + 1
print i
,而worksheet2.cell(j,2).value!= None和stopi2!= 1:
如果worksheet2.cell(j,2).value == all2:
iszam2 = j
print iszam2
print worksheet2.cell(j,3)
stopi2 = 1
j = j + 1
print j
如果iszam2< iszam:
iszam2!= iszam:
km = km + int(worksheet2.cell(iszam2 + 1,3).value)
iszam2 = iszam2 + 1
print km

elif iszam2> iszam:
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)#程序
frame.Show()
app.MainLoop()


解决方案

第一个启动IMHO的地方是找到其中的延迟。



首先将代码移入不同的功能/方法 - 这样你就可以分析代码并查看缓慢的地方。

例如。 (可能需要进一步分解)




  • 在构造函数中设置wx对象

  • 然后有一个函数与Google进行通信

  • ,另一个函数将数据写入wx。



<然后你就可以自己做分析了,或者使用python profiler:( http://docs.python.org/2/library/profile.html )。最重要的是找出需要花费时间的东西,然后你就会知道需要改进的地方。



我的猜测我想尽量远程地完成这些工作,但是应该尽可能多地抓住你想要的,然后在本地进行搜索 - 因为如果电子表格上的每个函数调用都会导致与服务器的通信,我不会感到惊讶。我还没有使用过Google Spreadsheet API,因此 只是一种预感 I want to make an application that reads out city names, road names, and distances between them from a Google Spreadsheet with several worksheets.

So far I have the code bellow working right. It reads out from spreadsheet, receives input also from a user who wants to find out the road name (like Rode 60) between two cities and also the distance between them. However when I run the application it is incredibly SLOW.

I think I have a server side-user side issue, but after reading tons of documentation I'm very confused. Maybe I should consider an entirely different approach. Maybe I need to read out all the spreadsheet with gspread and to work only user side. Anyway. Now it's slow and I want to have like thousands of city's in my spreadsheet later on and probably I will put there some more data about them, like is it a country road or is it a highway, national road etc. it will take ages until it will return the result with my current code.

Please help and please mind that I'm new to python, wxPython, Google API's or to IGraph if you suggest that I should do these things with graphs. Today I did set up IGraph as well for my Python 2.7. Maybe it's the key to my problem? Please at least give me the right way, the right tutorials. I'm not expecting anyone to do the dirty job for me. Thank you in advance!!!

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()

解决方案

The first place to start IMHO is to find where the delay is.

I'd start by moving the code into different functions/methods - that way you'll be able to profile the code and see where the slowness is.

eg. (probably want broken down further than this)

  • in the constructor just set up the wx objects
  • then have a function that communicates with Google
  • and another to write the data to wx.

After that you'll be able to do some profiling either yourself or using the python profiler: (http://docs.python.org/2/library/profile.html). The most important thing is finding what is taking the time and then you will know what needs improving.

My guess would be that you don't want to be doing it all remotely but should grab as much as you think you'll need straight away and then do searching locally - as I wouldn't be surprised if each function call on the spreadsheet results in communication to the server.But I've not used the Google Spreadsheet API so that is just a hunch

这篇关于太慢的wxPython应用程序从Google Spreadsheet和用户输入中获取数据需要加速解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆