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 可爱的大小写
Sep 06 Python
使用python 获取进程pid号的方法
Mar 10 Python
Python结合ImageMagick实现多张图片合并为一个pdf文件的方法
Apr 24 Python
解决tensorflow模型参数保存和加载的问题
Jul 26 Python
详解Python进阶之切片的误区与高级用法
Dec 24 Python
python 中如何获取列表的索引
Jul 02 Python
python运用pygame库实现双人弹球小游戏
Nov 25 Python
Python hmac模块使用实例解析
Dec 24 Python
Python内置函数locals和globals对比
Apr 28 Python
Python如何将字符串转换为日期
Jul 31 Python
Python+logging输出到屏幕将log日志写入文件
Nov 11 Python
python获取淘宝服务器时间的代码示例
Apr 22 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的引用原因分析
2012/09/06 PHP
php实现文件下载(支持中文文名)
2013/12/04 PHP
php+mysql数据库查询实例
2015/01/21 PHP
PHP+jQuery翻板抽奖功能实现
2015/10/19 PHP
实现PHP框架系列文章(6)mysql数据库方法
2016/03/04 PHP
php中实现字符串翻转的方法
2017/02/22 PHP
初识javascript 文档碎片
2010/07/13 Javascript
jquery下拉select控件操作方法分享(jquery操作select)
2014/03/25 Javascript
jQuery学习总结之jQuery事件
2014/06/30 Javascript
javascript将数字转换整数金额大写的方法
2015/01/27 Javascript
浅谈javascript事件取消和阻止冒泡
2015/05/26 Javascript
JavaScript对象属性检查、增加、删除、访问操作实例
2015/07/08 Javascript
深入理解JS中的Function.prototype.bind()方法
2016/10/11 Javascript
详解如何使用webpack打包Vue工程
2017/05/27 Javascript
react-router4按需加载(踩坑填坑)
2019/01/06 Javascript
微信小程序 授权登录详解(附完整源码)
2019/08/23 Javascript
加速vue组件渲染之性能优化
2020/04/09 Javascript
Node.js path模块,获取文件后缀名操作
2020/11/07 Javascript
Python实现设置windows桌面壁纸代码分享
2015/03/28 Python
Python扩展内置类型详解
2018/03/26 Python
python 限制函数调用次数的实例讲解
2018/04/21 Python
numpy中的delete删除数组整行和整列的实例
2018/05/09 Python
python读取word文档,插入mysql数据库的示例代码
2018/11/07 Python
python 经典数字滤波实例
2019/12/16 Python
解决TensorFlow GPU版出现OOM错误的问题
2020/02/03 Python
python_mask_array的用法
2020/02/18 Python
python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
2020/03/06 Python
基于Python爬取fofa网页端数据过程解析
2020/07/13 Python
Python环境配置实现pip加速过程解析
2020/11/27 Python
详解px单位html5响应式方案
2018/03/08 HTML / CSS
HTML5触摸事件实现移动端简易进度条的实现方法
2018/05/04 HTML / CSS
2013年学期结束动员演讲稿
2014/01/07 职场文书
ktv筹备计划书
2014/05/03 职场文书
党代会心得体会
2014/09/04 职场文书
高中教师个人总结
2015/02/10 职场文书
pytorch 中nn.Dropout的使用说明
2021/05/20 Python