Python uses the CMD module for more elegant running scripts and pythoncmd module scripts

Source: Internet
Author: User

Python uses the CMD module for more elegant running scripts and pythoncmd module scripts

This example describes how to use the CMD module to run scripts more elegantly in Python. Share it with you for your reference. The specific analysis is as follows:

It is inconvenient to debug some things for testers. Although some scripts are written.

The cmd module of python provides a more elegant way.

I just wrote some common test code:

#-*-Coding: UTF-8-*-import OS, sysfrom datetime import datetimeimport cmdimport warningsfrom django. conf import settingsdef not_exit (func): def _ func (* args, ** kwargs): try: res = func (* args, ** kwargs) return res failed t: print "there is a wrong in % s, please input 'help % s' to get help" % (func. _ name __, func. _ name _ [3:]) return None return _ funcclass NBScript (cmd. cmd): def _ init _ (self): cmd. cmd. _ init _ (self) # Set the command prompt self. prompt = '>' # configure the django environment 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 '] = 'setting' # CLEAR user 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 (sns_id) # Upgrade 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 () # Add User Pearl 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) # Fish 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) # split shell 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 () # Fish food 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 () # Fish Tank 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) # score item 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) # Brush job 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 () # Tune the synthesis level 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_quitif _ name _ = "_ main _": nb_script = NBScript () nb_script.nested loop ()

Here, we can use the cmd module for simple learning.

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.