Python使用CMD模組更優雅的運行指令碼

來源:互聯網
上載者:User
本文執行個體講述了Python使用CMD模組更優雅的運行指令碼的方法。分享給大家供大家參考。具體分析如下:

平時由於經常給測試人員調試一些東西,雖然寫了一些指令碼,感覺還是不方便。

python的cmd模組提供的更優雅的方式,很喜歡。

剛寫了一些平時常用的測試代碼:

# -*- coding:utf-8 -*-import os, sysfrom datetime import datetimeimport cmdimport warningsfrom django.conf import settingsdef not_exit(func):  def _func(*args, **kwargs):    try:      res = func(*args, **kwargs)      return res    except:      print "there is a wrong in %s, please input 'help %s' to get help" % (func.__name__, func.__name__[3:])      return None  return _funcclass NBScript(cmd.Cmd):  def __init__(self):    cmd.Cmd.__init__(self)    #設定命令提示字元    self.prompt = '> '    #配置django環境    pathname = os.path.dirname(sys.argv[0])    sys.path.append(os.path.abspath(pathname))    sys.path.append(os.path.normpath(os.path.join(os.path.abspath(pathname), '../')))    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'  #清使用者cache  def help_clear_cache(self):    print "Usage:clear_cache [sns_id]"    print "-- clear the user's cache"  @not_exit  def do_clear_cache(self, sns_id):    from scripts.update_cache import clear_member_all_cache    clear_member_all_cache(sns_id)  #使用者升級  def help_user_level_up(self):    print "Usage:user_level_up [sns_id] [level]"    print "-- level up the user to the level"  @not_exit  def do_user_level_up(self, parms):    from apps.members.cache import get_member_by_sns_id, get_or_create_almanac_info    from apps.members.constants import EXP_LEVEL    sns_id, level = parms.split()    sns_id = int(sns_id)    level = int(level)    member = get_member_by_sns_id(sns_id)    al_info = get_or_create_almanac_info(member.id)    al_info.exp = EXP_LEVEL[level-1] + 1    al_info.save()  #增加使用者珍珠  def help_add_pearls(self):    print "Usage:add_pearls [sns_id] [pearls]"    print "-- add some pearls to the user"  @not_exit  def do_add_pearls(self, parms):    from platforms.account.helper import add_account_pearls    sns_id, pearls = parms.split()    sns_id = int(sns_id)    pearls = int(pearls)    add_account_pearls('', '', sns_id, pearls, type='Test', money=0)  #分魚  def help_add_fishes(self):    print "Usage:add_fishes [sns_id] [fish1 fish2 ...]"    print "-- add some fishes to the user"  @not_exit  def do_add_fishes(self, parms):    from apps.members.cache import get_member_by_sns_id    from apps.members.helper import new_a_member_fish    from apps.products.cache import get_fish    array = parms.split()    sns_id = int(array[0])    fish_ids = [int(i) for i in array[1:]]    member = get_member_by_sns_id(sns_id)    for fish_id in fish_ids:      fish = get_fish(fish_id)      new_a_member_fish(member, fish.id, update_almanac=True)  #分貝殼  def help_add_shells(self):    print "Usage:add_shells [sns_id] [shells]"    print "-- add some shells to the user"  @not_exit  def do_add_shells(self, parms):    from apps.members.cache import get_member_by_sns_id    sns_id, num = parms.split()    sns_id = int(sns_id)    num = int(num)    member = get_member_by_sns_id(sns_id)    member.shells += num    member.save()  #分魚食  def help_add_fish_food(self):    print "Usage:add_fish_food [sns_id] [food_num]"    print "-- add some food to the user"  @not_exit  def do_add_fish_food(self, parms):    from apps.members.cache import get_member_by_sns_id    sns_id, num = parms.split()    sns_id = int(sns_id)    num = int(num)    member = get_member_by_sns_id(sns_id)    member.fish_food += num    member.save()  #分魚缸  def help_add_fish_tanks(self):    print "Usage:add_fish_food [sns_id] [tank1,tank2 ...]"    print "-- add some tanks to the user"  @not_exit  def do_add_fish_tanks(self, parms):    from apps.members.cache import get_member_by_sns_id    from apps.members.helper import new_a_member_tank    from apps.products.cache import get_fish_tank    array = parms.split()    sns_id = int(array[0])    tank_ids = [int(i) for i in array[1:]]    member = get_member_by_sns_id(sns_id)    for tank_id in tank_ids:      tank = get_fish_tank(tank_id)      new_a_member_tank(member, tank.id)  #分道具  def help_add_items(self):    print "Usage:add_items [sns_id] [itme1,item2 ...]"    print "-- add some itmes to the user"  @not_exit  def do_add_items(self, parms):    from apps.members.cache import get_member_by_sns_id    from apps.members.helper import new_a_member_item    from apps.products.cache import get_item    array = parms.split()    sns_id = int(array[0])    item_ids = [int(i) for i in array[1:]]    member = get_member_by_sns_id(sns_id)    for item_id in item_ids:      item = get_item(item_id)      new_a_member_item(member, item.id)  #刷任務  def help_refresh_task(self):    print "Usage:refresh_task [sns_id]"    print "-- refresh user's task"  @not_exit  def do_refresh_task(self, sns_id):    from apps.members.cache import get_member_by_sns_id    sns_id = int(sns_id)    member = get_member_by_sns_id(sns_id=sns_id)    member.task_start_time = None    member.save()  #調合成等級  def help_synthesis_level_up(self):    print "Usage:synthesis_level_up [sns_id] [level]"    print "-- level up user's synthesis level"  @not_exit  def do_synthesis_level_up(self, parms):    from apps.members.cache import get_member_by_sns_id    from apps.synthesis.cache import get_or_create_synth_info    sns_id, lev = parms.split()    sns_id = int(sns_id)    lev = int(lev)    member = get_member_by_sns_id(sns_id=sns_id)    exp = int(sum([i**1.5*20 for i in range(1,lev+1)]))    member = get_member_by_sns_id(sns_id)    synth = get_or_create_synth_info(member.id)    synth.synth_exp = exp    synth.save()  def help_quit(self):    print "Usage:quit"  def do_quit(self, line):    exit()  do_q = do_quitif __name__ == "__main__":  nb_script = NBScript()  nb_script.cmdloop()

這裡權當cmd模組的簡單學習了。

希望本文所述對大家的Python程式設計有所協助。

  • 聯繫我們

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