aiofile (async filesys operations) with a simple (dict-like or list-like) interface
Project description
aiofiledol
aiofile (async filesys operations) with a simple (dict-like or list-like) interface
To install: pip install aiofiledol
Get the bytes contents of the file k.
>>> import os
>>> from aiofiledol import AioFileBytesReader
>>> filepath = __file__
>>> dirpath = os.path.dirname(__file__) # path of the directory where I (the module file) am
>>> s = AioFileBytesReader(dirpath, max_levels=0)
>>>
>>> ####### Get the first 9 characters (as bytes) of this module #####################
>>> t = await s.aget(filepath)
>>> t[:14]
b'import asyncio'
>>>
>>> ####### Test key validation #####################
>>> await s.aget('not_a_valid_key') # this key is not valid since not under the dirpath folder
Traceback (most recent call last):
...
filesys.KeyValidationError: 'Key not valid (usually because does not exist or access not permitted): not_a_valid_key'
>>>
>>> ####### Test further exceptions (that should be wrapped in KeyError) #####################
>>> # this key is valid, since under dirpath, but the file itself doesn't exist (hopefully for this test)
>>> non_existing_file = os.path.join(dirpath, 'non_existing_file')
>>> try:
... await s.aget(non_existing_file)
... except KeyError:
... print("KeyError (not FileNotFoundError) was raised.")
KeyError (not FileNotFoundError) was raised.
Set the contents of file k
to be some bytes.
>>> from aiofiledol import AioFileBytesPersister
>>> from dol.filesys import mk_tmp_dol_dir
>>> import os
>>>
>>> rootdir = mk_tmp_dol_dir('test')
>>> rpath = lambda *p: os.path.join(rootdir, *p)
>>> s = AioFileBytesPersister(rootdir)
>>> k = rpath('foo')
>>> if k in s:
... del s[k] # delete key if present
...
>>> n = len(s) # number of items in store
>>> await s.asetitem(k, b'bar')
>>> assert len(s) == n + 1 # there's one more item in store
>>> assert k in s
>>> assert (await s[k]) == b'bar'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiofiledol-0.0.3.tar.gz
(2.9 kB
view details)
Built Distribution
File details
Details for the file aiofiledol-0.0.3.tar.gz
.
File metadata
- Download URL: aiofiledol-0.0.3.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f252bee7a6d8bd47b14f0ac675a3d0676247144b971e428cc93d6f75deba86f5 |
|
MD5 | 80d64aebf6f5f41a8fb48a580c055585 |
|
BLAKE2b-256 | 6492fe4aa093d013d0efe48514df8d89d248aaece66fd1d0f7cfa57e6b1216be |
File details
Details for the file aiofiledol-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: aiofiledol-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80ec856a9ec6cadead4be4be827b303a69b9d52f5d13e283de644239c869b08d |
|
MD5 | 746f10926a1e8e46e661447ce382d15b |
|
BLAKE2b-256 | 8f25d428b33b57278e7634c7db8a9effe30bf9f627871e7a6f31ddf3ad2a4c57 |