LearnPython_week3

函数说明

  1 # -*- coding:utf-8 -*-
  2 # Author:Wong Du
  3 
  4 
  5 ###函数,
  6 # 能避免代码重复,
  7 # 方便代码修改等操作
  8 def wong():
  9     print('I am wong..')
 10 wong()
 11 #输出:I am wong..
 12 
 13 ###形参和实参
 14 def calc(x,y):      #x,y是形式参数,即形参
 15     print('计算结果:',x**y)       #计算x的y次方
 16 calc(2,5)           #2,5是实际参数,即实参
 17 #输出:计算结果: 32
 18 
 19 ###默认参数,
 20 # 默认参数可以理解成为形参赋一个实际值,
 21 # 即当函数调用没有为默认参数赋值时,默认参数使用默认值
 22 def calc(x,y=2):      #x,y是形式参数,即形参;2是y的默认值
 23     print('计算结果:',x**y)       #计算x的y次方
 24 calc(2)           #默认参数可不予赋值,此时y=2,所以输出结果为4
 25 calc(2,5)         #可为默认参数赋值,此时y=5,所以输出结果为32
 26 
 27 ###位置参数和关键字参数,
 28 # 位置参数遵循函数形参的先后顺序,
 29 # 关键字参数则相当于变量赋值,
 30 # 函数调用时,会先匹配位置参数,再匹配关键字参数,
 31 # 所以同时使用位置参数和关键字参数赋值时,位置参数要在关键字参数的前面
 32 def calc(x,y=2):
 33     print('计算结果:',x**y)     #计算x的y次方
 34 calc(2,3)   #位置参数方式赋值,遵循顺序
 35 calc(y=2,x=5)   #关键词参数方式赋值,可不遵循顺序
 36 calc(2,y=5)     #混合方式赋值,位置参数要在关键字参数前面
 37 #calc(x=2,5)     #会报错
 38 #calc(2,x=5)     #参数赋值两次,会报错
 39 #输出:
 40 '''
 41 计算结果: 8
 42 计算结果: 25
 43 计算结果: 32
 44 '''
 45 
 46 ###返回值
 47 def calc(x,y=2):
 48     z = x**y     #计算x的y次方
 49     return z
 50 calc(2,2)
 51 print(calc(2,2))
 52 #输出:4
 53 
 54 ###不确定参数*args、**kwargs,
 55 # 使用不确定参数可以为函数赋无数个值,
 56 # 匹配*args的值会成为一个元组,
 57 # 匹配到**kwargs的值会成为一个字典,
 58 # 函数首次赋值是按顺序匹配的,所以如果有确定参数,位置应放在*args前
 59 def calc(x,*args,**kwargs):     #参数x应放在*args前面
 60     print(x,args,kwargs)
 61 
 62 calc(2,4,5,6,7,8,name='wong',age='23')
 63 #输出:2 (4, 5, 6, 7, 8) {'name': 'wong', 'age': '23'}
 64 
 65 ###全局变量和局部变量,
 66 # 全局变量在整个程序内有效,
 67 # 局部变量只在函数或其他子程序内有效
 68 name = 'wong'     #全局变量
 69 def edit():
 70     name = 'I am wong, my age is 23..'    #局部变量
 71     return name
 72 print(name)   #全局变量,没有改变
 73 print(edit()) #局部变量,做了修改
 74 #输出:
 75 '''
 76 wong
 77 I am wong, my age is 23..
 78 '''
 79 
 80 ###嵌套函数,
 81 # 嵌套函数和多层循环类似
 82 def wong():
 83     def wong2():
 84         def wong3():
 85             name = 'wong3'
 86             print(name)
 87         wong3()
 88         name = 'wong2'
 89         print(name)
 90     wong2()
 91     name = 'wong1'
 92     print(name)
 93 
 94 wong()
 95 #输出:
 96 '''
 97 wong3
 98 wong2
 99 wong1
100 '''
101 
102 ###递归,
103 # 在函数内部调用函数自身,叫做递归函数,
104 # 为防止内存栈溢出,python3默认只允许递归999次吧
105 def lef(count=0):
106     print('on the way..',count)
107     count += 1
108     return lef(count)
109 
110 lef()
111 ###递归函数应用:二分查找
112 list = [1,3,4,6,7,9,11,14,17,19,22,25,28,33,34,36,38,47,55]
113 def func(list,find_num):
114     mid = int(len(list)/2)
115     if mid > 0:
116         if find_num == list[mid]:
117             print("找到数字",list[mid])
118         elif find_num < list[mid]:
119             print("数字%s在列表的左边,找到%s位置" %(find_num,list[mid]))
120             return func(list[0:mid],find_num)
121         else:
122             print("数字%s在列表的右边,找到%s位置" %(find_num,list[mid]))
123             return func(list[mid:],find_num)
124     else:
125         if list[mid] == find_num:
126             print("找到数字了:",list[mid])
127         else:
128             print("没得分了,你要找的数字%s不在列表里..." %find_num)
129 
130 func(list,1)
131 
132 ###高阶函数,
133 # 1 把一个函数名当做实参传给另一个函数,此函数称之为高阶函数,
134 # 2 return 返回值中包含函数名,也可以称之为高阶函数
135 import time
136 def source():
137     time.sleep(3)
138     print('I am source code..')
139 def bar(*args):
140     print(args)
141     start_time = time.time()
142     for i in args:
143         i()
144     stop_time = time.time()
145     print("The func run time is %s"%(stop_time-start_time))
146 #source()
147 bar(source)
148 
149 # import time
150 # def source():
151 #     time.sleep(3)
152 #     print('I am source code..')
153 # def bar(func):
154 #     start_time = time.time()
155 #     func()
156 #     stop_time = time.time()
157 #     print("The func run time is %s"%(stop_time-start_time))
158 #     return func
159 # #print(bar(source))
160 # source = bar(source)
161 # source()
162 
163 ###匿名函数
164 def test1(x,y):
165     print(x*y)
166 
167 test2 = lambda x,y:print(x*y)
168 
169 test1(2,3)
170 test2(2,4)
171 #test1和test2效果一样

