Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import re
- import os
- def genrandfiles():
- loopstop = int(raw_input('Enter number of random monkey files to generate: '))
- randlength = random.randint(0,1)
- filenum = 0
- filename = '/home/david/pythonfiles/12monks%d'
- loops = 0
- go = 0
- up = 0
- mkd = 0
- # This makes the range dir for range of the files for later matched len of dictionary
- # regex word matches
- if mkd == 0:
- makerangedirs()
- # Then sets to one after function call which eliminates overwriting the directory
- # It won't actually overwrite, it throws an already exists error.
- mkd += 1
- # While loops is less than the number of user determined files to be generated
- # in loopstop
- while loops < loopstop:
- # loops as long as the random length of the file is less than the randomly
- # generated number
- if go <= randlength:
- # Increments 'go' by one on each randomly created character for random
- # length file until reached, then progresses
- go += 1
- # Find a random ordinal between 32 and 127 to place in random length file
- randord = random.randint(32,127)
- # Turn ordinal into a character
- charord = chr(randord)
- # Open or create a textile to store the random characters in
- textfile = file(filename % filenum, 'a+')
- # write the character to the file in lowercase
- textfile.write(charord.lower()+ 'zygote')
- textfile.close()
- # after textfile.close(), loops back through 'if go <= randlength'
- # regexfiles if random length file is through generating
- if go > randlength:
- # send regexfiles() the 'filename' and current 'filenum',
- # so it can be moved to the approp range directory. 'up', is also passed
- # should any other functions external to the master function
- # call for incremented variables.
- # This keep from resetting the variable when the external function is called again
- # and keeps from rewriting previously placed files, as we loop through
- # randomlen file creation, regex, and sorting process contained in the
- # module hierarchy
- regexfiles(filename % filenum, up)
- # We also increment the variable used in the external function when it
- # returns to the master function
- up +=1
- filenum += 1
- # reset go to loop through characters if another file will be generated
- go = 0
- loops += 1
- # This is called once by a boolean value in the master function
- # This function makes a 'range' directory containing different ranges
- # of lens that will be matched against the files in regexfiles()
- def makerangedirs():
- # Make ranges container dir
- os.mkdir('/home/david/pythonfiles/ranges')
- ranges = [
- '1000',
- '5000',
- '10000',
- '20000',
- '25000',
- '50000',
- '100000',
- '250000',
- '500000',
- '1000000',
- '10000000']
- # iterate through the ranges list, and create a dir under ranges for each
- for selection in ranges:
- os.mkdir('/home/david/pythonfiles/ranges/%d' % int(selection))
- def regexfiles(filename, up):
- textfile = file(filename, 'r')
- # Open the dictionary file
- dictionary = file('/usr/share/dict/american-english', 'r')
- search = 0
- finds = 0
- readict = dictionary.readlines()
- readfile = textfile.readlines()
- lenfile = len(readfile)
- wordlist = []
- for item in readict:
- wordlist.append(item)
- search += 1
- # 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'
- check = len(re.findall( str(item) + '*', str(readfile)))
- #print check, re.findall( str(item) + '*', str(readfile))
- if check > 0:
- #print 'true', check , finds
- finds += check
- sortfiles(finds, filename, up)
- def sortfiles(finds, filename, up):
- range1 = 1000
- range2 = 5000
- range3 = 10000
- range4 = 20000
- range5 = 25000
- range6 = 50000
- range7 = 100000
- range8 = 250000
- range9 = 500000
- range10 = 1000000
- range11 = 10000000
- if finds <= range1:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range1, up))
- if finds >= range2 and finds < range3:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range2, up))
- if finds >= range3 and finds < range4:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range3, up))
- if finds >= range4 and finds < range5:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range4, up))
- if finds >= range5 and finds < range6:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range5, up))
- if finds >= range6 and finds < range7:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range6, up))
- if finds >= range7 and finds < range8:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range7, up))
- if finds >= range8 and finds < range9:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range8, up))
- if finds >= range9 and finds < range10:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range9, up))
- if finds >= range10 and finds < range11:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range10, up))
- if finds >= range11:
- os.rename(filename, '/home/david/pythonfiles/ranges/%d/%d' % (range11, up))
- genrandfiles()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement