In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "sharing Python skills". 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 "sharing Python skills".
1. "disinfect" the input string
The problem of "sterilizing" user input applies to almost all programs you write. Converting characters to lowercase or uppercase is usually sufficient, and sometimes you can use regular expressions to do the job, but for complex situations, there are better ways:
User_input = "This\ nstring has\ tsome whitespaces...\ r\ n" character_map = {ord ('\ n'):', ord ('\ t'):', ord ('\ r'): None} user_input.translate (character_map) # This string has some whitespaces... "
In this example, you can see that the space characters "\ n" and "\ t" are replaced by a single space, while "\ r" is completely deleted. This is a simple example, but we can go a step further and use the unicodedata library and its combining () function to generate a larger remap table (remapping table) and use it to remove all accents from the string.
2. Slice the iterator
If you try to slice the iterator directly, you will get TypeError, indicating that the object is not not subscriptable, but there is a simple solution:
Import itertoolss = itertools.islice (range (50), 10, 20) # for val in s:.
Using itertools.islice, we can create an islice object, which is an iterator that can generate what we need. But there is an important reminder that it consumes all the elements before slicing and in the slicing object islice.
(translation Note: for more information on iterator slices, read Python Advanced: iterators and iterator slices.)
3. Skip the start of iterable objects
Sometimes you have to deal with files that start with a variable number of unwanted lines (such as comments). Once again, itertools provides a simple solution:
String_from_file = "" / / Author:... / / License:... / Date:... Actual content... "" import itertoolsfor line in itertools.dropwhile (lambda line:line.startswith ("/ /"), string_from_file.split ("\ n")): print (line)
This code prints only what is after the initial comment section. This is useful if we only want to discard the beginning of the iterator (comments in this case) and don't know how much is there.
4. Functions that only support keyword arguments (kwargs)
When you need a function to provide (force) clearer parameters, it may be useful to create a function that supports only keyword arguments:
Def test (*, a, b): passtest ("value for a", "value for b") # TypeError: test () takes 0 positional arguments...test (a = "value", b = "value 2") # Works...
As you can see, you can easily solve this problem by placing a single * parameter before the keyword parameter. If we put the position parameter before the * parameter, it is obvious that we can also have the position parameter.
5. Create objects that support with statements
We all know how to use with statements, such as opening files or acquiring locks, but can we implement our own? Yes, we can use the _ _ enter__ and _ _ exit__ methods to implement the context manager protocol:
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@contextmanagerdef 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 decorator. The first part of the tag function (before yield) is executed when entering the with statement, then the code block of with is executed, and finally the rest of the tag function is executed.
6. Save memory with _ _ slots__
If you have ever written a program that creates a large number of instances of a class, you may have noticed that your program suddenly needs a lot of memory. That's because Python uses dictionaries to represent the properties of class instances, which makes it faster, but not very memory efficient. Usually this is not a problem, but if your program has a problem, you can try using _ _ slots__:
Class Person: _ _ slots__ = ["first_name", "last_name", "phone"] def _ _ init__ (self, first_name, last_name, phone): self.first_name = first_name self.last_name = last_name self.phone = phone
What happens here is that when we define the _ _ slots__ attribute, Python uses a fixed-size small array instead of a dictionary, which greatly reduces the memory required for each instance. There are some drawbacks to using _ _ slots__-we can't declare any new properties, and we can only use properties in _ _ slots__. Similarly, classes with _ _ slots__ cannot use multiple inheritance.
7. Limit CPU and memory usage
If you don't want to optimize program memory or CPU usage, but want to limit it directly to a fixed number, Python also has a library that can do this:
Import signalimport resourceimport os# To Limit CPU timedef time_exceeded (signo, frame): print ("CPU exceeded...") Raise SystemExit (1) def set_max_runtime (seconds): # Install the signal handler and set a resource limit soft, hard = resource.getrlimit (resource.RLIMIT_CPU) resource.setrlimit (resource.RLIMIT_CPU, (seconds, hard) signal.signal (signal.SIGXCPU, time_exceeded) # To limit memory usagedef set_max_memory (size): soft, hard = resource.getrlimit (resource.RLIMIT_AS) resource.setrlimit (resource.RLIMIT_AS, (size, hard))
Here, we can see two options to set the maximum CPU elapsed time and memory usage limit. For the CPU limit, we first get the soft limit and hard limit for that particular resource (RLIMIT_CPU), and then set it by the number of seconds specified by the parameter and the hard limit previously obtained. Finally, if the CPU time is exceeded, we will register the signal that exits the system. As for memory, we get the soft and hard limits again and set them with setrlimit with the size parameter and the hard limits obtained.
8. Control what can be import
Some languages have obvious mechanisms for exporting members (variables, methods, interfaces), such as Golang, which exports only members that begin with uppercase letters. On the other hand, in Python, everything is exported unless we use _ _ all__:
Def foo (): passdef bar (): pass__all__ = ["bar"]
Using the code snippet above, we can limit what from some_module import * can import when using it. For the above example, only bar is imported when the wildcard is imported. In addition, we can set _ _ all__ to empty so that it cannot export anything, and AttributeError will be thrown when imported from this module using wildcards.
9. A simple method of comparing operators
Implementing all the comparison operators for a class can be annoying because there are many comparison operators-- _ _ lt__, _ _ le__, _ _ gt__, or _ _ ge__. But what if there is an easier way? Functools.total_ordering can be saved:
From functools import total_ordering@total_orderingclass 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.valueprint(Number(20) >Number (3)) print (Number (1)
< Number(5))print(Number(15) >= Number (15) print (Number (10) > > import pdb > pdb.pm () # Post-mortem debugger > script.py (2) func ()-> return 0 / 0 (Pdb)
We've seen the crash, so now let's set a breakpoint:
Def func (): breakpoint () # import pdb; pdb.set_trace () return 0 / 0func ()
Now run it again:
Script.py (3) func ()-> return 0 / 0 (Pdb) # we start here (Pdb) stepZeroDivisionError: pision by zero > script.py (3) func ()-> return 0 / 0 (Pdb)
Most of the time, printing statements and error messages is enough for debugging, but sometimes you need to look around to understand what's going on inside the program. In these cases, you can set a breakpoint, and then the program execution will stop at the breakpoint, and you can check the program, such as listing function parameters, evaluating expressions, listing variables, or stepping only as shown above.
Pdb is a full-featured Python shell, and theoretically you can execute anything, but you also need some debugging commands, which can be found here [4].
15. Define multiple constructors in a class
Function overloading is a very common feature in programming languages (excluding Python). Even if you can't overload a normal function, you can still overload the constructor using class methods:
Import datetimeclass Date: def _ init__ (self, year, month, day): self.year = year self.month = month self.day = day @ classmethod def today (cls): t = datetime.datetime.now () return cls (t.year, t.month, t.day) d = Date.today () print (f "{d.day} / {d.month} / {d.year}")
You might prefer to put all the logic of the alternative constructor in _ _ init__, and use * args, * * kwargs, and a bunch of if statements, rather than using class methods. That may work, but it becomes difficult to read and maintain.
Therefore, I recommend putting very little logic into _ _ init__, and performing all operations in a separate method / constructor. In this way, all you get is clean code for both the maintainer and the user of the class.
16. Use the decorator to cache function calls
Have you ever written a function that performs expensive I _ swap O operations or some fairly slow recursion that might benefit from caching (storing) its results? If you do, then there is a simple solution, that is, to use functools's lru_cache:
From functools import lru_cacheimport requests@lru_cache (maxsize=32) def get_with_cache (url): try: r = requests.get (url) return r.text except: return "Not Found" for url in ["https://google.com/"," https://martinheinz.dev/", "https://reddit.com/"," https://google.com/", "https://dev.to/martinheinz", "https://google.com/"]: get_with_cache (url) print (get_with_cache.cache_info ()) # CacheInfo (hits=2, misses=4, maxsize=32, currsize=4)
In this example, we use cacheable GET requests (up to 32 cached results). You can also see that we can use the cache_info method to check the cache information of the function. The decorator also provides a clear_cache method to invalidate the cached results.
I would also like to point out that this function should not be used with functions that have side effects or with functions that create mutable objects with each call.
17. Find the elements that occur most frequently in iterable objects
Finding the most common elements in the list is a very common task. You can use for loops and dictionaries (map), but this is not necessary because there are Counter classes in the collections module:
From collections import Countercheese = ["gouda", "brie", "feta", "cream cheese", "feta", "cheddar", "parmesan", "parmesan", "cheddar", "mozzarella", "cheddar", "gouda", "parmesan", "camembert", "emmental", "camembert", "parmesan"] cheese_count = Counter (cheese) print (cheese_count.most_common (3)) # Prints: [(parmesan', 4), ('cheddar') 3), ('gouda', 2)]
In fact, Counter is just a dictionary that maps elements to the number of occurrences, so you can use it as a normal dictionary:
Print (cheese_count ["mozzarella"]) # Prints: 1cheese_count ["mozzarella"] + = 1print (cheese_count ["mozzarella"]) # Prints: 2
In addition, you can easily add more elements using the update (more_words) method. Another cool feature of Counter is that you can use mathematical operations (addition and subtraction) to combine and subtract Counter instances.
Thank you for your reading, the above is the content of "sharing Python skills", after the study of this article, I believe you have a deeper understanding of sharing Python skills, and the specific use needs to be verified in 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: 292
*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.