fake module

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

class fake.Faker[source]

Bases: object

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


Usage example:

from faker import Faker

FAKER = 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 faker import Faker, TextPdfGenerator, GraphicPdfGenerator

FAKER = Faker()

graphic_pdf_file = Path("media") / "graphic_pdf.pdf"
graphic_pdf_file.write_bytes(
    FAKER.pdf(num_pages=100, generator=GraphicPdfGenerator)
)

text_pdf_file = Path("media") / "text_pdf.pdf"
text_pdf_file.write_bytes(
    FAKER.pdf(num_pages=100, generator=TextPdfGenerator)
)

Various image formats:

from pathlib import Path
from faker import Faker, TextPdfGenerator, GraphicPdfGenerator

FAKER = Faker()

png_file = Path("media") / "image.png"
png_file.write_bytes(FAKER.png())

svg_file = Path("media") / "image.svg"
svg_file.write_bytes(FAKER.svg())

bmp_file = Path("media") / "image.bmp"
bmp_file.write_bytes(FAKER.bmp())

gif_file = Path("media") / "image.gif"
gif_file.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.

date_between(start_date: str, end_date: str = '+0d') 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.

Returns:

A string representing the formatted date.

date_time_between(start_date: str, end_date: str = '+0d') 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.

Returns:

A string representing the formatted datetime.

docx(nb_pages: Optional[int] = 1, texts: Optional[List[str]] = None) bytes[source]
email(domain: str = 'example.com') str[source]
file_name(extension: str = 'txt') str[source]
first_name() str[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.

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]
ipv4() str[source]
last_name() str[source]
name() 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'>, **kwargs) bytes[source]

Create a PDF document of a given size.

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.

pybool() bool[source]
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]
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.

text(nb_chars: int = 200) str[source]
texts(nb: int = 3) List[str][source]
url(protocols: Optional[Tuple[str]] = None, tlds: Optional[Tuple[str]] = None, suffixes: Optional[Tuple[str]] = None) str[source]
word() str[source]
words(nb: int = 5) List[str][source]