服务器之家

服务器之家 > 正文

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

时间:2020-06-27 11:27     来源/作者:蛇小狼

本文实例讲述了Python使用CMD模块更优雅的运行脚本的方法。分享给大家供大家参考。具体分析如下:

平时由于经常给测试人员调试一些东西,虽然写了一些脚本,感觉还是不方便。

python的cmd模块提供的更优雅的方式,很喜欢。

刚写了一些平时常用的测试代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- 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程序设计有所帮助。

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部