python的学习之路(三)

一、set集合
#!/usr/bin/env python
# *_*coding:utf-8 *_*
# Author: harson

old_dict = {
"#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#2": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80}
}

# cmdb 新汇报的数据
new_dict = {
"#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 800},
"#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#4": {'hostname': 'c2', 'cpu_count': 2, 'mem_capicity': 80}
}
old = set(old_dict.keys())
new = set(new_dict.keys())

update_set = old.intersection(new)

del_set = old.symmetric_difference(update_set)
insert_set = new.symmetric_difference(update_set)

print(update_set)
print(del_set)
print(insert_set)
二、collection
#!/usr/bin/env python
# *_*coding:utf-8 *_*
# Author: harson

import collections

obj = collections.Counter('dsafd45f45d6s4t6we4g56ds4f5')
print(obj)
obj1 = obj.elements()
print(obj1)
for k in obj.elements():
print(k)
for k,v in obj.items():
print(k,v)

#有序字典
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic['k3'] = 'v3'
print(dic)
dic.move_to_end('k1')
print(dic)
dic.popitem()
print(dic)
dic.pop('k2')
print(dic)
dic.setdefault('k4','v4')
dic.update({'k1':'v111','k5':'v5'})
print(dic)

#默认字典
ddic = collections.defaultdict(list)
ddic['k1'].append('v123')
print(ddic)

#可命名元组
my_tuple = collections.namedtuple('my_tuple',['x','y','z'])
t = my_tuple(11,22,33)
d=t.x
print(d)

#双向队列
q = collections.deque()
q.append(11)
q.append(22)
q.appendleft(11)
c =q.count(11)
print(q)
print(c)

#单项队列
que = queue.Queue()
que.put(11)
que.put(22)
g = que.get()
print(g)

import copy

#字符串、数字的深拷贝、浅拷贝、赋值时内存地址相同
s = 11
str = 'zifuchuan'
s1 = s
str1 = copy.copy(str)
str2 = copy.deepcopy(str)
print(id(s))
print(id(s1))
print(id(str))
print(id(str1))
print(id(str2))

#其他、列表、字典、元组赋值时内存地址相同,浅拷贝及深拷贝内存地址不同,但是最底层元素内存地址相同

k = {'k1':'111','k1':'harson','k3':['lee','kobe']}
k1=k
print(id(k))
print(id(k1))
k2 = copy.copy(k)
k3 = copy.deepcopy(k)
print(id(k2))
print(id(k3))


#深拷贝应用

dic = {'cpu':[80,],'mem':[80,],'disk':[80,]}
print('before',dic)
new_dic = copy.deepcopy(dic)
new = copy.copy(dic)

new_dic['cpu'][0] = 50
new['cpu'][0] = 40

print(dic)
print(new_dic)
print(new)

二、函数
#无参数def mail():
    n = 11
n += 1
print(n)

mail()
d = mail()
d
e = mail
e()
#一个参数
def a(a):
print(a)

a(2)

#默认参数
def show(a=22,b=33):
print(a,b)

show()
show(55,66)

#指定参数
def show1(a,b):
print(a,b)

show1(b=3,a=4)

#动态参数、参数前加一个*代表把传入的参数转化为一个元组,参数前加两个*代表把传入的参数转化为字典
#两个带*参数也可混合使用,但是一个*的参数必须在前,**的在后
def test(*args):
print(args,type(args))

def test1(**args):
print(args,type(args))

def test2(*args,**kwargs):
print(args, type(args))
print(kwargs,type(kwargs))


test(11,22)
test1(n=33)
test2(n=55,n1=66)

#要想把l传入到args里需要前面加一个*,把k传入到kwargs里需要在参数前加两个*,否则则会把l,k当成两个参数转入到args中
l = [11,22,33,44]
k = {'n':33,'n1':'harson'}

test2(l,k)
test2(*l,**k)

#内置函数
#all序列的元素是否都为真
r = all([11,''])
print(r)

#r1序列的元素有一个为真则为真
r1 = any([22,''])
print(r1)

#bin转化为2进制
print(bin(10))

#bytearray转换为字节数组
print(bytearray(10))

#bytes转换为字节码
print(bytes(10))

#callable函数是否可被调用
fun = lambda a: a+1
l = []
c = callable(fun)
c1 = callable(l)
print(c)
print(c1)

#compile用于编译代码

#enumerate用于循环时给字段加下标
l = ['alex','harson','lee']
for i in l : print(i)

for i,item in enumerate(l,1) :print(i,item)

#eval把字符串转化成代码执行
str = '5*6'
e = eval(str)
print(e)

#map将序列中的每个元素进行操作,得到新的序列,元素个数不变
li = [11,22,33,44]
new_li = map(lambda x : x*6,li)
n = list(new_li)
print(n)


#filter将序列进行过滤得到新的序列
def fun(x) :
if x>22:
return True
else:
return False
_li = filter(fun,li)
_n = list(_li)
print(_n)

s1 = '456456asd'
s2 = s1.format()
print(s2)

# chr将数字转化为字符,ord将字符转化为数字
print(chr(99))
print(ord('A'))

#lambda表达式,用于简单函数的表示方式
#a表示传入的形式参数,a+1表示函数内容及return的返回值
fun = lambda a : a+1
ret = fun(5)
print(ret)

str ='%s is good' % ('alex')
print(str)

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

def mail(user):
ret = True
try:
msg = MIMEText('hello!', 'plain', 'utf-8')
msg['From'] = formataddr(["harson", 'lihasheng@126.com'])
msg['To'] = formataddr(["lee", '83968660@qq.com'])

msg['Subject'] = "主题"

server = smtplib.SMTP("smtp.126.com", 25)
server.login("lihasheng@126.com", "lhs529464")
server.sendmail('lihasheng@126.com', [user, ], msg.as_string())
server.quit()
except Exception:
ret = False
return ret

ret = mail('83968660@qq.com')
if ret :
print('发送成功')
else:
print('发送失败')

# f = open('test.log','w')
# f.write('hello world')
# f.close()

f = open('test.log','r',encoding='utf-8')
ret = f.read(1)
f.close()
print(ret)

#f.tell查看当前指针位置
#f.read指定读取字符
#f.seek指定当前指针位置
f = open('test.log','r')
print(f.tell())
f.read(1)
f.seek(1)
print(f.tell())
f.close()
 


posted @ 2019-05-08 17:03  HarsonLee  阅读(233)  评论(0编辑  收藏  举报