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

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

Uploaded Source

File details

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

File metadata

  • Download URL: rubymarshal-1.2.7.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.4

File hashes

Hashes for rubymarshal-1.2.7.tar.gz
Algorithm Hash digest
SHA256 94aa84fa42393f773c8215fab679bd3b72bbdb9f7931643d3672184cde9981d9
MD5 f24de49486964da3825a26654f7b28ff
BLAKE2b-256 97c8f8bde897d73ce51128609a2c72f7138c9d3a708864574f0b01aab7c4057f

See more details on using hashes here.

Supported by

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