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.

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

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. Takes care of Python 2/3 compatibility and 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 unicode 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 unicode / 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 unicode / 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 unicode / 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 unicode / 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 unicode / 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 unicode / 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 unicode / 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 unicode / 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 unicode / 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.

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.0.0.dev0.tar.gz (182.9 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.0.0.dev0-cp38-cp38-win_amd64.whl (303.5 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-2.0.0.dev0-cp38-cp38-manylinux1_x86_64.whl (308.1 kB view details)

Uploaded CPython 3.8

srsly-2.0.0.dev0-cp38-cp38-macosx_10_9_x86_64.whl (305.8 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-2.0.0.dev0-cp37-cp37m-win_amd64.whl (301.8 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-2.0.0.dev0-cp37-cp37m-manylinux1_x86_64.whl (307.6 kB view details)

Uploaded CPython 3.7m

srsly-2.0.0.dev0-cp37-cp37m-macosx_10_6_intel.whl (394.3 kB view details)

Uploaded CPython 3.7mmacOS 10.6+ Intel (x86-64, i386)

srsly-2.0.0.dev0-cp36-cp36m-win_amd64.whl (301.9 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-2.0.0.dev0-cp36-cp36m-manylinux1_x86_64.whl (307.8 kB view details)

Uploaded CPython 3.6m

srsly-2.0.0.dev0-cp36-cp36m-macosx_10_6_intel.whl (397.5 kB view details)

Uploaded CPython 3.6mmacOS 10.6+ Intel (x86-64, i386)

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev0.tar.gz
  • Upload date:
  • Size: 182.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0.tar.gz
Algorithm Hash digest
SHA256 31841f33e8cff87b2c992c54d7bab79e3e03383988f108d81dc3867a83ae3980
MD5 010429bb23badec8ef70b289347be479
BLAKE2b-256 dff432ed7dbabb84527faf9d0d188668a3e236fb94aef71df37c6c57a7c66dfd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 303.5 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.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 257432c16b196a3e2e4295e248010457beaf45bf8fc39135f6ed2f7c028b7eea
MD5 097b51bb71ba061d8a868f49e6c7e023
BLAKE2b-256 c04639f84dd50384a604001670d4e888337347b7b8a4eee4f3b49110492e4d00

See more details on using hashes here.

File details

Details for the file srsly-2.0.0.dev0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: srsly-2.0.0.dev0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 308.1 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5e93b23fd61d77f235809d209de2b9cc75d2b245a8b91a575bd8ff1938abb589
MD5 58edc7ecdd51582a3db65d8ad6f22050
BLAKE2b-256 d219c7d62ba909ab0f1b54ede1ee152483db6bbed3291754f6c09777a13be7da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 305.8 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.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8da85fead53c36e578f25857a0c62bd8051bce45ba1dbaf96e6e3949b4d47eef
MD5 72b9c67c22ad5d6e05afee33a09c8d18
BLAKE2b-256 df7e1630c6975fcfccffd7ba6483fb49ee12bc18454215bc7de710e5d6cba359

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 301.8 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.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 e835863e4e0f994a6ef498d7d4c55497c762c8228570553b5a680508398b33a0
MD5 2ecbe1388a6d3c0a153cd62eb4f71881
BLAKE2b-256 2d6c32f46dabe923739e4cf5fa7985d83b8396724efaf67ab2fcf3e553527db7

See more details on using hashes here.

File details

Details for the file srsly-2.0.0.dev0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: srsly-2.0.0.dev0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 307.6 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7de4998e4e4e957de26a122b547c67bbe6ccdbf0b91a5f50a01324b3d342427a
MD5 b9d5b2b18827e5afb853fad21b26b2b6
BLAKE2b-256 cb626cf5d5320d3c6505dc572f0022fc7f2f0e02796043aab34c728023e330f9

See more details on using hashes here.

File details

Details for the file srsly-2.0.0.dev0-cp37-cp37m-macosx_10_6_intel.whl.

File metadata

  • Download URL: srsly-2.0.0.dev0-cp37-cp37m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 394.3 kB
  • Tags: CPython 3.7m, macOS 10.6+ Intel (x86-64, i386)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp37-cp37m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 7d787fb51161ee34e13492165629a772ad22a11c4172e6edd6c3b02385528ef7
MD5 e39a85ebee36db7a6cd38b30f3f794f0
BLAKE2b-256 5bc6e18e4df69f0577258dfc8f6b819a3a2910969077f27b9a772bd8d51388b1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 301.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.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 a137694696790d6bf39a86015d63093d0a2452f05fdbac3bd6e649a65cc654bc
MD5 af1a5c5614a2a88d28f91324dee23c98
BLAKE2b-256 cd7ee548812616b779d221222ff91a681de61d2d3d675f350f47da5b7ec25323

See more details on using hashes here.

File details

Details for the file srsly-2.0.0.dev0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: srsly-2.0.0.dev0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 307.8 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f1a4591a903ccd54f241a18db3f90a5879d407ae45ce22d30cc160c0415b0b3a
MD5 03f60b18f9ef6ad40d2b8a66120c2dae
BLAKE2b-256 13174301771faea196a14b54afd89510557def859d72df9889206e53acf48de5

See more details on using hashes here.

File details

Details for the file srsly-2.0.0.dev0-cp36-cp36m-macosx_10_6_intel.whl.

File metadata

  • Download URL: srsly-2.0.0.dev0-cp36-cp36m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 397.5 kB
  • Tags: CPython 3.6m, macOS 10.6+ Intel (x86-64, i386)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.dev0-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 8ad7ec91a1d15397fd40e9d5029f6391f9d70776bdf2852d6a9581798c80c8a4
MD5 cdf762425a8c248c1e2bc513aa2242f8
BLAKE2b-256 9822fa5bc2c6d73c1c677e6564fa683c41005365a332ee975d536ddb504693e6

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