Paste number 92813: Generate IDs

Paste number 92813: Generate IDs
Pasted by: sbp
When:6 months, 4 weeks ago
Share:Tweet this! | http://paste.lisp.org/+1ZM5
Channel:#swhack
Paste contents:
Raw Source | XML | Display As
#!/usr/bin/env python

import sys, random

consonants = 'bcdfghjklmnpqrstvwxz'

def label(n=5): 
   return ''.join([random.choice(consonants) for i in xrange(n)])

def main(): 
   f = open(sys.argv[1])
   for line in f: 
      for n in (1, 2, 3): 
         header = '<h%i>' % n
         if line.count(header) == 1: 
            line = line.replace(header, '<h%i id="%s">' % (n, label()))
      if line.count('id=""') == 1: 
         line = line.replace('id=""', 'id="' + label() + '"')
      sys.stdout.write(line)
   f.close()

if __name__ == '__main__': 
   main()

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.