import argparse
import os
import numpy as np
import os
import sys
current_dir = os.getcwd()
sys.path.append(current_dir)

import torch
import transformers
from tqdm import tqdm

from bias_bench.dataset import load_sentence_debias_data
from bias_bench.debias import DensRay

from bias_bench.model import models
from bias_bench.util import generate_experiment_id

thisdir = os.path.dirname(os.path.realpath(__file__))
parser = argparse.ArgumentParser(
    description="Computes the bias subspace for Densray."
)
parser.add_argument(
    "--persistent_dir",
    action="store",
    type=str,
    default=os.path.realpath(os.path.join(thisdir, "..")),
    help="Directory where all persistent data will be stored.",
)
parser.add_argument(
    "--model",
    action="store",
    type=str,
    default="BertModel",
    choices=["BertModel", "AlbertModel", "RobertaModel", "GPT2Model"],
    help="Model (e.g., BertModel) to compute the SentenceDebias subspace for. "
    "Typically, these correspond to a HuggingFace class.",
)
parser.add_argument(
    "--model_name_or_path",
    action="store",
    type=str,
    default="bert-base-uncased",
    choices=["bert-base-uncased",'bert-base-multilingual-uncased', 'bert-base-multilingual-cased', "albert-base-v2", "roberta-base", "gpt2"],
    help="HuggingFace model name or path (e.g., bert-base-uncased). Checkpoint from which a "
    "model is instantiated.",
)
parser.add_argument(
    "--bias_type",
    action="store",
    type=str,
    choices=["gender", "religion", "race"],
    required=True,
    help="The type of bias to compute the bias subspace for.",
)
parser.add_argument(
    "--lang_debias",
    action="store",
    type=str,
    default='en',
    choices=['en','nl','de','fr','pl','ru','ca'],
    help="Batch size to use while encoding.",
)
parser.add_argument(
    "--batch_size",
    action="store",
    type=int,
    default=32,
    help="Batch size to use while encoding.",
)

if __name__ == "__main__":
    args = parser.parse_args()
    print('Densray')
    experiment_id = generate_experiment_id(
        name="densray",
        model=args.model,
        model_name_or_path=args.model_name_or_path,
        bias_type=args.bias_type,
        lang_debias=args.lang_debias,
    )

    print("Computing bias direction densray:")
    print(f" - persistent_dir: {args.persistent_dir}")
    print(f" - model_name_or_path: {args.model_name_or_path}")
    print(f" - model: {args.model}")
    print(f" - bias_type: {args.bias_type}")
    print(f" - batch_size: {args.batch_size}")
    print(f" - language debias : {args.lang_debias}")

    # Get the data to compute the SentenceDebias bias subspace.
    data = load_sentence_debias_data(
        persistent_dir=args.persistent_dir, bias_type=args.bias_type, lang_debias=args.lang_debias
    ) #female_example

    # Load model and tokenizer.
    model = getattr(models, args.model)(args.model_name_or_path)
    #for name, param in model.named_parameters():
    #    print(name)

    model.eval()
    tokenizer = transformers.AutoTokenizer.from_pretrained(args.model_name_or_path)
    #tokenize the examples (data['female_example] en data['male_example'])
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.to(device)
    all_embeddings_male = []
    all_embeddings_female = []

    n_batches = len(data) // args.batch_size
    print(n_batches)
    for i in tqdm(range(n_batches), desc="Encoding gender examples"):
        offset = args.batch_size * i

        inputs_male = tokenizer(
            [example["male_example"] for example in data[offset : offset + args.batch_size]],
            return_tensors="pt",
            truncation=True,
            padding="max_length",
            max_length=128,
        )

        inputs_female = tokenizer(
            [
                example["female_example"]
                for example in data[offset : offset + args.batch_size]
            ],
            return_tensors="pt",
            truncation=True,
            padding="max_length",
            max_length=128,
        )

        male_input_ids = inputs_male["input_ids"].to(device)
        female_input_ids = inputs_female["input_ids"].to(device)

        male_attention_mask = inputs_male["attention_mask"].to(device)
        female_attention_mask = inputs_female["attention_mask"].to(device)
        with torch.no_grad():
            # Compute average representation from last layer.
            # embedding_male.shape == (batch_size, 128, 768).
            embedding_male = model(
                input_ids=male_input_ids, attention_mask=male_attention_mask
            )["last_hidden_state"]
            embedding_male *= male_attention_mask.unsqueeze(-1)
            embedding_male = embedding_male.sum(dim=1)
            embedding_male /= male_attention_mask.sum(dim=1, keepdims=True)

            embedding_female = model(
                input_ids=female_input_ids, attention_mask=female_attention_mask
            )["last_hidden_state"]
            embedding_female *= female_attention_mask.unsqueeze(-1)
            embedding_female = embedding_female.sum(dim=1)
            embedding_female /= female_attention_mask.sum(dim=1, keepdims=True)

        embedding_male /= torch.norm(embedding_male, dim=-1, keepdim=True)
        embedding_female /= torch.norm(embedding_female, dim=-1, keepdim=True)

        all_embeddings_male.append(embedding_male.cpu().numpy())
        all_embeddings_female.append(embedding_female.cpu().numpy())

    # all_embeddings_male.shape == (num_examples, dim).
    all_embeddings_male = np.concatenate(all_embeddings_male, axis=0)
    all_embeddings_female = np.concatenate(all_embeddings_female, axis=0)
    #L, R = analogy.get_embeddings_from_cropus(corpus, layer)
    # compute densray
    print('now we will compute the bias dimensions')
    densray = DensRay(torch.from_numpy(all_embeddings_male),torch.from_numpy(all_embeddings_female))
    densray.fit()

    print(
        f"Saving computed eigenvector to: {args.persistent_dir}/results/densray/{experiment_id}.pt."
    )
    os.makedirs(f"{args.persistent_dir}/results/densray", exist_ok=True)
    torch.save(
        densray.eigvecs, f"{args.persistent_dir}/results/densray{experiment_id}.pt"
    )
    torch.save(densray.mean, f"{args.persistent_dir}/results/densray{experiment_id}_mean.pt")
    torch.save(densray.std, f"{args.persistent_dir}/results/densray{experiment_id}_std.pt")


import main1 as basic
import kivy
kivy.require('1.0.6')
from kivy.app import App
from kivy.properties import NumericProperty,ReferenceListProperty,ObjectProperty,StringProperty
from kivy.uix.widget import Widget
from kivy.vector import Vector
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from random import randint
#
class SearchScreen(Widget):
    b=[0,0,0,0,0]
    text_input=TextInput(multiline=False)
    text_output=TextInput()
    def put_bar(self):
        self.text=''
    def update(self,dt):
        ##print(str(self.text_input.text),self.text)
        if(str(self.text_input.text)==self.text+'\n'):
            self.text_input.text=''
            basic.insert1(self.text)
            self.text=''
            self.text_output.text+=(self.b[0].text+' ')
        elif(str(self.text_input.text)==self.text+' '):
            if(basic.in_list(0,self.b[0].text)):
                basic.insert1(self.b[0].text)
                self.text_output.text+=(self.b[0].text+' ')
            elif(self.b[1].text!=''):
                basic.insert1(self.b[1].text)
                self.text_output.text+=(self.b[1].text+' ')
            else:
                basic.insert1(self.b[0].text)
                self.text_output.text+=(self.b[0].text+' ')
            self.text_input.text=''
        elif(str(self.text_input.text)!=self.text and str(self.text_input.text!='')):
            self.text=self.text_input.text
            self.search_result=basic.search1(self.text)
            self.print_list=list(self.search_result.keys()).copy()
            #print(self.search_result)
            if(len(self.print_list)>0):
                n=len(self.print_list)
                for i in range (n-1):
                    ##print(self.#print_list)
                    for j in range(0,n-i-1):
                        if(self.search_result[self.print_list[j]]<self.search_result[self.print_list[j+1]]):
                    #        #print(self.search_result[self.#print_list[j]],self.search_result[self.#print_list[j+1]])
                    #        #print(self.#print_list[j],self.#print_list[j+1])
                            temp=self.print_list[j]
                            self.print_list[j]=self.print_list[j+1]
                            self.print_list[j+1]=temp
                    #        #print(self.search_result[self.#print_list[j]],self.search_result[self.#print_list[j+1]])
                    #        #print(self.#print_list[j],self.#print_list[j+1])
            self.send_list=[]
            if(len(self.print_list)>4):
                i=0
                while(len(self.send_list)!=5):
                    self.send_list.append(self.print_list[i])
                    i+=1
            else:
                self.send_list=self.print_list.copy()
            if(self.text) in self.send_list:
                self.send_list.pop(self.send_list.index(self.text))
                self.send_list.insert(0,self.text)
            else:
                self.send_list.insert(0,self.text)
            self.display_words(self.send_list)

    def display_words(self,word_list):
        try:
            for i in range(5):
                self.remove_widget(self.b[i])
        except:
            pass
        #print(word_list)
        if(len(word_list)<5):
            while(len(word_list)!=5):
                word_list.append('')
        for i in range (5):
            self.b[i]=Button(text=word_list[i],x=(self.x+(self.width*(i)/5)),center_y=(self.center_y+(self.height/2)-50),size=(self.width/5,self.height/10),font_size='12sp')
            self.b[i].bind(on_press=self.b_press)
            self.add_widget(self.b[i])
    def b_press(self,obj):
        basic.insert1(obj.text)
class SearchApp(App,Widget):
    def build(self):
        game=SearchScreen()
        game.put_bar()
        Clock.schedule_interval(game.update,1.0/60.0)
        return(game)

if __name__=="__main__":
    SearchApp().run()


from bs4 import BeautifulSoup
import requests
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os

SPOTIPY_CLIENT_ID = os.environ.get("SPOTIPY_CLIENT_ID")
SPOTIPY_CLIENT_SECRET = os.environ.get("SPOTIPY_CLIENT_SECRET")
USERNAME = os.environ.get("USERNAME")
USER_ID = os.environ.get("USER_ID")

SPOTIPY_REDIRECT_URI = "http://example.com"
SCOPE = "playlist-modify-public"

year = input("Which year do you want to travel to? Type the date in this format YYYY-MM-DD: ")
URL = f"https://www.billboard.com/charts/hot-100/{year}"
billboard = requests.get(url=URL)

soup = BeautifulSoup(billboard.text, "html.parser")

songs = soup.find_all("span", class_="chart-element__information__song text--truncate color--primary")

playlist = [song.text for song in songs]

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=SCOPE, redirect_uri=SPOTIPY_REDIRECT_URI))
playlist_url = sp.user_playlist_create(user=USERNAME, name=f"{year} Nostalgia Songs", description=f"{year} in Songs")
playlist_id = playlist_url["id"]

results = [sp.search(q=f"{song_name}", type="track") for song_name in playlist]

tracks = [x["tracks"]["items"][0]["external_urls"]["spotify"] for x in results]

sp.playlist_add_items(playlist_id=playlist_id, items=tracks)
print("Successfully Done Boss")


import util
from sklearn import tree
from sklearn.metrics import accuracy_score
from features import create_features_and_split
from sklearn import ensemble
from sklearn import linear_model
from sklearn.ensemble import GradientBoostingClassifier

def get_feats_labels(df):
    feats = df.drop('device_class', axis=1)
    labels = df['device_class']
    return feats, labels

def get_data():
    df = util.load_data_to_dataframe('dataset/train_split_orig.json')
    train, val = create_features_and_split(df)
    train_feats, train_labels = get_feats_labels(train)
    val_feats, val_labels = get_feats_labels(val)
    return train_feats, train_labels, val_feats, val_labels

