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

Uploaded CPython 3.8Windows x86-64

srsly-1.0.1-cp38-cp38-manylinux1_x86_64.whl (185.7 kB view details)

Uploaded CPython 3.8

srsly-1.0.1-cp38-cp38-macosx_10_9_x86_64.whl (183.3 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

srsly-1.0.1-cp37-cp37m-win_amd64.whl (179.3 kB view details)

Uploaded CPython 3.7mWindows x86-64

srsly-1.0.1-cp37-cp37m-manylinux1_x86_64.whl (185.2 kB view details)

Uploaded CPython 3.7m

srsly-1.0.1-cp37-cp37m-macosx_10_6_intel.whl (271.8 kB view details)

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

srsly-1.0.1-cp36-cp36m-win_amd64.whl (179.3 kB view details)

Uploaded CPython 3.6mWindows x86-64

srsly-1.0.1-cp36-cp36m-manylinux1_x86_64.whl (185.3 kB view details)

Uploaded CPython 3.6m

srsly-1.0.1-cp36-cp36m-macosx_10_6_intel.whl (275.1 kB view details)

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

srsly-1.0.1-cp35-cp35m-win_amd64.whl (174.6 kB view details)

Uploaded CPython 3.5mWindows x86-64

srsly-1.0.1-cp35-cp35m-manylinux1_x86_64.whl (183.8 kB view details)

Uploaded CPython 3.5m

File details

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

File metadata

  • Download URL: srsly-1.0.1.tar.gz
  • Upload date:
  • Size: 192.5 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-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1102b4984f9f56364540e47d83fac3e7543903dfbb92f0d0e5dd3bfd40528934
MD5 e474968f37ea5274d52055cfd92c4f99
BLAKE2b-256 bc3a73c6e385afcbdb71218f83d44c67691a3ee5c47931838f92d607586a7a0e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 181.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.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.8

File hashes

Hashes for srsly-1.0.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 abe3d98d9ea8f7dac898119cd9861466c49cfe0f16287c9f859e0d4cab43a7a4
MD5 7f77ad62645945929dcaf55815625447
BLAKE2b-256 a1bb0982e39b1a6dd652d7605f199cc5209746145f3a9e677c0014302cc22f66

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 185.7 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-1.0.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 51c47f98dc06d5c2d1d7806cd38dcc834ab9906dc12170bc21105e5a9590a6fd
MD5 69898c582969e6509ed5b8e4f18adad4
BLAKE2b-256 0e9faff51da5ae4994db320d1b04e153a8e3669ea5f3a417cb5e633e04759125

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 183.3 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-1.0.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1c4354095f63f59fc52a4362960faaddebcfa7a240f07209eb50e8f9ec39e700
MD5 89b1b0424c716246b6277b0ab61de7cb
BLAKE2b-256 51db0c04bb0fccb7aef7165721c4ce4e50300db98a8f6c07c3aae81eb87d6b96

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 179.3 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-1.0.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 a672ffaa77680f355933cf424739ae9ecff767908a374ad194692b53040fda01
MD5 f690a6e191f8822fba27da1328e3f722
BLAKE2b-256 03a86ddcf5f8ed280e28a41cbef9b38ef8e4e005760c4eb1860bd239c9c1be99

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 185.2 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-1.0.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3ceae42dbbda49b57a4937e0ca28f56c2a121c89008cc7ec09e0a9d8d705c03e
MD5 049aa627c4bf64a9b9369e8d280eced6
BLAKE2b-256 0eaff0f2e6cff739421a7e6165d6b1d044eb2d9ef82865895d2528d2c4c6f9c5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp37-cp37m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 271.8 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-1.0.1-cp37-cp37m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 de329ba0ff451308d59e40c39372f5231e7c364f4933d7457788203630bdede2
MD5 a09992ec3a3372fada17fe2b62a75154
BLAKE2b-256 2233fa6fed9e0dbadc75f1f88780ae7a5182f3dd4019a007a7a787468965355e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 179.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.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.8

File hashes

Hashes for srsly-1.0.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 4ce9d6ab6d1c617150455ef5ba8abd5107a8e65956f06c2efc86697f4cb4b431
MD5 92e678cf1798ddb6ad8be1901e138d66
BLAKE2b-256 7fb1c23588e5419575469d3508c5a41e582444e38d40ff632e72ae25b7bf5be8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 185.3 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-1.0.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d5c0c718b2f67fc425d9bb3cc26b6141cb2f53251cdc145f58b70095241a3308
MD5 c53b50a13f1eb5352fb0441e8dcf4a44
BLAKE2b-256 4f963350d3fa0cfa2b2ff341113d60b5bfe0ab8dd0e6b6b2c8b12157b4eb3000

See more details on using hashes here.

File details

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

File metadata

  • Download URL: srsly-1.0.1-cp36-cp36m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 275.1 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-1.0.1-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 c82e6dc3727454edc6ccdb1d07d5bc0aab3f43539fb8d9f973cf769135d2c7e4
MD5 104bb4698d822f24e732983be5ab7d7a
BLAKE2b-256 05172471890a71f730d91dca80fc6e730e7c261040d8b015f234077257b26410

See more details on using hashes here.

File details

Details for the file srsly-1.0.1-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: srsly-1.0.1-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 174.6 kB
  • Tags: CPython 3.5m, 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-1.0.1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 c6bdf53a87770139c6a9d75b3e664505bd81c022312fafca35ed38714e4ecdf1
MD5 d69e9d2e54e2c9a3425abdb1b22f9e33
BLAKE2b-256 d6998d8f1f1801b036216dc51ecb5836aee665783630c48d425ec543deeab47a

See more details on using hashes here.

File details

Details for the file srsly-1.0.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: srsly-1.0.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 183.8 kB
  • Tags: CPython 3.5m
  • 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-1.0.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ca1ec20ea6e14ad56ccaa84aa6c79d6e51fccf32e0040372b4d06c6e5dbb7fee
MD5 bea304a9ba459e62d9036d52ffef794b
BLAKE2b-256 cddc0f83f6159f4d9ce65ac84467318b9afb6b62fc70ff326f7aa4ef0b1d75af

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