Skip to main content

Write/read from memory instead of files when open() is called

@read_decorator fakes the existence of a file and provides the file content when open(mode='r'/mode='rb') from the builtins is called.

@write_decorator captures the output when open(mode='w'/mode='wb') from the builtins is called.

The decorators don't work with functions/methods that don't use the open() function (for example: cv2.imread / cv2.imwrite)

Some examples

import pandas as pd
import cv2
from PIL import Image
import numpy as np
import os.path
from fake_read_write_files import read_decorator, write_decorator

@read_decorator
def readutf8(filename, _file_data):
    with open(filename, mode="r", encoding="utf-8") as f:
        data = f.read()
    return data


@write_decorator
def write_pil_image(pilpic, filepath):
    pilpic.save(filepath)
    # don't use "return" here, the function will return a dict


@read_decorator
def read_bin_file(filename, _file_data):
    with open(filename, mode="rb") as f:
        data = f.read()
    return data


@read_decorator
def pandasread(filename, _file_data):
    return pd.read_csv(filename)


@write_decorator
def pandaswrite(df, filename):
    df.to_csv(filename)
    # don't use "return" here, the function will return a dict


# the read decorator always checks for the kwarg "_file_data"
# It must be passed as a kwarg
e = readutf8(
    filename="f:\\txtdoesnotexist.txt", _file_data="I am fake\nDid you know that?"
)
print(e)

# real file
bi = Image.open(r"C:\Users\Gamer\anaconda3\envs\dfdir\xxxxxxxxxx.png")

# writing to a fake file, returns a dict with all written files in the function,
# even if there is no return value declared
o = write_pil_image(bi, filepath="i_am_a_fake_image.png")
print(
    cv2.imdecode(np.frombuffer(o["i_am_a_fake_image.png"], np.uint8), cv2.IMREAD_COLOR)
)


binaryfile = read_bin_file(
    filename="i_am_a_fake_image.png", _file_data=o["i_am_a_fake_image.png"]
)


df = pandasread(filename="test.csv", _file_data="john,1\nmaria,2\ncarlos,3")
print(df)

pdcsv = pandaswrite(df, filename="test.csv")
print(pdcsv)


# output 
I am fake
Did you know that?
[[[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [254 255 255]
  [253 255 255]]
 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [254 255 255]
  [253 255 255]]
 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [254 255 255]
  [253 255 255]]
 ...
 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 254]
  [255 255 254]
  [255 255 254]]
 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 254]
  [255 255 254]
  [255 255 254]]
 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 254]
  [255 255 254]
  [255 255 254]]]
     john  1
0   maria  2
1  carlos  3
{'test.csv': ',john,1\r\n0,maria,2\r\n1,carlos,3\r\n'}

Metadata

Release files for fake-read-write-files 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fake-read-write-files 0.10
File Size Uploaded Provenance Actions
fake_read_write_files-0.10.tar.gz 4.7 kB Without provenance Details for fake_read_write_files-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fake-read-write-files 0.10
File Interpreter ABI Platform Actions
fake_read_write_files-0.10-py3-none-any.whl Python 3 none any Details for fake_read_write_files-0.10-py3-none-any.whl

Total release size: 11.2 kB

Release files / fake_read_write_files-0.10.tar.gz

Download URL fake_read_write_files-0.10.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e2001765429c1d3d496058f5e47e86272e403fc86a420c52c501878167ddf6b5
BLAKE2b-256 checksum
How to use checksums
7cb750a04281cc201b6ef3ed51c0402f9cd530f720a90e900d2fc3d4f6ced364
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / fake_read_write_files-0.10-py3-none-any.whl

Download URL fake_read_write_files-0.10-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e9ed8faf9c5637da144187cb33e058487ff3b3bbadcc2e317e26359a0787e8a
BLAKE2b-256 checksum
How to use checksums
97b6b47c18c59831bb57dbb0a696bb32d76159f2950eae431149f5147d894b90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for fake-read-write-files 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page