Faker File
Description:
- Work with fake file data
Resources:
- https://github.com/barseghyanartur/faker-file/
- https://faker-file.readthedocs.io/en/latest/
- https://faker-file.readthedocs.io/en/latest/recipes.html
- https://github.com/barseghyanartur/faker-file-api
- https://github.com/barseghyanartur/faker-file/tree/main/examples
Environment variables
Section titled “Environment variables”This notebook reads these environment variables. Set them before you start Jupyter:
SFTP_HOST: host name of an SFTP server you can write to.SFTP_PORT: SFTP port (optional, defaults to 22).SFTP_USERNAME: SFTP user name.SFTP_PASSWORD: SFTP password.
Only Example 22 (SFTPStorage) needs these; skip that cell otherwise.
Example 1: TxtFileProvider (Text Files)
Section titled “Example 1: TxtFileProvider (Text Files)”Create a text file with fake data
from faker import Fakerfrom faker_file.providers.txt_file import TxtFileProvider
faker = Faker()faker.add_provider(TxtFileProvider)
# Usage exampletxt_file = faker.txt_file(content="Lorem ipsum")
print(txt_file.data["filename"])# The following line would produce a text generated by Faker, used as# the content of the generated txt_file.print(txt_file.data["content"])Example 2: DocxFileProvider (DocX Files)
Section titled “Example 2: DocxFileProvider (DocX Files)”Create a docx file with fake data
from faker import Fakerfrom faker_file import providersfrom faker_file.providers.docx_file import DocxFileProvider
faker = Faker()faker.add_provider(DocxFileProvider)
docx_file = faker.docx_file( prefix="zzz", max_nb_chars=1_024, wrap_chars_after=80,)print(docx_file.data["filename"])Example 3: ZipFileProvider (Zip Files)
Section titled “Example 3: ZipFileProvider (Zip Files)”Create a zip file with fake data
from faker import Fakerfrom faker_file.providers.zip_file import ZipFileProviderfrom faker_file.providers.helpers.inner import create_inner_zip_file, create_inner_docx_file
faker = Faker()faker.add_provider(ZipFileProvider)
zip_file = faker.zip_file( options={"create_inner_file_args": {"content": "Lorem ipsum"}})print(zip_file.data["filename"])
zip_inner_file = faker.zip_file( prefix="nested_level_0_", options={ "create_inner_file_func": create_inner_zip_file, "create_inner_file_args": { "prefix": "nested_level_1_", "options": { "create_inner_file_func": create_inner_zip_file, "create_inner_file_args": { "prefix": "nested_level_2_", "options": { "count": 2, "create_inner_file_func": create_inner_docx_file, "create_inner_file_args": { "content": "HELLO" + "\n\n{{date}}", } } }, } }, })
print(zip_inner_file.data["filename"])Example 4: BinFileProvider (Bin Files)
Section titled “Example 4: BinFileProvider (Bin Files)”Import the bin file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(BinFileProvider)) and call faker.bin_file().
from faker_file.providers.bin_file import BinFileProviderExample 5: CsvFileProvider (CSV Files)
Section titled “Example 5: CsvFileProvider (CSV Files)”Import the CSV file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(CsvFileProvider)) and call faker.csv_file().
from faker_file.providers.csv_file import CsvFileProviderExample 6: EmlFileProvider (EML Files)
Section titled “Example 6: EmlFileProvider (EML Files)”Import the EML file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(EmlFileProvider)) and call faker.eml_file().
from faker_file.providers.eml_file import EmlFileProviderExample 7: EpubFileProvider (Epub Files)
Section titled “Example 7: EpubFileProvider (Epub Files)”Import the EPUB file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(EpubFileProvider)) and call faker.epub_file().
from faker_file.providers.epub_file import EpubFileProviderExample 8: IcoFileProvider (Ico Files)
Section titled “Example 8: IcoFileProvider (Ico Files)”Import the ICO file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(IcoFileProvider)) and call faker.ico_file().
from faker_file.providers.ico_file import IcoFileProviderExample 9: JpegFileProvider (Jpeg Files)
Section titled “Example 9: JpegFileProvider (Jpeg Files)”Import the JPEG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(JpegFileProvider)) and call faker.jpeg_file().
from faker_file.providers.jpeg_file import JpegFileProviderExample 10: Mp3FileProvider (Mp3 Files)
Section titled “Example 10: Mp3FileProvider (Mp3 Files)”Import the MP3 file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(Mp3FileProvider)) and call faker.mp3_file().
from faker_file.providers.mp3_file import Mp3FileProviderExample 11: OdsFileProvider (Ods Files)
Section titled “Example 11: OdsFileProvider (Ods Files)”Import the ODS file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(OdsFileProvider)) and call faker.ods_file().
from faker_file.providers.ods_file import OdsFileProviderExample 12: OdtFileProvider (Odt Files)
Section titled “Example 12: OdtFileProvider (Odt Files)”Import the ODT file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(OdtFileProvider)) and call faker.odt_file().
from faker_file.providers.odt_file import OdtFileProviderExample 13: PdfFileProvider (PDF Files)
Section titled “Example 13: PdfFileProvider (PDF Files)”Import the PDF file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(PdfFileProvider)) and call faker.pdf_file().
from faker_file.providers.pdf_file import PdfFileProviderExample 14: PngFileProvider (PNG Files)
Section titled “Example 14: PngFileProvider (PNG Files)”Import the PNG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(PngFileProvider)) and call faker.png_file().
from faker_file.providers.png_file import PngFileProviderExample 15: PptxFileProvider (PPTX Files)
Section titled “Example 15: PptxFileProvider (PPTX Files)”Import the PPTX file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(PptxFileProvider)) and call faker.pptx_file().
from faker_file.providers.pptx_file import PptxFileProviderExample 16: RtfFileProvider (RTF Files)
Section titled “Example 16: RtfFileProvider (RTF Files)”Import the RTF file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(RtfFileProvider)) and call faker.rtf_file().
from faker_file.providers.rtf_file import RtfFileProviderExample 17: SvgFileProvider (SVG Files)
Section titled “Example 17: SvgFileProvider (SVG Files)”Import the SVG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(SvgFileProvider)) and call faker.svg_file().
from faker_file.providers.svg_file import SvgFileProviderExample 18: WebpFileProvider (WEBP Files)
Section titled “Example 18: WebpFileProvider (WEBP Files)”Import the WEBP file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(WebpFileProvider)) and call faker.webp_file().
from faker_file.providers.webp_file import WebpFileProviderExample 19: XlsxFileProvider (xlsx Files)
Section titled “Example 19: XlsxFileProvider (xlsx Files)”Import the XLSX file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(XlsxFileProvider)) and call faker.xlsx_file().
from faker_file.providers.xlsx_file import XlsxFileProviderExample 20: GoogleCloudStorage (Google Cloud Files)
Section titled “Example 20: GoogleCloudStorage (Google Cloud Files)”Import the Google Cloud Storage backend. Pass an instance as storage= to any provider method to write the file to a bucket instead of the local disk.
from faker_file.storages.google_cloud_storage import GoogleCloudStorageExample 21: AWSS3Storage (S3 Files)
Section titled “Example 21: AWSS3Storage (S3 Files)”Import the AWS S3 storage backend. Pass an instance as storage= to any provider method to write the file to a bucket instead of the local disk.
from faker_file.storages.aws_s3 import AWSS3StorageExample 22: SFTPStorage (sftp Files)
Section titled “Example 22: SFTPStorage (sftp Files)”Create a sftp file with fake data
import osfrom faker import Fakerfrom faker_file.providers.txt_file import TxtFileProviderfrom faker_file.storages.sftp_storage import SFTPStorage
SFTP_STORAGE = SFTPStorage( host=os.environ["SFTP_HOST"], port=int(os.environ.get("SFTP_PORT", "22")), username=os.environ["SFTP_USERNAME"], password=os.environ["SFTP_PASSWORD"], root_path="/dir-name", # an existing directory on the server)
faker = Faker()faker.add_provider(TxtFileProvider)
txt_file = faker.txt_file(storage=SFTP_STORAGE)print(txt_file.data["filename"])Example 23: FileSystemStorage
Section titled “Example 23: FileSystemStorage”Import the local file-system storage backend (the default storage of every provider). Pass an instance as storage= to choose where files are written.
from faker_file.storages.filesystem import FileSystemStorageExample 24: GenericFileProvider
Section titled “Example 24: GenericFileProvider”Import the generic file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GenericFileProvider)) and call faker.generic_file().
from faker_file.providers.generic_file import GenericFileProviderExample 25: GraphicIcoFileProvider
Section titled “Example 25: GraphicIcoFileProvider”Import the graphic ICO file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicIcoFileProvider)) and call faker.graphic_ico_file().
from faker_file.providers.ico_file import GraphicIcoFileProviderExample 26: GraphicJpegFileProvider
Section titled “Example 26: GraphicJpegFileProvider”Import the graphic JPEG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicJpegFileProvider)) and call faker.graphic_jpeg_file().
from faker_file.providers.jpeg_file import GraphicJpegFileProviderExample 27: GraphicPngFileProvider
Section titled “Example 27: GraphicPngFileProvider”Import the graphic PNG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicPngFileProvider)) and call faker.graphic_png_file().
from faker_file.providers.png_file import GraphicPngFileProviderExample 28: GraphicWebpFileProvider
Section titled “Example 28: GraphicWebpFileProvider”Import the graphic WEBP file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicWebpFileProvider)) and call faker.graphic_webp_file().
from faker_file.providers.webp_file import GraphicWebpFileProviderExample 29: GttsMp3Generator
Section titled “Example 29: GttsMp3Generator”Import the gTTS MP3 generator. Use it with faker.mp3_file(mp3_generator_cls=GttsMp3Generator).
from faker_file.providers.mp3_file.generators.gtts_generator import ( GttsMp3Generator,)Example 30: BaseMp3Generator
Section titled “Example 30: BaseMp3Generator”Import the base class for writing your own MP3 generator.
from faker_file.providers.base.mp3_generator import BaseMp3GeneratorExample 31: EdgeTtsMp3Generator
Section titled “Example 31: EdgeTtsMp3Generator”Import the Edge TTS MP3 generator. Use it with faker.mp3_file(mp3_generator_cls=EdgeTtsMp3Generator).
from faker_file.providers.mp3_file.generators.edge_tts_generator import ( EdgeTtsMp3Generator,)Example 32: textaugment_augmenter
Section titled “Example 32: textaugment_augmenter”Import the TextAugment-based text augmenter, which AugmentFileFromDirProvider can use to vary the text of existing files.
from faker_file.providers.augment_file_from_dir.augmenters import ( textaugment_augmenter,)Example 33: nlpaug_augmenter
Section titled “Example 33: nlpaug_augmenter”Import the nlpaug-based text augmenter, which AugmentFileFromDirProvider can use to vary the text of existing files.
from faker_file.providers.augment_file_from_dir.augmenters import ( nlpaug_augmenter,)ganymede is made by Eugo