python基础入门五

xml模块

python中支持对xml文件的增删改查操作,
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:

 print(root.iter('year')) #全文搜索
 print(root.find('country')) #在root的子节点找,只找一个
 print(root.findall('country')) #在root的子节点找,找所有

其中如果xml文件有namespace的情况,需要额外处理,将namespace引入修改xml文件的脚本。否则,会出现ns0的默认namespace

#!/usr/bin/python
# -*- coding: utf-8 -*-


# ---------------------------------------
import time, os
import xml.etree.ElementTree as ET

project_list = ['CooTalk_Bonus', 'CooTalk_File_Server', 'CooTalk_GateWay', 'CooTalk_Moments', 'CooTalk_Notify',
                'CooTalk_Public', 'CooTalk_r', 'CooTalk_Service', 'sms_checkcode', 'ucenter_cootalk']

for i in project_list:
    os.chdir('/root/.jenkins/workspace/svn_test/' + i)

    ###解决带有命名空间的xml文件,将namespace复制进来,注册即可

    ET.register_namespace('', "http://maven.apache.org/POM/4.0.0")
    ET.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")
    tree = ET.parse("pom.xml")
    root = tree.getroot()
    egg = '{http://maven.apache.org/POM/4.0.0}'

    for child in root:

        artifactId = egg + 'artifactId'
        name = egg + 'name'
        description = egg + 'description'
        if child.tag == artifactId or child.tag == name or child.tag == description:
            print('before:', child.text)
            child.text = i
            print('after:', child.text)

        version = egg + 'version'
        if child.tag == version:
            print('before:', child.text)
            child.text = time.strftime("%Y%m%d")
            print('after:', child.text)

    if i == 'CooTalk_File_Server':

        for country in root.findall(egg + 'dependencies'):
            add1 = ET.Element('dependency')
            country.append(add1)
            groupId = ET.Element('groupId')
            groupId.text = 'jce'
            artifactId = ET.Element('artifactId')
            artifactId.text = 'jce'
            version = ET.Element('version')
            version.text = '1.0'
            add1.append(groupId)
            add1.append(artifactId)
            add1.append(version)
            ##############
            add2 = ET.Element('dependency')
            country.append(add2)
            groupId = ET.Element('groupId')
            groupId.text = 'rt'
            artifactId = ET.Element('artifactId')
            artifactId.text = 'rt'
            version = ET.Element('version')
            version.text = '1.0'
            add2.append(groupId)
            add2.append(artifactId)
            add2.append(version)

    tree.write('pom.xml')
time模块
###time and datetime

import time ,datetime

##时间戳
print(time.time())

##把struc_time格式的时间转换为字符串时间,默认转换localtime()
print(time.asctime())

###自定义输出当前时间格式
    # %Y  Year with century as a decimal number.
    # %m  Month as a decimal number [01,12].
    # %d  Day of the month as a decimal number [01,31].
    # %H  Hour (24-hour clock) as a decimal number [00,23].
    # %M  Minute as a decimal number [00,59].
    # %S  Second as a decimal number [00,61].
    # %z  Time zone offset from UTC.
    # %a  Locale's abbreviated weekday name.
    # %A  Locale's full weekday name.
    # %b  Locale's abbreviated month name.
    # %B  Locale's full month name.
    # %c  Locale's appropriate date and time representation.
    # %I  Hour (12-hour clock) as a decimal number [01,12].
    # %p  Locale's equivalent of either AM or PM.
print(time.strftime("%c %Y%m%d"))

##转换时间戳类型的时间秒数为struct_time元组,默认为当前时间戳时间的转换
print(time.localtime(1509160485.8049517))

##第一次调用程序运行时间,第二次调用及以后返回自第一次调用到这次调用的时间间隔,windows和linux不同
print(time.clock())

##可以将时间戳类型的秒数转换为标准字符串格式输出
print(time.ctime(123456789))

##可以将时间戳类型时间转换为struct_time
print(time.gmtime(12343243))

##可以把struct_time格式转换为时间戳类型
print(time.mktime(time.localtime()))

##字符串时间转化为struct_time时间
print(time.strptime('Fri Nov 30 05:33:09 1973'))

###1.struct_time元祖类型  2.字符串类型  3.时间戳类型

print(datetime.datetime.now())
print(datetime.date.fromtimestamp(time.time()))




random模块
import random

print(random.random())
print(random.randint(1,5))
print(random.sample(['11','a-q','00'],2))
# print(random.randint())
zimu = chr(random.randint(97,122))
num = str(random.randint(0,9))
# print(random.sample([zimu,num,zimu,num],4))
print(random.choice([zimu,num]))

