import torch
from torch.utils.data import Dataset, DataLoader
from tqdm import tqdm, trange
import torch.nn.functional as F
import os
import torch.utils.data as data
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from transformers import GPT2Tokenizer, GPT2LMHeadModel, AdamW, get_linear_schedule_with_warmup
from transformers import TextDataset,DataCollatorForLanguageModeling

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')

path_to_folders='SecLists-master'
model_name_to_save='gpt_passoword_model'
path_to_dataset='dataset.pt'

# tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')

def pack_tensor(new_tensor, packed_tensor, max_seq_len):
    if packed_tensor is None:
        return new_tensor, True, None
    if new_tensor.size()[1] + packed_tensor.size()[1] > max_seq_len:
        return packed_tensor, False, new_tensor
    else:
        packed_tensor = torch.cat([new_tensor, packed_tensor[:, 1:]], dim=1)
        return packed_tensor, True, None

def train( dataset, model, tokenizer,  batch_size=16, epochs=5, lr=2e-5,
    max_seq_len=400, warmup_steps=200,
    gpt2_type="gpt2", output_dir=".", output_prefix="wreckgar",
    test_mode=False,save_model_on_epoch=False,
):
    acc_steps = 100
    device=torch.device("cuda")
    model = model.cuda()
    model.train()

    optimizer = AdamW(model.parameters(), lr=lr)
    scheduler = get_linear_schedule_with_warmup(
        optimizer, num_warmup_steps=warmup_steps, num_training_steps=-1
    )

    train_dataloader = DataLoader(dataset, batch_size=1, shuffle=True)
    loss=0
    accumulating_batch_count = 0
    input_tensor = None

    for epoch in range(epochs):

        print(f"Training epoch {epoch}")
        print(loss)
        for idx, entry in tqdm(enumerate(train_dataloader)):
            (input_tensor, carry_on, remainder) = pack_tensor(entry, input_tensor, 768)

            if carry_on and idx != len(train_dataloader) - 1:
                continue

            input_tensor = input_tensor.to(device)
            outputs = model(input_tensor, labels=input_tensor)
            loss = outputs[0]
            loss.backward()

            if (accumulating_batch_count % batch_size) == 0:
                optimizer.step()
                scheduler.step()
                optimizer.zero_grad()
                model.zero_grad()

            accumulating_batch_count += 1
            input_tensor = None
        if save_model_on_epoch:
            torch.save(
                model.state_dict(),
                os.path.join(output_dir, f"{output_prefix}-{epoch}.pt"),
            )
    return model

dataset=torch.load(path_to_dataset)
train_dataloader = DataLoader(dataset, batch_size=1, shuffle=True)

model = train(train_dataloader, model, tokenizer)

torch.save(model,model_name_to_save)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('exams', '0035_auto_20150208_1951'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='madechoice',
            options={'ordering': ['user', 'choice__question__exam', 'choice__question']},
        ),
    ]


import tensorflow as tf
import numpy as np

keep_prob = 1.0
initializer = tf.random_uniform_initializer(-1, 1)

def biLstm(x, weights, biases,num_layers,num_units):
    outputs = x
    #track through the layers
    for layer in range(num_layers):
        with tf.variable_scope('encoder_{}'.format(layer),reuse=tf.AUTO_REUSE):

            #forward cells
            cell_fw = tf.contrib.rnn.LSTMCell(num_units,initializer=initializer)
            cell_fw = tf.contrib.rnn.DropoutWrapper(cell_fw, input_keep_prob = keep_prob)

            #backward cells
            cell_bw = tf.contrib.rnn.LSTMCell(num_units,initializer=initializer)
            cell_bw = tf.contrib.rnn.DropoutWrapper(cell_bw, input_keep_prob = keep_prob)

            (output_fw, output_bw), last_state = tf.nn.bidirectional_dynamic_rnn(cell_fw,cell_bw, outputs,dtype=tf.float32)
            state = last_state

    rnn_outputs_fw = tf.reshape(output_fw, [-1, num_units])
    rnn_outputs_bw = tf.reshape(output_bw, [-1, num_units])

    out_fw = tf.matmul(rnn_outputs_fw, weights['out']) + biases['out']
    out_bw = tf.matmul(rnn_outputs_bw, weights['out']) + biases['out']

    return np.add(out_fw,out_bw)


import media
import fresh_tomatoes

""" Create an object of type movie using the media.Movie class. For neatness,
    I assign values to each variable (title, storyline, poster_image,
    trailer_youtube), then pass the variables through to the class to create
    the instance."""

title = "Mulan"
storyline = ("The story of a man falling in love with another man only to "
            + "discover the man is actually a girl!")
poster_image = "http://www.reocities.com/Hollywood/5082/sword.jpg"
trailer_youtube = "https://www.youtube.com/watch?v=ZSS5dEeMX64"
mulan = media.Movie(title, storyline, poster_image, trailer_youtube)

title = "Cabin in the Woods"
storyline = "The most epic of horror films spoofs"
poster_image = "https://i.jeded.com/i/the-cabin-in-the-woods.31227.jpg"
trailer_youtube = "https://www.youtube.com/watch?v=uWX_RPnUpBk"
cabin_in_the_woods = media.Movie(title,storyline,poster_image, trailer_youtube)

title = "Silver Linings Playbook"
storyline = ("Jennifer Lawrence and Bradley Cooper try to outcrazy each other "
             + "in this quirky dramedy.")
poster_image = "http://www.impawards.com/2012/posters/silver_linings_playbook.jpg"
trailer_youtube = "https://www.youtube.com/watch?v=Lj5_FhLaaQQ"
silver_linings_playbook = media.Movie(title,storyline,poster_image,
                          trailer_youtube)

title = "Good Will Hunting"
storyline = ("Matt Damon is a janitor who is secretly smart." +
            " Robin Williams tells fart jokes.")
poster_image = "http://www.impawards.com/1997/posters/good_will_hunting.jpg"
trailer_youtube = "https://www.youtube.com/watch?v=PaZVjZEFkRs"
good_will_hunting = media.Movie(title,storyline,poster_image, trailer_youtube)

title = "La La Land"
storyline = "Old school musical style set in a modern world"
poster_image = "https://d35fkdjhhgt99.cloudfront.net/static/use-media-items/48/47382/full-1382x2048/587a5198/1.jpeg?resolution=0"
trailer_youtube = "https://www.youtube.com/watch?v=0pdqf4P9MB8"
la_la_land = media.Movie(title,storyline,poster_image, trailer_youtube)

title = "Get Out"
storyline = "White people try to become black people"
poster_image = "https://cdn.traileraddict.com/content/universal-pictures/get-out-2017-2.jpg"
trailer_youtube = "https://www.youtube.com/watch?v=xnkgiINreIk"
get_out = media.Movie(title,storyline,poster_image, trailer_youtube)

""" Here, we create a list of all the movies. We pass this list through to the
    open_movies_page function in the fresh_tomatoes module to create the
    webpage and open it."""
movies = [mulan, cabin_in_the_woods, silver_linings_playbook,
          good_will_hunting, la_la_land, get_out]
fresh_tomatoes.open_movies_page(movies)


import re
from .CanonicalIntervalSet import CanonicalIntervalSet

