Python【内置函数】、【装饰器】与【haproxyf配置文件的修改】

内置函数

•callable,检查是否能被执行/调用

def f1():
    pass
f2 = 123
print(callable(f1)) #输出
print(callable(f2)) #输出
True #f1可以被调用,返回值为True
False #f2不可以不调用,返回执为False

•chr()与ord()

chr() #将数字蒋欢为响应的ascll码
chr('66') #转换为大写B
B 
ord() #将ascll转换为数字
ord('B')
66 #转换为数字

•random,给它一个范围随机生成数

import random #导入random
li = []
for i in range(5): #循环5次
    tem = random.randrange(0,10) #随机生成0-9中任意数字
    li.append(str(temp))
print(''.join(li)) #调用列表的join方法输出列表中的元素,注意元素必须为字符串,将列表中的每个元素拼接起来
xxxxx #随机生成5个数字
import random
li = []
for i in range(6):
    tem = random.randrange(0,2) #随机生成0和1两个数字
    if tem == 1:
        temp = random.randrange(0,10) #如果生成数字1则添加数字到列表
        li.append(str(temp))
    else:
        temp = random.randrange(65,91) #如果生成数字0则添加字母到列表
        li.append(chr(temp))
print(''.join(li))
xxxxxx
生成六位数的验证码

•compile(),把字符串编译成python代码,不执行

补充理论知识:当我们代开文件的时候显示给我们的是字符串,当我们去执行的时候,python解释器会先将字符串加载到内存串,然后编译成python的字节码,最后执行代码

s = 'qwer'
print(compile(s,'<string>','exec')) #将字符串编译成python代码;
如果不是'<string>',就需要写一个文件名,然后就会自动打开这个文件进行编译
如果是single会编译成单行的python程序,eval会编译成表达式,exec编译成和python代码一模一样的东西
<code object <module> at 0x000000269DB9FAE0, file "<string>", line 1> #输出

•eval(),把字符串转换为python表达式去执行,执行并返回结果,专门做表达式的运算,并获取结果

s = '1+1'
b = eval(s) #只有eval可以执行表达式并且返回表按当时的值
print(b)
2

•exec(),执行python代码、字符串都可以,自己可以将字符串编译成python的代码然后执行,比eval高级,但是没有返回值

s = 'print(123)'
b = compile(s,'<string>','exec')
exec(s) #既可以执行字符串
exec(b) #又可以执行python代码

•dir()快速查看一个对象提供的哪些功能

•help()会显示详细的功能

•divmod()

res = divmod(10,3) #计算两个数相除
print(res)
(3, 1) #将商和余数放在一个元组中
n1,n2 = divmod(10,3)
n1 = 3 #商
n2 = 1 #余数

•isinstance,判断对象是否是某个类的实例

a = 'alex'
print(isinstance(a,str))
True #返回值为True或者False

•filter(函数,可迭代的对象)

def f1(i):
    if i > 3:
        return True
li = [1,2,3,4,5]
res = filter(f1,li) #遍历列表li,将li中每个元素放进f1,如果返回结果为真则合法,会将这个合法元素添加到res中
print(list(res))
[4,5]
li = [1,2,3,4]
print(list(filter(lambda a: a > 2,li))) #利用lambda表达式
[3,4]

•map(函数,可迭代的对象),对一个数据进行一个批量操作可用map函数

li = [1, 2, 3, 4]
def f1(a):
    return a + 1
res = map(f1,li) #对列表li所有元素做一个同意的操作
print(list(res))
[2, 3, 4, 5] #输出
li = [1, 2, 3, 4]
res = map(lambda a: a+1, li) #利用lambda表达式
print(list(res))
[2, 3, 4, 5]

•globals(),所有的全局变量,locals(),所有的局部变量,

NAME = 'alex'
def f1():
    a = 123
    print(globals()) 
    print(locals())
