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.3.1.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.3.1-cp39-cp39-win_amd64.whl (452.0 kB view details)

Uploaded CPython 3.9Windows x86-64

srsly-2.3.1-cp39-cp39-manylinux2014_x86_64.whl (457.4 kB view details)

Uploaded CPython 3.9

srsly-2.3.1-cp39-cp39-macosx_10_9_x86_64.whl (452.6 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

srsly-2.3.1-cp38-cp38-win_amd64.whl (452.4 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-2.3.1-cp38-cp38-manylinux2014_x86_64.whl (459.5 kB view details)

Uploaded CPython 3.8

srsly-2.3.1-cp38-cp38-macosx_10_9_x86_64.whl (450.4 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-2.3.1-cp37-cp37m-win_amd64.whl (450.6 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-2.3.1-cp37-cp37m-manylinux2014_x86_64.whl (457.3 kB view details)

Uploaded CPython 3.7m

srsly-2.3.1-cp37-cp37m-macosx_10_9_x86_64.whl (449.3 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-2.3.1-cp36-cp36m-win_amd64.whl (450.6 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-2.3.1-cp36-cp36m-manylinux2014_x86_64.whl (457.2 kB view details)

Uploaded CPython 3.6m

srsly-2.3.1-cp36-cp36m-macosx_10_9_x86_64.whl (451.4 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: srsly-2.3.1.tar.gz
  • Upload date:
  • Size: 314.8 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-2.3.1.tar.gz
Algorithm Hash digest
SHA256 3dcce93c69ff18e8a8d7decfdfdf8e1fd44c4446cdee8d0e5d545d6b172dc377
MD5 eafe8660348460a60895482301911d64
BLAKE2b-256 a32dc9a532776c2b08a2ffcae922d7c9d6e355a8381b00599aa677720b7763cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 452.0 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-2.3.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ed37bc399142493c7b35a852b93e8e043e2797ed2495efed592aecf1f324ab41
MD5 d523e9af7defe9db3706828fd0d686f9
BLAKE2b-256 44f76682dd149613a5dbe7c5c2b3ecba0af48bc21c8c3a48141e601d38097acc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 457.4 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-2.3.1-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a4ac35861790b33d08d22f1e7b85c9f6b20c72b3f08a81ed2c649c36832b71fa
MD5 745e0ea44ad73e424e282e1a90a9af6d
BLAKE2b-256 bc84058b8860c35ab2891f894ef3f0abc737a63e6d4956ac0953b026af8fd769

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 452.6 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-2.3.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d314bcf7517be565f092a3aff5ed79bce835cff6d39981ffb3cef90e5d860e5a
MD5 b65840c40309f1631521337c35822e4c
BLAKE2b-256 a61f8dc3f2b63e9de47f465d8752f24b2ba7bf65fc4a13a88216f7420a454260

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 452.4 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-2.3.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5a3c6926b95b0ece1fa1445c4e6215e433d862055bb012cf9c8525248d4abb6c
MD5 6e5f11f79bd3a24caf0ec07672ab3586
BLAKE2b-256 c114050b6ee1e29ea814b10faaf7ad41c53dbc76c0fe98647c3b3a0a6da2e3a1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 459.5 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-2.3.1-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8b1ad75d1eac1da189695d48c5d9e78cea14975cf961a91dc279f393e7f1b328
MD5 e40057804435f4909d02d73959fc95b1
BLAKE2b-256 050f461047290819fe5f400616a450670966dcae4b8b19e3d51a876bd9304222

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 450.4 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-2.3.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 408b4d696cfabca7d9c3dec716813c6a03a59bd65b5f50f287f8ab468d658de4
MD5 0b0cec90a2367efd55e99ce4d081564c
BLAKE2b-256 b781bbf65a114e0303d1deb4a85a9f23e99d832724da64cde845599879b246b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 450.6 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-2.3.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 99b8b46fc012a4a1e04286ac85bd99845ef767a0fcdd5643adab1ea4d547c868
MD5 f077571f46181b099a222de948d65287
BLAKE2b-256 3347a94e268696a1c5e188658d50ffc5572ab949748d4ad7024ac2aeaf7b0e75

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 457.3 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-2.3.1-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a1585dceb22b42ac2e6a4f20e120a0594209a0200b28e1348a028831da3df1a8
MD5 838f735e51af754f65c2f8be15e32994
BLAKE2b-256 634d483035ea31ac8ab54a3ba0fe2e83aa885709e79272f1495474824e1f961a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 449.3 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-2.3.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 750ed4db3e8b9c49cc7615738184f6a2684cee78e7293d1b56c6bada97bbd70f
MD5 76e4f5dc4230e2a765f9cc953b1192e0
BLAKE2b-256 07b9d20496ca229b9cd205e4bd181a674f67cc925d9dbc816a984087a8a87fd9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 450.6 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-2.3.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 2815d46f3fb13ca4cb6c1c2dc2b34f5d205b220455decafa999bc44fe407d484
MD5 bafe2faf3bdd4f42bd2feeca53833762
BLAKE2b-256 a7164f1545b76e267391d09f26639d12500b8904a2af2691c60762bb39645971

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 457.2 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-2.3.1-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 83b0407524ec188c81d4865f80381f0c4cd49a7c010a51f4b5e8928cb7c8b293
MD5 950e1dc011dc052cc39ce00b04663f11
BLAKE2b-256 63143df79b934853cf919aaa8a270cfd8881294e5742a96fb72502faf56162c2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.1-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 451.4 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-2.3.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 32705205c9c017a5f13e301c5c7ab9d2e1bc2db3bce3b81d1529e42a60586047
MD5 40dc562232c1f32731e3d5363deeb6f3
BLAKE2b-256 059499bc696ceb981b77d7fb4a778ab1345f76f687be3d4326a72bdb220bc9ac

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