胖蔡说技术
随便扯扯

Python 模块

python模块是一个比较常用的功能,模块类似于Java中的jar包,是一个将部分程序功能通过打包的形式包装成一个整体的形式,然后便于我们在需要使用的场景直接通过引用模块来实现功能,降低代码的耦合性,增加代码的复用性。模块的使用可以通过使用from关键字和import关键字,如下给出一个简单的小例子:

#通过import直接导入模块.
import sys,time
#通过from module import  function 导入random 模块中的shuffle函数.
from random import shuffle
#通过from module import * 导入collections模块的所有公开API.
from collections import *


#导入的sys模块的特性使用
print "system path:",sys.path
print time.asctime()
#导入的shuffle的使用,shuffle可以将序列随机打乱
list=[1,2,3,4,5,6,7,8,9,0]
shuffle(list)
print "\n\n",list
#导入的collection模块
q=deque(list)
q.append(12)
q.appendleft(45)
q.pop()
print q
q.rotate(-3)
print q 
system path: ['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.7/dist-packages/IPython/extensions', '/home/caihaifei/.ipython']
Tue Dec  5 13:17:53 2017


[4, 5, 0, 3, 8, 1, 9, 6, 2, 7]
deque([45, 4, 5, 0, 3, 8, 1, 9, 6, 2, 7])
deque([0, 3, 8, 1, 9, 6, 2, 7, 45, 4, 5])

如上为简单使用的几个python自带的模块的使用示例。其中导入的模块之间或者模块的属性之间可以通过*,来连接.通过import直接导入的模块使用其内部的属性或函数等的时候需要使用moduleName.beahavior来调用,而通过from moudle import name 方式调用的模块可以直接通过属性名称使用而不需要指定模块名称.如下,通过几个方面来了解模块的使用以及创建。

模块的创建

模块的创建其实就是创建一个py文件,让其可以通过在py文件中定义一些诸如“类、属性、函数”等,然后通过在另一个脚本文件(*.py)中导入执行或者直接通过命令行命令执行。如下根据一个简单的示例来介绍模块的创建过程.

#caculator.py

#!/usr/bin/env python
# -*- encoding:utf-8 -*-

import sys

def addition(*args):
    """
    Addition Operation.
    :param args:  All the operands.
    :return: the result of operation.
    """
    if len(args) > 0:
        result = args[0]
        try:
            for operand in args[1:]:
                result += operand
        except Exception as e:
            print str(e)
            pass
        return result
    else:
        print "args must not be empty."
        raise Exception("args must not be empty.")


def subtraction(*args):
    """
    Subtraction Operation.
    :param args: All the operands.
    :return: the result of operation.
    """
    if len(args) > 0:
        result = args[0]
        try:
            for operand in args[1:]:
                result -= operand
        except Exception as e:
            print str(e)
            pass
        return result
    else:
        print "args must not be empty."
        raise Exception("args must not be empty.")


def monocular(operator, *args):
    """
     Simple Operation
    :param operator:  Operator for monocular operations
    :param args: All the operands.
    :return:the result of operation.
    """
    if len(args) > 0:
         result = float(args[0])
        try:
            for operand in args[1:]:
                if operator in "+":
                    result += float(operand)
                elif operator in '-':
                    result -= float(operand)
                elif operator in '*':
                    result *= float(operand)
                elif operator in '/':
                    result /= float(operand)
        except Exception as e:
            print str(e)
            pass
        return result
    else:
        print "args must not be empty."
        raise Exception("args must not be empty.")


if __name__ == "__main__":
    """
      By  external call.
    """
    print "main step."
    # args of used.
    if len(sys.argv) >= 4:
        print sys.argv
        args = sys.argv[1:]
        result = monocular(args[0], *(args[1:]))
         print "get result:", result
import calculator,sys

#设置模块文件加载的路径
sys.path.append('../')
print calculator.monocular("/",46,3)
15

如上,为一个简单的单目计算器,提供了单独的计算加法、减法和通过指定运算符号(”+”、“-”、“*”、“/”)来计算结果,如上所示,上面通过sys.path添加模块位置。通过import导入caculator可以直接使用calculator中的计算方法。caculator中页提供了直接传入参数的方法,使用如下:

caihaifei@hfcai:#  python calculator.py - 12 4
['calculator.py', '-', '12', '4']
get result: 8.0

