# 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
```python
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'}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/fake_read_write_files",
"name": "fake-read-write-files",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "read,write,memory,open,faster",
"author": "Johannes Fischer",
"author_email": "<aulasparticularesdealemaosp@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/7c/b7/50a04281cc201b6ef3ed51c0402f9cd530f720a90e900d2fc3d4f6ced364/fake_read_write_files-0.10.tar.gz",
"platform": null,
"description": "\n# Write/read from memory instead of files when open() is called \n\n## @read_decorator fakes the existence of a file and provides the file content when open(mode='r'/mode='rb') from the builtins is called. \n## @write_decorator captures the output when open(mode='w'/mode='wb') from the builtins is called. \n\n#### The decorators don't work with functions/methods that don't use the open() function (for example: cv2.imread / cv2.imwrite)\n\n### Some examples \n```python\nimport pandas as pd\nimport cv2\nfrom PIL import Image\nimport numpy as np\nimport os.path\nfrom fake_read_write_files import read_decorator, write_decorator\n\n@read_decorator\ndef readutf8(filename, _file_data):\n with open(filename, mode=\"r\", encoding=\"utf-8\") as f:\n data = f.read()\n return data\n\n\n@write_decorator\ndef write_pil_image(pilpic, filepath):\n pilpic.save(filepath)\n # don't use \"return\" here, the function will return a dict\n\n\n@read_decorator\ndef read_bin_file(filename, _file_data):\n with open(filename, mode=\"rb\") as f:\n data = f.read()\n return data\n\n\n@read_decorator\ndef pandasread(filename, _file_data):\n return pd.read_csv(filename)\n\n\n@write_decorator\ndef pandaswrite(df, filename):\n df.to_csv(filename)\n # don't use \"return\" here, the function will return a dict\n\n\n# the read decorator always checks for the kwarg \"_file_data\"\n# It must be passed as a kwarg\ne = readutf8(\n filename=\"f:\\\\txtdoesnotexist.txt\", _file_data=\"I am fake\\nDid you know that?\"\n)\nprint(e)\n\n# real file\nbi = Image.open(r\"C:\\Users\\Gamer\\anaconda3\\envs\\dfdir\\xxxxxxxxxx.png\")\n\n# writing to a fake file, returns a dict with all written files in the function,\n# even if there is no return value declared\no = write_pil_image(bi, filepath=\"i_am_a_fake_image.png\")\nprint(\n cv2.imdecode(np.frombuffer(o[\"i_am_a_fake_image.png\"], np.uint8), cv2.IMREAD_COLOR)\n)\n\n\nbinaryfile = read_bin_file(\n filename=\"i_am_a_fake_image.png\", _file_data=o[\"i_am_a_fake_image.png\"]\n)\n\n\ndf = pandasread(filename=\"test.csv\", _file_data=\"john,1\\nmaria,2\\ncarlos,3\")\nprint(df)\n\npdcsv = pandaswrite(df, filename=\"test.csv\")\nprint(pdcsv)\n\n\n# output \nI am fake\nDid you know that?\n[[[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [254 255 255]\n [253 255 255]]\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [254 255 255]\n [253 255 255]]\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [254 255 255]\n [253 255 255]]\n ...\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 254]\n [255 255 254]\n [255 255 254]]\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 254]\n [255 255 254]\n [255 255 254]]\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 254]\n [255 255 254]\n [255 255 254]]]\n john 1\n0 maria 2\n1 carlos 3\n{'test.csv': ',john,1\\r\\n0,maria,2\\r\\n1,carlos,3\\r\\n'}\n\n\n```\n\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Write/read from memory instead of files when open() is called",
"version": "0.10",
"split_keywords": [
"read",
"write",
"memory",
"open",
"faster"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "7fa1fd0c66b903c439020a9f42af7490",
"sha256": "0e9ed8faf9c5637da144187cb33e058487ff3b3bbadcc2e317e26359a0787e8a"
},
"downloads": -1,
"filename": "fake_read_write_files-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7fa1fd0c66b903c439020a9f42af7490",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6514,
"upload_time": "2022-12-29T06:21:58",
"upload_time_iso_8601": "2022-12-29T06:21:58.876319Z",
"url": "https://files.pythonhosted.org/packages/97/b6/b47c18c59831bb57dbb0a696bb32d76159f2950eae431149f5147d894b90/fake_read_write_files-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "a50126f2fbb1c6e668c556ddff86104e",
"sha256": "e2001765429c1d3d496058f5e47e86272e403fc86a420c52c501878167ddf6b5"
},
"downloads": -1,
"filename": "fake_read_write_files-0.10.tar.gz",
"has_sig": false,
"md5_digest": "a50126f2fbb1c6e668c556ddff86104e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4659,
"upload_time": "2022-12-29T06:22:00",
"upload_time_iso_8601": "2022-12-29T06:22:00.220960Z",
"url": "https://files.pythonhosted.org/packages/7c/b7/50a04281cc201b6ef3ed51c0402f9cd530f720a90e900d2fc3d4f6ced364/fake_read_write_files-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-29 06:22:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "fake_read_write_files",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "fake-read-write-files"
}