import requests
import sys
import os

url = 'http://localhost:8181'
cnt = 0
err_cnt = 0
err = False

def assertEqual(actual, expected, itemName=None):
    global err
    if actual != expected:
        if not err:
            sys.stdout.write('\033[31mNG\033[0m\n')
        if itemName is not None:
            sys.stderr.write(itemName + ': ')
        sys.stderr.write('Expected {}, but got {}.\n'.format(expected, actual))
        err = True

def assertHeader(response, headerName, expected):
    actual = response.headers.get(headerName)
    assertEqual(actual, expected, headerName)

def assertContent(response, expected):
    actual = response.content
    if expected is None:
        assertEqual(actual, b'', 'content')
    else:
        assertEqual(actual, expected, 'content')

def test_get_request(path, status_code, content_type=None, content_length=None, file_path=None, content=None, location=None, host=None, headers=None):
    test_request(
            method='GET',
            path=path,
            status_code=status_code,
            content_type=content_type,
            content_length=content_length,
            file_path=file_path,
            content=content,
            location=location,
            host=host,
            data=None,
            headers=headers
            )

def test_post_request(path, status_code, content_type=None, content_length=None, file_path=None, content=None, location=None, host=None, data=None, headers=None):
    test_request(
            method='POST',
            path=path,
            status_code=status_code,
            content_type=content_type,
            content_length=content_length,
            file_path=file_path,
            content=content,
            location=location,
            host=host,
            data=data,
            headers=headers
            )

def test_request(method, path, status_code, content_type=None, content_length=None, file_path=None, content=None, location=None, host=None, data=None, headers=None):
    global err
    err = False
    assert(file_path is None or content is None) # Specifying both is not allowed
    global cnt
    cnt += 1
    # Print test name, cnt, path and host
    sys.stdout.write('Test {:02d}: {} {} '.format(cnt, method, path))
    if host is not None:
        sys.stdout.write('(Host: {}) '.format(host))
    # Print data (Trim if too long)
    if data is not None:
        if isinstance(data, bytes) and len(data) > 15:
            sys.stdout.write('(Body: {}...({} bytes)) '.format(data[:15], len(data)))
        else:
            sys.stdout.write('(Body: {}) '.format(data))
    sys.stdout.write(': ')
    sys.stdout.flush()
    if headers is None:
        headers = {}
    if host is not None:
        headers['Host'] = host
    if data is not None and content_length is None:
        if isinstance(data, str):
            headers['Content-Length'] = str(len(data))
    response = requests.request(method, url + path, headers=headers, data=data, allow_redirects=False)

    # Status Code
    assertEqual(response.status_code, status_code, 'status_code')
    # Content-Type
    if content_type is not None:
        assertHeader(response, 'Content-Type', content_type)
    # Content-Length
    if content_length is not None:
        assertHeader(response, 'Content-Length', str(content_length))
    # Location
    if location is not None:
        assertHeader(response, 'Location', location)

    # Content
    if file_path is not None:
        content = open(file_path, 'rb').read()

    if content is not None:
        # Content-Length again
        assertHeader(response, 'Content-Length', str(len(content)))
        # Content
        assertEqual(response.content, content, 'content')

    # OK with green color
    if err:
        global err_cnt
        err_cnt += 1
    else:
        print('\033[32mOK\033[0m')

# This is for chunked encoding test
def gen(message=None):
    if message is None:
        yield b'Hi, '
        yield b'there '
        yield b'bob'
        yield b'!'
    else:
        # yield message by chunk of 1000 bytes
        for i in range(0, len(message), 1000):
            yield message[i:i+1000]

if __name__ == '__main__':
    test_get_request(path='/tests/static/a.text', status_code=200, file_path='./tests/static/a.text', content_type='text/plain')
    test_get_request(path='/tests/static/b.text', status_code=200, file_path='./tests/static/b.text', content_type='text/plain')
    test_get_request(path='/tests/static/abe.html', status_code=200, file_path='./tests/static/abe.html', content_type='text/html')
    test_get_request(path='/tests/static/abe_files/abehiroshi.jpg', status_code=200, file_path='./tests/static/abe_files/abehiroshi.jpg', content_type='image/jpeg')
    test_get_request(path='/tests/static/abe_files/menu.html', status_code=200, file_path='./tests/static/abe_files/menu.html', content_type='text/html')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index.html', content_type='text/html')
    test_get_request(path='/cgi/echo.py', status_code=200, content_type=None, content_length=0)
    test_get_request(path='/cgi/hello.py', status_code=200, content_type='text/plain', content=b'Hello, world!\n')
    test_get_request(path='/root/', status_code=200, file_path='./tests/html/root/root.html', content_type='text/html')
    test_get_request(path='/root/root.html', status_code=200, file_path='./tests/html/root/root.html', content_type='text/html')
    test_get_request(path='/alias/', status_code=200, file_path='./tests/html/foo/index.html', content_type='text/html')
    test_get_request(path='/alias/foo.html', status_code=200, file_path='./tests/html/foo/foo.html', content_type='text/html')
    test_get_request(path='/index/', status_code=200, file_path='./tests/html/foo/foo.html', content_type='text/html')
    test_get_request(path='/kapouet/pouic/toto/pouet', status_code=200, file_path='/tmp/www/pouic/toto/pouet', content_type='text/plain')
    test_get_request(path='/multiple-index/', status_code=200, file_path='./tests/html/foo/foo.html', content_type='text/html')
    test_get_request(path='/error_page/nosuchfile', status_code=404, file_path='/tmp/www/404.html', content_type='text/html')
    test_get_request(path='/limit_except/post/', status_code=405, content_type='text/html')
    test_get_request(path='/limit_except/get/', status_code=200, file_path='./tests/html/foo/index.html', content_type='text/html')
    test_get_request(path='/redirect/url/', status_code=301, location='https://www.google.com')
    test_get_request(path='/redirect/path/', status_code=301, location='/kapouet/pouic/toto/pouet')
    test_get_request(path='/cgi-bin/a.out.cgi', status_code=200, content_type='text/html', content=b'<title>CGI</title>\n
CGI
\n
CGI is a standard for interfacing external applications with information servers, such as HTTP or web servers.
\n\n')
    test_get_request(path='/autoindex/', status_code=200, content_type='text/html')

    # Host header
    test_get_request(path='/', status_code=200, file_path='./tests/html/index.html', content_type='text/html', host=None)
    test_get_request(path='/', status_code=200, file_path='./tests/html/index.html', content_type='text/html', host='nosuchhost')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index1.html', content_type='text/html', host='webserv1')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index2.html', content_type='text/html', host='webserv2')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index3.html', content_type='text/html', host='webserv3')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index4.html', content_type='text/html', host='webserv4')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index5.html', content_type='text/html', host='webserv5')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index2.html', content_type='text/html', host='WEBSERV2')
    test_get_request(path='/', status_code=200, file_path='./tests/html/index3.html', content_type='text/html', host='WeBsErV3')

    # POST
    # test_post_request(path='/limit_except/post/', status_code=200, content_type='text/html', data=b'Hello, world!\n', content=b'Hello, world!\n') # TODO: Fix conf file
    test_post_request(path='/limit_except/get/', status_code=405, content_type='text/html', data=b'Hello, world!\n')
    test_post_request(path='/cgi/echo.py', status_code=200, content_type='text/plain', content=b'Hello, world!\n', data=b'Hello, world!\n')
    test_post_request(path='/cgi/echo.py', status_code=200, content_type='text/plain', content=b'42Tokyo', data=b'42Tokyo')
    test_post_request(path='/cgi/echo.py', status_code=200, content_type='text/plain', content=b'42Tokyo', data=b'42Tokyo')
    random_bytes = os.urandom(1000000)
    test_post_request(path='/cgi/echo.py', status_code=200, content_type='text/plain', content=random_bytes, data=random_bytes)
    test_post_request(path='/upload/', status_code=201, content_type='application/json', content=b'{"success":"true"}', data=b'Hello, world!\n') # TODO: Test Location /upload/1695790257-1622650073 (random)
    test_post_request(path='/upload/limit/', status_code=201, content_type='application/json', data=os.urandom(1024), content=b'{"success":"true"}')
    test_post_request(path='/upload/limit/', status_code=413, content_type='text/html', data=os.urandom(1025))

    # POST(Chunked)
    test_post_request(path='/cgi/echo.py', status_code=200, content_type='text/plain', content=b'Hi, there bob!', data=gen())
    test_post_request(path='/cgi/echo.py', status_code=200, content_type='text/plain', content=random_bytes, data=gen(random_bytes))

    # Server Error
    test_post_request(path='/cgi-invalid-handler/echo.py', status_code=500, content_type='text/html', data=b'Hello, world!\n')

    # GET (with query string)
    test_get_request(path='/?foo=bar', status_code=200, file_path='./tests/html/index.html', content_type='text/html')

    # GET (with fragment)
    # However, requests.request() will remove fragment in the actual HTTP request. So, we need another way to test this.
    # test_get_request(path='/#somefragment', status_code=200, file_path='./tests/html/index.html', content_type='text/html')
    # test_get_request(path='/?foo=bar#somefragment', status_code=200, file_path='./tests/html/index.html', content_type='text/html')

    # GET (with percent encoding)
    test_get_request(path='/alias/foo.html', status_code=200, file_path='./tests/html/foo/foo.html', content_type='text/html')
    test_get_request(path='/alias%2ffoo.html', status_code=200, file_path='./tests/html/foo/foo.html', content_type='text/html')

    # GET PATH_INFO
    test_get_request(path='/cgi/hello.py/foobar/path', status_code=200, content_type='text/plain', content=b'Hello, world!\n')

    # GET (with header name case insensitive)
    test_get_request(path='/', status_code=200, file_path='./tests/html/index1.html', content_type='text/html', headers={'Host': 'webserv1'})
    test_get_request(path='/', status_code=200, file_path='./tests/html/index1.html', content_type='text/html', headers={'host': 'webserv1'})
    test_get_request(path='/', status_code=200, file_path='./tests/html/index1.html', content_type='text/html', headers={'hOsT': 'webserv1'})

    # GET (with ../)
    # However, requests.request() will remove /../ in the actual HTTP request by resolving the relative path.
    # So, we need another way to test this.
    # test_get_request(path='/../', status_code=400, content_type='text/html')
    # test_get_request(path='/path/to/somewhere/../', status_code=400, content_type='text/html')
    # test_get_request(path='/path/to/somewhere/..', status_code=400, content_type='text/html')
    # GET (with .. but valid)
    test_get_request(path='/..path/to../..somewhere..', status_code=404, content_type='text/html')

    # Content-Length and Transfer-Encoding (Bad Request)
    test_post_request(path='/cgi/echo.py', status_code=400, content_type='text/html', data=gen(), headers={'Content-Length': '10', 'Transfer-Encoding': 'chunked'})

    ## Slow test
    ### GET (with query string)
    test_get_request(path='/cgi/echo.py?foo=bar', status_code=200, content_type='text/plain', content=b'')
    ### Server Error
    test_post_request(path='/cgi/infinite_loop.py', status_code=504, content_type='text/html', data=b'Hello, world!\n')
    test_get_request(path='/cgi/infinite_loop.py', status_code=504, content_type='text/html')

    if err_cnt > 0:
        sys.stdout.write('\033[31m' + str(err_cnt) + '/' + str(cnt) + ' tests failed.\033[0m\n')
        sys.exit(1)
    else:
        sys.stdout.write('\033[32m' + str(cnt) + '/' + str(cnt) + ' tests passed.\033[0m\n')
        sys.exit(0)