class MethodSet(CanonicalIntervalSet):
    """
    A class for holding a set of HTTP methods
    """
    all_methods_list = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']

    def __init__(self, all_methods=False):
        """
        :param bool all_methods: whether to create the object holding all methods
        """
        super().__init__()
        if all_methods:  # the whole range
            self.add_interval(self._whole_range_interval())

    def add_method(self, method):
        """
        Adds a given method to the MethodSet if the method is one of the eligible methods (in all_methods_list);
        otherwise raises ValueError exception
        :param str method: the method to add
        """
        index = self.all_methods_list.index(method)
        self.add_interval(self.Interval(index, index))

    def add_methods_from_regex(self, methods_regex):
        """
        Adds all methods in methods_regex to the MethodSet
        :param str methods_regex:
        """
        for index, method in enumerate(self.all_methods_list):
            if re.match(methods_regex, method):
                self.add_interval(self.Interval(index, index))

    def set_methods(self, methods):
        """
        Sets all methods from the given parameter
        :param CanonicalIntervalSet methods: the methods to set
        """
        for interval in methods:
            self.add_interval(interval)

    @staticmethod
    def _whole_range_interval():
        """
        :return: the interval representing the whole range (all methods)
        """
        return CanonicalIntervalSet.Interval(0, len(MethodSet.all_methods_list) - 1)

    @staticmethod
    def _whole_range_interval_set():
        """
        :return: the interval set representing the whole range (all methods)
        """
        interval = MethodSet._whole_range_interval()
        return CanonicalIntervalSet.get_interval_set(interval.start, interval.end)

    def is_whole_range(self):
        """
        :return: True if the MethodSet contains all methods, False otherwise
        """
        return self == self._whole_range_interval_set()

    @staticmethod
    def _get_method_names_from_interval_set(interval_set):
        """
        Returns names of methods represented by a given interval set
        :param CanonicalIntervalSet interval_set: the interval set
        :return: the list of method names
        """
        res = []
        for interval in interval_set:
            assert interval.start >= 0 and interval.end < len(MethodSet.all_methods_list)
            for index in range(interval.start, interval.end + 1):
                res.append(MethodSet.all_methods_list[index])
        return res

    @staticmethod
    def _get_compl_method_names_from_interval_set(interval_set):
        """
        Returns names of methods not included in a given interval set
        :param CanonicalIntervalSet interval_set: the interval set
        :return: the list of complement method names
        """
        res = MethodSet.all_methods_list.copy()
        for method in MethodSet._get_method_names_from_interval_set(interval_set):
            res.remove(method)
        return res

    def __str__(self):
        """
        :return: Compact string representation of the MethodSet
        """
        if self.is_whole_range():
            return '*'
        if not self:
            return 'Empty'
        method_names = self._get_method_names_from_interval_set(self)
        compl_method_names = self._get_compl_method_names_from_interval_set(self)
        if len(method_names) <= len(compl_method_names):
            values_list = ', '.join(method for method in method_names)
        else:
            values_list = 'all but ' + ', '.join(method for method in compl_method_names)

        return values_list

    def copy(self):
        # new_copy = copy.copy(self)  # the copy.copy() keeps the same reference to the interlval_set attribute
        new_copy = MethodSet()
        for interval in self.interval_set:
            new_copy.interval_set.append(interval.copy())
        return new_copy


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
from typing import Optional

class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reverseList_iter(self, head: Optional[ListNode]) -> Optional[ListNode]:
        #迭代，保存指向后一个节点的节点，然后将前一个指向前一个
        pre = None
        cur = head
        while cur:
            #将cur的下一个节点保存
            next = cur.next
            #将cur指向前一个节点
            cur.next = pre
            #都往前走一步
            pre = cur
            cur = next
        return pre

    #recursive
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head
        #当前节点后面的在调用函数中会反转, recersed 实际是一个新的链表的表头，里面的方向是倒序的
        reversed = self.reverseList(head.next)
        #反转当前节点
        #当前节点的下一个节点（正序）指向自己（逆序）
        head.next.next = head
        head.next=None

        return reversed

'''
206. Reverse Linked List
Easy
Given the head of a singly linked list, reverse the list, and return the reversed list.
Example 1:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Example 2:
Input: head = [1,2]
Output: [2,1]
Example 3:
Input: head = []
Output: []
Constraints:
The number of nodes in the list is the range [0, 5000].
-5000 <= Node.val <= 5000
'''


from typing import Tuple
from dataclasses import dataclass
from itertools import product

import numpy as np
import pandas as pd

from irtorch.dataset.entities import Dataset, Predictions
from irtorch.model.data import GRMInputs, GRMOutputs, GRMShapes

@dataclass()
class GRMMeta:
    item_category: pd.Categorical
    person_category: pd.Categorical
    level_category: pd.Categorical
    n_items: int
    n_persons: int
    n_grades: int
    n_levels: int

def inputs_from_dfs(dataset: Dataset) -> Tuple[GRMMeta, GRMInputs]:
    response_df, level_df = dataset.response_df, dataset.level_df
    if level_df is None:
        level_df = pd.DataFrame(columns=["item", "level"])

    assert "item" in response_df.columns
    assert "person" in response_df.columns
    assert "response" in response_df.columns

    assert np.issubdtype(response_df.response.dtype, np.integer)
    assert response_df.response.min() >= 1

    assert "item" in level_df.columns
    assert "level" in level_df.columns
    assert level_df.item.unique().all()

    response_df = response_df.astype({"item": "category", "person": "category"})
    item_category = response_df.item.dtype
    person_category = response_df.person.dtype

    level_df = pd.merge(
        item_category.categories.to_frame(name="item"),
        level_df
            .drop_duplicates(subset="item", keep="first")
            .astype({"item": item_category}),
        how="left"
    )
    level_df["level"] = level_df.level.fillna("_unknown").astype({"level": "category"})
    level_category = level_df.level.dtype

    n_items = len(item_category.categories)
    n_persons = len(person_category.categories)
    n_responses = len(response_df)
    n_grades = response_df.response.max()
    n_levels = len(level_category.categories)

    meta = GRMMeta(
        item_category,
        person_category,
        level_category,
        n_items, n_persons, n_grades, n_levels
    )
    inputs = GRMInputs(
        np.c_[response_df.item.cat.codes.values,
              response_df.person.cat.codes.values,
              response_df.response.values],
        GRMShapes(n_items, n_persons, n_grades, n_responses, n_levels),
        level_df.level.cat.codes.values.copy()
    )
    # ↑第3引数は、DataFrameのvalues (への参照) を直接返さないようにcopy()
    # ↑(第1引数はnp.c_[]で新たなndarrayが作られるのでcopy()不要)
    return meta, inputs

def outputs_to_dfs (meta: GRMMeta, outputs: GRMOutputs) -> Predictions:
    assert outputs.a_array.shape == (meta.n_items,)
    assert outputs.b_array.shape == (meta.n_items, meta.n_grades - 1)
    assert outputs.t_array.shape == (meta.n_persons,)
    assert outputs.level_mean_array.shape == (meta.n_levels, meta.n_grades - 1)
    assert outputs.level_std_array.shape == (meta.n_levels, meta.n_grades - 1)

    a_df = pd.DataFrame().assign(item=meta.item_category.categories, a=outputs.a_array)
    b_df = pd.DataFrame(
        product(meta.item_category.categories,
                np.arange(2, meta.n_grades + 1)),
        columns=["item", "grade"]) \
        .assign(b=outputs.b_array.flatten())
    t_df = pd.DataFrame().assign(person=meta.person_category.categories, t=outputs.t_array)
    level_df = pd.DataFrame(
        product(meta.level_category.categories,
                np.arange(2, meta.n_grades + 1)),
        columns=["level", "grade"]) \
        .assign(mean=outputs.level_mean_array.flatten(),
                std=outputs.level_std_array.flatten())

    return Predictions(a_df, b_df, t_df, level_df)


# -*- coding: utf-8 -*-
"""
Created on Tue Jan 21 02:50:04 2020

@author: dstus
"""
a = str(input())
c = 1
for i in range(1,len(a)):
    if a[i]==a[i-1]:
        c += 1
    else:
        c = 1
    if c==7:
        print('YES')
        break
if c!=7:
    print('NO')


from typing import List

import numpy as np
from sklearn.ensemble import IsolationForest

from video_recover.data_models.video import Video

class OutliersDetection:
    """Class to detect outliers in a video.

    It uses the Isolation Forest algorithm based on the deep features of the
    frames in the video.
    """

    def __init__(self):
        self.anomaly_detection_model = IsolationForest(random_state=0, n_estimators=120)

    def __call__(self, video: Video) -> List[int]:
        # get the features
        features = self.get_features(video)
        # fit the model
        self.anomaly_detection_model.fit(features)
        # get the indices of the noisy frames
        noisy_frames_indices = np.where(
            self.anomaly_detection_model.predict(features) == -1
        )[0]
        return noisy_frames_indices.tolist()

    def get_features(self, video: Video) -> np.ndarray:
        features = []
        for frame in video.frames:
            features.append(frame.deep_features)
        features = np.array(features)
        return features

class SimpleOutliersDetection:
    def __init__(self, threshold: float = 1.5):
        self.threshold = threshold

    def __call__(self, video: Video) -> List[int]:
        # get the count matches matrix
        count_matches_matrix = video.distance_metrics.count_matches
        # get the number of frames
        n_frames = len(count_matches_matrix)
        # get the number of matches for each frame
        n_matches = np.sum(count_matches_matrix, axis=1)
        # get the mean number of matches
        mean = np.mean(n_matches)
        # get the standard deviation of the number of matches
        std = np.std(n_matches)
        # get the outliers
        outliers = []
        for i in range(n_frames):
            if n_matches[i] < mean - self.threshold * std:
                outliers.append(i)
        return outliers


# from bs4 import BeautifulSoup
# import urllib.request

# import requests
# import shutil

# url=('https://www.reuters.com/pictures/pictures-600-days-war-ukraine-2023-10-15/')
# html_page = urllib.request.urlopen(url)
# soup = BeautifulSoup(html_page, features="html.parser")

# main = soup.find(id = 'main-content')

# count = 0
# for img in main.findAll('img'):
#     assa=(img.get('src'))
#     new_image=(url+assa)

