Update precompile.md

This commit is contained in:
blueloveTH 2024-04-14 16:25:51 +08:00
parent 8ca0e0168c
commit ecf49321e3

View File

@ -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
```