import os
import math
import sklearn.metrics
import pickle
import numpy as np
import sklearn.linear_model
import argparse
import scipy
import collections
import datetime
import csv

parser = argparse.ArgumentParser(
    description="Get model performance and simplified pesi score performance",
)
parser.add_argument("--path_to_data", type=str, required=True)
parser.add_argument("--path_to_output", type=str, required=True)
parser.add_argument("--compare_vs_pesi", action=argparse.BooleanOptionalAction)
parser.add_argument("--require_pe_status", type=str, required=False, default=None)
parser.add_argument("--compute_confidence", action=argparse.BooleanOptionalAction)
parser.add_argument("--relative_confidence", action=argparse.BooleanOptionalAction)
parser.add_argument("--metric", type=str, default="auroc")

args = parser.parse_args()

if args.require_pe_status is not None:
    args.require_pe_status = args.require_pe_status.lower() == "true"

tasks = (
    "PE",
    "1_month_mortality",
    "6_month_mortality",
    "12_month_mortality",
    "1_month_readmission",
    "6_month_readmission",
    "12_month_readmission",
    "12_month_PH",
)
ct_names = (
    "pe",
    "mort_1m",
    "mort_6m",
    "mort_12m",
    "read_1m",
    "read_6m",
    "read_12m",
    "ph_12m",
)

ct_name_map = {k: v for k, v in zip(tasks, ct_names)}

if args.require_pe_status != None:
    tasks = tasks[1:]

performances = {}

has_pe = {}
pid_split_assignment = {}
pesi_map = {}

with open(os.path.join(args.path_to_data, "cohort_0.2.0_master_file.csv")) as f:
    reader = csv.DictReader(f)
    for row in reader:
        pid = int(row["patient_id"])
        pid_split_assignment[pid] = row["split"]
        time = datetime.datetime.fromisoformat(row["procedure_time"])
        has_pe[(pid, time)] = row["pe_positive_nlp"] == "True"
        pesi_map[(pid, time)] = float("nan")

with open(f"{args.path_to_output}/simplified_pesi/pesi_scores.pkl", "rb") as f:
    pesi_predictions, pesi_patient_ids, pesi_times = pickle.load(f)

for prediction, patient_id, time in zip(pesi_predictions, pesi_patient_ids, pesi_times):
    pesi_map[(patient_id, time)] = prediction

print("Have pesi scores for ", sum(v == v for k, v in pesi_map.items()), " labels")

from itertools import chain, combinations

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))

def load_data(name, task, split):
    if name == "GBM":
        with open(
            f"{args.path_to_output}/gbm_model_results/{task}/predictions.pkl", "rb"
        ) as f:
            result = pickle.load(f)
    elif name == "MOTOR":
        with open(
            f"{args.path_to_output}/motor_results/{task}/predictions.pkl", "rb"
        ) as f:
            result = pickle.load(f)
    elif name == "sPESI":
        baseline = load_data("GBM", task, split)
        for i, (pid, time) in enumerate(zip(baseline[1], baseline[3])):
            if task == "PE":
                time = time + datetime.timedelta(days=1)
            baseline[0][i] = scipy.special.expit(pesi_map[(pid, time)])
        return baseline
    elif name == "CT":
        ct_name = ct_name_map[task]
        pids = []
        prediction_dates = []
        labels = []
        probs = []

        with open(f"{args.path_to_output}/ct_results/{ct_name}_pred_proba.csv") as f:
            reader = csv.DictReader(f)
            for line in reader:
                pid = int(line["patient_id"])
                prediction_date = datetime.datetime.fromisoformat(
                    line["procedure_time"]
                )

                if task == "PE":
                    prediction_date = prediction_date - datetime.timedelta(days=1)

                label = float(line["label"]) == 1
                prob = float(line["prob"])
                pids.append(pid)
                prediction_dates.append(prediction_date)
                labels.append(label)
                probs.append(prob)

        result = [probs, pids, labels, prediction_dates]

    if not isinstance(result[3][0], datetime.datetime):
        result[3] = result[3].astype(datetime.datetime)

    o = np.lexsort((result[1], result[3]))
    result = [np.array(v)[o] for v in result]
    valid_indices = []

    for i, (pid, time) in enumerate(zip(result[1], result[3])):
        if task == "PE":
            time = time + datetime.timedelta(days=1)
        good = pid_split_assignment[pid] == split
        if args.compare_vs_pesi:
            if pesi_map[(pid, time)] != pesi_map[(pid, time)]:
                good = False

        if args.require_pe_status is not None:
            if has_pe[(pid, time)] != args.require_pe_status:
                good = False

        if good:
            valid_indices.append(i)
    result = [a[valid_indices] for a in result]

    return result

baseline_model = "MOTOR"

baseline_performance = {}

def get_performance(labels, probabilities):
    if args.metric == "auroc":
        return sklearn.metrics.roc_auc_score(labels, probabilities)
    elif args.metric == "auprc":
        return sklearn.metrics.average_precision_score(labels, probabilities)
    elif args.metric == "ece":
        bins = np.linspace(0, 1, num=10)
        start_bin = bins[:-1]
        end_bin = bins[1:]

        total = 0

        for start, end in zip(start_bin, end_bin):
            total_true = 0
            total_prob = 0
            num_in_bin = 0
            for label, prob in zip(labels, probabilities):
                if start <= prob < end:
                    total_true += label
                    total_prob += prob
                    num_in_bin += 1
            if num_in_bin != 0:
                total += num_in_bin * abs(
                    total_true / num_in_bin - total_prob / num_in_bin
                )

        return total / len(labels)
    else:
        assert False, f"{args.metric} is not a valid metric"

if args.compute_confidence:
    for task in tasks:
        predictions, pids, labels, times = load_data(baseline_model, task, "test")

        samples = []
        for _ in range(1000):
            indices = sklearn.utils.resample(list(range(len(predictions))))
            l_s, p_s = labels[indices], predictions[indices]
            performance = get_performance(l_s, p_s)
            samples.append((indices, performance))

        baseline_performance[task] = samples

def compute_score_and_bootstrap(task, labels, predictions):
    total_score = get_performance(labels, predictions)

    if args.compute_confidence:
        samples = []
        for indices, baseline_perf in baseline_performance[task]:
            l_s, p_s = labels[indices], predictions[indices]
            perf = get_performance(l_s, p_s)
            if args.relative_confidence:
                perf -= baseline_perf
            samples.append(perf)
        confidence = np.quantile(samples, (0.025, 0.975))
    else:
        confidence = None

    return total_score, confidence

def get_model_performance(task, name):
    predictions, pids, labels, times = load_data(name, task, "test")

    return compute_score_and_bootstrap(task, labels, predictions)

def get_combined_model_performance(task, names):
    pids = {}
    times = {}
    labels = {}
    probs = {}

    for split in ["valid", "test"]:
        for name in names:
            k = (name, split)
            probs[k], pids[k], labels[k], times[k] = load_data(name, task, split)

            if name != names[0]:
                assert len(pids[(names[0], split)]) == len(
                    pids[(name, split)]
                ), f"{len(pids[(names[0], split)])} {len(pids[(name, split)])}"

                assert np.all(pids[(names[0], split)] == pids[(name, split)])
                assert np.all(times[(names[0], split)] == times[(name, split)])
                assert np.all(labels[(names[0], split)] == labels[(name, split)])

    all_probs = np.stack([probs[(name, "valid")] for name in names], axis=-1)
    features = scipy.special.logit(all_probs)
    features = np.clip(features, -1e10, 1e10)

    model = sklearn.linear_model.LogisticRegression(penalty=None, solver="newton-cg")

    model.fit(features, labels[(names[0], "valid")])

    if np.any(np.isnan(model.coef_)):
        # Could not converge. Fallback to average
        model.coef_ = np.ones_like(model.coef_)
        model.intercept_ = 0

    test_probs = np.stack([probs[(name, "test")] for name in names], axis=-1)
    test_features = scipy.special.logit(test_probs)
    test_features = np.clip(test_features, -1e10, 1e10)

    computed_probs = model.predict_proba(test_features)[:, 1]

    return compute_score_and_bootstrap(task, labels[(names[0], "test")], computed_probs)

# models = ["CT", "MOTOR", "GBM"]
models = ["MOTOR", "GBM"]
if args.compare_vs_pesi:
    models.append("sPESI")

aurocs = collections.defaultdict(dict)
fusion_aurocs = collections.defaultdict(dict)

best_perfs = {}
best_m_perfs = {}

def compare(a, b):
    if args.metric == "ece":
        return a > b
    else:
        return a < b

for task in tasks:
    for model in models:
        aurocs[model][task] = get_model_performance(task, model)
        if not args.compare_vs_pesi:
            if task not in best_perfs or compare(
                best_perfs[task][0][0], aurocs[model][task][0]
            ):
                best_perfs[task] = aurocs[model][task], model
        if task not in best_m_perfs or compare(
            best_m_perfs[task][0][0], aurocs[model][task][0]
        ):
            best_m_perfs[task] = aurocs[model][task], model

    if not args.compare_vs_pesi:
        for names in powerset(models):
            if len(names) > 1:
                fusion_aurocs[names][task] = get_combined_model_performance(task, names)

                if task not in best_m_perfs or compare(
                    best_m_perfs[task][0][0], fusion_aurocs[names][task][0]
                ):
                    best_m_perfs[task] = fusion_aurocs[names][task], names

print(aurocs)

print("MAIN TABLE")

for model, performances in aurocs.items():
    row = " "
    for i, model_column in enumerate(models):
        if i != 0:
            row += " &"
        if model == model_column:
            row += " $\\checkmark$"
    for task in tasks:
        row += "& "
        value = f"{performances[task][0]:0.3f} "
        if task in best_perfs and model == best_perfs[task][1]:
            value = f"\\underline{{" + value + f"}}"
        if task in best_m_perfs and model == best_m_perfs[task][1]:
            value = f"\\textbf{{" + value + f"}}"

        row += value
    row += "\\\\"
    print(row)

if not args.compare_vs_pesi:
    print("\\midrule")

    for eval_models, performances in fusion_aurocs.items():
        row = " "
        for i, model_column in enumerate(models):
            if i != 0:
                row += " &"
            if model_column in eval_models:
                row += "$\\checkmark$"
        for task in tasks:
            row += "& "
            if eval_models == best_m_perfs[task][1]:
                row += f"\\textbf{{" + f"{performances[task][0]:0.3f}" + f"}}"
            else:
                row += f"{performances[task][0]:0.3f} "
        row += "\\\\"
        print(row)

if args.compute_confidence:
    print("CONFIDENCE INTERVAL TABLE")

    for model, performances in aurocs.items():
        row = " "
        for i, model_column in enumerate(models):
            if i != 0:
                row += " &"
            if model == model_column:
                row += " $\\checkmark$"
        for task in tasks:
            row += "& "
            text = f"({performances[task][1][0]:0.2f}, {performances[task][1][1]:0.2f})"
            if args.relative_confidence and (
                all(v < 0 for v in performances[task][1])
                or all(v > 0 for v in performances[task][1])
            ):
                text = f"\\textbf{{" + text + f"}}"
            row += text
        row += "\\\\"
        print(row)

    print("\\midrule")

    for eval_models, performances in fusion_aurocs.items():
        row = " "
        for i, model_column in enumerate(models):
            if i != 0:
                row += " &"
            if model_column in eval_models:
                row += "$\\checkmark$"
        for task in tasks:
            row += "& "
            text = f"({performances[task][1][0]:0.2f}, {performances[task][1][1]:0.2f})"
            if args.relative_confidence and (
                all(v < 0 for v in performances[task][1])
                or all(v > 0 for v in performances[task][1])
            ):
                text = f"\\textbf{{" + text + f"}}"
            row += text
        row += "\\\\"
        print(row)


n = int(input('number of rows: '))
m = int(input('number of columns: '))
matrix = []
for i in range(n):
    row = input('input row: ')
    matrix.append(row)
print(matrix)
amount = 0
for i in range (0,m):
    if i % m == 1:
        el = matrix[i]
        rightel = matrix[(i + 1)-m]
        lowel = matrix[(i + m)-n]
        upel = matrix[(i + n)-m]
        if rightel == '1':
            if lowel == '1':
                if upel == '1':
                    el = '2'
print(matrix)
    #elif i % m == 0:
      #  el = matrix[i]
       # rightel = matrix[i + m]
       # lowel = 0
    #if t==1 and k==1:
     #   amount = amount+1
       # if rightel == '1' and lowel == '1':
           # amount += 1
    #elif t==1 and k != 1:
     #   amount =


"""
    compile active_cmssw:
        Compiles the CMSSW version which is linked under workspace/current

    Usage:
        compile [ncpu=1]

    Parameters:
        npcu: number of CPUs to be used for compilation. Default: 1
"""
import logging
import hepshell
import os
from hepshell.interpreter import time_function

LOG = logging.getLogger(__name__)
from l1t_cli.setup import ACTIVE_CMSSW
CMSSW_SRC = os.path.join(ACTIVE_CMSSW, 'src')

class Command(hepshell.Command):

    DEFAULTS = {
        'ncpu': 1,
    }

    def __init__(self, path=__file__, doc=__doc__):
        super(Command, self).__init__(path, doc)

    @time_function('compile', LOG)
    def run(self, args, variables):
        if not self.__can_run():
            return False
        self.__prepare(args, variables)
        n_jobs = int(self.__variables['ncpu'])
        commands = [
            'cd {CMSSW_SRC}',
            'source /cvmfs/cms.cern.ch/cmsset_default.sh',
            'eval `/cvmfs/cms.cern.ch/common/scram runtime -sh`',
            'scram b -j{n_jobs}'
        ]

        all_in_one = ' && '.join(commands)
        all_in_one = all_in_one.format(CMSSW_SRC=CMSSW_SRC, n_jobs=n_jobs)

        from hepshell.interpreter import call
        call(all_in_one, logger=LOG, shell=True)

        return True

    def __can_run(self):
        if not os.path.exists(CMSSW_SRC):
            LOG.error('CMSSW is not set up: {0}'.format(CMSSW_SRC))
            return False

        return True


class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        if not digits: return []
        mapp = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
                '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
        def dfs(idx, path, res):
            if idx == len(digits):
                res.append(path)
                return
            cur_letters = mapp[str(digits[idx])]
            for letter in cur_letters:
                path += letter
                dfs(idx+1, path, res)
                path = path[:-1]

        cur_idx = 0
        res = []
        path = ''
        dfs(cur_idx, path, res)
        return res

2-回溯、DFS、BFS/组合、排列、子集、分割/Leetcode17. 电话号码的字母组合（17）.py


def find_set(st):  # 대표원소 찾기
    if student[st] != st:  # 내가 나의 대표가 아닐 경우
        student[st] = find_set(student[st])

    return student[st]

def union(leader, member):  # 두 학생을 한 조로 만들기 = leader 통일하기
    student[find_set(member)] = find_set(leader)

T = int(input())
for tc in range(1, T+1):
    N, M = map(int, input().split())  # 학생수, 신청서수
    student = [i for i in range(N+1)]  # 학생 목록
    forms = list(map(int, input().split()))
    for i in range(M):  # 신청서를 순회하며 각 학생을 한 조로 만든다
        st1, st2 = forms[2*i], forms[2*i+1]
        union(st1, st2)

    # 아직 리더가 갱신되지 않은 학생이 있을 것이기 때문에 전체 순회하며 리더 갱신
    for i in range(1, N+1):
        if student[i] != i:
            student[i] = find_set(student[i])

    print('#{} {}'.format(tc, len(set(student))-1))


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 26 20:49:44 2020

