import torch
import numpy as np
import torch.nn as nn
import math
from PIL import Image
import os
from core.constants import palette, NUM_CLASSES, IGNORE_LABEL

def denorm(x):
    out = (x + 1) / 2
    return out.clamp(0, 1)

def norm(x):
    out = (x - 0.5) * 2
    return out.clamp(-1, 1)

def reset_grads(model, require_grad):
    for p in model.parameters():
        p.requires_grad_(require_grad)
    return model

def imresize_torch(image_batch, scale, mode):
    new_size = np.ceil(scale * np.array([image_batch.shape[2], image_batch.shape[3]])).astype(np.int)
    if mode=='bicubic':
        return nn.functional.interpolate(image_batch, size=(new_size[0], new_size[1]), mode=mode, align_corners=True)
    else:
        return nn.functional.interpolate(image_batch, size=(new_size[0], new_size[1]), mode=mode)

def calc_gradient_penalty(netD, real_data, fake_data, LAMBDA, device):
    alpha = torch.rand(1, 1)
    alpha = alpha.expand(real_data.size())
    alpha = alpha.to(device)

    interpolates = alpha * real_data + ((1 - alpha) * fake_data)
    interpolates = interpolates
    interpolates = torch.autograd.Variable(interpolates, requires_grad=True)

    disc_interpolates = netD(interpolates)

    gradients = torch.autograd.grad(outputs=disc_interpolates, inputs=interpolates,
                                    grad_outputs=torch.ones(disc_interpolates.size()).to(device),
                                    create_graph=True, retain_graph=True, only_inputs=True)[0]
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
    return gradient_penalty

def save_networks(path, netDst, netGst, netDts, netGts, Gst, Gts, Dst, Dts, opt, semseg_cs=None):
    if not opt.debug_run:
        try:
            os.makedirs(path)
        except OSError:
            pass
        if len(opt.gpus) > 1:
            torch.save(Dst + [netDst.module], '%s/Dst.pth' % (path))
            torch.save(Gst + [netGst.module], '%s/Gst.pth' % (path))
            torch.save(Dts + [netDts.module], '%s/Dts.pth' % (path))
            torch.save(Gts + [netGts.module], '%s/Gts.pth' % (path))
            if semseg_cs != None:
                torch.save(semseg_cs.module, '%s/semseg_cs.pth' % (path))
        else:
            torch.save(Dst + [netDst], '%s/Dst.pth' % (path))
            torch.save(Gst + [netGst], '%s/Gst.pth' % (path))
            torch.save(Dts + [netDts], '%s/Dts.pth' % (path))
            torch.save(Gts + [netGts], '%s/Gts.pth' % (path))
            if semseg_cs != None:
                torch.save(semseg_cs, '%s/semseg_cs.pth' % (path))

def colorize_mask(mask):
    # mask: tensor of the mask
    # returns: numpy array of the colorized mask
    new_mask = Image.fromarray(mask.cpu().numpy().astype(np.uint8)).convert('P')
    new_mask.putpalette(palette)
    new_mask = np.array(new_mask.convert('RGB')).transpose((2, 0, 1))
    return new_mask

def nanmean_torch(x):
    num = torch.where(torch.isnan(x), torch.full_like(x, 0), torch.full_like(x, 1)).sum()
    value = torch.where(torch.isnan(x), torch.full_like(x, 0), x).sum()
    return value / num

def confusion_matrix_torch(y_pred, y_true, num_classes):
    N = num_classes
    y = (N * y_true + y_pred).type(torch.long)
    y = torch.bincount(y)
    if len(y) < N * N:
        y = torch.cat((y, torch.zeros(N * N - len(y), dtype=torch.long).cuda()))
    y = y.reshape(N, N)
    return y

def compute_cm_batch_torch(y_pred, y_true, ignore_label, classes):
    batch_size = y_pred.shape[0]
    confusion_matrix = torch.zeros((classes, classes)).cuda()
    for i in range(batch_size):
        y_pred_curr = y_pred[i, :, :]
        y_true_curr = y_true[i, :, :]
        inds_to_calc = y_true_curr != ignore_label
        y_pred_curr = y_pred_curr[inds_to_calc]
        y_true_curr = y_true_curr[inds_to_calc]
        assert y_pred_curr.shape == y_true_curr.shape
        confusion_matrix += confusion_matrix_torch(y_pred_curr, y_true_curr, classes)
    return confusion_matrix

def compute_iou_torch(confusion_matrix):
    intersection = torch.diag(confusion_matrix)
    ground_truth_set = confusion_matrix.sum(dim=1)
    predicted_set = confusion_matrix.sum(dim=0)
    union = ground_truth_set + predicted_set - intersection
    iou = intersection / union.type(torch.float32)
    miou = nanmean_torch(iou)
    return iou, miou

def GeneratePyramid(image, num_scales, curr_scale, scale_factor, is_label=False):
    scales_pyramid = []
    if isinstance(image, Image.Image):
        for i in range(0, curr_scale + 1, 1):
            scale = math.pow(scale_factor, num_scales - i)
            curr_size = (np.ceil(scale * np.array(image.size))).astype(np.int)
            curr_scale_image = image.resize(curr_size, Image.BICUBIC if not is_label else Image.NEAREST)
            curr_scale_image = RGBImageToNumpy(curr_scale_image) if not is_label else ImageToNumpy(curr_scale_image)
            scales_pyramid.append(curr_scale_image)
    elif isinstance(image, torch.Tensor):
        for i in range(0, curr_scale + 1, 1):
            if is_label:
                scale = math.pow(scale_factor, num_scales - i)
            else:
                scale = math.pow(scale_factor, curr_scale - i)
            curr_scale_image = imresize_torch(image, scale, mode='nearest' if is_label else 'bicubic')
            curr_scale_image = curr_scale_image.squeeze(0) if is_label else curr_scale_image
            scales_pyramid.append(curr_scale_image)
    return scales_pyramid

def RGBImageToNumpy(im):
    im = ImageToNumpy(im)
    im = (im - 128.) / 128  # change from 0..255 to -1..1
    return im

def ImageToNumpy(im):
    im = np.asarray(im, np.float32)
    if len(im.shape) == 3:
        im = np.transpose(im, (2, 0, 1))
    return im

class runningScore(object):
    def __init__(self, n_classes):
        self.n_classes = n_classes
        self.confusion_matrix = np.zeros((n_classes, n_classes))

    def _fast_hist(self, label_true, label_pred, n_class):
        mask = (label_true >= 0) & (label_true < n_class)
        hist = np.bincount(
            n_class * label_true[mask].astype(int) + label_pred[mask],
            minlength=n_class ** 2,
            ).reshape(n_class, n_class)
        return hist

    def update(self, label_trues, label_preds):
        for lt, lp in zip(label_trues, label_preds):
            self.confusion_matrix += self._fast_hist(
                lt.flatten(), lp.flatten(), self.n_classes
            )

    def get_scores(self):
        """Returns accuracy score evaluation result.
            - overall accuracy
            - mean accuracy
            - mean IU
            - fwavacc
        """
        hist = self.confusion_matrix
        acc = np.diag(hist).sum() / hist.sum()
        acc_cls = np.diag(hist) / hist.sum(axis=1)
        acc_cls = np.nanmean(acc_cls)
        iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
        mean_iu = np.nanmean(iu)
        freq = hist.sum(axis=1) / hist.sum()
        fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
        cls_iu = dict(zip(range(self.n_classes), iu))

        return (
            {
                "Overall Acc: \t": acc,
                "Mean Acc : \t": acc_cls,
                "FreqW Acc : \t": fwavacc,
                "Mean IoU : \t": mean_iu,
            },
            cls_iu,
        )

    def reset(self):
        self.confusion_matrix = np.zeros((self.n_classes, self.n_classes))

def norm_image(im, norm_type='tanh_norm'):
    if norm_type == 'tanh_norm':
        out = (im + 1) / 2
    elif norm_type == 'general_norm':
        out = (im - im.min())
        out = out / out.max()
    else:
        raise NotImplemented()
    # assert torch.max(out) <= 1 and torch.min(out) >= 0
    torch.clamp(out, 0, 1)
    return out

def calculte_cs_validation_accuracy(semseg_net, target_val_loader, epoch_num, tb_path, device):
    from torch.utils.tensorboard import SummaryWriter
    semseg_net.eval()
    tb = SummaryWriter(tb_path)
    with torch.no_grad():
        running_metrics_val = runningScore(NUM_CLASSES)
        cm = torch.zeros((NUM_CLASSES, NUM_CLASSES)).cuda()
        for val_batch_num, (target_images, target_labels) in enumerate(target_val_loader):
            target_images = target_images.to(device)
            target_labels = target_labels.to(device)
            with torch.no_grad():
                pred_softs = semseg_net(target_images)
                pred_labels = torch.argmax(pred_softs, dim=1)
                cm += compute_cm_batch_torch(pred_labels, target_labels, IGNORE_LABEL, NUM_CLASSES)
                running_metrics_val.update(target_labels.cpu().numpy(), pred_labels.cpu().numpy())
                if val_batch_num == 0:
                    t = norm_image(target_images[0])
                    t_lbl = colorize_mask(target_labels[0])
                    pred_lbl = colorize_mask(pred_labels[0])
                    tb.add_image('Semseg/Validtaion/target', t, epoch_num)
                    tb.add_image('Semseg/Validtaion/target_label', t_lbl, epoch_num)
                    tb.add_image('Semseg/Validtaion/prediction_label', pred_lbl, epoch_num)
        iou, miou = compute_iou_torch(cm)

        # proda's calc:
        score, class_iou = running_metrics_val.get_scores()
        for k, v in score.items():
            print(k, v)

        for k, v in class_iou.items():
            print(k, v)

        running_metrics_val.reset()
    return iou, miou, cm


import os, math, sys
from collections import Counter

def get_vocabulary(item_class):
    class_vocabulary = []
    for filename in os.listdir('train/' + item_class):
        file = open(os.path.join('train/' + item_class, filename), encoding='latin-1')
        class_vocabulary += [word for line in file for word in line.split() if word.isalpha()]
    return class_vocabulary

