fake module

https://github.com/barseghyanartur/fake.py/

class fake.AuthorshipData[source]

Bases: object

first_names: Set[str] = {'Andre', 'Andrew', 'Andrey', 'Anthony', 'Barry', 'Ben', 'Benjamin', 'Christian', 'Collin', 'David', 'Donald', 'Eric', 'Ezio', 'George', 'Gregor', 'Guido', 'Guilherme', 'Jack', 'Jacques', 'Jiwon', 'Ka-Ping', 'Keith', 'Kenneth', 'Lars', 'Marc-Andre', 'Martin', 'Michael', 'Mike', 'Nadeem', 'Nick', 'Paul', 'Piers', 'Skip', 'Steen', 'Steven', 'Thomas', 'Victor', 'Vinay', 'Zooko'}
last_names: Set[str] = {'Ascher', 'Baxter', 'Bland', 'Boutsioukis', 'Dalke', 'Dart', 'Diederich', 'Dragon De Monsyne', 'Edds', 'Felt', 'Frechet', 'Gertzfield', 'Gust', 'Heimes', 'J', 'Kippes', 'Larson', 'Lauder', 'Lemburg', 'Lingl', 'Lumholt', 'McGuire', 'Melotti', 'Montanaro', "O'Whielacronx", 'Peterson', 'Petrov', 'Polo', 'Reitz', 'Roberge', 'Sajip', 'Seo', 'Stinner', 'Stufft and individual contributors', 'Vawda', 'Warsaw', 'Winter', 'Wouters', 'Yee', 'van Rossum', 'von Loewis'}
class fake.BaseStorage(*args, **kwargs)[source]

Bases: object

Base storage.

abstract abspath(filename: Any) str[source]

Return absolute path.

abstract exists(filename: Any) bool[source]

Check if file exists.

abstract generate_filename(extension: str, prefix: Optional[str] = None, basename: Optional[str] = None) Any[source]

Generate filename.

abstract relpath(filename: Any) str[source]

Return relative path.

Delete the file.

abstract write_bytes(filename: Any, data: bytes) int[source]

Write bytes.

abstract write_text(filename: Any, data: str, encoding: Optional[str] = None) int[source]

Write text.

class fake.DjangoModelFactory(**kwargs)[source]

Bases: ModelFactory

Django ModelFactory.

classmethod create(**kwargs)[source]
classmethod save(instance)[source]

Save the instance.

class fake.DocxGenerator(faker: Faker)[source]

Bases: object

DocxGenerator - generates a DOCX file with text.

Usage example:

from pathlib import Path
from fake import FAKER

Path("/tmp/example.docx").write_bytes(FAKER.docx(nb_pages=100))
create(nb_pages: Optional[int] = None, texts: Optional[List[str]] = None, metadata: Optional[MetaData] = None) bytes[source]
class fake.Factory(faker: Optional[Faker] = None)[source]

Bases: object

Factory.

property faker
class fake.FactoryMethod(method_name: str, faker: Optional[Faker] = None, **kwargs)[source]

Bases: object

class fake.Faker(alias: Optional[str] = None)[source]

Bases: object

fake.py - simplified, standalone alternative with no dependencies.


Usage example:

from fake import FAKER

print(FAKER.first_name())  # Random first name
print(FAKER.last_name())  # Random last name
print(FAKER.name())  # Random name
print(FAKER.word())  # Random word from the Zen of Python
print(FAKER.words(nb=3))  # List of 3 random words from Zen of Python
print(FAKER.sentence())  # Random sentence (5 random words by default)
print(FAKER.paragraph())  # Paragraph (5 random sentences by default)
print(FAKER.paragraphs())  # 3 random paragraphs
print(FAKER.text())  # Random text up to 200 characters
print(FAKER.file_name())  # Random filename with '.txt' extension
print(FAKER.email())  # Random email
print(FAKER.url())  # Random URL
print(FAKER.pyint())  # Random integer
print(FAKER.pybool())  # Random boolean
print(FAKER.pystr())  # Random string
print(FAKER.pyfloat())  # Random float

PDF:

from pathlib import Path
from fake import FAKER, TextPdfGenerator, GraphicPdfGenerator

Path("/tmp/graphic_pdf.pdf").write_bytes(
    FAKER.pdf(nb_pages=100, generator=GraphicPdfGenerator)
)

Path("/tmp/text_pdf.pdf").write_bytes(
    FAKER.pdf(nb_pages=100, generator=TextPdfGenerator)
)

Various image formats:

from pathlib import Path
from fake import FAKER

Path("/tmp/image.png").write_bytes(FAKER.png())

Path("/tmp/image.svg").write_bytes(FAKER.svg())

Path("/tmp/image.bmp").write_bytes(FAKER.bmp())

Path("/tmp/image.gif").write_bytes(FAKER.gif())

Note, that all image formats accept size (default: (100, 100)) and color`(default: `(255, 0, 0)) arguments.

bmp(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255)) bytes[source]

Create a BMP image of a specified color.

Parameters:
  • size – Tuple of width and height of the image in pixels.

  • color – Color of the image in RGB format (tuple of three integers).

Returns:

Byte content of the BMP image.

Return type:

bytes

bmp_file(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255), storage: Optional[BaseStorage] = None, basename: Optional[str] = None, prefix: Optional[str] = None) StringValue[source]
date(start_date: str = '-7d', end_date: str = '+0d', tzinfo: timezone = datetime.timezone.utc) date[source]

Generate random date between start_date and end_date.

Parameters:
  • start_date – The start date from which the random date should be generated in the shorthand notation.

  • end_date – The end date up to which the random date should be generated in the shorthand notation.

  • tzinfo – The timezone.

Returns:

A string representing the formatted date.

Return type:

date

date_time(start_date: str = '-7d', end_date: str = '+0d', tzinfo: timezone = datetime.timezone.utc) datetime[source]

Generate a random datetime between start_date and end_date.

Parameters:
  • start_date – The start datetime from which the random datetime should be generated in the shorthand notation.

  • end_date – The end datetime up to which the random datetime should be generated in the shorthand notation.

  • tzinfo – The timezone.

Returns:

A string representing the formatted datetime.

Return type:

datetime

docx(nb_pages: Optional[int] = 1, texts: Optional[List[str]] = None, metadata: Optional[MetaData] = None) bytes[source]
docx_file(nb_pages: int = 1, texts: Optional[List[str]] = None, metadata: Optional[MetaData] = None, storage: Optional[BaseStorage] = None, basename: Optional[str] = None, prefix: Optional[str] = None) StringValue[source]
email(domain: str = 'example.com') str[source]
file_name(extension: str = 'txt') str[source]
first_name() str[source]
first_names(nb: int = 5) List[str][source]
generic_file(content: Union[bytes, str], extension: str, storage: Optional[BaseStorage] = None, basename: Optional[str] = None, prefix: Optional[str] = None) StringValue[source]
static get_by_alias(alias: str) Optional[Faker][source]
static get_by_uid(uid: str) Optional[Faker][source]
gif(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255)) bytes[source]

Create a GIF image of a specified color.

Parameters:
  • size – Tuple of width and height of the image in pixels.

  • color – Color of the image in RGB format (tuple of three integers).

Returns:

Byte content of the GIF image.

Return type:

bytes

gif_file(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255), storage: Optional[BaseStorage] = None, basename: Optional[str] = None, prefix: Optional[str] = None) StringValue[source]
image(image_format: Literal['png', 'svg', 'bmp', 'gif'] = 'png', size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255)) bytes[source]
image_url(width: int = 800, height: int = 600, service_url: Optional[str] = None) str[source]

Image URL.

ipv4() str[source]
last_name() str[source]
last_names(nb: int = 5) List[str][source]
load_names() None[source]
load_words() None[source]
name() str[source]
names(nb: int = 5) List[str][source]
paragraph(nb_sentences: int = 5) str[source]
paragraphs(nb: int = 3) List[str][source]
pdf(nb_pages: int = 1, generator: ~typing.Union[~typing.Type[~fake.TextPdfGenerator], ~typing.Type[~fake.GraphicPdfGenerator]] = <class 'fake.GraphicPdfGenerator'>, metadata: ~typing.Optional[~fake.MetaData] = None, **kwargs) bytes[source]

