In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the common skills of Python programming". In daily operation, I believe many people have doubts about the common skills of Python programming. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the common skills of Python programming?" Next, please follow the editor to study!
First, string processing skills
1. Clean up user input
It is a common program requirement to clean up the entered values. For example, to do case conversion, to verify the injection of input characters, you can usually write regularities and use Regex to do special tasks. But for complex situations, you can use some techniques, such as the following:
User_input = "This\ nstring has\ tsome whitespaces...\ r\ n" character_map = {ord ('\ n'):', ord ('\ t'):', ord ('\ r'): None}
In this example, you can see that the space characters "\ n" and "\ t" are replaced with spaces, while "\ r" is deleted.
This is a simple example, and we can also use the unicodedata package and the combinin () function to generate a large mapping table to generate a mapping to replace the string.
two。 Prompt the user for input
A command line tool or script requires a user name and password to operate. A useful technique to use this feature is to use the getpass module:
Import getpass user = getpass.getuser () password = getpass.getpass ()
These three lines of code allow us to interact gracefully to remind the user to enter the password and capture the current system user and the entered password, and automatically block the display when entering the password to prevent it from being stolen.
3. Find string frequency
If you need to find words similar to some input strings, you can use difflib to do this:
Import difflib difflib.get_close_matches ('appel', [' ape', 'apple',' peach', 'puppy'], nasty 2)
# return ['apple',' ape']
Difflib.get_close_matches looks for the string with the most similar match. In this case, the first parameter matches the second parameter. Provides the optional parameter n, which specifies the maximum number of matches to return, and the parameter cutoff (default = 0.6) is set to thr to determine the score of the matching string.
4. Multiline string
Backslashes can be used in Python:
In [20]: multistr = "select * from test\...: where id
< 5" In [21]: multistr Out[21]: ' select * from test where id < 5' 还可以使用三引号: In [23]: multistr ="""select * from test ...: where id < 5""" In [24]: multistr Out[24]: 'select * test where id < 5' 上面方法共有的问题是缺少合适的缩进,如果我们尝试缩进会在字符串中插入空格。所以最后的解决方案是将字符串分为多行并且将整个字符串包含在括号中: In [25]: multistr = ("select * from multi_row " ...: "where row_id < 5 " ...: "order by age") In [26]: multistr Out[26]: 'select * from multi_row where row_id < 5 order by age' 5. 处理IP地址 日常常用的一个是验证和匹配IP地址,这个功能有个专门的模块ipaddress可以来处理。比如我们要用IP网段(CIDR用IP和掩码位)生成一个IP地址列表: import ipaddress net = ipaddress.ip_network('192.168.1.0/27') 结果: #192.168.1.0 #192.168.1.1 #192.168.1.2 #192.168.1.3 #... 另一个不错的功能IP地址是否在IP段的验证: ip = ipaddress.ip_address("192.168.1.2") ip in net # True ip = ipaddress.ip_address("192.168.1. 253") ip in net # False ip地址转字符串、整数值的互转: >> > str (ipaddress.IPv4Address ('192.168.0.1')) '192.168.0.1' > int (ipaddress.IPv4Address ('192.168.0.1')) 3232235521 > str (ipaddress.IPv6Address (':: 1'))':: 1' > int (ipaddress.IPv6Address (':: 1')) 1
Note that ipaddress also supports many other features, such as support for ipv4 and ipv6. For more information, please refer to the module documentation.
Second, performance optimization skills
1. Limit CPU and memory usage
If the Python program takes up too much resources and you want to limit the use of resources, you can use the resource package.
# CPU limit def time_exceeded (signo, frame): print ("CPU excess...") Raise SystemExit (1) def set_max_runtime (seconds): soft, hard = resource.getrlimit (resource.RLIMIT_CPU) resource.setrlimit (resource.RLIMIT_CPU, (seconds, hard)) signal.signal (signal.SIGXCPU, time_exceeded) # restrict memory usage def set_max_memory (size): soft, hard = resource.getrlimit (resource.RLIMIT_AS) resource.setrlimit (resource.RLIMIT_AS, (size, hard))
When you limit a CPU, you first get the soft limit and hard limit for a specific resource (RLIMIT_CPU), and then set it using the number of seconds specified by the parameter and the hard limit obtained. If the CPU time is exceeded, the signal that causes the system to exit will be registered.
For memory limits, you also get soft and hard limits and set them with setrlimit with the size parameter.
two。 Save memory through _ _ slots__
If there is a class in the program that needs to create a large number of instances, it may take up a lot of memory. Because Python uses dictionaries to represent the properties of class instances, this can speed up execution, but memory efficiency is poor, which is usually not a problem. You can use _ _ slots__ to optimize:
Import sys class FileSystem (object): def _ init__ (self, files, folders, devices): self.files = files self.folders = folders self.devices = devices print (sys.getsizeof (FileSystem)) class FileSystem1 (object): _ _ slots__ = ['files',' folders', 'devices'] def _ init__ (self, files, folders, devices): self.files = files self.folders = folders self.devices = devices print (sys.getsizeof (FileSystem1))
# Python 3.5
# 1-> 1016
# 2-> 888
When defining the _ _ slots__ attribute, Python uses a fixed-size array as the attribute instead of a dictionary, which greatly reduces the memory required for each instance. Of course, there are drawbacks to using _ _ slots__, for example, you cannot declare any new properties, and you can only use them on _ _ slots__, and classes of _ _ slots__ cannot use multiple inheritance.
3. Call with lru_cache cache function
It is said that the performance of Python is poor, especially in some computing, in fact, there are some general methods to solve the problems that the program can do, such as caching and memory. The problem of a large number of repeated iterative calls in iterative computation can be solved by using lru_cache in functools:
# CacheInfo (hits=2, misses=4, maxsize=32, currsize=4)
In the above example, we execute the GET request that is being cached (up to 3 cached results). The cache_info method is also used to check the cache information of the function. The decorator also provides a clear_cache method to delete the cache.
4. _ _ all__ controls import
Some languages support mechanisms for import members (variables, methods, interfaces). In Python, everything is import by default, but you can use _ _ all__ to limit
Def foo (): pass def bar (): pass _ _ all__ = ["bar"]
In this way we can limit what can be imported from some_module import *. In this example, only the import bar function. If you leave _ _ all__ blank, and when you use the wildcard import, nothing will be import and an AttributeError error will be triggered.
Object-oriented
1. Create objects that support With statements
We all know how to use open or close statements, such as opening a file or acquiring a lock, but how do we implement our own methods?
You can use the _ _ enter__ and _ _ exit__ methods to achieve:
Class Connection: def _ init__ (self):... Def _ _ enter__ (self): # Initialize connection... Def _ _ exit__ (self, type, value, traceback): # Close connection... With Connection () as c: # _ _ enter__ () executes... # conn.__exit__ () executes
This is the most common way to implement context management in Python, but there is a simpler way:
From contextlib import contextmanager @ contextmanager def tag (name): print (f "") yield print (f "") with tag ("H2"): print ("This is Title.")
The above code snippet implements the content management protocol using the contextmanager manager decorator. When you enter the with block, execute the first part of the marking function (before yield), then execute the block, and finally execute the rest of the marking function.
two。 The skill of overloading operational symbols
Considering that there are many comparison operators: _ _ lt__, _ _ le__, _ _ gt__, it can be annoying for a class to implement all comparison operators. You can use functools.total_ordering at this time:
From functools import total_ordering @ total_ordering class Number: def _ _ init__ (self, value): self.value = value def _ lt__ (self, other): return self.value
< other.value def __eq__(self, other): return self.value == other.value print(Number(20) >Number (3)) print (Number (1)
< Number(5)) print(Number(15) >= Number (15) print (Number (10) > > type (None) > type (abs)
The corresponding class type is returned for the class object type (). The following is to determine whether the type types of the two variables are the same:
> type (11) = = type (22) True > type ('abc') = = str True > type (' abc') = = type (33) False
Isinstance (): can show whether an object is of a certain type
Class Husty (Dog):... Pass. > > a = Animal () > > b = Dog () > > c = Husty () > isinstance (cMagneHusty) True > isinstance (cmaideDog) True > isinstance (cMagine Animal) True > isinstance (bMagazine) False
Husty is an object of type Husty, Dog, Animal, but Dog cannot be said to be an object of Husty.
Dir (): used to get all the methods and properties of an object. The return value is a list that contains a string:
> dir ('abc') [' _ _ add__','_ _ class__',. '_ _ hash__',' _ _ init__','_ _ I. Isalnum 'isidentifier',' islower',. 'translate',' upper', 'zfill']
Among them, properties and methods like _ _ xx__ have a special purpose. If you call the len () function view to get the length of an object, the _ _ len__ () method of the object is called automatically inside the len () function.
5. Iterator and slice
If you slice the Iterator directly, you will get TypeError, indicating that the generator object cannot be asked with a subscript, but there is a trick:
Import itertools s = itertools.islice (range (50), 10,20) for val in s:.
Using itertools.islice, you can create an islice object that is an iterator that generates the desired project. However, this consumes all generator items until slicing begins, and it also consumes all items in the islice object.
6. Skip some lines
Sometimes, you must use a known variable number of unwanted lines (such as comments). You can also use itertools:
String_from_file = "" / / Author:. / / License:. / Date:. Actual content... "" Import itertools for line in itertools.dropwhile (lambda line: line.startswith ("/ /"), string_from_file.split ("\ n"): print (line)
This code snippet generates lines only after the initial comment section. This method is useful if you only want to discard it at the beginning of the iterator and you don't know how many items are in it.
7. Named slicing
Using a large number of hard-coded index values can easily lead to tedious code and damage code readability. A common technique is to use constants for index values, but we can also use named slices:
In the example, you can see that you can index them by first naming them using the slice function, and then using them when cutting out a portion of the string. You can also use the properties of the slice object .start, .stop, and .step for more information.
Fourth, debugging skills
1. Script debugging
Script debugging for Python can be done using the pdb module. It allows us to set breakpoints at will in the script:
Import pdb pdb.set_trace ()
You can specify pdb.set_trace () and set breakpoints anywhere in the script, which is very convenient
two。 Debug programs in shell
In shell, you can start an interactive environment by using the-I option of python, where you can print run-time variable values and call functions, such as the following test.py script
Def func (): return 0 / 0 func ()
Run the script through python-I test.py in shell
We import pdb and then call pdb.pm () to start the debugger
It will show the place where the program crashes, where we exit the program and set a breakpoint:
Import pdb; def func (): pdb.set_trace () return 0 / 0 func ()
Run it again, it will stop at the breakpoint, step to the next step
In this way, we can debug and trace the execution of the program. By setting a breakpoint, and then when you run the program, execution stops at the breakpoint, and you can check the program, such as listing function parameters, evaluating expressions, listing variables, or step stepping.
Useful gadgets
1. One-click web service sharing
In Python, you can enable a HTTP server with one click of http.server, which is a very convenient sharing tool:
Python-m http.server
Open a server on the default listening port of 8000. You can customize the port, such as 8888.
Python-m http.server 8888
The code automatically completes the Jedi
Jedi is a library for automatic completion of Python code and static analysis. Jedi allows us to tap the code efficiently.
At present, Jedi has provided most of the editor plug-ins, including Vim (jedi-vim), VSC,Emacs,Sublime,Atom and so on.
two。 Beautify abnormal output pretty-errors
The default error output of Python is very messy, with a large head and poor readability. At this point, you need to use pretty-errors as an error beautification tool.
At this point, the study of "what are the common skills of Python programming" 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.