def main():
    train_feats, train_labels, val_feats, val_labels = get_data()

    gb = GradientBoostingClassifier(verbose=True, n_estimators=100, learning_rate = 0.05, max_features=20, max_depth = 20, random_state = 0)
    gb.fit(train_feats, train_labels)

    val_predict = gb.predict(val_feats)
    print(accuracy_score(val_labels, val_predict))

main()


import math
import numpy as np
import curses
alpha_values=np.linspace(0,2*math.pi,45)
beta_values=np.linspace(0,2*math.pi,200)
aux1=np.linspace(0,math.pi/4,50)
aux1rev=aux1[::-1]
xtheta_value=[*aux1,*aux1rev]
ytheta_value=np.linspace(0,math.pi,100)
ztheta_value=0
Rinx=8
Riny=4
Rinz=4
Rtx=20
Rty=9

zbuffer=np.full((40,60),-100)
shadsign=['.',',','-','~',':',';','=','!','*','#','$','@']

e=math.cos(ztheta_value)
e1=math.sin(ztheta_value)
curses.initscr()
win=curses.newwin(60,60,0,0)
curses.curs_set(False)
for k in range(100):
    win.clear()
    c=math.cos(xtheta_value[k])
    c1=math.sin(xtheta_value[k])
    d=math.cos(ytheta_value[k])
    d1=math.sin(ytheta_value[k])
    for i in range(200):
        b=math.cos(beta_values[i])
        b1=math.sin(beta_values[i])
        for j in range(45):
            a=math.cos(alpha_values[j])
            a1=math.sin(alpha_values[j])
            dx=round((Rtx+Rinx*a)*b*d*e+(Rty+Riny*a)*b1*(-c*e1+e*d1*c1)+Rinz*a1*(e*c*d1+e1*c1))+29
            dy=round((Rtx+Rinx*a)*b*d*e1+Rinz*a1*(d*e1*d1-e*c1)+(Rty+Riny*a)*b1*(e*c+e1*d1*c1))+13
            dz=round(-(Rtx+Rinx*a)*b*d1+e*(Rinz*c*a1+(Rty+Riny*a)*b1*c1))
            shadcomp=abs(round(11*(-a*b*d1+d*(c*a1+a*b1*c1))))
            if dz > zbuffer[dy,dx]:
                zbuffer[dy,dx]=dz
                win.addstr(dy,dx,shadsign[shadcomp])
            else:
                pass
    win.refresh()
    zbuffer=np.full((40,60),-100)
    win.move(0,0)
curses.endwin()


#Author: Jabulani Mavodze
#Task 3
#Compulsory Task 2
#Date :14 January 2022
#Corrected version

#This block of code declares the variable sentence
#Strips the exclamation mark from the string by using the str.strip() function
#It then changes the string from lower cases to upper cases using the str.upper() function.
sentence = "The!quick!brown!fox!jumps!over!the!lazy!dog!"
sentence = sentence.replace("!"," ")
print(sentence)
sentence = sentence.upper()
print(sentence)

#This block of code splits the string by using the str.split() function sing the empty space delimeter.
#since using the split function yields an array,
#I used indexing to access every word on the array from the tail of the of the array so that I can print it in reverse.
nWord = sentence.split(" ")
sChar1 = nWord[8]
rev1 = sChar1[::-1]+ " "
sChar2 = nWord[7]
rev2 = sChar2[::-1]+ " "
sChar3 = nWord[6]
rev3 = sChar3[::-1]+ " "
sChar4 = nWord[5]
rev4 = sChar4[::-1]+ " "
sChar5 = nWord[4]
rev5 = sChar5[::-1]+ " "
sChar6 = nWord[3]
rev6 = sChar6[::-1]+ " "
sChar7 = nWord[2]
rev7 = sChar7[::-1]+ " "
sChar8 = nWord[1]
rev8 = sChar8[::-1]+ " "
sChar9 = nWord[0]
rev9 = sChar9[::-1]+ " "

nWord2 = rev1 + rev2 + rev3 + rev4 + rev5 + rev6 + rev7 + rev8 + rev9
print(nWord2)


"""
This module tests the interaction of Kafka with Zookeeper with authentication enabled
"""
import logging
import uuid
import pytest

import shakedown

import sdk_auth
import sdk_cmd
import sdk_hosts
import sdk_install
import sdk_marathon
import sdk_repository
import sdk_utils

from tests import auth
from tests import config
from tests import test_utils

log = logging.getLogger(__name__)

@pytest.fixture(scope='module', autouse=True)
def kafka_principals():
    fqdn = "{service_name}.{host_suffix}".format(service_name=config.SERVICE_NAME,
                                                 host_suffix=sdk_hosts.AUTOIP_HOST_SUFFIX)

    brokers = [
        "kafka-0-broker",
        "kafka-1-broker",
        "kafka-2-broker",
    ]

    principals = []
    for b in brokers:
        principals.append("kafka/{instance}.{domain}@{realm}".format(
            instance=b,
            domain=fqdn,
            realm=sdk_auth.REALM))

    principals.append("client@{realm}".format(realm=sdk_auth.REALM))

    yield principals

def get_node_principals():
    """Get a list of zookeeper principals for the agent nodes in the cluster"""
    principals = []

    agent_ips = shakedown.get_private_agents()
    agent_dashed_ips = list(map(
        lambda ip: "ip-{dashed_ip}".format(dashed_ip="-".join(ip.split("."))), agent_ips))
    for b in agent_dashed_ips:
        principals.append("zookeeper/{instance}.{domain}@{realm}".format(
            instance=b,
            # TODO(elezar) we need to infer the region too
            domain="us-west-2.compute.internal",
            realm=sdk_auth.REALM))

    return principals

@pytest.fixture(scope='module', autouse=True)
def zookeeper_principals():
    zk_fqdn = "{service_name}.{host_suffix}".format(service_name="kafka-zookeeper",
                                                    host_suffix=sdk_hosts.AUTOIP_HOST_SUFFIX)

    zk_ensemble = [
        "zookeeper-0-server",
        "zookeeper-1-server",
        "zookeeper-2-server",
    ]

    principals = []
    for b in zk_ensemble:
        principals.append("zookeeper/{instance}.{domain}@{realm}".format(
            instance=b,
            domain=zk_fqdn,
            realm=sdk_auth.REALM))

    principals.extend(get_node_principals())
    yield principals

@pytest.fixture(scope='module', autouse=True)
def kerberos(configure_security, kafka_principals, zookeeper_principals):
    try:
        principals = []
        principals.extend(kafka_principals)
        principals.extend(zookeeper_principals)

        kerberos_env = sdk_auth.KerberosEnvironment()
        kerberos_env.add_principals(principals)
        kerberos_env.finalize()

        yield kerberos_env

    finally:
        kerberos_env.cleanup()

@pytest.fixture(scope='module')
def zookeeper_server(kerberos):
    service_kerberos_options = {
        "service": {
            "name": "kafka-zookeeper",
            "security": {
                "kerberos": {
                    "enabled": True,
                    "kdc_host_name": kerberos.get_host(),
                    "kdc_host_port": int(kerberos.get_port()),
                    "keytab_secret": kerberos.get_keytab_path(),
                }
            }
        }
    }

    # TODO: Remove once kafka-zookeeper is available in the universe
    zookeeper_stub = "https://infinity-artifacts.s3.amazonaws.com/permanent/kafka-zookeeper/20171128-113715-h2qHdoIXKgAEgPOy/stub-universe-kafka-zookeeper.json"
    stub_urls = sdk_repository.add_stub_universe_urls([zookeeper_stub, ])

    try:
        sdk_install.uninstall("beta-kafka-zookeeper", "kafka-zookeeper")
        sdk_install.install(
            "beta-kafka-zookeeper",
            "kafka-zookeeper",
            6,
            additional_options=service_kerberos_options,
            timeout_seconds=30 * 60)

        yield {**service_kerberos_options, **{"package_name": "beta-kafka-zookeeper"}}

    finally:
        sdk_install.uninstall("beta-kafka-zookeeper", "kafka-zookeeper")
        sdk_repository.remove_universe_repos(stub_urls)

@pytest.fixture(scope='module', autouse=True)
def kafka_server(kerberos, zookeeper_server):

    # Get the zookeeper DNS values
    zookeeper_dns = sdk_cmd.svc_cli(zookeeper_server["package_name"],
                                    zookeeper_server["service"]["name"],
                                    "endpoint clientport", json=True)["dns"]

    service_kerberos_options = {
        "service": {
            "name": config.SERVICE_NAME,
            "security": {
                "kerberos": {
                    "enabled": True,
                    "enabled_for_zookeeper": True,
                    "kdc_host_name": kerberos.get_host(),
                    "kdc_host_port": int(kerberos.get_port()),
                    "keytab_secret": kerberos.get_keytab_path(),
                }
            }
        },
        "kafka": {
            "kafka_zookeeper_uri": ",".join(zookeeper_dns)
        }
    }

    sdk_install.uninstall(config.PACKAGE_NAME, config.SERVICE_NAME)
    try:
        sdk_install.install(
            config.PACKAGE_NAME,
            config.SERVICE_NAME,
            config.DEFAULT_BROKER_COUNT,
            additional_options=service_kerberos_options,
            timeout_seconds=30 * 60)

        yield {**service_kerberos_options, **{"package_name": config.PACKAGE_NAME}}
    finally:
        sdk_install.uninstall(config.PACKAGE_NAME, config.SERVICE_NAME)

@pytest.fixture(scope='module', autouse=True)
def kafka_client(kerberos, kafka_server):

    brokers = sdk_cmd.svc_cli(
        kafka_server["package_name"],
        kafka_server["service"]["name"],
        "endpoint broker", json=True)["dns"]

    try:
        client_id = "kafka-client"
        client = {
            "id": client_id,
            "mem": 512,
            "user": "nobody",
            "container": {
                "type": "MESOS",
                "docker": {
                    "image": "elezar/kafka-client:latest",
                    "forcePullImage": True
                },
                "volumes": [
                    {
                        "containerPath": "/tmp/kafkaconfig/kafka-client.keytab",
                        "secret": "kafka_keytab"
                    }
                ]
            },
            "secrets": {
                "kafka_keytab": {
                    "source": kerberos.get_keytab_path(),

                }
            },
            "networks": [
                {
                    "mode": "host"
                }
            ],
            "env": {
                "JVM_MaxHeapSize": "512",
                "KAFKA_CLIENT_MODE": "test",
                "KAFKA_TOPIC": "securetest",
                "KAFKA_BROKER_LIST": ",".join(brokers)
            }
        }

        sdk_marathon.install_app(client)
        yield {**client, **{"brokers": list(map(lambda x: x.split(':')[0], brokers))}}

    finally:
        sdk_marathon.destroy_app(client_id)

@pytest.mark.dcos_min_version('1.10')
@sdk_utils.dcos_ee_only
@pytest.mark.sanity
def test_client_can_read_and_write(kafka_client, kafka_server):
    client_id = kafka_client["id"]

    auth.wait_for_brokers(kafka_client["id"], kafka_client["brokers"])

    topic_name = "authn.test"
    sdk_cmd.svc_cli(kafka_server["package_name"], kafka_server["service"]["name"],
                    "topic create {}".format(topic_name),
                    json=True)

    test_utils.wait_for_topic(kafka_server["package_name"], kafka_server["service"]["name"], topic_name)

    message = str(uuid.uuid4())

    assert auth.write_to_topic("client", client_id, topic_name, message)

    assert message in auth.read_from_topic("client", client_id, topic_name, 1)


