Python使用CMD模块更优雅的运行脚本


Posted in Python onMay 11, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
python共享引用(多个变量引用)示例代码
Dec 04 Python
python实现从web抓取文档的方法
Sep 26 Python
Python访问纯真IP数据库脚本分享
Jun 29 Python
Python的Django框架下管理站点的基本方法
Jul 17 Python
实现python版本的按任意键继续/退出
Sep 26 Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
Jun 13 Python
Python一行代码解决矩阵旋转的问题
Nov 30 Python
解决springboot yml配置 logging.level 报错问题
Feb 21 Python
Python + selenium + crontab实现每日定时自动打卡功能
Mar 31 Python
Python读写锁实现实现代码解析
Nov 28 Python
Python tkinter之ComboBox(下拉框)的使用简介
Feb 05 Python
python自动生成sql语句的脚本
Feb 24 Python
Python中DJANGO简单测试实例
May 11 #Python
python单元测试unittest实例详解
May 11 #Python
Python使用MYSQLDB实现从数据库中导出XML文件的方法
May 11 #Python
Python生成不重复随机值的方法
May 11 #Python
Python解析nginx日志文件
May 11 #Python
Python字符串替换实例分析
May 11 #Python
Python使用django获取用户IP地址的方法
May 11 #Python
You might like
PHP常用代码
2006/11/23 PHP
xajax写的留言本
2006/11/25 PHP
php图片上传存储源码并且可以预览
2011/08/26 PHP
php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
2015/12/21 PHP
php记录搜索引擎爬行记录的实现代码
2018/03/02 PHP
PHP排序二叉树基本功能实现方法示例
2018/05/26 PHP
javascript 打印页面代码
2009/03/24 Javascript
JavaScript 常用函数库详解
2009/10/21 Javascript
JQuery中each()的使用方法说明
2010/08/19 Javascript
javascript实现博客园页面右下角返回顶部按钮
2015/02/22 Javascript
推荐10 款 SVG 动画的 JavaScript 库
2015/03/24 Javascript
JS中跨页面调用变量和函数的方法(例如a.js 和 b.js中互相调用)
2016/11/01 Javascript
vue移动端UI框架实现QQ侧边菜单组件
2018/03/09 Javascript
Angularjs中date过滤器失效的问题及解决方法
2018/07/06 Javascript
vue系列之requireJs中引入vue-router的方法
2018/07/18 Javascript
JS实现给数组对象排序的方法分析
2019/06/24 Javascript
vue通过video.js解决m3u8视频播放格式的方法
2019/07/30 Javascript
layui 实现加载动画以及非真实加载进度的方法
2019/09/23 Javascript
使用js实现单链解决前端队列问题的方法
2020/02/03 Javascript
Python实现配置文件备份的方法
2015/07/30 Python
python 实时遍历日志文件
2016/04/12 Python
Python 列表(List) 的三种遍历方法实例 详解
2017/04/15 Python
Python字符串和字典相关操作的实例详解
2017/09/23 Python
matplotlib作图添加表格实例代码
2018/01/23 Python
Python实现识别图片内容的方法分析
2018/07/11 Python
详解如何从TensorFlow的mnist数据集导出手写体数字图片
2019/08/05 Python
python中upper是做什么用的
2020/07/20 Python
纯CSS3大转盘抽奖示例代码(响应式、可配置)
2017/01/13 HTML / CSS
物流管理专业大学生自荐信
2013/10/04 职场文书
人力资源管理专业自荐书范文
2014/02/10 职场文书
2014年教研活动总结范文
2014/04/26 职场文书
小学综治宣传月活动总结
2014/07/02 职场文书
会计工作检讨书
2015/02/19 职场文书
2019秋季运动会口号
2019/06/25 职场文书
SQL Server中常用截取字符串函数介绍
2022/03/16 SQL Server
分享几个实用的CSS代码块
2022/06/10 HTML / CSS