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. Before installing, make sure that your pip, setuptools and wheel are up to date.

pip install -U pip setuptools wheel
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
path 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
path 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
path 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
path 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
path 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
path 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
path 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
path 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.yml", data)
Argument Type Description
path 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.yml")
Argument Type Description
path 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.3.2.tar.gz (314.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.3.2-cp39-cp39-win_amd64.whl (451.3 kB view details)

Uploaded CPython 3.9Windows x86-64

srsly-2.3.2-cp39-cp39-manylinux2014_x86_64.whl (456.5 kB view details)

Uploaded CPython 3.9

srsly-2.3.2-cp39-cp39-macosx_10_9_x86_64.whl (451.8 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

srsly-2.3.2-cp38-cp38-win_amd64.whl (451.7 kB view details)

Uploaded CPython 3.8Windows x86-64

srsly-2.3.2-cp38-cp38-manylinux2014_x86_64.whl (458.6 kB view details)

Uploaded CPython 3.8

srsly-2.3.2-cp38-cp38-macosx_10_9_x86_64.whl (449.6 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-2.3.2-cp37-cp37m-win_amd64.whl (449.9 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-2.3.2-cp37-cp37m-manylinux2014_x86_64.whl (456.4 kB view details)

Uploaded CPython 3.7m

srsly-2.3.2-cp37-cp37m-macosx_10_9_x86_64.whl (448.5 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

srsly-2.3.2-cp36-cp36m-win_amd64.whl (450.0 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-2.3.2-cp36-cp36m-manylinux2014_x86_64.whl (456.3 kB view details)

Uploaded CPython 3.6m

srsly-2.3.2-cp36-cp36m-macosx_10_9_x86_64.whl (450.7 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: srsly-2.3.2.tar.gz
  • Upload date:
  • Size: 314.5 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-2.3.2.tar.gz
Algorithm Hash digest
SHA256 f78eaef8d982e98921ce026d4205e652a9333137259f0b621f5c7b579e746e9d
MD5 d19227388fc4566118182ccf747a79ee
BLAKE2b-256 b06e3e4e3126088e9a372d3c903045520fe1c8e0b727ecb0a36b58d86fe0ea67

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 451.3 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-2.3.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 b1adf08c6a236ae4b3026783ee60e91f8a6423ad57b989be437b01fe786fd853
MD5 760d9506d1da1c8d6a25e8efd2d5a2c8
BLAKE2b-256 38b4e5b4a320e58db1aa37819be8da8bae6387a3b6bfe69f6e2ee58e30286854

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.5 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-2.3.2-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b775027e6cf66b1c7a52137acc3d08cd2bb3ba5bae505eb7a3f4beae851a07ff
MD5 b480d81b48d5b3b382a759b6c92ea1f4
BLAKE2b-256 bd710c27900c5f2b1baec1d4802c742bf9e59bde59a0bc9f216b381283b85d7b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 451.8 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-2.3.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d8b119494cfeadc7cfd09e116d7cb715fc1f052cd42d949cce3c5b77378309a4
MD5 501e7f121571de2f7b23e31535aa89da
BLAKE2b-256 3a58bdb38066ac852220d260f144a37ade98df20b814ff24e6dd3b6f84f3a576

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 451.7 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-2.3.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 46a2f928737fafabc105d1ee3a9a312dc2a09ec525731070aaff1118cda14837
MD5 ef04244929fab98a1ea8b23737a47383
BLAKE2b-256 e211002fc0a4ade6f18864060059bd5a96b908f59c9c5185070b6cb2e32d7453

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 458.6 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-2.3.2-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98f0b79c4bc1fc39762b8c84336e286a54ddf5e25da2a485849a8094a2042d90
MD5 1d93d793eafa39ba5151c3675fb7071a
BLAKE2b-256 6a5a027359fd24437875dc5bbc137a380d9cb419f95fd783d5079c72e35461bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 449.6 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-2.3.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 90edca1d7d69dac9177bc148a488b654cad2d84bf43d181e9c28099e91264c6e
MD5 843f176a713e9b0a914ec1ff30b658b3
BLAKE2b-256 4c0c82f36afada6cfae88208fdb3f7a57fb2ce87da276ccf6a3957de35988cca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 449.9 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-2.3.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 32a10e4c36b06789ad618d5770d7060fe472596ce919ff8aca0c8fbbea13ecd7
MD5 a7d97382355e6a671ca201d29bd4448e
BLAKE2b-256 b1873471f377230c37b2d1ecc18b0a5b2ca38cf9f14e0431c5970eecaac5fa01

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.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-2.3.2-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8fe50e5b313167e323b01051bf8287ec63d6987fe70a5cbc46ea64641a3d3791
MD5 e784a4d38c862b2856dfd6ea66975533
BLAKE2b-256 0c4bf0c49d67ec3c62e7f5d5d352731a37be170add8294be5a67abbae4802419

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 448.5 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-2.3.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8bd9963bd3134ddf90bc366d34809830884d3fe2d98bdf845a5a196db88ada6c
MD5 3e2fa5fa7adb1694771835ed034fb7e5
BLAKE2b-256 5a904811cf3df9ed2237104505667544455e893f0cd07f2115dd0d044c046246

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 450.0 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-2.3.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 a54934e4a6605fa0249eda0bd049711cfa4a70e6d70fcde03e8fa20a3901cccb
MD5 787b413ef7a3d4be81fa45841b80f480
BLAKE2b-256 0adc154434612bf80d827622026c6eaa43e102259020c29f367710c5c58ba491

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 456.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-2.3.2-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 425260e030c3a1a6723c4d8b7a678877a5e22d7665c8cf7187da44835483ff18
MD5 420c56ebe3588f6f062248342b1efa14
BLAKE2b-256 b42cf349f9423d83cde4610899f18b3eed621e59f62f395d954038a6014869b2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.3.2-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 450.7 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-2.3.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1a522631dafd918801aa2e37b276734c78e65882196ebed8cbeeb579947d6b06
MD5 fa0a5a1b6f8367b145f144f6c9ca0827
BLAKE2b-256 c057d0650604091f8a874ef0ee2add519890a003cf51af1e4ae7463e99393d5f

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