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开发之字符串string操作方法实例详解
Nov 12 Python
python pandas 对series和dataframe的重置索引reindex方法
Jun 07 Python
对python中不同模块(函数、类、变量)的调用详解
Jul 16 Python
python 操作hive pyhs2方式
Dec 21 Python
PyTorch实现ResNet50、ResNet101和ResNet152示例
Jan 14 Python
在pycharm中实现删除bookmark
Feb 14 Python
python+opencv边缘提取与各函数参数解析
Mar 09 Python
python编写softmax函数、交叉熵函数实例
Jun 11 Python
Python生成器传参数及返回值原理解析
Jul 22 Python
Python如何读写二进制数组数据
Aug 01 Python
python 6行代码制作月历生成器
Sep 18 Python
Python中Cookies导出某站用户数据的方法
May 17 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.ini中文版(1)
2006/10/09 PHP
一个PHP数组应该有多大的分析
2009/07/30 PHP
PHP和Mysqlweb应用开发核心技术 第1部分 Php基础-3 代码组织和重用2
2011/07/03 PHP
php获取文章上一页与下一页的方法
2014/12/01 PHP
Yii配置文件用法详解
2014/12/04 PHP
PHP简单处理表单输入的特殊字符的方法
2016/02/03 PHP
php 微信公众平台开发模式实现多客服的实例代码
2016/11/07 PHP
PHP hebrev()函数用法讲解
2019/02/21 PHP
用javascript实现分割提取页面所需内容
2007/05/09 Javascript
兼容ie、firefox的图片自动缩放的css跟js代码分享
2013/08/12 Javascript
JavaScript 命名空间 使用介绍
2013/08/29 Javascript
JavaScript实现16进制颜色值转RGB的方法
2015/02/09 Javascript
Underscore.js 1.3.3 中文注释翻译说明
2015/06/25 Javascript
Javascript刷新窗口方法小结
2015/10/21 Javascript
快速掌握Node.js事件驱动模型
2016/03/21 Javascript
微信小程序开发之相册选择和拍照详解及实例代码
2017/02/22 Javascript
JS实现的倒计时恢复按钮点击功能【可用于协议阅读倒计时】
2018/04/19 Javascript
vue-cli与webpack处理静态资源的方法及webpack打包的坑
2018/05/15 Javascript
微信公众号开发之微信支付代码记录的实现
2019/10/16 Javascript
Python读写Redis数据库操作示例
2014/03/18 Python
socket + select 完成伪并发操作的实例
2017/08/15 Python
python opencv之SIFT算法示例
2018/02/24 Python
python实现月食效果实例代码
2019/06/18 Python
使用浏览器访问python写的服务器程序
2019/10/10 Python
python有几个版本
2020/06/17 Python
如何定义一个可复用的服务
2014/09/30 面试题
应届生高等护理求职信
2013/10/12 职场文书
软件工程师岗位职责
2013/11/16 职场文书
物业保安员岗位职责
2014/03/14 职场文书
年终考核实施方案
2014/05/26 职场文书
兵马俑导游词
2015/02/02 职场文书
vue.js Router中嵌套路由的实用示例
2021/06/27 Vue.js
Java并发编程之原子性-Atomic的使用
2022/03/16 Java/Android
详解Python flask的前后端交互
2022/03/31 Python
《遗弃》开发商删推文要跑路?官方回应:还在开发
2022/04/03 其他游戏
使用Bandicam录制鼠标指针并附带点击声音,还可以添加点击动画效果
2022/04/11 数码科技