Code Monkey home page Code Monkey logo

ast_ast_pickler's Introduction

ast-pickle

Description

ast-pickle is a proof of concept serialization library that generates Python code to construct objects.

Lately I've been interested in the "code is data" idiom. The idea of serialization is to create instructions that can be used to re-build the object graph at a later time. Normally, serialization makes use of an additional layer written in the language itself, or specific optimized C code, which builds and interprets these instructions. ast-pickle takes a different approach and generates .pyc files that contain Python code to directly construct the object.

One would expect this to be reasonably fast, at least compared to the other pure-Python solutions. However, I have not yet benchmarked it.

Usage

Implements the same interface as marshal and pickle:

  • dump -- Write an object to a file.
  • dumps -- Write an object to a string, and return the string.
  • load -- Load object from a file.
  • loads -- Load object from a string.

The result of dump and dumps is a marshalled Python module object (.pyc). It is also possible to get the intermediate ast with generate_module, which can be converted to text Python code using the supplied codegen module.

Warnings / Limitations

  • Does not support cycles in the object graph (unlike pickle). These currently result in an infinite loop.
  • Does not support all the Python built-in types yet.
  • As this library literally generates and executes Python code, the same warnings apply as for pickle and marshal:

do not use this over untrusted channels, or accept serialized objects in this format from untrusted sources.

  • Like with pickle, the module which contains pickled objects must be available for import when unpickling.
  • Note: this is just a toy experiment, it is not well-tested enough for production code. It needs code to handle corner cases etc...

Comparison to marshal

Marshal is the underlying format for Python modules. However, marshal can only serialize basic python data types, not complete objects. This library can do the same, but also supports serialization of arbitrary objects...

TODO

  • Support cycles in the object graph
  • Fall back on __getstate__ / __setstate__ if object does not support __to_node__ and cannot be trivially picked with its dict
  • As constructing an ast is over-verbose and annoying, parametrized ast fragments would be useful (can these be generated by the python parser or do we need to build our own?)

Authors

  • Wladimir van der Laan

ast_ast_pickler's People

Contributors

laanwj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.