mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
Update precompile.md
This commit is contained in:
parent
8ca0e0168c
commit
ecf49321e3
@ -118,3 +118,36 @@ StackOverflowError
|
|||||||
!!!
|
!!!
|
||||||
String compilation has no guarantee of compatibility between different versions of pkpy.
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user