def test(item_class):
    classifications = []
    for filename in os.listdir('test/' + item_class):
        file = open(os.path.join('test/' + item_class, filename), encoding='latin-1')
        # W ← EXTRACTTOKENSFROMDOC(V, d)
        tokens = [word for line in file for word in line.split() if word in all_vocab]

        # do score[c] ← log prior[c]
        score_ham = math.log(ham_prior)
        score_spam = math.log(spam_prior)

        for token in tokens:
            # do score[c] += log condprob[t][c]
            score_ham += math.log(ham_bayes_dict[token])
            score_spam += math.log(spam_bayes_dict[token])

        if score_ham > score_spam:
            classifications.append('ham')
        else:
            classifications.append('spam')
    return classifications

# Nc ← COUNTDOCSINCLASS(D, c)
spam_count = len(os.listdir('train/spam'))
ham_count = len(os.listdir('train/ham'))

# N ← COUNTDOCS(D)
total_doc_count = spam_count + ham_count

# prior[c] ← Nc/N

spam_prior = spam_count / total_doc_count
ham_prior = ham_count / total_doc_count

# textc ← CONCATENATETEXTOFALLDOCSINCLASS(D, c)

spam_vocab = get_vocabulary("spam")
ham_vocab = get_vocabulary("ham")

exclude_stopwords = sys.argv[1].split('=')[1]

# if the user wants to exclude stopwords
if exclude_stopwords == 'true':
    stopwords = [line.rstrip() for line in open('stopwords.txt')]
    spam_vocab = [word for word in spam_vocab if word not in stopwords]
    ham_vocab = [word for word in ham_vocab if word not in stopwords]

# V ← EXTRACTVOCABULARY(D)

all_vocab = spam_vocab + ham_vocab

# Tct ← COUNTTOKENSOFTERM(textc, t)

spam_vocab_count_dict = Counter(spam_vocab)
ham_vocab_count_dict = Counter(ham_vocab)

# do condprob[t][c] ← Tct+1/∑t′(Tct′+1)
spam_bayes_dict = { k: (spam_vocab_count_dict[k] + 1) / (len(spam_vocab) + len(spam_vocab_count_dict)) for k in all_vocab}
ham_bayes_dict = { k: (ham_vocab_count_dict[k] + 1) / (len(ham_vocab) + len(ham_vocab_count_dict)) for k in all_vocab}

spam_classifications = test('spam')
ham_classifications = test('ham')

spam_accuracy = spam_classifications.count('spam') / len(spam_classifications)
ham_accuracy = ham_classifications.count('ham') / len(ham_classifications)

spam_test_files_len = len(os.listdir('test/spam'))
ham_test_files_len = len(os.listdir('test/ham'))

spam_test_ratio = spam_test_files_len / (spam_test_files_len + ham_test_files_len)
ham_test_ratio = ham_test_files_len / (spam_test_files_len + ham_test_files_len)

total_accuracy = spam_accuracy * spam_test_ratio + ham_accuracy * ham_test_ratio

print("spam percent classified accurately: " + str(spam_accuracy))
print("ham percent classified accurately: " + str(ham_accuracy))

print("total accuracy: " + str(total_accuracy) )


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 16 17:57:13 2019

