from googleapiclient.http import MediaFileUpload
from .Google import Create_Service
import sys
import os

sys.path.insert(0, os.getcwd() + '/Drive/')

Client_Secret = 'Drive/client_secret.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPE = ['https://www.googleapis.com/auth/drive']

service = Create_Service(Client_Secret, API_NAME, API_VERSION, SCOPE)

def upload_files(file, parentID):
    file_metadata = {
        'name': file,
        'parents': [parentID]
    }
    media = MediaFileUpload('uploads/{0}'.format(file), mimetype='application/zip')
    temp = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    return temp['id']

def create_folder(folders: list, parentID):
    for folder in folders:
        file_metadata = {
            'name': folder,
            'mimeType': 'application/vnd.google-apps.folder',
            'parents': [parentID]
        }
    sub_folder = service.files().create(body=file_metadata).execute()
    return sub_folder['id']

def create_parent_folder(parentName):
    file_metadata = {
        'name': parentName,
        'mimeType': 'application/vnd.google-apps.folder'
    }
    root_folder = service.files().create(body=file_metadata).execute()
    return root_folder['id']

# parrent_id = '1hDSrXbTMiHYvDDDWd9RiSIH1DIMXY5hB'
# dir = ['vishnu']
#
# folder_id = create_folder(dir,parrent_id)
# print(folder_id)

# download_link = upload_files("111.zip", "1uEdloVc_v0cP55k5Hp8XbYCA_j-ZwrRK")
# print("https://drive.google.com/uc?export=download&id={0}".format(download_link))
def delete_file(id):
    service.files().delete(fileId=id, ).execute()


import time
import machine

class CuentaMs():
    def __init__(self, ms):
        self.ms = ms
        self.reset()

    def reset(self):
        self.proximo = time.ticks_ms() + self.ms

    def comprueba(self):
        if self.proximo <= time.ticks_ms():
            self.reset()
            return True
        else:
            return False

pin = [machine.Pin(14, machine.Pin.OUT),
       machine.Pin(12, machine.Pin.OUT),
       machine.Pin(13, machine.Pin.OUT)]

contador_ms = [CuentaMs(1000), CuentaMs(2333), CuentaMs(1698)]

while True:
    time.sleep(0.1)
    for i, e in enumerate(contador_ms):
        if e.comprueba():
            pin[i].value(not pin[i].value())
            print(" " * i, i, "ha pasado su tiempo")


x=input()
if len(x)<3 :
    print("99")
else :
    a=x[:len(x)-2]
    b=str(int(a)-1)+"99"
    c=a+"99"
    if int(x)-int(b) < int(c)-int(x) :
        print(b if b[0]!='0' else b[1:])
    else :
        print(c)


import os

from datetime import datetime

pathw1="/var/www/html/webcam1/"
pathw2="/var/www/html/webcam2/"
path="/home/pi/motion/"
path_arch = "archive/"
dest="/MalinaBencol/"

year = datetime.now ().strftime ('%Y')
month = datetime.now ().strftime ('%m')
day = datetime.now ().strftime ('%d')
def upload_files():

    if not os.path.exists(pathw1):

        return

    dir_list1 = os.listdir(pathw1)

    for file_name1 in dir_list1:

        if file_name1.endswith(".avi"):

            cmd = "sudo /home/pi/dropbox_uploader.sh upload " + pathw1 + file_name1 + " "+ datetime.now().strftime('%Y-%m-%d') + "/webcam1/" + file_name1

            os.system(cmd)

            if not os.path.exists(path + path_arch):
                os.system ('mkdir ' + path + path_arch)

            if not os.path.exists (path + path_arch + "/" + year + "/"):
                os.system ("mkdir " + path + path_arch + "/" + year)

            os.system("sudo mv /var/www/html/webcam1/" + file_name1 + " /home/pi/motion/archive/" + file_name1)

    if not os.path.exists(pathw2):
        return

    dir_list2 = os.listdir(pathw2)
    for file_name2 in dir_list2:
        if file_name2.endswith(".avi"):
            cmd = "sudo /home/pi/dropbox_uploader.sh upload " + pathw2 + file_name2 + " "+ datetime.now().strftime('%Y-%m-%d') + "/webcam2/" + file_name2
            os.system(cmd)
            if not os.path.exists(path + path_arch):
                os.system ('mkdir ' + path + path_arch)
            if not os.path.exists (path + path_arch + "/" + year + "/"):
                os.system ("mkdir " + path + path_arch + "/" + year)
            os.system("sudo mv /var/www/html/webcam2/" + file_name2 + " /home/pi/motion/archive/" + file_name2)

#if _name_ == "_main_":

upload_files()


import numpy as np
import pdb
st = pdb.set_trace

N = 12
digits = []
totals = np.zeros(N)
lines = 0

with open("inputfile.txt", "r") as ifile:
    for line in ifile:
        lines += 1
        digits.append(int(line, 2))
        for k, digit in enumerate(line):
            try:
                totals[k] += int(digit)
            except IndexError: # newline character
                continue

gamma, epsilon = totals > lines/2, totals < lines/2
gamma_dec, epsilon_dec = 0, 0 # decimal

for k in range(N):
    gamma_dec += gamma[N-k-1] * 2**k
    epsilon_dec += epsilon[N-k-1] * 2**k

print("Gamma: ", gamma_dec, "Epsilon: ", epsilon_dec)
print("Product: ", gamma_dec * epsilon_dec)

## Part 2, better way than before
# def dfilt(data, bit, hi=True):
#     mask = 1 << bit
#     if hi: # which bits are 1
#         return data[data & mask > 0]
#     else: # which bits are 0
#         ## CONTINUE HERE
#         return data[~data & mask > 0]

# def mostcommon(data, bit):
#     mask = 1 << bit
#     return (data & mask != 0).mean() >= 0.5

# def filt_common(data, bits=N):
#     st()
#     data_new = data
#     for b in range(bits):
#         bit = bits - 1 - b
#         hi = mostcommon(data, bit)
#         data_new = dfilt(data, bit, hi)

## Better way still

testdata = np.array([
    0b00100,
    0b11110,
    0b10110,
    0b10111,
    0b10101,
    0b01111,
    0b00111,
    0b11100,
    0b10000,
    0b11001,
    0b00010,
    0b01010])

def filter_bits(data, bits, get_leastcommon=False, debug=False):
    pow2_sum = 0
    for bit in range(bits-1, -1, -1):
        pow2 = 2**bit
        pow2_cmp = pow2 + pow2_sum

        if debug:
            print(data, pow2_cmp)

        if get_leastcommon:
            subset = data < pow2_cmp
        else:
            subset = data >= pow2_cmp

        most_common = np.sum(data >= pow2_cmp) >= data.size / 2 # is 1 or 0 more common

        if most_common:
            if not get_leastcommon:
                pow2_sum = pow2_cmp
        else:
            subset = ~subset
            if get_leastcommon:
                pow2_sum = pow2_cmp

        data = data[subset]
        if data.size == 1:
            break

    return data

# print(list_filt(np.array([1, 2, 3]), 0x1))

# newdata = testdata
# for k in range(5):

print("Test data: ", filter_bits(testdata, bits=5, get_leastcommon=False))

real_data = []
with open("inputfile.txt", "r") as ifile:
    for line in ifile:
        real_data.append(int(line, 2))

real_data = np.array(real_data)

oxy = filter_bits(real_data, bits=12, get_leastcommon=False)
co2 = filter_bits(real_data, bits=12, get_leastcommon=True)

print("Product of oxy and co2: ", oxy * co2)


#SWEA4839<이진탐색>
#변수 이름을 중복되지 않게 설정하자.
T = int(input())

for _ in range(1, T+1):
    P, A, B = map(int, input().split())

    start_a = 1
    cnt_a = 1
    end_a = P
    while True:
        m_a = int((start_a + end_a)/2)
        if A > m_a:
            start_a = m_a
            cnt_a += 1
        elif A < m_a:
            end_a = m_a
            cnt_a += 1
        else:
            break

    start_b = 1
    cnt_b = 1
    end_b = P
    while True:
        m_b = (start_b + end_b) // 2
        if B > m_b:
            start_b = m_b
            cnt_b += 1
        elif B < m_b:
            end_b = m_b
            cnt_b += 1
        else:
            break

    if cnt_a < cnt_b:
        print(f'#{_} A')
    elif cnt_a > cnt_b:
        print(f'#{_} B')
    else:
        print(f'#{_} 0')

'''
1
1000 299 578

1000
500
250
375
312
281
296
304
300
298
299
'''


import os
import mmcv
import urllib.request

dir_path = os.path.dirname(os.getcwd())

labelbox_file = dir_path + '/cleaning/export-2019-07-11T20_41_53.065Z.json'
out_dir = dir_path + '/cleaning/711_with_rotate/'

def main():
    mmcv.mkdir_or_exist(out_dir)

    items = mmcv.load(labelbox_file)
    print('downloading {} images. '.format(len(items)))

    count = 1
    for item in items:
        name = item['DataRow ID']
        image_link = item['Labeled Data']

        # img_data = requests.get(image_link).content
        # with open(out_dir+'{}.jpg'.format(name), 'wb') as handler:
        #     handler.write(img_data)
        urllib.request.urlretrieve(image_link, out_dir+'{}'.format(name))

        print(count)
        count = count + 1

if __name__ == '__main__':
    main()


# Telegram: @ZumbiPy __   _ ___
#  /_  / __ ____ _  / /  (_) _ \__ __
#   / /_/ // /  ' \/ _ \/ / ___/ // /
#  /___/\_,_/_/_/_/_.__/_/_/   \_, /
# E-mail: zumbipy@gmail.com   /___//dev - 17/02/2018
"""
48 - Faça um programa que peça um numero inteiro positivo e em seguida mostre este numero invertido.
Exemplo:
  12376489
  => 98467321
"""

def inverter_numero(numero):
    tnumero = str(numero)
    saida = ''
    for letra in range((len(tnumero) - 1), -1, -1):
        saida += tnumero[letra]
    return int(saida)

numero = int(input("Digite um numero inteiro positivo: "))
if numero > 0:
    print(inverter_numero(numero))
else:
    print("Número digitado não é positovo.")


import numpy as np
import matplotlib.pyplot as plt
import warnings
import os

def classify_kmeans(x, k, epsilon, seed=None, init='k++'):
    '''
    Classify x in R^(n,d) into k groups according to Lloyd's algorithm.
    Exit condition is that new centers are maximally displaced by epsilon.
    @init: allowed options are 'k++', 'box'
    @k: must be >=1

    '''
    n,d = x.shape
    if seed:
        np.random.seed(seed)

    if init == 'box':
        bbox = np.array([np.min(x, axis=0), np.max(x, axis=0)]) # Initialize within data range.
        centers = np.random.rand(k,d)*(bbox[1]-bbox[0])+bbox[0]
    elif init == 'k++':
        indices = np.arange(n)
        centers = x[np.random.choice(indices),:].reshape(1,d)
        for i in range(k-1):
            distances = np.array([np.sum(np.square(xi-centers), axis=1) for xi in x])
            weights = np.min(distances, axis=1)
            weights = weights/np.sum(weights)
            centers = np.append(centers, x[np.random.choice(indices, p=weights),:].reshape(1,d), axis=0)
    else:
        print('Unknown initialization scheme {}'.format(init))
        return None, None, None

    objective = []
    condition = True
    while condition:
        distances = np.array([np.sum(np.square(xi-centers), axis=1)  for xi in x])
        classify = np.argmin(distances, axis=1)
        objective.append(np.sum([np.sum(distances[classify==c, c])/np.sum(classify==c)
                                 if np.any(classify==c) else 0 for c in range(k)]))
        # If no points in class, reuse old center
        new_centers = np.array([np.mean(x[classify==c], axis=0) if np.any(classify==c)
                                else centers[c] for c in range(k)])
        if np.max(np.sum(np.square(new_centers - centers), axis=1)) < epsilon:
            condition = False
        centers = np.copy(new_centers)
    return classify, centers, objective

