Commons:Picture of the Year/2015/R1/Results/Python code
Jump to navigation
Jump to search
- POTY 2015 : Introduction – Rules – Discussion – Translations – Committee – Help – R1 Candidates / Gallery – R2 Gallery – Results
code
//Extremely hacky and ugly code, just to get something up. Never code like this in real life
import json
with open("poty2015r1.json") as f:
votes=json.loads(f.read())
//Note eligible voters that were marked in the json as ineligible
renamed_pairs=[[u'Zaphod',u'Jonnie Nord'],[u'Vikoula5',u'Reda Kerbouche'],[u'Ask27',u'SnapMeUp'],[u'Garik.Khachatryan',u'TonJ'],[u'Alifatehighahfarokhi',u'AliFatehi'],[u'জুলমাত আলাম',u'আজিজ'],[u'Scipio83',u'Scip.'],[u'Orkosiada',u'ים'],[u'محمد الفلسطيني',u'بدارين'],[u'JLanzer',u'Jacob Lanzer'],[u'Fsandlinux',u'Hatas'],[u'Piotrjutkiewicz',u'Ppiioottrr'],[u'Topshelvr',u'Topshelver'],[u'Marce79',u'Marcello Gianola'],[u'Theodor2001',u'Yuvalbab']]
eligible=[]
for pair in renamed_pairs:
eligible+=pair
//gather votes for each file
results=dict()
for voter in votes:
if votes[voter]['eligibility'] or voter in eligible:
for v in votes[voter]['votes']['votesByCandidate'].keys():
v=v.replace("_"," ")
if v in results:
results[v].append(voter)
else:
try:
results[v]=[voter,]
except:
print voter, v
print 1/0 //d'oh... never get here
//make sure no renamed user voted for the same image under two usernames
for res in results.values():
for p in renamed_pairs:
if p[0] in res and p[1] in res:
print p, "voted twice"
//put the wikitext of [[Commons:Picture_of_the_Year/2015/R1/Gallery]] here as a raw string
catstring=""""""
//split into chunks, luckily each has one gallery, thus one closing tag
cs=catstring.split("</gallery>")
//find the category names, populate dictionary with files and reverse dictionary to find cat of a file
categories=dict()
for cgroup in cs:
cgroup=cgroup.split("\n")
cat=""
files=[]
for line in cgroup:
if "== [[" in line:
if cat !="":
print "ERROR: cat already populated",cat,line
print 1/0
cat=line.replace("==","").strip()
else:
if line.startswith("File:"):
files.append(line.split("|")[0].replace("_"," "))
if len(cat)!=0:
categories[cat]=files[:]
catof=dict()
for cat in categories:
for f in categories[cat]:
catof[f]=cat
//Note file moves
filemoves={"File:Japanese beautyberry, October 2015 - Stacking.jpg":"File:Purple beautyberry, October 2015 - Stacking.jpg",
"File:Eastern great egret 2015-06-17.jpg":"File:Egretta garzetta 2015-06-17.jpg",
"File:2015.05.24.-15-Buchklingen--Pyramideneule-Raupe.jpg":"File:2015.05.24.-15-Buchklingen--Svenssons Pyramideneule-Raupe.jpg",
"File:Southern armyworm, pupae, side 2014-06-04-21.10.13 ZS PMax (15750996370).jpg":"File:Spodoptera eridania (Southern armyworm) pupae lateral view.jpg",
"File:Doña Isabel Cobos de Porc by Francisco Goya.jpg":"File:Portrait of Doña Isabel de Porcel by Francisco Goya.jpg",
"File:Set design by Philippe Chaperon for Act4 sc2 of Aida by Verdi 1871 Cairo.jpg":"File:Set design by Philippe Chaperon for Act4 sc2 of Aida by Verdi 1880 Paris.jpg",
"File:Black-faced impala from Etosha National Park.jpg":"File:Black-faced impala from Etosha National Park, Namibia, 2014.jpg",
"File:Cervo do Pantano Perfil.jpg":"File:Femea Cervo do Pantanal Perfil.jpg",
"File:Love padlocks at Butchers' Bridge (Ljubljana).JPG":"File:Love padlocks on the Butchers' Bridge (Ljubljana).jpg",
"File:Pažaislis Monastery interior 1, Kaunas, Lithania - Diliff.jpg":"File:Pažaislis Monastery interior 1, Kaunas, Lithuania - Diliff.jpg",
"File:San Cesareo de Appia (Rome) ,interior.jpg":"File:San Cesareo de Appia (Rome), interior.jpg",
"File:Brown-eared bulbuls perching on the cables.jpg":"File:White-cheeked Starlings perching on the cables.jpg"
}
//do per category
catres={}
for c in categories:
tempres=[]
for f in categories[c]:
if f in filemoves:
f=filemoves[f]
try:
tempres.append([f,len(results[f[5:].decode("utf-8")]),c])
except:
print f
catres[c]=sorted(tempres,key=lambda x: -x[1])
//get top 30 overall
from itertools import chain
fullres=sorted(chain(*catres.values()),key=lambda x: -x[1])
rank=0
for r in fullres:
rank+=1
r+=[rank,[a[0] for a in catres[r[-1]]].index(r[0])+1]
outres=fullres[:30]
//print in table format
for line in outres:
print "<tr>",
for item in line:
print "<td>",
if type(item) is str and "File:" in item:
print "[["+item+"|16px]] [[:"+item+"]]",
else:
print item,
print "</td>",
print "</tr>"
//note which files done so far
outnames=[a[0] for a in outres]
//do top two of each cat if not already done
for c in catres:
for i in (0,1):
if catres[c][i][0] not in outnames:
for item in catres[c][i]:
print "<td>",
if type(item) is str and "File:" in item:
print "[["+item+"|16px]] [[:"+item+"]]",
else:
print item,
print "</td>",
print "</tr>"
- This code is writtern by Storkk.