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.1.tar.gz (193.4 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.1-cp38-cp38-win_amd64.whl (307.0 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-2.0.1-cp38-cp38-manylinux1_x86_64.whl (311.6 kB view details)

Uploaded CPython 3.8

srsly-2.0.1-cp38-cp38-macosx_10_9_x86_64.whl (309.4 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-2.0.1-cp37-cp37m-win_amd64.whl (305.2 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-2.0.1-cp37-cp37m-manylinux1_x86_64.whl (311.0 kB view details)

Uploaded CPython 3.7m

srsly-2.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (308.2 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-2.0.1-cp36-cp36m-win_amd64.whl (305.3 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-2.0.1-cp36-cp36m-manylinux1_x86_64.whl (311.4 kB view details)

Uploaded CPython 3.6m

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

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: srsly-2.0.1.tar.gz
  • Upload date:
  • Size: 193.4 kB
  • Tags: Source
  • 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.1.tar.gz
Algorithm Hash digest
SHA256 fa3c7375be8fe75f23c27feafbfb5f738d55ffdbf02964c6896fb7684f519a52
MD5 52631199d5fe0143bad554d3eaf1622c
BLAKE2b-256 f40b22c6caff32757e6c350dcaecf3a55f4df57e15c9a8eaa8c6db6f29b99b54

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 307.0 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.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.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 ad8847d4836912ad659cda6d2f5adcae01a472acbc57bd4875934dbd21684c2c
MD5 144276863d9676c308036297b4a11911
BLAKE2b-256 816235160f4f08750c8d10490ac2e8166f82b335286153e83cf953ff04383eb8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 311.6 kB
  • Tags: CPython 3.8
  • 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.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e485fefa466e79ed17bcf47e3a0d061b295727a7dd91d5e97e09bb978f76c43d
MD5 bde5a306adf493bf2eda536e300f5987
BLAKE2b-256 f6267fcf0ae59a629019c3f7e339496bb2106cd43d051ab734d118fd523a739e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 309.4 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.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.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6a75ef36d28232666dd252488817fff056cfe3a0c5bd223f360e76428f8beddb
MD5 9d20534fe9934a41b7d9c9c4e1edeaa5
BLAKE2b-256 f7e9a0525041b79aac6ba6e7bd72c76048e285c8891614d93c41886bb7e42cf5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 305.2 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.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.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 776bdd058003079151d90e1dc23a70db6d0fced58f9d5332315f3857d092b033
MD5 493657acc14da6ac3b39103c42650091
BLAKE2b-256 ccc778f063e447b13a7307558d4ad058ea54a96bb1fac0ec7a21b91a9eab913d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 311.0 kB
  • Tags: CPython 3.7m
  • 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.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ddcb3f920b6d53d6d99d7eabf5ef103a837603f0a8d099751e5087de37e85229
MD5 223d3ba76ffad9ff1c5a51dc531d39bd
BLAKE2b-256 aad44defd08649bbfef54ec70856d79035b7362976ee50810f4ebd3db2266673

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 308.2 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.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.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 82dabdbf3057b266402edaefb9bee3b8779da618857b0c3df04381734755a608
MD5 057dd9ae9aba0550588bd295870791b9
BLAKE2b-256 dbb9a363f1a3ced4ed650d5ca0fbf4721ad8d6249af1a7045865df22746ea053

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 305.3 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.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.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 edf879fa7ff5c200951d325168aff1125021b045a5707bc06bfd5e1996d74d40
MD5 ee27a43d6db78a211bb45f341c659283
BLAKE2b-256 27679f4dccdfea5a0305f2cd988a5390ce4bce77b4188bb43fa2ead43bc26c80

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 311.4 kB
  • Tags: CPython 3.6m
  • 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.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c6fc930043dc6e7212974cfb5eee35c17854e840a8deb2b42b22397ab8cb6c65
MD5 11bf2ae5712253a378e71d0e1b65ebed
BLAKE2b-256 3dc49f22aa135736e309281ee03c4b34de89c1867597569fe6e16c7ad2040deb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.1-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.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 eea9a7906a646436a1e170e900199b778a0914848635c06aec6ffdd79c507f3c
MD5 895a79d3fcd303a5da515d03676932f9
BLAKE2b-256 e626f62c13f4f184f96fc64a9fc46797c854fdda10caac22b4fc4c8923ea8099

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