Extends the functions in the library. The extension is like registering a function in a Python class. (Assign to a variable under a class, each instance created from that class will have the same function)
Below, as an example, the code that extends the operation method of pathlib. Add extensions such as file copy and folder tree copy functions that are not originally available.
Extended trial item:
trial.py
# -*- coding: utf-8 -*-
#Try to extend the function in the library, try to extend the operation method of pathlib
import pathlib
import shutil
# sec: main
def main():
# sec:Test data preparation
pathlib.Path("./test/in1/in2").mkdir(parents=True, exist_ok=True)
pathlib.Path("./test/test.txt").write_text("0123456789")
# sec:Expansion 1:Added extension of size function to get file size which is not originally
path_1 = pathlib.Path("./test/test.txt")
print("Added extension of size function:", path_1.size())
# sec:Expansion 2:Extended copy function for folder copy that does not exist originally
path_1 = pathlib.Path("./test")
path_2 = path_1.copy("./test2")
input("Check if the folder test2 has been created:Stopping")
# sec:Expansion 3:Extended addition of unlink function for deleting folders that does not originally exist
path_2.unlink()
input("Check if the folder test2 has been deleted:Stopping")
# sec:Expansion 4:Extended copy function for file copy that is not originally added
path_1 = pathlib.Path("./test/test.txt")
path_2 = path_1.copy("./test2.txt")
input("File test2.Check if txt was created:Stopping")
# sec:Expansion 5:Check if the unlink function of the original file deletion also works
path_2.unlink()
input("File test2.Check if txt has been deleted:Stopping")
# sec:Expansion 6: 「/"Others"+Also added an extension to concatenate paths
path_1 = pathlib.Path("./test/test.txt")
path_2 = path_1.parent + "in1" + "in2" + "test3.txt"
print("「+But connect the paths:", path_2)
"""Console output example:
Added extension of size function: 10
File test2.Check if txt was created:Stopping
File test2.Check if txt has been deleted:Stopping
Check if the folder test2 has been created:Stopping
Check if the folder test2 has been deleted:Stopping
「+But connect the paths: test\in1\in2\test3.txt
"""
# sec:Expansion
#Get file size
pathlib.Path.size = lambda self: self.stat().st_size #Expansion addition
#File copy / folder tree copy
def pathlib_Path_copy_ex(self, path_to):
if self.is_file():
shutil.copy(str(self), str(path_to))
elif self.is_dir():
shutil.copytree(str(self), str(path_to))
return pathlib.Path(path_to)
pathlib.Path.copy = pathlib_Path_copy_ex #Expansion addition
#File deletion / folder tree deletion
def pathlib_Path_unlink_ex(self, *args, **argkv):
if self.is_file():
pathlib_Path_unlink_orig(self, *args, **argkv)
elif self.is_dir():
shutil.rmtree(str(self))
pathlib_Path_unlink_orig = pathlib.Path.unlink #For holding the original function
pathlib.Path.unlink = pathlib_Path_unlink_ex #Expansion addition
# 「+Connect paths with
pathlib.Path.__add__ = lambda a, b: a / b #Expansion addition
# sec: entry
if __name__ == "__main__": main()
I think it would be convenient if the copy
function that makes this file copy / folder tree copy is standard in the library pathlib ...
Recommended Posts