@author: jcunanan
"""
#######################################
#      Problem  Description
#Bob the Adventurer is one step away from solving the mystery of an ancient Mayan tomb.
#He just approched the secret chamber where the secret Mayan scriptures are locked in a chest.
#There are N ancient statues in the room.
#After long thought, Bob figured out that in order to open the treasure chest
#he needs to stand in the middle of the room and hit every statue with a laser ray at the same time.
#Bob is a highly experienced adventurer, so setting multiple laser rays at the same time is not a problem for him.
#Moreover, every ray that he creates is perfectly straight and never changes direction at all.

#The middle of the room, where Bob is standing, has coordinates (0, 0).
#Every statue is located at some point with coordinates (x, y).
#Each statue is made of pure glass, so that if any ray hits it, it does not stop,
#but goes through the statue and continues beyond in the same, unchanged direction.
#Bob wonders how he can hit every ancient statue in the room using the fewest rays possible.

#Assume that the following declarations are given:

#class Point2D { public int x; public int y; }

#Write a function

#class Solution { public int solution(Point2D[] A); }

#that, given an array of points A, representing the locations of the statues,
#returns the minimal number of rays that Bob must set in order to hit every statue in the room.

#For example, given an array A

#A[0].x = -1 A[0].y = -2 (statue 0) A[1].x = 1 A[1].y = 2 (statue 1) A[2].x = 2 A[2].y = 4 (statue 2) A[3].x = -3 A[3].y = 2 (statue 3) A[4].x = 2 A[4].y = -2 (statue 4)
#your function should return 4.

 #https://i.stack.imgur.com/ad5gc.png

#As is shown in the image, it is possible to create four rays in such a way that:

#the first will hit statue 0;
#the second will hit statues 1 and 2;
#the third will hit statue 3;
#the fourth will hit statue 4.
#Assume that:

#N is an integer within the range [1..100,000];
#the coordinates of each point in array A are integers within the range [−1,000,000,000..1,000,000,000];
#the elements of A are all distinct;
#Array A does not contain point (0,0).
#Complexity:

#expected worst-case time complexity is O(N);
#expected worst-case space complexity is O(N*log(N)), beyond input storage (not counting the storage required for input arguments).

#from extratypes import Point2D  # library with types used in the task
from sympy import Point2D

def solution(A):
    # write your code in Python 3.6
    N = len(A)
    slopeE = []
    slopeW = []
    sN = 0
    sS = 0

    for i in range(N):

        if Point2D(A[i]).x == 0 and Point2D(A[i]).y > 0:
            sN = 1

        elif Point2D(A[i]).x == 0 and Point2D(A[i]).y < 0:
            sS = 1

        if Point2D(A[i]).x >0 :
            if Point2D(A[i]).y / Point2D(A[i]).x not in slopeE:
                slopeE.append(Point2D(A[i]).y / Point2D(A[i]).x)

        if Point2D(A[i]).x < 0:
            if Point2D(A[i]).y / Point2D(A[i]).x not in slopeW:
                slopeW.append(Point2D(A[i]).y / Point2D(A[i]).x)

    return len(slopeE)+len(slopeW)+sN+sS


import numpy as np
import os
import datetime as dt

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func

from flask import Flask, jsonify

engine = create_engine("sqlite:///Resources/hawaii.sqlite")

# reflect an existing database into a new model
Base = automap_base()
Base.prepare(engine, reflect=True)

Measurement = Base.classes.measurement
Station = Base.classes.station

app = Flask(__name__)

@app.route("/")
def welcome():
    """List all available api routes."""
    return \
    (
        f"Available Routes:
"
        f"
"
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/start
"
        f"/api/v1.0/start/end
"
        f"
"
        f"NOTE: please format dates start and end as YYYY-MM-DD"
    )

@app.route("/api/v1.0/precipitation")
def precipitation():
    """Return amount of precipitation over the past year"""

    # initialize session
    session = Session(bind=engine)
    # Calculate the date 1 year ago from the last data point in the database
    maxdate = session.query(func.max(Measurement.date))
    mdsplit = maxdate.scalar().split("-")
    prevyear = dt.date(int(mdsplit[0]), int(mdsplit[1]), int(mdsplit[2])) - dt.timedelta(days=365)

    # Perform a query to retrieve the data and precipitation scores
    prevyearmeasurements = session.query(Measurement.date, Measurement.prcp).filter(Measurement.date>prevyear)

    # store query results in dictionary
    precipitation = {row[0]: row[1] for row in prevyearmeasurements}

    # close the session to end the communication with the database
    session.close()

    return jsonify(precipitation)

@app.route("/api/v1.0/stations")
def stations():
    """Return a list of all stations"""

    session = Session(bind=engine)

    # Query all stations
    stations = session.query(Station).all()

    # close the session to end the communication with the database
    session.close()

    # store query results in dictionary
    all_stations = []
    for station in stations:
        station_dict = {}
        station_dict["id"] = station.id
        station_dict["station"] = station.station
        station_dict["name"] = station.name
        station_dict["latitude"] = station.latitude
        station_dict["longitude"] = station.longitude
        station_dict["elevation"] = station.elevation
        all_stations.append(station_dict)

    return jsonify(all_stations)

@app.route("/api/v1.0/tobs")
def tobs():
    """Return a list of all temperature observatons for last year of measurements"""

    session = Session(bind=engine)

    maxdate = session.query(func.max(Measurement.date))
    mdsplit = maxdate.scalar().split("-")
    prevyear = dt.date(int(mdsplit[0]), int(mdsplit[1]), int(mdsplit[2])) - dt.timedelta(days=365)

    # query previous year's measurements
    prevyearmeasurements = session.query(Measurement.station, Measurement.tobs, Station.name).\
        filter(Measurement.station == Station.station, Measurement.date>prevyear)

    # close the session to end the communication with the database
    session.close()

    # store query results in dictionary
    all_temps = []
    for tobs in prevyearmeasurements:
        temp_dict = {}
        temp_dict["station"] = tobs[0]
        temp_dict["temp"] = tobs[1]
        temp_dict["name"] = tobs[2]
        all_temps.append(temp_dict)

    return jsonify(all_temps)

@app.route("/api/v1.0/<start>")
def tempstart(start):
    """Return temperature statistics for dates greater than and equal to the start date"""

    session = Session(bind=engine)

    # the start date variable is expected to be in the format YYYY-MM-DD. split it into parts, cast those
    # parts as ints as arguments to a date object for use in the query
    ssplit = start.split("-")
    datestart = dt.date(int(ssplit[0]),int(ssplit[1]),int(ssplit[2]))

    # query tmax, tmin, and tavg from the given start date
    tmax = session.query(func.max(Measurement.tobs)).filter(Measurement.date>=datestart).scalar()
    tmin = session.query(func.min(Measurement.tobs)).filter(Measurement.date>=datestart).scalar()
    tavg = round(session.query(func.avg(Measurement.tobs)).filter(Measurement.date>=datestart).scalar(), 1)

    # close the session to end the communication with the database
    session.close()

    return \
    (
        f"Temperature statistics starting from {start}:
"
        "
"
        f"Maximum Temperature: {tmax} °F
"
        f"Minimum Temperature: {tmin} °F
"
        f"Average Temperature: {tavg} °F"
    )

@app.route("/api/v1.0/<start>/<end>")
def tempstartend(start, end):
    """Return temperature statistics for dates between the start and end date inclusive"""

    session = Session(bind=engine)

    # turn the date variables into date objects for querying
    ssplit = start.split("-")
    datestart = dt.date(int(ssplit[0]),int(ssplit[1]),int(ssplit[2]))
    esplit = end.split("-")
    dateend = dt.date(int(esplit[0]),int(esplit[1]),int(esplit[2]))

    # query tmax, tmin, and tavg from the given start date
    tmax = session.query(func.max(Measurement.tobs)).filter(Measurement.date>=datestart, \
        Measurement.date<=dateend).scalar()
    tmin = session.query(func.min(Measurement.tobs)).filter(Measurement.date>=datestart, \
        Measurement.date<=dateend).scalar()
    tavg = round(session.query(func.avg(Measurement.tobs)).filter(Measurement.date>=datestart, \
        Measurement.date<=dateend).scalar(),1)

    # close the session to end the communication with the database
    session.close()

    return \
    (
        f"Temperature statistics starting from {start} and ending {end}:
"
        "
"
        f"Maximum Temperature: {tmax} °F
"
        f"Minimum Temperature: {tmin} °F
"
        f"Average Temperature: {tavg} °F"
    )

if __name__ == "__main__":
    app.run(debug=True)


# Сохраяем ИД пользователя/ИД ответа/значение ответа
# Собираем ответы - делаем словарь ответов и возвращаем
# Возвращаем список словарей-ответов
import os

def start_file():
    f = open('users_data.csv','w')
    f.close()

def insert_data(id,question_id,question_value):
    f = open('users_data.csv','a')
    file_out = f"{id};{question_id};{question_value};"
    f.write(file_out+'\n')
    f.close()
    print("Heelo")

# Собирем словарь
def check_questions():
    f = open('users_data.csv','r')
    # d = dict()
    key_list = []
    user_list = []
    for line in f:
        elements = line.split(';')
        elements.pop()
        key_list.append(elements[1])
        user_list.append(elements[0])
    f.close()
    key_set = set(key_list)
    user_set = set(user_list)
    user_lst = list(user_set)
    key_lst = list(key_set)
    user_lst.sort()
    key_lst.sort()

    dict_list = []
    for usr in user_lst:

        data_list = ['user']
        data_list.extend(key_lst)
        total_dict = dict.fromkeys(data_list,None)
        total_dict.update({"user":usr})
        dict_list.append(total_dict)
    f = open('users_data.csv','r')
    for line in f:
        elements = line.split(';')
        elements.pop()
        indx = user_lst.index(elements[0])
        dict_list[indx].update({elements[1]:elements[2]})

    print(dict_list)
    return dict_list

# total_dict = dict.fromkeys(cat_list_value(),0)

# fin_list = []
# f = open('costs_list.txt','r')
# for line in f:
#     elements = line.split(' ')
#     lst = [elements[0][1:],int(elements[1])]
#     fin_list.append(lst)

# for lst in fin_list:
#     total_dict[lst[0]] = total_dict[lst[0]] + lst[1]

# print(total_dict)

# insert_data(222,"start","1")
# insert_data(222,"quiz","0")
# insert_data(222,"finish","1")
# insert_data(222,"go","1")

# check_questions()
f = open('users_data.csv','r')
# список участников

numbers = []
for line in f:
    elements = line.split(";")
    numbers.append(elements[0])
nums = set(numbers)
# print(nums)
numbers = list(nums)

lst = []

for n in numbers:
    lst.append([])

print(numbers)
f.close()
f = open('users_data.csv','r')
question_code = ["quest_start_video","quest_present_lesson","quest_present_teacher","quest_present_company","quest_student_meet","quest_about_IT","quest_about_sphere","quest_practic_block","quest_grow_stairs","quest_softs","quest_projects","quest_methodiks","quest_price","quest_discount","quest_year","quest_end_slide"]
id = '679521347'
reg_status = []
for i in range(len(question_code)):
    reg_status.append(0)
print(reg_status)
d = dict.fromkeys(question_code,None)
dd = {"id":"414514928","value":d}
listdd = []
listdd.append(dd)
listdd.append(dd)

# Функция получат id и возвращает строку reg_status

def id_reg_status(id):
    # Читаем список типов данных
    f = open('users_data_types.csv','r')
    for line in f:
        question_code = line.split(';')
    question_code.pop()
    f.close()
    # Читаем - есть ли элемент
    f = open('users_data.csv','r')
    reg_status = []
    for i in range(len(question_code)):
        reg_status.append(0)
    for line in f:
        elements = line.split(";")
        if elements[0] == id:
            for i in range(len(question_code)):
                if elements[1]==question_code[i]:
                    reg_status[i] = 1
    f.close()
    return reg_status

for line in f:
    elements = line.split(";")
    for i in range(len(numbers)):
        if elements[0] ==numbers[i]:
            lst[i].append([elements[1],elements[2]])
print(id_reg_status('394735340'))
print(listdd[1])
f.close()


from keras.models import Sequential, load_model
from keras.layers import Embedding, Conv1D, MaxPooling1D, LSTM, GRU, Dense
from keras.layers.wrappers import Bidirectional

y_idx2word=8

def run_classifier(x_train_seq,x_test_seq,y_train_one_hot,y_test_one_hot):
    model = Sequential()
    print("-------------------------------------------building model----------------------------------------")
    # embedding layer
    model.add(Embedding(7000,
                        300,
                        input_length=20))
    # RNN layers
    for d in range(1):
        model.add(Bidirectional(GRU(units=100,
                                    dropout=0.2,
                                    recurrent_dropout=0.2,
                                    return_sequences=True)))

    model.add(Bidirectional(GRU(units=100,
                                dropout=0.2,
                                recurrent_dropout=0.2,
                                return_sequences=False)))

    # fully-connected output layer
    model.add(Dense(y_idx2word, activation='softmax'))

    # ---- COMPILE THE MODEL ----

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    model.summary()
    print("-------------------------------------------training model----------------------------------------")
    history = model.fit(x_train_seq,
                        y_train_one_hot,
                        batch_size=64,
                        epochs=15, validation_data=(x_test_seq, y_test_one_hot))
    print("-------------------------------------------model trained------------------------------------------")
    acc = model.evaluate(x_test_seq,y_test_one_hot)
    print("accuracy is:",acc[1]*100)
    return acc


from token_daniel import token

cabecalho_api_github = {
    'Content-Type': 'application/json',
    'Authorization': f"Bearer {token}"
}

colunas_node = ['nome', 'linguagem', 'stargazes', 'watchers', 'data_criacao', 'forks', 'url', 'releases']

colunas_repo_loc = ['nome', 'loc']


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
        curr=head
        sum1=0
        dummy=ListNode(0)
        dum=dummy
        while(curr):
            if(curr.val==0):
                dum.next=ListNode(sum1)
                dum=dum.next
                sum1=0
            else:
                sum1+=curr.val
            curr=curr.next
        return dummy.next.next


# -*- coding: utf-8 -*-
from openerp import api, fields, models
from openerp import tools

class OperatingUnitView(models.Model):
    # This class is solely for PABI2
    _name = 'operating.unit.view'
    _auto = False

    name = fields.Char(
        string='Name',
    )
    code = fields.Char(
        string='Code',
    )
    active = fields.Boolean(
        string='Active',
    )

    def init(self, cr):
        tools.drop_view_if_exists(cr, self._table)
        cr.execute("""CREATE or REPLACE VIEW %s as (%s)""" %
                   (self._table,
                    "select * from operating_unit",))


"""
Code adapted from https://github.com/uvavision/Double-Hard-Debias/blob/master/eval.py
"""
import glob
import os
import numpy as np
from sklearn.utils import Bunch
from sklearn.cluster import AgglomerativeClustering, KMeans
from six import iteritems

def evaluate_categorization(word_vectors, X, y, method='kmeans', seed=None):
    """
    Evaluate embeddings on categorization task.
    Parameters
    ----------
    w: dict
      Embeddings to test.
    X: vector, shape: (n_samples, )
      Vector of words.
    y: vector, shape: (n_samples, )
      Vector of cluster assignments.
    """

    # Get all word embeddings into a matrix
    vectors = []
    for word in word_vectors:
        vectors.append(word_vectors[word])

    # Mean of all embeddings
    mean_vector = np.mean(vectors, axis=0, keepdims=True)

    w = word_vectors

    new_x = []
    new_y = []
    exist_cnt = 0
    for idx, word in enumerate(X.flatten()):
        if word in w:
            new_x.append(X[idx])
            new_y.append(y[idx])
            exist_cnt += 1

    # Number of words in BLESS that also exists in our vocabulary
    print('exist {} in {}'.format(exist_cnt, len(X)))

    X = np.array(new_x)
    y = np.array(new_y)

    # Put all the words that were in both BLESS and our vocab into a matrix
    words = np.vstack([w.get(word, mean_vector) for word in X.flatten()])
    ids = np.random.RandomState(seed).choice(range(len(X)), len(X), replace=False)

    # Evaluate clustering on several hyperparameters of AgglomerativeClustering and
    # KMeans
    best_purity = 0

    if method == "all" or method == "agglomerative":
        best_purity = calculate_purity(y[ids], AgglomerativeClustering(n_clusters=len(set(y)),
                                                                       affinity="euclidean",
                                                                       linkage="ward").fit_predict(words[ids]))
        for affinity in ["cosine", "euclidean"]:
            for linkage in ["average", "complete"]:
                purity = calculate_purity(y[ids], AgglomerativeClustering(n_clusters=len(set(y)),
                                                                          affinity=affinity,
                                                                          linkage=linkage).fit_predict(words[ids]))
                best_purity = max(best_purity, purity)

    if method == "all" or method == "kmeans":
        purity = calculate_purity(y[ids], KMeans(random_state=seed, n_init=10, n_clusters=len(set(y))).
                                  fit_predict(words[ids]))
        best_purity = max(purity, best_purity)

    return best_purity

def calculate_purity(y_true, y_pred, verbose=False):
    """
    Calculate purity for given true and predicted cluster labels.
    Parameters
    ----------
    y_true: array, shape: (n_samples, 1)
      True cluster labels
    y_pred: array, shape: (n_samples, 1)
      Cluster assingment.
    Returns
    -------
    purity: float
      Calculated purity.
    """
    assert len(y_true) == len(y_pred)
    true_clusters = np.zeros(
        shape=(len(set(y_true)), len(y_true)))  # creates sparse array (categories, words both in bench and vocab)
    pred_clusters = np.zeros_like(true_clusters)  # creates sparse array (categories, words both in bench and vocab)
    for id, cl in enumerate(set(y_true)):
        if verbose:
            print("true:", id)
        true_clusters[id] = (y_true == cl).astype("int")  # Everwhere the label is of a certain class, put a 1
    for id, cl in enumerate(set(y_pred)):
        if verbose:
            print("pred:", id)
        pred_clusters[id] = (y_pred == cl).astype("int")  # Everwhere the label is in a certain cluster, put a 1

    # For each clust in the prediction, find the true cluster that has the MOST overlap
    # Sum up the number of words that overlap between the pred and true cluster that has the most overlap
    # Divide this by (the number of words both in bench and vocab)
    M = pred_clusters.dot(true_clusters.T)
    return 1. / len(y_true) * np.sum(np.max(M, axis=1))

def evaluate_cate(wv_dict, benchmarks, method='all', seed=None):
    categorization_tasks = benchmarks
    categorization_results = {}

    # Calculate results using helper function
    for name, data in iteritems(categorization_tasks):
        print("Sample data from {}, num of samples: {} : \"{}\" is assigned class {}".format(
            name, len(data.X), data.X[0], data.y[0]))
        categorization_results[name] = evaluate_categorization(wv_dict, data.X, data.y, method=method, seed=None)
        print("Cluster purity on {} {}".format(name, categorization_results[name]))

def create_bunches(bench_paths):
    output = {}
    for path in bench_paths:
        files = glob.glob(os.path.join(path, "*.txt"))
        name = os.path.basename(path)
        if name == 'EN-BATTIG':
            sep = ","
        else:
            sep = " "

        X = []
        y = []
        names = []
        for cluster_id, file_name in enumerate(files):
            with open(file_name) as f:
                lines = f.read().splitlines()[:]
                X += [l.split(sep) for l in lines]
                y += [os.path.basename(file_name).split(".")[0]] * len(lines)
        output[name] = Bunch(X=np.array(X, dtype="object"), y=np.array(y).astype("object"))

        if sep == ",":
            data = output[name]
            output[name] = Bunch(X=data.X[:, 0], y=data.y, freq=data.X[:, 1], frequency=data.X[:, 2], rank=data.X[:, 3],
                                 rfreq=data.X[:, 4])

    return output


def getadd():
    import requests
    import json
    import getip
    ip = getip.get()

    send_url = f'http://api.ipstack.com/{ip}?access_key=7cf3582503675544e752924eb3142e79&format=1'
    r = requests.get(send_url)
    j = json.loads(r.text)
    lat = str(j['latitude'])
    lon = str(j['longitude'])
    print(lat + ", " + lon)
    return j['city'] + ", " + j["country_code"]

def fame__():
    from config import db
    from flask import session
    views = 0
    user = "secret" #session.get('logged_in')
    print(user)
    rating = 0
    stmt = db.query("SELECT * FROM views WHERE friend_id = %s", (user,))

    for row in stmt:
        if row['to_user'] == user:
            if row['notification'].find("viewed") == 0:
                views += 1
    if views <= 5:
        rating += 1
    elif 5 < views < 50:
        for i in range(5, 50, 5):
            rating += i
    if views >= 50:
        rating = 10
    else:
        rating = 0
    print("up to here")
    db.query("UPDATE fame_rating SET rating = %s WHERE username = %s", (rating, user,))


# author: sunshine
# datetime:2021/8/5 下午3:17
import torch
import torch.nn as nn
from transformers import BertModel

class SMPNet(nn.Module):
    def __init__(self, args, num_class):
        super(SMPNet, self).__init__()
        self.bert = BertModel.from_pretrained(args.bert_path)
        self.fc1 = nn.Sequential(
            nn.Dropout(0.1),
            nn.Linear(768 * 4, 128),
            nn.ReLU(),
            nn.Linear(128, num_class)
        )
        self.fc2 = nn.Sequential(
            nn.Dropout(0.1),
            nn.Linear(768 * 4, 128),
            nn.ReLU(),
            nn.Linear(128, num_class)
        )

    def forward(self, x1=None, x2=None):
        out = ()
        if x1 is not None:
            x1 = self.bert(*x1, output_hidden_states=True)
            x1_hidden = x1.hidden_states
            concat1 = torch.cat([x1_hidden[-1][:, 0], x1_hidden[-2][:, 0], x1_hidden[-3][:, 0], x1.pooler_output],
                                dim=-1)
            out1 = self.fc1(concat1)
            out = out + (out1,)
        if x2 is not None:
            x2 = self.bert(*x2, output_hidden_states=True)
            x2_hidden = x2.hidden_states
            concat2 = torch.cat([x2_hidden[-1][:, 0], x2_hidden[-2][:, 0], x2_hidden[-3][:, 0], x2.pooler_output],
                                dim=-1)
            out2 = self.fc2(concat2)
            out = out + (out2,)
        return out


import keyboard
import time
import random
from ctypes import windll, wintypes, byref
from functools import reduce

from cafe_coding_download import enable

enable()

f = open('cafe_coding_download.py', 'r', encoding='UTF-8')

data = f.read()
f.close()
i = 0
s = ''

while True:
    if keyboard.read_key():
        s += data[i]

        print('\r' + s, end='')

        if data[i] == '\n':
            s = ''
            #改行後の空白は一気に表示
            while data[i+1] == ' ':
                s += data[i+1]
                i+=1
            print('\r' + s, end='')

        i+=1

    # すべて文字を表示しきったら初期化
    if i == len(data):
        i = 0
        s = ''
        print()
        print()


import networkx as nx
from random import randint
from math import exp
import numpy as np
from typing import List, Dict, FrozenSet, Iterator, Tuple
from pydantic import BaseModel
from .graph_utils import (
    list_subsets_of_given_size,
    pairs_of_sets,
)

class SubtreeData(BaseModel):
    agg_root: int
    agg_size: int
    att_root: int
    att_size: int

class SubtreeAnalizer():
    def __init__(self,
                 root: int,
                 size: int,
                 children_count: List[int],
                 bottom_up_order: List[int],
                 tree: nx.Graph,
                 graph: nx.Graph,
                 graph_colors: List[int],
                 colors: FrozenSet,
                 memory_array: Dict[FrozenSet, np.ndarray]
                 ):
        self._root = root
        self._size = size
        self._children_count = children_count
        self._bottom_up_order = bottom_up_order
        self._tree = tree
        self._graph = graph
        self._graph_colors = graph_colors
        self._colors = colors
        self._memory_array = memory_array
        self._mapping_restore: Dict[Tuple[FrozenSet[int], int, int],
                                    Tuple[Tuple[FrozenSet[int], int, int],
                                          Tuple[FrozenSet[int], int, int]]
                                    ] = {}

    def find_subtree(self):
        for con_data in self._subtree_connections_asc():
            agg_subt, agg_size, att_subt, att_size = con_data
            for colors_data in self._subtree_colorings(
                agg_size[1], att_size[1]
            ):
                cols_subset, agg_cols, att_cols = colors_data
                self._check_mappings(
                    cols_subset, agg_subt[1], agg_cols, att_subt[1], att_cols
                )
        try:
            g_root = list(self._memory_array[self._colors][0, :]).index(True)
            return self._restore_mapping(g_root)
        except (ValueError, KeyError):
            return None

    def _restore_mapping(self, g_root):
        mapping = [0] * len(self._tree)
        self._traverse(self._colors, mapping, g_root, 0)
        return mapping

    def _traverse(self, colors, mapping, g_root, t_root):
        mapping[t_root] = g_root
        if len(colors) == 1:
            return

        (col1, tr1, gr1), (col2, tr2, gr2) = self._mapping_restore[
                                                (colors, t_root, g_root)
                                             ]
        self._traverse(col1, mapping, gr1, tr1)
        self._traverse(col2, mapping, gr2, tr2)

    def _check_mappings(self, cols_subset, agg_subt, agg_cols, att_subt,
                        att_cols):
        for v in self._graph:
            for vn in self._graph.neighbors(v):
                is_first_colorable = self._memory_array[agg_cols][agg_subt, v]
                is_sec_colorable = self._memory_array[att_cols][att_subt, vn]
                if is_first_colorable and is_sec_colorable:
                    self._fill_mappings(agg_cols, agg_subt, v,
                                        att_cols, att_subt, vn,
                                        cols_subset)

    def _fill_mappings(self, agg_cols, agg_subt, v,
                       att_cols, att_subt, vn,
                       cols_subset):
        if cols_subset not in self._memory_array:

            match_array = np.zeros((len(self._tree), len(self._graph)),
                                   dtype=bool)
            self._memory_array[cols_subset] = match_array
        self._memory_array[cols_subset][agg_subt, v] = True
        connected_subt = (cols_subset, agg_subt, v)
        if connected_subt not in self._mapping_restore:
            last_agg = (agg_cols, agg_subt, v)
            last_att = (att_cols, att_subt, vn)
            self._mapping_restore[connected_subt] = (last_agg, last_att)

    def _subtree_connections_asc(self) -> Iterator[SubtreeData]:
        for agg_subt in self._bottom_up_order:
            agg_subt_size = 1
            for att_subt in self._get_direct_children(agg_subt):
                att_subt_size = self._children_count[att_subt] + 1
                yield SubtreeData(
                                  agg_root=agg_subt,
                                  agg_size=agg_subt_size,
                                  att_root=att_subt,
                                  att_size=att_subt_size
                                  )
                agg_subt_size = agg_subt_size + att_subt_size

    def _get_direct_children(self, node: int) -> Iterator[int]:
        for v in self._tree.neighbors(node):
            buo = self._bottom_up_order
            if buo.index(node) > buo.index(v):
                yield v

    def _subtree_colorings(self, agg_size, att_size):
        for cols_subset, agg_cols, att_cols in self._colors_subset_iterator(
                agg_size, att_size
        ):
            is_first_possible = agg_cols in self._memory_array
            is_second_possible = att_cols in self._memory_array
            if is_first_possible and is_second_possible:
                yield cols_subset, agg_cols, att_cols

    def _colors_subset_iterator(self, agg_size, att_size):
        avail_cols = agg_size + att_size
        for cols_subset in list_subsets_of_given_size(
            self._colors, avail_cols
        ):
            for agg_cols, att_cols in pairs_of_sets(
                cols_subset, agg_size, att_size
            ):
                yield cols_subset, agg_cols, att_cols

class SubtreeAnalizerFactory():
    def __init__(self, tree: nx.Graph, graph: nx.Graph):
        self._tree = tree
        self._graph = graph
        self._size = len(self._tree)
        self._nodes_order = list(self._bottom_up_order(0))
        self._children_count = self._count_all_children(self._nodes_order, 0)

    def create(self, graph_colors: List[int]) -> SubtreeAnalizer:
        if (len(graph_colors) != len(self._graph)):
            raise ValueError(
                "Graph colors mapping does not cover all graph vertices")
        colors = frozenset(graph_colors)
        mapping_restore = self._initialize_memory(graph_colors)

        return SubtreeAnalizer(
            0, self._size, self._children_count, self._nodes_order,
            self._tree, self._graph, graph_colors, colors, mapping_restore
        )

    def _bottom_up_order(self, root: int) -> Iterator[int]:
        top_down = nx.bfs_tree(self._tree, source=root)
        for g in reversed(list(top_down)):
            yield g

    def _count_all_children(self, nodes_order: List[int],
                            root: int) -> List[int]:
        """
        returns list of counts of descendants for every node in tree
        by the direction given by root node
        """
        nodes_order = list(nodes_order)
        children_count = [0]*len(self._tree)
        for v in nodes_order:
            vo = v

            count = 0
            for nv in self._tree.neighbors(vo):
                if nodes_order.index(nv) < nodes_order.index(vo):
                    count = count + 1 + children_count[nv]
            children_count[vo] = count

        return children_count

    def _initialize_memory(self, graph_colors) -> Dict[FrozenSet, np.ndarray]:
        iso_subtree = {}
        graph_size = len(self._graph)
        tree_size = len(self._tree)
        colors = set(graph_colors)
        for c in colors:
            match_array = np.zeros((tree_size, graph_size),
                                   dtype=bool)
            iso_subtree[frozenset([c])] = match_array
        for c in colors:
            for t in self._tree.nodes():
                for v in self._graph.nodes:
                    if graph_colors[v] == c:
                        iso_subtree[frozenset([c])][t, v] = True
        return iso_subtree

def color_coding_subtree(tree, graph):
    k = len(tree)
    attempts = int(exp(k+2))
    mapping = None
    random_coloring = None
    analizer_factory = SubtreeAnalizerFactory(tree, graph)
    for i in range(attempts):
        print(f"Attempt nr {i}", end='\r')
        random_coloring = [randint(0, k) for i in range(len(graph))]
        analizer = analizer_factory.create(random_coloring)
        mapping = analizer.find_subtree()
        if mapping is not None:
            break
    return mapping, random_coloring


from django.shortcuts import render
from .forms import UploadTransactionFileForm
from .models import TransactionFIles, Transactions
from .serializers import TransactionnsSerializer
from .utils.mixins import TransactionMixin
from rest_framework.generics import ListCreateAPIView
from rest_framework.views import APIView, Response, status
from django.http import HttpResponseRedirect
import os
import ipdb

def home_view(request):
    context = {}

    if request.POST:
        form = UploadTransactionFileForm(request.POST, request.FILES)

        if form.is_valid():
            file_upload = request.FILES["transaction_file"].read()
            file_text = file_upload.decode("UTF-8")
            file_arr = file_text.split("\n")
            # ipdb.set_trace()

            # for line in file_arr:
            #     ipdb.set_trace()

            data = []

            for line in file_arr:
                obj = {
                    "type": line[0],
                    "date": f"{line[1:5]}-{line[5:7]}-{line[7:9]}",
                    "value": line[9:19],
                    "cpf": line[19:30],
                    "credit_card": line[30:42],
                    "attendance_hour": f"{line[42:44]}:{line[44:46]}:{line[46:48]}",
                    "shop_owner": line[48:62],
                    "shop_name": line[62:80],
                }
                data.append(obj)

            # ipdb.set_trace()
            serializer = TransactionnsSerializer(data=data, many=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

            context["data"] = data

            return render(request, "table.html", context)

        else:
            form = UploadTransactionFileForm()

            context["form"] = form
            return render(request, "home.html", context)

    context["form"] = UploadTransactionFileForm()
    return render(request, "home.html", context)

# def home_view(request):
#     context = {}

#     if request.POST:
#         form = UploadTransactionFileForm(request.POST, request.FILES)

#         if form.is_valid():
#             upload = request.FILES["transaction_file"]

#             teste = TransactionFIles(upload=upload)
#             teste.save()

#             ipdb.set_trace()
#         else:
#             form = UploadTransactionFileForm()

#             context["form"] = form
#             return render(request, "home.html", context)

#     context["form"] = UploadTransactionFileForm()
#     return render(request, "home.html", context)

class TransactionsView(APIView):
    def post(self, request):

        upload_file = request.FILES["transaction_file"].read()
        upload_txt = upload_file.decode("UTF-8")
        upload_arr = upload_txt.split("\n")

        for i in upload_arr:
            obj = {
                "type": i[0],
                "date": f"{i[1:5]} - {i[5:7]} - {i[7:9]}",
                "value": int(i[9:17].i[17:19]),
                "cpf": i[19:30],
                "credit_card": i[30:42],
                "hour": f"{i[42:44]:{i[44:46]:i[46:48]}}",
                "shop_owner": i[48:62],
                "shop_name": i[62:80],
            }

        serializer = TransactionnsSerializer(data=obj)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(status.HTTP_201_CREATED)


from dataclasses import dataclass, field

# dataclass ja cria para nós o init, repr e eq
@dataclass(init=True)
class Pessoa:

    _nome: str
    _idade: float
    enderecos: list[str] = field(default_factory=list)

    def __post_init__(self):
        print("depois do init")

    @property
    def nome(self):
        return self._nome

if __name__ == "__main__":
    p1 = Pessoa('micael', 26)
    print(p1)


import queue
from threading import Thread
import numpy as np
from transformers import *
from openie import StanfordOpenIE
from utility.utility import *
#from bert_serving.client import BertClient
from rouge import Rouge
from stanfordcorenlp import StanfordCoreNLP
import pickle
from data.raw_data_loader import *
import argparse
'''
nlp = StanfordCoreNLP('/home/ziqiang/stanfordnlp_resources/stanford-corenlp-full-2018-10-05')
bc = BertClient(ip='localhost')
client = StanfordOpenIE()
rougex = Rouge()
'''
g_b=0

import threading

class threadsafe_generator:
    def __init__(self, it):
        self.it = it
        self.lock = threading.Lock()

    def __iter__(self):
        return self

    def __next__(self):
        with self.lock:
            return self.it.__next__()

class Example(object):
  """Class representing a train/val/test example for text summarization."""

  def __init__(self, article, abstract, tokenizer, rougex, nlp):

    """
    Initializes the Example, performing tokenization and truncation to produce the encoder, decoder and target sequences, which are stored in self.
    Args:
      article: source text; list of strings. each token is separated by a single space.
      abstract_sentences: list of strings, one per abstract sentence. In each sentence, each token is separated by a single space.
      vocab: Vocabulary object
      hps: hyperparameters
    """
    self.rougex=rougex
    self.nlp=nlp
    self.tokenizer=tokenizer
    article=article[:60]
    self.article=article
    self.abstract=abstract
    # Process the article
    self.article_fact=[]
    self.article_sent=[]
    self.article_fact_tag=[]
    for count,sent in enumerate(article):

        self.article_sent.append(self.tokenizer.encode(sent))
        sent=sent.strip(',')
        sent=sent.strip(':')

        sentfact=sent_split(sent,self.nlp)

        sentfact_file=[]
        for i in sentfact:
            if word_len(i) >50:
                ii=i.split(' ')
                ii=ii[0:50]
                sentfact_file.append(' '.join(ii))
                continue
            if len(i) >= 20:
                sentfact_file.append(i)

        self.article_fact_tag.append(len(sentfact_file))
        self.article_fact+=sentfact_file
    self.article_id=[]
    for fact in self.article_fact:
        self.article_id.append(self.tokenizer.encode(fact,add_special_tokens=False))
    self.article_len = len(self.article_id) # store the number of sentences of the article

    # Process the abstract
    self.original_abstract=[]
    self.abstract_fact=[]
    self.abstract_fact_all=[]
    for sent in abstract:

        self.original_abstract.append(self.tokenizer.encode(sent))

        if word_len(sent) > 20:
            sent=sent.strip(',')
            sent=sent.strip(':')
            sentfact=sent_split(sent,self.nlp)
        else:
            sentfact=[sent]

        self.abstract_fact_all+=sentfact

    for i in self.abstract_fact_all:
        if word_len(i) >50:
            ii=i.split(' ')
            ii=ii[0:50]
            self.abstract_fact.append(' '.join(ii))
        elif len(i) < 15:
            continue
        else:
            self.abstract_fact.append(i)
    self.abstract_id=[]
    for fact in self.abstract_fact:
        self.abstract_id.append(self.tokenizer.encode(fact,add_special_tokens=False))

    self.abstract_len = len(self.abstract_id) # store the number of sentences of the article

    self.enc_fact=[]
    self.enc_sent=[]
    self.dec_fact=[]

    self.dec_label_bert=[]
    self.dec_label_rouge=[]
    self.dec_label_sent=[]

    self.grap_sim_bert=np.zeros((self.article_len, self.article_len), dtype=np.float16)
    self.grap_sim_rouge=np.zeros((self.article_len, self.article_len), dtype=np.float16)
    self.grap_entity=np.zeros((self.article_len, self.article_len), dtype=np.float16)
    self.grap_cosent=np.zeros((self.article_len, self.article_len), dtype=np.float16)
    self.grap_sent=np.zeros((len(self.article), len(self.article)), dtype=np.float16)
  def get_enc_fact(self, max_len):

    """Pad the encoder input sequence with pad_id up to max_len."""

    for i in self.article_id:
        if len(i) > max_len:
            self.enc_fact.append(i[0:max_len])
        else:
            self.enc_fact.append(i)

    for i in self.article_sent:
        if len(i) > max_len*2:
            self.enc_sent.append(i[0:max_len*2])
        else:
            self.enc_sent.append(i)

  def get_dec_fact(self, max_len):

    """Pad the encoder input sequence with pad_id up to max_len."""

    for i in self.abstract_id:
        if len(i) > max_len:
            self.dec_fact.append(i[0:max_len])
        else:
            self.dec_fact.append(i)

  def get_grap(self):

      """Get the sim bert graph """

      """Get the sim rouge graph """
      for i,facti in enumerate(self.article_fact):
          for j,factj in enumerate(self.article_fact):
              scores = self.rougex.get_scores(facti, factj)
              self.grap_sim_rouge[i][j]=(scores[0]['rouge-1']['f']+scores[0]['rouge-2']['f'])/2

      """Get the sim sent graph """
      for i,facti in enumerate(self.article):
          for j,factj in enumerate(self.article):

              scores = self.rougex.get_scores(facti, factj)
              self.grap_sent[i][j]=(scores[0]['rouge-1']['f']+scores[0]['rouge-2']['f'])/2

      """Get the entity graph"""

      """Get the co-sent graph"""
      now=0
      for i in self.article_fact_tag:
          for x in range(now+i)[now:now+i]:
              for y in range(now+i)[now:now+i]:
                  self.grap_cosent[x][y]=1
          now=now+i

  def get_dec_label_bert(self):
      self.dec_label_bert=[]
      self.oral_score_bert=0

  def get_dec_label_rouge(self):
      rouge=[]
      score_rouge=[]
      index_rouge=[]
      for j in self.abstract_fact:
          score=[]
          for k in self.article_fact:
              scores = self.rougex.get_scores(j, k)
              score.append((scores[0]['rouge-1']['f']+scores[0]['rouge-2']['f'])/2)
          choose=score.index(max(score))
          index_rouge.append(choose)
          rouge.append(self.article_fact[choose])
          score_rouge.append(max(score))
      for i in range(len(self.article_fact)):
          if i in index_rouge:
              self.dec_label_rouge.append(1)
          else:
              self.dec_label_rouge.append(0)

      self.oral_score_rouge = self.rougex.get_scores(' . '.join(rouge), ' . '.join(self.abstract))

  def get_dec_label_rouge_sent(self):
    get_dec_label_sent=self.greedy_selection(self.article, self.abstract, 3, self.rougex)
    for i in range(len(self.article)):
      if i in get_dec_label_sent:
          self.dec_label_sent.append(1)
      else:
          self.dec_label_sent.append(0)

  def greedy_selection(self, doc_sent_list, abstract_sent_list, summary_size, rougex):
    selected = []
    max_rouge = 0.0
    reference=''
    for i in abstract_sent_list:
        reference+=i
        reference+=' . '
    for s in range(summary_size):
        cur_max_rouge = max_rouge
        cur_id = -1
        for i in range(len(doc_sent_list)):
            if (i in selected):
                continue
            c = selected + [i]
            candidates = ''
            for j in c:
                candidates+=doc_sent_list[j]
                candidates+=' . '
            scores = rougex.get_scores(candidates, reference)
            rouge_score = (scores[0]['rouge-1']['f']+scores[0]['rouge-2']['f'])/2
            if rouge_score > cur_max_rouge:
                cur_max_rouge = rouge_score
                cur_id = i
        if (cur_id == -1):
            return selected
        selected.append(cur_id)
        max_rouge = cur_max_rouge
    return sorted(selected)

class Batch(object):

  """Class representing a minibatch of train/val/test examples for text summarization."""

  def __init__(self, example_list, max_len):

    """
    Turns the example_list into a Batch object.
    Args:
       example_list: List of Example objects
       hps: hyperparameters
       vocab: Vocabulary object
    """

    self.init_encoder(example_list, max_len) # initialize the input to the encoder
    self.init_decoder(example_list, max_len) # initialize the input and targets for the decoder
    self.init_result(example_list)

  def init_result(self, example_list):
    self.original_article=[]
    self.original_abstract=[]
    self.original_sent=[]
    self.sent_to_fact=[]
    for ex in example_list:
        self.original_sent.append(ex.article)
        self.original_article.append(ex.article_fact)
        self.original_abstract.append(ex.abstract)
        self.sent_to_fact.append(ex.article_fact_tag)

  def init_encoder(self, example_list, max_len):
    self.enc_fact=[]
    self.enc_sent=[]
    self.grap_sim_bert=[]
    self.grap_sim_rouge=[]
    self.grap_entity=[]
    self.grap_cosent=[]
    self.grap_sent=[]

    for ex in example_list:
      ex.get_enc_fact(max_len)
      ex.get_grap()

    # Fill in the numpy arrays
    for ex in example_list:
      self.enc_fact.append(ex.enc_fact)
      self.enc_sent.append(ex.enc_sent)
      self.grap_sim_bert.append(ex.grap_sim_bert)
      self.grap_sim_rouge.append(ex.grap_sim_rouge)
      self.grap_entity.append(ex.grap_entity)
      self.grap_cosent.append(ex.grap_cosent)
      self.grap_sent.append(ex.grap_sent)

  def init_decoder(self, example_list, max_len):
    self.dec_fact=[]
    self.dec_label_sent=[]
    self.dec_label_bert=[]
    self.dec_label_rouge=[]
    self.dec_score_bert=[]
    self.dec_score_rouge=[]
    # Pad the inputs and targets
    for ex in example_list:
        ex.get_dec_fact(max_len)
        ex.get_dec_label_bert()
        ex.get_dec_label_rouge()
        ex.get_dec_label_rouge_sent()
    # Fill in the numpy arrays
    for ex in example_list:
      self.dec_fact.append(ex.dec_fact)
      self.dec_label_sent.append(ex.dec_label_sent)
      self.dec_label_bert.append(ex.dec_label_bert)
      self.dec_label_rouge.append(ex.dec_label_rouge)
      self.dec_score_bert.append(ex.oral_score_bert)
      self.dec_score_rouge.append(ex.oral_score_rouge)

class Batcher(object):

  """A class to generate minibatches of data. Buckets examples together based on length of the encoder sequence."""

  BATCH_QUEUE_MAX = 100 # max number of batches the batch_queue can hold

  def __init__(self, data_path, nlp_path):

    """Initialize the batcher. Start threads that process the data into batches.
    Args:
      data_path: tf.Example filepattern.
      vocab: Vocabulary object
      hps: hyperparameters
      single_pass: If True, run through the dataset exactly once (useful for when you want to run evaluation on the dev or test set). Otherwise generate random batches indefinitely (useful for training).
    """
    self._data_path = data_path
    self._max_len=50
    self._batch_size=4

    # Initialize a queue of Batches waiting to be used, and a queue of Examples waiting to be batched
    self._batch_queue = queue.Queue(self.BATCH_QUEUE_MAX)
    self._example_queue = queue.Queue(self.BATCH_QUEUE_MAX * self._batch_size)
    # Initialize the tool
    self.tokenizer=BertTokenizer.from_pretrained('bert-base-uncased')
    self.rougex=Rouge()
    self.nlp=StanfordCoreNLP(nlp_path)
    # Different settings depending on whether we're in single_pass mode or not
    self._num_example_q_threads = 1 # just one thread, so we read through the dataset just once
    self._num_batch_q_threads = 1  # just one thread to batch examples
    self._bucketing_cache_size = 50 # only load one batch's worth of examples before bucketing; this essentially means no bucketing
    self._finished_reading = False # this will tell us when we're finished reading the dataset
    #prepear dataloader

    self.input_gen = threadsafe_generator(example_generator_DMCNN(self._data_path))

    print('finish prepearing')
    # Start the threads that load the queues

    self._example_q_threads = []
    for _ in range(self._num_example_q_threads):
      self._example_q_threads.append(Thread(target=self.fill_example_queue))
      self._example_q_threads[-1].daemon = True
      self._example_q_threads[-1].start()

    self._batch_q_threads = []
    for _ in range(self._num_batch_q_threads):
      self._batch_q_threads.append(Thread(target=self.fill_batch_queue))
#      self._batch_q_threads[-1].daemon = True
      self._batch_q_threads[-1].start()

    print('threads started')

  def next_batch(self):

    """
    Return a Batch from the batch queue.
    batch: a Batch object, or None if we're in single_pass mode and we've exhausted the dataset.
    """
    # If the batch queue is empty, print a warning
    if self._batch_queue.qsize() == 0:
#      tf.logging.warning('Bucket input queue is empty when calling next_batch. Bucket queue size: %i, Input queue size: %i', self._batch_queue.qsize(), self._example_queue.qsize())
      pass
      if self._finished_reading and self._example_queue.qsize() == 0:
        print("Finished reading dataset in single_pass mode.")
        return None
    batch = self._batch_queue.get() # get the next Batch
    return batch

  def fill_example_queue(self):

    """Reads data from file and processes into Examples which are then placed into the example queue."""
    global g_b
    while True:
      g_b+=1
      if g_b%100==0:
          print('--------'+str(g_b)+'--------')
          print(self._example_queue.qsize())
          print(self._batch_queue.qsize())
      try:
        article, abstract = self.input_gen.__next__() # read the next example from file. article and abstract are both strings.
      except StopIteration: # if there are no more examples:
        print("The example generator for this example queue filling thread has exhausted data.")
        self._finished_reading = True
        break
      example = Example(article, abstract, self.tokenizer, self.rougex, self.nlp) # Process into an Example.
      self._example_queue.put(example) # place the Example in the example queue.

  def fill_batch_queue(self):

    """
    Takes Examples out of example queue, sorts them by encoder sequence length, processes into Batches and places them in the batch queue.
    In decode mode, makes batches that each contain a single example repeated.
    """
    while True:
    # Get bucketing_cache_size-many batches of Examples into a list, then sort
      inputs = []
      for _ in range(self._batch_size * self._bucketing_cache_size):
          if  self._finished_reading and self._example_queue.qsize() == 0:
              break
          inputs.append(self._example_queue.get())
      # Group the sorted Examples into batches, optionally shuffle the batches, and place in the batch queue.
      inputs.sort(key=self.get_sort)
      '''
      splits = []
      len_pre=-1
      for indexi,i in enumerate(inputs):
          len_now = i.article_len
          if len_pre != len_now:
              splits.append(indexi)
          len_pre=len_now
      batches=[]
      for indexi,i in enumerate(splits):
          if indexi+1 == len(splits):
              batches.append(inputs[i:])
          else:
              batches.append(inputs[i:splits[indexi+1]])
      batches_max=[]
      for i in batches:
          if len(i) <= self._batch_size:
              batches_max.append(i)
          else:
              batches_max+=[i[j:j+self._batch_size] for j in range(0, len(i), self._batch_size)]
      '''
      batches_max=[]
      for indexi,i in enumerate(inputs):
          if indexi % self._batch_size ==0:
              batches_max.append(inputs[indexi:indexi+self._batch_size])
      for b in batches_max:  # each b is a list of Example objects
        self._batch_queue.put(Batch(b, self._max_len))

  def get_sort(self, x):
      return x.article_len

'''
train_data_loader=Batcher('data/DMCNN/train_*', 'DMCNN')

count=0
countx=0
while True:
    batch = train_data_loader.next_batch()
    each_batch_size=len(batch.enc_fact)
    if train_data_loader._finished_reading == True:
        break
    f=open('data_file/DMCNN/train_file/'+str(count)+'_train_batch_of '+str(each_batch_size)+' examples.pkl','wb')
    pickle.dump(batch,f)
    f.close()
    count+=1
    countx+=each_batch_size
print('Total train data:')
print(countx)

train_data_loader=Batcher('data/DMCNN/val_*', 'DMCNN')

count=0
countx=0
while True:
    batch = train_data_loader.next_batch()
    each_batch_size=len(batch.enc_fact)
    if train_data_loader._finished_reading == True:
        break
    f=open('data_file/DMCNN/val_file/'+str(count)+'_val_batch_of '+str(each_batch_size)+' examples.pkl','wb')
    pickle.dump(batch,f)
    f.close()
    count+=1
    countx+=each_batch_size
print('Total val data:')
print(countx)
'''

def argLoader():

    parser = argparse.ArgumentParser()

    #device

    parser.add_argument('--nlp_path', type=str, default='/home/ziqiang/stanfordnlp_resources/stanford-corenlp-full-2018-10-05')

    # Data Setting

    parser.add_argument('--data_path', type=str, default='data/DMCNN/train*')

    parser.add_argument('--output_path', type=str, default='data_file/DMCNN/train_file/')

    args = parser.parse_args()

    return args

args = argLoader()

train_data_loader=Batcher(args.data_path, args.nlp_path)

count=0
countx=0
while True:
    batch = train_data_loader.next_batch()
    each_batch_size=len(batch.enc_fact)
    if train_data_loader._finished_reading == True and train_data_loader._batch_queue.qsize() == 0 and train_data_loader._example_queue.qsize() == 0:
        break
    f=open(args.output_path+str(count)+'_batch_of '+str(each_batch_size)+' examples.pkl','wb')
    pickle.dump(batch,f)
    f.close()
    count+=1
    countx+=each_batch_size

print('finish all')


from django.db import models

class BaseModel(models.Model):
    created_at = models.DateTimeField(
        "Data de Criação", auto_now=False, auto_now_add=True
    )
    modified_at = models.DateTimeField(
        "Data de Modificação", auto_now=True, auto_now_add=False
    )

    class Meta:
        abstract = True

class Manga(BaseModel):
    STATUS_CHOICES = (
        (1, "Cancelado"),
        (2, "Completo"),
        (3, "Completo (Em Tradução)"),
        (4, "Em publicação"),
        (5, "Pausado"),
    )

    cdm_id = models.CharField(
        "CDM ID", max_length=255, blank=False, null=False, unique=True
    )
    title = models.CharField("Título", max_length=255, blank=False, null=False)
    link = models.URLField("Link", max_length=255, blank=False, null=False, unique=True)
    img_url = models.URLField("Imagem", max_length=255, blank=False, null=True)
    publish_year = models.IntegerField("Ano", blank=False, null=True)
    status = models.IntegerField(
        "Status", choices=STATUS_CHOICES, blank=False, null=True
    )
    author = models.ForeignKey(
        "mangas.Author",
        verbose_name="Autor",
        on_delete=models.SET_NULL,
        blank=False,
        null=True,
    )
    designer = models.ForeignKey(
        "mangas.Designer",
        verbose_name="Artista",
        on_delete=models.SET_NULL,
        blank=False,
        null=True,
    )
    genres = models.ManyToManyField("mangas.Genre", verbose_name="Gênero")
    types = models.ManyToManyField("mangas.Type", verbose_name="Tipos")
    summary = models.TextField("Resumo", blank=False, null=True)

    class Meta:
        verbose_name = "mangá"
        verbose_name_plural = "mangás"
        ordering = ("created_at",)

    def __str__(self):
        return self.title

class Author(BaseModel):
    name = models.CharField("Nome", max_length=125, unique=True)

    class Meta:
        verbose_name = "autor"
        verbose_name_plural = "autores"
        ordering = ("created_at",)

    def __str__(self):
        return self.name

class Designer(BaseModel):
    name = models.CharField("Nome", max_length=125, unique=True)

    class Meta:
        verbose_name = "artista"
        verbose_name_plural = "artistas"
        ordering = ("created_at",)

    def __str__(self):
        return self.name

class Genre(BaseModel):
    name = models.CharField("Nome", max_length=125, unique=True)

    class Meta:
        verbose_name = "gênero"
        verbose_name_plural = "gêneros"
        ordering = ("created_at",)

    def __str__(self):
        return self.name

class Type(BaseModel):
    name = models.CharField("Nome", max_length=125, unique=True)

    class Meta:
        verbose_name = "tipo"
        verbose_name_plural = "tipos"
        ordering = ("created_at",)

    def __str__(self):
        return self.name


import rclpy
from rclpy.node import Node

from std_msgs.msg import String
from custom_interfaces.srv import ComponentStatus

class PerceptionNode(Node):

  def __init__(self):
    super().__init__('perception_node')

    self.cameraSubscriber = self.create_subscription(
        String,
        'camera',
        self.camera_callback,
        10
    )

    self.trashDetectionPublisher = self.create_publisher(
        String,
        'trash_detection',
        10
    )

    self.componentStatusService = self.create_service(
        ComponentStatus,
        'component_status',
        self.handle_component_status
    )

  def camera_callback(self, message):
    msg = String()
    """
    In practical cases, we process images to understand if trash is present
    """
    if (message.data == "trash"):
      msg.data = "I see trash"
    else:
      msg.data = "I see nothing"
    self.trashDetectionPublisher.publish(msg)
    self.get_logger().info('Publishing: "%s"' % msg.data)

  def handle_component_status(self, request, response):
    self.get_logger().info("Server called!")
    if request.component == "camera":
      response.status = "Camera Battery is at 100%"
    else:
      response.status = "Error: Component unavailable"

    return response

def main(args=None):
  rclpy.init(args=args)

  perceptionNode = PerceptionNode()

  rclpy.spin(perceptionNode)

  """
  Destroy the node explicitly
  (optional - otherwise it will be done automatically
  when the garbage collector destroys the node object)
  """
  perceptionNode.destroy_node()
  rclpy.shutdown()

if __name__ == '__main__':
  main()


import optparse
import xml.etree.ElementTree

from util import build_utils

MANIFEST_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="%(package)s"
    split="%(split)s">
  <uses-sdk android:minSdkVersion="21" />
  <application android:hasCode="%(has_code)s">
  </application>
</manifest>
"""

def ParseArgs():
  """Parses command line options.

  Returns:
    An options object as from optparse.OptionsParser.parse_args()
  """
  parser = optparse.OptionParser()
  build_utils.AddDepfileOption(parser)
  parser.add_option('--main-manifest', help='The main manifest of the app')
  parser.add_option('--out-manifest', help='The output manifest')
  parser.add_option('--split', help='The name of the split')
  parser.add_option(
      '--has-code',
      action='store_true',
      default=False,
      help='Whether the split will contain a .dex file')

  (options, args) = parser.parse_args()

  if args:
    parser.error('No positional arguments should be given.')

  # Check that required options have been provided.
  required_options = ('main_manifest', 'out_manifest', 'split')
  build_utils.CheckOptions(options, parser, required=required_options)

  return options

def Build(main_manifest, split, has_code):
  """Builds a split manifest based on the manifest of the main APK.

  Args:
    main_manifest: the XML manifest of the main APK as a string
    split: the name of the split as a string
    has_code: whether this split APK will contain .dex files

  Returns:
    The XML split manifest as a string
  """

  doc = xml.etree.ElementTree.fromstring(main_manifest)
  package = doc.get('package')

  return MANIFEST_TEMPLATE % {
      'package': package,
      'split': split.replace('-', '_'),
      'has_code': str(has_code).lower()
  }

def main():
  options = ParseArgs()
  main_manifest = file(options.main_manifest).read()
  split_manifest = Build(
      main_manifest,
      options.split,
      options.has_code)

  with file(options.out_manifest, 'w') as f:
    f.write(split_manifest)

  if options.depfile:
    build_utils.WriteDepfile(
        options.depfile,
        [options.main_manifest] + build_utils.GetPythonDependencies())

if __name__ == '__main__':
  main()


from __future__ import absolute_import
import errno
import os

from oio.common.easy_value import debinarize

def read_user_xattr(fd):
    """Read all extended attributes starting with "user." """
    if hasattr(fd, "fileno"):
        fd = fd.fileno()
    meta = {}
    try:
        meta = debinarize(
            {
                k[5:]: os.getxattr(fd, k)
                for k in os.listxattr(fd)
                if k.startswith("user.")
            }
        )
    except IOError as err:
        for code in ("ENOTSUP", "EOPNOTSUPP", "ENOENT"):
            if hasattr(errno, code) and err.errno == getattr(errno, code):
                raise err

    return meta


# 두 요소의 위치를 바꿔주는 helper function
def swap_elements(my_list, index1, index2):
    # 코드를 작성하세요.
    my_list[index1],my_list[index2]=my_list[index2],my_list[index1]

