day16_ million annual salary into the sixteenth day - a custom module, time, datetime, random

day16

Custom Modules

A custom module

import: import (take Toolbox)

# import test
# test.func()
Import happened
  • Open up a new space in the current namespace
  • All code module execution
  • To find (function) tools (module name.)
# print(locals())
# import test
# print(locals())

# import test
# print(test.name)
# print(test.func())

#错误的示例:
# import test.py
# # print(test.py.func())

# import test # 
# import test
# import test
# import test
# import test
# import test
# print(test.name)
import test as t: use aliases can make the file name shorter
# import test as t
# print(t.name)
low版
msg = """
1.扳手
2.螺丝刀
>>>
"""
# choose = input(msg)
# if choose == "1":
#     import meet
#     meet.func()
# elif choose == "2":
#     import test
#     test.func()
加强版
# choose = input(msg)
# if choose == "1":
#     import meet as t
# elif choose == "2":
#     import test as t
#
# t.func()
import module name from the function name or variable name
# from test import func
# func()
And from the difference between the import
  • import test as t: introducing the test file (alias can be changed)

    Disadvantages: relatively large memory footprint

    Pros: variable or function defined in the current file and will not conflict

     优点:  
    # import test
    # name = "宝元"
    # print(test.name)
    # print(name)
  • from test import func as f: takes a function (alias can be changed)

    Advantages: relatively small memory footprint

    Cons: variable or function defined in the current file and conflict

    缺点:   
    # name = "宝元"
    # from test import name
    # print(name)
    
    解决方法:
    # name = "宝元"
    # from test import name as n
    # print(name)
    # print(n)

from test import * (all methods in this document, there will be coverage of the phenomenon, can not solve) do not mind using

# name = "宝元"
# def func():
#     print("is 马桶推送器")

# from test import *
# print(name)
# func()

______all______ = [ 'function names and variable names may be introduced'] (* with use), the module can be used to limit the functions and variables passed

# from meet import *
# print(func)
# print(name)
Classification Module
  • Built-in module (standard library) - python interpreter that comes with .py files (modules)
  • Third-party modules (large variety of God written) - require additional download (concurrent programming began to explain pypy)
  • Custom Module (write your own) - no additional downloads
Two uses modules
  • Script: execute python test.py in cmd
  • Module: not used or introduced
Sub-module

Sub-module (a module can be called multiple files) benefits:

  • Avoid writing duplicate code
  • You can use many times
  • All-takenism
Imported modules pit

Do not let happen import cycle

Test and call module (______main______)

In the current module is used ______name______ '' ______main______ ''

When the module is imported ______name______ is to be imported module name

模块里面是测试:__name__ == '__main__'
运行时会运行
# 

#     func()

导入的时候:__main__ == 模块名
运行不会运行
Find module method

If you are looking for is not in this file folder:

  • Import path

    from day15 import meet

    # import meet
    # print(meet.name)
  • Using a relative path

    from day15.ti import meet

    # from day15.t1 import meet
    # print(meet.name)
  • Use an absolute path

    from sys import path

    path.insert (0, "absolute path")

    # from sys import path
    # path.insert(0,"D:\\")
    # import meet
    # print(meet.name)
Find module order

Memory load> Customize> Built-in> Third-party

time module

time (): timestamp (float)

import time
# print(time.time())  # 时间戳 浮点数
# print(time.time() + 5000000000)  # 时间戳 浮点数

sleep (2): Sleep (seconds)

# time.sleep(3) #  睡眠         # 秒

time.strftime ( "% Y-% m-% d% H:% M:% S")): posters time (intermediate Chinese symbol can not be used)

# print(time.strftime("%Y-%m-%d %H:%M:%S")) 

gmtime (): Structured time (data type is named tuple time)

# print(time.gmtime())  # 结构化时间 数据类型是是命名元组
# print(time.gmtime()[0])
# print(time.gmtime().tm_year)

The timestamp string conversion take time

time.strftime ( "% Y-% m-% d% H:% M:% S", time.gmtime (timestamp))

# print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(1564028611.631374)))

Converting a string to time stamp

time.mktime(time.strptime("2024-3-16 12:30:30","%Y-%m-%d %H:%M:%S")))

# print(time.mktime(time.strptime("2024-3-16 12:30:30","%Y-%m-%d %H:%M:%S")))
time Focus:

time.time (): View the current timestamp

time.sleep (): Sleep

time.gmtime () / time.localtime (): The time stamp is converted into a structured

time.strftime ( "% Y-% m-% d% H:% M:% S", "Structured Time"): time becomes string (posters)

time.strptime ( "string", "% Y-% m-% d% H:% M:% S"): Structured time becomes

time.mktime (Structured Time): timestamp into

datetime module

datetime-- objects

print(type(datetime.now()))
# <class 'datetime.datetime'>

datetime.now (): Get the current time

# print(datetime.now()) # 获取当前时间
# print(datetime(2019,5,20,15,14,00) - datetime(2019,5,20,14,20,00))

Converted to a current time stamp

# t = datetime.now()
# print(t.timestamp())

The conversion of the timestamp to the current time

import time
print(datetime.fromtimestamp(time.time()))
print(datetime.fromtimestamp(1564044926.701301))

Converted into string objects

print(datetime.strptime("2019-10-10 22:23:24","%Y-%m-%d %H:%M:%S")) 
# 2019-10-10 22:23:24(对象)

The object into a string

# print(str(datetime.now()))
# print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# print(datetime.strftime(datetime.now(),"%Y-%m-%d %H:%M:%S"))

datetime subtraction

# print(datetime.now() + timedelta(hours=30 * 24 * 12))
# print(datetime.now() - timedelta(hours=30 * 24 * 12))

random module

random-- random number

random()

print(random.random())          # 0 ~ 1里面的小数,不能是0和1

uniform()

print(random.uniform(1,10))     # 1 ~ 10里面的小数,不能是1和10

Randine ()

print(random.randint(1,50))     # 1 ~ 50(闭区间)

randrange ()

print(random.randrange(1,5,2))    # randrange(起始,终止,步长)

choice()

print(random.choice([1,2,3,4,5])) # 选择一个元素

choices()

print(random.choices([1,2,3,4,5],k=2))   # 选择两个元素,会有重复

sample()

print(random.sample((1,2,3,4,5),k=2))  # 选择两个元素,不会有重复(除非只有两个)

shuffle (): scramble

# lst = [1,2,3,4,5,6,7,8,9,0]
# random.shuffle(lst)  # 顺序打乱
# print(lst)

Guess you like

Origin www.cnblogs.com/NiceSnake/p/11284919.html