import numpy as np
import cv2
import glob
import time
from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler
from sklearn.calibration import CalibratedClassifierCV
from skimage.feature import hog
from lesson_functions import *
import pickle
import scipy.misc
from sklearn.model_selection import train_test_split

# Define a function to extract features from a list of images
# Have this function call bin_spatial() and color_hist()
def extract_features_append_channels(imgs, color_space='RGB', spatial_size=(32, 32),
                                     hist_bins=32, orient=9,
                                     pix_per_cell=8, cell_per_block=2,
                                     spatial_feat=True, hist_feat=True, hog_feat=True):
    # Create a list to append feature vectors to
    features = []
    # Iterate through the list of images
    for file in imgs:
        file_features = []
        # Read in each one by one
        # orig: image = mpimg.imread(file)

        image = scipy.misc.imread(file)

        # apply color conversion if other than 'RGB'
        if color_space != 'RGB':
            if color_space == 'HSV':
                feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
            elif color_space == 'LUV':
                feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2LUV)
            elif color_space == 'HLS':
                feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
            elif color_space == 'YUV':
                feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
            elif color_space == 'YCrCb':
                feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb)
        else:
            feature_image = np.copy(image)

        if spatial_feat == True:
            spatial_features = bin_spatial(feature_image, size=spatial_size)
            file_features.append(spatial_features)
        if hist_feat == True:
            # Apply color_hist()
            hist_features = color_hist(feature_image, nbins=hist_bins)
            file_features.append(hist_features)
        if hog_feat == True:
            # Extract every channel and append to features
            hog_features = get_hog_features(feature_image[:, :, 0], orient,
                                            pix_per_cell, cell_per_block, vis=False, feature_vec=True)
            file_features.append(hog_features)

            hog_features = get_hog_features(feature_image[:, :, 1], orient,
                                            pix_per_cell, cell_per_block, vis=False, feature_vec=True)
            file_features.append(hog_features)

            hog_features = get_hog_features(feature_image[:, :, 2], orient,
                                            pix_per_cell, cell_per_block, vis=False, feature_vec=True)
            file_features.append(hog_features)

        features.append(np.concatenate(file_features))
    # Return list of feature vectors
    return features

# Read data
# Read in cars and notcars
cars_list = glob.glob("train_images/vehicles/*/*.png")
not_cars_list = glob.glob("train_images/non-vehicles/*/*.png")

cars = []
notcars = []

for car in cars_list:
    cars.append(car)

for not_car in not_cars_list:
    notcars.append(not_car)

color_space = 'HSV' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb
pix_per_cell = 8 # HOG pixels per cell
cell_per_block = 2 # HOG cells per block
spatial_size = (16, 16) # Spatial binning dimensions
spatial_feat = True # Spatial features on or off
hist_feat = True # Histogram features on or off
hog_feat = True # HOG features on or off

for cell_per_block in [2, 4]:
    for orient in [9, 12]:
        for hist_bins in [32, 48]:
            y_start_stop = [None, None] # Min and max in y to search in slide_window()

            car_features = extract_features_append_channels(cars, color_space=color_space, \
                                    spatial_size=spatial_size, hist_bins=hist_bins, \
                                    orient=orient, pix_per_cell=pix_per_cell, \
                                    cell_per_block=cell_per_block, \
                                    spatial_feat=spatial_feat, hist_feat=hist_feat, hog_feat=hog_feat)

            notcar_features = extract_features_append_channels(notcars, color_space=color_space, \
                                    spatial_size=spatial_size, hist_bins=hist_bins, \
                                    orient=orient, pix_per_cell=pix_per_cell, \
                                    cell_per_block=cell_per_block, \
                                    spatial_feat=spatial_feat, hist_feat=hist_feat, hog_feat=hog_feat)

            X = np.vstack((car_features, notcar_features)).astype(np.float64)

            # Fit a per-column scaler
            X_scaler = StandardScaler().fit(X)

            # Apply the scaler to X
            scaled_X = X_scaler.transform(X)

            # Define the labels vector
            y = np.hstack((np.ones(len(car_features)), np.zeros(len(notcar_features))))

            # Split up data into randomized training and test sets
            rand_state = np.random.randint(0, 100)
            X_train, X_test, y_train, y_test = train_test_split(
                scaled_X, y, test_size=0.2, random_state=rand_state)

            print('Using: ', orient, 'orientations', pix_per_cell,
                  'pixels per cell and ', cell_per_block, 'cells per block ',
                   hist_bins, 'hist_bins => feature vector length: ', len(X_train[0]))

            # Use a linear SVC
            svc = LinearSVC()
            svc_model = CalibratedClassifierCV(svc)

            # Check the training time for the SVC
            t=time.time()
            svc_model.fit(X_train, y_train)

            t2 = time.time()
            print('   Seconds to train SVC: ' , round(t2-t, 2))

            # Check the score of the SVC
            print('   Train Accuracy of SVC: ', round(svc_model.score(X_train, y_train), 4))
            print('   Test  Accuracy of SVC:  ', round(svc_model.score(X_test, y_test), 4))

            # Check the prediction time for a single sample
            i = rand_seed = np.random.randint(0, 1000)
            t0 = time.time()
            prediction = svc_model.predict(X_test[i].reshape(1, -1))
            prob = svc_model.predict_proba(X_test[i].reshape(1, -1))

            print("      Prediction time with SVC: ", time.time()-t0)
            print("      Label ", int(y_test[i]), "Prediction {}".format(prediction))
            print("      Prob {}".format(prob))

            # Save Model
            filename = 'SVM_orient' + str(orient) + '_cellsPerBlock' + str(cell_per_block) + '_histBins' + str(hist_bins) + '.pkl'
            with open(filename, 'wb') as fp:
                pickle.dump([svc_model, X_scaler], fp)

            fp.close()


from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    if request.method == 'GET':
        return render(request, 'matrix_size_input.html')
    elif request.method == 'POST':
        if request.POST['form_submitted'] == 'matrix_size':
            m = int(request.POST['m'])
            n = int(request.POST['n'])
            request.session['m'] = m # Need to run python manage.py migrate before deploying
            request.session['n'] = n
            return render(request, 'matrix_input.html', context={'range_m':range(m),'range_n':range(n)})
        elif request.POST['form_submitted'] == 'matrix':
            m = request.session['m']
            n = request.session['n']
            # store the matrix in the variable A
            A = []
            for i in range(m):
                row = []
                for j in range(n):
                    row.append(request.POST.dict()['a_'+str(i)+'_'+str(j)])
                A.append(row)
            from .linear_equation_solver import find_solution
            return render(request, 'display_row_operations.html', context={'row_op':find_solution(A)})


#	FileName : Chapter1_15.py
'''
通过某个字段将字典、实例的序列进行分组：
关键点：
1、先通过sorted或者序列自带sort函数，利用lambda/itemgetter/attrgetter根据某字段进行排序
2、然后用itertools.groupby函数进行分组后返回迭代对象
'''
from operator import itemgetter
from itertools import groupby

if __name__ == '__main__':
	rows = [
		{'address': '5412 N CLARK', 'date': '07/01/2012'},
		{'address': '5148 N CLARK', 'date': '07/04/2012'},
		{'address': '5800 E 58TH', 'date': '07/02/2012'},
		{'address': '2122 N CLARK', 'date': '07/03/2012'},
		{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
		{'address': '1060 W ADDISON', 'date': '07/02/2012'},
		{'address': '4801 N BROADWAY', 'date': '07/01/2012'},
		{'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
	]
	rows.sort(key = itemgetter('date'))
	for data, items in groupby(rows, key=itemgetter('date')):
		print(data, ':')
		for item in items:
			print(' ', item)


from typing import List

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        l = len(s)

        dp = [False]*(l+1)
        dp[0] = True
        for i in range(1,l+1):
            for j in range(i):
                if dp[j] and s[j:i] in wordDict:
                    dp[i] = True
                    break;
        return dp[l]

#https://leetcode.com/problems/word-break/description/?envType=study-plan-v2&envId=dynamic-programming
#Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated
# sequence of one or more dictionary words.Note that the same word in the dictionary may be reused multiple times in the segmentation.


# ! encoding-UTF-8
"""
main.py -

author: inf20026@lehre.dhbw-stuttgart.de
date: 26.04.22
version: 0.0.1
license: MIT
"""
import math

def simulation(party):
    party.stillRunning = True
    party.paused = False
    while party.stillRunning:
        calculateIteration(party)
        while party.paused:
            pass

def calculateBestPositionForPerson(person, party):
    fieldsList = getNeighborFields(person, party.spielfeld)
    neuBefindlichkeiten = []
    for field in fieldsList:
        neuBefindlichkeiten.append((calculateWellBeeing(person, field, party), field))
    print(neuBefindlichkeiten)
    maxBefindlichkeit = person.aktuellebefindlichkeit
    newField = person.position
    for befindlichkeit in neuBefindlichkeiten:
        if befindlichkeit[0] > maxBefindlichkeit:
            maxBefindlichkeit = befindlichkeit[0]
            newField = befindlichkeit[1]
    return newField, maxBefindlichkeit

def calculateOnePerson(party, person):
    newField, maxBefindlichkeit = calculateBestPositionForPerson(person, party)
    party.spielfeld.abbild[person.position[0]][person.position[1]] = '#'
    party.spielfeld.abbild[newField[0]][newField[1]] = person.name[0]
    person.position = newField
    person.aktuelleBefindlichkeit = maxBefindlichkeit

def calculateIteration(party):
    for person in party.personenliste:
        calculateOnePerson(party, person)

def getNeighborFields(person, spielfeld):
    fieldslist = []
    position = person.position
    for i in range(-1, 2):
        for j in range(-1, 2):
            newY = position[1] + i
            newX = position[0] + j
            if spielfeld.raum_hoehe > newY >= 0:
                if spielfeld.raum_breite > newX >= 0:
                    if isPositionFree((newX, newY), spielfeld, person):
                        fieldslist.append([newX, newY])
    return fieldslist

def isPositionFree(position, spielfeld, person):
    if spielfeld.abbild[position[0]][position[1]] in ['#', person.name[0]]:
        return True
    return False

def calculateDistance(p1, p2):
    return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)

def calculateWellBeeing(person, field, party):
    spielfeld = party.spielfeld
    beziehungsbefindlichkeiten = []
    maxDistance = math.sqrt(
            spielfeld.raum_breite ** 2 + spielfeld.raum_hoehe ** 2)

    for beziehung in [i for i in person.beziehung if person.id != i.personid]:
        wunschAbstand = beziehung.wunschabstand
        if beziehung.personid == 0:
            tatAbstand = maxDistance
            for i in range(spielfeld.tisch_hoehe):
                for j in range(spielfeld.tisch_breite):
                    # spielfeld.abbild[spielfeld.tisch_x + j][spielfeld.tisch_y + i]
                    tischField = (spielfeld.tisch_x + j, spielfeld.tisch_y + i)
                    distance = calculateDistance(tischField, field)
                    tatAbstand = distance if distance < tatAbstand else tatAbstand
        else:
            personBeziehung = [i for i in party.personenliste if i.id == beziehung.personid][0]
            tatAbstand = calculateDistance(field, personBeziehung.position)
            #tatAbstandX = abs(field[0] - personBeziehung.position[0])
            #tatAbstandY = abs(field[1] - personBeziehung.position[1])

        #tatAbstand = math.sqrt(tatAbstandX ** 2 + tatAbstandY ** 2)
        befindlichkeitVonBeziehung = mapping_helper(abs(tatAbstand - wunschAbstand), 0, maxDistance, 5, 1)
        beziehungsbefindlichkeiten.append(befindlichkeitVonBeziehung)
    avgBefindlichkeit = sum(beziehungsbefindlichkeiten) / len(beziehungsbefindlichkeiten)
    return avgBefindlichkeit

# Stolen from Internetz
def mapping_helper(value, rangeAmin, rangeAmax, rangeBmin, rangeBmax):
    rangeAspan = rangeAmax - rangeAmin
    rangeBspan = rangeBmax - rangeBmin

    # Convert the math range into a 0-1 range (float)
    valueScaled = float(value - rangeAmin) / float(rangeAspan)

    # Convert the 0-1 range into a value in the right range.
    return rangeBmin + (valueScaled * rangeBspan)


#!/usr/bin/python3

__doc__ = """并查集可以压缩路径

"""
class UnionFind:
    def __init__(self, n: int):
        """初始化一个1-n的集合

        1、初始时各人都是各人的父节点
        2、当前连通分量
        """
        self.parent = list(range(n))
        self.size = [1] * n
        self.n = n
        # 当前连通分量数目
        self.setCount = n

    def findset(self, x: int) -> int:
        if self.parent[x] == x:
            return x
        # 不断递归查找x的根节点
        self.parent[x] = self.findset(self.parent[x])
        return self.parent[x]

    def unite(self, x: int, y: int) -> bool:
        """合并两个节点x,y所在的集合

        1、首先找到x和y的根节点
        2、然后将小集合归并到大集合上，并更新小集合根节点的parent为x
        3、更新集合大小与集合数目
        """
        x, y = self.findset(x), self.findset(y)
        if x == y:
            return False
        if self.size[x] < self.size[y]:
            x, y = y, x
        self.parent[y] = x
        self.size[x] += self.size[y]
        self.setCount -= 1
        return True

    def connected(self, x: int, y: int) -> bool:
        x, y = self.findset(x), self.findset(y)
        return x == y


class Solution:
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        List = []
        for i in range(rowIndex+1):
            sList = []
            for j in range(i+1):
                if j == 0 or j == i:
                   sList.append(1)
                else:
                    sList.append(List[j] + List[j-1])
            List = sList
        return sList


# Advent of Code - Day 1
# Part 1

n_increased = 0
prev_sum = 0
val = [0, 0, 0]

for line, value in enumerate(open('input.txt')):
    val = [val[1], val[2], int(value)]
    if line > 2:
        if sum(val) > prev_sum:
            n_increased += 1
    prev_sum = sum(val)

print(n_increased)


from flask import Flask, render_template, request, flash, redirect, session
app = Flask(__name__)
app.secret_key = "secret_key"

@app.route('/')

def index():
  return render_template("index.html")

@app.route('/check', methods=['POST'])

def check():
	if len(request.form['name']) == 0:
		flash('Name must not be blank')

	if len(request.form['comment']) > 120:
		flash('Comment must not be over 120 characters')

	if len(request.form['name']) == 0 or len(request.form['comment']) > 120:
		return redirect('/')

	session['all_info']	= request.form
	return redirect('/result')

@app.route('/result')

def result():
	return render_template('result.html', result=session['all_info'])

app.run(debug=True)


from openai import OpenAI
from dotenv import load_dotenv
import os

load_dotenv()
openai_api_key = os.getenv('OPENAI_API_KEY')

client = OpenAI(
    api_key = openai_api_key
)

def summarize_text(text):
    response = client.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=[
        {
            "role": "user",
            "content": f"""
            Sos un abogado especialista en justicia federal. Necesito que analices la siguiente sentencia de la Corte Superema de Justicia de la Nación, me la resumas en no más de 100 palabras y me digas que fue lo que se resolvió.
            Además quiero que lo devuelvas en un formato como el siguiente:
            1. *Título de la sentencia*:
            2. *Involucrados en la causa*:
            3. *Resolución*:
            4. *Resumen ejecutivo*:
            : {text}"""
        }
    ]
    )

    return response.choices[0].message.content


#!/usr/bin/env python3

from PIL import Image
from IPython.display import display
from data import *
import random
from colorGen import genColor
import json
'''
Each avatar will be unique and have 5 traits
'''

TOTAL_IMAGES = 50

all_images = []

def create_new_images():

	new_image = {}

	new_image["FACE"] = random.choices(face,face_weights)[0]
	new_image["EARS"] = random.choices(ears,ears_weights)[0]
	new_image["EYES"] = random.choices(eyes,eyes_weights)[0]
	new_image["HAIR"] = random.choices(hair, hair_weights)[0]
	new_image["MOUTH"] = random.choices(mouth, mouth_weights)[0]
	new_image["NOSE"] = random.choices(nose, nose_weights)[0]

	if new_image in all_images:
		return create_new_images()
	else:
		return new_image

for i in range(TOTAL_IMAGES):

    new_trait_image = create_new_images()

    all_images.append(new_trait_image)

def all_images_unique(all_images):
	seen = list()
	#print([i in seen or seen.append(i) for i in all_images])
	return any(i in seen or seen.append(i) for i in all_images)

i=0

for item in all_images:
	item["token_id"]=f"{i}"
	i+=1

''' Getting trait counts '''
mouth_count = {}
for item in mouth:
	mouth_count[item] = 0

face_count = {}
for item in face:
	face_count[item] = 0

eyes_count = {}
for item in eyes:
	eyes_count[item] = 0

nose_count = {}
for item in nose:
	nose_count[item] = 0

ears_count = {}
for item in ears:
	ears_count[item] = 0

hair_count = {}
for item in hair:
	hair_count[item] = 0

for image in all_images:
	face_count[image["FACE"]] += 1
	ears_count[image["EARS"]] += 1
	eyes_count[image["EYES"]] += 1
	hair_count[image["HAIR"]] += 1
	mouth_count[image["MOUTH"]] += 1
	nose_count[image["NOSE"]] += 1

for item in all_images:
	color = genColor()
	im1 = Image.open(f'substrapunks-master/scripts/face_parts/face/{face_files[item["FACE"]]}.png')
	im2 = Image.open(f'substrapunks-master/scripts/face_parts/eyes/{eyes_files[item["EYES"]]}.png')
	im3 = Image.open(f'substrapunks-master/scripts/face_parts/ears/{ears_files[item["EARS"]]}.png')
	im4 = Image.open(f'substrapunks-master/scripts/face_parts/hair/{hair_files[item["HAIR"]]}.png')
	im5 = Image.open(f'substrapunks-master/scripts/face_parts/mouth/{mouth_files[item["MOUTH"]]}.png')
	im6 = Image.open(f'substrapunks-master/scripts/face_parts/nose/{nose_files[item["NOSE"]]}.png')
	bg = Image.new("RGBA",im5.size,color=color)

	item["background"] = color

	com = Image.alpha_composite(bg,im1)
	com1 = Image.alpha_composite(com,im2)
	com2 = Image.alpha_composite(com1,im3)
	com3 = Image.alpha_composite(com2,im4)
	com4 = Image.alpha_composite(com3,im5)
	com5 = Image.alpha_composite(com4,im6)

	rgb_im = com5.convert("RGB")
	file_name = str(item["token_id"]) + ".png"

	rgb_im.save("./images/"+file_name)

METADATA_FILE = "./metadata/all_traits.json"

with open(METADATA_FILE,"w") as file:
	json.dump(all_images,file,indent=3)


from datetime import datetime

from typing import Iterable

class Message_INNER:
    FMT_BEGIN = ">"
    TS_FIELD = "ts"
    FIELDS = [{"name": "status", "fmt": "H", "ru": "Статус"},
              {f"name": "EMPTY", "fmt": "H", "ru": "EMPTY"},
              {"name": "actual_turnover", "fmt": "f", "ru": "Фактические обороты"},
              {"name": "generator_voltage", "fmt": "f", "ru": "Упр. Напряжением. На генераторе"},
              {"name": "brake_degree", "fmt": "f", "ru": "Степень торможения"},
              {"name": "body_temperature", "fmt": "f", "ru": "Температура на корпусе"},
              {"name": "windings_temperature", "fmt": "f", "ru": "Температура на обмотках"},
              {"name": "vibration_speed", "fmt": "f", "ru": "Виброскорость"},
              {"name": "specified_frequency", "fmt": "f", "ru": "Заданная частота"},
              {"name": "acceleration_frequency", "fmt": "f", "ru": "Частота разгона"},
              {"name": "current_consumption", "fmt": "f", "ru": "Ток потребления"},
              {"name": "output_voltage", "fmt": "f", "ru": "Выходное напряжение"},
              {"name": "output_power", "fmt": "f", "ru": "Выходная мощность"},
              {"name": "power_coefficient", "fmt": "f", "ru": "Коэффициент мощности"},
              {"name": "IGBT_temperature", "fmt": "H", "ru": "Температура IGBT модуля"},
              {"name": "start_stop", "fmt": "H", "ru": "Пуск/Стоп"}, ]

    def __init__(self, state: Iterable):
        self.ts = datetime.now().timestamp()
        self.state = [v for i, v in enumerate(state) if i not in self._empty_indexes()]
        self.FIELDS = [v for i, v in enumerate(self.FIELDS) if i not in self._empty_indexes()]

    @classmethod
    def _empty_indexes(cls):
        return [i for i, v in enumerate(cls.FIELDS) if v["name"] == "EMPTY"]

    @classmethod
    def build_fmt(cls):
        fmt = [cls.FMT_BEGIN]
        for field in cls.FIELDS:
            fmt.append(field["fmt"])
        return "".join(fmt)

    @classmethod
    def get_cols(cls):
        cols = [cls.TS_FIELD]
        cols.extend([val['name'] for i, val in enumerate(cls.FIELDS) if i not in cls._empty_indexes()])
        return cols

    @classmethod
    def dict_from_csv_row(cls, row: str):
        values = row.strip().split(',')
        cols = cls.get_cols()
        return {cols[i]: float(val) for i, val in enumerate(values)}

    def get_csv_row(self):
        """
        Format message as csv row
        :return: 'message.ts,val1,...valN'
        """
        return f"{self.ts}, {','.join([str(v) for v in self.state])}"

    def pretty_print(self):
        for i, v in enumerate(self.FIELDS):
            print(v["ru"], self.state[i])


from django.core.files import File
import os
import logging
from tabmgnt.settings import MEDIA_ROOT
import boto
from django.conf import settings
from boto.s3.connection import S3Connection, Bucket, Key

logging.basicConfig()
log = logging.getLogger(__name__)

def get_base_url():
    if not settings.ENVIRONMENT_PRODUCTION:
        return ""
    else:
        return "media/"

def convert_toS3_url(url):
    return "http://"+str(settings.AWS_STORAGE_BUCKET_NAME)+".s3.amazonaws.com/"+url if settings.ENVIRONMENT_PRODUCTION else url

