DEV Community

Ayush Moghe
Ayush Moghe

Posted on

Create a command pad to minimize your efforts and make your work easy

Hey all this is my first post on this site. The post is about a project that how to make a commanding pad in python that will minimize your efforts of downloading youtube videos, solving algebra and doing factorization with getting ip address of a site, converting image to ascii art, getting approximate average of numbers as well as many more things!

So let's start

First of all we have to download all the python modules listed below:

import keyboard
import os
import sys
import signal
import pyautogui
import sys, random, argparse

from sympy import *
from colorama import Fore, Back, Style
import speech_recognition as sr
import ply
Enter fullscreen mode Exit fullscreen mode

So basically what we are doing we are combining all the uses of our python modules and making the command pad named Asst Prompt

These are only some of the python modules we will see many others in our code.

Now, It's time to code

First of all we have to create a lexer and a paraser that will not show any error.

tokens = (
    'NAME','NUMBER',
    'PLUS','MINUS','TIMES','DIVIDE','EQUALS',
    'LPAREN','RPAREN',
    )

# Tokens

t_PLUS    = r'\+'
t_MINUS   = r'-'
t_TIMES   = r'\*'
t_DIVIDE  = r'/'
t_EQUALS  = r'='
t_LPAREN  = r'\('
t_RPAREN  = r'\)'
t_NAME    = r'[a-zA-Z_][a-zA-Z0-9_]*'

def t_NUMBER(t):
    r'\d+'
    try:
        t.value = int(t.value)
    except ValueError:
        print("Integer value too large %d", t.value)
        t.value = 0
    return t

# Ignored characters
t_ignore = " \t"

def t_newline(t):
    r'\n+'
    t.lexer.lineno += t.value.count("\n")

def t_error(t):
    t.lexer.skip(1)

# Build the lexer
import ply.lex as lex
lexer = lex.lex()

# Parsing rules

precedence = (
    ('left','PLUS','MINUS'),
    ('left','TIMES','DIVIDE'),
    ('right','UMINUS'),
    )

# dictionary of names
names = { }

def p_statement_assign(t):
    'statement : NAME EQUALS expression'
    names[t[1]] = t[3]

def p_statement_expr(t):
    'statement : expression'
    print(t[1])

def p_expression_binop(t):
    '''expression : expression PLUS expression
                  | expression MINUS expression
                  | expression TIMES expression
                  | expression DIVIDE expression'''
    if t[2] == '+'  : t[0] = t[1] + t[3]
    elif t[2] == '-': t[0] = t[1] - t[3]
    elif t[2] == '*': t[0] = t[1] * t[3]
    elif t[2] == '/': t[0] = t[1] / t[3]

def p_expression_uminus(t):
    'expression : MINUS expression %prec UMINUS'
    t[0] = -t[2]

def p_expression_group(t):
    'expression : LPAREN expression RPAREN'
    t[0] = t[2]

def p_expression_number(t):
    'expression : NUMBER'
    t[0] = t[1]

def p_expression_name(t):
    'expression : NAME'
    try:
        t[0] = names[t[1]]
    except LookupError:
        t[0] = 0

def p_error(t):
    pass

import ply.yacc as yacc
parser = yacc.yacc()
Enter fullscreen mode Exit fullscreen mode

Yes, it's done.

Why we have ignored errors? Because after all the things when we will run our pad we will get error after each line so this is the reason we have not inserted errors in it.

Now, The second thing is to make several functions:

sys.tracebacklimit=None
def set_variables(variables):
    return symbols(variables)
def check_user_input(input):
    try:
        # Convert it into integer
        val = int(input)
        print("{int}")
    except ValueError:
        try:
            # Convert it into float
            val = float(input)
            print("{float input}")
        except ValueError:
            print("{string}")
def getFactor(expression):
    '''do factorization in python'''
    try:print(factor(expression))
    except:print('Error')
def solve_expr(expression):
    '''solve a algebraic expression'''
    try:print('{',expand(expression),'}')
    except:
        print('Not able to solve the expression')
def download_from_youtube(url,path):
    '''download youtube videos to a path'''
    print('Downloading your request\n')
    import pytube  
    from pytube import YouTube  
    video_url = url  
    youtube = pytube.YouTube(video_url)  
    video = youtube.streams.first()  
    video.download(path)
    print('Your video is saved to',path)
def cls():
    import subprocess as sp
    sp.call('cls', shell=True)
Enter fullscreen mode Exit fullscreen mode

These are several functions that will make our Asst Prompt much better

Now let's add some custom animation to our Asst Prompt!

The reason we have added cls function in our code is displayed below

cls()
print("A")
cls()
print('As')
cls()
print('Ass')
cls()
print('Asst')
cls()
print('Asst ')
cls()
print('Asst p')
cls()
print('Asst pr')
cls()
print('Asst pro')
cls()
print('Asst prom')
cls()
print('Asst promp')
cls()
print('Asst prompt\n')
import platform

my_system = platform.uname()
print(f"RUNNING ON PC: {my_system.node} VERSION: {my_system.version}")

import time
time.sleep(2)
Enter fullscreen mode Exit fullscreen mode

Now it's time to handle the loop break when ctrl+c is pressed.

def keyboardInterruptHandler(signal, frame):
    print(("\nKeyboardInterrupt".format(signal)))
    while True:
        main()
signal.signal(signal.SIGINT, keyboardInterruptHandler)
Enter fullscreen mode Exit fullscreen mode

The function main here refers to the main part that will be executed in the while loop at the last.

It's time to add some more functions to our command pad(asst prompt)

def call_ip(website):
    '''call the ip address of your url'''
    import socket
    url=website
    print(socket.gethostbyname(url))
def av(num):
    sum_num = 0
    for i in num:
        sum_num = sum_num + i   

    avgr = sum_num/len(num)
    return avgr
def inspect(address):
    '''inspect a address'''
    try:
        import requests
        req = requests.get(address)
        print(req.text)
    except:
        print('Unable to get the code please check your url')
Enter fullscreen mode Exit fullscreen mode

okay, so we have complete some of the code that is required to make the asst prompt!

After this we have to proceed for user input.

def main():
    code=input('asst--> ')
    parser.parse(code)
Enter fullscreen mode Exit fullscreen mode

And this is the line where we have to parse our code.

But main() function cannot be so small so let's proceed for the commands of asst prompt.

