Python module details (i)

Source: Internet
Author: User
Tags string format time and date timedelta

Python's modules are commonly referred to as libraries or class libraries in other languages, which is lib. It is the third level of programming language encapsulation, the fourth level is the package, that is, the package combination of modules, and the first two levels are functions and classes. The benefits of encapsulation, naturally needless to say, cohesion poly, loose coupling, reduce code duplication. At the same time, the module is also the "wheel" of the representative, most of the predecessors have been implemented and tested the efficient code combination, its existence so that we do not have to repeat the "build wheel", can use take doctrine. However, the individual thinks a qualified programmer, although not to repeat the wheel, but must have the ability to build wheels, at least you have to understand his man-made wheels.

Types of Python modules

In Python, this is usually the case with many methods and properties that make up a class, many of which make up a. py file, and many. py files form a module. Modules are generally divided into three types:

    • Custom Modules
    • Built-in Modules
    • Third-party module (open source module)

Custom modules: You write your own. py file, via the Import keyword, in another. py file is called, then it becomes a custom module.

Built-in modules: some of the most commonly used classic modules provided by Python, such as OS, SYS, time, logging, etc., are mature, efficient, and widely used.

Third-party modules: non-Python-provided modules, such as Django, requests. The popularity of Python and its more third-party open source modules are inseparable, this is a benign ecological circle.

Import of Python modules

In Python, modules are typically imported in four ways:

Import modulefrom module.xx.xx import xxfrom module.xx.xx import xx as rename from  module.xx.xx import *

  Importing a module, that is, importing a. py file, the interpreter interprets the file, and when a package is imported, the __init__.py file under the package is imported, and the interpreter interprets the file. Here is the concept of a package, usually we will be some of the modules dealing with a large class of problems in the same folder, easy to manage and use, folders may also have subfolders, then how to let the interpreter search for the correct module? Python designed __init__.py such a file (which can be empty, as long as the file name is exactly the same), which tells the interpreter that all the files in the folder it belongs to are modules.

The above four kinds of methods have each applicable occasion:

1. For built-in modules, it is better to use the first one, such as import Os,sys,datetime

2. For a class or function that needs to be called frequently within a module, using the second one is more appropriate and can reduce code input to some extent, for example:

First, we set up a module do_something as follows:

Def print_welcome ():    print ("Welcome to login!")

Then I'm going to call the Print_welcome function a lot in another file. When using import do_something, we call this:

Import Do_somethingdo_something.print_welcome () ... do_something.print_welcome (... do_something.print_) ..... Welcome ()

And, if you use the From do_something import Print_welcome, the use is more concise and convenient, we call it this way:

From do_something import print_welcomeprint_welcome () ... print_welcome () ... print_welcome ()

3. When the import of many modules have the same name in the class or function, etc., from the xxx import xxx as xxx this way is more useful, it is actually to the module to take the individual name. For example:

In the module module_1:

def func ():        print ("This is module_1!")

In the module module_2:

def func ():        print ("This is module_2!")

In other files, we can invoke this so that it does not cause a conflict:

From Module_1 import func as Afrom module_2 import func as BA () b ()

4. As for the method from XXX import *, we do not advocate.

Path to the Python module

The Python module, as a standalone file, must have its save path in the file system. When we import the module, the interpreter cannot search for the entire file system, it must maintain a collection of specified search paths, and we can only import the Python modules in these paths. So where does this collection of paths reside? In the path of the SYS module, you can view it in the following way:

Import Sysfor i in Sys.path:    print (i) Result: f:\python\pycharm\s13c:\python35\python35.zipc:\python35\dllsc:\ Python35\libc:\python35c:\python35\lib\site-packages

Its order is this, first the current file storage directory, then the Python installation directory, and then the Python directory in the Site-packages, in fact, the "custom"-"official"-"third party" directory. At the same time, because there are multiple directories, so in the custom module, the name of the module must pay attention to, do not and the official standard module and some of the more famous third-party module duplicate, a careless, easy to appear module import error occurs. (Note: Site-packages may not have the same name in different operating systems)