#     count+=1

#     response = requests.get(assa).content#, stream=True)
#     #pic = urllib.urlopen(response)
#     with open('D:/Death&Disaster/Death-Disaster2023/images/ukraine/' + str(count) +'.jpg', 'w') as file:
#         file.write(response.read())

# print(count)

from bs4 import BeautifulSoup
import urllib.request
import requests

url = 'https://www.reuters.com/pictures/pictures-600-days-war-ukraine-2023-10-15/'
html_page = urllib.request.urlopen(url)
soup = BeautifulSoup(html_page, features="html.parser")

main = soup.find(id='main-content')

count = 0
# for img in main.findAll('img'):
#     img_src = img.get('src')
#     if img_src:
#         new_image_url = urllib.parse.urljoin(url, img_src)

#         count += 1

#         response = requests.get(new_image_url).content

#         with open(f'D:/Death&Disaster/Death-Disaster2023/images/ukraine/{count}.jpg', 'wb') as file:
#             file.write(response)

for caption in soup.findAll("div", class_="image-caption__caption__1npJ7"):
    text = caption.find("p", class_="text__text__1FZLe").get_text()
    count += 1
    print(text)

print(f"Downloaded {count} images.")


import requests

class ImageUrl():
    """
    存放图片的路径和删除路径
    """
    def __init__(self, name, url, delete):
        self.name = name
        self.url = url
        self.delete = delete

class SMMSTool():
    """
    sm.ms工具
    api：https://sm.ms/doc/
    """
    uploadUrl = 'https://sm.ms/api/upload'
    historyUrl = 'https://sm.ms/api/list'
    clearUrl = 'https://sm.ms/api/clear'
    urlList = []

    def __init__(self):
        self.cookies = dict(PHPSESSID='')

    def upload(self, filepath):
        """
        上传图片
        :param filepath: 待上传图片路径
        :return: [int, string] 状态码，状态
        """
        print("开始上传图片")
        files = {
            'smfile':open(filepath, 'rb')
        }
        params = {
            'ssl':True
        }
        r = requests.post(self.uploadUrl, params=params, files=files)
        commit_data = r.json()
        self.cookies['PHPSESSID'] = r.cookies['PHPSESSID']

        # 获取上传文件状态
        code = commit_data['code']
        if code == "success":       # 成功则构造ImageUrl加入urlList
            filename = commit_data['data']['filename']
            url = commit_data['data']['url']
            delete = commit_data['data']['delete']
            self.urlList.append(ImageUrl(filename, url, delete))
            self.outputInfo()
            return [0, "上传成功"]
        elif code == "error":       # 返回"error"则返回错误msg
            msg = commit_data['msg']
            return [-1, msg]
        else:
            return [-2, "未知异常"]

    def delete(self, imageUrl):
        """
        删除图片
        :param imageUrl: 待上传图片的ImageUrl对象
        :return: [int, string] 状态码，状态
        """
        print("开始删除图片")
        deleteUrl = imageUrl.delete
        r = requests.get(deleteUrl)
        if r.status_code == 200:
            return [0, "删除成功"]
        else:
            return [-1, "未知异常"]

    def history(self):
        """
        查询上传历史
        重要！！！：若要关闭程序后仍能查询到历史记录，应在upload中保存cookies，待施工
        :return: [int, string] 状态码，状态
        """
        print("开始查询历史记录")
        r = requests.get(self.historyUrl, cookies=self.cookies)
        commit_data = r.json()
        code = commit_data['code']
        if code == 'success':
            if len(commit_data['data']) == 0:
                return [-1, "没有已上传的文件"]
            else:
                return [0, commit_data['data']]
        elif code == 'error':
            return [-2, "没有已上传的文件"]

    def clear(self):
        """
        清空历史记录
        重要！！！：若要关闭程序后仍能清除历史记录，应在upload中保存cookies，待施工
        :return: [str, str] 状态码，状态
        """
        print("开始清除历史记录")
        r = requests.get(self.clearUrl, cookies = self.cookies)
        commit_data = r.json()
        code = commit_data['code']
        msg = commit_data['msg']
        return [code, msg]

    def outputInfo(self):
        print("内置输出")
        for url in self.urlList:
            print("url:" + url.url + "\n" + "delete:" + url.delete + "\n\n")

# if __name__ == "__main__":
#     tool = SMMSTool()
#
#     clearCode, clearMsg = tool.clear()
#     print(clearMsg)
#
#     filepath1 = r"D:\test.jpg"
#     uploadStatus, uploadMsg = tool.upload(filepath1)
#     print(uploadMsg)
#
#     for url in tool.urlList:
#         print("url:" + url.url + "\n" + "delete:" + url.delete + "\n\n")
#
#     filepath2 = r"D:\test2.jpg"
#     uploadStatus, uploadMsg = tool.upload(filepath2)
#     print(uploadMsg)
#
#     for url in tool.urlList:
#         print("url:" + url.url + "\n" + "delete:" + url.delete + "\n\n")
#
#     historyStatus, historyData = tool.history()
#     if historyStatus == -1 or -2:
#         print(historyData)
#     elif historyStatus == 0:
#         for data in historyData:
#             print("url:" + data['url'] + "\n" + "delete:" + data['delete'] + "\n\n")


from civicboom.tests import *
from pylons import config

#import json

#import logging
#log      = logging.getLogger(__name__)

class TestAssignmentLimitController(TestController):

    #---------------------------------------------------------------------------
    # Assignment Limit
    #---------------------------------------------------------------------------
    def test_publish_assignment_limit(self):
        """
        Create new user
        Create 5 assignments
        Forwarded to correct page when activated 6th
        """

        # Create Fresh User ----------------------------------------------------
        self.sign_up_as('assign_limit')

        # Set the number of assignments a normal user is limited too
        created_assignments = []
        for i in range(config['payment.free.assignment_limit']-1):
            created_assignments.append(self.create_assignment('Assignment Limit %d' % i))

        # Try to set one over the limit
        response = self.create_assignment('Dont allow this assignment', status=402)

        # Upgrade account
        self.set_account_type('plus')

        # Set a new assignment
        response = self.create_assignment('Assignment Limit > Paid')

        # Double check that all the assimgments appear in this users content list
        response = self.app.get(url('member_action', id='assign_limit', action='content', list='assignments',format='json'))
        response_json = json.loads(response.body)
        title_check = [] + [str(i) for i in range(config['payment.free.assignment_limit']-1)] + ['Paid']
        for assignment in response_json['data']['list']['items']:
            for title in title_check:
                if title in assignment['title']:
                    title_check.remove(title)
        self.assertEqual(len(title_check), 0)

    #---------------------------------------------------------------------------
    # Supporting Methods - Create/Delete Assignments
    #---------------------------------------------------------------------------

    def create_assignment(self, title, status=201):
        response = self.app.post(
            url('contents', format='json'),
            params={
                '_authentication_token': self.auth_token,
                'title'        : title,
                'contents'     : u'Testing assignment limit' ,
                'type'         : u'assignment' ,
                #'submit_publish': u'publish' ,
            },
            status=status
        )
        if status==201:
            response_json = json.loads(response.body)
            id = int(response_json['data']['id'])
            self.assertNotEqual(id, 0)
            ##print('created assignment %d' % id)
            return id
        return response


# trackbar ile renk ayarı

# trackbar : kaydırma çubuğu
# örneğin ses açma kapama yaparken kaydırdığın çubuk

import cv2
import numpy as np

# 0-255 arasında trackbarı ı oynattığımızda o trackbar ın hangi değerde olduğunu döndüren ifade
def nothing(x):
               #  print(x)
               pass

# uint 8 : pozitif integer sayılar
img = np.zeros((512,512,3), np.uint8)

# boş çerceve
cv2.namedWindow('resim')

# 0 ile 255 arasında trackbar ı oynatıcaz
# 2. parametre nereye eklenicek ?
# nothing şuan bir şey yapmıyor ama eklemen gerek
cv2.createTrackbar('R', "resim", 0,255, nothing)
cv2.createTrackbar('G', "resim", 0,255, nothing)
cv2.createTrackbar('B', "resim", 0,255, nothing)

# bir tane switch ekleyelim
cv2.createTrackbar('ON/OFF', 'resim', 0, 1, nothing)

# ekranda pencere olduğu sürece çalışan yer burası
while(1):
               cv2.imshow("resim", img)
               # 27 esc dir
               if cv2.waitKey(1) & 0xFF == 27:
                              break

               # rgb kodlaırnı çekiyoruz
               r = cv2.getTrackbarPos('R', 'resim')
               g = cv2.getTrackbarPos('G', 'resim')
               b = cv2.getTrackbarPos('B', 'resim')

               switch = cv2.getTrackbarPos('ON/OFF', 'resim')

               # img in tüm pixellerini rgb ye eşitliyoruz
               # sanırım 2 değerli olunca trackbar True False döndürüyor ?
               if switch:
                              img[:] = [r,g,b]

               else:
                              img[:] = 0