import os
import numpy as np
import sys
import time

from auspex.instruments.instrument import SCPIInstrument, StringCommand, FloatCommand, IntCommand
from auspex.experiment import Experiment, FloatParameter
from auspex.stream import DataStream, DataAxis, DataStreamDescriptor, OutputConnector
from auspex.filters.plot import Plotter
from auspex.filters.average import Averager
from auspex.filters.debug import Print
from auspex.filters.channelizer import Channelizer
from auspex.filters.integrator import KernelIntegrator

from auspex.log import logger, logging
logger.setLevel(logging.INFO)

class TestInstrument(SCPIInstrument):
    frequency = FloatCommand(get_string="frequency?", set_string="frequency {:g}", value_range=(0.1, 10))
    serial_number = IntCommand(get_string="serial?")
    mode = StringCommand(name="enumerated mode", scpi_string=":mode", allowed_values=["A", "B", "C"])

class TestExperiment(Experiment):
    """Here the run loop merely spews data until it fills up the stream. """

    # Create instances of instruments
    fake_instr_1 = TestInstrument("FAKE::RESOURE::NAME")

    # Parameters
    field = FloatParameter(unit="Oe")
    freq  = FloatParameter(unit="Hz")

    # DataStreams
    voltage = OutputConnector(unit="V")

    # Constants
    num_samples     = 1024
    delays          = 1e-9*np.arange(100, 10001,100)
    round_robins    = 2
    sampling_period = 2e-9
    T2              = 5e-6

    def init_instruments(self):
        pass

    def init_streams(self):
        descrip = DataStreamDescriptor()
        descrip.add_axis(DataAxis("samples", 2e-9*np.arange(self.num_samples)))
        descrip.add_axis(DataAxis("delay", self.delays))
        descrip.add_axis(DataAxis("round_robins", np.arange(self.round_robins)))
        self.voltage.set_descriptor(descrip)

    def __repr__(self):
        return "<SweptTestExperiment>"

    def run(self):
        pulse_start = 250
        pulse_width = 700

        #fake the response for a Ramsey frequency experiment with a gaussian excitation profile
        idx = 0
        for _ in range(self.round_robins):
            for delay in self.delays:
                if idx == 0:
                    records = np.zeros((5, self.num_samples), dtype=np.float32)
                time.sleep(0.005)
                records[idx,pulse_start:pulse_start+pulse_width] = np.exp(-0.5*(self.freq.value/2e6)**2) * \
                                                              np.exp(-delay/self.T2) * \
                                                              np.sin(2*np.pi * 10e6 * self.sampling_period*np.arange(pulse_width) \
                                                              + np.cos(2*np.pi * self.freq.value * delay))

                #add noise
                records[idx] += 0.1*np.random.randn(self.num_samples)

                if idx == 4:
                    self.voltage.push(records.flatten())
                    idx = 0
                else:
                    idx += 1

        logger.debug("Stream has filled {} of {} points".format(self.voltage.points_taken, self.voltage.num_points() ))

if __name__ == '__main__':

    exp = TestExperiment()
    channelizer = Channelizer(frequency=10e6, bandwidth=5e6, decimation_factor=8, name="Demod")
    ki = KernelIntegrator(kernel="", bias=0, simple_kernel=True, box_car_start=0, box_car_stop=64e-9, frequency=0, name="KI")
    avg1 = Averager("round_robins", name="Average channelizer RRs")
    avg2 = Averager("round_robins", name="Average KI RRs")
    pl1 = Plotter(name="2D Scope", plot_dims=2, palette="Spectral11")
    pl2 = Plotter(name="Demod", plot_dims=2, plot_mode="quad", palette="Spectral11")
    pl3 = Plotter(name="KI", plot_dims=1, plot_mode='real')
    # pl4 = Plotter(name="KI", plot_dims=2, palette="Spectral11")

    edges = [
            (exp.voltage, channelizer.sink),
            (channelizer.source, avg1.sink),
            (channelizer.source, ki.sink),
            (ki.source, avg2.sink),
            (avg1.source, pl2.sink),
            (avg2.source, pl3.sink)
            ]

    exp.set_graph(edges)

    exp.init_instruments()
    exp.add_sweep(exp.freq, 1e6*np.linspace(-0.1,0.1,3))
    exp.run_sweeps()


from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QWidget, QFont, QMessageBox
from ufwi_rpcd.common import tr
from .edw_data import EdwData
from .cell_data import BlankCell, AttributeComboBox, AttributeCell
from .cell_edw_generic import GenericGlobalStatus, GenericSumAttributeCell, GenericCheckBox, GenericFileDialogButton, GenericServiceStatus
from .strings import APP_TITLE

class GenericEdw(EdwData):
    BG_COLOR = "#55aadd"
    def __init__(self, window, edw_list, client):
        EdwData.__init__(self, window, client, tr('All firewalls'))
        self.name = tr('All firewalls')
        self.edw_list = edw_list
        self.dropped_packets_min = -1
        self.dropped_packets_max = 0

        # NuFace related
        self.send_rules = True
        self.template_list = []
        self.template = u''
        self.template_index = 0

        # NuConf related
        self.nuconf_update_send = True

        self.cell_classes = {
            'name' :                    AttributeCell(self, [], 'name'),
            'current_template' :        BlankCell(),
            'template_version' :        BlankCell(),
            'template_edition_time' :   BlankCell(),
            'links_validity' :          BlankCell(),
            'nuface_status' :           BlankCell(),
            'revision' :                BlankCell(),
            'edenwall_type' :           BlankCell(),
            'last_seen' :               BlankCell(),
            'nuconf_update_filepath' :  BlankCell(),
            'user_count' :              BlankCell(),
            'bandwidth_in' :            BlankCell(),
            'bandwidth_out' :           BlankCell(),
            'error' :                   BlankCell(),
            'global_status' :           GenericGlobalStatus(edw_list, []),
            'dropped_packets' :         GenericSumAttributeCell(edw_list, [], 'dropped_packets'),
            'nuauth_users' :            GenericSumAttributeCell(edw_list, [], 'nuauth_users'),
            'nuface_template' :         AttributeComboBox(self, ['nuface_write'], 'template_list', 'template_index', self.templateChanged),
            'send_rules' :              GenericCheckBox(self, edw_list, ['nuface_write'], 'send_rules'),
            'nuconf_update_browse' :    GenericFileDialogButton(self, edw_list, ['nuconf_write'], 'nuconf_update_filepath'),
            'nuconf_update_send' :      GenericCheckBox(self, edw_list, ['nuconf_write'], 'nuconf_update_send'),
            'nuconf_update_status' :    BlankCell(),
            'monitoring_load' :         BlankCell(),
            'monitoring_mem' :          BlankCell(),
        }

        widget = QWidget()
        self.font = QFont(widget.font())
        self.font.setBold(True)
        for cell in self.cell_classes.itervalues():
            cell.setFont(self.font)
            cell.setColor(self.BG_COLOR)

    def getServices(self):
        return {}

    def getCell(self, attr):
        if attr not in self.cell_classes.keys():
            self.refreshServices()
            self.refreshHdd()
        if attr not in self.cell_classes.keys():
            cell = BlankCell()
            cell.setFont(self.font)
            cell.setColor(self.BG_COLOR)
            return u'', cell.getCell()
        return EdwData.getCell(self, attr)

    def getVal(self, attr):
        if attr not in self.cell_classes.keys():
            self.refreshServices()
            self.refreshHdd()
        return EdwData.getVal(self, attr)

    def getCategoryCell(self, attr):
        cell = BlankCell()
        cell.setFont(self.font)
        cell.setColor(self.BG_COLOR)
        return u'', cell.getCell()

    def getCategoryVal(self, attr):
        return u''

    def setRulesetList(self, lst):
        self.ruleset_list = [r[0] for r in lst]
        self.ruleset_list.sort()
        self.emit(SIGNAL('display_cell'), self, 'nuface_rules')

    def setRulesetListError(self, err):
        self.ruleset_list = None
        self.emit(SIGNAL('display_cell'), self, 'nuface_rules')

    def setRulesetCurrent(self, data):
        self.ruleset_apply_time = data[0]
        self.ruleset = data[1]
        self.emit(SIGNAL('display_cell'), self, 'nuface_rules')
        self.emit(SIGNAL('display_cell'), self, 'last_local_apply')

    def setRulesetCurrentError(self, ruleset):
        self.ruleset = u''
        self.ruleset_apply_time = 0
        self.emit(SIGNAL('display_cell'), self, 'nuface_rules')
        self.emit(SIGNAL('display_cell'), self, 'last_local_apply')

    def refreshNuFaceData(self):
        return

    def setError(self, error):
        self.error = error
        self.emit(SIGNAL('display_cell'), self, 'error')

    def setTemplateList(self, lst, lst_time):
        self.template_list = lst
        if self.template not in lst:
            self.template = u''
        self.emit(SIGNAL('display_cell'), self, 'nuface_template')

    def askConfirmation(self, text):
        return QMessageBox.question(None, APP_TITLE, text, QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes

    def templateChanged(self, index):
        if index >= 0:
            self.template_index = index
        if index < 1:
            return
        host_count = self.countRole(['nuface_write'])
        if self.askConfirmation(tr("This will assign the template to %i hosts.\nAre you sure you want to do this?") % host_count):
            if index == 1:
                self.template = u''
            else:
                self.template = self.template_list[index]

            for edw in self.edw_list:
                if not edw.haveRole('nuface_write'):
                    continue
                edw.template = self.template
                edw.template_index = index
                edw.updateMissingLinks()
                self.emit(SIGNAL('display_cell'), edw, 'nuface_template')

        self.emit(SIGNAL('display_cell'), self, 'nuface_template')

    def refreshAttrMinMax(self, attr):
        first_val = True

        for edw in self.edw_list:
            try:
                val = getattr(edw, attr)
            except AttributeError:
                # The async call may not yet have finished for this edw
                continue
            if not isinstance(val, int):
                continue

            if first_val:
                first_val = False
                min = val
                max = val

            if val < min:
                min = val

            if val > max:
                max = val

            try:
                max_at_least = getattr(edw, attr + '_max_at_least')
                if max < max_at_least:
                    max = max_at_least
            except AttributeError:
                pass

        setattr(self, attr + '_min', min)
        setattr(self, attr + '_max', max)

        for edw in self.edw_list:
            edw.setAttrMinMax(attr, min, max)

    def isHeader(self):
        return True

    def refreshServices(self):
        for edw in self.edw_list:
            for svc in edw.getServices().iterkeys():
                if svc not in self.cell_classes.keys():
                    self.cell_classes[svc] = GenericServiceStatus(self.edw_list, [], svc)
                    self.cell_classes[svc].setFont(self.font)
                    self.cell_classes[svc].setColor(self.BG_COLOR)

    def refreshHdd(self):
        for edw in self.edw_list:
            for hdd in edw.cell_classes.iterkeys():
                if hdd[:4] == 'hdd_' and hdd not in self.cell_classes:
                    self.cell_classes[hdd] = BlankCell()
                    self.cell_classes[hdd].setColor(self.BG_COLOR)

    def haveRole(self, perm):
        # Generic firewalls don't check roles
        # It's done on a per firewall basis
        return True

    def haveRoles(self, perm):
        # Generic firewalls don't check roles
        # It's done on a per firewall basis
        return True

    def countRole(self, perm):
        count = 0
        for edw in self.edw_list:
            if edw.haveRoles(perm):
                count += 1
        return count


import requests
import collections
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates

params = {
    'action': 'query',
    'format': 'json',
    'prop':   'revisions',
    'rvlimit': 500,
    'rvstart':'2022-01-26T23:59:59Z',
    'rvend':  '2016-01-01T00:00:00Z',
    'rvprop': 'timestamp|user',
    'rvexcludeuser': 'Cyberbot_I', # Can only exclude one user. This bot posted 500 edits in 1 day
    'titles': 'Russo-Ukrainian War'
}
banned_users = ['Cyberbot_I', 'Aleksandr Grigoryev', 'RenatUK', '178.35.34.32']

timestamp_datetimes = []

while True:
    response = requests.get('https://en.wikipedia.org/w/api.php', params=params).json()
    timestamps = [rev['timestamp'] for rev in filter(lambda e: e['user'] not in banned_users, response['query']['pages']['42085878']['revisions'])]
    timestamp_datetimes.extend(map(lambda s: datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ'), timestamps))
    if 'continue' not in response:
        print('Reached final targeted revision')
        break
    else:
        if 'rvstart' in params:
            del params['rvstart']
        params['rvstartid'] = response['continue']['rvcontinue'].split('|')[1]

date_counts = collections.Counter(map(lambda stamp: stamp.date(), timestamp_datetimes))

dates = list(date_counts.keys())
values = list(date_counts.values())

print("no. of days =", dates[1] - dates[-1] + datetime.timedelta(days=1))

days_rolling_avg = 15
rolling_avg = {}
diff = datetime.timedelta(days=days_rolling_avg//2)
base_day = dates[0] - diff
while base_day >= dates[-1] + diff:
    date_range = [base_day + diff - datetime.timedelta(days=x) for x in range(days_rolling_avg)]
    date_values = list(map(lambda date: date_counts[date], date_range))
    rolling_avg[base_day] = sum(date_values)/len(date_values)
    base_day -= datetime.timedelta(days=1)

fig = plt.figure(figsize=(10.00, 6.50))
plt.style.use('fivethirtyeight')

ax1 = fig.add_subplot(111)
ax1.grid(True)
ax1.set_ylim([0.0, 18])
ax1.yaxis.set_major_locator(plt.MultipleLocator(base=2))
ax1.bar(matplotlib.dates.date2num(dates), values, color='tab:blue', width=4.0, alpha=0.20, label='Day-by-day')
for tl in ax1.get_yticklabels():
    tl.set_color('tab:blue')

ax2 = ax1.twinx()
ax2.grid(False)
ax2.set_ylim([0.0, 9.0])
ax2.yaxis.set_major_locator(plt.MultipleLocator(base=1.0))
ax2.plot_date(matplotlib.dates.date2num(list(rolling_avg.keys())), list(rolling_avg.values()), 'r-', linewidth=0.9, label=f'{days_rolling_avg}-day rolling average')
for tl in ax2.get_yticklabels():
    tl.set_color('r')

ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.title(label='Edits to Wikipedia article $\it{Russo-Ukrainian}$ $\it{War}$', pad=35)
fig.savefig('figure.png', bbox_inches='tight', pad_inches=0.45)
plt.show()


import os
from pathlib import Path

base_path = input('Please enter the path where you want to search for AAE Files: \n')
curr_folder = os.path.basename(base_path)
aae_folder_path = os.path.join(base_path,f'AAE Files ({curr_folder})')
# print(our_path)

entries = os.listdir(base_path)
# print(entries)

for file in entries:
    curr_file_path = os.path.join(base_path,file)
    # print(full_file_path)
    # print(Path(full_file_path).suffix.lower())
    # print(Path(full_file_path).is_file())
    # print(os.path.getsize(full_file_path)/(1024*1024))
    if Path(curr_file_path).suffix.lower()=='.aae' and Path(curr_file_path).is_file():
        # print(f'{file} is an AAE file.')
        if not os.path.exists(aae_folder_path):
            os.mkdir(aae_folder_path)
        new_file_path=os.path.join(aae_folder_path,file)
        os.rename(curr_file_path,new_file_path)
    with open(os.path.join(aae_folder_path,'AAE Files.txt'),'a+') as curr_log:
        curr_log.write(f'\nMoved AAE file - "{file}".')

# F:\Musical World\Music Videos\^ H ^\90's
# H:\Pics\# Timeline #\2022\August 2022\New folder (4)

My Projects/Scripts for Photos Management/Move AAE Files to AAE Files Folder.py


import requests
from selenium import webdriver
import json
from .base_tread import Base_tr
import random
import time
import base64
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import os
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Registr_fb_tr(Base_tr):

    def giv_name(self):
        try:
            self.driver.get("http://ipinfo.io/json")
            el = self.driver.find_element_by_xpath("//pre")
            settings = json.loads(el.text)
            self.country = settings["country"]
            if  self.country == "UA":

                last_name = [
                    "Ковалэнко",
                    "Бондарэнко",
                    "Злэнко",
                    "Саенко",
                    "Семеночко",
                    "Шумэйко",
                    "Мацэйко",
                    "Ламэйко",
                    "Бутэйко",
                    "Потебэнько",
                    "Опэнько",
                    "Витребэнько",
                    "Галаенко",
                    "Мотриенко",
                    "Стратиенко",
                    "Петрэнко",
                    "Павлэнко",
                    "Шевчэнко",
                    "Евэнко",
                    "Майстрэнко"]
                random.shuffle(last_name)

                first_name = [
                    "Агрипина",
                    "Ада",
                    "Аделаїда",
                    "Аделіна",
                    "Адріана",
                    "Богдана",
                    "Богуслава",
                    "Болеслава",
                    "Вероніка",
                    "Вікторина",
                    "Вікторія",
                    "Віола",
                    "Віолетта",
                    "Георгіна",
                    "Глафіра",
                    "Гликерія",
                    "Євлалія",
                    "Євлампія",
                    "Євпраксія",
                    "Єлизавета",
                    "Єпистима",
                    "Інга",
                    "Інеса",
                    "Інна",
                    "Іраїда",
                    "Кароліна",
                    "Катерина",
                    "Кіра",
                    "Клавдія",
                    "Клара",
                    "Клементина",
                    "Корнелія",
                    "Ліліана",
                    "Лілія",
                    "Ліна",
                    "Лія",
                    "Лукерія",
                    "Лукія",
                    "Меланія",
                    "Мелетія",
                    "Мелітина",
                    "Милана",
                    "Милослава",
                    "Сільвія",
                    "Сніжана",
                    "Соломія",
                    "Софія",
                    "Станіслава",
                    "Стелла"

                ]
                random.shuffle(first_name)

            elif  self.country == "RU":
                first_name = ["Агафья",
                              "Агриппина",
                              "Акулина",
                              "Алевтина",
                              "Александра",
                              "Алина",
                              "Алла",
                              "Анастасия",
                              "Ангелина",
                              "Анжела",
                              "Анжелика",
                              "Анна",
                              "Антонина",
                              "Валентина",
                              "Валерия",
                              "Варвара",
                              "Иоанна",
                              "Ираида",
                              "Ирина",
                              "Зинаида",
                              "Злата",
                              "Зоя",
                              "Маргарита",
                              "Марина",
                              "Мария",
                              "Марфа",
                              "Матрёна",
                              "Мирослава"]

                random.shuffle(first_name)
                last_name = [
                    "Амурская",
                    "Ангельская",
                    "Анненская",
                    "Афанасьева",
                    "Афинская",
                    "Бабочкина",
                    "Багирова",
                    "Баженова",
                    "Белоградская",
                    "Белозёрская",
                    "Березина",
                    "Беркутова",
                    "Благовещенская",
                    "Богословская",
                    "Бриллиантова",
                    "Василькова",
                    "Византийская",
                    "Воскресенская",
                    "Гиацинтова",
                    "Гончарова",
                    "Городецкая",
                    "Залесская",
                    "Елисеева",
                    "Златовласова",
                    "Златопольская",
                    "Знаменская",
                    "Зорина",
                    "Игнатьева",
                    "Истомина",
                    "Каменская",
                    "Колосовская",
                    "Лаврентьева",
                    "Луговая",
                    "Лучинская",
                    "Майская",
                    "Малиновская",
                    "Нагорная",
                    "Никитина",
                    "Озерова",
                    "Островская",
                    "Рассказова",
                    "Родионова",
                    "Рябинина",
                    "Румянцева",
                    "Сапфирова",
                    "Серебрянская",
                    "Солнцева",
                    "Ушанская",
                    "Цветкова",

                ]

                random.shuffle(last_name)

            else:
                first_name = ["Adriana", "Ada", "Abby", "Adrianne", "Aimee", "Beth", "Catherine", "Cathy", "Cheryl",
                              "ringo", "Jamie", "nicky", "Diane", "Donna", "trudie", "Esther", "lauren",
                              "ichabod", "arthur", "ashley", "Fiona", "julio", "lorraine", "floyd", "janet",
                              "lydia", "Irene", "Jasmine", "bradley"]
                random.shuffle(first_name)

                last_name = ["Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller",
                             "Wilson", "Moore", "Wilson", "Taylor", "Anderson", "Thomas", "Jackson", "White"]
                random.shuffle(last_name)

            self.f_name = first_name[0]
            self.l_name = last_name[0]
        except:
            return (None, None)

        return (self.f_name, self.l_name)

    def form_filling(self):

        xpath = "//a[@class='_42ft _4jy0 _6lti _4jy6 _4jy2 selected _51sy']"
        self.click_to_xpath(xpath,circle=10, time_sleep=1)

        xpath = "//input[@name='firstname']"
        if not self.set_text(xpath, self.f_name):
            return (False)
        time.sleep(4)

        xpath = "//input[@name='lastname']"
        if not self.set_text(xpath, self.l_name):
            return (False)

        time.sleep(4)
        xpath = "//input[@name='reg_email__']"
        if not self.set_text(xpath, self.settings["email_f"]):
            return (False)

        time.sleep(4)
        xpath = "//input[@name='reg_email_confirmation__']"
        if not self.set_text(xpath,  self.settings["email_f"]):
            return (False)

        time.sleep(4)
        xpath = "//input[@name='reg_passwd__']"
        if not self.set_text(xpath, self.settings["fb_password"]):
            return (False)

        time.sleep(4)
        index = random.randint(2, 27)
        xpath = "//select[@name='birthday_day']"
        if not self.set_current_index(xpath, index):
            self.answer = {"answer": False, "comment": "Не удалось выбрать день рождения"}
            return (False)

        time.sleep(2)
        index = random.randint(1, 10)
        xpath = "//select[@name='birthday_month']"
        if not self.set_current_index(xpath, index):
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = f"Не удалось выбрать месяц рождения"
            return (False)

        time.sleep(2)
        index = random.randint(20, 30)
        xpath = "//select[@name='birthday_year']"
        if not self.set_current_index(xpath, index):
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = f"Не удалось выбрать год рождения"
            return (False)

        time.sleep(2)
        index = 0
        xpath = "//input[@name='sex']"
        if not self.selected_chec_boxs(xpath, index):
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = f"Не удалось выбрать пол"
            return (False)

        time.sleep(2)
        xpath = "//button[@name='websubmit']"
        if not self.click_to_xpath(xpath):
            return (False)

        time.sleep(3)
        xpath = "//div[@id='reg_error_inner']"
        try:
            element = self.driver.find_element_by_xpath(xpath)
            height = element.size["height"]
            if height > 0:
                self.click_to_xpath(xpath)
        except:
            pass
        time.sleep(3)

        # elements = self.driver.find_elements_by_xpath(xpath)
        # if len(elements) > 0:
        #     self.click_to_xpath(xpath)

        # time.sleep(3)
        # elements = self.driver.find_elements_by_xpath(xpath)
        # if len(elements) > 0:

        xpath = "//div[@id='reg_error_inner']"
        try:
            element = self.driver.find_element_by_xpath(xpath)
            height = element.size["height"]
            if height > 0:

                self.answer["status"] = "Ошибка"
                self.answer["comment"] = f"Попытайтесь зарегестрироваться позже"
                return False
        except:
            pass

        return True

    def google_form_filling(self):
        xpath = "//button[@class='_42ft _4jy0 _4jy4 _4jy1 selected _51sy']"
        if self.click_to_xpath(xpath):
            time.sleep(10)
            window_after = self.driver.window_handles[1]
            self.driver.switch_to.window(window_after)

            if not self.click_to_xpath("//li[@class='M8HEDc ibdqA cd29Sd bxPAYd W7Aapd SmR8 znIWoc']"):

                xpath = "//input[@type='email']"
                if not self.set_text(xpath, self.settings["email_f"]):
                    self.answer["status"] = "Ошибка"
                    self.answer["comment"] = f"Время исчерпано, не удалось найти {xpath}"
                    return

                xpath = "//div[@id='identifierNext']"
                if not self.click_to_xpath(xpath):
                    return

                xpath = "//input[@type='password']"
                if not self.set_text(xpath, self.settings["email_password"]):
                    self.answer["status"] = "Ошибка"
                    self.answer["comment"] = f"Время исчерпано, не удалось найти {xpath}"
                    return

                xpath = "//div[@id='passwordNext']"
                if not self.click_to_xpath(xpath):
                    return

                xpath = "(//li[last()-1])[1]"
                if not self.click_to_xpath(xpath):
                    return

                elements = self.driver.find_elements_by_xpath("//button[@id='checkpointSubmitButton']")
                if len(elements) > 0:
                    self.answer["status"] = "Ошибка"
                    self.answer["comment"] = f"Аккаунт заблокированн"
                    return
                xpath = "//input[@id='identifierId']"
                if not self.set_text(xpath, self.settings["email_s"]):
                    self.answer["status"] = "Ошибка"
                    self.answer["comment"] = f"Время исчерпано, не удалось найти {xpath}"
                    return

                elements = self.driver.find_elements_by_xpath("//button[@id='checkpointSubmitButton']")
                if len(elements) > 0:
                    self.answer["status"] = "Ошибка"
                    self.answer["comment"] = f"Аккаунт заблокированн"
                    return
                xpath = "//input[@id='identifierId']"
                element = self.driver.find_element_by_xpath(xpath)
                element.send_keys(Keys.ENTER)

                elements = self.driver.find_elements_by_xpath("//button[@id='checkpointSubmitButton']")
                if len(elements) > 0:
                    self.answer["status"] = "Ошибка"
                    self.answer["comment"] = f"Аккаунт заблокированн"
                    return

            time.sleep(3)
            if len(self.driver.window_handles) > 1:
                window_after = self.driver.window_handles[1]
                self.driver.switch_to.window(window_after)
                self.driver.close()
                self.driver.switch_to.window(self.driver.window_handles[0])

            for i in range(3):
                webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
                time.sleep(1)

            try:
                xpath = "//input[@name='ok']"
                self.click_to_xpath(xpath)
            except:
                pass

            xpath = "(//div[@id='bluebar_profile_and_home']//div//div)[1]"
            self.click_to_xpath(xpath)
        return True

    def load_img(self):
        # xpath = "(//div[@class='linkWrap noCount'])[1]"
        # if not self.click_to_xpath(xpath):
        #     self.answer["status"] = "Ошибка"
        #     self.answer["comment"] = f"Время исчерпано, не удалось найти {xpath}"
        #     return (False)

        if "face_picture_href" in self.settings and self.settings["face_picture_href"] != False:
            self.scroll_page_up(10)
            self.face_picture = f"face_picture_{self.settings['id_scenarios']}.jpg"
            self.face_picture_href = self.settings["face_picture_href"]
            try:
                os.remove(self.face_picture)
            except:
                pass

            p = requests.get(self.face_picture_href)
            out = open(self.face_picture, "wb")
            out.write(p.content)
            out.close()

            xpath = "//div[contains(@class, 'fbTimelineProfilePicSelector')]"
            if not self.click_to_xpath(xpath):
                return (False)

            for i in range(40):
                try:
                    elem = self.driver.find_element_by_xpath("//a[@class='_3cia']/div[@class='_3jk']/input[@type='file']")
                    elem.send_keys(self.face_picture)
                    break
                except:
                    time.sleep(1)

            time.sleep(10)

            xpath = "//button[@data-testid='profilePicSaveButton']"
            if not self.click_to_xpath(xpath):
                return False

            try:
                os.remove(self.face_picture)
            except:
                pass

            time.sleep(5)
            for i in range(3):
                webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
                time.sleep(1)

            self.scroll_page_up(15)
        else:
            self.face_picture = None

        if "cover_picture_href" in self.settings and self.settings["cover_picture_href"] != False:
            self.cover_picture = f"cover_picture_{self.settings['id_scenarios']}.jpg"
            self.cover_picture_href = self.settings["cover_picture_href"]
            try:
                os.remove(self.cover_picture)
            except:
                pass

            p = requests.get(self.cover_picture_href)
            out = open(self.cover_picture, "wb")
            out.write(p.content)
            out.close()

            xpath = "//div[@id='fbProfileCoverPhotoSelector']"
            element_to_hover_over = self.driver.find_element_by_xpath(xpath)
            hover = ActionChains(self.driver).move_to_element(element_to_hover_over)
            hover.perform()

            if not self.click_to_xpath(xpath):
                return (False)

            time.sleep(3)
            elem = self.driver.find_element_by_xpath("//label//div[@class='_3jk']//input")
            elem.send_keys(self.cover_picture)
            time.sleep(10)

            xpath = "//button[@name='save']"
            if not self.click_to_xpath(xpath):
                return (False)

            try:
                os.remove(self.cover_picture)
            except:
                pass

        else:
            self.cover_picture = None

        return

    def login_mail(self):
        type_mail = self.settings["email_f"].split("@")[1]

        if type_mail == "yahoo.com":

            self.driver.get("https://login.yahoo.com/config/login?.src=fpctx&.intl=us&.lang=en-US&.done=https%3A%2F%2Fwww.yahoo.com")

            time.sleep(4)

            xpath = "//input[@id='login-username']"
            if not self.set_text(xpath, self.settings["email_f"]):
                return False
            else:
                self.enter_click(xpath)

            time.sleep(4)

            xpath = "//input[@id='login-passwd']"
            if not self.set_text(xpath, self.settings["email_password"]):
                return False

            else:
                self.enter_click(xpath)

            self.driver.get("https://mail.yahoo.com")

            time.sleep(5)
            if "mail.yahoo.com/d/folders/1" not in  self.driver.current_url:
                return False

        elif type_mail == "gmail.com":

            self.driver.get(
                "https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin")

            u = ["https://policies.google.com/privacy?gl=TM&hl=en",
                 "https://mail.google.com/mail/u/0/#inbox",
                 "https://mail.google.com/mail/#inbox",
                 "https://policies.google.com/privacy?gl=US&hl=ru",
                 "https://mail.google.com/mail/u/0/"]

            if not self.driver.current_url in u:

                xpath = "//div[@id='profileIdentifier']"
                if self.check_xpath(xpath):
                    xpath = "//input[@name='password']"
                    self.set_text(xpath, self.settings["email_password"])
                    self.enter_click(xpath)

                time.sleep(3)
                xpath = "//input[@id='identifierId']"
                if not self.set_text(xpath, self.settings["email_f"]):
                    return False
                else:
                    self.enter_click(xpath)

                time.sleep(4)
                xpath = "//input[@name='password']"
                if not self.set_text(xpath, self.settings["email_password"]):
                    return False
                else:
                    self.enter_click(xpath)

            time.sleep(5)

            self.driver.switch_to_window(self.driver.window_handles[0])

            xpath = "//li[last()-1]"
            if self.click_to_xpath(xpath,circle=5, time_sleep=1):
                xpath = "//input[@id='knowledge-preregistered-email-response']"
                self.set_text(xpath, self.settings["email_s"])
                self.enter_click(xpath)

            time.sleep(2)

            url = "accounts.google.com/signin/newfeatures"
            if url in self.driver.current_url:
                xpath = "//div[@class='U26fgb XHsn7e dURtfb Tk2jV']"
                self.click_to_xpath(xpath)
                xpath ="//div[@class='U26fgb XHsn7e dURtfb Tk2jV M9Bg4d']"
                for i in range(2):
                    self.click_to_xpath(xpath)

                xpath = "//div[@class='U26fgb O0WRkf zZhnYe e3Duub C0oVfc WVqvne ioikHf TMcGbb']"

                self.click_to_xpath(xpath)

            time.sleep(5)

            if not self.driver.current_url in u:
                return False

        else:
            return False

        return True

    def confirm_email(self):

        type_mail = self.settings["email_f"].split("@")[1]

        if type_mail == "yahoo.com":

            self.driver.get("https://mail.yahoo.com")

            xpath = "//button[@data-test-id='themes-cue-close']"
            self.click_to_xpath(xpath,circle=10, time_sleep=1)

            xpath = """(//a[@aria-label="Facebook's email"])[position() = last()]"""
            if self.click_to_xpath(xpath,circle=10, time_sleep=1):

                xpath = "//table/tbody/tr[3]/td[2]/table/tbody/tr[2]/td[3]/a"
                self.click_to_xpath(xpath,circle=10, time_sleep=1)
                return True

        elif type_mail == "gmail.com":

            self.driver.get("https://mail.google.com")

            xpath = "//button[@name='welcome_dialog_next']"
            self.click_to_xpath(xpath,circle=5, time_sleep=1)

            xpath = "//button[@name='ok']"
            self.click_to_xpath(xpath,circle=5, time_sleep=1)

            xpath = "(//span[@email='registration@facebookmail.com'])[position() = last()]/../../.."
            try:
                WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath)))
                self.click_to_xpath(xpath)
                xpath = "//span[@style='font-family:Helvetica Neue,Helvetica,Lucida Grande,tahoma,verdana,arial,sans-serif;white-space:nowrap;font-weight:bold;vertical-align:middle;color:#ffffff;font-size:14px;line-height:14px']"
                self.click_to_xpath(xpath)
                time.sleep(3)
                return True
            except:
                return False
        else:
            return False

        return False

    def answers_questions(self):
        #where did you go to university?

        ua_ans = {  "which city do you live in?": ["Kiev", "Dnipro", "Kharkov", "Odessa"],
                    "what city are you from?":["Kiev", "Dnipro", "Kharkov", "Odessa"],
                    "where did you go to high school?":["киев школа 54",
                                                "киев школа 79",
                                                "киев школа 313",
                                                "киев школа 252",
                                                "киев школа 88",
                                                "киев школа 302",
                                                "киев школа 3",
                                                "киев школа 235",
                                                "киев школа 204",
                                                "киев школа 207"],
                    "where did you go to college?":["Львовский национальный университет им. Ивана Франка",
                                            "Национальный технический университет «Харьковский политехнический институт»",
                                            "Сумской государственный университет",
                                            "Киевский национальный университет им. Шевченко",
                                            "Харьковский национальный университет им. Каразина."],
                    "where do you work?":["Self-employed",
                                "student",
                                "retired"],
                    "где вы живете?":["Kiev", "Dnipro", "Kharkov", "Odessa"],
                    "из какого вы города?":["Kiev", "Dnipro", "Kharkov", "Odessa"],
                    "в какой школе вы учились?":["киев школа 54",
                                                "киев школа 79",
                                                "киев школа 313",
                                                "киев школа 252",
                                                "киев школа 88",
                                                "киев школа 302",
                                                "киев школа 3",
                                                "киев школа 235",
                                                "киев школа 204",
                                                "киев школа 207"],
                    "в каком вузе вы учились?":["Львовский национальный университет им. Ивана Франка",
                                            "Национальный технический университет «Харьковский политехнический институт»",
                                            "Сумской государственный университет",
                                            "Киевский национальный университет им. Шевченко",
                                            "Харьковский национальный университет им. Каразина."],
                    "где вы работаете?":["Self-employed",
                                "student",
                                "retired"],
                    "У якому місті ви живете?":["Kiev", "Dnipro", "Kharkov", "Odessa"],
                    "в якому місті Ви мешкаєте?":["Kiev", "Dnipro", "Kharkov", "Odessa"],
                    "У якій школі ви вчилися?":["киев школа 54",
                                                "киев школа 79",
                                                "киев школа 313",
                                                "киев школа 252",
                                                "киев школа 88",
                                                "киев школа 302",
                                                "киев школа 3",
                                                "киев школа 235",
                                                "киев школа 204",
                                                "киев школа 207"],
                    "Який коледж ви відвідували?":["Львовский национальный университет им. Ивана Франка",
                                            "Национальный технический университет «Харьковский политехнический институт»",
                                            "Сумской государственный университет",
                                            "Киевский национальный университет им. Шевченко",
                                            "Харьковский национальный университет им. Каразина."],
                    "Де ви працюєте?":["Self-employed",
                                "student",
                                "retired"]}

        ru_ans = {
                    "which city do you live in?": ["Moscow Russia Moscow"],
                    "what city are you from?":["Moscow Russia Moscow"],
                    "where did you go to high school?":["москва школа 303",
                                                "москва школа 124",
                                                "москва школа 252",
                                                "москва школа 2025",
                                                "москва школа 54",
                                                "москва школа 42",
                                                "москва школа 12"],
                    "where did you go to college?":["Львовский национальный университет им. Ивана Франка",
                                                "Национальный технический университет «Харьковский политехнический институт»",
                                                "Сумской государственный университет",
                                                "Киевский национальный университет им. Шевченко",
                                                "Харьковский национальный университет им. Каразина."],
                    "where do you work?":["Self-employed","student","retired"],
                    "what is your position at retired":["retiree"],
                    "где вы живете?":["Moscow","Saint-Peterburg","Omsk"],
                    "из какого вы города?":["Moscow","Saint-Peterburg","Omsk"],
                    "в какой школе вы учились?":["москва школа 303",
                                                "москва школа 124",
                                                "москва школа 252",
                                                "москва школа 2025",
                                                "москва школа 54",
                                                "москва школа 42",
                                                "москва школа 12"],
                    "в каком вузе вы учились?":["Львовский национальный университет им. Ивана Франка",
                                                "Национальный технический университет «Харьковский политехнический институт»",
                                                "Сумской государственный университет",
                                                "Киевский национальный университет им. Шевченко",
                                                "Харьковский национальный университет им. Каразина."],
                    "где вы работаете?":["Self-employed","student","retired"],
                    "У якому місті ви живете?":["Moscow Russia Moscow"],
                    "в якому місті Ви мешкаєте?":["Moscow Russia Moscow"],
                    "У якій школі ви вчилися?":["москва школа 303",
                                                "москва школа 124",
                                                "москва школа 252",
                                                "москва школа 2025",
                                                "москва школа 54",
                                                "москва школа 42",
                                                "москва школа 12"],
                    "Який коледж ви відвідували?":["Львовский национальный университет им. Ивана Франка",
                                                    "Национальный технический университет «Харьковский политехнический институт»",
                                                    "Сумской государственный университет",
                                                    "Киевский национальный университет им. Шевченко",
                                                    "Харьковский национальный университет им. Каразина."],
                    "Де ви працюєте?":["Self-employed","student", "retired"]}

        if self.country == "UA":
            ans = ua_ans
        else :
            ans = ru_ans

        for i in range(5):
            time.sleep(5)
            for j in range(3):
                webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
                time.sleep(1)

            self.scroll_page_up(10)
            xpath = "((//div[@id='profile_timeline_info_review']/li/div/div/div/div)[2]/div)[1]"
            try:
                element = self.driver.find_element_by_xpath(xpath)
                question = element.text
                question = question.split(', ')
            except:
                continue

            if len(question) > 1:
                question = question[1]
            else:
                question = question[0]

            question = question.lower()

            if question not in ans:
                print(f"Не определено {question}")

                xpath = "//button[@value='deny']"
                self.click_to_xpath(xpath)

            else:
                for j in range(3):
                    webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
                    time.sleep(1)
                answears = ans[question]
                random.shuffle(answears)
                my_answer = answears[0]

                xpath = "//input[@data-testid='searchable-text-input']"
                self.set_text(xpath, my_answer)

                xpath = "//li[@data-testid='choose_contact_button']"
                self.click_to_xpath(xpath)

                xpath = "//input[@data-testid='searchable-text-input']"
                self.enter_click(xpath)

                xpath = "//button[@value='confirm']"
                self.click_to_xpath(xpath)

    def add_groups(self):
        try:
            time.sleep(3)
            self.driver.switch_to_window(self.driver.window_handles[1])
            time.sleep(3)
            self.driver.refresh()
        except:
            return

        if self.country == "RU":
            file_groups = "RU group.txt"

        elif self.country == "UA":
            file_groups = "UA group.txt"

        else:
            file_groups = "general.txt"

        links = []
        f = open(file_groups, "r")

        for link in f:
            if link == "\n":
                continue

            links.append(link.replace("\n",""))

        random.shuffle(links)

        for i in range(2):
            link = links[i]
            self.driver.get(link)

            for j in range(3):
                webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
                time.sleep(1)

            xpath = "//button[@data-testid='page_profile_like_button_test_id']"
            self.click_to_xpath(xpath)

    def run(self):
        self.cover_picture = None
        self.face_picture = None
        answer = self.connect_to_multilogin()
        if not answer["answer"]:
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = answer["comment"]
            try:
                self.driver.quit()
            except:
                pass
            return

        if not self.login_mail():
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = f"Не удалось зайти на почту"
            self.driver.quit()
            return

        self.giv_name()

        link = 'https://www.facebook.com'
        if not self.get_tu_link(link):
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = f"Не удалось зайти на https://www.facebook.com"
            self.driver.quit()
            return

        time.sleep(3)

        if not self.check_block():
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = f"Аккаунт заблокирован"
            self.driver.quit()
            return

        for i in range(3):
            webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
            time.sleep(1)

        xpath = "//span[@class='_2md']"
        if self.find_xpath(xpath, circle=5, time_sleep=1) and  not "www.facebook.com/confirmemail.php" in self.driver.current_url :
                self.answer["status"] = "Ошибка"
                self.answer["comment"] = f"Аккаунт уже зарегистрированн"
                self.driver.quit()
                return

        elif not "www.facebook.com/confirmemail.php" in self.driver.current_url:
            if not self.form_filling():
                self.driver.quit()
                return

        for i in range(3):
            webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
            time.sleep(1)

        if not self.check_block():
            self.answer["status"] = "Ошибка"
            self.answer["comment"] = f"Аккаунт заблокирован после заполнения формы"
            self.driver.quit()
            return

        if  not "www.facebook.com/confirmemail.php" in self.driver.current_url :
                self.answer["status"] = "Ошибка"
                self.answer["comment"] = f"Аккаунт уже зарегистрированн"
                self.driver.quit()
                return

        xpath = "//*[@id='bluebar_profile_and_home']/div/div/a"
        self.click_to_xpath(xpath)

        for i in range(4):
            webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
            time.sleep(1)

        self.load_img()

        self.answers_questions()

        if self.cover_picture == None:
            cover_picture = "caver не залит"
        else:
            cover_picture = "caver залит"

        if self.face_picture == None:
            face_picture = " face не залит"
        else:
            face_picture = "face залит"

        if not self.confirm_email():
            email_comment = "почта не подтверждена"
        else:
            email_comment = "почта подтверждена"
            self.add_groups()

        self.answer["status"] = "Выполнен"
        self.answer["comment"] = f"Аккаунт зарегистрирован в фб {email_comment,face_picture,cover_picture}"
        self.driver.quit()


from tkinter import *
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
import astropy.io.ascii as ascii
import astropy.table as table
import xlsxwriter
from scipy.signal import savgol_filter

# This function opern window to open several excel files and return a list of names of the files
def get_files():
    root = Tk()
    root.withdraw()
    root.filename = filedialog.askopenfilenames(initialdir = "/",title = "Select file",filetypes = (("asci files","*.asc"),("all files","*.*")))
    root.destroy()
    return root.filename

frame = get_files()

lista = []
for j in range(1, len(frame)+1):
    lista.append('df_%s' % j)

print(lista)

dic = {}
for i, j in zip(frame, lista):
    a = ascii.read(i, data_start=54) #54, 23
    dic[j] = pd.DataFrame(a.as_array())

#df = *dataframe with identifiers for rows and dates for columns*
#for date in list_of_dates:
#    df['New Column Name'] = *a new column with a new date to be added to the df*

def add_columns(dic,lista_nombres):
    df_0 = dic["df_1"]["col1"]
    n = 0
    for i in lista:
        c = dic[i]['col2']
        s_c = savgol_filter(c, 51, 3)
        s_c = pd.DataFrame(s_c, columns= ["df_"+ str(n)])
        df = pd.concat([df_0, s_c], axis=1)
        df_0 = df
        n += 1
    return df_0

dataframe = add_columns(dic,lista)

plt.plot(dataframe["col1"], dataframe.iloc[:,2:-1])
plt.show()

#This code plot df.iloc[:,0] and df.iloc[:,1] to every df_i
#df.iloc[:,0] is the wavelength and df.iloc[:,1] is the property
for i in lista:
    d = dic[i]
    plt.plot(d['col1'], d['col2'])
    plt.xlabel('Wavelength')
    plt.ylabel('Fluorescence')
    plt.xlim(332,600)

#archivo = str(input("Ingrese el nombre del archivo: "))
#archivo_0 = archivo + ".xlsx"

#this function open windows to save the file in xlsx format
def save_file():
    root = Tk()
    root.withdraw()
    root.filename = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("Excel files","*.xlsx"),("all files","*.*")))
    root.destroy()
    return root.filename

archivo_1 = save_file() + ".xlsx"
print(archivo_1)

with pd.ExcelWriter(archivo_1, engine='xlsxwriter') as writer:
    dataframe.to_excel(writer, sheet_name="datos_titulacion")


# Propriedade side

# Top (superior), Bottom (inferior), Right (Direito), Left (Esquerdo)

# Vincula uma extremidade a um componente.

from tkinter import *

janela = Tk()

lb1= Label(janela, text='Right',bg='white')
lb2= Label(janela, text='Bottom',bg='white')
lb3= Label(janela, text='Left',bg='white')
lb4= Label(janela, text='top',bg='white')

lb1.pack(side=RIGHT)
lb2.pack(side=BOTTOM)
lb3.pack(side=LEFT)
lb4.pack(side=TOP)

janela['bg'] = 'black'

janela.geometry('400x400+200+200')
janela.mainloop()


from itertools import combinations
class Solution(object):
    def numIdenticalPairs(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        pairs = 0
        dic = {}
        for i in range(len(nums)):
            if nums[i] in dic:
                (dic[nums[i]]).append(i)
            else:
                dic[nums[i]]=[i]
        for _,value in dic.items():
            pairs+=len(list(combinations(value,2)))
        return pairs


#!/usr/bin/env python3
#Interacting with Files. Read a file and print the content

import os

'''
cwd = os.getcwd() #get the current worlking director
files = os.listdir(cwd) #Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
'''

xmen_file = open('xmen_base.txt', 'r') #r means read

xmen_file.seek(0) #this enable you to seek at which position of the file. ) is the beginning
number_of_characters = len(xmen_file.read())
xmen_file.seek(0)
print(xmen_file.read())

print(f'File have {number_of_characters} number of characters')
xmen_file.close() #to close the file. Impt to close after using


from flask import Flask, render_template, request
# from flask_lt import run_with_lt
# from a2weboriginal import Query
from a2web import Query
import os
import time

Q = Query()
app = Flask(__name__)
# run_with_lt(app)
last_time = {}

barGraphExists = os.path.exists('./global_bargraph.png')
wordCloudExists = os.path.exists('./global_word_cloud.png')

if not (barGraphExists) and not (wordCloudExists):
    # Q.global_visualization(Q.topics_to_doc, Q.cleaned_docs)
    pass

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'GET':
        result = None
        query = None
        model = None
        length = None
        visualdata = None

    if request.method == 'POST':
        client_ip = request.remote_addr
        current_time = int(time.time())
        time_difference = current_time - last_time.get(client_ip, 0)
        if time_difference < 5:
            return 'Form submission rate exceeded.'
        else:
            last_time[client_ip] = current_time
            data = request.form
            query = data.get("query")
            model = data.get("model")
            print(query, model)
            result = Q.search(query, model)
            length = len(result["result"])
            visualdata = result["query_visualizations"]
            if model == "jdrs":
                model = "JDRS"
    return render_template('search.html', result=result, query=query, model=model, length=length, visualdata=visualdata)

@app.route('/test', methods=['GET'])
def test():
    return render_template('test1.html')

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=int(
        os.environ.get("FLASK_RUN_PORT", "80")), debug=True)


services = {
'ftp':21,
'ssh':22,
'smtp':25,
'domain':53,
'http':80,
'pop3':110,
'imap':143,
'https':443
}

return_codes = {
'OK':0,
'ERROR':1,
'TARGET_ERROR':50,      #Defined target error
'NOTSURE':100           #Service version detection inconclusive => possibly n/a
}


from flask import Flask
from flask import render_template
from flask import abort, redirect, url_for
import subprocess

app = Flask(__name__)
home = '/home/cloudy/Github/automation-sop-ajk-using-ansible/'

@app.route('/')
def hello_world():
    return render_template('index.html')

@app.route('/start')
def start():
    command = "ansible-playbook -i %shosts %sservices.yml --tags 'start'" % ( home, home )
    print(command)
    subprocess.call(command, shell=True)
    return redirect('/')

@app.route('/restart')
def restart():
    command = "ansible-playbook -i %shosts %sservices.yml --tags 'restart'" % ( home, home )
    print(command)
    subprocess.call(command, shell=True)
    return redirect('/')

@app.route('/stop')
def stop():
    command = "ansible-playbook -i %shosts %sservices.yml --tags 'stop'" % ( home, home )
    print(command)
    subprocess.call(command, shell=True)
    return redirect('/')


import torch
import cv2
import torchvision
import os.path as osp
import torch.nn as nn
import torch.optim as optim
import numpy as np
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
from itertools import product
from modules import *
from common import *

class L2Norm(nn.Module):
    """
    カスタムレイヤー
    ・お約束
        ⇒ nn.Modileを継承する
        ⇒ レイヤーのparameterはnn.Prameterで設定する。
        ⇒ 順伝播はforwardメソッドを定義する。
    """

    def __init__(self, in_channels=512, scale=20):
        super(L2Norm, self).__init__()  # 親クラスのコンストラクタ実行
        self.weight = nn.Parameter(torch.Tensor(in_channels))  # nn.Parameterの引数はTensor型
        self.scale = scale  # 係数weightの初期値として設定する値
        self.reset_parameters()  # parameterの初期化
        self.eps = 1e-10

    def reset_parameters(self):
        # 結合パラメータの値を大きさscaleの値にする初期化を実行
        nn.init.constant_(self.weight, self.scale)  # weightの値がすべてscale(=20)になる

    def forward(self, x):
        # norm: チャネル方向の2乗和の平方根
        norm = x.pow(2).sum(dim=1, keepdim=True).np.sqrt() + self.eps
        x = torch.div(x, norm)  # normで割ることで正規化

        # self.weightはtensor([512])なので、tensor([batch_num, 512, 特徴量マップHeight, 特徴量マップwidth])に変換
        weights = self.weight.unsqueeze(0).unsqueeze(2).unsqueeze(3).expand_as(x)
        out = weights * x  #  重み * 正規化された入力値
        return out

class DefaultBox:
    def __init__(self, cfg):
        super(DefaultBox, self).__init__()

        # 初期設定
        self.image_size = cfg["input_size"]  # 画像のサイズ(=300)
        self.feature_maps = cfg[
            "feature_maps"
        ]  # 各sourceの特徴量mapのサイズ(38*38, 19*19, 10*10, 5*5, 3*3, 1*1)
        self.num_priors = len(cfg["feature_maps"])  # sourceの個数(=6)
        self.steps = cfg["steps"]  # DBoxのピクセルサイズ
        self.min_sizes = cfg["min_sizes"]  # 小さい正方形Dboxのサイズ
        self.max_sizes = cfg["max_sizes"]  # 大きい正方形Dboxのサイズ
        self.aspect_ratio = cfg["aspect_ratio"]  # 長方形Dboxのアスペクト比

    def generate_dbox_list(
        self,
    ):
        """
        Dboxのリストを作成する関数
        """
        mean = []
        for n, f in enumerate(self.feature_maps):  # feature_maps: (38,19,10,5,3,1)
            for i, j in product(range(f), repeat=2):
                # 特徴量マッピングの画像サイズ
                f_k = self.image_size / self.steps[n]  # self.steps[n]: 8,16,32,64,

                # dboxの中心座標x,y (但し、0~1で規格化している)
                # 1 pixel毎にbboxを作成するため、+0.5すると1*1の中心部分に相当する
                # f_k(特徴量マップのサイズ)で割ることで中心座標を0~1に規格化している
                cx = (j + 0.5) / f_k  # dboxの中心x座標
                cy = (i + 0.5) / f_k  # dboxの中心y座標

                # アスペクト比(1,1)の小さいdbox(cx, cy, width, height)
                # min_sizes: [30,60,111,162,213,264]
                s_k = self.min_sizes[n] / self.image_size  # Dboxの1辺に長さ
                mean.append([cx, cy, s_k, s_k])

                # アスペクト比(1,1)の大きいdbox(cx, cy, width, height)
                s_k_prime = np.sqrt(s_k * (self.max_sizes[n] / self.image_size))
                mean.append([cx, cy, s_k_prime, s_k_prime])

                # その他アスペクト比のDboxたち
                for ar in self.aspect_ratio[n]:  # ex)1,2,3,1/2,1/3
                    mean.append(
                        [cx, cy, s_k * np.sqrt(ar), s_k / np.sqrt(ar)]
                    )  # ボックスの幅および高さを逆にする
                    mean.append([cx, cy, s_k / np.sqrt(ar), s_k * np.sqrt(ar)])

        output = torch.Tensor(mean).view(-1, 4)  # view：第2引数として設定した値x ⇒ (〇行, x列)に自動変換
        output = torch.clamp(output, max=1, min=0)  # dboxが画像からはみ出さないようにmin maxでのクリップ
        return output

class SSD(nn.Module):
    def __init__(self, phase, cfg):
        super(SSD, self).__init__()
        self.phase = phase
        self.num_classes = cfg["num_classes"]  # クラス数：21

        # SSDのネットワークを作成する
        self.vgg = generate_vgg()  # vggモジュールを作成
        self.extra = generate_extra()  # extraモジュールを作成
        self.L2Norm = L2Norm()  # L2Normクラス
        self.loc, self.conf = generate_loc_and_conf(
            num_classes=cfg["num_classes"],
            bbox_aspect_num=cfg["bbox_aspect_num"],
        )

        # dboxの作成
        dbox = DefaultBox(cfg)
        self.dbox_list = dbox.generate_dbox_list()

        # 推論時には"Detect"クラスを使用
        if self.phase == "interface":
            self.detect = Detect()

    def forward(self, x):
        sources = list()  # loc, confへ入力するsource1~6を格納する
        loc = list()  # locの出力を格納する
        conf = list()  # confの出力を格納する

        for i in range(23):  # vggの23層目まで実行し、source1を出力する
            x = vgg[i](x)

        # 23層目の出力をL2Normに入れ、source1とする
        source1 = self.L2Norm(x)
        sources.append(source1)

        # vggを最後まで計算させ、それをsource2として格納
        for i in range(23, len(self.vgg)):
            x = vgg[i](x)
        sources.append(x)

        # extraモジュールの計算を行いsource3~6を出力
        for n, v in enumerate(self.extra):
            x = nn.ReLU(v(x), inplace=True)  # 畳み込み後自前で活性化関数に入れ込む
            if n % 2 == 1:
                sources.append(x)

        # source1~6のそれぞれに対応する畳み込み層をloc, confモジュールから引っ張る
        for (x, l, c) in zip(sources, self.loc, self.conf):
            loc.append(
                l(x).permute(0, 2, 3, 1).contiguous()
            )  # contiguous(): メモリ上で要素配列を整える
            conf.append(c(x).permute(0, 2, 3, 1).contiguous())

        # locとconfの型を以下に変更
        # loc：Tensor([batch_num], 8732*(アスペクト比の種類数))
        # conf：Tensor([batch_num], 8732*(class数))
        loc = torch.cat([o.view(o.size(0), -1) for o in loc], 1)
        conf = torch.cat([o.view(o.size(0), -1) for o in conf], 1)

        # locとconfの型を更に整える
        # loc：Tensor([batch_num], 8732, (アスペクト比の種類数))
        # conf：Tensor([batch_num], 8732, (class数))
        loc = loc.view(loc.size(0), -1, 4)
        conf = conf.view(conf.size(0), -1, self.num_classes)

        # 最後に出力する
        output = (loc, conf, self.dbox_list)

        # 学習時はそのままoutput
        if self.phase == "train":
            return output
        else:
            # 推論時は推論結果をoutput
            return self.detect(output[0], output[1], output[2])

class Detect(torch.autograd.Function):
    """
    SSDの推論時にconfとlocの出力から、被りを除去したBboxを返す
    torch.autograd.Functionを継承し、クラス内でforward, backword関数を定義することで独自のautograd演算子を定義することができる
    """

    def __init__(self, conf_threshold=0.01, top_k=200, nms_threshold=0.45):
        self.softmax = nn.Softmax(dim=-1)  # confをsoftmax関数で正規化する
        self.conf_threshold = conf_threshold  # confが0.01以上のDboxのみを扱う
        self.top_k = top_k  # non_maxmum_supressionでconfが高い上位〇個を計算に使用する
        self.nms_threshold = nms_threshold  # non_maxmum_supression内でIOUが〇より大きいBboxは削除

    def forward(self, loc, conf, dbox_list):
        """
        順伝播の計算を実行する
        ・input
            ⇒ loc：オフセット情報
            ⇒ conf：検出の信頼度
            ⇒ dbox_list：Dboxのリスト[8732,4]
        ・outtput
            ⇒ torch.Size([batch_num, 21, confのtop_k個分, BBox情報])
        """

        # 各サイズを取得
        num_batch = loc.size(0)  # バッチサイズ
        num_dbox = loc.size(1)  # Dboxの数(8732個)
        num_classes = loc.size(2)  # クラス数

        # confはsoftmaxを用いて正規化する
        conf = self.softmax(conf)

        # 出力の型を作成する[batch_num, 21, confのtop_k個分, BBox情報]
        output = torch.zero(num_batch, num_classes, self.top_k, 5)

        # confの列を並び替える
        conf_pred = conf.transpose(2, 1)

        # ミニバッチごとのループ処理
        for i in range(num_batch):
            # decodeを用いてlocとDbox情報からBboxを求める
            decoded_boxes = decode(loc[i], dbox_list)

            # confのコピーを作成
            conf_score = conf_pred[i].clone()

            # クラスごとのループ処理(背景クラス(idx0)は処理しないため1から)
            for cl in range(1, num_classes):
                # conf_score:[21, 8732] ⇒ 各クラスのscoreをDbox毎に持っている
                cs = conf_score[cl]
                c_mask = cs.gt(
                    self.conf_threshold
                )  # 信頼度が〇以上のものは1, それ以外は0となる(gt：greater_than)
                scores = cs[c_mask]  # 閾値を超えたBboxの個数となる

                if scores.nelement() == 0:  # nelement:要素の個数を算出する ⇒ つまりscoreが[]の場合は何もしない
                    continue

                # c_mask([8732])をdecoded_scoreにも適応できるようにリサイズ
                l_mask = c_mask.unsqueeze(1).expand_as(
                    decoded_boxes
                )  # tensor([8732, 4])

                # decoded_boxes[l_mask]で1次元になってしまうため、viewでtensor([閾値以上のBbox数, 4])とする
                boxes = decoded_boxes[l_mask].view(-1, 4)

                # non_maximum_supressionを実施し、被っているBboxは除去する
                # ids：confの降順にnon_maximum_supressionを通過したBboxのindexリスト
                # count：non_maximum_supressionを通過したBboxの数
                ids, count = non_maximun_supression(
                    boxes, scores, self.nms_threshold, self.top_k
                )

                # outputにBbox結果を格納する
                out[i, cl, :count] = torch.cat(
                    (scores[ids[:count]].unsqueeze(1), boxes[ids[:count]]), 1
                )
        return output  # tensor([1, 21, 200, 5]) ⇒ 1枚毎のBbox情報(non_maximum_supression済)

class MultiBoxLoss(nn.Module):
    """
    ssdの損失関数を算出するクラス
    """

    def __init__(self, jaccard_threshold=0.5, neg_pos=3, device="cpu"):
        super(MultiBoxLoss, self).__init__()
        self.jaccard_threshold = jaccard_threshold  # jaccard係数の閾値
        self.neg_pos = neg_pos  # Hard Negative Mining の負と正の比率(ex 3 ⇒ neg:pos=3:1)
        self.device = device  # cpu or gpu

    def forward(self, predictions, targets):
        """
        損失関数の計算
        ・input
            ⇒ predictions：ssdの訓練時の出力(output：(loc,conf,dbox_list))
            ⇒ target：正解データ([num_batch, num_objects, 5]) ⇒ 5は(xmin,ymin,xmax.ymax,label_index)
        ・output
            ⇒ loss_l：locの損失関数
            ⇒ loss_c：confの損失関数
        """

        # ssdモデルの出力をばらす
        loc, conf, dbox_list = predictions

        # 要素数を把握
        num_batch = loc.size(0)
        num_dbox = loc.size(1)
        num_classes = conf.size(2)

        # 損失計算に使用するものを格納する変数を作成
        # conf_t_label：各Dboxに近い、正解のBboxのラベルを格納する
        # loc_t：各Dboxに近い、正解のBboxの位置を格納する
        conf_t_label = torch.LongTensor(num_batch, num_dbox).to(
            self.device
        )  # LongTensor: 64bitの符号付き整数
        loc_t = torch.Tensor(num_batch, num_dbox, 4).to(self.device)

        for idx in range(num_batch):
            ...


"""
DAG utility class
"""
from datetime import (datetime, timedelta,)

from helpers.configs import (
    DAG_EMAIL, DAG_EMAIL_ON_FAILURE, DAG_EMAIL_ON_RETRY,
    DAG_NO_OF_RETRIES, DAG_OWNER, DAG_RETRY_DELAY_IN_MINS,
)

class DagUtility:
    def __init__(self):
        pass

    @staticmethod
    def get_dag_default_args():
        return {
            'owner': DAG_OWNER,
            'depends_on_past': False,
            'start_date': datetime(2019, 10, 21),
            'email': [DAG_EMAIL],
            'email_on_failure': DAG_EMAIL_ON_FAILURE,
            'email_on_retry': DAG_EMAIL_ON_RETRY,
            'catchup_by_default': False,
            'retries': DAG_NO_OF_RETRIES,
            'retry_delay': timedelta(minutes=DAG_RETRY_DELAY_IN_MINS),
        }


import numpy as np
import random as r

def split(sampleBalls, splitSize):
    for i in range(0, len(sampleBalls), splitSize):
        yield sampleBalls[i:i + splitSize]

# here the logic is always to work with three groups to find the heaviest
# two will be weighed and one will be left out
# if the heaviest group is on the scale, one of the plates will come down
# if the heaviest group is off the scale, the two plates will be on the same level
# in both cases we know which group is heavier
def weighing(sampleBalls0, sampleBalls1):
    if np.sum(sampleBalls0) > np.sum(sampleBalls1):
        return 0
    elif np.sum(sampleBalls1) > np.sum(sampleBalls0):
        return 1
    else:
        return 2

# each 1 represents one of the 8 balls of the same weight
# the 5 represents the heaviest ball
# the goal is that the weighing always returns the 5, regardless of its position (that's why we use shuffle)
balls = [1, 1, 1, 1, 1, 1, 1, 1, 5]
r.shuffle(balls)

# part 1: find the heaviest trio
# divide the 9 balls into 3 groups of 3
firstThreeGroups = list(split(balls, 3))

# first weighing
res = weighing(firstThreeGroups[0], firstThreeGroups[1])

# part 2: find the heaviest ball from the heaviest trio
# divide the heaviest trio into 3 groups of 1
secondThreeGroups = list(split(firstThreeGroups[res], 1))
print("Heaviest trio: ", secondThreeGroups)

# second weighing
res = weighing(secondThreeGroups[0], secondThreeGroups[1])
print("Heaviest ball: ", secondThreeGroups[res])


from machine import I2C, Pin, RTC
from utime import sleep_ms, time, mktime
import ujson
import display

import ds3231
import pulseclock_new
import settings
import wifi

def clock_test():
    # Initialise the DS3231 battery-backed RTC
    i2c = I2C(0, scl=22, sda=21)
    ds  = ds3231.DS3231(i2c)
    print("DS3231 time   : {}".format(ds.rtc_tm))
    print("Hands position: {}".format(ds.alarm1_tm))

    # Initialise the hand position using the value stored in NV-RAM in the DS chip
    hand_position = ds.alarm1_tm
    hands         = hand_position[3] * 3600 + hand_position[4] * 60 + hand_position[5]  % 43200

    # Initialise the pulse clock itself - pulses of 200/100 seem reliable
    clock_settings = settings.load_settings("clock.json")
    pc = pulseclock_new.PulseClock(clock_settings, 0)

    try:
        while True:
            pc.step()
            sleep_ms(500)

    except KeyboardInterrupt:
        # Try to relinquish the I2C bus
        print("Hand position: {}".format(ds.alarm1_tm))
        i2c.deinit()
        tft.deinit()
        # del i2c

if __name__ == "__main__":
    dgclock()


import hashlib
from datetime import datetime
from flask import Flask
from flask import request
from copy import deepcopy
import json
import requests
node = Flask(__name__)

class Block:
    def __init__(self, index, timestamp, data, previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.hash_block()

    def hash_block(self):
        sha = hashlib.sha256()
        input_str = str(self.index) +\
                    str(self.timestamp) +\
                    str(self.data) +\
                    str(self.previous_hash)
        input_bytes = bytes(input_str, 'utf-8')
        sha.update(input_bytes)
        return sha.hexdigest()

def create_genesis_block():
    # Manually construct a block with
    # index zero and arbitrary previous hash
    return Block(0, datetime.now(),
                 {"proof-of-work": 9,
                  "transactions": None},
                 "0")

# A completely random address of the owner of this node
miner_address = "q3nf394hjg-random-miner-address-34nf3i4nflkn3oi"

blockchain = []
blockchain.append(create_genesis_block())
this_nodes_transactions = []
peer_nodes = []

mining = True

@node.route("/txion", methods=["POST"])
def transactions():
    new_txion = request.get_json()
    this_nodes_transactions.append(new_txion)
    print("New transaction")
    print("FROM: {}".format(new_txion["from"]))
    print("TO: {}".format(new_txion["to"]))
    print("AMOUNT: {}\n".format(new_txion["amount"]))
    return "Transaction submission successful\n"

@node.route("/blocks", methods=["GET"])
def get_blocks():
    chain_to_send = deepcopy(blockchain)
    for i in range(len(chain_to_send)):
        block = chain_to_send[i]
        block_index = str(block.index)
        block_timestamp = str(block.timestamp)
        block_data = str(block.data)
        block_hash = block.hash
        chain_to_send = {
            "index": block_index,
            "timestamp": block_timestamp,
            "data": block_data,
            "hash": block_hash
        }
    chain_to_send = json.dumps(chain_to_send)
    return chain_to_send

def find_new_chains():
    other_chains = []
    for node_url in peer_nodes:
        block = requests.get(node_url + "/blocks").content
        block = json.loads(block)
        other_chains.append(block)
    return other_chains

def consensus():
    global blockchain
    other_chains = find_new_chains()
    longest_chain = blockchain
    for chain in other_chains:
        if len(longest_chain) < len(chain):
            longest_chain = chain
    blockchain = longest_chain

def proof_of_word(last_proof):
    incrementor = last_proof + 1
    while not (incrementor % 9 == 0 and incrementor % last_proof == 0):
        incrementor += 1
    return incrementor

@node.route('/mine', methods=["Get"])
def mine():
    last_block = blockchain[-1]
    last_proof = last_block.data["proof-of-work"]
    proof = proof_of_word(last_proof)
    this_nodes_transactions.append(
        {"from": "network", "to": miner_address, "amount": 1}
    )

    new_block_data = {
        "proof-of-work": proof,
        "transactions": list(this_nodes_transactions)
    }
    new_block_index = last_block.index + 1
    new_block_timestamp = datetime.now()
    last_block_hash = last_block.hash

    this_nodes_transactions[:] = []

    mined_block = Block(
        new_block_index,
        new_block_timestamp,
        new_block_data,
        last_block_hash
    )

    blockchain.append(mined_block)

    return json.dumps({
        "index": new_block_index,
        "timestamp": str(new_block_timestamp),
        "data": new_block_data,
        "hash": last_block_hash,
    }) + "\n"

if __name__ == "__main__":
    node.run()