File operations for human.
It should follow PEP8 coding style and coded in OOP-style. It's better to have annotations and type hints for code readability.
Here quoted from import this:
- Explicit is better than implicit.
- Simple is better than complex.
- Readability counts.
- Errors should never pass silently.
- Unless explicitly silenced.
- Now is better than never.
- Although never is often better than right now.
- If the implementation is hard to explain, it's a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea -- let's do more of those!
- Introducing Python, 2nd Edition
- Read source code of requests for human for reference.
- Youtube: python command pattern
- https://awesome-python.com
- Google: python text manipulation library
- Google: python regex -> glob
- Google: regular expression visualizer -> Google: python regex -> Python RegEx
- https://github.com/yidao620c/design-pattern/tree/master/pattern12-command
- Breaking out of two loops -> Loop Like A Native - Ned Patchelder
I combain some old code, that I wrote in function-style, and made something new in command pattern.
Try to learn and implement something new to me here, such as regular expression (regex, or re), glob, pathlib, data class, design patterns, the debugger in VS Code, etc...
2019-11-22:
These class as basement of the whole project, and they are originally borrowed from Mastering Python Design Patterns, Chapter 11, as a demo of command pattern. I turn function delete_file() into class DeleteFile later on.
2019-12-15:
Class ShiftModificationTime bases on my old script change-file-timestamp.py in 2017.
- Google: python 3 change file timestamp -> http://www.dreamincode.net/forums/topic/307923-change-timestamp-on-file-to-current-time/
- Google: python3 os.scandir -> https://www.blog.pythonlibrary.org/2016/01/26/python-101-how-to-traverse-a-directory/ -> https://www.blog.pythonlibrary.org/2013/11/14/python-101-how-to-write-a-cleanup-script/
2020-03-16:
I made this class in order to prepare diaries for putting contents in a static-site-generator, such as Hexo, Pelican as I am escaping Wordpress.
I have written diary in text files (a.k.a. Markdown) each file per month. Later on, I meet Hexo and Pelican, and it is better to have my diaries each file per day. So it it better to let python to split entries into chunks for me. :)
- Google: python split file by delimiter -> Google: python split file into chunks by a given delimiter (= Google: python split text file by delimiter)
- https://stackoverflow.com/questions/7980288/splitting-large-text-file-by-a-delimiter-in-python -> use itertools.groupby to group lines after
----into lists. This question is similar to mine (e.g. to split content in a text file into files by '----' as a seperator.) - https://stackoverflow.com/questions/7980288/splitting-large-text-file-by-a-delimiter-in-python -> Google: python iter group -> std lib -> Google: python fileinput -> fileinput – Process lines from input streams - PyMOTW
- https://stackoverflow.com/questions/7980288/splitting-large-text-file-by-a-delimiter-in-python -> use itertools.groupby to group lines after
- Google: python TextIOWrapper -> https://python3-cookbook.readthedocs.io/zh_CN/latest/c05/p01_read_write_text_data.html, http://book.pythontips.com/en/latest/one_liners.html
- Google: regular expression visualizer -> Google: python regex -> Python RegEx
- Google: python regex -> Google: python glob
# Ref: # 1.1
import itertools as it
filename='test.dat'
with open(filename,'r') as f:
for key,group in it.groupby(f,lambda line: line.startswith(':Entry')):
if not key:
for line in group:
...2020-03-19:
The debugger in VS Code rocks! I can't make this the prototype (a.k.a commit d729a18) works without it. I cannot know the correct structure of it.groupby() without using the debugger.
No more print() but breakpoints.
2020-03-23:
- What if I use data classes for meta data of an entry?
TODO:
SplitFileIntoChunks- Get
SplitFileIntoChunksworks in function-style first.- Study
remodule. - Study
fileinputand process files in bulk.
- Study
- Use
fileinputto process a list of filename - Write a class to get a list of filename for fileinput
- Use debugger
RUNin VS Code instead ofprint()blah blah blah. - Trim empty lines
\nin the beginning and in the end of the output. - Handle exception when new filename is undefined. (e.g. no meta info from an entry.)
- Able to set source and target of files.
- Build meta data (for making new filename) as dataclasses.
- Get
2020-03-20:
Use of pathlib:
- 你应该使用pathlib替代os.path
- Python 工匠:高效操作文件的三个建议
- Google: python pathlib -> Python 3's pathlib Module: Taming the File System 3.1 -> Pathlib Cheatsheet
- Google: python pathlib listdir -> https://stackoverflow.com/questions/39909655/listing-of-all-files-in-directory 4.1 Google: python pathlib listdir -> python pathlib list subdirectories -> https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory
- python pathlib list subdirectories -> https://pbpython.com/pathlib-intro.html
- Youtube: python pathlib -> Python Pathlib Tutorial - JCharisTech & J-Secur1ty - YouTube -> https://blog.jcharistech.com/2020/03/11/python-pathlib-tutorial/
- https://python3-cookbook.readthedocs.io/zh_CN/latest/c05/p13_get_directory_listing.html
2020-03-21:
Use of generator to generat a list of file in a directory:
- Use generator to create iterator because a generator expression is much more memory efficient
1.1 Google: python generator -> https://www.programiz.com/python-programming/generator
If a function contains at least one yield statement (it may contain other yield or return statements), it becomes a generator function. Both yield and return will return some value from a function. The difference is that, while a return statement terminates a function entirely, yield statement pauses the function saving all its states and later continues from there on successive calls. 1.2 Google: python generator -> https://wiki.python.org/moin/Generators
General TODO:
- Read and study source code of requests for human as a coding reference.
- Use ABC and class inheritance to govern commands.
- Google: python word freq
- Google: python word freq in text
FindFile, SplitFilename, MarkdownToPdf, ListDirectory, Tree