If the Sys.path path list does not have the path you want, it can be added by Sys.path.append (' path '). For example:

Import Sysimport Osnew_path = Os.path.abspath ('.. /') Sys.path.append (New_path)
Built-in module one, serialized JSON and Pickle

  JSON and The pickle module is primarily used to serialize Python data and save it as a string format. the use of the two methods and almost identical, are mainly used in the following four methods:

Dump () dumps () load () loads ()

JSON can be more cross-lingual, cross-platform, but only for the basic types of Python operation, the Python class is powerless. In addition, JSON data requires the string to be enclosed in double quotation marks, and no extra commas are allowed.  This is because in other languages, double quotes are caused by strings, single quotation marks are caused by characters; Python programmers habitually add a comma after the last element of a list, tuple, or dictionary, which is not allowed in JSON and requires special attention. Although pickle can only be used between Python programs, it can operate on all types, including classes. Therefore it is more suitable for the storage of complex data of Python itself.   Note: There may be compatibility issues between different versions of Python. The following are examples of JSON usage:
Import jsons = ' {' K1 ': ' v1 ', ' K2 ': 123} '      # A string similar to a dictionary structure must be enclosed in single quotes, while curly braces are required to use double quotes dic = Json.loads (s)                   # The loads method is to convert the JSON format string to Python's data structure print (DIC) print (DIC) Run result: {' K1 ': ' v1 ', ' K2 ': 123}<class ' dict ' >
Import jsondic = {"K1": "V1", "K2": 123}s = Json.dumps (DIC) print (s) print (type (s)) Run result: {"K1": "V1", "K2": 123}<class ' Str ' >

Unlike loads and dumps, the load and dump method is to read and write JSON-formatted strings within a file

Import jsondic = {"K1": "V1", "K2": 123}s = Json.dump (dic,open (' file ', ' W ')) Run Result: Generate file in sibling directory, save {"K1": "V1", "K2": 123 } string

Import jsons = json.load (open (' file ')) print (s) print (type (s)) Run result: {' K1 ': ' v1 ', ' K2 ': 123}<class ' dict ' >

Pickle is basically the same as JSON, but the difference is that its serialized string is not readable and is not as intuitive as JSON.

Import pickledic = {"K1": "V1", "K2": 123}s = Pickle.dumps (DIC) print (s) print (type (s)) Run Result: B ' \x80\x03}q\x00 (x\x02\x00\ X00\x00k1q\x01x\x02\x00\x00\x00v1q\x02x\x02\x00\x00\x00k2q\x03k{u. ' <class ' bytes ' >
second, time and datetime

The time module is a must for every programmer. In Python, time and datetime modules provide us with a number of times-processing methods. Among them, Time.time (), Time.sleep (), Time.localtime () are very common.  

Time provides functionality that is closer to the operating system level , and most functions call the function of the same name on the Platform C library , and revolve around Unix Timestamp , The date range that can be expressed is limited to 1970-2038, and it is better to use a datetime module if you need to process a date outside of the range.

The time module mainly consists of three types of times: Struct_time,timestamp and String_time. Understanding these three types, there is a sense of convergence through the various methods in the time module.

    • Timestamp (timestamp) seconds after January 1, 1970: Time.time ()
    • Formatted string (string_time) 2016-06-11 12:0, i.e.: Time.strftime ('%y-%m-%d ')
    • Structured time (Struct_time) includes: year, day, week, etc... namely: Time.localtime ()

The relationship between the three is as follows:

Here is a general introduction to the time module:

