开发者

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

开发者 https://www.devze.com 2023-02-21 03:50 出处:网络
I have a legacy database which contains simple data structures (no CODE refs thank goodness) that have been written using the nfreeze me开发者_开发知识库thod of the Storable module in Perl.

I have a legacy database which contains simple data structures (no CODE refs thank goodness) that have been written using the nfreeze me开发者_开发知识库thod 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.


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/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消