def get_event_upload_path(instance, filename):
    return get_base_url() + "event/" + str(instance.event.uuid) + "/" + str(instance.id) + '-' + filename

def get_userImage_upload_path(instance, filename):
    return get_base_url() + "user/" + str(instance.user.uuid) + "/" + "images/" + filename

def get_main_home_upload_directory(instance):
    return get_base_url() + "homePage/" + str(instance.id)

def get_main_home_upload_path(instance, filename):
    uploadDirectory=get_main_home_upload_directory(instance)
    return uploadDirectory+ "/main/" + str(filename)

def get_siteInfo_image_upload_path(instance, filename):
    return get_base_url() + "homePage/" + str(instance.homePage.id) + "/siteInfo/" + filename

def remove_s3_file(filePath):
    if settings.ENVIRONMENT_PRODUCTION:
        conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
        bucket = Bucket(conn,settings.AWS_STORAGE_BUCKET_NAME)
        for key in bucket.list(prefix=filePath):
            key.delete()
        # print("-------------------"+filePath)
        # bucket.delete_key(filePath)

def createTestFile(content, name):
    print(MEDIA_ROOT + '/test/' + name)
    try:
        with open(MEDIA_ROOT + '/test/' + name, "wb") as f:
            f.write("" + content + "")
            f.close()
    except Exception as e:
        log.error("===================" + str(e))

def createFixedDepositHtmlFile(content, name, fixedDepositScrape):
    name = name.replace(' ', '')
    try:
        with open(MEDIA_ROOT + '/' + name, "wb") as f:
            f.write("" + content + "")
        django_file = File(open(MEDIA_ROOT + '/' + name, "rb"))
        fixedDepositScrape.fixedDepositFile.save(name, django_file, save=True)
        fixedDepositScrape.save()
        os.remove(MEDIA_ROOT + '/' + name)

    except Exception as e:
        log.error("===================" + str(e))


from pandas.core.frame import DataFrame
from general_mod import group_unique_entities
import os
# import codecs
import variables_mod as v
import validation_mod as val
import xml_mod as xml_m

import tkinter as tk
from tkinter import *
from tkinter import messagebox
from threading import Thread

root = tk.Tk()

def begin_process():

    try:

        conf = v.cp.RawConfigParser()

        # Path cmd
        Path = './ScriptFiles/config.txt'

        conf.read(Path)

        file_groups = conf.get('datosEntrada', 'fileGrupos')
        hoja_groups = conf.get('datosEntrada', 'hojaGrupos')
        file_schedules = conf.get('datosEntrada', 'fileHorarios')
        hoja_schedules = conf.get('datosEntrada', 'hojaHorarios')

        academicTerm = conf.get('periodBtt', 'periodo')

        flag_wrong_info = False
        flag_wrong_classroom = False
        flag_validacion_columns = False

        #errorHeader e errorBody em relação a propriedades de messageBox que apresenta o erro.
        flag_validacion_columns, errorHeader, errorBody = val.read_data(file_schedules,hoja_schedules, file_groups, hoja_groups,
                                                          validacion = True)

        if (flag_validacion_columns == False):

            #Para apresentar erro na messageBox...não em forma de lista, mas sim de String
            if isinstance(errorBody, list):

                errorBody = ','.join(errorBody)

            tk.messagebox.showerror(errorHeader, 'Check:\n\n' + errorBody)

        else:

            df_events, df_grupos, df_courses, df_salas= val.read_data(file_schedules, hoja_schedules, file_groups, hoja_groups)

            # DATA PREPARACION
            # df_events = df_events[df_events[v.v_id_event_guarani] == '25513']
            df_events = val.cleaning_data (df_events)
            df_courses = val.cleaning_data (df_courses)
            df_grupos = val.cleaning_data (df_grupos)
            df_salas =  val.cleaning_data (df_salas)

            # Verify null Values (extract all nulls except values number_students (null student convert to value 0))

            #Passar Variavel a dizer que ficou con dados incompletos(Para Apresentar no fim)
            df_events = val.check_nulls(df_events)

            # Extract Null Values after verify values students

            # Number students < 0 Retirados de eventos a importar...Esta a escrever em ficheiro tambem...
            df_events = val.verify_students_null(df_events)

            # ID to Asig Salas
            df_events_salas = df_events.copy()

            # Verificar a validação de Salas que não foram respectivamente mapeadas...
            # Insert classrooms BC
            df_join_events_salas = val.get_name_salas_btt(df_events_salas, df_salas)

            df_events = df_join_events_salas.copy()

            df_events = val.replace_minutes_hours(df_events)

            #Verificar se eventos tem hora de inicio superior a hora de fim(verificar como mostrar a validação)

            df_events, df_wrong_hours = val.verify_hour_begin_end(df_events)

            #### verificar como agregar eventos com a asigancion ###
            # df_analisis = df_events.copy()

            # df_analisis_1 = df_analisis [[v.v_comision, v.v_subcomision, v.v_classrooms_code_guarani, v.v_id_event_guarani, v.v_students,v.v_group,
            #                               v.v_mod_cod, v.v_weeks, v.v_day]].copy()
            # df_analisis_1['comisionTemp'] = df_analisis_1  [v.v_comision] + '_' + df_analisis_1  [v.v_subcomision]

            # df_analisis_1 = df_analisis_1 [[ 'comisionTemp', v.v_classrooms_code_guarani, v.v_id_event_guarani, v.v_students,v.v_group,
            #                                v.v_mod_cod, v.v_weeks, v.v_day]]

            # df_analisis_1 = group_unique_entities (df_analisis_1, v.v_id_event_guarani)

            # df_analisis_1 = df_analisis_1[(df_analisis_1['comisionTemp'].str.contains(','))]

            # df_analisis_1.to_excel('./New_Files/ComisionesCompartidas.xlsx', 'Hoja1', index = False)
            #### verificar como agregar eventos com a asigancion ###

            #Os duplicados de professores ja foram retirados em metodo anterior...

            df_events = val.join_curriculum(df_events, df_courses, df_grupos)

            #Agrupar Eventos para inserir em XML:

            df_events = val.grouped_data(df_events)

            df_events = val.insert_name_section(df_events)

            df_events = val.insert_name_acad_term(df_events, academicTerm)

            df_events = val.final_weeks(df_events)

            # df_compartidas = val.prepare_data_xml_compartidas(df_compartidas)

            df_events = val.selec_data_comissiones_compartidas_to_import(df_events)

            # df_events.to_excel('./New_Files/EventsGrouped.xlsx', 'Hoja1', index = False)

            file_name = 'Horarios_UCA_'
            XML_AGREGADO = (v.xml_header + '\n'.join(df_events.apply(xml_m.xml_btt, axis=1)) + '\n' + v.xml_footer)

            #Write File
            path_file = './Xml_Files'

            if os.path.isdir(path_file):

                timestr = v.t.strftime("%Y%m%d_%H%M%S")
                with open('./Xml_Files/' + file_name + timestr + '.xml' ,'w') as f:
                    f.write(XML_AGREGADO)

            else:

                os.mkdir(path_file)
                timestr = v.t.strftime("%Y%m%d_%H%M%S")
                with open('./Xml_Files/' + file_name + timestr + '.xml' ,'w') as f:
                    f.write(XML_AGREGADO)

            root.withdraw()
            tk.messagebox.showinfo('EventsXML', 'XML File Generated:\n\nCheck Folder XML_Files.')

    except PermissionError as e:

        tk.messagebox.showerror('Close File', 'Check:\n\n' + 'Close Incomplete_Data on Folder New_Files')


from file_writer.file_writer import FileWriter
from problem4.problem4 import Problem4
from problem5.problem5 import Problem5
from problem6.problem6 import Problem6

time_out = FileWriter('Problem7Output.csv')

problem4 = Problem4(time_out)
problem5 = Problem5(time_out)
problem6 = Problem6(time_out)
problem8 = Promblem8()

problem4.run()
problem5.run()
problem6.run()
problem8.run()

time_out.close()


import time
import os

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

EMAIL = os.environ.get("EMAIL")
PASS = os.environ.get("PASS")

url = "https://tinder.com"
decline_cookies_xpath = '//*[@id="u-1238065328"]/main/div[2]/div/div/div[1]/div[2]/button/div[2]/div[2]'
next_xpath = '//*[@id="identifierNext"]/div/button/span'

options = Options()
options.add_experimental_option("detach", True)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)

# stay_open.add_argument("--headless=new")
# stay_open.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.get("https://tinder.com/404")
time.sleep(1)

driver.get(url)
print(driver.current_window_handle)
time.sleep(3)

original_window = driver.current_window_handle

driver.find_element(By.LINK_TEXT, "Log in").click()
time.sleep(2)
driver.find_element(By.XPATH, decline_cookies_xpath).click()
driver.find_element(By.TAG_NAME, "iframe").click()

wait = WebDriverWait(driver, 10)
wait.until(EC.number_of_windows_to_be(2))

for window_handle in driver.window_handles:
    if window_handle != original_window:
        driver.switch_to.window(window_handle)
print(driver.current_window_handle)

driver.find_element(By.TAG_NAME, "input").send_keys(EMAIL)
driver.find_element(By.XPATH, next_xpath).click()
time.sleep(1)
driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(PASS, Keys.ENTER)
time.sleep(2)
keep_going = input("Hit ENTER to continue.")
driver.switch_to.window(original_window)
# Allow location
driver.find_element(By.XPATH, '//*[@id="u-1238065328"]/main/div/div/div/div[3]/button[1]/div[2]/div[2]').click()
#Don't allow notifications
driver.find_element(By.XPATH, '//*[@id="u-1238065328"]/main/div/div/div/div[3]/button[2]/div[2]/div[2]').click()
time.sleep(5)
# Start smashing those hearts
for n in range(10):
    driver.find_element(By.XPATH,
                        '//*[@id="u490315748"]/div/div[1]/div/div/main/div/div/div[1]/div/div[3]/div/div[4]/button/'
                        'span/span/svg/path')\
        .click()
    time.sleep(1)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('varer', '0012_auto_20141222_0524'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='salgsvarepris',
            name='kassenr',
        ),
        migrations.AddField(
            model_name='salgsvare',
            name='kassenr',
            field=models.PositiveSmallIntegerField(blank=True, help_text='Nr i varekatalog i kassa', null=True),
            preserve_default=True,
        ),
    ]


#Ejercicio 10
#Una juguetería tiene mucho éxito en dos de sus productos: payasos y muñecas.
#Suele hacer venta por correo y la empresa de logística les cobra por peso de cada paquete
#así que deben calcular el peso de los payasos y muñecas que saldrán en cada paquete a demanda.
# cada payaso pesa 112 g y cada muñeca 75 g. Escribir un programa que lea el número de payasos
#  y muñecas vendidos en el último pedido y calcule el peso total del paquete que será enviado.

payaso_gramos = 112
muneca_gramos = 75

payasos = int(input('Cuantos payasos vendiste en el ultimo periodo? => '))
munecas = int(input('Cuantas munecas vendiste en el ultimo periodo? => '))
peso_total = (payasos * payaso_gramos) + (munecas * muneca_gramos)
print(f'El peso total del paquete a enviar es de {peso_total} gramos')


