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实现连接mongodb的方法
May 08 Python
使用Python3 编写简单信用卡管理程序
Dec 21 Python
1 行 Python 代码快速实现 FTP 服务器
Jan 25 Python
Python查找两个有序列表中位数的方法【基于归并算法】
Apr 20 Python
在双python下设置python3为默认的方法
Oct 31 Python
Python切片操作去除字符串首尾的空格
Apr 22 Python
python 一个figure上显示多个图像的实例
Jul 08 Python
解决pycharm启动后总是不停的updating indices...indexing的问题
Nov 27 Python
解决matplotlib.pyplot在Jupyter notebook中不显示图像问题
Apr 22 Python
keras做CNN的训练误差loss的下降操作
Jun 22 Python
python爬虫中url管理器去重操作实例
Nov 30 Python
Python实现拼音转换
Jun 07 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常用的安全过滤函数集锦
2014/10/09 PHP
PHP抽奖算法程序代码分享
2015/10/08 PHP
PHP错误Warning:mysql_query()解决方法
2015/10/24 PHP
CodeIgniter读写分离实现方法详解
2016/01/20 PHP
php格式化json函数示例代码
2016/05/12 PHP
php中引用&的用法分析【变量引用,函数引用,对象引用】
2016/12/12 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
jQuery示例收集
2010/11/05 Javascript
js的表单操作 简单计算器
2011/12/29 Javascript
append和appendTo的区别以及appendChild用法
2013/12/24 Javascript
JavaScript 开发工具webstrom使用指南
2014/12/09 Javascript
jQuery实现带动画效果的多级下拉菜单代码
2015/09/08 Javascript
浅析javascript中的事件代理
2015/11/06 Javascript
Vue v2.4中新增的$attrs及$listeners属性使用教程
2018/01/08 Javascript
Node.js Windows Binary二进制文件安装方法
2019/05/16 Javascript
Vue.js中Line第三方登录api的实现代码
2020/06/29 Javascript
python实现人人网登录示例分享
2014/01/19 Python
python中hashlib模块用法示例
2017/10/30 Python
django admin添加数据自动记录user到表中的实现方法
2018/01/05 Python
Python根据欧拉角求旋转矩阵的实例
2019/01/28 Python
Python matplotlib学习笔记之坐标轴范围
2019/06/28 Python
Python + Flask 实现简单的验证码系统
2019/10/01 Python
python为QT程序添加图标的方法详解
2020/03/09 Python
浅析python 定时拆分备份 nginx 日志的方法
2020/04/27 Python
OpenCV+python实现实时目标检测功能
2020/06/24 Python
CSS超出文本指定宽度用省略号代替和文本不换行
2016/05/05 HTML / CSS
加利福尼亚州威尼斯的女性奢侈品设计师服装和概念店:Mona Moore
2018/09/13 全球购物
房屋出售协议书
2014/04/10 职场文书
《放小鸟》教学反思
2014/04/20 职场文书
软件项目开发计划书
2014/05/01 职场文书
基层党员公开承诺书
2014/05/29 职场文书
不听老师话的万能检讨书
2014/10/04 职场文书
学生会辞职信
2015/03/02 职场文书
2015年社区科普工作总结
2015/05/13 职场文书
解决tk mapper 通用mapper的bug问题
2021/06/16 Java/Android
使用 CSS 构建强大且酷炫的粒子动画效果
2022/08/14 HTML / CSS