Python module and python Module

Source: Internet
Author: User

Python module and python Module

Module, which implements a set of code for a function with a lump of code.

Similar to functional programming and process-oriented programming, functional programming completes a function. Other code can be called to provide code reusability and code coupling. For a complex function, multiple functions may be required. in The py file), n. A collection of code composed of py files is called a module.

For example, OS is a system-related module, and file is a file-related module.

There are three modules:

  • Custom Module
  • Built-in modules
  • Open-source module
Custom ModuleAll the files whose names end with. py can be called as custom modules. before calling the file, you must first import the file and then use the features defined in the module. Import ModuleImport Module name from folder import module name as Alias # rename the imported module when importing the module, Python will search for it based on the path, and the path is stored in sys. path.
>>> import sys>>> sys.path['', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']>>>

The first space indicates searching in the current folder. If no space is found, search by the list path. Generally, we place third-party modules in C: \ Python27 \ lib \ site-packages (Linux/usr/lib/python2.7/site-packages/)

If the sys. path list does not have the desired path, you can use sys. path. append ('path') to add it.
Various directories can be obtained through the OS module, for example:

import sysimport ospre_path = os.path.abspath('../')sys.path.append(pre_path)
Install third-party LibrariesThere are two ways to download and install: 1. pip3 install requests yum apt-get 2. Source Code installation: Download the source code, unzip the package, switch to the current directory, and execute python setup. py install
Download the source code, decompress the source code, and go to the directory to compile the source code python setup. py build installation source code python setup. py install
The gcc compilation and Python development environments may be used when you download and install the source code. You must first execute: yum install gcc yum install python - devel Or apt - get python - devPython built-in modules 1. sys Provides system-related operation commands
Sys. argv command line parameter List. The first element is the program path sys. exit (n) exit the program. exit (0) sys when the program Exits normally. obtain the version information of the Python interpreter sys. the maximum Int value of maxint sys. path: return the search path of the module. during initialization, use the value of the PYTHONPATH environment variable sys. platform returns the name of the operating system platform sys. stdout. write ('Please: ') val = sys. stdin. readline () [:-1]
Ii. haslib

Used for encryption-related operations, instead of the md5 module and sha module, mainly provides SHA1, sha1_, SHA256, SHA384, SHA512, MD5 Algorithm

import hashlib # ######## md5 ######## hash = hashlib.md5()hash.update('admin')print hash.hexdigest() # ######## sha1 ######## hash = hashlib.sha1()hash.update('admin')print hash.hexdigest() # ######## sha256 ######## hash = hashlib.sha256()hash.update('admin')print hash.hexdigest()  # ######## sha384 ######## hash = hashlib.sha384()hash.update('admin')print hash.hexdigest() # ######## sha512 ######## hash = hashlib.sha512()hash.update('admin')print hash.hexdigest()

In order to avoid database hit attempts to reverse encryption information, we can add the key value to the encrypted information and add 'sale'

import hashlib  # ######## md5 ########  Hash = hashlib. md5 ('898oafs09f') # The content in the brackets is equivalent to 'yan' hash.update('admin') print hash.hexdigest()Some built-in modules have been written before. You can go to other blogs to check out the python built-in module-rePython built-in module: OS module python built-in module-json and picklepython built-in module-randomPython built-in module-time & datatimepython built-in module-collections module function _ name _ only when the current file is executed, _ main ____ file _ # path of the current file _ doc _ # comment _ cache _ # pyc file

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.