RSS

Monthly Archives: December 2020

Python: function to Calculate the sum a math expression as strings inside a list

This function is to evaluate the stuff inside a list. In my case the specific situation was that I had a list like this mylist=[“10″,”+”,”20″,”-“,”20″,”+”,”20″,”-“,”20”] and I want to get a return 10. ie., ((((10+20)-20)+20)-20)=10

def sum_of_all_values(list):
    value_in_mem=0
    iter_i=0#current iteration
    for i in list:
        iter_i+=1
        index_of_i=iter_i-1
        check_value=i.isnumeric()
        if check_value:
            if i == list[0]:
                value_in_mem=int(i)
        elif i== list[-1]:
            value_in_mem=value_in_mem
        else:
            if i == "+":
                x=int(list[index_of_i-1])
                y=int(list[index_of_i+1])
                if x == list[0]:
                    value_in_mem=x+y
                else:
                    value_in_mem=value_in_mem+y
            if i == "-":
                x=int(list[index_of_i-1])
                y=int(list[index_of_i+1])
                if x == list[0]:
                    value_in_mem=x-y
                else:
                    value_in_mem=value_in_mem-y
            if i == "*":
                x=int(list[index_of_i-1])
                y=int(list[index_of_i+1])
                if x == list[0]:
                    value_in_mem=x*y
                else:
                    value_in_mem=value_in_mem*y
            if i == "/":
                x=int(list[index_of_i-1])
                y=int(list[index_of_i+1])
                if x == list[0]:
                    value_in_mem=x/y
                else:
                    value_in_mem=value_in_mem/y
    return value_in_mem

the above function takes in a list and return the calculated value.

example:

print(sum_of_all_values(mylist))
 
Leave a comment

Posted by on December 28, 2020 in Python

 

WhatsApp message flooding with Python

This code can be used to send a set of images with caption repeatedly. You can change the number of executions and intervals.

from selenium import webdriver
#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support.ui import Select
#from selenium.webdriver.common.by import By
#from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time

contact = "Contact name" #specify the contact or group to whome you will send the msgs
#text = "Hey, this message was sent using python"
#filepath = 'D:/Userfiles/Desktop/Python Whatsapp/1.png'
driver = webdriver.Chrome() #we need to download chromedriver to the script directory for this to work.
driver.get("https://web.whatsapp.com") #Open whatsapp web and scan qr code. then hit enter.
print("Scan QR Code, And then Enter")
input()
print("Logged In")

#inp_xpath_search = "//input[@title='Search or start new chat']"
#input_box_search = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search))
#input_box_search.click()
#time.sleep(2)
#input_box_search.send_keys(contact)
#time.sleep(2)

#find your contact on the left pane and click on them to select.
selected_contact = driver.find_element_by_xpath("//span[@title='"+contact+"']")
selected_contact.click()

#filepaths for your images.
filesforme={1:"filepath1",2:"filepath2",3:"filepath3",4:"filepath4",5:"filepath5",6:"filepath6",7:"filepath7",8:"filepath8",9:"filepath9",10:"filepath10"}

#number of time the loop should run, each time sending one msg.
for m in range (0,10):
    #change the filepath each time.
    for key in filesforme:
            filepath_= (filesforme[key])

            #send an image then break out of loop. I think this loop is actually not needed.
            #i will work on it later
            for i in range(0,1):
                attachment_box=driver.find_element_by_xpath('//div[@title="Attach"]')
                attachment_box.click()

                image_box=driver.find_element_by_xpath('//input[@accept="image/*,video/mp4,video/3gpp,video/quicktime"]')
                image_box.send_keys(filepath)
                time.sleep(3)

                caption_button=driver.find_element_by_xpath('//div[@class="_1awRl copyable-text selectable-text"]')
                caption_button.send_keys("LDF")
                time.sleep(1)

                send_button=driver.find_element_by_xpath('//span[@data-icon="send"]')
                send_button.click()
                time.sleep(5)
                break




#inp_xpath = ('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
#input_box = driver.find_element_by_xpath(inp_xpath)
#time.sleep(2)
#for i in range(0,11):
#    input_box.send_keys(text + Keys.ENTER)
#time.sleep(2)
#driver.quit()
 
Leave a comment

Posted by on December 23, 2020 in Python

 

Delete part of a filenames in a folder with python

import os
#ask user for DIR 
files_dir=input("Enter DIR path:")
for i in files_dir:
    if i == "\\":
        index_of_the_slash = files_dir.find(i)
        filePath= files_dir.replace(rename_files_dir[index_of_the_slash],"/")
#change dir
os.chdir(filePath)
#list of files in dir
filenames=os.listdir()
#Replace x with the number of letters you need
for m in filenames:
    #customise to get what you need
    os.rename(m,(m[0:x]+".pdf"))

 
Leave a comment

Posted by on December 21, 2020 in Python