# safeIO
> Safely make I/O operations to files in Python even from multiple threads... and more!
# Table of Content
1. [safeIO](#safeio)
2. [Table of Content](#table-of-content)
3. [What is it?](#what-is-it)
4. [Installation](#installation)
5. [Objects](#objects)
- [TextFile()](#textfile)
- [BinaryFile()](#binaryfile)
- [JSONFile()](#jsonfile)
6. [Tips](#tips)
# What is it?
It's a module which lets you manage your files (most of the time, Input/Output operations) without worrying about accessing the file from two simultaneously.
Some functions may help you managing your files more easily as they are intuitive and things like substractions (TextFile object - TextFile Object returns the Cosine Similarity of the two files), equality (Object == Object), iteration (for line in TextFile object), the rename/move/delete methods are made easier!.
# Installation
**From PIP**
```sh
pip install safeIO --upgrade
```
# Objects
## TextFile(filepath, encoding="utf-8", blocking=True)
> A Text File object
### isfile
*isfile(callback=None)*
> Wether the file exists on the disk or not
### delete
*delete(callback=None)*
> Deletes the file
### rename
*rename(newName,overwrite=False,callback=None)*
> Renames the file and returns its new path
### move
*move(newPath,overwrite=False,callback=None)*
> Moves the file and returns its new path
### name
*name(callback=None)*
> Returns the file name
### fileno
*fileno(callback=None)*
> Returns the file descriptor (int) used by Python to request I/O operations from the operating system.
### read
*read(position=0,callback=None)*
> Reads the entire file and returns its content
### write
*write(data,position=0,callback=None)*
> Writes (or overwrites) to the file and returns the number of characters written
### append
*append(data,callback=None)*
> Appends to the file and returns the number of characters written
### readline
*readline(position=0,callback=None)*
> Returns the line of the current position (from the position to the linebreak)
### readlines
*readlines(position=0,callback=None)*
> Reads the whole file and returns the lines (separated by a line break)
### writelines
*writelines(data,position=0,callback=None)*
> Writes (or overwrites) the given list of lines to the file
### appendlines
*appendlines(data,callback=None)*
> Appends the given list of lines to the file
### detach
*detach(mode="r",callback=None)*
> Returns the opened IO (TextIOWrapper)
>
**Warning: Make sure to close the file correctly after using the file with detach**
---
## BinaryFile(filepath, blocking=True)
> A Binary File object
### isfile
*isfile(callback=None)*
> Wether the file exists on the disk or not
### delete
*delete(callback=None)*
> Deletes the file
### rename
*rename(newName,overwrite=False,callback=None)*
> Renames the file and returns its new path
### move
*move(newPath,overwrite=False,callback=None)*
> Moves the file and returns its new path
### name
*name(callback=None)*
> Returns the file name
### fileno
*fileno(callback=None)*
> Returns the file descriptor (int) used by Python to request I/O operations from the operating system.
### read
*read(position=0,callback=None)*
> Reads the entire file and returns its content
### write
*write(data,position=0,callback=None)*
> Writes (or overwrites) to the file and returns the number of bytes written
### append
*append(data,callback=None)*
> Appends to the file and returns the number of bytes written
### readline
*readline(position=0,callback=None)*
> Returns the line of the current position (from the position to the linebreak)
### readlines
*readlines(position=0,callback=None)*
> Reads the whole file and returns the lines (separated by a line break)
### writelines
*writelines(data,position=0,callback=None)*
> Writes (or overwrites) the given list of lines to the file
### appendlines
*appendlines(data,callback=None)*
> Appends the given list of lines to the file
### detach
*detach(mode="rb",callback=None)*
> Returns the opened IO (TextIOWrapper)
>
> Tips: Make sure to include the "b" access mode in the mode\n
**Warning: Make sure to close the file correctly after using the file with detach**
---
## JSONFile(filepath, ensure_ascii=False, minify=False, indent=4, separators=(', ', ': '), encoding="utf-8", blocking=True)
> A JSON File object
### isfile
*isfile(callback=None)*
> Wether the file exists on the disk or not
### delete
*delete(callback=None)*
> Deletes the file
### rename
*rename(newName,overwrite=False,callback=None)*
> Renames the file and returns its new path
### move
*move(newPath,overwrite=False,callback=None)*
> Moves the file and returns its new path
### name
*name(callback=None)*
> Returns the file name
### fileno
*fileno(callback=None)*
> Returns the file descriptor (int) used by Python to request I/O operations from the operating system.
### read
*read(position=0,callback=None)*
> Reads the entire file and returns its content
### write
*write(data,position=0,callback=None)*
> Writes (or overwrites) to the file and returns the number of characters written
### append
*append(data,callback=None)*
> Appends to the file and returns the number of characters written
### detach
*detach(mode="r",callback=None)*
> Returns the opened IO (TextIOWrapper)
**Warning: Make sure to close the file correctly after using the file with detach**
# Tips
- You can temporarily make the operations blocking with the "with" statement like so:
```python
from safeIO import TextFile
f = TextFile("example.txt", blocking=False)
print(f.read()) # prints "None"
with f:
print(f.read()) # prints the content of example.txt
with f as reading_file:
print(reading_file.read()) # prints the content of example.txt
```
- Try to define the safeIO object at the top of your script and use the same object for all of the operations to the file with:
```python
from safeIO import JSONFile
data_file = JSONFile("data.json", minify=True, blocking=False)
# do a bunch of stuff
data_file.write({"type": "new_data"})
# do more stuff
with data_file as data:
new = data.read()
new["type"] = "new!"
data_file.write(new)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Animenosekai/safeIO",
"name": "safeIO",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "file,io,thread-safe,thread,management,file-management,animenosekai,threading",
"author": "Anime no Sekai",
"author_email": "niichannomail@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/23/2f/00e4f435cc49f1b600807356e17ec7625f011a5ae86d8b893ab8ddb5bd98/safeIO-1.2.tar.gz",
"platform": "",
"description": "# safeIO \n\n> Safely make I/O operations to files in Python even from multiple threads... and more! \n\n# Table of Content \n\n1. [safeIO](#safeio)\n2. [Table of Content](#table-of-content)\n3. [What is it?](#what-is-it)\n4. [Installation](#installation)\n5. [Objects](#objects)\n - [TextFile()](#textfile)\n - [BinaryFile()](#binaryfile)\n - [JSONFile()](#jsonfile)\n6. [Tips](#tips)\n\n\n# What is it?\nIt's a module which lets you manage your files (most of the time, Input/Output operations) without worrying about accessing the file from two simultaneously.\n\nSome functions may help you managing your files more easily as they are intuitive and things like substractions (TextFile object - TextFile Object returns the Cosine Similarity of the two files), equality (Object == Object), iteration (for line in TextFile object), the rename/move/delete methods are made easier!.\n\n# Installation\n**From PIP**\n```sh\npip install safeIO --upgrade\n```\n\n\n# Objects\n## TextFile(filepath, encoding=\"utf-8\", blocking=True) \n\n> A Text File object \n\n\n### isfile \n\n*isfile(callback=None)* \n\n> Wether the file exists on the disk or not \n\n### delete \n\n*delete(callback=None)* \n\n> Deletes the file \n\n### rename \n\n*rename(newName,overwrite=False,callback=None)* \n\n> Renames the file and returns its new path \n\n### move \n\n*move(newPath,overwrite=False,callback=None)* \n\n> Moves the file and returns its new path \n\n### name \n\n*name(callback=None)* \n\n> Returns the file name \n\n### fileno \n\n*fileno(callback=None)* \n\n> Returns the file descriptor (int) used by Python to request I/O operations from the operating system. \n\n### read \n\n*read(position=0,callback=None)* \n\n> Reads the entire file and returns its content \n\n### write \n\n*write(data,position=0,callback=None)* \n\n> Writes (or overwrites) to the file and returns the number of characters written \n\n### append \n\n*append(data,callback=None)* \n\n> Appends to the file and returns the number of characters written \n\n### readline \n\n*readline(position=0,callback=None)* \n\n> Returns the line of the current position (from the position to the linebreak) \n\n### readlines \n\n*readlines(position=0,callback=None)* \n\n> Reads the whole file and returns the lines (separated by a line break) \n\n### writelines \n\n*writelines(data,position=0,callback=None)* \n\n> Writes (or overwrites) the given list of lines to the file \n\n### appendlines \n\n*appendlines(data,callback=None)* \n\n> Appends the given list of lines to the file \n\n### detach \n\n*detach(mode=\"r\",callback=None)* \n\n> Returns the opened IO (TextIOWrapper) \n\n> \n\n**Warning: Make sure to close the file correctly after using the file with detach**\n\n--- \n\n## BinaryFile(filepath, blocking=True) \n\n> A Binary File object \n\n### isfile \n\n*isfile(callback=None)* \n\n> Wether the file exists on the disk or not \n\n### delete \n\n*delete(callback=None)* \n\n> Deletes the file \n\n### rename \n\n*rename(newName,overwrite=False,callback=None)* \n\n> Renames the file and returns its new path \n\n### move \n\n*move(newPath,overwrite=False,callback=None)* \n\n> Moves the file and returns its new path \n\n### name \n\n*name(callback=None)* \n\n> Returns the file name \n\n### fileno \n\n*fileno(callback=None)* \n\n> Returns the file descriptor (int) used by Python to request I/O operations from the operating system. \n\n### read \n\n*read(position=0,callback=None)* \n\n> Reads the entire file and returns its content \n\n### write \n\n*write(data,position=0,callback=None)* \n\n> Writes (or overwrites) to the file and returns the number of bytes written \n\n### append \n\n*append(data,callback=None)* \n\n> Appends to the file and returns the number of bytes written \n\n### readline \n\n*readline(position=0,callback=None)* \n\n> Returns the line of the current position (from the position to the linebreak) \n\n### readlines \n\n*readlines(position=0,callback=None)* \n\n> Reads the whole file and returns the lines (separated by a line break) \n\n### writelines \n\n*writelines(data,position=0,callback=None)* \n\n> Writes (or overwrites) the given list of lines to the file \n\n### appendlines \n\n*appendlines(data,callback=None)* \n\n> Appends the given list of lines to the file \n\n### detach \n\n*detach(mode=\"rb\",callback=None)* \n\n> Returns the opened IO (TextIOWrapper) \n\n> \n\n> Tips: Make sure to include the \"b\" access mode in the mode\\n \n\n**Warning: Make sure to close the file correctly after using the file with detach**\n\n--- \n\n## JSONFile(filepath, ensure_ascii=False, minify=False, indent=4, separators=(', ', ': '), encoding=\"utf-8\", blocking=True) \n\n> A JSON File object \n\n### isfile \n\n*isfile(callback=None)* \n\n> Wether the file exists on the disk or not \n\n### delete \n\n*delete(callback=None)* \n\n> Deletes the file \n\n### rename \n\n*rename(newName,overwrite=False,callback=None)* \n\n> Renames the file and returns its new path \n\n### move \n\n*move(newPath,overwrite=False,callback=None)* \n\n> Moves the file and returns its new path \n\n### name \n\n*name(callback=None)* \n\n> Returns the file name \n\n### fileno \n\n*fileno(callback=None)* \n\n> Returns the file descriptor (int) used by Python to request I/O operations from the operating system. \n\n### read \n\n*read(position=0,callback=None)* \n\n> Reads the entire file and returns its content \n\n### write \n\n*write(data,position=0,callback=None)* \n\n> Writes (or overwrites) to the file and returns the number of characters written \n\n### append \n\n*append(data,callback=None)* \n\n> Appends to the file and returns the number of characters written \n\n### detach \n\n*detach(mode=\"r\",callback=None)* \n\n> Returns the opened IO (TextIOWrapper) \n\n**Warning: Make sure to close the file correctly after using the file with detach**\n\n\n\n# Tips\n- You can temporarily make the operations blocking with the \"with\" statement like so:\n\n```python\nfrom safeIO import TextFile\n\nf = TextFile(\"example.txt\", blocking=False)\nprint(f.read()) # prints \"None\"\nwith f:\n print(f.read()) # prints the content of example.txt\nwith f as reading_file:\n print(reading_file.read()) # prints the content of example.txt\n```\n\n- Try to define the safeIO object at the top of your script and use the same object for all of the operations to the file with:\n\n```python\nfrom safeIO import JSONFile\ndata_file = JSONFile(\"data.json\", minify=True, blocking=False)\n\n# do a bunch of stuff\ndata_file.write({\"type\": \"new_data\"})\n\n# do more stuff\nwith data_file as data:\n new = data.read()\nnew[\"type\"] = \"new!\"\ndata_file.write(new)\n```",
"bugtrack_url": null,
"license": "MIT",
"summary": "Safely make I/O operations to files in Python even from multiple threads... and more!",
"version": "1.2",
"split_keywords": [
"file",
"io",
"thread-safe",
"thread",
"management",
"file-management",
"animenosekai",
"threading"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "a08b29da46c990f02a5242d6c70d1dca",
"sha256": "d480a6dab01a390ebc24c12d6b774ad00cef3db5348ad07d8bd11d272a808cd3"
},
"downloads": -1,
"filename": "safeIO-1.2.tar.gz",
"has_sig": false,
"md5_digest": "a08b29da46c990f02a5242d6c70d1dca",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7983,
"upload_time": "2021-02-23T21:12:49",
"upload_time_iso_8601": "2021-02-23T21:12:49.569370Z",
"url": "https://files.pythonhosted.org/packages/23/2f/00e4f435cc49f1b600807356e17ec7625f011a5ae86d8b893ab8ddb5bd98/safeIO-1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-02-23 21:12:49",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Animenosekai",
"github_project": "safeIO",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "safeio"
}