f1()
{'__spec__': None, 'NAME': 'alex', '__file__': 'C:/Users/Administrator/PycharmProjects/s13/day4/class/practice.py', 'f1': <function f1 at 0x0000007B739E80D0>, '__cached__': None, '__name__': '__main__', '__doc__': None, '__package__': None, '__builtins__': <module 'builtins' (built-in)>, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000007B73475B00>} #全局变量
{'a': 123} #局部变量

•hash(),哈希值,转换出的哈希值是不变的,一般用于字典的key保存

s = 'aelx'
print(hash(s))
7442245827626114712 #输出

•hex(),将十进制转换十六进制,bin(),十进制转换二进制,oct(),十进制转换八进制,int(),十进制转换十进制

a = 10
print(hex(a))
0xa #输出

•pow(),指数函数

pow(2,3)
8 #输出

•id(),查看内存地址

a = 10
print(id(a))
1687285840 #输出内存地址

•len(),计算长度,2.0按照字节输出长度,3.0既可以按照字符输出长度也可以按照字节输出长度,默认是字符长度

s = '李杰'
b = bytes(s,encoding='utf-8') #将字符转换为字节
print(len(s))
print(len(b))
2 #输出,按照字符找
6 #输出,按照字节

•max()最大值,min()最小值

print(max(1,2,3,4)) #找出最大值
print(min(1,2,3,4)) #找出最小值
4 #输出
1 #输出

•sum(),求和,注意:sum(必须为一个列表或者元组)

print(sum((1,2)))
print(sum([1,2]))
3 #输出
3 #输出

•memoryview,和内存地址相关的一个类

•round(),四舍五入,注意:4.5四舍五入为4

print(round(4.4))
print(round(4.6))
4 #输出
5 #输出

•reversed(),反转,和列表中的reverse一样

li = [1,2,3,4]
print(list(reversed(li)))
[4, 3, 2, 1] #输出

•sorted(),排序

s = [4,5,2,1,7]
print(sorted(s))
[1, 2, 4, 5, 7] #输出

•zip(),合并元素,索引一个存在一个不存在合并后则不存在

l1 = [1,2,3,4]
l2 = [2,3,4]
l3 = zip(l1,l2)
print(list(l3))
[(1, 2), (2, 3), (3, 4)] #输出元素类型为元组的列表

装饰器

装饰器是在函数执行前或者函数执行后额外的执行某些代码而不改变原函数的内部结构,位置是在函数外部装饰,在原函数前或者后操作,不影响原函数

•不带返回值的装饰器

def outer(arg): # 定义一个名为outer的装饰器函数
    def inner(): # 装饰器的内层函数
        print('before')
        arg()
    return inner # 返回一个函数
@outer  #在函数外部添加装饰器,自动执行outer函数并将函数名f1当做参数传递到outer中,outer函数的返回值重新付给f1
def f1():
    print('F1')
f1() # 输出,在F1前面输出了before
before
F1

•带返回值的装饰器

def outer(arg):
    def inner():
        print('before')
        return arg() #return回的返回值为None,因为f1函数没有返回值
    return inner
@outer
def f1():
    print('F1')
res = f1()
print(res) #返回值
before
F1
None

•万能装饰器

def outer(func):
    def inner(*args,**kwargs): #设置万能参数,多少个参数都可以接收
        print('before')
        res = func(*args,**kwargs)
        print('after')
        return res # 设置函数的返回值
    return inner
@outer
def f1(name,age):
    print(name,age)
    return '我是返回值'
rest = f1('alex',31)
print(rest) #输出
before
alex 31
after
我是返回值

•简单升级版的装饰器

LOGIN_USER = {'is_login': False}
def outer(func):
    def inner(*args, **kwargs):
        if LOGIN_USER['is_login']:
            print('before')
            res = func(*args, **kwargs)
            print('after')
            return res
        else:
            print('请先登入')
    return inner
@outer
def order():
    print('欢迎%s登录' % LOGIN_USER['current'])
    return '我是返回值'