Create a PDF document of a given size.

pdf_file(nb_pages: int = 1, generator: ~typing.Union[~typing.Type[~fake.TextPdfGenerator], ~typing.Type[~fake.GraphicPdfGenerator]] = <class 'fake.GraphicPdfGenerator'>, metadata: ~typing.Optional[~fake.MetaData] = None, storage: ~typing.Optional[~fake.BaseStorage] = None, basename: ~typing.Optional[str] = None, prefix: ~typing.Optional[str] = None, **kwargs) StringValue[source]
png(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255)) bytes[source]

Create a PNG image of a specified color.

Parameters:
  • size – Tuple of width and height of the image in pixels.

  • color – Color of the image in RGB format (tuple of three integers).

Returns:

Byte content of the PNG image.

Return type:

bytes

png_file(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255), storage: Optional[BaseStorage] = None, basename: Optional[str] = None, prefix: Optional[str] = None) StringValue[source]
pybool() bool[source]
pydecimal(left_digits: int = 5, right_digits: int = 2, positive: bool = True) Decimal[source]

Generate a random Decimal number.

Parameters:
  • left_digits – Number of digits to the left of the decimal point.

  • right_digits – Number of digits to the right of the decimal point.

  • positive – If True, the number will be positive, otherwise it can be negative.

Returns:

A randomly generated Decimal number.

Return type:

Decimal

Raises:

ValueError

pyfloat(min_value: float = 0.0, max_value: float = 10.0) float[source]
pyint(min_value: int = 0, max_value: int = 9999) int[source]
pystr(nb_chars: int = 20) str[source]
sentence(nb_words: int = 5) str[source]
sentences(nb: int = 3) List[str][source]
slug() str[source]
slugs(nb: int = 5) List[str][source]
svg(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255)) bytes[source]

Create a SVG image of a specified color.

Parameters:
  • size – Tuple of width and height of the image in pixels.

  • color – Color of the image in RGB format (tuple of three integers).

Returns:

Byte content of the SVG image.

Return type:

bytes

svg_file(size: Tuple[int, int] = (100, 100), color: Tuple[int, int, int] = (0, 0, 255), storage: Optional[BaseStorage] = None, basename: Optional[str] = None, prefix: Optional[str] = None) StringValue[source]
text(nb_chars: int = 200) str[source]
texts(nb: int = 3) List[str][source]
txt_file(nb_chars: Optional[int] = 200, storage: Optional[BaseStorage] = None, basename: Optional[str] = None, prefix: Optional[str] = None, text: Optional[str] = None) StringValue[source]
url(protocols: Optional[Tuple[str]] = None, tlds: Optional[Tuple[str]] = None, suffixes: Optional[Tuple[str]] = None) str[source]
username() str[source]
usernames(nb: int = 5) List[str][source]
uuid() UUID[source]
uuids(nb: int = 5) List[UUID][source]
word() str[source]
words(nb: int = 5) List[str][source]
class fake.FileRegistry[source]

Bases: object

Stores list StringValue instances.

from fake import FAKER, FILE_REGISTRY

txt_file_1 = FAKER.txt_file()
txt_file_2 = FAKER.txt_file()
...
txt_file_n = FAKER.txt_file()

# The FileRegistry._registry would then contain this:
{
    txt_file_1,
    txt_file_2,
    ...,
    txt_file_n,
}

# Clean up created files as follows:
FILE_REGISTRY.clean_up()
add(string_value: StringValue) None[source]
clean_up() None[source]
remove(string_value: Union[StringValue, str]) bool[source]
search(value: str) Optional[StringValue][source]
class fake.FileSystemStorage(root_path: Optional[Union[str, Path]] = '/tmp', rel_path: Optional[str] = 'tmp', *args, **kwargs)[source]

Bases: BaseStorage

File storage class using pathlib for path handling.

Usage example:

from fake import FAKER, FileSystemStorage

