Created
April 2, 2022 23:08
-
-
Save Wind010/d39419bd222bf589b1ac85a7557ceb01 to your computer and use it in GitHub Desktop.
Script to generate random string content
This file contains hidden or 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
| import sys, getopt, os, random, string | |
| def generate_random_string(character_space: str, size_in_bytes: int) -> str: | |
| return "".join([random.choice(character_space) for i in range(size_in_bytes)]) | |
| def generate_random_string_file(filepath: str, size_in_bytes: int) -> None: | |
| """ | |
| Generate big random letters/alphabets to a file. | |
| :param filepath: The name of the path and filename to create. | |
| :param size_in_bytes: Size in bytes | |
| :return: None | |
| """ | |
| character_space = string.ascii_letters + string.digits + " " + "\n" | |
| random_string = generate_random_string(character_space, size_in_bytes) | |
| print(f"Creating '{filepath}'...") | |
| with open(filepath, 'w') as f: | |
| f.write(random_string) | |
| def generate_random_content_files(filepath: str, size_in_bytes: int, number_of_files: int) -> None: | |
| if number_of_files == 0: | |
| raise | |
| if number_of_files == 1: | |
| generate_random_string_file(filepath, size_in_bytes) | |
| return | |
| filename = os.path.basename(filepath) | |
| dir = os.path.dirname(filepath) | |
| filename, ext = tuple(filename.split('.')) | |
| for n in range(number_of_files): | |
| new_filename = f"{filename}_{str(n).zfill(number_of_files)}.{ext}" | |
| new_filepath = os.path.join(dir, new_filename) | |
| generate_random_string_file(new_filepath, size_in_bytes) | |
| def main(argv) -> None: | |
| size_in_bytes: int = 1 | |
| number_of_files: int = 1 | |
| filepath, container, connection_string = "", "", "" | |
| USAGE_MSG = "content_generator.py -s <size_in_bytes> -n <number_of_files> -f <filepath> or -s <connection_string> -c <container>" | |
| try: | |
| opts, args = getopt.getopt(argv, "hn:s:f:cs:c:", [ | |
| "number_of_files=", | |
| "size_in_bytes=", | |
| "filepath=", | |
| "connection_string=", | |
| "container=" | |
| ]) | |
| except getopt.GetoptError: | |
| print(USAGE_MSG) | |
| sys.exit(2) | |
| for opt, arg in opts: | |
| if opt == '-h': | |
| print(USAGE_MSG) | |
| sys.exit() | |
| elif opt in ("-n", "--number_of_files"): | |
| number_of_files = int(arg) | |
| elif opt in ("-s", "--size_in_bytes"): | |
| size_in_bytes = int(arg) | |
| elif opt in ("-f", "--filepath"): | |
| filepath = arg | |
| elif opt in ("-s", "--connection_string"): | |
| connection_string = arg | |
| elif opt in ("-c", "--container"): | |
| container = arg | |
| if filepath: | |
| generate_random_content_files(filepath, size_in_bytes, number_of_files) | |
| else: | |
| raise NotImplementedError("TODO") | |
| if __name__ == "__main__": | |
| main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment