Python--day6
Learning to have a position, clear goals to learn. I hope I can stick to it and get something---leaves
PYTHON05--Python functions, file storytelling, and modules and flask Web frameworks
I. file processing and related functions
Open (file path, open mode) or with open () as F:
Read/write, append files
Database
Write a dictionary #configparse configuration file
Read ReadLine ReadLines
Write Writelines
File pointer function
The location of the tell () file pointer
Seek () Move pointer
Flush () writes memory to the hard disk
Second, function
**python Core thought: Functional programming ==> Focus
2.1 Function Concepts
Functions: Basic elements in Python programming
def function name (parameter can be ignored):
function Body
return value
The main understanding of the 2.2 function: Functional programming FP
One, note the difference between Hello and hello (), the former is an abstract function, the latter is a specific return value
The function can be a parameter to pass
# #代码示例
in [1]: def fishing (): ...: print "diaoyu is good " ...: return ' Fish ' in [3]: fishing () diaoyu is good out[3]: ' fish ' in [5]: def dosth (FN): ...: print fn () + ' is got ' ...: return ' OK ' in [6] : dosth (fishing) diaoyu is good fishis got out[6]: ' OK ' # # #sorted () The function is implemented with the Lambda anonymous function to sort in [7]: arr = [(' age ', 1), (' Age ', (), "(' Age ',"), (' Age ', "31"), (' Age ', ' + '), (' Age ',]in [8]: print sorted), (' Age ', ' a ') (Arr,key=lambda x:x[1]) [(' Age ', 1), (' Age ', 12), ("Age", 13), (' age ', 19), (' age ', 31), (' age ', 41), (' age ', 51)]
Third, the module
3.1 Module definition
Module: Simple understanding, the module is the file, we can put a function in a file, through the import call.
3.2 Classification of modules
The modules are divided into three categories as a whole:
1.python modules, such as Sys,os,math,random,time modules
2. The module developed by the third party developer can be installed via PIP install, and then import call is used, such as flask,requests,pyquery,psutil, etc.
3. Self-developed modules
extension: Python folder by default can not be imported, if you need to import a folder, you need to add __init__.py file, __init__.py file can be empty, its main purpose is to make the folder into a package to achieve the purpose of import.
Call format: From "File directory name" Import imported function "
# #自己开发的模块示例:
# #自己编写的模块 [[email protected] code]# pwd/usr/local/src/newbie/05/code# #文件编写的模块内容 [[email protected] code]# cat Hello.pydef Hello_world ():p rint "Hello World" # #ipython中导入模块及使用In [1]: Import Helloin [3]: Print Hello.hello_world () Hello Worldnonein [4]: Pwdout[4]: U '/usr/local/src/newbie/05/code '
3.3 How the module is called
There are three main ways to use the module:
1.import Module Name
Call: module name. function name ()
2. From Module name Import function name
Call: Function name ()
3. From Module name import *
Call: Function name () #导入模块中所有函数, deprecated
3.3.1 Search Path order for import:
Current Path---> Systempath (System path)
1. Current Path: Current directory
2.systempath
3. Do not repeat the file name with the module name to avoid calling the exception
# #systempath View method: Import Syssys.pathin [7]: sys.pathout[7]:[', '/usr/local/python27/bin ', '/usr/local/python27/lib /python27.zip ', '/usr/local/python27/lib/python2.7 ', '/usr/local/python27/lib/python2.7/plat-linux2 ', '/usr/local /python27/lib/python2.7/lib-tk ', '/usr/local/python27/lib/python2.7/lib-old ', '/usr/local/python27/lib/python2.7 /lib-dynload ', '/usr/local/python27/lib/python2.7/site-packages ', '/usr/local/python27/lib/python2.7/ Site-packages/ipython/extensions ', '/root/.ipython ']
3.3.2 * * Note that import will execute the code in the module again.
#示例代码
[[email protected] code]# python run.py Welcome Module hellohello worldnone[[email protected] code]# cat hello.pydef Hello _world ():p rint "Hello World" print "Welcome Module hello" [[email protected] code]# cat run.py import Helloprint Hello.hello _world () [[email protected] code]# [[email protected] code]# python run.py Welcome Module Hellohello worldnone
Contradictions in 3.3.3 Import module
Problems, such as the code in 3.3.2, if introduced I do not want to execute the Hello_world () function outside the print statement, how to solve it.
In order to solve what is not needed in the Introduction module when the module is introduced, Python adds a judgment to resolve
if __name__ = = "__main__":
code block
The code that is introduced into the module itself needs to be executed within the if judgment to prevent the invocation from being arbitrarily executed.
#示例代码如下:
# #run. py content [[email protected] code]# cat run.py import Helloprint Hello.hello_world () # #hello. py contents [[Email protected] code]# cat Hello.pydef Hello_world ():p rint "Hello world" if __name__ = = ' __main__ ': #<==== practice __name__ variable print "Welcome Mo Dule hello "[[email protected] code]# python run.py hello worldnone[[email protected] code]#
3.3.4 python comes with variable __name__
__NAME__: This variable is primarily intended to identify whether the. py file executed is performed by the function's file itself, and if __name__ = = "__main__" proves to be executed by itself, otherwise the function is called to execute. At this time __name__ equals the name of the file being called.
# # # #__name__ == "__main__" Situation # #主文件的内容 [[Email protected] code]# cat hello.pydef hello_world ():p rint "Hello world" print " __name__ == '%s ' " % (__ name__) if __name__ == ' __main__ ': Hello_world () print "Welcome module hello" [[ email protected] code]# ## #执行主文件的结果 [[email protected] code]# python hello.pyhello world __name__ == ' __main__ ' welcome module hello### Invoked execution: # # #被引用的hello. py file [[Email protected] code]# cat hello.pydef hello_world (): print "Hello world" print " __name__ == '%s ' " % (__name__) if __ name__ == ' __main__ ': Hello_world () print "Welcome module hello" [[email protected] code]# ## #主程序文件run. PY content [[email protected] code]# cat run.py import Helloprint hello.hello_world() # #程序的执行结果, the __name__ is equal to the name of the file being called, if the non-main program executes. [[email protected] code]# python run.py __name__ == ' Hello ' hello worldnone
Iv. Web Development flask Module
4.1 Installing flask
Pip install-y Flask
# #安装flask [[email protected] code] #pip install-y Flask # #查看flask是否安装 [[email protected] code]# pip list |grep flask flask ( 0.11.1) Flask-jsonrpc (0.3.1) [[email protected] code]#
Introduction to 4.2 Web development
With the outbreak of scripting languages such as PHP, Python, JavaScript, and so on, web development has entered a new era.
URL: Uniform Resource Identifier
Protocol://Domain: Port/resource address? Carry parameters 1=2& parameter 2=3# page Anchor point
URI: Uniform Resource Locator
Network protocol Five Tuples (network protocol, User IP, user port, service IP, service port)
Comparison of Flask and djiago of web development Framework
Flask:
Concise, quick to get started. Third-party customization Easy
Django:
Chatty, it's hard to use a third-party customization, but Django is full. The documentation is comprehensive.
4.3 first Flask program
Flask Call Format:
[[email protected] code]# cat flask_web.py# introduced flask boot module, write dead from flask import flask# #new app (create a new Flask app also written dead) app = Flask (__name__) # #监听路由, is the URL in the domain name and the port after the # #当域名和端口后面, only one/time, this route triggers @app.route ('/') # #路由对应的处理函数, when the route is triggered to execute the # processing function return value, Display in Browser def index (): Return "Hello index" @app. Route ('/test ') def Test (): Return "Hello Test" if __name__ = = "__main__": # #启动app, listening port portapp.run (host= ' 0.0.0.0 ', port=888,debug=true)
The results are as follows: Note that the return value of the processing function after the start of the listening route is displayed on the browser side
This article from "11931192" blog, declined reprint!
Learn Python-day6 from scratch