Python 板


LINE

Cog 是一個 python-based 的程式碼產生器,http://nedbatchelder.com/code/cog/ 讓使用者可以把 python 混在 C++ 的程式碼裡面來自動產生 C++ 程式碼, 舉個例子,這是一個 C++ 原始檔,'Actions.cpp' /*[[[cog import cog fnames = ['DoSomething', 'DoAnotherThing', 'DoLastThing'] for fn in fnames: cog.outl("void %s();" % fn) ]]]*/ //[[[end]]] 你可以看到,python 的程式碼,實際上是寫在 C++ 的註解裡面, 在我們用命令列執行過 cog 以後: python cog.py -r Actions.cpp 他就變成這樣: /*[[[cog import cog fnames = ['DoSomething', 'DoAnotherThing', 'DoLastThing'] for fn in fnames: cog.outl("void %s();" % fn) ]]]*/ void DoSomething(); void DoAnotherThing(); void DoLastThing(); //[[[end]]] 黃色的部份是被產生出來的 C++ 程式碼。 最後一行的 //[[[end]]] 是讓 cog 可以辨識出哪些是自動產生的程式碼, 這樣使用者就可以在同一個檔案重複來生成原始碼, 而不是去使用兩個檔案,一個來源檔跟一個目的檔。 這是一個不錯的方法來自動產生 Java 或是 C++ 的 setter/getter 成員函式。 對於大部分的類別而言,我們常常需要一大票的 setter/getter 方法。 舉個 Java 的例子好了: class Color { int red ; int green ; int blue ; void setRed (int r) { red = r; } void setGreen (int g) { green = r; } void setBlue (int b) { blue = r; } int getRed () { return red ; } int getGreen () { return green ; } int getBlue () { return blue ; } } 使用 cog 的話,我們可以這樣寫: class Color { /*[[[cog import cog data = ['red', 'green', 'blue'] for d in data: cog.outl("int %s;" % d) cog.outl("void set%s%s(int v){%s=v;}" % (d[0].upper(), d[1:], d) ) cog.outl("int get%s%s(){return %s;}" % (d[0].upper(), d[1:], d) ) ]]]*/ //[[[end]]] } 重新模組化一下,這樣寫也不錯 /*[[[cog import cog def data_member(d) : cog.outl("int %s;" % d) cog.outl("void set%s%s(int v){%s=v;}" % (d[0].upper(), d[1:], d) ) cog.outl("int get%s%s(){return %s;}" % (d[0].upper(), d[1:], d) ) ]]]*/ //[[[end]]] class Color { /*[[[cog map ( data_member, ['red', 'green', 'blue'] ) ]]]*/ //[[[end]]] } 這樣當你還有別的類別,你就可以直接使用 data_member 這個函數了。 甚至呢,我們可以維護一個自己常用的 python 模組,像是 'mycog.py': import cog import MySQLdb # create data member with setter/getter def data_member(d) : cog.outl("int %s;" % d) cog.outl("void set%s%s(int v){%s=v;}" % (d[0].upper(), d[1:], d) ) cog.outl("int get%s%s(){return %s;}" % (d[0].upper(), d[1:], d) ) # create a class according to a table in database def create_table_class(table_name) : c = MySQLdb.connect(...).cursor() res = c.query('describe ' + table_name).fetchall() cog.outl('class %s%s {' % (table_name[0].upper(), table_name[1:])) for field = res : cog.outl(...) cog.outl('};') 這樣,你就可以像這樣複用這個模組: class Color { /*[[[cog import mycog map ( mycog.data_member, ['red', 'green', 'blue'] ) ]]]*/ //[[[end]]] } /*[[[cog mycog.create_table_class(student) ]]]*/ //[[[end]]] 你大概會得到(根據你資料庫表格欄位而定): class Color { /*[[[cog import mycog ap ( mycog.data_member, ['red', 'green', 'blue'] ) ]]]*/ int red; void setRed(int v){red=v;} int getRed(){return red;} int green; void setGreen(int v){green=v;} int getGreen(){return green;} int blue; void setBlue(int v){blue=v;} int getBlue(){return blue;} //[[[end]]] } /*[[[cog mycog.create_table_class(Student) ]]]*/ class Student { String name ; Date dayOfBirth ; String gender ; String Department ; ... } ; //[[[end]]] -- To iterate is human, to recurse is divine. 遞迴只應天上有, 凡人該當用迴圈.   L. Peter Deutsch --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.78.40 ※ 編輯: yoco315 來自: 140.114.78.40 (11/13 03:37) ※ 編輯: yoco315 來自: 140.114.78.40 (11/13 03:37) ※ 編輯: yoco315 來自: 140.114.78.40 (11/13 03:40)
1F:推 godfat:推,可惜不熟 python XD 11/13 12:08
2F:推 weijr:cog.outl("//[[[end]]]")會處理嗎? 11/13 15:19
3F:推 ykjiang:如果能再搭配 target language 的 parser 就更方便了 11/14 10:07
4F:→ yoco315:這個的意思是 @@? 11/14 20:33
5F:推 godfat:比方說 cog 可以分析 c++ 程式吧? 11/14 21:34
6F:推 ykjiang:例如說分析C code,遇到特定模式,就插入特定片段之類的 11/15 23:51
7F:推 PcGrek:推~ 12/27 12:25







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Soft_Job站內搜尋

TOP