Posts

Showing posts from June, 2023

使用自定义类捕获 Python 控制台输出 - Capturing Console Output in Python Using Custom Class

在python中,当执行一段代码时,如果想要获取这段代码输出到控制台的信息的话,可以使用重定向stdout的方法。 In Python, if you want to capture the information output to the console when executing a piece of code, you can use the method of redirecting stdout. 首先,建立一个类,里面有两个函数,write和flush First, establish a class with two functions, write and flush. ```python class StdoutRedirect: def __init__(self): self.content = '' def write(self, text): self.content += text def flush(self): self.content = '' ``` 实例化这个类,并且重定向sys.stdout Instantiate this class and redirect sys.stdout to it. ```python my_stdout = StdoutRedirect() sys.stdout = my_stdout ``` 之后的任何输出都会进入my_stdout的content里。 Any subsequent output will go into the content of my_stdout. 在所需重定向输出的代码结束后,只需将sys.stdout重定向回默认的输出流即可。 At the end of the code where you need to redirect the output, you just need to redirect sys.stdout back to the default output stream. ```python sys.stdout = sys.__stdout__ ``` 完整代码如下 The complete code is as follo...

在Python中模拟程序重启 - Simulating Program Restart in Python

 python中,可以使用以下命令模拟重启效果。 The following command can be used in Python to simulate a restart effect. ```python os.spawnv(os.P_WAIT, sys.executable, [sys.executable] + sys.argv) exit(0) ``` 本命令的原理是,首先,使用spawnv生成一个子进程,然后挂起父进程。子进程是使用相同的python程序执行相同的脚本。子进程结束后,父进程也立即退出。 The principle of this command is that it first generates a child process using spawnv and then suspends the parent process. The child process executes the same script using the same Python program. Once the child process ends, the parent process also exits immediately. 但是,其与真正意义上的重启不同的是,父进程是在子进程结束后才结束,所以,在子进程中,执行列出所有进程的代码,可以查看到父进程。如下所示: However, it's different from a true restart in that the parent process ends only after the child process has finished. Therefore, if you run code to list all processes in the child process, you can see the parent process. This is demonstrated as follows: ```python import os import sys import time import psutil pids = [p for p in psutil.pids() if psutil.Process(p).name() == 'python....

有国才有家?——读bbc关于朝鲜的采访有感 - Only With a Country, Can There be a Home? — Reflections on BBC's Interview About North Korea

Image
由于本文可能稍有敏感,这里仅展示英文版。 https://www.zhihu.com/question/596369328/answer/2998679965 Image Content: Question: Why are Westerners so selfish that they can't understand "Only with a country, can there be a home"? Question Description: We Chinese know that for the sake of our country, we would pass our house three times without entering, and even sacrifice our entire family on the battlefield. How can Westerners be so thoroughly brainwashed by universal values??? Answer: Having a country doesn't necessarily mean having a home (a photo of Pol Pot). On June 18, 2023, BBC published an interview about North Korea. It described how the lives of three ordinary North Korean citizens were becoming increasingly difficult, to the point where some people in North Korea were even dying of hunger.  https://www.bbc.com/news/world-asia-65881803 By the way, As I write this article, I happen to be in Seoul, a city just 40 kilometers away fr...

制作nachos - Making nachos

Image
Nachos是一种墨西哥风味的小吃/前菜,中文名叫做墨西哥烤干酪辣玉米片。中国人接受nachos也许有些难度,因为中国菜中并没有奶酪和玉米片。但是这道菜的味道绝对毋庸置疑,热量也是max级别的。 Nachos are a Mexican-style snack or appetizer. known in Chinese as Mexican grilled cheese spicy corn chips. It may be somewhat challenging for Chinese people to accept nachos, because cheese and corn chips are not common ingredients in Chinese cuisine. However, there's no doubt about the fantastic taste of this dish, and the calorie content in nachos is extremely high. 首先,去任意一家超市,把玉米片和萨尔萨酱买下来。(它俩通常在一个货架上) First, go to any supermarket and buy corn chips and salsa sauce. (They're usually on the same shelf) 把一个洋葱,一个墨西哥辣椒去皮,切成小块。解冻适量肉馅,猪肉牛肉均可,炒熟,捣成小块。然后加入上述碎蔬菜,倒入半瓶萨尔萨酱,翻炒。 Peel an onion and a jalapeno (Mexican pepper), and cut them into small pieces. Defrost an appropriate amount of minced meat, either pork or beef, cook it, and break it into small pieces. Then add the aforementioned chopped vegetables, pour in half a bottle of salsa sauce, a...

热门博文 - Popular posts