import pytest

@pytest.mark.parametrize(
    'raw_signature,expected',
    (
        ('balanceOf(address who)', 'balanceOf(address)'),
        ('balanceOf( address who )', 'balanceOf(address)'),
        ('balanceOf( address who )', 'balanceOf(address)'),
        ('transfer(address who, uint amount)', 'transfer(address,uint256)'),
        ('register(bytes32[] names, uint[] values)', 'register(bytes32[],uint256[])'),
    )
)
def test_import_from_raw_text_signature(raw_signature, expected, models):
    sig, _ = models.Signature.import_from_raw_text_signature(raw_signature)
    assert sig.text_signature == expected

@pytest.mark.parametrize(
    'bad_signature',
    (
        'balanceOf(',
        '0x1234abcd',
        '[]',
    )
)
def test_import_from_raw_text_signature_with_bad_signatures(bad_signature, models):
    res = models.Signature.import_from_raw_text_signature(bad_signature)
    assert res is None


import pytest

from lrctoolbox import SyncedLyrics

artist = "Pritam, Arijit Singh"
title = "Shayad"
album = "Love Aaj Kal"
lyricist = "Irshad Kamil"
uri = "spotify:track:foobarbazqux"
length = "200000"
re_name = "LRCMaker"
version = "1.0.0"
author = "DrB"
synced_lyrics_lines = [
    "[00:00.00]Foo bar",
    "[00:05.00]Baz qux",
    "[00:10.00]Quux quuz",
]

lines = [
    f"[ar:{artist}]",
    f"[ti:{title}]",
    f"[al:{album}]",
    f"[au:{lyricist}]",
    f"[by:{author}]",
    f"[re:{re_name}]",
    f"[ve:{version}]",
    f"[uri:{uri}]",
    f"[length:{length}]",
] + synced_lyrics_lines

@pytest.fixture(scope="module")
def only_lyrics():
    return synced_lyrics_lines

@pytest.fixture
def lines_with_metadata():
    return lines

@pytest.fixture
def metadata():
    return {
        "artist": artist,
        "title": title,
        "album": album,
        "lyricist": lyricist,
        "author": author,
        "re_name": re_name,
        "version": version,
        "uri": uri,
        "length": length,
    }

@pytest.fixture
def sample_synced_lyrics(lines_with_metadata):
    return SyncedLyrics.load_from_lines(lines_with_metadata)


from BeautifulSoup import BeautifulSoup
from selenium import webdriver
import time

dict_group = {}
data_total_page = 0
urls = {'culture':"https://www.douban.com/group/explore/culture"
,'travel':"https://www.douban.com/group/explore/travel"
,'ent':"https://www.douban.com/group/explore/ent"
,'fashion':"https://www.douban.com/group/explore/fashion"
,'life':"https://www.douban.com/group/explore/life"
,'tech':"https://www.douban.com/group/explore/tech"}

def getFromText():
	file_fromText = open('txt.txt','r')
	contents = file_fromText.readlines()
	return '\n'.join(contents)

def getPageSource(url):
	driver.get(url)
	time.sleep(1)
	return driver.page_source

driver = webdriver.Firefox()
for key in urls:
	html_source = getPageSource(urls[key])
	soup = BeautifulSoup(html_source)

	for page in soup.findAll('span',attrs={'class':'thispage'}):
		data_total_page = int(page['data-total-page']) + 1

	step = 1
	for count in range(1,data_total_page):
		html_source =getPageSource( urls[key] + '?start=' + str(step) )
		soup = BeautifulSoup(html_source)
		step = 30 * count
		for sub in soup.findAll('span',attrs={'class':'from'}):
			group_name = sub.a.string
			href = sub.a['href']
			if group_name not in dict_group:
				dict_group[group_name] = href

	writer = open(key+'.txt','w')
	for key in dict_group:
		writer.write(dict_group[key])
	writer.close()
	dict_group = {}


import tensorflow as tf
from network.silknet import LoadInterface
from network.silknet.FolderDataReader import FolderDataReader
from interface import implements
import configparser as cp
import gzip
import pickle
import os
from tensorflow.contrib.ndlstm.python import lstm2d as lstm2d_lib
import numpy as np
import cv2
import sys

slim = tf.contrib.slim

class DataLoader(implements(LoadInterface)):
    def load_datum(self, full_path):
        # The file is compressed, so load it using gzip
        f = gzip.open(os.path.join(full_path, '__dump__.pklz'), 'rb')
        doc = pickle.load(f)
        f.close()

        # We don't need to anything with this
        input_tensor = doc.input_tensor

        # Convert left-share to one-hot encoding
        left_class = doc.classes_tensor[:,:,0]
        left_class_one_hot = np.zeros((256,256,2))
        left_class_one_hot[left_class==0, 0] = 1
        left_class_one_hot[left_class==1, 1] = 1

        # Convert top-share to one-hot encoding
        top_class = doc.classes_tensor[:,:,1]
        top_class_one_hot = np.zeros((256,256,2))
        top_class_one_hot[top_class==0, 0] = 1
        top_class_one_hot[top_class==1, 1] = 1

        loss_mask = doc.zone_mask * doc.word_mask

        return input_tensor, left_class_one_hot, top_class_one_hot, loss_mask

class Parser2d:
    def __init__(self, config_section):
        config = cp.ConfigParser()
        config.read('config.ini')
        self.train_path = config[config_section]['train_data_path']
        self.test_path = config[config_section]['test_data_path']
        self.validation_data_path = config[config_section]['validation_data_path']
        self.learning_rate = float(config[config_section]['learning_rate'])
        self.save_after = int(config[config_section]['save_after'])
        self.model_path = config[config_section]['model_path']
        self.from_scratch = int(config[config_section]['from_scratch']) == 1
        self.batch_size = int(config[config_section]['batch_size'])
        self.summary_path = config[config_section]['summary_path']

        self.alpha_left = float(config['zone_segment']['alpha_left'])
        self.alpha_top = float(config['zone_segment']['alpha_top'])

        # For usage from other functions
        self.input_placeholder = None
        self.classifier_left_same_placeholder = None
        self.classifier_top_same_placeholder = None
        self.optimizer = None
        self.loss_mask_placeholder = None
        self.loss = None
        self.loss_left = None
        self.loss_top = None

    @staticmethod
    def arg_scope(weight_decay=0.0005):
        with slim.arg_scope([slim.conv2d, slim.fully_connected],
                            activation_fn=None,
                            weights_regularizer=slim.l2_regularizer(weight_decay),
                            biases_initializer=tf.zeros_initializer()):
            with slim.arg_scope([slim.conv2d], padding='SAME') as arg_sc:
                return arg_sc

    def construct_graphs(self):
        self.input_placeholder = tf.placeholder("float32", shape=[self.batch_size, 256, 256, 308])
        self.classifier_left_same_placeholder = tf.placeholder("float32", shape=[self.batch_size, 256, 256, 2])
        self.classifier_top_same_placeholder = tf.placeholder("float32", shape=[self.batch_size, 256, 256, 2])
        self.loss_mask_placeholder = tf.placeholder("float32", shape=[self.batch_size, 256, 256])

        network = lstm2d_lib.separable_lstm(self.input_placeholder, 100) # (B,256,256,100)

        with slim.arg_scope(Parser2d.arg_scope()):
            output_left_same = slim.conv2d(network, 2, [1, 1], scope='logits_left_same') # (B,256,256,2)
            output_top_same = slim.conv2d(network, 2, [1, 1], scope='logits_top_same') # (B,256,256,2)

        # Apply softmax cross entropy
        loss_left = tf.nn.softmax_cross_entropy_with_logits(labels=self.classifier_left_same_placeholder, logits=output_left_same)
        loss_top = tf.nn.softmax_cross_entropy_with_logits(labels=self.classifier_top_same_placeholder, logits=output_top_same)

        # Mask the loss
        loss_left = tf.multiply(loss_left, self.loss_mask_placeholder)
        loss_top = tf.multiply(loss_top, self.loss_mask_placeholder)

        # Reduce mean (over only the masked indices)
        # Reducing twice in axis 1 will keep the batch dimension (as required)
        num_words = tf.reduce_sum(tf.reduce_sum(self.loss_mask_placeholder, axis=1), axis=1) # (B)
        # Finally, we have to reduce mean across batch dimension
        self.loss_left = tf.reduce_mean(tf.reduce_sum(tf.reduce_sum(loss_left, axis=1), axis=1) / num_words)
        self.loss_top = tf.reduce_mean(tf.reduce_sum(tf.reduce_sum(loss_top, axis=1), axis=1) / num_words)

        left_predicted_class = tf.argmax(output_left_same, axis=3) #(B,H,W)
        top_predicted_class = tf.argmax(output_left_same, axis=3)

        left_gt_class = tf.argmax(self.classifier_left_same_placeholder, axis=3) #(B,H,W)
        top_gt_class = tf.argmax(self.classifier_top_same_placeholder, axis=3)

        self.accuracy_left = tf.reduce_sum(tf.reduce_sum(
            tf.cast(tf.equal(left_predicted_class, left_gt_class), tf.float32) * self.loss_mask_placeholder, axis=1), axis=1) / num_words # (B)
        self.accuracy_top = tf.reduce_sum(tf.reduce_sum(
            tf.cast(tf.equal(top_predicted_class, top_gt_class), tf.float32) * self.loss_mask_placeholder, axis=1), axis=1) / num_words # (B)

        self.positive_predicted_left = tf.reduce_sum(
            tf.reduce_sum(tf.cast(tf.equal(left_predicted_class, tf.zeros_like(left_predicted_class)), tf.float32) * self.loss_mask_placeholder, axis=1),
            axis=1) / num_words
        self.positive_predicted_top = tf.reduce_sum(
            tf.reduce_sum(tf.cast(tf.equal(top_predicted_class, tf.zeros_like(top_predicted_class)), tf.float32) * self.loss_mask_placeholder, axis=1),
            axis=1) / num_words

        self.num_words = num_words

        # Combine both top and left loss
        self.loss = self.alpha_left * self.loss_left + self.alpha_top * self.loss_top

        summary_loss_complete = tf.summary.scalar('loss_complete', self.loss)
        summary_loss_left = tf.summary.scalar('loss_left', self.loss_left)
        summary_loss_top = tf.summary.scalar('loss_top', self.loss_top)
        summary_accuracy_top = tf.summary.scalar('accuracy_top', tf.reduce_mean(self.accuracy_top))
        summary_accuracy_left = tf.summary.scalar('accuracy_left', tf.reduce_mean(self.accuracy_left))
        summary_predicted_left_zero = tf.summary.scalar('predicted_left_zero', tf.reduce_mean(self.positive_predicted_left))
        summary_predicted_top_zero = tf.summary.scalar('predicted_top_zero', tf.reduce_mean(self.positive_predicted_left))

        self.summaries = tf.summary.merge(
            [summary_loss_complete, summary_loss_left, summary_loss_top, summary_accuracy_top, summary_accuracy_left,
             summary_predicted_left_zero, summary_predicted_top_zero])

        # Optimizer is Adam
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss)

        self.saver_all = tf.train.Saver()

    def clean_summary_dir(self):
        for the_file in os.listdir(self.summary_path):
            file_path = os.path.join(self.summary_path, the_file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
            except Exception as e:
                print(e)

    def train(self):
        train_set = FolderDataReader(self.train_path, DataLoader())
        train_set.init()
        init = tf.global_variables_initializer()

        print("\n\nNOTE: The cost in the following log will be mean across batch. However, accuracies and positive figures will"
              "be for the first data point in the batch for better debugging.\n\n")

        with tf.Session() as sess:
            if self.from_scratch:
                self.clean_summary_dir()

            summary_writer = tf.summary.FileWriter(self.summary_path, sess.graph)
            sess.run(init)

            if not self.from_scratch:
                self.saver_all.restore(sess, self.model_path)
                print("\n\nINFO: Saving model\n\n")
                with open(self.model_path+'.txt', 'r') as f:
                    iteration = int(f.read())
            else:
                iteration = 0

            while True:
                # Save the model and iteration number to ckpt and txt files respectively
                if iteration % self.save_after == 0:
                    print("\n\nINFO: Saving model\n\n")
                    self.saver_all.save(sess, self.model_path)
                    with open(self.model_path+'.txt', 'w') as f:
                        f.write(str(iteration))

                data, epochs, ids = train_set.next_batch(self.batch_size)
                inputs = [data[i][0] for i in range(len(data))]
                left_same = [data[i][1] for i in range(len(data))]
                top_same = [data[i][2] for i in range(len(data))]
                loss_mask = [data[i][3] for i in range(len(data))]

                input_feed = {
                    self.input_placeholder : inputs,
                    self.classifier_left_same_placeholder : left_same,
                    self.classifier_top_same_placeholder : top_same,
                    self.loss_mask_placeholder : loss_mask
                }

                run_ops = [self.optimizer, self.loss, self.accuracy_left, self.accuracy_top,
                           self.positive_predicted_left, self.positive_predicted_top, self.summaries]

                ops_results = sess.run(run_ops, feed_dict=input_feed)

                print("Cost", ops_results[1], "Accuracy Left", ops_results[2][0], "Positive Left",
                      ops_results[4][0], "Accuracy Top", ops_results[3][0], "Positive top", ops_results[5][0])

                summary_writer.add_summary(ops_results[6], iteration)
                iteration += 1

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("Error in arguments")
        print("Usage: python table_parse_2d/parser_2d.py [config_section_name]")
        print("\tFor example, if the section name in the config file is `zone_segment` you can issue:\n"
              "\tpython table_parse_2d/parser_2d.py zone_segment")
        sys.exit(-1)
    parser = Parser2d(sys.argv[1])
    parser.construct_graphs()
    parser.train()


#!/usr/bin/env python3
# coding=utf-8
# © 2019 Mastergatto
# This code is covered under GPLv2+, see COPYING
#####################

###############
## VARIABLES ##
###############
if __name__ == "__main__":
    import os, platform
    # Set the current directory to that of this file.
    os.chdir(os.path.dirname(os.path.realpath(__file__)))

    # Before importing or do anything, we must set those env variable so that this program can work correctly on Linux
    #Gdk.set_allowed_backends("x11, win32, quartz")
    system = platform.system()
    if system == 'Linux':
        #TODO: For now force x11 backend on linux, remove this later.
        os.environ['GDK_BACKEND'] = 'x11'
        if os.environ.get('GDK_BACKEND'):
            if os.environ['GDK_BACKEND'] == 'x11':
                try:
                    import ctypes
                    x11 = ctypes.cdll.LoadLibrary('libX11.so')
                    x11.XInitThreads()
                except:
                    print("Warning: failed to XInitThreads()")
        if not os.environ.get( 'PYOPENGL_PLATFORM' ):
            os.environ['PYOPENGL_PLATFORM'] = 'egl'
    elif system == 'Windows':
        os.environ["PYSDL2_DLL_PATH"] = f"{os.path.dirname(os.path.realpath(__file__))}\\"
    elif system == 'Darwin':
        pass

    #try:
    #    # Keep GTK+ from mixing languages
    #    #Locale.setlocale(Locale.LC_MESSAGES, "C")
    #    locale.setlocale(locale.LC_ALL, "")
    #except locale.Error:
    #    log.err("Unsupported locale setting. Fix your locales")

#############
## MODULES ##
#############
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
gi.require_version("Gio", "2.0")
gi.require_version("GLib", "2.0")

from gi.repository import Gtk, Gdk, Gio, GLib
from gettext import gettext as _
import argparse, sys

import logging as log
import utils.logging as u_log
import widget.window as w_main

###############
## FUNCTIONS ##
###############

##########
## MAIN ##
##########

class GoodOldM64pApp(Gtk.Application):

    def __init__(self, app_id):
        self.app_id = app_id
        super().__init__(application_id=self.app_id,
                                flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE)
        self.connect('command-line', self.do_command_line)
        GLib.set_application_name(_("Good Old Mupen64+"))
        GLib.set_prgname('gom64p')
        #GLib.setenv("")
        #self.settings = Gio.Settings.new('org.mupen64plus.good-old-m64p')

        self.main = None
        self.args = None
        self.frontend_conf = None
        self.logger = u_log.Logger()

    def do_startup(self):
        '''
        Gtk.Application startup handler
        '''

        Gtk.Application.do_startup(self)
    def do_activate(self):
        '''
        Gtk.Application activate handler
        '''

        if not self.main:
            self.main = w_main.GoodOldM64pWindow(self)

    def do_shutdown(self):
        '''
        Gtk.Application shutdown handler
        Do clean up before the application is closed.
        this is triggered when self.quit() is called.
        '''

        log.debug("Closing...")
        if self.frontend_conf != None:
            self.frontend_conf.write()
        Gtk.Application.do_shutdown(self)

    def do_command_line(self, argc, argv):
        '''
        Gtk.Application command line handler
        called if Gio.ApplicationFlags.HANDLES_COMMAND_LINE is set.
        must call the self.do_activate() to get the application up and running.
        '''

        #https://docs.python.org/3/library/argparse.html
        command_parser = argparse.ArgumentParser(prog="Good Old Mupen64+",
                        usage=sys.argv[0] + " [OPTIONS]",
                        description="gom64p is a frontend (in Python \
                        and GTK+3) for mupen64plus, a free software and \
                        multi-platform Nintendo 64 emulator.")
        command_parser.add_argument('-d','--debug', action='store_true',
                      help='Enable debug mode (not related with mupen64plus\'s \
                      debugger!)')
        command_parser.add_argument('--enable-csd', action='store_true',
                      help='Enable Client Side Decorations (only on GNOME)')
        command_parser.add_argument('--lib', action='store',
                      help='path to libmupen64plus')
        command_parser.add_argument('--configdir', action='store',
                      help='path to mupen64plus\'s configs dir')
        command_parser.add_argument('--plugindir', action='store',
                      help='path to mupen64plus\'s plugin dir')
        command_parser.add_argument('--datadir', action='store',
                      help='path to mupen64plus\'s data dir')
        self.args = command_parser.parse_args(argv.get_arguments()[1:])
        self.do_activate()
        return 0

if __name__ == "__main__":
    try:
        application = GoodOldM64pApp('net.Mastergatto.gom64p')

        exit_status = application.run(sys.argv)
        sys.exit(exit_status)
    except KeyboardInterrupt:
        log.warning("Keyboard interrupt (Ctrl + C), shutting down...")
        sys.exit()


import json
import logging
import polars as pl
import boto3

logger = logging.Logger(__name__)
logger.setLevel(logging.INFO)
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(sh)

def messages_to_df(messages: list) -> pl.DataFrame:
    """Converts list of messages to a dataframe

    Args:
        messages (list): messages

    Returns:
        pl.DataFrame: dataframe
    """

    measurements = []
    for message in messages:
        body = json.loads(message["Body"])
        measurement = json.loads(body["Message"])
        measurements.append(measurement)

    df = pl.from_dicts(measurements)

    return df

def receive_messages(
    queue_url: str,
    region_name: str,
    max_nr_messages: int = 100000,
    visibility_timeout: int = 1800,
    page_size_messages: int = 10
    ) -> list:
    """Queries for messages on aws sqs

    Args:
        queue_url (str): url of the queue
        region_name (str): region name of the queue
        max_nr_messages (int, optional): Maximum nr of messages to receive. Defaults to 100000.
        visibility_timeout (int, optional): Visibility timeout for received messages. Defaults to 1800.
        page_size_messages (int, optional): Amount of messages to receive per call. Defaults to 10.

    Returns:
        list: received messages
    """

    # define sqs client
    sqs = boto3.client("sqs", region_name=region_name)
    messages = []
    messages_received = 0
    # receive messages until no more messages visible or mx nar messages received
    while messages_received < max_nr_messages:
        logger.warning(f"{messages_received} messages received...")
        # query queue
        response = sqs.receive_message(
            QueueUrl=queue_url,
            MaxNumberOfMessages=page_size_messages,
            VisibilityTimeout=visibility_timeout
            )

        # add message to message list
        if "Messages" in response:
            current_messages = response["Messages"]
            messages_received += len(current_messages)
            messages.extend(current_messages)
        else:
            logger.warning(f"no messages received...")
            break

    return messages


from urllib.parse import urlencode
from luqum.parser import parser
import luqum
from flask import Flask, request, redirect, render_template, abort

application = Flask(__name__)

@application.route('/search/activity/')
@application.route('/search/activity')
def activity_search():
    '''
    Returns a redirect to the v2 (new) datastore.

    Request params are mapped onto OIPA filters.
    '''

    test = False

    request_args = dict([(k, v) for (k, v) in request.args.items()])

    # Current location of v2 (new) datastore
    base_url = 'https://datastore.codeforiati.org/api/1/access/activity.xml?unwrap=True'

    # We collect redirect request params here
    if (request_args.get('wt') != 'xslt') and (request_args.get('tr') != 'activity'):
        could_not_redirect('Datastore Classic is only able to serve IATI XML requests, as the format of JSON and CSV outputs differ from Datastore v2.')

    if request_args.get('test') is not None:
        test = True
        del request_args['test']

    filters = {}

    def fix_group(tree):
        if (type(tree) in (luqum.tree.Group, luqum.tree.FieldGroup)) and (len(tree.children) == 1):
            tree = tree.children[0]
        return tree

    def fix_name(name):
        # DS Classic automatically searches on activities and transactions
        # for sectors and recipient countries
        fixed = name.replace("transaction_", "")
        if fixed in ["sector", "recipient_country_code"]:
            return fixed
        return name

    def get_from_or(tree, filters):
        seen_fields = []
        for child in tree.children:
            if type(child) == luqum.tree.SearchField:
                fixed_name = fix_name(child.name)
                if (len(seen_fields) > 0):
                    if fixed_name not in seen_fields:
                        return abort(400, "Not implemented: ORs with different fields")
                expr = fix_group(child.expr)
                if fixed_name not in filters:
                    filters[fixed_name] = expr.value
                else:
                    if expr.value not in filters[fixed_name]:
                        filters[fixed_name] += '|' + expr.value
                seen_fields.append(fixed_name)
        return filters

    def get_from_and(tree, filters):
        for child in tree.children:
            if type(child) == luqum.tree.SearchField:
                child_expr = fix_group(child.expr)
                filters[child.name] = child_expr.value
            else:
                if (type(child) == luqum.tree.Group) and (len(child.children) == 1):
                    child = child.children[0]
                if type(child) == luqum.tree.OrOperation:
                    filters = get_from_or(child, filters)
        return filters

    def parse_expression(expr):
        filters = {}
        tree = parser.parse(expr)
        tree = fix_group(tree)
        if type(tree) == luqum.tree.OrOperation:
            filters = get_from_or(tree, filters)
        elif type(tree) == luqum.tree.AndOperation:
            filters = get_from_and(tree, filters)
        elif type(tree) == luqum.tree.SearchField:
            expr = fix_group(tree.expr)
            filters[tree.name] = expr.value
        return filters

    # From: https://docs.google.com/spreadsheets/d/19Qs6naJhoMIDpgbtNWr2Uab1mzzJge61_vfP4mEYCTs/edit

    solr_request_args = parse_expression(request_args.get('q'))

    mappings = {
        'iati_identifier': 'iati-identifier',
        'recipient_country_code': 'recipient-country',
        'recipient_region_code': 'recipient-region',
        'reporting_org_ref': 'reporting-org',
        'sector': 'sector'
    }

    # Check if any unexpected URL params have been used ...
    unknown_filters = [x for x in solr_request_args.keys()
                       if x not in mappings.keys()]
    if unknown_filters != []:
        # ... if so, error
        return could_not_redirect('Unknown filter(s): {}'.format(
            ', '.join(unknown_filters)))

    # Check if any of the undefined mappings have been used ...
    undefined_mappings = [x for x in solr_request_args.keys()
                          if not mappings.get(x)]
    if undefined_mappings != []:
        # ... if so, error
        return could_not_redirect('Mapping to new filter(s) not known: {}'.format(
            ', '.join(undefined_mappings)))

    # Build the redirect request params
    for old_filter, value in solr_request_args.items():
        new_filter = mappings[old_filter]
        # v2 (new) datastore uses `,` as a separator
        # for lists of values, whereas
        # v1 (old) datastore used `|`
        filters[new_filter] = value.replace(',', '|')

    rows = request_args.get('rows', 1)
    if (int(rows) > 1000):
        filters['stream'] = 'True'
    else:
        filters['limit'] = rows

    # Return the redirect
    url = base_url
    if filters:
        url = base_url + '&' + urlencode(filters)
    if test:
        return url
    return redirect(url)

@application.route('/api/activities/')
@application.route('/api/activities')
def activity():
    test = False
    '''
    Returns a redirect to the v2 (new) datastore.

    Request params are mapped onto OIPA filters.
    '''

    request_args = dict([(k, v) for (k, v) in request.args.items()])

    # Current location of v2 (new) datastore
    base_url = 'https://datastore.codeforiati.org/api/1/access/activity.xml?unwrap=True'

    # We collect redirect request params here
    # NB the default of this DSv2 endpoint is XML so we
    # also redirect this.
    if request_args.get('format', 'xml') != 'xml':
        return could_not_redirect('Datastore Classic is only able to serve XML requests, as the format of JSON and CSV outputs differ from Datastore v2.')
    if 'format' in request.args: del request_args['format']

    if request_args.get('test') is not None:
        test = True
        del request_args['test']

    filters = {}

    # From: https://docs.google.com/spreadsheets/d/19Qs6naJhoMIDpgbtNWr2Uab1mzzJge61_vfP4mEYCTs/edit
    mappings = {
        'iati_identifier': 'iati-identifier',
        'recipient_country': 'recipient-country',
        'recipient_region': 'recipient-region',
        'reporting_organisation_identifier': 'reporting-org',
        'sector': 'sector',
        'participating_organisation': 'participating-org',
        'related_activity_id': 'related-activity',
        'transaction_provider_organisation': 'transaction_provider-org',
        'transaction_provider_activity': 'transaction_provider-org.provider-activity-id',
        'transaction_receiver_organisation': 'transaction_receiver-org',
        'transaction_receiver_activity': 'transaction_receiver-org.receiver-activity-id',
        'actual_start_date_lte': 'start-date__lt',
        'actual_start_date_gte': 'start-date__gt',
        'actual_end_date_lte': 'end-date__lt',
        'actual_end_date_gte': 'end-date__gt',
        'rows': 'limit'
    }

    # Check if any unexpected URL params have been used ...
    unknown_filters = [x for x in request_args.keys()
                       if x not in mappings.keys()]
    if unknown_filters != []:
        # ... if so, error
        return could_not_redirect('Unknown filter(s): {}'.format(
            ', '.join(unknown_filters)))

    # Check if any of the undefined mappings have been used ...
    undefined_mappings = [x for x in request_args.keys()
                          if not mappings.get(x)]
    if undefined_mappings != []:
        # ... if so, error
        return could_not_redirect('Mapping to new filter(s) not known: {}'.format(
            ', '.join(undefined_mappings)))

    # Build the redirect request params
    for old_filter, value in request_args.items():
        new_filter = mappings[old_filter]
        # v2 (new) datastore uses `,` as a separator
        # for lists of values, whereas
        # v1 (old) datastore used `|`
        if (new_filter == 'limit') and (int(value) > 1000):
            filters['stream'] = 'True'
        else:
            filters[new_filter] = value.replace(',', '|')

    # Return the redirect
    url = base_url
    if filters:
        url = base_url + '&' + urlencode(filters)
    if test:
        return url
    return redirect(url)

def could_not_redirect(message):
    return abort(400, message)

@application.errorhandler(400)
def error_400(error):
    return render_template('could_not_redirect.html', error=error), 400

@application.errorhandler(404)
def error_404(error):
    return render_template(
        'could_not_redirect.html',
        error={'description': "Redirects have not been implemented for that route."}), 404

@application.route('/')
def home():
    '''
    Serve the homepage.

    Very simple - just read and return the contents of index.html
    '''
    with open('index.html') as handler:
        html = handler.read()
    return html


from numpy import ones, power

from .utils import BandedContinuum, Continuum, dry_air_number_density, P0, radiation_term, \
                   Spectrum, subgrid_bounds, T0

class CarbonDioxideContinuum(BandedContinuum):
    def __init__(self):
        self.bands = [CarbonDioxideHartmannContinuum(self.path),]

class CarbonDioxideHartmannContinuum(Continuum):
    """Carbon dioxide continuum coefficients.

    Attributes:
        data: Spectrum object containing data read from an input dataset.
        t_correction: Array of temperature correction coefficients.
        xfac_co2: Array of chi-factors.
    """
    def __init__(self, path):
        self.data = Spectrum(path, "bfco2")

        x = Spectrum(path, "tdep_bandhead")
        lower, upper = subgrid_bounds(self.data.grid, x.grid)
        self.t_correction = ones(self.data.data.size)
        self.t_correction[lower:upper + 1] = x.data[:]

        x = Spectrum(path, "x_factor_co2")
        lower, upper = subgrid_bounds(self.data.grid, x.grid)
        self.xfac_co2 = ones(self.data.data.size)
        self.xfac_co2[lower:upper + 1] = x.data[:]

    def spectra(self, temperature, pressure, vmr):
        nco2 = dry_air_number_density(pressure, temperature, vmr)*vmr["CO2"]
        rad = radiation_term(self.grid()[:], temperature)
        return \
            nco2*1.e-20*(pressure/P0)*(T0/temperature)*rad[:] * \
            self.xfac_co2[:]*power(temperature/246., self.t_correction[:]) * \
            self.data.data[:]

    def grid(self):
        return self.data.wavenumbers()


import pandas as pd
import string
from nltk.corpus import stopwords

def count_punctuation(df):
    # Counts the number of punctuations in the text
    count = lambda l1, l2: sum([1 for x in l1 if x in l2])
    df['count_punct'] = df.motivation.apply(lambda s: count(s, string.punctuation))
    df.to_csv(r'..\data\processed\motivation_liwc_meta.csv', index=False)

def count_stopwords(df):
    # Counts the number of stopwords in the text
    stopword_list = set(stopwords.words('Dutch'))
    df['count_stopwords'] = df['motivation'].str.split().apply(lambda x: len(set(x) & stopword_list))
    df.to_csv(r'..\data\processed\motivation_liwc_meta.csv', index=False)

# Add extra columns to the dataset
if __name__ == "__main__":
    # df = pd.read_csv(r'..\data\processed\liwc_results.csv')
    # print(count_punctuation(df))

    df = pd.read_csv(r'..\data\processed\motivation_liwc_meta.csv')
    count_stopwords(df)


import pytest

from mock import patch

from payit import Transaction, TransactionError, GatewayNetworkError
from payit.gateways import ZarinpalGateway

from tests.mockup.zarinpal_gateway import get_side_effect

config = {
    "merchant": "534534534532225234234",
    "description": "",
    "callback_url": "http://localhost/callback",
    "proxies": "socks5://127.0.0.1:9050",
}

def domock(sideeffect):
    return patch("payit.gateways.zarinpal.Client", side_effect=sideeffect)

def test_gateway():
    with domock(get_side_effect()):
        gateway = ZarinpalGateway(config=config)
        transaction = gateway.request_transaction(
            Transaction(amount=1000, order_id=1)
        )
        gateway.get_redirection(transaction)

        # validation
        valid_transaction = gateway.validate_transaction(
            {"Authority": "RETURNED_TOKEN", "Status": "OK"}
        )
        assert valid_transaction.validate_status

        invalid_transaction = gateway.validate_transaction(
            {"Authority": "RETURNED_TOKEN", "Status": "NOK"}
        )
        assert not invalid_transaction.validate_status

    # success verification
    with domock(get_side_effect(returned_status=100)):
        gateway.verify_transaction(transaction, dict())

    # verification fails
    with domock(get_side_effect(returned_status=300)):
        with pytest.raises(TransactionError):
            gateway.verify_transaction(transaction, dict())

    # verification already done
    with domock(get_side_effect(returned_status=101)):
        with pytest.raises(TransactionError):
            gateway.verify_transaction(transaction, dict())

    # verification network error
    with domock(get_side_effect(raise_zeep_error=True)):
        with pytest.raises(GatewayNetworkError):
            gateway.verify_transaction(transaction, dict())

    # no token
    with domock(get_side_effect(returned_token=None)):
        with pytest.raises(TransactionError):
            gateway.request_transaction(Transaction(amount=1000, order_id=1))

    # transation error
    with domock(get_side_effect(raise_zeep_fault=True)):
        with pytest.raises(TransactionError):
            gateway.request_transaction(Transaction(amount=1000, order_id=1))

    # gateway error
    with domock(get_side_effect(raise_zeep_error=True)):
        with pytest.raises(GatewayNetworkError):
            gateway.request_transaction(Transaction(amount=1000, order_id=1))


from collections import deque

seats = [x for x in input().split(", ")]
first_sequence = deque([int(num) for num in input().split(", ")])
second_sequence = deque([int(num) for num in input().split(", ")])

seat_matches = []
rotation = 0

while True:
    if len(seat_matches) == 3 or rotation == 10:
        break

    first_num = first_sequence.popleft()
    last_num = second_sequence.pop()

    symbol = chr(first_num + last_num)
    seat_1 = f'{first_num}{symbol}'
    seat_2 = f'{last_num}{symbol}'
    rotation += 1

    if seat_1 in seat_matches or seat_2 in seat_matches:
        continue

    if seat_1 in seats:
        seat_matches.append(seat_1)

    if seat_2 in seats:
        seat_matches.append(seat_2)

    if seat_1 not in seat_matches and seat_2 not in seat_matches:
        first_sequence.append(first_num)
        second_sequence.appendleft(last_num)

print(f"Seat matches: {', '.join([str(num) for num in seat_matches])}")
print(f"Rotations count: {rotation}")

10.Exam Preparation/12.Exam 18 August 2022/01.Stewards.py


#!/usr/bin/env python3
""" An evaluation script for MNASNet. """

import os
import typing
import collections

import torch
import torchvision.models as models
import numpy
import tqdm

import imagenet
import metrics
import log

IMAGENET_DIR = os.path.expanduser("~/datasets/imagenet")

def eval(model_name: str) -> None:
    if model_name == "mnasnet0_5":
        model = models.mnasnet0_5(num_classes=1000, pretrained=True).cuda()
    elif model_name == "mnasnet0_75":
        model = models.mnasnet0_75(num_classes=1000).cuda()
    elif model_name == "mnasnet1_0":
        model = models.mnasnet1_0(num_classes=1000, pretrained=True).cuda()
    elif model_name == "mnasnet1_3":
        model = models.mnasnet1_3(num_classes=1000).cuda()
    else:
        raise ValueError("Don't know how to evaluate {}".format(model_name))

    loss = torch.nn.CrossEntropyLoss().cuda()
    val_dataset = imagenet.validation(IMAGENET_DIR)
    val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=512,
                                             shuffle=False, num_workers=8,
                                             pin_memory=True)
    all_metrics = metrics.default()

    model.eval()
    with torch.no_grad():
        val_losses = []
        metric_dict = collections.defaultdict(list)
        for batch_index, (inputs, truth) in enumerate(tqdm.tqdm(val_loader)):
            outputs = model(inputs.cuda()).cpu()
            val_losses.append(loss(outputs, truth).item())
            for name, metric_fn in all_metrics:
                metric_dict[name].append(metric_fn(outputs, truth))

        print(
            numpy.mean(val_losses),
            list([(name, numpy.mean(vals))
                  for name, vals in metric_dict.items()]))

