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.yaml", 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.yaml")
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.dev2.tar.gz (317.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-2.1.0.dev2-cp38-cp38-win_amd64.whl (455.6 kB view details)

Uploaded CPython 3.8Windows x86-64

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

Uploaded CPython 3.8

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

Uploaded CPython 3.8macOS 10.9+ x86-64

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

Uploaded CPython 3.7mWindows x86-64

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

Uploaded CPython 3.7m

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

Uploaded CPython 3.7mmacOS 10.9+ x86-64

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

Uploaded CPython 3.6mWindows x86-64

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2.tar.gz
  • Upload date:
  • Size: 317.5 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.dev2.tar.gz
Algorithm Hash digest
SHA256 0d2ceeb3f9012e4544e774c0f133797fc88c243d1e17615955eb5f11b041ea25
MD5 63751301fff73a0a61dc9bb5e6e00b55
BLAKE2b-256 08e83f7587f215537ef65d37b6a0a6703631e21e508e409b3795fa29f7fd1aa5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 455.6 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.dev2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 8374c15eedbb47b471b28f96b3098b3133c25b20d226ab3dd929b7fa2cc0a443
MD5 417c724c929b8823cc056931f8c838a4
BLAKE2b-256 8fe9c81cc93d5cd0076893e6baffafde55f7154b5a8503abd68debd1a9c3108f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 456.8 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.dev2-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a5cdef4a12024813c39b66875975dc94306ebbdd06c26d6f458f6e40d9a84c4f
MD5 4f20f5e440e1683db7aeeb83fa225c78
BLAKE2b-256 6eacc52ebd6f7d2adf0718fdf5eaf9a3c0c8e993c99faec97a42a6809911ed80

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 453.9 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.dev2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e2c9925f76107770c57983a322c3ae0c835be3fd538b320885fc0ccd2a45fcfc
MD5 ef9dedab7c98139d7dee29566afceaf2
BLAKE2b-256 ed7e5fdebf511a4f7e94749b61f6e2b85f78810237b1d50f3548a76d339cc2a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 453.7 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.dev2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 85e6a3a8d578cb706bc752a514bad79b5683f0483e5649404105874d102f3475
MD5 2c7d5518a57e9f4b3718bb70a4a5a344
BLAKE2b-256 17483749ddf73488a918e87a192143505a816fbd75fba0f36185aec45babd6f4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 457.1 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.dev2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 affa1b0c9fdddfdaa4d7147ff78bffa54439f681fe29dad61d58362b8eed8869
MD5 cefff65c097514e4fb3e91f3ca498665
BLAKE2b-256 009976b8e68e1c87c825362ee63d57380a6dc40e1eb29f6100e1b53bd6a4b922

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 452.9 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.dev2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1c9f272ca3b713580137e95a9d3fb0ee4564d7b558a5429aa2db898110f0e43f
MD5 e0ca79c1ff46c74a2ccf79d5873541aa
BLAKE2b-256 d136fc934d100543eddbfbc58b4592d582699d151830c56750c3227d05d6ae30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 453.8 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.dev2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 3d17aad3b95b9c760e3abe362e50226ef6e2add33174009e16a2b8cf782ec5d3
MD5 ad186728da3c9ec547c568bba60d3e9c
BLAKE2b-256 cf4c1a30b9637758a449855ece3120d46479a9f0ac52b460398d89abe88a6b9c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 456.9 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.dev2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2f88d9b30dce42ce36c8aca56aec090e85ead43da4c693a8871e1a1e5155d322
MD5 1118257a1dbd2c228fd31a8d714b3377
BLAKE2b-256 fc670a38f2aa717acb7baf1491f84c09eda05d82085b9c53d5dc773d570ea91b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.1.0.dev2-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 455.0 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.dev2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b94bc34f4365ae454161b25a8312ddc3df2a295641ed40b992d6f72928b670b8
MD5 ffe81f1cc4994ec8f2bf6096629233c7
BLAKE2b-256 8975bffb9bb077a4228697c94b47292def4400a81de1545e1a3dc155a31dfb6b

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