@author: Home
"""

from collections import Counter
import numpy as np
import pandas as pd
import nltk
import re
from nltk.corpus import stopwords
import fuzzywuzzy
from fuzzywuzzy import fuzz
from  nltk import word_tokenize
import torch.optim as optim

def isEnglish(s):
    try:
        s.encode(encoding='utf-8').decode('ascii')
    except UnicodeDecodeError:
        return False
    else:
        return True
def remove_emoji(string):
    emoji_pattern = re.compile("["
                           u"\U0001F600-\U0001F64F"  # emoticons
                           u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                           u"\U0001F680-\U0001F6FF"  # transport & map symbols
                           u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                           u"\U00002702-\U000027B0"
                           u"\U000024C2-\U0001F251"
                           "]+", flags=re.UNICODE)
    return emoji_pattern.sub(r'', string)

f = "english/agr_en_train.csv"

# preprocessing english tweets.
#ingesting english csv file
df = pd.read_csv(f,names = ['source','comment','annotation'],encoding='UTF-8')
df['comment'] = df.comment.str.strip()   # removing spaces

comments = np.asarray(df['comment'])    # dividing the dataframe into comments and tags and converting to array
tags = np.asarray(df['annotation'])
print((len(comments)))
print(len(tags))

stop_words = set(stopwords.words('english'))  #english stop words list
processed_tokens = []
for comment in comments:
#    comment = "Also see ....hw ur RSS activist caught in Burkha .... throwing beef in d holy temples...https://www.google.co.in/amp/www.india.com/news/india/burkha-clad-rss-activist-caught-throwing-beef-at-temple-pictures-go-viral-on-facebook-593154/amp/,NAGfacebook_corpus_msr_403402,On the death of 2 jawans in LOC CROSS FIRING"
    comment = comment.lower()   #lower casing each tweets
    Digit_REMOVAL = re.sub(r'[0-9]+', '',comment) #removal of numbers
    URL_REMOVAL = re.sub(r"http\S+", "", Digit_REMOVAL) # removal of URLS
    tokenizer = nltk.RegexpTokenizer(r"\w+")   # removal of punctuation and tokenizing
    new_words = tokenizer.tokenize(URL_REMOVAL)
    sentence = []
    for word in new_words:
        if word not in stop_words:           #checking for stop words on each sentence
            sentence.append(word)
    processed_tokens.append(sentence)

#-----------------For hinglish dataset

Hindi_text  = "hindi/agr_hi_dev.csv"
df1 = pd.read_csv(Hindi_text,names = ['source','comment','annotation'],encoding='UTF-8')
df1['comment'] = df1.comment.str.strip()   # removing spaces
hindi_comments = np.asarray(df1['comment'])    # dividing the dataframe into comments and tags and converting to array
hindi_tags = np.asarray(df1['annotation'])
print((hindi_comments[1]))
processed_Hindi_tokens = []
for comment in hindi_comments:
#    comment = "Also see ....hw ur RSS activist caught in Burkha .... throwing beef in d holy temples...https://www.google.co.in/amp/www.india.com/news/india/burkha-clad-rss-activist-caught-throwing-beef-at-temple-pictures-go-viral-on-facebook-593154/amp/,NAGfacebook_corpus_msr_403402,On the death of 2 jawans in LOC CROSS FIRING"
    comment = comment.lower()   #lower casing each tweets
    Digit_REMOVAL = re.sub(r'[0-9]+', '',comment) #removal of numbers
    URL_REMOVAL = re.sub(r"http\S+", "", Digit_REMOVAL) # removal of URLS
    Emoji_removal = remove_emoji(URL_REMOVAL)
    if (isEnglish(Emoji_removal) == True):
        Emoji_removal = re.sub(r'[^\w\s]','',Emoji_removal)# removal of punctuation and tokenizing
    processed_Hindi_tokens.append(word_tokenize(Emoji_removal))
processed_Hindi_tokens[0]
processed_Hindi_tokens[11]
processed_Hindi_tokens[6]
processed_Hindi_tokens[11]

#-----------Transliteration and translation
transliteration_dict = "B:\CS 695\Assignment3\Classification-of-Offensive-Tweets-in-Hinglish-Language/transliterations.hi-en.csv"
t_dict = pd.read_csv(transliteration_dict,names = ['Hinglish','Hindi'],encoding='UTF-8',sep='\t')
t_dict['Hinglish'] = t_dict['Hinglish'].str.strip()
t_dict['Hindi'] = t_dict['Hindi'].str.strip()
t_dict = np.asarray(t_dict)

#--------------profanity dictionary
profanity_dict = "ProfanityText.txt"
P_dict = pd.read_csv(profanity_dict,names = ['Hinglish','English'],encoding='UTF-8',sep='\t')
P_dict['Hinglish'] = P_dict['Hinglish'].str.strip()
P_dict['English'] = P_dict['English'].str.strip()
P_dict = np.asarray(P_dict)

print(t_dict)
processed_Hindi_tokens[4]
for i in range(0,len(processed_Hindi_tokens)):
    print(i)
    for j in range (0,len(processed_Hindi_tokens[i])):
        flag = 0
        Str1 = (processed_Hindi_tokens[i][j])
        max_ratio = 60
        max_ratio_P = 75   #needs to be adjusted
        if (Str1 in EH_dict): # check whether the values exists in english dictionary or not.
            continue;
        for l in range(0,len(P_dict)):
            Str2 = P_dict[l][0]
            Ratiostr1 = fuzz.ratio(Str1,Str2)
            if (Ratiostr1 >= max_ratio_P):
                print(Ratiostr1)
                max_ratio_P = Ratiostr1
                processed_Hindi_tokens[i][j] = P_dict[l][1]
                flag = 1
                print(flag)
                break;
        for p in EH_dict_F:
            RAtionstr1 = fuzz.ratio(Str1,str(p))
            if(Ratiostr1 >= 98):
                flag = 1
                break;
        if (flag == 1):
            continue;

        else:
            for k in range(0,len(t_dict)):
                Str2 = t_dict[k][0]
                Ratiostr1 = fuzz.ratio(Str1,Str2)
                if (Ratiostr1 > max_ratio):
                    max_ratio = Ratiostr1
                    processed_Hindi_tokens[i][j] = t_dict[k][1]
processed_Hindi_tokens[0]
processed_Hindi_tokens[1]
(processed_tokens[12])

#------------------Translation of hindi text back to english-------

Hindi_dict = "Hindi_English_Dict.csv"
H_dict = pd.read_csv(Hindi_dict,names = ['Hindi','English'],encoding='UTF-8')

HE_dict_F = "HE_dictionary_functions.csv"
H_dict_F = pd.read_csv(HE_dict_F,names = ['Hindi','English'],encoding='UTF-8')
H_dict_F['Hindi'] = H_dict_F['Hindi'].str.strip()
H_dict_F['English'] = H_dict_F['English'].str.strip()
H_hindi_F = np.asarray(H_dict_F['Hindi'])
H_english_F = np.asarray(H_dict_F['English'])

H_dict['Hindi'] = H_dict['Hindi'].str.strip()
H_dict['English'] = H_dict['English'].str.strip()
H_hindi = np.asarray(H_dict['Hindi'])
H_english = np.asarray(H_dict['English'])

HE_dict = dict(zip(H_hindi,H_english))
H_dict_F = dict(zip(H_hindi_F,H_english_F))

EH_dict = {v:k for k, v in HE_dict.items()}
EH_dict_F = {v:k for k, v in H_dict_F.items()}

for i in range(0,len(processed_Hindi_tokens)):
    print(i)
    for j in range (0,len(processed_Hindi_tokens[i])):
        Str = processed_Hindi_tokens[i][j]
        if(Str in HE_dict):
            processed_Hindi_tokens[i][j] = HE_dict[Str]
        elif(Str in H_dict_F):
            processed_Hindi_tokens[i][j] = H_dict_F[Str]

#------------------------------MODEL

import torch
from torch import nn
from torch.autograd import Variable
import torch.nn.functional as F

class MIMCT(nn.Module):
    def __init__(self,input_channel,output_channel,embedding_dim,hidden_dim,kernel_size,feature_linear):
        super(MIMCT, self).__init__()
        self.CNN_Layers = nn.Sequential(
            nn.Conv1d(input_channel, output_channel,kernel_size[0], stride=1),
            nn.Conv1d(input_channel, output_channel, kernel_size[1], stride=1),
            nn.Conv1d(input_channel, output_channel, kernel_size[2], stride=1),
            nn.Flatten(),nn.Dropout(p=0.25),
            nn.Linear(feature_linear, 3),
            nn.Softmax()
            )
        #create a sequential for LSTM.
        self.LSTM_Layers = nn.Sequential(
            nn.LSTM(embedding_dim, hidden_dim, output_channel,dropout),
            nn.Linear(hidden_dim, 3),
            nn.Dropout(p=0.20),
            nn.Softmax()
        )
    def forward(self,x):
        x = self.CNN_Layers(x)
      #  y = self.LSTM_Layers(x)
        #concat the outputs the compile layer with categorical cross-entropy the loss function,
       # print(y)
        return x

relu = nn.ReLU()

#try with output channel 1 as well.
batch_size = 1
input_channel = 16
embedding_dim = 200
output_channel = 16
kernel_size = [20,15,10]
Feature_layer1 = embedding_dim - kernel_size[0] + 1
Feature_layer2 = Feature_layer1 - kernel_size[1] + 1
Feature_layer3 = Feature_layer2 - kernel_size[2] + 1
feature_linear = Feature_layer3 * input_channel

#Parameters for LSTM
hidden_dim = 128
dropout = 0.25,
#recurrent_dropout = 0.3

model = MIMCT(input_channel,output_channel,embedding_dim,hidden_dim,kernel_size,feature_linear)

#Adam Optimizer
optimizer = optim.Adam(model.parameters(), lr=0.01)
input1 = torch.randn(batch_size,input_channel,embedding_dim)
output = model(input1)
#create the loss cretirion and training loops

'''
m = nn.Conv1d(1, 2,1,stride=2)
input1 = torch.randn(10)
cnn1d_2 = nn.Conv1d(in_channels=1, out_channels=1, kernel_size=3, stride=2)
input1 = input1.unsqueeze(0).unsqueeze(0)
input1.shape
cnn1d_2(input1)

#------------CNN_Class----------------#
kernel_size = [4,3,2]
embedding_dim = 10
Feature_layer1 = embedding_dim - kernel_size[0] + 1
Feature_layer2 = Feature_layer1 - kernel_size[1] + 1
Feature_layer3 = Feature_layer2 - kernel_size[2] + 1

Layer1 = nn.Conv1d(16, 16, 4, stride=1)
Layer2 = nn.Conv1d(16, 16, 3, stride=1)
Layer3 = nn.Conv1d(16, 16, 2, stride=1)
Dropout_layer = nn.Dropout(p=0.25)
dense_layer = nn.Linear(Feature_layer3, 3)
nn.Flatten

#------------Forward------------------#
batch_size = 1
input_channel = 16
embedding_dim = 10
input1 = torch.randn(batch_size,input_channel,embedding_dim)
output = Layer1(input1)
output1 = Layer2(output)
output2 = Layer3(output1)
output3 = Dropout_layer(output2)
output4 = dense_layer(output3)
nn.flatten

x = torch.tensor([[1.0, -1.0],[0.0,  1.0],[0.0,  0.0]])

in_features = x.shape[1]  # = 2
out_features = 2

m = nn.Linear(in_features, out_features)
m.weight

class MIMCT(nn.Module):
    def __init__(self,input_channel,output_channel,embedding_dim,kernel_size,feature_linear):
        super(MIMCT, self).__init__()
        self.CNN_Layers = nn.Sequential( nn.Conv1d(input_channel, output_channel,kernel_size[0], stride=1),nn.Conv1d(input_channel, output_channel, kernel_size[1], stride=1),nn.Conv1d(input_channel, output_channel, kernel_size[2], stride=1),nn.Flatten(),nn.Dropout(p=0.25),nn.Linear(feature_linear, 3),nn.Softmax())

    def forward(self,x):
        x = self.CNN_Layers(x)
        return x

#try with output channel 1 as well.
batch_size = 1
input_channel = 16
embedding_dim = 200
output_channel = 16
kernel_size = [20,15,10]
embedding_dim = 200
Feature_layer1 = embedding_dim - kernel_size[0] + 1
Feature_layer2 = Feature_layer1 - kernel_size[1] + 1
Feature_layer3 = Feature_layer2 - kernel_size[2] + 1
feature_linear = Feature_layer3 * input_channel
model = MIMCT(input_channel,output_channel,embedding_dim,kernel_size,feature_linear)

output = model(input1)
'''

''''
kernel_size = [20,15,10]
embedding_dim = 200
Feature_layer1 = embedding_dim - kernel_size[0] + 1
Feature_layer2 = Feature_layer1 - kernel_size[1] + 1
Feature_layer3 = Feature_layer2 - kernel_size[2] + 1
feature_linear = Feature_layer3 * input_channel
m = nn.Sequential( nn.Conv1d(input_channel,16,kernel_size[0], stride=1),nn.Conv1d(input_channel, output_channel, kernel_size[1], stride=1),nn.Conv1d(input_channel, output_channel, kernel_size[2], stride=1),nn.Flatten(),nn.Dropout(p=0.25),nn.Linear(feature_linear, 3),nn.Softmax())

#------------Forward------------------#
batch_size = 1
input_channel = 16
embedding_dim = 200
input1 = torch.randn(batch_size,input_channel,embedding_dim)
test_input = input1
output = m(test_input)
nn.flatten
'''


# program to print duplicate numbers in a given orbit
# provided input
orbit = [3,2,1,2,2]

dup=[]
for a in orbit:
        d=orbit.count(a)
        if d > 1:
                if dup.count(a) == 0:
                    dup.append(a)
yy=len(dup)
print(len(orbit))
print(yy)
# This code is contributed by Himanshu Khune


from app.vendors import GraphQL, load_schema_from_path, make_executable_schema, MutationType, QueryType
from fastapi.middleware.cors import CORSMiddleware
from .db_main import resolve_custom_method, resolve_generic_query, resolve_generic_update, resolve_query_clients, resolve_update_client, resolve_update_user

type_defs = load_schema_from_path('app/db')
query = QueryType()
mutation = MutationType()

@mutation.field('customMethod')
async def custom_method(_, info, value=''):
    return (resolve_custom_method(info, value))

@query.field('genericQuery')
async def generic_query(_, info, value=''):
    return (resolve_generic_query(info, value))

@mutation.field('genericUpdate')
async def generic_update(_, info, value=''):
    return (resolve_generic_update(info, value))

@query.field('queryClients')
async def query_client(_, info, value=''):
    return (resolve_query_clients(info, value))

@mutation.field('updateClient')
async def update_client(_, info, value=''):
    return (resolve_update_client(info, value))

@mutation.field('updateUser')
async def update_user(_, info, value):
    return (resolve_update_user(info, value))

schema = make_executable_schema(type_defs, query, mutation)

GraphQLApp: GraphQL = CORSMiddleware(
    GraphQL(schema), allow_origins=['http://localhost:3000', 'http://127.0.0.1:3000'], allow_methods=['*'], allow_headers=['*'], allow_credentials=True
)


# random test/experimental code that william unaccountably wanted to
# check into git

import math
import numpy as np
import scipy.sparse as sp

import tensorflow as tf

from tensorlog import matrixdb,mutil

def go():
  db = matrixdb.MatrixDB.loadFile("test-data/textcattoy_corpus.cfacts")
  m = db.matEncoding[('hasWord',2)]
  print('m',m.shape,m.nnz)
  def tfidf_transform(tf_matrix):
    # implements Salton's TFIDF transformation, ie l2-normalized
    # vector after scaling by: log(tf+1.0) * log(#docs/idf)
    df = tf_matrix.sum(axis=0)
    # count docs by summing word counts w/in each row of m,
    # clipping the sum down to 1, and and then adding up
    ndoc = np.clip(tf_matrix.sum(axis=1),0.0,1.0).sum()
    # this ensures idfs will be zero for any terms with df==0, after I
    # take the log
    df[df==0] = ndoc
    idf = np.log( np.reciprocal(df) * ndoc )
    # first compute log(tf+1.0)
    scaled_tf_matrix = mutil.mapData(lambda v:np.log(v+1.0),tf_matrix)
    # now multiply by idf factor
    unnormalized_tfidf_matrix = mutil.broadcastAndComponentwiseMultiply(scaled_tf_matrix, sp.csr_matrix(idf))
    # compute normalizer needed for l2 normalization
    normalizer = mutil.mapData(lambda v:v*v, unnormalized_tfidf_matrix).sum(axis=1)
    normalizer = np.sqrt(normalizer)
    # finally, multiply unnormalized_tfidf_matrix and normalizer,
    # which is complicated since they are sparse, scipy.sparse doesn't
    # support broadcasting, and
    # mutil.broadcastAndComponentwiseMultiply can only broadcast along
    # rows. so we need to transpose everything before and after, and
    # convert into csr matrices
    tmp1 = sp.csr_matrix(unnormalized_tfidf_matrix.transpose())
    tmp2 = sp.csr_matrix(normalizer.transpose())
    tmp2.eliminate_zeros()
    tmp3 = mutil.mapData(np.reciprocal,tmp2)
    tmp4 = mutil.broadcastAndComponentwiseMultiply(tmp1,tmp3)
    result = sp.csr_matrix(tmp4.transpose())
    return result
  m = tfidf_transform(m)

def go1():
  dense = tf.Variable([[0,0,10,1,0,0],[0,0,-2,3,0,0]], dtype=tf.float32)
  sm1 = tf.nn.softmax(dense)

  denseReplacing0WithNeg10 = tf.where(
      dense>0.0,
      dense,
      tf.ones(tf.shape(dense), tf.float32)*(-10.0))
  sm2 = tf.nn.softmax(denseReplacing0WithNeg10)

  nz_indices = tf.where(tf.not_equal(dense, tf.constant(0, dtype=tf.float32)))
  nz_values = tf.gather_nd(dense,nz_indices)
  sparse = tf.SparseTensor(nz_indices, nz_values, dense.get_shape())
  sm3 = tf.sparse_softmax(sparse)
  dm3a = tf.sparse_to_dense(sm3.indices,sm3.get_shape(),sm3.values)
  dm3b = tf.scatter_nd(sm3.indices,sm3.values,dense.get_shape())

  session = tf.Session()
  session.run(tf.global_variables_initializer())
  from tensorflow.python.framework import ops
  for v in nz_indices,nz_values,sparse,sm3,dm3a,dm3b:
    print('gradient of op',v,ops.get_gradient_function(v.op))

  print('dense sm - direct',session.run(sm1))
  print('dense sm - with -10 trick',session.run(sm2))
  print('sparse sm',session.run(sm3))
  print('densified sparse sm - old',session.run(dm3a))
  print('densified sparse sm - new',session.run(dm3a))

if __name__ == "__main__":
  print('trying')
  go()


from multiprocessing import Process, Pipe

def revr(conn, x):
  conn.send(x[::-1])

if __name__ == "__main__":
  print("Enter a word : ")
  a= input()
  parent, child = Pipe()
  p= Process(target=revr, args=(child,a))
  p.start()
  p.join()

  print("Reverse of Word : ")
  print(parent.recv())


import torch
import sys
sys.path.append('../MatSciBERT')
from normalize_text import normalize
from transformers import AutoModel, AutoTokenizer
from sklearn.metrics.pairwise import cosine_similarity
import pickle
import pandas as pd
import re
from tqdm import tqdm
import os
import numpy as np

def sort_composition(compositions):

	composition_name_new = []

	for name in compositions:
		composition = name
		composition_split = re.findall('[A-Z][^A-Z]*', composition)
		composition_split.sort()

		for i, element in enumerate(composition_split):
			if not element[-1].isnumeric():
				composition_split[i] = composition_split[i] + '1'
		composition_sorted = "".join(composition_split)
		composition_name_new.append(composition_sorted)

	return composition_name_new

def make_robodata(save_dir, robo_data = None):

	if robo_data is None:
		robo_dir = 'robo_descriptions'

		file1 = open(robo_dir, "r")
		lines = file1.readlines()
		file1.close()

		lines = [l[:-1] for l in lines]
		names = [l.split(' ')[0] for l in lines]

		robo_data = pd.DataFrame({'composition_name':names,'sentence':lines})

	mat_table = 'JMC_Data_Kappa_Unique_Averaged.csv'
	table = pd.read_csv(mat_table)
	names_new = sort_composition(robo_data['composition_name'].values)
	robo_data['composition_name'] = names_new
	composition_name_new = sort_composition(table['composition_name'].values)

	robo_data_use = robo_data[(np.in1d(names_new, composition_name_new))]

	robo_data_use.to_pickle(save_dir+'/robo_data_kappa.pkl')

	return robo_data_use

def run_matscibert(robo_data):

	save_dir = '../robo_matscibert_embedding'
	model = AutoModel.from_pretrained('m3rg-iitd/matscibert')
	tokenizer = AutoTokenizer.from_pretrained('m3rg-iitd/matscibert')
	#sentences = ['SiO2 is a network former.']
	embeddings = []
	for row in tqdm(robo_data.iterrows()):

		sentence = row[1].sentence
		composition = row[1].composition_name

		norm_sents = [normalize(s) for s in [sentence]]
		tokenized_sents = tokenizer(norm_sents)
		tokenized_sents = {k: torch.Tensor(v).long() for k, v in tokenized_sents.items()}
		token_size = int(tokenized_sents['input_ids'].shape[1])
		sentence_embedding = []
		for i in range(0,token_size,512):
			with torch.no_grad():
				last_hidden_state = model(input_ids=tokenized_sents['input_ids'][:,i:i+512],token_type_ids=tokenized_sents['token_type_ids'][:,i:i+512],
				attention_mask=tokenized_sents['attention_mask'][:,i:i+512])[0]

			sentence_embedding.append(last_hidden_state.detach().cpu().numpy()[0])
			#np.save(save_dir+'/{}.npy'.format(composition),last_hidden_state.detach().cpu().numpy()[0])

		embeddings.append(np.concatenate(sentence_embedding).mean(0).reshape(1,-1))

	robo_data['embeddings'] = embeddings
	robo_data.to_pickle('robo_descriptions_with_embed_noprop.pkl')

if __name__ == "__main__":

	save_dir = '../'
	data = pd.read_pickle('robo_descriptions.pkl')

	if not os.path.isfile(save_dir+'robo_data_kappa_with_prop.pkl'):
		robo_data = make_robodata(save_dir, data)
	else:
		robo_data = pd.read_pickle(save_dir+'robo_data_kappa_with_prop.pkl')

	run_matscibert(data)


import yaml
import os
import sys
import cv2
import glob
from terminaltables import AsciiTable
from progress.bar import Bar

def openAndResize(path,size):
    try:
        im = cv2.imread(path)
        old_size = im.shape[:2] # old_size is in (height, width) format

        ratio = float(size)/max(old_size)
        new_size = tuple([int(x*ratio) for x in old_size])

        # new_size should be in (width, height) format
        im = cv2.resize(im, (new_size[1], new_size[0]))

        delta_w = size - new_size[1]
        delta_h = size - new_size[0]
        top, bottom = delta_h//2, delta_h-(delta_h//2)
        left, right = delta_w//2, delta_w-(delta_w//2)

        color = [0, 0, 0]
        new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
            value=color)
        return 1,new_im
    except:
        print(sys.exc_info()[0])
        print("Error Image : ",path)
        return -1,[] #error

dir_path = os.path.dirname(os.path.realpath(__file__))
# no slash dir path
with open("config.yml", "r") as ymlfile:
    cfg = yaml.safe_load(ymlfile)

# {'input': {'folder': 'input'}, 'output': {'size': 32, 'folder': 'output'}}

inputPath = cfg['input']['folder']
outputPath = cfg['output']['folder']
imgSize = cfg['output']['size']

folders = [f for f in glob.glob(inputPath + "/*/", recursive=True)]

# Print input table
input_data = [
    ['Letter', 'count'],
]
total_count = 0
letters = []
for fol in folders:
    letter = fol.rsplit("/")[1]
    letters.append(letter)

    files = [f for f in glob.glob(fol + "**/*.png", recursive=True)]
    # Append to table
    #input/ആ/
    input_data.append([letter,len(files)])
    total_count+= len(files)

table = AsciiTable(input_data)
print("Input")
print(table.table)
print("Total :"+str(total_count))

count = 0
error_count = 0
bar = Bar('Processing', max=total_count)
for fol in folders:
    files = [f for f in glob.glob(fol + "**/*.png", recursive=True)]
    letter = fol.rsplit("/")[1]
    outputFolder = outputPath+"/"+letter+"/"
    #make output folder of not exist
    os.makedirs(outputFolder, exist_ok=True)
    current_count = 0
    for file in files:
        status,image = openAndResize(path=file,size=imgSize)
        filename = outputFolder+str(current_count)+".png"
        if(status != -1):
            cv2.imwrite(filename, image)
            count+=1
            current_count+=1
            bar.next()
        else:
            error_count+=1

print("\nDone!")
print("Success Images: "+str(count))
print("Error Images: "+str(error_count))

# files = [f for f in glob.glob(path + "**/*.txt", recursive=True)]

# r=root, d=directories, f = files


from summary_stats   import *

#= JSON File Type Processors ===================================================
def message_type_processor(entry, site):
  visitor   = entry["from"]
  timestamp = entry["timestamp"]

  # Keep track of unique visitors
  site["unique_visitors"].add(visitor)

  # Record who sent this message when
  if timestamp not in site["message_activity"]:
    site["message_activity"][timestamp] = []
  site["message_activity"][timestamp].append(visitor)

  # Update Site Stats
  update_stats(site)

def status_type_processor(entry, site):
  operator = entry["from"]

  # Keep track of unique operators
  site["unique_operators"].add(operator)

  # Add site operator state change and resort
  site["operator_activity"].append((entry["timestamp"], True if entry["data"]["status"] == "online" else False, operator))
  site["operator_activity"] = sorted( site["operator_activity"], key=lambda i: i[0] )

  # Update Site Stats
  update_stats(site)

type_processors = {
  "message": message_type_processor,
  "status":  status_type_processor
}
#===============================================================================


from collections import Counter
from unittest import TestCase
from helper.helper import Helper

class HelperTests(TestCase):

    def setUp(self):
        self.helper = Helper()
        self.board = self.helper.add_board('Board Name')

    def test_board(self):
        self.assertEqual(self.board.name, 'Board Name')
        self.assertEqual(self.board.closed, False)
        self.board.close()
        self.assertEqual(self.board.closed, True)
        self.board.open()
        self.assertEqual(self.board.closed, False)
        self.assertEqual(self.board.id, 'Board Name_id')
        self.assertEqual(self.board.open_lists(), [])
        self.assertEqual(self.board.open_cards(), [])
        self.assertEqual(self.board.get_labels(), [])

    def test_list(self):
        trello_list_1 = self.board.add_list('List 1')
        trello_list_2 = self.board.add_list('List 2')
        trello_list_3 = self.board.add_list('List 3')
        self.assertEqual(self.board.open_lists(), [trello_list_1, trello_list_2, trello_list_3])

    def test_card(self):
        trello_list_1 = self.board.add_list('List 1')
        trello_list_2 = self.board.add_list('List 2')
        trello_list_3 = self.board.add_list('List 3')
        card_1 = trello_list_1.add_card('Card 1')
        card_2 = trello_list_1.add_card('Card 2')
        card_3 = trello_list_1.add_card('Card 3')
        self.assertEqual(trello_list_1.list_cards(), [card_1, card_2, card_3])

    def test_create_test_data(self):
        boards = self.helper.create_test_data()
        board_names = [board.name for board in boards]
        expected_boards = ['imaging', 'vector', 'claims', 'infrastructure', 'mips', 'misc', 'apps']
        self.assertCountEqual(board_names, expected_boards)
        expected_lists = {'coding done': 7, 'qa': 7, 'in progress': 7, 'production': 7, 'final qa': 7, 'backlog': 7}
        list_counter = Counter()
        for board in boards:
            list_counter += Counter([trello_list.name for trello_list in board.open_lists()])
        self.assertEqual(dict(list_counter), expected_lists)
        self.assert_labels(boards)

    def assert_labels(self, boards):
        label_counter = Counter()
        no_labels = []
        for board in boards:
            cards = board.open_cards()
            for card in cards:
                if card.labels:
                    label_counter += Counter([label.name for label in card.list_labels])
                else:
                    no_labels.append(card)
        self.assertEqual(len(no_labels), 42)
        expected_labels = {'task': 126, 'production feedback': 84, 'staging feedback': 42}
        self.assertEqual(dict(label_counter), expected_labels)


kamus_file = ""
kamus = []
kata_dasar = ""
def BacaKamus():
	global kamus
	kamus_file = open("kamus.txt")
	kamus = str(kamus_file.read()).split(" ")

def CekKamus(kata):
	try:
		index = kamus.index(kata)
		if index > 0: return True
		else: return False
	except:
		return False
def KataDasar(kata):
	kata_olah1 = ""
	kata_olah2 = ""
	kata_olah3 = ""
	BacaKamus()
	kata_olah1 = HapusInflectionSuffixes(kata)
	akhiran, kata_olah2 = HapusDerivationSuffixes(kata_olah1)
	kata_olah3 = HapusDerivationPrefix(akhiran, kata_olah2, kata)

	if CekKamus(kata_olah1):
		return kata_olah2
	elif CekKamus(kata_olah2):
		return kata_olah2
	elif CekKamus(kata_olah3):
		return kata_olah3

	if kata_olah3=="" or kata_olah3==None:
		if kata_olah2=="":
			if kata_olah1=="" or kata_olah2==None:
				return kata
			else:
				return kata_olah1
		else:
			return kata_olah2
	else:
		return kata_olah3

def HapusInflectionSuffixes(kata):
	if kata.endswith("lah") or kata.endswith("kah") or kata.endswith("nya"):
		return left(kata,len(kata) - 3)
	elif kata.endswith("mu") or kata.endswith("ku"):
		return left(kata,len(kata) - 2)
	elif kata.endswith("lah") or kata.endswith("kah") or kata.endswith("tah") or kata.endswith("pun"):
		kata_olah1 = left(kata,len(kata) - 3)
		kata_olah2 = HapusInflectionSuffixes(kata_olah1)
		return kata_olah2
	else:
		return kata

def HapusDerivationSuffixes(kata):
	akhiran = ""
	kata_olah1 = ""
	kata_olah2 = ""

	if kata.endswith("i"):
		kata_olah1 = mid(kata,0,len(kata)-1)
		akhiran = "i"
	elif kata.endswith("an"):
		kata_olah1 = mid(kata,0,len(kata)-2)
		akhiran = "an"
	elif kata.endswith("kan"):
		kata_olah1 = mid(kata,0,len(kata)-3)
		akhiran = "kan"

	if CekKamus(kata_olah1):
		return akhiran,kata_olah1
	else:
		if kata_olah1.endswith("k"):

			kata_olah2 = mid(kata,0,len(kata_olah1)-1)
			if CekKamus(kata_olah2):
				return akhiran,kata_olah2
			else:
				return akhiran,kata_olah2
		else:

			if kata_olah1 != "":
				kata_olah2 = kata_olah1
			else:
				kata_olah2 = kata

	return akhiran,kata_olah2

def HapusDerivationPrefix(akhiran, kata_olah2, kata):
	ketemu = False
	kata_olah3 = ""
	if akhiran != "":
		if kata_olah2.startswith("be") and kata_olah2.endswith("i"):
			ketemu = CekKamus(kata_olah2)
		elif (kata_olah2.startswith("di") or kata_olah2.startswith("me")) and kata_olah2.endswith("an"):
			ketemu = CekKamus(kata_olah2)
		elif (kata_olah2.startswith("ke") or kata_olah2.startswith("se") ) and (kata_olah2.endswith("i") or kata_olah2.endswith("kan")):
			ketemu = CekKamus(kata_olah2)

	if ketemu:
		return kata_olah2
	else:
		if kata_olah2.startswith("ke"):
			return mid(kata_olah2,2,len(kata_olah2))
		if kata_olah2.startswith("se"):
			return mid(kata_olah2,2,len(kata_olah2))
		if kata_olah2.startswith("pe") or kata_olah2.startswith("dipe"):
			kata_olah3 = mid(kata_olah2,2,len(kata_olah2))

			if CekKamus(kata_olah3):
				return kata_olah3
			else:
				if kata_olah2.startswith("pen"):
					kata_olah3 = mid(kata_olah2,3,len(kata_olah2))

					if CekKamus(kata_olah3):
						return kata_olah3
					else:
						if CekKamus("t" + kata_olah3):
							return "t" + kata_olah3
						if kata_olah2.startswith("peny"):
							kata_olah3 = mid(kata_olah2,4,len(kata_olah2))

							if CekKamus("s" + kata_olah3):
								return "s" + kata_olah3
							else:
								return "s" + kata_olah3
				elif kata_olah2.startswith("per") or kata_olah2.startswith("diper"):
					if kata_olah2.startswith("per"):
						kata_olah3 = mid(kata_olah2,3,len(kata_olah2))

						if CekKamus(kata_olah3):
							return kata_olah3
						else:
							return kata_olah3
					elif kata_olah2.startswith("diper"):
						kata_olah3 = mid(kata_olah2,5,len(kata_olah2))

						if CekKamus(kata_olah3):
							return kata_olah3
						else:
							return kata_olah3
				elif kata_olah2.startswith("pem"):
					kata_olah3 = mid(kata_olah2,3,len(kata_olah2))

					if CekKamus(kata_olah3):
						return kata_olah3
					else:
						if CekKamus("p" + kata_olah3):
							return "p" + kata_olah3
						else:
							return "p" + kata_olah3
		if kata_olah2.startswith("me"):
			kata_olah3 = mid(kata_olah2,2,len(kata_olah2))

			if CekKamus(kata_olah3):
				# if (kata_olah3.startswith("l") or kata_olah3.startswith("m") or kata_olah3.startswith("n") or kata_olah3.startswith("r") or kata_olah3.startswith("w")):
				return kata_olah3
			else:
				if kata_olah2.startswith("meng"):

					kata_olah3 = mid(kata_olah2,4,len(kata_olah2))

					if CekKamus(kata_olah3):
						return kata_olah3
					else:
						if CekKamus("g" + kata_olah3):
							return "g" + kata_olah3
						elif CekKamus("h" + kata_olah3):
							return "h" + kata_olah3
						elif CekKamus("k" + kata_olah3):
							return "k" + kata_olah3
				elif kata_olah2.startswith("mem"):
					kata_olah3 = mid(kata_olah2,3,len(kata_olah2))

					if CekKamus(kata_olah3):
						return kata_olah3
					else:
						if CekKamus("p" + kata_olah3):
							return "p" + kata_olah3
						else:
							return "p" + kata_olah3
					return kata_olah3
				elif kata_olah2.startswith("men"):
					kata_olah3 = mid(kata_olah2,3,len(kata_olah2))
					if CekKamus(kata_olah3):
						return kata_olah3
					else:
						if CekKamus("t" + kata_olah3):
							return "t" + kata_olah3
						if kata_olah2.startswith("meny"):
							kata_olah3 = mid(kata_olah2,4,len(kata_olah2))

							if CekKamus("s" + kata_olah3):
								return "s" + kata_olah3

					return kata_olah3
		if kata_olah2.startswith("ber"):
			kata_olah3 = mid(kata_olah2,3,len(kata_olah2))
			if CekKamus(kata_olah3):
				return kata_olah3

	return kata_olah3

def vowel(huruf):
	if (huruf == 'a' or huruf == 'i' or huruf == 'u' or huruf == 'e' or huruf == 'o'):
		return True
	else:
		return False

def left(s, amount):
	#left
    return s[:amount]

def right(s, amount):
	#right
    return s[-amount:]

def mid(s, offset, amount):
	#mid
    return s[offset:offset+amount]
# print KataDasar("mengadopsi")


'''
UNIVERSIDAD DEL VALLE DE GUATEMALA
Criptografía y Cifrado de la Información
Sección 10

Walter Danilo Saldaña Salguero 19897
Jose Abraham Gutierrez Corado 19111
Javier Alejandro Cotto Argueta 19324

LABORATORIO #3
'''

import numpy as np
import matplotlib.pyplot as plt
import re
from skimage import data, io   # estas librerías solo se usan para
from PIL import Image               # llamar al ejemplo de cameraman.png
import streamCipher as sc

def xor(a, b):
    m = len(a)
    n = len(b)
    maxx = max(m,n)
    if (m < n):
        a = a + (n-m)*'0'
    if (n < m):
        b = b + (m-n)*'0'

    c = ''
    for i in range(0, maxx):
        c = c + str(int(a[i]) ^ int(b[i]))
    return c

def img2bits(I):
    ''' Convierte una imagen en escala de grises a cadena de bits.
        Input:  I = imagen, como numpy array de shape (m,n).
        Output: s = string de bits, donde se concatenan cada pixel de I.
    '''
    m, n = I.shape
    s = ''
    for i in range(0, m):
        for j in range(0, n):
            s = s + '{0:08b}'.format(I[i,j])
    return s

def bits2img(x, shape):
    ''' Convierte una cadena de bits a una imagen en escala de grises.
        Input:  s = string de bits, donde se concatenan cada pixel de I.
                shape = dimensiones (m,n) de la imagen de salida I.
        Output: I = imagen, como numpy array de shape (m,n).
    '''

    m, n = shape
    I = np.zeros(m*n).astype(np.uint8)
    bts = re.findall('........', x)
    for i in range(0, len(bts)-1):
        I[i] = int(bts[i], 2)
    I = I.reshape(m,n)
    return I

I = io.imread('mono.png', as_gray = True)

J = Image.fromarray(I)
J = J.resize((J.size[0]//2, J.size[1]//2), Image.LANCZOS)
I = np.array(J) * 255
I = I.astype(float).astype(int)

plt.figure()
plt.imshow(I, cmap='gray')
plt.show()

s0 = img2bits(I)

#Stream 1
s1 = sc.LCG(3,5,254,8,int(len(s0)/8))
r1 = xor(s0, s1[0])

#Stream 2
s2 = sc.LFSR(r1, len(r1), [1, 2, 3, 4, 5, 6, 9, 11, 14, 15], 10)
r2 = xor(r1, s2[0])

#Stream 3
s3 = sc.wichmanHill(8,int(len(s0)/8))
r3 = xor(r2, s3[0])

rf = xor(s0, r3)

I1 = bits2img(r1, I.shape)
I2 = bits2img(r2, I.shape)
I3 = bits2img(r3, I.shape)
I4 = bits2img(rf, I.shape)

plt.figure(figsize=(15,8))

#Graficar primera transformación (Con algoritmo LCG)
plt.subplot(2,2,1)
plt.imshow(I1, cmap='gray')

#Graficar segunda transformación (Con algoritmo LFSR)
plt.subplot(2,2,2)
plt.imshow(I2, cmap='gray')

#Graficar tercera transformación (Con algoritmo wichmanHill)
plt.subplot(2,2,3)
plt.imshow(I3, cmap='gray')

#Graficar cuarta transformación (xor de la transformación resultante con la imagen original)
plt.subplot(2,2,4)
plt.imshow(I4, cmap='gray')

plt.show()


#!/usr/bin/env python
"""
    Script to replay fuzzer .log files created by caringcaribou
    (c) Jannik Schmied, 2023
"""
import os
import subprocess
import sys
from argparse import ArgumentParser, Namespace

def parse_args() -> Namespace:
    """Parse command line arguments"""
    parser: ArgumentParser = ArgumentParser()
    parser.add_argument("--file", "-f", type=str, required=True)
    parser.add_argument("--interface", "-i", type=str, required=True)

    return parser.parse_args()

def main():
    args = parse_args()

    if os.path.exists(args.file):
        with open(args.file, 'r') as messages:
            for msg_data in messages:
                msg = ["cansend", args.interface, msg_data]

                try:
                    proc = subprocess.Popen(msg, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    print(f"[i] Current: {msg.strip()}", end='\r')
                except KeyboardInterrupt:
                    print("\n[*] Stopped. (Interrupted by user)")
                    exit(0)
                except Exception as e:
                    print("[!] Error:", e)
                finally:
                    proc.terminate()
    else:
        print("[!] Error: file does not exist!")
        sys.exit(1)

    print("\n[+] Finished.\n")
    sys.exit(0)

if __name__ == '__main__':
    main()


'''
Descripttion:
version:
Author: Catop
Date: 2021-02-10 07:47:09
LastEditTime: 2021-06-19 16:34:27
'''
#coding:utf-8

import os
import time
import random
import json
from flask import Flask,request,jsonify

from globalAPI import goapi as goapi
from globalAPI import dbconn as dbconn
from globalAPI.CB_logger import plog as CB_logger

from router import CB_router as CB_router

app = Flask(__name__)
@app.route('/', methods=['POST'])
def getEvent():
    data = request.json
    post_type = data.get('post_type')
    if(post_type == 'message'):
        message_type = data.get('message_type')
        message = data.get('message')
        message_id = data.get('message_id')
        user_id = str(data.get('user_id'))
        sender = data.get('sender')
        #sender为dict
        if(message_type=='private'):
            #处理私聊消息
            CB_logger('Flask',f'接收私聊消息@{user_id}:{message[:20]}')
            CB_router(user_id,message,'private',sender=sender,message_id=message_id)
        elif(message_type=='group'):
            #处理群聊消息
            group_id = data.get('group_id')

            CB_logger('Flask',f'接收群消息@{group_id}@{user_id}:{message[:20]}')
            CB_router(user_id,message,'group',group_id,sender=sender,message_id=message_id)

    elif(post_type == 'request'):
        request_type = data.get('request_type')
        if(request_type=='friend'):
            user_id = str(data.get('user_id'))
            comment = str(data.get('comment'))
            flag = str(data.get('flag'))
            CB_logger('Flask',f'\n[flask]接收加好友请求@{user_id}:{comment[:20]}')
            time.sleep(random.randint(5,10))
            goapi.add_request(flag)
            time.sleep(random.randint(5,10))
            goapi.sendMsg(user_id,"欢迎！\n请先注册，例如'注册@张三@信安20-2'\n 班级请严格按格式输入，否则可能统计不上哦")
    else:
        #暂不处理其他类型上报，为防止go-cq报错而设置
        pass

    return data

if __name__ == '__main__':
    conf_info = "conf/botConf.json"
    with open(conf_info,"r") as f:
        conf_dict = json.load(f)
    flask_host = conf_dict['Flask']['Host']
    flask_port = conf_dict['Flask']['Port']
    flask_debug = conf_dict['Flask']['Debug']
    if flask_debug=="False":
        flask_debug = False
    else:
        flask_debug = True

    app.run(host=flask_host,port=flask_port,debug=flask_debug)


import random
def list_random(range_list=10):  # создание списк рандомных наутральных чисел
    i = 1
    l = []
    while i <= range_list:
        l.append(random.randint(0, 9))
        i = i + 1
    return l
def find_me(find_num, range=10):  # поиск числа в списке и вывод его индекса
    l = list_random(range)
    print(l)
    for i in l:
        if i == find_num:
            return l.index(i)
    return False
range = int(input('Input range of list: '))
x = int(input('Input number to find in list: '))
list_of_random_numbers = (list_random(range))
find = find_me(x, range)
if find != False:
    print('First entry of number', x, 'into the list', find)
else:
    print('Number', x, 'not found in the list')


from copy import copy
from enum import Enum

import numpy as np
import sympy as sp

from .linear_circuit import ACircuit
from perceval.utils import Matrix, format_parameters, BasicState, StateVector, Parameter

class BSConvention(Enum):
    Rx = 0
    Ry = 1
    H = 2

class BS(ACircuit):
    """Beam splitter"""
    DEFAULT_NAME = "BS"

    def __init__(self, theta=sp.pi/2, phi_tl=0, phi_bl=0, phi_tr=0, phi_br=0,
                 convention: BSConvention = BSConvention.Rx):
        super().__init__(2)
        self._convention = convention
        self._theta = self._set_parameter("theta", theta, 0, 4*sp.pi)
        self._phi_tl = self._set_parameter("phi_tl", phi_tl, 0, 2*sp.pi)
        self._phi_bl = self._set_parameter("phi_bl", phi_bl, 0, 2*sp.pi)
        self._phi_tr = self._set_parameter("phi_tr", phi_tr, 0, 2*sp.pi)
        self._phi_br = self._set_parameter("phi_br", phi_br, 0, 2*sp.pi)

    @property
    def name(self):
        return f'{self.DEFAULT_NAME}({self._convention.name})'

    @property
    def convention(self):
        return self._convention

    @staticmethod
    def H(theta=sp.pi/2, phi_tl=0, phi_bl=0, phi_tr=0, phi_br=0):
        return BS(theta, phi_tl, phi_bl, phi_tr, phi_br, convention=BSConvention.H)

    @staticmethod
    def Rx(theta=sp.pi / 2, phi_tl=0, phi_bl=0, phi_tr=0, phi_br=0):
        return BS(theta, phi_tl, phi_bl, phi_tr, phi_br, convention=BSConvention.Rx)

    @staticmethod
    def Ry(theta=sp.pi / 2, phi_tl=0, phi_bl=0, phi_tr=0, phi_br=0):
        return BS(theta, phi_tl, phi_bl, phi_tr, phi_br, convention=BSConvention.Ry)

    @staticmethod
    def r_to_theta(r):
        """Compute theta given a reflectivity value
        """
        return 2*np.arccos(np.sqrt(float(r)))

    @staticmethod
    def theta_to_r(theta):
        if isinstance(theta, Parameter) and not theta.defined:
            return sp.cos(theta.spv/2)**2
        else:
            return np.cos(float(theta)/2)**2

    @property
    def reflectivity(self):
        return self.theta_to_r(self._theta)

    def _compute_unitary(self, assign=None, use_symbolic=False):
        if use_symbolic:
            theta = self._theta.spv
            cos_theta = sp.cos(theta/2)
            sin_theta = sp.sin(theta/2)
            phi_tl = self._phi_tl.spv
            phi_tr = self._phi_tr.spv
            phi_bl = self._phi_bl.spv
            phi_br = self._phi_br.spv
            u00_mul = sp.exp((phi_tl + phi_tr)*sp.I)
            u01_mul = sp.exp((phi_tr + phi_bl)*sp.I)
            u10_mul = sp.exp((phi_tl + phi_br)*sp.I)
            u11_mul = sp.exp((phi_br + phi_bl)*sp.I)
        else:
            cos_theta = np.cos(float(self._theta)/2)
            sin_theta = np.sin(float(self._theta)/2)
            phi_tl_tr = float(self._phi_tl) + float(self._phi_tr)
            u00_mul = np.cos(phi_tl_tr) + 1j*np.sin(phi_tl_tr)
            phi_tr_bl = float(self._phi_tr) + float(self._phi_bl)
            u01_mul = np.cos(phi_tr_bl) + 1j*np.sin(phi_tr_bl)
            phi_tl_br = float(self._phi_tl) + float(self._phi_br)
            u10_mul = np.cos(phi_tl_br) + 1j*np.sin(phi_tl_br)
            phi_bl_br = float(self._phi_bl) + float(self._phi_br)
            u11_mul = np.cos(phi_bl_br) + 1j*np.sin(phi_bl_br)

        umat = self._matrix_template(use_symbolic)
        umat[0, 0] *= u00_mul*cos_theta
        umat[0, 1] *= u01_mul*sin_theta
        umat[1, 1] *= u11_mul*cos_theta
        umat[1, 0] *= u10_mul*sin_theta
        return umat

    def _matrix_template(self, use_symbolic):
        if self._convention == BSConvention.Rx:
            if use_symbolic:
                return Matrix([[1, sp.I], [sp.I, 1]], True)
            return Matrix([[1, 1j], [1j, 1]], False)
        elif self._convention == BSConvention.Ry:
            return Matrix([[1, -1], [1, 1]], use_symbolic)
        elif self._convention == BSConvention.H:
            return Matrix([[1, 1], [1, -1]], use_symbolic)
        raise NotImplementedError(f'Unitary matrix computation not implemented for convention {self._convention.name}')

    def get_variables(self, map_param_kid=None):
        parameters = {}
        if map_param_kid is None:
            map_param_kid = self.map_parameters()
        self.variable_def(parameters, "theta", "theta", sp.pi/2, map_param_kid)
        self.variable_def(parameters, "phi_tl", "phi_tl", 0, map_param_kid)
        self.variable_def(parameters, "phi_bl", "phi_bl", 0, map_param_kid)
        self.variable_def(parameters, "phi_tr", "phi_tr", 0, map_param_kid)
        self.variable_def(parameters, "phi_br", "phi_br", 0, map_param_kid)
        return parameters

    def describe(self, map_param_kid=None):
        parameters = self.get_variables(map_param_kid)
        parameters['convention'] = f'BSConvention.{self._convention.name}'
        params_str = format_parameters(parameters, separator=', ')
        return "BS(%s)" % params_str

    def inverse(self, v=False, h=False):
        if not self.defined:
            raise ValueError('Cannot invert BS with variable parameters')
        phi_bl = float(self._phi_bl)
        phi_tl = float(self._phi_tl)
        phi_tr = float(self._phi_tr)
        phi_br = float(self._phi_br)
        if v:
            self._phi_bl.set_value(phi_tl, force=True)
            self._phi_tr.set_value(phi_br, force=True)
            self._phi_tl.set_value(phi_bl, force=True)
            self._phi_br.set_value(phi_tr, force=True)
            # For Rx BS, vertical inversion does not impact theta parameter
            if self._convention == BSConvention.Ry:
                self._theta.set_value(- float(self._theta), force=True)
            elif self._convention == BSConvention.H:
                self._theta.set_value(2*np.pi - float(self._theta), force=True)
        if h:
            self._phi_bl.set_value(-phi_br, force=True)
            self._phi_tr.set_value(-phi_tl, force=True)
            self._phi_tl.set_value(-phi_tr, force=True)
            self._phi_br.set_value(-phi_bl, force=True)
            # For H BS, horizontal inversion does not impact theta parameter
            if self._convention == BSConvention.Rx or self._convention == BSConvention.Ry:
                self._theta.set_value(- float(self._theta), force=True)

    def definition(self):
        return BS(Parameter('theta'), Parameter('phi_tl'), Parameter('phi_bl'), Parameter('phi_tr'),
                  Parameter("phi_br"), self._convention).U

class PS(ACircuit):
    """Phase shifter"""
    DEFAULT_NAME = "PS"

    def __init__(self, phi):
        super().__init__(1)
        self._phi = self._set_parameter("phi", phi, 0, 2*sp.pi)

    def _compute_unitary(self, assign=None, use_symbolic=False):
        self.assign(assign)
        if use_symbolic:
            return Matrix([[sp.exp(self._phi.spv*sp.I)]], True)
        else:
            return Matrix([[np.cos(float(self._phi)) + 1j * np.sin(float(self._phi))]], False)

    def get_variables(self, map_param_kid=None):
        parameters = {}
        if map_param_kid is None:
            map_param_kid = self.map_parameters()
        self.variable_def(parameters, "phi", "phi", None, map_param_kid)
        return parameters

    def describe(self, map_param_kid=None):
        parameters = self.get_variables(map_param_kid)
        params_str = format_parameters(parameters, separator=', ')
        return "PS(%s)" % params_str

    def inverse(self, v=False, h=False):
        if h:
            if self._phi.is_symbolic():
                raise ValueError('Cannot invert PS with variable parameters')
            else:
                self._phi.set_value(-float(self._phi), force=True)

class WP(ACircuit):
    """Wave plate"""
    DEFAULT_NAME = "WP"
    _supports_polarization = True

    def __init__(self, delta, xsi):
        super().__init__(1)
        self._delta = self._set_parameter("delta", delta, -sp.pi, sp.pi)
        self._xsi = self._set_parameter("xsi", xsi, -sp.pi, sp.pi)

    def _compute_unitary(self, assign=None, use_symbolic=False):
        self.assign(assign)
        if use_symbolic:
            delta = self._delta.spv
            xsi = self._xsi.spv
            return Matrix([[
                            sp.cos(delta)+sp.I*sp.sin(delta)*sp.cos(2*xsi),
                            sp.I*sp.sin(delta)*sp.sin(2*xsi)
                           ], [
                            sp.I * sp.sin(delta) * sp.sin(2 * xsi),
                            sp.cos(delta) - sp.I * sp.sin(delta) * sp.cos(2 * xsi)
                           ]], True)
        else:
            delta = float(self._delta)
            xsi = float(self._xsi)
            return Matrix([[
                            np.cos(delta)+1j*np.sin(delta)*np.cos(2*xsi),
                            1j*np.sin(delta)*np.sin(2*xsi)
                           ], [
                            1j * np.sin(delta) * np.sin(2 * xsi),
                            np.cos(delta) - 1j * np.sin(delta) * np.cos(2 * xsi)
                           ]], False)

    def get_variables(self, map_param_kid=None):
        parameters = {}
        if map_param_kid is None:
            map_param_kid = self.map_parameters()
        self.variable_def(parameters, "xsi", "xsi", None, map_param_kid)
        self.variable_def(parameters, "delta", "delta", None, map_param_kid)
        return parameters

    def describe(self, map_param_kid=None):
        parameters = self.get_variables(map_param_kid)
        params_str = format_parameters(parameters, separator=', ')
        return "WP(%s)" % params_str

    def inverse(self, v=False, h=False):
        raise NotImplementedError("inverse not yet implemented")

class HWP(WP):
    """Half wave plate"""
    DEFAULT_NAME = "HWP"

    def __init__(self, xsi):
        super().__init__(sp.pi/2, xsi)

    def definition(self):
        return HWP(xsi=Parameter('xsi')).U

class QWP(WP):
    """Quarter wave plate"""
    DEFAULT_NAME = "QWP"

    def __init__(self, xsi):
        super().__init__(sp.pi/4, xsi)

    def definition(self):
        return QWP(xsi=Parameter('xsi')).U

class PR(ACircuit):
    """Polarization rotator"""
    _supports_polarization = True
    DEFAULT_NAME = "PR"

    def __init__(self, delta):
        super().__init__(1)
        self._delta = self._set_parameter("delta", delta, -sp.pi, sp.pi)

    def _compute_unitary(self, assign=None, use_symbolic=False):
        self.assign(assign)
        if use_symbolic:
            delta = self._delta.spv
            return Matrix([[sp.cos(delta), sp.sin(delta)],
                           [-sp.sin(delta), sp.cos(delta)]], True)
        else:
            delta = float(self._delta)
            return Matrix([[np.cos(delta), np.sin(delta)],
                           [-np.sin(delta), np.cos(delta)]], False)

    def get_variables(self, map_param_kid=None):
        parameters = {}
        if map_param_kid is None:
            map_param_kid = self.map_parameters()
        self.variable_def(parameters, "delta", "delta", None, map_param_kid)
        return parameters

    def describe(self, map_param_kid=None):
        parameters = self.get_variables(map_param_kid)
        params_str = format_parameters(parameters, separator=', ')
        return "PR(%s)" % params_str

    def inverse(self, v=False, h=False):
        raise NotImplementedError("inverse not yet implemented")

class Unitary(ACircuit):
    """Generic component defined by a unitary matrix"""
    DEFAULT_NAME = "Unitary"

    def __init__(self, U: Matrix, name: str = None, use_polarization: bool = False):
        assert U is not None, "A unitary matrix is required"
        assert U.is_unitary(), "U parameter must be a unitary matrix"
        # A symbolic matrix is not a use case for this component
        assert not U.is_symbolic(), "U parameter must not be symbolic"
        self._u = U
        m = U.shape[0]
        self._supports_polarization = use_polarization
        if use_polarization:
            assert m % 2 == 0, "Polarization matrix should have an even number of rows/col"
            m //= 2
        super().__init__(m, name)

    def _compute_unitary(self, assign: dict = None, use_symbolic: bool = False) -> Matrix:
        # Ignore assign and use_symbolic parameters as __init__ checked the unitary matrix is numeric
        return self._u

    def inverse(self, v=False, h=False):
        if v:
            self._u = np.flip(self._u)
        if h:
            self._u = self._u.inv()

    def describe(self, _=None):
        params = [f"Matrix('''{self._u}''')"]
        if self.name != Unitary.DEFAULT_NAME:
            params.append(f"name='{self._name}'")
        if self._supports_polarization:
            params.append("use_polarization=True")
        return f"Unitary({', '.join(params)})"

class PERM(Unitary):
    """Permutation"""
    DEFAULT_NAME = "PERM"

    def __init__(self, perm):
        assert isinstance(perm, list), "permutation Operator needs list parameter"
        assert (min(perm) == 0 and
                max(perm)+1 == len(perm) == len(set(perm)) == len([n for n in perm if isinstance(n, int)])),\
            "%s is not a permutation" % perm
        n = len(perm)
        u = Matrix.zeros((n, n), use_symbolic=False)
        for i, v in enumerate(perm):
            u[v, i] = 1
        super().__init__(U=u)

    def get_variables(self, _=None):
        return {'PERM': ''}

    def describe(self, _=None):
        return "PERM(%s)" % str(self.perm_vector)

    def definition(self):
        return self.U

    @property
    def perm_vector(self):
        nz = np.nonzero(self._u)
        m_list = nz[1].tolist()
        return [m_list.index(i) for i in nz[0]]

    def apply(self, r, sv):
        if isinstance(sv, BasicState):
            sv = StateVector(sv)

        min_r = r[0]
        max_r = r[-1] + 1

        permutation = self.perm_vector
        inv = np.empty_like(permutation)
        inv[permutation] = np.arange(len(inv), dtype=inv.dtype)
        inv = [inv[i].item() for i in range(len(inv))]

        nsv = copy(sv)
        nsv.clear()
        nsv.update({
            BasicState(state.set_slice(slice(min_r, max_r), BasicState([state[i + min_r] for i in inv]))): prob_ampli
            for state, prob_ampli in sv.items()
        })
        return nsv

class PBS(Unitary):
    """Polarized beam spliter"""
    _supports_polarization = True
    DEFAULT_NAME = "PBS"

    def __init__(self):
        u = Matrix([[0, 0, 1, 0],
                    [0, 1, 0, 0],
                    [1, 0, 0, 0],
                    [0, 0, 0, 1]])
        super().__init__(U=u, use_polarization=True)

    def get_variables(self, map_param_kid=None):
        return {}

    # noinspection PyMethodMayBeStatic
    def describe(self, _=None):
        return "PBS()"


import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import pandas as pd

# Initialized by selenium driver
driver = webdriver.Firefox()

# Categories used
categories = ['whisky', 'vino', 'cervezas',
              'tonica', 'tequilas-y-piscos', 'ron']

# ----------------------------------------------------------------
driver.get("https://www.tiendasjumbo.co/supermercado/vinos-y-licores/whisky")
# ----------------------------------------------------------------

# sleep for a few seconds before loading the page
time.sleep(15)

# class vtex button load more
initial_XPATH = "//div[contains(@class,'vtex-button__label flex items-center justify-center h-100 ph5')]"
# define the max clicks for page
max_click_SHOW_MORE = 20
# count the number of clicks
count = 1
while count <= max_click_SHOW_MORE:
    try:
        WebDriverWait(driver, 30).until(
            EC.visibility_of_element_located((By.XPATH, initial_XPATH))).click()
        WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.XPATH, initial_XPATH))).click()
        count += 1
        time.sleep(10)
    except TimeoutException:
        break

items = driver.find_elements(
    By.CSS_SELECTOR,  ".vtex-product-summary-2-x-element.pointer.pt3.pb4.flex.flex-column.h-100")

data_jumbo = []

for i in items:
    try:
        name = i.find_element(
            By.CSS_SELECTOR, ".vtex-product-summary-2-x-productBrand.vtex-product-summary-2-x-brandName.t-body").text.strip().upper()
    except:
        name = "SIN DESCRIPCION"
    try:
        brand = i.find_element(
            By.CSS_SELECTOR, ".vtex-product-summary-2-x-productBrandName").text.strip().upper()
    except:
        brand = "SIN MARCA"
    try:
        price_regular = i.find_element(
            By.CSS_SELECTOR, ".tiendasjumboqaio-jumbo-minicart-2-x-cencoPriceWithoutDiscount").text
    except:
        price_regular = 0
    try:
        price_now = i.find_element(
            By.CSS_SELECTOR, ".vtex-flex-layout-0-x-flexRow.vtex-flex-layout-0-x-flexRow--selling-price").text
    except:
        price_now = 0
    try:
        discount = i.find_element(
            By.CSS_SELECTOR, "flex items-center justify-center tiendasjumboqaio-jumbo-minicart-2-x-containerFlag")
    except:
        discount = 0

    data_jumbo.append({"shop": "JUMBO", "name": name, "brand": brand, "price_regular": price_regular,
                      "price_now": price_now, "discount": discount})

df = pd.DataFrame(data_jumbo)
df.to_csv('C:\workflow\web_scraping_dislicores\prod\jumbo\whisky_data.txt',
          index=False, encoding='utf-8')


"""
Find top result from spotify with search term
and reply to posts with the keyword
SpotifyIt!
"""

import os
import sys
import time
import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import spotipy
import praw
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

def get_spotify_result(comment_text):
    """
    Query Spotify to return top result
    """
    print('getting spotify result...')
    sys.stdout.flush()
    try:
        query = comment_text.split('!', 1)

        spotify_response = SPOTIFY.search(q=query[1], limit=1)
        spotify_response = json.dumps(spotify_response)
        spotify_response = json.loads(spotify_response)

        spotify_response = spotify_response["tracks"]["items"]
        artist = [item["album"]["artists"][0]["name"] for item in spotify_response]
        track = [item["name"] for item in spotify_response]
        album = [item["album"]["name"] for item in spotify_response]
        url = [item["external_urls"]["spotify"] for item in spotify_response]

        top_result = 'Artist: {0} \n\nTrack: {1} \n\nAlbum: \
        {2} \n\nURL: {3}'.format(artist[0], track[0], album[0], url[0])

    except Exception as error:
        top_result = 'No results returned. \n\nPlease try again with different keywords.'
        print('[spotify] Error: {0}'.format(error))
        sys.stdout.flush()

    return top_result

def search_for_comments():
    """
    Request comments with KEYWORD
    """
    print('getting comment list...')
    sys.stdout.flush()
    try:
        request = requests.get(\
        'https://api.pushshift.io/reddit/search?q=%22SpotifyIt!%22&limit=100',\
        headers={'User-Agent':'SpotifyItBot-Agent'}, verify=False)
        pushshift_response = request.json()
        pushshift_response = pushshift_response["data"]
    except Exception as error:
        print('[pushshift] Error: {0}'.format(error))
        sys.stdout.flush()

    return pushshift_response

def main():
    """
    Check for duplicates and comment on new searches
    """
    while True:
        comments = search_for_comments()
        try:
            for com in comments:
                comment_text = com["body"]
                comment_id = com["id"]
                submission = REDDIT.comment(comment_id)
                if comment_id not in SPOTIFIED_POSTS:
                    reply = get_spotify_result(comment_text)
                    SPOTIFIED_POSTS.append(comment_id)
                    disclaimer = "I'm a bot bleep bloop. \
                    \n\nPM me for more information or to report any issues."
                    submission.reply('{0}\n\n\n{1}'.format(reply, disclaimer))
                    print('Replied to {0} \n{1}'.format(comment_id, reply))
                    sys.stdout.flush()
                    print('writing to file...')
                    sys.stdout.flush()
                    # write to file
                    with open("SPOTIFIED_POSTS.txt", "w") as text_file:
                        for comment_id in SPOTIFIED_POSTS:
                            text_file.write(comment_id + "\n")
        except Exception as error:
            print('[main] Error: {0}'.format(error))
            sys.stdout.flush()
        time.sleep(60)

# runner
print("start...")
sys.stdout.flush()

REDDIT = praw.Reddit('spotify_it_bot')
SPOTIFY = spotipy.Spotify()
SUB = REDDIT.subreddit('all')
KEYWORD = 'SpotifyIt!'

# create file to record replies
if not os.path.isfile("SPOTIFIED_POSTS.txt"):
    SPOTIFIED_POSTS = []
else:
    with open("SPOTIFIED_POSTS.txt", "r") as f:
        SPOTIFIED_POSTS = f.read()
        SPOTIFIED_POSTS = SPOTIFIED_POSTS.split("\n")
        SPOTIFIED_POSTS = list(filter(None, SPOTIFIED_POSTS))

main()


# -*- coding: utf-8 -*-
import whisper
import zhconv
import time
from collections import deque
import logging
from logging.handlers import RotatingFileHandler
import os, json
from math import ceil
import queue
from recorder import read_audio_stream
from threading import Thread
import wave
import numpy as np

error_logger = logging.getLogger(__name__)

# 日志文件
ai_log_path = '/data/ai/ai.log'
ai_audio_path = '/data/ai/audio'
# 字典文件 json字典文件中char_map为替换的单字,words为替换的词语
with open('speech_command_dict.json', 'r') as f:
    speech_command_dict = json.load(f)

#  正式：rtsp://127.0.0.1:554/stream0/asound_rockchiphdmi   测试用：export RTSP_URL=rtmp://192.168.1.8:1935/live/1
# 设置错误信息日志的保存位置和文件名
file_handler = RotatingFileHandler(ai_log_path, maxBytes=10*1024*1024, backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
error_logger.addHandler(file_handler)
error_logger.setLevel(logging.ERROR)

if not os.path.exists(ai_audio_path):
    os.makedirs(ai_audio_path)
else:
    # 清理掉ai_audio_path下的所有文件
    for file_name in os.listdir(ai_audio_path):
        file_path = os.path.join(ai_audio_path, file_name)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as e:
            error_logger.error("Unable to delete file: " + str(e))

def speech_rec(wait_rec_audio_queue, message_queue,):

    """
    语音识别函数
    :param wait_rec_audio_queue: 存放音频文件地址和能量阈值的队列
    :param message_queue: 存放识别结果的队列
    """

    speech_config = '/data/ai/speech_config.json'
    rtsp_url_env = os.environ.get('RTSP_URL')
    try:
        with open(speech_config, 'r') as f:
            config = json.load(f)
            rtsp_url = config['RTSP_URL'] if not rtsp_url_env else rtsp_url_env
            model_type = str(config['MODEL_TYPE'])
    except Exception as e:
        rtsp_url = '......'
        model_type = '1'

    if model_type == '1':
        model = whisper.load_model("/usr/lib/wpm/base.pt")
    elif model_type == '2':
        model = whisper.load_model("/usr/lib/wpm/small.pt")
    elif model_type == '3':
        model = whisper.load_model("/usr/lib/wpm/medium.pt")
    else:
        model = whisper.load_model("/usr/lib/wpm/tiny.pt")
    # 第一次启动程序识别
    # result = model.transcribe('start_voice.mp3', language="Chinese")
    # print(result['text'])
    result = model.transcribe('start_test.wav', language="Chinese")
    print(result['text'])

    while True:
        wav_path, ENERGY_THRESHOLD = wait_rec_audio_queue.get()
        start_time = time.time()
        # with wave.open(wav_path, 'rb') as wav_file:
        #     audio_data = wav_file.readframes(-1)
        #     audio_data = np.frombuffer(audio_data, dtype=np.int16).flatten().astype(np.float32) / 32768.0
        if not os.path.exists(wav_path):
            continue

        for i in range(5):
            try:
                print("%s speech rec start..." % wav_path)
                result = model.transcribe(wav_path, language="Chinese")
                break
            except Exception as e:
                print(e)
                time.sleep(0.1)

        audio_rec_time = time.time() - start_time
        audio_start_time = int(wav_path.split('/')[-1].split('.')[0]) - 1

        segments = result['segments']
        if len(segments) == 0:
            print("No speech content recgnation")
            continue

        duration = ceil(max([i['end'] for i in segments])) + 1
        text_result = zhconv.convert(result['text'], 'zh-cn')
        text_result = text_translate(text_result)
        # 清理音频文件
        if os.path.exists(wav_path):
            os.remove(wav_path)

        print("********************************************************* 推理时间: %d 等待时间：%f 音频时长：%f " % (audio_rec_time, time.time() - audio_start_time, duration))
        print("********************************************************* 识别内容: " + result['text'])
        print("********************************************************* 翻译内容: " + text_result)

        # 组织返回的数据
        data = {
            "rtsp_id": rtsp_url,
            "start_time": audio_start_time,
            "duration": duration,
            "text": text_result,
            "energy_threashold": ENERGY_THRESHOLD
        }
        # 发送消息
        message_queue.put((1, data))

def main(message_queue):
    # 注意 这个队列中存的是两个值，一个音频文件地址 一个energy_threashold
    wait_rec_audio_queue = queue.Queue()
    get_rec_audio = Thread(target=read_audio_stream, args=(wait_rec_audio_queue,))
    rec_audio = Thread(target=speech_rec, args=(wait_rec_audio_queue, message_queue,))
    get_rec_audio.start()
    rec_audio.start()
    wait_rec_audio_queue.join()
    get_rec_audio.join()
    rec_audio.join()

def text_translate(text):
    """
    翻译文本
    :param text: 待翻译文本
    :return: 翻译后文本
    """
    global speech_command_dict
    char_map = speech_command_dict['char_map']
    words = speech_command_dict['words']

    translate_text = ""
    for o_char in text:
        if o_char in char_map:
            translate_text += char_map[o_char]
        else:
            translate_text += o_char

    for i in words:
        translate_text = translate_text.replace(i[0], i[1]).replace("音响", "影响").replace("Go", "9")

    return translate_text

if __name__ == "__main__":
    message_queue = queue.Queue()
    main(message_queue)


#Программа принимает действительное положительное число x и
# целое отрицательное число y. Выполните возведение числа x в степень y.
# Задание реализуйте в виде функции my_func(x, y).
# При решении задания нужно обойтись без встроенной функции возведения числа в степень.

def my_func(x, y):
    num=x**y
    return num

def my_func_2(x, y):
    y=abs(y-1)#смена знака + 1 проход
    rez=1 #значение для первго прохода
    while y>1: #вход в цикл
        rez=rez * x #умножаем на количество циклов х
        y=y-1
    return (1/rez)

num_x=int(input("Введите положительное число х:"))
num_y=int(input("Введите отрицательное целое число Y:"))

print("Вариант первый:", my_func(num_x, num_y))
print("Вариант через цикл:", my_func_2(num_x, num_y))


"""project URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from app import views

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^$', views.home, name="home"),
    url(r'^signup/$', views.signup, name="signup"),
    url(r'^user_login/', views.user_login, name="user_login"),
    url(r'^menu$', views.menu, name="menu"),
    url(r'^logout/$', views.user_logout, name="logout"),
    url(r'^mess/$', views.mess, name="mess"),
    url(r'^breakfast/$', views.breakfast, name="breakfast"),
    url(r'^lunch/$', views.lunch, name="lunch"),
    url(r'^dinner/$', views.dinner, name="dinner"),
    url(r'^restaurant/$', views.restaurant, name="restaurant"),
    url(r'^rbreakfast/$', views.rbreakfast, name="rbreakfast"),
    url(r'^rlunch/$', views.rlunch, name="rlunch"),
    url(r'^rdinner/$', views.rdinner, name="rdinner"),
]


from inpost.static import ParcelStatus, Parcel

pending_statuses = [ParcelStatus.READY_TO_PICKUP, ParcelStatus.CONFIRMED,
                    ParcelStatus.ADOPTED_AT_SORTING_CENTER, ParcelStatus.ADOPTED_AT_SOURCE_BRANCH,
                    ParcelStatus.COLLECTED_FROM_SENDER, ParcelStatus.DISPATCHED_BY_SENDER,
                    ParcelStatus.DISPATCHED_BY_SENDER_TO_POK, ParcelStatus.OUT_FOR_DELIVERY,
                    ParcelStatus.OUT_FOR_DELIVERY_TO_ADDRESS, ParcelStatus.SENT_FROM_SOURCE_BRANCH,
                    ParcelStatus.TAKEN_BY_COURIER, ParcelStatus.TAKEN_BY_COURIER_FROM_POK,
                    ParcelStatus.STACK_IN_BOX_MACHINE, ParcelStatus.STACK_IN_CUSTOMER_SERVICE_POINT]

welcome_message = 'Hello!\nThis is a bot helping you to manage your InPost parcels!\n' \
                  'If you want to contribute to Inpost development you can find us there: ' \
                  '[Inpost](https://github.com/IFOSSA/inpost-python)\n\n' \
                  'Log in using button that just shown up below the text box ' \
                  'or by typing `/init <phone_number>`!\n\n' \
                  '**List of commands:**\n' \
                  '/start - display start message and allow user to login with Telegram\n' \
                  '/init - login using phone number `/init <phone_number>`\n' \
                  '/me - show information about all initialized accounts\n' \
                  '/pending - return pending parcels\n' \
                  '/delivered - return delivered parcels\n' \
                  '/parcel - return parcel `/parcel <shipment_number>`\n' \
                  '/friends - list all known inpost friends \n' \
                  '/share <reply to parcel message> - share parcel to listed friend\n' \
                  '/all - return all available parcels\n' \
                  '/clear - if you accidentally invoked `/start` and annoying box sprang up\n\n' \
                  'Deprecated commands:\n' \
                  '/refresh - refresh authorization token\n' \
                  '/confirm <sms code> - confirm login with sms code' \
                  '\n\n\nBeware, before you start using this bot you must **set your consent** to data collecting. ' \
                  'It will affect all your connected accounts. Dev team assure you, that this data **will not** be ' \
                  'used or sold and all sensitive ones like your name and surname **will be** removed. ' \
                  'Collected data are used for three reasons:\n' \
                  '    - cache - if we save your data bot has to less frequently gather data from inpost,\n' \
                  '    - development,\n' \
                  '    - debugging - in case some error occurs.\n' \
                  '\n\nYou can set your consent by typing `/consent yes` or `/consent no`' \

not_enough_parameters_provided = 'No phone number provided or no option selected!\n' \
           'You can set your default phone number by sending `/set_default_phone_number <phone_number>` ' \
           'or invoke this command by sending message following this template' \
           '`/command <phone_number> <following part of command (e.g shipment number, On/Off)>`'

def courier_message_builder(package: Parcel) -> str:
    return f'📤 **Sender:** `{package.sender.sender_name}`\n' \
           f'📦 **Shipment number:** `{package.shipment_number}`\n' \
           f'📮 **Status:** `{package.status.value}`\n'

def delivered_message_builder(package: Parcel) -> str:  # Duplicate just to be clear
    return f'📤 **Sender:** `{package.sender.sender_name if package.sender is not None else None}`\n' \
           f'📦 **Shipment number:** `{package.shipment_number}`\n' \
           f'📮 **Status:** `{package.status.value}`\n'

def multicompartment_message_builder(amount: int, package: Parcel, other: str) -> str:
    return f'⚠ **THIS IS MULTICOMPARTMENT CONTAINING {amount} PARCELS!** ⚠\n\n' \
           f'📤 **Sender:** `{package.sender.sender_name}`\n' \
           f'📦 **Shipment number:** `{package.shipment_number}`\n' \
           f'📮 **Status:** `{package.status.value}`\n' \
           f'📥 **Pick up point:** `{package.pickup_point}, {package.pickup_point.city} ' \
           f'{package.pickup_point.street} {package.pickup_point.building_number}`\n\n' \
           f'Other parcels inside:\n{other}'

def compartment_message_builder(package: Parcel) -> str:
    return f'📤 **Sender:** `{package.sender.sender_name}`\n' \
           f'📦 **Shipment number:** `{package.shipment_number}`\n' \
           f'📮 **Status:** `{package.status.value}`\n' \
           f'📥 **Pick up point:** `{package.pickup_point}, {package.pickup_point.city} ' \
           f'{package.pickup_point.street} {package.pickup_point.building_number}`'

def details_message_builder(parcel: Parcel, events: str) -> str:
    return f'**Shipment number**: {parcel.shipment_number}\n' \
           f'**Stored**: {parcel.stored_date.to("local").format("DD.MM.YYYY HH:mm")}\n' \
           f'**Open code**: {parcel.open_code}\n' \
           f'**Events**:\n{events}\n\n'

def open_comp_message_builder(parcel: Parcel) -> str:
    return f'Compartment opened!\nLocation:\n   ' \
           f'Side: {parcel.compartment_location.side}\n   ' \
           f'Row: {parcel.compartment_location.row}\n   ' \
           f'Column: {parcel.compartment_location.column}'

def ready_to_pickup_message_builder(parcel: Parcel, events: str, air_quality: str | None) -> str:
    msg = f'**Stored**: {parcel.stored_date.to("local").format("DD.MM.YYYY HH:mm")}\n' \
          f'**Open code**: {parcel.open_code}\n' \
          f'**Events**:\n{events}'

    if air_quality:
        msg = msg + air_quality

    return msg

def out_of_range_message_builder(parcel: Parcel) -> str:
    return f'Your location is outside the range that is allowed to open this parcel machine. ' \
           f'Confirm that you are standing nearby, there is description:' \
           f'\n\n**Name: {parcel.pickup_point.name}**' \
           f'\n**Address: {parcel.pickup_point.post_code} {parcel.pickup_point.city}, ' \
           f'{parcel.pickup_point.street} {parcel.pickup_point.building_number}**\n' \
           f'**Description: {parcel.pickup_point.description}**\n\n' \
           f'Do you still want me to open it for you?'

def friend_invitations_message_builder(friend) -> str:
    return f'**Name**: {friend["friend"]["name"]}\n' \
           f'**Phone number**: {friend["friend"]["phoneNumber"]}\n' \
           f'**Invitation code**: `{friend["invitationCode"]}`\n' \
           f'**Expiry date**: {friend["expiryDate"]}'

def use_command_as_reply_message_builder(command: str) -> str:
    return f'Buttons works only with default phone number. ' \
           f'Please set up one before using them or type following command and send it as a reply to desired parcel: ' \
           f'\n`{command} <phone_number>'


import pytest
import responses

from norris import get_joke, joke_length

"""
Response will look something like this
{
  "category": "['science']",
  "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
  "id": "7SIYDCQ6QiiTLtd-B3sBEQ",
  "url": "http://api.chucknorris.io/jokes/7SIYDCQ6QiiTLtd-B3sBEQ",
  "value": "Chuck Norris broke the speed of sound. With his elbow."
}
"""

@pytest.fixture
def my_responses():

    test_response = {
        "category": ['science'],
        "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
        "id": "7SIYDCQ6QiiTLtd-B3sBEQ",
        "url": "http://api.chucknorris.io/jokes/7SIYDCQ6QiiTLtd-B3sBEQ",
        "value": "Chuck Norris broke the speed of sound. With his elbow."
    }

    with responses.RequestsMock() as rsps:
        rsps.add(responses.GET, 'https://api.chucknorris.io/jokes/random?category=science',
                 json=test_response, status=200)
        yield rsps

def test_pytest_norris_basic(my_responses):
    verify_response = {
        "category": ['science'],
        "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
        "id": "7SIYDCQ6QiiTLtd-B3sBEQ",
        "url": "http://api.chucknorris.io/jokes/7SIYDCQ6QiiTLtd-B3sBEQ",
        "value": "Chuck Norris broke the speed of sound. With his elbow."
    }
    result = get_joke()
    assert result == verify_response

def test_norris_value(my_responses):

    test_value = len("Chuck Norris broke the speed of sound. With his elbow.")
    # test_value = 5

    result = joke_length()
    assert result == test_value

# This test is to demonstrate when you might want to use curl or postman
# to troubleshoot an API call
"""
Imagine the response used to look like:
{
  "data": {
      "category": "['science']",
      "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
      "id": "7SIYDCQ6QiiTLtd-B3sBEQ",
      "url": "http://api.chucknorris.io/jokes/7SIYDCQ6QiiTLtd-B3sBEQ",
      "value": "Chuck Norris broke the speed of sound. With his elbow."
  }
}
"""
def test_norris_badkey():
    result = get_joke()
    # This will fail because there's no such key as data anymore
    assert result["data"]["value"] == "This is a bad key"