今日内容回顾7.14

绝对导入与相对导入

只要涉及到模块导入,那么sys.path永远以执行文件为准。

  • 绝对导入

    其实就是以执行文件所在在sys.path为起始位置,一层层查找导入。

    使用from句式

    from ccc import b

    from ccc.ddd.eee import b

  • 相对导入

    储备知识

    . 在路径中的意思是,当前路径
    .. 在路径中的意思是,上一层路径
    ../.. 在路径中的意思是,上上一层路径

    相对导入可以不参考执行文件所在的路径,直接以当前模块文件路径为准

    只能在模块文件中使用,不能再执行文件中使用

    相对导入再项目比较复杂的情况下可能会出错,尽量少用推进使用绝对导入。

包的概念

  • 什么是包

    包就是内部含有 __ init __ .py文件的文件夹,也可以简单的理解为它就是一个文件夹

  • 包的作用

    内部存放多个模块文件,仅仅是为了更方便的管理模块文件

  • 具体使用

    import 包名

    导入报名其实就是导入里面的 __ init __ .py文件,其内部有哪些模块你就能用哪些模块,也可以跨过 __ init __ .py文件直接导入包里面的其他模块文件。

    python2版本 文件夹下必须有 __ init __ .py才能被当作包
    python3版本 文件夹下有没有 __ init __ .py文件都是包

编程思想的进阶

  1. 小白阶段

    按照需求从上往下堆叠代码(面条版)

  2. 函数阶段

    将代码按照功能的不同封装成不同的函数

  3. 模块阶段

    根据功能的不同拆分不同的模块文件

  • 目的就是为了更加方便快捷的管理资源。

软件开发目录规范

模块阶段,拆分不同模块的文件还需要有文件夹,有了多个文件夹

我们所使用的所有的程序目录都要有一定的规范

文件夹名称 作用
bin 存储程序的启动文件 start.py
conf 存储程序的配置文件 settings.py
core 存储程序的核心逻辑 src.py
lib 存储程序的公共功能 common.py
db 存储程序的数据文件 useribfo.txt
log 存储程序的日志文件 log.log
interface 存储程序的接口文件 user.py、order.py、goods.py
readme 编写程序的说明、介绍、广告等
requirements.txt 存储程序所需要的第三方模块和版本

我们在编写软件的时候 可以不完全遵循上面的文件名
start.py可以放在bin文件夹下也可以直接放在项目根目录
db文件夹等我们学到真正的项目会被数据库软件替代
log文件夹等我们学到真正的项目会被专门的日志服务替代

常用内置模块

collections模块
  • 具名元组 from collections import namedtuple

    from collections import namedtuple  
    point = namedtuple('二维坐标系',['x','y'])
     res = point(20,3)
     print(point)  # 二维坐标系(x=20, y=3)
    
  • 双端队列 from collections import deque

    from collections import deque 
    q = deque(['a','l','s'])
    print(q)
    q.append('x')
    q.appendleft('a')
    print(q)
    q.pop()
    q.popleft()
    print(q)
    
  • 有序字典 from collections import OrderedDict

    from collections import OrderedDict 
    od = OrderedDict([('name','jason'),('age',18),('salary',3000.1)])
    print(od)
    
  • 默认值字典 from collections import defaultdict

    from collections import defaultdict  
    d = defaultdict(k1=[],k2=[])
    print(d)  # # defaultdict(None, {'k1': [], 'k2': []})
    
    
  • 统计数据种字符的个数 from collections import Counter

    from collections import Counter 
    a = 'asdfgasdfasdasaasdfg'
    print(Counter(a))
    #Counter({'a': 6, 's': 5, 'd': 4, 'f': 3, 'g': 2})
    
    
time模块,时间模块
  • 表达时间的三种模式

    1. 时间戳 time.time

      计算1970年一月一号零时零分零秒到当前时间,以秒为单位。

      time_stamp= time.time()
      print(time_stamp)
      # 1657795246.6333282
      
    2. 结构化时间

      2.1以UTC时间为准计算当前的时间 time.gmtime

      structured_time = time.gmtime()
      print(structured_time)
      """
      time.struct_time(tm_year=2022, tm_mon=7, tm_mday=14,
      tm_hour=10, tm_min=43, tm_sec=53,
      tm_wday=3, tm_yday=195, tm_isdst=0)
      

      2.2以本地时间为准计算当前时间 time.localtime

      local_time = time.localtime()
      print(local_time)
      """
      time.struct_time(tm_year=2022, tm_mon=7, tm_mday=14, 
      tm_hour=19, tm_min=1, tm_sec=41, 
      tm_wday=3, tm_yday=195, tm_isdst=0)
      
      """
      
    3. 格式化时间 time.strftime

      以个格式化的方式计算当前时间

      format_time = time.strftime('%Y-%m-%d %H:%M:%S')
      print(format_time)  # 2022-07-14 18:50:04
      
  • 结构化时间转时间戳 time.mktime

    change = time.mktime(local_time)
    print(change)  # 1657796654.0
    
  • 格式化时间转结构化 time.strptime

    change3 = time.strptime('2022-07-14 18:50:04','%Y-%m-%d %H:%M:%S')
    print(change3)
    """
    time.struct_time(tm_year=2022, tm_mon=7, tm_mday=14, 
    tm_hour=18, tm_min=50, tm_sec=4,
     tm_wday=3, tm_yday=195, tm_isdst=-1)
    """
    
posted @ 2022-07-14 23:27  瓮小辉  阅读(25)  评论(0)    收藏  举报