Package: faketory
FaketoryA simple implementation of the Factory pattern, build any number of elements with random values generated byFaker.RequirementsPython>= 3.7IMPORTANT: This library depends onFaker. If by any chance you have any errors, make sure you have it installed. If you're using Python < 3.7, you can adapt this library by downgrading to a version of Faker that's compatible with it, you'll have to followinstallation for devs/contributorsand either build the library or just copyfaketoryfolder to your Python project's root.InstallationpipinstallfaketoryInstallation for devs/contributorsClone this repository.Run./install.sh.Note: You can build the library for distribution by simply runningpoetry build.Quick StartCreatemy_file.py, then copy and paste this code:from faketory.factory import Faketory
from faketory.gens import Fake


class MyModel:
    def __init__(self, age, name, email):
        self.age = age
        self.name = name
        self.email = email


class MyFactory(Faketory):
    age = Fake('pyint', min_value=18, max_value=120)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = MyModel

# () -> Builds an instance with autogenerated values
my_model = MyFactory()
print(f'My name is {my_model.name}\n')

# (age=20) -> Custom value
my_model_custom = MyFactory(age=20)
print(f'My age is {my_model_custom.age}\n')  # 20

# (_qty=9) -> Builds a list of 9 objects
my_models_list = MyFactory(_qty=9)
for index, my_mod in enumerate(my_models_list):
    print(f'{index + 1} {my_mod.email}')Run the file usingpython my_file.py.FaketoryBuilds 1 or more fake model instances of a Model class._qty: Number of fake model instances to return. Minimum value is 1._resolver: Function to resolve once the model instance is created. (Check Django ORM example below)_type: Define the type of the model instance you want to return. Default: 'list', but you can have 'generator' or 'set' instead.**custom_fields: These fields will override the Fake generated values of the Factory.TutorialImport Fake and Faketory:from faketory.factory import Faketory
from faketory.gens import FakeImport your Model(s):from my_poject.models import MyModelCreate your Factory by passing Fake generators to each field you want to pass values in your Model constructor. Also add your Model to the model attribute of the Meta class:class MyFactory(Faketory):
    my_field = Fake('name')
    my_other_field = Fake('text')

    class Meta:
        model = MyModelNote: You can pass any value to any field to your Model constructor instead of a Fake generator.Create your single Model instance:my_model_instance = MyFactory()Now the attributes ofmy_model_instancewill have randomly generated values by faker. For example,my_fieldcould be 'Jon' andmy_other_fieldcould be 'Some text here'.Inheritanceclass SampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = SampleChildThis piece of code is equivalent to:class BaseSampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')

    class Meta:
        model = SampleChild


class SampleFactory(BaseSampleFactory):
    name = Fake('name')FaketoryGenA Faketory can have other Faketories... But if you pass a Faketory instance, it'll be treated as a fixed value. If you want to have different values, you'll need to use a FaketoryGen.from faketory.factory import Faketory
from faketory.gens import FaketoryGen


class SampleChild(Sample):
    def __init__(self, age, email, name):
        self.age = age
        self.email = email
        self.name = name


class SampleHasChild:
    def __init__(self, child, email):
        self.child = child
        self.email = email


class SampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = SampleChild


class SampleHasChildFactory(Faketory):
    child = FaketoryGen(SampleFactory)
    email = Fake('email')

    class Meta:
        model = SampleHasChildDjango ORM exampleYou can pass any Django Model toFaketory.Meta.model, but if you want to save it to your database, make sure you pass_resolver='save'to your Faketory instance.class SampleDjangoFactory(Faketory):
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = MyDjangoModel

my_django_instance = Factory(_resolver='save')FakeThis class simply calls the functions you get from Faker object, so you should check their docs. Here a quick example of how you can generate a fake name:Fakerfrom faker import Faker


fake = Faker()

# Generate a name
first_name = fake.name()Fakefrom faketory.gens import Fake


first_name = Fake('name').generate()Generate a random number# Faker
fake.pyint(min_value=18, max_value=21)

# Fake
Fake('pyint', min_value=18, max_value=21).generate()Generate unique random numbers# Faker
numbers = set(
    fake.unique.random_int(min=1, max=10) for i in range(10)
)

# Fake
numbers = set(
    Fake('unique.random_int', min=1, max=10).generate() for i in range(10)
)CreatorWαя€vเℓ -jg@warevil.dev