In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use the ecological tools of python". In the daily operation, I believe that many people have doubts about how to use the ecological tools of python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the ecological tools of python". Next, please follow the editor to study!
1. Start a download server in 1 second:
Python2:python-m SimpleHTTPServer
Python3:python-m http.server
Visit: http://192.168.191.100:8000/
2. Convert string to json
[root@localhost ~] # echo'{"job": "developer", "name": "Imx", "sex": "male"}'| python-m json.tool
{
"job": "developer"
"name": "Imx"
"sex": "male"
}
[root@localhost ~] # echo'{"address": {"province": "zhejiang", "city": "hangzhou"}, "name": "Imx", "sex": "male"}'| python-m json.tool
{
"address": {
"city": "hangzhou"
"province": "zhejiang"
}
"name": "Imx"
"sex": "male"
}
3. Check whether the third-party library is installed correctly
1) Interactive
[root@localhost ~] # python
Python 2.6.6 (r266 84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
> import paramiko
2)-c parameter
[root@localhost ~] # python-c "import paramiko"
4. Pip usage
Pip is a substitute for easy_install.
Pip is in the epel feed, so you need to download the epel feed.
Yum-y install epel-release
Yum install python-pip
Pip install-U pip upgrade pip version
1), search for installation packages
Pip search flask
2) install a specific version of the installation package
Pip install flask==0.8
3), delete the installation package
Pip uninstall Werkzeug
4), view the information of the installation package
Pip show flask
5) check whether the dependencies of the installation package are complete (the latest subcommands provided by pip 9.0.1)
Pip check flask
6), view the list of installed packages
Pip list
7) Export the list of installed packages on the system to the requirements file
Pip freeze > requirements.txt
8) install from requirements file
Pip install-r requirements.txt
9) use the pip command to complete
Pip completion-- bash > > ~ / .profile
Soure / .profile
After completion with the command, pip install will be automatically entered by typing pip I
5. Techniques for accelerating pip installation
1) installation of source acceleration software using Douban or Aliyun
Pip install-I https://pypi.douban.com/simple/ flask
It is troublesome to specify the image source address every time, which can be solved as follows:
Vim / .pip/pip.conf
[global]
Index-url = https://pypi.douban.com/simple/
2) download the software to the local deployment
# download to local
Pip install-- download='pwd'-r requirements.txt
Or
Pip install-download='pwd' flask
After ls, you can see that after downloading to the current directory, pip will automatically deal with the dependency problem, that is, download the dependency package as well.
# Local installation
Pip install-- no-index-f file://'pwd'-r requirements.txt
Or
Pip install-download='pwd' flask
6. Code completion plug-in
Finally found a plug-in, for Vim code block automatic completion support is very good. Recommend snipMate to everyone. SnipMate can help you implement Textmate-like functionality on vim, automatic code blocks are very powerful, and code blocks are customizable. We only need to edit the * .snippets file in the ~ / .vim / snippets/ directory to automatically generate the code according to our own requirements.
Use
Download snipMate and extract it to your Vim directory: ~ / .vim/
Download address: http://www.vim.org/scripts/script.php?script_id=2540
Create a test file:
[root@t-mysql01 .vim] # vim a.py
#! / bin/python
After entering for, press the tab key to complete the code automatically.
7. Edit prompt plug-in jedi-vim
Unlike snipmate, the plug-in is smarter, and jedi-vim is more affectionately known as programming hints rather than code completion plug-ins.
Installation method:
Environment:
Python 2.6 +
Vim > = 7.3
Install an administrative tool for the python plug-in, vim-pathogen or vundle, where pathogen is installed
Mkdir-p ~ / .vim/autoload ~ / .vim/bundle & &\
Curl-LSso ~ / .vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
Edit ~ / .vimrc file, add
Execute pathogen#infect ()
It is important to note here that if the vimrc file contains
Syntax on
Filetype plugin indent on
And so on, then put the above statement before these, such as the following:
Execute pathogen#infect ()
Syntax on
Filetype plugin indent on
Install jedi
Pip install jedi
Get jedi-vim
Cd ~ / .vim/bundle/ & & git clone-- recursive https://github.com/davidhalter/jedi-vim.git
8. Syntax checking and highlighting plug-ins
Syntastic is a plug-in used by Vim to verify syntax, validating files and presenting errors to users through an external syntax validator. This process can be done when needed, or automatically when the file is saved. "that's what the plug-in's official documentation says." If a syntax error is detected, it will prompt the user, because you can know the syntax error without compiling the code or executing the script, and the user will enjoy it. "
The installation process is similar to the method mentioned in the first part, you only need to run the following command:
Cd / .vim/bundle/
Git clone https://github.com/scrooloose/syntastic.git
Once you have successfully installed the plug-in (that is, the above command was successfully executed), you do not need to make any configuration-the plug-in is automatically loaded when Vim starts.
Now, open a source file and save it with the: W Vim command to use the plug-in. After waiting for a while, if there is a good syntax error in the source code, it will be highlighted.
9. Vim sets python automatic indentation
Set the automatic indentation of python in Vim:
A), edit vimrc file: enter command
Vim/ etc/vim/vimrc/
B), paste the following code and save it (basically, if seems to be a problem with the test)
Set filetype=python
Au BufNewFile,BufRead * .py,*.pyw setf python
Set autoindent "same level indent
Set smartindent "next level indent
Set expandtab
Set tabstop=4
Set shiftwidth=4
Set softtabstop=4
10. Use ipython for interactive programming
1), better editor
Yum install ipython
# ipython
In [10]: line = "Mysql slave binlog position: master host '10.173.33.35, filename' mysql-binlog0002',position '43242343243"
In [11]: line.split ("'")
Out [11]:
['Mysql slave binlog position: master host'
'10.173.33.35'
"filename"
'mysql-binlog0002'
"position"
'43242343243'
']
In [12]: host = line.split ("'") [1]
In [13]: print host
10.173.33.35
In [18]: filename = line.split ("'") [3]
In [19]: print filename
Mysql-binlog0002
In [21]: position = line.split ("'") [5]
In [22]: print position
43242343243
In [25]: print (host,filename,position)
('10.173.33.35,' mysql-binlog0002', '43242343243')
In [27]: position = int (position)
In [28]: type (position)
Out [28]: int
In [29]: print (host,filename,position)
('10.173.33.35,' mysql-binlog0002', 43242343243)
2) better access to help information
In [30]: import os
In [32]:? os.path.is*
Os.path.isabs
Os.path.isdir
Os.path.isfile
Os.path.islink
Os.path.ismount
In [33]: os.path.isfile?
Type: function
String Form:
File: / usr/lib64/python2.6/genericpath.py
Definition: os.path.isfile (path)
Docstring: Test whether a path is a regular file
In [35]: import json
In [36]: json.dump?
In [36]: json.dump?? / / use two question marks to get more comprehensive help information, and even include the source code for the implementation of the function
In [39]: d = dict
In [40]: json.dump?
In [42]: json.dumps (d) / / convert dictionary to json string
Out [42]:'{"a": 1, "c": 3, "b": 2}'
In addition to using the question mark to get the help information of the object, ipython also provides another way to get the object information, such as the definition, document and file of the object.
In [43]: import json
In [44]:% pdef json
Object is not callable.
In [45]:% pdef json.dump
Json.dump (obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, * * kw)
In [47]:% pfile json.dump
In [47]:% pdoc json.dump
In [49]:% pinfo json.dump
3), magic function
All functions provided by ipython start with%, and these functions become magic functions.
In [50]:% lsmagic or% to get all magic functions.
In [51]:% save? / / View help for the magic function
4), interact with the operating system
You can use% ls% pwd, etc., to execute commands under linux, or! ls! cd to execute commands under linux.
You can also capture the command test output by assignment:
In [7]: data =! df-h
In [9]: data
Out [9]:
['Filesystem Size Used Avail Use% Mounted on'
'/ dev/sda3 96G 3.9G 88G 5% /'
'tmpfs 1000M 68K 1000M 1% / dev/shm'
'/ dev/sda1 477m 41m 411m 9% / boot']
11. The use of jupyter notebook
[root@localhost ~] # pip install jupyter
If you report Python.h: No such file or directory, you can solve it by yum install python-devel.
At this point, the study on "how to use the ecological tools of python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.