Python--Module

Source: Internet
Author: User
Tags timedelta

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

A)     import import
Sometimes a file or a package already has a function that we need to introduce in another Python code or a function of that file, how to solve it? Python gives us a keyword import, let's look at its usage:
1,     If it is a local import file, use: Import filename
2,     If the import is a package, the package must have a __init__.py file to import, or error, only with the __init__.py file, the Python parser will consider this directory is the package
commonly used import module format:
form XXX import xxx
import xxx
At the time of import, the suffix of. PY is omitted directly, if it is a multilevel package, or if you want to import a function inside a package, you can use from to import, for example:
from AAA import BBB
Import OS
Explanation: The first example is to import the BBB module under the AAA package or to import the BBB class or function under the AAA file
The second import is to import the System Module OS module directly

Import Ling.test as AAA Print ('aaaaaaaaaaaaaaaaaaaaaa') Aaa.hello () Print ('aaaaaaaaaaaaaaaaaaaaaa')
View Code

Deepen understanding

#Import Importing module What to do:" "1. Create a new namespace 2. Executes the file's code 3 with the new namespace as the global namespace. Get a module name spam, point to spam.py generated namespace" "#money=1000000000000000000000000000000000000000000#print (Spam.money)#print (SPAM.READ1)#Spam.read1 ()#def read1 ():#print (' from test.py ')#spam.read2 ()##money=10#Spam.change ()#Print (Money)Importspam as x#print (X.money)#From ... import ...#From spam import Money,read1,read2,change" "1. Generate a new namespace 2. Executes the code 3 of the file with the new namespace as the global namespace. Just get the name in the namespace that spam.py produces." "" "From ... import ... Advantages: Convenience Without prefix disadvantage: easy to conflict with the name space of the current file" "#Print (Money)#Read1 ()#money=10#del Money#Print (Money)##def read1 ():#print (' =========from test.py read1 ')#read2 ()#Import Time#money=100#Print (Money)#Time.sleep ($)##read1=10000000#From spam import read1#Read1 ()#From spam Import *##Print (Money)#Read1 ()#__all__=[' money ']#From spam Import *#Print (Money)##Read1 ()
View Code

b) Use of datetime

The distinction between naive and aware doesn ' t apply to Timedelta objects.

Subclass Relationships:

Object
Timedelta
Tzinfo
Time
Date
Datetime
Time
In the Python documentation, time is categorized in generic Operating system services, in other words, it provides functionality that is closer to the operating system level. Read through the documentation, the time module is built around Unix Timestamp.

Import time
time1 = Time.time ()
Time.sleep (5)
Python development process, we often use to get the current time, based on the current time to generate a and day-to-date related files, so that we look at the time to find the file is a lot of convenience, then python how to get the current time?
From datetime import datetime
Now_time = DateTime.Now ()
A = Now_time.strftime ('%y-%m-%d ')
Print (Now_time)
Print (a)
Print (Type (now_time))
Results:
2017-04-25 14:23:37.339000
2017-04-25
<type ' Datetime.datetime ' >
Explain:
1, first import the DateTime class
2, you get the current time by using the now method of datetime
3, DateTime still has a lot of methods, can get year, month, etc., please view 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:
Format parameters:
%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
These parameters are the most commonly used time we use.

Example: fromDatetimeImportdatetime, Timedeltanow_time=DateTime.Now () a= Now_time.strftime ('%c')Print(now_time)Print(a) b= Now_time + Timedelta (days=-1)Print(b) Results:2017-04-25 14:37:26.99600004/25/17 14:37:262017-04-24 14:37:26.996000Explanation: Timedelta can receive days and seconds two parameters, the positive number represents the previous day, the negative number represents a few days ago. So B represents the time of day. Three ways of existence: Time object, time string, timestamp. (1) string to datetime: fromDatetimeImportdatetime, Timedeltastring='2017-04-25 11:59:58'time1= Datetime.strptime (String,'%y-%m-%d%h:%m:%s')Print(time1)Print(Type (TIME1)) Results:2017-04-25 11:59:58<type'Datetime.datetime'>(2) datetime to string: fromDatetimeImportdatetime, Timedeltastring='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(Type (TIME1_STR))Print(TIME1_STR) Results:2017-04-25 11:59:58<type'Datetime.datetime'><type'Str'>2017-04-25 11:59:58Explanation: (3Timestamp-to- time object: fromDatetimeImportdatetime, TimedeltaImporttimetime1=time.time ()Print(time1) time1_str=Datetime.fromtimestamp (time1)Print(TIME1_STR)Print(Type (TIME1_STR)) Results:1493107955.662017-04-25 16:12:35.660000<type'Datetime.datetime'>Explanation:1, the 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 refers to the total number of seconds GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) to the present. 3, DateTime Below a function fromtimestamp (timestamp) will automatically convert the timestamp to a datetime type
View Code

Python--Module

Related Article

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.