0


【python】模块制作及嵌套的包

🌍个人简介

  1. 🍁作者简介:大家好,我是姐姐划船吗?运维领域创作者,🏅阿里云ACE认证高级工程师🏅
  2. ✒️个人主页:姐姐划船吗?🔥
  3. 🕺支持我:点赞👍+收藏⭐️+留言📝
  4. 🫀格言:你未必出类拔萃,但一定与众不同!🔥

🧊系列专栏:
🎒 阶段一:windows基础
🎒 阶段二:Linux基础知识
🎒 阶段三:shell基础+shell高级
🎒 阶段四:学会python,逆天改命
🎒 阶段五:Linux网络服务
🎒 阶段六:集群原理及架构
🎒 阶段七:云计算虚拟化技术

学习目标:

  1. ✏️ 学习模块的制作
  2. ✏️ 了解 什么是嵌套的包
  3. ✏️ 模块知识扩展

1.模块制作

1.1定义自己的模块

在Python中,每个Python文件都可以作为一个模块,模块的名字就是文件的名字。比如有这样一个文件test.py,在test.py中定义了函数add。

调用自己的模块

参考上一篇文章

使用__name__测试模块

参考上一篇文章

1.2Python中的包

Python中的包

包将有联系的模块组织在一起,即放到同一个文件夹下,并且在这个文件夹创建一个名字为__init__.py 文件,那么这个文件夹就称之为包。

__all__在包中的作用:

在__init__.py文件中,定义一个__all__变量,它控制着 from 包名 import *时导入的模块

可以在__init__.py文件中编写内容

[root@localhost ~]# mkdir msg

[root@localhost ~]# cd msg/

[root@localhost msg]# vim sendmsg.py

def sendmsg():

  1. print("正在发送短信...")
  2. print("已经成功发送...")

[root@localhost msg]# vim recvmsg.py

def recvmsg():

  1. print("接收到一条短信.....")

[root@localhost msg]# tree

.

├── recvmsg.py

└── sendmsg.py

0 directories, 2 files

[root@localhost msg]# ipython

In [1]: import msg.sendmsg

In [2]: msg.sendmsg.sendmsg()

正在发送短信...

已经成功发送...

In [3]: import msg.recvmsg

In [4]: msg.recvmsg.recvmsg()

接收到一条短信.....

In [5]: import msg.recvmsg as re

In [6]: re.recvmsg()

接收到一条短信.....

In [1]: from msg import sendmsg,recvmsg

In [2]: sendmsg.sendmsg()

正在发送短信...

已经成功发送...

In [3]: from msg.sendmsg import sendmsg

In [4]: sendmsg()

正在发送短信...

已经成功发送...

In [5]: from msg import *

In [6]: sendmsg.sendmsg()

正在发送短信...

已经成功发送...

[root@localhost msg]# vim init.py

all=['sendmsg','recvmsg']

[root@localhost ~]# ipython

In [1]: from msg import *

In [2]: sendmsg.sendmsg()

正在发送短信...

已经成功发送...

2.嵌套的包

2.1 目录结构

假定我们的包的例子有如下的目录结构:

Phone/

  1. __init__.py
  2. common_util.py
  3. Voicedta/
  4. __init__.py
  5. Pots.py
  6. Isdn.py
  7. Fax/
  8. __init__.py
  9. G3.py
  10. Mobile/
  11. __init__.py
  12. Analog.py
  13. igital.py
  14. Pager/
  15. __init__.py
  16. Numeric.py

2.2导入子包和使用模块

Phone 是最顶层的包,Voicedta 等是它的子包。 我们可以这样导入子包:

import Phone.Mobile.Analog

Phone.Mobile.Analog.dial()

2.3也可使用 from xxx import xxx 实现不同需求的导入

第一种方法是只导入顶层的子包,然后使用属性/点操作符向下引用子包树:

from Phone import Mobile

Mobile.Analog.dial('555-1212')

此外,我们可以还引用更多的子包:

from Phone.Mobile import Analog

Analog.dial('555-1212')

事实上,你可以一直沿子包的树状结构导入:

from Phone.Mobile.Analog import dial

dial('555-1212')

在我们上边的目录结构中,我们可以发现很多的 init.py 文件。这些是初始化模块,from-import 语句导入子包时需要用到它。 如果没有用到,他们可以是空文件。

**2.4包同样支持 from xxx import * **

包同样支持 from-import all 语句:

from package.module import *

然而,这样的语句会导入哪些文件取决于操作系统的文件系统。所以我们在__init__.py 中加入 all 变量。该变量包含执行这样的语句时应该导入的模块的名字。它由一个模块名、字符串列表组成。

3.模块知识扩展

常用模块简介

Python有一套很有用的标准库。标准库会随着Python解释器,一起安装在你的电脑里。它是Python的一个组成部分。这些标准库是Python为你准备好的利器可以让编程事半功倍

3.1常用标准库

标准库

说明

builtins

内建函数默认加载

os

操作系统接口

sys

python自身的运行环境

functools

常用的工具

json

编码和解码json对象

logging

记录日志、调试

multiprocessing

多进程

threading

多进程

copy

拷贝

import json

'''

a = ["aa","bb","cc"]

f = open("test.txt","w")

f.write(str(a))

f.close()

'''

f = open("test.txt","r")

result = f.read()

#print(list(result))

print(type(result))

In [4]: import sendmsg

In [5]: sendmsg.test2()

----sendmsg---test2----

In [6]: import hashlib

In [7]: m=hashlib.md5()

In [8]: s="123456"

In [9]: m.update(s.encode("utf-8"))

In [10]: m.hexdigest()

Out[10]: 'e10adc3949ba59abbe56e057f20f883e'

3.2应用案例,注册和登录

import hashlib

class LoginSys(object):

  1. #加密方法
  2. def setMd5(self,password):
  3. m = hashlib.md5()
  4. m.update(password.encode("utf-8"))
  5. return m.hexdigest()
  6. def main(self):
  7. f = open("password.txt","a+")
  8. f.seek(0,0)
  9. content = f.read()
  10. f.close()
  11. if len(content) <=0:
  12. #注册
  13. print("请根据提示进行注册")
  14. name = input("请输入您要注册的用户名:")
  15. password = input("请输入您要注册的密码:")
  16. fw = open("password.txt","w")
  17. fw.write(name)
  18. fw.write("\n")
  19. #加密
  20. fw.write(self.setMd5(password))
  21. fw.close()
  22. else:
  23. #登录
  24. print("请根据提示进行登录!")
  25. name = input("请输入您的账号:")
  26. password = input("请输入您的密码:")
  27. f = open("password.txt","a+")
  28. f.seek(0,0)
  29. nameSave = f.readline()
  30. passwordSave = f.readline()
  31. password = self.setMd5(password)
  32. a = nameSave.split()[0]
  33. print("从文件中读取过来的账号是:",a)
  34. if name == a:
  35. if password == passwordSave:
  36. print("欢迎%s登录本系统!"%name)
  37. else:
  38. print("密码错误!")
  39. else:
  40. print("账号错误!")

l = LoginSys()

l.main()

👑👑👑结束语👑👑👑

实习渠道哪家强,中国北京找优加!
只要实习相关,找我们,给你一套适合你的解决方案,从简历制作、面试题库、行业资源、模拟面试到大厂资源。

标签: 运维 linux python

本文转载自: https://blog.csdn.net/qq_62294245/article/details/124825135
版权归原作者 姐姐划船吗? 所有, 如有侵权,请联系我们删除。

“【python】模块制作及嵌套的包”的评论:

还没有评论