Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5fc60d

Browse files
committed
compile and uncompile
1 parent 50d3afa commit a5fc60d

File tree

11 files changed

+121
-2
lines changed

11 files changed

+121
-2
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
269269

270270
## [geohash](content/geohash.md)
271271

272+
## [py_compile](content/py_compile.md)
273+
274+
## [uncompyle6](content/uncompyle6.md)
275+
272276
## [tools](content/tools.md)
273277

274278
## [Other_thing](content/other_thing.md)

‎code/compileall_demo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# coding=utf-8
2+
3+
import compileall
4+
5+
print compileall.compile_file("test.py")
6+
print compileall.compile_dir("home")

‎code/home/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# coding=utf-8

‎code/home/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# coding=utf-8
2+
3+
4+
def index():
5+
return "home"

‎code/py_compile_demo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# coding=utf-8
2+
3+
import py_compile
4+
5+
py_compile.compile("test.py")

‎code/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# coding=utf-8
2+
3+
print "test"

‎code/uncompyle6_demo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# coding=utf-8
2+
3+
import uncompyle6
4+
5+
with open("test.py", "w") as f:
6+
print uncompyle6.uncompyle_file("test.pyc", f)

‎content/libtorrent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ if __name__ == '__main__':
321321
```
322322
{
323323
"announce"="http://btfans.3322.org:8000/announce" ;tracker 服务器的URL(字符串)
324-
"announce-list"=["http://..","http://.."] ;备用tracker服务器列表(列表)
324+
"announce-list"=[["http://.."],["http://.."]] ;备用tracker服务器列表(列表),会覆盖 announce 字段
325325
"creation date"=1175204110 ;种子创建的时间,Unix标准时间格式
326326
"encoding"="utf-8" ;编码
327327
"comment"="备注"

‎content/pickle.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ My name is Mary and I'm 21 years old
7979
'name': 'Xidian university',
8080
'num': 30000},
8181
'year': 68}
82-
```
82+
```
83+
84+
### 重点
85+
86+
pickle 与 json 的区别,json 的 dumps 和 loads 的返回都是字典,而 pickle 可以是对象,将对象 dumps 然后 loads 出来。
87+
88+
pickle 返回的对象是 完全的重现传入的对象,不仅仅是构造参数,类属性和实例属性。还有私有变量和挂载的的变量方法。

‎content/py_compile.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## py_compile
2+
3+
将 py 文件编译为 pyc 文件,在前两年还说是别人都无法反编译出源文件,但是现在连[在线反编译网站](https://tool.lu/pyc/)都有了。
4+
5+
源代码保护还是需要再想想别的法子.
6+
7+
py_compile 是 python 自带的官方库,在单个 python 文件中边解释边运行,在 python 类库里引用运行的时候就会自动编译出 pyc 文件。
8+
9+
pyc 文件是跨平台,区分版本的。在 python 虚拟机中运行,和 Java 类似。
10+
11+
### 使用
12+
13+
```
14+
# coding=utf-8
15+
16+
import py_compile
17+
18+
py_compile.compile("test.py")
19+
20+
```
21+
22+
或者 在命令行中执行
23+
24+
```
25+
$ python -m py_compile test.py
26+
```
27+
28+
### 高级用法
29+
30+
编译整个文件夹,使用 `compileall`
31+
32+
```
33+
# coding=utf-8
34+
35+
import compileall
36+
37+
# 编译单个文件
38+
print compileall.compile_file("test.py")
39+
40+
# 编译整个文件夹
41+
print compileall.compile_dir("home")
42+
43+
```
44+
45+
1. 使用 `compileall.compile_dir` 实际上也是将文件夹中的源代码单个调用 `compileall.compile_file` 方法
46+
2. 使用 `compileall.compile_file` 实际上也是调用 `py_compile.compile` 方法
47+
3. 编译完成后的 pyc 文件在源目录下
48+
4. 调用成功后会返回 `1`
49+
50+
```
51+
$ python compileall_demo.py
52+
Compiling test.py ...
53+
1
54+
Listing home ...
55+
Compiling home/__init__.py ...
56+
Compiling home/index.py ...
57+
1
58+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /