anystore.io
Generic io helpers
anystore
is built on top of
fsspec
and
provides an easy wrapper for reading and writing content from and to arbitrary
locations using the io
command:
Command-line usage
Python usage
IOFormat
ModelWriter
Bases: Writer
A generic writer for pydantic objects to any out uri, either json or csv
Source code in anystore/io.py
Writer
A generic writer for python dict objects to any out uri, either json or csv
Source code in anystore/io.py
logged_items(items, action, chunk_size=10000, item_name=None, logger=None, **log_kwargs)
Log process of iterating items for io operations.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items
|
Iterable[T]
|
Sequence of any items |
required |
action
|
str
|
Action name to log |
required |
chunk_size
|
int | None
|
Log on every chunk_size |
10000
|
item_name
|
str | None
|
Name of item |
None
|
logger
|
Logger | BoundLogger | None
|
Specific logger to use |
None
|
Yields:
Type | Description |
---|---|
T
|
The input items |
Source code in anystore/io.py
smart_open(uri, mode=DEFAULT_MODE, **kwargs)
IO context similar to pythons built-in open()
.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_MODE
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Yields:
Type | Description |
---|---|
IO[AnyStr]
|
A generic file-handler like context object |
Source code in anystore/io.py
smart_read(uri, mode=DEFAULT_MODE, **kwargs)
Return content for a given file-like key directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_MODE
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Returns:
Type | Description |
---|---|
AnyStr
|
|
Source code in anystore/io.py
smart_stream(uri, mode=DEFAULT_MODE, **kwargs)
Stream content line by line.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_MODE
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Yields:
Type | Description |
---|---|
AnyStr
|
A generator of |
Source code in anystore/io.py
smart_stream_csv(uri, **kwargs)
Stream csv as python objects.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Yields:
Type | Description |
---|---|
SDictGenerator
|
A generator of |
Source code in anystore/io.py
smart_stream_csv_models(uri, model, **kwargs)
Stream csv as pydantic objects
Source code in anystore/io.py
smart_stream_data(uri, input_format, **kwargs)
Stream data objects loaded as dict from json or csv sources
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
input_format
|
Formats
|
csv or json |
required |
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Yields:
Type | Description |
---|---|
SDictGenerator
|
A generator of |
Source code in anystore/io.py
smart_stream_json(uri, mode=DEFAULT_MODE, **kwargs)
Stream line-based json as python objects.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_MODE
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Yields:
Type | Description |
---|---|
SDictGenerator
|
A generator of |
Source code in anystore/io.py
smart_stream_json_models(uri, model, **kwargs)
Stream json as pydantic objects
Source code in anystore/io.py
smart_stream_models(uri, model, input_format, **kwargs)
Stream json as pydantic objects
Source code in anystore/io.py
smart_write(uri, content, mode=DEFAULT_WRITE_MODE, **kwargs)
Write content to a given file-like key directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
content
|
bytes | str
|
|
required |
mode
|
str | None
|
open mode, default |
DEFAULT_WRITE_MODE
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Source code in anystore/io.py
smart_write_csv(uri, items, mode=DEFAULT_WRITE_MODE, **kwargs)
Write python data to csv
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
items
|
Iterable[SDict]
|
Iterable of dictionaries |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_WRITE_MODE
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Source code in anystore/io.py
smart_write_data(uri, items, mode=DEFAULT_WRITE_MODE, output_format='json', **kwargs)
Write python data to json or csv
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
items
|
Iterable[SDict]
|
Iterable of dictionaries |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_WRITE_MODE
|
output_format
|
Formats | None
|
csv or json (default: json) |
'json'
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Source code in anystore/io.py
smart_write_json(uri, items, mode=DEFAULT_WRITE_MODE, **kwargs)
Write python data to json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
items
|
Iterable[SDict]
|
Iterable of dictionaries |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_WRITE_MODE
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Source code in anystore/io.py
smart_write_model(uri, obj, mode=DEFAULT_WRITE_MODE, output_format='json', clean=False, **kwargs)
Write a single pydantic object to the target
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
obj
|
BaseModel
|
Pydantic object |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_WRITE_MODE
|
clean
|
bool | None
|
Apply clean_dict |
False
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|
Source code in anystore/io.py
smart_write_models(uri, objects, mode=DEFAULT_WRITE_MODE, output_format='json', clean=False, **kwargs)
Write pydantic objects to json lines or csv
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
string or path-like key uri to open, e.g. |
required |
objects
|
Iterable[BaseModel]
|
Iterable of pydantic objects |
required |
mode
|
str | None
|
open mode, default |
DEFAULT_WRITE_MODE
|
clean
|
bool | None
|
Apply clean_dict |
False
|
**kwargs
|
Any
|
pass through storage-specific options |
{}
|