diff --git a/docs/features/precompile.md b/docs/features/precompile.md index 629275bc..ee9a6479 100644 --- a/docs/features/precompile.md +++ b/docs/features/precompile.md @@ -118,3 +118,36 @@ StackOverflowError !!! String compilation has no guarantee of compatibility between different versions of pkpy. !!! + +You can use this snnipet to convert every python file in a directory into precompiled strings: + +```python +# precompile.py +import sys +import os + +count = 0 + +def precompile(filepath: str): + global count + ++count + with open(filepath, 'r') as f: + source = f.read() + source = compile(source, filepath, 'exec') + with open(filepath, 'w') as f: + f.write(source) + +def traverse(root: str): + for entry in os.listdir(root): + entrypath = os.path.join(root, entry) + if os.path.isdir(entrypath): + traverse(entrypath) + elif entrypath.endswith(".py"): + precompile(entrypath) + +traverse(sys.argv[2]) +``` +```bash +pkpy.exe precompile.py ./path/to/directory +``` +