如上,通过命令行传入参数,py中通过导入sys模块,对输入参数通过sys.args进行接收,接受对象为一个list,将参数依照顺传入,然后对参数进行过滤处理,参数参数,回调返回运算结果.综上,可以简单概况模块创建使用的几个步骤:

  • 创建模块文件(*.py)
    这是模块功能执行的核心,需要通过编写逻辑代码完成功能。
  • 导入模块
    在需要使用模块功能的文件中导入需要使用的模块,通过使用importfrom实现导入。
  • 查找模块
    导入模块后在使用模块的时候,需要找到对应的模块位置,默认python查找py文件位置在系统设置的path路径中,可以通过sys模块中的path追加查找目录。
  • 功能使用 若是通过import方式导入的需要通过模块名.功能调用,若是通过from module import function导入的可以直接使用对应的特性或函数调用。

包与模块

包可以说python中一种特殊的模块形式,包中可以包含多个模块(以py为后缀的单个脚本文件),同时由于包的隔离也会避免多个不同模块的重名问题,和java中的包有异曲同工之妙。python中的包的结构有两个部分组成:一个是包的识别文件,任意一个python包中均需要包含一个定义包的文件(init.py)在包的根目录下(且需要注意的是对于python中的包而言,每个层次的文件夹下均需要包含init.py文件用于申明包的合法地位);另一个部分就是包中包含的子模块,python中需要将一个模块添加到一个包内,仅仅需要将该模块文件(可以是一个包也可以是一个py文件)添加到包的文件夹目录下即可。如下,为一个简单的包的结构形式展示:

package-dir-
     ---- __init__.py  #包模块定义文件,可以不包含任何内容,但文件一定需要存在.
     ----package_A
          ---- __init__.py #子包中的定义文件
          ----A.py   #package_A的子模块文件
          ----B.py
     ----A.py   * pacakge-dir-的自模块文件
     ----B.py
     ----C.py

对于包模块的使用和一般的模块使用类似,对于包模块而言,模块名即是包名,对于包内的自模块的使用可以通过使用包名.模块名来使用。使用如
下示例:

import smtplib
from email.mime.text import MIMEText  
from email.header import Header

# Create a text/plain message
msg = MIMEText('测试文本...', 'plain', 'utf-8')

me = 'root@enjoytoday.cn'
you = 'caihaifei@enjoytoday.cn'
msg['Subject'] = Header('The contents of %s' % msg,'utf-8')
msg['From'] = Header(me,'utf-8')
msg['To'] = Header(you,'utf-8')

try:
    s = smtplib.SMTP('localhost')
    s.sendmail(me, [you], msg.as_string())
    print ("邮件发送成功-----------------")
    s.quit()
except smtplib.SMTPException as e:
    error="无法发送邮件:",e
    print(error)

如上为一个使用Python邮件处理的包发送邮件的示例,但一般不一定能发送成功需要做一些必要的处理,这里主要是通过其看包的使用,如上包为email,其中mime为其中的一个包模块,header为其中的一个普通模块,textmine内的一个模块,导入如上通过:

from package.module import function
from package.module import *

通过上面的格式导入一个包内的模块的函数和特性,导入完成后的使用和一般模块并没有什么不一样.

模块中的一些魔法属性以及模块的使用说明

python中的标准模块和三方模块都较多。所以学会如何去查看一个模块的基本信息以及掌握如何使用就显得很重要,一般可以通过网上的教程和文档去了解使用,同时也可以通过对模块内部的文档和属性说明进行了解。

  • 文档说明

可以通过模块提供的注解文档进行了解模块的使用,文档的获取可以通过两种方式获取:一种是通过内建函数help获取,一种通过文档内的doc魔法属性进行了解。如下,为对email模块的注释文档信息获取.

import email

print "help get info:"
help(email)
print "\n\n __doc__  info:"
print email.__doc__
help get info:
Help on package email:

NAME
    email - A package for parsing, handling, and generating email messages.

FILE
    /home/hfcai/anaconda3/envs/py2/lib/python2.7/email/__init__.py

MODULE DOCS
    https://docs.python.org/library/email

PACKAGE CONTENTS
    _parseaddr
    base64mime
    charset
    encoders
    errors
    feedparser
    generator
    header
    iterators
    message
    mime (package)
    parser
    quoprimime
    utils

FUNCTIONS
    message_from_file(fp, *args, **kws)
        Read a file and parse its contents into a Message object model.

        Optional _class and strict are passed to the Parser constructor.

    message_from_string(s, *args, **kws)
        Parse a string into a Message object model.

        Optional _class and strict are passed to the Parser constructor.

