def upload(): req = request.json # Making sure the request is in JSON if req == None: return generate_error(400, "Request body must contain valid JSON") # Making sure all required fields are in the body if not 'image' in req: return generate_error(400, "Request body must contain image field") # Getting the image in binary image = base64.b64decode(req['image']) # Validating the image if 20000 > len(image) or len(image) > 15000000: return generate_error(400, 'Image must be larger than 20 KB and smaller than 15 MB') # Generate a random slug & secret slug = exrex.getone('([a-z0-9]{8})') secret = exrex.getone('([a-zA-Z0-9]{24})') try: # Try to insert the document into the collection MONGO_COLLECTION.insert_one({ '_id': slug, 'secret': secret, 'image': image }) return jsonify({'slug': slug, 'secret': secret}), 200 except Exception as ignored: return generate_error(500, 'Internal Database Error') return generate_error(500, 'Internal Server Error')
def generate_string(sch, field): if sch["properties"][field]["type"] == "array": try: pat = sch["properties"][field]["items"]["pattern"] return exrex.getone(pat) except KeyError: letters = string.ascii_lowercase try: length = random.randint( sch["properties"][field]["items"]["minlength"], sch["properties"][field]["items"]["maxlength"]) except KeyError: length = random.randint(3, 10) return ''.join(random.choice(letters) for i in range(length)) else: try: pat = sch["properties"][field]["pattern"] return exrex.getone(pat) except KeyError: letters = string.ascii_lowercase try: length = random.randint(sch["properties"][field]["minlength"], sch["properties"][field]["maxlength"]) except KeyError: length = random.randint(3, 10) return ''.join(random.choice(letters) for i in range(length))
def getone_test(tries=200): for regex, _ in RS.items(): for _ in range(tries): try: s = getone(regex) if IS_PY3: assert re.match(regex, s, re.U) else: assert re.match(regex, s.encode("utf-8"), re.U) except Exception: if IS_PY3: print('[E] Assertion error! "%s"\n\t%s not match' % (regex, s)) return -1 else: print('[E] Assertion error! "%s"\n\t%s not match' % (regex.decode("utf-8"), s)) return -1 for regex in BIGS: for _ in range(tries): try: s = getone(regex) if IS_PY3: assert re.match(regex, s, re.U) else: assert re.match(regex, s.encode("utf-8"), re.U) except: if IS_PY3: print('[E] Assertion error! "%s"\n\t%s not match' % (regex, s)) return -1 else: print('[E] Assertion error! "%s"\n\t%s not match' % (regex.decode("utf-8"), s)) return -1 return 0
def string_all(self, lengthlist, randomvalue): ''' lengthlist : 取值长度的case列表 randomvalue : 是否包含动态值,包含的话就是占位符,不包含则为空串 ''' allobj = [] if lengthlist: for length in lengthlist: lengthispass = '******' if length['ispass'] else '长度不合适' value = exrex.getone(self.regular['all'] + '{' + str(length['value']) + '}') allobj.append({ 'title': '合规字符串' + lengthispass, 'value': value + randomvalue, 'ispass': True and length['ispass'] }) else: for i in range(5): #随机生成5条正向记录 value = exrex.getone(self.regular['all'] + '+') allobj.append({ 'title': '符合要求的字符串', 'value': value + randomvalue, 'ispass': True }) return allobj
def generate_not_squirmy(): squirmy = '(A+(TAC|CAT)A)T(CG*T)*(A+(TAC|CAT)A)CG*T' not_squirmy = '^((?!(' + squirmy + '))(A|C|T|G))*$' ns = exrex.getone(not_squirmy,limit=18) while ns.strip() == "": ns = exrex.getone(not_squirmy,limit=18) return ns
def generate(self): if self.regex.lower() == 'none': if self.min_length > self.max_length: self.min_length, self.max_length = self.max_length, self.min_length string = getone('[ -~]{' + str(self.min_length) + ',' + str(self.max_length) + '}') else: string = getone(self.regex) return string
def generate_email_websites(n=20): emails = [] for i in range(n): emails.append(exrex.getone('[a-z0-9]{10}\@[a-z]{5}\.[a-z]{3}')) emails = np.asarray(emails) website_addrs = [] for i in range(n): website_addrs.append(exrex.getone('www\.[a-z0-9]{10}\.[a-z]{3}')) website_addrs = np.asarray(website_addrs) X = np.concatenate((emails, website_addrs)) y = np.concatenate((['email']*n, ['website']*n)) return shuffle_data(X, y)
def getone_test(tries): for regex,_ in RS.items(): for _ in range(tries): s = getone(regex) try: assert match(regex, s) except: print '[E] Assertion error! "%s"\n\t%s not match' % (regex, s) for regex in BIGS: for _ in range(tries): s = getone(regex) try: assert match(regex, s) except: print '[E] Assertion error! "%s"\n\t%s not match' % (regex, s)
def getone_test(tries): for regex, _ in RS.items(): for _ in range(tries): s = getone(regex) try: assert match(regex, s) except: print '[E] Assertion error! "%s"\n\t%s not match' % (regex, s) for regex in BIGS: for _ in range(tries): s = getone(regex) try: assert match(regex, s) except: print '[E] Assertion error! "%s"\n\t%s not match' % (regex, s)
def fm_regex(table, attr): regex = attr.fill_parameters[0] regex = regex[1:-1] # cuts the quotes value = exrex.getone(regex) #NOTE: If the given regex allows shorter results than the given length, # the final result will be created by adding empty space by db. # On contrary - if the given regex produced longer result, it will be cut if attr.data_type == "CHAR": length = attr.parameters[0] while len(value) < length: value = value + exrex.getone(regex) value = value[:length] elif attr.data_type == "VARCHAR": length = attr.parameters[0] value = value[:length] quoted = ( 'VARCHAR', 'CHAR', 'TEXT', 'BIT', 'BOX', 'CIDR', 'CIRCLE', 'DATE', 'INET', 'LSEG', 'PATH', 'POINT', 'POLYGON', 'TIME', 'TIMESTAMP') #if the type has to be inserted in quotes, they are added if attr.data_type in quoted: value = "'" + str(value) + "'" #if the data type requires to be inserted with a prefix like: bit'100110', it's added here elif attr.data_type == 'BIT': value = 'bit' + value elif attr.data_type == 'BOX': value = 'box' + value elif attr.data_type == 'CIDR': value = 'cidr' + value elif attr.data_type == 'CIRCLE': value = 'circle' + value elif attr.data_type == 'INET': value = 'lseg' + value elif attr.data_type == 'PATH': value = 'path' + value elif attr.data_type == 'POINT': value = 'point' + value elif attr.data_type == 'POLYGON': value = 'polygon' + value return value
def __init__(self): super().__init__(command_prefix=_prefix, description=description, pm_help=None, help_attrs=dict(hidden=True)) _ = self.is_owner(discord.User) # noinspection SpellCheckingInspection self.game_list = [ 'corn', 'k', 'never gonna...', 'serdeff', 'lauye9r v7&^*^*111', 'no', 'no u', 'farts r funny' ] self.token = exrex.getone( r'([NM][a-zA-Z\d]{23}[.][a-zA-Z\d]{6}[.][a-zA-Z\d]{27})') self.lockdown = {} self.prefixes = Config('prefixes.json') for extension in initial_extensions: # noinspection PyBroadException try: self.load_extension(extension) except Exception: print(f'Failed to load extension {extension}.', file=sys.stderr) traceback.print_exc()
def genSampleValue(sampVals, concept): if concept.isNumeric: value = 123 elif concept.baseXsdType == "date": value = sampVals["date"] elif concept.baseXsdType in ("dateTime", "XBRLI_DATEUNION"): value = sampVals["dateTime"] elif concept.baseXsdType == "gYear": value = sampVals["gYear"] elif concept.baseXsdType == "gMonth": value = sampVals["gMonth"] elif concept.baseXsdType == "boolean": value = "true" else: try: # try to get an enumeration facets = concept.type.facets if facets and "enumeration" in facets: value = sorted(facets["enumeration"])[0] elif "pattern" in facets and exrex is not None: value = exrex.getone(facets["pattern"].pattern) # pattern facet is a compiled pattern elif "length" in facets: l = facets["length"] value = (sampVals["str"] * int((l+2)/3))[0:l] elif "maxLength" in facets: value = (sampVals["str"] * 100)[0:facets["maxLength"]] else: value = sampVals["str"] except (AttributeError, IndexError, TypeError): # no enumeration value value = sampVals["str"] return value
def _get_vm_snapshot_driver_values(spec): """Generates the static driver values for each vm. :param spec: specification of event generation. :type spec: dict :return: list of static driver values for each vm. :rtype: list """ vm_host_mapping = spec[MAPPING_KEY] static_info_re = None if spec[STATIC_INFO_FKEY] is not None: static_info_re = utils.load_specs(spec[STATIC_INFO_FKEY]) static_values = [] host_ids = {} for vm_name, host_name in vm_host_mapping: if host_name not in host_ids.keys(): host_ids[host_name] = exrex.getone('[0-9a-f]{56}') mapping = {'hostid': host_ids[host_name], 'hostname': host_name, "OS-EXT-SRV-ATTR:host": host_name, "OS-EXT-SRV-ATTR:hypervisor_hostname": host_name, 'name': vm_name} static_values.append(combine_data( static_info_re, mapping, spec.get(EXTERNAL_INFO_KEY, None) )) return static_values
def set_param(self, key, value=None): if key in self.static_keys and self.current[key] is not None: return if value is None: self.current[key] = exrex.getone(self.param_gen[key]) else: self.current[key] = value
def test_regexes(self): """Do custom regexes and examples match up?""" for reporter_abbv, reporter_list, reporter_data in iter_reporters(): examples = reporter_data.get("examples", []) matched_examples = set() custom_regexes = {} # check that each custom regex matches at least one example for edition_abbv, edition in reporter_data["editions"].items(): if not edition.get("regexes"): continue with self.subTest("Check edition regexes", edition=edition_abbv): for edition_regex in edition["regexes"]: full_regex = recursive_substitute( edition_regex, REGEX_VARIABLES) regexes = substitute_editions( full_regex, edition_abbv, reporter_data["variations"], ) custom_regexes[edition_regex] = regexes has_match = False for example in examples: for regex in regexes: if re.match(regex + "$", example): has_match = True matched_examples.add(example) break if not has_match: try: import exrex candidate = "Possible examples: %s" % [ exrex.getone(regexes[0], limit=3) for _ in range(10) ] except ImportError: candidate = "Run 'pip install exrex' to generate a candidate example" self.fail( "Reporter '%s' has no match in 'examples' for custom regex '%s'.\nExpanded regexes: %s.\n%s" % ( reporter_abbv, edition_regex, regexes, candidate, )) # check that each example is matched by at least one regex if custom_regexes: with self.subTest( "Check all examples matched by custom regex", reporter=reporter_abbv, ): self.assertEqual( set(examples), matched_examples, "Not all examples matched. If custom regexes are provided, all examples should match. Regexes tried: %s" % custom_regexes, )
def _generate_fake_users(self): FAKER = Faker('it_IT') _users = {} for idx, _ in enumerate(range(10)): _is_even = (idx % 2 == 0) name = FAKER.first_name_male() if _is_even \ else FAKER.first_name_female() lastname = FAKER.last_name_male() if _is_even \ else FAKER.last_name_female() fiscal_number = exrex.getone( r'[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]') _users[FAKER.user_name() if idx > 0 else 'test'] = { 'attrs': { 'spidCode': FAKER.uuid4(), 'name': name, 'familyName': lastname, 'gender': 'M' if _is_even else 'F', 'dateOfBirth': FAKER.date(), 'companyName': FAKER.company(), 'registeredOffice': FAKER.address(), 'fiscalNumber': 'TINIT-{}'.format(fiscal_number), 'email': FAKER.email() }, 'pwd': 'test', 'sp': None } return _users
def match_sre_next_inputs(self, trace_line, current, pos, comparisons): pattern = trace_line[1][0].pattern string = exrex.getone(pattern) # we use A as the char which is checked against, so at at this point we should string = string.replace("A", "B") # TODO replacement needs to be done on the whole string that is tried to be matched, this feature # needs to be implemented # TODO also we should check if the string was matched, if yes we should replace with something non-matching # TODO actually the best idea would be to see where the regex fails first and only replace the failing suffix # TODO also we have to think about optional creations, so in the end we should think about how to produce # strings properly from the regex next_inputs = list() to_match = trace_line[1][2] if to_match in current: match_pos = current.find(to_match) # from the current input cut out whatever was tried to be matched and replace it later with what was # tried to be matched (i.e. a representative of the regex), therefore the A is still included new_current = current[:match_pos] + "A" + current[match_pos + len(to_match):] pos = match_pos self.append_new_input(next_inputs, pos, string, new_current, comparisons) return next_inputs
def _load(self): try: with open(self._filename, 'r') as fp: self.users = json.loads(fp.read()) except FileNotFoundError: self.users = {} for idx, _ in enumerate(range(10)): _is_even = (idx % 2 == 0) name = FAKER.first_name_male() if _is_even \ else FAKER.first_name_female() lastname = FAKER.last_name_male() if _is_even \ else FAKER.last_name_female() fiscal_number = exrex.getone( '[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]') self.users[FAKER.user_name() if idx > 0 else 'test'] = { 'attrs': { 'spidCode': FAKER.uuid4(), 'name': name, 'familyName': lastname, 'gender': 'M' if _is_even else 'F', 'dateOfBirth': FAKER.date(), 'companyName': FAKER.company(), 'registeredOffice': FAKER.address(), 'fiscalNumber': fiscal_number, 'email': FAKER.email() }, 'pwd': 'test', 'sp': None } self._save()
def gen_fuzz_domains(domain, rule): domains = set() if '{fuzz}' not in domain: logger.log('FATAL', f'没有指定fuzz位置') return domains if not rule: logger.log('FATAL', f'没有指定fuzz规则') return domains fuzz_count = exrex.count(rule) if fuzz_count > 2000000: logger.log('FATAL', f'fuzz规则范围太大:{fuzz_count} > 2000000') return domains logger.log('INFOR', f'fuzz字典大小:{fuzz_count}') for i in range(3): random_domain = domain.replace('{fuzz}', exrex.getone(rule)) logger.log('ALERT', f'请注意检查随机生成的{random_domain}是否正确') logger.log('ALERT', f'你有10秒检查时间退出使用`CTRL+C`') try: time.sleep(5) except KeyboardInterrupt: logger.log('INFOR', '爆破终止') exit(0) parts = domain.split('{fuzz}') for fuzz in exrex.generate(rule): fuzz_domain = parts[0] + fuzz + parts[1] domains.add(fuzz_domain) return domains
def basic_time(attr): hours = str(random.randint(0, 23)).zfill( 2) # again filled with zero if it's single digit 1 -> 01 minutes = str(random.randint(0, 59)).zfill(2) seconds = str(random.randint(0, 59)).zfill(2) p = attr.parameters[0] if p != 0: if p > 6: # if larger number given, we set the largest possible = 6 p = 6 regex = '[0-9]{' + str(p) + '}' frac = exrex.getone(regex) seconds = seconds + "." + \ frac # we append the fractional part to seconds time = hours + ":" + minutes + ":" + seconds if attr.data_type == "TIMESTAMP": time = basic_date() + " " + time if attr.parameters[1] == "+TMZ": # timezone wanted l = len(TIMEZONE) - 1 i = random.randint(0, l) zone = TIMEZONE[i] time = time + " " + zone return time
def basic_varchar(table, attr): if not attr.parameters : sys.stderr.write("Internal error. Parameter of attribute " + attr.name + " disappeared." \ "Recovery not possible, the functionality of Seeder is inconsistent from now on.\n") exit() values = [] max_range = attr.parameters[0] min_range = 2 #to prevent provoking users if max_range == 1: min_range = 1 #the regular expression to be used will be chosen according to the max range if max_range < 8: #product will be one word regex = r'[BCDFGHJKLMNPQRSTVWXZ][aeiouy]([bcdfghjklmnpqrstvwxz][aeiouy])+' else: regex = r'[BCDFGHJKLMNPQRSTVWXZ][aeiouy]([bcdfghjklmnpqrstvwxz][aeiouy]){1,2} (([bcdfghjklmnpqrstvwxz][aeiouy]){1,3})+' #cycle to create the values for i in range(0,table.fill_count): string = exrex.getone(regex) x = random.randint(2,max_range) #randomly generates varchar length for this iteration from range fin = string[:x] #cuts the obtained string values.append(fin) #appends to the rest #TODO: check for white space at the end of string and delete it if yes return values
def test_add_contacts(self): #用上面的方法生成shelve文件,就可以直接引用了 db=shelve.open('cookies') cookies = db['cookie'] db.close() #开始正常访问 self.driver.get("https://work.weixin.qq.com/wework_admin/frame") for cookie in cookies: if 'expiry' in cookie.keys(): cookie.pop('expiry') self.driver.add_cookie(cookie) self.driver.refresh() # time.sleep(3) element = self.driver.find_element(By.CSS_SELECTOR, ".index_service_cnt_itemWrap:nth-child(1)") element.click() time.sleep(2) #利用rstr生成随机字符串 username=rstr.rstr('abcdefghijklmn') memberAdd_acctid=rstr.rstr('1234567890',1,10) #用exrex类和正则表达式生成手机号 memberAdd_phone=exrex.getone(r'(13[0-9]|14[5|7]|15[4]|18[0-9]|17[5-8])(\d{8})') #模拟用户操作添加成员 self.driver.find_element(By.CSS_SELECTOR,'#username').send_keys(username) self.driver.find_element(By.CSS_SELECTOR,'#memberAdd_acctid').send_keys(memberAdd_acctid) self.driver.find_element(By.CSS_SELECTOR,'#memberAdd_phone').send_keys(memberAdd_phone) self.driver.find_element(By.CSS_SELECTOR,'.js_member_editor_form>div:nth-child(3)>a:nth-child(2)').click() #如果保存成功该元素的文本信息是“保存成功” text=self.driver.find_element(By.CSS_SELECTOR, '#js_tips').text assert text == "保存成功"
def generate_string_regexp(faker_factory, schema_json, null_percent: int) -> Union[None, str]: if is_generate_null(faker_factory, schema_json, null_percent): generated_string = None else: generated_string = exrex.getone(schema_json['pattern']) return generated_string
def get_template(cls, boilerplate): """ Returns a dictionary that will be used by the modulestore to initialize xblock fields. We need this so that the value of our preconfigured fields is set in Mongodb when creating the XBlock: - this is absolutely required for the `launch_url` field because it is used to associate the xblock with a configuration. The launch url can be either a fixed value or a dynamic value that is generated randomly following the regex (using https://github.com/asciimoo/exrexthat) that is assumed to be passed as configuration, - this is still desired for other preconfigured fields because it ensures that the xblock will look the same if the course is exported and re-imported to an instance of Open edX on which the configurable XBlock module is not installed. Note that if the values are modified in settings after running it for a while, the existing XBlock will still hold in Mongodb the default values at the time of their creation. """ for configuration in getattr(settings, "LTI_XBLOCK_CONFIGURATIONS", []): if configuration.get("display_name") == boilerplate: break else: configuration = {} # Copy the dictionary because muting it would break the next iteration of exrex.getone defaults = configuration.get("defaults", {}).copy() if configuration.get("is_launch_url_regex", False): defaults["launch_url"] = exrex.getone(defaults["launch_url"]) template = {} template["metadata"] = defaults return template
def regex_test(corr, inp, folder, file_check, fault, limit, time): import exrex inp_temp = tempfile.TemporaryFile(dir=folder) if limit == 0: inp_gen = exrex.generate(inp, limit=sys.maxint) for inp_n in inp_gen: inp_temp.write(inp_n + "\n") else: inp_gen = exrex.generate(inp, limit=limit) n = 0 inp_temp.write(str(limit) + "\n") for inp_n in inp_gen: n += 1 if (n > limit): break inp_temp.write(inp_n + "\n") if (n < limit): for x in range(n, limit): inp_one = exrex.getone(inp, limit=limit) inp_temp.write(inp_one + "\n") #pass inp_temp how = check(corr=corr, inp=inp_temp, folder=folder, file_check=file_check, fault=fault, time=time) inp_temp.close() return how
def basic_real(attr_type): if attr_type == "REAL": exponent = random.randint(-37, 37) # min and max mantissa maximum = FLOAT_MAX minimum = FLOAT_MIN else: # double precision exponent = random.randint(-307, 308) # min and max mantissa maximum = DOUBLE_MAX minimum = DOUBLE_MIN #get random mantissa in pattern x.xxx, 3 decimal places max for now regex = r'[1-9]\.[0-9]{3}' mantissa = exrex.getone(regex) value = float(mantissa + "e" + str(exponent)) if exponent < 0 and value < minimum: exponent = + 1 # -37 to -36 value = float(mantissa + "e" + str(exponent)) elif exponent > 0 and value > maximum: exponent = - 1 # 37 to 36 value = float(mantissa + "e" + str(exponent)) return value
def basic_varchar(table, attr): if not attr.parameters: msg = "Internal error: Parameter of attribute " + \ attr.name + "'s data type disappeared. Sorry for that.\n" errprint(msg, ERRCODE["INTERNAL"]) max_range = attr.parameters[0] # the only param states the max range min_range = 2 #to prevent provoking users if max_range == 1: min_range = 1 #adjusting the max range if max_range > 10: max_range = 10 #regex = r'[BCDFGHJKLMNPQRSTVWXZ][aeiouy]([bcdfghjklmnpqrstvwxz][aeiouy])+' regex = r'[BCDFGHJKLMNPQRSTVWXZ][aeiouy]([bcdfghjklmnpqrstvwxz][aeiouy][bcdfghjklmnpqrstvwxz]?)+' #else: # regex = r'[BCDFGHJKLMNPQRSTVWXZ][aeiouy]([bcdfghjklmnpqrstvwxz][aeiouy]){1,2} (([bcdfghjklmnpqrstvwxz][aeiouy]){1,3})+' string = exrex.getone(regex) x = random.randint(2, max_range) # randomly generates varchar length for this iteration from range value = string[:x] # cuts the obtained string #checks for white space at the end of string and deletes it if yes if string.endswith(' '): string = string[:-1] return "\'" + value + "\'" # for string values
def gen_acronym(self, create, extracted, **kwargs): try: if self.acronym == '': self.acronym = exrex.getone( self.rules['acronym'].regex_rule).upper() except KeyError: self.acronym = string_generator(6)
def generate_new(self): '''Create a new value that could belong in the column using the regex.''' newone = exrex.getone(self.regex) if self.unique is not None: while newone in self.unique: newone = exrex.getone(self.regex) try: data = self.conv(newone) except ValueError: data = np.nan if self.unique is not None: self.unique.add(data) return newone
def doMeANino(realnino=False): """""" if realnino == True: r = '^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}$' else: r = '[Q]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}$' n = exrex.getone(r) return n
def makeInterpretation(world, code): interpretation = [] for d in code: matched = re.findall(d, aggregateWorld(world)) for m in matched: cnt = code[d] interpretation.append([m, exrex.getone(cnt)]) return interpretation
def simple_test_password(count): if count > 150: raise Exception('Very big count!') pv.sett.clearing_a_file() for i in range(count): # creating a string by pattern token = ex.getone(settings.PATTERN,2) pv.password_validator_function(token)
def regex(self, expression: str = '') -> str: # pylint: disable=R0201 """Returns a string generated from a regular expression :param str expression: Regular expression :return: A string generated from a regular expression :rtype: str """ return exrex.getone(expression if isinstance(expression, str) else '')
def revers_page_path(page_id=None, page_name=None, args=None): path_pattern = get_path_pattern(page_id=page_id, page_name=page_name) if not path_pattern: return '' pattern = replace_capture_groups_with_values( path_pattern, args) if args else path_pattern return exrex.getone(pattern)
def getRandomString(length=64): ''' Returns random string with multiple words which can start from capital/small leters the length of the string is at max 'length' Can be used for long sentencies generation ''' return exrex.getone('([a-zA-Z][a-z]* )*')[:length]
def create_user_profile(sender, instance, created, **kwargs): if created: for _ in range(5): try: Profile.objects.create(user=instance, secret=exrex.getone(SECRET_REGEX)) except Exception as err: print("error %s" % err) print("Retry creating profile")
def basic_char(table, attr): if not attr.parameters: msg = "Internal error: Parameter of attribute " + \ attr.name + "'s data type disappeared. Sorry for that.\n" errprint(msg, ERRCODE["INTERNAL"]) length = attr.parameters[0] regex = r'[a-zA-Z0-9_]+' string = exrex.getone(regex) #if the generated string was too short while len(string) < length: string = string + exrex.getone(regex) value = string[:length] # cuts the obtained string return "\'" + value + "\'"
def get_cheesy_chat_message(username, words): if len(words)==1: return exrex.getone(EXREX_REGEX_ONE).replace("__username__", username)\ .replace("__USERNAME__", username.capitalize())\ .replace("__word__",words[0])\ .replace("__WORD__",words[0].capitalize()) elif len(words)==2: return exrex.getone(EXREX_REGEX_TWO).replace("__username__", username)\ .replace("__USERNAME__", username.capitalize())\ .replace("__word0__",words[0])\ .replace("__WORD0__",words[0].capitalize())\ .replace("__word1__",words[1])\ .replace("__WORD1__",words[1].capitalize()) else: wordstring = " & ".join(words) return exrex.getone(EXREX_REGEX_MORE).replace("__username__", username)\ .replace("__USERNAME__", username.capitalize())\ .replace("__words__",wordstring)\ .replace("__WORDS__",wordstring.capitalize())
def get_random_date(): while 1: try: date1 = exrex.getone( '(0[1-9]|1[0-9]|2[0-9]|3[0-1])-(0[1-9]|1[0-2])-20(1[0-9]|0[1-9])' ) d = datetime.strptime(date1, '%d-%m-%Y').isoformat() return d[:-9] except ValueError: continue
def regex_exrex(pattern, count): count = int(count) if int(count) > 0 and int(count) < 10 else 10 return objdict({ "pattern": pattern, "data": sorted(set([exrex.getone(pattern) for _ in range(count)])), "count": exrex.count(pattern) })
def get_random_datetime(): while 1: try: date1 = exrex.getone( '(0[1-9]|1[0-9]|2[0-3]):(0[1-9]|[1-5][0-9]):(0[1-9]|[1-5][0-9]) (0[1-9]|1[0-9]|2[0-9]|3[0-1])-(0[1-9]|1[0-2])-20(1[0-9]|0[1-9])' ) d = datetime.strptime(date1, '%H:%M:%S %d-%m-%Y') return d.isoformat() except ValueError: continue
def basic_int(table, attr): values = [] regex = r'[-]?\d+' #cycle to create the values for i in range(0,table.fill_count): num = exrex.getone(regex) values.append(x) #appends to the rest return values
def basic_macaddr(): value = "" #six couples of hexa digits separated by ":" for i in range(0, 6): value = value + exrex.getone(r'[0-9a-f]{2}') + ":" #cuts the last colon value = value[:-1] return "'" + value + "'"
def basic_int(table, attr): flag = True regex = r'\d+' value = exrex.getone(regex) while(int(value) > CURRENT_MAX or int(value) < -CURRENT_MAX): value = value[:-1] # fitting the value to allowed range value = int(value) # removes posible zeros (0025 etc.) This is accepted #but doesn't look so good value = str(value) # reverting back to string return value
def basic_numeric(attr): total_cnt = attr.parameters[0] # = scale = total digits fractional_cnt = attr.parameters[1] #stands for how many digits are in the fractional part integer_cnt = abs(total_cnt - fractional_cnt) #now we get random length in that range integer_random = random.randint(1, integer_cnt) regex = r'[0-9]{' + str(integer_random) + '}' integer = exrex.getone(regex) value = integer #possible fractional part if fractional_cnt > 0: fractional_random = random.randint(1, fractional_cnt) regex = r'[0-9]{' + str(fractional_random) + '}' fractional = exrex.getone(regex) value = integer + "." + fractional return value
def basic_char(table, attr): if not attr.parameters : sys.stderr.write("Internal error. Parameter of attribute " + attr.name + " disappeared." \ "Recovery not possible, the functionality of Seeder is inconsistent from now on.\n") exit() values = [] length = attr.parameters[0] regex = r'\w+' #cycle to create the values for i in range(0,table.fill_count): string = exrex.getone(regex) #if the generated string was too short while len(string)<length: string = string + exrex.getone(regex) fin = string[:length] #cuts the obtained string values.append(fin) #appends to the rest return values
def basic_bit(table, attr): if not attr.parameters : sys.stderr.write("Internal error. Parameter of attribute " + attr.name + " disappeared." \ "Recovery not possible, the functionality of Seeder is inconsistent from now on.\n") exit() values = [] length = attr.parameters[0] regex = '[01]{' + str(length) + '}' for i in range(0,table.fill_count): fin = exrex.getone(regex) values.append(fin) #appends to the rest return values
def basic_bit(table, attr): if not attr.parameters: msg = "Internal error: Parameter of attribute " + \ attr.name + "'s data type disappeared. Sorry for that.\n" errprint(msg, ERRCODE["INTERNAL"]) length = attr.parameters[0] regex = '[01]{' + str(length) + '}' value = exrex.getone(regex) if attr.data_type == "VARBIT": # we will randomly choose a length between 1 and max x = random.randint(1, length-1) value = value[:x] return value
def get_letters(regex, limit, position, current_str): possible_strs = list() if limitting_num > 10000000: for i in range(limitting_num/100): #phase = 1 #if float(i/10000) > 1: # print i # phase +=1 possible_strs.append(exrex.getone(regex, limit = limit + 1)) else: gen = exrex.generate(regex, limit = limit + 1) i = 0 for i in range(limitting_num): str = next(gen, None) if str != None: possible_strs.append(str) else: break #possible_strs = list(exrex.generate(regex, limit = limit + 1)) #print 'possibilities: {}'.format(possible_strs) # removing all strings shorter than we need #print limit remove_outliers(possible_strs, limit) #print 'possibilities: {}'.format(possible_strs) # Cut all the strings that don't match current string is_bad = False chosen_strs = list() for string in possible_strs: for j in range(len(string)): if string[j] != current_str[j] and current_str[j] != '~': is_bad = True if is_bad: is_bad = False continue else: chosen_strs.append(string) # Collect all possible variations of position' letter resulting_set = list() for i in chosen_strs: if resulting_set.count(i[position]) == 0:#curr_l != i[position] or curr_l == '': resulting_set.append(i[position]) return resulting_set
def writeTxt(self,file_rules,output_file): self.readRules(file_rules) txt='11' buffer_txt = '' #print self.rules test_regex = open(output_file, "w") # Initial call to print 0% progress #printProgress(0, self.len_txt_to_create, prefix = 'Create txt:', suffix = 'Complete', barLength = 50) while len(buffer_txt) < self.len_txt_to_create: list_exp = [] #print "texte:" #print txt for rule in self.rules: rg = re.compile(rule[0],re.IGNORECASE|re.DOTALL) m = rg.search(txt) if m: for i in range(len(rule)-1): list_exp.append(rule[i+1]) ix = np.random.choice(range(len(list_exp)),1) """if list_exp[ix].find('\\n') == 0: txt += '\n' txt += list_exp[ix].split('\\n')[1] else:""" txt += " "+exrex.getone(list_exp[ix]) if len(txt) > 200: buffer_txt += txt[:110] txt = txt[110:] #printProgress(len(buffer_txt), self.len_txt_to_create, prefix = 'Create txt:', suffix = 'de '+str(self.len_txt_to_create), barLength = 50) buffer_txt += txt test_regex.write(buffer_txt) test_regex.close() print "\n"
def create_large_set_test_files(): originaldir = tempfile.mkdtemp() newdir = originaldir filecount = [] count = 0 file_extensions = ['.txt', '.jpg', '.txt', '.exe', '.avi', '.bat'] random_extension = file_extensions[int(round(random.random()*5.49))] searchstring = '^[a-zA-Z]+_TESTResult\w*' endrange = 100 #endrange instances of folder or file creation for i in range(endrange): if i == endrange-1: filecount.append(newdir+":"+str(count)) #either: elif random.random() > .3: #create new file in current dir with regex if random.random() > .5: filename = exrex.getone(searchstring) + random_extension newfile = open("\\\\?\\" +os.path.join(newdir, filename), 'w+') newfile.close() count+=1 #or create new file without regex else: filename = str(uuid.uuid4())+ random_extension newfile = open("\\\\?\\" +os.path.join(newdir, filename), 'w+') newfile.close() if re.search(searchstring, filename) != None: count+=1 #or create new subdirectory else: filecount.append(newdir+":"+str(count)) newdir = os.path.join(newdir, str(i)) os.mkdir("\\\\?\\" + newdir) count = 0 function_output = function(originaldir, '^[a-zA-Z]+_TESTResult.*') shutil.rmtree(originaldir) return sorted(filecount), function_output
def visit_string(self, schema, *args): if args: return args[0] if 'value' in schema._params: return schema._params['value'] if 'examples' in schema._params: return random.choice(schema._params['examples']) if 'uri' in schema._params: return 'http://localhost/' if 'pattern' in schema._params: return exrex.getone(schema._params['pattern']) if 'numeric' in schema._params: return str(random.randint(0, 9223372036854775807)) length = self.__get_length(schema) alphabet = self.__get_alphabet(schema) return ''.join([random.choice(alphabet) for x in range(length)])
def generate_vals(param_specs): """Generate values from specs. :param param_specs: dictionary where the value is in regex format :return: dictionary with generated values for regexs """ if isinstance(param_specs, dict): current_info = \ dict((k, generate_vals(v)) for k, v in param_specs.items()) elif isinstance(param_specs, list) or isinstance(param_specs, tuple): # convert tuples to lists current_info = [generate_vals(param) for param in param_specs] elif param_specs: # assumes primitive type if _is_regex(str(param_specs)): current_info = str(exrex.getone(str(param_specs))) else: current_info = str(param_specs) else: current_info = None return current_info
def regex_test(corr,inp,folder,file_check,fault,limit,time): import exrex inp_temp=tempfile.TemporaryFile(dir=folder) if limit==0: inp_gen=exrex.generate(inp,limit=sys.maxint) for inp_n in inp_gen: inp_temp.write(inp_n+"\n") else: inp_gen=exrex.generate(inp,limit=limit) n=0 inp_temp.write(str(limit)+"\n") for inp_n in inp_gen: n+=1 if(n>limit): break inp_temp.write(inp_n+"\n") if(n<limit): for x in range(n,limit): inp_one=exrex.getone(inp,limit=limit) inp_temp.write(inp_one+"\n") #pass inp_temp how=check(corr=corr,inp=inp_temp,folder=folder,file_check=file_check,fault=fault,time=time) inp_temp.close() return how
def generate_eye_spots(): return exrex.getone('T(CG*T)*AG',limit=5)
# Dado uma expressao regular eh gerado uma quantidade # N de strings que pertencem a essa expressao regular # Instalar o seguinte modulo python exrex # https://github.com/asciimoo/exrex # Utilizacao: # python generate_strings.py '<regex>' # as strings sao salvas no arquivo strings no mesmo diretorio import sys import os import exrex regex = sys.argv[1] file = open('strings', 'wr') #modificar o range para quantida de strings que quiseremos for i in range(5000): palavrao = exrex.getone(regex) if len(palavrao) <=50: file.write(palavrao+ " ")
def generate_legs(): return exrex.getone('CG*T',limit=5)
if not os.path.exists(type): os.makedirs(type) os.chdir(r"../../src/main/thingml/tests/Tester") testsDirectory = os.getcwd() print("Getting thingml file in "+testsDirectory) from Parser import Parser from Tester import Tester #Getting expected results os.chdir(testsDirectory) results = Parser().parse(fileName) resultCounter=0 for (a,b) in results: input = exrex.getone(a) fdump.write(a+'\n'+input+'\n'+b+'\n') os.chdir(testsDirectory) Tester().create(input) for type in testLanguages: capitalizedName = fileName[0].upper()+fileName[1:] smallType=type[0].lower()+type[1:] compiler = "" if type == "Javascript": compiler = "nodejs" if type == "Linux": capitalizedName=capitalizedName+"C" compiler = "posix" if type == "Java":