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
------------
```python3
pip install rubymarshal
```
Usage
-----
```python3
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:
```python3
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:
```python3
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:
```python3
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:
```python3
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
Raw data
{
"_id": null,
"home_page": "https://github.com/d9pouces/RubyMarshal",
"name": "rubymarshal",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Matthieu Gallet",
"author_email": "github@19pouces.net",
"download_url": "https://files.pythonhosted.org/packages/97/c8/f8bde897d73ce51128609a2c72f7138c9d3a708864574f0b01aab7c4057f/rubymarshal-1.2.7.tar.gz",
"platform": "",
"description": "RubyMarshal\n===========\n\nRead and write Ruby-marshalled data.\nOnly basics Ruby data types can be directly read and written, but you can use any custom Python and Ruby types: \n\n * `float`,\n * `bool`,\n * `int`,\n * `str` (mapped to `rubymarshal.classes.RubyString` if dumped with instance variables),\n * `nil` (mapped to `None` in Python),\n * `array` (mapped to `list`),\n * `hash` (mapped to `dict`),\n * symbols and other classes are mapped to specific Python classes.\n\nInstallation\n------------\n\n```python3\n pip install rubymarshal\n```\n\nUsage\n-----\n\n```python3\n from rubymarshal.reader import loads, load\n from rubymarshal.writer import writes, write\n with open('my_file', 'rb') as fd:\n content = load(fd)\n with open('my_file', 'wb') as fd:\n write(fd, my_object)\n loads(b\"\\x04\\bi\\xfe\\x00\\xff\")\n writes(-256)\n```\n\nYou can map custom Ruby types to Python ones:\n\n```python3\n from rubymarshal.reader import loads\n from rubymarshal.classes import RubyObject, registry\n\n class DomainError(RubyObject):\n ruby_class_name = \"Math::DomainError\"\n \n registry.register(DomainError)\n\n loads(b'\\x04\\x08c\\x16Math::DomainError')\n```\n\n\nYou can use custom registries instead of the global one:\n\n\n```python3\n from rubymarshal.reader import loads\n from rubymarshal.classes import RubyObject, ClassRegistry\n\n class DomainError(RubyObject):\n ruby_class_name = \"Math::DomainError\"\n \n registry = ClassRegistry()\n registry.register(DomainError)\n\n loads(b'\\x04\\x08c\\x16Math::DomainError', registry=registry)\n```\n\nYou can use Ruby's symbols:\n\n```python3\n from rubymarshal.reader import loads\n from rubymarshal.writer import writes\n from rubymarshal.classes import Symbol\n \n x = Symbol(\"test\")\n dump = writes(Symbol(\"test\"))\n y = loads(dump)\n assert y is x\n```\n\n\nThe default Writer class is customizable to write custom Python classes:\n\n```python3\n from rubymarshal.writer import writes, Writer\n from rubymarshal.classes import Symbol\n \n class Constant:\n def __init__(self, name):\n self.name = name\n \n class ConstantWriter(Writer):\n def write_python_object(self, obj):\n if isinstance(obj, Constant):\n return self.write(Symbol(obj.name))\n super().write_python_object(obj)\n \n dump = writes([Constant(\"test\")], cls=ConstantWriter)\n print(dump)\n\n```\n\nInfos\n-----\n\n * Code is on github: https://github.com/d9pouces/RubyMarshal \n * Documentation is on readthedocs: http://rubymarshal.readthedocs.org/en/latest/ \n * Tests are on travis-ci: https://travis-ci.org/d9pouces/RubyMarshal",
"bugtrack_url": null,
"license": "WTFPL",
"summary": "Read and write Ruby-marshalled data",
"version": "1.2.7",
"project_urls": {
"Homepage": "https://github.com/d9pouces/RubyMarshal"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "97c8f8bde897d73ce51128609a2c72f7138c9d3a708864574f0b01aab7c4057f",
"md5": "f24de49486964da3825a26654f7b28ff",
"sha256": "94aa84fa42393f773c8215fab679bd3b72bbdb9f7931643d3672184cde9981d9"
},
"downloads": -1,
"filename": "rubymarshal-1.2.7.tar.gz",
"has_sig": false,
"md5_digest": "f24de49486964da3825a26654f7b28ff",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9647,
"upload_time": "2020-08-10T21:41:30",
"upload_time_iso_8601": "2020-08-10T21:41:30.285428Z",
"url": "https://files.pythonhosted.org/packages/97/c8/f8bde897d73ce51128609a2c72f7138c9d3a708864574f0b01aab7c4057f/rubymarshal-1.2.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-08-10 21:41:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "d9pouces",
"github_project": "RubyMarshal",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"test_requirements": [
{
"name": "hypothesis",
"specs": [
[
">=",
"5.24.0"
]
]
},
{
"name": "nose",
"specs": [
[
">=",
"1.3.7"
]
]
},
{
"name": "poetry",
"specs": []
},
{
"name": "pytest",
"specs": []
},
{
"name": "wheel",
"specs": []
}
],
"lcname": "rubymarshal"
}