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
importpandasaspdimportcv2fromPILimportImageimportnumpyasnpimportos.pathfromfake_read_write_filesimportread_decorator, write_decorator@read_decoratordefreadutf8(filename, _file_data):
withopen(filename, mode="r", encoding="utf-8") asf:
data=f.read()
returndata@write_decoratordefwrite_pil_image(pilpic, filepath):
pilpic.save(filepath)
# don't use "return" here, the function will return a dict@read_decoratordefread_bin_file(filename, _file_data):
withopen(filename, mode="rb") asf:
data=f.read()
returndata@read_decoratordefpandasread(filename, _file_data):
returnpd.read_csv(filename)
@write_decoratordefpandaswrite(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 kwarge=readutf8(
filename="f:\\txtdoesnotexist.txt", _file_data="I am fake\nDid you know that?"
)
print(e)
# real filebi=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 declaredo=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 IamfakeDidyouknowthat?
[[[255255255]
[255255255]
[255255255]
...
[255255255]
[254255255]
[253255255]]
[[255255255]
[255255255]
[255255255]
...
[255255255]
[254255255]
[253255255]]
[[255255255]
[255255255]
[255255255]
...
[255255255]
[254255255]
[253255255]]
...
[[255255255]
[255255255]
[255255255]
...
[255255254]
[255255254]
[255255254]]
[[255255255]
[255255255]
[255255255]
...
[255255254]
[255255254]
[255255254]]
[[255255255]
[255255255]
[255255255]
...
[255255254]
[255255254]
[255255254]]]
john10maria21carlos3
{'test.csv': ',john,1\r\n0,maria,2\r\n1,carlos,3\r\n'}
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.