11 python --模組

來源:互聯網
上載者:User

標籤:否則   本地匯入   執行   unix   money   app   blog   方式   目前時間   

在日常的開發工作中,我們要寫很多的python代碼,如果都寫在一個檔案中,會導致代碼特別難維護,為了拓展代碼的可維護性,我們把函寫在不同的檔案裡,這樣每個檔案包含的檔案就比較少,邏輯更加清楚。在python中,我們建立的檔案基本都是以.py結尾,那一個.py的檔案就稱之為模組。
為了方便管理方便管理模組,python中又引了包(Package)這個概念。每個包下面都有一個__init__.py檔案,這個檔案是必須存在的,否則,Python就把這個目錄當成普通目錄,而不是一個包。__init__.py可以是空檔案,也可以有Python代碼,因為__init__.py本身就是一個模組

a)    import匯入
有時候一個檔案或者一個包中已經出現了一個函數,我們在另一個python代碼中需要引入該檔案或者該檔案的某個函數,那怎麼解決呢?python給我們提供一個關鍵字import,下面我們來瞭解一下它的用法:
1,    如果是本地匯入檔案,直接使用:import filename
2,    如果匯入的是一個包,該包下面必須是有__init__.py檔案才可以匯入,否則報錯,只有有了__init__.py檔案,python解析器才會把這個目錄當成是的包
常用的匯入模組常用的格式:
form xxx import xxx
import xxx
在匯入的時候,.py的尾碼直接省略,如果是多級的包,或者想匯入包裡面的函數等,可以使用from進行匯入,舉個例子:
from aaa import bbb
import os
解釋:第一個例子是匯入aaa包下面的bbb模組或者匯入aaa檔案下面的bbb類或者函數
第二個import是直接匯入系統模組os模組

import ling.test as aaaprint(‘aaaaaaaaaaaaaaaaaaaaaa‘)aaa.hello()print(‘aaaaaaaaaaaaaaaaaaaaaa‘)
View Code

加深瞭解

#import匯入模組乾的事:‘‘‘1.產生新的名稱空間2.以建立的名稱空間為全域名稱空間,執行檔案的代碼3.拿到一個模組名spam,指向spam.py產生的名稱空間‘‘‘# money=1000000000000000000000000000000000000000000# print(spam.money)# print(spam.read1)# spam.read1()# def read1():#     print(‘from test.py‘)# spam.read2()## money=10# spam.change()# print(money)import spam as x# print(x.money)# from ... import ...# from spam import money,read1,read2,change‘‘‘1.產生新的名稱空間2.以建立的名稱空間為全域名稱空間,執行檔案的代碼3.直接拿到就是spam.py產生的名稱空間中名字‘‘‘‘‘‘from ... import ...優點:方便,不用加首碼缺點:容易跟當前檔案的名稱空間衝突‘‘‘# 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(200)## read1=10000000# from spam import read1# read1()# from spam import *## print(money)# read1()# __all__=[‘money‘]# from spam import *# print(money)## read1()
View Code

b)    datetime的使用

The distinction between naive and aware doesn’t apply to timedelta objects.

Subclass relationships:

object
    timedelta
    tzinfo
    time
    date
        datetime
time
在 Python 文檔裡,time是歸類在Generic Operating System Services中,換句話說, 它提供的功能是更加接近於作業系統層面的。通讀文檔可知,time 模組是圍繞著 Unix Timestamp 進行的。

import time
time1 = time.time()
time.sleep(5)
python 開發過程中,我們經常會用到擷取目前時間,根據目前時間產生一個和當天時間相關的檔案,這樣我們在後期看尋找檔案的時候就方便了很多,那python又如何來擷取目前時間呢?
from datetime import datetime
now_time = datetime.now()
a = now_time.strftime(‘%Y-%m-%d‘)
print(now_time)
print(a)
print(type(now_time))
結果:
2017-04-25 14:23:37.339000
2017-04-25
<type ‘datetime.datetime‘>
解釋:
1,    先匯入datetime類
2,    通過datetime的now方法就獲得當前所需要的時間
3,    datetime還是有好多方法的,可以擷取year, month,等,請通過dir()方法查看
4,    此時我們獲得的now_time還是一個datetime類,所以我們需用用strftime方法轉換成字串,strftime的參數就是最終需要字串的形式。
5,    Strftime需要便是時間的參數有很多,下面我們介紹一下最長用的一些參數:
格式參數:
%Y 帶世紀部分的十制年份
%m 十進位表示的月份
%d 十進位表示的每月的第幾天
%H 24小時制的小時
%M 十時製表示的分鐘數
%S 十進位的秒數
%c  標準時間,如:04/25/17 14:35:14  類似於這種形式
這幾個參數就是最常用到的我們用的時間

例子:from datetime import datetime, timedeltanow_time = datetime.now()a = now_time.strftime(‘%c‘)print(now_time)print(a)b = now_time + timedelta(days=-1)print(b)結果:2017-04-25 14:37:26.99600004/25/17 14:37:262017-04-24 14:37:26.996000解釋:Timedelta可以接收days和seconds兩個參數,正數代表幾天之前的,負數代表幾天之前的。所以b代表的是一天之前的時間。時間的三種存在方式:時間對象,時間字串,時間戳記。(1)    字串轉datetime:from datetime import datetime, timedeltastring = ‘2017-04-25 11:59:58‘time1 = datetime.strptime(string, ‘%Y-%m-%d %H:%M:%S‘)print(time1)print(type(time1))結果:2017-04-25 11:59:58<type ‘datetime.datetime‘>(2)    datetime轉字串:from datetime import datetime, 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)結果:2017-04-25 11:59:58<type ‘datetime.datetime‘><type ‘str‘>2017-04-25 11:59:58解釋:(3)時間戳記轉時間對象:from datetime import datetime, timedeltaimport timetime1 = time.time()print(time1)time1_str = datetime.fromtimestamp(time1)print(time1_str)print(type(time1_str))結果:1493107955.662017-04-25 16:12:35.660000<type ‘datetime.datetime‘>解釋:1,    datetime模組是封裝了time模組的,所以只需要瞭解datetime模組就可以。2,    time.time()是或者目前時間的時間戳記,時間戳記是指格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至現在的總秒數。3,    datetime下面下面有個函數fromtimestamp(時間戳記)會把時間戳記自動轉換成datetime類型
View Code

 

11 python --模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.