PyO3's PyAny as a serde data format

Overview

serde-pyobject

PyO3's PyAny as a serde data format

Usage

Serialize T: Serialize into &'py PyAny:

use serde::Serialize;
use pyo3::{Python, types::{PyAny, PyDict}};

#[derive(Serialize)]
struct A {
    a: u32,
    b: String,
}

Python::with_gil(|py| {
    let a = A { a: 1, b: "test".to_string() };
    let obj: &PyAny = serde_pyobject::to_pyobject(py, &a).unwrap();
    assert!(obj.is_instance_of::<PyDict>());
});

Deserialize &'py PyAny into T: Deserialize<'de>:

use serde::Deserialize;
use pyo3::{Python, types::{PyAny, PyDict}};
use serde_pyobject::{from_pyobject, pydict};

#[derive(Debug, PartialEq, Deserialize)]
struct A {
    a: u32,
    b: String,
}

Python::with_gil(|py| {
    let a: &PyDict = pydict! { py,
      "a" => 1,
      "b" => "test"
    }
    .unwrap();
    let a: A = from_pyobject(a).unwrap();
    assert_eq!(a, A { a: 1, b: "test".to_string() });
});

Mapping between Python and serde data model

serde data model PyO3 type Rust Python
i8, i16, i32, i64, isize,
u8, u16, u32, u64, usize
PyLong 123 123
f32, f64 PyFloat 1.0 1.0
bool PyBool true true
char, string PyString 'a', "test" "a", "test"
option PyAny 1 None, Some(1) None, 1
unit PyTuple () ()
unit struct PyTuple struct Unit ()
unit variant PyDict E::A in enum E { A, B } { "E": "A" }
newtype struct PyDict A(32) of struct A(u8) { "A": 32 }
newtype variant PyDict E::N(41) of enum E { N(u8) } { "E": ("N", 41) }
seq PyList vec![1, 2, 3] [1, 2, 3]
tuple PyTuple (1, "test") (1, "test")
tuple struct PyDict T(1, "a") of struct T(u32, String) { "T": (1, "a") }
tuple variant PyDict - -
map PyDict - -
struct PyDict A { a: 1, b: "test" } of struct A { a: u32, b: String } { "A": { "a": 1, "b": "test"} }
struct variant PyDict - -

License

© 2023 Jij Inc.

This project is licensed under either of

at your option.

Footnotes

  1. Some(value) is serialized as value

You might also like...
CLI tool that make it easier to perform multiple lighthouse runs towards a single target and output the result in a plotable format.

Lighthouse Aggregator CLI tool that make it easier to perform multiple lighthouse runs towards a single target and output the result in a "plotable" f

Single File Assets is a file storage format for images

SFA (Rust) Single File Assets is a file storage format for images. The packed images are not guaranteed to be of same format because the format while

CLI application to run clang-format on a set of files specified using globs in a JSON configuration file.
CLI application to run clang-format on a set of files specified using globs in a JSON configuration file.

run_clang_format CLI application for running clang-format for an existing .clang-format file on a set of files, specified using globs in a .json confi

Format codebase in documentation 🦤

Gelatyx Format codebase in documentation 🦤 Features Format language code block inside documentation files Check mode. Ask Gelatyx is the documentatio

A low-level MVCC file format for storing blobs.

Sediment This repository isn't ready for public consumption. It just reached a stage where I wanted to start sharing ideas with others as well as usin

Grid-based drum sequencer plugin as MIDI FX in CLAP/VST3 format
Grid-based drum sequencer plugin as MIDI FX in CLAP/VST3 format

dr-seq Grid-based drum sequencer plugin as MIDI FX in CLAP/VST3 format. WARNING: This project is in a very early state. So there is no guarantee for a

jf "jf: %q" "JSON Format"

jf jf "jf: %q" "JSON Format" jf is a jo alternative to help safely format and print JSON objects in the commandline. However, unlike jo, where you bui

A tool to filter sites in a FASTA-format whole-genome pseudo-alignment

Core-SNP-filter This is a tool to filter sites (i.e. columns) in a FASTA-format whole-genome pseudo-alignment based on: Whether the site contains vari

Polyexen demo of Plonkish Arithmetiation Format (Plaf) on the zkevm-circuits

Plaf demo This is a demo of Plaf: Plonkish Arithmetiation Format on the zkevm-circuits Steps to run this: Clone these three repositories in the same f

Releases(0.1.0)
  • 0.1.0(Nov 20, 2023)

    What's Changed

    • Deserialize to PyAny by @termoshtt in https://github.com/Jij-Inc/serde-pyobject/pull/1

    New Contributors

    • @termoshtt made their first contribution in https://github.com/Jij-Inc/serde-pyobject/pull/1

    Full Changelog: https://github.com/Jij-Inc/serde-pyobject/commits/0.1.0

    Source code(tar.gz)
    Source code(zip)
Owner
Jij
Jij
Given a set of kmers (fasta format) and a set of sequences (fasta format), this tool will extract the sequences containing the kmers.

Kmer2sequences Description Given a set of kmers (fasta / fastq [.gz] format) and a set of sequences (fasta / fastq [.gz] format), this tool will extra

Pierre Peterlongo 22 Sep 16, 2023
Serde definition of Cargo.toml structure

Deserialize Cargo.toml This is a definition of fields in Cargo.toml files for serde. It allows reading of Cargo.toml data, and serializing it using TO

null 11 Dec 20, 2023
Databento Binary Encoding (DBZ) - Fast message encoding and storage format for market data

dbz A library (dbz-lib) and CLI tool (dbz-cli) for working with Databento Binary Encoding (DBZ) files. Python bindings for dbz-lib are provided in the

Databento, Inc. 15 Nov 4, 2022
UniSBOM is a tool to build a software bill of materials on any platform with a unified data format.

UniSBOM is a tool to build a software bill of materials on any platform with a unified data format. Work in progress Support MacOS Uses system_profile

Simone Margaritelli 32 Nov 2, 2022
An apocalypse-resistant data storage format for the truly paranoid.

Carbonado An apocalypse-resistant data storage format for the truly paranoid. Designed to keep encrypted, durable, compressed, provably replicated con

diba-io 30 Dec 29, 2022
a simple, non-self-describing data-interchange format.

rust-fr 'rust-fr' (aka rust for real) is a simple, non-self-describing data-interchange format. installation You can use either of these methods. Add

Ayush 4 Feb 28, 2024
Concurrent and multi-stage data ingestion and data processing with Rust+Tokio

TokioSky Build concurrent and multi-stage data ingestion and data processing pipelines with Rust+Tokio. TokioSky allows developers to consume data eff

DanyalMh 29 Dec 11, 2022
⚗️ Superfast CLI interface for the conventional commits commit format

resin ⚗️ Superfast CLI interface for the conventional commits commit format ❓ What is resin? resin is a CLI (command-line interface) tool that makes i

Matt Gleich 23 Oct 12, 2022
⚗️ Superfast CLI interface for the conventional commits commit format

resin ⚗️ Superfast CLI interface for the conventional commits commit format ❓ What is resin? resin is a CLI (command-line interface) tool that makes i

Matt Gleich 23 Oct 12, 2022
Crate to generate files in ROFF format (Rust)

roffman A crate to generate roff man pages. Usage Add the following to the Cargo.toml: [dependencies] roffman = "0.3" Example use roffman::{Roff, Roff

Wojciech Kępka 23 Jul 13, 2022