zoukankan      html  css  js  c++  java
  • 2018/12/17(python)

    查询功能

    #文件名:查询功能
    backend 123456.org
            server 2.2.2.2 weight 20 maxconn 3000
            server 3.2.2.2 weight 20 maxconn 3000
            server 4.2.2.2 weight 20 maxconn 3000
            server 5.2.2.2 weight 20 maxconn 3000
            server 6.2.2.2 weight 20 maxconn 3000
    backend 123
            1
            2
            3
            4
            5
    #encoding:utf-8
    def fetch(data):
        print('查询')
        print('数据',data)
        backend_data = 'backend %s' % data
        with open('查询功能', 'r') as read_f:
            tag = False  # 判断标志
            ret=[]
            for read_line in read_f:
                if read_line.strip() == backend_data:  # read_line.strip()去掉空格回车
                    tag = True  # 找到了目标,准备打印下面内容
                    continue
                if tag:
                    if read_line.startswith('backend'):
                        break
                if tag:
                    print(read_line, end='')
                    ret.append(read_line)
        return ret
    
    
    def add():
        pass
    
    def change():
        pass
    
    def delete():
        pass
    
    if __name__ == "__main__":      #执行代码放在他下面,程序入口
        msg='''     
        1;查询
        2;添加
        3;修改
        4;删除
        5;退出
        '''     #字符串
        msg_dic={
            '1':fetch,
            '2':add,
            '3':change,
            '4':delete,
        }
        while True:
            print(msg)
            choice=input('请输入选项:').strip()
            if not choice:continue
            if choice=='5':break
            data=input('请输入数据:').strip()
            res=msg_dic[choice](data)
            print(res)

    修改文件名

    # encoding:utf-8
    import os
    os.rename('实验文件.txt','新实验文件')      #前面是旧的,后面是新的

    程序解藕

    模块一的实现依赖于模块二,更改模块二后,模块一也得更改,那么二者就有耦合。修改程序,使得更改模块二后,模块一不受影响,那么就叫解藕

    module模块与包

    .py文件就是模块

    模块分为3类

    1.python标准库

    2.第三方模块

    3.自定义模块

     

    #文件名test1
    import test2            #引用test2
    print(test2.add(3,4))
    print(test2.sub(7,5))
    
    
    #文件名test2
    def add(a,b):
        return a+b
    def sub(a,b):
        return a-b            

    法2,不用引用

    from test2 import add
    from test2 import sub
    print(add(3,4))
    print(sub(7,5))

    法3,*代表所有(不推荐)

    from test2 import *
    print(add(3,4))
    print(sub(7,5))

    使用import,会把引用文件执行一遍

    from (引用文件上一层目录路径) import (引用文件) 

    包——包含不同模块的文件夹。用途——组织模块

    引用包下面的包    from 第一层 . 第二层 import 文件

    __name__  在本文件中是__main__,其他文件调用时是调用文件的路径

    python内置模块

    time模块

    #encoding:utf-8
    import  time
    
    print(time.time())              #时间戳,从1970年到现在,单位秒
    
    
    print(time.localtime())         #本地时间
    
    
    t=time.localtime()               #获取年月日时分秒
    print(t.tm_year)
    print(t.tm_hour)
    
    
    print(time.gmtime())             #世界标准时间
    
    
    #将结构化时间转化为字符串时间
    print(time.strftime('%Y-%m-%d %X',time.localtime()))
    
    
    #将字符串时间转化为结构化时间
    print(time.strptime('2016:12:24:17:50:36','%Y:%m:%d:%X'))
    
    #直接看
    print(time.asctime())
    print(time.ctime())
    
    #延时
    time.sleep(5)
    print('OK')
    
    #clock表时间差
    
    #另一种显示当前时间
    import datetime
    print(datetime.datetime.now())

    random模块

    #encoding:utf-8
    import random
    
    #产生一个0到1的数
    a=random.random()
    print(a)
    
    #产生1到10int型
    b=random.randint(1,10)
    print(b)
    
    c=random.choice([11,22,33])
    print(c)
    
    #任意选两个
    d=random.sample([11,22,44,66,33],2)
    print(d)
    
    #任意1-3范围浮点型
    e=random.uniform(1,3)
    print(e)
    
    #验证码
    import random
    def v_code():
        ret=""
        for i in range(5):
            num=random.randint(0,9)
            #随机字母
            alf=chr(random.randint(65,122))
            s=str(random.choice([num,alf]))
            ret+=s
        return ret
    print(v_code())
  • 相关阅读:
    Java解析XML(一)、SAX
    Java注解
    NTKO OFFICE文档控件为何不能自动装载?
    Java解析XML(二)、DOM
    JAVA反射机制
    如何手工卸载和安装NTKO OFFICE文档控件
    使用内省的方式操作JavaBean
    JDK自带的native2ascii转码工具使用详解
    HTTP协议详解
    跳过编译器,获取泛型参数的实际类型
  • 原文地址:https://www.cnblogs.com/2018-1025/p/10150258.html
Copyright © 2011-2022 走看看