CTRL F with parser and python

Update: 10-10-19
1) Added a codeblock for easier reading and copying.
2) This code will now work on windows also, with the install of python colorama.
THANK YOU COLORAMA.
docs: https://pypi.org/project/colorama/
3) As always take it, break it, bend it, whatever, I made this blog to share the code for those of us learning. Just like any skill, practicing will help improve the language in your brain. 
4) Cheers, and happy practicing friends!

Also big shout out to the Learn More Python The Hard Way by Zed Shaw.  Without knowing which direction to travel , having a road map was invaluable.

Image:  A screenshot of my useless octopus dictionary with the word 'node's color changed to green so that every instance of the word stands out on command prompt.



Making a command line CTRL F:

I wrote this, thinking it would be handy.  Then I realized this is what CTRL F does.  And yes. Handy. So here's a more complete version.  Moving on tomorrow to do more for class, but still very happy with my progress.
Note: Blogger hates copy and paste my Visual studio code, but converting it to a text file, it copies fine.
11-25-2017  Updated version.

--start code block--

import sys 
import argparse 
import re
from colorama import init #<-- pip install  this is necessary for Windows ascii to print color

init() # VS CODE hates this line, but it works.

parser = argparse.ArgumentParser(description=" parser to find similarities in file for searched similate.")  
parser.add_argument('file', help="file to search through -required") 
parser.add_argument('similate', type=str, help='character similate to find in fuzz. -required') 
parser.add_argument('-normal', help=' search = strip all non-alpha numerics out', action='store_true') 
parser.add_argument('-ignore', help='search = ignore Case of letters', action='store_true') 
parser.add_argument('-include', help='search = do not strip out regular expressions', action='store_true') 
parser.add_argument('-print', help='print file with similarities highlighted', action='store_true')
 
special = parser.add_mutually_exclusive_group()
 
special.add_argument('-whitespace', help='find similarities that include whitespace (to include all lines, use -print) No other options work with this yet.', action='store_true')
 
args = parser.parse_args()
 
"""
Window's ascii color is turned off by default.  
link:https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling/1300251#1300251

Colorama solves the windows ascii color turned off. 
Colorama docs: https://pypi.org/project/colorama/
Color codes written by: Michele Locati
 ^<ESC^>[31m [31mRed[0m
 ^<ESC^>[32m [32mGreen[0m
 ^<ESC^>[33m [33mYellow[0m
 ^<ESC^>[34m [34mBlue[0m
"""
def help():
    pass
def normal(args): 
    count = 0 
    similar_stuff= [] 
    with open(args.file) as f: 
        for line in f: 
            # first clean removes all non alpha numerics 
            clean = re.sub("[^\w]", " ", line).split() 
            index = 0
 
            for items in clean: 
                current = clean[index] 
                #print(current)      
                similate = args.similate 
                index += 1
 
                if similate in current: 
                    count+=1 
                    similar_stuff.append(current) 
                    #print(current) 
        print(f" simularity count = {count}") 
    print(f" simular = {similar_stuff}")
 
    if args.print: 
        highlight(args)
 
 
def highlight(args): 
    with open(args.file) as f: 
        linecount = 0 
        count = 0 
        # green 
        stop_color = '\x1b[0m' 
       
        color = '\x1b[1;32;40m' 
        # blue highlighted 
        solidblue = '\x1b[2;30;46m'
       
 
        for line in f: 
            index = 0 
            linecount += 1 
            newline = line.split() 
            if not args.ignore: 
                for words in newline: 
                    current = newline[index] 
                    index += 1 
                    if args.similate in current:    
                        words = args.similate  
                        count += 1 
                        newcolor = color + words + stop_color 
                       
                        line = line.replace(words, newcolor) 
                        #print(current)
 
            elif args.ignore: 
                for words in newline: 
                    current = newline[index] 
                    current1 = current.lower() 
                    similate1 = args.similate 
                    similate1 = similate1.lower() 
                    index += 1
 
                    if similate1 in current1:    
                        count += 1 
                        newcolor = color + current + stop_color 
                        line = line.replace(current, newcolor) 
            print(f"{linecount}  {line}", end='')
        print(f" \n {solidblue} similarity count =  {count}{stop_color} ") 
 
def expressionish(args): 
    count = 0 
    similar_stuff= [] 
    with open(args.file) as f: 
        for line in f: 
            clean = line.split() 
            index = 0
 
            for items in clean: 
                current = clean[index] 
                #print(current)   
                similate = args.similate 
                index += 1 
                if similate in current: 
                    count+=1 
                    similar_stuff.append(current) 
        print(f" simularity count = {count}") 
    print(f" simular = {similar_stuff}")
 
    if args.print: 
        highlight(args)
 
 
def ignore_case(args): 
    count = 0 
    similar_stuff= [] 
    with open(args.file) as f: 
        for line in f: 
            clean = line.split() 
            index = 0 
            for items in clean: 
                current1 = clean[index] 
                #print(current) 
                current = current1.lower() 
                #print(current)   
                similate = args.similate 
                similate = similate.lower()
                index += 1

                if similate in current:
                    count+=1
                    similar_stuff.append(current1)
        print(f" simularity count = {count}")
    print(f" simular = {similar_stuff}")
 
    if args.print: 
        highlight(args)
 
 
# I think this is entirely unneccessary, but for some reason it felt like I needed
 
# to return the lowercase instead of transforming it in the functions..   
 
def lowercase(args): 
    return args.lower()
  
def normal_ignore(args): 
    count = 0 
    similar_stuff= [] 
    with open(args.file) as f: 
        for line in f: 
            clean = re.sub("[^\w]", " ", line).split() 
            index = 0 
            for items in clean: 
                current1 = clean[index] 
                current = current1.lower() 
                #print(current) 
                similate = lowercase(args.similate) 
                index += 1
 
                if similate in current:
                    count+=1 
                    similar_stuff.append(current1)
        print(f" simularity count = {count}") 
    print(f" simular = {similar_stuff}")
 
    if args.print: 
        highlight(args) 
 
def include_ignore(args): 
    count = 0 
    similar_stuff= [] 
    with open(args.file) as f: 
        for line in f: 
            clean = line.split() 
            index = 0
 
            for items in clean: 
                current1 = clean[index] 
                current = lowercase(current1) 
                #print(current) 
                similate = lowercase(args.similate)  
                index += 1
 
                if similate in current:
                    count+=1
                    similar_stuff.append(current1) 
        print(f" simularity count = {count}") 
    print(f" simular = {similar_stuff}") 

    if args.print: 
        highlight(args) 
 
 
# if the similar search is in the line, insert color before and after the found text 
def whitespace(args): 
    count = 0 
    similar_stuff = [] 
    linecount = 0
 
    with open(args.file) as f: 
        for line in f: 
            linecount += 1
 
            if args.similate in line:   
                words = args.similate 
                #green 
                stop_color = '\x1b[0m' 
                color = '\x1b[1;32;40m' 
                newcolor = color + words + stop_color 
                line = line.replace(words, newcolor) 
                print(f"{linecount}  {line}", end='')
 
            elif args.print: 
                print(f"{linecount}  {line}", end='')
 
  
 
if args.whitespace: 
    print("whitespace ignores all other options.") 
    print("to print whole file, use -print") 
    whitespace(args)
 
 
elif args.normal and args.include: 
    print("Error:  option -normal and -include are mutually exclusive") 
    print("These options will not work together, try again.") 
 
elif args.normal and args.ignore: 
    normal_ignore(args)  
elif args.include and args.ignore: 
    include_ignore(args)
 
 
elif args.normal:
    normal(args)  
 
elif args.include: 
    expressionish(args)         
 
elif args.ignore: 
    ignore_case(args)
 
 
else:
 
    print("something was missing. Did you remember to specify the search type?")
 



--end code block--




Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame