Blog 17

1 package

  • What is the package

    It is the module package

  • What is the use package

    1. When too many functions within the module, in order to facilitate management functions, the plurality of functions into a plurality of modules, but various changes introduced original manner, the plurality of modules into a package (folder) inside.

    2. By coating, it can be introduced without changing the way the user (and the decoration does not change to be called by the same token decorative function), to improve the user experience
    3. Future package is the lead guide__init__

  • The main attention in the usage of the package for two

    1. Package containing __init__.pythe folder; leader packet is introduced__init__

    2. Packet must be treated as module file import file search path module to perform document shall prevail

  • Import introducing relative and absolute (only used internally in the package)

    Relative imports:

    • .Directory representation (the same folder) of the current file
    • ..It represents the current parent directory files
    • ...Grandpa directory represents the current file

    Absolute imports:

    • Is no point represent directories, direct write the name

    2. time module

  • The role of time module

    It provides three different types of time (time stamp), three different types of interchangeable times

  • Three types of time

    • Formatting time
    • Structured time
    • Timestamp

    == time they are structured intermediate medium, time and structured format can be converted each time; time stamp may be converted into each other and structured ==

1. Priority grasp

import time
time.time() # 时间戳 (从计算机元年开始到现在的时间,以秒计算显示)

time.sleep(1)  # 让程序运行到这一步暂停1秒

2. Understand

import time

print(time.time())  # 时间戳形式

# 格式化时间
print(time.strftime('%Y-%m-%d %X'))

# 结构化时间
print(time.localtime())


# 结构化时间 --》 格式化时间
struct_time = time.localtime(3600*24*365)
print(time.strftime('%Y-%m-%d %X',struct_time))


# 格式化时间 --》 结构化时间
format_time = time.strftime('%Y-%m-%d %X')
print(time.strptime(format_time,'%Y-%m-%d %X'))


# 结构化时间 --》 时间戳
struct_time = time.localtime(3600*24*365)
print(time.mktime(struct_time))

# 时间戳 --》 结构化时间
time_stamp = time.time()
print(time.localtime(time_stamp))

3. datetime module

  • The role of datetime module

    Time of addition and subtraction can be achieved

1. Priority grasp

import datetime
# 获取当前时间
now = datetime.datetime.now()
print(now) # 2019-09-28 19:56:44.330734

# 默认3天 # 2019-10-01 19:56:44.330734
print(now + datetime.timedelta(3)) 


# 加3周 #2019-10-19 19:56:44.330734
print(now + datetime.timedelta(weeks=3))

# 加3小时 2019-09-28 22:56:44.330734
print(now + datetime.timedelta(hours=3))

# 减3小时 # 2019-09-28 16:56:44.330734
print(now - datetime.timedelta(hours=3))
print(now + datetime.timedelta(hours=-3))

# 1949-10-01 10:01:00
print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))

4. random module

  • The role of random module

    Generate random numbers

1. Priority grasp

import random


# 掌握

# 0-1
print(random.random())

# 产生一个[1-3]之间包括首尾的随机数
print(random.randint(1,3))

# 打乱
lt=[1,2,3]
random.shuffle(lt)
print(lt)

# 随机选择一个
print(random.choice(lt))

# 只随机一次  --> 梅森旋转算法
import time
# random.seed(time.time())
# random.seed(111111111111)
seed() 方法是改变随机数生成器的种子,也就是说,当使用seed()方法后,后面的产生的随机数就是一样的了。
seed()括号内的数不同,产生的随机数种子也不同
就是说 例如:
random.seed(1) 后面再产生的随机数都为 2
random.seed(2)后面再产生的随机数都为 3
print(random.random())

2. Understand

# 了解
print(random.sample([1,'a','c',2,3,4],2))

5. hashlib module and the module hmac

  • The role of hashlib module

    The character encryption

  • The role of hmac module

    Character encryption key and adding, with corresponds to the encrypted layers.

  • Examples hashlib module

import hashlib

# 叠加性
m = hashlib.md5()
# m.update(b'say')
# m.update(b'hello')  # 981fe96ed23ad8b9554cfeea38cd334a
m.update(b'hash123456')
print(m.hexdigest())  # 对于不同的字符而言,用不重复

# 981fe96ed23ad8b9554cfeea38cd334a

# 手机号/生日/性别/qq账号/以前的密码/   --》 挖矿(算法)

# 1 2 3 5 71113 111111111111111 - 1111111111111111111111 111111111111111111111111111111111111111111111111111

hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'

pwd_list = [
    'hash3714',
    'hash1313',
    'hash94139413',
    'hash123456',
    '123456hash',
    'h123ash',
]

for pwd in pwd_list:
    m = hashlib.md5()
    m.update(pwd.encode('utf8'))
    res = m.hexdigest()
    if res == hash_pwd:
        print(f'获取密码成功:{pwd}')
  • Examples hamc module

    import hmac
    
    m = hmac.new(b'maerzi')
    m.update(b'hash123456')  # f82317e44545b0ab087109454814b5c4
    print(m.hexdigest())
    
    m = hmac.new(b'sdfjhjk2394879ul%$$Y#($&')
    m.update(b'hash123456')  # 2a70fd0f13cb49357f40d326a4e071a2
    print(m.hexdigest())
    
    pwd_list = [
        'hash3714',
        'hash1313',
        'hash94139413',
        'hash123456',
        '123456hash',
        'h123ash',
    ]