if __name__ == "__main__":
    for m in ["mnasnet1_0", "mnasnet0_5"]:
        print("Evaluating pretrained", m)
        eval(m)


import json
import boto3
import os
import requests
import openai

BUCKET_NAME='minutes-prod-contents'

s3 = boto3.resource('s3')

def main(event, context):
    print('ðŸ”¥')
    print(event)
    fname = "/tmp/leothefootball"
    openai.api_key = get_parameter()
    event_object = event['Records'][0]['s3']['object']['key']
    bucket = s3.Bucket(BUCKET_NAME)
    bucket.download_file(event_object, '/tmp/hoge.m4a')
    audio_file = open('/tmp/hoge.m4a', "rb")
    transcript = openai.Audio.transcribe("whisper-1", audio_file)
    txt = "\n".join(transcript['text'].split())
    f = open(fname + ".txt", "w")
    f.write(txt)
    f.close()
    upload(fname + ".txt")

def get_parameter():
    end_point = 'http://localhost:2773'
    path = '/systemsmanager/parameters/get/'
    query_parameter = '?name=/minutes/prod/OPENAI_API_KEY&withDecryption=true'
    url = end_point + path + query_parameter
    headers = {
        'X-Aws-Parameters-Secrets-Token': os.environ['AWS_SESSION_TOKEN']
    }
    res = requests.get(url, headers=headers)
    return res.json()['Parameter']['Value']

