User:Slashme/parliament.py

From Wikipedia, the free encyclopedia

  1. !/sys/pkg/bin/python

import cgi, re, math, random print "Content-type: text/html" print print """ <html> <head><title>Format parliament diagram</title></head> <body>

Contents

Under construction

Format parliament diagram

See <a href="http://en.wikipedia.org/wiki/User:Slashme/parliament.py">the source code</a> and <a href="http://en.wikipedia.org/wiki/User_talk:Slashme/parliament.py">the bug list</a>

How to use this script:

  • Type the title of your graph (e.g. "Blueland Senate 2093") in the box marked "graph title"
  • Type the list of parties in the box marked "List of parties". Each party must have a name and a number of seats (can be 0), and if you like you can also add a colour. The parties in the list must be separated by semicolons (";") and the party information must be separated by commas (",") (For example, if you type: "Party A, 33; Party B, 22, #99FF99" you will have two parties, one labeled "Party A" with 33 seats and a random colour, and one labeled "Party B" in light green.)
  • The hight of the diagram (in blocks) is chosen automatically, but if you need to change this, you can set it by hand by typing a number in the box marked "Height of diagram in blocks"
  • The script will give the code of the svg diagram when you press "send". You can cut and paste the code into a text editor and either edit it or upload it onto Wikipedia.

""" form = cgi.FieldStorage() inputlist = form.getvalue("inputlist", "") title = form.getvalue("title", "") inputht = form.getvalue("inputheight", "") inputheight = 0 if inputht:

 inputheight = int(inputht)

if inputlist:

 print "[Party, support, colour]
" #initialize list of parties partylist=[] #Keep a running total of the number of delegates in the diagram, for use later. sumdelegates=0 #error flag: This seems ugly, but what should I do? error=0 for i in re.split("\s*;\s*",inputlist): partylist.append(re.split('\s*,\s*', i)) for i in partylist: if len(i)<2: print "short record: "+str(i)+"
" error=1 elif re.search('[^0-9]', i[1]): print "not a positive integer: "+i[1]+"
" error=1 else: i[1]=int(i[1]) sumdelegates += i[1] if sumdelegates > 10000: print "More than 10000 members? That's an election result, not a legislature!
" error=1 if len(i)<3: i.append("#%02x%02x%02x" % (random.randrange(255), random.randrange(255), random.randrange(255))) elif not re.match('^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]$',i[2]): print "RGB colour wanted, \""+i[2]+"\" found. Random colour assigned." i[2]=("#%02x%02x%02x" % (random.randrange(255), random.randrange(255), random.randrange(255))) print str(i)+"
" if sumdelegates < 3: print "Less than 3 members? That's not even a town council, let alone a legislature!
" error=1 if not error: #Initialize counters for use in layout spotcounter=0 lines=0 #If the user specified a column height, use that if inputheight < 100 and inputheight > 1: colheight=inputheight #Or make the diagram a rectangle of approx. 2:1 aspect ratio: else: colheight=int(math.sqrt(sumdelegates/2))

print "

Cut and paste the following SVG code:

" print '

' print "
"
    #Write svg header:
    print '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
    print '<svg xmlns:svg="http://www.w3.org/2000/svg"'
    print 'xmlns="http://www.w3.org/2000/svg" version="1.0"'
    #Make blocks fit into a 400px column with a space of 1/3 of the block width between them
    blockwidth=1200/(4*colheight+1)
    #Height of diagram is 400 px plus the height of the key and title.
    totalwidth=int(blockwidth*(1.0/3+math.ceil(sumdelegates/float(colheight))*4/3))
    print('width="'+str(totalwidth)+'" height="'+str(440+20*(3*len(partylist)+1))+'">')
    print('<g>')
    print('<text x="'+str(totalwidth/2)+'" y="37" style="font-size:36px;font-weight:bold;text-align:center;text-anchor:middle;font-family:Sans">'+title+'</text>')
    #Will need a numerical counter, so don't use "for i in partylist"
    for i in range(len(partylist)):
      #Make each party's blocks an svg group
      print('  <g style="fill:'+partylist[i][2]+'">')
      for j in range(partylist[i][1]):
        print('    <rect x="'+str(lines*int(blockwidth*4/3)+blockwidth/3)+'" y="'+str(spotcounter*int(blockwidth*4/3)+blockwidth/3+40)+'" width="'+str(blockwidth)+'" height="'+str(blockwidth)+'"/>')
        spotcounter+=1
        if spotcounter == colheight:
          spotcounter = 0
          lines+=1
      #Include the rectangle in the key in the party's group, for ease in updating colours.
      print('    <rect x="'+str(int(blockwidth/3))+'" y="'+str(440+20+60*i)+'" width="30" height="30"/>')
      print('  </g>')
      print('  <text style="font-size:24px" x="'+str(int(blockwidth/3)+40)+'" y="'+str(440+20+60*i+24)+'">'+str(partylist[i][1])+": "+str(int(100*partylist[i][1]/sumdelegates))+"% "+partylist[i][0]+'</text>')
    print('</g>')
    print('</svg>')
    print "
" print '

'

print """

 <form method="post" action="testpy.cgi">

Graph title: <input type="text" name="title"/>

List of parties: <input type="text" name="inputlist"/>

Height of diagram in blocks (optional): <input type="INT" name="inputheight"/>

<INPUT type="submit" value="Send">

 </form>

</body> </html> """