Application to display and search local memos (diary) in Python

I've been away from programming for a long time, but my desire to make something has reached its limit It doesn't make much sense to make it yourself It was going to be a good rehabilitation practice, so I started making it.

The things (planned) that we are planning to realize are as follows. -A list of files under a certain directory can be displayed (the contents can also be displayed). -Keyword (regular expression) search is possible -Display the result on GUI (Can I display it on a WEB browser?)

I haven't done the GUI part at all yet, but I was able to search. The composition is like this so far.

source file function
AxMemoBrowser.py Body. Call when exe is made
PyCMemo.py Class to implement the command equivalent around CUI
PyCBrowser.py Class that implements around GUI
PyCSearchResult.py Class for saving search results

Implemented as follows. I'm new to it, so there are likely to be many better implementations.

AxMemoBrowser.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2014/11/26

@author: kuromame
'''
from PyCMemo import PyCMemo

#cmemo = PyCMemo("/Users/kuromame/Desktop");
cmemo = PyCMemo();

#cmemo.ReadDir();
cmemo.SearchString("variable");

PyCMemo.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2014/11/26

@author: kuromame
'''

#Import required modules
import os;
import sys;
import re;
import codecs;


class PyCMemo:
    def __init__(self,root_dir=''):
        #Initialization of class members
        print('[PyCMemo.__init__]');
        self.pwd = '';                  #Root directory when searching
        self.file_list = [];            #List of searched files
        self.pattern = [];              #Search word(Regular expression possible).リスト形式で最後尾が最新のSearch word.
        self.isRead = 0;                #Flag for confirming whether you have searched even once
        #Specify the current directory or the specified directory
        if root_dir != '':              
            self.pwd = root_dir;
        else:
            self.pwd = '';                  

    def ReadDir(self):
        #Variable initialization
        print('[PyCMemo.ReadDir]');
        if self.pwd == '':
            self.pwd = os.path.abspath(os.path.dirname(__file__));
        print(self.pwd);
        #Search under the current directory.
        #Search from the top down.
        for root, dirs, files in os.walk(self.pwd):
            for file_ in files:
                full_path = os.path.join(root, file_);
                self.file_list.append(full_path);
                print(full_path);
        #Sort search results
        self.file_list.sort();
        #print(self.file_list);
                
    def SearchString(self,pattern):
        #Variable initialization
        print('[PyCMemo.SearchString]');
        self.pattern.append(pattern);
        #Regular expression search engine initialization
        ptn = re.compile(self.pattern[-1]);
        #Call ReadDir if the file has never been read
        if self.isRead == 0: self.ReadDir(),
        self.isRead = 1;
        #Recursively search for files
        for fname in self.file_list:
            print('=Start search=');
            print(fname);
            try:
                fp = codecs.open(fname,'r','utf-8');
                line_num = 1;
                try:
                    for line in fp.readlines():
                        if ptn.search(line): print(fname,line_num,line),
                        line_num += 1;
                        #print(fname,line_num);
                except UnicodeDecodeError:
                    print("Could not search due to an encoding error.")
                finally:
                    #Do nothing unexpectedly.(Originally, it is necessary to devise such as throwing an exception)
                    pass
            finally:
                fp.close();
                print('=Search end=')
    
                    

By the way, from this time on, I started to put the code on GitHub in practice. https://github.com/ambitious-kuromame/AxMemoBrowser

Recommended Posts

Application to display and search local memos (diary) in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Display numbers and letters assigned to variables in python print
Function synthesis and application in Python
How to display bytes in the same way in Java and Python
Search and play YouTube videos in Python
How to display multiplication table in python
How to display Hello world in python
How to use is and == in Python
Implementation of particle filters in Python and application to state space models
Receive and display HTML form data in Python
Send messages to Skype and Chatwork in Python
Steps to develop a web application in Python
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
I want to display the progress in Python!
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
Binary search in Python
Linear search in Python
Binary search in Python (binary search)
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
Write tests in Python to profile and check coverage
[Python] How to sort dict in list and instance in list
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
Recursively search for files and directories in Python and output
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) in python
Python OpenCV tried to display the image in text.
Script to count and stop up to 5 seconds in Python in Blender
To flush stdout in Python
How to swap elements in an array in Python, and how to reverse an array.
Display UTM-30LX data in Python
Login to website in Python
Search for strings in Python
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
Tips for coding short and easy to read in Python
Binary search in Python / C ++
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
Algorithm in Python (binary search)
A standard way to develop and distribute packages in Python
Useful tricks related to list and for statements in Python
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Stack and Queue in Python
Speech to speech in python [text to speech]
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
Unittest and CI in Python
How to develop in Python
[Python] Depth-first search and breadth-first search
How to execute external shell scripts and commands in python
Just try to receive a webhook in ngrok and python
How to log in to AtCoder with Python and submit automatically
Post to Slack in Python
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
Determine the date and time format in Python and convert to Unixtime
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
processing to use notMNIST data in Python (and tried to classify it)
How to install OpenCV on Cloud9 and run it in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)