Skip to main content

Read and write Ruby-marshalled data

Project description

RubyMarshal

Read and write Ruby-marshalled data. Only basics Ruby data types can be directly read and written, but you can use any custom Python and Ruby types:

  • float,
  • bool,
  • int,
  • str (mapped to rubymarshal.classes.RubyString if dumped with instance variables),
  • nil (mapped to None in Python),
  • array (mapped to list),
  • hash (mapped to dict),
  • symbols and other classes are mapped to specific Python classes.

Installation

    pip install rubymarshal

Usage

    from rubymarshal.reader import loads, load
    from rubymarshal.writer import writes, write
    with open('my_file', 'rb') as fd:
        content = load(fd)
    with open('my_file', 'wb') as fd:
        write(fd, my_object)
    loads(b"\x04\bi\xfe\x00\xff")
    writes(-256)

You can map custom Ruby types to Python ones:

    from rubymarshal.reader import loads
    from rubymarshal.classes import RubyObject, registry

    class DomainError(RubyObject):
        ruby_class_name = "Math::DomainError"

    registry.register(DomainError)

    loads(b'\x04\x08c\x16Math::DomainError', registry=registry)

You can use custom registries instead of the global one:

    from rubymarshal.reader import loads
    from rubymarshal.classes import RubyObject, ClassRegistry

    class DomainError(RubyObject):
        ruby_class_name = "Math::DomainError"

    registry = ClassRegistry()
    registry.register(DomainError)

    loads(b'\x04\x08c\x16Math::DomainError', registry=registry)

You can use Ruby's symbols:

    from rubymarshal.reader import loads
    from rubymarshal.writer import writes
    from rubymarshal.classes import Symbol

    x = Symbol("test")
    dump = writes(Symbol("test"))
    y = loads(dump)
    assert y is x

The default Writer class is customizable to write custom Python classes:

    from rubymarshal.writer import writes, Writer
    from rubymarshal.classes import Symbol

    class Constant:
        def __init__(self, name):
            self.name = name

    class ConstantWriter(Writer):
        def write_python_object(self, obj):
            if isinstance(obj, Constant):
                return self.write(Symbol(obj.name))
            super().write_python_object(obj)

    dump = writes([Constant("test")], cls=ConstantWriter)
    print(dump)

Infos

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

rubymarshal-1.2.4.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

rubymarshal-1.2.4-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file rubymarshal-1.2.4.tar.gz.

File metadata

  • Download URL: rubymarshal-1.2.4.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1

File hashes

Hashes for rubymarshal-1.2.4.tar.gz
Algorithm Hash digest
SHA256 a6f8658556bb0f7484c7bcb3c6ccf7a06feadb9297a5eedeb80a193ec001d681
MD5 886ecff8042f2b5002e59c5f3c620e70
BLAKE2b-256 9fa8fb894a95550eb242c16930c0fbe8bdda995dd8bcfe0f68a5aae78de3c249

See more details on using hashes here.

File details

Details for the file rubymarshal-1.2.4-py3-none-any.whl.

File metadata

  • Download URL: rubymarshal-1.2.4-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1

File hashes

Hashes for rubymarshal-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 182728a79259e12cc4ff87843451017ec99b490ea2e2db3a2916f90a4d3ae171
MD5 3c197bc3279777861836da33ec5e0773
BLAKE2b-256 d5cd778c3763a774f4bf1b14bc7258154599ab322e12c5ed2c9a869e658bdd87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page