Import time# time.sleep (1) #让程序睡眠指定秒数, can be a floating-point number print (Time.time ()) #返回当前系统时间戳print (Time.ctime ()) #输出当前系统时间的字符串格式, which can be accepted when parameter of the inter-stamp format print (Time.ctime (Time.time () -86640)) #将时间戳转为字符串格式print (Time.asctime (Time.localtime ())) # Convert formatted time to string format print (Time.gmtime (Time.time () -86640)) #将时间戳转换成struct_time格式, GMT time, 8 hours with Beijing print ( Time.localtime (Time.time () -86640)) #将时间戳转换成struct_time格式, returning the local time print (Time.mktime (Time.localtime ())) # In contrast to the time.localtime () function, the struct_time format is reversed to the timestamp format print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ()) #将struct_ The time format is converted to the specified string format print (Time.strptime ("2016-01-28", "%y-%m-%d")) #将字符串格式转换成struct_time格式运行结果: 1465194377.782088Mon June 6 14:26:17 2016Sun June 5 14:22:17 2016Mon June 6 14:26:17 2016time.struct_time (tm_year=2016, tm_mon=6, Tm_mday=5, TM _hour=6, tm_min=22, tm_sec=17, tm_wday=6, tm_yday=157, tm_isdst=0) time.struct_time (tm_year=2016, tm_mon=6, Tm_mday=5, Tm_hour=14, tm_min=22, tm_sec=17, tm_wday=6, tm_yday=157, tm_isdst=0) 1465194377.02016-06-06 06:26:17time.struct_time (tm_year=2016, Tm_mon=1, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=28, Tm_isdst=-1) 

For timestamps, typically used to calculate the difference in time, a typical use is to test the program run time, for example:

Import timedef func ():    passt1 = Time.time () func () t2 = time.time () result = float (T2)-Float (T1) print (result)

For formatted strings: "%y-%m-%d%h:%m:%s", where each letter represents the meaning can be viewed in the official documents, not listed here.

And for a structured time object, its various properties are as follows:

Property Significance
Tm_year Years
Tm_mon Month
Tm_mday Day
Tm_hour Hours
Tm_min Minutes
Tm_sec Seconds
Tm_wday Day of the week, starting at 0
Tm_yday Number of days in the current day
Tm_isdst Whether daylight saving time, Default-1, indicates automatic judgment

 

Its various properties can be called through an object, for example:

Import times = Time.localtime () print ("Tm_year:  ", s.tm_year) Run Result: tm_year:   2016

Datetime
DateTime can be understood as a time-based encapsulation, which provides more useful functions. Several classes are included in the DateTime module, as follows:

    • Timedelta # primarily used to calculate time spans
    • Tzinfo # time Zone related
    • Time # only focuses on times
    • Date # focus on dates only
    • DateTime # has both time and date

In practical practice, the use of more is datetime.datetime and Datetime.timedelta. Use the Datetime.datetime.now () method to get an instance of a Datetime.datetime class at the current moment, which has the following properties and common methods:

    • Datetime.year
    • DateTime.Month
    • DateTime.Day
    • Datetime.hour
    • Datetime.minute
    • Datetime.second
    • Datetime.microsecond
    • Datetime.tzinfo
Import Datetimeti = Datetime.datetime.now () print (TI) print (Type (TI)) print (ti.year) print (ti.month) print (ti.day) Running result: 2016-06-06 14:36:00.763951<class ' Datetime.datetime ' >2016660

In addition to instantiating objects, the DateTime module itself provides a number of useful methods, such as:

Import Time,datetimeprint (Datetime.date.today ()) #输出格式 2016-06-06print (Datetime.date.fromtimestamp (Time.time ()- 864400)) #将时间戳转成日期格式current_time = Datetime.datetime.now () #实例化当前时间print (current_time) #输出2016-01-26 19:04:30.3 35935print (Current_time.timetuple ()) #返回struct_time格式 #datetime.replace ([year[, month[, day[, hour[, minute[, second[ , microsecond[, Tzinfo]]) print (Current_time.replace (2016,7,7)) #输出2016-07-07 14:06:24.074900, return to current time, But the specified value will be replaced by str_to_date = Datetime.datetime.strptime ("21/11/06 16:30", "%d/%m/%y%h:%m") #将字符串转换成日期格式new_date = Datetime.datetime.now () + Datetime.timedelta (days=10) #比现在加10天new_date = Datetime.datetime.now () + Datetime.timedelta (days=-10) #比现在减10天new_date = Datetime.datetime.now () + Datetime.timedelta (hours=-10) #比现在减10小时new_date = Datetime.datetime.now () + Datetime.timedelta (seconds=120) #比现在 +120sprint (new_date) Operating results: 2016-06-062016-05-272016-06-06 14:42:21.503262time.struct_time (tm_year=2016, tm_mon=6, tm_mday=6, tm_hour=14, Tm_min=42, Tm_sec=21, Tm_wday=0, tm_yday=158, tm_isdst=-1) 2016-07-07 14:42:21.5032622016-06-06 14:44:21.523263 

