gnome-games r8800 - trunk/gnome-sudoku/src/lib
- From: thomashpa svn gnome org
- To: svn-commits-list gnome org
- Subject: gnome-games r8800 - trunk/gnome-sudoku/src/lib
- Date: Tue, 3 Mar 2009 22:06:42 +0000 (UTC)
Author: thomashpa
Date: Tue Mar 3 22:06:42 2009
New Revision: 8800
URL: http://svn.gnome.org/viewvc/gnome-games?rev=8800&view=rev
Log:
Fix crasher bug where the a dict was being changed in one thread while another was iterating over the dict. Fixes bug #529792
Modified:
trunk/gnome-sudoku/src/lib/sudoku_generator_gui.py
trunk/gnome-sudoku/src/lib/sudoku_maker.py
Modified: trunk/gnome-sudoku/src/lib/sudoku_generator_gui.py
==============================================================================
--- trunk/gnome-sudoku/src/lib/sudoku_generator_gui.py (original)
+++ trunk/gnome-sudoku/src/lib/sudoku_generator_gui.py Tue Mar 3 22:06:42 2009
@@ -75,7 +75,8 @@
self.dialog.show_all()
self.dialog.set_transient_for(self.ui.w)
self.dialog.present()
- self.setup_base_status()
+ self.update_available()
+ self.initally_generated = self.sudoku_maker.n_puzzles()
def generate_method_changed_cb (self, *args):
if not self.generateForTargetRadio.get_active():
@@ -98,9 +99,7 @@
self.generateButton.set_sensitive(False)
def generate_cb (self, *args):
- self.ngenerated = 0
- self.generated_by_cat = {}
- self.toward_target = 0
+ self.initally_generated = self.sudoku_maker.n_puzzles()
self.pauseButton.set_sensitive(True)
self.stopButton.set_sensitive(True)
self.generateButton.set_sensitive(False)
@@ -143,7 +142,7 @@
self.dialog.hide()
self.dialog.destroy()
- def setup_base_status (self):
+ def update_available (self):
"""Setup basic status.
"""
for diff,lab in [('easy',self.easyLabel),
@@ -151,34 +150,20 @@
('hard',self.hardLabel),
('very hard',self.veryHardLabel)]:
num = self.sudoku_maker.n_puzzles(diff)
- self.increment_label(lab,num)
+ lab.set_text(ngettext("%(n)s puzzle","%(n)s puzzles",num)%{'n':num})
- def increment_label (self, lab, val=0):
- curtext = lab.get_text()
- if (not curtext):
- lab.pcount = val
- else:
- lab.pcount += 1
- newtext = ngettext("%(n)s puzzle","%(n)s puzzles",lab.pcount)%{'n':lab.pcount}
- lab.set_text(newtext)
+ def generated (self):
+ return self.sudoku_maker.n_puzzles() - self.initally_generated
def update_status (self, *args):
"""Update status of our progress bar and puzzle table.
"""
#print 'update_status!'
- npuzzles = sum(self.sudoku_maker.new_puzzles.values())
- #print 'npuzzles=',npuzzles,'ngenerated=',self.ngenerated
- if npuzzles > self.ngenerated:
- # updating gui...
- self.ngenerated=self.toward_target=npuzzles
- for cat in self.sudoku_maker.new_puzzles:
- to_add = self.sudoku_maker.new_puzzles[cat] - self.generated_by_cat.get(cat,0)
- self.increment_label(self.cat_to_label[cat],to_add)
- self.generated_by_cat[cat] = self.sudoku_maker.new_puzzles[cat]
+ self.update_available()
self.update_progress_bar()
if (self.generateForTargetRadio.get_active()
and
- self.toward_target>=int(self.newSudokusSpinButton.get_value())
+ self.generated()>=int(self.newSudokusSpinButton.get_value())
):
print 'Done!'
self.stop_cb()
@@ -200,12 +185,12 @@
if self.generateForTargetRadio.get_active():
tot = int(self.newSudokusSpinButton.get_value())
self.prog.set_fraction(
- float(self.toward_target)/tot
+ float(self.generated())/tot
)
try:
txt = ngettext('Generated %(n)s out of %(total)s puzzle',
'Generated %(n)s out of %(total)s puzzles',
- tot)%{'n':self.toward_target,'total':tot}
+ tot)%{'n':self.generated(),'total':tot}
except TypeError:
# Allow for fuzzy translation badness caused by a
# previous version having this done the wrong way
@@ -215,13 +200,13 @@
try:
txt = ngettext('Generated %(n)s out of %(total)s puzzle',
'Generated %(n)s out of %(total)s puzzles',
- tot)%(self.toward_target,tot)
+ tot)%(self.generated(),tot)
except:
# Fallback to English
- txt = 'Generated %s out of %s puzzles'%(self.toward_target,tot)
+ txt = 'Generated %s out of %s puzzles'%(self.generated(),tot)
else:
self.prog.pulse()
- txt = ngettext('Generated %(n)s puzzle','Generated %(n)s puzzles',self.toward_target)%{'n':self.toward_target}
+ txt = ngettext('Generated %(n)s puzzle','Generated %(n)s puzzles',self.generated())%{'n':self.generated()}
if self.paused: txt = txt + ' (' + _('Paused') + ')'
self.prog.set_text(txt)
Modified: trunk/gnome-sudoku/src/lib/sudoku_maker.py
==============================================================================
--- trunk/gnome-sudoku/src/lib/sudoku_maker.py (original)
+++ trunk/gnome-sudoku/src/lib/sudoku_maker.py Tue Mar 3 22:06:42 2009
@@ -303,7 +303,6 @@
puzzle_maker_args={'symmetrical':True},
batch_size = 5,
pickle_to = os.path.join(DATA_DIR,'puzzles')):
- self.new_puzzles={}
self.pickle_to = pickle_to
self.paused = False
self.terminated = False
@@ -313,6 +312,7 @@
self.load()
self.all_puzzles = {}
self.played = self.get_pregenerated('finished')
+ self.n_available_sudokus = {'easy':None,'medium':None,'hard':None,'very hard':None}
def load (self):
try:
@@ -380,12 +380,15 @@
if not difficulty_category:
return sum([self.n_puzzles(c,new=new) for c in sudoku.DifficultyRating.categories])
else:
+ if self.n_available_sudokus[difficulty_category]:
+ return self.n_available_sudokus[difficulty_category]
lines = self.get_pregenerated(difficulty_category)
count = 0
for line in lines:
if (not new) or line.split('\t')[0] not in self.played:
count+=1
- return count
+ self.n_available_sudokus[difficulty_category] = count
+ return self.n_available_sudokus[difficulty_category]
def list_puzzles (self, difficulty_category=None, new=True):
"""Return a list of all puzzles we have generated.
@@ -525,11 +528,8 @@
# self.solutions_by_puzzle[puzstring]=key
# self.all_puzzles[puzstring] = diff
# self.names[puzstring] = self.get_puzzle_name(_('Puzzle'))
- self.new_puzzles[diff.value_category()] = self.new_puzzles.get(diff.value_category(),0) + 1
outpath = os.path.join(self.pickle_to,
diff.value_category().replace(' ','_'))
- print 'Writing to ',os.path.join(self.pickle_to,
- diff.value_category().replace(' ','_'))
# Read through the existing file and make sure we're
# not a duplicate puzzle
existing = self.get_pregenerated(diff.value_category())
@@ -538,11 +538,10 @@
outfi = file(outpath,'a')
outfi.write(puzstring+'\t'+str(diff.value)+'\n')
outfi.close()
+ self.n_available_sudokus[diff.value_category()]+=1
except IOError, e:
print 'Error appending pregenerated puzzle: %s' % e.strerror
- print 'done writing...'
-
def pause (self, *args):
if hasattr(self,'new_generator'): self.new_generator.pause()
self.paused = True
@@ -565,7 +564,6 @@
self.terminated = False
if hasattr(self,'new_generator'): self.new_generator.termintaed = False
self.paused = False
- #self.new_puzzles = {}
generated = 0
while not limit or generated < limit:
if self.terminated:
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]