A toy compiler that can convert Python scripts to pickle bytecode 🥒

Overview

Pickora 🐰

A small compiler that can convert Python scripts to pickle bytecode.

Requirements

  • Python 3.8+

No third-party modules are required.

Usage

usage: pickora.py [-h] [-d] [-r] [-l {none,python,pickle}] [-o OUTPUT] file

A toy compiler that can convert Python scripts to pickle bytecode.

positional arguments:
  file                  the Python script to compile

optional arguments:
  -h, --help            show this help message and exit
  -d, --dis             disassamble compiled pickle bytecode
  -r, --eval, --run     run the pickle bytecode
  -l {none,python,pickle}, --lambda {none,python,pickle}
                        choose lambda compiling mode
  -o OUTPUT, --output OUTPUT
                        write compiled pickle to file

Lambda syntax is disabled (--lambda=none) by default.

For exmple, you can run:

python3 pickora.py -d samples/hello.py -o output.pkl

to compile samples/hello.py to output.pkl and show the disassamble result of the compiled pickle bytecode.

But this won't run the pickle for you. If you want you should add -r option, or execute the following command after compile:

python3 -m pickle output.pkl

Special Syntax

RETURN

RETURN is a keyword reserved for specifying pickle.load(s) result. This keyword should only be put in the last statement alone, and you can assign any value / expression to it.

For example, after you compile the following code and use pickle.loads to load the compiled pickle, it returns a string 'INT_MAX=2147483647'.

# source.py
n = pow(2, 31) - 1
RETURN = "INT_MAX=%d" % n

It might look like this:

$ python3 pickora.py source.py -o output.pkl
Saving pickle to output.pkl

$ python3 -m pickle output.pkl
'INT_MAX=2147483647'

Todos

  • Operators (compare, unary, binary, subscript)
  • Unpacking assignment
  • Augmented assignment
  • Macros (directly using GLOBAL, OBJECT bytecodes)
  • Lambda (I don't want to support normal function, because it seems not "picklic" for me)
    • Python bytecode mode
    • Pickle bytecode mode

Impracticable

  • Function call with kwargs
    • NEWOBJ_EX only support type object (it calls __new__)

FAQ

What is pickle?

RTFM.

Why?

It's cool.

Is it useful?

No, not at all, it's definitely useless.

So, is this garbage?

Yep, it's cool garbage.

Would it support syntaxes like if / while / for ?

No. All pickle can do is just simply define a variable or call a function, so this kind of syntax wouldn't exist.

But if you want to do things like:

ans = input("Yes/No: ")
if ans == 'Yes':
  print("Great!")
elif ans == 'No':
  exit()

It's still achievable! You can rewrite your code to this:

from functools import partial
condition = {'Yes': partial(print, 'Great!'), 'No': exit}
ans = input("Yes/No: ")
condition.get(ans, repr)()

ta-da!

For the loop syntax, you can try to use map / reduce ... .

And yes, you are right, it's functional programming time!

You might also like...
CompilerGym is a library of easy to use and performant reinforcement learning environments for compiler tasks
CompilerGym is a library of easy to use and performant reinforcement learning environments for compiler tasks

CompilerGym is a library of easy to use and performant reinforcement learning environments for compiler tasks

The repository contain code for building compiler using puthon.

Building Compiler This is a python implementation of JamieBuild's "Super Tiny Compiler" Overview JamieBuilds developed a wonderfully educative compile

SuperSonic, a new open-source framework to allow compiler developers to integrate RL into compilers easily, regardless of their RL expertise
3ds-Ghidra-Scripts - Ghidra scripts to help with 3ds reverse engineering

3ds Ghidra Scripts These are ghidra scripts to help with 3ds reverse engineering

Convert Pytorch model to onnx or tflite, and the converted model can be visualized by Netron
Convert Pytorch model to onnx or tflite, and the converted model can be visualized by Netron

Convert Pytorch model to onnx or tflite, and the converted model can be visualized by Netron

Python-experiments - A Repository which contains python scripts to automate things and make your life easier with python
Python-experiments - A Repository which contains python scripts to automate things and make your life easier with python

Python Experiments A Repository which contains python scripts to automate things

AI virtual gym is an AI program which can be used to exercise and can be used to see if we are doing the exercises

AI virtual gym is an AI program which can be used to exercise and can be used to see if we are doing the exercises

A python script to convert images to animated sus among us crewmate twerk jifs as seen on r/196
A python script to convert images to animated sus among us crewmate twerk jifs as seen on r/196

img_sussifier A python script to convert images to animated sus among us crewmate twerk jifs as seen on r/196 Examples How to use install python pip i

Comments
  • Set up build tools and install cli

    Set up build tools and install cli

    ❯ pip install git+https://github.com/Isekai-Seikatsu/Pickora.git

    Collecting git+https://github.com/Isekai-Seikatsu/Pickora.git
      Cloning https://github.com/Isekai-Seikatsu/Pickora.git to /private/var/folders/0q/b68c14rn36348fhlpch14k480000gn/T/pip-req-build-5p65sbhx
      Installing build dependencies ... done
      Getting requirements to build wheel ... done
        Preparing wheel metadata ... done
    Building wheels for collected packages: pickora
      Building wheel for pickora (PEP 517) ... done
      Created wheel for pickora: filename=pickora-0.1.0-py3-none-any.whl size=7299 sha256=fe84e54c46df83996c719da61e7ff3153a73bd509e094cc6d29d0cb410b82b09
      Stored in directory: /private/var/folders/0q/b68c14rn36348fhlpch14k480000gn/T/pip-ephem-wheel-cache-rjayxz93/wheels/d5/fe/12/9fc154eb00eeeb349ba0ecb3cb5e9332942d8c5778920b7712
    Successfully built pickora
    Installing collected packages: pickora
    Successfully installed pickora-0.1.0
    

    ❯ pickora -h

    usage: pickora [-h] [-d] [-r] [-l {none,python,pickle}] [-o OUTPUT] file
    
    A toy compiler that can convert Python scripts to pickle bytecode.
    
    positional arguments:
      file                  the Python script to compile
    
    optional arguments:
      -h, --help            show this help message and exit
      -d, --dis             disassamble compiled pickle bytecode
      -r, --eval, --run     run the pickle bytecode
      -l {none,python,pickle}, --lambda {none,python,pickle}
                            choose lambda compiling mode
      -o OUTPUT, --output OUTPUT
                            write compiled pickle to file
    
    Documentation can be found at https://github.com/splitline/Pickora
    

    And If you wish, upload to PyPI and let everyone have fun : ) https://pypi.org/search/?q=pickora

    ❯ poetry publish --build --username xxx --password xxx

    opened by Isekai-Seikatsu 0
  • Lambda doesn't see changes of global variables

    Lambda doesn't see changes of global variables

    The globals for lambda is just a clone/snapshot of current context. https://github.com/splitline/Pickora/blob/7e0e8eb2f66408d78553a8af3e2bc85e2f5a36d8/compiler.py#L355-L358

    So if any global variables are changed after the lambda definition, the lambda won't see those changes For example:

    val = 'before'
    f = lambda:val
    val = 'after'
    print(f())  # before
    

    I am not sure if there are any way to fix this 🤔

    bug 
    opened by splitline 0
