faker-file¶
Generate files with fake data
Prerequisites¶
All of core dependencies of this package are MIT licensed. Most of optional dependencies of this package are MIT licensed, while a few are BSD-, Apache 2- or GPLv3 licensed. All licenses are mentioned below between the brackets.
Core package requires Python 3.7, 3.8, 3.9, 3.10 or 3.11.
Faker
(MIT) is the only required dependency.Django
(BSD) integration withfactory_boy
(MIT) has been tested withDjango
2.2, 3.0, 3.1, 3.2, 4.0 and 4.1.DOCX
file support requirespython-docx
(MIT).EPUB
file support requiresxml2epub
(MIT) andjinja2
(BSD).ICO
,JPEG
,PNG
,SVG
andWEBP
files support requiresimgkit
(MIT).MP3
file support requiresgtts
(MIT) oredge-tts
(GPLv3).PDF
file support requirespdfkit
(MIT).PPTX
file support requirespython-pptx
(MIT).ODP
file support requiresodfpy
(Apache 2).ODS
file support requirestablib
(MIT) andodfpy
(Apache 2).ODT
file support requiresodfpy
(Apache 2).XLSX
file support requirestablib
(MIT) andopenpyxl
(MIT).PathyFileSystemStorage
storage support requirespathy
(Apache 2).AWSS3Storage
storage support requirespathy
(Apache 2) andboto3
(Apache 2).AzureCloudStorage
storage support requirespathy
(Apache 2) andazure-storage-blob
(MIT).GoogleCloudStorage
storage support requirespathy
(Apache 2) andgoogle-cloud-storage
(Apache 2).AugmentFileFromDirProvider
provider requiresnlpaug
(MIT),torch
(BSD),transformers
(Apache 2),numpy
(BSD),pandas
(BSD) andtika
(Apache 2).
Documentation¶
Documentation is available on Read the Docs.
For bootstrapping check the Quick start.
For various ready to use code examples see the Recipes.
Installation¶
Latest stable version from PyPI¶
WIth all dependencies
pip install faker-file[all]
Only core
pip install faker-file
With most common dependencies
Everything, except ML libraries which are required for data augmentation only
pip install faker-file[common]
With DOCX support
pip install faker-file[docx]
With EPUB support
pip install faker-file[epub]
With images support
pip install faker-file[images]
With MP3 support
pip install faker-file[mp3]
With XLSX support
pip install faker-file[xlsx]
With ODS support
pip install faker-file[ods]
With ODT support
pip install faker-file[odt]
With data augmentation support
pip install faker-file[data-augmentation]
Or development version from GitHub¶
pip install https://github.com/barseghyanartur/faker-file/archive/main.tar.gz
Features¶
Supported file types¶
BIN
CSV
DOCX
EML
EPUB
ICO
JPEG
MP3
ODS
ODT
ODP
PDF
PNG
RTF
PPTX
SVG
TAR
TXT
WEBP
XLSX
ZIP
Additional providers¶
AugmentFileFromDirProvider
: Make an augmented copy of randomly picked file from given directory. The following types are supported :DOCX
,EML
,EPUB
,ODT
,PDF
,RTF
andTXT
.RandomFileFromDirProvider
: Pick a random file from given directory.
Supported file storages¶
Native file system storage
AWS S3 storage
Azure Cloud Storage
Google Cloud Storage
Usage examples¶
With Faker
¶
One way
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file()
Or another
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
FAKER = Faker()
FAKER.add_provider(TxtFileProvider)
file = FAKER.txt_file()
With factory_boy
¶
upload/models.py¶
from django.db import models
class Upload(models.Model):
# ...
file = models.FileField()
upload/factories.py¶
Note, that when using faker-file
with Django
and native file system
storages, you need to pass your MEDIA_ROOT
setting as root_path
value
to the chosen file storage as show below.
import factory
from django.conf import settings
from factory import Faker
from factory.django import DjangoModelFactory
from faker_file.providers.docx_file import DocxFileProvider
from faker_file.storages.filesystem import FileSystemStorage
from upload.models import Upload
FS_STORAGE = FileSystemStorage(
root_path=settings.MEDIA_ROOT,
rel_path="tmp"
)
factory.Faker.add_provider(DocxFileProvider)
class UploadFactory(DjangoModelFactory):
# ...
file = Faker("docx_file", storage=FS_STORAGE)
class Meta:
model = Upload
File storages¶
All file operations are delegated to a separate abstraction layer of storages.
The following storages are implemented:
FileSystemStorage
: Does not have additional requirements.PathyFileSystemStorage
: Requirespathy
.AzureCloudStorage
: Requirespathy
and Azure related dependencies.GoogleCloudStorage
: Requirespathy
and Google Cloud related dependencies.AWSS3Storage
: Requirespathy
and AWS S3 related dependencies.
Usage example with storages¶
FileSystemStorage example¶
Native file system storage. Does not have dependencies.
import tempfile
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.filesystem import FileSystemStorage
FS_STORAGE = FileSystemStorage(
root_path=tempfile.gettempdir(), # Use settings.MEDIA_ROOT for Django
rel_path="tmp",
)
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file(storage=FS_STORAGE)
FS_STORAGE.exists(file)
PathyFileSystemStorage example¶
Native file system storage. Requires pathy
.
import tempfile
from pathy import use_fs
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.cloud import PathyFileSystemStorage
use_fs(tempfile.gettempdir())
PATHY_FS_STORAGE = PathyFileSystemStorage(
bucket_name="bucket_name",
root_path="tmp"
rel_path="sub-tmp",
)
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file(storage=PATHY_FS_STORAGE)
PATHY_FS_STORAGE.exists(file)
AWSS3Storage example¶
AWS S3 storage. Requires pathy
and boto3
.
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.aws_s3 import AWSS3Storage
S3_STORAGE = AWSS3Storage(
bucket_name="bucket_name",
root_path="tmp", # Optional
rel_path="sub-tmp", # Optional
# Credentials are optional too. If your AWS credentials are properly
# set in the ~/.aws/credentials, you don't need to send them
# explicitly.
credentials={
"key_id": "YOUR KEY ID",
"key_secret": "YOUR KEY SECRET"
},
)
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file(storage=S3_STORAGE)
S3_STORAGE.exists(file)
Testing¶
Simply type:
pytest -vrx
Or use tox:
tox
Or use tox to check specific env:
tox -e py310-django41
Writing documentation¶
Keep the following hierarchy.
=====
title
=====
header
======
sub-header
----------
sub-sub-header
~~~~~~~~~~~~~~
sub-sub-sub-header
^^^^^^^^^^^^^^^^^^
sub-sub-sub-sub-header
++++++++++++++++++++++
sub-sub-sub-sub-sub-header
**************************
License¶
MIT
Support¶
For security issues contact me at the e-mail given in the Author section.
For overall issues, go to GitHub.
Project documentation¶
Contents:
- faker-file
- Prerequisites
- Documentation
- Installation
- Features
- Usage examples
- File storages
- Testing
- Writing documentation
- License
- Support
- Author
- Project documentation
- Quick start
- Recipes
- When using with
Faker
- Imports and initializations
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a ZIP file consisting of TXT files with static content
- Create a ZIP file consisting of 3 DOCX files with dynamically generated content
- Create a ZIP file of 9 DOCX files with content generated from template
- Create a nested ZIP file
- Create a ZIP file with variety of different file types within
- Create a EML file consisting of TXT files with static content
- Create a EML file consisting of 3 DOCX files with dynamically generated content
- Create a nested EML file
- Create an EML file with variety of different file types within
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a PDF file with predefined template containing dynamic fixtures
- Create a MP3 file
- Create a MP3 file by explicitly specifying MP3 generator class
- Create a MP3 file with custom MP3 generator
- Pick a random file from a directory given
- Generate a file of a certain size
- Generate a lot of files using multiprocessing
- Generating files from existing documents using NLP augmentation
- When using with
Django
(andfactory_boy
)
- When using with
- Security Policy
- Release history and notes
- Package
- faker_file package
- Subpackages
- faker_file.providers package
- Subpackages
- faker_file.providers.augment_file_from_dir package
- faker_file.providers.helpers package
- Submodules
- faker_file.providers.helpers.inner module
create_inner_bin_file()
create_inner_csv_file()
create_inner_docx_file()
create_inner_eml_file()
create_inner_epub_file()
create_inner_ico_file()
create_inner_jpeg_file()
create_inner_mp3_file()
create_inner_odp_file()
create_inner_ods_file()
create_inner_odt_file()
create_inner_pdf_file()
create_inner_png_file()
create_inner_pptx_file()
create_inner_rtf_file()
create_inner_svg_file()
create_inner_tar_file()
create_inner_txt_file()
create_inner_webp_file()
create_inner_xlsx_file()
create_inner_zip_file()
fuzzy_choice_create_inner_file()
- Module contents
- faker_file.providers.mixins package
- faker_file.providers.mp3_file package
- Submodules
- faker_file.providers.bin_file module
- faker_file.providers.csv_file module
- faker_file.providers.docx_file module
- faker_file.providers.eml_file module
- faker_file.providers.epub_file module
- faker_file.providers.ico_file module
- faker_file.providers.jpeg_file module
- faker_file.providers.ods_file module
- faker_file.providers.odt_file module
- faker_file.providers.pdf_file module
- faker_file.providers.png_file module
- faker_file.providers.pptx_file module
- faker_file.providers.random_file_from_dir module
- faker_file.providers.rtf_file module
- faker_file.providers.svg_file module
- faker_file.providers.txt_file module
- faker_file.providers.webp_file module
- faker_file.providers.xlsx_file module
- faker_file.providers.zip_file module
- Module contents
- Subpackages
- faker_file.storages package
- faker_file.tests package
- Submodules
- faker_file.tests.test_augment_file_from_dir_provider module
- faker_file.tests.test_django_integration module
- faker_file.tests.test_providers module
ProvidersTestCase
ProvidersTestCase.setUp()
ProvidersTestCase.test_broken_imports
ProvidersTestCase.test_faker
ProvidersTestCase.test_faker_retry_failures
ProvidersTestCase.test_mp3_file_generate_not_implemented_exception()
ProvidersTestCase.test_standalone_providers
ProvidersTestCase.test_standalone_providers_allow_failures
ProvidersTestCase.test_standalone_providers_retry_failures
ProvidersTestCase.test_standalone_tar_file
ProvidersTestCase.test_standalone_tar_file_allow_failures
ProvidersTestCase.test_standalone_zip_file
ProvidersTestCase.test_standalone_zip_file_allow_failures
- faker_file.tests.test_sqlalchemy_integration module
- faker_file.tests.test_storages module
TestStoragesTestCase
TestStoragesTestCase.test_base_storage_exceptions
TestStoragesTestCase.test_cloud_storage_exceptions
TestStoragesTestCase.test_file_system_storage_abspath()
TestStoragesTestCase.test_pathy_file_system_storage_abspath()
TestStoragesTestCase.test_storage
TestStoragesTestCase.test_storage_generate_filename_exceptions
TestStoragesTestCase.test_storage_initialization_exceptions
- faker_file.tests.texts module
- Module contents
- faker_file.providers package
- Submodules
- faker_file.base module
- faker_file.constants module
- faker_file.helpers module
- Module contents
- Subpackages
- faker_file package
- Indices and tables
- Quick start
- Recipes
- When using with
Faker
- Imports and initializations
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a ZIP file consisting of TXT files with static content
- Create a ZIP file consisting of 3 DOCX files with dynamically generated content
- Create a ZIP file of 9 DOCX files with content generated from template
- Create a nested ZIP file
- Create a ZIP file with variety of different file types within
- Create a EML file consisting of TXT files with static content
- Create a EML file consisting of 3 DOCX files with dynamically generated content
- Create a nested EML file
- Create an EML file with variety of different file types within
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a PDF file with predefined template containing dynamic fixtures
- Create a MP3 file
- Create a MP3 file by explicitly specifying MP3 generator class
- Create a MP3 file with custom MP3 generator
- Pick a random file from a directory given
- Generate a file of a certain size
- Generate a lot of files using multiprocessing
- Generating files from existing documents using NLP augmentation
- When using with
Django
(andfactory_boy
)
- When using with
- Security Policy
- Release history and notes
- Package
- faker_file package
- Subpackages
- faker_file.providers package
- Subpackages
- faker_file.providers.augment_file_from_dir package
- faker_file.providers.helpers package
- Submodules
- faker_file.providers.helpers.inner module
create_inner_bin_file()
create_inner_csv_file()
create_inner_docx_file()
create_inner_eml_file()
create_inner_epub_file()
create_inner_ico_file()
create_inner_jpeg_file()
create_inner_mp3_file()
create_inner_odp_file()
create_inner_ods_file()
create_inner_odt_file()
create_inner_pdf_file()
create_inner_png_file()
create_inner_pptx_file()
create_inner_rtf_file()
create_inner_svg_file()
create_inner_tar_file()
create_inner_txt_file()
create_inner_webp_file()
create_inner_xlsx_file()
create_inner_zip_file()
fuzzy_choice_create_inner_file()
- Module contents
- faker_file.providers.mixins package
- faker_file.providers.mp3_file package
- Submodules
- faker_file.providers.bin_file module
- faker_file.providers.csv_file module
- faker_file.providers.docx_file module
- faker_file.providers.eml_file module
- faker_file.providers.epub_file module
- faker_file.providers.ico_file module
- faker_file.providers.jpeg_file module
- faker_file.providers.ods_file module
- faker_file.providers.odt_file module
- faker_file.providers.pdf_file module
- faker_file.providers.png_file module
- faker_file.providers.pptx_file module
- faker_file.providers.random_file_from_dir module
- faker_file.providers.rtf_file module
- faker_file.providers.svg_file module
- faker_file.providers.txt_file module
- faker_file.providers.webp_file module
- faker_file.providers.xlsx_file module
- faker_file.providers.zip_file module
- Module contents
- Subpackages
- faker_file.storages package
- faker_file.tests package
- Submodules
- faker_file.tests.test_augment_file_from_dir_provider module
- faker_file.tests.test_django_integration module
- faker_file.tests.test_providers module
ProvidersTestCase
ProvidersTestCase.setUp()
ProvidersTestCase.test_broken_imports
ProvidersTestCase.test_faker
ProvidersTestCase.test_faker_retry_failures
ProvidersTestCase.test_mp3_file_generate_not_implemented_exception()
ProvidersTestCase.test_standalone_providers
ProvidersTestCase.test_standalone_providers_allow_failures
ProvidersTestCase.test_standalone_providers_retry_failures
ProvidersTestCase.test_standalone_tar_file
ProvidersTestCase.test_standalone_tar_file_allow_failures
ProvidersTestCase.test_standalone_zip_file
ProvidersTestCase.test_standalone_zip_file_allow_failures
- faker_file.tests.test_sqlalchemy_integration module
- faker_file.tests.test_storages module
TestStoragesTestCase
TestStoragesTestCase.test_base_storage_exceptions
TestStoragesTestCase.test_cloud_storage_exceptions
TestStoragesTestCase.test_file_system_storage_abspath()
TestStoragesTestCase.test_pathy_file_system_storage_abspath()
TestStoragesTestCase.test_storage
TestStoragesTestCase.test_storage_generate_filename_exceptions
TestStoragesTestCase.test_storage_initialization_exceptions
- faker_file.tests.texts module
- Module contents
- faker_file.providers package
- Submodules
- faker_file.base module
- faker_file.constants module
- faker_file.helpers module
- Module contents
- Subpackages
- faker_file package
- Indices and tables