本文实例讲述了Python使用CMD模块更优雅的运行脚本的方法。分享给大家供大家参考。具体分析如下:
平时由于经常给测试人员调试一些东西,虽然写了一些脚本,感觉还是不方便。
python的cmd模块提供的更优雅的方式,很喜欢。
刚写了一些平时常用的测试代码:
# -*- coding:utf-8 -*- import os, sys from datetime import datetime import cmd import warnings from django.conf import settings def 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 _func class 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_quit if __name__ == "__main__": nb_script = NBScript() nb_script.cmdloop()
这里权当cmd模块的简单学习了。
希望本文所述对大家的Python程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Python使用CMD模块更优雅的运行脚本”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年12月29日
2024年12月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]