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 torubymarshal.classes.RubyString
if dumped with instance variables),nil
(mapped toNone
in Python),array
(mapped tolist
),hash
(mapped todict
),- 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
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
The default Writer class is customizable to write extra 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
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
Release history Release notifications | RSS feed
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.3.tar.gz
(13.7 kB
view hashes)
Built Distribution
Close
Hashes for rubymarshal-1.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01410567d3821a29a2b373afb72f6846b29bf6533407be381e0642edb444a9b8 |
|
MD5 | 7a0e96004f1902832012073a7c9d75e4 |
|
BLAKE2b-256 | 929c7fc694586455893bf058fabddc0114d5381842819aa0569dcc98c9986560 |