print('Введи номер, с которого будет идти нумерация')
number = int(input())
while True:
    print('Введи текст (для выхода введи 0)')
    text = input()
    if text == '0':
        break
    print('Введи жанр цифрой(Художественный - 0, научный - 1, публицистический - 2)')
    genre = int(input())
    file_name = 'Sample_Text_' + str(number) + '.txt'

    #Добавил кодировку
    file = open(file_name, 'w', encoding='utf-8')

    file.write(text)
    file.close()
    answer_name = 'Genre_' + str(number) + '.txt'

    # Добавил кодировку и тут
    file = open(answer_name, 'w', encoding='utf-8')

    if genre == 0:
        file.write(u'Художественный')
    elif genre == 1:
        file.write(u'научный')
    elif genre == 2:
        file.write(u'публицистический')
    file.close()
    number += 1


from django.conf.urls.defaults import patterns, url

# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'wiki.views.home', name='home'),
    url(r'^manage$', 'wiki.views.manage',name='manage'),
    url(r'^view/(?P<page_id>\d+)/$', 'wiki.views.view',name='view'),
    url(r'^new$', 'wiki.views.new', name='new'),
    url(r'^add/(?P<page_id>\d+)/$', 'wiki.views.add', name='add'),
    url(r'^edit/(?P<page_id>\d+)/$', 'wiki.views.edit', name='edit'),
    url(r'^save$', 'wiki.views.save', name='save'),

    url(r'^del/(?P<page_id>\d+)/$', 'wiki.views.delete', name='delete'),
    url(r'^login$', 'django.contrib.auth.views.login', {'template_name': 'web2/login.html'}),
    url(r'^logout$', 'django.contrib.auth.views.logout', {'next_page':"/"}),

    url(r'^api/$', 'wiki.views.api', name='api'),
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    #url(r'^admin/', include(admin.site.urls)),
)


# to convert to 8-bit TIFF files that have been accidentally saved as 12-bit
# usage: python bitCut.py /path/or/paths/to/images

import numpy as np
from PIL import Image
from os import path, walk, mkdir
import sys

basePath = sys.argv[1:]
for bp in basePath:
    for root, dirs, files in walk(bp):
        files = [f for f in files if f.split(".")[-1] == "tif"]
        OPdir = path.join(root,"bit_fix")
        try:
            mkdir(OPdir)
        except OSError:
            nop=True
        for f in files:
            IPpath = path.join(root,f)
            OPpath = path.join(OPdir,f)
            im = Image.open(IPpath)
            img = np.array(im).astype(np.uint16)
            img = np.floor(np.divide(img,2**4)).astype(np.uint8)
            im = Image.fromarray(img)
            im.save(OPpath)


"""
Tornado handler for the browse page
"""
from __future__ import print_function
import collections
import sys
import os
from datetime import datetime
import json
import sqlite3
import tornado.web

# this is needed for the following imports
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../plot_app'))
from config import get_db_filename, get_overview_img_filepath
from db_entry import DBData, DBDataGenerated
from helper import flight_modes_table, get_airframe_data, html_long_word_force_break

#pylint: disable=relative-beyond-top-level,too-many-statements
from .common import get_jinja_env, get_generated_db_data_from_log

BROWSE_TEMPLATE = 'browse.html'

#pylint: disable=abstract-method

class BrowseDataRetrievalHandler(tornado.web.RequestHandler):
    """ Ajax data retrieval handler """

    def get(self, *args, **kwargs):
        """ GET request """
        search_str = self.get_argument('search[value]', '').lower()
        order_ind = int(self.get_argument('order[0][column]'))
        order_dir = self.get_argument('order[0][dir]', '').lower()
        data_start = int(self.get_argument('start'))
        data_length = int(self.get_argument('length'))
        draw_counter = int(self.get_argument('draw'))

        json_output = {}
        json_output['draw'] = draw_counter

        # get the logs (but only the public ones)
        con = sqlite3.connect(get_db_filename(), detect_types=sqlite3.PARSE_DECLTYPES)
        cur = con.cursor()

        sql_order = ' ORDER BY Date DESC'

        ordering_col = ['',#table row number
                        'Logs.Date',
                        '',#Overview - img
                        'Logs.Description',
                        'LogsGenerated.MavType',
                        '',#Airframe - not from DB
                        'LogsGenerated.Hardware',
                        'LogsGenerated.Software',
                        'LogsGenerated.Duration',
                        'LogsGenerated.StartTime',
                        '',#Rating
                        'LogsGenerated.NumLoggedErrors',
                        '' #FlightModes
                        ]
        if ordering_col[order_ind] != '':
            sql_order = ' ORDER BY ' + ordering_col[order_ind]
            if order_dir == 'desc':
                sql_order += ' DESC'

        cur.execute('SELECT Logs.Id, Logs.Date, '
                    '       Logs.Description, Logs.WindSpeed, '
                    '       Logs.Rating, Logs.VideoUrl, '
                    '       LogsGenerated.* '
                    'FROM Logs '
                    '   LEFT JOIN LogsGenerated on Logs.Id=LogsGenerated.Id '
                    'WHERE Logs.Public = 1 AND NOT Logs.Source = "CI" '
                    +sql_order)

        # pylint: disable=invalid-name
        Columns = collections.namedtuple("Columns", "columns search_only_columns")

        def get_columns_from_tuple(db_tuple, counter, all_overview_imgs):
            """ load the columns (list of strings) from a db_tuple
            """

            db_data = DBDataJoin()
            log_id = db_tuple[0]
            log_date = db_tuple[1].strftime('%Y-%m-%d')
            db_data.description = db_tuple[2]
            db_data.feedback = ''
            db_data.type = ''
            db_data.wind_speed = db_tuple[3]
            db_data.rating = db_tuple[4]
            db_data.video_url = db_tuple[5]
            generateddata_log_id = db_tuple[6]
            if log_id != generateddata_log_id:
                print('Join failed, loading and updating data')
                db_data_gen = get_generated_db_data_from_log(log_id, con, cur)
                if db_data_gen is None:
                    return None
                db_data.add_generated_db_data_from_log(db_data_gen)
            else:
                db_data.duration_s = db_tuple[7]
                db_data.mav_type = db_tuple[8]
                db_data.estimator = db_tuple[9]
                db_data.sys_autostart_id = db_tuple[10]
                db_data.sys_hw = db_tuple[11]
                db_data.ver_sw = db_tuple[12]
                db_data.num_logged_errors = db_tuple[13]
                db_data.num_logged_warnings = db_tuple[14]
                db_data.flight_modes = \
                    {int(x) for x in db_tuple[15].split(',') if len(x) > 0}
                db_data.ver_sw_release = db_tuple[16]
                db_data.vehicle_uuid = db_tuple[17]
                db_data.flight_mode_durations = \
                   [tuple(map(int, x.split(':'))) for x in db_tuple[18].split(',') if len(x) > 0]
                db_data.start_time_utc = db_tuple[19]

            # bring it into displayable form
            ver_sw = db_data.ver_sw
            if len(ver_sw) > 10:
                ver_sw = ver_sw[:6]
            if len(db_data.ver_sw_release) > 0:
                try:
                    release_split = db_data.ver_sw_release.split()
                    release_type = int(release_split[1])
                    if release_type == 255: # it's a release
                        ver_sw = release_split[0]
                except:
                    pass
            airframe_data = get_airframe_data(db_data.sys_autostart_id)
            if airframe_data is None:
                airframe = db_data.sys_autostart_id
            else:
                airframe = airframe_data['name']

            flight_modes = ', '.join([flight_modes_table[x][0]
                                      for x in db_data.flight_modes if x in
                                      flight_modes_table])

            m, s = divmod(db_data.duration_s, 60)
            h, m = divmod(m, 60)
            duration_str = '{:d}:{:02d}:{:02d}'.format(h, m, s)

            start_time_str = 'N/A'
            if db_data.start_time_utc != 0:
                try:
                    start_datetime = datetime.fromtimestamp(db_data.start_time_utc)
                    start_time_str = start_datetime.strftime("%Y-%m-%d %H:%M")
                except ValueError as value_error:
                    # bogus date
                    print(value_error)

            # make sure to break long descriptions w/o spaces (otherwise they
            # mess up the layout)
            description = html_long_word_force_break(db_data.description)

            search_only_columns = []

            if db_data.ver_sw is not None:
                search_only_columns.append(db_data.ver_sw)

            if db_data.ver_sw_release is not None:
                search_only_columns.append(db_data.ver_sw_release)

            if db_data.vehicle_uuid is not None:
                search_only_columns.append(db_data.vehicle_uuid)

            image_col = '
 Not rendered / No GPS
'
            overview_image_filename = log_id+'.png'
            if overview_image_filename in all_overview_imgs:
                image_col = ''

            return Columns([
                counter,
                ''+log_date+'',
                image_col,
                description,
                db_data.mav_type,
                airframe,
                db_data.sys_hw,
                ver_sw,
                duration_str,
                start_time_str,
                db_data.rating_str(),
                db_data.num_logged_errors,
                flight_modes
            ], search_only_columns)

        # need to fetch all here, because we will do more SQL calls while
        # iterating (having multiple cursor's does not seem to work)
        db_tuples = cur.fetchall()
        json_output['recordsTotal'] = len(db_tuples)
        json_output['data'] = []
        if data_length == -1:
            data_length = len(db_tuples)

        filtered_counter = 0
        all_overview_imgs = set(os.listdir(get_overview_img_filepath()))
        if search_str == '':
            # speed-up the request by iterating only over the requested items
            counter = data_start
            for i in range(data_start, min(data_start + data_length, len(db_tuples))):
                counter += 1

                columns = get_columns_from_tuple(db_tuples[i], counter, all_overview_imgs)
                if columns is None:
                    continue

                json_output['data'].append(columns.columns)
            filtered_counter = len(db_tuples)
        else:
            counter = 1
            for db_tuple in db_tuples:
                counter += 1

                columns = get_columns_from_tuple(db_tuple, counter, all_overview_imgs)
                if columns is None:
                    continue

                if any(search_str in str(column).lower() for column in \
                        (columns.columns, columns.search_only_columns)):
                    if data_start <= filtered_counter < data_start + data_length:
                        json_output['data'].append(columns.columns)
                    filtered_counter += 1

        cur.close()
        con.close()

        json_output['recordsFiltered'] = filtered_counter

        self.set_header('Content-Type', 'application/json')
        self.write(json.dumps(json_output))

class DBDataJoin(DBData, DBDataGenerated):
    """Class for joined Data"""

    def add_generated_db_data_from_log(self, source):
        """Update joined data by parent data"""
        self.__dict__.update(source.__dict__)

class BrowseHandler(tornado.web.RequestHandler):
    """ Browse public log file Tornado request handler """

    def get(self, *args, **kwargs):
        """ GET request """
        template = get_jinja_env().get_template(BROWSE_TEMPLATE)

        template_args = {}

        search_str = self.get_argument('search', '').lower()
        if len(search_str) > 0:
            template_args['initial_search'] = json.dumps(search_str)

        self.write(template.render(template_args))


import random

__author__ = 'Sam Broderick'

def pick_ball(bucket):
    """
    Randomly pick a ball from a bucket of green and red balls, initially 4 & 4
    :type bucket: list
    :param bucket: Bucket of up to 8 balls
    """
    return random.randint(0, len(bucket) - 1)

def drawing_without_replacement_sim(numTrials):
    """
    Runs numTrials trials of a Monte Carlo simulation
    of drawing 3 balls out of a bucket containing
    4 red and 4 green balls. Balls are not replaced once
    drawn. Returns a float - the fraction of times 3
    balls of the same color were drawn in the first 3 draws.
    :rtype: float
    :type num_trials: int
    :param num_trials:
    """
    # Your code here
    trials = []

    for i in range(numTrials):
        # bucket of 4 red (0) and 4 green (1) balls
        bucket = [0, 0, 0, 0, 1, 1, 1, 1]
        # track the balls picked per trial in a list
        trial = []

        for ball in range(3):
            # pick 3 balls and remove them from the list
            trial.append(bucket.pop(pick_ball(bucket)))

        # track if all balls are the same color
        if trial[0] == trial[1] and trial[1] == trial[2]:
            trials.append(1)  # all balls the same color
        else:
            trials.append(0)  # 1 doesn't match the others

    # using the number coding allows for a simple calculation of the fraction
    return sum(trials) / float(len(trials))

print(drawing_without_replacement_sim(10000))


import logging
import torch
from .compressor import Pruner

__all__ = ['L1FilterPruner', 'L2FilterPruner', 'FPGMPruner']

logger = logging.getLogger('torch weight rank filter pruners')

class WeightRankFilterPruner(Pruner):
    """
    A structured pruning base class that prunes the filters with the smallest
    importance criterion in convolution layers to achieve a preset level of network sparsity.
    """

    def __init__(self, model, config_list):
        """
        Parameters
        ----------
        model : torch.nn.module
            Model to be pruned
        config_list : list
            support key for each list item:
                - sparsity: percentage of convolutional filters to be pruned.
        """

        super().__init__(model, config_list)
        self.register_buffer("if_calculated", torch.tensor(0)) # pylint: disable=not-callable

    def get_mask(self, base_mask, weight, num_prune):
        raise NotImplementedError('{} get_mask is not implemented'.format(self.__class__.__name__))

    def calc_mask(self, layer, config, **kwargs):
        """
        Calculate the mask of given layer.
        Filters with the smallest importance criterion of the kernel weights are masked.
        Parameters
        ----------
        layer : LayerInfo
            the layer to instrument the compression operation
        config : dict
            layer's pruning config
        Returns
        -------
        dict
            dictionary for storing masks
        """

        weight = layer.module.weight.data
        op_type = layer.type
        assert 0 <= config.get('sparsity') < 1, "sparsity must in the range [0, 1)"
        assert op_type in ['Conv1d', 'Conv2d'], "only support Conv1d and Conv2d"
        assert op_type in config.get('op_types')
        if_calculated = kwargs["if_calculated"]
        if if_calculated:
            return None
        mask_weight = torch.ones(weight.size()).type_as(weight).detach()
        if hasattr(layer.module, 'bias') and layer.module.bias is not None:
            mask_bias = torch.ones(layer.module.bias.size()).type_as(layer.module.bias).detach()
        else:
            mask_bias = None
        mask = {'weight': mask_weight, 'bias': mask_bias}
        try:
            filters = weight.size(0)
            num_prune = int(filters * config.get('sparsity'))
            if filters < 2 or num_prune < 1:
                return mask
            mask = self.get_mask(mask, weight, num_prune)
        finally:
            if_calculated.copy_(torch.tensor(1)) # pylint: disable=not-callable
        return mask

class L1FilterPruner(WeightRankFilterPruner):
    """
    A structured pruning algorithm that prunes the filters of smallest magnitude
    weights sum in the convolution layers to achieve a preset level of network sparsity.
    Hao Li, Asim Kadav, Igor Durdanovic, Hanan Samet and Hans Peter Graf,
    "PRUNING FILTERS FOR EFFICIENT CONVNETS", 2017 ICLR
    https://arxiv.org/abs/1608.08710
    """

    def __init__(self, model, config_list):
        """
        Parameters
        ----------
        model : torch.nn.module
            Model to be pruned
        config_list : list
            support key for each list item:
                - sparsity: percentage of convolutional filters to be pruned.
        """

        super().__init__(model, config_list)

    def get_mask(self, base_mask, weight, num_prune):
        """
        Calculate the mask of given layer.
        Filters with the smallest sum of its absolute kernel weights are masked.
        Parameters
        ----------
        base_mask : dict
            The basic mask with the same shape of weight or bias, all item in the basic mask is 1.
        weight : torch.Tensor
            Layer's weight
        num_prune : int
            Num of filters to prune

        Returns
        -------
        dict
            dictionary for storing masks
        """

        filters = weight.shape[0]
        w_abs = weight.abs()
        w_abs_structured = w_abs.view(filters, -1).sum(dim=1)
        threshold = torch.topk(w_abs_structured.view(-1), num_prune, largest=False)[0].max()
        mask_weight = torch.gt(w_abs_structured, threshold)[:, None, None, None].expand_as(weight).type_as(weight)
        mask_bias = torch.gt(w_abs_structured, threshold).type_as(weight)

        return {'weight': mask_weight.detach(), 'bias': mask_bias.detach()}

class L2FilterPruner(WeightRankFilterPruner):
    """
    A structured pruning algorithm that prunes the filters with the
    smallest L2 norm of the weights.
    """

    def __init__(self, model, config_list):
        """
        Parameters
        ----------
        model : torch.nn.module
            Model to be pruned
        config_list : list
            support key for each list item:
                - sparsity: percentage of convolutional filters to be pruned.
        """

        super().__init__(model, config_list)

    def get_mask(self, base_mask, weight, num_prune):
        """
        Calculate the mask of given layer.
        Filters with the smallest L2 norm of the absolute kernel weights are masked.
        Parameters
        ----------
        base_mask : dict
            The basic mask with the same shape of weight or bias, all item in the basic mask is 1.
        weight : torch.Tensor
            Layer's weight
        num_prune : int
            Num of filters to prune
        Returns
        -------
        dict
            dictionary for storing masks
        """
        filters = weight.shape[0]
        w = weight.view(filters, -1)
        w_l2_norm = torch.sqrt((w ** 2).sum(dim=1))
        threshold = torch.topk(w_l2_norm.view(-1), num_prune, largest=False)[0].max()
        mask_weight = torch.gt(w_l2_norm, threshold)[:, None, None, None].expand_as(weight).type_as(weight)
        mask_bias = torch.gt(w_l2_norm, threshold).type_as(weight)

        return {'weight': mask_weight.detach(), 'bias': mask_bias.detach()}

class FPGMPruner(WeightRankFilterPruner):
    """
    A filter pruner via geometric median.
    "Filter Pruning via Geometric Median for Deep Convolutional Neural Networks Acceleration",
    https://arxiv.org/pdf/1811.00250.pdf
    """

    def __init__(self, model, config_list):
        """
        Parameters
        ----------
        model : pytorch model
            the model user wants to compress
        config_list: list
            support key for each list item:
                - sparsity: percentage of convolutional filters to be pruned.
        """
        super().__init__(model, config_list)

    def get_mask(self, base_mask, weight, num_prune):
        """
        Calculate the mask of given layer.
        Filters with the smallest sum of its absolute kernel weights are masked.
        Parameters
        ----------
        base_mask : dict
            The basic mask with the same shape of weight and bias, all item in the basic mask is 1.
        weight : torch.Tensor
            Layer's weight
        num_prune : int
            Num of filters to prune
        Returns
        -------
        dict
            dictionary for storing masks
        """
        min_gm_idx = self._get_min_gm_kernel_idx(weight, num_prune)
        for idx in min_gm_idx:
            base_mask['weight'][idx] = 0.
            if base_mask['bias'] is not None:
                base_mask['bias'][idx] = 0.
        return base_mask

    def _get_min_gm_kernel_idx(self, weight, n):
        assert len(weight.size()) in [3, 4]

        dist_list = []
        for out_i in range(weight.size(0)):
            dist_sum = self._get_distance_sum(weight, out_i)
            dist_list.append((dist_sum, out_i))
        min_gm_kernels = sorted(dist_list, key=lambda x: x[0])[:n]
        return [x[1] for x in min_gm_kernels]

    def _get_distance_sum(self, weight, out_idx):
        """
        Calculate the total distance between a specified filter (by out_idex and in_idx) and
        all other filters.
        Optimized verision of following naive implementation:
        def _get_distance_sum(self, weight, in_idx, out_idx):
            w = weight.view(-1, weight.size(-2), weight.size(-1))
            dist_sum = 0.
            for k in w:
                dist_sum += torch.dist(k, weight[in_idx, out_idx], p=2)
            return dist_sum
        Parameters
        ----------
        weight: Tensor
            convolutional filter weight
        out_idx: int
            output channel index of specified filter, this method calculates the total distance
            between this specified filter and all other filters.
        Returns
        -------
        float32
            The total distance
        """
        logger.debug('weight size: %s', weight.size())
        assert len(weight.size()) in [3, 4], 'unsupported weight shape'

        w = weight.view(weight.size(0), -1)
        anchor_w = w[out_idx].unsqueeze(0).expand(w.size(0), w.size(1))
        x = w - anchor_w
        x = (x * x).sum(-1)
        x = torch.sqrt(x)
        return x.sum()

    def update_epoch(self, epoch):
        for wrapper in self.get_modules_wrapper():
            wrapper.registered_buffers['if_calculated'].copy_(torch.tensor(0)) # pylint: disable=not-callable


# Packages
import os
import requests
import json
# Local Imports
from twitch_errors import OAuthError, TwitchAPIError

def get_twitch_oauth():
    """
    Sends a POST to Twitch API to obtain Oauth token for future API requests

    Raises:
        TwitchAPIError: Any OAuth token error will be raised and logged

    Returns:
        Str: OAuth token string
    """
    oauth_url = "https://id.twitch.tv/oauth2/token"
    client_id = os.getenv('CLIENT_ID')
    client_secret = os.getenv('CLIENT_SECRET')
    query = {
        "client_id": client_id,
        "client_secret": client_secret,
        "grant_type": "client_credentials"
    }
    response = requests.post(oauth_url, params=query)
    # Return successful API call
    if response.status_code == 200:
        result = json.loads(response.text)
        token = result['access_token']
        return token
    else:
        print(f"Error getting oauth token: {response.text}")
        raise TwitchAPIError

def get_top_channels(oauth, quantity):
    """
    Obtain [quantity] most popular channels

    Args:
        oauth (Str): OAuth token string
        quantity (Int): Number of channels to collect

    Raises:
        OAuthError: Invalid OAuth token, usually because old one expired
        TwitchAPIError: Any future TwitchAPI error

    Returns:
        List: List of channel names
    """
    streams_url = "https://api.twitch.tv/helix/streams"
    client_id = os.getenv('CLIENT_ID')
    # This api uses client-id with a - instead of _
    headers = {
        'Authorization': "Bearer " + oauth,
        "client-id": client_id,
    }
    query = {
        "first": quantity,
    }
    response = requests.get(streams_url, params=query, headers=headers)
    # Handle successful API call
    if response.status_code == 200:
        result = json.loads(response.text)
        channels = []
        # API responds with JSON object with a bunch of channel data
        for channel in result['data']:
            channels.append(channel['user_login'])
        return channels
    # 401 == unauthorized (usually because of Oauth mismatch)
    elif response.status_code == 401:
        print("Oauth invalid!")
        print(response.text)
        raise OAuthError
    # Log any other error
    else:
        print(response.status_code)
        print(response.text)
        raise TwitchAPIError


import sys, getopt
sys.path.append('/storage/praha1/home/mezic/.local/lib/python3.7/site-packages')
import netket as nk
import numpy as np
import jax
import time
import json
print("Python version: {}".format(sys.version))
print("NetKet version: {}".format(nk.__version__))
print("NumPy version: {}".format(np.__version__))

file = sys.argv[-1]
if len(sys.argv) == 1:
    file = "config"
print(file)
fq = __import__(file)
from lattice_and_ops import Lattice
from lattice_and_ops import Operators
from lattice_and_ops import HamOps
from lattice_and_ops import permutation_sign
from lattice_and_ops import log_results
ho = HamOps()

OUT_NAME = fq.MACHINE+str(fq.SITES) # output file name
OUT_LOG_NAME = "out.txt"            # filename for output logging
print("N = ",fq.SITES, ", samples = ",fq.SAMPLES,", iters = ",fq.NUM_ITER, ", sampler = ",fq.SAMPLER, ", TOTAL_SZ = ", fq.TOTAL_SZ, ", machine = ", fq.MACHINE, ", dtype = ", fq.DTYPE, ", alpha = ", fq.ALPHA, ", eta = ", fq.ETA, sep="")
with open(OUT_LOG_NAME,"a") as out_log_file:
    out_log_file.write("N = {}, samples = {}, iters = {}, sampler = {}, TOTAL_SZ = {}, machine = {}, dtype = {}, alpha = {}, eta = {}\n".format(fq.SITES,fq.SAMPLES,fq.NUM_ITER,fq.SAMPLER, fq.TOTAL_SZ,fq.MACHINE, fq.DTYPE, fq.ALPHA, fq.ETA))

lattice = Lattice(fq.SITES)

if not fq.USE_PBC and fq.SITES != 16:
    raise Exception("Non-PBC are implemented only for 4x4 lattice!!!")

# Construction of custom graph according to tiled lattice structure defined in the Lattice class.
edge_colors = []
for node in range(fq.SITES):
    if fq.USE_PBC or not node in [3,7,11,15]:
        edge_colors.append([node,lattice.rt(node), 1])  # horizontal connections
    if fq.USE_PBC or not node in [12,13,14,15]:
        edge_colors.append([node,lattice.bot(node), 1]) # vertical connections
    row, column = lattice.position(node)

    SS_color = 3 if not fq.USE_PBC and node in [3,7,4,12,13,14,15] else 2
    if column%2 == 0:
        if row%2 == 0:
            edge_colors.append([node,lattice.lrt(node),SS_color]) # diagonal bond
        else:
            edge_colors.append([node,lattice.llft(node),SS_color]) # diagonal bond

# Define the netket graph object
g = nk.graph.Graph(edges=edge_colors)

hilbert = nk.hilbert.Spin(s=.5, N=g.n_nodes, total_sz=fq.TOTAL_SZ)

# This part is only relevant for GCNN or pRBM machine
print("There are", len(g.automorphisms()), "full symmetries.")
# deciding point between DS and AF phase is set to 0.5
if fq.JEXCH1 < 0.5:
    # DS phase is partly anti-symmetric
    characters = []
    from lattice_and_ops import permutation_sign
    for perm in g.automorphisms():
        # print(perm, "with sign", permutation_sign(np.asarray(perm)))
        characters.append(permutation_sign(np.asarray(perm)))
    characters_1 = np.asarray(characters,dtype=complex)
    characters_2 = characters_1
else:
    # AF phase if fully symmetric
    characters_1 = np.ones((len(g.automorphisms()),), dtype=complex)
    characters_2 = characters_1

# operator of total magnetization
m_z = sum(nk.operator.spin.sigmaz(hilbert, i) for i in range(hilbert.size))
with open("out_mag.txt", "a") as file_mag:
    print("# H_Z    E       E_err   E_inter_mean   E_iter_std  MSR:   E   E_err   E_inter_mean   E_iter_std ", file=file_mag)

for H_Z in fq.STEPS:
    # Hamiltonian definition
    ha_1 = nk.operator.GraphOperator(hilbert, graph=g, bond_ops=ho.bond_operator(fq.JEXCH1,fq.JEXCH2, h_z = H_Z, use_MSR=False), bond_ops_colors=ho.bond_color)
    ha_2 = nk.operator.GraphOperator(hilbert, graph=g, bond_ops=ho.bond_operator(fq.JEXCH1,fq.JEXCH2, h_z = H_Z, use_MSR=True ), bond_ops_colors=ho.bond_color)

    # Exact diagonalization
    if g.n_nodes < 20:
        evals, eigvects = nk.exact.lanczos_ed(ha_1, k=3, compute_eigenvectors=True)
        exact_ground_energy = evals[0]
    else:
        exact_ground_energy = 0
        eigvects = None

    if fq.MACHINE == "RBM":
        machine_1 = nk.models.RBM(dtype=fq.DTYPE, alpha=fq.ALPHA)
        machine_2 = nk.models.RBM(dtype=fq.DTYPE, alpha=fq.ALPHA)
    elif fq.MACHINE == "sRBM":
        machine_1 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=fq.ALPHA)
        machine_2 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=fq.ALPHA)
    elif fq.MACHINE == "GCNN":
        machine_1 = nk.models.GCNN(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=fq.num_layers, features=fq.feature_dims, characters=characters_1)
        machine_2 = nk.models.GCNN(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=fq.num_layers, features=fq.feature_dims, characters=characters_2)
    elif fq.MACHINE == "pRBM":
        from pRBM import pRBM
        machine_1 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=fq.ALPHA, characters=characters_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
        machine_2 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=fq.ALPHA, characters=characters_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
    else:
        raise Exception(str("undefined MACHINE: ")+str(fq.MACHINE))

    # Selection of sampler type
    if fq.SAMPLER == 'local':
        sampler_1 = nk.sampler.MetropolisLocal(hilbert=hilbert)
        sampler_2 = nk.sampler.MetropolisLocal(hilbert=hilbert)
    elif fq.SAMPLER == 'exact':
        sampler_1 = nk.sampler.ExactSampler(hilbert=hilbert)
        sampler_2 = nk.sampler.ExactSampler(hilbert=hilbert)
    else:
        sampler_1 = nk.sampler.MetropolisExchange(hilbert=hilbert, graph=g)
        sampler_2 = nk.sampler.MetropolisExchange(hilbert=hilbert, graph=g)
        if fq.SAMPLER != 'exchange':
            print("Warning! Undefined fq.SAMPLER:", fq.SAMPLER, ", dafaulting to MetropolisExchange fq.SAMPLER")

    # Optimzer
    optimizer_1 = nk.optimizer.Sgd(learning_rate=fq.ETA)
    optimizer_2 = nk.optimizer.Sgd(learning_rate=fq.ETA)

    # Stochastic Reconfiguration
    sr_1 = nk.optimizer.SR(diag_shift=0.01)
    sr_2 = nk.optimizer.SR(diag_shift=0.01)

    # The variational state (drive to byla nk.variational.MCState)
    vs_1 = nk.vqs.MCState(sampler_1, machine_1, n_samples=fq.SAMPLES)
    vs_2 = nk.vqs.MCState(sampler_2, machine_2, n_samples=fq.SAMPLES)
    vs_1.init_parameters(jax.nn.initializers.normal(stddev=0.01))
    vs_2.init_parameters(jax.nn.initializers.normal(stddev=0.01))

    gs_1 = nk.VMC(hamiltonian=ha_1 ,optimizer=optimizer_1,preconditioner=sr_1,variational_state=vs_1)   # 0 ... symmetric
    gs_2 = nk.VMC(hamiltonian=ha_2 ,optimizer=optimizer_2,preconditioner=sr_2,variational_state=vs_2)   # 1 ... symmetric+MSR

    ops = Operators(lattice,hilbert,ho.mszsz,ho.exchange)

    no_of_runs = 2 #2 ... bude se pocitat i druhý způsob (za použití MSR)
    use_2 = 0 # in case of one run
    if exact_ground_energy != 0 and fq.VERBOSE == True:
        print("H_Z =",H_Z,"; Expected exact energy:", exact_ground_energy)
    for i,gs in enumerate([gs_1,gs_2][use_2:use_2+no_of_runs]):
        start = time.time()
        gs.run(out=OUT_NAME+"_"+str(round(H_Z,2))+"_"+str(i), n_iter=int(fq.NUM_ITER),show_progress=fq.VERBOSE)
        end = time.time()
        if H_Z == fq.STEPS[0]:
            print("The type {} of {} calculation took {} min".format(i,fq.MACHINE ,(end-start)/60))
        # second short run to estimate errorbars
        gs.run(out=OUT_NAME+"_"+str(round(H_Z,2))+"_"+str(i)+"errs", n_iter=int(fq.N_POST_ITER),show_progress=fq.VERBOSE, obs={'magnetization':m_z})

    threshold_energy = 0.995*exact_ground_energy
    data = []
    for i in range(no_of_runs):
        data.append(json.load(open(OUT_NAME+"_"+str(round(H_Z,2))+"_"+str(i)+".log")))
    if type(data[0]["Energy"]["Mean"]) == dict: #DTYPE in (np.complex128, np.complex64):#, np.float64):# and False:
        energy_convergence = [data[i]["Energy"]["Mean"]["real"] for i in range(no_of_runs)]
    else:
        energy_convergence = [data[i]["Energy"]["Mean"] for i in range(no_of_runs)]
    steps_until_convergence = [next((i for i,v in enumerate(energy_convergence[j]) if v < threshold_energy), -1) for j in range(no_of_runs)]

    if fq.VERBOSE == True:
        for i,gs in enumerate([gs_1,gs_2][use_2:use_2+no_of_runs]):
            print("Trained RBM with MSR:" if i else "Trained RBM without MSR:")
            print("m_z =", gs.estimate(ops.m_z))
            print("m_d^2 =", gs.estimate(ops.m_dimer_op))
            print("m_p =", gs.estimate(ops.m_plaquette_op_MSR))
            print("m_s^2 =", gs.estimate(ops.m_s2_op_MSR))
            print("m_s^2 =", gs.estimate(ops.m_s2_op), "<--- no MSR!!")

    # estimating the errorbars
    data = []
    for i in range(no_of_runs):
        data.append(json.load(open(OUT_NAME+"_"+str(round(H_Z,2))+"_"+str(i)+"errs.log")))
    if type(data[0]["Energy"]["Mean"]) == dict: #DTYPE in (np.complex128, np.complex64):#, np.float64):# and False:
        energy_convergence = [data[i]["Energy"]["Mean"]["real"] for i in range(no_of_runs)]
        mag_convergence = [data[i]["magnetization"]["Mean"]["real"] for i in range(no_of_runs)]
    else:
        energy_convergence = [data[i]["Energy"]["Mean"] for i in range(no_of_runs)]
        mag_convergence = [data[i]["magnetization"]["Mean"] for i in range(no_of_runs)]

    with open("out_mag.txt", "a") as file_mag:
        print("{:5.2f}   {:10.5f} {:10.5f} {:10.5f} {:10.5f}   {:10.5f} {:10.5f} {:10.5f} {:10.5f}".format(H_Z, gs_1.energy.mean.real, gs_1.energy.error_of_mean, np.mean(energy_convergence[0]), np.std(energy_convergence[0]), gs_2.energy.mean.real, gs_2.energy.error_of_mean, np.mean(energy_convergence[1]), np.std(energy_convergence[1])), file=file_mag)

    if no_of_runs==2:
        log_results(H_Z,gs_1,gs_2,ops, samples = fq.SAMPLES, iters = fq.NUM_ITER, exact_energy = exact_ground_energy,steps_until_convergence = steps_until_convergence,filename=OUT_LOG_NAME)
    else:
        log_results(H_Z,gs_1,gs_1,ops, samples = fq.SAMPLES, iters = fq.NUM_ITER, exact_energy = exact_ground_energy,steps_until_convergence = steps_until_convergence,filename=OUT_LOG_NAME)


