锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 # -*- coding: utf-8 -*-
3
4 '''
5 gcc2vs.py
6
7 鍔熻兘錛?br /> 8 灝嗗壀鍒囨澘涓璯cc鐨勭紪璇戣緭鍑烘牸寮忚漿鎴恦s鏍煎紡錛岀敤浜巚s璺寵漿鍒伴敊璇銆?br /> 9 vs2017 linux 緙栬瘧杈撳嚭涓?nbsp;gcc 鏍煎紡錛屾棤娉曠偣鍑昏煩杞紝濡傦細
10 /var/tmp/src/db
e/Linux-Debug/Src/Team.cpp:16:1: 閿欒錛?#8216;x’涓嶆槸涓涓被鍨嬪悕
11 欏昏漿涓簐s鏍煎紡, 濡?br />12 /var/tmp/src/db
e/Linux-Debug/Src/Team.cpp(16):1: 閿欒錛?#8216;x’涓嶆槸涓涓被鍨嬪悕
13
14 濡備綍浣跨敤錛?br />15
16 棣栧厛欏誨畨瑁?nbsp;python, 騫跺畨瑁?nbsp;pyperclip
17 pip install pyperclip
18
19 鍋囪鏈枃浠朵負 d:/tools/gcc2vs.py,
20 vs璁劇疆澶栭儴宸ュ叿錛氬伐鍏?>澶栭儴宸ュ叿->娣誨姞
21 鏍囬錛歡cc2vs(&V)
22 鍛戒護錛歱ython.exe
23 鍙傛暟錛歞:/tools/gcc2vs.py
24 閫変腑"浣跨敤杈撳嚭紿楀彛"
25
26 鍙傝冿細VS2010鎵嬪姩娣誨姞澶栭儴宸ュ叿鍜屽揩鎹烽敭
27 https://www.cnblogs.com/ChinaHook/p/4698733.html
28
29 褰揕inux鏋勫緩杈撳嚭鍚庯紝鐐瑰嚮杈撳嚭紿楀彛錛宑trl-A 閫夋嫨鍏ㄩ儴錛宑trl-C 澶嶅埗杈撳嚭鍒板壀鍒囨澘錛?br />30 鐒跺悗 alt-T,V 榪愯娣誨姞鐨勫閮ㄥ伐鍏?nbsp;gcc2vs(&V), 鏇存敼杈撳嚭鏍煎紡錛岀劧鍚庡氨鍙互鐐瑰嚮閿欒璺寵漿浜嗐?br />31 '''
32
33 import re
34 import pyperclip
35
36 # 寰呮浛鎹㈢殑鏍煎紡
37 pattern = re.compile(r'/var/tmp/src/
..-
.-
.-
.-


/Linux-Debug/(.*):([0-9]*):([0-9]*): ')
38
39 test_lines_src = '''
40 /var/tmp/src/db71a8ec-90bb-2838-98df-2dd35e71166e/Linux-Debug/003_servers/103_LobbyServer/Src/Team.cpp:16:1: 閿欒錛?#8216;x’涓嶆槸涓涓被鍨嬪悕
41 鐢熸垚澶辮觸銆?br />42 '''
43 test_lines_dst = '''
44 003_servers/103_LobbyServer/Src/Team.cpp(16):1: 閿欒錛?#8216;x’涓嶆槸涓涓被鍨嬪悕
45 鐢熸垚澶辮觸銆?br />46 '''
47 assert test_lines_dst == re.sub(pattern, r'\1(\2):\3: ', test_lines_src)
48
49 # 鍓垏鏉夸腑鐨刧cc鏍煎紡杈撳嚭
50 src = pyperclip.paste()
51 # 杞垚vs鏍煎紡
52 dst = re.sub(pattern, r'\1(\2):\3: ', src)
53 print(dst)
54
]]>
(閲戝簡鐨勪笓鏍?
絳栧垝鐨勯厤緗〃涓?xlsx 琛ㄦ牸錛屽彲浠ユ湁娉ㄩ噴錛屽叕寮忋?br />鏈嶅姟鍣ㄥ拰瀹㈡埛绔嬌鐢ㄧ殑閰嶇疆鏂囦歡闇瑕佽漿鎴?csv 鏂囦歡銆?br />浣跨敤 WPS 鍙﹀瓨鏃犳硶鎵歸噺杞崲錛屽茍涓旂粨鏋滀笉鏄痷tf8緙栫爜鐨勶紝榪橀渶瑕佺敤Notepad++杞紪鐮併?br />
闄や簡 xlsx 杞負 csv, 鍏朵粬鏍煎紡鏂囦歡淇濇寔鍘熸牱錛屽 *.ini, *.xml, *.lua.
server/ 瀛愮洰褰曠壒孌婂鐞嗭紝涓嶈兘澶嶅埗鍒板鎴風銆?br />
鐢╬ython鑴氭湰瀹炵幇錛屼緷璧?openpyxl 搴撱?br />
#!/usr/bin/env python
# coding: utf-8
# datatab.py
# 浠庣瓥鍒掗厤緗〃鐩綍 game\Design\閰嶇疆琛╘”
# 鐢熸垚鏈嶅姟鍣ㄧ殑 game\Program\server\six\datatab\” 鐩綍錛?br /># 鍜屽鎴風鐨?game\Program\client\Assets\Config\” 鐩綍銆?br /># 鎵鏈墄lsx鏂囦歡鐢熸垚csv鏂囦歡錛屽叾浠栨枃浠跺師鏍峰鍒躲?br /># 鍏朵腑 server\ 鐩綍鐗規畩澶勭悊錛屼粎瀵規湇鍔″櫒鏈夋晥錛屽鎴風璺寵繃銆?
#
# 渚濊禆openpyxl搴擄細http://openpyxl.readthedocs.org/en/latest/
# 鍙傝冧唬鐮?http://segmentfault.com/q/1010000003006437?_ea=273128
# 嫻嬭瘯鐜錛歅ython3.4
# Usage: datatab.py <game dir>
# Example: datatab.py "d:\game"
# <game dir> 鏄牴鐩綍錛屽寘鍚獶esign/, Program/ 鐩綍銆?br />
from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.cell import get_column_letter
from openpyxl import load_workbook
import csv
import os
import sys
import shutil
def xlsx2csv(filename):
# try:
xlsx_file_reader = load_workbook(filename = filename, data_only = True)
for sheet in xlsx_file_reader.get_sheet_names():
# 浠呯1涓猻heet杈撳嚭鍒頒竴涓猚sv鏂囦歡涓紝鏂囦歡鍚嶅悗緙鏇挎崲涓?csv
csv_filename = os.path.splitext(filename)[0] + '.csv'
csv_file = open(csv_filename, 'w', encoding='utf8', newline='')
csv_file_writer = csv.writer(csv_file)
sheet_ranges = xlsx_file_reader[sheet]
for row in sheet_ranges.rows:
row_container = []
for cell in row:
row_container.append(cell.value)
csv_file_writer.writerow(row_container)
# End of for row.
csv_file.close()
break # 浠呰緭鍑虹1涓猻heet
# End of for sheet.
# End of try.
# except Exception as e:
# print(e)
# End of xlsx2csv().
def datatab_convert(game_dir):
'''浠?game\Design\閰嶇疆琛╘ 杈撳嚭鍒?br /> game\Program\server\six\datatab\
game\Program\client\Assets\Config\
'''
design_dir = os.path.join(game_dir, 'Design/閰嶇疆琛?')
server_dir = os.path.join(game_dir, 'Program/server/six/datatab/')
client_dir = os.path.join(game_dir, 'Program/client/Assets/Config/')
# 鍒犻櫎鏃ф枃浠躲?br /> print("Delete " + server_dir)
try:
shutil.rmtree(server_dir)
except:
pass
print("Delete " + client_dir)
try:
shutil.rmtree(client_dir)
except:
pass
# 鐢熸垚server鏂囦歡
print("Creating " + server_dir)
shutil.copytree(design_dir, server_dir)
files = get_files(server_dir)
convert_files(files)
# 澶嶅埗client鏂囦歡
print("Copy " + client_dir)
shutil.copytree(server_dir, client_dir)
shutil.rmtree(os.path.join(client_dir, 'server/'))
print("Done. Total files: %d" % len(files))
# End of datatab_convert().
def get_files(dir):
'''Get a list of files under input dir.'''
result = []
for root,dirs,files in os.walk(dir):
for f in files:
result.append(os.path.join(root, f))
return result
# End of get_files().
def convert_files(files):
'''杞崲涓鎵規枃浠?
files 鏄垪琛紝鍏冪礌涓哄畬鏁磋礬寰勫悕銆?br /> '''
for f in files:
ext = os.path.splitext(f)[1].lower()
if '.xlsx' != ext:
print(f + " -> keep")
continue
print(f + " -> csv")
xlsx2csv(f)
os.remove(f)
# End of convert_files().
if __name__ == '__main__':
if len(sys.argv) != 2:
print('usage: datatab <game dir>')
else:
datatab_convert(sys.argv[1])
sys.exit(0)
# Usage: datatab.py <game dir>
# Example: datatab.py "d:\game"
# <game dir> 鏄疭VN鏍圭洰褰曪紝鍖呭惈Design/, Program/ 鐩綍銆?br />
涓烘柟渚夸嬌鐢紝灝哾atatab.py 鎵撳寘鎴?exe, 榪欐牱涓嶈兘瀹夎Python灝辮兘榪愯銆?br />涓嬭澆騫跺畨瑁匬ython3, 瀹夎openpyxl鍖咃紝淇濊瘉鏈湴鍙互榪愯 datatab.py.
涓嬭澆騫跺畨瑁匬yInstaller錛?br /> pip install pyinstaller
榪愯
pyinstaller --onefile datatab.py
ImportError: No module named 'jdcal'
鍙兘openpyxl瀹夎鏃惰嚜甯︾殑jdcal鏃犳硶鎵懼埌錛屽垹闄?
C:\Python34\Lib\site-packages\jdcal-1.0-py3.4.egg
閲嶆柊瀹夎錛歱ip install jdcal
]]>
鍋囪protobuf-py宸茬粡鎸夌収protobuf鐨勫畨瑁呰鏄庡畨瑁呬簡銆?br />鍙戠幇 protobuf-2.5.0鐗堢殑python鍖呮槸python2鐨勶紝娌℃湁鎵懼埌python3鐨勩?br />璇曠潃2to3.py杞崲涓涓嬶紝緇撴灉杞崲鍚庢棤娉曞畨瑁呫?br />鍙ソ灝唒ython3.3鎹㈡垚python2.7
python2.7鐨刴imetypes.py鏈夐敊璇紝娉ㄥ唽琛ㄤ腑HKEY_CLASSES_ROOT鏈変腑鏂囩殑鎵╁睍鍚嶆椂錛?br />瀹夎setuptools澶辮觸錛屽弬鑰?http://www.oschina.net/question/1409342_134600
鍏堢紪璇?rpz, 灝嗙敓鎴愮殑
protoc-gen-cpp_rpcz.exe
protoc-gen-python_rpcz.exe
澶嶅埗鍒扮郴緇熸悳绱㈢洰褰曪紝濡備笌protoc.exe鏀懼湪涓璧楓?br />
榪涘叆 rpcz 鐨?python 鐩綍錛岃繍琛?br />python setup.py install
鍑虹幇濡備笅鐨勫悇縐嶉敊璇細
../include\rpcz/connection_manager.hpp(21) : fatal error C1083: Cannot open incl
ude file: 'boost/function.hpp': No such file or directory
../include\rpcz/macros.hpp(20) : fatal error C1083: Cannot open include file: 'g
oogle/protobuf/stubs/common.h': No such file or directory
../include\rpcz/rpc.hpp(24) : fatal error C1083: Cannot open include file: 'rpcz
/rpcz.pb.h': No such file or directory
LINK : fatal error LNK1181: cannot open input file 'rpcz.lib'
LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc100-mt-1_54.lib'
鐩存帴鏀逛簡setup.py濡備笅銆?br />
ext_modules=[
Extension("rpcz.pywraprpcz", ["cython/pywraprpcz.cpp"],
libraries=["rpcz", "libprotobuf", "libzmq"],
include_dirs=['../include', '../build/src',
'D:/Boost/include/boost-1_54',
'G:/workspace/rpcz_jinq/src',
'D:/LibSrc/protobuf-2.5.0/vsprojects/include'],
library_dirs=['../build/deps/lib', '../build/src/rpcz',
'D:/Boost/lib',
'G:/workspace/rpcz_jinq/src/rpcz/Release',
'D:/LibSrc/zeromq-4.0.3/builds/msvc/Release',
'D:/LibSrc/protobuf-2.5.0/vsprojects/Release'],
language='c++')
],
涓嶇煡閬撳浣曞仛鎴愰氱敤鐨勮剼鏈?br />rpcz鍘熸潵搴旇鏄緩绔嬩竴涓浐瀹氫綅緗殑build鐩綍錛屽叾涓湁deps鐩綍錛屽寘鍚墍鏈夊簱銆?br />榪欏氨瑕佹眰cmake鐨勬瀯寤虹洰褰曞浐瀹氫負build.
python2.7鏄敤VC2008緙栬瘧鐨勶紝鐢╒C2010緙栬瘧python鎵╁睍浼氭湁闂銆?br />浼氭姤閿欙細
error: Unable to find vcvarsall.bat
寮哄埗浣跨敤VC2010:
SET VS90COMNTOOLS=%VS100COMNTOOLS%
鍙傝冿細http://stackoverflow.com/questions/2817869/error-unable-to-find-vcvarsall-bat
鏈鍚庤繘鍏?examples\python錛屽厛 generate_protos.gy 鐢熸垚娑堟伅鍜屾帴鍙o紝鐒跺悗榪愯 server, client.
榪愯鏃墮渶瑕?libzmq.dll, rpcz.dll.
]]>
錛堥噾搴嗙殑涓撴爮錛?br />
涓孌電畝鍗曠殑浠g爜錛岃鍙杝tdin, 鏇挎崲杈撳嚭鍒皊tdout:
#!/usr/bin/env python
import os, sys
input_file = sys.stdin
output_file = sys.stdout
for s in input_file:
output_file.write(s.replace("abc", "def"))
浠g爜鏀硅嚜CookBook鐨?nbsp;
Recipe 2.3. Searching and Replacing Text in a File
鍦↙inux涓婅繍琛屾甯革細
$ cat input.txt | ./replace.py
浣嗘槸鍦╓indows鐨凞os紿楀彛榪愯浼氭姤閿欙細
> type input.txt | replace.py
Traceback...
for s in input_file:
IOError: [Errno 9] Bad file descriptor
濡傛灉鐢╬ython璋冪敤灝辨甯革細
> type input.txt | python replace.py
鍘熷洜鏄痗md.exe閫氳繃鍚庣紑鍏寵仈榪愯py鏂囦歡瀛樺湪闂銆?br />璇﹁錛欱ad pipe filedescriptor when reading from stdin in python
http://stackoverflow.com/questions/1057638/bad-pipe-filedescriptor-when-reading-from-stdin-in-python
It seems that stdin/stdout redirect does not work when starting from a file association. This is not specific to python, but a problem caused by win32 cmd.exe.
]]>
錛堥噾搴嗙殑涓撴爮錛?br />
Flash鍜孶nity3D娓告垙鏈嶅姟鍣ㄩ渶瑕佸紑鍚竴涓猄ocket Policy鏈嶅姟鍣ㄣ?br />璇︾粏璇存槑瑙侊細Setting up a socket policy file server
http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html
鍗忚濡備笅錛?br />Flash鎴朥3D鎾斁鍣ㄥ悜Socket Policy鏈嶅姟鍣ㄥ彂閫佽姹傦細
“<policy-file-request/>\0”錛?br />鏈嶅姟鍣ㄨ繑鍥炰竴涓獂ml鏂囨湰錛屽叾涓湁鍖呭惈<cross-domain-policy>閰嶇疆銆?br />
Adobe鎻愪緵浜哖erl鍜孭ython浠g爜銆俇nity3D鎻愪緵浜咰Sharp浠g爜銆?br />cs浠g爜鐢ㄤ簬Windows. Linux涓婂噯澶囦嬌鐢≒ython浠g爜銆?br />鍦ㄤ互涓婄綉欏典笅杞斤細flashpolicyd_v0.6.zip銆?br />鍏朵腑鏈?涓増鏈細init鏈嶅姟錛寈inetd鏈嶅姟錛岀嫭绔嬪簲鐢ㄣ傚彧闇涓縐嶅嵆鍙?br />
鎸夊簲鐢ㄧ殑鎬ц川錛岃姹傞噺鏋佸皬錛屾墍浠ユ渶閫傚悎搴旂敤xinetd.
鍙渶榪愯鍏朵腑鐨刬nstall.sh灝變細瀹夎銆?br />榪橀渶鎵嬪伐鍦?ect/services涓坊鍔犳湇鍔″悕flashpolicy鍜岀鍙?43.
榪愯浠ヤ笅鎸囦護鍙互嫻嬭瘯涓涓嬶紝杈撳嚭搴旇鏄厤緗枃浠剁殑鍐呭銆?br />python -c 'print "<policy-file-request/>%c" % 0' | nc 127.0.0.1 843
鍏跺疄浠ヤ笂嫻嬭瘯鎸囦護鍦ㄥ熬閮ㄥ浜嗕釜'\n', 涓嶆槸鏍囧噯鐨勬祴璇曘?br />鍦╥n.flashpolicyd.py涓浜嗕釜strip()鍘婚櫎榪欎釜'\n'.
瀹為檯鐢║3D嫻嬭瘯鏃訛紝鍥犱負娌℃湁'\n'緇撳熬錛屾墍浠n.flashpolicyd.py鏃犳硶姝e父宸ヤ綔銆?br />闇瑕佸皢35琛宺eadline()濡備笅鏇存敼錛?br /> request = sys.stdin.readline().strip()
鏀逛負
request = sys.stdin.readline(len('<policy-file-request/>\0')).strip()
榪欐牱灝變笉闇瑕乪ol灝卞彲浠ヨ鍙栦簡銆?br />
鏌ョ湅鍏朵粬涓や釜鐗堟湰鐨勪唬鐮佹病鏈夎繖涓敊璇紝浣嗘槸鏈夊彟涓涓敊璇紝
鍗蟲湁鍙兘鍙涓鍗婄殑璇鋒眰閫犳垚鍒ゆ柇鍑洪敊銆?br />
榪樻湁涓涓棶棰樻槸鍥犱負鏈嶅姟鏄敱nobody榪愯鐨勶紝鐜鍙橀噺涓嶅悓錛?br />灝唅n.flashpolicyd.py澶撮儴鐨?br />#!/usr/bin/env python
鏄庣‘涓?br />#!/usr/local/sbin/python
鍙伩鍏嶈皟鐢ㄤ綆鐗堟湰鐨刾ython鑰屾姤閿欍?br />
]]>
]]>
鏈鏂扮増鏈錛?a >http://wiki.woodpecker.org.cn/moin/PP3eD
19.4. Pickled Objects
19.4. Pickle瀵硅薄
Probably
the biggest limitation of DBM keyed files is in what they can store:
data stored under a key must be a simple text string. If you want to
store Python objects in a DBM file, you can sometimes manually convert
them to and from strings on writes and reads (e.g., with str and eval
calls), but this takes you only so far. For arbitrarily complex Python
objects such as class instances and nested data structures, you need
something more. Class instance objects, for example, cannot be later
re-created from their standard string representations. Custom to-string
conversions are error prone and not general.
DBM
閿帶鏂囦歡錛圖BM keyed
file錛夋渶澶х殑闄愬埗涔熻鍦ㄤ簬浠栦滑鍙互瀛樺偍鐨勪笢瑗匡細涓涓敭鍊間笅瀛樺偍鐨勬暟鎹繀欏繪槸涓畝鍗曟枃鏈瓧絎︿覆銆傚鏋滄偍鎯寵鍦―BM鏂囦歡涓偍瀛楶ython瀵硅薄錛屾湁鏃舵偍
鍙互鍦ㄨ鍐欑殑鏃跺欙紝鎵嬪姩榪涜涓庡瓧絎︿覆鐨勮漿鎹紙渚嬪錛岀敤str鍜宔val璋冪敤錛夛紝浣嗗彧鑳藉仛鍒拌繖鏍楓傚浠繪剰澶嶆潅鐨凱ython瀵硅薄錛屽綾誨疄渚嬪拰宓屽鐨勬暟鎹粨
鏋勶紝鎮ㄩ渶瑕佹洿澶氱殑涓滆タ銆備緥濡傦紝綾誨疄渚嬪璞′互鍚庢棤娉曚粠鍏舵爣鍑嗗瓧絎︿覆琛ㄨ揪錛坰tring
representation錛夐噸寤恒傝嚜瀹氫箟鐨勫埌瀛楃涓茬殑杞崲瀹規槗鍑洪敊錛屽茍涓斾笉閫氱敤銆?
The
Python pickle module, a standard part of the Python system, provides
the conversion step needed. It converts nearly arbitrary Python
in-memory objects to and from a single linear string format, suitable
for storing in flat files, shipping across network sockets between
trusted sources, and so on. This conversion from object to string is
often called serializationarbitrary data structures in memory are
mapped to a serial string form.
Python
緋葷粺鐨勬爣鍑嗛儴浠訛紝pickle妯″潡錛屾彁渚涗簡鎵闇鐨勮漿鎹㈡楠ゃ傚畠鍙互灝嗗嚑涔庝換鎰忕殑Python鍐呭瓨瀵硅薄錛岃漿鎹負鍗曚竴綰挎х殑瀛楃涓叉牸寮忥紝浣夸箣閫備簬鏃犳牸寮忔枃浠跺瓨
鍌紝鎴栧湪鍙潬鏉ユ簮涔嬮棿璺ㄨ秺緗戠粶濂楁帴鍙d紶杈撶瓑絳夛紝騫跺彲鍙嶅悜杞崲銆傝繖縐嶄粠瀵硅薄鍒板瓧絎︿覆鐨勮漿鎹㈤氬父琚О涓哄簭鍒楀寲錛坰erialization錛夛細灝嗗唴瀛樹腑鐨勪換鎰?
鏁版嵁緇撴瀯鏄犲皠涓轟覆琛屽瓧絎︿覆褰㈠紡銆?
The
string representation used for objects is also sometimes referred to as
a byte stream, due to its linear format. It retains all the content and
references structure of the original in-memory object. When the object
is later re-created from its byte string, it will be a new in-memory
object identical in structure and value to the original, though located
at a different memory address. The re-created object is effectively a
copy of the original.
瀵硅薄鐨勫瓧絎︿覆琛ㄨ揪鐢變簬鍏剁嚎鎬х殑鏍煎紡錛屾湁鏃朵篃琚О涓哄瓧鑺傛祦銆傚畠鍖呭惈浜嗗師濮嬪唴瀛樹腑瀵硅薄鐨勬墍鏈夊唴瀹瑰拰寮曠敤緇撴瀯銆傚綋瀵硅薄鍚庢潵浠庡叾瀛楄妭涓查噸寤烘椂錛屽唴瀛樹腑鏂板緩鐨勫璞′笌鍘熷璞″叿鏈夌浉鍚岀殑緇撴瀯鍜屽鹼紝浣嗕綅浜庝笉鍚岀殑鍐呭瓨鍦板潃銆傝閲嶅緩瀵硅薄瀹為檯涓婃槸鍘熷璞$殑澶嶅埗銆?
Pickling
works on almost any Python datatypenumbers, lists, dictionaries, class
instances, nested structures, and moreand so is a general way to store
data. Because pickles contain native Python objects, there is almost no
database API to be found; the objects stored are processed with normal
Python syntax when they are later retrieved.
Pickle鍙敤浜庡嚑涔庢墍鏈夌殑Python鏁版嵁綾誨瀷錛氭暟瀛椼佸垪琛ㄣ佸瓧鍏搞佺被瀹炰緥銆佸祵濂楃粨鏋勶紝絳夌瓑錛屽洜姝ゅ畠鏄瓨鍌ㄦ暟鎹殑閫氱敤鏂規硶銆傚洜涓簆ickle鍖呭惈鐨勬槸Python鏈湴瀵硅薄錛屾墍浠ュ嚑涔庢病鏈夋暟鎹簱鐨凙PI錛涘璞″瓨鍌ㄤ笌澶勭悊鍙婂悗鏉ョ殑鎻愬彇鐢ㄧ殑閮芥槸閫氬父鐨凱ython璇硶銆?
19.4.1. Using Object Pickling
19.4.1. 浣跨敤瀵硅薄pickle
Pickling
may sound complicated the first time you encounter it, but the good
news is that Python hides all the complexity of object-to-string
conversion. In fact, the pickle module 's interfaces are incredibly
simple to use. For example, to pickle an object into a serialized
string, we can either make a pickler and call its methods or use
convenience functions in the module to achieve the same effect:
絎?
涓嬈″惉鍒皃ickle錛屽彲鑳借寰楁湁鐐瑰鏉傦紝浣嗗ソ娑堟伅鏄紝Python闅愯棌浜嗘墍鏈変粠瀵硅薄鍒板瓧絎︿覆杞崲鐨勫鏉傛с備簨瀹炰笂錛宲ickle妯″潡鐨勬帴鍙g畝鍗曟槗鐢紝綆
鐩翠護浜洪毦浠ョ疆淇°備緥濡傦紝瑕乸ickle瀵硅薄鍒頒竴涓簭鍒楀寲瀛楃涓詫紝鎴戜滑鍙互鐢熸垚涓涓猵ickler錛屽茍璋冪敤鍏舵柟娉曪紝鎴栦嬌鐢ㄦā鍧椾腑鐨勪究鎹峰嚱鏁版潵杈懼埌鐩稿悓鐨勬晥
鏋滐細
P = pickle.Pickler( file)
Make a new pickler for pickling to an open output file object file.
鐢熸垚涓涓柊鐨刾ickler錛岀敤鏉ickle鍒頒竴涓墦寮鐨勮緭鍑烘枃浠跺璞ile銆?
P.dump( object)
Write an object onto the pickler's file/stream.
鍐欎竴涓璞″埌pickler鐨勬枃浠?嫻併?
pickle.dump( object, file)
Same as the last two calls combined: pickle an object onto an open file.
絳夊悓浜庝笂涓や釜璋冪敤鐨勭粍鍚堬細pickle瀵硅薄鍒頒竴涓墦寮鐨勬枃浠躲?
string = pickle.dumps( object)
Return the pickled representation of object as a character string.
榪斿洖涓涓瓧絎︿覆浣滀負宸瞤ickle瀵硅薄鐨勮〃杈俱?
Unpickling
from a serialized string back to the original object is similarboth
object and convenience function interfaces are available:
浠庝竴涓簭鍒楀寲瀛楃涓瞮npickle鍥炲師濮嬪璞℃槸綾諱技鐨勶紝鍙互鐢ㄥ璞′篃鍙互鐢ㄤ究鎹峰嚱鏁版帴鍙o細
U = pickle.Unpickler( file)
Make an unpickler for unpickling from an open input file object file.
鐢熸垚涓涓猽npickler錛岀敤鏉ヤ粠涓涓墦寮鐨勬枃浠跺璞ile unpickle銆?
object = U.load( )
Read an object from the unpickler's file/stream.
浠巙npickler鐨勬枃浠?嫻佽鍙栦竴涓璞°?
object = pickle.load( file)
Same as the last two calls combined: unpickle an object from an open file.
絳夊悓浜庝笂涓や釜璋冪敤鐨勭粍鍚堬細浠庝竴涓墦寮鐨勬枃浠秛npickle涓涓璞°?
object = pickle.loads( string)
Read an object from a character string rather than a file.
浠庡瓧絎︿覆璇誨彇涓涓璞★紝鑰屼笉鏄粠鏂囦歡銆?
Pickler
and Unpickler are exported classes. In all of the preceding cases, file
is either an open file object or any object that implements the same
attributes as file objects:
Pickler鍜孶npickler鏄鍑虹被銆傚湪涓婅堪鎵鏈夋儏鍐典笅錛宖ile鏄釜宸叉墦寮鐨勬枃浠跺璞★紝鎴栬呮槸瀹炵幇浜嗕互涓嬫枃浠跺璞″睘鎬х殑浠諱綍瀵硅薄錛?
Pickler calls the file's write method with a string argument.
Pickler浼氳皟鐢ㄦ枃浠剁殑write鏂規硶錛屽弬鏁版槸涓瓧絎︿覆銆?
Unpickler calls the file's read method with a byte count, and readline without arguments.
Unpickler浼氳皟鐢ㄦ枃浠剁殑read鏂規硶錛屽弬鏁版槸瀛楄妭鏁幫紝浠ュ強readline錛屾棤鍙傛暟銆?
Any
object that provides these attributes can be passed in to the file
parameters. In particular, file can be an instance of a Python class
that provides the read/write methods (i.e., the expected file-like
interface). This lets you map pickled streams to in-memory objects with
classes, for arbitrary use. For instance, the StringIO standard library
module discussed in Chapter 3 provides classes that map file calls to
and from in-memory strings.
浠?
浣曟彁渚涜繖浜涘睘鎬х殑瀵硅薄閮藉彲浠ヤ綔涓篺ile鍙傛暟浼犲叆銆傜壒鍒槸錛宖ile鍙互鏄竴涓彁渚涗簡璇?鍐欐柟娉曠殑Python綾誨疄渚嬶紙鍗抽鏈熺殑綾諱技鏂囦歡鐨勬帴鍙o級銆傝繖璁╂偍
鍙互鐢ㄧ被鏄犲皠pickle嫻佸埌鍐呭瓨瀵硅薄錛屽茍鍙換鎰忎嬌鐢ㄣ備緥濡傦紝絎?绔犺璁虹殑鏍囧噯搴撴ā鍧桽tringIO鎻愪緵鐨勭被錛屽畠浠彲鏄犲皠鏂囦歡璋冪敤鍒板唴瀛樺瓧絎︿覆鎴栧弽涔嬨?
This
hook also lets you ship Python objects across a network, by providing
sockets wrapped to look like files in pickle calls at the sender, and
unpickle calls at the receiver (see the sidebar "Making Sockets Look
Like Files," in Chapter 13, for more details). In fact, for some,
pickling Python objects across a trusted network serves as a simpler
alternative to network transport protocols such as SOAP and XML-RPC;
provided that Python is on both ends of the communication (pickled
objects are represented with a Python-specific format, not with XML
text).
璇?
鎸傞挬涔熷彲浠ヨ鎮ㄩ氳繃緗戠粶浼犺緭Python瀵硅薄錛屽彧瑕佸皝瑁呭鎺ュ彛錛屼嬌涔嬬湅涓婂幓鍍忓彂閫佺pickle璋冪敤涓殑鏂囦歡錛屼互鍙婂儚鎺ユ敹绔痷npickle璋冪敤涓殑鏂囦歡
錛堣瑙佺13绔犱晶鏍?#8220;浣垮鎺ュ彛鐪嬩笂鍘誨儚鏂囦歡”錛夈備簨瀹炰笂錛屽涓浜涗漢鏉ヨ錛宲ickle
Python瀵硅薄騫跺湪涓涓煎緱淇¤禆鐨勭綉緇滀笂浼犺緭錛屾槸鏇夸唬濡係OAP鍜孹ML-RPC涔嬬被緗戠粶浼犺緭鍗忚鐨勪竴涓畝鍗曟柟娉曪紱鍙閫氫俊鐨勪袱绔兘鏈塒ython錛堣
pickle鐨勫璞℃槸鐢≒ython涓撴湁鐨勬牸寮忚〃杈劇殑錛岃屼笉鏄敤XML鏂囨湰錛夈?
19.4.2. Picking in Action
19.4.2. Pickle瀹炴垬
In more typical use, to pickle an object to a flat file, we just open the file in write mode and call the dump function:
鍏稿瀷鐨勪嬌鐢ㄦ儏鍐墊槸錛宲ickle瀵硅薄鍒版棤鏍煎紡鏂囦歡錛屾垜浠彧闇浠ュ啓妯″紡鎵撳紑鏂囦歡錛屽茍璋冪敤dump鍑芥暟錛?
% python
>>> table = {'a': [1, 2, 3],
'b': ['spam', 'eggs'],
'c': {'name':'bob'}}
>>>
>>> import pickle
>>> mydb = open('dbase', 'w')
>>> pickle.dump(table, mydb)
Notice
the nesting in the object pickled herethe pickler handles arbitrary
structures. To unpickle later in another session or program run, simply
reopen the file and call load:
娉ㄦ剰榪欎釜琚玴ickle瀵硅薄涓殑宓屽錛歱ickler鍙互澶勭悊浠繪剰緇撴瀯銆傜劧鍚庯紝鍦ㄥ彟涓涓細璇濇垨紼嬪簭涓璾npickle錛屽彧瑕侀噸鏂版墦寮璇ユ枃浠訛紝騫惰皟鐢╨oad錛?
% python
>>> import pickle
>>> mydb = open('dbase', 'r')
>>> table = pickle.load(mydb)
>>> table
{'b': ['spam', 'eggs'], 'a': [1, 2, 3], 'c': {'name': 'bob'}}
The
object you get back from unpickling has the same value and reference
structure as the original, but it is located at a different address in
memory. This is true whether the object is unpickled in the same or a
future process. In Python-speak, the unpickled object is == but is not
is:
unpickle鎵寰楃殑瀵硅薄鍏鋒湁涓庡師瀵硅薄鐩稿悓鐨勫煎拰寮曠敤緇撴瀯錛屼絾瀹冧綅浜庝笉鍚岀殑鍐呭瓨鍦板潃銆傛棤璁哄湪鍚屼竴榪涚▼鎴栧彟涓榪涚▼unpickle錛岄兘鏄繖鏍楓傜敤Python鐨勮瘽鏉ヨ錛寀npickle鍚庣殑瀵硅薄鏄?#8220;==”鍏崇郴錛屼絾涓嶆槸“is”鍏崇郴錛?
% python
>>> import pickle
>>> f = open('temp', 'w')
>>> x = ['Hello', ('pickle', 'world')] # list with nested tuple
>>> pickle.dump(x, f)
>>> f.close( ) # close to flush changes
>>>
>>> f = open('temp', 'r')
>>> y = pickle.load(f)
>>> y
['Hello', ('pickle', 'world')]
>>>
>>> x == y, x is y
(True, False)
To
make this process simpler still, the module in Example 19-1 wraps
pickling and unpickling calls in functions that also open the files
where the serialized form of the object is stored.
涓轟簡璁╄繖涓榪囩▼鏇寸畝鍗曪紝渚?9-1涓殑妯″潡鎶妏ickle鍜寀npickle璋冪敤灝佽鍦ㄥ嚱鏁頒腑錛屽湪鍑芥暟涓悓鏃惰繕鎵撳紑鏂囦歡錛屽茍灝嗗璞$殑搴忓垪鍖栧瓨鍌ㄥ湪璇ユ枃浠朵腑銆?
Example 19-1. PP3E\Dbase\filepickle.py
1 import pickle
2
3 def saveDbase(filename, object):
4 file = open(filename, 'w')
5 pickle.dump(object, file) # pickle to file
6 file.close( ) # any file-like object will do
7
8 def loadDbase(filename):
9 file = open(filename, 'r')
10 object = pickle.load(file) # unpickle from file
11 file.close( ) # re-creates object in memory
12 return object
To
store and fetch now, simply call these module functions; here they are
in action managing a fairly complex structure with multiple references
to the same nested objectthe nested list called L at first is stored
only once in the file:
鐜板湪錛屽瓨鍌ㄥ拰鎻愬彇鏃跺彧闇璋冪敤榪欎簺妯″潡鍑芥暟錛涗互涓嬪疄渚嬫槸涓浉褰撳鏉傜殑緇撴瀯錛屽叿鏈夊鍚屼竴宓屽瀵硅薄鐨勫閲嶅紩鐢紝璇ュ祵濂楀垪琛紝鍗崇1涓狶錛屽湪鏂囦歡涓彧浼氫繚瀛樹竴嬈★細
C:\...\PP3E\Dbase>python
>>> from filepickle import *
>>> L = [0]
>>> D = {'x':0, 'y':L}
>>> table = {'A':L, 'B':D} # L appears twice
>>> saveDbase('myfile', table) # serialize to file
C:\...\PP3E\Dbase>python
>>> from filepickle import *
>>> table = loadDbase('myfile') # reload/unpickle
>>> table
{'B': {'x': 0, 'y': [0]}, 'A': [0]}
>>> table['A'][0] = 1 # change shared object
>>> saveDbase('myfile', table) # rewrite to the file
C:\...\PP3E\Dbase>python
>>> from filepickle import *
>>> print loadDbase('myfile') # both L's updated as expected
{'B': {'x': 0, 'y': [1]}, 'A': [1]}
Besides
built-in types like the lists, tuples, and dictionaries of the examples
so far, class instances may also be pickled to file-like objects. This
provides a natural way to associate behavior with stored data (class
methods process instance attributes) and provides a simple migration
path (class changes made in module files are automatically picked up by
stored instances). Here's a brief interactive demonstration:
闄?
浜嗗唴緗殑綾誨瀷錛屽浠ヤ笂渚嬪瓙涓殑鍒楄〃錛屽厓緇勫拰瀛楀吀錛岀被瀹炰緥涔熷彲琚玴ickle鍒扮被浼兼枃浠剁殑瀵硅薄涓傝繖鎻愪緵浜嗕竴涓嚜鐒剁殑鏂瑰紡鏉ュ叧鑱旇涓轟笌瀛樺偍鐨勬暟鎹紙綾繪柟娉曞
鐞嗗疄渚嬬殑灞炴э級錛屽茍鎻愪緵浜嗕竴鏉$畝鍗曠殑榪佺Щ璺緞錛堣瀛樺偍鐨勫疄渚嬪皢鑷姩鑾峰緱妯″潡鏂囦歡涓綾葷殑鏇存敼錛夈備互涓嬫槸涓畝鐭殑浜や簰婕旂ず錛?
>>> class Rec:
def _ _init_ _(self, hours):
self.hours = hours
def pay(self, rate=50):
return self.hours * rate
>>> bob = Rec(40)
>>> import pickle
>>> pickle.dump(bob, open('bobrec', 'w'))
>>>
>>> rec = pickle.load(open('bobrec'))
>>> rec.hours
40
>>> rec.pay( )
2000
We'll
explore how this works in more detail in conjunction with shelves later
in this chapteras we'll see, although the pickle module can be used
directly, it is also the underlying translation engine in both shelves
and ZODB databases.
鎴戜滑灝嗕笌鏈珷涓嬮潰鐨剆helve涓璧瘋緇嗘帰璁ㄨ繖鏄姝ゅ伐浣滅殑銆傛垜浠皢浼氱湅鍒幫紝铏界劧pickle妯″潡鍙洿鎺ヤ嬌鐢紝浣嗗畠涔熸槸shelve鍜孼ODB鏁版嵁搴撶殑搴曞眰緲昏瘧寮曟搸銆?
In fact, Python can pickle just about anything, except for:
浜嬪疄涓婏紝Python鍙互pickle浠諱綍涓滆タ錛岄櫎浜嗭細
Compiled
code objects; functions and classes record just their names in pickles,
to allow for later reimport and automatic acquisition of changes made
in module files.
緙栬瘧鐨勪唬鐮佸璞★紱鍑芥暟鍜岀被鍦╬ickle涓彧鏄褰曚簡瀹冧滑鐨勫悕瀛楋紝浠ヤ究鍚庢潵閲嶆柊瀵煎叆鍜岃嚜鍔ㄨ幏鍙栨ā鍧楁枃浠朵腑鐨勬洿鏀廣?
Instances
of classes that do not follow class importability rules (more on this
at the end of the section "Shelve Files," later in this chapter).
涓嶉伒瀹堢被鍙鍏ヨ鍒欙紙class importability rule錛夌殑綾誨疄渚嬶紙鏈珷鍚庨潰“Shelve鏂囦歡”涓鑺傜殑灝鵑儴鏈夋洿澶氱浉鍏沖唴瀹癸級銆?
Instances
of some built-in and user-defined types that are coded in C or depend
upon transient operating system states (e.g., open file objects cannot
be pickled).
鐢–緙栫爜鐨勬垨渚濊禆浜庢搷浣滅郴緇熺灛鎬佺殑涓浜涘唴緗殑鍜岀敤鎴峰畾涔夌被鍨嬬殑瀹炰緥錛堜緥濡傦紝鎵撳紑鐨勬枃浠跺璞℃棤娉昿ickle錛夈?
A PicklingError is raised if an object cannot be pickled.
濡傛灉瀵硅薄涓嶈兘pickle錛屼細寮曞彂PickleError銆?
19.4.3. Pickler Protocols and cPickle
19.4.3. Pickler鍗忚鍜宑Pickle
In
recent Python releases, the pickler introduced the notion of
protocolsstorage formats for pickled data. Specify the desired protocol
by passing an extra parameter to the pickling calls (but not to
unpickling calls: the protocol is automatically determined from the
pickled data):
鍦ㄦ渶榪戠殑Python鐗堟湰涓紝pickler鎺ㄥ嚭浜嗗崗璁殑姒傚康錛歱ickle鏁版嵁鐨勪繚瀛樻牸寮忋傞氳繃pickle璋冪敤鏃朵紶鍏ヤ竴涓澶栫殑鍙傛暟錛屽彲鎸囧畾鎵闇鐨勫崗璁紙浣唘npickle璋冪敤涓嶉渶瑕侊細鍗忚鏄嚜鍔ㄤ粠宸瞤ickle鐨勬暟鎹‘瀹氱殑錛夛細
pickle.dump(object, file, protocol)
Pickled
data may be created in either text or binary protocols. By default, the
storage protocol is text (also known as protocol 0). In text mode, the
files used to store pickled objects may be opened in text mode as in
the earlier examples, and the pickled data is printable ASCII text,
which can be read (it's essentially instructions for a stack machine).
Pickle
鏁版嵁鍙互鎸夋枃鏈崗璁垨浜岃繘鍒跺崗璁駭鐢熴傞粯璁ゆ儏鍐典笅錛屽瓨鍌ㄥ崗璁槸鏂囨湰鍗忚錛堜篃縐頒負0鍙峰崗璁級銆傚湪鏂囨湰妯″紡涓嬶紝鐢ㄦ潵瀛樺偍pickle瀵硅薄鐨勬枃浠跺彲浠ョ敤鏂囨湰妯″紡
鎵撳紑錛屽涓婅堪鐨勪緥瀛愶紝騫朵笖pickle鐨勬暟鎹槸鍙墦鍗扮殑ASCII鏂囨湰錛屽茍涓旀槸鍙鐨勶紙榪欏熀鏈笂鏄鍫嗘爤鏈哄疄鐜扮殑鎸囩ず錛夈?
The
alternative protocols (protocols 1 and 2) store the pickled data in
binary format and require that files be opened in binary mode (e.g.,
rb, wb). Protocol 1 is the original binary format; protocol 2, added in
Python 2.3, has improved support for pickling of new-style classes.
Binary format is slightly more efficient, but it cannot be inspected.
An older option to pickling calls, the bin argument, has been subsumed
by using a pickling protocol higher than 0. The pickle module also
provides a HIGHEST_PROTOCOL variable that can be passed in to
automatically select the maximum value.
鍏?
浠栧崗璁紙1鍙峰拰2鍙峰崗璁?
錛変互浜岃繘鍒舵牸寮忓瓨鍌╬ickle鏁版嵁錛屽茍瑕佹眰鏂囦歡浠ヤ簩榪涘埗妯″紡鎵撳紑錛堜緥濡傦細rb銆亀b錛夈?鍙峰崗璁槸鍘熷浜岃繘鍒舵牸寮忥紱2鍙峰崗璁槸Python
2.3澧炲姞鐨勶紝瀹冩敼鍠勪簡瀵規柊鍨嬬被pickle鐨勬敮鎸併備簩榪涘埗鏍煎紡鏁堢巼鏇撮珮涓鐐癸紝浣嗗畠鏃犳硶榪涜鏌ョ湅銆傛棫鐨刾ickle璋冪敤鏈変竴涓夐」錛屽嵆bin鍙傛暟錛岀幇宸茶
褰掑叆浣跨敤澶т簬0鐨勫崗璁俻ickle妯″潡榪樻彁渚涗簡涓涓狧IGHEST_PROTOCOL鍙橀噺錛屼紶鍏ュ畠鍙互鑷姩閫夋嫨鏈澶х殑鍗忚鍊箋?
One
note: if you use the default text protocol, make sure you open pickle
files in text mode later. On some platforms, opening text data in
binary mode may cause unpickling errors due to line-end formats on
Windows:
娉ㄦ剰錛氬鏋滄偍浣跨敤榛樿鐨勬枃鏈崗璁紝浠ュ悗璇峰姟蹇呬互鏂囨湰妯″紡鎵撳紑pickle鏂囦歡銆傚湪涓浜涘鉤鍙頒笂錛屽洜涓篧indows鐨勮灝炬牸寮忎笉鍚岋紝浠ヤ簩榪涘埗妯″紡鎵撳紑鏂囨湰鏁版嵁鍙兘浼氬鑷磚npickle閿欒:
>>> f = open('temp', 'w') # text mode file on Windows
>>> pickle.dump(('ex', 'parrot'), f) # use default text protocol
>>> f.close( )
>>>
>>> pickle.load(open('temp', 'r')) # OK in text mode
('ex', 'parrot')
>>> pickle.load(open('temp', 'rb')) # fails in binary
Traceback (most recent call last):
File "<pyshell#337>", line 1, in -toplevel-
pickle.load(open('temp', 'rb'))
...lines deleted...
ValueError: insecure string pickle
One
way to sidestep this potential issue is to always use binary mode for
your files, even for the text pickle protocol. Since you must open
files in binary mode for the binary pickler protocols anyhow (higher
than the default 0), this isn't a bad habit to get into:
鍥為伩榪欎釜娼滃湪闂鐨勬柟娉曚箣涓鏄紝鎬繪槸浣跨敤浜岃繘鍒舵ā寮忕殑鏂囦歡錛屽嵆浣挎槸鐢ㄦ枃鏈琾ickle鍗忚銆傝嚦灝戝浜庝簩榪涘埗pickler鍗忚錛堥珮浜庨粯璁?錛夛紝鎮ㄥ繀欏諱互浜岃繘鍒舵ā寮忔墦寮鏂囦歡錛屾墍浠ヨ繖涓嶆槸涓涓潖涔犳儻錛?
>>> f = open('temp', 'wb') # create in binary mode
>>> pickle.dump(('ex', 'parrot'), f) # use text protocol
>>> f.close( )
>>>
>>> pickle.load(open('temp', 'rb'))
('ex', 'parrot')
>>> pickle.load(open('temp', 'r'))
('ex', 'parrot')
Refer
to Python's library manual for more information on the pickler. Also
check out marshal, a module that serializes an object too, but can
handle only simple object types. pickle is more general than marshal
and is normally preferred.
璇峰弬鑰働ython搴撴墜鍐岋紝浠ヤ簡瑙f洿澶歱ickler鐨勪俊鎭傚彟澶栵紝璇鋒煡闃卪arshal錛屽畠涔熸槸涓涓簭鍒楀寲瀵硅薄鐨勬ā鍧楋紝浣嗗彧鑳藉鐞嗙畝鍗曞璞$被鍨嬨俻ickle姣攎arshal鏇撮氱敤錛屽茍閫氬父鏄閫夈?
And
while you are flipping (or clicking) through that manual, be sure to
also see the entries for the cPickle modulea reimplementation of pickle
coded in C for faster performance. You can explicitly import cPickle
for a substantial speed boost; its chief limitation is that you cannot
subclass its versions of Pickle and Unpickle because they are
functions, not classes (this is not required by most programs). The
pickle and cPickle modules use compatible data formats, so they may be
used interchangeably.
鑰?
褰撲綘緲葷湅錛堟垨鐐瑰嚮錛塒ython鎵嬪唽鏃訛紝璇蜂竴瀹氫篃瑕佺湅鐪媍Pickle妯″潡鐨勬潯鐩紝瀹冩槸pickle鐨凜璇█瀹炵幇錛屾ц兘涓婃洿蹇傛偍鍙互鏄懼紡瀵煎叆
cPickle鏇夸唬pickle錛屼互澶у箙鎻愬崌閫熷害錛涘叾涓昏鐨勯檺鍒舵槸錛屼綘涓嶈兘緇ф壙璇ョ増鏈殑Pickle鍜孶npickle錛屽洜涓哄畠浠槸鍑芥暟錛岃屼笉鏄被錛堝鏁?
紼嬪簭騫朵笉瑕佹眰瀹冧滑鏄被錛夈俻ickle鍜宑Pickle妯″潡浣跨敤鍏煎鐨勬暟鎹牸寮忥紝鎵浠ュ畠浠彲浠ヤ簰鎹嬌鐢ㄣ?
If
it is available in your Python, the shelve module automatically chooses
the cPickle module for faster serialization, instead of pickle. I
haven't explained shelve yet, but I will now.