包、编程思想、软件开发目录规范、常用内置模块

包的具体使用

包的使用

	一个模块就是py文件,再python里为了对模块分类管理,就需要划分不同的文件夹。
	多个有联系的模块可以将其放到同一个文件夹下,为了称呼方便,一般把python里的一个代码文件夹称为一个包

导入包的方式

'''
在python2中文件夹内必须要有__init__.py文件才会识别为包.
而python3中没有严格要求,但是为了兼容性考虑最好还是加上__init__.py
'''
1.直接 import xxx方式导入指定模块
2.使用 from xxx import xxx 方式导入指定模块
	(1)from xxx import 000:将xxx里面的000调用过来
    (2)from xxx import * :将xxx里面所有的函数加载到内存,即都调用过来

编程思想转变

小白阶段>>>函数阶段>>>模块阶段
按需求从上往下堆叠代码>>>根据不同的功能将代码封装成不同的函数>>>根据不同的功能该分成不同的模块文件
单文件>>>多文件
'''一切都是为了能高效便捷地进行资源管理'''

软件开发目录规范

文件及目录的名字可以变换 但是思想是不变的 分类管理
目录规范主要规定开发程序的过程中针对不同的文件功能需要做不同的分类
1.bin文件夹
	用于存储程序的启动文件start.py
2.conf文件夹configuration
	用于存储程序的配置文件settings.py
3.core文件夹
	用于存储程序的核心逻辑src.py(source)
4.lib文件夹library
	用于存储程序的公共功能 common.py
5.db文件夹database
	用于存储程序的数据文件 user info.txt
6.log文件
	用于存储程序的日志文件log.log
7.interface文件夹(核心的延伸)
	用于存储程序的接口文件user.py order.py goods.py
8.readme文件(文本文件)
	用于编写程序的说明 介绍 广告 类似于产品说明书
9.requirements.txt文件
	用于存储程序所需的第三方模块名称和版本

常用内置模块

collection模块

1.namedtuple 命名元组
	from collections import namedtuple
    Point = namedtuple('二维坐标系',['x', 'y'])
    res1 = Point(1, 3)
    res2 = Point(10, 49)
    print(res1, res2)
    print(res1.x)	# 还可以通过点的方式直接获取单独的数值
    print(res1.y)
    
Point = nametuple('三维坐标系', 'x y z')#还可以不用列表,通过字符串空格隔开表示几个数据
res1 = Point(1, 3, 44)
res2 = Point(10, 49, 55)
print(res1, res2)
2.队列
	队列于堆栈
    	队列:先进先出
        堆栈:先进后出
    队列和堆栈都是一边只能及一边只能出

时间模块

import time
'''
三种时间表现形式
	1.时间戳
		秒数
	2.结构化时间
		主要是给计算机看的
	3.格式化时间
		主要是给人看的
'''
print(time.time())	# 1666150097.5481427
print(time.localtime())	# time.struct_time(tm_year=2022, tm_mon=10,tm_mday=19, tm_hour=11,tm_min=32, tm_sec=50, tm_wday=2, tm_yday=292, tm_isdst=0)
print(time.strftime('%Y-%m-%d'))	# 2022-10-19
print(time.strftime('%Y/%m/%d'))	# 2022/10/19
print(time.strftime('%Y/%m/%d %H:%M:%S'))	# 2022/10/19 15:53:23
print(time.strftime('%Y/%m/%d %x'))	# 2022/10/19 15:55:43
time.sleep(10)	# 让程序原地阻塞指定的秒数


import datetime
print(datetime.datetime.now())	# 2022-10-19 15:59:18.768551
print(datetime.date.today())	# 2022-10-19 15:59:18.768551
'''
datetime 年月日 时分秒
date	 年月日
time	 时分秒
'''
from datetime import date, datetime
print(date.today())
print(datetime.today())
print(datetime.utcnow())
import datetime	# c = datetime.datetime(2017, 5, 23, 12, 20 )
print('指定日期:'c)	# 指定日期:	2017-05-23 12:20:00
from datetime import datetime
d=datetime.strptime('2017/9/30','%Y/%m/%d')
print(d)	# 2017-09-30 00:00:00
e=datetime.strptime('2017年9月30日星期六','%Y年%m月%d日星期六')
print(e)
f=datetime.strptime('2017年9月30日星期六8时42分24秒','%Y年%m月%d欸星期六%H时%M分%S秒')
print(f)
import datetime
ctime = datetime.date.today()
print(ctime)
time_del = datetime.timedelta(days=3)
print(ctime + time_del)

ctime = datetime.datetime.today()
print(ctime)
time_del = datetime.timedelta(minutes=20)
print(ctime + time_del)

随机数模块

首先引入随机数模块 import random
print(random.random())	# 随机产生0到1之间的小数
print(random.randint(1, 10))	# 随机产生1到10之间的整数

img

print(random.randrnage(1, 10, 2))	# (开始整数,结束整数,步长)会在[1, 3, 5, 7, 9]中随机抽取

img

print(random.choice(['一等奖', '二等奖', '三等奖', '谢谢惠顾']))	# 随机抽取一个样本
print(random.choices(['一等奖', '二等奖', '三等奖', '谢谢惠顾']))	# 随机抽取一个样本

image-20221019164312223.png

print(random.sample(['jason', 'kevin', 'tony', 'oscar', 'jerry', 'tom'], 2))	# 随机抽指定样本数

image-20221019164711218.png

l1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
random.shuffle(l1)	# 随机打乱数据集
print(l1)

image-20221019165227245.png

def get_code(n):
    code = ''
    for i in range(n):
        # 1.先产生随机的大小写字母,数字
        random_upper = chr(random.randint(65, 90))
        random_lower = chr(random.randint(97, 122))
        random_int = str(random.randint(0,9))
        # 2.随机三选一
        temp = random.choice([random_upper,random_lower, random_int])
        code += temp
    return code

res = get_code(10)
print(res)
res = get_code(4)
print(res)

image-20221019170216072.png

posted @ 2022-10-19 17:11  吴仁耀  阅读(40)  评论(0)    收藏  举报