From ecf49321e32e50f73b88daf64b9aad578324bc1a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 14 Apr 2024 16:25:51 +0800 Subject: [PATCH] Update precompile.md --- docs/features/precompile.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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 +``` +