python-python base 5

I. Introduction module

Definition: is the end of the python .py file in nature. Module, with a mound of code to achieve a set of code of a function. 

Similar to functional programming and process-oriented programming, functional programming is completed a function that calls the other to provide a coupling between the code and code reuse. For a complicated function, may require multiple functions to complete (and can function in different .py file), the set of codes consisting of n .py file called module.

Import module:

the essence is to import python file explain it again. For example import test, the test is in the test all the code assigned to this variable, so call the test elsewhere test.xxx need to call.

And from test import * In this way, all the code is executed directly inside the test call can be directly xxx.

from ... import ... consistent with the import statement, the only difference is: after using import foo import modules, reference module names need to add foo as a prefix, used from foo import x, get, change. , foo foo module can be referenced directly in the name of the currently executing file as follows

from foo Import x, get, Change # module foo x and get into the current namespace 
A = x # directly module foo of x assigned to A 
() get # direct the get function foo of 
change () # even currently have the same name x, modify the source file is still in the x

No need to prefix the benefits is to make our code more concise, the downside is likely to conflict with the name of the current namespace, current namespace if the presence of the same name, the name of the definition of the previously defined overrides name.

View import module search path: sys.path, if not a python module that comes, usually only find in the same directory module, if you need to import modules in other directories, put the module where the parent directory to the chaser sys.path inside. example:

p=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(p)

If you are importing a package, then the package is executed following __init__ file.

If you want to import a module of a bag, that can be written from the __init__.py file in the package. Import xxx, xxx import module

We can also play an alias for the imported modules in the current location

Import foo AS f # of imported modules foo alias the current position f, f alias to use when later used 
F.X 
f.get ()

You can also import a name from the alias

from foo import get as get_x
get_x()

Usually too long when using the alias name was imported from the way to streamline the code, the other from the alias name to be imported can well avoid a conflict with the current name, there is a very important point is this: can keep calling mode consistency, for example, we have two pickle and json modules while achieving load method, it is to parse the data structure of the file from an open, but with different formats parsed, the following codes may be selectively load different the module

IF DATA_FORMAT == ' json ' :
     Import json the serialize AS # if the data format is json, and then introduced into the json module named the serialize 
elif DATA_FORMAT == ' pickle ' :
     Import pickle the serialize AS # If the data format is a pickle, then introduced into the pickle module and was named the serialize 

the Data = serialize.load (the Fn) # mode is consistent with the final call

Classification Module

Module is divided into three types:

  • Custom Modules
  • Built-in standard modules (also known as the standard library)
  • Open Source Module

 

Two, time & datetime module

time module

In Python, there are usually several ways to represent this time: 1) the time stamp 2) 3 formatted time string) tuple (struct_time) total of nine elements. Due to the time Python module to achieve the main C library calls, so each platform may vary.

1 . Manner tuple returns the current local time
 >>> time.localtime () 
a time.struct_time (tm_year = 2017, tm_mon =. 5, tm_mday =. 8, tm_hour = 16, = 13 is tm_min, tm_sec = 34 is, tm_wday = 0, = 128 tm_yday, the tm_isdst = 0)

 2 . tuple returns manner GMT
 >>> time.gmtime ()    
a time.struct_time (tm_year = 2017, tm_mon =. 5, tm_mday =. 8, tm_hour =. 8, 13 is tm_min = , tm_sec = 38 is, tm_wday = 0, tm_yday = 128, the tm_isdst = 0)

 . 3 . the conversion time stamp tuple
 >>> X = time.localtime ()
 >>> time.mktime (X)
 1,494,232,890.0 

. 4 . the tuple time format into a string of time
 >>> X = time.localtime ()
 >>> The time.strftime ( ' % Y-M-% D%% H:% M:%S',x)
' 2017-05-08 16:57:38 '
 
5 . Converts a tuple string format time format time
 >>> the time.strptime ( ' 2017-05-08 17:03:12 ' , ' % Y-% % D% H M-:% M:% S ' ) 
a time.struct_time (tm_year = 2017, tm_mon =. 5, tm_mday =. 8, tm_hour =. 17, tm_min =. 3, tm_sec = 12 is, tm_wday = 0, tm_yday = 128, -1 = the tm_isdst )

 . 6 . tuple time format into a string format time
 >>> that time.asctime ()
 ' Tue On May 2017 15:23:21. 9 ' 
>>> X = time.localtime ()
 >>> time .asctime (X)
 ' Tue On May 2017 15:23:39. 9 '
 
. 7 . time stamp format converted into a string
 >>> time.ctime()
'Tue May  9 16:07:24 2017'
>>> time.ctime(987867475)
'Sat Apr 21 23:37:55 2001'

Format Reference:

String Features
%a Local (locale) to simplify the days of the week
%A Local full weekday name
%b Local simplify the name of the month
%B Local full month name
%c The corresponding local date and time representation
%d The first few days of the month (01--31)
%H The first few hours (24-hour format, 00--23) of the day
%I The first few hours (12-hour clock, 01--12)
%j The first day of the year (001--366)
%m Month (01--12)
%M The number of minutes (00--59)
%p Am or pm corresponding local character of
%S Sec (01--61)
%w The first few days of the week (0 - 6,0 Sunday)
%W % U and basically the same, except that% W with a week beginning Monday.
%x Local corresponding date
%X Local response time
%Y Removing the century years (00--99)
%Y Full year
%FROM Time zone name (if there is no null character)
%% %'character
% U Weeks of the year. (00--53, Sunday is the beginning of a week.) All days prior to the first Sunday in Week 0

time module 1.png

time module 2.png

datetime module

import datetime
1.返回当前时间
>>> datetime.datetime.now()
datetime.datetime(2017, 5, 9, 17, 7, 0, 514481)

2.时间戳转换成日期
>>> datetime.date.fromtimestamp(1178766678)
datetime.date(2007, 5, 10)

3.当前时间+3天
>>> datetime.datetime.now() + datetime.timedelta(+3)
datetime.datetime(2017, 5, 12, 17, 12, 42, 124379)

4.当前时间-3天
>>> datetime.datetime.now() + datetime.timedelta(-3)
datetime.datetime(2017, 5, 6, 17, 13, 18, 474406)

5.当前时间+3小时
>>> datetime.datetime.now() + datetime.timedelta(hours=3)
datetime.datetime(2017, 5, 9, 20, 13, 55, 678310)

6.当前时间+30分钟
>>> datetime.datetime.now() + datetime.timedelta(minutes=30)
datetime.datetime(2017, 5, 9, 17, 44, 40, 392370)

 

Guess you like

Origin www.cnblogs.com/jehuzzh/p/12340022.html