python中騰訊微博管家-定時發布進度

來源:互聯網
上載者:User

這兩天抽空完成了定時發布功能的設定部分,比如添加定時,刪除定時,顯示當前已定時任務。

定時功能由主菜單選擇進入
增加一條定時任務命令格式:
add 日期時間 發布內容

add 2014-11-05/08:00:00 這是一條微博!
顯示當前工作清單命令為show,列出當前任務的索引編號、定時時間、發布內容。
刪除某條任務,命令格式:
del 索引編號

del 1
基本實現代碼:

 代碼如下 複製代碼
#定時發布任務設定
p = re.compile(r'\s+')
while True:
    code = raw_input('設定或修改定時發布任務:')
    if code=='':
        showMainMenu()
        break
    code = p.sub(' ', code)
    args = code.split(' ')
    operation = {
        'add': lambda x,y:add_auto_post_task(x,y),
        'del': lambda x,y:del_auto_post_task(x),
        'show':lambda x,y:show_auto_post_task()
        }
    #輸入如show 或del刪除命令時,由於args數組長度不夠,我也沒想出什麼更好的方法,所以這裡進行了長度填充。
    while len(args)<3:
        args.append(' ')
    print operation[args[0]](args[1],args[2])

這裡使用了字典+匿名函數來類比switch功能,而且由於我定義的匿名函數參數列表長度相等,所以在最後一行 可以統一調用。
三個匿名函數調用的方法代碼如下,其中autoPostTaskListT和C分別為儲存任務時間和任務發布內容的兩個同長度列表,日期時間還應該轉為時間戳記,我這裡暫時沒處理:

 代碼如下 複製代碼
#添加定時任務
def add_auto_post_task(t,c):
    try:
        autoPostTaskListT.append(t)
        autoPostTaskListC.append(c)
        return True
    except:
        return False
#刪除定時任務
def del_auto_post_task(i):
    try:
        i = int(i)
        del autoPostTaskListT[i]
        del autoPostTaskListC[i]
        return True
    except:
        return False
#列出當前定時工作清單
def show_auto_post_task():
    try:
        for i in xrange(len(autoPostTaskListT)):
            print i,"\t",autoPostTaskListT[i], "\t", autoPostTaskListC[i]
        return True
    except:
        return False

剩下任務:如何設定一個子線程去進行定時掃描list來實現定時。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.