from .abc import BaseObject
from .ability import AbilityEffectChange
from .common import APIObject, MachineVersionDetail, Name, NamedAPIObject, VerboseEffect

__all__ = (
    "Move",
    "MoveFlavorText",
    "MoveMetaData",
    "MoveStatChange",
    "PastMoveStatValues",
    "ContestComboDetail",
    "ContestComboSet"
)

class Move(BaseObject):
    """Represents a move object from the API.

    .. versionadded:: 0.1.0a

    .. container:: operations

        .. describe:: str(x)

            Returns the move's name.

        .. describe:: x[y]

            Returns a move's y attribute.

        .. describe:: x == y

            Check if two moves are the same.

        .. describe:: x != y

            Check if two moves are *not* the same.

    Attributes
    ----------
    id: :class:`int`
        The identifier for the move.
    name: :class:`str`
        The name for the move.
    accuracy: :class:`int`
        The percent value of how likely the move is to be successful.
    effect_chance: :class:`int`
        The percent value of how likely it is that the move's effect will happen.
    pp: :class:`int`
        Power points. The number of times the move can be used.
    power_points: :class:`int`
        An alias for pp.
    priority: :class:`int`
        A value between -8 and 8. Sets the order in which the move is executed during battle.
    power: :class:`int`
        The base power of the move with a value of 0 if it does not have a base power.
    contest_combos: :class:`ContestComboSets`
        A detail of normal and super contest combos that require the move.
    contest_type: :class:`NamedAPIObject`
        The type of appeal the move gives a Pokémon when used in a contest.
    contest_effect: :class:`APIObject`
        The effect the move has when used in a contest.
    super_contest_effect: :class:`APIObject`
        The effect the move has when used in a super contest.
    damage_class: :class:`NamedAPIObject`
        The type of damage the move inflicts on the target, e.g. physical.
    effect_entries: List[:class:`VerboseEffect`]
        The effect of the move listed in different languages.
    flavor_text_entries: List[:class:`MoveFlavorText`]
        The flavor text of the move listed in different languages.
    generation: :class:`NamedAPIObject`
        The generation in which the move was introduced.
    meta: :class:`MoveMetaData`
        Metadata about the move.
    names: List[:class:`Name`]
        The name of the move listed in different languages.
    past_values: List[:class:`PastMoveStatValues`]
        A list of move value changes across version groups of the game.
    stat_changes: List[:class:`MoveStatChange`]
        A list of stats this move effects and how much it effects them.
    effect_changes: List[:class:`AbilityEffectChange`]
        The list of previous effects the move has had across version groups of the games.
    target: :class:`NamedAPIObject`
        The type of target that will receive the effects of the move.
    type: :class:`NamedAPIObject`
        The elemental type of the move.
    machines: :class:`MachineVersionDetail`
        A list of the machines that teach this move."""
    __slots__ = (
        "accuracy", "effect_chance", "pp", "power_points", "priority", "power", "contest_type", "type", "target",
        "generation", "damage_class", "meta", "stat_changes", "names", "effect_entries", "flavor_text_entries",
        "past_values", "effect_changes", "contest_effect", "super_contest_effect", "machines"
    )

    def __init__(self, data: dict):
        super().__init__(data)

        self.accuracy = data["accuracy"]
        self.effect_chance = data["effect_chance"]
        self.pp = data["pp"]
        self.power_points = self.pp
        self.priority = data["priority"]
        self.power = data["power"]

        self.contest_type = NamedAPIObject(data["contest_type"])
        self.type = NamedAPIObject(data["type"])
        self.target = NamedAPIObject(data["target"])
        self.generation = NamedAPIObject(data["generation"])
        self.damage_class = NamedAPIObject(data["damage_class"])
        self.contest_effect = APIObject(data["contest_effect"])
        self.super_contest_effect = APIObject(data["super_contest_effect"])

        self.effect_changes = [AbilityEffectChange(d) for d in data["effect_changes"]]
        self.meta = MoveMetaData(data["meta"])
        self.stat_changes = [MoveStatChange(d) for d in data["stat_changes"]]
        self.names = [Name(d) for d in data["names"]]
        self.effect_entries = [VerboseEffect(d) for d in data["effect_entries"]]
        self.flavor_text_entries = [MoveFlavorText(d) for d in data["flavor_text_entries"]]
        self.past_values = [PastMoveStatValues(d) for d in data["past_values"]]
        self.machines = [MachineVersionDetail(d) for d in data["machines"]]

class MoveFlavorText:
    """Represents the flavor text of a move associated with a language.

    .. versionadded:: 0.1.0a

    Attributes
    ----------
    flavor_text: :class:`str`
        The localized flavor text for the move in the associated language.
    language: :class:`NamedAPIObject`
        The language the text is in.
    version_group: :class:`NamedAPIObject`
        The version group that uses the text."""
    __slots__ = ("flavor_text", "language", "version_group")

    def __init__(self, data: dict):
        self.flavor_text = data["flavor_text"]
        self.language = NamedAPIObject(data["language"])
        self.version_group = NamedAPIObject(data["version_group"])

    def __repr__(self) -> str:
        return "<MoveFlavorText language='{0.language}' version_group='{0.version_group}'>".format(self)

class MoveMetaData:
    """Represents the metadata about a move.

    .. versionadded:: 0.1.0a

    Attributes
    ----------
    ailment: :class:`NamedAPIObject`
        The status ailment the move inflicts on it's target.
    category: :class:`NamedAPIObject`
        The category of move the move falls under, e.g. damage or ailment.
    min_hits: Optional[:class:`int`]
        The minimum number of times the move hits. ``None`` if it always only hits once.
    max_hits: Optional[:class:`int`]
        The maximum number of times the move hits. ``None`` if it always only hits once.
    min_turns: Optional[:class:`int`]
        The minimum number of turns the move continues to take effect. ``None`` if it always only lasts one turn.
    max_turns: Optional[:class:`int`]
        The maximum number of turns the move continues to take effect. ``None`` if it always only lasts one turn.
    drain: :class:`int`
        HP drain (if positive) or recoil damage (if negative), in percent of damage done.
    healing: :class:`int`
        The amount of hp gained by the attacking Pokemon, in percent of it's maximum HP.
    crit_rate: :class:`int`
        Critical hit rate bonus.
    ailment_chance: :class:`int`
        The likelihood the move will cause an ailment.
    flinch_chance: :class:`int`
        The likelihood the move will cause the target Pokémon to flinch.
    stat_chance: :class:`int`
        The likelihood the mpve will cause a stat change in the target Pokémon.
    """
    __slots__ = (
        "ailment", "category", "min_hits", "max_hits", "min_turns", "max_turns", "drain", "healing", "crit_rate",
        "ailment_chance", "flinch_chance", "stat_chance"
    )

    def __init__(self, data: dict):
        self.ailment = NamedAPIObject(data["ailment"])
        self.category = NamedAPIObject(data["category"])

        self.min_hits = data["min_hits"]
        self.max_hits = data["max_hits"]
        self.min_turns = data["min_turns"]
        self.max_turns = data["max_turns"]
        self.drain = data["drain"]
        self.healing = data["healing"]
        self.crit_rate = data["crit_rate"]
        self.ailment_chance = data["ailment_chance"]
        self.flinch_chance = data["flinch_chance"]
        self.stat_chance = data["stat_chance"]

    def __repr__(self) -> str:
        return "<MoveMetaData category='{0.category}'>".format(self)

class MoveStatChange:
    """Represents a stat change in a :class:`move`

    .. versionadded:: 0.1.0a

    Attributes
    ----------
    change: :class:`int`
        The amount of change.
    stat: :class:`NamedAPIObject`
        The stat being affected."""
    __slots__ = ("change", "stat")

    def __init__(self, data: dict):
        self.change = data["change"]
        self.stat = NamedAPIObject(data["stat"])

    def __repr__(self) -> str:
        return "<MoveStatChange change={0.change} stat='{0.stat}'>".format(self)

class PastMoveStatValues:
    """Represents changed values of a :class:`Move` in a version group.

    .. versionadded:: 0.1.0a

    Attributes
    ----------
    accuracy: :class:`int`
        The percent value of how likely the move is to be successful.
    effect_chance: :class:`int`
        The percent value of how likely it is the moves effect will take effect.
    power: :class:`int`
        The base power of the move with a value of 0 if it does not have a base power.
    pp: :class:`int`
        Power points. The number of times the move can be used.
    effect_entries: List[:class:`VerboseEffect`]
        The effect of the move listed in different languages.
    type: :class:`NamedAPIObject`
        The elemental type of the move.
    version_group: :class:`NamedAPIObject`
        The version group in which these move stat values were in effect."""
    __slots__ = ("accuracy", "effect_chance", "power", "pp", "effect_entries", "type", "version_group")

    def __init__(self, data: dict):
        self.accuracy = data["accuracy"]
        self.effect_chance = data["effect_chance"]
        self.power = data["power"]
        self.pp = data["pp"]
        self.effect_entries = [VerboseEffect(d) for d in data["effect_entries"]]
        self.type = NamedAPIObject(data["type"])
        self.version_group = NamedAPIObject(data["version_group"])

    def __repr__(self) -> str:
        return "<PastMoveStatValues type='{0.type}' version_group='{0.version_group}'>".format(self)

class ContestComboDetail:
    """Represents a detail of moves that can be used to grain additional
    appeal points in contests.

    .. versionadded:: 0.1.0a

    Attributes
    ----------
    use_before: List[:class:`NamedAPIObject`]
        A list of moves to use before this move.
    use_after: List[:class:`str`]
        A list of moves to use after this move."""
    __slots__ = ("use_before", "use_after")

    def __init__(self, data: dict):
        self.use_before = [NamedAPIObject(d) for d in data["use_before"]]
        self.use_after = [NamedAPIObject(d) for d in data["use_after"]]

    def __repr__(self) -> str:
        return "<ContestComboDetail use_before={0.use_before} use_after={0.use_after}>".format(self)

