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.0.dev0.tar.gz (314.8 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.0.dev0-cp39-cp39-win_amd64.whl (451.2 kB view details)

Uploaded CPython 3.9Windows x86-64

srsly-2.4.0.dev0-cp39-cp39-manylinux2014_x86_64.whl (456.2 kB view details)

Uploaded CPython 3.9

srsly-2.4.0.dev0-cp39-cp39-macosx_10_9_x86_64.whl (452.0 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

srsly-2.4.0.dev0-cp38-cp38-win_amd64.whl (451.6 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-2.4.0.dev0-cp38-cp38-manylinux2014_x86_64.whl (458.4 kB view details)

Uploaded CPython 3.8

srsly-2.4.0.dev0-cp38-cp38-macosx_10_9_x86_64.whl (449.9 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-2.4.0.dev0-cp37-cp37m-win_amd64.whl (449.8 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-2.4.0.dev0-cp37-cp37m-manylinux2014_x86_64.whl (456.3 kB view details)

Uploaded CPython 3.7m

srsly-2.4.0.dev0-cp37-cp37m-macosx_10_9_x86_64.whl (449.2 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-2.4.0.dev0-cp36-cp36m-win_amd64.whl (449.8 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-2.4.0.dev0-cp36-cp36m-manylinux2014_x86_64.whl (456.2 kB view details)

Uploaded CPython 3.6m

srsly-2.4.0.dev0-cp36-cp36m-macosx_10_9_x86_64.whl (450.6 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

Details for the file srsly-2.4.0.dev0.tar.gz.

File metadata

  • Download URL: srsly-2.4.0.dev0.tar.gz
  • Upload date:
  • Size: 314.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0.tar.gz
Algorithm Hash digest
SHA256 6483bd693b905e99af828bafc9ec2d8fc55207b59314a3207e07e2e088ef5e5a
MD5 abae81e8aa911cc10c3fc56969c9c302
BLAKE2b-256 a0724abb69db12d346c4339c4bfb4e6087fa5cb0f70fa0631923cd471ec21bd9

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 451.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d72acd97c0a9ec93779ca34a982ef3ab69b0e45a7f0b95a6b2c00ac58bb6c5e7
MD5 a3a070324f66d0ad0b0269fccf15a2d7
BLAKE2b-256 555a2ae4b9e0d542784b46f373f106e0548bca3e70d284f1f62fb69a8bf31ec2

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.2 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 185802aa9907fbf86e697f5ba1520c44956463865be7c9fd18674bf4e060fead
MD5 afdda5ed6b69de4f6d72e807c405a37f
BLAKE2b-256 6cc80669880b269ef19fac7d136d66112f3aa07021fe8768a56fd19007798b2e

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 452.0 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 38e3afb35c6ea218cfea190e0e23ae28f242aa428250b5d8aece31cf6744d740
MD5 26511f424ef8ad9e6f98fb2a6328421e
BLAKE2b-256 01ecab8e39affeea743f0e7358c5396ab083041cdbf5500d3a48eed848ee3b67

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 451.6 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 fa5879c934456285e327ba0585683f8b2fd64604b7d01c34be64db26e08a6ab0
MD5 12ebf06ff1c9d8e667c6e135659e5fe8
BLAKE2b-256 fd66d9343654326a10a9d1adfe8aac3f815fcc30ce6080b78e6f46687ff63cbf

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 458.4 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 339d10c8c1b1152933b6b47f6636685a9193381b4b09d794db926f86215c4207
MD5 34c74912a9e743137aac040e978ce053
BLAKE2b-256 42c59aba835134b07113ffb5580b237b996e9dd06cce2c0a0a94dcae47721f52

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 449.9 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c4a61e049c16befbaa6b04be2f63372da5fc1b5a33b7b2f4c0bcca6a01cf61a3
MD5 72dd5da1667e12abb3945537b66fbbbb
BLAKE2b-256 53f7d7a04b8f1d55b2c61e1d552846e942b97585c653619524b5b35ec80c484e

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 449.8 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 910429a47f31891b3d559f1527c0fba5cf93c008277bce8adb9576721f80943b
MD5 2f0061e5c517aa10255f01ce0b38f443
BLAKE2b-256 57332b5ee1382dd7fc707a1441afa2f98d0c1b9898cf1e6d4407bebc729aca3d

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.3 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 757796a245a44bc4db6d93c4db6319debd226c2e85df42ae74307f8e87283f77
MD5 ff440636b931d0a42048cea73d379161
BLAKE2b-256 b734b28726a3ec50b9e3c4722b400e8ce555693684b46a15fea0d47a7d546412

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 449.2 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 33b92995fe21c4fb9293711f4c58043d278338fe730df7a09fe21f19ca1a2386
MD5 4a48df57edf8038e308405f63db8171f
BLAKE2b-256 56aaf4d0a0b3b7f986aa3011b7880dd35619e2bccaa968ee9becb02aedde1092

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 449.8 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 fde75eee3a9c4a6a7cf573ebb63173ad89e5d8058242f705413465d651f92437
MD5 b258e7f08b0965204082485209613c3b
BLAKE2b-256 3c8e5306c85f409f5e063701004bf05549a9b5e99f153ccf2822fc8bb1891f9b

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.2 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 15b82845d2a455d302ccc5c564c6a12190419dcc57968cb3b4d4e34dd4c3949b
MD5 5373b35148d220ec5fa581d4bfd3d0d3
BLAKE2b-256 ce1cbdf1d10e540cf7525d82b804ffe37336af3ce94e6177c20684e44641cfcd

See more details on using hashes here.

File details

Details for the file srsly-2.4.0.dev0-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: srsly-2.4.0.dev0-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 450.6 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for srsly-2.4.0.dev0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8cb167622c2cb886893d7ac91afbffd8ce12c3df5dba95ec3006bae4fb0947e7
MD5 77528450220188270b47f9c33ae2fc18
BLAKE2b-256 65008e3144aedfa3d56102f354f623bb11247cc174e27db5955b686ca6931b48

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