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.dev1.tar.gz (192.0 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.dev1-cp38-cp38-win_amd64.whl (305.5 kB view details)

Uploaded CPython 3.8Windows x86-64

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

Uploaded CPython 3.8

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

Uploaded CPython 3.8macOS 10.9+ x86-64

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

Uploaded CPython 3.7mWindows x86-64

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

Uploaded CPython 3.7m

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

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

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

Uploaded CPython 3.6mWindows x86-64

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

Uploaded CPython 3.6m

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

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

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1.tar.gz
  • Upload date:
  • Size: 192.0 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.dev1.tar.gz
Algorithm Hash digest
SHA256 2b7ac3f5b9cdd36952e83aeb93a7ce7998b4dc1882133e4523fcb626140ab707
MD5 8b9b9084f609149f7b1b0d73080e4fb3
BLAKE2b-256 1920096a13911be3adfc7baabe0e9bdd7689dae2875d60c4f5c2ac6948055e49

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for srsly-2.0.0.dev1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 b67b2e1d31adc25b4ceeef2aab62d50c03459c6be1d9f751e04767628a0a92cc
MD5 82468acda6cd59a465d24dcabbfeea37
BLAKE2b-256 2d9ce2fa4e5d951ea58cda6842fe8cc616c85160965fcf2d9cebe0029875fa73

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 310.2 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.dev1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c6967feac1fd67c5f754e2d065a7c72406f08849b2c5b2d6ed11a3709bc18d63
MD5 b078f8dcfa3ecf2d11dc666aeaa4257c
BLAKE2b-256 d557f920e00445e4fe0db29562f3ffb09f6ec28d6b79321bfd1fa385e968e37e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 307.9 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.dev1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3017e1478b5488dcc5ae64a190c812c06759d0de85f8113379651d24c080f6a2
MD5 9f17d5f23d1dc249b3efd31892ccb381
BLAKE2b-256 9874a79f00f5737fb243057865ad608b6c29393b91c5fbbf6bffbb386f2027a2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for srsly-2.0.0.dev1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 64704bf797e8f24b6e462e54bcc7529d62545c35dc41ef29843594cce3331f07
MD5 ad911366e40401667b53bb034a921f75
BLAKE2b-256 f38332bf7d7bf19d27c876d1594fec73d7bfe6e22407e463bebd05ec1e2e1c2b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 309.5 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.dev1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a205732b18fd3b3b847bbb49f3057b51b071e44167c56e427946ad10007fb8e5
MD5 15a1b6111ef8b53006f22697d00736c4
BLAKE2b-256 d455a460badbd4e130c57471e36703b8c464020add25797b0256848d683e6458

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1-cp37-cp37m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 396.9 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.dev1-cp37-cp37m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 29807bf1197d0cd18274157c7321e8b1e3ac081484e82c390654e783fe6ee15b
MD5 2bb5b9e9605fca84ed792e7735d35fc9
BLAKE2b-256 c377c112effc4e8a3bec556f790b2b7d1da1c89ec9827f96f4498bcdfaae3ec7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 303.8 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.dev1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 41d9711a3f83af35119c1749ddd03724d762bb58d96633556217179a2d8630aa
MD5 7222546d5fa2030ddcdd0b5291a44851
BLAKE2b-256 9ab9709bc1f33a705ecf074f620cea59268e14ed07bbcd8cc19f949d309406e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 309.9 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.dev1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4bdffac9261eff1c6691765f2e6441e3cac1da9a8ba1e8f470734ebaf90f3558
MD5 b1303eb640c5e2ec7fcb5b4b2210bf25
BLAKE2b-256 1e6e99f1f8a7bc171776c3d926f1502dc09faf4653253f0f7aaf218316fd6e61

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-2.0.0.dev1-cp36-cp36m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 400.4 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.dev1-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 2f572861ce38bce52a80b48f216166df2f5f6311a371ba83e7fb0c1eaad4dffd
MD5 2573e562f0ed2fa191d0329e9af87552
BLAKE2b-256 ffa0e8ef48e7f891816fa7c5e9d9f2879d49a3e2533a69eea6e55d02b7cdc608

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