class ContestComboSet:
    """Represents a set of super and normal contest combos.

    .. versionadded:: 0.1.0a

    Attributes
    ----------
    normal: :class:`ContestComboDetail`
        A detail of moves this move can be used before or after, granting additional appeal points in contests.
    super: :class:`ContestComboDetail`
        A detail of moves this move can be used before or after, granting additional appeal points in super contests."""
    __slots__ = ("normal", "super")

    def __init__(self, data: dict):
        self.normal = ContestComboDetail(data["normal"])
        self.super = ContestComboDetail(data["super"])

    def __repr__(self):
        return "<ContestComboSet normal={0.normal} super={0.super}>".format(self)


"""
author: andre
date: 02/05/2021
"""
#Se importan las librerias básicas
import cv2
import numpy as np
#Creamos función que reciba las mascaras B-G-R y el color respectivo que detecta la mascara
def mark(mask,color):
    contorns,_=cv2.findContours(mask,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)#Detectamos los contornos de las mask
    for i in contorns:
        area=cv2.contourArea(i)#Calculamos las áreas de los contornos
        if area>3000:#Nos quedamos unicamente con las áres de interes, eliminando ruido
            momentos=cv2.moments(i)
            if momentos['m00']== 0:momentos['m00']=1
            cx=int(momentos['m10']/momentos['m00'])#Obtenemos cordenadas del centro de lo detectado
            cy=int(momentos['m01']/momentos['m00'])
            area=int(momentos['m00'])#Obtenemos el area de lo detectado
            cv2.putText(frame,(f'Area: {area} Pixeles^2'),(cx-60,cy+20),1,0.5,[255,255,255])#Texto
            cv2.circle(frame,(cx,cy),3,[0,255,255],3)#Circulo
            cv2.putText(frame,(f'x={cx} y= {cy}'),(cx-60,cy-20),1,0.5,[255,255,255])#texto
            cv2.circle(frame,(cx,cy),6,color,3)#Circulo
            xr,yr,w,h=cv2.boundingRect(i)#rectangulo
            _,radio = cv2.minEnclosingCircle(i)#circulo
            radio=int(radio)
            cv2.circle(frame,(cx,cy),radio,0,3)#circulo
            cv2.rectangle(frame,(xr,yr),(xr+w,yr+h),color,3)#rectangulo
imag=cv2.VideoCapture(0)#Capturamos video camara principal
blue=[255,0,0]
red=[0,0,255]
green=[0,255,0]
#mascaras hsv
azulclaro = np.array([100,100,20],np.uint8)#h=100,s=100,v=20
azulfuerte = np.array([125,255,255],np.uint8)#h=125,s=255,v=255
verdeclaro = np.array([40,100,20],np.uint8)#h=40,s=100,v=20
verdefuerte = np.array([80,255,255],np.uint8)#h=80,s=255,v=255
rojoclaro=np.array([170,100,100],np.uint8)#h=170,s=100,v=100
rojofuerte=np.array([179,255,255],np.uint8)#h=179,s=255,v=255
while(True):
    ret, frame = imag.read()#Ret valor boleano cuando detecta o no señal, frame la imagen
    if ret==True:
        hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)#se convierte la captura de imagen a hsv
        maskblue=cv2.inRange(hsv,azulclaro,azulfuerte)#se convinan los rangos de colores hsv para b
        maskgreen=cv2.inRange(hsv,verdeclaro,verdefuerte)#se convinan los rangos de colores hsv para g
        maskred=cv2.inRange(hsv,rojoclaro,rojofuerte)#se convinan los rangos de colores hsv para r
        #Se manda a llamar a la función
        mark(maskblue,blue)
        mark(maskgreen,green)
        mark(maskred,red)
        cv2.imshow('FRAME', frame)#Colores detectados
        cv2.imshow('MASKB',maskblue)#Mascara blue detectando
        cv2.imshow('MASKG',maskgreen)#Mascara green detectando
        cv2.imshow('MASKR',maskred)#Mascara red detectando
        if( cv2.waitKey(1) & 0xFF == ord('s')):#Salir precionando la tecla s
            break
    else:
        print('No se pudo acceder a la camara')
imag.release()
cv2.destroyAllWindows()


print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *")
print("*                                                               *")
print("*                   Coded by Vishwas Luhana                     *")
print("*   Mehran University of Engineering and Technology Jamshoro    *")
print("*     Department of Computer Systems Engineering (18-Batch)     *")
print("*                                                               *")
print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *")

def new_student():
    global all_details
    details = []
    while True:
        print("******************************")
        roll_no = input("Enter roll no: ")
        if roll_no in all_details:
            print("Roll no. already exists. Try other roll no.")
        else:
            break
    details.append(roll_no)
    name = input("Enter name: ")
    details.append(name)
    f_name = input("Enter father's name: ")
    details.append(f_name)
    phone = input("Enter contact number: ")
    details.append(phone)
    address = input("Enter address: ")
    details.append(address)
    all_details[roll_no] = details
    print("***********Student successfully added.***********")

def search_student():
    global all_details
    while True:
        roll_no = input("Enter roll no: ")
        if roll_no in all_details:
            print("******************************")
            print("Roll no.: ", all_details[roll_no][0])
            print("Name: ", all_details[roll_no][1])
            print("Father's name: ", all_details[roll_no][2])
            print("Contact: ", all_details[roll_no][3])
            print("Address: ", all_details[roll_no][4])
            print("******************************")
            break
        else:
            print("***********Could not find record with provided roll no.***********")
            if int(input("Enter 1 to re-enter roll no. \nEnter any other key for Main menu \nEnter your choice: ")) == 1:
                continue
            else:
                break

def edit_detials():
    global all_details
    while True:
        roll_no = input("Enter roll no: ")
        if roll_no in all_details:
            print("******************************")
            print("Previous details: ")
            print("Roll no.: ", all_details[roll_no][0])
            print("Name: ", all_details[roll_no][1])
            print("Father's name: ", all_details[roll_no][2])
            print("Contact: ", all_details[roll_no][3])
            print("Address: ", all_details[roll_no][4])

            while True:
                print("******************************")
                print("What do you want to change: ")
                change = int(input("1. Roll no. \n 2. Name \n 3. Father's name \n 4. Contact \n 5. Address\n Your choice: "))
                if change == 1:
                    new_rollNo = input("Enter new Roll no.: ")
                    all_details[new_rollNo] = all_details[roll_no]
                    all_details[new_rollNo][0] = new_rollNo
                    all_details.pop(roll_no)
                    roll_no = new_rollNo
                    del new_rollNo
                    print("***********Roll no. successfully changed***********")

                elif change == 2:
                    all_details[roll_no][1] = input("Enter new Name: ")
                    print("***********Name successfully changed***********")

                elif change == 3:
                    all_details[roll_no][2] = input("Enter new Father's name: ")
                    print("***********Father's name successfully changed***********")

                elif change == 4:
                    all_details[roll_no][3] = input("Enter new Contact: ")
                    print("***********Contact successfully changed***********")

                elif change == 5:
                    all_details[roll_no][4] = input("Enter new Address: ")
                    print("***********Address successfully changed***********")
                else:
                    print("Invalid choice")
                    continue
                if input("Do you want to change anything else? (Y for YES, any other key for NO): ").capitalize() == "Y":
                    continue
                else: break
            break
        else:
            print("***********Could not find record with provided roll no.***********")
            if int(input("Enter 1 to re-enter roll no. \nEnter any other key for Main menu \nEnter your choice: ")) == 1:
                continue
            else:
                break

all_details = {}
while True:
    print("***********Main menu***********")
    print("1. Add new student")
    print("2. Search for existing student")
    print("3. Edit student details")
    print("Enter any other key to close program")
    choice = int(input("Enter your choice: "))

    if choice == 1:
        new_student()
        continue
    elif choice == 2:
        search_student()
    elif choice == 3:
        edit_detials()
    else:
        break


from Signals import Event

class EventManager (object):
    """EventManager () -> EventManager

    An event distribution system.

    The EventManager enables objects to receive events. Each object can
    register several signal types, on which occurance the EventManager
    will call the object's 'notify' method with that event.

    Events also can be distributed by invoking the 'emit()' method of
    the EventManager.

    Attributes:
    queues       - A dict with signal-list associations of registered objects.
    eventgrabber - The event grabbing object, which will receive all events.
    """

    __slots__ = ["queues", "_grabber"]

    def __init__ (self):
        self.queues = {}
        self._grabber = None

    def add_object (self, obj, *signals):
        """E.add_object (...) -> None

        Adds an object to the EventManger.

        Adds an object as listener for one or more events to the
        EventManager. Each event type in the *signals argument will
        cause the object to be added to a respective queue, on which
        events with the same type will be emitted.

        Raises an AttributeError, if the passed 'obj' argument does
        not have a callable notify attribute.
        """
        if not hasattr (obj, "notify") or not callable (obj.notify):
            raise AttributeError ("notify() method not found in object %s"
                                  % obj)
        for key in signals:
            self.queues.setdefault (key, []).append (obj)

    def remove_object (self, obj, *signals):
        """E.remove_object (...) -> None

        Removes an object from the EventManager.

        Removes the object from the queues passed as the 'signals'
        arguments. If 'signals' is None, the object will be removed
        from all queues of the EventManager.
        """
        if signals:
            evlist = signals
        else:
            evlist = self.queues.keys ()

        for signal in evlist:
            if obj in self.queues[signal]:
                self.queues[signal].remove (obj)

    def grab_events (self, obj):
        """E.grab_events (...) -> None

        Sets an event grabber object for the EventManager.

        Causes the EventManager to send _all_ its event only to this
        object instead of the objects in its queues. It is up to the
        event grabbing object to filter the events, it received.
        """
        if obj and (not hasattr (obj, "notify") or not callable (obj.notify)):
            raise AttributeError ("notify() method not found in object %s"
                                  % obj)
        self._grabber = obj

    def emit (self, signal, data):
        """E.emit (...) -> None

        Emits an event, which will be sent to the objects.

        Emits an event on a specific queue of the EventManager, which
        will be sent to the objects in that queue. If one of the
        receiving objects sets the 'handled' attribute of the event to
        True, the emission will stop immediately so that following
        objects will not receive the event.
        """
        ev = Event (signal, data)
        if self.eventgrabber:
            self.eventgrabber.notify (ev)
            return

        evlist = self.queues.get (signal, [])
        for obj in evlist:
            obj.notify (ev)
            if ev.handled:
                break

    eventgrabber = property (lambda self: self._grabber,
                             lambda self, var: self.grab_events (var),
                             doc = "Sets the event grabber object.")


from turtle import Turtle

class Paddle(Turtle):
    def __init__(self, pos) -> None:
        super().__init__()
        self.penup()
        self.shape('square')
        self.color('white')
        self.shapesize(stretch_wid=5, stretch_len=1)
        self.goto(pos)

    def move_up(self):
        (x, y) = self.pos()
        self.setpos(x, y+20)

    def move_down(self):
        (x, y) = self.pos()
        self.setpos(x, y-20)


import json

import pymongo

host = "127.0.0.1"
port = 27017
client = pymongo.MongoClient(host=host, port=port)
db = client["clone_projects"]
col = db["items"]

with open("../star_proj_get_2021_9_7_pre50.json", "r") as f:
    json_data = json.load(f)

for data in json_data:
    project_name = data["name"]
    cur = col.find_one({"name": project_name})
    if not cur:
        print(project_name)