def save(db, obj):
try:
# save attr1
db.execute('a sql stmt', obj.attr1)
# save attr2
db.execute('another sql stmt', obj.attr2)
except DBError:
db.rollback()
else:
db.commit()
def print_prime2(n):
for i in range(2, n):
for j in range(2, i):
if i % j == 0:
break
else:
print('%d is a prime number'+ str(i))
print_prime2(111)
a,b,c = 1,'aa','Filte'
a_string = ','.join([str(a),b,c,"测试"])
print(a_string)
return: 1,aa,Filte,测试
def f(v):
if v%2 == 0:
v = v**2
else:
v = v+1
return v
[f(v) for v in [2,3,4,-1] if v>0]
return: [4, 4, 16]
fh = open("test.txt", "r")
result = [i for i in fh if "abc" in i] #
文件句柄可以当做可迭代对象
print result
class Pizza(object):
def __init__(self,name,size,price):
self.name = name
self.size = size
self.price = price
def getPizzaInfo(self):
return self.name,self.size,self.price
def showPizzaInfo(self):
print("Pizza name:"+self.name)
print("Pizza size:"+str(self.size))
print("Pizza price:"+str(self.price))
def changeSize(self,size):
self.size = size
def changePrice(self,price):
self.price = price
class Order(object):
def __init__(self,name):
self.costomername = name
self.pizzaList = []
self.pizzaList.append(Pizza("Mushroom",12,30))
def ordermore(self,pizza):
self.pizzaList.append(pizza)
def changeName(self,name):
self.costomername=name
def getorderdetail(self):
print("customer name:"+self.costomername)
for i in self.pizzaList:
i.showPizzaInfo()
print(id(i))
def getPizza(self,number):
return self.pizzaList[number]
customer1=Order("zhangSan")
customer1.ordermore(Pizza("seafood",9,40))
customer1.ordermore(Pizza("fruit",12,35))
print("customer1 order infomation:")
customer1.getorderdetail()
print("-------------------------------")
# customer2=copy.copy(customer1) #浅拷贝
customer2=copy.deepcopy(customer1) # 深拷贝
print("order 2 customer name:"+customer2.costomername)
customer2.changeName("li")
customer2.getPizza(2).changeSize(9)
customer2.getPizza(2).changePrice(30)
print("customer2 order infomation:")
customer2.getorderdetail()
print("-------------------------------------")
customer1.getorderdetail()
cfg.ini配置文件:
[DEFAULT]
in_default = 'an option value in default'
[section2]
[section1]
option1=True
conf = configparser.ConfigParser()
conf.read('cfg.ini')
print(conf.getboolean('section1', 'option1'))
print(conf.get('section2', 'in_default'))
##输出
True
'an option value in default'
##
###cfg.ini
[DEFAULT]
conn_str = %(dbn)s://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s
dbn = mysql
user = root
host=192.168.127.2
port = 3306
[db1]
user = test1
pw=test_123
db=ucds_1
[db2]
user = test2
host=192.168.127.1
pw=test_123
db=ucds_2
port=3308
conf = configparser.ConfigParser()
conf.read('cfg.ini')
print("mysql conn",conf.get('db1', 'conn_str'))
print("mysql conn",conf.get('db2', 'conn_str'))
##输出
mysql conn mysql://test1:[email protected]:3306/ucds_1
mysql conn mysql://test2:[email protected]:3308/ucds_2
##
class TelMixin:
def telfunc(self, num):
self.name = num
print("call Phone ",self.name)
class SmsMixin:
def smsfunc(self, num):
self.name = num
print("send Sms info:",self.name)
class SongMixin:
def songfunc(self):
print("我可以放音乐")
class Phone(TelMixin,SmsMixin,SongMixin):
def __init__(self):
pass
class Ipod(SongMixin):
def __init__(self, sn):
self.num = num
p = Phone()
p.telfunc("110")
p.smsfunc("110")
p.songfunc()
[devops@my-dev ccod_check_back_info]$ pip install pylint
[devops@my-dev ccod_check_back_info]$ pylint atest.py
************* Module atest
atest.py:5:17: C0303: Trailing whitespace (trailing-whitespace)
atest.py:7:0: C0301: Line too long (119/100) (line-too-long)
atest.py:9:0: C0301: Line too long (170/100) (line-too-long)
atest.py:1:0: C0114: Missing module docstring (missing-module-docstring)
atest.py:4:0: C0116: Missing function or method docstring (missing-function-docstring)
atest.py:17:0: C0116: Missing function or method docstring (missing-function-docstring)
atest.py:19:4: C0103: Variable name "Passwd" doesn't conform to snake_case naming style (invalid-name)
atest.py:22:12: C0103: Variable name "Passwd" doesn't conform to snake_case naming style (invalid-name)
-----------------------------------
本文由mdnice多平台发布