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 read and written:

  • 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 Ruby types to Python ones:

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

    class DomainError(RubyObject):
        ruby_class_name = "Math::DomainError"
    
    class_mapping = {"Math::DomainError": DomainError}

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

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

Infos

Code is on github: https://github.com/d9pouces/RubyMarshal Documentation is on readthedocs: http://rubymarshal.readthedocs.org/en/latest/ Tests are on travis-ci: https://travis-ci.org/d9pouces/RubyMarshal

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

Uploaded Source

File details

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

File metadata

  • Download URL: rubymarshal-1.2.2.tar.gz
  • Upload date:
  • Size: 12.7 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.2.tar.gz
Algorithm Hash digest
SHA256 ae29dcb7f07bc6c6f3665ca1b145b95a041f0daa10f332e6674fb1e1b5396050
MD5 12aac167406fdbaaa4feb07c4534da68
BLAKE2b-256 4235660872cf5845bf780d0bf3f0bb4917fe0fc2a191b419bb7e36ca9c22bccb

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