# bunu yazmadan bi bak farkı gör
cv2.destroyAllWindows()

2_OpenCV ile Görüntü İşleme/0_/5_trackbar.py


from PySide2 import QtWidgets, QtCore
from typing import Set, List
from forms.ui_cpu_edit_widget import Ui_CPUEditWidget
from flowlayout import FlowLayout
from common_widget import SelectButton, clean_layout

class CPUEditWidget(QtWidgets.QWidget):
    cpus_changed = QtCore.Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self._ui = Ui_CPUEditWidget()
        self._ui.setupUi(self)
        self._ui.frame_ops.hide()
        self._layout = FlowLayout(self._ui.frame_cpus)

        self._editable = True
        self._items: List[SelectButton] = list()
        self._cpu_count = 0

    def set_cpu_count(self, count):
        self._cpu_count = count
        clean_layout(self._layout)
        self._items.clear()

        for i in range(count):
            item = SelectButton(str(i), self._ui.frame_cpus)
            self._layout.addWidget(item)
            item.clicked.connect(self._on_item_changed)
            self._items.append(item)

    def set_editable(self, editable: bool):
        for item in self._items:
            item.setEnabled(editable)

    def set_cpus(self, cpus: Set[int]):
        for idx, item in enumerate(self._items):
            item.setChecked(idx in cpus)

    def get_cpus(self) -> Set[int]:
        cpus = set()
        for idx, item in enumerate(self._items):
            if item.isChecked():
                cpus.add(idx)
        return cpus

    def _on_item_changed(self):
        cpus = list()
        for idx, item in enumerate(self._items):
            if item.isChecked():
                cpus.append(idx)

        self.cpus_changed.emit()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = CPUEditWidget()
    w.set_cpu_count(4)
    w.show()
    app.exec_()
    pass


import tensorflow as tf
from helpers.conversion import AZ_MOVE_COUNT
from helpers.io_helpers import ensure_dir_exists
import numpy as np

def create_ccgan():
    NOISE_DIM = (1,)
    IN_NODES = (8, 8, 12)
    GENERATED_NODES = AZ_MOVE_COUNT
    discriminator_optimizer = tf.keras.optimizers.Adam(0.0002, 0.5)
    generator_optimizer = tf.keras.optimizers.Adam(0.0002, 0.5)

    # -- GENERATOR
    # -----------------------------------------------------------------------------------------------------
    generator_board_input = tf.keras.layers.Input(shape=IN_NODES, name="generator_board_input")
    generator_noise_input = tf.keras.layers.Input(shape=NOISE_DIM, name="generator_noise")
    generator_convolution = tf.keras.layers.Conv2D(2, 3, activation='relu')(generator_board_input)
    generator_flatten = tf.keras.layers.Flatten()(generator_convolution)
    generator_concat = tf.keras.layers.concatenate([generator_flatten, generator_noise_input])
    generator_dense = tf.keras.layers.Dense(256, activation='relu')(generator_concat)
    generator_output = tf.keras.layers.Dense(GENERATED_NODES, activation='softmax', name="generator_output")(generator_dense)

    generator = tf.keras.Model([generator_board_input, generator_noise_input], generator_output, name="generator")

    # -- DISCRIMINATOR
    # -------------------------------------------------------------------------------------------------
    discriminator_board_input = tf.keras.layers.Input(shape=(8, 8, 12), name="discriminator_board_input")
    discriminator_move_input = tf.keras.layers.Input(shape=(GENERATED_NODES,), name="discriminator_move_input")

    discriminator_board_convolve = tf.keras.layers.Conv2D(2, 3, activation=tf.nn.leaky_relu)(discriminator_board_input)
    discriminator_board_flatten = tf.keras.layers.Flatten()(discriminator_board_convolve)
    discriminator_concat = tf.keras.layers.concatenate([discriminator_move_input, discriminator_board_flatten])

    discriminator_dense = tf.keras.layers.Dense(256, activation=tf.nn.leaky_relu)(discriminator_concat)

    discriminator_output = tf.keras.layers.Dense(1, activation='sigmoid', name="discriminator_output")(discriminator_dense)

    discriminator = tf.keras.Model([discriminator_board_input, discriminator_move_input], discriminator_output,
                                   name="discriminator")
    discriminator.compile(optimizer=discriminator_optimizer, loss='binary_crossentropy', metrics=['accuracy'])
    discriminator.trainable = False

    # -- GAN
    # -----------------------------------------------------------------------------------------------------------
    gan_input_boards = tf.keras.layers.Input(shape=(8, 8, 12), name="gan_board_input")
    gan_input_noise = tf.keras.layers.Input(shape=NOISE_DIM, name="gan_noise_input")

    generated = generator([gan_input_boards, gan_input_noise])  # rename
    valid = discriminator([gan_input_boards, generated])  # rename

    gan = tf.keras.Model([gan_input_boards, gan_input_noise], valid)
    gan.compile(optimizer=generator_optimizer, loss='binary_crossentropy')

    return gan, generator, discriminator

def train_ccgan(dataset, generator, discriminator, gan, epochs, steps_per_epoch, nth_epoch=0, save_models_fn=None):
    # initialise
    next_pair_op = dataset.make_one_shot_iterator().get_next()
    history = []

    with tf.Session() as sess:
        for epoch in range(epochs):
            print("Epoch %d" % (epoch + 1))
            for step in range(steps_per_epoch):

                boards, moves = sess.run(next_pair_op)  # Fetch data, otherwise each training call increments the iterator

                generator_boards, real_boards = boards[::2], boards[1::2]
                _, real_moves = moves[::2], moves[1::2]

                # Need both to support odd-length batches
                half_shape_1st = tf.shape(moves[::2, 0])
                half_shape_2nd = tf.shape(real_moves[..., 0])
                full_shape = tf.shape(moves[..., 0])

                # GENERATE, uses half batch-size noise
                noise = tf.random.normal(half_shape_1st)

                fake = generator.predict([generator_boards, noise], steps=1)

                # TRAIN DISCRIMINATOR
                zeros = tf.zeros(half_shape_1st)
                ones = tf.ones(tf.shape(half_shape_2nd))

                discriminator_loss_fake = discriminator.train_on_batch([generator_boards, fake], zeros)  # half batch-size false

                discriminator_loss_real = discriminator.train_on_batch([real_boards, real_moves], ones)  # half batch-size true

                # average loss from generated and real moves
                discriminator_loss = 0.5 * np.add(discriminator_loss_fake, discriminator_loss_real)

                # TRAIN GENERATOR
                generator_loss = gan.train_on_batch([boards, tf.random.normal(full_shape)], tf.ones(full_shape))  # full batch-size noise, batch-size true

                # Include generator output in history every nth epoch
                history.append((discriminator_loss[0], discriminator_loss[1], generator_loss, fake if nth_epoch != 0 and (epoch % nth_epoch == 0 or epoch == epochs - 1) else None))

            current_epoch_history = history[epoch * steps_per_epoch:]
            d_loss_avg = sum(map(lambda x: x[0], current_epoch_history)) / steps_per_epoch
            d_acc_avg = sum(map(lambda x: x[1], current_epoch_history)) / steps_per_epoch
            g_loss_avg = sum(map(lambda x: x[2], current_epoch_history)) / steps_per_epoch
            print("\t[mean D loss: %f, mean acc.: %.2f%%] [mean G loss:% f]" % (d_loss_avg, 100 * d_acc_avg, g_loss_avg))

            # Make a checkpoint every nth epoch
            if save_models_fn is not None and ((epoch + 1) % nth_epoch == 0 or epoch == epochs - 1 or epoch == 0):
                save_models_fn(generator, discriminator, gan, epoch + 1)

    print("End of final epoch")
    return history

def get_saver(out_dir):  # Create a closure over out_dir so that train_ccgan need not handle the
                         # output path
    out_dir = out_dir if out_dir[-1] is '/' else out_dir + '/'  # Sanitise output directory
    ensure_dir_exists(out_dir)
    out_path_like = out_dir + "__%s/"

    def save_models(generator, discriminator, gan, epoch):
        nonlocal out_path_like  # Capture closed variable
        epoch_path = out_path_like % epoch
        ensure_dir_exists(epoch_path)
        epoch_path += "%s.h5"
        tf.keras.models.save_model(generator, epoch_path % "GENERATOR")
        tf.keras.models.save_model(discriminator, epoch_path % "DISCRIMINATOR")
        tf.keras.models.save_model(gan, epoch_path % "GAN")

    return save_models

def load_models(in_dir):  # Loads models saved by a function return from get_saver
    file_like = in_dir + "%s.h5"
    generator = tf.keras.models.load_model(file_like % "GENERATOR")
    discriminator = tf.keras.models.load_model(file_like % "DISCRIMINATOR")
    gan = tf.keras.models.load_model(file_like % "GAN")
    return generator, discriminator, gan


from flask import Flask, render_template, request, redirect, url_for, jsonify
from datetime import datetime
from pymongo import MongoClient
from werkzeug.utils import secure_filename

app = Flask(__name__)

# DB Setting
from pymongo import MongoClient
my_client = MongoClient('localhost', 27017)
my_db = my_client.smkim # sign_in_up db 만들기
my_collection = my_db.image_upload

# Page
@app.route('/')
def home():
    return render_template('index.html')

# 파일 업로드 방식
# 1. File 창 열어서 진행 Ver
# 2. 모달 창 Ver
@app.route('/fileupload', methods=['POST'])
def file_upload():
    f_name = request.form['f_name']
    file = request.files['file'] # werkzeug.datastructures.FileStorage
    extension = secure_filename(file.filename).split('.')[-1]
    f_name = f_name.replace('.' + extension, '')

    # 파일 이름 , Local 이미지 저장
    today = datetime.now()
    filename = f_name + '-' + today.strftime('%Y-%m-%d-%H-%M-%S')
    save_path = 'static/' + filename + '.' + extension
    file.save(save_path)

    # DB에 Image Path 저장
    doc = {'f_name':f_name, 'img_path': filename + '.' + extension}
    my_collection.insert_one(doc)
    return jsonify({'result':'success'})

if __name__ == '__main__':
    app.run('0.0.0.0', port=5000, debug=True)


"""
Component to show with breaking_changes.

For more details about this component, please refer to
https://github.com/custom-components/breaking_changes
"""
import os
from datetime import timedelta
import logging
import requests
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers import discovery
from homeassistant.util import Throttle
from .const import (
    DOMAIN_DATA,
    DOMAIN,
    ISSUE_URL,
    PLATFORMS,
    REQUIRED_FILES,
    STARTUP,
    URL,
    VERSION,
    CONF_NAME,
    DEFAULT_NAME,
)

REQUIREMENTS = ["pyhaversion"]

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema(
    {DOMAIN: vol.Schema({vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string})},
    extra=vol.ALLOW_EXTRA,
)

async def async_setup(hass, config):
    """Set up this component."""

    # Print startup message
    startup = STARTUP.format(name=DOMAIN, version=VERSION, issueurl=ISSUE_URL)
    _LOGGER.info(startup)

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}
    hass.data[DOMAIN_DATA]["components"] = ["homeassistant"]
    hass.data[DOMAIN_DATA]["potential"] = {}

    # Load platforms
    for platform in PLATFORMS:
        # Get platform specific configuration
        platform_config = config[DOMAIN]

        hass.async_create_task(
            discovery.async_load_platform(
                hass, platform, DOMAIN, platform_config, config
            )
        )

    async def loaded_platforms(hass):
        """Load platforms after HA startup."""
        for component in hass.config.components:
            hass.data[DOMAIN_DATA]["components"].append(component)

        _LOGGER.debug("Loaded components %s", hass.data[DOMAIN_DATA]["components"])
        await update_data(
            hass, no_throttle=True
        )  # pylint: disable=unexpected-keyword-arg

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, loaded_platforms(hass))

    return True

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def update_data(hass):
    """Update data."""
    if len(hass.data[DOMAIN_DATA]["components"]) == 1:
        return
    from pyhaversion import Version

    session = async_get_clientsession(hass)
    haversion = Version(hass.loop, session)

    await haversion.get_local_version()
    currentversion = haversion.version.split(".")[1]

    await haversion.get_pypi_version()
    remoteversion = haversion.version.split(".")[1]

    if currentversion == remoteversion:
        _LOGGER.debug(
            "Current version is %s and remote version is %s skipping update",
            currentversion,
            remoteversion,
        )
        return

    versions = []

    for platform in hass.data[DOMAIN_DATA]["components"]:
        if "homeassistant.components." in platform:
            name = platform.split("homeassistant.components.")[1]
            if "." in name:
                name = name.split(".")[0]
            if name not in hass.data[DOMAIN_DATA]["components"]:
                hass.data[DOMAIN_DATA]["components"].append(name)
    _LOGGER.debug("Loaded components - %s", hass.data[DOMAIN_DATA]["components"])

    try:
        _LOGGER.debug("Running update")

        for version in range(int(currentversion) + 1, int(remoteversion) + 1):
            versions.append(version)
            request = requests.get(URL.format(version))
            jsondata = request.json()
            _LOGGER.debug(jsondata)
            for platform in jsondata:
                _LOGGER.debug(platform["component"])
                if platform["component"] is None or platform["component"] is "None":
                    platform["component"] = "homeassistant"
                if platform["component"] in hass.data[DOMAIN_DATA]["components"]:
                    data = {
                        "component": platform["component"],
                        "prlink": platform["prlink"],
                        "doclink": platform["doclink"],
                        "description": platform["description"],
                    }
                    hass.data[DOMAIN_DATA]["potential"][platform["pull_request"]] = data
        hass.data[DOMAIN_DATA]["potential"]["versions"] = versions
    except Exception as error:  # pylint: disable=broad-except
        _LOGGER.error("Could not update data - %s", error)

async def check_files(hass):
    """Return bool that indicates if all files are present."""
    # Verify that the user downloaded all files.
    base = "{}/custom_components/{}/".format(hass.config.path(), DOMAIN)
    missing = []
    for file in REQUIRED_FILES:
        fullpath = "{}{}".format(base, file)
        if not os.path.exists(fullpath):
            missing.append(file)

    if missing:
        _LOGGER.critical("The following files are missing: %s", str(missing))
        returnvalue = False
    else:
        returnvalue = True

    return returnvalue


import requests
import json
from userge import userge, Message

@userge.on_cmd("memes", about={
    'header': "Get memes ",
    'usage': "!memes"})
async def meme(message:Message):
	      memeget = requests.get('https://meme-api.herokuapp.com/gimme')
	      memeSent = memeget.json()['url']
	      memeMsg = '' + memeSent
	      await message.edit(memeMsg)


import socket

ADDRESS = '127.0.0.1'
PORT = 1234

def client():
    print("Starting Client...................")

    ## Server Connection #####################################
    my_socket = socket.socket()
    my_socket.connect((ADDRESS,PORT))

    ## Sending Data ######################################
    my_socket.sendall(b'Hello from Client\n')

    ## Receiving Data ######################################
    print("Received from server:", my_socket.recv(1024)) # Max bytes

    ## Closing when done ####################################
    my_socket.close()

def server():
    print("Starting Server...................")

    ## Server Connection #####################################
    my_socket = socket.socket()
    my_socket.bind((ADDRESS,PORT))
    my_socket.listen() #Listen for incomping connections
    new_socket,addr = my_socket.accept()

    ## Receiving Data #######################################
    received_data = new_socket.recv(1024) # Max bytes
    print("Received from client:", received_data)
    print("Converting to string:", received_data.decode())

    ## Sending Data #########################################
    new_socket.sendall(b'Hello from Server\n')

    ## Closing when done ####################################
    new_socket.close()
    my_socket.close()

if input("Type something to become the client. Else, just enter to become server: ") == "":
    server()
else:
    client()

input() # Keep text on the screen

School Exercises/3. H2 Computing Notes/Socket/basic.py


'''
여러 이미지를 합쳐서 하나의 이미지로 만들어주는 프로그램 제작 프로젝트

[사용자 시나리오]
1. 사용자는 합치려는 이미지를 1개 이상 선택한다.
2. 합쳐진 이미지가 저장될 경로를 지정한다.
3. 가로넓이, 간격, 포맷 옵션을 지정한다.
4. 시작 버튼을 통해 이미지를 합친다.
5. 닫기 버튼을 통해 프로그램을 종료한다.

[기능 명세]
1. 파일추가 : 리스트 박스에 파일 추가
2. 선택삭제 : 리스트 박스에서 선택된 항목 삭제
3. 찾아보기 : 저장할 폴더를 선택하면 텍스트 위젯에 입력
4. 가로넓이 : 이미지 넓이 지정(원본유지, 1024, 800, 640)
5. 간격 : 이미지 간의 간격 지정(없음, 좁게, 보통, 넓게)
6. 포맷 : 저장 이미지 포맷 지정(PNG, JPG, BMP)
7. 시작 : 이미지 합치기 작업 실행
8. 진행상황 : 현재 진행중인 파일 순서에 맞게 반영
9. 닫기 : 프로그램 종료
'''
import os
from tkinter import *
import tkinter.ttk as ttk
import tkinter.messagebox as msgbox
from tkinter import filedialog

