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. Before installing, make sure that your pip, setuptools and wheel are up to date.

pip install -U pip setuptools wheel
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
path 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
path 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
path 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
path 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
path 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
path 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
path 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
path 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
path 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
path 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.4.1.tar.gz (315.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.4.1-cp39-cp39-win_amd64.whl (451.5 kB view details)

Uploaded CPython 3.9Windows x86-64

srsly-2.4.1-cp39-cp39-manylinux2014_x86_64.whl (457.3 kB view details)

Uploaded CPython 3.9

srsly-2.4.1-cp39-cp39-macosx_10_9_x86_64.whl (452.4 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

srsly-2.4.1-cp38-cp38-win_amd64.whl (451.9 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-2.4.1-cp38-cp38-manylinux2014_x86_64.whl (458.7 kB view details)

Uploaded CPython 3.8

srsly-2.4.1-cp38-cp38-macosx_10_9_x86_64.whl (450.2 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-2.4.1-cp37-cp37m-win_amd64.whl (450.0 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-2.4.1-cp37-cp37m-manylinux2014_x86_64.whl (456.5 kB view details)

Uploaded CPython 3.7m

srsly-2.4.1-cp37-cp37m-macosx_10_9_x86_64.whl (449.5 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-2.4.1-cp36-cp36m-win_amd64.whl (450.1 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-2.4.1-cp36-cp36m-manylinux2014_x86_64.whl (456.5 kB view details)

Uploaded CPython 3.6m

srsly-2.4.1-cp36-cp36m-macosx_10_9_x86_64.whl (449.9 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: srsly-2.4.1.tar.gz
  • Upload date:
  • Size: 315.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1.tar.gz
Algorithm Hash digest
SHA256 b0f2aec0a329e6e7e742a0a60e99a74968ca29be71f35c5c4de221e328176926
MD5 ef34f04ffca2cbac3cee4721485bad87
BLAKE2b-256 3fea367e2a83354463447ca959feaf8248c72a2e6bc47c55bfd071716975cb41

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 451.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 869fdcf664edf20cd374cf1add869d67960061276478025a5887e080d8f99e1c
MD5 5efe3f3d464f2a4df7c7a3d02b9a5513
BLAKE2b-256 d69c38c3f98d86196e212bb9cbc0612875c218d427a067e8394ca038fe08989b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 457.3 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 178aa6d350c9cfedb8adadb5e1f96b7aadde203d088917063415fcd689eb6e42
MD5 e23ca46bad0bc24949b4e69fea11547c
BLAKE2b-256 1d14351817645e5115c15fd64cf6d608dd1faaef136c64612f9d12793806a14f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 452.4 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 76b11e0ec0056bda4ad009b6e0db37f3ad0005a0501d587080023d4312ad2ada
MD5 1655bfbb686ef9b50b332ce35d136270
BLAKE2b-256 0ca0621c9371a422c7cf5ce30d6c9e2d9eb84c6043f997a00cbc600d5d7d6115

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 451.9 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 867d1154ff7b60043584fe048de9b6d9a7d5a7fc61437850922ae4bd46d3be16
MD5 18817741e82191d0f5f5bd56196dc984
BLAKE2b-256 72a584dfc59269ee2c8a083c7cc05488d90fd2d9b552fa4eefb9153a9c36a563

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 458.7 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ff36dc01df8890a239e5d15cffa3ae3b272c19e5ae279840f2d30085d361c20a
MD5 bf93b367edb178dc923ba82da58ae198
BLAKE2b-256 86573911f886b208eacd42fe88683818d44e63c6cfeb816105b9a55f0fe2916b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 450.2 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e896d516ca2e2e89cc01df8c9c8b1528701d6f49e9c814332582cc701af64a91
MD5 73910894bba73792fea5453d6d4af90d
BLAKE2b-256 5ab364337a9908f2bb2a3aa6e3b4d444100c40a73fc1a9474fda50a547585b90

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 450.0 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 b1bd4a55bafbb8cf86be15bf18aa2ba2c953161ad71ce7d2dae0c141201a7d89
MD5 3b41d476679ea28d8b7cd48b8f5ef6e2
BLAKE2b-256 e7f862520edb641dde8ba57f7ba9aa82f3c8e6567b8b8aacb690615c9800d156

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.5 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cefe06912f3944b5729d555ee110f434a0787843c6676b90f4987ff7a0a69500
MD5 7c9c5162f357e951010d29902d3ef7f9
BLAKE2b-256 c384dfdfc9f6f04f6b88207d96d9520b911e5fec0c67ff47a0dea31ab5429a1e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 449.5 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 20f11d5d6ae29e3cc97e93c862d7bf8b75023668daf1ac5892598c512302e5d3
MD5 11bff5eccf0c5b75944fa3960b01341a
BLAKE2b-256 ff95acde60d539724a1580644e3aad295b78ba01487767149399d93ee9d6078e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 450.1 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 129c85db752b5945c6398a1952294e03b7d20fa111eb7fd1083c4a6b1d02f7c7
MD5 b18904c530fadc2958985b173c579d5c
BLAKE2b-256 6ea67af70173f04e17f3e898d3b4dbf805be66019606a08f07890ff7a4b2e728

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.5 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9625a584b26e522b6afb7c24be8783228ff44d7ac624e500020b0b888e09c6b6
MD5 53a7adb012e96d73651a74f8e92d9b0e
BLAKE2b-256 6f883d5e47abe88fa07d5ec3b4324e9deb546a94218f6a5c68671d41b4a6b553

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.4.1-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 449.9 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f7c3374184bfb1aa852bcb8e45747b02f2dde0ebe62b4ddf4b0141affeab32e1
MD5 dbc5b800fe1c5f37552ed5bcefb7f7d0
BLAKE2b-256 2e60c1ace0474ef7c122abb847750f0b747635783d7285de2919ed3835f7ed97

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