if 'press' in code:
        string = code
        word = 'press'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        try:keyboard.send(out)
        except:print('unable to press key')
    if 'type' in code:
        string = code
        word = 'type'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        print(out)
    if 'python install' in code:
        string = code
        word = 'python install'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        print('Installing',out,'using pip')
        import time
        time.sleep(3)
        import pip
        pip.main(['install',out])
        print('successfully installed',out)
    if 'delete' in code:
        string = code
        word = 'delete'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        import os
        try:
            os.system('del '+out)
        except:
            print('unable to delete the required please check the syntax and path')
    if '[' and ']' in code:
        print(code)
    if 'bin' in code:
        string = code
        word = 'bin'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        out=(int(out))
        try:print(bin(out))
        except:print('invalid syntax')
    if code == 'exit':
        sys.exit()
    if 'shutdown' in code:
        os.system('shutdown /s')
    if 'clickon{' in code:
        xx=int(input('...: x> '))
        yy=int(input('...: y> '))
        clk=int(input('...: clicks> '))
        import pyautogui
        pyautogui.click(x=xx,y=yy,clicks=clk)
    if code=='position-pointer':
        import pyautogui
        print(pyautogui.position())
    if '@' in code:
        string = code
        word = '@'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        print(set(out))
    if code=='{':
        while code!='}':
            code=input('...: ')
    if 'ip' in code:
        string = code
        word = 'ip'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        try:
            call_ip(out)
        except:print('invalid address')
    if 'inspect' in code:
        string = code
        word = 'inspect'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        inspect(out)
    if 'open' in code:
        string = code
        word = 'open'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        import os
        os.system('start '+out)
    if 'solve' in code:
        string = code
        word = 'solve'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z=set_variables(['a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g',
 'h',
 'i',
 'j',
 'k',
 'l',
 'm',
 'n',
 'o',
 'p',
 'q',
 'r',
 's',
 't',
 'u',
 'v',
 'w',
 'x',
 'y',
 'z'])
        solve_expr(out)
    if 'factor' in code:
        string = code
        word = 'factor'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z=set_variables(['a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g',
 'h',
 'i',
 'j',
 'k',
 'l',
 'm',
 'n',
 'o',
 'p',
 'q',
 'r',
 's',
 't',
 'u',
 'v',
 'w',
 'x',
 'y',
 'z'])
        getFactor(out)
    if code=='cls':
        import subprocess as sp
        sp.call('cls', shell=True)
    if 'typeof' in code:
        string = code
        word = 'typeof'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        print(),check_user_input(out)
    if 'set-path' in code:
        string = code
        word = 'set-path'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        global path
        path=out
    if 'youton' in code:
        string = code
        word = 'youton'
        word_list = string.split()
        nout=(' '.join([i for i in word_list if i not in word]))
        download_from_youtube(nout, path)
    if 'set-char' in code:
        string=code
        word='set-char'
        word_list=string.split()
        g=(''.join([i for i in word if i not in word]))
        newg=g
        cork=newg.split()
    def ascii(imgpath,output_file="ascii.txt"):
        # pass the image as command line argument
        image_path = imgpath
        img = Image.open(image_path)

        # resize the image
        width, height = img.size
        aspect_ratio = height/width
        new_width = 80
        new_height = aspect_ratio * new_width * 0.55
        img = img.resize((new_width, int(new_height)))
        # new size of image
        # print(img.size)

        # convert image to greyscale format
        img = img.convert('L')

        pixels = img.getdata()

        # replace each pixel with a character from array
        chars = ["~","`","^","1","@","$","%","*","!",":","."]
        new_pixels = [chars[pixel//25] for pixel in pixels]
        new_pixels = ''.join(new_pixels)

        # split string of chars into multiple strings of length equal to new width and create a list
        new_pixels_count = len(new_pixels)
        ascii_image = [new_pixels[index:index + new_width] for index in range(0, new_pixels_count, new_width)]
        ascii_image = "\n".join(ascii_image)

        # write to a text file.
        with open(output_file, "w") as f:
            f.write(ascii_image)
        return ascii_image
    if code == 'ascii':
        asc()
    if 'ascii-' in code:
        string = code
        word = 'ascii-'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        try:
            try:ascii(out)
            except:print('!')
        except:print('!')
    if 'length'in code:
        string = code
        word = 'length'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        print(len(out))
    if 'derive' in code:
        string = code
        word = 'derive'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        outt=out+':'
        import os
        os.system('start '+outt)
    if '?' in code:
        mystring=code
        keyword='?'
        bk, keyword, ak = mystring.partition(keyword)
        print('late value:',{ak},'ease var:',{bk})
    if '?#' in code:
        mystring=code
        keyword='?#'
        bk, keyword, ak = mystring.partition(keyword)
        c=bk.split(',')
        ins=[int(float(i)) for i in c]
        try:print(av(ins))
        except:print('Error:Compiling:cms is unable to compile ease var')
    if code=='asst':
        print('\nASST PROMPT VERSION:1.2.0; COMPILING MADE SYSTEM:CODE:402 VERSION:0.5.3 CURRENTLY RUNNING ON PC',my_system.node,'RELEASE',my_system.release)
    if '?!' in code:
        s=code
        k='?!'
        bk,k,ak=s.partition(k)
        c=bk.split(',')
        ins=[int(float((i))) for i in c]
        ins.sort()
        try:print(ins[-1])
        except:print('Error:Compiling:cms is unable to compile ease var')
    if 'cr-note' in code:
        mystring=code
        keyword='cr-note'
        bk, keyword, ak = mystring.partition(keyword)
        global lango
        lango=ak
        import pyaudio
        # -*- coding: utf-8 -*-
        """
        Created on Sat Dec  5 10:27:47 2020
        @author: moghe
        """
        while True:
            import speech_recognition as sr
            recognizer = sr.Recognizer()
            ''' recording the sound '''
            with sr.Microphone() as source:
                recognizer.adjust_for_ambient_noise(source, duration=0.2)
                print("recording...")
                cmd = recognizer.listen(source)
                print(':::\n')
                try:
                    global text
                    text = recognizer.recognize_google(
                    cmd, 
                        language=lango

                        )
                    print(text)
                except:print(text)
    if 'sqrt' in code:
        string = code
        word = 'sqrt'
        word_list = string.split()
        out=(' '.join([i for i in word_list if i not in word]))
        try:
            out=int(float(out))
            print()
            print(out*0.5)
        except:print('Error:cms')
Enter fullscreen mode Exit fullscreen mode

It seems to be much large code! let's discuss some of it's commands

1. press: presses the keyboard keys

For example:

press win+r
Enter fullscreen mode Exit fullscreen mode

And we all know what happens when we press windows key + r
It will open run pad

Then the next is type command which is just same as the echo command of cmd / batch script that will print the text we want

And these two are the most basic commands of asst prompt.

Have you noticed a function ascii is added in the code where all the commands are mentioned

Yes it is for converting image to ascii format.

Just type ascii and hit enter. Then type the path of the image, it will convert it to ascii.

The solve command is another command in asst prompt which will help us to solve algebra and alongside there is a factor command for factorization of algebraic expression. When combined and used as

solve factor expression of your choice

can also divide the polynomials. The credit goes to the creators of sympy module.

There is also a function named delete this acts just like the del command of command prompt.

Bin converts the numbers to binnary code.

@ separates common words and characters from the sentence or string.

ip command calls the ip address of the website

inspect shows the code of inspect column of a webpage.

open command opens the required link or a path of our pc.

There is also a thing named late value and ease var in asst prompt that I have added.

late value refers to the line, character, integer or string that is after the question mark

likewise ease var refers to the line, character, integer or string before the string

For Example:

asst--> a?b
late value: {'b'} ease var: {'a'}
Enter fullscreen mode Exit fullscreen mode

With the help of late value and ease var we can also find average and many other things.

Like:
For average

asst--> 2,4,6?#
6
late value: {'#'} ease var: {'2,4,6'}
4.0
Enter fullscreen mode Exit fullscreen mode

Ignore the number 6 that is displayed.

For getting biggest number written among many numbers

asst--> 1,2,4,34,6,8?! 
late value: {'!'} ease var: {'1,2,4,34,6,8'}
34
Enter fullscreen mode Exit fullscreen mode
  • Note: except late value and ease var all other commands should be before the line and there should be space in each of them.

So Now it's time to execute our Asst Prompt!

while True:
    try:
        main()
    except:
        print('ERROR:Restarting')
        import time
        time.sleep(2)
        cls()
        main()
Enter fullscreen mode Exit fullscreen mode

Output:

image

You can also explore more commands of asst prompt in it's code and can also add your own.

Thanks!

You can find the whole code on github

Top comments (0)