Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2010
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. import random
  2. import re
  3. import os
  4.  
  5.    
  6. def genrandfiles():
  7.     loopstop = int(raw_input('Enter number of random monkey files to generate: '))
  8.     randlength = random.randint(0,1)
  9.     filenum = 0
  10.     filename = '/home/david/pythonfiles/12monks%d'
  11.     loops = 0
  12.     go = 0
  13.     up = 0
  14.     mkd = 0
  15.     # This makes the range dir for range of the files for later matched len of dictionary
  16.     # regex word matches
  17.     if mkd == 0:
  18.         makerangedirs()
  19.         # Then sets to one after function call which eliminates overwriting the directory
  20.         # It won't actually overwrite, it throws an already exists error.
  21.         mkd += 1
  22.         # While loops is less than the number of user determined files to be generated
  23.         # in loopstop
  24.     while loops < loopstop:
  25.         # loops as long as the random length of the file is less than the randomly
  26.         # generated number
  27.         if go <= randlength:
  28.             # Increments 'go' by one on each randomly created character for random
  29.             # length file until reached, then progresses
  30.             go += 1
  31.         # Find a random ordinal between 32 and 127 to place in random length file
  32.             randord = random.randint(32,127)
  33.         # Turn ordinal into a character
  34.             charord = chr(randord)
  35.         # Open or create a textile to store the random characters in
  36.             textfile = file(filename % filenum, 'a+')    
  37.         # write the character to the file in lowercase
  38.  
  39.             textfile.write(charord.lower()+ 'zygote')
  40.             textfile.close()
  41.         # after textfile.close(), loops back through 'if go <= randlength'
  42.     # regexfiles if random length file is through generating
  43.         if go > randlength:
  44.             # send regexfiles() the 'filename' and current 'filenum',
  45.             # so it can be moved to the approp range directory. 'up', is also passed
  46.             # should any other functions external to the master function
  47.             # call for incremented variables.
  48.             # This keep from resetting the variable when the external function is called again
  49.             # and keeps from rewriting previously placed files, as we loop through
  50.             # randomlen file creation, regex, and sorting process contained in the
  51.             # module hierarchy
  52.             regexfiles(filename % filenum, up)
  53.             # We also increment the variable used in the external function when it
  54.             # returns to the master function
  55.             up +=1
  56.             filenum += 1
  57.             # reset go to loop through characters if another file will be generated        
  58.             go = 0
  59.             loops += 1
  60.  
  61. # This is called once by a boolean value in the master function
  62. # This function makes a 'range' directory containing different ranges
  63. # of lens that will be matched against the files in regexfiles()
  64. def makerangedirs():
  65.     # Make ranges container dir
  66.     os.mkdir('/home/david/pythonfiles/ranges')
  67.     ranges = [
  68.     '1000',
  69.     '5000',
  70.     '10000',
  71.     '20000',
  72.     '25000',
  73.     '50000',
  74.     '100000',
  75.     '250000',
  76.     '500000',
  77.     '1000000',
  78.     '10000000']
  79.     # iterate through the ranges list, and create a dir under ranges for each
  80.     for selection in ranges:
  81.             os.mkdir('/home/david/pythonfiles/ranges/%d' % int(selection))
  82.  
  83. def regexfiles(filename, up):
  84.     textfile = file(filename, 'r')
  85.                 # Open the dictionary file
  86.     dictionary = file('/usr/share/dict/american-english', 'r')
  87.     search = 0
  88.     finds = 0
  89.     readict = dictionary.readlines()
  90.     readfile = textfile.readlines()
  91.     lenfile = len(readfile)
  92.     wordlist = []
  93.     for item in readict:
  94.         wordlist.append(item)
  95.         search += 1
  96.         # print search, 'item = ' , item, str(item) , '\nreadfile = ' , str(readfile) , '\nre.findall =' , re.findall( str.rstrip(item), str(readfile)) , '\nre.search = ' , re.search(str.rstrip(item), str(readfile)), '\n'
  97.         check = len(re.findall( str(item) + '*', str(readfile)))
  98.         #print check, re.findall( str(item) + '*', str(readfile))
  99.         if check > 0:
  100.             #print 'true', check , finds
  101.             finds += check
  102.     sortfiles(finds, filename, up)
  103.  
  104. def sortfiles(finds, filename, up):
  105.     range1 = 1000
  106.     range2 = 5000
  107.     range3 = 10000
  108.     range4 = 20000
  109.     range5 = 25000
  110.     range6 = 50000
  111.     range7 = 100000
  112.     range8 = 250000
  113.     range9 = 500000
  114.     range10 = 1000000
  115.     range11 = 10000000
  116.     if finds <= range1:
  117.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range1, up))
  118.     if finds >= range2 and finds < range3:
  119.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range2, up))
  120.     if finds >= range3 and finds < range4:
  121.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range3, up))
  122.     if finds >= range4 and finds < range5:
  123.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range4, up))
  124.     if finds >= range5 and finds < range6:
  125.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range5, up))
  126.     if finds >= range6 and finds < range7:
  127.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range6, up))
  128.     if finds >= range7 and finds < range8:
  129.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range7, up))
  130.     if finds >= range8 and finds < range9:
  131.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range8, up))
  132.     if finds >= range9 and finds < range10:
  133.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range9, up))
  134.     if finds >= range10 and finds < range11:
  135.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range10, up))
  136.     if finds >= range11:
  137.         os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range11, up))
  138.  
  139.  
  140. genrandfiles()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement