Code Monkey home page Code Monkey logo

literal-python-to-pickle's Introduction

Convert literal Python to Pickle / Faster ast.literal_eval

Why

ast.literal_eval is very slow. json.loads shows that it can be much faster. pickle.loads is yet faster.

This provides a much faster alternative to ast.literal_eval.

(Related StackOverflow question.)

Usage

Can be compiled as a standalone executable (mostly for testing), or as a library.

Library compilation:

c++ -std=c++11 -DLIB py-to-pickle.cpp -shared -fPIC -o libpytopickle.so

Then you can use common.py_to_pickle(s: bytes) -> bytes.

In your code, if you have eval(s), replace it by pickle.loads(py_to_pickle(s))

Statistics

Run ./time-read.py:

Gunzip + read time: 0.1663219928741455
Size: 22540270
py_to_pickle: 0.539439306
pickle.loads+py_to_pickle: 0.7234611099999999
compile: 3.3440755870000003
parse: 3.6302585899999995
eval: 3.306765757000001
ast.literal_eval: 4.056752016000003
json.loads: 0.3230752619999997
pickle.loads: 0.1351051709999993
marshal.loads: 0.10351717500000035

So our implementation is 4.5x faster than eval, and 5.5x faster than ast.literal_eval.

Properties

We consider objects which can be serialized with repr and reconstructed via ast.literal_eval. I.e. it uses only the basic types: str, bool, int, float, list, tuple, dict, set. I.e. we assume for such an object obj:

obj == ast.literal_eval(repr(obj))

We guarantee:

obj == pickle.loads(py_to_pickle(repr(obj)))

(At least that is the intention. If this is not the case, consider this a bug.) (At the time of writing, it is slightly incomplete. E.g. bool and tuple not supported yet.)

This is not intended to 100% cover all valid Python code (but all code generated by repr), i.e. maybe might not hold for some bytes s (although we are not aware of such cases):

ast.literal_eval(s) == pickle.loads(py_to_pickle(s))

E.g. we do not support these:

  • Joined strings "abc" "def"
  • String prefixes, e.g. r"abc"
  • bytes currently not supported

literal-python-to-pickle's People

Contributors

albertz 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.