Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate string data from regex

I would like to be able to take a regex and generate conforming data using the python hypothesis library. For example given a regex of

regex = re.compile('[a-zA-Z]')

This would match any english alpha characters. An example generator for this could be.

import hypothesis
import string

hypothesis.strategies.text(alphabet=string.ascii_letters)

But Ideally I want to construct a string that will match any regex passed in.

like image 945
Mark Lakewood Avatar asked Oct 15 '25 13:10

Mark Lakewood


1 Answers

There's a work in progress pull request for adding this feature. Nothing extant will let you do it easily, but looking at the PR might give you a good idea about how to translate any specific example you need.

Update: the from_regex strategy was added in Hypothesis 3.19.

like image 103
DRMacIver Avatar answered Oct 18 '25 19:10

DRMacIver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!