python Day4

#冒泡算法

ll = [22,13,11,99,6,33,67,4]

p = len(ll)-1

print p

while True:

    for i in xrange(p):

        n1 = ll[i]

        n2 = ll[i+1]

        if n1 > n2:

            tmpn = n1

            ll[i] = n2

            ll[i+1] = tmpn

    p -= 1

#    print ll

    if p == 0:

        print ll

        break

 

 

#上期作业

#取

def fetch(backend):

    with open('ha.conf','r') as obj:

        fetch_list = []

        flag = False

        for line in obj:

            if line.strip() == "backend %s" % backend:

                flag =True

                print line.strip()

                continue

            if flag and line.strip().startswith("backend"):

                print line.strip()

                print line.strip().startswith("backend")

                break

            if flag and line.strip():

                fetch_list.append(line.strip())

#        print fetch_list

                   return fetch_list

 

#fetch("www.oldboy.org")

 

#增

def addi(dic_info):

#    print dic_info

    backend_titel = dic_info.get("backend")

#    print backend_titel

    server_titel = dic_info.get("record")

#    print server_titel

#    print server_titel.get("server")

    current_titel = "backend %s" % backend_titel

#    print current_titel

    crrent_record = "server %s %s weight %s maxconn %s" % (server_titel.get("server"),server_titel.get("server"),server_titel.get("weight"),server_titel.get("maxconn"))

#    print crrent_record

 

    fetch_list = fetch(backend_titel)

    #print 'fetch',fetch_list

    if fetch_list:

        #存在backend

#        print fetch_list

        if crrent_record in fetch_list:

            pass

        else:

            fetch_list.append(crrent_record)

        #print fetch_list

        with open('ha.conf') as read_obj,open('ha.new','w') as write_obj:

            flag = False

            has_write = False

            for line in read_obj:

                if line.strip() == current_titel:

#                    print line

                    write_obj.write(line)

                    flag = True

                    continue

                if flag and line.strip().startswith("backend"):

                    flag = False

                if flag:

#                    print fetch_list

                    if not has_write:

                        for new_line in fetch_list:

                            temptype = "%s %s\n" % (" "*8,new_line)

                            write_obj.write(temptype)

                        has_write = True

                else:

#                    print line

                    write_obj.write(line)

 

    else:

 

        with open('ha.conf') as read_obj,open('ha.new','w') as write_obj:

            for line in read_obj:

                write_obj.write(line)

            write_obj.write(current_titel)

            temptype = "%s %s\n" % (" "*8,crrent_record)

            write_obj.write(temptype)

 

    os.rename('ha.conf','ha.bak')

    os.rename('ha.new','ha.conf')

 

s = '{"backend":"www.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":"30"}}'

 

data_dic = json.loads(s)

#print data_dic

addi(data_dic)

 

 

 

=======================================================================================

lambda 简化的函数定义

#处理简单逻辑  自动return

func = lambda arg: arg + 1

func(10)

 

等同于

def func(arg):

         return arg + 1

func(10)

 

lambda a,b,**kwargs: #可以多个参数

 

 

###map  遍历

def func(a1,a2):

         return a1 + a2

 

l1 = [12,43,76]

l2 = [1,2,3]

new_list = map(func,l1)

print new_list

 

new_list = map(lambda a1,a2:a1+a2,l1,l2)

print new_list

 

 

###filter  过滤 筛选  默认取bool为True的数据

li = [11,22,33,44,55,0]

filter(None,li)

print filter(lambda a:a>33,li)

 

 

###reduce                 对序列内所有元素进行累计操作

print reduce(lambda a1,a2:a1+a2,li,1000)   #1000为基数  以此为基础进行累计操作

 

 

###yield  

#记住上一次的操作,下次在执行时,继续执行

def func():

    yield 1

    yield 2

    yield 3

 

for i in func():

    print i

        

###装饰器

def wrapper(func):
    if login("alex"):
        return func
    else:
        return

def
login(user):
    if user == "alex":
        return True
    else:
        print "no user"
@wrapper
def home():
    print "this is home"

home()

posted @ 2015-11-20 15:36  plzros  阅读(132)  评论(0编辑  收藏  举报