Daily Reading 9
Ten Thousand 4
Describe the types of automation in Python to automate everyday tasks. What are some possible applications of this knowledge in the real-world context?
Python is capable of doing folder and file actions on the operating system itself, which lends it the capacity for doing rote mass actions such as renaming 500 files or sorting a jumble of files into specific categorized folders. Weekly organizing of a download folder by filetype is the automation example the reading provides.
What is monkey patching in Python? Discuss its benefits and potential risks. Provide an example where monkey patching can be beneficial.
Monkey patching is the act of patching or changing the behavior of a python program during the span of runtime, without reaching into the source code or libraries in any way. Monkey patching can be used to simulate what the effects of a change to a library would cause, or can serve as a way of testing how an improvised addition to an existing method will behave. Monkey patching can be very dangerous and is generally ill advised for user-facing software, as not backing up the original version of the method you are monkey patching can and will result in irreversible changes to your libraries that can only be solved with a fresh instance of python, AND will make the behavior of your version of python “non vanilla” and impossible for others to diagnose bugs for.
How can you use Pytest and its Capsys fixture to test printed output in Python? Can you describe a situation where this technique would be particularly useful?
Capsys is a fixture of Pytest that allows you to grab the console output of a python program at runtime, along with it’s error message output if applicable. The end result of Capsys running with default settings is a tuple consisting of 2 strings, the first being the console output, and the second being any error messages.
Capsys sounds like it could be used for streamlined testing of programs that require user input, and make significant use of the console output. Instead of checking manually that your program is responding in the console in expected ways each time, you can bypass the inputs with hardcoded responses, and capture the resulting console logging to run tests upon, to ensure that each of the console messages meets the expected parameters for the situation.