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

⚠️ 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. 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.tar.gz (193.2 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-cp38-cp38-win_amd64.whl (306.7 kB view details)

Uploaded CPython 3.8Windows x86-64

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

Uploaded CPython 3.8

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

Uploaded CPython 3.8macOS 10.9+ x86-64

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

Uploaded CPython 3.7mWindows x86-64

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

Uploaded CPython 3.7m

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

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

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

Uploaded CPython 3.6mWindows x86-64

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

Uploaded CPython 3.6m

srsly-2.0.0-cp36-cp36m-macosx_10_9_x86_64.whl (309.8 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

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

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

File details

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

File metadata

  • Download URL: srsly-2.0.0.tar.gz
  • Upload date:
  • Size: 193.2 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0.tar.gz
Algorithm Hash digest
SHA256 785b00e00406120dbef4ca82925051e6b60fe870c5f84f0d22b3632d574eb870
MD5 194f718e8b30c19b15f63c268cc322ca
BLAKE2b-256 462f8938af85873f1249a771788a82acbb8b684c444ba1a17d769ac28aeac55f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 306.7 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1866dc0b875d5958090b62ac126a92a75619664d24c0b18657bb14f89c2140d9
MD5 42cb161b166f9380827d12de8cd4fc36
BLAKE2b-256 f3c245f73122980d85490f11c554f27fd569d01203c88a740a977ca240a92aa3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 311.4 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dfaf84e2043b3310bf31c1561c1991c9747469b14131c33e77d20c548f18628d
MD5 493db5e223bd2a28f739bd49848f0725
BLAKE2b-256 ba6671adfd22ace2b904b5766855c2f6c2ef9aad19b399b401a9b51b4c8173aa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 309.1 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b271c0c916d61f5712198b2d9e499113a56ad7529f9e361eafc678b58f682354
MD5 99dfc826c0954c7c1264a5bde68dee6f
BLAKE2b-256 dfe3879407619ac0b321b8d2a0c8e614f8fdb35ce3120e1149153aa93b4dba25

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 305.0 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 01f5a47aa3036a26251eedd68b28b4a1fe1e3efab386ec95d21bb7b4ddfc62ab
MD5 f5e9642f23b2507a0e973d48d73bc517
BLAKE2b-256 fbb8140e04611bba9cbdd62022d0eae1fd99d76a0866128c81b75890f8e547cc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 310.7 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 79e0476bcd2747b76bdc94a9a80af51956438f3281b3e18caabb91ca3c7895a5
MD5 b4911f86954a28e8d5a3dc134344c6b2
BLAKE2b-256 eb30b8c06be8ba53c1ef62b7ffb28621e865ec4bb0a34dd9538a4f157e9a4468

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp37-cp37m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 398.2 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp37-cp37m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 5af468b2c740db357790a2eab3c31d699d81d82e57d06408d0cec4aae3a58020
MD5 244de99c2291897f202738738faaaa1a
BLAKE2b-256 94905dc602b889ce40a1453bd7bfa5f15cf8a47885ea9fcf9c632b894bbb461d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 305.0 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 081d25a1a2024c4e1e1d92e12b694aa6d3f9c452ac3380d175e17e73c859f4de
MD5 4ada93e482c879a210c9eaf5f9a1f522
BLAKE2b-256 235f27b6cdcbce577007b3964e974d79b65d825ff5e5196ac8ecd181045cb1c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 311.1 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dea2d1ad877868395a7acf1376e5b772efff1f259a0d7dbbcad3d16c3a5ce4c6
MD5 d44cb9bd5576054c2f26e26ead649ce1
BLAKE2b-256 a5ff0ed98f94298d5665e18879f82548406239c96805fe9102a81dfe7c1770a5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 309.8 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.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 203ab7bc1725e6a2db90c50bef4971dbd93bbf579a384cf7bd96cc1131998164
MD5 07198a0fd9f17c8e5cafc06fc841063b
BLAKE2b-256 2eaa6ef6f25234c7a26b4cb2299ab8f0432d88cf9aa4439ca00cf66b2da92811

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0-cp36-cp36m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 401.6 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.1 CPython/3.6.8

File hashes

Hashes for srsly-2.0.0-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 f9b1688b6c2aeaceae45d672b65f50750d0ed70940aa214841dfd81c96027026
MD5 0ed00fb7373427ff3376ece1754a5dc3
BLAKE2b-256 119c4be6adc575f3d1312792e82ca412a6215d97cc4ff9c04598b72b870282e7

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