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. Currently supports JSON, JSONL, MessagePack, Pickle and YAML.

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

⚠️ Note that v2.x is only compatible with Python 3.6+. For 2.7+ compatibility, use v1.x.

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. 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 str 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 str / 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 str / 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 str / 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 str / 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 str / 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 str / 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 str / 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 str / 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 str / 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.

YAML

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

function srsly.yaml_dumps

Serialize an object to a YAML string. See the ruamel.yaml docs for details on the indentation format.

data = {"foo": "bar", "baz": 123}
yaml_string = srsly.yaml_dumps(data)
Argument Type Description
data - The JSON-serializable data to output.
indent_mapping int Mapping indentation. Defaults to 2.
indent_sequence int Sequence indentation. Defaults to 4.
indent_offset int Indentation offset. Defaults to 2.
sort_keys bool Sort dictionary keys. Defaults to False.
RETURNS str The serialized string.

function srsly.yaml_loads

Deserialize unicode or a file object to a Python object.

data = 'foo: bar\nbaz: 123'
obj = srsly.yaml_loads(data)
Argument Type Description
data str / file The data to deserialize.
RETURNS - The deserialized Python object.

function srsly.write_yaml

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

data = {"foo": "bar", "baz": 123}
srsly.write_yaml("/path/to/file.yml", data)
Argument Type Description
location str / Path The file path or "-" to write to stdout.
data - The JSON-serializable data to output.
indent_mapping int Mapping indentation. Defaults to 2.
indent_sequence int Sequence indentation. Defaults to 4.
indent_offset int Indentation offset. Defaults to 2.
sort_keys bool Sort dictionary keys. Defaults to False.

function srsly.read_yaml

Load YAML from a file or standard input.

data = srsly.read_yaml("/path/to/file.yml")
Argument Type Description
location str / Path The file path or "-" to read from stdin.
RETURNS dict / list The loaded YAML content.

function srsly.is_yaml_serializable

Check if a Python object is YAML-serializable.

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

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-2.1.0.tar.gz (316.0 kB view details)

Uploaded Source

Built Distributions

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

srsly-2.1.0-cp38-cp38-win_amd64.whl (453.7 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-2.1.0-cp38-cp38-manylinux1_x86_64.whl (455.0 kB view details)

Uploaded CPython 3.8

srsly-2.1.0-cp38-cp38-macosx_10_9_x86_64.whl (452.1 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-2.1.0-cp37-cp37m-win_amd64.whl (451.9 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-2.1.0-cp37-cp37m-manylinux1_x86_64.whl (455.3 kB view details)

Uploaded CPython 3.7m

srsly-2.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (451.0 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-2.1.0-cp36-cp36m-win_amd64.whl (451.9 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-2.1.0-cp36-cp36m-manylinux1_x86_64.whl (455.1 kB view details)

Uploaded CPython 3.6m

srsly-2.1.0-cp36-cp36m-macosx_10_9_x86_64.whl (453.2 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: srsly-2.1.0.tar.gz
  • Upload date:
  • Size: 316.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0.tar.gz
Algorithm Hash digest
SHA256 6310c5dac1c9121b6d919507ced0871c44690cb93c8d5b979db5b455cbc087eb
MD5 ab5622c6a546f4fe433ee780aa82ea3b
BLAKE2b-256 ed1e70b81604f8bc703c4eaf3fcd64d697fb85d5ab178ff66a2363c9e349853e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 453.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 78ae1fb1d9fdd10b53521529dd2865ebcac0a28cbfcaf8bab730a31ae1f7d000
MD5 9108e294bf6df6caa10214a1f603a033
BLAKE2b-256 1f85b9a5934c9c369546411878dd388781896355b03626e8305c80a0c6099538

See more details on using hashes here.

File details

Details for the file srsly-2.1.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: srsly-2.1.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 455.0 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3361349f359591410b5ee7ce50c83fa7783f12b43705189683040ae6f9c8f7b3
MD5 5b5f9056dbfe1f16626431db677ddfdd
BLAKE2b-256 68199c878537fa32a15095cf03f248175eba617e3d1763fd96482179f1154d8d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 452.1 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9320157d2e33000fdd98f43a3815d69726e2963c24c07b5506be3ed6b557f56a
MD5 4bb7d2d770721a12dd7404acb323d4a4
BLAKE2b-256 1c1223b980a8dec8b83c1645b89cbb1eb7215a64e240ccf8f32d608fe3698fa2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 451.9 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 6f263c116ac67781420a6ea24bc661b25573e44a2fca4ecf15b09aacae66d5ac
MD5 68ffb6e5a1f1c1a1fcb1b8014f448d3a
BLAKE2b-256 1b233698af5ebe6716b6787582dbbb7cf87fd0690de03cda7dd9b64b221cff5a

See more details on using hashes here.

File details

Details for the file srsly-2.1.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: srsly-2.1.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 455.3 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 203020cd4d96c9fdfb73b24ae8099482a25af1c97d38d32e7f082b6adaaaca0b
MD5 0f78a2a0363af154a5cb0ed71fd4bf24
BLAKE2b-256 8bdf5b5dd4f584d1ef5133306225a02fbb38c923b99710b50194083299bcc891

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 451.0 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7a332d7665dbdacf07275c904ae37bb68fa18a002bd255aacb011b059e61cd99
MD5 317be4836a00b3dba917eccbc92190b3
BLAKE2b-256 afe793d370885f443ac659b7c90ef79ea029f4a2b5ae2bac6a96d25cfcbfb46e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 451.9 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 5b2129c3bdbe79eb567aff6b97bf1c175862235f9557bdf4cb289ed187854dd4
MD5 48752da6a2a1e3e3b31362e3d7f5fe42
BLAKE2b-256 476d1a4ac74d85c54632cd2684ec0e25b64dc4240bd81ad31d41b1a1ace2a70d

See more details on using hashes here.

File details

Details for the file srsly-2.1.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: srsly-2.1.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 455.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3e91583e7069c4b33acc211dec11e867d726603d12ae1a6108837de70edf6494
MD5 58394d5bcef3a62738369c4478a205e8
BLAKE2b-256 2ab17c1111e072d49d67a4bedfdae17df27725be96bdf90c5a8afa4d1f718411

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 453.2 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for srsly-2.1.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 76d8f4cb2bf9b166d3d1f2793a61cc167ca1891499071f10ca1d840334bcc4c0
MD5 5f3459f93be9f875229d7742607fc622
BLAKE2b-256 d5cc0c08d533ce2d50be4e38ab2323d99ae49174b7d1b865bda36fd195e9cfe7

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