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-1.0.3.tar.gz (193.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

srsly-1.0.3-cp39-cp39-win_amd64.whl (287.7 kB view details)

Uploaded CPython 3.9Windows x86-64

srsly-1.0.3-cp39-cp39-manylinux2014_x86_64.whl (294.3 kB view details)

Uploaded CPython 3.9

srsly-1.0.3-cp39-cp39-macosx_10_9_x86_64.whl (289.9 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

srsly-1.0.3-cp38-cp38-win_amd64.whl (288.1 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-1.0.3-cp38-cp38-manylinux2014_x86_64.whl (296.5 kB view details)

Uploaded CPython 3.8

srsly-1.0.3-cp38-cp38-macosx_10_9_x86_64.whl (287.9 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-1.0.3-cp37-cp37m-win_amd64.whl (286.3 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-1.0.3-cp37-cp37m-manylinux2014_x86_64.whl (294.4 kB view details)

Uploaded CPython 3.7m

srsly-1.0.3-cp37-cp37m-macosx_10_9_x86_64.whl (287.1 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-1.0.3-cp36-cp36m-win_amd64.whl (286.3 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-1.0.3-cp36-cp36m-manylinux2014_x86_64.whl (294.3 kB view details)

Uploaded CPython 3.6m

srsly-1.0.3-cp36-cp36m-macosx_10_9_x86_64.whl (288.5 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: srsly-1.0.3.tar.gz
  • Upload date:
  • Size: 193.7 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-1.0.3.tar.gz
Algorithm Hash digest
SHA256 25bd33819a0e95dbe5d49072d07c361827658cf5f7d69d15e580c43d5adb873a
MD5 725143676676a9d13827239f0659edaf
BLAKE2b-256 9a78317b51c66faabbfc521fa93ce766df16925ad9487f258021ead49be66dad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 287.7 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-1.0.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 15d3edf65eb91281991f9c02405c7c0c00d6e92cf50fefbd896b96df21c325a4
MD5 d236e4f893851fcd1c560ae41eb6003a
BLAKE2b-256 cf1dc3120242778e54577f4e8a06d7bf7edd7cc3fb2436044157b64d4967bcc5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 294.3 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-1.0.3-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3a80183e7f31a1c9d9e4777c34e62b08105131412b72498a7406702843cfe522
MD5 e30fd7cb6a48bd25cc708723e84df03b
BLAKE2b-256 95fbf10a388897129209458c73f8d09298ff58d3933dd4a5933fa388b44b352f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 289.9 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-1.0.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0d74328b0c9d6c4694ee2d0ab35232d1d7996cf1c205b253a636a9e1a2686490
MD5 3df83a8647a8bcf1f9dc565905825905
BLAKE2b-256 9aada780af68a036760efe928587e50f51c44b8a968f1bb5f88664df9b4f4fd9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 288.1 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-1.0.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 e68198bc7a27d5c485aefaeac4dbd8630f4b501a95b34b3d09993db00fa9bd45
MD5 4f5aca89c7622bc91cb9b2c39830b60c
BLAKE2b-256 96e84f8e5907c1610fd479b95f6691fa57c1d39cf32aef30118b129ac5d967f5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 296.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-1.0.3-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c23ff73ade789bc621fa1152d407622ba4bbe6b113fd84fcb65bdceb12997aab
MD5 33b68513ab84eaa7d67bf92b3c3310a4
BLAKE2b-256 d46deb71c9bc9e7e982727fbc07a3e9f93a60ba0e6a0d338173eaebfc093d66b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 287.9 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-1.0.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 924a73334503d69b17ad85af22378bffe55ffb7210a0966fac115831a3e1ddec
MD5 d2cad6fa65b0c6f05ccabfb32d108c55
BLAKE2b-256 2293fcf712105791cc16c9487daef6b881a1a8c9df565f9055dda3110877ec14

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 286.3 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-1.0.3-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 2e1359a6fe106904b129651fed3dd240df07fe0669aaa68755e0133a6f222e40
MD5 70ea6d4faef24675f24b3e9af3779a14
BLAKE2b-256 60d0fdf20201a12c598110e954701dde4b43bb89b55b541e9f4995b844aa73ac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 294.4 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-1.0.3-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 228665f648e5f88a21790c044428bd05d05b71e4466a89bba0752e0da0b8c791
MD5 f0833211964be7e44a846083be0159d2
BLAKE2b-256 0774d2b6edbfa91fd7fff7b74aa5d1cdcac6f5b07cc324bb1a79766d65403a43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 287.1 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-1.0.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6186dff418f4240a274ede369e69c304d7486de110f911d1b748d26af8138413
MD5 1239aff0380a612be7ef75ba3241f052
BLAKE2b-256 ebb88e2c5fdf8955d2d45ac553ad82fa866508e592824e572ec5249c95ccc28d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 286.3 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-1.0.3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 310568b273602bfa922866bd503fd1892aef6e62040af4a783bd5de24865b28e
MD5 e798ccd3a5d5cb85f8c7d71d745e0b2f
BLAKE2b-256 86beee5769609e2683bc0ee6d960b9eb232e5d3a511f92c244738e5559b6c9d8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 294.3 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-1.0.3-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c8bf0111ce888831e22d092a573135281b495dee9d9b3c18dddae0fcacd0e4ca
MD5 a92f79f144d33db1841ff54df747414c
BLAKE2b-256 128e97441bb157cac4e74d1ab41d6fc23fce0cca58e5627ad46b2fe493bcde41

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.3-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 288.5 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-1.0.3-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fb575a6b288da19fed05ed5f97528e9242a276e9a109b18f94074117cc435431
MD5 5bd9d68f91d7f28748e197e3f6fd3f20
BLAKE2b-256 37e817c5faf64e84e2b651ad266c52fe692b83efb58b8ba7ee9ec7bb4e0d4bdc

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