fake.py
Minimalistic, standalone alternative fake data generator with no dependencies.
fake.py is a standalone, portable library designed for generating various random data types for testing.
It offers a simplified, dependency-free alternative for creating random texts, (person) names, URLs, dates, file names, IPs, primitive Python data types (such as uuid, str, int, float, bool) and byte content for multiple file formats including PDF, DOCX, PNG, SVG, BMP, and GIF.
The package also supports file creation on the filesystem and includes factories (dynamic fixtures) compatible with Django, TortoiseORM, and Pydantic.
Features
Generation of random texts, (person) names, emails, URLs, dates, IPs, and primitive Python data types.
Support for various file formats (PDF, DOCX, TXT, PNG, SVG, BMP, GIF) and file creation on the filesystem.
Basic factories for integration with Django, Pydantic, and TortoiseORM.
Prerequisites
Python 3.8+
Installation
pip
pip install fake.py
Download and copy
fake.py
is the sole, self-contained module of the package. It includes
tests too. If it’s more convenient to you, you could simply download the
fake.py
module and include it in your repository.
Since tests are included, it won’t have a negative impact on your test coverage (you might need to apply tweaks to your coverage configuration).
Documentation
Documentation is available on Read the Docs.
For various ready to use code examples see the Recipes.
For tips on
PDF
creation see Creating PDF.For tips on
DOCX
creation see Creating DOCX.For tips on images creation see Creating images.
For guidelines on contributing check the Contributor guidelines.
Usage
Generate data
Person names
from fake import FAKER
FAKER.first_name()
FAKER.last_name()
FAKER.name()
FAKER.username()
Random texts
from fake import FAKER
FAKER.slug()
FAKER.word()
FAKER.sentence()
FAKER.paragraph()
FAKER.text()
Internet
from fake import FAKER
FAKER.email()
FAKER.url()
FAKER.ipv4()
Filenames
from fake import FAKER
FAKER.filename()
Primitive data types
from fake import FAKER
FAKER.pyint()
FAKER.pybool()
FAKER.pystr()
FAKER.pyfloat()
Dates
from fake import FAKER
FAKER.date()
FAKER.date_time()
Generate files
As bytes
from fake import FAKER
FAKER.pdf()
FAKER.docx()
FAKER.png()
FAKER.svg()
FAKER.bmp()
FAKER.gif()
As files on the file system
from fake import FAKER
FAKER.pdf_file()
FAKER.docx_file()
FAKER.png_file()
FAKER.svg_file()
FAKER.bmp_file()
FAKER.gif_file()
FAKER.txt_file()
Factories
This is how you could define a factory for Django’s built-in User
model.
from django.conf import settings
from django.contrib.auth.models import User
from fake import (
FACTORY,
DjangoModelFactory,
FileSystemStorage,
SubFactory,
pre_save,
)
STORAGE = FileSystemStorage(root_path=settings.MEDIA_ROOT, rel_path="tmp")
class UserFactory(DjangoModelFactory):
username = FACTORY.username()
first_name = FACTORY.first_name()
last_name = FACTORY.last_name()
email = FACTORY.email()
last_login = FACTORY.date_time()
is_superuser = False
is_staff = False
is_active = FACTORY.pybool()
date_joined = FACTORY.date_time()
class Meta:
model = User
get_or_create = ("username",)
@pre_save
def __set_password(instance):
instance.set_password("test")
And this is how you could use it:
user = UserFactory()
users = UserFactory.create_batch(5)
Tests
Run the tests with unittest:
python -m unittest fake.py
Or pytest:
pytest
Differences with alternatives
fake.py is Faker + factory_boy + faker-file in one package, radically simplified and reduced in features, but without any external dependencies (not even Pillow or dateutil).
fake.py is modeled after the famous Faker package. Its’ API is highly compatible, although drastically reduced. It’s not multilingual and does not support postal codes or that many RAW file formats. However, you could easily include it in your production setup without worrying about yet another dependency.
On the other hand, fake.py factories look quite similar to factory_boy factories, although again - drastically simplified and reduced in features.
The file generation part of fake.py are modelled after the faker-file. You don’t get a large variety of file types supported and you don’t have that much control over the content of the files generated, but you get dependency-free valid files and if that’s all you need, you don’t need to look further.
However, at any point, if you discover that you “need more”, go for Faker, factory_boy and faker-file combination.
License
MIT
Project documentation
Contents:
- fake.py
- Recipes
- Creating images
- Creating PDF
- Creating DOCX
- Factories
- Security Policy
- Contributor guidelines
- Contributor Covenant Code of Conduct
- Release history and notes
- Package
- fake module
AuthorshipData
BaseStorage
DjangoModelFactory
DocxGenerator
Factory
Factory.bmp_file()
Factory.date()
Factory.date_time()
Factory.docx_file()
Factory.email()
Factory.first_name()
Factory.ipv4()
Factory.last_name()
Factory.name()
Factory.paragraph()
Factory.pdf_file()
Factory.png_file()
Factory.pybool()
Factory.pyfloat()
Factory.pyint()
Factory.pystr()
Factory.sentence()
Factory.slug()
Factory.svg_file()
Factory.text()
Factory.txt_file()
Factory.url()
Factory.username()
Factory.uuid()
Factory.word()
Faker
Faker.bmp()
Faker.bmp_file()
Faker.date()
Faker.date_time()
Faker.docx()
Faker.docx_file()
Faker.email()
Faker.file_name()
Faker.first_name()
Faker.gif()
Faker.gif_file()
Faker.image()
Faker.ipv4()
Faker.last_name()
Faker.load_names()
Faker.load_words()
Faker.name()
Faker.paragraph()
Faker.paragraphs()
Faker.pdf()
Faker.pdf_file()
Faker.png()
Faker.png_file()
Faker.pybool()
Faker.pyfloat()
Faker.pyint()
Faker.pystr()
Faker.sentence()
Faker.sentences()
Faker.slug()
Faker.svg()
Faker.svg_file()
Faker.text()
Faker.texts()
Faker.txt_file()
Faker.url()
Faker.username()
Faker.uuid()
Faker.word()
Faker.words()
FileRegistry
FileSystemStorage
GraphicPdfGenerator
MetaData
ModelFactory
StringValue
SubFactory
TextPdfGenerator
TortoiseModelFactory
post_save()
pre_save()
run_async_in_thread()
trait()
- fake module
- Indices and tables