Owner
ꌗᖘ꒒ꀤ꓄꒒ꀤꈤꍟ
I hate coding.
ꌗᖘ꒒ꀤ꓄꒒ꀤꈤꍟ
simple_pytorch_example project is a toy example of a python script that instantiates and trains a PyTorch neural network on the FashionMNIST dataset

simple_pytorch_example project is a toy example of a python script that instantiates and trains a PyTorch neural network on the FashionMNIST dataset

Ramón Casero 1 Jan 7, 2022
A toy project using OpenCV and PyMunk

A toy project using OpenCV, PyMunk and Mediapipe the source code for my LindkedIn post It's just a toy project and I didn't write a documentation yet,

Amirabbas Asadi 82 Oct 28, 2022
This repository implements and evaluates convolutional networks on the Möbius strip as toy model instantiations of Coordinate Independent Convolutional Networks.

Orientation independent Möbius CNNs This repository implements and evaluates convolutional networks on the Möbius strip as toy model instantiations of

Maurice Weiler 59 Dec 9, 2022
Some toy examples of score matching algorithms written in PyTorch

toy_gradlogp This repo implements some toy examples of the following score matching algorithms in PyTorch: ssm-vr: sliced score matching with variance

Ending Hsiao 21 Dec 26, 2022
GAN JAX - A toy project to generate images from GANs with JAX

GAN JAX - A toy project to generate images from GANs with JAX This project aims to bring the power of JAX, a Python framework developped by Google and

Valentin Goldité 14 Nov 29, 2022
Pytoydl: A toy deep learning framework built upon numpy.

Documents: https://pytoydl.readthedocs.io/zh/latest/ Pytoydl A toy deep learning framework built upon numpy. You can star this repository to keep trac

null 28 Dec 10, 2022
Simple-System-Convert--C--F - Simple System Convert With Python

Simple-System-Convert--C--F REQUIREMENTS Python version : 3 HOW TO USE Run the c

Jonathan Santos 2 Feb 16, 2022
Pgn2tex - Scripts to convert pgn files to latex document. Useful to build books or pdf from pgn studies

Pgn2Latex (WIP) A simple script to make pdf from pgn files and studies. It's sti

null 12 Jul 23, 2022
Omniverse sample scripts - A guide for developing with Python scripts on NVIDIA Ominverse

Omniverse sample scripts ここでは、NVIDIA Omniverse ( https://www.nvidia.com/ja-jp/om

ft-lab (Yutaka Yoshisaka) 37 Nov 17, 2022