def upload(file):
    response = s3.Object(BUCKET_NAME, f'documents/{os.path.basename(file)}').upload_file(file)
    return response

if __name__ == "__main__":
  event = { 'data': '', 'part': '0' }
  main(event, '')


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

import numpy as np
import pickle as pkl
import matplotlib.pyplot as plt

#data = pkl.load(file("TME6_lettres.pkl","rb"))
with open('TME6_lettres.pkl', 'rb') as f:
    data = pkl.load(f, encoding='latin1')
X = np.array(data.get('letters')) # récupération des données sur les lettres
Y = np.array(data.get('labels')) # récupération des étiquettes associées

# affichage d'une lettre
def tracerLettre(let):
    a = -let*np.pi/180; # conversion en rad
    coord = np.array([[0, 0]]); # point initial
    for i in range(len(a)):
        x = np.array([[1, 0]]);
        rot = np.array([[np.cos(a[i]), -np.sin(a[i])],[ np.sin(a[i]),np.cos(a[i])]])
        xr = x.dot(rot) # application de la rotation
        coord = np.vstack((coord,xr+coord[-1,:]))
    plt.figure()
    plt.plot(coord[:,0],coord[:,1])
    plt.savefig("exlettre.png")
    return

def discretise(Xcont, d):
    intervalle = 360 / d;
    return np.array([np.floor(x/intervalle) for x in Xcont]);

def groupByLabel( y):
    index = []
    for i in np.unique(y): # pour toutes les classes
        ind, = np.where(y==i)
        index.append(ind)
    return index;

def learnMarkovModel(Xc, d):
    #A = np.zeros((d,d))
    #Pi = np.zeros(d)
    A = np.ones((d,d))
    Pi = np.ones(d)
    for element in Xc:
        last = np.int(element[0]);
        Pi[last] += 1;
        for i in range(1, element.size):
            current = np.int(element[i]);
            A[last][current] += 1.0;
            last = current;

    A_norm = A/np.maximum(A.sum(1).reshape(d,1),1) # normalisation
    Pi = Pi/Pi.sum()
    return(Pi, A_norm)

def probaSequence(s,Pi,A):
    last = np.int(s[0]);
    proba = np.log(Pi[last]);
    for i in range(1,len(s)):
        current = np.int(s[i]);
        proba += np.log(A[last][current]);
        last = current;
    return proba;

def calculProba(d, Xlocal1, Ylocal1, Xlocal2):
    Xd1 = discretise(Xlocal1,d)  # application de la discrétisation
    Xd2 = discretise(Xlocal2,d)
    index = groupByLabel(Ylocal1)  # groupement des signaux par classe
    models = []
    for cl in range(len(np.unique(Ylocal1))): # parcours de toutes les classes et optimisation des modèles
        models.append(learnMarkovModel(Xd1[index[cl]], d))

    proba = np.array([
        [probaSequence(Xd2[i], models[cl][0], models[cl][1]) for i in range(len(Xlocal2))]
        for cl in range(len(np.unique(Ylocal1)))
    ])
    return (proba,models)

### Evaluation des performances ###

# Char à numero
proba, models = calculProba(20, X, Y, X)
Ynum = np.zeros(Y.shape);
for num,char in enumerate(np.unique(Y)):
    Ynum[Y==char] = num;

pred = proba.argmax(0);
print("teste avec le méme donnée")
print(np.where(pred != Ynum, 0.,1.).mean())

### Evalution juste ###

# separation app/test, pc=ratio de points en apprentissage
def separeTrainTest(y, pc):
    indTrain = []
    indTest = []
    for i in np.unique(y): # pour toutes les classes
        ind, = np.where(y==i)
        n = len(ind)
        indTrain.append(ind[np.random.permutation(n)][:int(np.floor(pc*n))])
        indTest.append(np.setdiff1d(ind, indTrain[-1]))
    return indTrain, indTest
# exemple d'utilisation
itrain,itest = separeTrainTest(Y,0.8)

def iTrain():
    ia = []
    for i in itrain:
        ia += i.tolist()
    it = []
    for i in itest:
        it += i.tolist()
    return (ia, it)

ia,it=iTrain()

# Char à numero
Ynum = np.zeros(Y[it].shape);
for num,char in enumerate(np.unique(Y)):
    Ynum[Y[it]==char] = num;

# change dimension et regarde l'effet sur les resultats
predictions = []
for d in range(10,90,10):
    Xd_test = discretise(X[it], d)

    proba = calculProba(d, X[ia], Y[ia], X[it])[0]

    pred = proba.argmax(0);
    print("teste avec le separation à training et test, d =",d)
    print(np.where(pred != Ynum, 0.,1.).mean())
    predictions.append(pred);

def visualise_conf(verite, prediction, Ylocal):
    conf = np.zeros((26,26))
    for cl in range (len(Ynum)):
        v = int(verite[cl])
        p = prediction[cl]
        conf[v][p] += 1

    plt.figure()
    plt.imshow(conf, interpolation='nearest')
    plt.colorbar()
    plt.xticks(np.arange(26),np.unique(Ylocal))
    plt.yticks(np.arange(26),np.unique(Ylocal))
    plt.xlabel(u'Vérité terrain')
    plt.ylabel(u'Prédiction')
    plt.savefig("mat_conf_lettres.png")

def prochaineEtat(dist):
    r = np.random.uniform()
    cs = dist.cumsum()
    i = 0
    while(cs[i] < r):
        i += 1
    return i

def generate(pi, model, N):
    liste = []
    current = prochaineEtat(pi)
    for i in range(N):
        liste.append(current)
        dist = model[current]
        current = prochaineEtat(dist)
    return liste

#Evaluation qualitative
pred = predictions[2] # d = 20
visualise_conf(Ynum, pred, Y);
#Modèle génératif
models = calculProba(d, X, Y, X)[1]
newa = generate(models[0][0],models[0][1], 25) # generation d'une séquence d'états
intervalle = 360./d # pour passer des états => valeur d'angles
newa_continu = np.array([i*intervalle for i in newa]) # conv int => double
tracerLettre(newa_continu)


# blender CAM collision.py (c) 2012 Vilem Novak
#
# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****

import bpy
import time

from cam import simple
from cam.simple import *

BULLET_SCALE = 10000
# this is a constant for scaling the rigidbody collision world for higher precision from bullet library
CUTTER_OFFSET = (-5 * BULLET_SCALE, -5 * BULLET_SCALE, -5 * BULLET_SCALE)
# the cutter object has to be present in the scene , so we need to put it aside for sweep collisions,
# otherwise it collides itself.