In fact, a Timedelta object can be obtained by subtracting two DateTime objects directly. It is also recommended that you use the Business_calendar module if you need to calculate the needs of your workday.

Third, logging log module

A log is a feature that each program should have to help you debug code, find problems, alert notifications, and so on. Python has a built-in log module called logging, which provides a standard log interface through which logs can be stored in a variety of formats, and logging logs are divided into,,, and debug info warning errorcritical五个级别。在logging内部,每个级别由一个数字表示如下:

  CRITICAL = 50   ERROR  = 40   WARNING  = 30   INFO  =  20   DEBUG  = 10   If you simply enter the log into the screen, you can do this:
Import Logging logging.warning ("User [Jack] attempted wrong password more than 3 times") logging.critical ("Server was down") #输出WARNING: Root:user [Jack] attempted wrong password more than 3 timesCRITICAL:root:server are down

If you want to write the log to a file, you can do so:

Import Logging  logging.basicconfig (filename= ' Log.log ', #这一行指定日志文件                    format= '% (asctime) s-% (name) s-% ( LevelName) s-% (module) s:  % (message) s ', # # This line controls the format of each log                    datefmt= '%y-%m-%d%h:%m:%s%p ',  # This line controls the time format                    level=10)  # System will only write information above level to file Logging.debug (' Debug ') logging.info (' info ') logging.warning (' warning ') Logging.error (' ERROR ') logging.critical (' critical ') logging.log (' log ')

For the output format, such as Asctime/name/levelname in the example above, there are other properties such as:

If you want to print the logs both on the screen and in the file log, you must have a deeper understanding of the logging module.

First, we need to understand the four parts of the logging module: loggers, handlers, filters, formatters.

Loggers: Provides interfaces for direct use of program code

Handlers: Send logger-generated logs to the desired destination

Filter: Used to filter logs, will match the conditions of the log output

Formatters: Format the log before output to match the format you want.

Here we look at a specific example:

Import logging# Create Loggerlogger = Logging.getlogger (' Jack ') # Use the GetLogger method and specify the user name Logger.setlevel (logging. DEBUG) # Set the global logging level to debug# create the handler to send to the screen, and set its local log level to DEBUGCH = logging. Streamhandler () Ch.setlevel (logging. DEBUG) # Create a handler to send to the log file and set its local log level to WARNINGFH = logging. Filehandler ("Access.log") # Sets the log file name to Access.logfh.setLevel (logging. WARNING) # Create a formatter format, you can create any kind of formatter = logging as needed. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') # Add Formatter format for previously created handlers Ch.setformatter ( Formatter) Fh.setformatter (formatter) # Add handlers for the logger that was created at the beginning, that is, the processing logic, a few handlers add several times Logger.addhandler (CH) Logger.addhandler (FH) # Below is the code for the Application logger.debug (' Debug Message ') Logger.info (' info message ') Logger.warn (' Warn message ') logger.error (' Error message ') logger.critical (' critical message ') screen display: 2016-06-06 16:53:37,554-jack-debug-debug message2016-06-06 16:53:37,555-jack-info-info message2016-06-06 16:53:37,555-jack-warning-warn message2016-06- 16:53:37,555-jack-error-Error message2016-06-06 16:53:37,555-jack-critical-critical messageaccess.log The contents of the log file: 2016-06-06 16:53:37,555-ja  Ck-warning-warn message2016-06-06 16:53:37,555-jack-error-error message2016-06-06 16:53:37,555-jack-critical -Critical message

  

In fact, it is the above process, step by step to complete it, logic is actually very simple, learning module is actually learning a software, nothing complicated.

Not to be continued

  

Python module details (i)

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.