6. typing module

  • The role of typing module

    A function associated with the control function parameter data type, a data type other than the type of underlying data (e.g. Iterable, Iterator, Generator)

  • Examples

    def func(x: int, lt: Iterable) -> list:
        return [1, 2, 3]
    
    func(10, '123123')

7. requests module

  • a request action module

    Climb data modules, analog url browser sends a request to retrieve data

  • Examples

    # url ——> 一个特定的网址 -》 永不重复
    import requests
    
    response = requests.get('https://ishuo.cn')
    data = response.text
    print(data)

8. re module

  • The role of the re module

    String having a certain shape selected from a large characteristic string

  • Regular Expressions: Pattern

  • Regular expression itself is a small, highly specialized programming language, which is not part of Python.

  • Regular expression pattern is compiled into a series of bytecodes, then executed by the matching engine written in C

  •  以下必须得记住
    
    # .*?
    # 贪婪和非贪婪
    # findall
    # re.S
    # match和sarch的区别
    # 分组
    # 有名分组:给分组加名字

1. re module regex metacharacters and grammar

s = 'abcdabc'
#    abc
#        abc
#     bc  bc

# ^:以...开头
res = re.findall('^ab', s)
print(res)
res = re.findall('^bc', s)
print(res)
# $: 以..结尾
s = 'abcdabc'
res = re.findall('bc$', s)
print(res)

# .: 任意字符
s = 'abc红abc'
res = re.findall('abc.', s)
print(res)

# \d: 数字
s = 'skld2342ljk'
res = re.findall('\d', s)
print(res)

# \w: 非空,数字字母下划线
s = 'skld_23 42ljk'
res = re.findall('\w', s)
print(res)

# \s:空,空格/\t/\n
s = 'skld_23 42ljk'
res = re.findall('\s', s)
print(res)

# \D: 非数字
s = 'skld2342ljk'
res = re.findall('\D', s)
print(res)

# \W: 空
s = 'skld_23 42ljk'
res = re.findall('\W', s)
print(res)

# \S:非空
s = 'skld_23 42ljk'
res = re.findall('\S', s)
print(res)

# +: 前面的一个字符至少1个
s = 'abcddddd abcd abc'
print(re.findall('abcd+', s))

# ?:前面的一个字符0-1个
s = 'abcddddd abcd abc'
print(re.findall('abcd?', s))

# *:前面的一个字符至少0个
s = 'abcdddddddddddddddddd abcd abc'
print(re.findall('abcd*', s))

# []: 中括号内的都可以
s = 'abc bbc cbc dbc'
print(re.findall('[abc]bc', s))

# [^]: 中括号的都不可以
s = 'abc bbc cbc dbc'
print(re.findall('[^abc]bc', s))

# |:或
s = 'abc bbc dbc'
print(re.findall('abc|bbc', s))

# {2}:前面的字符2个

s = 'abccabc abccc'
print(re.findall('abc{2}', s))

# {1,2}:前面的字符2个

s = 'abccabc abccc'
print(re.findall('abc{1,2}', s))

2. greedy and non-greedy mode

# 贪婪模式

# .(一个任意字符)*(0-无穷个)

s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg'
print(re.findall('a.*g', s))

# 非贪婪模式(*******)

# .(任意字符)*(0-无穷个)?(让他进入非贪婪模式)
s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg'
print(re.findall('a.*?g', s))

3. Examples of matching mailbox

s = '#@#@#@[email protected]$$$$////[email protected]$$#$#$[]][email protected]@$2423423lksdlfj#'
# \w(字母/数字/下划线)+(0-无穷个)@ \w(字母/数字/下划线)+(0-无穷个).com
print(re.findall('\w+@\w+\.com', s)) # \. 是使 点 成为一个普通的字符

4. re common function module functions

## compile 用来创建Pattern对象
s = 'abcd abcddd abc'
# res = re.compile('abcd*')
email_pattern = re.compile('\w+@\w+.com')
phone_patter = re.compile('\d{13}')
print(re.findall(email_pattern, s))

print(re.findall('abcd*', s))

# ## match:  从开头找一个,找得到就不找了 ;找不到报错 --》
# s = 'ab abcddd abc'
# res = re.match('abcd*', s)
# print(res.group())

## search: 从字符串找一个,就不找了
s = 'ab abcddd abc'
res = re.search('abcd*', s)
print(res.group())

## split
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(re.split('\d+', s))

## sub == replace
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(re.sub('\d+', ' ', s))

## subn --> 替换了多少次
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(re.subn('\d+', ' ', s))

5. Modifier re.S

  • The flags argument is

    ## 修饰符 --> re.S会让.可以匹配换行符\n
    
    # .不匹配换行
    print(re.findall('abc.abc', s))  # ['abc*abc']
    # 匹配换行
    print(re.findall('abc.abc', s, re.S))  # ['abc\nabc', 'abc*abc']

6. SUPPLEMENTARY

## 分组 --> 只要括号里的(*****)
s = 'abc abcd abcdd'
print(re.findall('a(.)c(d)', s))

## 有名分组(了解)
s = 'abc abcd abcdd'
print(re.search('a(?P<name>.)c(?P<name2>d)', s).groupdict())

# 超高级用法
s = 'abc123abc123'  # c123a
print(re.sub('c(\d+)a', ' ', s))
print(re.sub('c(?P<name1>\d+)a', ' \g<name1> ', s))  # \g<name1>这个东西不能替换掉

Guess you like

Origin www.cnblogs.com/Mcoming/p/11604846.html