DATA
    Charset = <email.LazyImporter object>
    Encoders = <email.LazyImporter object>
    Errors = <email.LazyImporter object>
    Generator = <email.LazyImporter object>
    Header = <email.LazyImporter object>
    Iterators = <email.LazyImporter object>
    MIMEAudio = <email.LazyImporter object>
    MIMEBase = <email.LazyImporter object>
    MIMEImage = <email.LazyImporter object>
    MIMEMessage = <email.LazyImporter object>
    MIMEMultipart = <email.LazyImporter object>
    MIMENonMultipart = <email.LazyImporter object>
    MIMEText = <email.LazyImporter object>
    Message = <email.LazyImporter object>
    Parser = <email.LazyImporter object>
    Utils = <email.LazyImporter object>
    __all__ = ['base64MIME', 'Charset', 'Encoders', 'Errors', 'Generator',...
    __version__ = '4.0.3'
    base64MIME = <email.LazyImporter object>
    quopriMIME = <email.LazyImporter object>

VERSION
    4.0.3




 __doc__  info:
A package for parsing, handling, and generating email messages.

如上,发现明显的help可以获取更多的信息,而相对的doc可能比较随意,只是一些简单的描述。

  • 基本的使用功能

为了更快的掌握模块的使用,可能需要尽量的了解比较常用的功能(类和函数等),这样可以节约不少时间,python中为此也提供了几种方式帮助我妈快速了解整个模块的功能结果。一样的,这个也提供两种方式,一种属于python中自动生成处理的(dir()),一个是人工手动设置的(all),使用如下:

import email

print "dir of email:",dir(email)

print "\n\n email usually functions:",email.__all__
dir of email: ['Charset', 'Encoders', 'Errors', 'FeedParser', 'Generator', 'Header', 'Iterators', 'LazyImporter', 'MIMEAudio', 'MIMEBase', 'MIMEImage', 'MIMEMessage', 'MIMEMultipart', 'MIMENonMultipart', 'MIMEText', 'Message', 'Parser', 'Utils', '_LOWERNAMES', '_MIMENAMES', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_name', '_parseaddr', 'base64MIME', 'base64mime', 'charset', 'email', 'encoders', 'errors', 'feedparser', 'generator', 'header', 'importer', 'iterators', 'message', 'message_from_file', 'message_from_string', 'mime', 'parser', 'quopriMIME', 'quoprimime', 'sys', 'utils']


 email usually functions: ['base64MIME', 'Charset', 'Encoders', 'Errors', 'Generator', 'Header', 'Iterators', 'Message', 'MIMEAudio', 'MIMEBase', 'MIMEImage', 'MIMEMessage', 'MIMEMultipart', 'MIMENonMultipart', 'MIMEText', 'Parser', 'quopriMIME', 'Utils', 'message_from_string', 'message_from_file', 'base64mime', 'charset', 'encoders', 'errors', 'generator', 'header', 'iterators', 'message', 'mime', 'parser', 'quoprimime', 'utils']

一般情况下会将模块中的所有方法属性信息都列出来,看起来比较难以多(包含许多我们并不需要的信息如魔法属性等),而all为模块创建者为了提供的公有API接口,具有较大的参考性.

  • 模块源码

最最有效的方式但往往也是最浪费时间的方法,直接看模块的源代码来推理逻辑,这个会对模块的结构更加详细的了解,但需要花费较大的精力,python中提供了file属性来帮助我们查看源码,如下:

import email

email.__file__
'/home/hfcai/anaconda3/envs/py2/lib/python2.7/email/__init__.pyc'

该属性会输出源码的文件位置(包类型的模块指定的位置为对应的init.py文件),可以直接查看源码或者复制代码,一般输出的文件类型为.py或者.pyc

python中的自带常用模块的使用

python中自带了许多标准的模块来帮助我们处理一些基础的功能。如实现时间的处理的time、常用的sys模块、操作系统服务访问模块os、文件操作模块fileinput模块和网络处理模块urlliburllib2等。这些模块可以实现一些比较重要的和复杂的事,让我们更加专注与具体事务的处理。模块的具体使用方法和模块功能介绍可以通过pythonlibrary文档进行了解和使用,地址为:https://docs.python.org/2/library/index.html

赞(0) 打赏
转载请附上原文出处链接:胖蔡说技术 » Python 模块
分享到: 更多 (0)

请小编喝杯咖啡~

支付宝扫一扫打赏

微信扫一扫打赏