<commit_before><commit_msg>make base class for file generators<commit_after>import os
import random
import string
import tempfile


class FileGenerator(object):
    def __init__(self, directory, prefix='', suffix='', delete=False):
        """ Constructor of FileGenerator

        Args:
            directory: the directory for saving generated files
            prefix: the prefix of the filename for generated files
            suffix: the suffix of the filename for generated files
            delete: whether to delete the temp files on file close or not
        """
        if not os.path.isdir(directory):
            raise AttributeError("{} is not a valid directory.".format(directory))
        self.directory = directory
        self.prefix = prefix
        self.suffix = suffix
        self.delete = delete

    def make_file(self, size, filename=None, filename_length=10, is_binary=True, **kwargs):
        """make a file

        Args:
            size: file size in bytes
            others omitted

        Return:
            the file object of the created file (not closed)
        """
        content = self.generate_file_content(size=size, **kwargs)
        filename = filename or self.random_filename(filename_length)
        if is_binary:
            mode = 'wb'
        else:
            mode = 'w'
        tmp_file = tempfile.NamedTemporaryFile(mode=mode,
                                               prefix=self.prefix,
                                               suffix=self.suffix,
                                               dir=self.directory,
                                               delete=self.delete)
        tmp_file.write(content)
        tmp_file.flush()
        return tmp_file

    def generate_file_content(self, size, **kwargs):
        raise NotImplementedError()

    def random_filename(self, length=10):
        choices = string.ascii_letters + string.digits
        random_str = ''.join([random.choice(choices) for n in range(length)])
        filename = ''.join([self.prefix, random_str, self.suffix])
        return filename
{'fraction_non_alphanumeric': 0.08013435700575816, 'fraction_numerical': 0.0019193857965451055, 'mean_word_length': 1.864010989010989, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3847234', 'n_tokens_mistral': 502, 'n_tokens_neox': 486, 'n_words': 156}