• Python—模块


    模块?引用某大大的话就是“模块,用一坨代码实现了某个功能的代码集合。”

    类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。
    例如:os是系统相关的模块,time是时间相关的模块。
    模块分为三种:
    ·自定义模块;
    ·内置标准模块(又称标准库);
    ·开源模块。

    1、自定义模块:
     1 #!/usr/bin/env python
     2 # -.- coding: utf-8 -.-
     3 # By Sandler
     4 
     5 DATABASE = {
     6     "engine":"mysql",
     7     "host":"localhost",
     8     "port":3306,
     9     "user":"root",
    10     "passwd":"123"
    11 }
    2、开源模块:
    开源模块安装有两种方式:
    1 下载源码
    2 解压源码
    3 进入目录
    4 编译源码    python setup.py build
    5 安装源码    python setup.py install

    现在一般都不需要编译了,直接安装源码就可以,但,在使用源码安装时,需要使用到gcc和python开发环境,所以,需要先执行:

    1 yum install gcc
    2 yum install python-devel
    3 apt-get python-dev  #apt-get是ubuntu系统上安装程序的命令。
    开源模块官方发布网站,下载地址:https://pypi.python.org/pypi
    下载安装的模块会安装到指定的目录中:
    1 #!/usr/bin/env python
    2 # -.- coding: utf-8 -.-
    3 # By Sandler
    4 
    5 import sys
    6 sys.path
    7 ['C:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pydev', 'C:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pydev', 'C:\Python35\python35.zip', 'C:\Python35\DLLs', 'C:\Python35\lib', 'C:\Python35', 'C:\Python35\lib\site-packages', 'C:\Users\Sandler\PycharmProjects\PyS']
    8 #安装的开源模块会默认安装到'site-packages'目录,linux下也在site-packages目录
    如果sys.path路径列表没有你想要的路径,可以通过 sys.path.append('路径') 添加。
    通过os模块可以获取各种目录,例如:
    1 #!/usr/bin/env python
    2 # -.- coding: utf-8 -.-
    3 # By Sandler
    4 
    5 import sys
    6 import os
    7 pre_path = os.path.abspath('../')
    8 sys.path.append(pre_path)
    3、导入模块:
    导入模块有以下几种方法:
    1 import module
    2 #导入模块
    3 from module.xx.xx import xx
    4 #导入指定目录下的指定模块
    5 from module.xx.xx import xx as rename  
    6 #导入指定目录下的指定模块并赋予别名
    7 from module.xx.xx import *
    8 #导入指定目录下的所有模块,这种方法可能会导致导入的模块和现有的函数重名,所以一般不用
    导入模块其实就是告诉Python解释器去解释那个py文件
    导入一个py文件,解释器解释该py文件
    导入一个包,解释器解释该包下的__init__.py文件

    举例:
    开源模块 paramiko的下载和安装
    paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现实。
    1、下载安装
     1 # pycrypto,由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto
     2  
     3 # 下载安装 pycrypto
     4 wget http://files.cnblogs.com/files/wupeiqi/pycrypto-2.6.1.tar.gz
     5 tar -xvf pycrypto-2.6.1.tar.gz
     6 cd pycrypto-2.6.1
     7 python setup.py build
     8 python setup.py install
     9  
    10 # 进入python环境,导入Crypto检查是否安装成功
    11  
    12 # 下载安装 paramiko
    13 wget http://files.cnblogs.com/files/wupeiqi/paramiko-1.10.1.tar.gz
    14 tar -xvf paramiko-1.10.1.tar.gz
    15 cd paramiko-1.10.1
    16 python setup.py build
    17 python setup.py install
    18  
    19 # 进入python环境,导入paramiko检查是否安装成功
    2、使用模块
    执行命令 - 通过用户名和密码连接服务器
     1 #!/usr/bin/env python
     2 # -.- coding:utf-8 -.-
     3 # By Sandler
     4 import paramiko
     5 ssh = paramiko.SSHClient()
     6 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     7 ssh.connect('192.168.11.11', 22, 'jack', '111111')
     8 stdin, stdout, stderr = ssh.exec_command('df')
     9 print stdout.read()
    10 ssh.close();

    执行命令 - 过密钥链接服务器

     1 #!/usr/bin/env python
     2 # -.- coding:utf-8 -.-
     3 # By Sandler
     4 import paramiko
     5 private_key_path = '/home/auto/.ssh/id_rsa'
     6 key = paramiko.RSAKey.from_private_key_file(private_key_path)
     7 ssh = paramiko.SSHClient()
     8 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     9 ssh.connect('主机名 ', 端口, '用户名', key)
    10 stdin, stdout, stderr = ssh.exec_command('df')
    11 print stdout.read()
    12 ssh.close()

    上传或者下载文件 - 通过用户名和密码

     1 #!/usr/bin/env python
     2 # -.- coding:utf-8 -.-
     3 # By Sandler
     4 #上传文件
     5 import os,sys
     6 import paramiko
     7 t = paramiko.Transport(('192.168.11.11',22))
     8 t.connect(username='jack',password='111111')
     9 sftp = paramiko.SFTPClient.from_transport(t)
    10 sftp.put('/tmp/test.py','/tmp/test1.py') 
    11 t.close()
    12 #下载文件
    13 import os,sys
    14 import paramiko
    15 t = paramiko.Transport(('192.168.11.11',22))
    16 t.connect(username='jack',password='111111')
    17 sftp = paramiko.SFTPClient.from_transport(t)
    18 sftp.get('/tmp/test.py','/tmp/test1.py')
    19 t.close()

    上传或下载文件 - 通过密钥

     1 #!/usr/bin/env python
     2 # -.- coding:utf-8 -.-
     3 # By Sandler
     4 #上传文件
     5 import paramiko
     6 pravie_key_path = '/home/auto/.ssh/id_rsa'
     7 key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
     8 t = paramiko.Transport(('192.168.11.11',22))
     9 t.connect(username='jack',pkey=key)
    10 sftp = paramiko.SFTPClient.from_transport(t)
    11 sftp.put('/tmp/test3.py','/tmp/test4.py') 
    12 t.close()
    13 #下载文件
    14 import paramiko
    15 pravie_key_path = '/home/auto/.ssh/id_rsa'
    16 key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
    17 t = paramiko.Transport(('192.168.11.11',22))
    18 t.connect(username='jack',pkey=key)
    19 sftp = paramiko.SFTPClient.from_transport(t)
    20 sftp.get('/tmp/test3.py','/tmp/test4.py') 
    21 t.close()

    内置模块
    TIME模块
     1 #!/usr/bin/env python
     2 # -.- coding: utf-8 -.-
     3 # By Sandler
     4 
     5 import time
     6 import datetime
     7  
     8 print(time.clock()) #返回处理器时间,3.3开始已废弃
     9 print(time.process_time()) #返回处理器时间,3.3开始已废弃
    10 print(time.time()) #返回当前系统时间戳
    11 print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间
    12 print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式
    13 print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
    14 print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
    15 print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
    16 #time.sleep(4) #sleep
    17 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
    18 print(time.strptime("2016-01-28","%Y-%m-%d") ) #将字符串格式转换成struct_time格式
    19  
    20 #datetime module
    21  
    22 print(datetime.date.today()) #输出格式 2016-01-26
    23 print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式
    24 current_time = datetime.datetime.now() #
    25 print(current_time) #输出2016-01-26 19:04:30.335935
    26 print(current_time.timetuple()) #返回struct_time格式
    27  
    28 #datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
    29 print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
    30  
    31 str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
    32 new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
    33 new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
    34 new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
    35 new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
    36 print(new_date)
    DirectiveMeaningNotes
    %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.  
    %d Day of the month as a decimal number [01,31].  
    %H Hour (24-hour clock) as a decimal number [00,23].  
    %I Hour (12-hour clock) as a decimal number [01,12].  
    %j Day of the year as a decimal number [001,366].  
    %m Month as a decimal number [01,12].  
    %M Minute as a decimal number [00,59].  
    %p Locale’s equivalent of either AM or PM. (1)
    %S Second as a decimal number [00,61]. (2)
    %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
    %w Weekday as a decimal number [0(Sunday),6].  
    %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
    %x Locale’s appropriate date representation.  
    %X Locale’s appropriate time representation.  
    %y Year without century as a decimal number [00,99].  
    %Y Year with century as a decimal number.  
    %z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
    %Z Time zone name (no characters if no time zone exists).  
    %% A literal '%' character.
     
    LOGGING模块
    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug(), info(), warning(), error() and critical() 5个级别,下面我们看一下怎么用。
     
    最简单用法
     1 #!/usr/bin/env python
     2 # -.- coding: utf-8 -.-
     3 # By Sandler
     4 import logging
     5  
     6 logging.warning("user [alex] attempted wrong password more than 3 times")
     7 logging.critical("server is down")
     8  
     9 #输出
    10 WARNING:root:user [alex] attempted wrong password more than 3 times
    11 CRITICAL:root:server is down

    看一下这几个日志级别分别代表什么意思

    LevelWhen it’s used
    DEBUG Detailed information, typically of interest only when diagnosing problems.
    INFO Confirmation that things are working as expected.
    WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
    ERROR Due to a more serious problem, the software has not been able to perform some function.
    CRITICAL A serious error, indicating that the program itself may be unable to continue running.
     如果想把日志写到文件里,也很简单
    1 #!/usr/bin/env python
    2 # -.- coding: utf-8 -.-
    3 # By Sandler
    4 import logging
    5  
    6 logging.basicConfig(filename='example.log',level=logging.INFO)
    7 logging.debug('This message should go to the log file')
    8 logging.info('So should this')
    9 logging.warning('And this, too')

    其中下面这句中的level=loggin.INFO意思是,把日志纪录级别设置为INFO,也就是说,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,在这个例子, 第一条日志是不会被纪录的,如果希望纪录debug的日志,那把日志级别改成DEBUG就行了。

    1 logging.basicConfig(filename='example.log',level=logging.INFO)

    感觉上面的日志格式忘记加上时间啦,日志不知道时间怎么行呢,下面就来加上!

     1 #!/usr/bin/env python
     2 # -.- coding: utf-8 -.-
     3 # By Sandler
     4 
     5 import logging
     6 
     7 logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
     8 logging.warning('is when this event was logged.')
     9  
    10 # 输出
    11 12/12/2010 11:46:36 AM is when this event was logged.
    如果想同时把log打印在屏幕和文件日志里,就需要了解一点复杂的知识 了

    The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.

    日志库采用模块化的方法,提供了几种类型的组件:伐木者、处理程序、过滤器和格式器

    • Loggers expose the interface that application code directly uses.
    • Loggers公开接口,应用程序代码中直接使用
    • Handlers send the log records (created by loggers) to the appropriate destination.
    • 处理程序发送日志记录(由Loggers)到相应的目的地
    • Filters provide a finer grained facility for determining which log records to output.
    • 过滤器提供更细粒度的功能确定哪些日志记录输出
    • Formatters specify the layout of log records in the final output.
    • 格式器指定的布局在最终的输出日志记录
     1 #!/usr/bin/env python
     2 # -.- coding: utf-8 -.-
     3 # By Sandler
     4 
     5 import logging
     6 
     7 #create logger
     8 logger = logging.getLogger('TEST-LOG')  # 定义日志记录用户
     9 logger.setLevel(logging.DEBUG)  # 定义全局日志等级
    10 # create console handler and set level to debug
    11 ch = logging.StreamHandler()    # 定义屏幕输出
    12 ch.setLevel(logging.DEBUG)  # 定义屏幕输出日志等级
    13 # create file handler and set level to warning
    14 fh = logging.FileHandler("access.log")  # 定义日志输出文件
    15 fh.setLevel(logging.WARNING)    # 定义文件日志输出等级
    16 # create formatter
    17 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    18 # 日志格式化
    19 # add formatter to ch and fh
    20 ch.setFormatter(formatter)  # 日志屏幕输出格式化
    21 fh.setFormatter(formatter)  # 日志文件输出格式化
    22 # add ch and fh to logger
    23 logger.addHandler(ch)   
    24 logger.addHandler(fh)
    25 # 'application' code
    26 logger.debug('debug message')
    27 logger.info('info message')
    28 logger.warn('warn message')
    29 logger.error('error message')
    30 logger.critical('critical message')
  • 相关阅读:
    连号区间数|2013年蓝桥杯B组题解析第十题-fishers
    带分数|2013年蓝桥杯B组题解析第九题-fishers
    翻硬币|2013年蓝桥杯B组题解析第八题-fishers
    错误票据|2013年蓝桥杯B组题解析第七题-fishers
    三部排序|2013年蓝桥杯B组题解析第六题-fishers
    前缀判断|2013年蓝桥杯B组题解析第五题-fishers
    黄金连分数|2013年蓝桥杯B组题解析第四题-fishers
    第39级台阶|2013年蓝桥杯B组题解析第三题-fishers
    马虎的算式|2013年蓝桥杯B组题解析第二题-fishers
    高斯日记|2013年蓝桥杯B组题解析第一题-fishers
  • 原文地址:https://www.cnblogs.com/sandler613/p/5562047.html
Copyright © 2020-2023  润新知