python learning modules - Module (E)

5.10 package

5.10.1 package concept

Official website [explain]
Packages are a way of structuring Python's module namespace by using "dotted module names"
package by using a 'module name' python modules organized manner namespace.

Specifically: a package that contains a __init__.pyfile folder, so in fact our purpose is to create a package with the file folder / modules organized

【Emphasize】:

  1. In python3 in, even if no package __init__.py file, import the package is still not being given, but in python2, the next package must have the file, or import the package error
  2. The purpose is not to create a package to run, but was introduced into use, remember, is only one form of bag module only, the package is a modular nature

[Three things happen when you create a package]:

  1. The encapsulated __init__py file is loaded into memory.
  2. Create a package with the name of the namespace.
  3. By package name. The reference __init__in the name of all.

5.10.2 import Import

This method is not commonly used, but to understand

1 import glance.db.models
2 glance.db.models.register_models('mysql') 

Examples of exercises:

import aaa
# 1. 在执行文件写入 import aaa
# 2. aaa的 __init__ 里面 写 from aaa import m1
# 3. 然后在执行文件  aaa.m1.a
# print(aaa.m1.a)

5.10.3 from ... import ... Import

from a.b.c import d

[Note] after from import module imports must be clear not a little, or there will be a syntax error, such as: from a import bc wrong syntax

(b) between the abc and import from package must be

# from ... import ...练习
# 通过这种方式不用设置__init__文件
# from aaa import m1
# m1.func()

# from aaa.bbb.m2 import func1  #aaa外包,bbb内包
# func1()
# from aaa.bbb import m2
# m2.func1()

# from a.b.c import d
# c的. 的前面一定是包
# import 的后面一定是名字,并且不能 再有点

[Bag] embedded in the package: if the inner bag module is added to make the outer layer __init__, the need to use

from wrapper import inner  # wrappe外包名,inner内包名
# 举例
# 如何在当前文件中,引用 aaa包的bbb包.
# 1. 在执行文件写入 import aaa
# 2. aaa的 __init__ 里面 写 from aaa import bbb
# 3. 然后在执行文件  aaa.bbb
# print(aaa.bbb)
# 如何在当前文件中,引用 aaa包的bbb包 的 变量 name.
# 1. 在执行文件写入 import aaa
# 2. aaa的 __init__ 里面 写 from aaa import bbb
# 3. 然后在执行文件  aaa.bbb
# print(aaa.bbb)
# 如何在当前文件中,引用 aaa包的bbb包 的 mb文件的函数func.
# 1. 在执行文件写入 import aaa
# 2. 在aaa包的__init__ 写上 from aaa import bbb  (这样写 bbb包的__init__里面所有的名字都能引用)
# print(aaa.bbb.name)
# 3. 在bbb包的__init__ 写上 from aaa.bbb import mb
# aaa.bbb.mb.func3()

5.10.4 Importing absolute and relative import

Top pack glance was written by someone else, and then there will be demand for imported between each other in mutual glance inside the package, this time there is an absolute and relative import import in two ways:

Absolute imports: the glance as a starting

Introducing relative: .. manner or with most start (only in a package, it can not be used within different directories).

For example: We want to import glance / cmd / manage.py at glance / api / version.py in

【to sum up】

绝对导入与相对导入

# 绝对导入: 以执行文件的sys.path为起始点开始导入,称之为绝对导入
#     优点: 执行文件与被导入的模块中都可以使用
#     缺点: 所有导入都是以sys.path为起始点,导入麻烦

# 相对导入: 参照当前所在文件的文件夹为起始开始查找,称之为相对导入
#     符号: .代表当前所在文件的文件加,..代表上一级文件夹,...代表上一级的上一级文件夹
#     优点: 导入更加简单
#     缺点: 只能在导入包中的模块时才能使用
#注意:
   1. 相对导入只能用于包内部模块之间的相互导入,导入者与被导入者都必须存在于一个包内
   2. attempted relative import beyond top-level package # 试图在顶级包之外使用相对导入是错误的,言外之意,必须在顶级包内使用相对导入,每增加一个.代表跳到上一级文件夹,而上一级不应该超出顶级包

[Note] when naming packages have been updated, but many projects have followed the old name, you can use import a new name as the old name from the alias in the project, can be resolved

5.11 collection module

On the basis of built-in data types (dict, list, set, tuple) on, collections module also provides several types of additional data: Counter, deque, defaultdict, namedtuple OrderedDict and the like.

1.namedtuple: generation can use the name to access the content of the element tuple

2.deque: deque, from the other side can be quickly and additional objects Release

3.Counter: a counter for counting the main

4.OrderedDict: ordered dictionary

5.defaultdict: Dictionary with default values

# namedtuple 带名称的元组
from collections import namedtuple
point = namedtuple('point',['x','y'])
p = point(1,2)
print(p)
print(p.x)
# deque双端列表
from collections import deque
q = deque([1,2,3,4,5])
q.appendleft('d')
print(q)
q.popleft()
print(q)
# OrderDict 有序字典
from collections import OrderedDict
d = OrderedDict([('1','a'),('2','b'),('3','c')])
print(d)
#counter计数器
from collections import Counter
c = Counter('asdsadgasdasdasfsgfasgf')
print(c)  #生成一个字典,里边存放的是每个字母已经弃数量
# defaultdict
# 有如下值集合 [11,22,33,44,55,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中,即: {'k1': 大于66 , 'k2': 小于66}
#普通版
li = [11,22,33,44,55,77,88,99,90]
result = {}
for row in li:
    if row > 66:
        if 'key1' not in result:
            result['key1'] = []
        result['key1'].append(row)
    else:
        if 'key2' not in result:
            result['key2'] = []
        result['key2'].append(row)