while True:
    user_input = input('1、manager 2、登入:')
    if user_input == '2':
        user_name = input('name:')
        user_pass = input('pass:')
        if user_name == 'alex' and user_pass == '123':
            LOGIN_USER['current'] = 'alex'
            LOGIN_USER['is_login'] = True
            rest = order()
            print(rest)
    if user_input == '1':
        order()
# 用户必须先登入才可以进行管理,如果不登入就要管理,程序会自动返回并提示用户先登入

haproxy配置文件的修改

•haproxy原配置文件,注意:代码中ha.conf为原配置文件,new.conf为新配置文件

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 ww.oldboy.org
        server 100.1.6.9 100.1.7.9 weight 20 maxconn 3000
        server 100.1.6.8 100.1.6.8 weight 20 maxconn 3000

backend buy.oldboy.org
        server 100.1.7.9 100.1.7.90 weight 20 maxconn 5000
View Code

•获取haproxy配置文件中backend的信息

# 获取ha信息
def fetch(backend):
    with open('ha.conf','r',encoding='utf-8') as f1:
        flag = False #设置标志位
        li = []
        for line in f1:
            if line.strip().startswith('backend') and line.strip() == 'backend' + ' ' +backend: #如果以backend开头并且匹配输入的域名
                flag = True  #匹配成功修改标志位
                continue #匹配到结束本次循环开始下次循环
            if line.strip().startswith('backend') and flag:
                break
            if flag and line.strip(): #添加列表中
                li.append(line.strip() + '\n')
    return li
res = fetch('ww.oldboy.org')
print(''.join(res))
server 100.1.6.9 100.1.7.9 weight 20 maxconn 3000 #输出
server 100.1.6.8 100.1.6.8 weight 20 maxconn 3000 
View Code

 •增加haproxy配置文件中backend的信息

# 获取ha信息
def fetch(backend):
    with open('ha.conf', 'r', encoding='utf-8') as f1:
        flag = False
        li = []
        for line in f1:
            if line.strip().startswith('backend') and line.strip() == 'backend' + ' ' + backend:
                flag = True
                continue
            if line.strip().startswith('backend') and flag:
                break
            if flag and line.strip():
                li.append(line.strip() + '\n')
    return li
# 先实现简单的逻辑再去实现复杂的逻辑
# 添加ha信息
def add(backend,record):
    res = fetch(backend)
    if not res:  # 如果backend不存在则添加
        with open('ha.conf', 'r', encoding='utf-8') as old_file,open('new.conf', 'w', encoding='utf-8') as new_file:
            for oldline in old_file:
                new_file.write(oldline)  # 将旧文件写入新文件
            new_file.write('\nbackend ' + backend + '\n')  # 往新文件中添加backend记录
            new_file.write(' '*8 + record + '\n')
    else:  # backend存在
        if record in res:  # 如果record存在则不添加
            with open('ha.conf', 'r', encoding='utf-8') as old_file,open('new.conf', 'w', encoding='utf-8') as new_file:
                for oldline in old_file:
                    new_file.write(oldline)  # 将旧文件写入新文件
        else:  # 如果record不存在则添加
            res.append(record + '\n')
            with open('ha.conf', 'r', encoding='utf-8') as old_file,open('new.conf', 'w', encoding='utf-8') as new_file:
                flag = False
                for line in old_file:
                    if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
                        flag = True
                        new_file.write(line)
                        for i in res:
                            new_file.write(' '*8 + i)  # 一口气把添加的那部分写完
                        continue
                    if flag and line.strip().startswith('backend'):  # 以backend开头部分剩余的行写入
                        flag = False
                        new_file.write(line)
                        continue
                    if line.strip() and not flag:  # 不符合条件的所有行自行写入
                        new_file.write(line)
backend = 'ww.oldboy.org'
record = 'server 100.1.6.8 100.1.6.8 weight 20 maxconn 1111000'
add(backend,record)
View Code

 

posted @ 2016-05-30 16:37  中国陆特  阅读(239)  评论(0编辑  收藏  举报