from PIL import Image

root = Tk()
root.title("Combine Image Program")  # 조건 1 - OK

# 파일 추가 함수
def add_file():
    files = filedialog.askopenfilenames(title="이미지 파일을 선택하세요", filetypes=(("PNG 파일", "*.png"), ("모든파일", "*.*")),
                                        initialdir=r"C:\Users\user\Desktop\PythonProject\tkinter_project\image_project")

    # 사용자가 선택한 파일 목록 출력
    for file in files:
        list_file.insert(END, file)

# 선택 삭제
def del_file():
    for index in reversed(list_file.curselection()):
        list_file.delete(index)

# 저장 경로(폴더)
def browse_dest_path():
    folder_selected = filedialog.askdirectory()
    if folder_selected == "":
        return
    txt_dest_path.delete(0, END)
    txt_dest_path.insert(0, folder_selected)

# 이미지 통합
def merge_image():

    try:
        # 가로 넓이
        img_width = cmb_width.get()
        if img_width == "원본유지":
            img_width = -1  # -1 일때는 원본 기준으로
        else:
            img_width = int(img_width)

        # 간격
        img_space = cmb_space.get()
        if img_space == "좁게":
            img_space = 30
        elif img_space == "보통":
            img_space = 60
        elif img_space == "넓게":
            img_space = 90
        else:
            img_space = 0

        # 포멧
        img_format = cmb_format.get().lower()

        ##############################################################

        images = [Image.open(x) for x in list_file.get(0, END)]

        # 이미지 사이즈 리스트에 넣어 하나씩 처리
        image_sizes = []  # [(width1, height2), (width1, height2) ...]
        if img_width > -1:
            image_sizes = [(int(img_width), int(img_width * x.size[1] / x.size[0])) for x in images]

        else:
            image_sizes = [(x.size[0], x.size[1]) for x in images]

        # 이미지들의 가로, 세로 크기를 각각 widths, heights에 저장
        # size -> size[0] : width, size[1] : height
        widths, heights = zip(*(image_sizes))

        max_width, total_height = max(widths), sum(heights)

        # 크기에 맞는 흰색 배경 준비
        # 이미지 간격 옵션 적용
        if img_space > 0 :
            total_height += (img_space * (len(images) -1))
        result_img = Image.new("RGB", (max_width, total_height), (255, 255, 255))
        y_offset = 0  # y 위치

        for idx, img in enumerate(images):
            # width가 원본 유지가 아닐 때 이미지 크기 조정이 필요
            if img_width > -1:
                img = img.resize(image_sizes[idx])

            result_img.paste(img, (0, y_offset))
            y_offset += (img.size[1] + img_space)  # img의 height + 사용자가 지정한 간격 값 만큼 더해줌

            progress = (idx + 1) / len(images) * 100  # 실제 퍼센트 정보를 계산
            p_var.set(progress)
            progress_bar.update()

        # 포맷 옵션 처리
        file_name = "result." + img_format
        dest_path = os.path.join(txt_dest_path.get(), file_name)
        result_img.save(dest_path)
        msgbox.showinfo("알림", "작업이 완료되었습니다.")
    except Exception as err: # 예외처리
        msgbox.showerror("에러", err)

# 시작
def start():
    # 각 옵션들 값을 확인
    # print("가로넓이 :", cmb_width.get())
    # print("간격 :", cmb_space.get())
    # print("포맷 :", cmb_format.get())

    # 파일 목록 확인
    if list_file.size() == 0:
        msgbox.showwarning("경고", "이미지 파일을 추가하세요")
    # 저장 경로 확인
    if len(txt_dest_path.get()) == 0:
        msgbox.showwarning("경고", "저장 경로를 선택하세요")

    # 이미지 통합 작업
    merge_image()

# 파일 프레임(파일 추가, 선택 삭제 영역)
file_frame = Frame(root)
file_frame.pack(fill="x", padx=5)

btn_add_file = Button(file_frame, padx=5, pady=5, width=12, text="파일추가", command=add_file)
btn_add_file.pack(side="left", padx=5, pady=5)

btn_del_file = Button(file_frame, padx=5, pady=5, width=12, text="선택삭제", command=del_file)
btn_del_file.pack(side="right", padx=5)

# 리스트 프레임
list_frame = Frame(root)
list_frame.pack(fill="both", padx=5, pady=5)

scrollbar = Scrollbar(list_frame)
scrollbar.pack(side="right", fill="y")

list_file = Listbox(list_frame, selectmode="extended", height=15, yscrollcommand=scrollbar.set)
list_file.pack(side="left", fill="both", expand=True)
scrollbar.config(command=list_file.yview)

# 저장 경로 프레임
path_frame = LabelFrame(root, text="저장경로")
path_frame.pack(fill="x", padx=5, pady=5, ipady=5)

txt_dest_path = Entry(path_frame)
txt_dest_path.pack(side="left", fill="x", expand=True, padx=5, pady=5, ipady=3)

btn_dest_path = Button(path_frame, text="찾아보기", width=10, command=browse_dest_path)
btn_dest_path.pack(side="right", padx=5, pady=5)

# 옵션 프레임
frame_option = LabelFrame(root, text="옵션")
frame_option.pack(padx=5, pady=5, ipady=5)

# 1. 가로 넓이 옵션
# 가로 넓이 레이블
lbl_width = Label(frame_option, text="가로넓이", width=8)
lbl_width.pack(side="left", padx=5, pady=5)
# 가로 넓이 콤보
opt_width = ["원본유지", "1024", "800", "640"]
cmb_width = ttk.Combobox(frame_option, state="readonly", values=opt_width, width=10)
cmb_width.current(0)
cmb_width.pack(side="left", padx=5, pady=5)

# 2. 간격 옵션
# 간격 옵션 레이블
lbl_space = Label(frame_option, text="간격", width=8)
lbl_space.pack(side="left", padx=5, pady=5)
# 간격 옵션 콤보
opt_space = ["없음", "좁게", "보통", "넓게"]
cmb_space = ttk.Combobox(frame_option, state="readonly", values=opt_space, width=10)
cmb_space.current(0)
cmb_space.pack(side="left", padx=5, pady=5)

# 3. 파일 포맷 옵션
# 파일 포맷 레이블
lbl_format = Label(frame_option, text="포맷", width=8)
lbl_format.pack(side="left", padx=5, pady=5)
# 파일 포맷 콤보
opt_format = ["PNG", "JPG", "BMP"]
cmb_format = ttk.Combobox(frame_option, state="readonly", values=opt_format, width=10)
cmb_format.current(0)
cmb_format.pack(side="left", padx=5, pady=5)

# 진행 상황 Progress Bar
frame_progress = LabelFrame(root, text="진행상황")
frame_progress.pack(fill="x", padx=5, pady=5, ipady=5)

p_var = DoubleVar()
progress_bar = ttk.Progressbar(frame_progress, maximum=100, variable=p_var)
progress_bar.pack(fill="x", padx=5, pady=5)

# 실행 프레임
frame_run = Frame(root)
frame_run.pack(fill="x", padx=5, pady=5)

btn_close = Button(frame_run, padx=5, pady=5, text="닫기", width=12, command=root.quit)
btn_close.pack(side="right", padx=5, pady=5)

btn_start = Button(frame_run, padx=5, pady=5, text="시작", width=12, command=start)
btn_start.pack(side="right", padx=5, pady=5)

root.resizable(False, False)
root.mainloop()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 18 11:37:50 2018

