1. Nicolas Dumazet
  2. wikipedia-dumzibot

Commits

Nicolas Dumazet  committed 9620960

use an EvaluationResult class to clean up code

  • Participants
  • Parent commits e2cbeba
  • Branches default

Comments (0)

Files changed (1)

File statistics.py

View file
 modify it under the terms of the MIT License.
 """
 
-import urllib, re, cPickle
+import urllib, re
 from collections import defaultdict
+import UserDict
 import pywikibot
 
 def putNb(page,nb):
 importance = [u'maximum', u'élevée', u'moyenne', u'faible', u'inconnue']
 quality = [u'AdQ', u'A', u'BA', u'B', u'BD', u'ébauche', u'inconnu']
 
-URLbase = u'http://toolserver.org/~bayo/intercat.php?'
-patURL = u'formCat1=%s d\'avancement %s&formCat2=%s d\'importance %s'
+URLbase = u'http://toolserver.org/~bayo/intercat.php?' \
+           'formCat1=%s d\'avancement %s&formCat2=%s d\'importance %s'
+
+class EvaluationResult(UserDict.IterableUserDict):
+    # Filling up a dictionary where keys are meant for template
+    # https://secure.wikimedia.org/wikipedia/fr/wiki/Utilisateur:NicDumZ/Eval_Detail
+    # Table positions from 0 to 48, plus 'projet'
+    def __init__(self, project_name, prefix):
+        UserDict.IterableUserDict.__init__(self)
+        self['projet'] = project_name
+        self.prefix = prefix
+
+    def qualitySum(self, quality_index, category_name, nb):
+        # last column of the table: 6,12,18,24,30,36,42 sums by quality
+        val = u'[[:%s|{{formatnum:%s}}]]' % (category_name, nb)
+        self[6*(quality_index + 1)] = val
+
+    def importanceSum(self, importance_index, category_name, nb):
+        # last line of the table: [43; 48], sums by importance
+        val = u'[[:%s|{{formatnum:%s}}]]' % (category_name, nb)
+        self[43 + importance_index] = val
+
+    def grid(self, qid, iid, nb):
+        q = urllib.quote(quality[qid].encode('utf-8'))
+        i = urllib.quote(importance[iid].encode('utf-8'))
+        # 6 columns per row
+        index = 6*(qid + 1) + (iid + 1)
+        if nb:
+            url = URLbase % (self.prefix, q, self.prefix, i)
+            url = url.replace(' ', '%20')
+            self[index] = u'<span class="plainlinks">[%s {{formatnum:%s}}]' \
+                    '</span>' % (url, nb,)
+        else:
+            self[index] = u''
+
+    def total(self, nb):
+        # bottom right
+        self[48] = '{{formatnum:%s}}' % totalEval
+
 
 pywikibot.setAction(u'Mise à jour des statistiques WP 1.0')
 
 #        except (IOError, EOFError):
 #            oldArticles = {}
 
-
-        # Filling up a dictionary where keys are meant for template
-        # https://secure.wikimedia.org/wikipedia/fr/wiki/Utilisateur:NicDumZ/Eval_Detail
-        # Keys from 0 to 48, plus 'projet', plus 'total'
-        # XXX it would be nicer to use an appropriate Evaluation class instead
-        #   of a dict: it would allow cleaner semantics
-        results[project] = {}
-
-        results[project]['projet'] = project
+        # quote the prefix so it can be used in an URL
+        if isinstance(prefix, unicode):
+            prefix = prefix.encode('utf-8')
+        prefix = urllib.quote(prefix)
+        results[project] = EvaluationResult(project, prefix)
 
         total = 0
         # first walk an importance axis
                 artByCat[imp].add(p)
                 articles[p.title()][0] = imp
             lenCat = len(artByCat[imp])
-            # last line of the table: [43; 48], sums by importance
-            results[project][43 + i] = u'[[:%s|{{formatnum:%s}}]]' % (catname, lenCat)
+            results[project].importanceSum(i, catname, lenCat)
             total += lenCat
 
         # and then walk the quality axis
                     articles[p.title()][1] = av
                 except KeyError:
                     pass
-            # last column of the table: 6,12,18,24,30,36,42 sums by quality
-            results[project][6*(i + 1)] = u'[[:%s|{{formatnum:%s}}]]' % (catname, len(artByCat[av]))
+            results[project].qualitySum(i, catname, len(artByCat[av]))
 
-
-        # quote the prefix so it can be used in an URL
-        if isinstance(prefix, unicode):
-            prefix = prefix.encode('utf-8')
-        prefix = urllib.quote(prefix)
-
-        i = 1
-        for qual in quality:
-            a = urllib.quote(qual.encode('utf-8'))
-            for imp in importance:
-                im = urllib.quote(imp.encode('utf-8'))
+        # perform intersections for center of the table
+        for qid, qual in enumerate(quality):
+            for iid, imp in enumerate(importance):
                 inter = len(artByCat[av].intersection(artByCat[imp]))
-                if inter:
-                    url = URLbase + patURL % (prefix, a, prefix, im)
-                    url = url.replace(' ', '%20')
-                    results[project][i] = u'<span class="plainlinks">[%s {{formatnum:%s}}]</span>' % (url, inter,)
-                else:
-                    results[project][i] = ''
-                i += 1
-            # skip the sum
-            i += 1
+                results[project].grid(qid, iid, inter)
 
 ### This is unfinished/untested feature "history" of evaluations
 #        oldKeys = set(oldArticles.keys())
         t = pywikibot.Page(pywikibot.Link(u'Projet:%s/Évaluation/Total évalué' % project))
         putNb(t, totalEval)
 
-        # bottom right value
-        results[project][48] = '{{formatnum:%s}}' % totalEval
+        results[project].total(totalEval)
 
 
     tl = u'<!-- BEGIN BOT SECTION -->'