#script to calculate for each vertex the correlation of the vertex time series with time series of selected parcels
#input:fmri series of twin subjects
#output:tabels containig correlation values for each twin, one table per vertex
        #correlation matrices for each subjects as intermediate output
import numpy as np
import nibabel as nib
import pandas as pd
from glob import glob
import os, sys
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
import settings as s
import pickle
from myfuns import subject_into_csv_vertex

WhichModelType='Original'   #'Original' # setting for antomical aligned data ('Original') or functional aligned data ('AfterEmbedding')
corr_mats_calculated=False     #set if correlatin matrix of time series have aleady been calculated

#paths to fmri data, parcellation, id of vetices without signal,chosen rois
infile =s.HCP_information_sheet_path         # \
subjectpath1=s.HCProot+'HCP_3T_RESTA_fmri/'# used to get paths to fmri runs of twin subjects
subjectpath2=s.HCProot+'HCP_3T_RESTB_fmri/'# /

zerovertexlh=np.load('../../Deliveries/0verticeslh.npy')# id of vertices without signal
zerovertexrh=np.load('../../Deliveries/0verticesrh.npy')

lhparpath='../../Deliveries/lh.Schaefer2018_600Parcels_7Networks_order.annot'# path to parcelation
rhparpath='../../Deliveries/rh.Schaefer2018_600Parcels_7Networks_order.annot'

lhrois=list(np.load('../../Deliveries/chosenroislh.npy'))
rhrois=list(np.load('../../Deliveries/chosenroisrh.npy'))
lhrois=lhrois[1:]
rhrois=rhrois[1:]
nameslhrois=['l_'+str(s) for s in lhrois]
namesrhrois=['r_'+str(s) for s in rhrois]

# set additional input and output paths
if WhichModelType=='AfterEmbedding':
   source_dir=s.projectfolder+'7NETS_vertex/5_7nets_corresponding/' # path containig id of functionally corresponding vertices of each twin to reference
   corr_mat_dir=s.projectfolder+'7NETS_vertex/6_7nets_corr_matrices_cosine/'# correlation values for each subject, intermediate output
   targetdir=s.projectfolder+'7NETS_vertex/7_7nets_ROIs_cosine/'# tables for each vertex
   if not os.path.exists(corr_mat_dir):
     os.mkdir(corr_mat_dir)
   if not os.path.exists(targetdir):
     os.mkdir(targetdir)

if WhichModelType=='Original':
   corr_mat_dir=s.projectfolder+'7NETS_vertex/1_7net_corr_matrices/'# correlation values for each subject, intermediate output
   targetdir=s.projectfolder+'7NETS_vertex/2_7nets_ROIs/'# tables for each vertex
   if not os.path.exists(corr_mat_dir):
     os.mkdir(corr_mat_dir)
   if not os.path.exists(targetdir):
     os.mkdir(targetdir)

lhannot=nib.freesurfer.io.read_annot(lhparpath)
lhlabels=lhannot[0]
rhannot=nib.freesurfer.io.read_annot(rhparpath)
rhlabels=rhannot[0]
labelslh=np.delete(lhlabels,zerovertexlh,0)
labelsrh=np.delete(rhlabels,zerovertexrh,0)

#get assigenment of parcels to yeo nets based on color table
lhnetwork=np.zeros((9))
rhnetwork=np.zeros((9))
lhnetwork[8]=301
rhnetwork[8]=301
c1=1
c2=1

for i in range(1,301):
    if abs(lhannot[1][i][0]-lhannot[1][i-1][0])>5:
       lhnetwork[c1]=int(i)
       c1=c1+1

    if abs(rhannot[1][i][0]-rhannot[1][i-1][0])>5:
       rhnetwork[c2]=int(i)
       c2=c2+1

#Get paths to mgh-files(fmri runs) of available twin subjects
xl=pd.ExcelFile(infile)
dataframe1=xl.parse('Sheet1')
isNotTwin=dataframe1['Twin_Stat']=='NotTwin'
isNotTwin=np.where(isNotTwin)[0]
dataframe2=dataframe1.drop(isNotTwin,0)
Subjects=dataframe2['Subject'].values

path1=[]
path2=[]
for i in range(Subjects.shape[0]):
    path1.append(subjectpath1+str(Subjects[i]))
    path2.append(subjectpath2+str(Subjects[i]))

fmri_LH_LR_R1=[]
fmri_RH_LR_R1=[]
fmri_LH_RL_R1=[]
fmri_RH_RL_R1=[]
fmri_LH_LR_R2=[]
fmri_RH_LR_R2=[]
fmri_LH_RL_R2=[]
fmri_RH_RL_R2=[]
truesubjects=[]

for i in range(Subjects.shape[0]):
    if os.path.isdir(path1[i])==True:
        fmri_LH_LR_R1.append(path1[i]+'/lh.rfMRI_REST1_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        fmri_RH_LR_R1.append(path1[i]+'/rh.rfMRI_REST1_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_LH_RL_R1.append(path1[i]+'/lh.rfMRI_REST1_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_RH_RL_R1.append(path1[i]+'/rh.rfMRI_REST1_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        fmri_LH_LR_R2.append(path1[i]+'/lh.rfMRI_REST2_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        fmri_RH_LR_R2.append(path1[i]+'/rh.rfMRI_REST2_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_LH_RL_R2.append(path1[i]+'/lh.rfMRI_REST2_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_RH_RL_R2.append(path1[i]+'/rh.rfMRI_REST2_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        truesubjects.append(Subjects[i])
    if os.path.isdir(path2[i])==True:
        fmri_LH_LR_R1.append(path2[i]+'/lh.rfMRI_REST1_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        fmri_RH_LR_R1.append(path2[i]+'/rh.rfMRI_REST1_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_LH_RL_R1.append(path2[i]+'/lh.rfMRI_REST1_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_RH_RL_R1.append(path2[i]+'/rh.rfMRI_REST1_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        fmri_LH_LR_R2.append(path2[i]+'/lh.rfMRI_REST2_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        fmri_RH_LR_R2.append(path2[i]+'/rh.rfMRI_REST2_LR_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_LH_RL_R2.append(path2[i]+'/lh.rfMRI_REST2_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        #fmri_RH_RL_R2.append(path2[i]+'/rh.rfMRI_REST2_RL_Atlas_hp2000_clean_bpss_gsr_fs4.mgh')
        truesubjects.append(Subjects[i])

#calculate correlation matrices for subjects
if corr_mats_calculated==False:

  for i in range(len(truesubjects)):

       lh1=nib.load(fmri_LH_LR_R1[i]).get_data() #choose LR or RL runs
       lh2=nib.load(fmri_LH_LR_R2[i]).get_data()
       rh1=nib.load(fmri_RH_LR_R1[i]).get_data()
       rh2=nib.load(fmri_RH_LR_R2[i]).get_data()

       lh1.resize(lh1.shape[1],lh1.shape[3])
       lh2.resize(lh2.shape[1],lh2.shape[3])
       rh1.resize(rh1.shape[1],rh1.shape[3])
       rh2.resize(rh2.shape[1],rh2.shape[3])

       lh=np.concatenate((lh1,lh2),axis=1)
       rh=np.concatenate((rh1,rh2),axis=1)
       lh=np.delete(lh,zerovertexlh,axis=0)
       rh=np.delete(rh,zerovertexrh,axis=0)

       if WhichModelType=='Original':
          lhframe=pd.DataFrame(np.transpose(lh))
          lhframe=lhframe.groupby(labelslh,axis=1).mean()
          rhframe=pd.DataFrame(np.transpose(rh))
          rhframe=rhframe.groupby(labelsrh,axis=1).mean()

          lhframe=lhframe.iloc[:,lhrois]
          lhframe.columns=nameslhrois
          rhframe=rhframe.iloc[:,rhrois]
          rhframe.columns=namesrhrois
          frame=pd.concat([lhframe,rhframe],axis=1)
          for j in range(len(labelslh)):
           if labelslh[j]!=0:
              if not os.path.exists(corr_mat_dir+'lh_vertex_'+str(j+1)):
                 os.makedirs(corr_mat_dir+'lh_vertex_'+str(j+1))
              vertex=pd.DataFrame(np.transpose(lh[j,:]))
              vertex.columns=[str(j+1)]
              assign=lhnetwork>labelslh[j]
              assign=np.expand_dims(assign,0)
              assign=np.where(np.any(assign,axis=0))[0][0]
              assign=assign-2
              frame=frame.drop(nameslhrois[assign],1)
              frame=pd.concat([vertex,frame],1)
              framecor=frame.corr()
              outfileLR=corr_mat_dir +'lh_vertex_'+str(j+1)+'/'+ str(i) + '.csv.gz'
              framecor.to_csv(outfileLR, compression='gzip')
              frame=pd.concat([lhframe,rhframe],axis=1)

          for j in range(len(labelsrh)):
           if labelsrh[j]!=0:
              if not os.path.exists(corr_mat_dir+'rh_vertex_'+str(j+1)):
                 os.makedirs(corr_mat_dir+'rh_vertex_'+str(j+1))
              vertex=pd.DataFrame(np.transpose(rh[j,:]))
              vertex.columns=[str(j+1)]
              assign=rhnetwork>labelsrh[j]
              assign=np.expand_dims(assign,0)
              assign=np.where(np.any(assign,axis=0))[0][0]
              assign=assign-2
              frame=frame.drop(namesrhrois[assign],1)
              frame=pd.concat([vertex,frame],1)
              framecor=frame.corr()
              outfileLR=corr_mat_dir +'rh_vertex_'+str(j+1)+'/'+ str(i) + '.csv.gz'
              framecor.to_csv(outfileLR, compression='gzip')
              frame=pd.concat([lhframe,rhframe],axis=1)

       if WhichModelType=='AfterEmbedding':
          for j in range(len(labelslh)):
           if labelslh[j]!=0:
             if not os.path.exists(corr_mat_dir+'lh_vertex_'+str(j+1)):
                 os.makedirs(corr_mat_dir+'lh_vertex_'+str(j+1))
             functional=pickle.load(open(source_dir+'lh_'+str(j+1)+'correspondingvertices.p','rb'))
             brain=np.concatenate([lh,rh],axis=0)
             brain=brain[np.squeeze(functional[0][i]),:]
             brainframe=pd.DataFrame(np.transpose(brain))
             brainframe=brainframe.groupby(functional[1],axis=1).mean()

             assign=lhnetwork>labelslh[j]
             assign=np.expand_dims(assign,0)
             assign=np.where(np.any(assign,axis=0))[0][0]
             assign=assign-2
             brainframe.columns=[str(j+1)]+nameslhrois[0:assign]+nameslhrois[assign+1:7]+namesrhrois
             framecor=brainframe.corr()

             outfileLR=corr_mat_dir +'lh_vertex_'+str(j+1)+'/'+ str(i) + '.csv.gz'
             framecor.to_csv(outfileLR, compression='gzip')

          for j in range(len(labelsrh)):
           if labelsrh[j]!=0:
             if not os.path.exists(corr_mat_dir+'rh_vertex_'+str(j+1)):
                 os.makedirs(corr_mat_dir+'rh_vertex_'+str(j+1))
             functional=pickle.load(open(source_dir+'rh_'+str(j+1)+'correspondingvertices.p','rb'))
             brain=np.concatenate([lh,rh],axis=0)
             brain=brain[np.squeeze(functional[0][i]),:]
             brainframe=pd.DataFrame(np.transpose(brain))
             brainframe=brainframe.groupby(functional[1],axis=1).mean()

             assign=rhnetwork>labelsrh[j]
             assign=np.expand_dims(assign,0)
             assign=np.where(np.any(assign,axis=0))[0][0]
             assign=assign-2
             brainframe.columns=[str(j+1)]+nameslhrois+namesrhrois[0:assign]+namesrhrois[assign+1:7]
             framecor=brainframe.corr()

             outfileLR=corr_mat_dir +'rh_vertex_'+str(j+1)+'/'+ str(i) + '.csv.gz'
             framecor.to_csv(outfileLR, compression='gzip')
  print('correlation matrices: done')

# create tables for each vertex based on correlation matrices
if WhichModelType=='Original':
#lh
  subject_into_csv_vertex(len(labelslh),'lh',truesubjects,WhichModelType,labelslh,lhnetwork,corr_mat_dir,targetdir,nameslhrois,namesrhrois,dataframe2,source_dir=None)
#rh
  subject_into_csv_vertex(len(labelsrh),'rh',truesubjects,WhichModelType,labelsrh,rhnetwork,corr_mat_dir,targetdir,nameslhrois,namesrhrois,dataframe2,source_dir=None)

if WhichModelType=='AfterEmbedding':
#lh
  subject_into_csv_vertex(len(labelslh),'lh',truesubjects,WhichModelType,labelslh,lhnetwork,corr_mat_dir,targetdir,nameslhrois,namesrhrois,dataframe2,source_dir=source_dir)
#rh
  subject_into_csv_vertex(len(labelsrh),'rh',truesubjects,WhichModelType,labelsrh,rhnetwork,corr_mat_dir,targetdir,nameslhrois,namesrhrois,dataframe2,source_dir=source_dir)

print('Finished')


#!/usr/bin/python3
import threading, time, re, MySQLdb
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from dd_individual import scrapeListing
from decimal import Decimal

#Create new .csv file for results
filename = "donedeal_vehicles.csv"
f_results = open(filename, "w", encoding='utf-8')
f_results.write("title, price, year, engine, mileage, posted, county, listing_url, image_url\n")

#Create new .csv file for results
filename = "donedeal_individual.csv"
f_individual = open(filename, "w", encoding='utf-8')
f_individual.write("key, value\n")

exitFlag = 0

#Declare class myThread
class myThread (threading.Thread):
	#Initialise class (i.e. the constructor)
   def __init__(self, threadID, name, counter):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name
      self.counter = counter
   def run(self):
      print ("Starting " + self.name + " " + str(time.ctime())) #Declare thread name and start-time
      print_time(self.name, self.counter) #Call print_time() method
      print ("Exiting " + self.name + " " + str(time.ctime())) #Declare thread name and finish-time

def print_time(threadName, counter):
	for x in range(counter, 71000, 448):
		url = "https://www.donedeal.ie/cars/" + "?start=" + str(x)
		uClient = uReq(url)#Opening the connection, storing the page HTML, closing the connection
		html = uClient.read()
		uClient.close()
		dd_soup = soup(html, "html.parser")#Parse this iteration's HTML with BeautifulSoup

		containers = dd_soup.findAll("li", {"class":"card-item"})#Store the HTML of each listing card in an array called containers

		for container in containers:#Loop through all containers, extracting necessary data.
			try:#Enclosed in try-catch block, which will ignore irrelevant advertising

				title_container = container.findAll("p", {"class":"card__body-title"})#Extract vehicle title
				title = title_container[0].text

				price_container = container.findAll("p", {"class":"card__price"})#Extract price
				price = str(price_container[0].span.text)
				if(price != "No Price"):
					price = Decimal(re.sub(r'[^\d.]', '', price))
				else:
					price = 0

				card_href_container = container.find("a", {"class":"card__link"})#Extract the URL of each listing
				card_href = card_href_container.attrs["href"]

				card_media_container = container.find("div", {"class":"card__photo"})#Extract image URL of each listing
				image_url_container = card_media_container.find("img",{"data-lazy-img":True})
				image_url = image_url_container.attrs["data-lazy-img"]

				info_container = container.find("ul", {"card__body-keyinfo"})#Extract key info
				info = info_container.findAll("li")
				if len(info) == 5:#Items are stored in consistent order in a "ul". Loop to extract
					year = info[0].text
					engine = info[1].text
					mileage = info[2].text
					posted = info[3].text
					county = info[4].text
				elif len(info) == 4:#If mileage is missing, replace with "Undisclosed"
					year = info[0].text
					engine = info[1].text
					mileage = 0
					posted = info[2].text
					county = info[3].text
				else:
					break

				#Call scrapeListing() function from dd_individual file
				scrapeListing(card_href, price, posted, card_href, image_url)

				#Print extracted values to console
				#print("Vehicle title: ",  title, "\nPrice: ",  price, "\nYear: ",  year, "\nEngine: ",  engine, "\nMileage: ",  mileage, "\nPosted: ",  posted, "\nCounty: ",  county, "\nHREF: ",  card_href, "\nImage URL: ",  image_url)

				#Write values to the .csv file. These files' cells are deliminated by a comma, so replace all commas in extracted falues with pipes ("|")
				f_results.write(title.replace(",", "|") + "," + str(price) + "," + year.replace(",", "|") + "," + engine.replace(",", "|") + "," + mileage.replace(",", "|") + "," + posted.replace(",", "|") + "," + county.replace(",", "|") + "," + card_href.replace(",", "|") + "," + image_url.replace(",", "|") +"\n")

			except AttributeError:
				pass

# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 28)
thread3 = myThread(3, "Thread-3", 56)
thread4 = myThread(4, "Thread-4", 84)
thread5 = myThread(5, "Thread-5", 112)
thread6 = myThread(6, "Thread-6", 140)
thread7 = myThread(7, "Thread-7", 168)
thread8 = myThread(8, "Thread-8", 196)
thread9 = myThread(9, "Thread-9", 224)
thread10 = myThread(10, "Thread-10", 252)
thread11 = myThread(11, "Thread-11", 280)
thread12 = myThread(12, "Thread-12", 308)
thread13 = myThread(13, "Thread-13", 336)
thread14 = myThread(14, "Thread-14", 364)
thread15 = myThread(15, "Thread-15", 392)
thread16 = myThread(16, "Thread-16", 420)

# Start new Threads
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()
thread10.start()
thread11.start()
thread12.start()
thread13.start()
thread14.start()
thread15.start()
thread16.start()
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()
thread8.join()
thread9.join()
thread10.join()
thread11.join()
thread12.join()
thread13.join()
thread14.join()
thread15.join()
thread16.join()

print ("Exiting Main Thread")
f_results.close()
f_individual.close()


from nsepy import get_history
from datetime import date, datetime
import os
from tradingview_ta import TA_Handler, Interval, Exchange
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

os.system("figlet -c Python Trading Bot ")
Today = date.today()
y = Today.strftime("%Y")
m = Today.strftime("%m")
d = Today.strftime("%d")
# d = "30"

###
#driver.find_element_by_xpath("//label[@class='check']/input[@class='word' and @type='text' and @value='hello']").click()
#driver.find_element_by_xpath("//label[@class='check']/input[@class='word' and @type='text' and @value='hello']").clear()
#driver.find_element_by_xpath("//label[@class='check']/input[@class='word' and @type='text' and @value='hello']").send_keys("apple")
#"//button[@class = ‘kn’]/"
###

#last order
last_order="sell"
sold_before = False
bought_before = False
current_price = 0
take_profit = 0.0
take_loss = 0.0

TAKE_PROFIT = 20       # profit target
TAKE_LOSS = 10         # stop loss
BREAK_PRICE = 2075     # price the algo triggers at
life = True            # timer bool for now
ETH = "0.1"

#load chrome driver
#s=Service(ChromeDriverManager().install())
#driver = webdriver.Chrome(service=s)

#brave_path = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
path = "C:/Program Files/Google/Chrome/Application/chrome.exe"
option = webdriver.ChromeOptions()
option.binary_location = path
option.add_argument("--start-maximized")
option.add_argument("--disable-blink-features=AutomationControlled")
option.add_experimental_option("useAutomationExtension", False)
option.add_argument("--disable-plugins-discovery")
option.add_argument(r"--user-data-dir=C:/Users/gunde/AppData/Local/Google/Chrome/User Data")
option.add_argument("--profile-directory=Person 2")
driver = webdriver.Chrome(executable_path="Users/gunde/Downloads/chrome_driver_chrome/chromedriver.exe", options=option)
# option.add_argument("--incognito") OPTIONAL
# option.add_argument("--headless") OPTIONAL
driver.get("https://www.deribit.com/futures/ETH-PERPETUAL")
driver.maximize_window()

time.sleep(15)

#initiating tradingview handler
ssw = TA_Handler(
    symbol="ETHUSD.P",
    screener="crypto",
    exchange="DERIBIT",
    interval=Interval.INTERVAL_5_MINUTES
)
def countdown(t):

    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        t -= 1

while True:
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    if(current_time >= "09:30:00" or current_time < "15:10:00"):

        rec = ssw.get_analysis()

        m = driver.find_element(By.XPATH,"//*[@id=\"mainHeader\"]/header/div/div[2]/div[1]/div/button")     #clicks on futures button
        a = ActionChains(driver)
        a.move_to_element(m).perform()

        driver.find_element(By.XPATH,"//*[@id=\"futuresTabPanel-0\"]/div/div[2]/div[1]/div[2]/div/button[3]/span[1]").click()  #clicks on latest date

        m = driver.find_element(By.XPATH,"//*[@id=\"AccountSummary-default\"]/div[2]/div/div/div/div/div[2]/div[2]/div[1]/div[2]/div/div/div[1]/div[3]/div/div/span[1]")    #bullshit button
        a.move_to_element(m).perform()

        time.sleep(5)
        print("1243")
        #current_price =
        driver.find_element(By.XPATH,"//html/body/div[1]/div[3]/div[2]")   #finds price22

        driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[1]/button[2]").click()  #market button
        driver.find_element(By.XPATH,"/html/body/div[4]/div[3]/ul/li[1]").click()  # drop down market button
        driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").click()    # eth button
        driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").clear()    # eth button
        driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").send_keys(ETH)    # eth button

        if (current_price >= BREAK_PRICE):
            print("Buying 1 stock of ETH")
            last_order="buy"
            print(last_order)
            print(sold_before)
            #buy 1 stock of ETH
            driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[5]/button[1]").click()     #buy button
            current_price = driver.find_element(By.XPATH,"//*[@id=\"instrument-bar-header\"]/div[1]/span[3]").text
            print(current_price)
            take_profit = float(current_price) + TAKE_PROFIT
            take_loss = float(current_price) - TAKE_LOSS
            while True:
                print("Time left till next call - ")
                countdown(int(2))
                rec = ssw.get_analysis()
                current_price = driver.find_element(By.XPATH,"//*[@id=\"instrument-bar-header\"]/div[1]/span[3]").text
                if(float(current_price) < (BREAK_PRICE - 0.1)):
                    #sell the stock
                    print("Selling 1 stock of SONATSOFTW")
                    last_order="sell"
                    print(last_order)
                    #sell 1 stock of ETH
                    driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[1]/button[2]").click()  #market button
                    driver.find_element(By.XPATH,"/html/body/div[4]/div[3]/ul/li[1]").click()  # drop down market button
                    driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").click()    # eth button
                    driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").clear()    # eth button
                    driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").send_keys(ETH)    # eth button
                    driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[5]/button[2]").click()     #sell button
                    break
                else:
                    print("no adjustment required")

        '''elif (current_price <= BREAK_PRICE):
            print("selling stock of ETH")
            driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[1]/button[2]").click()  #market button
            driver.find_element(By.XPATH,"/html/body/div[4]/div[3]/ul/li[1]").click()  # drop down market button
            driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").click()    # eth button
            driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").clear()    # eth button
            driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").send_keys(ETH)    # eth button
            driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[5]/button[2]").click()     #sell button
            current_price = driver.find_element(By.XPATH,"//*[@id=\"instrument-bar-header\"]/div[1]/span[3]").text
            print(current_price)
            take_profit = float(current_price) - TAKE_PROFIT
            take_loss = float(current_price) + TAKE_LOSS
            while True:
                print("Time left till next call - ")
                countdown(int(2))
                rec = ssw.get_analysis()
                current_price = driver.find_element(By.XPATH,"//*[@id=\"instrument-bar-header\"]/div[1]/span[3]").text
                if(float(current_price) > (BREAK_PRICE + 0.1)):
                    #buy the stock
                    print("Buying the stock")
                    driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[1]/button[2]").click()  #market button
                    driver.find_element(By.XPATH,"/html/body/div[4]/div[3]/ul/li[1]").click()  # drop down market button
                    driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").click()    # eth button
                    driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").clear()    # eth button
                    driver.find_element(By.XPATH,"//*[@id=\"amountInput2\"]").send_keys(ETH)    # eth button
                    driver.find_element(By.XPATH,"//*[@id=\"OrderForm-default\"]/div[2]/div/div[5]/button[1]").click()     #buy button
                    break
                else:
                    print("no adjustment required")'''

    elif(current_time >= "15:10:00" and life == False):
        print("Time to close for the day")
        # #fetch open profit
        #open_profit = driver.find_element(By.XPATH,"//div[4]/div[1]/div[1]/div[1]/div[2]/div[3]/div[1]").text
        # print(open_profit)
        # P = "1000"
        print("Calculating profit :",open_profit)
        break
    else:
        if(current_time >= "09:15:00" and current_time < "09:30:00"):
            print("Analysing market","\n\n")
        elif(current_time < "9:15:00"):
            print("Waiting for market to open")
        else:
            print("No action required")


__version__ = '0.1.7'

BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (70, 70, 0)
WHITE = (0, 0, 0)
ORANGE = (255, 40, 0)

NORMAL = 'normal'
SIREN = 'siren'
WARNING = 'warning'
PROGRESSING = 'progressing'


import requests, re, datetime
import pandas as pd
from pathlib import Path
from bs4 import BeautifulSoup as bs

# Retrieve HTML data
url = "http://ke.customer.decent.cz/a022/mon/"
filename = "meu_data.xlsx"

soup = bs(requests.get(url).content, 'html5lib')
tables = soup.find_all('table')

# Get and format timestamp from first table.
raw_datetime = tables[2].get_text()
date = re.search(r'(\d+).(\d+).(\d+)', raw_datetime)
time = re.search(r'(\d+)\s(\d+)', raw_datetime)

# Create datetime object from data, not very pretty..
timestamp = datetime.datetime(
    int(date.group(3)),
    int(date.group(2)),
    int(date.group(1)),
    int(time.group(1)),
    int(time.group(2)))

# Get data and import it into a Pandas DataFrame, format it.
raw_data = tables[4]
rows = raw_data.find_all('tr')
rows = [x.find_all('td') for x in rows]
data = [[col.get_text() for col in row] for row in rows]

headers = ['activity', 'queue', 'x','active_booths', 'wait_time','x']
table = pd.DataFrame( data[1:], columns=headers).set_index('activity')[:11]
table = table.drop('x', axis=1)

tables = {}

for col in table:
    tables[col] = pd.DataFrame(table[col].str.extract("(\d+)", expand=False).fillna('0'))
    tables[col].columns = [timestamp]
    tables[col] = tables[col].transpose()

# Create 3D array with queue, open booths, and wait time.
data_current = pd.Panel(tables)

data_old = {}

# Check if file exists
if Path.is_file(Path(filename)):

    # Load excel, each sheet to separate dataframe and create Panel
    excel_data = pd.read_excel(filename, sheetname=None, header=0, index_col=0)
    excel_data = pd.Panel(excel_data)

    for sheet in excel_data:
        data_old[sheet] = excel_data[sheet].append(data_current[sheet])

    data_current = pd.Panel(data_old)

data_merged = data_current

data_merged.to_excel(filename)


from PIL import Image, ImageFont, ImageDraw

achtergrond = Image.open("meme.PNG")
afbeelding = Image.open("meme.PNG")

breedte = str(afbeelding.width)
hoogte = str(afbeelding.height)

lettertype = ImageFont.truetype("impact", 40)

tekengebied = ImageDraw.Draw(achtergrond)

tekst = "Teacher: what comes after 69?\nMe: 70\nTeacher: why did you write 'Nice'"
tekengebied.multiline_text((10,10), tekst, font=lettertype, fill=(0,0,0))

achtergrond.show()

achtergrond.save("meme_met_tekst.jpg")


def bt(value, idx):
    global plus,minus,mul,div,min_v,max_v

    if idx == N:
        max_v = max(value, max_v)
        min_v = min(value, min_v)
        return
    if plus:
        plus -= 1
        bt(value + A[idx], idx+1)
        plus += 1
    if minus:
        minus -= 1
        bt(value - A[idx], idx+1)
        minus += 1
    if mul:
        mul -= 1
        bt(value * A[idx], idx+1)
        mul += 1

    if div:
        div -= 1
        bt(int(value / A[idx]), idx+1)
        div += 1

N = int(input())
A = list(map(int, input().split()))
plus,minus,mul,div = map(int, input().split())

visited = [0]*(N-1)
min_v = 1e22
max_v = -1e22
bt(A[0], 1)
print(max_v)
print(min_v)


# Given two non-negative
# integers num1 and num2 represented as string, return the sum of num1 and num2.

# Note:

# The length of both num1 and num2 is < 5100.
# Both num1 and num2 contains only digits 0-9.
# Both num1 and num2 does not contain any leading zero.
# You must not use any built-in BigInteger library or convert
# the inputs to integer directly.

####################
class Solution(object):
    def addStrings(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        max_num, min_num = num2,num1
        if len(num1) >= len(num2):
            max_num = num1
            min_num = num2
        length_diff = len(max_num) - len(min_num)
        res = []
        carry = 0
        min_num = list(min_num)[::-1]
        max_num = list(max_num)[::-1]
        for idx,ch in enumerate(min_num):
            dig, carry = (int(ch)+int(max_num[idx])+carry)%10,(int(ch)+int(max_num[idx])+carry)//10
            res.append(str(dig))

        min_length = len(min_num)
        for i in range(length_diff):
            dig, carry = (int(max_num[i+min_length])+carry)%10,(int(max_num[i+min_length])+carry)//10
            res.append(str(dig))

        if carry:
        	res.append(str(carry))

        # print(res[::-1])
        return "".join(res[::-1])

def main():
	s = Solution()
	print(s.addStrings("1","9"))

if __name__ == '__main__':
	main()


from rest_framework import serializers
from core.models import Category, Contact, Course, Branch

class CategorySerializer(serializers.ModelSerializer):
    """Serializer for Category objects, used as a link to Category serializer"""

    class Meta:
        model = Category
        fields = (
            "name",
            "imgpath",
        )

class BranchSerializer(serializers.ModelSerializer):
    """Serializer for Branch objects, used as a link to Category serializer"""

    class Meta:
        model = Branch
        fields = (
            "latitude",
            "longitude",
            "address",
        )

class ContactSerializer(serializers.ModelSerializer):
    """Serializer for Contact objects, used as a link to Category serializer"""

    class Meta:
        model = Contact
        fields = (
            "contact_type",
            "value",
        )

class CourseSerializer(serializers.ModelSerializer):
    """Serializer for Course objects, main serializer in this project required to serialize multiple relations"""
    contacts = ContactSerializer(
        many=True,
    )
    branches = BranchSerializer(
        many=True,
    )

    class Meta:
        model = Course
        fields = (
            "name",
            "description",
            "category",
            "uuid",
            "logo",
            "contacts",
            "branches",
        )

        extra_kwargs = {'id': {'read_only': True}}

    def create(self, validated_data):
        branches_data = validated_data.pop("branches")
        contacts_data = validated_data.pop("contacts")
        course = Course.objects.create(**validated_data)

        for branch_data in branches_data:
            Branch.objects.create(course=course, **branch_data)

        for contact_data in contacts_data:
            Contact.objects.create(course=course, **contact_data)

        return course


from telegram import Update, ReplyKeyboardMarkup, KeyboardButton, \
    InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import Updater, Dispatcher, Filters, CallbackContext, \
    CommandHandler, MessageHandler, CallbackQueryHandler
import local_settings
import requests
from telegram.ext.filters import Filters
updater = Updater(token=local_settings.TELEGRAM_TOKEN)
from mwt import MWT

def start(update: Update, context: CallbackContext):
    update.message.reply_text('Salom!',reply_markup=ReplyKeyboardMarkup( keyboard=[[KeyboardButton(text='/currency')]],resize_keyboard=True))

@MWT(timeout=60*60)
def get_currency():
    response = requests.get('https://nbu.uz/uz/exchange-rates/json/')
    results = response.json()

    if len(results):
        print('Currency data obtained')
        return results
    else:
        return None
def currency(update: Update, context: CallbackContext):
    keyboard = generate_list()
    update.message.reply_text(
        'Valyutani tanla:',
        reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard)
    )
def generate_list():
    results = get_currency()
    print(results)
    keyboard = []
    for i in range(6):
        currency_key = []
        for j in range(4):
            n = i * 4 + j
            cur = results[n]['code']
            currency_key.append(InlineKeyboardButton(f"{cur}", callback_data=cur))
        keyboard.append(currency_key)
    return keyboard

def callback_handler(update: Update, context: CallbackContext):
    keyboard = generate_list()
    data = update.callback_query.data
    results = get_currency()
    if len(results):
        for i in range(len(results)):
            if data == results[i]['code']:
                update.callback_query.message.edit_text(f"1 {results[i]['code']}  {results[i]['cb_price']} som \n"
                                                              f"Yangilangan vaqti: {results[i]['date']}\n\n"
                                                              'Valyutani tanla:',
                    reply_markup = InlineKeyboardMarkup(inline_keyboard=keyboard))

dispatcher: Dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('Start', start))
#dispatcher.add_handler(CommandHandler('search', search))
dispatcher.add_handler(CommandHandler('currency', currency))
dispatcher.add_handler(MessageHandler(Filters.all, start))
dispatcher.add_handler(CallbackQueryHandler(callback_handler))

updater.start_polling()
updater.idle()


import matplotlib
matplotlib.use('TKagg')
import matplotlib.pyplot as plt
import numpy as np
import cv2
from scipy import optimize
import os
plt.ion()

def get_disk(radius):

    r = get_radius_array(radius)
    r[r < radius] = 1
    r[r > radius] = 0
    disk = r.astype(np.bool)

    return disk

def get_radius_array(disk_center, nx, ny):

    x = np.arange(0, nx)
    y = np.arange(0, ny)
    xv, yv = np.meshgrid(x, y)
    radius_array = np.sqrt( (xv - disk_center[0])**2 + (yv - disk_center[1])**2 )

    return radius_array

def fit_bkg(bkg_avg, xmin, xmax, radius_grid, rmin):
    # Define function for calculating a power law
    powerlaw = lambda x, amp, index: amp * (x ** index)
    # define our (line) fitting function
    fitfunc = lambda p, x: p[0] + p[1] * x
    errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err

    xdata = np.arange(xmin, xmax, 1)
    ydata = bkg_avg[xdata]
    yerr = 0.2 * ydata
    logx = np.log10(xdata)
    logy = np.log10(ydata)
    logyerr = 0.001 * np.ones(ydata.shape)

    pinit = [1.0, -1.0]
    out = optimize.leastsq(errfunc, pinit,
                           args=(logx, logy, logyerr), full_output=1)
    pfinal = out[0]
    covar = out[1]
    index = pfinal[1]
    amp = 10.0 ** pfinal[0]

    bkg_2d = powerlaw(radius_grid, amp, index)
    bkg_2d[rr <= rmin] = 0

    return bkg_2d, amp, index

files = ['/Users/rattie/Data/Eclipse/Stack_mean_aligned_series/aligned/stack_mean_C_exp250.tiff',
         '/Users/rattie/Data/Eclipse/Stack_mean_aligned_series/aligned/stack_mean_C_exp30.tiff',
         '/Users/rattie/Data/Eclipse/Stack_mean_aligned_series/aligned/stack_mean_C_exp1.tiff']

savedir = '/Users/rattie/Data/Eclipse/Background_removed/'

# Exposure times
nexp = 3
times_array = np.array([250, 30, 1])
sum_times = times_array.sum()
# Normalization factors
max16 = 2**16-1
max14 = 2**14-1
# Load a sample
img0 = cv2.imread(files[0], cv2.IMREAD_COLOR + cv2.IMREAD_ANYDEPTH)
nx = img0.shape[1]
ny = img0.shape[0]
# Load all images and convert from openCV BGR to RGB
images = np.zeros([nexp, ny, nx, 3])
for i in range(nexp):
    images[i, :, :, :] = cv2.imread(files[i], cv2.IMREAD_COLOR + cv2.IMREAD_ANYDEPTH)
    images[i, :, :, :] = np.fliplr(images[i,:,:,:].reshape(-1,3)).reshape(images[i,:,:,:].shape)

# Get a reference version that can be visualized as float, normalized from [0-1].
# Because of the flat fielding, saturation occurs above max14. So we choose to normalize to a meaningful value,
# maxval =  maximum near-saturating intensity in the prominence.
maxval = images[:, 1080:1120, 1900:1960, 0].max() # ~ 20000
imagesf = images/maxval
# Clip within 0, 1 - for visualization only. Processing is made on an unclipped version.
imagesf = np.clip(imagesf, 0, 1)
# Coordinate of the center
center = (2120, 1361)
radius = 323
# Plot one image and the circle to check the center and radius
#circle1 = plt.Circle((0, 0), 0.2, color='r')
plt.figure(0, figsize=(15,11))
ax1 = plt.gcf().add_subplot(111)
ax1.imshow(np.clip(imagesf[0,...]*5, 0, 1), origin='lower')
#ax1.imshow(np.clip(imagesf[0,...], 0, 1), origin='lower')
ax1.add_artist(plt.Circle(center, radius, color='green', fill=False, linewidth=1))
ax1.add_artist(plt.Circle(center, 330, color='yellow', fill=False))
ax1.axis([1000, 3000, 500, 2500])
plt.title('1/250s , brightness x 5')
plt.tight_layout()

# Normalize to the exposure time, so we get the same intensity per second + noise
tarray3 = np.reshape(times_array, [nexp, 1, 1, 1])
images_exp = images * tarray3 # * (2**16 -1) / (2**14 -1)
# Divide again to maxval to have a visualization equivalent to "imagesf" (consider that 1s should look the same).
images_expf = images_exp/maxval
# Clip saturated pixels in the exposure-normalized images
images_expf_clipped = np.clip(images_expf, 0, 1)
# Display the 3 images in RGB.
fig = plt.figure(1, figsize=(19,10))
plt.subplot(231)
plt.imshow(imagesf[0,...])
plt.title('1/250s')
plt.subplot(232)
plt.imshow(imagesf[1,...])
plt.title('1/30s')
plt.subplot(233)
plt.imshow(imagesf[2,...])
plt.title('1s')

plt.subplot(234)
plt.imshow(images_expf_clipped[0,...])
plt.title('x 250')
plt.subplot(235)
plt.imshow(images_expf_clipped[1,...])
plt.title('x 30')
plt.subplot(236)
plt.imshow(images_expf_clipped[2,...])
plt.title('x 1')
plt.tight_layout()
plt.savefig('/Users/rattie/Data/Eclipse/figures/imagesRGB_and_exp_normalized.png')

## Remap to polar coordinates
pimages_rgb = np.zeros([nexp, ny, nx, 3])
for exp in range(0,3):
    for ch in range(0,3):
        # Azimuthal average
        pimages_rgb[exp,: , :, ch] = cv2.linearPolar(images_exp[exp, :, :, ch], center, nx, cv2.INTER_LANCZOS4 + cv2.WARP_FILL_OUTLIERS)

pimages_rgbf = np.clip(pimages_rgb / maxval, 0, 1)

# Get a selection to take the median over the y-axis.
ymin = 990
ymax = 1100

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(19, 8))
axes[0].imshow(np.clip(pimages_rgbf[0,...], 0, 1), origin='lower')
axes[0].set_title('1/250s')
axes[1].imshow(np.clip(pimages_rgbf[1,...], 0, 1), origin='lower')
axes[1].set_title('1/30s')
axes[2].imshow(np.clip(pimages_rgbf[2,...]*5, 0, 1), origin='lower')
axes[2].set_title('1s x 5')

axes[1].axhline(y=ymin, ls='--', linewidth=1, color='yellow')
axes[1].axhline(y=ymax, ls='--', linewidth=1, color='yellow')

plt.tight_layout()

# Extract and average background radial profile
bkg_az_avg1 = pimages_rgbf[1, ymin:ymax, :, :].mean(axis=0)
bkg_az_avg2 = pimages_rgbf[2, ymin:ymax, :, :].mean(axis=0)
rr = get_radius_array(center, nx, ny)

rmin = 1300
rmax = 1800
# Red
bkg_r, amp_r, index_r = fit_bkg(bkg_az_avg1[:,0], rmin, rmax, rr, radius)
# Green
bkg_g, amp_g, index_g = fit_bkg(bkg_az_avg1[:,1], rmin, rmax, rr, radius)
# Blue
bkg_b, amp_b, index_b = fit_bkg(bkg_az_avg1[:,2], rmin, rmax, rr, radius)

# 1s exposure background fit
rmin = 1300
rmax = 1800
# Red
bkg_r2, amp_r2, index_r2 = fit_bkg(bkg_az_avg2[:,0], rmin, rmax, rr, radius)
# Green
bkg_g2, amp_g2, index_g2 = fit_bkg(bkg_az_avg2[:,1], rmin, rmax, rr, radius)
# Blue
bkg_b2, amp_b2, index_b2 = fit_bkg(bkg_az_avg2[:,2], rmin, rmax, rr, radius)

# for plotting, get a longer array of radius values for extrapolating the profile to the center.
xdata2 = np.arange(radius, nx, 1)
powerlaw = lambda x, amp, index: amp * (x ** index)

fig = plt.figure(figsize=(15, 10))
plt.subplot(2,1,1)
plt.plot(bkg_az_avg1[:,0], 'r-')
plt.plot(bkg_az_avg1[:,1], 'g-')
plt.plot(bkg_az_avg1[:,2], 'b-')
plt.plot(xdata2, powerlaw(xdata2, amp_r, index_r), 'k--')
plt.plot(xdata2, powerlaw(xdata2, amp_g, index_g), 'k-.')
plt.plot(xdata2, powerlaw(xdata2, amp_b, index_b), 'k:')
plt.title('Background 1/30 x 30 azimuthal average over y=[{:d} ; {:d}]'.format(ymin, ymax))
plt.subplot(2,1,2)
plt.plot(bkg_az_avg2[:,0], 'r-')
plt.plot(bkg_az_avg2[:,1], 'g-')
plt.plot(bkg_az_avg2[:,2], 'b-')
plt.plot(xdata2, powerlaw(xdata2, amp_r2, index_r2), 'k--')
plt.plot(xdata2, powerlaw(xdata2, amp_g2, index_g2), 'k-.')
plt.plot(xdata2, powerlaw(xdata2, amp_b2, index_b2), 'k:')
plt.title('Background 1s azimuthal average over y=[{:d} ; {:d}]'.format(ymin, ymax))
plt.tight_layout()

vmin = 0
vmax = 0.5
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(18, 6))
# Show fitted background image
axs[0].imshow(bkg_r, vmin=vmin, vmax=vmax, cmap='gray', origin='lower')
axs[1].imshow(bkg_g, vmin=vmin, vmax=vmax, cmap='gray', origin='lower')
axs[2].imshow(bkg_b, vmin=vmin, vmax=vmax, cmap='gray', origin='lower')
axs[0].set_title('Fitted red background')
axs[1].set_title('Fitted green background')
axs[2].set_title('Fitted blue background')
plt.tight_layout()
plt.savefig('/Users/rattie/Data/Eclipse/figures/fitted_backgrounds.png')

# Remove background from the images
bkg_stack = np.stack([bkg_r, bkg_g, bkg_b], axis=-1)
bkg_stack2 = np.stack([bkg_r2, bkg_g2, bkg_b2], axis=-1)
images_bkg_removed = np.clip(images_expf - bkg_stack, 0, 1)
images_bkg_removed2 = images_expf - bkg_stack2
images_bkg_removed2[images_bkg_removed2 < 0 ] = 0

images_bkg_removed_v = np.clip((images_expf - bkg_stack)*5, 0, 1)

# Display the 3 images in RGB with background removed
fig, axs = plt.subplots(nrows=3, ncols=3, figsize=(19,11))
axs[0,0].imshow(images_expf_clipped[0,...])
axs[0,0].set_title('x 250')
axs[0,1].imshow(images_expf_clipped[1,...])
axs[0,1].set_title('x 30')
axs[0,2].imshow(images_expf_clipped[2,...])
axs[0,2].set_title('x 1')

axs[1,0].imshow(images_bkg_removed[0,...])
axs[1,0].set_title('bkg removed')
axs[1,1].imshow(images_bkg_removed[1,...])
axs[1,1].set_title('bkg removed')
axs[1,2].imshow(images_bkg_removed[2,...])
axs[1,2].set_title('bkg removed')

axs[2,0].imshow(images_bkg_removed2[0,...])
axs[2,0].set_title('bkg removed (2)')
axs[2,1].imshow(images_bkg_removed2[1,...])
axs[2,1].set_title('bkg removed (2)')
axs[2,2].imshow(images_bkg_removed2[2,...])
axs[2,2].set_title('bkg removed (2)')

plt.tight_layout()

plt.savefig('/Users/rattie/Data/Eclipse/figures/Images_background_removed.png')

# Compare the far end of the corona once the backgroun is removed => multiply brightness by some factor
images_bkg_removed2_v = np.clip((images_expf - bkg_stack2)*5, 0, 1)

fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(19,11))
axs[0,0].imshow(images_bkg_removed_v[0,...])
axs[0,0].set_title('bkg removed')
axs[0,1].imshow(images_bkg_removed_v[1,...])
axs[0,1].set_title('bkg removed')
axs[0,2].imshow(images_bkg_removed_v[2,...])
axs[0,2].set_title('bkg removed')

axs[1,0].imshow(images_bkg_removed2_v[0,...])
axs[1,0].set_title('bkg removed (2)')
axs[1,1].imshow(images_bkg_removed2_v[1,...])
axs[1,1].set_title('bkg removed (2)')
axs[1,2].imshow(images_bkg_removed2_v[2,...])
axs[1,2].set_title('bkg removed (2)')
plt.tight_layout()

plt.savefig('/Users/rattie/Data/Eclipse/figures/Images_background_removed_compare.png')

# Radial filter. Try with 1/r

# Exporting to Tiff files: must restore the equivalent data range by multiplying by the exposure and maxval

suffix = ['250th', '30th', '1']
exposures = [250, 30, 1]

# Save the images as numpy arrays
fname = os.path.join(savedir, 'img_bkg_removed.npz')
np.savez(fname, images_bkg_removed2)

#images = np.zeros([nexp, ny, nx, 3])
# "De-normalize" the data to restore the equivalent data range falling within 14-bit depth.
images = images_bkg_removed2 * maxval / tarray3

for exp, s in enumerate(suffix):
    # convert from openCV RGBB to BGR
    image_bgr = cv2.cvtColor(images[exp, ...].astype(np.uint16), cv2.COLOR_RGB2BGR)
    fname = os.path.join(savedir, 'bkg_removed_{:s}.tiff'.format(s))
    cv2.imwrite(fname, image_bgr)

#TODO: remove background independent from 30th and 1s exposure. The 30th is done already. Only the 1s needs to be done.
#TODO: No need to remove the background from the 250s
#TODO: The corona will have to be radially attenuated at the begnining for any overlay with prominence, and increased with radius.


import functools
import pathlib
import re
import typing as typ

import attr
import bs4
import requests

from . import Candidate, Election

FRAGMENT_RE = re.compile(
    r'[0-9`~!@#$%^&*()_|+\=?;:\'"’,.<>\{\}\[\]\\\/]', re.IGNORECASE
)

class InvalidPage(Exception):
    pass

@functools.lru_cache(maxsize=1)
def bs4_loader():
    for loader in ["lxml", "html.parser"]:
        try:
            bs4.BeautifulSoup("<html></html>", loader)
            return loader
        except:
            pass
    raise ValueError("Cannot find a suitable loader")

def none_if_default(value, default):
    return None if value == default else value

def text_if_present(soup):
    if soup:
        return "".join(soup.stripped_strings)
    else:
        return None

def parse(base_url, content):
    _, _, id = base_url.strip("/").rpartition("/")
    soup = bs4.BeautifulSoup(content, bs4_loader())
    print("  ", soup.title)
    if "field_electiontitle" in soup.title.string:
        raise InvalidPage()
    type, election, a_electorate = parse_election(
        "".join(soup.find("h1").stripped_strings)
    )
    b_electorate = parse_electorate(
        "".join(soup.find(class_="views-field-field-subtitle").stripped_strings)
    )
    if a_electorate is None:
        electorate = b_electorate
    else:
        assert b_electorate is not None, f"{a_electorate!r}  {b_electorate!r}"
        assert a_electorate == b_electorate, f"{a_electorate!r}  {b_electorate!r}"
        electorate = a_electorate
    print(f"  {election!r} | {electorate!r}")
    return Election(
        id,
        type,
        election,
        electorate,
        [
            parse_candidate(base_url, election, electorate, candidate)
            for candidate in soup.find_all(class_="candidate")
        ],
    )

def parse_election(heading):
    type, name, ward = None, heading, None
    if heading.endswith(" Ward)"):
        useful, _, _ = heading.partition(" Ward)")
        name, _, ward = useful.partition(" (")
    elif "(" in heading or ")" in heading:
        raise ValueError("Possible invalid election")

    if name.endswith(" Regional Council"):
        name, _, _ = name.partition(" Regional Council")
        type = "Regional Council"
    elif name.endswith(" District Council"):
        name, _, _ = name.partition(" District Council")
        type = "Council"
    elif name.endswith(" City Council"):
        name, _, _ = name.partition(" City Council")
        type = "Council"
    elif name.endswith(" Council"):
        name, _, _ = name.partition(" Council")
        type = "Council"
    elif name.endswith(" Local Board"):
        name, _, _ = name.partition(" Local Board")
        type = "Local Board"
    elif name.endswith(" DHB"):
        name, _, _ = name.partition(" DHB")
        type = "DHB"
    elif name.startswith("Mayor of ") and name.endswith(" District"):
        name, _, _ = name.partition(" District")
        _, _, name = name.partition("Mayor of")
        type = "Mayor"
    elif name.startswith("Mayor of "):
        _, _, name = name.partition("Mayor of")
        type = "Mayor"
    else:
        raise ValueError(f"Unknown election {name} / {heading}")
    if type == "Council" and name.startswith("Mayor of "):
        _, _, name = name.partition("Mayor of")
        type = "Mayor"

    return type, name, ward

def parse_electorate(subtitle):
    if subtitle.startswith("Your vote for the members of"):
        # DHB & local board(for Auckland)
        return None
    elif subtitle.startswith("Your vote for the mayor of"):
        return None
    elif subtitle.startswith("Uncontested: there will be no vote "):
        _, _, part = subtitle.partition("Uncontested: there will be no ")
        return parse_electorate(f"Your {part}")
    elif " Ward/" in subtitle:
        a, _, b = subtitle.rpartition(" Ward/")
        return parse_electorate(f"{a}/{b} Ward")
    elif subtitle.startswith("Your vote for the councillors for the"):
        _, _, area = subtitle.partition("Your vote for the councillors for the")
        return parse_electorate_area(area)
    elif subtitle.startswith("Your vote for the councillor for the"):
        _, _, area = subtitle.partition("Your vote for the councillor for the")
        return parse_electorate_area(area)
    else:
        raise ValueError(f"Unknown electorate type: {subtitle}")

def parse_electorate_area(area):
    if area.endswith("Ward"):
        electorate, _, _ = area.rpartition("Ward")
        assert electorate, f"{area} => Ward"
    elif area.endswith("Constituency"):
        electorate, _, _ = area.rpartition("Constituency")
        assert electorate, f"{area} => Constituency"
    else:
        raise ValueError(f"Unknown councillor type: {area}")
    return electorate.strip()

def parse_candidate(base_url, election, electorate, soup):
    fname = text_if_present(soup.find(class_="field-name-field-first-name")) or ""
    lname = text_if_present(soup.find(class_="field-name-field-last-name")) or ""
    name = f"{fname} {lname}"
    fragment = FRAGMENT_RE.sub("", f"{fname} {lname}")

    extras = {}

    return Candidate(
        election=election,
        electorate=electorate,
        url=f"{base_url}#{fragment}",
        name=name,
        image=none_if_default(
            soup.find(class_="field-name-field-image").find("img")["src"],
            "https://www.policylocal.nz/sites/default/files/styles/round_portrait/public/default_images/default_profile_image.png?itok=bTqalX0V",
        ),
        affiliation=text_if_present(soup.find(class_="field-name-field-affiliation")),
        why_stand=parse_headered_section(soup.find(class_="field-name-field-cq-5")),
        priorities=parse_priorities(soup.find(class_="field-name-field-cq-5")),
        why_elect=parse_headered_section(soup.find(class_="field-name-field-cq-9")),
        before_politics=parse_headered_section(
            soup.find(class_="field-name-field-cq-2")
        ),
        age=parse_headered_section(soup.find(class_="field-name-field-cq-3")),
        lives_in=parse_headered_section(soup.find(class_="field-name-field-cq-4")),
        social_media=parse_social_media(soup.find(class_="group-social")),
    )

def parse_headered_section(soup):
    content = "".join(c for c in soup.children if isinstance(c, bs4.NavigableString))
    return none_if_default(content, "No response given")

def parse_priorities(soup):
    # This will return an empty list if the content is `No response given`
    return ["".join(p.stripped_strings) for p in soup.find_all(class_="priority")]

def parse_social_media(soup):
    if not soup:
        return {}
    social = {}
    for e in soup.find_all(class_="item-list"):
        media_type = e["id"]
        _, _, media_type = media_type.partition("linkicon-node-candidate-field-")
        media_type, _, _ = media_type.rpartition("-")
        assert media_type, e["id"]
        social[media_type] = e.find("a")["href"]


def get_matrix(text=''):
    n, m = [int(i) for i in input(f'Enter size of{text} matrix:').split()]
    matrix = []
    print(f'Enter{text} matrix:')
    for i in range(n):
        row = [float(i) for i in input().split()]
        matrix.append([])
        for j in range(m):
            matrix[i].append(row[j])
    return matrix

def print_matrix(matrix):
    print('The result is:')
    for i in range(len(matrix)):

        print(' '.join(["{:.3f}".format(num) for num in matrix[i]]))

def transpose(matrix):
    n = len(matrix)
    m = len(matrix[0])
    output = []
    for i in range(n):
        output.append([])
        for j in range(m):
            output[i].append(matrix[j][i])
    return output

def determinant(matrix):
    if len(matrix) == 1:
        return matrix[0][0]
    elif len(matrix) == 2:
        return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
    else:
        output = 0
        for i in range(len(matrix)):
            sign = 1
            if i % 2 == 1:
                sign = -1
            output += sign *  matrix[i][0] * determinant(minor(matrix, i, 0))
        return output

def minor(matrix, n, m):
    minor = []
    skip = 0
    for i in range( len(matrix)):
        if i != n:
            minor.append([])
            for j in range(len(matrix[0])):
                if j != m:
                    minor[i - skip].append(matrix[i][j])
        else:
            skip = 1
    return minor

def multiply_by_const(matrix, const):
    output = []
    for i in range(len(matrix)):
        output.append([num * const for num in matrix[i]])
    return output

choice = ''

while choice != '0':
    print('''
1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
4. Transpose matrix
5. Calculate a determinant
6. Inverse matrix
0. Exit''')
    choice = input('Your choice:')
    # Add matrices
    if choice == '1':
        matrix1 = get_matrix(' first')
        matrix2 = get_matrix(' second')
        n1 = len(matrix1)
        m1 = len(matrix1[0])
        n2 = len(matrix2)
        m2 = len(matrix2[0])
        if n2 != n1 or m2 != m1:
            print('ERROR')
        else:
            output = []
            print('The result is:')

            for i in range(n1):
                output.append([])
                for j in range(m1):
                    output[i].append(matrix1[i][j] + matrix2[i][j])
            print_matrix(output)

    # Multiply matrix by a constant
    elif choice == '2':
        matrix = get_matrix()
        scalar = int(input())
        print('The result is:')
        matrix = multiply_by_const(matrix, scalar)
        print_matrix(matrix)

    # Multiply matrices
    elif choice == '3':
        matrix1 = get_matrix(' first')
        matrix2 = get_matrix(' second')
        n1 = len(matrix1)
        m1 = len(matrix1[0])
        n2 = len(matrix2)
        m2 = len(matrix2[0])
        if m1 != n2:
            print('ERROR')
        else:
            output = []
            for row1 in range(n1):
                output.append([])
                for col2 in range(m2):
                    total = 0
                    for row2 in range(n2):
                        total += matrix1[row1][row2] * matrix2[row2][col2]
                    output[row1].append(total)
            print_matrix(output)

    # Transpose matrix
    elif choice == '4':
        print('''
1. Main diagonal
2. Side diagonal
3. Vertical line
4. Horizontal line''')
        diagonal_chioce = input('Your choice:')

        # Main diagonal
        if diagonal_chioce == '1':
            matrix = get_matrix()
            matrix = transpose(matrix)
            print_matrix(matrix)

        # Side diagonal
        elif diagonal_chioce == '2':
            matrix = get_matrix()
            matrix = matrix[::-1]
            matrix = [row[::-1] for row in matrix]
            matrix = transpose(matrix)
            print_matrix(matrix)

        # Vertical line
        elif diagonal_chioce == '3':
            matrix = get_matrix()
            matrix = [row[::-1] for row in matrix]
            print_matrix(matrix)

        # Horizontal line
        elif diagonal_chioce == '4':
            matrix = get_matrix()
            matrix = matrix[::-1]
            print_matrix(matrix)

    # Calculate a determinant
    elif choice == '5':
        matrix = get_matrix()
        n = len(matrix)
        m = len(matrix[0])
        if n != m:
            print('Error')
        else:
            print(determinant(matrix))

    # Inverse matrix
    elif choice == '6':
        matrix = get_matrix()
        n = len(matrix)
        m = len(matrix[0])
        output = None
        if n != m:
            print('Error')
        else:
            det = determinant(matrix)
            if det == 0:
                print("This matrix doesn't have an inverse.")
            else:
                cofactor = []
                for i in range(n):
                    cofactor.append([])
                    for j in range(m):
                        sign = 1
                        if (i + j) % 2 == 1:
                            sign = -1
                        num = sign * determinant(minor(matrix, i, j))
                        cofactor[i].append(num)

                output = multiply_by_const(transpose(cofactor), 1 / det)
                print_matrix(output)


from worklist.constants import SEARCH_BY_NAME_FOR_WORKLISTS, SEARCH_BY_USERNAME_FOR_WORKLISTS, \
    ARTICLE_NUMBER_TO_STATUS_MAPPING
from worklist.models import WorkList, Task

# Helper function for searching worklists
def search_worklist_helper(search_term, search_type):
    worklists = []
    if search_type == '':
        # Default type for search
        search_type = SEARCH_BY_NAME_FOR_WORKLISTS

    if search_term == '':
        # Output all results in case search term is empty
        results = WorkList.objects.all()
    else:
        # In either types the pattern is searched instead of complete names
        if search_type == SEARCH_BY_NAME_FOR_WORKLISTS:
            results = WorkList.objects.filter(name__icontains=search_term)
        elif search_type == SEARCH_BY_USERNAME_FOR_WORKLISTS:
            results = WorkList.objects.filter(created_by__icontains=search_term)

    for result in results:
        worklist = {
            'name': result.name,
            'tags': result.tags,
            'description': result.description,
            'created_by': result.created_by,
            'psid': result.psid,
        }

        worklists.append(worklist)

    return worklists

# Helper function for searching tasks
def search_task_helper(search_term, worklist_name, worklist_created_by):
    tasks = []

    if search_term == '':
        # Output all tasks of that worklist in case search term is empty
        results = Task.objects.filter(worklist__name=worklist_name,
                                      worklist__created_by=worklist_created_by)
    else:
        # Here the pattern is searched instead of complete names
        results = Task.objects.filter(worklist__name=worklist_name,
                                      worklist__created_by=worklist_created_by,
                                      article__name__icontains=search_term)

    for result in results:
        task = {
            'article_name': result.article.name,
            'description': result.description,
            'created_by': result.created_by,
            'status': ARTICLE_NUMBER_TO_STATUS_MAPPING[result.status],
            'progress': result.progress,
            'effort': result.effort,
            'claimed_by': result.claimed_by
        }

        tasks.append(task)

    return tasks


#!/usr/bin/env python3

from email.mime.text import MIMEText
import smtplib
import time

from sump_web import app, get_db

def send_alert(v):
    v = round(v, 1)
    subject = "ALERT: {} water depth is now at {} cm".format(app.config['NAME'], v)
    body = "{}'s average water depth over the past {} seconds is now at {} cm!"
    body = body.format(app.config['NAME'], app.config['CHECK_INTERVAL'], v)
    msg = MIMEText(body)
    msg['From'] = app.config['GMAIL_USERNAME']
    msg['To'] = ', '.join(app.config['ALERT_RECIPIENTS'])
    msg['Subject'] = subject
    gm = smtplib.SMTP('smtp.gmail.com', 587)
    gm.starttls()
    gm.ehlo()
    gm.login(app.config['GMAIL_USERNAME'], app.config['GMAIL_PASSWORD'])
    gm.send_message(msg)
    gm.quit()

if __name__ == '__main__':
    with app.app_context():
        db = get_db()
        last_alert_time = 0
        while True:
            cur = db.execute("SELECT ? - AVG(value) FROM data WHERE timestamp >= datetime('now', 'unixepoch') - ?",
                [app.config['SUMP_DEPTH'], app.config['CHECK_INTERVAL']])
            if cur.rowcount:
                avg = cur.fetchone()[0]
                if avg >= app.config['ALERT_DEPTH'] and int(time.time()) - app.config['CHECK_INTERVAL'] > last_alert_time:
                    try:
                        send_alert(avg)
                    except Exception as e:
                        print(e)
                    else:
                        last_alert_time = int(time.time())
            time.sleep(app.config['CHECK_INTERVAL'])


"""URL configuration for the Tasks application."""
from django.urls import path

from tasks.views import TaskView, task_add, task_complete, task_output_form

app_name = "tasks"
urlpatterns = [
    path("", TaskView.as_view(), name="task_index"),
    path("form", task_output_form, name="task_add_form"),
    path("add", task_add, name="task_add"),
    path("<int:pk>/complete", task_complete, name="task_complete"),
]


import requests
import selenium
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
from datetime import date
import pandas as pd

option=webdriver.ChromeOptions
option.binary_location='C:\Program Files\Google\Chrome\Application\chrome.exe'
location = 'C:/Users/karan/Downloads/chromedriver_win32/chromedriver'
driver = webdriver.Chrome(location)
driver.get('https://www.sbipensionfunds.com/historical-nav/')

def automation():
    frompath='//*[@id="f_date_p1"]'
    topath='//*[@id="f_date_p2"]'
    searchdata="/html/body/section/div/div/div/div/div/div/div/table/tbody/tr/td[5]/input"

    driver.find_element_by_xpath(frompath).send_keys("15-05-2009")
    driver.find_element_by_xpath(topath).send_keys(date.today().strftime("%d-%m-%Y"))
    driver.find_element_by_xpath(searchdata).click()
    driver.implicitly_wait(10)
    datafind()

def datafind():
    L=[]
    bs = BeautifulSoup(driver.page_source,'html5lib')
    table= bs.find("table",class_="table table-hover table-condensed table-bordered")
    rows= table.find_all("tr")

    for i in rows:
        data= i.find_all("td")
        L1=[]
        for j in data:
            L1.append(j.text.strip())
        L.append(L1)

    csvmode= pd.DataFrame(L)
    csvmode.to_csv('Mazic.csv',index=False)

automation()
print("Process Done")
driver.quit()