Modules & Packages
Module concept: In the development of computer programs, as the program code more written, the code in a file will be more and more long, maintenance is more and more difficult. In order to write maintainable code, we grouped many functions into separate files, and many languages used this organization code, in Python a. py file is called a module.
Benefits of using modules: greatly improve the maintainability of your code. Second, writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.
Therefore, there are three types of modules:
- Python Standard library
- Third-party modules
- Application Custom Modules
In addition, the use of modules can also avoid conflicting function names and variable names. Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name.
Module Import Method 1 import statement
import module1[, module2[,... moduleN]
When we use the import statement, how does the Python interpreter find the corresponding file? The answer is that the interpreter has its own search path, which exists in the Sys.path.
[‘‘, ‘/usr/lib/python3.4‘, ‘/usr/lib/python3.4/plat-x86_64-linux-gnu‘, ‘/usr/lib/python3.4/lib-dynload‘, ‘/usr/local/lib/python3.4/dist-packages‘, ‘/usr/lib/python3/dist-packages‘] ``` 因此若像我一样在当前目录下存在与要引入模块同名的文件,就会把要引入的模块屏蔽掉。### 2 from…import 语句
From ModName import name1[, name2[, ... Namen]]
这个声明不会把整个modulename模块导入到当前的命名空间中,只会将它里面的name1或name2单个引入到执行这个声明的模块的全局符号表。### 3 From…import* 语句
From ModName Import *
这提供了一个简单的方法来导入一个模块中的所有项目。然而这种声明不该被过多地使用。大多数情况, Python程序员不使用这种方法,因为引入的其它来源的命名,很可能覆盖了已有的定义。### 4 运行本质
#1 Import Test
#2 from test Import add
```
Whether 1 or 2, first find test.py through Sys.path, then execute the test script (all execution), the difference is that 1 will load the variable name of test into the namespace, and 2 will only load the add variable name in.
OS Module
An OS module is an interface that interacts with the operating system
OS.GETCWD () Gets the current working directory, that is, the directory path of the current Python script work os.chdir ("DirName") changes the current script working directory, equivalent to the shell Cdos.curdir return the current directory: ('. ') Os.pardir Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' dirname1/dirname2 ') can generate a multi-level recursive directory Os.removedirs (' dirname1 ') if the directory is empty, then deleted, and recursively to the previous level of the directory, if also empty, then delete, and so on Os.mkdir (' DirName ') to generate a single-level directory, equivalent to the shell mkdir dirnameos.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir Dirnameos.listdir (' dirname ') lists all files and subdirectories under the specified directory, including hidden files, and prints os.remove () Delete a file Os.rename ("Oldname", "newname") Rename File/directory Os.stat (' Path/filename ') Get File/directory information OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/" OS.LINESEP output the current platform using the line terminator, win under "\t\n", Linux "\ N "os.pathsep output is used to split the file path of the string win under;, Linux: The Os.name output string indicates the current use of the platform. Win-> ' NT '; Linux-> ' POSIX ' Os.system ("Bash command") runs a shell command that directly displays the Os.environ get system environment variable Os.path.abspath (PATH) Return path normalized absolute path Os.path.split (path) splits path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) OS. path.exists (path) If path exists, returns true if path does not exist, returns Falseos.path.isabs (path) If path is an absolute path, return Trueos.path.isfile (PATH) Returns true if path is a file that exists. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists. Otherwise return Falseos.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored Os.path.getatime (path) returns the last access time of the file or directory to which path is pointing os.path.getmtime (path) Returns the last modified time of the file or directory to which path is pointing
SYS module
sys.argv 命令行参数List,第一个元素是程序本身路径sys.exit(n) 退出程序,正常退出时exit(0)sys.version 获取Python解释程序的版本信息sys.maxint 最大的Int值sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值sys.platform 返回操作系统平台名称
Small instance: progress bar
import sys,timefor i in range(10): sys.stdout.write(‘#‘) time.sleep(1) sys.stdout.flush()
JSON module
Before we learned to use the eval built-in method to turn a string into a Python object, however, the Eval method is limited and can be used for normal data types, json.loads and eval, but when it comes to special types, eval doesn't work. So the focus of eval is usually to execute a string expression and return the value of the expression.
import jsonx="[null,true,false,1]"print(eval(x))print(json.loads(x))
What is serialization?
The process of changing an object (variable) from memory to a storage or transfer is called serialization, and in Python it is called pickling, which is also called serialization,marshalling,flattening in other languages, and so on.
After serialization, the serialized content can be written to disk or transferred over the network to another machine.
In turn, re-reading the variable contents from the serialized object into memory is called deserialization, i.e. unpickling.
If we are going to pass objects between different programming languages, we have to serialize the object into a standard format, such as XML, but the better way is to serialize it to JSON, because JSON represents a string that can be read by all languages, easily stored to disk, or transmitted over a network. JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient.
JSON represents objects that are standard JavaScript language objects, and JSON and Python have built-in data types that correspond to the following:
#----------------------------序列化import jsondic={‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘}print(type(dic))#<class ‘dict‘>j=json.dumps(dic)print(type(j))#<class ‘str‘>f=open(‘序列化对象‘,‘w‘)f.write(j) #-------------------等价于json.dump(dic,f)f.close()#-----------------------------反序列化import jsonf=open(‘序列化对象‘)data=json.loads(f.read())# 等价于data=json.load(f)
It is important to note that:
import json#dct="{‘1‘:111}"#json 不认单引号#dct=str({"1":111})#报错,因为生成的数据还是单引号:{‘one‘: 1}dct=‘{"1":"111"}‘print(json.loads(dct))#conclusion:# 无论数据是怎样创建的,只要满足json格式,就可以json.loads出来,不一定非要dumps的数据才能loads
Pickle Module
##----------------------------序列化import pickledic={‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘}print(type(dic))#<class ‘dict‘>j=pickle.dumps(dic)print(type(j))#<class ‘bytes‘>f=open(‘序列化对象_pickle‘,‘wb‘)#注意是w是写入str,wb是写入bytes,j是‘bytes‘f.write(j) #-------------------等价于pickle.dump(dic,f)f.close()#-------------------------反序列化import picklef=open(‘序列化对象_pickle‘,‘rb‘)data=pickle.loads(f.read())# 等价于data=pickle.load(f)print(data[‘age‘])
The problem with Pickle is the same as for all other programming language-specific serialization problems, that is, it can only be used in Python, and may be incompatible with each other in Python, so it's okay to save only those unimportant data with pickle and not successfully deserialize it.
Shelve module
The shelve module is simpler than the Pickle module, with only one open function, which returns a dictionary-like object, readable and writable; key must be a string, and the value can be a data type supported by Python. Know
import shelvef = shelve.open(r‘shelve.txt‘)# f[‘stu1_info‘]={‘name‘:‘alex‘,‘age‘:‘18‘}# f[‘stu2_info‘]={‘name‘:‘alvin‘,‘age‘:‘20‘}# f[‘school_info‘]={‘website‘:‘oldboyedu.com‘,‘city‘:‘beijing‘}### f.close()print(f.get(‘stu_info‘)[‘age‘])
XML module
XML is a protocol that implements data exchange between different languages or programs, much like JSON, but JSON is simpler to use, but in ancient times, in the Dark Ages when JSON was not yet born, you could only choose to use XML, and so far many traditional companies, such as the financial industry, are mostly XML-like interfaces.
The format of XML is as follows: by the <> node to distinguish the data structure.
<?xml version="1.0"?><data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country></data>
XML protocols are supported in each language, and in Python you can manipulate XML with the following modules:
import xml.etree.ElementTree as ETtree = ET.parse("xmltest.xml")root = tree.getroot()print(root.tag)#遍历xml文档for child in root: print(child.tag, child.attrib) for i in child: print(i.tag,i.text)#只遍历year 节点for node in root.iter(‘year‘): print(node.tag,node.text)#---------------------------------------import xml.etree.ElementTree as ETtree = ET.parse("xmltest.xml")root = tree.getroot()#修改for node in root.iter(‘year‘): new_year = int(node.text) + 1 node.text = str(new_year) node.set("updated","yes")tree.write("xmltest.xml")#删除nodefor country in root.findall(‘country‘): rank = int(country.find(‘rank‘).text) if rank > 50: root.remove(country)tree.write(‘output.xml‘)
Create your own XML document:
import xml.etree.ElementTree as ETnew_xml = ET.Element("namelist")name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})age = ET.SubElement(name,"age",attrib={"checked":"no"})sex = ET.SubElement(name,"sex")sex.text = ‘33‘name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})age = ET.SubElement(name2,"age")age.text = ‘19‘et = ET.ElementTree(new_xml) #生成文档对象et.write("test.xml", encoding="utf-8",xml_declaration=True)ET.dump(new_xml) #打印生成的格式
Re module
import reret=re.findall(‘a..in‘,‘helloalvin‘)print(ret)#[‘alvin‘]ret=re.findall(‘^a...n‘,‘alvinhelloawwwn‘)print(ret)#[‘alvin‘]ret=re.findall(‘a...n$‘,‘alvinhelloawwwn‘)print(ret)#[‘awwwn‘]ret=re.findall(‘a...n$‘,‘alvinhelloawwwn‘)print(ret)#[‘awwwn‘]ret=re.findall(‘abc*‘,‘abcccc‘)#贪婪匹配[0,+oo] print(ret)#[‘abcccc‘]ret=re.findall(‘abc+‘,‘abccc‘)#[1,+oo]print(ret)#[‘abccc‘]ret=re.findall(‘abc?‘,‘abccc‘)#[0,1]print(ret)#[‘abc‘]ret=re.findall(‘abc{1,4}‘,‘abccc‘)print(ret)#[‘abccc‘] 贪婪匹配
Note: The previous *,+,?, etc. are greedy matches, that is, match as much as possible, followed by the number to make it an inert match
ret=re.findall(‘abc*?‘,‘abcccccc‘)print(ret)#[‘ab‘]
Python Basics Five: Modules