Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Python module to "thaw" data frozen using Perl's Storable?

I have a legacy database which contains simple data structures (no CODE refs thank goodness) that have been written using the nfreeze method of the Storable module in Perl.

Now I have a need to load this data into a Python application. Does anyone know of a Python implementation of Storable's thaw? Google hasn't helped me.

If it comes to it, I can reverse engineer the data format from the Storable source, but I'd prefer to avoid that fun if it's been done already.

To express in code: Given a Perl program like this:

#!/usr/bin/perl
use strict;
use warnings;

use MIME::Base64;
use Storable qw/nfreeze/;

my $data = {
    'string' => 'something',
    'arrayref' => [1, 2, 'three'],
    'hashref' => {
        'a' => 'b',
    },
};

print encode_base64( nfreeze($data) );

I'm after a magic_function such that this Python:

#!/usr/bin/env python
import base64
import pprint
import sys

def magic_function(frozen):
    # A miracle happens
    return thawed

if __name__ == '__main__':
    frozen = base64.b64decode(sys.stdin.read())
    data = magic_function(frozen)
    pprint.pprint(data)

prints:

{'string': 'something', 'arrayref': [1, 2, 'three'], 'hashref': {'a': 'b'}}

when run against the output of the Perl program.

like image 224
Day Avatar asked Mar 29 '11 21:03

Day


2 Answers

It's not immediately clear to me how far along this project is, but it appears to aim to do what you want:

https://pypi.org/project/storable/

like image 73
senderle Avatar answered Nov 04 '22 12:11

senderle


If your first option doesn't work, another option would be to write a simple perl script to thaw the data, and then write it out in JSON or YAML or some format that you can easily work with in Python.

like image 30
runrig Avatar answered Nov 04 '22 12:11

runrig