Day 03, day
Key knowledge point: Function Definition, recursive function call, flag usage, eval () function
#! C: \ Program Files \ Python35 \ bin #-*-conding: UTF-8-*-# author: Frankdef search_backend (): search_list = [] # defines an empty list, store the backend information search_input = input ("please input the backend which you want to search \ n") with open ('haproxy. cfg ') as f: backend_flag = False # initialization flag False for line in f: if line. strip () = 'backend % s' % search_input: # When the backend information to be queried is matched, the flag is True, continue the next cycle backend_flag = True conti Nue if backend_flag and line. strip (). startswith ('backend'): # in the previous if statement, the flag is set to True. if it starts with 'backend' again, it indicates that the next backend information is already break if backend_flag and line. strip (): # append the rows except the last two if conditions to the search_list list, that is, the row search_list.append (line. strip () if len (search_list) = 0: print ("\ 033 [31; 1 m % s not exit, please re_input \ 033 [0 m" % search_input) search_backend () else: for node in search_list: # print each row of the List print (node) def add _ Backend (): args = ''' {'backend': '% s', 'record': {'server': '% s', 'weight ': '% s', 'maxconn':' % s'} ''' % (input ('backend: '), input ('server :'), input ('weight: '), input ('maxconn:') B = eval (args) with open ('haproxy. cfg ', 'a +', encoding = 'utf-8') as f: # print ('', file = f) print ('backend % s' % B ['backend'], file = f, flush = True) # Use two prints to set backend, print ('\ t \ tserver % s weight % s maxconn % s' % (B ['reco Rd '] ['server'], B ['record'] ['weight'], B ['record '] ['maxconn']), file = f, flush = True) def delete_backend (): # The deletion method is the opposite of the query method. search appends the result to the list, delete is to exclude the objects to be deleted with open ('haproxy. cfg ', 'R') as f_ori, open ('haproxy _ new. cfg ', 'a +') as f_new: delete_input = input ("please enter backend which you want to delete:") backend_flag = True for line in f_ori: if line. strip () = 'backend % s' % delete_input: backend_flag = Fals E continue if line. strip (). startswith ('backend') and line. strip ()! = 'Backend % s' % delete_input: backend_flag = True if backend_flag: # All rows except deletion are written to a new file f_new.write (line) Mesg = ''' 1. search backend2. Add backend3. delete backend4. quit ''' while True: print (Mesg) user_input = input ("please input which do you want:") if user_input = "1 ": search_backend () elif user_input = "2": add_backend () elif user_input = "3": delete_backend () elif user_input = '4': exit () else: exit (print ("Invaild input, exit "))View Code
# Result 1. search backend2. Add backend3. delete backend4. quitplease input which do you want: 1 please input the backend which you want to searchwww. baidu. comserver 10.22.23.45 weight 20 maxconn 300001. search backend2. Add backend3. delete backend4. quitplease input which do you want: 2 backend: www. google. cnserver: 10.88.123.40weight: 20 maxconn: 34001. search backend2. Add backend3. delete backend4. quitplease input which do you want: 4
View Code
Define three functions
Search_backend ()
Add_backend ()
Delete_backend ()
For add_backend () and delete_backend () functions, the flag is used (JSON is not used here)
1. for the following text information, I want to delete the information corresponding to www.baidu.com and read the information of the two rows using the for loop and flag bit conversion.
Backend www.baidu.com server Upload weight 20 maxconn 30000 backend upload server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000 server 100.1.7.93 100.1.7.93 weight 20 maxconn 300 search_list = [] with open. cfg ') as f: backend_flag = False # initialization flag False for line in f: if line. strip () = 'backend www.baidu.com ": # When the backend information to be queried is matched, the flag is True. When this loop is exceeded, backend_flag = True
Search_list.append (line. strip ("\ n") continue if backend_flag and line. strip (). startswith ('backend'): # in the previous if statement, the flag is set to True. if it starts with 'backend' again, it indicates that the next backend information is already break if backend_flag and line. strip (): # append the rows except the last two if conditions to the search_list list, that is, the row search_list.append (line. strip ())
2. search_backend () uses the recursive call of the Function
If len (search_list) = 0: # No query result print ("\ 033 [31; 1 m % s not exit, please re_input \ 033 [0 m" % search_input) is found) search_backend ()
3. eval ()
Eval usage: Eval (expression, [globals = None, [locals = None]
Functions: Calculates the value of expression. expression can only be a single operation expression.
- Expression: Required parameter. eval () arg 1 must be a string, bytes or code object. If expression is a string, it will be analyzed and interpreted as a python expression, exactly what is used in this Code
>>> a = '''{... 'backend':'www.baidu.com',... 'record':{... 'weight':'20',... 'maxconn':'3400'... }... }'''>>> a"{\n\t 'backend':'www.baidu.com',\n\t 'record':{\n\t\t\t'weight':'20',\n\t\t\t 'maxconn':'3400'\n\t\t }\n }">>> eval(a){'record': {'weight': '20', 'maxconn': '3400'}, 'backend': 'www.baidu.com'}>>>View Code
- Globals: optional parameter, which indicates the global namespace. If yes, it must be a dictionary object.
- Locals: optional parameter, indicating the current local namespace. It can be any ing object. If omitted, the same value as globals is obtained.
- If globals and locals are omitted, eval () takes the namespace value in the called Environment
Return Value:
If expression is a code object and the mode parameter of the complie function is "exec" when the code object is created, the eval () function returns None.
If expression is an output statement, such as print (), eval () returns None;
Otherwise, the result of the expression is the return value of the eval () function;
For instance:
>>> x=10>>> y=20>>> eval('x+y')30>>> eval('x+y',{'x':1,'y':2})3>>> eval('x+y',{'x':1,'y':2},{'y':3,'z':4})4>>> eval('print(x,y)')10 20>>> print(eval('print(x,y)'))10 20None