#!/usr/local/cpython-3.9/bin/python3 """ Attempt to recreate the re problem seen in music-pipeline, as an SSCCE. In this one, we do things pretty close to how music-pipeline does them. """ import random import re import string def one_alphanum_random_string(): """Return one alphanumeric string of 20 characters.""" alphabet = string.ascii_letters + string.digits return ''.join(random.choices(alphabet, k=20)) def one_regex(): """Return one semi-random, not-compiled regex string.""" return r'^\./{0}/{0}/{0}\.'.format(one_alphanum_random_string()) def one_filename(): """Return one semi-random, filename.""" return './{0}/{0}/{0}.mp3'.format(one_alphanum_random_string()) def main(): """Start the ball rolling.""" regex_strings = [one_regex() for repno in range(2_046)] regex_compiled = [re.compile(pattern) for pattern in regex_strings] filenames = [one_filename() for repno in range(14_000)] matches = 0 for filename in filenames: for regex in regex_compiled: if regex.match(filename): matches += 1 print(matches) main()