Skip to main content

Minimalistic, standalone alternative fake data generator with no dependencies.

Project description

Minimalistic, standalone alternative fake data generator with no dependencies.

PyPI Version Supported Python versions Build Status Documentation Status MIT Coverage

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

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.contrib.auth.models import User
from fake import (
    FACTORY,
    DjangoModelFactory,
    pre_save,
    trait,
)

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",)

    @trait
    def is_admin_user(self, instance: User) -> None:
        instance.is_superuser = True
        instance.is_staff = True
        instance.is_active = True

    @pre_save
    def _set_password(self, instance: User) -> None:
        instance.set_password("test")

And this is how you could use it:

# Create just one user
user = UserFactory()

# Create 5 users
users = UserFactory.create_batch(5)

# Create a user using `is_admin_user` trait
user = UserFactory(is_admin_user=True)

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

Author

Artur Barseghyan <artur.barseghyan@gmail.com>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fake.py-0.4.1.tar.gz (60.5 kB view details)

Uploaded Source

Built Distribution

fake.py-0.4.1-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file fake.py-0.4.1.tar.gz.

File metadata

  • Download URL: fake.py-0.4.1.tar.gz
  • Upload date:
  • Size: 60.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for fake.py-0.4.1.tar.gz
Algorithm Hash digest
SHA256 57c3e2a36dd9d90365e72ec96d516c089eb49907d856d3750666e69f66f06398
MD5 9d4f63866999f98aa9f2fe1a5818cb65
BLAKE2b-256 8a378d6a27fbfccc1c37251ecf1490e6301f2567f243791b0898423f60454d33

See more details on using hashes here.

File details

Details for the file fake.py-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: fake.py-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for fake.py-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66d1cf4db6d2a4fd4ccfd0b054b329dc1060d6d6e347fa0476a8dbeb6ef14c13
MD5 6d387f0ae0c9912164ca3d0cccb0ae64
BLAKE2b-256 c3a959dc580d39ff14bdf23dceebaea425fcdf0b6d96eec6ec164baeef740528

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page