小程序

修改haproxy配置文件 

global
        log 127.0.0.1 local2
        daemon
        maxconn 256
        log 127.0.0.1 local2 info
defaults
        log global
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
        option  dontlognull

listen stats :8888
        stats enable
        stats uri       /admin
        stats auth      admin:1234

frontend oldboy.org
        bind 0.0.0.0:80
        option httplog
        option httpclose
        option  forwardfor
        log global
        acl www hdr_reg(host) -i www.oldboy.org
        use_backend www.oldboy.org if www

backend www.jumpserver.org
        server 100.1.7.8 weight 20 maxconn 3000
        server 100.1.7.9 weight 20 maxconn 3000

backend www.caiyundo.cn
        server 100.1.7.9 weight 32 maxconn 64
haproxy配置文件
 1 # -*- coding:utf-8 -*-
 2 # Author:Wong Du
 3 arg = {
 4     'backend':'',
 5     'record':{
 6         'server':'',
 7         'weight':'',
 8         'maxconn':''
 9     }
10 }
11 def look():
12     with open('haproxy','r+',encoding='utf-8') as f:
13         backend = input("您要查找的记录:")
14         for line in f:
15             if ['backend',backend] == line.strip().split():
16                 print(line.strip())
17                 print(f.readline())
18 def add():
19     with open('haproxy','a+',encoding='utf-8') as f:
20         backend = input("新增的域名:")
21         arg['backend'] = backend
22         server = input("新增的serverip:")
23         arg['record']['server'] = server
24         weight = input("宽度:")
25         arg['record']['weight'] = weight
26         maxconn = input("最大conn:")
27         arg['record']['maxconn'] = maxconn
28         f.write('\nbackend '+ arg['backend'])
29         f.write('\n\t\tserver %s weight %s maxconn %s \n'% (arg['record']['server'],
30                                 arg['record']['weight'],
31                                 arg['record']['maxconn']))
32 def delete():
33     data = ''
34     with open('haproxy','r',encoding='utf-8') as f:
35         delwww = input("要删除的域名记录:")
36         for line in f:
37             if ['backend',delwww] == line.strip().split():
38                 f.readline()
39             else:
40                 data += line
41     with open('haproxy','w',encoding='utf-8') as f:
42         # for line2 in data:
43             f.write(data)
44 
45 
46 
47 while True:
48     select = input("What are you doing ('l'or'a'or'd')>>")
49     if select == 'l':
50         look()
51     elif select == 'a':
52         add()
53     elif select == 'd':
54         delete()
55     elif select == 'q':
56         break
实现代码

 

posted @ 2018-02-27 16:28  糕事情  阅读(244)  评论(0编辑  收藏  举报