day17

  • 定义:

    包就是模块的一种形式,包的本质就是一个含有.py文件的文件夹

  • 为什么要用包:模块的第一个文件就只有十个功能,但是未来在扩展版本的时候,模块名和用法应该最好不要去修改,但是这只是对使用者友好,而由于版本扩展,文件越来越大,模块设计者对模块的管理、维护会越来越复杂,因此我们可以使用包来扩展模块的功能。

  • 导入包发生的三件事情:

  1. 创建一个包的名称空间
  2. 由于包是一个文件夹,无法执行包,因此执行包下的.py文件,将执行官过程中产生的名字存放于包名称空间中(即包名称空间中存放的名字都是来源于.py)
  3. 在当前执行文件中拿到一个名字aaa,aaa是指向包的名称空间的。
  • 导入包的两种方式:
  1. import...
  2. from...import...

  • 包是含有_init_.py的文件夹;导包就是导入_init_

​ 包一定是被当做模块文件导入的,模块文件m1.py/m2.py的搜索路径以执行文件 包的介绍.py路径为基准

​ 相对导入绝对导入:只能在包中内部使用

time模块

time模块:提供了三种不同类型的时间(时间戳),三种不同类型的时间可以互相转换

*****主要掌握

time.time()
time.sleep(1)

datetime模块

datetime模块:时间的加减

import datetime
now = datetime.datetime,now()
print(now)

默认3天
print(now + datetime.timedelta(3))

减3周
print(now - datetime.timedelta(weeks = 3))

random模块

随机数模块

  • 掌握

随机0-1之间的数

print(random.random())

[1-3]

print(random.randint(1,3))

  • 随机选择一个

lt = [1,2,3]

print(random.choice(lt))

  • 了解

print(random.sample([1,2,'a','c',2],2))

运行结果:

['c', 1]

hashlib模块和hmac模块

  • hashlib模块:对字符加密
  • hmac模块:对字符加密,并且加上密钥
import hashlib

m=hashlib.md5()
m.update(b'hfss1313')
print(m.hexdigest())

hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'

pwd_list = [
    'hfss1313',
    'hash1313',
    'hash94139413',
    'hash123456',
    '123456hash',
    'h123ash',
]

hash是一种算法

tying模块

与函数联用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型

  • 类型检查,防止运行时出现参数和返回值类型不符合
  • 作为开发文档附加说明,方便使用者调用时传入和返回参数类型
  • 该模块加入后并不会影响程序的运行,不会报正式的错误,只有提醒
  • 注意:tying模块只有在python3.5以上的版本中才可以使用,pycharm目前支持tying检查

tying常用类型

  • int、long、float:整型、长整型、浮点型
  • bool、str:布尔型,字符串类型
  • list、tuple、dict、set:列表、元组、字典、集合
  • iterable、iterator:可迭代类型、迭代器类型
  • generator:生成器类型

requests模块

爬虫——》爬取数据,模拟浏览器对url发送请求,拿到数据

re模块

re模块:去字符串 找符合某种特点的字符串

  • 贪婪模式

.(任意类型)*(0-无穷个)

  • 非贪婪模式

.(任意类型)*(0-无穷个)?(让他进入非贪婪模式)

  • 特殊构造

s='a123 aaaa a234 abc'

a(?=\d):a后面是数字,但是不要数字,不消耗字符串内容

匹配邮箱

s=#@#@#@nickchen121@163.com$$$$nick@qq.com$$#$#$[]]2287273393@162.com@$243lks'
# \w(字母/数字/下划线)+(0-无穷个)@ \w(字母/数字/下划线)+(0-无穷个).com
print(re.findall('\w+@\w+.com', s))

以下必须记住的:

  • 贪婪.(任意字符)*(0-无穷个)
s = 'abcd abcdkkkkkksb abcd '
print(re.findall('a.*k',s))
运行结果:['abcd abcdkkkkkk']
  • 非贪婪:.*?
s = 'abckkkkkkkkkkkkkkkb '
print(re.findall('a.*?b',s))
运行结果:['ab']
  • findall:不匹配换行符
s = 'abcd abcd*asb abcd '
print(re.findall('abc.abc',s))
print(re.findall('abcd.abcd',s,re.S))
运行结果:
[]
['abcd abcd']
  • re.S:修饰符,匹配换行符
s = 'abcd abcd*asb abcd '
print(re.findall('abcd.abcd',s,re.S))
运行结果:['abcd abcd']
  • match:从头开始找,找到就停止查找;找不到就报错
s = 'abcd abc abcd '
res = re.match('abcd*',s)
print(res.group())
运行结果:abcd
  • search:从字符串中找,找到就不找了
s = 'abcd abc abcd '
res = re.search('abc',s)
print(res.group())
运行结果:abc
  • 分组:只要括号里面的
s = 'abcd aaaa abcd '
print(re.findall('a(.)c(d)',s))
运行结果:[('b', 'd'), ('b', 'd')]
  • 有名分组:给分组加名字
s = 'abcd aaaa abcd '
print(re.search('a(?P<name1>.)c(?P<name2>d)', s).groupdict())
运行结果:{'name1': 'b', 'name2': 'd'}

了解:

元字符

特殊构造的元字符

特殊修饰符

函数

转载于:https://www.cnblogs.com/gfhh/p/11604488.html

Day17 中,我们可以通过 Flask 框架快速搭建一个 BBS 论坛。具体步骤如下: 1. 创建 Flask 应用 ```python from flask import Flask app = Flask(__name__) ``` 2. 创建数据库 ```python from flask_sqlalchemy import SQLAlchemy app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bbs.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) ``` 3. 创建数据库模型 ```python class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(20), nullable=False) class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) ``` 4. 创建路由和视图函数 ```python @app.route('/') def index(): posts = Post.query.all() return render_template('index.html', posts=posts) @app.route('/post/<int:post_id>') def post(post_id): post = Post.query.get(post_id) return render_template('post.html', post=post) @app.route('/new_post', methods=['GET', 'POST']) def new_post(): if request.method == 'POST': title = request.form['title'] content = request.form['content'] user_id = 1 # 假设当前用户为 id 为 1 的用户 post = Post(title=title, content=content, user_id=user_id) db.session.add(post) db.session.commit() return redirect('/') return render_template('new_post.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] user = User.query.filter_by(username=username, password=password).first() if user: session['user_id'] = user.id return redirect('/') else: flash('用户名或密码错误') return render_template('login.html') @app.route('/logout') def logout(): session.pop('user_id', None) return redirect('/') ``` 5. 创建 HTML 模板 创建 index.html、post.html、new_post.html、login.html 四个模板文件,并且使用 jinja2 模板引擎渲染数据。 6. 运行应用 ```python if __name__ == '__main__': app.run() ``` 以上就是快速搭建 BBS 论坛的主要步骤,当然在实际应用中还需要考虑更多细节问题,比如用户认证、数据校验、页面美化等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值