def getCutterBullet(o):
    """cutter for rigidbody simulation collisions
        note that everything is 100x bigger for simulation precision."""

    s = bpy.context.scene
    if s.objects.get('cutter') is not None:
        c = s.objects['cutter']
        activate(c)

    type = o.cutter_type
    if type == 'END':
        bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius= o.cutter_diameter / 2,
                                            depth= o.cutter_diameter, end_fill_type='NGON',
                                            align='WORLD', enter_editmode=False, location = CUTTER_OFFSET,
                                            rotation=(0, 0, 0))
        cutter = bpy.context.active_object
        cutter.scale *= BULLET_SCALE
        bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
        bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
        bpy.ops.rigidbody.object_add(type='ACTIVE')
        cutter = bpy.context.active_object
        cutter.rigid_body.collision_shape = 'CYLINDER'
    elif type == 'BALLNOSE':

            # ballnose ending used mainly when projecting from sides.
            # the actual collision shape is capsule in this case.
            bpy.ops.mesh.primitive_ico_sphere_add(subdivisions = 3, radius = o.cutter_diameter / 2,
                                                  align='WORLD', enter_editmode=False,
                                                location = CUTTER_OFFSET, rotation=(0, 0, 0))
            cutter = bpy.context.active_object
            cutter.scale *= BULLET_SCALE
            bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
            bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
            bpy.ops.rigidbody.object_add(type='ACTIVE')
            cutter = bpy.context.active_object
            #cutter.dimensions.z = 0.2 * BULLET_SCALE  # should be sufficient for now... 20 cm.
            cutter.rigid_body.collision_shape = 'CAPSULE'
            #bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)

    elif type == 'VCARVE':

        angle = o.cutter_tip_angle
        s = math.tan(math.pi * (90 - angle / 2) / 180) / 2  # angles in degrees
        cone_d = o.cutter_diameter * s
        bpy.ops.mesh.primitive_cone_add(vertices=32, radius1 = o.cutter_diameter / 2, radius2=0,
                                        depth =  cone_d, end_fill_type='NGON',
                                        align='WORLD', enter_editmode=False, location = CUTTER_OFFSET,
                                        rotation=(math.pi, 0, 0))
        cutter = bpy.context.active_object
        cutter.scale *= BULLET_SCALE
        bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
        bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
        bpy.ops.rigidbody.object_add(type='ACTIVE')
        cutter = bpy.context.active_object
        cutter.rigid_body.collision_shape = 'CONE'
    elif type == 'CYLCONE':

        angle = o.cutter_tip_angle
        s = math.tan(math.pi * (90 - angle / 2) / 180) / 2  # angles in degrees
        cylcone_d =(o.cutter_diameter - o.cylcone_diameter) * s
        bpy.ops.mesh.primitive_cone_add(vertices = 32, radius1 = o.cutter_diameter / 2,
                                        radius2 = o.cylcone_diameter / 2,
                                        depth = cylcone_d, end_fill_type='NGON',
                                        align = 'WORLD', enter_editmode = False,
                                        location = CUTTER_OFFSET,rotation = (math.pi, 0, 0))
        cutter = bpy.context.active_object
        cutter.scale *= BULLET_SCALE
        bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
        bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
        bpy.ops.rigidbody.object_add(type='ACTIVE')
        cutter = bpy.context.active_object
        cutter.rigid_body.collision_shape = 'CONVEX_HULL'
        cutter.location = CUTTER_OFFSET
    elif type == 'BALLCONE':
        angle = math.radians(o.cutter_tip_angle)/2
        cutter_R = o.cutter_diameter/2
        Ball_R = o.ball_radius/math.cos(angle)
        conedepth = (cutter_R - o.ball_radius)/math.tan(angle)
        bpy.ops.curve.simple(align='WORLD', location=(0, 0, 0), rotation=(0, 0, 0),
                                         Simple_Type='Point', use_cyclic_u=False)
        oy = Ball_R
        for i in range(1 , 10):
            ang = -i * (math.pi/2-angle) / 9
            qx = math.sin(ang) * oy
            qy = oy - math.cos(ang) * oy
            bpy.ops.curve.vertex_add(location=(qx , qy , 0))
        conedepth += qy
        bpy.ops.curve.vertex_add(location=(-cutter_R , conedepth , 0))
        #bpy.ops.curve.vertex_add(location=(0 , conedepth , 0))
        bpy.ops.object.editmode_toggle()
        bpy.ops.object.convert(target='MESH')
        bpy.ops.transform.rotate(value = -math.pi / 2, orient_axis = 'X')
        bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
        ob = bpy.context.active_object
        ob.name = "BallConeTool"
        ob_scr = ob.modifiers.new(type='SCREW', name = 'scr')
        ob_scr.angle = math.radians(-360)
        ob_scr.steps = 32
        ob_scr.merge_threshold = 0
        ob_scr.use_merge_vertices = True
        bpy.ops.object.modifier_apply(modifier='scr')
        bpy.data.objects['BallConeTool'].select_set(True)
        cutter = bpy.context.active_object
        cutter.scale *= BULLET_SCALE
        bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
        bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
        bpy.ops.rigidbody.object_add(type='ACTIVE')
        cutter.location = CUTTER_OFFSET
        cutter.rigid_body.collision_shape = 'CONVEX_HULL'
        cutter.location = CUTTER_OFFSET
    elif type == 'CUSTOM':
        cutob = bpy.data.objects[o.cutter_object_name]
        activate(cutob)
        bpy.ops.object.duplicate()
        bpy.ops.rigidbody.object_add(type='ACTIVE')
        cutter = bpy.context.active_object
        scale = o.cutter_diameter / cutob.dimensions.x
        cutter.scale *= BULLET_SCALE * scale
        bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
        bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')

        # print(cutter.dimensions,scale)
        bpy.ops.rigidbody.object_add(type='ACTIVE')
        cutter.rigid_body.collision_shape = 'CONVEX_HULL'
        cutter.location = CUTTER_OFFSET

    cutter.name = 'cam_cutter'
    o.cutter_shape = cutter
    return cutter

def subdivideLongEdges(ob, threshold):
    print('subdividing long edges')
    m = ob.data
    scale = (ob.scale.x + ob.scale.y + ob.scale.z) / 3
    subdivides = []
    n = 1
    iter = 0
    while n > 0:
        n = 0
        for i, e in enumerate(m.edges):
            v1 = m.vertices[e.vertices[0]].co
            v2 = m.vertices[e.vertices[1]].co
            vec = v2 - v1
            l = vec.length
            if l * scale > threshold:
                n += 1
                subdivides.append(i)
        if n > 0:
            print(len(subdivides))
            bpy.ops.object.editmode_toggle()

            # bpy.ops.mesh.quads_convert_to_tris(quad_method='BEAUTY', ngon_method='BEAUTY')
            # bpy.ops.mesh.tris_convert_to_quads()

            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='EDGE')
            bpy.ops.object.editmode_toggle()
            for i in subdivides:
                m.edges[i].select = True
            bpy.ops.object.editmode_toggle()
            bpy.ops.mesh.subdivide(smoothness=0)
            if iter == 0:
                bpy.ops.mesh.select_all(action='SELECT')
                bpy.ops.mesh.quads_convert_to_tris(quad_method='SHORTEST_DIAGONAL', ngon_method='BEAUTY')
            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.object.editmode_toggle()
            ob.update_from_editmode()
        iter += 1

# n=0
#

def prepareBulletCollision(o):
    """prepares all objects needed for sampling with bullet collision"""
    progress('preparing collisions')

    print(o.name)
    active_collection = bpy.context.view_layer.active_layer_collection.collection
    t = time.time()
    s = bpy.context.scene
    s.gravity = (0, 0, 0)
    # cleanup rigidbodies wrongly placed somewhere in the scene
    for ob in bpy.context.scene.objects:
        if ob.rigid_body is not None and (bpy.data.objects.find('machine') > -1
                                          and ob.name not in bpy.data.objects['machine'].objects):
            activate(ob)
            bpy.ops.rigidbody.object_remove()

    for collisionob in o.objects:
        bpy.context.view_layer.objects.active = collisionob
        collisionob.select_set(state=True)
        bpy.ops.object.duplicate(linked=False)
        collisionob = bpy.context.active_object
        if collisionob.type == 'CURVE' or collisionob.type == 'FONT':  # support for curve objects collision
            if collisionob.type == 'CURVE':
                odata = collisionob.data.dimensions
                collisionob.data.dimensions = '2D'
            bpy.ops.object.convert(target='MESH', keep_original=False)

        if o.use_modifiers:
            depsgraph = bpy.context.evaluated_depsgraph_get()
            mesh_owner = collisionob.evaluated_get(depsgraph)
            newmesh = mesh_owner.to_mesh()

            oldmesh = collisionob.data
            collisionob.modifiers.clear()
            collisionob.data = bpy.data.meshes.new_from_object(mesh_owner.evaluated_get(depsgraph),
                                                               preserve_all_data_layers=True,
                                                               depsgraph=depsgraph)
            bpy.data.meshes.remove(oldmesh)

        # subdivide long edges here:
        if o.optimisation.exact_subdivide_edges:
            subdivideLongEdges(collisionob, o.cutter_diameter * 2)

        bpy.ops.rigidbody.object_add(type='ACTIVE')
        # using active instead of passive because of performance.TODO: check if this works also with 4axis...
        collisionob.rigid_body.collision_shape = 'MESH'
        collisionob.rigid_body.kinematic = True
        # this fixed a serious bug and gave big speedup, rbs could move since they are now active...
        collisionob.rigid_body.collision_margin = o.skin * BULLET_SCALE
        bpy.ops.transform.resize(value=(BULLET_SCALE, BULLET_SCALE, BULLET_SCALE),
                                 constraint_axis=(False, False, False), orient_type='GLOBAL', mirror=False,
                                 use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1,
                                 texture_space=False, release_confirm=False)
        collisionob.location = collisionob.location * BULLET_SCALE
        bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
        bpy.context.view_layer.objects.active = collisionob
        if active_collection in collisionob.users_collection:
            active_collection.objects.unlink(collisionob)

    getCutterBullet(o)

    # machine objects scaling up to simulation scale
    if bpy.data.objects.find('machine') > -1:
        for ob in bpy.data.objects['machine'].objects:
            activate(ob)
            bpy.ops.transform.resize(value=(BULLET_SCALE, BULLET_SCALE, BULLET_SCALE),
                                     constraint_axis=(False, False, False), orient_type='GLOBAL',
                                     mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH',
                                     proportional_size=1, texture_space=False,
                                     release_confirm=False)
            ob.location = ob.location * BULLET_SCALE
    # stepping simulation so that objects are up to date
    bpy.context.scene.frame_set(0)
    bpy.context.scene.frame_set(1)
    bpy.context.scene.frame_set(2)
    progress(time.time() - t)

def cleanupBulletCollision(o):
    if bpy.data.objects.find('machine') > -1:
        machinepresent = True
    else:
        machinepresent = False
    for ob in bpy.context.scene.objects:
        if ob.rigid_body is not None and not (machinepresent and ob.name in bpy.data.objects['machine'].objects):
            delob(ob)
    # machine objects scaling up to simulation scale
    if machinepresent:
        for ob in bpy.data.objects['machine'].objects:
            activate(ob)
            bpy.ops.transform.resize(value=(1.0 / BULLET_SCALE, 1.0 / BULLET_SCALE, 1.0 / BULLET_SCALE),
                                     constraint_axis=(False, False, False), orient_type='GLOBAL',
                                     mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH',
                                     proportional_size=1, texture_space=False,
                                     release_confirm=False)
            ob.location = ob.location / BULLET_SCALE

def getSampleBullet(cutter, x, y, radius, startz, endz):
    """collision test for 3 axis milling. Is simplified compared to the full 3d test"""
    scene = bpy.context.scene
    pos = scene.rigidbody_world.convex_sweep_test(cutter, (x * BULLET_SCALE, y * BULLET_SCALE, startz * BULLET_SCALE),
                                                  (x * BULLET_SCALE, y * BULLET_SCALE, endz * BULLET_SCALE))

    # radius is subtracted because we are interested in cutter tip position, this gets collision object center

    if pos[3] == 1:
        return (pos[0][2] - radius) / BULLET_SCALE
    else:
        return endz - 10

def getSampleBulletNAxis(cutter, startpoint, endpoint, rotation, cutter_compensation):
    """fully 3d collision test for NAxis milling"""
    cutterVec = Vector((0, 0, 1)) * cutter_compensation
    # cutter compensation vector - cutter physics object has center in the middle, while cam needs the tip position.
    cutterVec.rotate(Euler(rotation))
    start = (startpoint * BULLET_SCALE + cutterVec).to_tuple()
    end = (endpoint * BULLET_SCALE + cutterVec).to_tuple()
    pos = bpy.context.scene.rigidbody_world.convex_sweep_test(cutter, start, end)

    if pos[3] == 1:
        pos = Vector(pos[0])
        # rescale and compensate from center to tip.
        res = pos / BULLET_SCALE - cutterVec / BULLET_SCALE

        return res
    else:
        return None


from django.shortcuts import render
#getting the Http Responce on template
from django.http import HttpResponse

from .forms import  StudnetRegisterForm,UserForm
from django.views import  generic
from .models import  StudentRegister
from django.urls import reverse_lazy
#filtering Student Objects
from .filters import Searchfilter

