In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the development skills of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what development skills does Python have"?
1. How do I view the source code in the running state?
Looking at the source code of the function, we usually use IDE to do it.
In PyCharm, for example, you can Ctrl + click to enter the source code of the function.
What if there is no IDE?
When we want to use a function, how do we know which parameters the function needs to receive?
When we have a problem using a function, how can we troubleshoot the problem by reading the source code?
At this point, we can use inspect instead of IDE to help you do these things.
# demo.py import inspect def add (x, y): return x + y print ("=") print (inspect.getsource (add))
The running result is as follows
$python demo.py = def add (x, y): return x + y
two。 How do I turn off the exception auto-correlation context?
When you are dealing with an exception, when you throw another exception due to improper handling or other problems, the exception thrown will also carry the original exception information.
Just like this.
Try: print (1 / 0) except Exception as exc: raise RuntimeError ("Something bad happened")
You can see two abnormal messages from the output
Traceback (most recent call last): File "demo.py", line 2, in print (1 / 0) ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "demo.py", line 4, in raise RuntimeError ("Something bad happened") RuntimeError: Something bad happened
If an exception is thrown in an exception handler or finally block, by default, the exception mechanism implicitly appends the previous exception to the _ _ context__ attribute of the new exception. This is the automatic association exception context enabled by Python by default.
If you want to control the context yourself, you can add the from keyword (there is a restriction in the from syntax that the second expression must be another exception class or instance.) To indicate which exception your new exception is directly caused by
Try: print (1 / 0) except Exception as exc: raise RuntimeError ("Something bad happened") from exc
The output is as follows
Traceback (most recent call last): File "demo.py", line 2, in print (1 / 0) ZeroDivisionError: division by zero The above exception was the direct cause of the following exception: Traceback (most recent call last): File "demo.py", line 4, in raise RuntimeError ("Something bad happened") from exc RuntimeError: Something bad happened
Of course, you can also set the context _ _ context__ property for the exception through the with_traceback () method, which can also better display the exception information in traceback.
Try: print (1 / 0) except Exception as exc: raise RuntimeError (bad thing) .with_traceback (exc)
Finally, what if I want to completely turn off this mechanism for automatically associating exception contexts? Is there anything I can do?
You can use raise...from None, and from the following example, there are no original exceptions
$cat demo.py try: print (1 / 0) except Exception as exc: raise RuntimeError ("Something bad happened") from None $python demo.py Traceback (most recent call last): File "demo.py", line 4, in raise RuntimeError ("Something bad happened") from None RuntimeError: Something bad happened (PythonCodingTime)
03. The fastest way to view the package search path
When you use import to import a package or module, Python will look in some directories, which are in order of priority, and normal people will use sys.path to view them.
> import sys > from pprint import pprint > pprint (sys.path) [','/ usr/local/Python3.7/lib/python37.zip','/ usr/local/Python3.7/lib/python3.7','/ usr/local/Python3.7/lib/python3.7/lib-dynload','/ home/wangbm/.local/lib/python3.7/site-packages','/ usr/local/Python3.7/lib/python3.7/site-packages'] >
Is there a faster way?
Do I have a method that doesn't even have to enter console mode?
You might think of this, but it's essentially no different from the above.
[wangbm@localhost ~] $python-c "print ('\ n'.join (_ _ import__ ('sys') .path)" / usr/lib/python2.7/site-packages/pip-18.1-py2.7.egg / usr/lib/python2.7/site-packages/redis-3.0.1-py2.7.egg / usr/lib64/python27.zip / usr/lib64/python2.7/ usr/lib64/python2.7/plat-linux2 / usr/lib64/python2 . 7/lib-tk / usr/lib64/python2.7/lib-old / usr/lib64/python2.7/lib-dynload / home/wangbm/.local/lib/python2.7/site-packages / usr/lib64/python2.7/site-packages/ usr/lib64/python2.7/site-packages/gtk-2.0 / usr/lib/python2.7/site-packages
What I'm going to introduce here is a much more convenient method than both of the above, which can be solved with one command.
[wangbm@localhost ~] $python3-m site sys.path = ['/ home/wangbm','/ usr/local/Python3.7/lib/python37.zip','/ usr/local/Python3.7/lib/python3.7','/ usr/local/Python3.7/lib/python3.7/lib-dynload','/ home/wangbm/.local/lib/python3.7/site-packages' '/ usr/local/Python3.7/lib/python3.7/site-packages',] USER_BASE: / home/wangbm/.local' (exists) USER_SITE:' / home/wangbm/.local/lib/python3.7/site-packages' (exists) ENABLE_USER_SITE: True
From the output, you can see that the path of this column is more complete than sys.path, which contains the directory of the user environment.
4. Write a nested for loop as a single line
We often have nested for loop code like this
List1 = range (1, 3) list2 = range (4, 6) list3 = range (7, 9) for item1 in list1: for item2 in list2: for item3 in list3: print (item1+item2+item3)
Here are just three for loops, and in the actual coding, there may be more layers.
This kind of code, the readability is very poor, many people do not want to write this, but there is no better way to write.
Here is a common way of writing that uses the library itertools to achieve more elegant and easy-to-read code.
From itertools import product list1 = range (1p3) list2 = range (4p6) list3 = range (7pc9) for item1,item2,item3 in product (list1, list2, list3): print (item1+item2+item3)
The output is as follows
$python demo.py 12 13 13 14 14 15
5. How to use print to output logs
Beginners like to use print to debug code and record the running process of the program.
However, print will only output the content to the terminal and cannot be persisted to the log file, which is not conducive to troubleshooting.
If you are keen to use print to debug your code (although this is not a best practice) and record how the program is running, the print usage described below may be useful to you.
As a function, print in Python 3 becomes more powerful because it can receive more parameters. Specify some parameters to output the contents of print to the log file.
The code is as follows:
With open ('test.log', mode='w') as f:... Print ('hello, python', file=f, flush=True) > exit () $cat test.log hello, python
6. How to quickly calculate the running time of a function
Calculate the run time of a function, and you might do this
Import time start = time.time () # run the function end = time.time () print (end-start)
Look how many lines of code you have written to calculate the running time of the function.
Is there a way to calculate the run time more easily?
Yes.
There is a built-in module called timeit
Use it with only one line of code
Import time import timeit def run_sleep (second): print (second) time.sleep (second) # use only this line print (timeit.timeit (lambda: run_sleep (2), number=5))
The running result is as follows
2 2 2 10.020059824
7. Improve efficiency by using built-in caching mechanism
Caching is a way to save quantitative data to meet the needs of subsequent acquisition, which aims to speed up the speed of data acquisition.
The data generation process may need to be calculated, organized, remote acquisition and other operations. If the same data needs to be used many times, it will be a waste of time to regenerate each time. Therefore, if the data obtained by operations such as calculations or remote requests are cached, the subsequent data acquisition requirements will be accelerated.
To implement this requirement, Python 3.2 + provides us with a mechanism that can be easily implemented without requiring you to write such logic code.
This mechanism is implemented in the lru_cache decorator in the functool module.
@ functools.lru_cache (maxsize=None, typed=False)
Parameter interpretation:
Maxsize: the maximum number of calls to this function can be cached. If None, there is no limit. When set to a power of 2, the performance is the best.
Typed: if True, calls of different parameter types are cached separately.
For instance
From functools import lru_cache @ lru_cache (None) def add (x, y): print ("calculating:% s +% s"% (x, y)) return x + y print (add (1,2)) print (add (1,2)) print (add (2,3))
The output is as follows, you can see that the second call does not really execute the function body, but directly returns the result in the cache
Calculating: 1 + 2 3 3 calculating: 2 + 3 5
The following is the classic Fibonacci series, and when you specify a large n, there will be a lot of double counting.
Def fib (n): if n
< 2: return n return fib(n - 2) + fib(n - 1) 第六点介绍的 timeit,现在可以用它来测试一下到底可以提高多少的效率。 不使用 lru_cache 的情况下,运行时间 31 秒 import timeit def fib(n): if n < 2: return n return fib(n - 2) + fib(n - 1) print(timeit.timeit(lambda :fib(40), number=1)) # output: 31.2725698948 由于使用了 lru_cache 后,运行速度实在太快了,所以我将 n 值由 30 调到 500,可即使是这样,运行时间也才 0.0004 秒。提高速度非常显著。 import timeit from functools import lru_cache @lru_cache(None) def fib(n): if n < 2: return n return fib(n - 2) + fib(n - 1) print(timeit.timeit(lambda :fib(500), number=1)) # output: 0.0004921059880871326 8. 在程序退出前执行代码的技巧 使用 atexit 这个内置模块,可以很方便的注册退出函数。 不管你在哪个地方导致程序崩溃,都会执行那些你注册过的函数。 示例如下If the clean () function has arguments, you can call atexit.register (clean_1, argument 1, argument 2, argument 3, arguments xxxx') without a decorator.
You may have other ways to deal with this requirement, but it is certainly more elegant and convenient than not using atexit, and it is easy to extend.
But there are still some limitations to using atexit, such as:
If the program is killed by a system signal that you have not processed, the registered function will not execute properly.
If a serious Python internal error occurs, the function you registered will not execute properly.
If you call os._exit () manually, the function you registered will not execute properly.
9. Implement deferred calls similar to defer
There is a mechanism for delaying calls in Golang with the keyword defer, such as the following example
Import "fmt" func myfunc () {fmt.Println ("B")} func main () {defer myfunc () fmt.Println ("A")}
The output is as follows, and the call to myfunc is completed one step before the function returns, even if you write the call to myfunc on the first line of the function, which is the deferred call.
A B
So is there such a mechanism in Python?
Of course there are, but it's not as simple as Golang.
You can use the context manager to achieve this effect in Python
Import contextlib def callback (): print ('B') with contextlib.ExitStack () as stack: stack.callback (callback) print ('A')
The output is as follows
A B
10. How to read G super-large files by stream
Use with...open... You can read data from a file, which is familiar to all Python developers.
But if you use it improperly, it will also cause a lot of trouble.
For example, when you use the read function, in fact, Python will load all the contents of the file into memory at once. If the file has 10 gigabytes or more, then your computer will consume a lot of memory.
# read with open ("big_file.txt", "r") as fp: content = fp.read ()
For this problem, you may want to use readline to make a generator to return row by line.
Def read_from_file (filename): with open (filename, "r") as fp: yield fp.readline ()
But if the content of this file is only one line, one line is only 10 gigabytes, in fact, you will still read all the contents at once.
The most elegant solution is to specify that only a fixed size of content is read at a time when using the read method, as in the following code, only 8kb returns are read at a time.
Def read_from_file (filename, block_size = 1024 * 8): with open (filename, "r") as fp: while True: chunk = fp.read (block_size) if not chunk: break yield chunk
There is no problem with the function of the above code, but the code still looks a little bloated.
The code can be optimized with the help of partial functions and iter functions.
From functools import partial def read_from_file (filename, block_size = 1024 * 8): with open (filename, "r") as fp: for chunk in iter (partial (fp.read, block_size), "): yield chunk Thank you for your reading. This is the content of" what are the development skills of Python ". After the study of this article, I believe you have a deeper understanding of the development skills of Python. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.