跳到主要內容

發表文章

目前顯示的是有「Python」標籤的文章

C0 Scanner and Parser in Python

下雨天在家沒事作,整理書架時,翻了一下之前介紹過的 系統程式 一書,突然想到自己好一陣子沒機會寫 Python ,就試著用 Python 改寫書中所實作的 C0 語言 scanner  以及 parser。C0 是一個類似 C 的語言,但經過大幅簡化,所以很適合當作一個下午的娛樂。:-) scanner 的部份基本只用了基本的字串處理,輸出 token list 後,餵給 parser 。parser 則用簡單的 recursive descent 完成。可以從 github 上抓下來玩玩,運行結果如下: [mars@dream compiler]$ cat sample0.c0 reto = 9; reto = reto + 1; return reto; [mars@dream compiler]$ python parser.py sample0.c0 :PROG  :BASELIST   :BASE    :STATMENT     :id reto     := =     :EXP      :number 9    :; ;   :BASE    :STATMENT     :id reto     := =     :EXP      :id reto      :+ +      :number 1    :; ;   :BASE    :STATMENT     :return return     :id reto    :; ;

Using descriptor as explicit delegation in Python

Descriptors in Python is an interesting approach to simplifying many difficult design problems. I would like to share one of my application of this useful feature. Consider the following code snippet: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class MailEvent ( object ): def fire ( self , instance , cls ): print "handling mail event from " + str ( cls . __name__ ) + " " + str ( instance . name ) class LogEvent ( object ): def fire ( self , instance , cls ): print "handling log event from " + str ( cls . __name__ ) + " " + str ( instance . name ) # using inheritance class BaseWorker ( object ): def __init__ ( self , name ): self . name = name self . event_factory = { "mail" : MailEvent (), "log" : LogEvent ()} def event ( self , evt ): if evt in self . event_factory : self ....

How to write a software watchdog in Python?

Software watchdog is essential to monitor system's status, especially for those run for very long period of time, such as server programs. The basic idea is simple: construct a trustworth process or thread to monitor the real working process or thread. However, after googling a while, I could not get any satisfactory solution in Python. I think it's time to write this by myself. In this article, I will share how I implement a software watchdog in Python. Before we go on, you may want to pull my code from github to play with. The first thing is to determine what kind of model we want watchdog to run? There are two choices: be a thread, or be a process. Being a thread, we can simplify communication between watchdog and working thread. The problem is that if there are some resources hold on with working thread, watchdog might deadlock. Moreover if working thread is trapped in some loop, we have no graceful way to kill it. My choice is obvious: let watchdog and working on...

How to debug recursive functions in Python?

Recursive function is the natural way to solve problems when you encounter functional programming. However it is sometimes hard to realize the run-time execution flow even the function is simple. I used to running python debugger and use "commands" to investigate the behavior: ( Pdb )  l    1   - >   def  iseven ( n ) :    2           return   True   if  n ==  0   else  isodd ( n -  1 )    3    4       def  isodd ( n ) :    5           return   False   if  n ==  0   else  iseven ( n -  1 )    6    7       print ( iseven ( 4 ) ) [ EOF ] ( Pdb )  b  2 Breakpoint  1  at /home/mars/ try /python/decorator/use_pdb. py : 2 ( Pdb )  b  5 Breakpoint  2 ...