#StudentForm Details and User Deatils
class StudentReg(generic.TemplateView):
    template_name='register.html'
    def get(self,request):
        context = {
            'student': StudnetRegisterForm,
            'user': UserForm
          }
        return render(request,self.template_name,context)

    def post(self,request):
        user_form = UserForm(data=request.POST)
        profile_form = StudnetRegisterForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            return   HttpResponse("sucess")

#get the all the StudentDetails

class StudentDetails(generic.ListView):
    model=StudentRegister

    def get_context_data(self,**kwargs):
        context=super().get_context_data(**kwargs)
        context['filter'] = Searchfilter(self.request.GET, queryset=self.get_queryset())
        return  context

#update the all the StudentDetails

class StudentUpdate(generic.UpdateView):
    model =StudentRegister
    fields = '__all__'
    success_url = reverse_lazy('stu_list')

#delete the all the StudentDetails

class StudentDelete(generic.DeleteView):
    model =StudentRegister
    success_url = reverse_lazy('stu_list')


import json
import os
import shlex
import shutil
from subprocess import list2cmdline

import click

from .github import Github

# default paths
objection_path = os.path.join(os.path.expanduser('~'), '.objection')
gadget_versions = os.path.join(objection_path, 'gadget_versions')

def list2posix_cmdline(seq):
    """
        Translate a sequence of arguments into a command line
        string.

        Implemented using shlex.quote because
        subprocess.list2cmdline doesn't work with POSIX
    """

    return ' '.join(map(shlex.quote, seq))

class BasePlatformGadget(object):
    """ Class with base methods for any platforms Gadget downloaded """

    def __init__(self, github: Github) -> None:
        """
            Build a new instance with an existing Github instance.

            :param github:
        """

        self.github = github

    @staticmethod
    def get_local_version(gadget_type: str) -> str:
        """
            Check and return the local version of the FridaGadget
            type we have.

            :return:
        """

        if not os.path.exists(gadget_versions):
            return '0'

        with open(gadget_versions, 'r') as f:
            versions = f.read()

        # load the json.
        try:

            versions = json.loads(versions)

        except json.decoder.JSONDecodeError:
            return '0'

        if gadget_type in versions:
            return versions[gadget_type]

        return '0'

    def set_local_version(self, gadget_type: str, version: str):
        """
            Writes the version number to file, recording it as
            the current local version.

            :param gadget_type:
            :param version:
            :return:
        """

        # read the current versions if it exists, else start
        # a new dictionary for it
        if os.path.exists(gadget_versions):

            # load the json from disk
            try:

                with open(gadget_versions, 'r') as f:
                    versions = json.loads(f.read())

            except json.decoder.JSONDecodeError:
                versions = {}

        else:
            versions = {}

        # add the new version
        versions[gadget_type] = version

        # and write it to file
        with open(gadget_versions, 'w') as f:
            f.write(json.dumps(versions))

        return self

class BasePlatformPatcher(object):
    """ Base class with methods used by any platform patcher. """

    # extended classes should fill this property
    required_commands = {}

    def __init__(self):

        # check dependencies
        self.have_all_commands = self._check_commands()
        self.command_run_timeout = 60 * 5

        if os.name == 'nt':
            self.list2cmdline = list2cmdline
        else:
            self.list2cmdline = list2posix_cmdline

    def _check_commands(self) -> bool:
        """
            Check if the shell commands in required_commands are
            available.

            :return:
        """

        for cmd, attributes in self.required_commands.items():

            location = shutil.which(cmd)

            if location is None:
                click.secho('Unable to find {0}. Install it with: {1} before continuing.'.format(
                    cmd, attributes['installation']), fg='red', bold=True)

                return False

            self.required_commands[cmd]['location'] = location

        return True

    def are_requirements_met(self):
        """
            Checks if the command requirements have all been met.

            :return:
        """

        return self.have_all_commands


from django.conf.urls import url
from django.views.generic import TemplateView
from . import views
print(views)
urlpatterns = [
# ex: /polls/
	url(r'^$', views.indexView.as_view(), name = 'index'),
    #url(r'^polls/$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^polls/(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    # ex: /polls/5/results/
    url(r'^polls/(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^polls/(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]


import spidev, time
spi = spidev.SpiDev()
spi.open(0, 0)
def analog_read(channel):
    r = spi.xfer2([1, (8 + channel) << 4, 0])
    adc_out = ((r[1]&3) << 8) + r[2]
    return adc_out
while True:
    adc_arus = analog_read(0)
    adc_tegangan = analog_read(1)
    adc_resistansi = analog_read(2)

    #Pengukuran arus
    Vadc_arus = adc_arus * 3.3 / 1024
    R_arus = 10 # Resistor sensor arus
    I_ukur = Vadc_arus/R_arus # Rumus arus dalam ampere
    I_mili = I_ukur*1000 #ARumus arus dalam miliampere

    #Pengukuran tegangan
    Vadc_tegangan = adc_tegangan * 3.3 / 1024
    R1 = 100000 # Resistor R1=100kohm
    R2 = 1000 # Resistor R2=1kohm
    V_ukur = Vadc_tegangan*(R1+R2)/R2 # Rumus tegangan dalam volt

    #Pengukuran resistansi
    Vadc_resistansi = adc_resistansi * 3.3 / 1024
    Vcc = 3.3
    R_sensor = 1000 # Resistor Sensor R_sensor=1kohm
    R_ukur = R_sensor/((Vcc/Vadc_resistansi)-1) # Rumus Resistansi dalam ohm

    print("Arus(mA)=%f\tTegangan(Volt)=%f\tResistansi(Ohm)=%f" % (I_mili, V_ukur, R_ukur))
    time.sleep(1)


#!/usr/bin/env python3

from argparse import ArgumentParser
import os

import pandas as pd
import seaborn as sns

def plot_empirical_cdf(df, field):
    df = df.copy()
    df["cancer"] = df["cancer"].map(lambda x: "Case" if x else "Control")
    df = df.rename(columns={"cancer": "group"})
    g = sns.FacetGrid(df, hue="group")
    g.map(sns.distplot, field)
    g.add_legend()
    return g

def compute_code_counts(df):
    # only care about unique counts since features are binary
    cts = df.groupby(["UID", "cancer"])["diag_cd"].agg(lambda x: len(set(x)))
    cts = cts.to_frame(name="ct")
    cts = cts.reset_index()
    return cts

def compute_time_intervals(df):
    time = df.groupby(["UID", "cancer"])["date"].agg(lambda x: max(x) - min(x))
    time = time.to_frame(name="time_interval")
    time = time.reset_index()
    return time

def get_args():
    parser = ArgumentParser(
        description=
        "Plot distribution of unique codes counts and time interval (i.e. history length)"
    )
    parser.add_argument("--input", type=str, help="Input feather dataframe")
    parser.add_argument(
        "--output_dir",
        type=str,
        help="Path to output directory",
    )
    return parser.parse_args()

def main():
    args = get_args()
    df = pd.read_feather(args.input)

    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    df_codes = compute_code_counts(df)
    ax_codes = plot_empirical_cdf(df_codes, "ct")
    ax_codes.set_xlabels("# Diagnosis Codes")
    ax_codes.set_ylabels("Empirical CDF (patients)")
    codes_plot_path = os.path.join(args.output_dir, "code_ct_distribution.pdf")
    ax_codes.savefig(codes_plot_path)

    df_time = compute_time_intervals(df)
    ax_time = plot_empirical_cdf(df_time, "time_interval")
    ax_time.set_xlabels("Time interval of medical history (days)")
    ax_time.set_ylabels("Empirical CDF (patients)")
    time_plot_path = os.path.join(args.output_dir,
                                  "time_interval_distribution.pdf")
    ax_time.savefig(time_plot_path)

if __name__ == "__main__":
    try:
        main()
    except Exception as err:
        import pdb
        pdb.post_mortem()


import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np

batch_size = 128

# transforms
transforms = transforms.Compose([
    transforms.RandAugment(),
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,)),

])

train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms, download=False)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)

def add_noise(image, noise_factor=0.5) :
    # noise_factor -> 잡음의 정도를 결정 하는 인자
    noisy_images = image + noise_factor * torch.rand_like(image)
    noisy_images = torch.clamp(noisy_images, -1, 1)

    return noisy_images

for images, _ in train_loader :
    print(images.shape)

    noisy_images = add_noise(images)
    break

import matplotlib.pyplot as plt

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(np.transpose(torchvision.utils.make_grid(images[:8], padding=2, normalize=True), (1,2,0)))
axes[0].set_title('Original Images')
axes[0].axis('off')

axes[1].imshow(np.transpose(torchvision.utils.make_grid(noisy_images[:8], padding=2, normalize=True), (1,2,0)))
"""
RGB 이미지의 경우는 일반적으로 높이 너비 채널 순가지고요
그레이 높이 너비 즉 높이, 너비, 채널 형식의 이미지를 시각화할때 (1, 2, 0),오르 np.transpose 사용하여 차원을 재배열 합니다.
"""
axes[1].set_title('Noisy Images')
axes[1].axis('off')

plt.show()


import numpy as np
import matplotlib.pyplot as plt

# Source: https://www.youtube.com/watch?v=35fzyblVdmA&ab_channel=ComputationalScientist

class Coordinate:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    @staticmethod
    def get_distance(a:int, b:int) -> float:
        return np.sqrt(np.abs(a.x - b.x) + np.abs(a.y + b.y))

    @staticmethod
    def get_total_distance(coords:list) -> float:
        dist = 0
        for first, second in zip(coords[:-1], coords[1:]):
            dist += Coordinate.get_distance(first, second)
        dist += Coordinate.get_distance(coords[0], coords[-1])
        return dist

if __name__ == '__main__':

    # Read in the coordinates from file
    # with open('coordinates.txt', 'r') as f:
    #     lines = [(line.strip().split(', ')) for line in f]
    #     coords = [Coordinate(int(l[0]), int(l[1])) for l in lines]

    coords = [Coordinate(np.random.uniform(), np.random.uniform()) for _ in range(10)]

    #Plot
    fig = plt.figure(figsize=(10, 5))
    ax1 = fig.add_subplot(121)
    ax2 = fig.add_subplot(122)

    for first, second in zip(coords[:-1], coords[1:]):
        ax1.plot([first.x, second.x], [first.y, second.y], 'b')
    ax1.plot([coords[0].x, coords[-1].x], [coords[0].y, coords[-1].y], 'b')

    for c in coords:
        ax1.plot(c.x, c.y, 'ro')

    # Simulated Annealing Algorithm
    cost0 = Coordinate.get_total_distance(coords)

    T = 30
    factor = 0.99
    T_init = T

    for i in range(1000):
        print(f'{i} cost = {cost0}')

        T = T * factor
        for j in range(100):
            # Exchange two coordinates and get a new neighbour solution
            r1, r2 = np.random.randint(0, len(coords), size=2) # Get two numbers from the indices of the coords

    #         # switch r1 & r2
            temp = coords[r1]
            coords[r1] = coords[r2]
            coords[r2] = temp

    #         # Get the new cost
            cost1 = Coordinate.get_total_distance(coords=coords)

            if cost1 < cost0:
                cost0 = cost1
            else:
                x = np.random.uniform()
                if x < np.exp((cost0-cost1)/T):
                    cost0 = cost1
                else:
                    # switch r1 & r2
                    temp = coords[r1]
                    coords[r1] = coords[r2]
                    coords[r2] = temp

    # #Plot
    for first, second in zip(coords[:-1], coords[1:]):
        ax2.plot([first.x, second.x], [first.y, second.y], 'b')
    ax2.plot([coords[0].x, coords[-1].x], [coords[0].y, coords[-1].y], 'b')

    for c in coords:
        ax2.plot(c.x, c.y, 'ro')

    plt.show()


