Ver Fonte

Update precompile.md

blueloveTH há 1 ano atrás
pai
commit
ecf49321e3
1 ficheiros alterados com 33 adições e 0 exclusões
  1. 33 0
      docs/features/precompile.md

+ 33 - 0
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
+```
+