Python使用CMD模块更优雅的运行脚本
作者:蛇小狼 发布时间:2022-05-16 03:53:50
标签:Python,CMD
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- def Num2MoneyFormat( change_number ): ""&q
- 我们知道在Windows下多版本共存的配置方法就是改可执行文件的名字,配置环境变量。Linux中的配置原理差不多,思路就是生成软链接,配置到
- 在网上看过很多版本的PHP异步请求方法,这里简单总结几个常用方法分享给大家 1、用CURL实现一步请求 CURL扩展是我们在开发过程中最常用
- 这篇文章主要介绍了python多进程并行代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 一、MySQL Workbench简介MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品。
- 程序代码: '关键字的搜索 str="select * from tableNam
- 本文实例讲述了python解析xml文件操作的实现方法。分享给大家供大家参考。具体方法如下:xml文件内容如下:<?xml versi
- Environs是解析环境变量的Python库。它的开发受envparse启发,底层使用marshmallow验证并序列化值。在运行一个项目
- 随着MYSQL版本的更新以及电脑系统的变化,我们给大家整理了各种电脑环境下安装MYSQL的图解过程,希望我们整理的内容能够帮助到大家:mys
- Tip:本文仅供学习与交流,切勿用于非法用途!!!背景介绍有个同学问我:“XXX,有没有办法搜集一下淘宝的商品信息啊,我想要做个统计”。于是
- 目录step1 搭建框架step2 填充搭建好了的Promise框架总结step1 搭建框架1. 首先我们需要在这里放置一个promise函
- 有的时候取出全部数据库记录也许正好满足你的要求,不过,在大多数情况下我们通常只需得到部分记录。这样一来该如何设计查询呢?当然会更费点脑筋了,
- 环境 python -m venv venvvenv\Scripts\activate 激活环境失败 解决方案输入get-execution
- #Filename:brn.py #Description: batch replace certain words in file nam
- 一、安装pycryto能实现大致3种类型的数据加密(单向加密、对称加密 和非对称加密),产生随机数,生成密钥对,数字签名单向加密:Crypt
- 网上关于Python的音视频播放示例都集中在简单的多媒体库或者PyGame这样的游戏库,有些库使用简单,但功能单一,有些库功能丰富,支持的格
- 最近开发了一个网站,大量采用了html5和css3,希望用户都以webkit内核打开页面,但是测试却发现360的以ie内核打开为推荐模式,不
- Python是一个很酷的语言,因为你可以在很短的时间内利用很少的代码做很多事情。不仅如此,它还能轻松地支持多任务,比如多进程等。Python
- 线程线程(Thread),有时也被称为轻量级进程(Lightweight Process,LWP),是操作系 * ⽴调度和分派的基本单位,本质
- 铺垫在大量的实践中,似乎我们总是通过类似的方式来使用异步编程:监听事件事件发生执行对应的回调函数回调完成(可能产生新的事件添加进监听队列)回