Python整理笔记

Python

目录

  • 基本编程语法
    • Python脚本抬头
    • Python脚本接受参数
    • 异常捕获代码
    • 创建Python实体类
    • 数据库访问
    • 执行系统命令行
    • python使用枚举对象
    • python中的元类的理解
  • 内置方法
  • 模块介绍
    • 核心模块介绍
    • 标准模块
    • 线程和进程
    • 数据标示
    • 文件格式
    • 邮件和新闻消息处理
    • 网络协议
    • 国际化模块
    • 更多模块
  • Python面向对象编程

基本编程语法

Python脚本抬头

使用Python作为脚本,需要输入以下两段文字

#!/usr/bin/python   # 标示该脚本使用Python执行
# -*-coding:utf-8-*-    # 标示该脚本使用utf-8编码

如果Python脚本在运行代码是,出现编码错误则加入以下代码来解决

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

Python脚本接受参数

在脚本中加入以下代码,即可接受脚本参数

import argparse
def __main__():
    newParaser =argparse.ArgumentParser(description=description,usage=quick_usage)
    
    newParaser.add_argument("-root_path", dest="root_path", help="所有根目录,也就是在哪个目录下进行搜索",default="***@bio.com")
    newParaser.add_argument("-search_key", dest="search_key", help="用户id,将文件推送给对应用户", default="")
    
    args = newParaser.parse_args()
    argsDict = args.__dict__
    
    root_path = argsDict.get('root_path')
    search_key = argsDict.get('search_key')
    
if __name__ == "__main__":
    __main__()

异常捕获代码

try:
    x = 1000
except IoException as e:
    throw e
except ValueError:
    print 'error'

创建Python实体类

class Entity:
    def __init__(self, param1, param2):  # 构造方法
        self.param1 = param1
        self.param2 = param2
    
    def __str__(self):  # str方法的打印语句
        return "{param1:%s,param2:%s}" % (self.param1, str(self.param2))
        
    def __repr__(self): # 内嵌对象打印语句
        return "{param1:%s,param2:%s}" % (self.param1, str(self.param2))

数据库访问

import MySQLdb
# 创建数据库连接
connect = MySQLdb.connect(host=hostIp, user=userName, passwd=password, db=dbName)
# 查询语句
cur = connect.cursor()
cur.execute(sql)
rows = cur.fetchall()
# update语句
cur = connect.cursor()
cur.execute(sql)
connect.commit()

执行系统命令行

# 使用os模块
import os
os.system(cmd)
# 或者使用
os.popen(cmd)

python使用枚举对象

from enum import Enum
FileType = Enum('img', 'doc', 'xls')

python中的元类的理解

深刻理解Python中的元类(metaclass)

内置方法

类常用内置方法,具体的方法还请查考python魔法方法指南该魔法指南很详细的介绍的python中每一种内置方法的用法

  • __init__(self,...)
  • __del__(self)
  • __new__(cls,*args,**ked)
  • __str__(self)
  • __getitem__(self,key)
  • __len__(self)
  • __cmp__(stc,det)

模块介绍

核心模块介绍

  • __builtin__
  • exceptions
  • os
  • os.path
  • stat
  • string
  • re
  • math
  • cmath
  • operator
  • copy
  • sys
  • atexit
  • time
  • types
  • gc

标准模块

  • fileinput
  • shutil
  • tempfile
  • StringIO
  • cStringIO
  • mmap
  • UserDist
  • UserList
  • UserString
  • traceback
  • errno
  • getopt
  • getpass
  • glob
  • fnmatch
  • random
  • whrandom
  • md5
  • sha
  • crypt
  • rotor
  • zlib
  • code

线程和进程

  • threading
  • Queue
  • thread
  • commands
  • pipes
  • popen2
  • signal

数据标示

  • array
  • struct
  • xdrlib
  • marshal
  • pickle
  • cPickle
  • copy_reg
  • pprint
  • repr
  • base64
  • binhex
  • quopri
  • uu
  • binascii

文件格式

  • xmllib
  • xml.parsers.expat
  • sgmllib
  • htmllib
  • htmlentitydefs
  • ConfigParser
  • netrc
  • shlex
  • zipfile
  • gzip

邮件和新闻消息处理

  • rfc822
  • mimetools
  • MimeWriter
  • mailbox
  • mailcap
  • mimetypes
  • packmail
  • mimify
  • multifile

网络协议

  • socket
  • select
  • asyncore
  • asynchat
  • urlib
  • urlparser
  • cookie
  • rebotparser
  • ftplib
  • gopherlib
  • httplib
  • poplib
  • imaplib
  • smtplib
  • telnetlib
  • nntplib
  • SocketServer
  • BaseHTTPServer
  • SimpleHTTPServer
  • CGIHTTPServer
  • cgi
  • webbrower

国际化模块

  • locale
  • unicodedata
  • ucnhash

更多模块

更多模块请参考 python 标准库

Python面向对象编程

Object-Oriented Programming in Python

你可能感兴趣的:(Python整理笔记)