storage = FileSystemStorage()
docx_file = storage.generate_filename(prefix="zzz_", extension="docx")
storage.write_bytes(docx_file, FAKER.docx())

Initialization with params:

from fake import FAKER, FileSystemStorage

storage = FileSystemStorage()
docx_file = FAKER.docx_file(storage=storage)
abspath(filename: str) str[source]

Return absolute path.

exists(filename: str) bool[source]

Check if file exists.

generate_filename(extension: str, prefix: Optional[str] = None, basename: Optional[str] = None) str[source]

Generate filename.

relpath(filename: str) str[source]

Return relative path.

Delete the file.

write_bytes(filename: str, data: bytes) int[source]

Write bytes.

write_text(filename: str, data: str, encoding: Optional[str] = None) int[source]

Write text.

class fake.GraphicPdfGenerator(faker: Faker)[source]

Bases: object

Graphic PDF generatr.

Usage example:

from pathlib import Path
from fake import FAKER, GraphicPdfGenerator

Path("/tmp/graphic_example.pdf").write_bytes(
    FAKER.pdf(nb_pages=100, generator=GraphicPdfGenerator)
)
create(nb_pages: int = 1, image_size: Tuple[int, int] = (100, 100), image_color: Tuple[int, int, int] = (255, 0, 0), **kwargs) bytes[source]
image_color: Tuple[int, int, int]
image_size: Tuple[int, int]
nb_pages: int
class fake.LazyAttribute(func)[source]

Bases: object

class fake.LazyFunction(func)[source]

Bases: object

class fake.MetaData[source]

Bases: object

add_content(content: Union[str, List[str]]) None[source]
content: Optional[str]
class fake.ModelFactory(**kwargs)[source]

Bases: object

ModelFactory.

class Meta[source]

Bases: object

get_or_create = ('id',)
classmethod create(**kwargs)[source]
classmethod create_batch(count, **kwargs)[source]
classmethod save(instance)[source]

Save the instance.

class fake.PostSave(func, *args, **kwargs)[source]

Bases: object

execute(instance)[source]
class fake.PreSave(func, *args, **kwargs)[source]

Bases: object

execute(instance)[source]
class fake.SQLAlchemyModelFactory(**kwargs)[source]

Bases: ModelFactory

SQLAlchemy ModelFactory.

classmethod create(**kwargs)[source]
classmethod save(instance)[source]

Save the instance.

class fake.StringValue(value, *args, **kwargs)[source]

Bases: str

data: Dict[str, Any]
class fake.SubFactory(factory_class, **kwargs)[source]

Bases: object

class fake.TextPdfGenerator(faker: Faker)[source]

Bases: object

Text PDF generatr.

Usage example:

from pathlib import Path
from fake import FAKER, TextPdfGenerator

Path("/tmp/text_example.pdf").write_bytes(
    FAKER.pdf(nb_pages=100, generator=TextPdfGenerator)
)
create(nb_pages: Optional[int] = None, texts: Optional[List[str]] = None, metadata: Optional[MetaData] = None, **kwargs) bytes[source]
nb_pages: int
texts: List[str]
class fake.TortoiseModelFactory(**kwargs)[source]

Bases: ModelFactory

Tortoise ModelFactory.

classmethod create(**kwargs)[source]
classmethod save(instance)[source]

Save the instance.

fake.fill_dataclass(dataclass_type: Type) Any

Fill dataclass with data.

fake.fill_pydantic_model(object_type: Type) Any
fake.post_save(func)[source]
fake.pre_save(func)[source]
fake.provider(func)[source]
fake.run_async_in_thread(coroutine: Coroutine) Awaitable[source]

Run an asynchronous coroutine in a separate thread.

Parameters:

coroutine – An asyncio coroutine to be run.

Returns:

The result of the coroutine.

Return type:

Awaitable

fake.trait(func)[source]
fake.xor_transform(val: str, key: int = 10) str[source]

Simple, deterministic string encoder/decoder.

Usage example:

val = "abcd"
encoded_val = xor_transform(val)
decoded_val = xor_transform(encoded_val)