@author: cassandra
"""
import glob
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

#
# Read out some aviso map of sla
#
directory = '/data/aviso_data/monthly_mean/'
files = glob.glob(directory+'*.hdf')
file = files[0]

df_sla = pd.read_hdf(file)

#
# Get out the time indexed map of sea surface height
#
#ssh_file = '/home/cassandra/Data/aviso_data/dataset-duacs-rep-global-merged-allsat-phy-l4_1532022114265.nc.hdf'
#df_ssh = pd.read_hdf(ssh_file)

#
# Get the sea level anomaly (in meters)
#

# What is this nv number? its either 0 or 1...
sla = df_sla.query('nv == 0')['sla'].unstack(1).values

#
# Retrieve lons and lats from the index
#
lons_sla = np.unique(df_sla.index.get_level_values('lon'))
lats_sla = np.unique(df_sla.index.get_level_values('lat'))

#
# Get the sea surface height (in meters)
#
#ssh = df_ssh.unstack(1).values

#
# Adjust the longitudes from [0, 360] to [-180, 180]
#
#west_lons = np.where(lons > 180.0)[0]
#for i in west_lons:
#    lons[i] = lons[i] - 360.0

#
# Generate a basemap
#
f = plt.figure(1)
map = Basemap()
#map.fillcontinents(color='#ddaa66',lake_color='aqua')
map.drawcoastlines()

#
# Adopt coordinates to map coordinates after forming a grid
#
#x, y = np.meshgrid(lons, lats)
#x, y = map(x,y)

#
# Plot the data!
#
map.pcolor(lons_sla, lats_sla, sla*100, cmap='coolwarm', latlon=True)

cbar = plt.colorbar(orientation='horizontal')
cbar.set_label('Sea Level Anomaly (cm)')

plt.title(file.split('/')[-1])
plt.tight_layout

plt.show()


from flask_app.config.mysqlconnection import connectToMySQL
from flask_app import DATABASE, bcrypt
from flask_app import EMAIL_REGEX

from flask import flash, session

class Users_profile:

    def __init__(self,data):
        self.address_1 = data["address_1"]

        self.city = data["city"]
        self.state = data["state"] #2 characters

        self.zip_code = data["zip_code_"]
        self.country = data["country"]

        self.phone = data["phone"] ### - ### - ####

        self.birth_date = data["birth_date"]  #month / day / year

        self.created_at = data["created_at"]
        self.updated_at = data["updated_at"]

#============================================


import warnings
from abc import ABC
from contextlib import contextmanager
from inspect import signature

import imageio
import numpy as np
import tensorflow as tf
from deprecated import deprecated
from PIL import Image, ImageDraw, ImageFont

from ..utils import listify

class Callback(ABC):
    """Abstract class for defining callbacks.
    """
    def on_begin(self, **kwargs) -> None:
        """Called at the begin of optimization process.

        Args:
            kwargs: The parameters that was passed to
                :obj:`tf_keras_vis.activation_maximization.ActivationMaximization.__call__()`.
        """
        pass

    def __call__(self, i, values, grads, scores, model_outputs, **kwargs) -> None:
        """This function will be called after updating input values by gradient descent in
        :obj:`tf_keras_vis.activation_maximization.ActivationMaximization.__call__()`.

        Args:
            i: The current number of optimizer iteration.
            values: A list of tf.Tensor that indicates current `values`.
            grads: A list of tf.Tensor that indicates the gradients with respect to model input.
            scores: A list of tf.Tensor that indicates score values with respect to each the model
                outputs.
            model_outputs: A list of tf.Tensor that indicates the model outputs.
            regularizations:  A list of tuples of (str, tf.Tensor) that indicates the regularizer
                values.
            overall_score: A list of tf.Tensor that indicates the overall scores that includes the
                scores and regularization values.
        """
        pass

    def on_end(self) -> None:
        """Called at the end of optimization process.
        """
        pass

@deprecated(version='0.7.0', reason="Use `Progress` instead.")
class PrintLogger(Callback):
    """Callback to print values during optimization.

    Warnings:
        This class is now **deprecated**!
        Please use :obj:`tf_keras_vis.activation_maximization.callbacks.Progress` instead.
    """
    def __init__(self, interval=10):
        """
        Args:
            interval: An integer that indicates the interval of printing.
                Defaults to 10.
        """
        self.interval = interval

    def __call__(self, i, values, grads, scores, model_outputs, regularizations, **kwargs):
        i += 1
        if (i % self.interval == 0):
            tf.print('Steps: {:03d}\tScores: {},\tRegularization: {}'.format(
                i, self._tolist(scores), self._tolist(regularizations)))

    def _tolist(self, ary):
        if isinstance(ary, list) or isinstance(ary, (np.ndarray, np.generic)):
            return [self._tolist(e) for e in ary]
        elif isinstance(ary, tuple):
            return tuple(self._tolist(e) for e in ary)
        elif tf.is_tensor(ary):
            return ary.numpy().tolist()
        else:
            return ary

class GifGenerator2D(Callback):
    """Callback to construct a gif of optimized image.
    """
    def __init__(self, path) -> None:
        """
        Args:
            path: The file path to save gif.
        """
        self.path = path

    def on_begin(self, **kwargs) -> None:
        self.data = None

    def __call__(self, i, values, *args, **kwargs) -> None:
        if self.data is None:
            self.data = [[] for _ in range(len(values))]
        for n, value in enumerate(values):
            value = value[0].numpy() if tf.is_tensor(value[0]) else value[0]
            img = Image.fromarray(value.astype(np.uint8))  # 1st image in the batch
            ImageDraw.Draw(img).text((10, 10), f"Step {i + 1}", font=ImageFont.load_default())
            self.data[n].append(np.asarray(img))

    def on_end(self) -> None:
        path = self.path if self.path.endswith(".gif") else f"{self.path}.gif"
        for i in range(len(self.data)):
            with imageio.get_writer(path, mode='I', loop=0) as writer:
                for data in self.data[i]:
                    writer.append_data(data)

class Progress(Callback):
    """Callback to print values during optimization.
    """
    def on_begin(self, steps=None, **kwargs) -> None:
        self.progbar = tf.keras.utils.Progbar(steps)

    def __call__(self, i, values, grads, scores, model_outputs, regularizations, **kwargs) -> None:
        if len(scores) > 1:
            scores = [(f"Score[{j}]", score_value) for j, score_value in enumerate(scores)]
        else:
            scores = [("Score", score_value) for score_value in scores]
        scores += regularizations
        self.progbar.update(i + 1, scores + regularizations)

@contextmanager
def managed_callbacks(callbacks=None, **kwargs):
    activated_callbacks = []
    try:
        for c in listify(callbacks):
            if len(signature(c.on_begin).parameters) == 0:
                warnings.warn("`Callback#on_begin()` now must accept keyword arguments.",
                              DeprecationWarning)
                c.on_begin()
            else:
                c.on_begin(**kwargs)
            activated_callbacks.append(c)
        yield activated_callbacks
        for _ in range(len(activated_callbacks)):
            activated_callbacks.pop(0).on_end()
    finally:
        for c in activated_callbacks:
            try:
                c.on_end()
            except Exception as e:
                tf.print("Exception args: ", e)
                pass


"""
Author: Akash Malhotra
Github repo: https://github.com/akashjorss/Decision_Modeling_Assignments
"""

import pandas as pd
import numpy as np
import scipy.stats as ss
import networkx as nx
import matplotlib.pyplot as plt

def majority_rule(value_1, value_2, df):
    values = np.zeros(2)
    for i in range(len(df)):
        row = df.loc[i, :].values
        num_votes = row[0]
        i_1 = np.where(row == value_1)
        i_2 = np.where(row == value_2)
        if i_1 < i_2:
            #value_1 is prefered to value_2
            values[0]+= num_votes
        else:
            #value_2 is prefered to value_1
            values[1]+= num_votes

    if values[0] > values[1]:
        return value_1
    else:
        return value_2

def plurality(df):

    #look at only the data of preference 1
    pref_1 = df[df.columns[1]]
    num_votes = df[df.columns[0]]
    result = []

    my_dict = dict() #this dictionary holds {candidates: # votes}
    for i in range(len(pref_1)):
        if pref_1[i] in my_dict:
            my_dict[pref_1[i]] += num_votes[i]
        else:
            my_dict[pref_1[i]] = num_votes[i]

    return max(my_dict, key=my_dict.get)

def plurality_runoff(df):

    #look at only the data of preference 1
    pref_1 = df[df.columns[1]]
    num_votes = df[df.columns[0]]
    result = []

    my_dict = dict() #this dictionary holds {candidates: # votes}
    for i in range(len(pref_1)):
        if pref_1[i] in my_dict:
            my_dict[pref_1[i]] += num_votes[i]
        else:
            my_dict[pref_1[i]] = num_votes[i]

    first = max(my_dict, key=my_dict.get)
    #check if its > 50 %
    if (my_dict[first]/np.sum(num_votes)) > 0.5:
        return first
    else:
        my_dict.pop(first)
        second = max(my_dict, key=my_dict.get)
        print("Pluraty run off: After the first round, following candidates remain")
        print("First: ", first)
        print("Second:", second)

        return majority_rule(first, second, df)

def topological_sorting(matrix):
    """This is a helper function to help condorcet voting"""
    rows = matrix.shape[0]
    cols = matrix.shape[1]
    #find the number of in-degrees for each vertex (the sum of elements per column)
    in_degrees = [0] * cols
    for i in range(rows):
        for j in range(cols):
            in_degrees[j]+=matrix[i][j]
    #number of already visited nodes
    visited_nodes=0
    #queue of the visited nodes
    queue = []
    #queue for the output nodes
    output_queue = []
    #add the nodes with in-degree 0 to the queue
    for i in range(cols):
        if in_degrees[i]==0:
            queue.append(i)
    #for each element visited Increment count of visited nodes by 1. Then decrease in-degree by 1 for all its neighboring nodes.
    while queue!=[]:
        element_being_visited=queue.pop(0)
        output_queue.append(element_being_visited)
        visited_nodes+=1
        for j in range(cols):
            if matrix[element_being_visited][j]==1:
                in_degrees[j]-=1
    # If the in-degree of a neighboring nodes is reduced to zero, then add it to the queue
                if in_degrees[j]==0:
                    queue.append(j)
    #If count of visited nodes is not equal to the number of nodes in the graph then the topological sort is not possible
    #for the given graph.
    if visited_nodes!=cols:
        #print("Topological ordering isn't possible for this graph")
        return None
    else:
        #print("The topological sorting for the following relation would be: "))
        return output_queue

#Vizualize the matrix as a directed graph
def show_graph_with_labels(adjacency_matrix):
    """Helper function to visualize graph"""
    rows, cols = np.where(adjacency_matrix == 1)
    edges = zip(rows.tolist(), cols.tolist())
    gr = nx.DiGraph()
    gr.add_edges_from(edges)
    nx.draw(gr, node_size=1000, with_labels=True)
    plt.show()

def condorcet_voting(df):
    """returns the winner by condorcet voting rule
    can be done by topological sort"""

    # find the unique values in df
    values = []
    for col in df.columns[1:]:
        vals = np.unique(df[col].values)
        values = np.append(values, vals)
    distinct_values = np.unique(values)

    preference_matrix = np.zeros([len(distinct_values), len(distinct_values)])
    # create a preference matrix
    for i in range(len(distinct_values)):
        for j in range(i, len(distinct_values)):
            pref = majority_rule(distinct_values[i], distinct_values[j], df)
            if i!=j:
                if pref == distinct_values[i]:
                    preference_matrix[i][j] = 1
                else:
                    preference_matrix[j][i] = 1

    print("Preference Matrix:")
    print(preference_matrix)
    show_graph_with_labels(preference_matrix)
    top_result = topological_sorting(preference_matrix)
    if top_result == None:
        return None
    return distinct_values[top_result[0]]

def borda_voting(df):
    my_dict = dict() #this dictionary holds {candidates: # votes}
    for i in range(len(df)):
        row = df.loc[i, :].values
        num_votes = row[0]
        for j in range(1,len(row)):
            if row[j] in my_dict:
                my_dict[row[j]] += j*num_votes
            else:
                my_dict[row[j]] = j*num_votes

    #return the key which has minimum value
    print("The borda score is ", my_dict)
    return min(my_dict, key=my_dict.get)

#read the test data
df = pd.DataFrame([[5, 'a', 'b', 'c', 'd'],
 [4, 'a', 'c', 'b', 'd'],
 [2, 'd', 'b', 'a', 'c'],
 [6, 'd', 'b', 'c', 'a'],
 [8, 'c', 'b', 'a', 'd'],
 [2, 'd', 'c', 'b', 'a']], columns=['num_votes', 'pref_1', 'pref_2', 'pref_3', 'pref_4'])
print(df)

print("Winner by plurality: ", plurality(df))

_df = pd.DataFrame([[10, 'a', 'b', 'c'], [6, 'b', 'c', 'a'], [5, 'c', 'b', 'a']])
print("\nAnother Example\n", _df)
print("Winner by plurality: ", plurality(_df))

print("Winner by plurality run off: ", plurality_runoff(df))
_df = pd.DataFrame([[10, 'a', 'b', 'c'], [6, 'b', 'c', 'a'], [5, 'c', 'b', 'a']])
print("\nAnother Example\n", _df)
print("Winner by plurality run off: ", plurality_runoff(_df))

result = condorcet_voting(df)
if result != None:
    print("Winner by Condorcet: ", result)
else:
    print("There is no condorcet winner")

_df = pd.DataFrame([[10, 'a', 'b', 'c'], [6, 'b', 'c', 'a'], [5, 'c', 'b', 'a']])
print("\nAnother Example\n", _df)
print("Winner by Condorcet: ", condorcet_voting(_df))

print("\nExample where condorcet shouldn't work:")

_df = pd.DataFrame([[1, 'a', 'b', 'c'],
                   [1, 'b', 'c', 'a'],
                   [1, 'c', 'a', 'b']])
if condorcet_voting(_df) == None:
    print("There is no condorcet winner")

print("Winner by Bording voting rule: ", borda_voting(df))

print("\nAnother example for Borda")

_df = pd.DataFrame([[2, 'b', 'a', 'c', 'd'],
            [1, 'a', 'c', 'd', 'b']])
print(_df)
print("Winner by Bording voting rule: ", borda_voting(_df))


inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'],
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}
#add key
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
#sort list alphabetically
inventory['backpack'].sort()
#remove dagger
inventory['backpack'].remove('dagger')
#add 50 to 'gold'
inventory['gold'] += 50
print(inventory)


"""Semantic transformations.

