. Python Module

Source: Internet
Author: User
Tags timedelta

What is a module?

In the daily development work, we have to write a lot of Python code, if all written in a file, it will cause the code is particularly difficult to maintain, in order to expand the maintainability of the code, we write the letter in different files, so that each file contains fewer files, logic more clear. In Python, the file we create is basically the end of a. py, and the. py file is called a module.

In order to facilitate the management of convenient management module, Python also cited the concept of package. Each package has a __init__.py file, which must exist, otherwise Python will treat the directory as a normal directory, not a package. __init__.py can be either an empty file or a Python code, because __init__.py itself is a module, for example: The test directory has __init__.py, aaa.py,bbb.py three files, as follows:

[Email protected] ~]# tree test

Test

├──aaa.py

├──bbb.py

└──__init__.py

0 directories, 3 files


1. Import

Pycharm shortcut keys for commonly used import modules: ALT + ENTER

Sometimes a function is already present in a file or a package, and we need to introduce the file or a function of that file in another Python code.

Python gives us a keyword import, let's look at its usage:

1, if it is a local import file, directly use: Import filename (ie: import filename.py)

2, if the import is a package, the package must have a __init__.py file to import, otherwise error, only with the __init__.py file, the Python parser will take this directory as the package


Common formats for commonly used import modules:

form XXX Import xxx

Import xxx

When importing, the suffix of. PY is omitted directly, if it is a multilevel package, or if you want to import the function inside the package, you can use the From to import


As an example:

From AAA import BBB

Import OS

Import CCC.QQQ

From ddd.eee Import FFF

Explain:

First example: Import the BBB module below the AAA package or import the BBB class or function under the AAA file

Second example: Direct import of the System Module OS module

The third example: Import the custom module file directly (it is cumbersome to write, usually in the first and fourth types)

Fourth example: deepening of the first example, importing the FFF method within the Eee module file in the DDD Module package (method ==> "function" or "class" or "Ddd.eee alias")


Chestnuts are as follows:

Import Ling.test as AAA # #aaa is ling.test alias

Print (' aaaaaaaaaaaaaaaaaaaaaa ')

Aaa.hello () # # alias usage.

Print (' aaaaaaaaaaaaaaaaaaaaaa ')

Summarize:

Import modules, which are layers of calls

When importing with import only, what is imported after import, what must be written at the time of the call, unless you add the import from.

Do not write suffix names when importing module files


2. DateTime Time Module Acquisition time

Time module is often used in the work, print logs and so on will use the time module.

Let's take a look at the relationships between a couple of subclasses

Object

Timedelta

Tzinfo

Time

Date

Datetime


①time Module

Basic not used to take time, to take time to recommend the use of the DateTime module

Time-Exclusive usage:

For I in Xrange (1,10):

Print (i)

Time.sleep (3) # #休眠3秒


②datetime Module

From datetime import datetime

Now_time = DateTime.Now ()

Print (Now_time)

A = Now_time.strftime ('%y-%m-%d%h:%m:%s ')

Print (a)


Explain:

1, first import the DateTime class

2, you get the current time by using the now method of datetime

3,datetime still have a lot of methods, can get year, month, etc., please check through the Dir () method

4, at this point we get the now_time is still a DateTime class, so we need to use the Strftime method to convert to a string, strftime parameter is the final form of the string.

5,strftime need is a lot of time parameters, below we introduce the longest use of some parameters:


The format parameters are as follows:

%Y 10 year with century part

%m the month represented by decimal

The day ordinal of a month in%d decimal notation

%H 24-Hour Hour

%M minutes in a 10 o'clock-hour representation

%s number of seconds in decimal

%c standard Time, such as: 04/25/17 14:35:14 similar to this form

Above, these parameters are the most commonly used time we use


In addition , in the development process, often to take yesterday 's time, or tomorrow 's time, what should be done?

Example:

From datetime import datetime, Timedelta

Now_time = DateTime.Now ()

A = Now_time.strftime ('%c ')

Print (Now_time)

Print (a)

b = now_time + Timedelta (days=-1)

Print (b)

Explain:

Timedelta can receive days and seconds two parameters, a positive number represents a few years later, the negative number represents a few days ago

So, b represents the time of day.


3. Time formats convert each other

three ways of existence : Time object, time string, timestamp.

(1) String to datetime:

From datetime import datetime

string = ' 2017-04-25 11:59:58 '

time1 = Datetime.strptime (String, '%y-%m-%d%h:%m:%s ')

Print (TIME1)

Results:

2017-04-25 11:59:58


(2) DateTime goto string:

From datetime import datetime

string = ' 2017-04-25 11:59:58 '

time1 = Datetime.strptime (String, '%y-%m-%d%h:%m:%s ')

Print (TIME1)

Print (Type (time1))

Time1_str = Datetime.strftime (time1, '%y-%m-%d%h:%m:%s ')

Print (TIME1_STR)

Print (Type (TIME1_STR))

Results:

2017-04-25 11:59:58

<type ' Datetime.datetime ' >

2017-04-25 11:59:58

<type ' str ' >


(3) Timestamp to time object:

From datetime import datetime

Import time

time1 = Time.time ()

Print (TIME1)

Time1_str = Datetime.fromtimestamp (time1)

Print (TIME1_STR)

Print (Type (TIME1_STR))


Results:

1493107955.66

2017-04-25 16:12:35.660000

<type ' Datetime.datetime ' >


Explain:

The 1,datetime module is packaged with the time module, so you just need to understand the DateTime module.

2,time.time () is the timestamp of the current time, and the timestamp is the total number of seconds from GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) to now.

3,datetime Below a function fromtimestamp (timestamp) will automatically convert the timestamp to a datetime type (Time object type)


This article is from the "Note space" blog, so be sure to keep this source http://286577399.blog.51cto.com/10467610/1980461

. Python 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.