Background task
Before, when a test system, often need to use the phone number, but a phone number can not be used again after use, so often want some of the available phone numbers, such as 18,888,888,888 and so on, each time you want the phone number is also very troublesome , so this thinking about a small tool to generate a mobile phone number.
Implementation process
1, the basic realization
import random list_1 = ["134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172", "178", "182", "183", "184", "187", "188", "198"] # 中国移动号码段 list_2 = ["130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186"] # 中国联通号码段 list_3 = ["133", "149", "153", "173", "177", "180", "181", "189", "191", "199", "193"] # 中国电信号码段 num = ["0", "1", "2", "3", "4", "5", "6", "7", "8", " 9 " ] # store 0-9, 4-11 digit number taken from here phone_all = List () # store all generated telephone number phone_output = List () # telephone number stored after deduplication DEF create_phone (COUNT, choice): # is the number of parameters to generate a number of parameters selected for the operator 2 for T in Range (COUNT): Phone = the random.choice (choice) + "" .join (the random.choice (NUM) for I in the Range (8 )) # use random function to generate phone numbers IF phone not in phone_all: # determine the phone number is not appeared phone_output.append (phone) # no-show, then put phone_output phone_all.append (phone) # the number of each generation are stored up to a weight ratio of spent Print (phone_output) # after printing to heavy telephone IF the __name__ == ' __main__ ' : create_phone ( 10, list_3)
2, using the Tkinter interface to do a small tool
code show as below
# - * - Coding: UTF-8 - * - Import Tkinter AS tk from Tkinter Import TTK from Tkinter Import * Import Random class Phone (): DEF __init__ (Self): self.window = tk.Tk () # create window window self.window.title ( " phone number generator " ) # define a window Title # self.window.resizable (0,0) # Do not adjust the window size self.menu = ttk.Combobox (self.window, width =. 6 ) Self .path = StringVar () # Self.lab1 = tk.Label (self.window, text = "target path:") self.lab2 = tk.Label (self.window, text = " Select a carrier: " ) self.lab3 = tk.Label ( self.window, text = " generate the number: " ) self.count = tk.Entry (self.window, width = 5 ) self.info = tk.Text (self.window, height = 20) # create a text box to show , and sized self.menu [ ' value ' ] = ( ' China Unicom ' , ' China mobile ' , ' China Telecom ' ) self.menu.current(0) # Add a button to trigger the generation number self.t_button1 = tk.Button (self.window, text = ' generates a number ' , Relief = tk.RAISED, width =. 8, height =. 1 , Command = self.create_phone) # adding a button to trigger flushing of the function block output self.c_button2 = tk.Button (self.window, text = ' empty output ' , Relief = tk.RAISED, width =. 8, height =. 1, Command = self.cle) DEF gui_arrang (Self): "" " completion page layout element, the position of each member is provided " "" # self.lab1.grid (Row = 0, column = 0) self.lab2.grid (Row = 0, column 0 = ) column=0) #Title selection button operator position self.menu.grid (Row = 0, column. 1 =, = W is Sticky) # selection drop-down box operator position self.lab3.grid (Row =. 1, column = 0) # generated header position Number self.count.grid (Row =. 1, column =. 1, W is = Sticky) # generates the number of input frame position self.info.grid (row = 2, rowspan = 5, column = 0, columnspan = 3, padx = 15, = 15 pady) # show results of the text box position self.t_button1.grid (Row = 0, column = 2) # generated number buttons position self.c_button2.grid (Row =. 1, column = 2) # Clear output button DEF get_choice ( Self): category = { ' list_1 ' : [ " 134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172", "178", "182", "183", "184", "187", "188", "198"], # 中国移动号码段 'list_2': ["130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186"], # 中国联通号码段 'list_3': ["133", "149", "153", "173", "177", "180", "181" , " 189 " , " 191 " , " 199 " , " 193 " ] # telecommunication number segment China } CID = None IF self.menu.get () == " China Unicom " : CID = category [ " list_1 " ] elif self.menu.get () == " China mobile " : cid = category [ " list_2 "] elif self.menu.get() == "中国电信": cid = category["list_3"] return cid @staticmethod def basic_num(): num = ["0", "1", "2", "3", "4", "5", "6", "7", ". 8 " , " . 9 " ] return NUM DEF create_phone (Self): phone_all = List () # store all generated telephone number phone_output = List () # stored telephone number after the de-emphasis for T in Range (int (self.count .get ())): Phone = The random.choice (self.get_choice ()) + "" .join (The random.choice (self.basic_num ()) for I in Range (. 8 )) IF Phone Not in phone_all: phone_output .append (phone) # Determined telephone number is not occurred, does not appear to be appended to the phone_output # phone_output = "" .join (Phone) phone_all.append (Phone) # the generated numbers are appended to each phone_all (with reference to the weight thereof) # = phone_all "" .join (Phone) # Print (phone_output) STEP =. 6 # sets a value, each display number 6 for B in [phone_output [I: I + STEP] for I in Range (0, len (phone_output ), STEP)]: # every print number 6 Print ( " , " .join (B)) # taken out of the list and the number of "," separated, in the form of a string self.info.insert ( ' End' , " , " .Join (B) + ' \ n- ' ) # output to the page, and each outputs a set (6) is added to a newline DEF CLE (Self): "" " Define a function, for empty the contents of the output of block "" " self.info.delete ( 1.0, " End " ) # clear from the first row to the last row DEF main (): T = Phone () t.gui_arrang () tk.mainloop () IF the __name__ == ' __main__ ' : main ()
Interface is as follows:
Problems encountered
Since the beginning of time is output to the interface according to the size of the display frame wrap, so sometimes encounter a number displayed in two rows, in order to solve this problem, consider the following:
(1) each output only six phone numbers, and that is, for a group of six
(2) a group of outputs, outputs a newline followed by
On the first point, the Internet search a bit, how to output data in accordance with a list of a certain number of groups, as follows
Reference blog: HTTPS: //blog.csdn.net/Mr_Cat123/article/details/80584988
A = [1,2,3,4,5,6,7,8,9,10,11 ]
STEP =. 3
B = [A [I: I + STEP] for I in Range (0, len (A), STEP)]
Print (B)
>>> [[. 1, 2,. 3], [. 4,. 5,. 6], [. 7 ,. 8,. 9], [10,. 11]]
>>> B [ . 1]
[ . 4, . 5, . 6]
above step 3 refers to the output of each digit
On the second point, appending a newline starts after each list, each was found to display interface displays a {}
Later, the problem may be thought of the data format, the newline is not added to the list, then treated a bit first converted into a list of characters (using join () method), then each is added after a newline character, the following