《Python基礎教程》第20章學習筆記

來源:互聯網
上載者:User

標籤:pattern   tle   bsp   hub   stdin   size   south   文檔   clu   

python實現:https://github.com/captainwong/instant_markup

c++實現:https://github.com/captainwong/instant_markup_cpp

要點:

1.標準輸入輸出資料流的重新導向

python markup.py < test_input.txt > test_output.html

上述命令將標準輸入裝置重新導向為檔案input.txt,將標準輸出裝置重新導向為檔案test_output.html。

Python中使用的標準輸入裝置為sys.stdin, 輸出使用函數print。C語言使用stdin和函數printf等。c++使用cin。 cout。


2.使用字串調用函數

如依據字串"foo"尋找函數foo並調用之。

def callback(self, prefix, name, *args):method = getattr(self, prefix+name, None)if callable(method):return method(*args)def start(self, name):self.callback(‘start_‘, name)def end(self, name):self.callback(‘end_‘, name)def sub(self, name):def substitution(match):result = self.callback(‘sub_‘, name, match)if result is None: match.group(0)return resultreturn substitution


使用時可通過調用

start(‘document‘)

來調用start_document函數。



c++無此特性。這裡我使用map儲存函數名和函數指標的方法來類比這樣的功能。

首先定義函數指標:

typedef void (CHandler::*pFunc)();

定義靜態map成員:

static map<string, pFunc> m_funcmap;

使用宏定義簡化初始化操作:

#define STR(str) #str#define ASSIGN_FUNC(func_name) CHandler::m_funcmap[STR(func_name)] = (CHandler::pFunc)&func_name;

初始化:

CHTMLRenderer::CHTMLRenderer(){ASSIGN_FUNC(CHTMLRenderer::start_document);ASSIGN_FUNC(CHTMLRenderer::end_document);ASSIGN_FUNC(CHTMLRenderer::start_paragraph);ASSIGN_FUNC(CHTMLRenderer::end_paragraph);ASSIGN_FUNC(CHTMLRenderer::start_heading);ASSIGN_FUNC(CHTMLRenderer::end_heading);ASSIGN_FUNC(CHTMLRenderer::start_list);ASSIGN_FUNC(CHTMLRenderer::end_list);ASSIGN_FUNC(CHTMLRenderer::start_listitem);ASSIGN_FUNC(CHTMLRenderer::end_listitem);ASSIGN_FUNC(CHTMLRenderer::start_title);ASSIGN_FUNC(CHTMLRenderer::end_title);ASSIGN_FUNC_SUB(CHTMLRenderer::sub_emphasis);ASSIGN_FUNC_SUB(CHTMLRenderer::sub_url);ASSIGN_FUNC_SUB(CHTMLRenderer::sub_mail);}


調用方法:
void CHandler::callback(const string &str){funcmap_iter iter = m_funcmap.find(str);if(iter != m_funcmap.end())(this->*(iter->second))();elsecout << "invalid function name : " << str << endl;}void CHandler::start(const string &func_name){callback(string("CHTMLRenderer::start_") + func_name);}void CHandler::end(const string &func_name){callback(string("CHTMLRenderer::end_") + func_name);}

3.使用正則表達式

Python標準庫提供了re包來進行正則表達式的處理。而c++標準庫沒有實現regex,boost::regex功能強大,但為了寫一個小demo,包括一大堆庫太麻煩。

我使用一個輕量級的開源c++實現正則庫deelx。官網: http://www.regexlab.com/

整個庫就是一個標頭檔deelx.h,使用時include之就可以。

示範範例:

string filtering(const string& block, const string& pattern,const string& sub_name, CHandler* handler){static CRegexpT <char> regexp;regexp.Compile(pattern.c_str());MatchResult result = regexp.Match(block.c_str());if(result.IsMatched()){char* content = regexp.Replace(block.c_str(),handler->sub(sub_name).c_str());string new_block(content);regexp.ReleaseString(content);return new_block;}return block;}
deelx原始碼與文檔可在其官網下載。



結果例如以:


《Python基礎教程》第20章學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.