print(result)

#defaultdict版
from collections import defaultdict

values = [11, 22, 33,44,55,77,88,99,90]

my_dict = defaultdict(list)

for value in  values:
    if value>66:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)
print(my_dict)

5.12 re module

Regular use of some symbol combinations that have special meaning or the method described character string together (referred to as regular expressions). Or: Regular rule is used to describe a class of things. In Python) it is embedded in Python, and is achieved by re module. Regular expression pattern is compiled into a series of byte code, written in C and then executed by the matching engine.

5.12.1 metacharacters

Metacharacters Matched content
\w Match letter (containing Chinese), or numbers, or underscores
\W Matching non-alphabetic (contains Chinese), or numbers, or underscores
\s Matches any whitespace
\S Matches any non-whitespace
\d Matching numbers
\D p matching non-numeric
\A From the beginning of the string match
\with Matches the end of the string, if it is for the line, only the matching results of the previous wrap
\n Matches a newline
\t A matching tab
^ Matches the beginning of the string
$ End of the string
. Matches any character except newline, when re.DOTALL flag is specified, it will match any character comprises a newline.
[...] Matches the character set of characters
[^...] Matches all characters except the characters in the character set
* Match zero or more characters to the left.
+ Match one or more characters left.
Matches zero or one character to the left, non-greedy way.
{n} Precisely matches the n preceding expression.
{n,m} N m times to match the regular expression by the preceding definition segment, greedy manner
a|b A matching or b.
() Matching expression in parentheses, also represents a group

5.12.2 match mode

import re
#单个字符匹配
print(re.findall('\d\d','12a34567890 alex *(_'))
print(re.findall('\w','太白jx 12*() _'))
print(re.findall('\d','1234567890 alex *(_'))
print(re.findall('\Ahel','hello 太白金星 -_- 666'))
print(re.findall('\n','hello \n 太白金星 \t*-_-*\t \n666'))
print(re.findall('.','da\nsdasda\nsdf231\t2314!#!@#!@'))
print(re.findall('hh$','dajqwdadnhhldsdoashhdosasdhh'))
# 元字符匹配
# . 匹配任意一个字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符
print(re.findall('a.b','ab aacb a*b a2b a牛ab a\nb')) #以a开头,以b结尾中间必须有一个字符
print(re.findall('a..b','ab aacb a*b a2b a牛ab a\nb',re.DOTALL)) #以a开头,以b结尾中间必须有两个字符
# ?匹配0个或1个由左边字符定义的片段
print(re.findall('a?b', 'ab aab abb aaaab a牛b aba**b')) #要么有一个a要么没有a
# * 匹配0个或多个左边字符表达式,满足贪婪算法
print(re.findall('a*b','ab aab aaab abbbbb'))  #以b结尾,前边有多少个a都无所谓
print(re.findall('ab*','ab cacacb aaab abbbbb')) #比较前后两个字符,必须要以a开头,结尾可以是1个或者0个b
print(re.findall('b*','ab aab aaab abbbbb')) #一个字符一个字符的比较,是b输出,不是b跳过
# + 匹配1个或者多个左边字符的表达式,满足贪婪算法
print(re.findall('a+b',' b ab aab acaab aaab abbb')) #以b结尾,前边可以有一个甚至多个a
print(re.findall('ca+b',' cb ab aab acaab aaab cabbb')) #以b结尾,前边可以有一个甚至多个ca组合
print(re.findall('a+bc',' cbc abc aabc acaab aaab cabbbc')) #以bc结尾,前边可以有一个甚至多个a组
# {n,m}匹配n个至m个左边字符表达式,满足贪婪算法
print(re.findall('a{2,4}b','ab aab aaab aaaaabb'))
# .* 组合,贪婪匹配,从头到尾
print(re.findall('a.*b','ab aab a*()b aasbdsdsdsb'))  #以a开头,以b结尾,中间可以有任意字符,ab中间如果还有ab以最外层的ab为准
# .*? 此时的?不是对左边的字符进行0次或者1次的匹配,
# 而只是针对.*这种贪婪匹配的模式进行一种限定:告知他要遵从非贪婪匹配 推荐使用!
print(re.findall('a.*?b','ab aab a*()b aasbdsadsdsb'))
# 练习,寻找_zs结尾的元素
s = '皇子_zs 赵信_zs 盖伦_zs 凯特琳_adc 慎_rz'
print(re.findall('\w*_zs',s))
print(re.findall('\w+_zs',s))

#输出时间
s1 = '''
时间就是1995-04-27,2005-04-27
1999-04-27 德玛西亚
赵信 1980-04-27:1980-04-27
2018-12-08
'''
print(re.findall('\d{4}-\d{2}-\d{2}',s1))

# 匹配一个qq账号 10000开始 第一个元素规定就是非零数字,后面的是随意的数字长度大于5位.
print(re.findall('[1-9][0-9]{4,}','12335345 3453453453 45345123123 0432040320 4324'))

Guess you like

Origin www.cnblogs.com/jjzz1234/p/11122948.html