Minimalistic, standalone alternative fake data generator with no dependencies.
Project description
Minimalistic, standalone alternative fake data generator with no dependencies.
Overview
fake.py is a standalone and portable library that allows you to generate various types of random data for testing and other purposes. The package provides a simplified, dependency-free alternative for generating random words, sentences, paragraphs, file names, URLs, person names, as well as bytes content for PDF-, DOCX- and various image formats (such as PNG, SVG, BMP and GIF). It can create files directly on your filesystem too. It comes with factories (works with Django, TortoiseORM, Pydantic).
Requirements
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).
Usage
Generate data
Imports and initialization
from fake import Faker
FAKER = Faker()
first_name
Returns a random first name.
FAKER.first_name()
last_name
Returns a random last name.
FAKER.last_name()
name
Returns a random full name.
FAKER.name()
word
Returns a random word.
FAKER.word()
words
Returns a list of nb random words.
FAKER.words()
Arguments:
nb (type: int, default value: 5) is an optional argument.
sentence
Returns a random sentence with nb_words number of words.
FAKER.sentence()
Arguments:
nb_words (type: int, default value: 5) is an optional argument.
sentences
Returns nb number of random sentences.
FAKER.sentences()
Arguments:
nb (type: int, default value: 3) is an optional argument.
paragraph
Returns a random paragraph with nb_sentences number of sentences.
FAKER.paragraph()
Arguments:
nb_sentences (type: int, default value: 5) is an optional argument.
paragraphs
Returns nb number of random paragraphs.
FAKER.paragraphs()
Arguments:
nb (type: int, default value: 3) is an optional argument.
text
Returns random text with up to nb_chars characters.
FAKER.text()
Arguments:
nb_chars (type: int, default value: 200) is an optional argument.
texts
Returns nb number of random texts.
FAKER.texts()
Arguments:
nb (type: int, default value: 3) is an optional argument.
file_name
Returns a random file name with the given extension.
FAKER.file_name()
Arguments:
extension (type: str, default value: txt) is an optional argument.
Returns a random email with the specified domain.
FAKER.email()
Arguments:
domain (type: str, default value: example.com) is an optional argument.
url
Returns a random URL.
FAKER.url()
Arguments:
protocols (type: Optional[Tuple[str]], default value: None) is an optional argument.
tlds (type: Optional[Tuple[str]], default value: None) is an optional argument.
suffixes (type: Optional[Tuple[str]], default value: None) is an optional argument.
pyint
Returns a random integer between min_value and max_value.
FAKER.pyint()
Arguments:
min_value (type: int, default value: 0) is an optional argument.
max_value (type: int, default value: 9999) is an optional argument.
pybool
Returns a random boolean value.
FAKER.pybool()
pystr
Returns a random string of nb_chars length.
FAKER.pystr()
Arguments:
nb_chars (type: int, default value: 20) is an optional argument.
pyfloat
Returns a random float between min_value and max_value.
FAKER.pyfloat()
Arguments:
min_value (type: float, default value: 0.0) is an optional argument.
max_value (type: float, default value: 10.00) is an optional argument.
ipv4
Returns a random IPv4 address.
FAKER.ipv4()
date_between
Generates a random date between start_date and end_date.
FAKER.date_between(start_date="-1d", end_date="+1d")
Arguments:
start_date (type: str) is a required argument.
end_date (type: str, default value: +0d) is an optional argument.
date_time_between
Generates a random datetime between start_date and end_date.
FAKER.date_time_between(start_date="-1d", end_date="+1d")
Arguments:
start_date (type: str) is a required argument.
end_date (type: str, default value: +0d) is an optional argument.
Generates a content (bytes) of a PDF document.
FAKER.pdf()
Arguments:
nb_pages (type: int, default value: 1) is an optional argument.
texts (type: List[str], default value: None) is an optional argument.
generator (type: Union[Type[TextPdfGenerator], Type[GraphicPdfGenerator]], default value: GraphicPdfGenerator) is an optional argument.
image
Generates a content (bytes) of a image of the specified format and colour.
FAKER.image() # Supported formats are `png`, `svg`, `bmp` and `gif`
Arguments:
image_format (type: str, default value: png) is an optional argument.
size (type: Tuple[int, int], default value: (100, 100)) is an optional argument.
color (type: Tuple[int, int, int], default value: (0, 0, 255)) is an optional argument.
docx
Generates a content (bytes) of a DOCX document.
FAKER.docx()
Arguments:
nb_pages (type: int, default value: 1) is an optional argument.
texts (type: List[str], default value: None) is an optional argument.
pdf_file
Generates a PDF file.
FAKER.pdf_file()
Arguments:
storage (type: BaseStorage, default value: None) is an optional argument.
basename (type: str, default value: None) is an optional argument.
prefix (type: str, default value: None) is an optional argument.
png_file
Generates a PNG file.
FAKER.png_file()
Arguments:
storage (type: BaseStorage, default value: None) is an optional argument.
basename (type: str, default value: None) is an optional argument.
prefix (type: str, default value: None) is an optional argument.
svg_file
Generates an SVG file.
FAKER.svg_file()
Arguments:
storage (type: BaseStorage, default value: None) is an optional argument.
basename (type: str, default value: None) is an optional argument.
prefix (type: str, default value: None) is an optional argument.
bmp_file
Generates a BMP file.
FAKER.bmp_file()
Arguments:
storage (type: BaseStorage, default value: None) is an optional argument.
basename (type: str, default value: None) is an optional argument.
prefix (type: str, default value: None) is an optional argument.
gif_file
Generates a GIF file.
FAKER.gif_file()
Arguments:
storage (type: BaseStorage, default value: None) is an optional argument.
basename (type: str, default value: None) is an optional argument.
prefix (type: str, default value: None) is an optional argument.
txt_file
Generates a TXT file.
FAKER.txt_file()
Arguments:
storage (type: BaseStorage, default value: None) is an optional argument.
basename (type: str, default value: None) is an optional argument.
prefix (type: str, default value: None) is an optional argument.
Factories
Django example
factories.py
from django.conf import settings
from fake import (
DjangoModelFactory,
Factory,
FileSystemStorage,
SubFactory,
pre_save,
)
from article.models import Article
STORAGE = FileSystemStorage(root_path=settings.MEDIA_ROOT, rel_path="tmp")
class ArticleFactory(DjangoModelFactory):
title = Factory.sentence()
slug = Factory.slug()
content = Factory.text()
image = Factory.png_file(storage=STORAGE)
pub_date = Factory.date()
safe_for_work = Factory.pybool()
minutes_to_read = Factory.pyint(min_value=1, max_value=10)
author = SubFactory(UserFactory)
class Meta:
model = Article
Usage example
article = ArticleFactory() # Create one article
articles = ArticleFactory.create_batch(5) # Create 5 articles
Pydantic example
from pathlib import Path
from fake import Factory, FileSystemStorage, ModelFactory, SubFactory
from article.models import Article
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"
STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
class ArticleFactory(ModelFactory):
id = Factory.pyint()
title = Factory.sentence()
slug = Factory.slug()
content = Factory.text()
image = Factory.png_file(storage=STORAGE)
pub_date = Factory.date()
safe_for_work = Factory.pybool()
minutes_to_read = Factory.pyint(min_value=1, max_value=10)
author = SubFactory(UserFactory)
class Meta:
model = Article
Used just like in previous example.
TortoiseORM example
from pathlib import Path
from fake import Factory, FileSystemStorage, SubFactory, TortoiseModelFactory
from article.models import Article, User
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"
STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
class ArticleFactory(TortoiseModelFactory):
id = Factory.pyint()
title = Factory.sentence()
slug = Factory.slug()
content = Factory.text()
image = Factory.png_file(storage=STORAGE)
pub_date = Factory.date_time()
safe_for_work = Factory.pybool()
minutes_to_read = Factory.pyint(min_value=1, max_value=10)
author = SubFactory(UserFactory)
class Meta:
model = Article
Used just like in previous example.
Tests
Run the tests with unittest:
python -m unittest
Or pytest:
pytest
Differences with Faker
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.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fake.py-0.2.tar.gz
.
File metadata
- Download URL: fake.py-0.2.tar.gz
- Upload date:
- Size: 47.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 345fa46e5456afcbe2ca503d760bcdc72ae9015a247e0b5ac1d9648db4338c5c |
|
MD5 | 66d1a13ffa4d0b07c2a2915453fc7f7a |
|
BLAKE2b-256 | 5fc9dd79c0ecaf932ba1a92422ece2f7a14c32ebbec01adb550d54028b07ea85 |
File details
Details for the file fake.py-0.2-py3-none-any.whl
.
File metadata
- Download URL: fake.py-0.2-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c0184b3c1be4439660282161bf768624fbbf8d144c94943dde5b68eaed4f50d |
|
MD5 | 8acc02940e9f3d880d67ea18bc17be6b |
|
BLAKE2b-256 | cb6ffbdeff62b601ec24c253f67de52876f2a6b8272b1e4ec653930fb6c03c66 |