Created
April 28, 2016 18:31
-
-
Save nowox/fd62b89b69ea730f3dbd0969e7693fbe to your computer and use it in GitHub Desktop.
Python script that creates plenty of files and can alter them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import loremipsum | |
import random | |
import bisect | |
import numpy | |
import textwrap | |
class RandomWeight: | |
def __init__(self, weight): | |
self.items = weight.keys() | |
self.count = len(self.items) | |
self.breakpoints = numpy.cumsum(weight.values()) | |
def next(self): | |
i = bisect.bisect(self.breakpoints, random.random() * self.breakpoints[-1]) | |
return self.items[i] | |
def alter_file(filepath): | |
newtext = [] | |
if random.random() < 0.05: | |
return | |
for line in open(filepath, 'r').readlines(): | |
words = line.split() | |
if len(words) > w.count: | |
for _ in random.sample(range(0, len(words)), w.next()): | |
words[random.randint(0, len(words) - 1)] = random.choice(loremipsum.get_sentence()[2:-1].split()) | |
newtext.append(' '.join(words) + '\n') | |
open(filepath, 'w').write('\n'.join(newtext)) | |
w = RandomWeight({0:10, 1:1, 2:0.5, 3:0.25, 4:0.05}) | |
for m in ['a', 'b', 'c']: | |
for t in ['1', '2', '3', '4']: | |
dirname = os.path.join('data', ''.join([m,t])) | |
if not os.path.exists(dirname): | |
os.makedirs(dirname) | |
for i in range(512): | |
filepath = os.path.join(dirname, '.'.join([str(i), 'yml'])) | |
if os.path.isfile(filepath): | |
alter_file(filepath) | |
else: | |
open(filepath, 'w').write('\n\n'.join('\n'.join(textwrap.wrap(i, 80)) for i in loremipsum.get_paragraphs(4))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment