<s> import string <EOL> import random <EOL> import collections <EOL> import six <EOL> from mongo_dynamic_fixture . exceptions import NotGeneratedException <EOL> class BaseField ( object ) : <EOL> blank_value = '<STR_LIT>' <EOL> def __init__ ( self , required = True , null = False , blank = False , <EOL> not_present_prob = <NUM_LIT:0> , null_prob = <NUM_LIT:0> , blank_prob = <NUM_LIT:0> ) : <EOL> self . _required = required <EOL> self . _null = null <EOL> self . _blank = blank <EOL> self . _not_present_prob = not_present_prob <EOL> self . _null_prob = null_prob <EOL> self . _blank_prob = blank_prob <EOL> @ property <EOL> def required ( self ) : <EOL> return self . _required <EOL> @ property <EOL> def null ( self ) : <EOL> return self . _null <EOL> @ property <EOL> def blank ( self ) : <EOL> return self . _blank <EOL> @ property <EOL> def not_present_prob ( self ) : <EOL> return self . _not_present_prob <EOL> @ property <EOL> def null_prob ( self ) : <EOL> return self . _null_prob <EOL> @ property <EOL> def blank_prob ( self ) : <EOL> return self . _blank_prob <EOL> def generate ( self ) : <EOL> r1 = random . random ( ) <EOL> if not self . required and r1 < self . not_present_prob : <EOL> raise NotGeneratedException <EOL> r2 = random . random ( ) <EOL> if self . null and r2 < self . null_prob : <EOL> value = None <EOL> elif self . blank and r2 < self . null_prob + self . blank_prob : <EOL> value = self . blank_value <EOL> else : <EOL> value = self . _generate_value ( ) <EOL> return value <EOL> def generate_value ( self ) : <EOL> raise NotImplementedError <EOL> def _generate_value ( self ) : <EOL> return self . generate_value ( ) <EOL> class ChoosableBaseField ( BaseField ) : <EOL> def __init__ ( self , choices = None , ** kwargs ) : <EOL> super ( ChoosableBaseField , self ) . __init__ ( ** kwargs ) <EOL> self . _choices = choices <EOL> @ property <EOL> def choices ( self ) : <EOL> return self . _choices <EOL> def _generate_value ( self ) : <EOL> if self . choices is not None : <EOL> value = random . choice ( self . choices ) <EOL> else : <EOL> value = self . generate_value ( ) <EOL> return value <EOL> class NumericalField ( ChoosableBaseField ) : <EOL> def __init__ ( self , min_value , max_value , ** kwargs ) : <EOL> super ( NumericalField , self ) . __init__ ( ** kwargs ) <EOL> self . _min_value = min_value <EOL> self . _max_value = max_value <EOL> @ property <EOL> def min_value ( self ) : <EOL> return self . _min_value <EOL> @ property <EOL> def max_value ( self ) : <EOL> return self . _max_value <EOL> class IntegerField ( NumericalField ) : <EOL> blank_value = <NUM_LIT:0> <EOL> def __init__ ( self , min_value = <NUM_LIT:0> , max_value = <NUM_LIT:100> , ** kwargs ) : <EOL> super ( IntegerField , self ) . __init__ ( min_value , max_value , ** kwargs ) <EOL> def generate_value ( self ) : <EOL> return random . randint ( self . min_value , self . max_value ) <EOL> class DoubleField ( NumericalField ) : <EOL> blank_value = <NUM_LIT:0.0> <EOL> def __init__ ( self , min_value = <NUM_LIT:0.0> , max_value = <NUM_LIT:1.0> , ** kwargs ) : <EOL> super ( DoubleField , self ) . __init__ ( min_value , max_value , ** kwargs ) <EOL> def generate_value ( self ) : <EOL> return random . uniform ( self . min_value , self . max_value ) <EOL> class BooleanField ( BaseField ) : <EOL> blank_value = False <EOL> def generate_value ( self ) : <EOL> return random . choice ( [ True , False ] ) <EOL> class StringField ( ChoosableBaseField ) : <EOL> blank_value = '<STR_LIT>' <EOL> def __init__ ( self , min_length = <NUM_LIT:1> , max_length = <NUM_LIT:10> , charset = None , ** kwargs ) : <EOL> super ( StringField , self ) . __init__ ( ** kwargs ) <EOL> self . _min_length = min_length <EOL> self . _max_length = max_length <EOL> self . _charset = charset or ( string . ascii_letters + string . digits ) <EOL> @ property <EOL> def min_length ( self ) : <EOL> return self . _min_length <EOL> @ property <EOL> def max_length ( self ) : <EOL> return self . _max_length <EOL> @ property <EOL> def charset ( self ) : <EOL> return self . _charset <EOL> def generate_value ( self ) : <EOL> return '<STR_LIT>' . join ( [ <EOL> random . choice ( self . charset ) for _ in range ( random . randint ( <EOL> self . min_length , self . max_length ) ) ] ) <EOL> class ArrayField ( BaseField ) : <EOL> blank_value = [ ] <EOL> def __init__ ( self , content_fields , min_length = <NUM_LIT:1> , max_length = <NUM_LIT:10> , ** kwargs ) : <EOL> super ( ArrayField , self ) . __init__ ( ** kwargs ) <EOL> self . _min_length = min_length <EOL> self . _max_length = max_length <EOL> if not isinstance ( content_fields , collections . Iterable ) : <EOL> content_fields = [ content_fields ] <EOL> self . _content_fields = content_fields <EOL> @ property <EOL> def min_length ( self ) : <EOL> return self . _min_length <EOL> @ property <EOL> def max_length ( self ) : <EOL> return self . _max_length <EOL> @ property <EOL> def content_fields ( self ) : <EOL> return self . _content_fields <EOL> def generate_value ( self ) : <EOL> return [ random . choice ( self . content_fields ) . generate ( ) for _ in range ( <EOL> random . randint ( self . min_length , self . max_length ) ) ] <EOL> class ObjectField ( BaseField ) : <EOL> blank_value = { } <EOL> def __init__ ( self , schema , ** kwargs ) : <EOL> super ( ObjectField , self ) . __init__ ( ** kwargs ) <EOL> self . _schema = schema <EOL> @ property <EOL> def schema ( self ) : <EOL> return self . _schema <EOL> def generate_value ( self ) : <EOL> def _generate_value ( schema ) : <EOL> generated = { } <EOL> for k , v in six . iteritems ( schema ) : <EOL> if isinstance ( v , BaseField ) : <EOL> generated [ k ] = v . generate ( ) <EOL> else : <EOL> sub_generated = { } <EOL> for sub_k , sub_v in six . iteritems ( v ) : <EOL> sub_generated [ sub_k ] = _generate_value ( { <EOL> sub_k : sub_v } ) [ sub_k ] <EOL> generated [ k ] = sub_generated <EOL> return generated <EOL> return _generate_value ( self . schema ) </s>
