Python trivia point (4)--module-related

Source: Internet
Author: User
Tags hmac local time parent directory

1. Module :

definition : Used to logically organize python code (variables , functions, classes, logic: Implementing a function), which is essentially the end of A. py Python file (file name:test.py, corresponding module name:test).

Package : Used to logically organize the module, the essence is the folder (directory), must have a __init__.py file.

The essence of the import package is to interpret the __init__.py file under this package .

under a module you need to import the contents of a package, you need to modify the import method in the __init__.py file below the package, the syntax is:from. Import Module_name #. represents the current path under

Module Import Method :

Import module_name(module name)===>> The real export is the module name name.test ()

Import module_name,m1,m2

From module Import * (function)===> essence is to copy the code to the current location (not recommended)

From module import module_name,m1,m2

From module import module_name as MODELE_NAME_FROM_WFB (as alias )

Import Essence ( path Search and search path ):

(1)import module_name = = = All the code of the imported module is interpreted uniformly once, and then assigned to Module_ Name this variable, for example:module_name= "modele_name.py all Code". "module_name.name"

Import module_name----> execute path to module_name.py---->module_name.py --- Sys.path

(2)frommodule import module_name = ="Put the module module_ The name section is placed in the current file to be executed once. "name()"

Summary: The essence of the import module is to interpret the Python file again.

2. import files or packages that are not in the same directory:

because the import module or package is the essence of the system under the environment variable (path) to find the imported file or whether it exists, can be executed correctly, otherwise you need to add the absolute path of the file or package to the system's environment variables dynamically. Using the Sys,os module

Import Sys,os
Lujin=os.path.dirname (Os.path.abspath (__file__))

Sys.path.append (Lujin)

The purpose is to obtain the path (parent directory) of the specified file or package in the current file.

Explain:

Os.path.abspath (__file__) gets the absolute path to the current file

Os.path.dirname: Gets the parent directory of the current file .

Sys.path.append (Lujin): Adds the acquired path to the system environment variable.

3. Import optimization

(1) Import module_name

Module_name.name ().

The essence: First find the module, looking for the method under the module . "Finding more is inefficient. "

(2) from module_name import name

Name ():

essence: Take the method in the module to the specified position to execute it again. The process of finding multiple searches is less than import .

4. Module classification

A. Standard library (built-in)

1.  Time and datetime.

Gmtime (): Get Standard Time (0 time zone)

LocalTime (): get local time (East eight zone)

Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ()) #%Y corresponds to Tm_year%m corresponds to Tm.mon

5.json and pickle modules

module provides four functions: dumps , dump , loads , load

module provides four functions: dumps , dump , loads , load

The. JSON and pickle modules only recommend dumps and loads once, otherwise it is cumbersome to take them out in the order they were previously the dumps is serialized as a string.

6. Shelve Module

The shelve module is a simple k,v module that persists memory data through a file and can persist any python that pickle can support data format, "can be understood as a possible implementation of multiple dumps and, loads "

Use ="

Import shelve

D = Shelve.open (' shelve_test ') # open a file

Save:d["Key"]=value

Fetch:d,get ("key")

Cryptographic modules

Hashlib Module

Encryption in Chinese/ English:

Import Hashlib

M=HASHLIB.MD5 ()

M.update (b "Hello")

M.update ("It's Me Hello ". Encode (encoding= ' utf-8 '))

Encryption of the information:

Import HMAC

M=hmac.new (b ' King cover to the Tiger ', ' Pagoda Town River Demon ') encode (encoding= ' utf-8 ')

B ' King of the Land Tiger ': Must be bytes type

' Pagoda Town River Demon '. Encode (encoding= ' utf-8 ': encode the need to contain Chinese (encode) into binary. There is no difference between the main and the B.

Python trivia point (4)-module-related

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.