Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __load_cls(cls, cls_type):
if isinstance(cls_type, _.string_types):
if '.' in cls_type:
app_label, model_name = cls_type.split(".")
return apps.get_model(app_label, model_name)
else:
try:
if cls_type not in cls.models_cache:
cls.__update_cache()
return cls.models_cache[cls_type]
except KeyError:
raise ValueError('Model "%s" not found.' % cls_type)
return cls_type
def __load_cls(cls_type):
if isinstance(cls_type, _.string_types):
mod, cls_type = cls_type.rsplit('.', 1)
mod = _.import_module(mod)
cls_type = getattr(mod, cls_type)
return cls_type
def __load_cls(cls_type):
if isinstance(cls_type, _.string_types):
mod, cls_type = cls_type.rsplit('.', 1)
mod = import_module(mod)
cls_type = getattr(mod, cls_type)
return cls_type
def __load_cls(cls_type):
if isinstance(cls_type, _.string_types):
mod, cls_type = cls_type.rsplit('.', 1)
mod = _.import_module(mod)
cls_type = getattr(mod, cls_type)
return cls_type
return cls.models_cache[cls_type]
except KeyError:
raise ValueError('Model "%s" not found.' % cls_type)
return cls_type
def __update_cache(cls):
""" Update apps cache for Django < 1.7. """
for app in apps.all_models:
for name, model in apps.all_models[app].items():
cls.models_cache[name] = model
class TypeMixer(_.with_metaclass(TypeMixerMeta, BaseTypeMixer)):
""" TypeMixer for Django. """
__metaclass__ = TypeMixerMeta
factory = GenFactory
def postprocess(self, target, postprocess_values):
""" Fill postprocess_values. """
for name, deffered in postprocess_values:
if not isinstance(deffered.scheme, GenericForeignKey):
continue
name, value = self._get_value(name, deffered.value)
setattr(target, name, value)
('percent', decimal.Decimal): faker.percent_decimal,
('percent', int): faker.percent,
('phone', str): faker.phone_number,
('site', str): faker.url,
('slug', str): faker.slug,
('street', str): faker.street_name,
('time_zone', str): faker.timezone,
('timezone', str): faker.timezone,
('title', str): faker.title,
('url', t.URL): faker.uri,
('url', str): faker.uri,
('username', str): faker.user_name,
}
types = {
_.string_types: str,
_.integer_types: int,
}
@classmethod
def cls_to_simple(cls, fcls):
""" Translate class to one of simple base types.
:return type: A simple type for generation
"""
if fcls in cls.types:
return cls.types[fcls]
if fcls in cls.generators:
return fcls
def __init__(self, cls, mixer=None, factory=None, fake=True):
self.middlewares = []
self.__factory = factory or self.factory
self.__fake = fake
self.__gen_values = defaultdict(set)
self.__fabrics = dict()
self.__mixer = mixer
self.__scheme = cls
self.__fields = _.OrderedDict(self.__load_fields())
Short format is a python formating string
::
for counter in range(3):
mixer.blend(Scheme, name=mixer.sequence('test{0}'))
"""
if len(args) > 1:
def gen():
while True:
for o in args:
yield o
return gen()
func = args and args[0] or None
if isinstance(func, _.string_types):
func = func.format
elif func is None:
func = lambda x: x
def gen2():
counter = 0
while True:
yield func(counter)
counter += 1
return gen2()
def __getattr__(self, name):
raise AttributeError('Use "cycle" only for "blend"')
# Support depricated attributes
class _MetaMixer(type):
FAKE = property(lambda cls: t.Fake())
MIX = property(lambda cls: t.Mix())
RANDOM = property(lambda cls: t.Random())
SELECT = property(lambda cls: t.Select())
SKIP = property(lambda cls: SKIP_VALUE)
class Mixer(_.with_metaclass(_MetaMixer)):
""" This class is using for integration to an application.
:param fake: (True) Generate fake data instead of random data.
:param factory: (:class:`~mixer.main.GenFactory`) Fabric's factory
::
class SomeScheme:
score = int
name = str
mixer = Mixer()
instance = mixer.blend(SomeScheme)
print instance.name # Some like: 'Mike Douglass'
params['fakers'] = fakers
params['types'] = types
return super(GenFactoryMeta, mcs).__new__(mcs, name, bases, params)
@staticmethod
def __flat_keys(d):
for key, value in d.items():
if isinstance(key, (tuple, list)):
for k in key:
yield k, value
continue
yield key, value
class GenFactory(_.with_metaclass(GenFactoryMeta)):
""" Make generators for types. """
generators = {
bool: faker.pybool,
float: faker.pyfloat,
int: faker.random_int,
str: faker.pystr,
bytes: faker.pybytes,
list: faker.pylist,
set: faker.pyset,
tuple: faker.pytuple,
dict: faker.pydict,
datetime.date: faker.date,
datetime.datetime: faker.date_time,
datetime.time: faker.time,