1.反射机制
就是通过字符串操作对象/模块中的字段 —> 使用反射机制
反射的使用场景:
例如: 在一个模块中有多个不同的行为 使用者输入不同的行为名字 根据对应的名字指定对应的行为
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "Person name:%s age:%d" % (self.name, self.age)
__repr__ = __str__
from reflect.person import Person
def main():
p = Person("小沐", 18)
# 获取小沐这个值
print(p.name)
# p.__dict__对象调用 获取的一个字典 字典中的内容是:属性名:属性值,属性名:属性值
res = p.__dict__
print(res) # {'name': '小沐', 'age': 18}
# 给你一个字符串 "name" ---> 获取小沐这个值
value = getattr(p,"name")
print(value)
# 设置
setattr(p, "age", 19)
print(p)
# 判断是否有该属性
res = hasattr(p, "names")
print(res)
if res:
value = getattr(p, "names")
else:
print("没有该属性")
# 删除对象的属性字段
delattr(p, "age")
print(p)
if __name__ == '__main__':
main()
2.邮件的发送
邮件传输协议:SMTP
邮件发送的时候程序会使用SMTP协议来发送电子邮件到接收者邮件服务器
SMTP协议对应的端口号是25
如果想使用SMTP发送邮件的话 需要查看发件人是否开启了SMTP的协议
借助于163邮箱来进行测验
---> 163邮箱的网站
---> 登录进去 ---> 设置 ---> POP3/SMTP/... ---> 客户端授权码 ---> 开启
注意:授权码一定要记住 发送邮件时 登录邮箱需要的授权码
smtp协议所在的模块:smtplib
邮件信息相关的模块:email
发送纯文本
准备工作:
邮件服务器: "smtp.163.com"
端口号: 25
发件人的用户名[必须开启了SMTP协议]:
授权码: abc123
接收人: ["","",""]
邮件内容: 发送纯文本
邮件主题:
import smtplib
from email.mime.text import MIMEText # MIMEText 声明文本信息对应的类
if __name__ == '__main__':
# 服务器地址
server_addr = "smtp.163.com"
# 端口号
port = 25
# 发件人
sender = "[email protected]"
# 授权码
psw = "abc123"
#接受者
receivers = ["[email protected]", "[email protected]", "[email protected]"]
# 内容
content = "人生苦短, 我用Python"
# 主题
title = "Python"
# 填装信息
message = MIMEText(content, "plain", "utf-8")
message["From"] = sender
# 接受者这一块设置字符串数据 如果有多个收件人的话 每个收件人之间使用逗号隔开
message["To"] = ",".join(receivers)
message["Subject"] = title
# 使用smtp协议将邮件进行发送
smtpobj = smtplib.SMTP(server_addr, port)
# 进行登录验证
smtpobj.login(sender, psw)
smtpobj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
发送带有附件的
也会发送文本 MimeText
也会附件 MiMEApplacation
不许可进行拼接的内容进行发送 MimeMultipart
附件发送的是文件的内容
在邮箱中生成一个虚拟文件(需要传递一个文件 为虚拟文件起名) 来存放发送的文件内容
import smtplib
from email.mime.text import MIMEText # 写文本的
from email.mime.application import MIMEApplication # 设置附件
from email.mime.multipart import MIMEMultipart # 拼接内容的
import os
if __name__ == '__main__':
# 服务器地址
server_addr = "smtp.qq.com"
# 协议端口号
port = 25
# 发送者
sender = "[email protected]"
# 授权码
psw = "pcpoxyjdxkmngfgb"
# 接受者
receivers = ["[email protected]", "[email protected]", "[email protected]"]
# 先设置可拼接的内容
multipart = MIMEMultipart()
multipart["From"] = sender
multipart["To"] = ",".join(receivers)
multipart["Subject"] = "今天是个好日子"
content = "阳光明媚, 万里无云"
text = MIMEText(content, "plain", "utf-8")
# 拼接到要发送的可变部分中
multipart.attach(text)
# 设置附件 参数中存放的是文件中的字节数据
with open("send_text.py", "rb") as handle:
pypart = MIMEApplication(handle.read())
pypart.add_header('content-disposition', 'attachment', filename='send.py')
multipart.attach(pypart)
# 设置附件 参数中存放的是文件中的字节数据
path = r"C:\Users\11143\Desktop\男.jpg"
with open(path, "rb") as handle:
imgpart = MIMEApplication(handle.read())
imgpart.add_header('content-disposition', 'attachment', filename=os.path.basename(path))
multipart.attach(imgpart)
# 设置服务器
smptobj = smtplib.SMTP_SSL(server_addr, 465)
# 进行登录
smptobj.login(sender, psw)
# 发送
smptobj.sendmail(sender, receivers, multipart.as_string())
print("发送成功")
3.海龟画图
是turtle模块 在坐标系上 借助于海龟形状的画笔 在画布上运动 将其运动轨迹显示在画布
坐标系: 原点是在画布的中心点
画图像:
import turtle
#1. 生成一个画布
screen = turtle.Screen()
#2. 设置画布的大小
screen.setup(0.9, 0.8)
#3. 设置画布的背景颜色
screen.bgcolor("orange")
screen.bgcolor(0.8, 0.5, 0.7)
# 画笔
pen = turtle.Turtle()
# 设置画笔的形状
# 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.
pen.shape("turtle")
# 设置画笔的颜色
# 第一个是线的颜色 第二个是乌龟壳的颜色
pen.color("red", "yellow")
# 设置线宽
pen.pensize(5)
# 移动的速度
pen.speed(1)
# 向前行走100px 按照乌龟头部方向向前走100
pen.forward(100)
# 向左旋转多少度 乌龟头部向左旋转
pen.left(90)
# 向右旋转
pen.right(45)
# 更改线的颜色
pen.pencolor("green")
# 向后行走100px 按照乌龟头部方向后走100
pen.backward(100)
# 移动画笔的位置
# 抬笔
pen.penup()
# 指定画笔的位置
pen.goto(-100, 300)
# 落笔
pen.pendown()
# 全圆
pen.circle(20)
# extend ---> 画的圆弧对应的角度
pen.circle(50, 90)
# 填充颜色
pen.fillcolor("")
# 开始填充
pen.begin_fill()
# 结束填充
pen.end_fill()
pen.circle(50, steps=6)
# 隐藏画笔
pen.hideturtle()
# 显示画笔
pen.showturtle()
# 清除画布内容
pen.clear()
# 写文本
# normal bold italic
pen.write("hello World", True, "left", ("楷体", 20, "italic"))
# 事件 点击屏幕任意位置再结束
screen.exitonclick()