from builtins import zip
import matplotlib
matplotlib.use("Agg")
import numpy as np
import unittest
import lsst.sims.maf.metrics as metrics
import lsst.utils.tests

class TestSimpleMetrics(unittest.TestCase):

    def setUp(self):
        dv = np.arange(0, 10, .5)
        dv2 = np.arange(-10, 10.25, .5)
        self.dv = np.array(list(zip(dv)), dtype=[('testdata', 'float')])
        self.dv2 = np.array(list(zip(dv2)), dtype=[('testdata', 'float')])

    def testMaxMetric(self):
        """Test max metric."""
        testmetric = metrics.MaxMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), self.dv['testdata'].max())

    def testMinMetric(self):
        """Test min metric."""
        testmetric = metrics.MinMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), self.dv['testdata'].min())

    def testMeanMetric(self):
        """Test mean metric."""
        testmetric = metrics.MeanMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), self.dv['testdata'].mean())

    def testMedianMetric(self):
        """Test median metric."""
        testmetric = metrics.MedianMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), np.median(self.dv['testdata']))

    def testAbsMedianMetric(self):
        testmetric = metrics.AbsMedianMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), np.abs(np.median(self.dv['testdata'])))

    def testFullRangeMetric(self):
        """Test full range metric."""
        testmetric = metrics.FullRangeMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), self.dv['testdata'].max()-self.dv['testdata'].min())

    def testCoaddm5Metric(self):
        """Test coaddm5 metric."""
        testmetric = metrics.Coaddm5Metric(m5Col='testdata')
        self.assertEqual(testmetric.run(self.dv), 1.25 * np.log10(np.sum(10.**(.8*self.dv['testdata']))))

    def testRmsMetric(self):
        """Test rms metric."""
        testmetric = metrics.RmsMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), np.std(self.dv['testdata']))

    def testSumMetric(self):
        """Test Sum metric."""
        testmetric = metrics.SumMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), self.dv['testdata'].sum())

    def testCountUniqueMetric(self):
        """Test CountUniqueMetric"""
        testmetric = metrics.CountUniqueMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), np.size(np.unique(self.dv['testdata'])))
        d2 = self.dv.copy()
        d2['testdata'][1] = d2['testdata'][0]
        self.assertEqual(testmetric.run(d2), np.size(np.unique(d2)))

    def testCountMetric(self):
        """Test count metric."""
        testmetric = metrics.CountMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), np.size(self.dv['testdata']))

    def testCountRatioMetric(self):
        """Test countratio metric."""
        testmetric = metrics.CountRatioMetric('testdata', normVal=2.)
        self.assertEqual(testmetric.run(self.dv), np.size(self.dv['testdata'])/2.0)

    def testCountSubsetMetric(self):
        """Test countsubset metric."""
        testmetric = metrics.CountSubsetMetric('testdata', subset=0)
        self.assertEqual(testmetric.run(self.dv), 1)

    def testMaxPercentMetric(self):
        testmetric = metrics.MaxPercentMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), 1.0/len(self.dv)*100.0)
        self.assertEqual(testmetric.run(self.dv2), 1.0/len(self.dv2)*100.0)

    def testAbsMaxPercentMetric(self):
        testmetric = metrics.AbsMaxPercentMetric('testdata')
        self.assertEqual(testmetric.run(self.dv), 1./len(self.dv)*100.)
        self.assertEqual(testmetric.run(self.dv2), 2./len(self.dv2)*100.)

    def testRobustRmsMetric(self):
        """Test Robust RMS metric."""
        testmetric = metrics.RobustRmsMetric('testdata')
        rms_approx = (np.percentile(self.dv['testdata'], 75) - np.percentile(self.dv['testdata'], 25)) / 1.349
        self.assertEqual(testmetric.run(self.dv), rms_approx)

    def testFracAboveMetric(self):
        cutoff = 5.1
        testmetric = metrics.FracAboveMetric('testdata', cutoff=cutoff)
        self.assertEqual(testmetric.run(self.dv),
                         np.size(np.where(self.dv['testdata'] >= cutoff)[0])/float(np.size(self.dv)))
        testmetric = metrics.FracAboveMetric('testdata', cutoff=cutoff, scale=2)
        self.assertEqual(testmetric.run(self.dv),
                         2.0*np.size(np.where(self.dv['testdata'] >= cutoff)[0])/float(np.size(self.dv)))

    def testFracBelowMetric(self):
        cutoff = 5.1
        testmetric = metrics.FracBelowMetric('testdata', cutoff=cutoff)
        self.assertEqual(testmetric.run(self.dv),
                         np.size(np.where(self.dv['testdata'] <= cutoff)[0])/float(np.size(self.dv)))
        testmetric = metrics.FracBelowMetric('testdata', cutoff=cutoff, scale=2)
        self.assertEqual(testmetric.run(self.dv),
                         2.0*np.size(np.where(self.dv['testdata'] <= cutoff)[0])/float(np.size(self.dv)))

    def testNoutliersNsigma(self):
        data = self.dv
        testmetric = metrics.NoutliersNsigmaMetric('testdata', nSigma=1.)
        med = np.mean(data['testdata'])
        shouldBe = np.size(np.where(data['testdata'] > med + data['testdata'].std())[0])
        self.assertEqual(shouldBe, testmetric.run(data))
        testmetric = metrics.NoutliersNsigmaMetric('testdata', nSigma=-1.)
        shouldBe = np.size(np.where(data['testdata'] < med - data['testdata'].std())[0])
        self.assertEqual(shouldBe, testmetric.run(data))

    def testMeanAngleMetric(self):
        """Test mean angle metric."""
        rng = np.random.RandomState(6573)
        dv1 = np.arange(0, 32, 2.5)
        dv2 = (dv1 - 20.0) % 360.
        dv1 = np.array(list(zip(dv1)), dtype=[('testdata', 'float')])
        dv2 = np.array(list(zip(dv2)), dtype=[('testdata', 'float')])
        testmetric = metrics.MeanAngleMetric('testdata')
        result1 = testmetric.run(dv1)
        result2 = testmetric.run(dv2)
        self.assertAlmostEqual(result1, (result2+20)%360.)
        dv = rng.rand(10000)*360.0
        dv = dv
        dv = np.array(list(zip(dv)), dtype=[('testdata', 'float')])
        result = testmetric.run(dv)
        result = result
        self.assertAlmostEqual(result, 180)

    def testFullRangeAngleMetric(self):
        """Test full range angle metric."""
        rng = np.random.RandomState(5422)
        dv1 = np.arange(0, 32, 2.5)
        dv2 = (dv1 - 20.0) % 360.
        dv1 = np.array(list(zip(dv1)), dtype=[('testdata', 'float')])
        dv2 = np.array(list(zip(dv2)), dtype=[('testdata', 'float')])
        testmetric = metrics.FullRangeAngleMetric('testdata')
        result1 = testmetric.run(dv1)
        result2 = testmetric.run(dv2)
        self.assertAlmostEqual(result1, result2)
        dv = np.arange(0, 358, 5)
        dv = np.array(list(zip(dv)), dtype=[('testdata', 'float')])
        result = testmetric.run(dv)
        self.assertAlmostEqual(result, 355)
        dv = rng.rand(10000)*360.0
        dv = np.array(list(zip(dv)), dtype=[('testdata', 'float')])
        result = testmetric.run(dv)
        result = result
        self.assertGreater(result, 355)

class TestMemory(lsst.utils.tests.MemoryTestCase):
    pass

def setup_module(module):
    lsst.utils.tests.init()

if __name__ == "__main__":
    lsst.utils.tests.init()
    unittest.main()


import datetime
import numbers
import time

class UnboundField(object):
    """
        Uninitialized version of all field objects, which records in what order
        it was initialized.  This is primarily used to ensure each time a new
        Model instance is initialized a new field object is created, rather
        than having to worry about invoking Field.__copy__() and worrying about
        any changes that might have happened to the original instance attached
        to the Model class.
    """
    idx = 0

    def __init__(self, cls, **kwargs):
        self.field_type = cls
        self.kwargs = kwargs
        UnboundField.idx += 1
        self.idx = UnboundField.idx

    def bind(self, name, trans_name, owner):
        kwargs = dict(self.kwargs)
        kwargs.update({'_name': name, '_trans_name': trans_name,
                       '_owner': owner})
        instance = self.field_type(**kwargs)
        instance.idx = self.idx
        instance.unbound_field = self
        setattr(owner.c, name, instance)

class MetaField(type):
    """
        Returns an unbound field if we don't know all the information about
        the field yet.
    """
    def __call__(cls, **kwargs):
        if len({'_trans_name', '_owner', '_name'} & set(kwargs.keys())) == 3:
            instance = cls.__new__(cls)
            instance.__init__(**kwargs)
            return instance
        else:
            return UnboundField(cls, **kwargs)

class Field(object):
    """
        Basic Field object, to be attached to models
    """
    __metaclass__ = MetaField

    @property
    def value(self):
        if self._idx != self.owner.current_idx:
            self._value = self.owner._records[self.owner.current_idx]
            self._value = self.to_python(self._value)
            self._idx = self.owner.current_idx

        return self._value

    @value.setter
    def value(self, val):
        if self._value != val:
            self._changed = True
            self._value = val

    def __init__(self, default=None, null=False, **kwargs):
        self.default = default
        self.null = null
        self._idx = None
        self._changed = False
        self._value = None

    def to_python(self, val):
        return value

    def to_db(self, val):
        return value

class Integer(Field):
    def __init__(self, length=None, unsigned=False, autoincrement=False, **kwargs):
        super(Integer, self).__init__(**kwargs)
        self.length = length
        self.unsigned = unsigned
        self.autoincrement = autoincrement

class TinyInt(Integer):
    pass

class SmallInt(Integer):
    pass

class MediumInt(Integer):
    pass

class BigInt(Integer):
    pass

class UnixTimestamp(Integer):
    def to_python(self, val):
        """
            Attempts to return a datetime object when returning data from
            the database, should throw a TypeError if the conversion fails.
        """
        return datetime.datetime.fromtimestamp(val)

    def to_db(self, val):
        """
            Attempts to convert the object to an int() if possible, except
            when the object happens to be an expression/column/model from
            the ORM, in which case the original value is returned, as that
            indicates it is an expression that the db will need to evaluate
            when the query is actually executed.
        """
        if isinstance(val, numbers.Integral) or hasattr(val, 'token_type'):
            return val
        elif hasattr(val, 'timetuple'):
            return int(time.mktime(val.timetuple()))
        else:
            return int(val)

class Decimal(Field):
    def __init__(self, precision=None, scale=None, unsigned=False, **kwargs):
        super(Field, self).__init__(self, **kwargs)
        self.precision = precision
        self.scale = scale
        self.unsigned = unsigned

class Char(Field):
    def __init__(self, length=None, **kwargs):
        super(Field, self).__init__(self, **kwargs)
        self.length = length

class Timestamp(Field):
    def __init__(self, on_update=None, **kwargs):
        super(Field, self).__init__(self, **kwargs)
        self.on_update = on_update