# 퀵 정렬에서 사용되는 partition 함수
def partition(my_list, start, end):
    # 코드를 작성하세요.
    b=start
    i=start
    p=end
    pivot=my_list[p]
    while 1:
        if i==end:
            break
        elif pivot>my_list[i]:
            swap_elements(my_list, b, i)
            b+=1
            i+=1
        else:
            i+=1
    swap_elements(my_list, b, i)
    return b

def quicksort(my_list, start=0, end=None):
    # 코드를 작성하세요.
    if end == None:
        end = len(my_list) - 1

    if end-start<0:
        return
    else:
        quicksort(my_list, partition(my_list, start, end) + 1, end)
        quicksort(my_list, start, partition(my_list, start, end)-1)

# 테스트 1
list1 = [1, 3, 5, 7, 9, 11, 13, 11]
quicksort(list1) # start, end 파라미터 없이 호출
print(list1)

# 테스트 2
list2 = [28, 13, 9, 30, 1, 48, 5, 7, 15]
quicksort(list2) # start, end 파라미터 없이 호출
print(list2)

# 테스트 3
list3 = [2, 5, 6, 7, 1, 2, 4, 7, 10, 11, 4, 15, 13, 1, 6, 4]
quicksort(list3) # start, end 파라미터 없이 호출
print(list3)


from ctypes import alignment
from tkinter import *
from tkinter import messagebox as mb
import json

#Class for GUI components
class Assessment(object):

    def __init__(self, database_filename, gui):

        #Snag data
        with open(database_filename) as f:
            data = json.load(f)

        self.gui = gui

        #Set statements, framing and responses
        self.statements = (data['statements'])
        self.framing = (data['framing'])
        self.responses = (data['responses'])

        #Set statement number to 0
        self.statement_num=0

        #Display title and instructions
        self.display_title()

        #Assigns statements to display_statements function to update later
        self.display_statements()

        #Holds an integer value which is used for selected option in a question.
        self.resp_selected=IntVar()

        #Display radio buttons
        self.resps = self.radio_buttons()

        #Display responses
        self.display_responses()

        #Display next button
        self.buttons()

        #Number of statements
        self.data_size = len(self.statements)

        #Counter of score
        self.score=0

    # generate score for a statement and add to self.score
    def generate_score(self, statement_num):

        #self.resp_selected.get() is an int not a string!
        # Please check if my associations for P and N
        # are correct.

        #Calculate scoring for positively framed questions
        if self.framing[statement_num] == "P":
            if self.resp_selected.get() == 1:
                self.score += 5
            elif self.resp_selected.get() == 2:
                self.score += 4
            elif self.resp_selected.get() == 3:
                self.score += 3
            elif self.resp_selected.get() == 4:
                self.score += 2
            elif self.resp_selected.get() == 5:
                self.score += 1

        #Calculate scoring for negatively framed questions
        else:
            if self.resp_selected.get() == 1:
                self.score += 1
            elif self.resp_selected.get() == 2:
                self.score += 2
            elif self.resp_selected.get() == 3:
                self.score += 3
            elif self.resp_selected.get() == 4:
                self.score += 4
            elif self.resp_selected.get() == 5:
                self.score += 5

        #print(self.score)

    #Display result in message box
    def display_result(self):

        #Calculate average score
        result = round(float(self.score / self.data_size), 2)

        score_1 = "You scored a", result, "which means you have extremely low grit."
        score_2 = "You scored a", result, "which means you have low grit."
        score_3 = "You scored a", result, "which means you have medium grit."
        score_4 = "You scored a", result, "which means you have high grit."
        score_5 = "You scored a", result, "which means you have extremely high grit."

        #Message box to display results
        if result <= 1:
            mb.showinfo("Result", score_1)
        elif result <= 2:
            mb.showinfo("Result", score_2)
        elif result <= 3:
            mb.showinfo("Result", score_3)
        elif result <= 4:
            mb.showinfo("Result", score_4)
        elif result <= 5:
            mb.showinfo("Result", score_5)

    #Show next statement
    def next_btn(self):

        enter_value = "Please select a value before hitting next."

        #Message box to display error message
        if self.resp_selected.get() == 0:
            mb.showerror("Error", enter_value)
            return

        # score current statement
        self.generate_score(self.statement_num)

        #Move to next Question by incrementing the statement_num counter
        self.statement_num += 1

        #Check if statement_num = data size
        if self.statement_num == self.data_size:

            # if it is correct then it displays the score
            self.display_result()

            # destroys the GUI
            gui.destroy()

        else:
            #Show the next question
            self.display_statements()
            self.display_responses()

    #Display button on the screen
    def buttons(self):

        #Next button
        next_button = Button(self.gui, text="Next", command=self.next_btn, width=10,
                             bg="green", fg="white", font=("Arial", 16, "bold"))
        next_button.place(x=350, y=380)

        #Quit button
        quit_button = Button(self.gui, text="Quit", command=gui.destroy, width=10,
                             bg="red", fg="white", font=("Arial", 16, "bold"))
        quit_button.place(x=40, y=380)

    #Display responses (next to radio buttons including select/deselect behavior)
    def display_responses(self):
        val=0

        #Deselect radio buttons
        self.resp_selected.set(0)

        #Display responses next to radio buttons
        for response in self.responses[self.statement_num]:
            self.resps[val]['text']=response
            val+=1

    #Display statements
    def display_statements(self):

        #Statement formatting
        statement_num = Label(self.gui, text=self.statements[self.statement_num],
                              width=100, font=("Arial", 16, "bold"), anchor='w')

        #Statement placement
        statement_num.place(x=70, y=100)

    #Display radio buttons
    def radio_buttons(self):

        #Empty response list
        r_list = []

        #Response placement
        y_pos = 150

        #Add responses to list
        while len(r_list) < 5:

            #Radio button formatting
            radio_btn = Radiobutton(self.gui, text=" ", variable=self.resp_selected, value=len(r_list)+1, font=("Arial", 14))

            #Add radio button to list
            r_list.append(radio_btn)

            #Radio button placement
            radio_btn.place(x=100, y=y_pos)

            #Increment the y-axis position by 40
            y_pos += 40

        #Return radio buttons
        return r_list

    #This method is used to Display Title
    def display_title(self):

        #Display title
        title = Label(gui, text="Grit Scale Questionnaire", fg="black", font=("Arial", 20, "bold"))

        #Place title
        title.place(x=0, y=2)

        #Display instructions
        title = Label(gui, text="Please respond to the following statement. Be honest - there are no right or wrong answers.", fg="black", font=("Arial", 12,))

        #Place instructions
        title.place(x=0, y=40)

#GUI window
gui = Tk()

#GUI window size
gui.geometry("1200x600")

#GUI window title
gui.title("Grit Scale")

#Object of class
assessment = Assessment("data.json", gui)

#Launch GUI
gui.mainloop()

#Intro landing page
#WHAT IS GRIT?
#• Grit is defined as perseverance and passion for long-term goals
#• It entails working strenuously toward challenges, maintaining effort and interest over years despite failure, adversity, and plateaus in progress
#• Grit is unrelated to talent and can be built through a growth mindset


import sys
import math
import time

m = 26
n = 3
barList = [1, 2, 3,4,5,6,7,8]

ergListSumme = []
ergListSumme.append([1,3,4,7,10])
ergListSumme.append([1,3,4,8,10])
ergListSumme.append([1,2,5,8,10])
ergListSumme.append([1,2,5,9,9])
ergListSumme.append([1,1,5,8,10])

erg = ""
ergList = []
ergebnis = []

laenge = 99
zwErg = []
zwWert = 0
zwLaenge = 99
ergL1 = []
ergL2 = []
for ergList in ergListSumme:
    if sum(ergList) > zwWert:
        zwWert = sum(ergList)
for ergList in ergListSumme:
    if sum(ergList) == zwWert:
        ergL1.append(ergList)

for ergList in ergL1:
    if len(ergList) < laenge:
        laenge = len(ergList)
for ergList in ergL1:
    if len(ergList) == laenge:
        ergL2.append(ergList)
ergebnis = ergL2.pop()
for ergList in ergL2:
    kleiner = False
    for e in range(0,len(ergebnis)):
        if ergebnis[e] < ergList[e]:
            break
        if ergebnis[e] > ergList[e]:
            kleiner = True
            break
    if kleiner:
        ergebnis = ergList[:]

print(sum(ergebnis),file=sys.stderr)
for e in ergebnis:
    erg = erg + " " + str(e)
print(str(erg[1:]))


import datetime

class Message:
    """Represents a message sent to a chat.

    Attributes
    ----------
    chat: :class:`models.Chat`
        The chat the message belongs to
    type: :class:`str`
        The type of message sent
    id: :class:`str`
        The id of the message
    content: :class:`str`
        What the message contained
    created_at: :class:`datetime`
        When the message was sent
    author: :class:`models.User`
        Who sent the message
    command: Optional[:class:`dlive.Command`]
        The command of the message
    """

    def __init__(self, bot, data, chat, author):
        self.chat = chat
        self._data = data
        self._bot = bot
        self.type = data["type"]
        self.id: int = data["id"]
        self.content = data["content"]
        self.created_at = datetime.datetime.utcfromtimestamp(
            int(data["createdAt"][:-9]))
        self.author = author
        self.command = None

    def __str__(self):
        return self.content

    async def delete(self):
        """Deletes the message from the chat."""
        chat = await self._bot.get_chat(self._data["sender"]["username"])

        return await self._bot.http.delete_chat_message(chat, self.id)


# https://leetcode.com/problems/valid-anagram/

def isAnagram(s: str, t: str) -> bool:

    if len(s) != len(t):
        return False

    s_hash = {}
    t_hash = {}

    for i in range(len(s)):
        s_hash[s[i]] = s_hash.get(s[i], 0) + 1
        t_hash[t[i]] = t_hash.get(t[i], 0) + 1

    if len(s_hash) != len(t_hash):
        return False

    for i in s_hash:
        if s_hash[i] != t_hash.get(i, 0):
            return False
    return True


import json, os, requests, subprocess

# Your Discogs username and API key
username = ''
api_key = ''

# The ID of the folder containing your collection
folder_id = 0

# Base URL for Discogs API
base_url = 'https://api.discogs.com'

# Endpoint for retrieving collection releases
endpoint = f'/users/{username}/collection/folders/{folder_id}/releases'

# Set up authentication headers
headers = {'User-Agent': 'MyDiscogsClient/1.0', 'Authorization': f'Discogs token={api_key}'}

# Make request to API
response = requests.get(base_url + endpoint, headers=headers)

directory = 'thumbs/'

# Make request to API and retrieve all pages of results
page = 1
thumbnails = []
while True:
    response = requests.get(base_url + endpoint, headers=headers, params={'page': page})
    response_data = response.json()
    # Extract album thumbnails from each release
    for release in response_data['releases']:
        # Check if release has an image and add it to the list of thumbnails
        if release['basic_information']['cover_image']:
            thumbnails.append(release['basic_information']['cover_image'])
    # Check if there are more pages of results
    if 'next' in response.links:
        page += 1
    else:
        break

for thumbnail in thumbnails:
    filename = thumbnail.split("/")[-1]
    file_path = f"{directory}{filename}"
    if os.path.isfile(file_path):
        print(f"{filename} already exists in {directory}")
    else:
        # Download the file using wget
        print(f"Downloading {filename} from {thumbnail} to {directory}")
        subprocess.run(['wget', '-P', directory, thumbnail])