On Wed, Jul 16, 2014 at 11:49:37PM +0400, Maria Kustova wrote: > +def string_validator(current, strings): > + """Return a random string value from the list not equal to the current. > + > + This function is useful for selection from valid values except current > one. > + """ > + val = random.choice(strings) > + if val == current: > + return string_validator(current, strings) > + else: > + return val
There is a pattern to these validator functions: def validator(current, pick, choices): while True: val = pick(choices) if val != current: return val int_validator is validator(_, random_from_intervals, _) bit_validator is validator(_, random_bits, _) string_validator is validator(_, random.choice, _) The code duplication in int_validator, bit_validator, and string_validator can be eliminated. > + > + > +def selector(current, constraints, fmt=None): > + """Select one value from all defined by constraints > + > + Each constraint produces one random value satisfying to it. The function > + randomly selects one value satisfying at least one constraint (depending > on > + constraints overlaps). > + """ > + validate = { > + 'bitmask': bit_validator, > + 'string': string_validator > + }.get(fmt, int_validator) Is the indirection through a string necessary? def selector(current, constraints, fmt=int_validator): Callers can pass bit_validator or string_validator if they want. It makes the code a little simpler and it stays readable since string_validator's name tells you what it does.
pgp4eiix6q5mq.pgp
Description: PGP signature