标签:
shutil 高级的 文件、文件夹、压缩包 处理模块 http://www.cnblogs.com/wupeiqi/articles/4963027.html
shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的
shelve模块
shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
import shelve
d = shelve.open(‘shelve_test‘) #打开一个文件
class Test(object):
def __init__(self,n):
self.n = n
t = Test(123)
t2 = Test(123334)
name = ["alex","rain","test"]
d["test"] = name #持久化列表
d["t1"] = t #持久化类
d["t2"] = t2
d.close()
持久化存储,如果已经有的key-value,再次写时如果value有更新就会更新。
用shelve.open(‘test‘), 如果test本身不存在,会创建三个文件,test.dir, test.bak, test.dat. 下次要用到时,还是shelve.open(‘test‘)就可以。
而pickle再重新打开文件,dump时,如果不是追加模式,原来文件里的内容会冲掉。
xml module
PyYAML
configparser
hashlib
hmac
subprocess
python2.7下
logging
import logging
#create logger
logger = logging.getLogger(‘TEST-LOG‘)
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create file handler and set level to warning
fh = logging.FileHandler("access.log")
fh.setLevel(logging.WARNING)
# create formatter
formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
# add formatter to ch and fh
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)
# ‘application‘ code
logger.debug(‘debug message‘)
logger.info(‘info message‘)
logger.warn(‘warn message‘)
logger.error(‘error message‘)
logger.critical(‘critical message‘)
类的三个特性: 封装,继承,多态
类变量与实例变量
例子1:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Role(object):
members = 0
ac = None
def __init__(self,name,role,weapon,life_value):
self.name = name
self.role = role
self.weapon = weapon
self.life_val = life_value
Role.members +=1
def buy_weapon(self,weapon):
print("%s is buying [%s]" %(self.name,weapon))
self.weapon = weapon
print(self.ac)
#role 的实例
#把一个抽象的类变成一个具体的对象的过程 叫实例化
p1= Role("SanJiang",‘Police‘,"B10",90)
t1= Role("ChunYun",‘Terrorist‘,"B11",100)
t2= Role("T2",‘Terrorist‘,"B17",100)
t3= Role("T3",‘Terrorist‘,"B19",100)
p1.buy_weapon("AK47") #Role.buy_weapon(p1,"AK47")
t1.buy_weapon("B51")
p1.ac = "China Brand"
t1.ac = "US Brand"
Role.ac = "Janpanese Brand"
print("P1:",p1.weapon,p1.ac)
print("T1:",t1.weapon,t1.ac)
print("T2:",t2.ac,t2.weapon)
print("T3:",t3.ac,t3.weapon)
print(Role.buy_weapon)
例子二:
class SchoolMember(object):
member_nums = 0
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
self.enroll()
def enroll(self):
SchoolMember.member_nums +=1
print("\033[32;1mThe [%s] school member [%s] is enrolled!\033[0m" %(self.member_nums,self.name))
类的继承
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class SchoolMember(object):
member_nums = 0
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
self.enroll()
def enroll(self):
SchoolMember.member_nums +=1
print("\033[32;1mThe [%s] school member [%s] is enrolled!\033[0m" %(self.member_nums,self.name))
def tell(self):
print("Hello my name is %s" %self.name)
class Teacher(SchoolMember):
def __init__(self,name,age,sex,course,salary):
super(Teacher,self).__init__(name,age,sex)
self.course = course
self.salary = salary
#SchoolMember.__init__(self,name,age,sex)
def teaching(self):
print("Teacher [%s] is teaching [%s]" %(self.name,self.course))
class Student(SchoolMember):
def __init__(self,name,age,sex,course,tuition):
super(Student,self).__init__(name,age,sex)
self.course = course
self.tuition = tuition
def pay_tution(self):
print("cao ,student [%s] paying tution [%s]" %(self.name,self.tuition))
t1 = Teacher("Alex",22,‘F‘,"PY",1000)
t2 = Teacher("Tenglan",25,‘N/A‘,"PY",900)
s1 = Student("SanJiang",24,"Female","python",15000)
s2 = Student("BaoAn",23,"F","python",5000)
t1.tell()
t1.teaching()
s1.tell()
s1.pay_tution()
标签:
原文地址:http://www.cnblogs.com/joey251744647/p/5244801.html