def clean_ax(ax):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')

# =================================================================================================
if __name__ == '__main__':
    from mnist import MNIST
    mndata = MNIST(r'../CSE-546-HW-1/data/')
    X_train, labels_train = map(np.array, mndata.load_training())
    X_test, labels_test = map(np.array, mndata.load_testing())
    X_train = X_train/255.0
    X_test = X_test/255.0

    # 1. Plot centers for uniform (bounding box) initialization
    all_classes = []
    all_centers = []
    all_objectives = []
    for k in [5,10,20]:
        kclasses, kcenters, kobj = classify_kmeans(X_train, k, epsilon=1e-1, init='box')
        # Save data
        all_classes.append(kclasses)
        all_centers.append(kcenters)
        all_objectives.append(kobj)
        # Plot center images
        fig, ax = plt.subplots(1, k, figsize=[k,10])
        for c, anAx in zip(kcenters, ax):
            anAx.imshow(c.reshape([28,28]))
            clean_ax(anAx)
        fig.subplots_adjust(wspace=0, hspace=0)
        fig.savefig(os.path.join(r'./figs','imshow_box_{}.pdf'.format(k)))

    # 2. Plot centers for k-means++ initialization
    all_classes_2 = []
    all_centers_2 = []
    all_objectives_2 = []
    for k in [5,10,20]:
        kclasses, kcenters, kobj = classify_kmeans(X_train, k, epsilon=1e-1, init='k++')
        # Save data
        all_classes_2.append(kclasses)
        all_centers_2.append(kcenters)
        all_objectives_2.append(kobj)
        # Plot center images
        fig, ax = plt.subplots(1, k, figsize=[k,10])
        for c, anAx in zip(kcenters, ax):
            anAx.imshow(c.reshape([28,28]))
            clean_ax(anAx)
        fig.subplots_adjust(wspace=0, hspace=0)
        fig.savefig(os.path.join(r'./figs','imshow_kpp_{}.pdf'.format(k)))

    # 3. Plot k-means objective as a function of iteration
    fig, ax = plt.subplots(1,2, figsize=[10,4])

    for obj in all_objectives:
        ax[0].plot(np.arange(len(obj)), obj, lw=5, alpha=0.75)
        ax[0].set_xlabel('Iteration (box)', fontsize = 12)
        ax[0].set_ylabel('k-means Objective', fontsize=12)
    ax[0].legend(['k=5','k=10','k=20'],fontsize=12)
    ax[0].grid(True)

    for obj in all_objectives_2:
        ax[1].plot(np.arange(len(obj)), obj, lw=5, alpha=0.75)
        ax[1].set_xlabel('Iteration (k++)', fontsize = 12)
        ax[1].set_yticklabels([])
    ax[1].legend(['k=5','k=10','k=20'],fontsize=12)
    ax[1].grid(True)

    # Fix shared axis size using (experimental) limits
    for iax in ax:
        iax.set_ylim([0,3000])
        iax.set_xlim([-1,15])

    fig.subplots_adjust(wspace=0)
    fig.savefig(os.path.join(r'./figs','kmeans_combo_objs.pdf'))


import cv2
import numpy as np

colorPath = "Images/week1/boy.jpg"

img = cv2.imread(colorPath, cv2.IMREAD_COLOR)
new_img = img.copy()

#creating an empty matrix of desired dimensions
empty_mat1 = np.zeros((100,200,3), dtype = 'uint8')
empty_mat2 = 255 * np.ones((100,200,3), dtype = 'uint8')

#creating an empty matrix similar to a existing image
mat1 = np.zeros_like(img)
mat2 = 255 * np.ones_like(img)

print(mat1.shape)
cv2.imshow("hello", mat2)
cv2.waitKey(0)
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
"""
Created on Wed Nov 11 23:55:50 2020

@author: kalan
"""
# =============================================================================
#
# 500. Keyboard Row
# Easy
#
# Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

# Example:
#
# Input: ["Hello", "Alaska", "Dad", "Peace"]
# Output: ["Alaska", "Dad"]
# =============================================================================
# =============================================================================

class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        row1 = 'qwertyuiop'
        row2 = 'asdfghjkl'
        row3 = 'zxcvbnm'

        out= []

        cnt=0
        for word in words:
            for letter in word:
                if letter.lower() in row2:
                    cnt+= 1
            #        print(letter, cnt)

            if len(word) == cnt:
                out.append(word)
            cnt =0

        cnt=0
        for word in words:
            for letter in word:
                if letter.lower() in row1:
                    cnt+= 1
             #       print(letter, cnt)

            if len(word) == cnt:
                out.append(word)
            cnt =0

        cnt=0
        for word in words:
            for letter in word:
                if letter.lower() in row3:
                    cnt+= 1
              #      print(letter, cnt)

            if len(word) == cnt:
                out.append(word)
            cnt =0
        return out

a=Solution()
a.findWords( words =  ["Hello", "Alaska", "Dad", "Peace"])

        #words =['Alaska', 'Dad']


from django.shortcuts import render
from .models import Collection
from product.models import Product
from user.models import Customer
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.db.models import Count
import json

# Create your views here.

def returnJson(data=None, pageCount=0, errorCode=0):
	if data is None:
		data = []
	return JsonResponse({'errorCode': errorCode, 'data': data, 'pageCount' : int(pageCount)})

# collection
@login_required
def customer_collection_list(request):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	collections = Collection.objects.filter(customer=customer)

	return returnJson([dict(collection.body()) for collection in collections])

@login_required
def latest_customer_collection_list(request):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	collections = Collection.objects.filter(customer=customer).order_by('-id')

	return returnJson([dict(collection.body()) for collection in collections])

def latest_customer_collection_list_by_page(request, pageNum):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	collections = Collection.objects.filter(customer=customer)
	pages = (collections.count()+9)/10
	collections = collections.order_by('-id')[((pageNum-1)*10):(pageNum*10)]

	return returnJson([dict(collection.body()) for collection in collections],pages)

@login_required
def create_customer_collection(request):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	data = json.loads(request.body)

	try:
		product = Product.objects.get(id = data["productId"])
	except Product.DoesNotExist:
		return returnJson([],0, 404)

	try:
		collection = Collection.objects.get(customer=customer, product=product)
	except Collection.DoesNotExist:
		collection = Collection.objects.create(product=product, customer=customer)
		return returnJson([dict(collection.body())])

	return returnJson([],0, 400)

@login_required
def get_customer_collection(request, pk_product):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	try:
		product = Product.objects.get(id = pk_product)
	except Product.DoesNotExist:
		return returnJson([],0, 404)

	try:
		collection = Collection.objects.get(customer=customer, product=product)
	except Collection.DoesNotExist:
		return returnJson([],0, 404)

	return returnJson([dict(collection.body())])

@login_required
def edit_customer_collection(request, pk_product):
	try:
		customer = Customer.objects.get(id=request.user.id)
	except Customer.DoesNotExist:
		return returnJson([],0, 403)

	try:
		product = Product.objects.get(id = pk_product)
	except Product.DoesNotExist:
		return returnJson([],0, 404)

	try:
		collection = Collection.objects.get(customer=customer, product=product)
	except Collection.DoesNotExist:
		return returnJson([],0, 404)

	if request.method == 'DELETE':
		collection.delete()
		return returnJson()

def most_popular_product_list_by_page(request,pageNum):
	products = Product.objects.annotate(count = Count('collection'))
	pages = (products.count()+9)/10
	products = products.order_by('-count')[((pageNum-1)*10):(pageNum*10)]
	return returnJson([dict(product.body()) for product in products], pages)

def most_popular_product_list_by_category_and_page(request,pageNum):
	data = json.loads(request.body)
	products = Product.objects.filter(category=data["category"]).annotate(count = Count('collection'))
	pages = (products.count()+9)/10
	products = products.order_by('-count')[((pageNum-1)*10):(pageNum*10)]

	return returnJson([dict(product.body()) for product in products], pages)


import socket
import sys
import os
import logging
import random
import _thread
from checksum import Checksum
from packet import Packet

logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")

UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789

class GBN:
    def __init__(self, windowSize, sequenceBit):
        self.sequence_bits = sequenceBit
        self.window_size = windowSize
        self.expected_seq_number = 1
        self.once = True

    def receive_packet(self, packet):
        if packet.sequenceNumber == 4 and self.once:
            self.once = False
            return self.expected_seq_number, True
        if packet.sequenceNumber == self.expected_seq_number:
            self.expected_seq_number += 1
            return self.expected_seq_number, False
        else:
            return self.expected_seq_number, True  # to discard because the packet is either out of order or duplicate

class SR:
    def __init__(self, windowSize, sequenceBit):
        self.window_size = windowSize
        self.sequence_bits = sequenceBit
        self.r_base = 1
        self.sequence_max = 2 ** self.sequence_bits
        self.queue = {}
        self.next_seq_num = 1
        self.mutex = _thread.allocate_lock()  # slide window mutex

    def is_packet_in_order(self, sequenceNumber):
        if sequenceNumber in self.queue:
            return True
        elif sequenceNumber < self.next_seq_num:
            return True
        else:
            return False

    def add_one_to_queue(self):
        self.queue[self.next_seq_num] = 'waiting'
        self.next_seq_num += 1

    def slide_window(self):
        for key in self.queue:
            if self.queue[key] == 'rcvd':
                del self.queue[key]
                self.add_one_to_queue()
            else:
                return

    def init_queue(self):
        for i in range(self.r_base, self.window_size + 1):
            self.add_one_to_queue()

    def receive_packet(self, packet):
        self.mutex.acquire()
        if self.is_packet_in_order(packet.sequenceNumber):
            self.queue[packet.sequenceNumber] = 'rcvd'
            self.slide_window()
            self.mutex.release()
            return packet.sequenceNumber, False
        else:
            self.mutex.release()
            return packet.sequenceNumber, True  # to discard because the packet is out of order

class UDPHelper:
    def __init__(self, protocol_name, windowSize, sequenceBit):
        self.ip_address = '127.0.0.1'
        self.port_number = 6789
        self.serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
        self.receiver_running = False
        self.receiver = None
        self.protocol = None
        self.packets = 0
        self.selectedPacket = 5
        if protocol_name == "GBN":
            self.protocol = GBN(windowSize, sequenceBit)
        elif protocol_name == "SR":
            self.protocol = SR(windowSize, sequenceBit)
            self.protocol.init_queue()
        else:
            raise Exception('***** Invalid protocol name *****')

    def simulatePacketLoss(self):
        self.packets += 1
        if self.packets == 10:
            self.packets = 0
            self.selectedPacket = random.randint(0, 10)
        if self.selectedPacket == self.packets:
            self.selectedPacket = None
            return True
        return False

    def startListening(self):
        while True:
            try:
                data, addr = self.serverSock.recvfrom(1024)
                packet = Packet()
                packet.deserializePacket(data)

                if self.simulatePacketLoss():
                    print("!!!!! Packet: ", packet.sequenceNumber, " lost :(   !!!!!")
                    continue

                if Checksum.verify(packet.data, packet.checksum):
                    ack_num, discard = self.protocol.receive_packet(packet)
                    if discard:
                        print("!!!!! Discarding packet: " + str(packet.sequenceNumber), " !!!!!")
                    else:
                        print("!!!!! Received Segment: ", str(packet.sequenceNumber), " !!!!!")
                    _ = self.serverSock.sendto(str(ack_num).encode(), addr)  # sending ack with ack_num
                    print("!!!!! ACK Sent: ", str(ack_num) ,"!!!!!")
                else:
                    print("!!!!! Checksum Error!!!!! Packet: ", packet.sequenceNumber, " discarded !!!!!")

            except KeyboardInterrupt:
                print('*****Interrupted*****')
                os._exit(0)
            except ConnectionResetError:
                pass  # Do nothing.
            except Exception:
                raise Exception
        return

    def waitToReceive(self):
        self.receiver.join()
        return

if __name__ == "__main__":
    if len(sys.argv) is not 3:
        print("***** Please insert 4 arguments! ***** ")
        print("***** Syntax *****")
        print("***** Receiver.py inputfile(GBN/SR) portNumber *****")
    else:
        try:
            UDP_PORT_NO = int(sys.argv[2])
            file = open(sys.argv[1]).readlines()
            protocol = file[0].strip()
            sequence_bits = int(file[1].strip().split(' ')[0])
            window_size = int(file[1].strip().split(' ')[1])
            timeout_period = float(file[2].strip())
            segment_size = int(file[3].strip())

            if protocol == "GBN":
                udp_helper = UDPHelper('GBN', window_size, sequence_bits)
            elif protocol == "SR":
                udp_helper = UDPHelper('SR', window_size, sequence_bits)

            udp_helper.startListening()

        except Exception:
            raise Exception

Go-back-n and Selective Repeat/receiver.py


import krogon.either as E
import json
from krogon import run_in_cluster,  gke_conn
from krogon.k8s import cron_job
from tests.helpers.mock_file_system import find_write_template_calls, MockFileSystem
from tests.helpers.mock_os_system import MockOsSystem
from base64 import b64encode
from tests.helpers.entry_mocks import mock_krogon_dsl

def test_can_generate_cron_job_template():
    def _run_dsl(args):
        os: MockOsSystem = args["os_system"]
        fs: MockFileSystem = args['file_system']
        project_id = "project1"
        job_name = 'test-service'
        cluster_name = 'prod-us-east1'
        kubectl_version = "v1.15.3"
        image_url = "test-service:1.0.0"
        service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8'))

        os.mock_clusters_list([cluster_name])
        os.mock_kubernetes_release(E.success(kubectl_version))
        os.mock_download_install_kubectl(kubectl_version, E.success())
        os.mock_create_kube_config(cluster_name, E.success())
        os.mock_kubectl_apply_temp_file(cluster_name, E.success())

        _, result = run_in_cluster(
            conn=gke_conn(cluster_name, project_id, service_account_b64),
            templates=[
                cron_job(job_name, image_url)
            ]
        )

        yamls = find_write_template_calls(fs)
        assert yamls[0]['kind'] == 'CronJob'
        assert yamls[0]['metadata']['name'] == job_name
        container = yamls[0]['spec']['jobTemplate']['spec']['template']['spec']['containers'][0]
        assert container['name'] == job_name
        assert container['image'] == image_url

    mock_krogon_dsl(_run_dsl)

def test_can_set_secret_for_job():
    def _run_dsl(args):
        fs: MockFileSystem = args['file_system']
        os: MockOsSystem = args["os_system"]
        project_id = "project1"
        job_name = 'test-service'
        cluster_name = 'prod-us-east1'
        kubectl_version = "v1.15.3"
        image_url = "test-service:1.0.0"
        service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8'))

        os.mock_clusters_list([cluster_name])
        os.mock_kubernetes_release(E.success(kubectl_version))
        os.mock_download_install_kubectl(kubectl_version, E.success())
        os.mock_create_kube_config(cluster_name, E.success())
        os.mock_kubectl_apply_temp_file(cluster_name, E.success())

        _, result = run_in_cluster(
            conn=gke_conn(cluster_name, project_id, service_account_b64),
            templates=[
                cron_job(job_name, image_url)
                    .with_environment_secret('coolSecret', {'ENV_NAME': 'secretkey'})
            ]
        )

        yamls = find_write_template_calls(fs)
        container = yamls[0]['spec']['jobTemplate']['spec']['template']['spec']['containers'][0]
        assert container['env'][0]['name'] == 'ENV_NAME'
        assert container['env'][0]['valueFrom']['secretKeyRef']['name'] == 'coolSecret'
        assert container['env'][0]['valueFrom']['secretKeyRef']['key'] == 'secretkey'

    mock_krogon_dsl(_run_dsl)