def makecode(num):
    '''用户输入验证码位数,随机生成带有字母和数字的验证码'''
    res = ''
    for i in range(num):
        zimu = chr(random.randint(97,122))
        digt = str(random.randint(0,9))
        res += random.choice([zimu,digt])
    return res

print(makecode(8))
os模块和sys模块
import os,sys,time

print(os.getcwd())
print(os.pardir)
print(os.path.getatime('modules.py'))


def gettopdir():
    '''获取当前文件的父目录,或者定居目录'''
    top = os.path.normpath(os.path.join(
        os.path.abspath(__file__),
        os.pardir,
        os.pardir
    ))
    return top

top = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(top)


print(sys.platform)
## %s 输出字符串,中间加数字制定字符串宽度,加‘-数字’表示制定宽度,并且左对齐
print('[%12s]'%'@')
## \r 覆盖之前打印内容
end = 0
while True:
    print('\r[%2s]'%'#',end='')
    end +=1
    if end == 4:
        break


###进度条########
def process(percent,width=50):
        if percent >=1:
            percent = 1
        showpro = '[%%-%ds]'%width% (int(width*percent)*'#')
        print('\r%s %d%%'%(showpro,int(percent*100)),file=sys.stdout,flush=True,end='')


data_size = 1050
recv_size = 0
while recv_size < data_size:
    time.sleep(0.2)
    recv_size+=60
    percent = recv_size/data_size

    process(percent)



logging模块

logging模块用来打印日志,由于打印日志需要用到logger,handler,fliter,formatter等几个函数,如果写在功能函数里会显得格外凌乱,于是,准备了配置文件配置完整的logging打印功能,到时只需调用即可。
首先了解logging模块的原理:

import logging

#1,负责产生日志

mylog = logging.getLogger(__name__)

#2.Handler负责接收日志,控制往哪输出

tt1 = logging.FileHandler('tt1.log')
tt2 = logging.FileHandler('tt2.log')

#3. formatter 负责追加格式

formmater1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

formmater2=logging.Formatter('%(asctime)s :  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

# 4.绑定Handler和formatter

tt1.setFormatter(formmater1)
tt2.setFormatter(formmater2)

# 5. 关联mylog和 Handler

mylog.addHandler(tt1)
mylog.addHandler(tt2)
mylog.setLevel(10)

##测试
mylog.error("dsaddddddddddd")

然后是可以用到生产环境的logging配置,

"""
logging配置
"""

import os
import logging.config

# 定义三种日志输出格式 开始

standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
                  '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字

simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'

id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'

# 定义日志输出格式 结束

logfile_dir = os.path.dirname(os.path.abspath(__file__))  # log文件的目录

logfile_name = 'all2.log'  # log文件名

# 如果不存在定义的日志目录就创建一个
if not os.path.isdir(logfile_dir):
    os.mkdir(logfile_dir)

# log文件的全路径
logfile_path = os.path.join(logfile_dir, logfile_name)

# log配置字典
LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': standard_format
        },
        'simple': {
            'format': simple_format
        },
    },
    'filters': {},
    'handlers': {
        #打印到终端的日志
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # 打印到屏幕
            'formatter': 'simple'
        },
        #打印到文件的日志,收集info及以上的日志
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
            'formatter': 'standard',
            'filename': logfile_path,  # 日志文件
            'maxBytes': 1024*1024*5,  # 日志大小 5M
            'backupCount': 5,
            'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
        },
    },
    'loggers': {
        #logging.getLogger(__name__)拿到的logger配置
        '': {
            'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            'level': 'DEBUG',
            'propagate': True,  # 向上(更高level的logger)传递
        },
    },
}


def load_my_logging_cfg():
    logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
    logger = logging.getLogger(__name__)  # 生成一个log实例
    logger.info('It works!')  # 记录该文件的运行状态

if __name__ == '__main__':
    load_my_logging_cfg()

测试应用:

"""
MyLogging Test
"""

import time
import logging
import my_logging  # 导入自定义的logging配置

logger = logging.getLogger(__name__)  # 生成logger实例


def demo():
    logger.debug("start range... time:{}".format(time.time()))
    logger.info("测试开始。。。")
    for i in range(10):
        logger.debug("i:{}".format(i))
        time.sleep(0.2)
    else:
        logger.debug("over range... time:{}".format(time.time()))
    logger.info("测试结束。。。")

if __name__ == "__main__":
    my_logging.load_my_logging_cfg()  # 在你程序文件的入口加载自定义logging配置
    demo()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值