Python------module definition, import, optimization------time Module

Source: Internet
Author: User

1. Module Introduction

2.time & DateTime

3.random

4.os

5.sys

6.shutil

7.json & Pickle

8.shelve

9.xml processing

10.yaml processing

11.configparser

12.hashlib

13.subprocess

14.logging Module

15.re Regular Expressions

I. Definition

Modules: Used to logically organize Python code (defining variables, functions, classes, logic, implementing a function), which is essentially a Python file at the end of the. Py.

Package: Used to logically organize the module, essentially a directory (with a __init__.py file)

Two. How to import

Import Module

Import xx,xxx,xxxx    #xx. yyy
from XXX import xxx,xx,x
from XXX import * (not advocated)
from xxx import xxx as xx (alias) #xx

Importing Packages
Import Package_module
from. Import test1 #从当前路径导入, __init__ current path




three. Import Essence
the essence of the import module is to interpret the Python file again,
difference:
import M #需要用 '. ' Call m.xxx
From m import xxx #直接调用 xxx


The nature of the import package: Execute the __init__.py file under the package


Os.path.abspath (__file__) #获取文件的绝对路径Os.path.diename (Os.path.abspath (__file__))four. Import OptimizationFrom ... Import ... Five. Classification of modules1. Standard library
1.1 Time and DateTime
>>> Help (Time. Sleep)
UTC (Coordinated Universal Time, world co-ordination) is GMT, world standard Time, utc+8 in China.
DST (Daylight saving time) is daylight saving time. Summer season (Daylight saving
time:dst), also known as daylight saving and daylight saving time,
is a system for saving energy and for people to set local time, the unified time used during the implementation of this system is called "Daylight saving period".

There are usually several ways to represent time in Python:
1) timestamp; (seconds)
Timestamp: Represents the offset that is calculated in seconds, starting January 1, 1970 00:00:00.
>>>timetime ()

2) The formatted time string,

3) tuple (struct_time) a total of nine elements.

>>>time.localtime ()
Time.struct_time (tm_year=2018, tm_mon=9, Tm_mday=1, tm_hour=15, tm_min=43, tm_sec=40, tm_wday=5, tm_yday=244, TM_ISDST =0)

  Variables:  
TIMEZONE--UTC and local time difference;
altzone--
daylight--
tzname--
functions:
Time ()
Sleep ():
Gmtime (): The timestamp is converted to UTC, (when no default value is present, the current timestamp is converted to standard UTC; You can also enter a parameter).
LocalTime (): Gets the local current utc+8.
asctime (): Get string format tuple---> string

>>> time.asctime ()
' Sat Sep 1 16:29:17 2018 '

     CTime (): Timestamp     ----> string
Mktime ():
Strftime ():


Strptime (format, struct_time)----> "Formatted string"

>>> time.strftime ("%y-%m-%d%h:%m:%s", LocalTime ())
' 2018-09-01 16:38:38 '

strptime (' string ', format)

>>> time.strptime (' 2018-09-01 17:04:00 ', "%y-%m-%d%h:%m:%s")
Time.struct_time (tm_year=2018, tm_mon=9, Tm_mday=1, tm_hour=17, tm_min=4, tm_sec=0, tm_wday=5, tm_yday=244, tm_isdst=- 1)

1.2  datetime
class: datetime.date ()
Datetime.time ()
Datetime.datetime ()

>>> Datetime.datetime.now ()
Datetime.datetime (2018, 9, 1, 17, 25, 19, 837197)
>>> print (Datetime.datetime.now ())
2018-09-01 17:25:44.738970

>>> Datetime.datetime.now () +Datetime.timedelta (3) #三天后的此刻, the default is in days, Timedelta () cannot exist alone.
Datetime.datetime (2018, 9, 4, 28, 2, 46990)

>>> Datetime.datetime.now (+datetime.timedelta)(hours=3)
Datetime.datetime (2018, 9, 1, 30, 26, 491917)

>>> Datetime.datetime.now (+datetime.timedelta) (minutes=30)
Datetime.datetime (2018, 9, 1, 18, 2, 48, 972030)

>>> Datetime.datetime.now (). Replace (minute=3,hour=2) #修改时间
Datetime.datetime (2018, 9, 1, 2, 3, 51, 622663)




2. Open source module (third party libraries)
3. Custom Modules

Python------module definition, import, optimization------time Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.