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 torubymarshal.classes.RubyStringif dumped with instance variables),nil(mapped toNonein 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, 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
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rubymarshal-1.2.10.tar.gz.
File metadata
- Download URL: rubymarshal-1.2.10.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
899b46e6485200d087858ff56295881764ff5268f6fdf01b7ecc4e4e03d3ed7c
|
|
| MD5 |
869c82457b758b53ee3850f192c9e8ef
|
|
| BLAKE2b-256 |
4ae8c7b7da8861475dcc7388042c84da68a74a5a425ad6b37b572a727326aa53
|
File details
Details for the file rubymarshal-1.2.10-py3-none-any.whl.
File metadata
- Download URL: rubymarshal-1.2.10-py3-none-any.whl
- Upload date:
- Size: 21.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cac74725cf6024efd143dad9e7c46c9f0c3a4ffa94c53ca546640de83f54555
|
|
| MD5 |
e06cea9be6d2fea373a7bfdfa4ec88fd
|
|
| BLAKE2b-256 |
7cc2a52d6d66a878d16e6cfa8f89ba0395540decf37e0e81fa2d7e1a80e6fb3b
|