ftmq.io
KeepOpen
Bases: RawIOBase
Pass writes through to a handle that outlives this wrapper.
The csv / pack statement writers wrap the output in a TextIOWrapper,
which closes what it wraps as soon as it is collected - tearing down a
shared stdout for whatever else runs in the same process. Closing the real
handle is smart_open's job.
Source code in ftmq/io.py
smart_read_proxies(uri, query=None, entity_type=ValueEntity, **store_kwargs)
Stream proxies from an arbitrary source
Example
from ftmq import Query, M
from ftmq.io import smart_read_proxies
# remote file-like source
for proxy in smart_read_proxies("s3://data/entities.ftm.json"):
print(proxy.schema)
# multiple files
for proxy in smart_read_proxies(["./1.json", "./2.json"]):
print(proxy.schema)
# nomenklatura store
for proxy in smart_read_proxies("redis://localhost", dataset="default"):
print(proxy.schema)
# apply a query to sql storage
q = Query().where(M(dataset="my_dataset"), M(schema="Person"))
for proxy in smart_read_proxies("sqlite:///data/ftm.db", query=q):
print(proxy.schema)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Uri | Iterable[Uri]
|
File-like uri or store uri or multiple uris |
required |
query
|
Query | None
|
Filter |
None
|
**store_kwargs
|
Any
|
Pass through configuration to statement store |
{}
|
Yields:
| Type | Description |
|---|---|
Entities
|
A generator of |
Source code in ftmq/io.py
smart_read_statements(uri, format=CSV, **store_kwargs)
Stream raw statements from a store or a statement stream file.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Uri
|
Store uri or file-like uri of a statement stream |
required |
format
|
str
|
Statement stream format ( |
CSV
|
**store_kwargs
|
Any
|
Pass through configuration to statement store |
{}
|
Yields:
| Type | Description |
|---|---|
Statements
|
A generator of |
Source code in ftmq/io.py
smart_write_proxies(uri, proxies, mode='wb', **store_kwargs)
Write a stream of proxies (or data dicts) to an arbitrary target.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Uri
|
File-like uri or store uri |
required |
proxies
|
Iterable[Entity]
|
Iterable of proxy data |
required |
mode
|
str | None
|
Open mode for file-like targets (default: |
'wb'
|
**store_kwargs
|
Any
|
Pass through configuration to statement store |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
Number of written proxies |
Source code in ftmq/io.py
smart_write_statements(uri, statements, format=CSV, **store_kwargs)
Write a stream of statements to a store or a statement stream file.
A statement is written as it comes in: its canonical_id is preserved
verbatim, never re-derived from the target store's linker (see
PreservingLinker), so a stream that
was resolved with nomenklatura apply-statements stays resolved. For a
SQL-family store the write is an upsert keyed on the statement id, so
loading a resolved dump back into the store it came from updates those rows
in place.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Uri
|
Store uri or file-like uri of a statement stream |
required |
statements
|
Iterable[Statement]
|
Iterable of |
required |
format
|
str
|
Statement stream format ( |
CSV
|
**store_kwargs
|
Any
|
Pass through configuration to statement store |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
Number of written statements |