[Spambayes-checkins] spambayes/Outlook2000/dialogs AsyncDialog.py,
1.3, 1.4
Mark Hammond
mhammond at users.sourceforge.net
Thu May 15 16:52:03 EDT 2003
Update of /cvsroot/spambayes/spambayes/Outlook2000/dialogs
In directory sc8-pr-cvs1:/tmp/cvs-serv32395/dialogs
Modified Files:
AsyncDialog.py
Log Message:
The training dialog now shows a correct progress bar for the *complete*
operation - training *and* scoring (and saving, but that isn't accurate -
always exactly 1 "tick" per save.
Index: AsyncDialog.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/Outlook2000/dialogs/AsyncDialog.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** AsyncDialog.py 7 Nov 2002 22:30:10 -0000 1.3
--- AsyncDialog.py 15 May 2003 22:52:01 -0000 1.4
***************
*** 32,50 ****
self.dlg = dlg
self.stopping = False
def set_max_ticks(self, m):
! win32api.PostMessage(self.hprogress, commctrl.PBM_SETRANGE, 0, MAKELPARAM(0,m))
! win32api.PostMessage(self.hprogress, commctrl.PBM_SETSTEP, 1, 0)
! win32api.PostMessage(self.hprogress, commctrl.PBM_SETPOS, 0, 0)
def tick(self):
! win32api.PostMessage(self.hprogress, commctrl.PBM_STEPIT, 0, 0)
! #self.p.StepIt()
def set_status(self, text):
! self.dlg.progress_status = text
win32api.PostMessage(self.hdlg, MYWM_SETSTATUS)
def warning(self, text):
! self.dlg.progress_warning = text
win32api.PostMessage(self.hdlg, MYWM_SETWARNING)
def error(self, text):
! self.dlg.progress_error = text
win32api.PostMessage(self.hdlg, MYWM_SETERROR)
def request_stop(self):
--- 32,96 ----
self.dlg = dlg
self.stopping = False
+ self.total_control_ticks = 100
+ self.current_stage = 0
+ self.set_stages( (("", 1.0),) )
+
+ def set_stages(self, stages):
+ self.stages = []
+ start_pos = 0.0
+ for name, prop in stages:
+ stage = name, start_pos, prop
+ start_pos += prop
+ self.stages.append(stage)
+ assert(abs(start_pos-1.0)) < 0.001, "Proportions must add to 1.0"
+
+ def _next_stage(self):
+ if self.current_stage == 0:
+ win32api.PostMessage(self.hprogress, commctrl.PBM_SETRANGE, 0, MAKELPARAM(0,self.total_control_ticks))
+ win32api.PostMessage(self.hprogress, commctrl.PBM_SETSTEP, 1, 0)
+ win32api.PostMessage(self.hprogress, commctrl.PBM_SETPOS, 0, 0)
+ self.current_control_tick = 0
+
+ self.current_stage += 1
+ assert self.current_stage <= len(self.stages)
+
+ def _get_current_stage(self):
+ return self.stages[self.current_stage-1]
+
def set_max_ticks(self, m):
! self._next_stage()
! self.current_stage_tick = 0
! self.current_stage_max = m
!
def tick(self):
! self.current_stage_tick += 1
! # Calc how far through this stage.
! this_prop = float(self.current_stage_tick) / self.current_stage_max
! # How far through the total.
! stage_name, start, end = self._get_current_stage()
! # Calc the perc of the total control.
! stage_name, start, prop = self._get_current_stage()
! total_prop = start + this_prop * prop
! # How may ticks is this on the control
! control_tick = int(total_prop * self.total_control_ticks)
! #print "Tick", self.current_stage_tick, "is", this_prop, "through the stage,", total_prop, "through the total - ctrl tick is", control_tick
! while self.current_control_tick < control_tick:
! self.current_control_tick += 1
! #print "ticking control", self.current_control_tick
! win32api.PostMessage(self.hprogress, commctrl.PBM_STEPIT, 0, 0)
!
! def _get_stage_text(self, text):
! stage_name, start, end = self._get_current_stage()
! if stage_name:
! text = stage_name + ": " + text
! return text
def set_status(self, text):
! self.dlg.progress_status = self._get_stage_text(text)
win32api.PostMessage(self.hdlg, MYWM_SETSTATUS)
def warning(self, text):
! self.dlg.progress_warning = self._get_stage_text(text)
win32api.PostMessage(self.hdlg, MYWM_SETWARNING)
def error(self, text):
! self.dlg.progress_error = self._get_stage_text(text)
win32api.PostMessage(self.hdlg, MYWM_SETERROR)
def request_stop(self):
***************
*** 113,116 ****
--- 159,167 ----
self.seen_finished = False
self.running = True
+ # Drop my thread priority, so outlook can keep repainting
+ # and doing its stuff without getting stressed.
+ import win32process, win32api
+ THREAD_PRIORITY_BELOW_NORMAL=-1
+ win32process.SetThreadPriority(win32api.GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL)
self._DoProcess()
finally:
***************
*** 123,124 ****
--- 174,204 ----
t = threading.Thread(target=thread_target, args =(self.GetSafeHwnd(), _Progress(self)))
t.start()
+
+ if __name__=='__main__':
+ # Test my "multi-stage" code
+ class HackProgress(_Progress):
+ def __init__(self): # dont use dlg
+ self.hprogress = self.hdlg = 0
+ self.dlg = None
+ self.stopping = False
+ self.total_control_ticks = 100
+ self.current_stage = 0
+ self.set_stages( (("", 1.0),) )
+
+ p = HackProgress()
+ p.set_max_ticks(10)
+ for i in range(10):
+ p.tick()
+
+ p = HackProgress()
+ stages = ("Stage 1", 0.2), ("Stage 2", 0.8)
+ p.set_stages(stages)
+ # Do stage 1
+ p.set_max_ticks(10)
+ for i in range(10):
+ p.tick()
+ # Do stage 2
+ p.set_max_ticks(1000)
+ for i in range(1000):
+ p.tick()
+
More information about the Spambayes-checkins
mailing list