1 A lot of people's efforts are a scratch.
2. A lot of people are still trying.
3. Half a year after the turn
Bubbling algorithm:
For-N in range (1,len (LI)): for m in range (Len (LI)-n): num1 = li[m] num2 = li[m+1]if num1 > num2: Temp = Li[m]li[m] = num2li[m+1] = tempprint Li
Ha Configuration Ideas
#!/usr/bin/env python#-*-coding:utf-8-*-# powered by xiaobing# Check, add, delete # define read function Import Os,jsondef Fetch (BACKEND): #打开文件ha With open (' ha ') as Obj:flag = False fetch_list = [] #按行读取文件, read only one line at a time in obj: #建个空列表, for subsequent storage of the value under backend with #如果这行内容等于 backend parameter value, set a flage value of true, think found the code block to find, and then jump out of this loop, read the next line of code block If Line.strip () = = "Backend%s"% Backend:flag = True Continue #如果这个flag = T Rue Description The code block was found, and then added the line to the empty list fetch_list if flag and Line.strip (): Fetch_list.append (Line.strip ()) #如果这个flag = True to indicate that a code block has been found and the code that begins with backend is encountered, then the following is irrelevant and does not need to continue looping if Line.strip (). StartsWith ("backend") and Flag:break return fetch_listdef Add_ha (dict_info): #获取要添加的 backend Value backend_title = Dict _info.get ("backend") Current_title = "Backend%s"% backend_title #要添加的backend server record server_record_dict = dic T_info.get ("Record")Current_record = "Server%s%s weight%s maxconn%s"% (Server_record_dict.get ("Server"), Server_record_dict.get ("Server" ), Server_record_dict.get ("Weight"), Server_record_dict.get ("Maxconn")) #获取当前这个backend记录下的server列表 serverlist = Fetch (Backend_title) #判断这个新增backend whether there is an if serverlist: #增加server记录 #打开旧文件 and opens a new file with the open (' Ha ') as Read_obj,open (' Ha_new ', W) as write_obj: #设置是否已经读取的标识 flag = False #设置是否写入新记录的标识 Has_write_flage = False #读取文件, read for lines in Read_obj by line: #如果读取行到了要写入的backend行 , set read identity as Ture, and jump out of this loop, read the next line if line.strip () = = Current_title:flag = True Continue #如果已读取到写入行 and reads a new backend record, set the IF flag and Line.strip (). StartsWith ("backend" ): Flag = False If flag:if not has_write_flage: For New_line in ServerList: Write_obj.write ("%s%s \ n"% (" "*8,new_line)" Else:has_write_flage=true Else: Write_obj.write (line) Else: #增加backend并添加记录 #打开文件写入新记录 while opening old and new files with open (' ha ') as Read_obj,o Pen (' ha_new ', ' W ') as Write_obj:for line in Read_obj:write_obj.write (line) write_obj . write ("\ n") write_obj.write (current_title+ "\ n") Write_obj.write ("%s%s \ n"% (" *8,current_record) #os. Rename (' Ha ', ' Ha.bak ') #os. Rename (' Ha_new ', ' ha ') s = ' {' backend ': ' test.oldboy.org ', "Record": {"Server": "100.1.7.9", "Weight": $, "maxconn": +} ' read_input=json.loads (s) add_ha (read_input) Fetch (" Www.oldboy.org ")
Lambda simplification functions
When learning conditional operations, for simple if else statements, you can use a ternary operation to represent
#处理简单函数
#自动return
#!/usr/bin/env python#-*-coding:utf-8-*-# powered by xiaobingdef func1 (ARG): Return Arg+1result = func1 (+) print Resultfunc2 = Lambda A:a+1result = FUNC2 (+) Print result
Map function
L1=[1,2,3]l2=[3,4,5]l3=[4,5,6]print map (Lambda a1,a2,a3:a1+a2+a3,l1,l2,l3)
Filter () Filters only data that meets the criteria
L4 = [1,44,55,22,11,33]print filter (lambda a:a>33,l4)
Reduce () cumulative operation for all elements in a sequence
L5 = [1,2,3,4,5,6,7,8,9,10]print reduce (lambda a1,a2:a1+a2,l5)
Map all element operations
Filter filters to get the specified collection of elements
Reduce, cumulative operation
Yield
Remember the last action, and the next time you execute it, continue
#!/usr/bin/env python#-*-coding:utf-8-*-# powered by xiaobing "Def func1 (): yield 1 yield 2 yield 3for i in Func1 (): print i "Def mrange (ARG): seed = 0 while True: seed = seed +1 If seed >10:
return Else: yield seedfor i in Mrange (Ten): print I
Adorner:
Adorners are functions, except that the function can have special meanings, adorners are used to decorate functions or classes, and adorners can be used to add actions before and after the function executes.
Scenario: If a Python program already has 500 functions, but suddenly the boss asks us to increase the login verification, only through the verification of the ability to execute, and do not change the way the user calls.
#!/usr/bin/env python#-*-coding:utf-8-*-# powered by Xiaobingdef Wrapper (func): If Login ("Xiaobing"): Return Funcelse:print ' unauthenticated ' def login (user): if user = = ' xiaobing ': return trueelse:print ' Error name ' @wrapperdef home ():p rint ' ho Me ' @wrapperdef shop ():p rint "Shop" Def over ():p rint ' over ' #home = wrapper (Home) Home () Shop ()
The path of Python Day4