What are the common Python libraries?

王林
Release: 2023-04-27 16:40:07
forward
1109 people have browsed it

1. difflib

五个常用的 Python 库

difflib is a Python module focused on comparing data sets (especially strings). To get a concrete idea of ​​a few things you can do with this module, let's examine some of its most common functions.

SequenceMatcher

SequenceMatcher is a function that compares two strings and returns data based on their similarity. By using ratio() we will be able to quantify this similarity in terms of ratio/percentage.

Syntax:

SequenceMatcher(None, string1, string2)
Copy after login

The following simple example shows the function of this function:

from difflib import SequenceMatcher
phrase1 = "Tandrew loves Trees."
phrase2 = "Tandrew loves to mount Trees."
similarity = SequenceMatcher(None, phrase1, phrase2)
print(similarity.ratio())
# Output: 0.8163265306122449
Copy after login

get_close_matches

Next is get_close_matches, which returns the same The closest match to the string passed in as argument.

Syntax:

get_close_matches(word, possibilities, result_limit, min_similarity)
Copy after login

Here’s an explanation of these potentially confusing parameters:

word is the target word that the function will look at.

possibilities is an array containing the matches that the function will look for and find the closest match.

result_limit is the limit on the number of results returned (optional).

min_similarity is the minimum similarity that two words need to have to be considered a return value by the function (optional).

Here is an example of its use:

from difflib import get_close_matches
word = 'Tandrew'
possibilities = ['Andrew', 'Teresa', 'Kairu', 'Janderson', 'Drew']
print(get_close_matches(word, possibilities))
# Output: ['Andrew']
Copy after login

In addition there are several other methods and classes belonging to Difflib that you can check out: unified_diff, Differ and diff_bytes

2. sched

sched is a useful module that centers on event scheduling that works across platforms, in contrast to tools like the task scheduler on Windows. Most of the time when using this module, you will use the schedular class.

The more common time module is usually used together with sched because they both deal with the concepts of time and scheduling.

Create a schedular instance:

schedular_name = sched.schedular(time.time, time.sleep)
Copy after login

Various methods can be called from this instance.

When run() is called, the events/entries in the scheduler will be called in order. This function usually appears at the end of the program after the event has been scheduled. In addition, search the public account Linux to learn how to reply "git books" in the background and get a surprise gift package.

enterabs() is a function that essentially adds events to the scheduler's internal queue. It receives several parameters in the following order:

  • The time when the event is executed
  • Activity priority
  • The event itself (a function)
  • Event Parameters of the function
  • Keyword parameter dictionary of the event

Here is an example of how to use these two functions together:

import sched
import time
def event_notification(event_name):
print(event_name + " has started")
my_schedular = sched.scheduler(time.time, time.sleep)
closing_ceremony = my_schedular.enterabs(time.time(), 1, event_notification, 
("The Closing Ceremony", ))
my_schedular.run()
# Output: The Closing Ceremony has started
Copy after login

There are also several extensions Functions used by the sched module: cancel(), enter(), and empty().

3. binaascii

binaascii is a module for converting between binary and ASCII.

b2a_base64 is a method in the binaascii module that converts base64 data to binary data. Here is an example of this approach:

import base64
import binascii
msg = "Tandrew"
encoded = msg.encode('ascii')
base64_msg = base64.b64encode(encoded)
decode = binascii.a2b_base64(base64_msg)
print(decode)
# Output: b'Tandrew'
Copy after login

This code should be self-explanatory. Simply put, it involves encoding, converting to base64, and converting it back to binary using the b2a_base64 method.

Here are some other functions that belong to the binaascii module: a2b_qp(), b2a_qp(), and a2b_uu().

4. tty

tty is a module containing several utility functions that can be used to deal with tty devices. Here are its two functions:

setraw() changes the mode of the file descriptor in its argument (fd) to raw.

setcbreak() changes the mode of the file descriptor in its argument (fd) to cbreak.

This module is only available on Unix due to the need to use the termios module, such as specifying the second parameter (when=termios.TCSAFLUSH) in the above two functions.

5. weakref

weakref is a module for creating weak references to objects in Python.

Weak references are references that do not protect a given object from being collected by the garbage collection mechanism.

The following are two functions related to this module:

  • getweakrefcount() accepts an object as a parameter and returns the number of weak references that refer to the object.
  • getweakrefs() accepts an object and returns an array containing all weak references that refer to the object.

Usage examples of weakref and its functions:

import weakref
class Book:
def print_type(self):
print("Book")
lotr = Book
num = 1
rcount_lotr = str(weakref.getweakrefcount(lotr))
rcount_num = str(weakref.getweakrefcount(num))
rlist_lotr = str(weakref.getweakrefs(lotr))
rlist_num = str(weakref.getweakrefs(num))
print("number of weakrefs of 'lotr': " + rcount_lotr)
print("number of weakrefs of 'num': " + rcount_num)
print("Weakrefs of 'lotr': " + rlist_lotr)
print("Weakrefs of 'num': " + rlist_num)
# Output:
# number of weakrefs of 'lotr': 1
# number of weakrefs of 'num': 0
# Weakrefs of 'lotr': []
# Weakrefs of 'num': []
Copy after login

Output We can see its effect from the output function return value. Since num has no weak references, the array returned by getweakrefs() is empty. Extension: Taking over private work

Here are some other functions related to the weakref module: ref(), proxy(), and _remove_dead_weakref().

The above is the detailed content of What are the common Python libraries?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template