These might require external dependencies and are only
used if the dependencies are found.

"""
from typing import List, Tuple, Any
import pandas as pd
import numpy as np
from .base import Transformation

from dateparser import parse
from word2number import w2n

def to_number(value: str) -> float:
    """Convert value to a number.

    Args:
        value: A string.

    Returns:
        Numeric value of string. If cannot convert, return `np.nan`.
    """
    try:
        number = w2n.word_to_num(value)
    except:
        number = np.nan
    return number

class WordToNumber(Transformation):
    """Convert words to numbers."""

    allowed = ["object", "category"]

    def __call__(self, column: pd.Series) -> pd.DataFrame:
        return column.map(to_number).to_frame()

    @classmethod
    def arguments(cls, column: pd.Series) -> List[Tuple[()]]:
        for value in column.drop_duplicates():
            v = to_number(value)
            if not np.isnan(v):
                return [()]
        return []

class NormaliseTimedelta(Transformation):
    """Normalise time deltas."""

    allowed = ["object", "category"]
    time_map = {"day": 1, "week": 7, "month": 30, "year": 365}

    def __call__(self, column: pd.Series) -> pd.DataFrame:
        pass

    @classmethod
    def arguments(cls, column: pd.Series) -> List[Tuple[()]]:
        values = np.unique(column)
        times = cls.time_map.keys()
        if any(any(t in value for t in times) for value in values):
            return [()]
        return []

class TimeFeatures(Transformation):
    """Date features."""

    allowed = ["string", "datetime64"]
    directives = [
        "%A",
        "%w",
        "%-d",
        "%B",
        "%-m",
        "%-y",
        "%Y",
        "%-H",
        "%-I",
        "%p",
        "%-M",
        "%-S",
        "%f",
        "%z",
        "%Z",
        "%-j",
        "%U",
        "%W",
    ]

    def __call__(self, column: pd.Series) -> pd.DataFrame:
        # ensure that it's a datetime
        if column.dtype == "string":
            datetimes = column.apply(parse)
        # extract features
        features = list()
        for directive in self.directives:
            new = datetimes.apply(lambda x: x.strftime(directive)).rename(directive[1:])
            if new.str.isnumeric().all():
                new = pd.to_numeric(new)
            features.append(new)
        return pd.concat(features, axis=1)

    @classmethod
    def arguments(cls, column: pd.Series) -> List[Tuple[Any]]:
        head = column.head(100)
        if head.apply(parse).isna().sum() < (len(head) // 2):
            return [()]
        return []


# -*- coding:utf-8 -*-
import urllib
from http.cookiejar import CookieJar,MozillaCookieJar,LWPCookieJar

def get_cookie():
    c = CookieJar()
    c_manage = urllib.request.HTTPCookieProcessor(c)
    opener = urllib.request.build_opener(c_manage)
    response = opener.open('http://www.baidu.com')
    for item in c:
        print('{} =  {}'.format(item.name,item.value))

def save_cookie_on_file(path):
    # 第一种保存cookie到文件的方式
    #cookie = MozillaCookieJar(path)

    # 第二种保存cookie到文件的方式
    cookie = LWPCookieJar(path)
    cookie_manage = urllib.request.HTTPCookieProcessor(cookie)
    opener = urllib.request.build_opener(cookie_manage)
    response = opener.open('https://www.baidu.com')
    cookie.save(ignore_discard=True,ignore_expires=True)

def use_cookies_local(filename):
    cookie = LWPCookieJar()
    cookie.load('cookies.txt',ignore_discard=True,ignore_expires=True)
    cookie_handler = urllib.request.HTTPCookieProcessor(cookie)
    opener = urllib.request.build_opener(cookie_handler)
    response = opener.open('https://www.baidu.com')
    print(response.read().decode('utf-8'))

def main():
    path = r'cookies.txt'
    #save_cookie_on_file(path)
    use_cookies_local(path)

if __name__ == '__main__':
    main()