json_autoarray
Write JSON-serializable python objects to file as JSON array.
Raises a SerializationError if you send it an object that cannot be serialized by whatever json module you are using..
Tries to use python-cjson (python2.x only) or UltraJSON for serializing objects, because you wouldn't be needing a silly thing like this if you weren't handling big old objects.
Objects successfully sent to the writer are always contained in an array. JSONAutoArray will always attempt to close the array upon file closure, regardless of any exceptions which may lead to a premature end of your process.
Why?
Suppose you are making with the ETL, and are pulling objects from a janky stream. Should your stream close prematurely, or send you some sort of madness, JSONAutoArray will throw out the bad object, and close the array before closing the file.
Installation
Install using pip:
pip install JSONAutoArray
Usage
import random
import logging
from tempfile import TemporaryDirectory
from json_autoarray import JSONAutoArray
from pathlib import Path
objects = [
{"this": "that"},
["the", "other"],
{"hippie": "joe"},
{"facist": {"bullyboy": ["me", "you", "them"]}},
list(set(["a", "a", "b"])),
list(range(3)),
]
# open with pathlib.Path
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(Path(tempdir, "foo.json")) as writer:
for obj in objects:
writer.write(obj)
# open with string
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(os.path.join(tempdir, "foo.json")) as writer:
for obj in objects:
writer.write(obj)
# will fail in python3 if file opened as binary!
logging.warning("Expect TypeError for 'wb'")
with TemporaryDirectory() as tempdir:
f = open(Path(tempdir, "bar.json"), "wb")
try:
with JSONAutoArray.ArrayWriter(f) as writer:
for obj in objects:
writer.write(obj)
except TypeError as error:
logging.warning(error)
# use encoding
with TemporaryDirectory() as tempdir:
f = open(Path(tempdir, "bar.json", encoding="utf-8"), "w")
try:
with JSONAutoArray.ArrayWriter(f) as writer:
for obj in objects:
writer.write(obj)
except TypeError as error:
logging.warning(error)
logging.warning("Expect Serialization Error for lambda object.")
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(Path(tempdir, "foo.json")) as writer:
try:
writer.write(lambda x: "foo")
except JSONAutoArray.SerializationError as error:
logging.warning(error)
# write ten thousand random flabberdabs
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(Path(tempdir, "flabberdabs.json")) as writer:
try:
for i in range(10000):
writer.write([{"flabberdab": random.randint(1, 1000)}])
except JSONAutoArray.SerializationError as error:
logging.warning(error)
# close array on StopIteration error
def rando_gen():
for i in range(100):
yield {
"".join(
[chr(random.randint(1, 100)) for i in range(5)]
): random.randint(1, 100)
}
logging.warning("Expect uncaught StopIteration")
with TemporaryDirectory() as tempdir:
quux = Path(tempdir, "quux.json")
with JSONAutoArray.ArrayWriter(quux) as writer:
try:
rg = rando_gen()
while True:
if sys.version_info[0] < 3:
writer.write(rg.next())
else:
writer.write(next(rg))
except JSONAutoArray.SerializationError as s_error:
logging.warning(s_error)
except StopIteration as i_error:
logging.warning("%s - %s", i_error, "Hello StopIteration")
Metadata
Release files for JSONAutoArray 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| JSONAutoArray-1.0.4.tar.gz | 4.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| JSONAutoArray-1.0.4-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / JSONAutoArray-1.0.4.tar.gz
| Download URL | JSONAutoArray-1.0.4.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e25ad96bed246b36d13b9a13bac0f53f1608e90f19a55332cd5824261a2ce90d
|
|
BLAKE2b-256 checksum How to use checksums |
157a718b5b27fea07a5601cc28ff85aacdafaab885748e8701b67bb191f0cba7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3
|
Release files / JSONAutoArray-1.0.4-py2.py3-none-any.whl
| Download URL | JSONAutoArray-1.0.4-py2.py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
aca8bd38fba4f63046da2f54327741cf9acc9a98f4c57ad8d14cf1a9cdc95cb2
|
|
BLAKE2b-256 checksum How to use checksums |
9326d080543a091951cc725af446d5baddae2c565d4439cd08d8bda5506463c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3
|
Security for JSONAutoArray 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.