class Solution(object):
    def dfs(self, remains,  visited, n, k, solution):
        if n < k:
            solution.append(visited)
            ln = len(remains)
            lv = len(visited)
            for i in range(ln):
                temp = visited[:]
                temp.append(remains[i])
                self.dfs(remains[i+1:], temp, n+1, k, solution)
        else:
            solution.append(visited)
    def dfs_2(self, remains, visited, n, k, solution):
        if n < k:
            ln = len(remains)
            for i in range(ln):
                temp = visited[:]
                temp.remove(remains[i])
                self.dfs_2(remains[i+1:], temp, n+1, k, solution)
        else:
            solution.append(visited)
    def combine(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[List[int]]
        """
        remains = [i+1 for i in range(n)]
        solution = []
        self.dfs(remains, [], 0, k, solution)
        return solution

if __name__ == "__main__":
    solution = Solution()
    answer = solution.combine(3,3)
    print(answer)


import setuptools

# from src.aio_dtls import __version__

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name='aio_dtls',
    version='0.0.2',  # __version__,
    test_requires=[],
    url='https://github.com/businka/aio_dtls',
    license='MIT',
    author='Razgovorov Mikhail',
    author_email='1338833@gmail.com',
    description='asyncio implementation of the dtls protocol in python',
    long_description=long_description,
    long_description_content_type="text/markdown",
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Natural Language :: Russian',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Topic :: Software Development',
        "Operating System :: OS Independent"
    ],
    keywords='python tls dtls mbed asyncio',
    python_requires='>=3.9',
    zip_safe=False,
    install_requires=[
        "construct",
        "cryptography",
    ],
    package_dir={"": "src"},
    packages=setuptools.find_packages(where="src")
)


from datetime import datetime, timedelta

from bs4 import BeautifulSoup
import pytest

from app.database.models import Event
from app.routers.dayview import (
    DivAttributes,
    is_all_day_event_in_day,
    is_specific_time_event_in_day,
)

from app.routers.event import create_event

def create_dayview_event(events, session, user):
    for event in events:
        create_event(
            db=session,
            title="test",
            start=event.start,
            end=event.end,
            owner_id=user.id,
            color=event.color,
        )

def test_minutes_position_calculation(event_with_no_minutes_modified):
    div_attr = DivAttributes(event_with_no_minutes_modified)
    assert div_attr._minutes_position(div_attr.start_time.minute) is None
    assert div_attr._minutes_position(div_attr.end_time.minute) is None
    assert div_attr._minutes_position(0) is None
    assert div_attr._minutes_position(60) == 4

def test_div_attributes(event1):
    div_attr = DivAttributes(event1)
    assert div_attr.total_time == "07:05 - 09:15"
    assert div_attr.grid_position == "32 / 40"
    assert div_attr.length == 130
    assert div_attr.color == "grey"

@pytest.mark.parametrize(
    "minutes,css_class,visiblity",
    [
        (90, "title-size-small", True),
        (45, "title-size-xsmall", False),
        (30, "title-size-tiny", False),
    ],
)
def test_font_size_attribute(minutes, css_class, visiblity):
    start = datetime(year=2021, month=2, day=3, hour=7)
    end = start + timedelta(minutes=minutes)
    event = Event(
        title="test",
        content="test",
        start=start,
        end=end,
        owner_id=1,
    )
    div_attr = DivAttributes(event)
    assert div_attr.title_size_class == css_class
    assert div_attr.total_time_visible == visiblity

def test_div_attr_multiday(multiday_event):
    day = datetime(year=2021, month=2, day=1)
    assert DivAttributes(multiday_event, day).grid_position == "55 / 101"
    day += timedelta(hours=24)
    assert DivAttributes(multiday_event, day).grid_position == "1 / 101"
    day += timedelta(hours=24)
    assert DivAttributes(multiday_event, day).grid_position == "1 / 55"

def test_is_specific_time_event_in_day(all_day_event1, event3):
    day = datetime(year=2021, month=2, day=3, hour=0, minute=0)
    day_end = day + timedelta(hours=24)
    function_returns_true = is_specific_time_event_in_day(
        event=event3,
        day=day,
        day_end=day_end,
    )
    function_returns_false = is_specific_time_event_in_day(
        event=all_day_event1,
        day=day,
        day_end=day_end,
    )
    assert function_returns_true
    assert not function_returns_false

def test_is_all_day_event_in_day(all_day_event1, event3):
    day = datetime(year=2021, month=2, day=3, hour=0, minute=0)
    day_end = day + timedelta(hours=24)
    function_returns_true = is_all_day_event_in_day(
        event=all_day_event1,
        day=day,
        day_end=day_end,
    )
    function_returns_false = is_all_day_event_in_day(
        event=event3,
        day=day,
        day_end=day_end,
    )
    assert function_returns_true
    assert not function_returns_false

def test_div_attributes_with_costume_color(event2):
    div_attr = DivAttributes(event2)
    assert div_attr.color == "blue"

def test_wrong_timeformat(session, user, client, event1, event2, event3):
    create_dayview_event([event1, event2, event3], session=session, user=user)
    response = client.get("/day/1-2-2021")
    assert response.status_code == 404

def test_dayview_html(event1, event2, event3, session, user, client):
    create_dayview_event([event1, event2, event3], session=session, user=user)
    response = client.get("/day/2021-2-1")
    soup = BeautifulSoup(response.content, "html.parser")
    assert "FEBRUARY" in str(soup.find("div", {"id": "top-tab"}))
    assert "event1" in str(soup.find("div", {"id": "event1"}))
    assert "event2" in str(soup.find("div", {"id": "event2"}))
    assert soup.find("div", {"id": "event3"}) is None

@pytest.mark.parametrize(
    "day,grid_position",
    [
        ("2021-2-1", "55 / 101"),
        ("2021-2-2", "1 / 101"),
        ("2021-2-3", "1 / 55"),
    ],
)
def test_dayview_html_with_multiday_event(
    multiday_event,
    session,
    user,
    client,
    day,
    grid_position,
):
    create_dayview_event([multiday_event], session=session, user=user)
    session.commit()
    response = client.get(f"/day/{day}")
    soup = BeautifulSoup(response.content, "html.parser")
    grid_pos = f"grid-row: {grid_position};"
    assert grid_pos in str(soup.find("div", {"id": "event1"}))


def is_abecedarian(wort):
    """Returns True if the letters in a given word appear in alphabetical order."""
    for i in range(len(wort) - 1):
        if wort[i] > wort[i + 1]:
            return False
    return True

fin = open("/Users/hejtor/OneDrive/CS/ThinkPython stuff/words.txt")

counter = 0
abc_counter = 0

for line in fin:
    counter += 1
    word = line.strip()
    if is_abecedarian(word):
        abc_counter += 1
        print(word)

print(abc_counter, "words have their letters in alphabetical order.")


from time import sleep

from IoTPy.interfaces.gpio import GPIO

from IoTPy.boards.uper import UPER1

def button_callback(event, obj):
    obj['counter'] += 1
    print("Interrupt %i" % obj['counter'])

    button_state = (event['values'] >> event['id']) & 0x1
    obj['led'].write(button_state)

with UPER1() as board, board.digital(27) as redPin, board.digital(18) as buttonPin:

    my_object = {'led': redPin, 'counter': 0}
    buttonPin.attach_irq(GPIO.CHANGE, button_callback, my_object)

    redPin.setup(GPIO.OUTPUT)
    redPin.write(1)

    while True:
        sleep(0.5)


"""
  Harness for conducting black box optimisation experiments.
  -- kandasamy@cs.cmu.edu
"""

# pylint: disable=invalid-name
# pylint: disable=no-member
# pylint: disable=abstract-class-not-used
# pylint: disable=star-args
# pylint: disable=maybe-no-member

from argparse import Namespace
from datetime import datetime
import numpy as np
import os
# Local imports
from opt.blackbox_optimiser import Initialiser
from opt.ga_optimiser import ga_optimise_from_args
from opt.nn_gp_bandit import nngpb_from_func_caller, nnrandbandit_from_func_caller
from opt.nn_opt_utils import get_initial_pool
from utils.experimenters import BasicExperimenter

class NNOptExperimenter(BasicExperimenter):
  """ Base class for running experiments. """
  # pylint: disable=attribute-defined-outside-init

  def __init__(self, experiment_name, func_caller, worker_manager, max_capital, methods,
               num_experiments, save_dir, experiment_options, save_file_prefix='',
               method_options=None, reporter=None, **kwargs):
    """ Constructor. Also see BasicExperimenter for more args. """
    # pylint: disable=too-many-arguments
    save_file_name = self._get_save_file_name(save_dir, experiment_name,
      worker_manager.num_workers, save_file_prefix, worker_manager.get_time_distro_info(),
      max_capital)
    super(NNOptExperimenter, self).__init__(experiment_name, num_experiments,
                                            save_file_name, reporter=reporter, **kwargs)
    self.func_caller = func_caller
    self.worker_manager = worker_manager
    self.max_capital = float(max_capital)
    # Methods
    self.methods = methods
    self.num_methods = len(methods)
    self.domain = func_caller.domain
    self.method_options = (method_options if method_options else
                           {key: None for key in methods})
    # Experiment options will have things such as if the evaluations are noisy,
    # the time distributions etc.
    self.experiment_options = experiment_options
    self._set_up_saving()

  @classmethod
  def _get_save_file_name(cls, save_dir, experiment_name, num_workers, save_file_prefix,
                          time_distro_str, max_capital):
    """ Gets the save file name. """
    save_file_prefix = save_file_prefix if save_file_prefix else experiment_name
    save_file_name = '%s-M%d-%s-c%d-%s.mat'%(save_file_prefix, num_workers,
      time_distro_str, int(max_capital), datetime.now().strftime('%m%d-%H%M%S'))
    save_file_name = os.path.join(save_dir, save_file_name)
    return save_file_name

  def _set_up_saving(self):
    """ Runs some routines to set up saving. """
    # Store methods and the options in to_be_saved
    self.to_be_saved.max_capital = self.max_capital
    self.to_be_saved.num_workers = self.worker_manager.num_workers
    self.to_be_saved.methods = self.methods
    self.to_be_saved.method_options = self.method_options # Some error created here.
    self.to_be_saved.time_distro_str = self.worker_manager.get_time_distro_info()
    # Data about the problem
    self.to_be_saved.true_opt_val = (self.func_caller.opt_val
      if self.func_caller.opt_val is not None else -np.inf)
    self.to_be_saved.true_opt_pt = (self.func_caller.opt_pt
      if self.func_caller.opt_pt is not None else 'not-known')
    self.to_be_saved.domain_type = self.domain.get_type()
#     # For the results
    self.data_to_be_saved = ['query_step_idxs',
                             'query_points',
                             'query_vals',
                             'query_true_vals',
                             'query_send_times',
                             'query_receive_times',
                             'query_eval_times',
                             'curr_opt_vals',
                             'curr_true_opt_vals',
                             'num_jobs_per_worker',
                            ]
    self.data_not_to_be_mat_saved.extend(['method_options', 'query_points'])
    self.data_not_to_be_pickled.extend(['method_options'])
    for data_type in self.data_to_be_saved:
      setattr(self.to_be_saved, data_type, self._get_new_empty_results_array())

  def _get_new_empty_results_array(self):
    """ Returns a new empty arrray to be used for saving results. """
#     return np.empty((self.num_methods, 0), dtype=np.object)
    return np.array([[] for _ in range(self.num_methods)], dtype=np.object)

  def _get_new_iter_results_array(self):
    """ Returns an empty array to be used for saving results of current iteration. """
#     return np.empty((self.num_methods, 1), dtype=np.object)
    return np.array([['-'] for _ in range(self.num_methods)], dtype=np.object)

  def _print_method_header(self, full_method_name):
    """ Prints a header for the current method. """
    experiment_header = '-- Exp %d/%d on %s:: %s with cap %0.4f. ----------------------'%(
      self.experiment_iter, self.num_experiments, self.experiment_name, full_method_name,
      self.max_capital)
    self.reporter.writeln(experiment_header)

  def get_iteration_header(self):
    """ Header for iteration. """
    noisy_str = ('no-noise' if self.func_caller.noise_type == 'none' else
                 'noisy(%0.2f)'%(self.func_caller.noise_scale))
    opt_val_str = ('?' if self.func_caller.opt_val is None
                       else '%0.5f'%(self.func_caller.opt_val))
    ret = '%s (M=%d), td: %s, max=%s, max-capital %0.2f, %s'%(self.experiment_name,
      self.worker_manager.num_workers, self.to_be_saved.time_distro_str, opt_val_str,
      self.max_capital, noisy_str)
    return ret

  def _print_method_result(self, method, comp_opt_val, num_evals):
    """ Prints the result for this method. """
    result_str = 'Method: %s achieved max-val %0.5f in %d evaluations.\n'%(method,
                  comp_opt_val, num_evals)
    self.reporter.writeln(result_str)

  def _get_pre_eval_points_and_vals(self):
    """ Gets Initial points for all methods. """
    if self.experiment_options.pre_eval_points == 'generate':
      init_pool = get_initial_pool(self.domain.get_type())
    else:
      # Load from the file.
      raise NotImplementedError('Not written reading results from file yet.')
    # Create an initialiser
    initialiser = Initialiser(self.func_caller, self.worker_manager)
    initialiser.options.get_initial_points = lambda _: init_pool
    initialiser.options.max_num_steps = 0
    _, _, init_hist = initialiser.initialise()
    pre_eval_points = init_hist.query_points
    pre_eval_vals = init_hist.query_vals
    pre_eval_true_vals = init_hist.query_true_vals
    return pre_eval_points, pre_eval_vals, pre_eval_true_vals

  def run_experiment_iteration(self):
    """ Runs each method in self.methods once and stores the results to be saved. """
    curr_iter_results = Namespace()
    for data_type in self.data_to_be_saved:
      setattr(curr_iter_results, data_type, self._get_new_iter_results_array())

    # Fetch pre-evaluation points.
    self.worker_manager.reset()
    (pre_eval_points_for_all_meths, pre_eval_vals_for_all_meths,
     pre_eval_true_vals_for_all_meths) = self._get_pre_eval_points_and_vals()
    self.reporter.writeln('Using %d pre-eval points with values: eval: %s, true: %s'%(
      len(pre_eval_vals_for_all_meths), pre_eval_vals_for_all_meths,
      pre_eval_true_vals_for_all_meths))

    # Will go through each method in this loop.
    for meth_iter in range(self.num_methods):
      curr_method = self.methods[meth_iter]
      curr_options = self.method_options[curr_method]
      # Set pre_eval points and vals
      curr_options.pre_eval_points = pre_eval_points_for_all_meths
      curr_options.pre_eval_vals = pre_eval_vals_for_all_meths
      curr_options.pre_eval_true_vals = pre_eval_true_vals_for_all_meths
      # Reset worker manager
      self.worker_manager.reset()
      self.reporter.writeln('\nResetting worker manager: worker_manager.optimiser:%s'%(
                            str(self.worker_manager.optimiser)))

      # Call the method here.
      self._print_method_header(curr_method)
      _, _, history = optimise_with_method_on_func_caller(curr_method, self.func_caller,
                        self.worker_manager, self.max_capital, meth_options=curr_options,
                        reporter=self.reporter)

      # Now save results for current method
      for data_type in self.data_to_be_saved:
        data = getattr(history, data_type)
        data_pointer = getattr(curr_iter_results, data_type)
        data_pointer[meth_iter, 0] = data
      # Print out results
      comp_opt_val = history.curr_true_opt_vals[-1]
      num_evals = len(history.curr_true_opt_vals)
      self._print_method_result(curr_method, comp_opt_val, num_evals)
      # Save results of current iteration
      self.update_to_be_saved(curr_iter_results)
      self.save_pickle()
      self.save_results()
      # for meth_iter ends here
    # Save here
    self.update_to_be_saved(curr_iter_results)
    self.save_pickle()
    # No need to explicitly save_results() here - it is done by the parent class.

  def update_to_be_saved(self, curr_iter_results):
    """ Updates the results of the data to be saved with curr_iter_results."""
    for data_type in self.data_to_be_saved:
      data = getattr(curr_iter_results, data_type)
      curr_data_to_be_saved = getattr(self.to_be_saved, data_type)
      if curr_data_to_be_saved.shape[1] == self.experiment_iter:
        updated_data_to_be_saved = curr_data_to_be_saved
        updated_data_to_be_saved[:, -1] = data.ravel()
      elif curr_data_to_be_saved.shape[1] < self.experiment_iter:
        updated_data_to_be_saved = np.concatenate((curr_data_to_be_saved, data), axis=1)
      else:
        raise ValueError('Something wrong with data saving.')
      setattr(self.to_be_saved, data_type, updated_data_to_be_saved)

# --------------------------------------------------------------------------------------
def optimise_with_method_on_func_caller(method, func_caller, worker_manager, max_capital,
                                        meth_options, reporter):
  """ This function does the optimisation. """
  # Mode should be specified in the first three letters
  meth_options.mode = method[0:3]
  specific_method = method[3:]
  if specific_method == 'GA':
    return ga_optimise_from_args(func_caller, worker_manager, max_capital,
                                 meth_options.mode, meth_options.mutation_op,
                                 options=meth_options, reporter=reporter)
  elif specific_method in ['HEI', 'EI', 'HTS', 'TS', 'HUCB', 'UCB']:
    meth_options.acq = specific_method
    return nngpb_from_func_caller(func_caller, worker_manager, meth_options.tp_comp,
                                  max_capital, acq=specific_method,
                                  options=meth_options, reporter=reporter)
  elif specific_method == 'RAND':
    meth_options.acq = specific_method
    return nnrandbandit_from_func_caller(func_caller, worker_manager, max_capital,
                                         options=meth_options, reporter=reporter)
  else:
    raise ValueError('Unknown method: %s'%(method))


import re

import pymongo

from asyncmongoengine.base.document import BaseDocument
from asyncmongoengine.base.errors import NotUniqueError, OperationError
from asyncmongoengine.base.metaclasses import (DocumentMetaclass,
                                               TopLevelDocumentMetaclass)
from asyncmongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
from asyncmongoengine.context_managers import set_write_concern

class EmbeddedDocument(BaseDocument, metaclass=DocumentMetaclass):

    __slots__ = ("_instance",)

    my_metaclass = DocumentMetaclass

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._instance = None
        self._changed_fields = []

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self._data == other._data
        return False

    def __ne__(self, other):
        return not self.__eq__(other)

class Document(BaseDocument, metaclass=TopLevelDocumentMetaclass):

    my_metaclass = TopLevelDocumentMetaclass

    __slots__ = ("__objects",)

    @property
    def pk(self):
        return None

    async def save_async(
        self,
        force_insert=False,
        validate=True,
        clean=True,
        write_concern=None,
        _refs=None,
        **kwargs
    ):
        if validate:
            self.validate(clean=clean)

        doc = self.to_mongo()

        if write_concern is None:
            write_concern = {}

        try:
            # Save a new document or update an existing one
            object_id = await self._save_create_async(doc, force_insert, write_concern)

        except pymongo.errors.DuplicateKeyError as err:
            message = "Tried to save duplicate unique keys (%s)"
            raise NotUniqueError(message % err)
        except pymongo.errors.OperationFailure as err:
            message = "Could not save document (%s)"
            if re.match("^E1100[01] duplicate key", str(err)):
                # E11000 - duplicate key error index
                # E11001 - duplicate key on update
                message = "Tried to save duplicate unique keys (%s)"
                raise NotUniqueError(message % err)
            raise OperationError(message % err)

        return self

    async def _save_create_async(self, doc, force_insert, write_concern):
        """Save a new document.

        Helper method, should only be used inside save().
        """
        collection = self._get_collection()

        with set_write_concern(collection, write_concern) as wc_collection:
            if "_id" in doc:
                raw_object = await wc_collection.find_one_and_replace(
                    filter={"_id": doc["_id"]},
                    replacement=doc
                )
                if raw_object:
                    return doc["_id"]

            object_id = (await wc_collection.insert_one(doc)).inserted_id

        return object_id

    def to_mongo(self, *args, **kwargs):
        data = super().to_mongo(*args, **kwargs)

        # If '_id' is None, try and set it from self._data. If that
        # doesn't exist either, remove '_id' from the SON completely.
        if data["_id"] is None:
            if self._data.get("id") is None:
                del data["_id"]
            else:
                data["_id"] = self._data["id"]

        return data

    @classmethod
    def _get_db(cls):
        """Some Model using other db_alias"""
        return get_db(cls._meta.get("db_alias", DEFAULT_CONNECTION_NAME))

    @classmethod
    def _get_collection(cls):
        """Return the PyMongo collection corresponding to this document.

        Upon first call, this method:
        1. Initializes a :class:`~pymongo.collection.Collection` corresponding
           to this document.
        2. Creates indexes defined in this document's :attr:`meta` dictionary.
           This happens only if `auto_create_index` is True.
        """
        if not hasattr(cls, "_collection") or cls._collection is None:
            db = cls._get_db()
            collection_name = cls._get_collection_name()
            cls._collection = db[collection_name]

        return cls._collection


import os
from pdf2image import convert_from_path
from pathlib import Path

def convert_file(before_dir, after_dir, file_name):
    file_name = file_name.replace("/", "")
    input_pdf = Path(f"{before_dir}/{file_name}")
    images = convert_from_path(input_pdf)

    output_path = Path(f"{after_dir}/{file_name}")
    output_pdf = images[0].convert("RGB")
    output_pdf.save(output_path, save_all=True, append_images=images[1:])

def main():
    current_dir = os.getcwd()

    try:
        before_dir = os.mkdir(current_dir+"/PDF_ANTES")
    except:
        before_dir = current_dir+"/PDF_ANTES"

    try:
        after_dir = os.mkdir(current_dir+"/PDF_DEPOIS")
    except:
        after_dir = current_dir+"/PDF_DEPOIS"

    dir_list = os.listdir(current_dir+"/PDF_ANTES")

    for file_name in dir_list:
        print("Convertendo... "+file_name)
        convert_file(before_dir, after_dir, file_name)

main()


import math
from fractions import Fraction

def Pt2(a,b,c):
    while True:
        if a == 0 and b == 0:
            print("Mot trong so a, b phai khac khong.")
            a = float(input("Nhap so a: "))
            b = float(input("Nhap so b: "))
        else:
            delta = b**2 - 4 * a * c

            if delta < 0:
                print("Phuong trinh vo nghiem.")
                return ""
            elif delta == 0:
                print(f"x1 va x2 co nghiem kep la x1 = x2 = {-b / (2 * a)}.")
                return ""
            else:
                print("x1 va x2 co hai nghiem rieng biet lan luot la:")
                print(f"Nghiem cua a la {Fraction((-b + math.sqrt(delta))/(2*a))}")
                print(f"Nghiem cua b la {Fraction((-b - math.sqrt(delta))/(2*a))}")
                return ""


from lan.pinger import Pinger
from hue.client import Client
import time
import json

def main():
    client = Client('192.168.50.16')
    pinger = Pinger(
        '192.168.50.201',
        connected_interval=120,
        disconnected_interval=4,
        on_connect=client.resume,
        on_disconnect=client.stop,
        max_retries=3
    )
    pinger.listen()

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        with open('/usb/huemgr/crash.log','w') as f:
            f.write(f"{time.time()} {e.with_traceback()}\n")


from .core import TransformConverterBase, parameter_to_value

class Rotate3DConverter(TransformConverterBase):
    """
    ASDF support for serializing rotation models that
    use the rotate3d tag.
    """

    tags = ("tag:stsci.edu:asdf/transform/rotate3d-*",)
    types = (
        "astropy.modeling.rotations.RotateNative2Celestial",
        "astropy.modeling.rotations.RotateCelestial2Native",
        "astropy.modeling.rotations.EulerAngleRotation",
    )

    def to_yaml_tree_transform(self, model, tag, ctx):
        from astropy.modeling import rotations

        if isinstance(model, rotations.RotateNative2Celestial):
            node = {
                "phi": parameter_to_value(model.lon),
                "theta": parameter_to_value(model.lat),
                "psi": parameter_to_value(model.lon_pole),
                "direction": "native2celestial",
            }
        elif isinstance(model, rotations.RotateCelestial2Native):
            node = {
                "phi": parameter_to_value(model.lon),
                "theta": parameter_to_value(model.lat),
                "psi": parameter_to_value(model.lon_pole),
                "direction": "celestial2native",
            }
        else:
            node = {
                "phi": parameter_to_value(model.phi),
                "theta": parameter_to_value(model.theta),
                "psi": parameter_to_value(model.psi),
                "direction": model.axes_order,
            }

        return node

    def from_yaml_tree_transform(self, node, tag, ctx):
        from astropy.modeling import rotations

        if node["direction"] == "native2celestial":
            return rotations.RotateNative2Celestial(node["phi"], node["theta"], node["psi"])

        if node["direction"] == "celestial2native":
            return rotations.RotateCelestial2Native(node["phi"], node["theta"], node["psi"])

        return rotations.EulerAngleRotation(node["phi"], node["theta"], node["psi"], axes_order=node["direction"])

class RotationSequenceConverter(TransformConverterBase):
    """
    ASDF support for serializing rotation sequence models.
    """

    tags = ("tag:stsci.edu:asdf/transform/rotate_sequence_3d-*",)
    types = (
        "astropy.modeling.rotations.RotationSequence3D",
        "astropy.modeling.rotations.SphericalRotationSequence",
    )

    def to_yaml_tree_transform(self, model, tag, ctx):
        from astropy.modeling import rotations

        node = {"angles": list(model.angles.value)}
        node["axes_order"] = model.axes_order
        if isinstance(model, rotations.SphericalRotationSequence):
            node["rotation_type"] = "spherical"
        elif isinstance(model, rotations.RotationSequence3D):
            node["rotation_type"] = "cartesian"
        else:
            msg = f"Cannot serialize model of type {type(model)}"
            raise TypeError(msg)
        return node

    def from_yaml_tree_transform(self, node, tag, ctx):
        from astropy.modeling import rotations

        angles = node["angles"]
        axes_order = node["axes_order"]
        rotation_type = node["rotation_type"]
        if rotation_type == "cartesian":
            return rotations.RotationSequence3D(angles, axes_order=axes_order)

        if rotation_type == "spherical":
            return rotations.SphericalRotationSequence(angles, axes_order=axes_order)

        msg = f"Unrecognized rotation_type: {rotation_type}"
        raise ValueError(msg)


import os
import fitz  # PyMuPDF
import pandas as pd

# Import custom FinderAlgorithm from find module
from FindBezoldiging import FinderAlgorithm
from NameFinder import PDFTextExtractor, detect_high_probability_names, merge_keywords_in_range
from scannedConvert import SearchablePDFConverter

# Function to process all PDF files in a folder
def process_pdfs(df, pdf_folder, output_csv_path):
    results_data = []

    # Iterate through each entry in the DataFrame
    for index, row in df.iterrows():
        pdf_file_name = row['FileName']
        page_number = row['Pagenumber']

        # Construct the full path to the PDF file
        pdf_path = os.path.join(pdf_folder, f"{pdf_file_name}")

        # Check if the PDF file exists
        if not os.path.exists(pdf_path):
            print(f"Warning: PDF file not found: {pdf_path}")
            continue

        # Extract text from the specified page
        # pdf_extractor = PDFTextExtractor(pdf_path, page_number, 1)
        word_space_size = 500
        model_path = 'name_detection_model.h5'
        tokenizer_path = 'tokenizer.pkl'

        high_prob_df = detect_high_probability_names(pdf_path, word_space_size, page_number, model_path, tokenizer_path)

        # Scanned Paper
        if len(high_prob_df) == 0:
            # Replace 'path_to_tesseract_executable' with the actual path to your Tesseract OCR executable
            tesseract_cmd_path = r'D:/CODING/Tesseract-OCR/tesseract.exe'

            # Create an instance of SearchablePDFConverter
            pdf_converter = SearchablePDFConverter(pdf_path, tesseract_cmd_path)

            # Example 2: Convert a specific page of the PDF to a searchable PDF with post-processing
            searchable_pdf_page = pdf_converter.convert_to_searchable_pdf_page(page_number)

            # Save the output searchable PDF for the specific page to a file
            pdf_path = f'tempSearchable.pdf'
            searchable_pdf_page.save(pdf_path)
            page_number = 1
            high_prob_df = detect_high_probability_names(pdf_path, word_space_size, page_number, model_path,
                                                         tokenizer_path)

        # Call FinderAlgorithm with the extracted sentences
        box_coordinates, salary_coordinates_list, text_region_coordinates_list, result_list = FinderAlgorithm(pdf_path,
                                                                                high_prob_df[
                                                                                'Name'],
                                                                                page_number - 1)

        # Create a DataFrame from the result_list
        result_df = pd.DataFrame(result_list, columns=['Name', 'Bezoldiging'])

        # Add additional columns for FileName and PageNumber
        result_df['filename'] = pdf_file_name
        result_df['PageNumber'] = page_number

        # Append the DataFrame to the results_data list
        results_data.append(result_df)

    # Concatenate all DataFrames in the results_data list
    final_result_df = pd.concat(results_data, ignore_index=True)

    # Save the results to a CSV file
    final_result_df.to_csv(output_csv_path, index=False)
    print(f"Results saved to: {output_csv_path}")

print("FINDING NAME AND KEYWORD ..........")

# # Example usage
# csv_path = "CSV/AllDatainput.csv"
# pdf_folder = "pdfs"
# output_csv_path = "CSV/AllDataResults.csv"

# # Read the CSV file into a DataFrame
# csv_data = pd.read_csv(csv_path)

# # Process all PDFs in the folder based on the DataFrame
# process_pdfs(csv_data, pdf_folder, output_csv_path)


import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep

def get_source(driver_path, website):
    driver = webdriver.Chrome(driver_path)
    driver.get(website)
    sleep(3)
    source = driver.page_source
    return source

def get_table_bs(driver_path, urls):
    source_best = get_source(driver_path, urls['Best'])
    source_good = get_source(driver_path, urls['Good'])
    soup_best = BeautifulSoup(source_best, 'html.parser')
    soup_good = BeautifulSoup(source_good, 'html.parser')
    table_best = soup_best.find('table', class_="CryptoTable_table__P2T2N")
    table_good = soup_good.find('table', class_="CryptoTable_table__P2T2N")
    return table_best, table_good

def selected_data_dic(table):
    retrieved_data = {}
    for c in table.find_all('tbody'):
        rows = c.find_all('tr', {'class': lambda value: value and value.startswith('CryptoTable')})
        for row in rows:
            symbol = row.find('a').text
            # print(symbol)
            rank = row.find('strong').text
            price = row.find_all('div', {'class': 'right'})[0].text
            price_ath = row.find_all('div', {'class': 'right'})[1].text
            change_24 = row.find_all('div', {'class': lambda value: value and value.startswith('center Percent')})[
                0].text
            change_30 = row.find_all('div', {'class': lambda value: value and value.startswith('center Percent')})[
                1].text
            change_ath = row.find_all('div', {'class': lambda value: value and value.startswith('center Percent')})[
                2].text
            retrieved_data[symbol] = {'Price': price,
                                      'Price ATH': price_ath,
                                      'Rank': rank,
                                      'Change(24H)': change_24,
                                      'Change(30D)': change_30,
                                      'Change(ATH)': change_ath
                                       }
    return retrieved_data

def main():
    base_url = 'https://4kings.xyz/crypto'
    urls = {'Best': base_url + '/best',
            'Good': base_url + '/good'}
    driver_path = '/Users/thomasgrandguillot/Desktop/chromedriver'
    table_best, table_good = get_table_bs(driver_path, urls)
    data_best_dic = selected_data_dic(table_best)
    data_good_dic = selected_data_dic(table_good)
    df_best = pd.DataFrame.from_dict(data_best_dic, orient='index')
    df_good = pd.DataFrame.from_dict(data_good_dic, orient='index')
    return df_best, df_good

if __name__ == "__main__":
    df_best, df_good = main()


import argparse
import csv
import datetime
import pathlib
import typing

def get_arguments():
	parser = argparse.ArgumentParser()
	parser.add_argument("--folder", default=None, help="Folder that contains log files you want to update")
	parser.add_argument("--file", default=None, help="Log file you want to update")

	return parser.parse_args()

class GpsRollover:
	FILE_FORMAT = "WG%Y%m%d%H%M%S.log"
	GPS_SENTENCE_CODE = "$GPRMC" # the code we need to look for to modify the date
	OFFSET_DAYS = 7168

	@classmethod
	def date_to_str(cls, date: datetime.date) -> str:
		return date.strftime("%0d%0m") + date.strftime("%Y")[2:]

	@classmethod
	def str_to_date(cls, date_string: str) -> datetime.date:
		assert len(date_string) == 6, "Incorrect length for date_string"
		day, month, year = date_string[0:2], date_string[2:4], date_string[4:6]
		return datetime.date(int("20"+year), int(month), int(day))

	@classmethod
	def add_offset(cls, date_obj: datetime.date) -> datetime.date:
		return date_obj + datetime.timedelta(days=cls.OFFSET_DAYS)

	@classmethod
	def fix_files_in_folder(cls, folder: str) -> None:
		path = pathlib.Path(folder).expanduser()
		for entry in path.iterdir():
			if not entry.is_file() or entry.suffix != ".log":
				continue
			if entry.name[:6] > "WG2021":  # We shouldn't have to touch these files
				continue

			cls.fix_file(entry)

	@classmethod
	def fix_file(cls, file: typing.Union[pathlib.Path, str]) -> None:
		if isinstance(file, str):
			filepath = pathlib.Path(file).expanduser()
		elif isinstance(file, pathlib.Path):
			filepath = file
		else:
			raise ValueError("File must be a Path or str")

		file_datetime = datetime.datetime.strptime(filepath.name, cls.FILE_FORMAT)

		target_path = pathlib.Path(filepath.with_name((cls.add_offset(file_datetime).strftime(cls.FILE_FORMAT))))

		writer = csv.writer(target_path.open("w"), delimiter=",", quoting=csv.QUOTE_NONE)
		reader = csv.reader(filepath.open("r"), delimiter=",")

		for row in reader:
			if row[0] == cls.GPS_SENTENCE_CODE:
				row[-4] = cls.date_to_str(cls.add_offset(cls.str_to_date(row[-4])))
			writer.writerow(row)

if __name__ == "__main__":
	args = get_arguments()
	if args.file:
		GpsRollover.fix_file(args.file)
	elif args.folder:
		GpsRollover.fix_files_in_folder(args.folder)


#from math import *
import sys, os, argparse, time
from re import split as regexp_split
import numpy as np
#import numpy.ma as ma
import general_scripts as gs
#import general_maths as gm
#import transforms3d.quaternions as qops
#import transforms3d_supplement as qs
import fitting_Ct_functions as fitCt
import spectral_densities as sd

def parse_rotdif_params(D=None, tau=None, aniso=None):
    #Determine diffusion model.
    if args.D is None:
        if args.tau is None:
            print("= = ERROR: No global tumbling parameters given!", file=sys.stderr)
            sys.exit(1)
        else:
            Diso  = 1.0/(6*args.tau)
            if aniso is None or aniso == 1.0:
                return sd.globalRotationalDiffusion_Isotropic(D=Diso)
            else:
                return sd.globalRotationalDiffusion_Axisymmetric(D=[Diso, aniso])
    else:
        tmp   = [float(x) for x in regexp_split('[, ]', D) if len(x)>0]
        Diso  = tmp[0]
        if len(tmp)==1:
            if aniso is None:
                return sd.globalRotationalDiffusion_Isotropic(D=Diso)
            else:
                return sd.globalRotationalDiffusion_Axisymmetric(D=[Diso, aniso])
        elif len(tmp)==2:
            return sd.globalRotationalDiffusion_Axisymmetric(D=tmp, bConvert=True)
        else:
            print("WARNING: fully anisotropic global rotdif not implemented.", file=sys.stderr)
            return None

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Optimisation of spin relaxation parameters based on multiple experiments, '
                                     'based on the simulated and given local and global tumlbing chaaracteristics.\n'
                                     'operates similarly to calcualte_relaxations_from_Ct. All internal units are in picoseconds',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('expFiles', type=str, nargs='+',
                        help='One or more formatted spin relaxation experimental data, resembling this:\n'
                             '# Type NOE\n'
                             '# NucleiA   15N\n'
                             '# NucleiB    1H\n'
                             '# Frequency 600.133\n'
                             '1 0.9 0.05\n'
                             '...'
                       )
#    parser.add_argument('--ROTDIF', dest='inputFileROTDIF', type=str, default=None,
#                        help='As an alternative to the above, a ROTDIF-format input file can be given.')
    parser.add_argument('-o', '--outpref', type=str, dest='out_pref', default='out',
                        help='Output file prefix.')
    parser.add_argument('-f', '--infn', type=str, dest='in_Ct_fn', required=True,
                        help='Read a formatted file with fitted C_internal(t), taking from it the parameters.')
    parser.add_argument('--refpdb', type=str, dest='refPDBFile', default=None,
                        help='Reference PDB file to compute axisymmetric rotational diffusion using X-H bond vectors wound within. '
                             'This assumes that the PDB file is already in the principal-axis frame aligned with the diffusion tensor.')
    parser.add_argument('--distfn', type=str, dest='distfn', default=None,
                        help='Vector orientation distribution of the X-H dipole in spherical-polar coordinates. '
                             'This is used to compute axisymmetric rotational diffusion and overrules '
                             'the reference PDB argument. Vectors assumed to be in the principal axes frame.')
#    parser.add_argument('--shiftres', type=int, default=0,
#                        help='Shift the MD residue indices, e.g., to match the experiment.')
#    parser.add_argument('--tu', '--time_units', type=str, dest='time_unit', default='ps',
#                        help='Time units of the autocorrelation file.')
    parser.add_argument('--tau', type=float, dest='tau', default=None,
                        help='Isotropic relaxation time constant. Overwritten by Diffusion tensor values when given.')
    parser.add_argument('--aniso', type=float, dest='aniso', default=None,
                        help='Diffusion anisotropy (prolate/oblate). Overwritten by Diffusion tensor values when Dpar and Dperp values are given.')
    parser.add_argument('-D', '--DTensor', type=str, dest='D', default=None,
                        help='The Diffusion tensor. Single values are parsed as isotropic component. '
                             'Two values are parsed as  Dpar,Dperp. '
                             'Three values are directly parsed as the diffusion tensor, although this is not currently accepted. '
                             'Entries are either comma-separated or space separated in a quote.\n'
                             'Note: In axisymmetric forms, when Daniso < 1 the unique axis is considered to point along x, and '
                             'when Daniso > 1 the unique axis is considered to point along z.')
    parser.add_argument('--zeta', type=float, default=0.890023,
                        help='Input optional manual zeta factor to scale-down '
                             'the S^2 of MD-based derivations by zero-point vibrations known in QM. '
                             'This is by convention 0.890023 (1.02/1.04)^6 - see Trbovic et al., Proteins, 2008 and Case, J Biomol NMR, 1999.')
    parser.add_argument('--csa', type=str, default=None,
                        help='Input manual average CSA value, if different from the assumed value, e.g. -170 ppm for 15N. '
                             'Residue-specific variations is set if a file name is given. This file should '
                             'specify on each line the residue index and then its respective CSA value.')
    parser.add_argument('--opt', '--fit', type=str, dest='listOptParams', default=None,
                        help='Perform optimisation against all given experimental data, over the following possible parameters %s:\n'
                             '  - Diso, isotropic tumbling rate of the domain,\n'
                             '  - Daniso, axisymmetric tumbling anisotropy,\n'
                             '  - zeta, QM zero-point vibration contribution to local autocorrelation,\n'
                             '  - CSA, mean chemical shift anisotropy of the heavy nuclei (nucleiA),\n'
                             '  - rsCSA, residue-specific chemical-shift anisotropies.\n'
                             'Set these using a single comma-separated string as the argument. '
                             'The order given will be the order used inthe optimisation loop\n' \
                             % (sd.spinRelaxationExperiments.listAllowedOptimisationVariables) )
    parser.add_argument('--cycles', type=int, default=10,
                        help='For the residue-specific CSA fitting algorithm, do a maximum of N cycles between the global Diso versus the local CSA fits. '
                             'Each cycle begins by optimising Diso over all residues, then optimising each CSA separately against the new global value.\n'
                             'Adjust in case algorithm fails to find convergence within 2 cycles.')
    parser.add_argument('--tol', type=float, default=1e-6,
                        help='The tolerance criteria for terminating the global/local optimisation cycles early, as a fractional change.')

    # = = = Initial parameter setting.
    time_start=time.time()
    args = parser.parse_args()

    # = = = Declare internal time units.
    standardTimeUnit = 'ps'

    # = = = Parse and set up local tumbling model
    localCtModel = fitCt.read_fittedCt_parameters( args.in_Ct_fn )
    if localCtModel.nModels == 0:
        print( "= = = ERROR: The fitted-Ct file %s was read, but did not yield any usable parameters!" % fittedCt_file )
        sys.exit(1)

    nResidSim = localCtModel.nModels
    residSim  = [ int(k) for k in localCtModel.model.keys() ]

    # = = = Parse and set up global tumbling model
    globalRotDif = parse_rotdif_params(args.D, args.tau, args.aniso)

    if not args.distfn is None:
        globalRotDif.import_frame_vectors(args.distfn)
    elif not args.refPDBFile is None:
        globalRotDif.import_frame_vectors_pdb(args.refPDBFile)
    #globalRotDif.report()

    #if bQuatRot:
    #    print( "    ....rotating input vectors into PAF frame using q_rot." )
    #    vecXH = qs.rotate_vector_simd(vecXH, q_rot)
    #    print( "    ....X-H vector input processing completed." )

    # = = = Parse and set up individual experiments.
    objExpts = sd.spinRelaxationExperiments(globalRotDif, localCtModel)

    for f in args.expFiles:
        objExpts.add_experiment( f )
    if args.zeta != 1.0:
        print( " = = Applying scaling of all C(t) magnitudes to account for zero-point QM vibrations (zeta) of %g" % args.zeta )
        objExpts.set_global_zeta( args.zeta )
    #objExpts.report()

    # = = = This mapping is necessary to match simulation names to experimental names,
    #       as not all peaks are resolved or assigned in a given experimental conditiosn.
    objExpts.map_experiment_peaknames_to_models()
    objExpts.report_maps()

    # = = = Parameters for global/local meta-optimisation.
    nRefinementCycles   = args.cycles
    refinementTolerance = args.tol

# = = = Section interpreting CSA input, check if it a custom single value for backwards compatibility, or an actual file.
    CSAvaluesArray = None
    if args.csa is None:
        print( "= = = Using default CSA value respective to each experiment.")
        #print( objExpts.spinrelax[0].angFreq.gA.csa )
        #CSAvaluesArray = np.repeat( relax_obj.gX.csa, num_vecs )
    else:
        # = = = Check is it is a numeric input and/or a file. File takes precedence.
        try:
            fp = open(args.csa, 'r')
            fp.close()
            bFileFound=True
        except:
            bFileFound=False

        try:
            tmp=float(args.csa)
            bIsNumeric=True
        except ValueError:
            bIsNumeric=False

        if bFileFound:
            # = = = Temporary work-around
            residCSA, CSAvaluesArray = gs.load_xy( args.csa )
            residCSA=[ str(int(x)) for x in residCSA ]
            print( "= = = Using input CSA values from file %s - please ensure that the names match those found in C(t) models." % args.csa )
            if np.fabs(CSAvaluesArray[0]) > 1.0:
                print( "= = = NOTE: the first value is > 1.0, so assume a necessary conversion to ppm." )
                CSAvaluesArray *= 1e-6
            objExpts.initialise_CSA_array(residCSA, CSAvaluesArray)
        elif bIsNumeric:
            print( "= = = Using user-input CSA value: %g" % tmp )
            if np.fabs(tmp)>1.0:
                print( "= = = NOTE: this value is > 1.0, so assume a necessary conversion to ppm." )
                tmp*=1e-6
            CSAvaluesArray = np.repeat( tmp, objExpts.localCtModels.nModels )
            objExpts.initialise_CSA_array(objExpts.localCtModels.get_names(), CSAvaluesArray)
        else:
            print( "= = = ERROR at parsing the --csa argument!", file=sys.stderr )
            sys.exit(1)

    # = = = No optimisation. Simply print the direct prediction for each experiment. = = =
    if args.listOptParams is None:
        objExpts.eval_all(bVerbose=True)
        objExpts.export_xvg(args.out_pref, bIncludeExpt=False)

        time_stop=time.time()
        print( "= = Finished. Total seconds elapsed: %g" % (time_stop - time_start) )
        sys.exit()
    # = = = End No optimisation. = = =

    # = = = evaluation section. Determine what is to be done wioth the data.
    listOpt = args.listOptParams.split(',')
    objExpts.parse_optimisation_params(listOpt)
    print("= = = Parsed optimiser input %s." % args.listOptParams )
    print("    ... conducting global optimisations over parameters %s ..." % (objExpts.listUpdateVariables))
    if objExpts.bDoLocalOpt:
        print("    ... conducting local optimisations over residue-specific CSA...")
    chisq = objExpts.perform_optimisation(maxCycles=nRefinementCycles,tol=refinementTolerance)
    print("= = = Optimisation complete. Final chi-value: %g" % np.sqrt(chisq) )

    objExpts.export_xvg(args.out_pref, bIncludeExpt=True)
    if objExpts.bDoLocalOpt and objExpts.bOptCompleted:
        names       = objExpts.localCtModels.get_names()
        optCSAArray = objExpts.get_first_csa()
        fp = open( args.out_pref+'_CSA_opt.dat', 'w')
        for x, y in zip(names, optCSAArray):
            print("%s %g" % (x,y), file=fp)
        fp.close()

    time_stop=time.time()
    print( "= = Finished. Total seconds elapsed: %g" % (time_stop - time_start) )
    sys.exit()


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

Apparatus_volume = float(input("Введите объем аппарата м.куб: "))
Fluid_speed = float(input("Введите скорость потока м.куб/cек: "))
Start_concentration = float(input("Введите начальную концентрацию на входе в реактор кмоль/м.куб:  "))
Reactor_concentration = float(input("Введите начальную концентрацию внутри реактора кмоль/м.куб:  "))

dt = 0.1
E = 0.0001
Reactor_time = Apparatus_volume/Fluid_speed
c = [0]
t = [0]

c[0] = Reactor_concentration
t[0] = 0
i = 0

while True:
    i = i+1
    t.append(t[i-1] + dt)
    print(t[i])
    c.append(c[i-1] + dt * (Start_concentration - c[i - 1]) / Reactor_time)
    print(c[i])

    if abs(c[i]-c[i-1]) < E:
        break
plt.title("Зависимость концентрации от времени в реакторе")
plt.xlabel("Время, секунды")
plt.ylabel("концентрацияб кмоль/м.куб")
plt.plot(t,c)
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Mon Feb  1 17:38:38 2021

@author: KeliDu
"""
# =================================
# Import statements
# =================================

import os
import pandas as pd
import numpy as np
from sklearn import preprocessing as prp
import random
from measures import welsh_t
from measures import wilcoxon_ranksum
#from measures import KL_Divergence
from measures import chi_square
from measures import LLR
from measures import DP_Gries
# =================================
# Functions: calculate
# =================================

def make_idlists(metadatafile, separator, contrast):
    """
    This function creates lists of document identifiers based on the metadata.
    Depending on the contrast defined, the two lists contain various identifiers.
    """
    with open(metadatafile, "r", encoding='utf-8') as infile:
        metadata = pd.read_csv(infile, sep=separator)
        print("\nmetadata\n", metadata.head())
        #metadata = metadata.drop("Unnamed: 0", axis=1)
        metadata.set_index("idno", inplace=True)
        print("\nmetadata\n", metadata.head())
        if contrast[0] != "random":
            list1 = list(metadata[metadata[contrast[0]].isin([contrast[1]])].index)
            list2 = list(metadata[metadata[contrast[0]].isin([contrast[2]])].index)
            #print("list1", list1)
            #print("list2", list2)
        elif contrast[0] == "random":
            allidnos = list(metadata.loc[:, "idno"])
            allidnos = random.sample(allidnos, len(allidnos))
            list1 = allidnos[:int(len(allidnos)/2)]
            list2 = allidnos[int(len(allidnos)/2):]
            #print(list1[0:5])
            #print(list2[0:5])
        idlists = [list1, list2]
        #print(idlists)
        return idlists

def filter_dtm(dtmfolder, parameterstring, idlists, absolutefreqs, relativefreqs, binaryfreqs, tf_frame):
    """
    This function splits the DTM in two parts.
    Each part consists of the segments corresponding to one partition.
    Each segment is chosen based on the file id it corresponds to.
    """
    ids1 = "|".join([str(idno)+".*" for idno in idlists[0]])
    print(ids1)
    ids2 = "|".join([str(id)+".*" for id in idlists[1]])
    binary = binaryfreqs
    relative = relativefreqs
    absolute = absolutefreqs
    binary1 = binary.T.filter(regex=ids1, axis=1)
    binary2 = binary.T.filter(regex=ids2, axis=1)
    relative1 = relative.T.filter(regex=ids1, axis=1)
    relative2 = relative.T.filter(regex=ids2, axis=1)
    absolute1 = absolute.T.filter(regex=ids1, axis=1)
    absolute2 = absolute.T.filter(regex=ids2, axis=1)
    tf_frame1 = tf_frame.T.filter(regex=ids1, axis=1)
    tf_frame2 = tf_frame.T.filter(regex=ids2, axis=1)
    print("\nbinary1\n", binary1.head())
    return binary1, binary2, relative1, relative2, absolute1, absolute2, tf_frame1, tf_frame2

def get_indicators(binary1, binary2, relative1, relative2, tf_frame1, tf_frame2):
    """
    Indicators are the mean relative frequency or the document proportions,
    depending on the method chosen.
    """
    docprops1 = np.mean(binary1, axis=1)
    docprops1 = pd.Series(docprops1, name="docprops1")
    docprops2 = np.mean(binary2, axis=1)
    docprops2 = pd.Series(docprops2, name="docprops2")
    relfreqs1 = np.mean(relative1, axis=1)*1000
    relfreqs1 = pd.Series(relfreqs1, name="relfreqs1")
    relfreqs2 = np.mean(relative2, axis=1)*1000
    relfreqs2 = pd.Series(relfreqs2, name="relfreqs2")
    tf_framefreqs1 = np.mean(tf_frame1, axis=1)
    tf_framefreqs2 = np.mean(tf_frame2, axis=1)
    print("\ndocprops1\n", docprops1.head(20))
    print("\ndocprops2\n", docprops2.head(20))
    print("\nrelfreqs1\n", relfreqs1.head())
    print("\nrelfreqs2\n", relfreqs2.head())
    print("\tf_framefreqs2\n", tf_framefreqs2.head())
    print("\tf_framefreqs1\n", tf_framefreqs1.head())
    return docprops1, docprops2, relfreqs1, relfreqs2, tf_framefreqs1, tf_framefreqs2

def scaling_results (scaler, Series):
    Series_index = Series.index
    Series = scaler.fit_transform(Series.values.reshape(-1, 1))
    Series = [value[0] for value in Series]
    scaled_Series = pd.Series(data=Series, index=Series_index)
    return scaled_Series

def calculate_scores(docprops1, docprops2, absolute1, absolute2, relfreqs1, relfreqs2, logaddition, segmentlength, idlists, tf_framefreqs1, tf_framefreqs2, scaling):
    """
    This function implements several distinctive measures.
    """
    # Define logaddition and division-by-zero avoidance addition
    logaddition = logaddition+1
    divaddition = 0.00000000001
    print("---calculating scores: 1/9, 'original Zeta'---")
    try:
        # sd0 - Subtraction, docprops, untransformed a.k.a. "original Zeta"
        zeta_sd0 = docprops1 - docprops2
        zeta_sd0 = pd.Series(zeta_sd0, name="zeta_sd0")
        # Prepare scaler to rescale variants to range of sd0 (original Zeta)
        scaler = prp.MinMaxScaler(feature_range=(min(zeta_sd0),max(zeta_sd0)))
    except:
        print("Something went wrong while calculating 'original Zeta'")
        zeta_sd0 = pd.Series()
        scaler = prp.MinMaxScaler(feature_range=(-1,1))

    print("---calculating scores: 2/9, 'Zeta_log2-transformed'---")
    try:
        # sd2 - Subtraction, docprops, log2-transformed
        zeta_sd2 = np.log2(docprops1 + logaddition) - np.log2(docprops2 + logaddition)
        zeta_sd2 = pd.Series(zeta_sd2, name="zeta_sd2")
    except:
        print("Something went wrong while calculating 'Zeta_log2-transformed'")
        zeta_sd2 = pd.Series()

    print("---calculating scores: 3/9, 'ratio of relative frequencies'---")
    try:
        rrf_dr0 = (relfreqs1 + divaddition) / (relfreqs2 + divaddition)
        rrf_dr0 = pd.Series(rrf_dr0, name="rrf_dr0")
    except:
        print("Something went wrong while calculating 'ratio of relative frequencies'")
        rrf_dr0 = pd.Series()

    print("---calculating scores: 4/9, 'Eta, deviation of proportions based distinctiveness'---")
    # == Calculate subtraction variants ==
    # sg0 - Subtraction, devprops, untransformed a.k.a. "dpd", ("g" for Gries)
    try:
        devprops1 = DP_Gries.main(absolute1, segmentlength)
        devprops2 = DP_Gries.main(absolute2, segmentlength)
        eta_sg0 = (devprops1 - devprops2) * -1
        eta_sg0 = pd.Series(eta_sg0, name="eta_sg0")
    except:
        print("Something went wrong while calculating 'Eta, deviation of proportions based distinctiveness'")
        eta_sg0 = pd.Series()

    #Calculate Welshs-t-test
    print("---calculating scores: 5/9, 'Welshs-t-test'---")
    try:
        welsh_t_value = welsh_t.main(absolute1, absolute2)
    except:
        print("Something went wrong while calculating 'Welshs-t-test'")
        welsh_t_value = pd.Series()

    #Calculate Wilcoxon rank-sum test
    print("---calculating scores: 6/9, 'Wilcoxon rank-sum test'---")
    try:
        ranksumtest_value = wilcoxon_ranksum.main(absolute1, absolute2)
    except:
        print("Something went wrong while calculating 'Wilcoxon rank-sum test'")
        ranksumtest_value = pd.Series()
    '''
    #Calculate Kullback-Leibler Divergence
    print("---calculating scores: 7/10, 'Kullback-Leibler Divergence'---")
    try:
        KLD_value = KL_Divergence.main(absolute1, absolute2, 2, logaddition)
    except:
        print("Something went wrong while calculating 'Kullback-Leibler Divergence'")
        KLD_value = pd.Series()
    '''
    #Calculate Chi-squared test
    print("---calculating scores: 7/9, 'Chi-squared test'---")
    try:
        chi_square_value = chi_square.main(absolute1, absolute2)
    except:
        print("Something went wrong while calculating 'Chi-squared test'")
        chi_square_value = pd.Series()

    #Calculate Log-likelihood-Ratio test
    print("---calculating scores: 8/9, 'Log-likelihood-Ratio test'---")
    try:
        LLR_value = LLR.main(absolute1, absolute2)
    except:
        print("Something went wrong while calculating 'Log-likelihood-Ratio test'")
        LLR_value = pd.Series()

    print("---calculating scores; 9/9, 'Tf-idf weighted absolutefreqs absbased distinctiveness'---")
    try:
        tf_idf = tf_framefreqs1 - tf_framefreqs2
    except:
        print("Something went wrong while calculating 'Tf-idf weighted absolutefreqs absbased distinctiveness'")
        tf_idf = pd.Series()

    if scaling == True:
        zeta_sd2 = scaling_results (scaler, zeta_sd2)
        rrf_dr0 = scaling_results (scaler, rrf_dr0)
        eta_sg0 = scaling_results (scaler, eta_sg0)
        welsh_t_value = scaling_results (scaler, welsh_t_value)
        ranksumtest_value = scaling_results (scaler, ranksumtest_value)
        chi_square_value = scaling_results (scaler, chi_square_value)
        LLR_value = scaling_results (scaler, LLR_value)
        tf_idf = scaling_results (scaler, tf_idf)
        return zeta_sd0, zeta_sd2, rrf_dr0, eta_sg0, welsh_t_value, ranksumtest_value, chi_square_value, LLR_value, tf_idf #KLD_value

    if scaling == False:
        return zeta_sd0, zeta_sd2, rrf_dr0, eta_sg0, welsh_t_value, ranksumtest_value, chi_square_value, LLR_value, tf_idf #KLD_value

def get_meanrelfreqs(dtmfolder, parameterstring, relativefreqs):
    meanrelfreqs = relativefreqs.T
    print("\nrelfreqs_df\n", meanrelfreqs.head())
    meanrelfreqs_index = meanrelfreqs.index
    meanrelfreqs = np.mean(meanrelfreqs, axis=1)*1000
    meanrelfreqs = pd.Series(data=meanrelfreqs, index=meanrelfreqs_index)
    print("\nmeanrelfreqs_series\n", meanrelfreqs.head(10))
    return meanrelfreqs

def combine_results(docprops1, docprops2, relfreqs1, relfreqs2, meanrelfreqs, zeta_sd0, zeta_sd2, rrf_dr0, eta_sg0, welsh, ranksum, chi_square, LLR, tf_idf):
    results = pd.DataFrame({
    "docprops1" : docprops1,
    "docprops2" : docprops2,
    "relfreqs1" : relfreqs1,
    "relfreqs2" : relfreqs2,
    "meanrelfreqs" :meanrelfreqs,
    "zeta_sd0" : zeta_sd0,
    "zeta_sd2" : zeta_sd2,
    "rrf_dr0" : rrf_dr0,
    "eta_sg0" : eta_sg0,
    "welsh" : welsh,
    "ranksum" : ranksum,
    #"KL_Divergence": KL_Divergence,
    "chi_square": chi_square,
    "LLR": LLR,
    "tf_idf": tf_idf})
    results = results[[
    "docprops1",
    "docprops2",
    "relfreqs1",
    "relfreqs2",
    "meanrelfreqs",
    "zeta_sd0",
    "zeta_sd2",
    "rrf_dr0",
    "eta_sg0",
    "welsh",
    "ranksum",
    #"KL_Divergence",
    "chi_square",
    "LLR",
    "tf_idf"]]
    results.sort_values(by="zeta_sd0", ascending=False, inplace=True)
    print("\nresults-head\n", results.head(10), "\nresults-tail\n", results.tail(10))
    return results

def save_results(results, resultsfile):
    with open(resultsfile, "w", encoding='utf-8') as outfile:
        results.to_csv(outfile, sep="\t")

# =================================
# Function: main
# =================================

def main(datafolder, dtmfolder, metadatafile, separator, contrast, logaddition, resultsfolder, segmentlength, featuretype, absolutefreqs, relativefreqs, binaryfreqs, tf_frame, scaling=True):
    print("--calculate")
    if not os.path.exists(resultsfolder):
        os.makedirs(resultsfolder)
    parameterstring = str(segmentlength) +"-"+ str(featuretype[0]) +"-"+ str(featuretype[1])
    #print(parameterstring)
    contraststring = str(contrast[0]) +"_"+ str(contrast[2]) +"-"+ str(contrast[1])
    #print(contraststring)
    resultsfile = resultsfolder + "results_" + parameterstring +"_"+ contraststring +".csv"
    idlists = make_idlists(metadatafile, separator, contrast)
    #print(idlists)
    binary1, binary2, relative1, relative2, absolute1, absolute2, tf_frame1, tf_frame2 = filter_dtm(dtmfolder, parameterstring, idlists, absolutefreqs, relativefreqs, binaryfreqs, tf_frame)
    #print(binary1)
    docprops1, docprops2, relfreqs1, relfreqs2, tf_framefreqs1, tf_framefreqs2 = get_indicators(binary1, binary2, relative1, relative2, tf_frame1, tf_frame2)
    zeta_sd0, zeta_sd2, rrf_dr0, eta_sg0, welsh, ranksum, chi_square, LLR, tf_idf = calculate_scores(docprops1, docprops2, absolute1, absolute2, relfreqs1, relfreqs2, logaddition, segmentlength, idlists, tf_framefreqs1, tf_framefreqs2, scaling)
    meanrelfreqs = get_meanrelfreqs(dtmfolder, parameterstring, relativefreqs)
    results = combine_results(docprops1, docprops2, relfreqs1, relfreqs2, meanrelfreqs, zeta_sd0, zeta_sd2, rrf_dr0, eta_sg0, welsh, ranksum, chi_square, LLR, tf_idf)
    save_results(results, resultsfile)


__all__ = [
    'DEFAULT_WINDOW_SIZE',
    'BACKGROUND_COLOR', 'MENU_COLOR',
    'LIGHT_SQUARE_COLOR', 'DARK_SQUARE_COLOR',
    'LIGHT_MOVE_COLOR', 'DARK_MOVE_COLOR',

    'PIECE_TYPES', 'PROFILE_NAMES',
]

DEFAULT_WINDOW_SIZE = (1280, 720)

BACKGROUND_COLOR = (49, 46, 43)
MENU_COLOR = (39, 37, 34)
LIGHT_SQUARE_COLOR = (238, 238, 210)
DARK_SQUARE_COLOR = (118, 150, 86)
LIGHT_MOVE_COLOR = (246, 246, 105)
DARK_MOVE_COLOR = (186, 202, 43)

PIECE_TYPES = ('pawn', 'knight', 'bishop', 'rook', 'queen', 'king')
PROFILE_NAMES = (
    'chess', 'board', 'light_square', 'dark_square',
    'pawn', 'knight', 'bishop', 'rook', 'queen', 'king',

    'enpassant', 'castle', 'check', 'checkmate', 'stalemate',
    'material', 'fork', 'pin',

    'sicilian_defense', 'kings_gambit', 'queens_gambit', 'bongcloud',

    'opening', 'midgame', 'endgame', 'zugzwang', 'puzzle', 'elo',
)