def turn_around(): p="fullv3" if len(sys.argv)==4: p=sys.argv[4] print "Using profile", profiles[p]['description'] profile=profiles[p]['profile'] d={'result': {}} try: execfile(sys.argv[1], d,d) except UnicodeError: common.unicode_execfile(sys.argv[1], d,d) f=open(sys.argv[2], "wt") for k in d['result']['phonebook']: print >>f, output_entry(d['result']['phonebook'][k], profile) f.close()
def turn_around(): p = "fullv3" if len(sys.argv) == 4: p = sys.argv[4] print "Using profile", profiles[p]['description'] profile = profiles[p]['profile'] d = {'result': {}} try: execfile(sys.argv[1], d, d) except UnicodeError: common.unicode_execfile(sys.argv[1], d, d) with file(sys.argv[2], "wt") as f: for k in d['result']['phonebook']: print >> f, output_entry(d['result']['phonebook'][k], profile)
def _gen_print_data(self): if not self._date_changed and \ not self._style_changed and \ self._html is not None: return self._dt_start=self._start_date.GetValue() self._dt_end=self._end_date.GetValue() if not self._dt_start.IsValid() or not self._dt_end.IsValid(): return print_data=( (self._regular_template, self._regular_style, self._get_list_data), (self._monthly_template, self._monthly_style, self._get_monthly_data)) print_style=self._print_style.GetSelection() print_dict=print_data[print_style][2]() if self._xcp is None: self._xcp=xyaptu.xcopier(None) tmpl=file(guihelper.getresourcefile(print_data[print_style][0]), 'rt').read() self._xcp.setupxcopy(tmpl) elif self._style_changed: tmpl=file(guihelper.getresourcefile(print_data[print_style][0]), 'rt').read() self._xcp.setupxcopy(tmpl) if self._dns is None: self._dns={ 'common': __import__('common') } self._dns['guihelper']=__import__('guihelper') self._dns['events']=[] self._dns['events']=print_dict self._dns['date_range']='%s - %s'%\ (self._dt_start.FormatDate(), self._dt_end.FormatDate()) html=self._xcp.xcopywithdns(self._dns.copy()) sd={'styles': {}, '__builtins__': __builtins__ } try: execfile(guihelper.getresourcefile(print_data[print_style][1]), sd, sd) except UnicodeError: common.unicode_execfile(guihelper.getresourcefile(print_data[print_style][1]), sd, sd) try: self._html=bphtml.applyhtmlstyles(html, sd['styles']) except: if __debug__: file('debug.html', 'wt').write(html) raise self._date_changed=self._style_change=False
def OnTemplateLoad(self, _): with guihelper.WXDialogWrapper( wx.FileDialog(self, "Select a file to load", wildcard="*.tmpl", style=wx.OPEN | wx.FILE_MUST_EXIST), True ) as (dlg, retcode): if retcode == wx.ID_OK: result = {} try: execfile(dlg.GetPath()) except UnicodeError: common.unicode_execfile(dlg.GetPath()) exist_keys = {} for i, e in enumerate(self._templates): exist_keys[e.name] = i for d in result["templates"]: data_struct = DataStruct("new struct") data_struct.set(d) if exist_keys.has_key(data_struct.name): self._templates[exist_keys[data_struct.name]] = data_struct else: self._templates.append(data_struct)
def OnTemplateLoad(self, _): dlg=wx.FileDialog(self, 'Select a file to load', wildcard='*.tmpl', style=wx.OPEN|wx.FILE_MUST_EXIST) if dlg.ShowModal()==wx.ID_OK: result={} try: execfile(dlg.GetPath()) except UnicodeError: common.unicode_execfile(dlg.GetPath()) exist_keys={} for i,e in enumerate(self._templates): exist_keys[e.name]=i for d in result['templates']: data_struct=DataStruct('new struct') data_struct.set(d) if exist_keys.has_key(data_struct.name): self._templates[exist_keys[data_struct.name]]=data_struct else: self._templates.append(data_struct) dlg.Destroy()
def OnTemplateLoad(self, _): dlg=wx.FileDialog(self, 'Select a file to load', wildcard='*.tmpl', style=wx.OPEN|wx.FILE_MUST_EXIST) if dlg.ShowModal()==wx.ID_OK: result={} try: execfile(dlg.GetPath()) except UnicodeError: common.unicode_execfile(dlg.GetPath()) exist_keys={} for i,e in enumerate(self._templates): exist_keys[e.name]=i for d in result['templates']: data_struct=DataStruct('new struct') data_struct.set(d) if exist_keys.has_key(data_struct.name): self._templates[exist_keys[data_struct.name]]=data_struct else: self._templates.append(data_struct) dlg.Destroy() def OnTemplateSaveAs(self, _): dlg=wx.FileDialog(self, 'Select a file to save', wildcard='*.tmpl', style=wx.SAVE|wx.OVERWRITE_PROMPT) if dlg.ShowModal()==wx.ID_OK: r=[x.get() for x in self._templates] common.writeversionindexfile(dlg.GetPath(), { 'templates': r }, 1) dlg.Destroy() def OnTemplateApply(self, _): if not self._templates: return choices=[x.name for x in self._templates] dlg=wx.SingleChoiceDialog(self, 'Select a template to apply:', 'Apply Data Template', choices) if dlg.ShowModal()==wx.ID_OK: try: res=self._apply_template(dlg.GetStringSelection()) self._display_result(res) except: raise w=wx.MessageDialog(self, 'Apply Template raised an exception', 'Apply Template Error', style=wx.OK|wx.ICON_ERROR) w.ShowModal() w.Destroy() dlg.Destroy() def OnTemplateEdit(self, _): dlg=TemplateDialog(self) dlg.set(self._templates) if dlg.ShowModal()==wx.ID_OK: self._templates=dlg.get() dlg.Destroy() def OnSearch(self, evt): dlg=wx.TextEntryDialog(self, 'Enter data to search (1 0x23 045 ...):', 'Search Data') if dlg.ShowModal()==wx.ID_OK: l=dlg.GetValue().split(' ') s='' for e in l: if e[0:2]=='0x': s+=chr(int(e, 16)) elif e[0]=='0': s+=chr(int(e, 8)) else: s+=chr(int(e)) i=self.data[self.current_ofs:].find(s) if i!=-1: self._search_string=s self.highlightstart=i+self.current_ofs self.highlightend=self.highlightstart+len(s) self.needsupdate=True self.Refresh() self.set_sel(self.highlightstart, self.highlightend) else: self._search_string=None def OnSearchAgain(self, evt): if self._search_string is not None: i=self.data[self.current_ofs:].find(self._search_string) if i==-1: return self.highlightstart=i+self.current_ofs self.highlightend=self.highlightstart+len(self._search_string) self.needsupdate=True self.Refresh() self.set_sel(self.highlightstart, self.highlightend) def OnSize(self, evt): if evt is None: self.width=(self.widthinchars+3)*self.charwidth self.height=self.charheight*20 self.SetClientSize((self.width, self.height)) self.SetCaret(wx.Caret(self, (self.charwidth, self.charheight))) self.GetCaret().Show(True) else: self.width,self.height=self.GetClientSizeTuple() self.needsupdate=True def OnGainFocus(self,_): self.hasfocus=True self.needsupdate=True self.Refresh() def OnLoseFocus(self,_): self.hasfocus=False self.needsupdate=True self.Refresh() def highlightrange(self, start, end): self.needsupdate=True self.highlightstart=start self.highlightend=end self.Refresh() self.set_pos(None) self.set_sel(self.highlightstart, self.highlightend) self.set_val(None) def _ishighlighted(self, pos): return pos>=self.highlightstart and pos<self.highlightend def sethighlight(self, foreground, background): self.highlight=foreground,background def setnormal(self, foreground, background): self.normal=foreground,background def setfont(self, font): dc=wx.ClientDC(self) dc.SetFont(font) self.charwidth, self.charheight=dc.GetTextExtent("M") self.font=font self.updatescrollbars() def updatescrollbars(self): lines=len(self.data)/16 if lines==0 or len(self.data)%16: lines+=1 self.datalines=lines self.widthinchars=8+2+3*16+1+2+16 self.SetScrollbars(self.charwidth, self.charheight, self.widthinchars, lines, self.GetViewStart()[0], self.GetViewStart()[1]) def _setnormal(self,dc): dc.SetTextForeground(self.normal[0]) dc.SetTextBackground(self.normal[1]) def _sethighlight(self,dc): dc.SetTextForeground(self.highlight[0]) dc.SetTextBackground(self.highlight[1]) def _setstatus(self,dc): dc.SetTextForeground(self.normal[1]) dc.SetTextBackground(self.normal[0]) dc.SetBrush(wx.BLACK_BRUSH) def OnDraw(self, dc): xd,yd=self.GetViewStart() st=0 dc.BeginDrawing() dc.SetBackgroundMode(wx.SOLID) dc.SetFont(self.font) for line in range(yd, min(self.datalines, yd+self.height/self.charheight+1)): self._setnormal(dc) st=0 dc.DrawText("%08X" % (line*16), 0, line*self.charheight) for i in range(16): pos=line*16+i if pos>=len(self.data): break hl=self._ishighlighted(pos) if hl!=st: if hl: st=1 self._sethighlight(dc) else: st=0 self._setnormal(dc) if hl: space="" if i<15: if self._ishighlighted(pos+1): space=" " if i==7: space=" " else: space="" c=self.data[pos] dc.DrawText("%02X%s" % (ord(c),space), (10+(3*i)+(i>=8))*self.charwidth, line*self.charheight) if not (ord(c)>=32 and string.printable.find(c)>=0): c='.' dc.DrawText(c, (10+(3*16)+2+i)*self.charwidth, line*self.charheight) dc.EndDrawing() def updatebuffer(self): if self.buffer is None or \ self.buffer.GetWidth()!=self.width or \ self.buffer.GetHeight()!=self.height: if self.buffer is not None: del self.buffer self.buffer=wx.EmptyBitmap(self.width, self.height) mdc=wx.MemoryDC() mdc.SelectObject(self.buffer) mdc.SetBackground(wx.TheBrushList.FindOrCreateBrush(self.GetBackgroundColour(), wx.SOLID)) mdc.Clear() self.PrepareDC(mdc) self.OnDraw(mdc) mdc.SelectObject(wx.NullBitmap) del mdc def OnPaint(self, event): if self.needsupdate: self.needsupdate=False self.updatebuffer() dc=wx.PaintDC(self) dc.BeginDrawing() dc.DrawBitmap(self.buffer, 0, 0, False) dc.EndDrawing() def OnScrollWin(self, event): self.needsupdate=True self.Refresh() event.Skip() class HexEditorDialog (wx.Dialog) : _pane_widths=[-2, -3, -4] _pos_pane_index=0 _sel_pane_index=1 _val_pane_index=2 def __init__(self, parent, data='', title='BitPim Hex Editor', helpd_id=-1): super(HexEditorDialog, self).__init__(parent, -1, title, size=(500, 500), style=wx.DEFAULT_DIALOG_STYLE|\ wx.RESIZE_BORDER) self._status_bar=wx.StatusBar(self, -1) self._status_bar.SetFieldsCount(len(self._pane_widths)) self._status_bar.SetStatusWidths(self._pane_widths) vbs=wx.BoxSizer(wx.VERTICAL) self._hex_editor=HexEditor(self, _set_pos=self.set_pos, _set_val=self.set_val, _set_sel=self.set_sel) self._hex_editor.SetData(data) self._hex_editor.SetTitle(title) vbs.Add(self._hex_editor, 1, wx.EXPAND|wx.ALL, 5) vbs.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5) ok_btn=wx.Button(self, wx.ID_OK, 'OK') vbs.Add(ok_btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) vbs.Add(self._status_bar, 0, wx.EXPAND|wx.ALL, 0) self.SetSizer(vbs) self.SetAutoLayout(True) vbs.Fit(self) def set_pos(self, pos): """Display the current buffer offset in the format of Pos: 0x12=18 """ if pos is None: s='' else: s='Pos: 0x%X=%d'%(pos, pos) self._status_bar.SetStatusText(s, self._pos_pane_index) def set_sel(self, sel_start, sel_end): if sel_start is None or sel_start==-1 or\ sel_end is None or sel_end ==-1: s='' else: sel_len=sel_end-sel_start sel_end-=1 s='Sel: 0x%X=%d to 0x%X=%d (0x%X=%d bytes)'%( sel_start, sel_start, sel_end, sel_end, sel_len, sel_len) self._status_bar.SetStatusText(s, self._sel_pane_index) def set_val(self, v): if v: s='Val: 0x%02X=%d'%(ord(v[0]), ord(v[0])) if len(v)>1: u_s=struct.unpack('<H', v[:struct.calcsize('<H')])[0] s+=' 0x%04X=%d'%(u_s, u_s) if len(v)>3: u_i=struct.unpack('<I', v[:struct.calcsize('<I')])[0] s+=' 0x%08X=%d'%(u_i, u_i) else: s='' self._status_bar.SetStatusText(s, self._val_pane_index) def set(self, data): self._hex_editor.SetData(data) if __name__=='__main__': import sys if len(sys.argv)!=2: print 'Usage:',sys.argv[0],'<File Name>' sys.exit(1) app=wx.PySimpleApp() dlg=HexEditorDialog(None, file(sys.argv[1], 'rb').read(), sys.argv[1]) if True: dlg.ShowModal() else: import hotshot f=hotshot.Profile("hexeprof",1) f.runcall(dlg.ShowModal) f.close() import hotshot.stats stats=hotshot.stats.load("hexeprof") stats.strip_dirs() stats.sort_stats("time", "calls") stats.print_stats(30) dlg.Destroy() sys.exit(0) if __name__=='__main__': import sys if len(sys.argv)!=2: print 'Usage:',sys.argv[0],'<File Name>' sys.exit(1) app=wx.PySimpleApp() dlg=HexEditorDialog(None, file(sys.argv[1], 'rb').read(), sys.argv[1]) if True: dlg.ShowModal() else: import hotshot f=hotshot.Profile("hexeprof",1) f.runcall(dlg.ShowModal) f.close() import hotshot.stats stats=hotshot.stats.load("hexeprof") stats.strip_dirs() stats.sort_stats("time", "calls") stats.print_stats(30) dlg.Destroy() sys.exit(0)
'wallpapers': ['wallpaper', 'use'], 'flags': ['secret'], 'memos': ['memo'], 'numbers': ['number', 'type', 'speeddial'], # serials is in parent object }) _knowndictproperties=basedataobject._knowndictproperties.copy() _knowndictproperties.update( {'repeat': ['daily', 'orange']} ) phonebookobjectfactory=dataobjectfactory(phonebookdataobject) # use the phonebook out of the examples directory try: execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) except UnicodeError: common.unicode_execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) ensurerecordtype(phonebook, phonebookobjectfactory) phonebookmaster=phonebook def testfunc(): global phonebook, TRACE, db # note that iterations increases the size of the # database/journal and will make each one take longer and # longer as the db/journal gets bigger if len(sys.argv)>=2: iterations=int(sys.argv[1]) else: iterations=1
def deleteold(self, tablename, uids=None, minvalues=3, maxvalues=5, keepoldest=93): """Deletes old entries from the database. The deletion is based on either criterion of maximum values or age of values matching. @param uids: You can limit the items deleted to this list of uids, or None for all entries. @param minvalues: always keep at least this number of values @param maxvalues: maximum values to keep for any entry (you can supply None in which case no old entries will be removed based on how many there are). @param keepoldest: values older than this number of days before now are removed. You can also supply None in which case no entries will be removed based on age. @returns: number of rows removed,number of rows remaining """ if not self.doestableexist(tablename): return (0,0) timecutoff=0 if keepoldest is not None: timecutoff=time.time()-(keepoldest*24*60*60) if maxvalues is None: maxvalues=sys.maxint-1 if uids is None: uids=[u[0] for u in self.sql("select distinct __uid__ from %s" % (idquote(tablename),))] deleterows=[] for uid in uids: deleting=False for count, (rowid, deleted, timestamp) in enumerate( self.sql("select __rowid__,__deleted__, __timestamp__ from %s where __uid__=? order by __rowid__ desc" % (idquote(tablename),), [uid])): if count<minvalues: continue if deleting: deleterows.append(rowid) continue if count>=maxvalues or timestamp<timecutoff: deleting=True if deleted: deleterows.append(rowid) continue if count>0: deleterows.append(rowid) continue self.sqlmany("delete from %s where __rowid__=?" % (idquote(tablename),), [(r,) for r in deleterows]) return len(deleterows), self.sql("select count(*) from "+idquote(tablename)).next()[0] def savelist(self, tablename, values): """Just save a list of items (eg categories). There is no versioning or transaction history. Internally the table has two fields. One is the actual value and the other indicates if the item is deleted. """ tn=(idquote(tablename),) if not self.doestableexist(tablename): self.sql("create table %s (__rowid__ integer primary key, item, __deleted__ integer)" % tn) delete=[] known=[] revive=[] for row, item, dead in self.sql("select __rowid__,item,__deleted__ from %s" % tn): known.append(item) if item in values: if dead: revive.append((row,)) continue if dead: continue delete.append((row,)) create=[(v,) for v in values if v not in known] self.sqlmany("update %s set __deleted__=0 where __rowid__=?" % tn, revive) self.sqlmany("update %s set __deleted__=1 where __rowid__=?" % tn, delete) self.sqlmany("insert into %s (item, __deleted__) values (?,0)" % tn, create) if __debug__: vdup=values[:] vdup.sort() vv=self.loadlist(tablename) vv.sort() assert vdup==vv def loadlist(self, tablename): """Loads a list of items (eg categories)""" if not self.doestableexist(tablename): return [] return [v[0] for v in self.sql("select item from %s where __deleted__=0" % (idquote(tablename),))] savemajordict=ExclusiveWrapper(savemajordict) getmajordictvalues=ExclusiveWrapper(getmajordictvalues) deleteold=ExclusiveWrapper(deleteold) savelist=ExclusiveWrapper(savelist) loadlist=ExclusiveWrapper(loadlist) def getchangescount(self, tablename): """Return the number of additions, deletions, and modifications made to this table over time. Expected fields containted in this table: __timestamp__,__deleted__, __uid__ Assuming that both __rowid__ and __timestamp__ values are both ascending """ if not self.doestableexist(tablename): return {} tn=idquote(tablename) sql_cmd='select distinct __timestamp__ from %s' % tn res={} for t in self.sql(sql_cmd): res[t[0]]={ 'add': 0, 'del': 0, 'mod': 0 } existing_uid={} sql_cmd='select __timestamp__,__uid__,__deleted__ from %s order by __timestamp__ asc' % tn for e in self.sql(sql_cmd): tt=e[0] uid=e[1] del_flg=e[2] if existing_uid.has_key(uid): if del_flg: res[tt]['del']+=1 del existing_uid[uid] else: res[tt]['mod']+=1 else: existing_uid[uid]=None res[tt]['add']+=1 return res if __name__=='__main__': import common import sys import time import os sys.excepthook=common.formatexceptioneh class phonebookdataobject(basedataobject): _knownlistproperties=basedataobject._knownlistproperties.copy() _knownlistproperties.update( {'names': ['title', 'first', 'middle', 'last', 'full', 'nickname'], 'categories': ['category'], 'emails': ['email', 'type'], 'urls': ['url', 'type'], 'ringtones': ['ringtone', 'use'], 'addresses': ['type', 'company', 'street', 'street2', 'city', 'state', 'postalcode', 'country'], 'wallpapers': ['wallpaper', 'use'], 'flags': ['secret'], 'memos': ['memo'], 'numbers': ['number', 'type', 'speeddial'], }) _knowndictproperties=basedataobject._knowndictproperties.copy() _knowndictproperties.update( {'repeat': ['daily', 'orange']} ) phonebookobjectfactory=dataobjectfactory(phonebookdataobject) try: execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) except UnicodeError: common.unicode_execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) ensurerecordtype(phonebook, phonebookobjectfactory) phonebookmaster=phonebook def testfunc(): global phonebook, TRACE, db if len(sys.argv)>=2: iterations=int(sys.argv[1]) else: iterations=1 if iterations >1: TRACE=False db=Database("testdb") b4=time.time() for i in xrange(iterations): phonebook=phonebookmaster.copy() db.savemajordict("phonebook", extractbitpimserials(phonebook)) v=db.getmajordictvalues("phonebook") assert v==extractbitpimserials(phonebook) del phonebook[17] db.savemajordict("phonebook", extractbitpimserials(phonebook)) v=db.getmajordictvalues("phonebook") assert v==extractbitpimserials(phonebook) phonebook[15]['addresses'][0]['city']="Bananarama" db.savemajordict("phonebook", extractbitpimserials(phonebook)) v=db.getmajordictvalues("phonebook") assert v==extractbitpimserials(phonebook) after=time.time() print "time per iteration is",(after-b4)/iterations,"seconds" print "total time was",after-b4,"seconds for",iterations,"iterations" if iterations>1: print "testing repeated reads" b4=time.time() for i in xrange(iterations*10): db.getmajordictvalues("phonebook") after=time.time() print "\ttime per iteration is",(after-b4)/(iterations*10),"seconds" print "\ttotal time was",after-b4,"seconds for",iterations*10,"iterations" print print "testing repeated writes" x=extractbitpimserials(phonebook) k=x.keys() b4=time.time() for i in xrange(iterations*10): xcopy=x.copy() for l in range(i,i+len(k)/3): del xcopy[k[l%len(x)]] db.savemajordict("phonebook",xcopy) after=time.time() print "\ttime per iteration is",(after-b4)/(iterations*10),"seconds" print "\ttotal time was",after-b4,"seconds for",iterations*10,"iterations" if len(sys.argv)==3: def profile(filename, command): import hotshot, hotshot.stats, os file=os.path.abspath(filename) profile=hotshot.Profile(file) profile.run(command) profile.close() del profile howmany=100 stats=hotshot.stats.load(file) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(100) stats.sort_stats('cum', 'calls') stats.print_stats(100) stats.sort_stats('calls', 'time') stats.print_stats(100) sys.exit(0) profile("dbprof", "testfunc()") else: testfunc() if __debug__: TRACE=False else: TRACE=False if __name__=='__main__': import common import sys import time import os sys.excepthook=common.formatexceptioneh class phonebookdataobject(basedataobject): _knownlistproperties=basedataobject._knownlistproperties.copy() _knownlistproperties.update( {'names': ['title', 'first', 'middle', 'last', 'full', 'nickname'], 'categories': ['category'], 'emails': ['email', 'type'], 'urls': ['url', 'type'], 'ringtones': ['ringtone', 'use'], 'addresses': ['type', 'company', 'street', 'street2', 'city', 'state', 'postalcode', 'country'], 'wallpapers': ['wallpaper', 'use'], 'flags': ['secret'], 'memos': ['memo'], 'numbers': ['number', 'type', 'speeddial'], }) _knowndictproperties=basedataobject._knowndictproperties.copy() _knowndictproperties.update( {'repeat': ['daily', 'orange']} ) phonebookobjectfactory=dataobjectfactory(phonebookdataobject) try: execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) except UnicodeError: common.unicode_execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) ensurerecordtype(phonebook, phonebookobjectfactory) phonebookmaster=phonebook def testfunc(): global phonebook, TRACE, db if len(sys.argv)>=2: iterations=int(sys.argv[1]) else: iterations=1 if iterations >1: TRACE=False db=Database("testdb") b4=time.time() for i in xrange(iterations): phonebook=phonebookmaster.copy() db.savemajordict("phonebook", extractbitpimserials(phonebook)) v=db.getmajordictvalues("phonebook") assert v==extractbitpimserials(phonebook) del phonebook[17] db.savemajordict("phonebook", extractbitpimserials(phonebook)) v=db.getmajordictvalues("phonebook") assert v==extractbitpimserials(phonebook) phonebook[15]['addresses'][0]['city']="Bananarama" db.savemajordict("phonebook", extractbitpimserials(phonebook)) v=db.getmajordictvalues("phonebook") assert v==extractbitpimserials(phonebook) after=time.time() print "time per iteration is",(after-b4)/iterations,"seconds" print "total time was",after-b4,"seconds for",iterations,"iterations" if iterations>1: print "testing repeated reads" b4=time.time() for i in xrange(iterations*10): db.getmajordictvalues("phonebook") after=time.time() print "\ttime per iteration is",(after-b4)/(iterations*10),"seconds" print "\ttotal time was",after-b4,"seconds for",iterations*10,"iterations" print print "testing repeated writes" x=extractbitpimserials(phonebook) k=x.keys() b4=time.time() for i in xrange(iterations*10): xcopy=x.copy() for l in range(i,i+len(k)/3): del xcopy[k[l%len(x)]] db.savemajordict("phonebook",xcopy) after=time.time() print "\ttime per iteration is",(after-b4)/(iterations*10),"seconds" print "\ttotal time was",after-b4,"seconds for",iterations*10,"iterations" if len(sys.argv)==3: def profile(filename, command): import hotshot, hotshot.stats, os file=os.path.abspath(filename) profile=hotshot.Profile(file) profile.run(command) profile.close() del profile howmany=100 stats=hotshot.stats.load(file) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(100) stats.sort_stats('cum', 'calls') stats.print_stats(100) stats.sort_stats('calls', 'time') stats.print_stats(100) sys.exit(0) profile("dbprof", "testfunc()") else: testfunc()
'emails': ['email', 'type'], 'urls': ['url', 'type'], 'ringtones': ['ringtone', 'use'], 'addresses': ['type', 'company', 'street', 'street2', 'city', 'state', 'postalcode', 'country'], 'wallpapers': ['wallpaper', 'use'], 'flags': ['secret'], 'memos': ['memo'], 'numbers': ['number', 'type', 'speeddial'], }) _knowndictproperties=basedataobject._knowndictproperties.copy() _knowndictproperties.update( {'repeat': ['daily', 'orange']} ) phonebookobjectfactory=dataobjectfactory(phonebookdataobject) try: execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) except UnicodeError: common.unicode_execfile(os.getenv("DBTESTFILE", "examples/phonebook-index.idx")) ensurerecordtype(phonebook, phonebookobjectfactory) phonebookmaster=phonebook def testfunc(): global phonebook, TRACE, db if len(sys.argv)>=2: iterations=int(sys.argv[1]) else: iterations=1 if iterations >1: TRACE=False db=Database("testdb") b4=time.time() for i in xrange(iterations): phonebook=phonebookmaster.copy() db.savemajordict("phonebook", extractbitpimserials(phonebook))