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
实现代码
LearnPython_week3的更多相关文章
随机推荐
- 使用Mybatis的TypeHandler加解密数据
使用Mybatis的TypeHandler加解密数据 一.背景 二.解决方案 三.需求 四.实现思路 1.编写一个实体类,凡是此实体类的数据都表示需要加解密的 2.编写一个加解密的`TypeHandl ...
- 基于docker-compose搭建sonarqube代码质量检测平台
一.需求 在我们开发的过程中,难免有时候代码写的不规范,或存在一些静态的bug问题,这个时候一个良好的代码检查工具就很有必要,而sonarqube正好可以满足整个要求. 二. docker-compo ...
- IOC和DI之刨根问底之第一节
很多freshman上来就想搞清楚什么是IOC和DI,其实很多先进的理论和技术都在老的基础上升华出来的,最终目的是为了解放生产力. 所以先来说说下面两点基础知识: Direct Dependency( ...
- 使用 ASP.NET Core 3.1 的微服务开发指南
使用 ASP.NET Core 3.1 的微服务 – 终极详细指南 https://procodeguide.com/programming/microservices-asp-net-core/ A ...
- Python pip 和pip3区别 联系
python 有python2和python3的区别 那么pip也有pip和pip3的区别 大概是这样的 pip是python的包管理工具,pip和pip3版本不同,都位于Scripts\目录下: 如 ...
- hdu 2571 命运(水DP)
题意: M*N的grid,每个格上有一个整数. 小明从左上角(1,1)打算走到右下角(M,N). 每次可以向下走一格,或向右走一格,或向右走到当前所在列的倍数的列的位置上.即:若当前位置是(i,j), ...
- jQuery中onload与ready区别
onload和ready的区别document.ready和onload的区别为:加载程度不同.执行次数不同.执行速度不同.1.加载程度不同 document.ready:是DOM结构绘制完毕后就执行 ...
- vue.js+elementUI文件上传、文件导入、文件下载
1.文件下载 <el-button plain @click ="exportVmExcel()" size='mini' icon="el-icon-downlo ...
- oracle 账号解锁 java.sql.SQLException: ORA-28000: the account is locked
日志报错:ORA-28000: the account is locked 1.plsql登录提示用户被锁定 2.sys登录sqlplus登录查看 SQL> select username,ac ...
- 目录扫描工具 dirsearch 使用详解
介绍 dirsearch 是一个python开发的目录扫描工具.和我们平时使用的dirb.御剑之类的工具一样,就是为了扫描网站的敏感文件和目录从而找到突破口. 特点 多线程 可保持连接 支持多种后缀( ...