Skip to main content

Modern high-performance serialization utilities for Python

Project description

srsly: Modern high-performance serialization utilities for Python

This package bundles some of the best Python serialization libraries into one standalone package, with a high-level API that makes it easy to write code that's correct across platforms and Pythons. This allows us to provide all the serialization utilities we need in a single binary wheel.

Azure Pipelines PyPi conda GitHub Python wheels

Motivation

Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.

At the same time, we noticed that having a lot of small dependencies was making maintenance harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.

srsly currently includes forks of the following packages:

Installation

srsly can be installed from pip:

pip install srsly

Or from conda via conda-forge:

conda install -c conda-forge srsly

Alternatively, you can also compile the library from source. You'll need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler (XCode command-line tools on macOS / OS X or Visual C++ build tools on Windows), pip, virtualenv and git installed.

pip install -r requirements.txt  # install development dependencies
python setup.py build_ext --inplace  # compile the library

API

JSON

📦 The underlying module is exposed via srsly.ujson. However, we normally interact with it via the utility functions only.

function srsly.json_dumps

Serialize an object to a JSON string. Takes care of Python 2/3 compatibility and falls back to json if sort_keys=True is used (until it's fixed in ujson).

data = {"foo": "bar", "baz": 123}
json_string = srsly.json_dumps(data)
Argument Type Description
data - The JSON-serializable data to output.
indent int Number of spaces used to indent JSON. Defaults to 0.
sort_keys bool Sort dictionary keys. Defaults to False.
RETURNS unicode The serialized string.

function srsly.json_loads

Deserialize unicode or bytes to a Python object.

data = '{"foo": "bar", "baz": 123}'
obj = srsly.json_loads(data)
Argument Type Description
data unicode / bytes The data to deserialize.
RETURNS - The deserialized Python object.

function srsly.write_json

Create a JSON file and dump contents or write to standard output.

data = {"foo": "bar", "baz": 123}
srsly.write_json("/path/to/file.json", data)
Argument Type Description
location unicode / Path The file path or "-" to write to stdout.
data - The JSON-serializable data to output.
indent int Number of spaces used to indent JSON. Defaults to 2.

function srsly.read_json

Load JSON from a file or standard input.

data = srsly.read_json("/path/to/file.json")
Argument Type Description
location unicode / Path The file path or "-" to read from stdin.
RETURNS dict / list The loaded JSON content.

function srsly.write_gzip_json

Create a gzipped JSON file and dump contents.

data = {"foo": "bar", "baz": 123}
srsly.write_gzip_json("/path/to/file.json.gz", data)
Argument Type Description
location unicode / Path The file path.
data - The JSON-serializable data to output.
indent int Number of spaces used to indent JSON. Defaults to 2.

function srsly.read_gzip_json

Load gzipped JSON from a file.

data = srsly.read_gzip_json("/path/to/file.json.gz")
Argument Type Description
location unicode / Path The file path.
RETURNS dict / list The loaded JSON content.

function srsly.write_jsonl

Create a JSONL file (newline-delimited JSON) and dump contents line by line, or write to standard output.

data = [{"foo": "bar"}, {"baz": 123}]
srsly.write_jsonl("/path/to/file.jsonl", data)
Argument Type Description
location unicode / Path The file path or "-" to write to stdout.
lines iterable The JSON-serializable lines.
append bool Append to an existing file. Will open it in "a" mode and insert a newline before writing lines. Defaults to False.
append_new_line bool Defines whether a new line should first be written when appending to an existing file. Defaults to True.

function srsly.read_jsonl

Read a JSONL file (newline-delimited JSON) or from JSONL data from standard input and yield contents line by line. Blank lines will always be skipped.

data = srsly.read_jsonl("/path/to/file.jsonl")
Argument Type Description
location unicode / Path The file path or "-" to read from stdin.
skip bool Skip broken lines and don't raise ValueError. Defaults to False.
YIELDS - The loaded JSON contents of each line.

function srsly.is_json_serializable

Check if a Python object is JSON-serializable.

assert srsly.is_json_serializable({"hello": "world"}) is True
assert srsly.is_json_serializable(lambda x: x) is False
Argument Type Description
obj - The object to check.
RETURNS bool Whether the object is JSON-serializable.

msgpack

📦 The underlying module is exposed via srsly.msgpack. However, we normally interact with it via the utility functions only.

function srsly.msgpack_dumps

Serialize an object to a msgpack byte string.

data = {"foo": "bar", "baz": 123}
msg = srsly.msgpack_dumps(data)
Argument Type Description
data - The data to serialize.
RETURNS bytes The serialized bytes.

function srsly.msgpack_loads

Deserialize msgpack bytes to a Python object.

msg = b"\x82\xa3foo\xa3bar\xa3baz{"
data = srsly.msgpack_loads(msg)
Argument Type Description
data bytes The data to deserialize.
use_list bool Don't use tuples instead of lists. Can make deserialization slower. Defaults to True.
RETURNS - The deserialized Python object.

function srsly.write_msgpack

Create a msgpack file and dump contents.

data = {"foo": "bar", "baz": 123}
srsly.write_msgpack("/path/to/file.msg", data)
Argument Type Description
location unicode / Path The file path.
data - The data to serialize.

function srsly.read_msgpack

Load a msgpack file.

data = srsly.read_msgpack("/path/to/file.msg")
Argument Type Description
location unicode / Path The file path.
use_list bool Don't use tuples instead of lists. Can make deserialization slower. Defaults to True.
RETURNS - The loaded and deserialized content.

pickle

📦 The underlying module is exposed via srsly.cloudpickle. However, we normally interact with it via the utility functions only.

function srsly.pickle_dumps

Serialize a Python object with pickle.

data = {"foo": "bar", "baz": 123}
pickled_data = srsly.pickle_dumps(data)
Argument Type Description
data - The object to serialize.
protocol int Protocol to use. -1 for highest. Defaults to None.
RETURNS bytes The serialized object.

function srsly.pickle_loads

Deserialize bytes with pickle.

pickled_data = b"\x80\x04\x95\x19\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x03foo\x94\x8c\x03bar\x94\x8c\x03baz\x94K{u."
data = srsly.pickle_loads(pickled_data)
Argument Type Description
data bytes The data to deserialize.
RETURNS - The deserialized Python object.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

srsly-1.0.4.tar.gz (193.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

srsly-1.0.4-cp39-cp39-win_amd64.whl (287.2 kB view details)

Uploaded CPython 3.9Windows x86-64

srsly-1.0.4-cp39-cp39-manylinux2014_x86_64.whl (293.5 kB view details)

Uploaded CPython 3.9

srsly-1.0.4-cp39-cp39-macosx_10_9_x86_64.whl (289.3 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

srsly-1.0.4-cp38-cp38-win_amd64.whl (287.5 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-1.0.4-cp38-cp38-manylinux2014_x86_64.whl (295.8 kB view details)

Uploaded CPython 3.8

srsly-1.0.4-cp38-cp38-macosx_10_9_x86_64.whl (287.2 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-1.0.4-cp37-cp37m-win_amd64.whl (285.8 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-1.0.4-cp37-cp37m-manylinux2014_x86_64.whl (293.6 kB view details)

Uploaded CPython 3.7m

srsly-1.0.4-cp37-cp37m-macosx_10_9_x86_64.whl (286.5 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-1.0.4-cp36-cp36m-win_amd64.whl (285.8 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-1.0.4-cp36-cp36m-manylinux2014_x86_64.whl (293.6 kB view details)

Uploaded CPython 3.6m

srsly-1.0.4-cp36-cp36m-macosx_10_9_x86_64.whl (287.9 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

Details for the file srsly-1.0.4.tar.gz.

File metadata

  • Download URL: srsly-1.0.4.tar.gz
  • Upload date:
  • Size: 193.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9ca5633a5303ce0d0b84d1bdb6d029f665ba2b7d320f5482525b125ddfb8a390
MD5 f79f9b4f9154abb8f00aaa1371615e96
BLAKE2b-256 d1615a503487f711f42136a3a0986ac1bb46b2c96134634332238cc003e78a05

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 287.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2ae6327e013934126f2b7082ce36284785eed951635ac99c73e39de75ecd1345
MD5 238aa109b684cc2e2a095107218a2e02
BLAKE2b-256 d1dd833df39c98aa2bc01ceeb98966ac7e8957d6ae40d31cf028dbd42a615afc

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 293.5 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 713766bf4289ebe6f21d1e5203b6b75f71fd4512e877dbe5a3f660b4a51d77ac
MD5 417185077f2ffa745f70f7631cbbb65d
BLAKE2b-256 467da56b536fae4715817ff8051716eef813352e40b4d5539b7701cfea2fc962

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 289.3 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f7aee40cd66f190e7694d7e7c83be018b1a7b9546a305db52c8793d93b1d0a7e
MD5 87cc7a80d080f71977ce2ff9b8ba2363
BLAKE2b-256 ed3bba24772f87a6b9879e0f883c1bec2ebd9a6efb07f6d1ab62d39b8b112bf7

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 287.5 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 3f46e5efbbb0e857f302394257539aae4487c201cdafc3519db4bde897341fee
MD5 03362406a671cf680f1a6fa2ef1e7001
BLAKE2b-256 478eff3842903eb93bff64a1ae9f9b44664a18c5e862a8ecbe123c1a40c673c3

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 295.8 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2b9e2b7c00e522d6e54952564d702e72bc4729cc828e27a01b4dac70b870d44a
MD5 22eddf80216220d76c31eb1cf6f51e84
BLAKE2b-256 e27ab18b4d9506a2e1414bdcf526d44720c03dc576207fb3103425bb6457d718

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 287.2 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 660b80ffa3ea4ef9db254e5a22d2f52294f15342910c3274844a8f5a81369cbc
MD5 b40369744cb49c9ad4179edfb8da6ed8
BLAKE2b-256 a8a5f3ba047b9898247a48648abe9c089bde8a71dec961ce7dfc7bf039de1e72

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 285.8 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 4b8bd311fa20fa5d1a0deac0dd8f3c43c7ca23df2c483a7102d96acf2cddb506
MD5 64512fc4d4856b771e2b96c0acced95b
BLAKE2b-256 415876032df5afe0774d677e07014afc835b6ce0cfc2edcd16b73008125d7648

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 293.6 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 deee259837443ea8a4c194967f56eb62809c3cf3a11cd9d4f425de4aea5ecb81
MD5 d0fdecf88c21cc03637586e998eece02
BLAKE2b-256 2500cb908056dbc796f2f68b80e9a8cf5119103c685a26ecbbcf474a6de25f56

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 286.5 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b4fb0ff57e25a99e9ac9a61b262478efe8493b3cff0f50812c09534c76688b0b
MD5 2527aac1c25486014b54de74246a51da
BLAKE2b-256 06c106d598af404bcecd1d6ab865181034e28831e67488a3afb7f68a1174afd6

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 285.8 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 c1fa001bbd87a771998c8e2487991fc0dfe16edc09e3dec0d64f9472832f89bd
MD5 c06ebb2ab66971610aa317d2744e102c
BLAKE2b-256 e8fd5cf74d7454295fa2d13a9f325a09183aa5f71a10f31264108a0b895481ab

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 293.6 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 02acfa09769380023e9bbed5c63f5f41b04dbc13ff392a7284df685127d224ae
MD5 ec5f354964d18406ccb6b83bbd3b79af
BLAKE2b-256 8ddd42b99e980dd42b0915957999594684c8963bd76006950b48ab7fb373d846

See more details on using hashes here.

File details

Details for the file srsly-1.0.4-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.4-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 287.9 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for srsly-1.0.4-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 39a8bd6b383b4ea5c3a6793d790e1356e236701f421badef278a0ea80bc95d30
MD5 637341424f9aedd2875ee5f8f9b5647d
BLAKE2b-256 2b23b1683fd6c77a88c2dad6e184d0ee97b22c09bba3929ba6eb673a23b3d7ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page