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, content)
    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')

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.9.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

rubymarshal-1.2.9-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rubymarshal-1.2.9.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for rubymarshal-1.2.9.tar.gz
Algorithm Hash digest
SHA256 3aa4db1d5d9db8ee1298fe8ef7eb5f6920fb7ca389fcf9b35f97545bd7a80418
MD5 0cf569d46d4b292acc0797a75d34b4d6
BLAKE2b-256 397c17e82e0f80fea1ff8fa26f491aa42573d42f43029533d9f9861d046d847b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rubymarshal-1.2.9-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for rubymarshal-1.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d2960ee715402d51472d87235df69e3b621946c7e52b290d00a7448424143ba0
MD5 1b4d6e7494bf8269bc36aeefe809e032
BLAKE2b-256 7439a9534dead829fcdde98dc9164c811a630057e5ec7454f4d83f981dded978

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