class Edge:
    def __init__(self, point1, point2, cost):
        self.point1 = point1
        self.point2 = point2
        self.cost = cost

    def __lt__(self, other):
        return self.cost < other.cost

class UnionFind:
    def __init__(self, size):
        self.root = [i for i in range(size + 1)]
        self.rank = [1] * (size + 1)

    def find(self, x):
        if x == self.root[x]:
            return x
        self.root[x] = self.find(self.root[x])
        return self.root[x]

    def union(self, x, y):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX != rootY:
            if self.rank[rootX] > self.rank[rootY]:
                self.root[rootY] = rootX
            elif self.rank[rootX] < self.rank[rootY]:
                self.root[rootX] = rootY
            else:
                self.root[rootY] = rootX
                self.rank[rootX] += 1

    def connected(self, x, y):
        return self.find(x) == self.find(y)

class Solution:
    def minimumCost(self, n: int, connections: List[List[int]]) -> int:
        # To connect n cities with minimum cost, there has to be ATLEAST
        # n - 1 connections.
        if len(connections) < n - 1:
            return -1

        pq = []
        cost, count = 0, 0

        for src, des, weight in connections:
            heapq.heappush(pq, (weight, src, des))

        uf = UnionFind(n)

        while pq and count != n - 1:
            weight, des, src = heapq.heappop(pq)

            if uf.connected(src, des):
                continue

            cost += weight
            uf.union(src, des)
            count += 1

        return cost


import wx

# Create the Application Object
app = wx.App()

# Now create a Frame (representing the window)
frame = wx.Frame(parent=None, title='Simple Hello World')
# And add a text label to it
text = wx.StaticText(parent=frame, label='Hello Python')

# Display the window (frame)
frame.Show()

# Start the event loop
app.MainLoop()


import codecs
import spacy
from collections import defaultdict
from snorkel.models import construct_stable_id
from spacy.tokens import Doc
from snorkel.parser import DocPreprocessor
from snorkel.models import Document, split_stable_id
from snorkel.parser import Parser, ParserConnection, Spacy, Sentence

class LineCorpusParser(Parser):
    """
    Slight modification of SpaCy parser to allow whitespace tokenization and manual
    sentence boundary detection (SBD). Due to the clunky way SpaCy deals with SBD,
    we just implement a new class to deal with things.
    """
    def __init__(self, annotators=['tagger', 'parser', 'entity'],
                 lang='en', num_threads=1, tokenize_on_whitespace=True, verbose=False):

        super(LineCorpusParser, self).__init__(name="line_space_corpus")
        self.model = spacy.load(lang, create_make_doc=WhitespaceTokenizer) if tokenize_on_whitespace else \
            spacy.load(lang)
        self.num_threads = num_threads

        self.pipeline = []
        for proc in annotators:
            self.pipeline += [self.model.__dict__[proc]]

    def _original_string(self, tokens, offsets):
        """
        Recreate string with original char offsets
        :param tokens:
        :param offsets:
        :return:
        """
        s = ""
        for t, i in zip(tokens, offsets):
            diff = i - len(s)
            if diff:
                s += ' ' * diff
            s += t
        return s

    def connect(self):
        return ParserConnection(self)

    def parse(self, document, text):
        '''
        Transform spaCy output to match Snorkel's default format
        :param document:
        :param text:
        :return:
        '''
        offsets, sents = zip(*text)
        sents = map(self.to_unicode, list(sents))
        sents = map(Parser.strip_null_bytes, list(sents))

        # parse each individual sentence
        position = 0
        sentences = []
        for abs_char_offsets, text in zip(offsets, sents):

            parts = defaultdict(list)
            doc = self.model.make_doc(text)
            for proc in self.pipeline:
                proc(doc)
            assert doc.is_parsed

            # recreate original text (with correct offsets)
            char_offsets = [i - abs_char_offsets[0] for i in abs_char_offsets]
            text = self._original_string(text.split(), char_offsets)

            for sent in list(doc.sents):
                for i,token in enumerate(sent):
                    parts['words'].append(str(token))
                    parts['lemmas'].append(token.lemma_)
                    parts['pos_tags'].append(token.tag_)
                    parts['ner_tags'].append(token.ent_type_ if token.ent_type_ else 'O')
                    head_idx = 0 if token.head is token else token.head.i - sent[0].i + 1
                    parts['dep_parents'].append(head_idx)
                    parts['dep_labels'].append(token.dep_)

            # Add null entity array (matching null for CoreNLP)
            parts['entity_cids'] = ['O' for _ in parts['words']]
            parts['entity_types'] = ['O' for _ in parts['words']]

            # make char_offsets relative to start of sentence
            parts['char_offsets'] = [
                p - parts['char_offsets'][0] for p in parts['char_offsets']
            ]
            parts['position'] = position

            # Link the sentence to its parent document object
            parts['document'] = document
            parts['text'] = text

            # Add null entity array (matching null for CoreNLP)
            parts['entity_cids'] = ['O' for _ in parts['words']]
            parts['entity_types'] = ['O' for _ in parts['words']]

            parts['char_offsets'] = char_offsets
            parts['abs_char_offsets'] = abs_char_offsets

            # Assign the stable id as document's stable id plus absolute
            # character offset
            abs_sent_offset = abs_char_offsets[0]
            abs_sent_offset_end = abs_sent_offset + char_offsets[-1] + len(parts['words'][-1])
            if document:
                parts['stable_id'] = construct_stable_id(document, 'sentence', abs_sent_offset, abs_sent_offset_end)

            position += 1

            yield parts

class WhitespaceTokenizer(object):
    def __init__(self, nlp):
        self.vocab = nlp.vocab

    def __call__(self, text):
        words = text.split(' ')
        spaces = [True] * len(words)
        return Doc(self.vocab, words=words, spaces=spaces)

class PretokenizedDocPreprocessor(DocPreprocessor):
    """
    Assumes text has already been preprocessed for:
     - Sentence Boundary Detection (SBD)
     - Word tokenization

    Format:
    DOCUMENT_ID     SENTENCE

    """

    def _doc_generator(self, file_path, encoding="utf-8"):
        """

        """
        with codecs.open(file_path, "rU", encoding=encoding) as fp:
            curr = None
            lines = []
            for line in fp:

                s = line.strip().split("\t")
                doc_name = s[0]
                s[1] = int(s[1])
                s[2] = map(int, s[2].split(","))

                if curr == doc_name or curr == None:
                    lines.append(s)
                    curr = doc_name
                elif curr != doc_name:
                    yield lines
                    curr = doc_name
                    lines = [s]

            if lines:
                yield lines

    def _line_corpus_parser(self, content):
        """
        :param content:
        :return:
        """

        doc_name = None
        sents = []
        for line in content:
            doc_name = line[0]
            sents.append(line[2:])

        stable_id = self.get_stable_id(doc_name)

        # Form a Document
        doc = Document(
            name=doc_name, stable_id=stable_id,
            meta={}
        )

        # Return the doc
        return doc, sents

    def parse_file(self, file_path, file_name):
        """
        Parse abstracts

        :param file_path:
        :param file_name:
        :return:
        """
        for content in self._doc_generator(file_path, self.encoding):
            doc, txt = self._line_corpus_parser(content)
            yield doc, txt


import utils2
import numpy as np
import deepxde as dde
import wandb
import time

utils2.inizia()
confi = [0.0001046, 3, 165, "sigmoid", "He normal", 1, 1, 1, 10000]
ded = utils2.create_system(confi)
# o = utils2.train_model(d, "sys")
oeo = utils2.restore_model(ded, "sys")

# # Implement observer 1D
learning_rate, num_dense_layers, num_dense_nodes, activation, initialization, w_domain, w_bc0, w_bc1, w_ic = [0.00004285, 5, 26, "sigmoid", "Glorot uniform", 58, 164, 97, 164]
config = [0.00004285, 5, 26, "sigmoid", "Glorot uniform", 58, 164, 97, 164]

# start a new wandb run to track this script
wandb.init(
    # set the wandb project where this run will be logged
    project="obs-simulation",

    # track hyperparameters and run metadata
    config={
        "learning rate": learning_rate,
        "num_dense_layers": num_dense_layers,
        "num_dense_nodes": num_dense_nodes,
        "activation": activation,
        "initialization": initialization,
        "w_domain": w_domain,
        "w_bc0": w_bc0,
        "w_bc1": w_bc1,
        "w_ic": w_ic
    }
)

# Print the hyper-parameters.
print("learning rate: {0:.1e}".format(learning_rate))
print("num_dense_layers:", num_dense_layers)
print("num_dense_nodes:", num_dense_nodes)
print("activation:", activation)
print("initialization:", initialization)
print("w_domain:", w_domain)
print("w_bc0:", w_bc0)
print("w_bc1:", w_bc1)
print("w_ic:", w_ic)
print()

start_time = time.time()

# Create the neural network with these hyper-parameters.
mm = utils2.create_observer(config)
nn = utils2.train_model(mm, f"obs_ft1")
# possibility to change where we save
error = utils2.l2_penalty(oeo, nn, 25)
# print(accuracy, 'accuracy is')

if np.isnan(error):
    error = 10 ** 5

end_time = time.time()
time_spent = end_time - start_time

# Store the configuration and error in a DataFrame
data = {
    "Learning Rate": learning_rate,
    "Num Dense Layers": num_dense_layers,
    "Num Dense Nodes": num_dense_nodes,
    "Activation": activation,
    "Initialization": initialization,
    "W_domain": w_domain,
    "W_bc0": w_bc0,
    "W_bc1": w_bc1,
    "W_ic": w_ic,
    "Error": error,
    "Time Spent": time_spent
}

wandb.log({"err": error})
wandb.finish()

n_guide = 25
utils2.plot_1obs(oeo, nn, n_guide)
utils2.plot_1obs_tf(oeo, nn, n_guide)
utils2.plot_1obs_l2(oeo, nn, n_guide)


from datetime import datetime, timedelta
from funciones_ETL_airflow import *
from funciones_MinIO import *

from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.empty import EmptyOperator
from airflow.operators.python import PythonOperator

default_args = {
    'owner' : 'LBS',
    'retries' : 5,
    'retry_delay' : timedelta(minutes=10)
}

with DAG(
    default_args = default_args,
    dag_id = 'ETL_0',
    description = 'ETL | Carga inicial',
    start_date = days_ago(1),
    schedule_interval = '@once'
) as dag:

# Extracción y Transformación

    task_0 = EmptyOperator(
        task_id = 'ETL_inicio',
        dag = dag
    )

    task_1 = PythonOperator(
        task_id = 'ET_sucursal',
        python_callable = transform_sucursal,
        op_kwargs= {'bucket': 't-carga-inicial'}
    )

    task_2 = PythonOperator(
        task_id = 'ET_producto',
        python_callable = transform_producto,
        op_kwargs= {'bucket': 't-carga-inicial'}
    )

    task_3 = PythonOperator(
        task_id = 'ET_precios',
        python_callable = transform_precios,
        op_kwargs= {'bucket': 't-carga-inicial'}
    )

    task_4 = EmptyOperator(
        task_id = 'T_fin',
        dag = dag
    )

# Carga

    task_5 = PythonOperator(
        task_id = 'L_PostgreSQL',
        python_callable = load,
        op_kwargs= {'bucket': 't-carga-inicial'}
    )

    task_6 = EmptyOperator(
        task_id = 'ETL_fin',
        dag = dag
    )

    task_0 >> (task_1, task_2, task_3) >> task_4 >> task_5 >> task_6


from io import TextIOWrapper  # noqa: F401
import sys
from typing import Callable, Iterable, TextIO, Union  # noqa: F401

from wsgi_prof.profiler import LineProfiler
#from wsgi_lineprof.stats import FilterType  # noqa: F401

class LineProfilerMiddleware(object):
    def __init__(self,
                 app,  # type: Callable
                 stream=None,  # type: Union[TextIO, TextIOWrapper]
                 filters=tuple()  # type: Iterable[FilterType]
                 ):
        # type: (...) -> None
        self.app = app
        self.stream = sys.stdout if stream is None else stream
        self.filters = filters

    def __call__(self, env, start_response):
        profiler = LineProfiler()
        profiler.enable()
        result = self.app(env, start_response)
        profiler.disable()
        stats = profiler.get_stats()

        for f in self.filters:
            stats = stats.filter(f)

        stats.write_text(self.stream)

        return result


from tqdm import tqdm

from utils.config import raw_path

def extract_names(raw_file, label):
    import jieba.posseg as psg
    with open(raw_file, encoding='utf-8') as f:
        text = f.readlines()
    # for t in text:
    #     res = psg.cut(t)
    # print([(item.word, item.flag) for item in res])
    dict = {}
    for t in tqdm(text):
        res = psg.cut(t)
        for item in res:
            if item.flag == 'nr' and item.word in dict:
                dict[item.word] += 1
            elif item.flag == 'nr' and item.word not in dict:
                dict[item.word] = 1

    # 合并同类
    name_merged = {}
    for n, f in dict.items():
        if n[0] == '和' or n[0] == '但' or n[0] == '与':
            coll = False
            for ot in [k for k in dict.keys() if k != n]:
                if ot == n[1:] or (ot[0] != '和' and ot[0] != '与' and ot[0] != '但' and ot[1:] == n[1:]):
                    coll = True
                    break
            if not coll:
                name_merged.setdefault(n, 0)
                name_merged[n] += f
        else:
            prefx = False
            prefx_ot = None
            for ot in [k for k in dict.keys() if k != n]:
                if ot[1:] == n:
                    prefx = True
                    prefx_ot = ot
                    break
            if prefx:
                name_merged.setdefault(prefx_ot, 0)
                name_merged[prefx_ot] += f
            else:
                name_merged.setdefault(n, 0)
                name_merged[n] += f
    name_count = sorted(name_merged.items(), key=lambda x: x[1], reverse=True)
    name_count = name_count[:min(len(name_count), 200)]
    with open(raw_path + '{}/charac_name.txt'.format(label), 'w') as f:
        for name, freq in name_count:
            if len(name) > 1:
                f.write(name + '\n')
    with open(raw_path + '{}/charac_alias.txt'.format(label), 'w') as f:
        for name, freq in name_count:
            if len(name) == 3:
                f.write("{}:{}\n".format(name, name[1:]))


import os
import discord
from discord.ext import commands
from discord.commands import Option
from cogs import crud, schemas, embed_msg
from cogs.db import async_session
from logging import getLogger, config
from main import log_config

config.dictConfig(log_config)

class Commands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.logger = getLogger('commands')

    @commands.slash_command(guild_ids=[785157404161081374], description='にゃっはろ～!!')
    async def nyahello(self, ctx: discord.ApplicationContext, name: Option(str, '名前を入力して下さい', default=None)):
        name = name or ctx.author.name
        try:
            await ctx.respond(f'{name}さん、にゃっはろ～!!')
        except Exception as e:
            self.logger.error(e)

    @commands.slash_command(guild_ids=[785157404161081374], description='みこちのチャンネル内にある配信/動画/スペースの情報を表示します')
    async def eb_serch_video(
        self,
        ctx: discord.ApplicationContext,
        platform: Option(str, 'プラットフォームを選択してください', choices=['YouTube', 'Twitch', 'Twitter']),
        id: Option(str, '配信/動画/スペースのIDを入力してください')
        ):
        try:
            await ctx.respond(f'{platform}の{id}の情報を取得しています...')
        except Exception as e:
            self.logger.error(e)

def setup(bot):
    return bot.add_cog(Commands(bot))


"""
Following is the implementation of bin sort algorithm.
It will sort the input array with time complexity 0(N + K)
N being the size of array, K being the number of buckets.
Space complexity is at worst O(NK).
"""

def insertion_sort(a):
    """
    :param a: list
        List of objects with an order
    :return: list
        Sorted list with insertion sort algorithm
    """

    for i in range(1, len(a)):
        current_value = a[i]
        position = i

        while position > 0 and a[position - 1] > current_value:
            a[position] = a[position - 1]
            position -= 1
            a[position] = current_value

    return a

def get_buckets(a, bucket_size):
    """
    :param a: list
        List of objects with an order
    :param bucket_size: int
        Size of buckets to fill
    :return: list
        List of buckets to sort array with
    """

    min_v, max_v = min(a), max(a)  # min and max values in array
    bucket_count = (max_v - min_v) // bucket_size + 1
    buckets = [[] for _ in range(int(bucket_count))]   # initialize buckets
    for x in a:  # distribute values into buckets
        buckets[int((x - min_v) // bucket_size)].append(x)
    return buckets

def bin_sort(a, bucket_size=16):
    """
    :param a: list
        List of objects with an order
    :param bucket_size: int
        Size of buckets to fill
    :return: list
        Sorted list with bin sort algorithm
    """

    if len(a) == 0:  # trivial case
        return a

    buckets = get_buckets(a, bucket_size)  # get buckets
    a = []
    for bucket in buckets:  # sort each bucket ...
        a += insertion_sort(bucket)  # ... and place back into array
    return a

def main():
    """
    Sorts sample list with bin sort algorithm,
    then prints sorted list
    """

    unsorted_list = [
        4372.30, 851821, 184.681, 173673, 133.06, 768361, 431.982, 9567.00,
        5566.81, 1982.08, 983511, 170.469, 313978, 552536, 3348.18, 527289,
        95.9491, 303675, 532988, 65143
    ]

    sorted_list = bin_sort(unsorted_list)
    print(sorted_list)

if __name__ == '__main__':
    main()


import json
from pathlib import Path
from urllib.parse import urlparse

import pytest
from fastai.vision import open_image

from api import APP_PATH
from api.dog_cat import Predictor

samples_path = Path(APP_PATH).joinpath("tests", "assertions")

def get_sample(*args, **_kwargs):
    fname = samples_path.joinpath(Path(urlparse(args[1]).path).name)
    return open_image(fname)

@pytest.fixture
def mock_get_by_url(monkeypatch):
    """here we replace result of web request to content of local file"""
    monkeypatch.setattr(
        Predictor, "get_by_url", lambda *args, **kwargs: get_sample(*args, **kwargs)
    )

def test_dog_cat(mock_get_by_url, client):
    payload = json.dumps(
        [
            {"uri": "https://www.publicdomainpictures.net/pictures/170000/velka/cat-on-the-white-14629665801Rd.jpg"},
            {"uri": f"file://{samples_path}/cat-on-the-white-14629665801Rd.jpg"},
            {"uri": "https://no.image"},
        ]
    )
    res = client.post("/dog_cat", data=payload, content_type="application/json")
    assert res.status_code == 200
    assert "cats" == json.loads(res.json)[0]["result"]
    assert "cats" == json.loads(res.json)[1]["result"]
    assert "processing error" == json.loads(res.json)[2]["result"]


from pyteal import *
import os

def approval_program():

    # don't need any real fancy initialization
    handle_creation = Seq([
        Return(Int(1))
    ])

    # initialize scratch variables
    # total amounts to make sure that the contract
    # receives at least as much as is sent out + fees
    totalAmountRec = ScratchVar(TealType.uint64)
    totalAmountSent = ScratchVar(TealType.uint64)
    # i for the for loop
    i = ScratchVar(TealType.uint64)

    # for each arg send a transaction of that amount
    # to the corresponding account in the accounts array
    send_inner_txns = Seq(
        For(i.store(Int(0)), i.load() < Txn.application_args.length(), i.store(i.load() + Int(1))).Do(
            Seq(
                InnerTxnBuilder.Begin(),
                InnerTxnBuilder.SetFields({
                    TxnField.type_enum: TxnType.Payment,
                    TxnField.receiver: Txn.accounts[i.load() + Int(1)],
                    TxnField.amount: Btoi(Txn.application_args[i.load()]),
                }),
                InnerTxnBuilder.Submit()
        )),
        Int(1)
    )

    # make sure that the contract receives at least as much as the
    # amount it sends out + fees, and that each transaction sends
    # the algos to the contract's address
    check_valid = Seq(
        totalAmountRec.store(Int(0)),
        totalAmountSent.store(Int(0)),

        For(i.store(Int(0)), i.load() < Global.group_size() - Int(1), i.store(i.load() + Int(1))).Do(Seq(
            totalAmountRec.store(totalAmountRec.load() + Gtxn[i.load()].amount()),
            If(Gtxn[i.load()].receiver() != Global.current_application_address(), Return(Int(0))))),

        For(i.store(Int(0)), i.load() < Txn.application_args.length(), i.store(i.load() + Int(1))).Do(Seq(
            totalAmountSent.store(totalAmountSent.load() + Btoi(Txn.application_args[i.load()])))),

        totalAmountRec.load() >= totalAmountSent.load() + Global.min_txn_fee() * Txn.application_args.length(),
        )

    handle_noop = And(
        check_valid,
        Txn.accounts.length() == Txn.application_args.length(), # make sure each amount has an account to send to
        Txn.accounts.length() <= Int(16), # contracts can send max 16 innerTxns
        send_inner_txns
    )

    # doesn't need anyone to opt in
    handle_optin = Return(Int(0))

    # only the creator can closeout the contract
    handle_closeout = Return(Txn.sender() == Global.creator_address())

    # nobody can update the contract
    handle_updateapp = Return(Int(0))

    # only creator can delete the contract
    handle_deleteapp = Return(Txn.sender() == Global.creator_address())

    # handle the types of application calls
    program = Cond(
        [Txn.application_id() == Int(0), handle_creation],
        [Txn.on_completion() == OnComplete.OptIn, handle_optin],
        [Txn.on_completion() == OnComplete.CloseOut, handle_closeout],
        [Txn.on_completion() == OnComplete.UpdateApplication, handle_updateapp],
        [Txn.on_completion() == OnComplete.DeleteApplication, handle_deleteapp],
        [Txn.on_completion() == OnComplete.NoOp, Return(handle_noop)]
    )
    return compileTeal(program, mode=Mode.Application, version=5)

# let clear state happen
def clear_state_program():
    program = Return(Int(1))
    return compileTeal(program, mode=Mode.Application, version=5)

# compile to teal and write to file
if __name__ == "__main__":
    path = os.path.dirname(os.path.abspath(__file__))

    with open(os.path.join(path,"approval.teal"), "w") as f:
        f.write(approval_program())

    with open(os.path.join(path,"clear.teal"), "w") as f:
        f.write(clear_state_program())


import os
import json
import requests
from jinja2 import Template
from typing import Union, List

class Service:
    def __init__(self):
        self.client = requests.Session()
        self.base_url = os.getenv("MAILGUN_BASE_URL")
        self.auth = ("api", os.getenv("MAILGUN_API_KEY"))

    def send_mail(self, email_id: Union[str, List[str]], email_type: str, **kwargs):
        """Send email to users with appropriate templates
           using smtp.

        Args:
            email_id (str): `To`
            email_type (str): Template to select
        """
        with open("./core/email/email_config.json") as f:
            f = json.loads(f.read())

        with open(f[email_type]["html"]) as _file:
            template = Template(_file.read())
            html = template.render(**kwargs)

        if os.getenv("CI"):
            print("NOT SENDING EMAILS...")
            return

        if isinstance(email_id, str):
            email_id = [email_id]

        data = {
            "from": "CodeChefSRM <codechefsrm@gmail.com>",
            "to": email_id,
            "subject": f[email_type]["subject"],
            "html": html,
        }
        response = self.client.post(url=self.base_url, auth=self.auth, data=data)
        return response


# from __future__ import unicode_literals
from django.contrib.auth.models import (
    AbstractBaseUser,
    PermissionsMixin,
    UserManager as _UserManager,
)

from django.core.mail import send_mail
from django.core import validators
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils import timezone

from phonenumber_field.modelfields import PhoneNumberField

class UserManager(_UserManager):
    pass

class User(AbstractBaseUser, PermissionsMixin):
    """
    The user model we use instead of User model provided by Django.
    Based on Django's User model.

    Username, password and email are required. Other fields are optional.
    """

    username = models.CharField(
        _("username"),
        max_length=30,
        unique=True,
        help_text=_(
            "Required. 30 characters or fewer. Letters, digits and " "@/./+/-/_ only."
        ),
        validators=[
            validators.RegexValidator(
                r"^[\w.@+-]+$", _("Enter a valid username."), "invalid"
            )
        ],
    )
    realname = models.CharField(_("real name"), max_length=60, blank=True)
    email = models.EmailField(_("email address"), blank=True)
    is_staff = models.BooleanField(
        _("staff status"),
        default=False,
        help_text=_("Designates whether the user can log into this admin " "site."),
    )
    is_active = models.BooleanField(
        _("active"),
        default=True,
        help_text=_(
            "Designates whether this user should be treated as "
            "active. Unselect this instead of deleting accounts."
        ),
    )
    date_joined = models.DateTimeField(_("date joined"), default=timezone.now)
    phone_number = PhoneNumberField(blank=True)

    objects = UserManager()

    USERNAME_FIELD = "username"
    REQUIRED_FIELDS = ["email"]

    # TODO: ModelAdmin for this custom thing
    # see https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#custom-users-and-django-contrib-admin

    class Meta:
        verbose_name = _("user")
        verbose_name_plural = _("users")

    def get_full_name(self):
        "Returns the full name for the user."
        return self.realname

    def get_short_name(self):
        "Returns the short name for the user."
        return self.realname

    def email_user(self, subject, message, from_email=None, **kwargs):
        """
        Sends an email to this User.
        """
        send_mail(subject, message, from_email, [self.email], **kwargs)

class Semester(models.Model):
    SPRING = "1-SPRING"  # number prefixed for easier sorting
    FALL = "2-FALL"
    SEM_CHOICES = ((SPRING, "Vår"), (FALL, "Høst"))

    semester = models.CharField(max_length=8, choices=SEM_CHOICES)
    year = models.SmallIntegerField()

    def __str__(self):
        return self.get_semester_display() + " " + str(self.year)

    class Meta:
        ordering = ["-year", "-semester"]
        unique_together = ("semester", "year")

class Card(models.Model):
    CARD_NUMBER_REGEX = r"^\d{6}\.\d{2}\.\d{7}(\.\d)?$"
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    comment = models.CharField(max_length=20, blank=True)
    disabled = models.BooleanField(default=False)
    card_number = models.CharField(
        max_length=20,
        unique=True,
        validators=[
            validators.RegexValidator(
                CARD_NUMBER_REGEX, _("Enter a valid card number."), "invalid"
            )
        ],
    )

    def __str__(self):
        return "%s - %s (%s)" % (self.user.username, self.card_number, self.comment)

class NfcCard(models.Model):
    CARD_UID_REGEX = r"^[a-f0-9]{4,}$"

    card_uid = models.CharField(
        unique=True,
        max_length=8,
        validators=[
            validators.RegexValidator(
                CARD_UID_REGEX, _("Enter valid card uid."), "invalid"
            )
        ],
    )
    user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE)
    intern = models.BooleanField(default=False)
    comment = models.CharField(max_length=20, blank=True)

    def __str__(self):
        if not self.user:
            return self.card_uid
        return "%s (%s)" % (self.card_uid, self.user)

class Group(models.Model):
    name = models.CharField(max_length=50, unique=True)
    leader = models.ForeignKey(User, on_delete=models.CASCADE)
    description = models.CharField(max_length=300, null=True, blank=True)

    def __str__(self):
        return self.name


from typing import List

class Solution:
    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
        if len(mat) * len(mat[0]) != r * c:
            return mat

        result_list = []
        for row in range(len(mat)):
            for col in range(len(mat[0])):
                result_list.append(mat[row][col])
        matrix_result = []
        n = 0
        for row in range(r):
            temp_matrix = []
            for col in range(c):
                temp_matrix.append(result_list[n])
                n += 1
            matrix_result.append(temp_matrix)

        return matrix_result

    def matrixReshape_2(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
        if len(mat) * len(mat[0]) != r * c:
            return mat

        for i in mat[1:]:
            mat[0] += i
        mat = mat[0]

        newmat = []
        for i in range(r):
            newmat.append(mat[:c])
            mat = mat[c:]

        return newmat


nums = [3, 4, -99, 78, 4 ,-99, 3]
x = int(input("Enter an integer:"))
# must not use count() or in or index()
found = False
for num in nums:
    if num ==x:
        print("Found!!!")
        found = True
        break
# if found:
#     print("Found")
# else:
#     print("not found")

#Linear search


import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.parameter import Parameter
device = 'cuda' if torch.cuda.is_available() else 'cpu'

class LMA(nn.Module):
    def __init__(self, mu=0.99, num_bins=8):
        super(LMA, self).__init__()
        self.mu = mu
        self.num_bins = num_bins
        self.mv_mean = torch.tensor(0.0)#.to(device)
        self.mv_std = torch.tensor(0.0)#.to(device)
        self.init_alpha = torch.tensor([0.0 for k in range(num_bins//2)]+[1.0 for k in range(num_bins - num_bins//2)]).cuda()
        self.alphas = Parameter(torch.tensor([0.0 for k in range(num_bins)]))
        self.betas = Parameter(torch.tensor([0.0 for k in range(num_bins)]))

    def forward(self, x):
        mean = x.mean().detach()
        std = x.std().detach()
        new_mean = self.mv_mean * self.mu + mean * (1-self.mu)
        new_std = self.mv_std * self.mu + std * (1-self.mu)
        self.mv_mean = new_mean.clone()
        self.mv_std = new_std.clone()
        if not self.training:
            mean = self.mv_mean.detach()
            std = self.mv_std.detach()
        step = 6*std / self.num_bins
        idx = torch.clamp(((x-mean+3*std)/step).long(), 0, self.num_bins-1).view(-1)
        alphas = torch.index_select(self.alphas + self.init_alpha, dim=0, index=idx).view(x.size())
        betas = torch.index_select(self.betas, dim=0, index=idx).view(x.size())
        return alphas * x + betas

class APLU(nn.Module):
    def __init__(self, dim, num_bins=8):
        super(newAPLU, self).__init__()
        # because there is a fixed cut points, zero point.
        self.num_bins = num_bins-2
        self.dim = dim
        self.alphas = Parameter(torch.Tensor(dim, self.num_bins))
        self.betas = Parameter(torch.Tensor(dim, self.num_bins))
        self.alphas.data.uniform_(-0.5,0.5)
        self.betas.data.normal_(0, 0.5)

    def forward(self, x):
        pre_size = x.size()
        x = x.view(-1, self.dim, 1)
        pos = nn.ReLU()(x)
        xs = nn.ReLU()(-x+self.betas.view(1,self.dim,self.num_bins))
        neg = torch.sum(xs*self.alphas.view(1,self.dim,self.num_bins), -1, keepdim=True)
        return (pos + neg).view(pre_size)

class PReLU(nn.Module):
    def __init__(self):
        super(PReLU, self).__init__()
        self.alpha = Parameter(torch.tensor(0.25))

    def forward(self, x):
        return nn.ReLU()(x) - self.alpha * nn.ReLU()(-x)

class Swish(nn.Module):
    def __init__(self, dim):
        super(Swish, self).__init__()
        self.betas = Parameter(torch.ones(dim))
        self.dim = dim

    def forward(self, x):
        pre_size = x.size()
        return x * nn.Sigmoid()(self.betas.view(-1,self.dim)*x.view(-1,self.dim)).view(pre_size)


from mintersdk.shortcuts import to_bip

from bot.helpers.converter import currency_in_user_currency

def choice_payment_currency_text(order):
    payment_currency_list = order.payment_currency

    txt = f'{order.user.get_text(name="order-select_payment_currency").format(currency=order.trade_currency)}\n\n'
    if len(payment_currency_list) > 0:
        txt += f'{order.user.get_text(name="order-your_choice")}\n'
        for currency in payment_currency_list:
            txt += f'**{currency}**\n'

    return txt

def wallet_info(user):
    user_currency = user.settings.currency

    virt_wallets = user.virtual_wallets.exclude(currency__in=['UAH', 'RUB', 'USD'])
    txt = user.get_text(name='wallet-wallet_info')

    for w in virt_wallets:

        if w.balance > 0:
            balance_in_user_currency = currency_in_user_currency(w.currency, user_currency, to_bip(w.balance))
        else:
            balance_in_user_currency = 0

        txt += f'\n{round(to_bip(w.balance), 4)} {w.currency} ~{round(balance_in_user_currency, 2)} {user_currency}'
    return txt


import logging
import pandas as pd

__all__ = ('ExampleFetcher',)

from utils.fetcher.base_epidemiology import BaseEpidemiologyFetcher

logger = logging.getLogger(__name__)

class ExampleFetcher(BaseEpidemiologyFetcher):
    LOAD_PLUGIN = False
    SOURCE = 'CND_XXX'

    def fetch(self):
        # a csv file to be downloaded
        url = 'https://someserver.com/path/source.csv'
        return pd.read_csv(url)

    def run(self):
        data = self.fetch()

        for index, record in data.iterrows():
            # assumption is that the CSV file has the following columns:
            # date, province, confirmed cases, deaths, recoveries
            date = record[0]  # we expect date to be in YYYY-MM-DD format
            country = record[1]
            province = record[2]
            confirmed = int(record[3])
            dead = int(record[4])
            recovered = int(record[5])

            # we need to build an object containing the data we want to add or update
            upsert_obj = {
                # source is mandatory and is a code that identifies the  source
                'source': self.SOURCE,
                # date is also mandatory, the format must be YYYY-MM-DD
                'date': date,
                # country is mandatory and should be in English
                # the exception is "Ships"
                'country': country,
                # countrycode is mandatory and it's the ISO Alpha-3 code of the country
                # an exception is ships, which has "---" as country code
                'countrycode': 'CDN',
                # adm_area_1, when available, is a wide-area administrative region, like a
                # Canadian province in this case. There are also subareas adm_area_2 and
                # adm_area_3
                'adm_area_1': province,
                # confirmed is the number of confirmed cases of infection, this is cumulative
                'confirmed': confirmed,
                # dead is the number of people who have died because of covid19, this is cumulative
                'dead': dead,
                # recovered is the number of people who have healed, this is cumulative
                'recovered': recovered
            }

            # see the main webpage or the README for all the available fields and their
            # semantics

            # update data
            self.upsert_data(**upsert_obj)

            # alternatively, we can issue the query directly using self.db.execute(query, data)
            # but use it with care!


import sqlite3

conn = None
cursor = None

def connect(db_file):
    global conn, cursor
    conn = sqlite3.connect(db_file)
    cursor = conn.cursor()

def get_volumes():
    volumes = []
    query = "SELECT name, type, status, num_bricks FROM volumes"
    for row in cursor.execute(query):
        volumes.append({"name": row[0],
                        "type": row[1],
                        "status": row[2],
                        "num_bricks": row[3]})
    return volumes

def table_cleanup_volumes(volume=None):
    query = "DELETE FROM volumes"

    if volume:
        query += " WHERE name = ?"
        cursor.execute(query, (volume,))
    else:
        cursor.execute(query)

    conn.commit()

def table_cleanup_bricks(volume=None):
    query = "DELETE FROM bricks"

    if volume:
        query += " WHERE volume = ?"
        cursor.execute(query, (volume,))
    else:
        cursor.execute(query)

    conn.commit()

def table_cleanup_options(volume=None):
    query = "DELETE FROM options"

    if volume:
        query += " WHERE volume = ?"
        cursor.execute(query, (volume,))
    else:
        cursor.execute(query)

    conn.commit()

def table_cleanup_all(volume=None):
    table_cleanup_volumes(volume)
    table_cleanup_bricks(volume)
    table_cleanup_options(volume)

def volumes_add(vols):
    query = """INSERT INTO volumes(id, name, type, status, num_bricks,
    transport, updated_at) VALUES(?, ?, ?, ?, ?, ?, datetime('now'))"""

    cursor.executemany(query, vols)
    conn.commit()

def bricks_add(bricks):
    query = """INSERT INTO bricks(volume, brick, updated_at)
    VALUES(?, ?, datetime('now'))"""
    cursor.executemany(query, bricks)
    conn.commit()

def options_add(options):
    query = """INSERT INTO options(volume, name, value, updated_at)
    VALUES(?, ?, ?, datetime('now'))"""
    cursor.executemany(query, options)
    conn.commit()

def update_volume(volume, key, value):
    query = """UPDATE volumes SET %s = ?, updated_at = datetime('now')
    WHERE name = ?""" % key
    cursor.execute(query, (value, volume))
    conn.commit()

def setup():
    queries = ["DROP TABLE IF EXISTS volumes",
               "DROP TABLE IF EXISTS bricks",
               """CREATE TABLE volumes(
               id VARCHAR(40),
               name VARCHAR(100),
               type VARCHAR(20),
               status VARCHAR(10),
               transport VARCHAR(20),
               num_bricks INT,
               updated_at DATETIME,
               created_at DATETIME DEFAULT CURRENT_TIMESTAMP
               )
               """,
               """CREATE TABLE bricks(
               volume VARCHAR(100),
               brick VARCHAR(200),
               status VARCHAR(10),
               updated_at DATETIME,
               created_at DATETIME DEFAULT CURRENT_TIMESTAMP
               )
               """,
               """CREATE TABLE options(
               volume VARCHAR(100),
               key VARCHAR(500),
               value VARCHAR(500),
               updated_at DATETIME,
               created_at DATETIME DEFAULT CURRENT_TIMESTAMP
               )
               """]
    for query in queries:
        cursor.execute(query)


import rose.upgrade
import re
import sys

class UpgradeError(Exception):

    """Exception created when an upgrade fails."""

    def __init__(self, msg):
        self.msg = msg

    def __repr__(self):
        sys.tracebacklimit = 0
        return self.msg

    __str__ = __repr__

class vn55_t822(rose.upgrade.MacroUpgrade):

    """Upgrade macro from JULES by Maggie Hendry"""

    BEFORE_TAG = "vn5.5"
    AFTER_TAG = "vn5.5_t822"

    def _rename_section_fast(self, config, section, new_section):
        """Rename a setting in the configuration."""
        config.value[new_section] = config.value[section]
        config.unset([section])
        info = self.INFO_RENAMED_SECT.format(section, new_section)
        self.add_report(new_section, None, None, info)
        self.add_report(section, None, None, self.INFO_REMOVED)

    def upgrade(self, config, meta_config=None):
        """Upgrade a JULES runtime app configuration."""
        self.add_setting(config,
                         ["namelist:jules_radiation", "fixed_sea_albedo"],
                         "0.0")
        self.add_setting(config,
                         ["namelist:jules_radiation", "i_sea_alb_method"],
                         "1")
        self.add_setting(config,
                         ["namelist:jules_radiation", "l_sea_alb_var_chl"],
                         ".false.")
        self.add_setting(config,
                         ["namelist:jules_radiation", "l_spec_sea_alb"],
                         ".false.")
        self.add_setting(config,
                         ["namelist:jules_radiation", "l_dolr_land_black"],
                         ".false.")

        # jules_vegetation
        self.add_setting(config, ["namelist:jules_vegetation",
                                  "l_nrun_mid_trif"],
                         ".false.")
        self.add_setting(config, ["namelist:jules_vegetation",
                                  "l_trif_init_accum"],
                         ".false.")
        # The default has been changed from .true. to .false. in the code,
        # however if l_triffid=F then the code changed this to .false. anyway.
        # It's also trigger ignored when l_triffid=F and the compulsory hasn't
        # changed, so probably doesn't make any difference. Adding for
        # completeness
        self.add_setting(config, ["namelist:jules_vegetation",
                                  "l_veg_compete"],
                         ".true.")
        self.add_setting(config, ["namelist:jules_vegetation", "frac_min"],
                         "1.0e-6")
        self.add_setting(config, ["namelist:jules_vegetation", "frac_seed"],
                         "0.01")
        self.add_setting(config, ["namelist:jules_vegetation", "ilayers"],
                         "10")
        self.add_setting(config, ["namelist:jules_vegetation", "pow"],
                         "20.0")

        # Rename jules_cable* to cable_* so CABLE namelists appear together and
        # separately from JULES namelists (copied from vn47_t334).
        namelist = []

        namelsts = self.get_setting_value(config, ["file:pft_params.nml",
                                                   "source"],)
        if (namelsts):
            namelsts=namelsts.replace("namelist:jules_pftparm_cable",
                                      "namelist:cable_pftparm")
            self.change_setting_value(config, ["file:pft_params.nml","source"],
                                      namelsts)

        namelsts = self.get_setting_value(config,
                                          ["file:jules_soilparm_cable.nml",
                                           "source"],)
        if (namelsts):
            namelsts=namelsts.replace("namelist:jules_soilparm_cable",
                                      "namelist:cable_soilparm")
            self.change_setting_value(config, ["file:jules_soilparm_cable.nml",
                                               "source"],
                                      namelsts)

        for obj in config.get_value():
            if re.search(r'namelist:jules_pftparm_cable',obj):
                namelist.append(obj)
            elif re.search(r'namelist:jules_soilparm_cable',obj):
                namelist.append(obj)

        for obj in namelist:
            if re.search(r'namelist:jules_pftparm_cable',obj):
                new_obj = obj.replace("namelist:jules_pftparm_cable",
                                      "namelist:cable_pftparm")
                self._rename_section_fast(config, obj,new_obj)
            elif re.search(r'namelist:jules_soilparm_cable',obj):
                new_obj = obj.replace("namelist:jules_soilparm_cable",
                                      "namelist:cable_soilparm")
                self._rename_section_fast(config, obj,new_obj)

        # Rename the CABLE namelist file to keep it consistent
        self.rename_setting(config, ["file:jules_soilparm_cable.nml"],
                            ["file:cable_soilparm.nml"])

        return config, self.reports

class vn55_t864(rose.upgrade.MacroUpgrade):

    """Upgrade macro from JULES by Douglas Clark"""

    BEFORE_TAG = "vn5.5_t822"
    AFTER_TAG = "vn5.5_t864"

    def upgrade(self, config, meta_config=None):
        """Upgrade a JULES runtime app configuration."""

        # Add new switch and related parameters for photosynthesis model.
        self.add_setting(config, ["namelist:jules_vegetation", "photo_model"], "1")
        npft = int(self.get_setting_value(config, ["namelist:jules_surface_types", "npft"]))
        self.add_setting(config, ["namelist:jules_pftparm", "act_jmax_io"],','.join(['50.0e3']*npft))
        self.add_setting(config, ["namelist:jules_pftparm", "act_vcmax_io"],','.join(['72.0e3']*npft))
        self.add_setting(config, ["namelist:jules_pftparm", "alpha_elec_io"],','.join(['0.4']*npft))
        self.add_setting(config, ["namelist:jules_pftparm", "deact_jmax_io"],','.join(['200.0e3']*npft))
        self.add_setting(config, ["namelist:jules_pftparm", "deact_vcmax_io"],','.join(['200.0e3']*npft))
        self.add_setting(config, ["namelist:jules_pftparm", "ds_jmax_io"],','.join(['646.0']*npft))
        self.add_setting(config, ["namelist:jules_pftparm", "ds_vcmax_io"],','.join(['649.0']*npft))
        self.add_setting(config, ["namelist:jules_pftparm", "jv25_ratio_io"],','.join(['1.97']*npft))
        return config, self.reports

class vn55_vn56(rose.upgrade.MacroUpgrade):
    """Version bump macro"""

    BEFORE_TAG = "vn5.5_t864"
    AFTER_TAG = "vn5.6"

    def upgrade(self, config, meta_config=None):
        # Nothing to do
        return config, self.reports


import json
import requests
import time
import argparse
from utils import mp3_files, save_to_pickle
from download_podcasts import create_folder

upload_endpoint = "https://api.assemblyai.com/v2/upload"
transcript_endpoint = "https://api.assemblyai.com/v2/transcript"
polling_endpoint = "https://api.assemblyai.com/v2/transcript"

class AssemblyAiTranscriber():
    def __init__(self, credentials_file, time_limit=None) -> None:
        filename = credentials_file or "assembly_ai_credentials.json"
        self._read_credentials(filename)
        self.time_limit = time_limit

    def upload_files(self):
        header = {
	        'authorization': self._api_key,
	        'content-type': 'application/json'
        }
        for file_name in mp3_files('downloads'):
            print(f'Uploading {file_name}...')
            # Upload the file
            upload_response = requests.post(
                upload_endpoint,
                headers=header, data=self._read_file(file_name)
            )
            yield upload_response.json()['upload_url']

    def start_transcribe(self, name_generator):
        header = {
	        'authorization': self._api_key,
	        'content-type': 'application/json'
        }
        for upload_url in name_generator:
            transcript_request = {
                'audio_url': upload_url
            }
            if self.time_limit:
                transcript_request['audio_start_from'] = 0
                transcript_request['audio_end_at'] = self.time_limit * 1000
            transcript_response = requests.post(
                transcript_endpoint,
                json=transcript_request,
                headers=header
            )
            yield transcript_response.json()['id']

    def wait_results(self, jobs_generator):
        header = {
	        'authorization': self._api_key,
	        'content-type': 'application/json'
        }

        jobs = {job for job in jobs_generator}
        completed_jobs = set()
        while len(jobs) > len(completed_jobs):
            for job in {j for j in jobs if not j in completed_jobs }:
                endpoint = polling_endpoint + f'/{job}'
                polling_response = requests.get(endpoint, headers=header)
                polling_response = polling_response.json()

                print('Polling status:' + polling_response['status'])
                if polling_response['status'] == 'completed':
                    completed_jobs.add(job)
                    yield (job, self._get_paragraphs(endpoint, header))
            time.sleep(5)

    def _get_paragraphs(self, polling_endpoint, header):
        paragraphs_response = requests.get(polling_endpoint + "/paragraphs", headers=header)
        paragraphs_response = paragraphs_response.json()

        paragraphs = [para['text'] for para in paragraphs_response['paragraphs']]

        return ''.join(paragraphs)

    def _read_credentials(self, filename):
        with open(filename, 'r') as f:
            credentials = json.load(f)
        self._api_key = credentials['assemly_ai_api_key']

    def _read_file(self, filename, chunk_size=5242880):
        with open(filename, 'rb') as _file:
            while True:
                data = _file.read(chunk_size)
                if not data:
                    break
                yield data

if __name__ == '__main__':
    parser=argparse.ArgumentParser()
    parser.add_argument("--limit", help="How many seconds of podcast to transcribe")
    args=parser.parse_args()

    limit = None
    if args.limit:
        limit = int(args.limit)

    assemblyAiTranscriber = AssemblyAiTranscriber('assembly_ai_credentials.json', time_limit=limit)

    transcriptions_folder = 'assembly_ai_transcriptions'
    create_folder(transcriptions_folder)

    upload = assemblyAiTranscriber.upload_files()
    transcriptions = assemblyAiTranscriber.start_transcribe(upload)
    count = 0
    for transcription in assemblyAiTranscriber.wait_results(transcriptions):
        count += 1
        print(f'Transcription: {transcription}')
        save_to_pickle(transcriptions_folder + '/' + transcription[0], transcription[1])
    print(f'Saved {count} transcrptions')


import os
import time
import argparse
import numpy as np
import tensorflow as tf

from dataset_ffhq import get_ffhq_dataset
from stylegan2.utils import preprocess_fit_train_image, postprocess_images, merge_batch_images
from stylegan2.generator import Generator
from stylegan2.discriminator import Discriminator
# from tf_utils.utils import allow_memory_growth

# allow_memory_growth()

class Trainer(object):
    def __init__(self, t_params, name):
        self.model_base_dir = t_params['model_base_dir']
        self.tfrecord_dir = t_params['tfrecord_dir']
        self.shuffle_buffer_size = t_params['shuffle_buffer_size']
        self.g_params = t_params['g_params']
        self.d_params = t_params['d_params']
        self.g_opt = t_params['g_opt']
        self.d_opt = t_params['d_opt']
        self.batch_size = t_params['batch_size']
        self.n_total_image = t_params['n_total_image']
        self.n_samples = min(t_params['batch_size'], t_params['n_samples'])
        self.lazy_regularization = t_params['lazy_regularization']

        self.r1_gamma = 10.0
        # self.r2_gamma = 0.0
        self.max_steps = int(np.ceil(self.n_total_image / self.batch_size))
        self.out_res = self.g_params['resolutions'][-1]
        self.log_template = 'step {}: elapsed: {:.2f}s, d_loss: {:.3f}, g_loss: {:.3f}, r1_reg: {:.3f}, pl_reg: {:.3f}'
        self.print_step = 10
        self.save_step = 100
        self.image_summary_step = 100
        self.reached_max_steps = False

        # setup optimizer params
        self.g_opt = self.set_optimizer_params(self.g_opt)
        self.d_opt = self.set_optimizer_params(self.d_opt)
        self.pl_mean = tf.Variable(initial_value=0.0, name='pl_mean', trainable=False)
        self.pl_decay = 0.01
        self.pl_weight = 1.0
        self.pl_denorm = 1.0 / np.sqrt(self.out_res * self.out_res)

        # grab dataset
        print('Setting datasets')
        self.dataset = get_ffhq_dataset(self.tfrecord_dir, self.out_res, self.shuffle_buffer_size,
                                        self.batch_size, epochs=None)

        # create models
        print('Create models')
        self.generator = Generator(self.g_params)
        self.discriminator = Discriminator(self.d_params)
        self.d_optimizer = tf.keras.optimizers.Adam(self.d_opt['learning_rate'],
                                                    beta_1=self.d_opt['beta1'],
                                                    beta_2=self.d_opt['beta2'],
                                                    epsilon=self.d_opt['epsilon'])
        self.g_optimizer = tf.keras.optimizers.Adam(self.g_opt['learning_rate'],
                                                    beta_1=self.g_opt['beta1'],
                                                    beta_2=self.g_opt['beta2'],
                                                    epsilon=self.g_opt['epsilon'])
        self.g_clone = Generator(self.g_params)

        # finalize model (build)
        test_latent = np.ones((1, self.g_params['z_dim']), dtype=np.float32)
        test_labels = np.ones((1, self.g_params['labels_dim']), dtype=np.float32)
        test_images = np.ones((1, 3, self.out_res, self.out_res), dtype=np.float32)
        _, __ = self.generator([test_latent, test_labels], training=False)
        _ = self.discriminator([test_images, test_labels], training=False)
        _, __ = self.g_clone([test_latent, test_labels], training=False)
        print('Copying g_clone')
        self.g_clone.set_weights(self.generator.get_weights())

        # setup saving locations (object based savings)
        self.ckpt_dir = os.path.join(self.model_base_dir, name)
        self.ckpt = tf.train.Checkpoint(d_optimizer=self.d_optimizer,
                                        g_optimizer=self.g_optimizer,
                                        discriminator=self.discriminator,
                                        generator=self.generator,
                                        g_clone=self.g_clone)
        self.manager = tf.train.CheckpointManager(self.ckpt, self.ckpt_dir, max_to_keep=2)

        # try to restore
        self.ckpt.restore(self.manager.latest_checkpoint)
        if self.manager.latest_checkpoint:
            print('Restored from {}'.format(self.manager.latest_checkpoint))

            # check if already trained in this resolution
            restored_step = self.g_optimizer.iterations.numpy()
            if restored_step >= self.max_steps:
                print('Already reached max steps {}/{}'.format(restored_step, self.max_steps))
                self.reached_max_steps = True
                return
        else:
            print('Not restoring from saved checkpoint')

    def set_optimizer_params(self, params):
        if self.lazy_regularization:
            mb_ratio = params['reg_interval'] / (params['reg_interval'] + 1)
            params['learning_rate'] = params['learning_rate'] * mb_ratio
            params['beta1'] = params['beta1'] ** mb_ratio
            params['beta2'] = params['beta2'] ** mb_ratio
        return params

    @tf.function
    def d_train_step(self, z, real_images, labels):
        with tf.GradientTape() as d_tape:
            # forward pass
            fake_images, _ = self.generator([z, labels], training=True)
            real_scores = self.discriminator([real_images, labels], training=True)
            fake_scores = self.discriminator([fake_images, labels], training=True)

            # gan loss
            d_loss = tf.math.softplus(fake_scores)
            d_loss += tf.math.softplus(-real_scores)
            d_loss = tf.reduce_mean(d_loss)
        d_gradients = d_tape.gradient(d_loss, self.discriminator.trainable_variables)
        self.d_optimizer.apply_gradients(zip(d_gradients, self.discriminator.trainable_variables))
        return d_loss

    @tf.function
    def d_reg_train_step(self, z, real_images, labels):
        with tf.GradientTape() as d_tape:
            # forward pass
            fake_images, _ = self.generator([z, labels], training=True)
            real_scores = self.discriminator([real_images, labels], training=True)
            fake_scores = self.discriminator([fake_images, labels], training=True)

            # gan loss
            d_loss = tf.math.softplus(fake_scores)
            d_loss += tf.math.softplus(-real_scores)

            # simple GP
            with tf.GradientTape() as p_tape:
                p_tape.watch(real_images)
                real_loss = tf.reduce_sum(self.discriminator([real_images, labels], training=True))

            real_grads = p_tape.gradient(real_loss, real_images)
            r1_penalty = tf.reduce_sum(tf.math.square(real_grads), axis=[1, 2, 3])
            r1_penalty = tf.expand_dims(r1_penalty, axis=1)
            r1_penalty = r1_penalty * self.d_opt['reg_interval']

            # combine
            d_loss += r1_penalty * (0.5 * self.r1_gamma)
            d_loss = tf.reduce_mean(d_loss)

        d_gradients = d_tape.gradient(d_loss, self.discriminator.trainable_variables)
        self.d_optimizer.apply_gradients(zip(d_gradients, self.discriminator.trainable_variables))
        return d_loss, tf.reduce_mean(r1_penalty)

    @tf.function
    def g_train_step(self, z, labels):
        with tf.GradientTape() as g_tape:
            # forward pass
            fake_images, _ = self.generator([z, labels], training=True)
            fake_scores = self.discriminator([fake_images, labels], training=True)

            # gan loss
            g_loss = tf.math.softplus(-fake_scores)
            g_loss = tf.reduce_mean(g_loss)

        g_gradients = g_tape.gradient(g_loss, self.generator.trainable_variables)
        self.g_optimizer.apply_gradients(zip(g_gradients, self.generator.trainable_variables))
        return g_loss

    @tf.function
    def g_reg_train_step(self, z, labels):
        with tf.GradientTape() as g_tape:
            # forward pass
            fake_images, _ = self.generator([z, labels], training=True)
            fake_scores = self.discriminator([fake_images, labels], training=True)

            # gan loss
            g_loss = tf.math.softplus(-fake_scores)

            # path length regularization
            # Compute |J*y|.
            with tf.GradientTape() as pl_tape:
                fake_images, w_broadcasted = self.generator([z, labels], training=True)

                pl_noise = tf.random.normal(tf.shape(fake_images), mean=0.0, stddev=1.0, dtype=tf.float32) * self.pl_denorm
                pl_noise_added = tf.reduce_sum(fake_images * pl_noise)

            pl_grads = pl_tape.gradient(pl_noise_added, w_broadcasted)
            pl_lengths = tf.math.sqrt(tf.reduce_mean(tf.reduce_sum(tf.math.square(pl_grads), axis=2), axis=1))

            # Track exponential moving average of |J*y|.
            pl_mean_val = self.pl_mean + self.pl_decay * (tf.reduce_mean(pl_lengths) - self.pl_mean)
            self.pl_mean.assign(pl_mean_val)

            # Calculate (|J*y|-a)^2.
            pl_penalty = tf.square(pl_lengths - self.pl_mean)

            # compute
            pl_reg = pl_penalty * self.pl_weight

            # combine
            g_loss += pl_reg
            g_loss = tf.reduce_mean(g_loss)

        g_gradients = g_tape.gradient(g_loss, self.generator.trainable_variables)
        self.g_optimizer.apply_gradients(zip(g_gradients, self.generator.trainable_variables))
        return g_loss, tf.reduce_mean(pl_reg)

    def train(self):
        if self.reached_max_steps:
            return

        # start actual training
        print('Start Training')

        # setup tensorboards
        train_summary_writer = tf.summary.create_file_writer(self.ckpt_dir)

        # loss metrics
        metric_g_loss = tf.keras.metrics.Mean('g_loss', dtype=tf.float32)
        metric_d_loss = tf.keras.metrics.Mean('d_loss', dtype=tf.float32)
        metric_r1_reg = tf.keras.metrics.Mean('r1_reg', dtype=tf.float32)
        metric_pl_reg = tf.keras.metrics.Mean('pl_reg', dtype=tf.float32)

        # start training
        print('max_steps: {}'.format(self.max_steps))
        losses = {'g_loss': 0.0, 'd_loss': 0.0, 'r1_reg': 0.0, 'pl_reg': 0.0}
        t_start = time.time()

        for real_images in self.dataset:
            # preprocess inputs
            z = tf.random.normal(shape=[tf.shape(real_images)[0], self.g_params['z_dim']], dtype=tf.dtypes.float32)
            real_images = preprocess_fit_train_image(real_images, self.out_res)
            labels = tf.ones((tf.shape(real_images)[0], self.g_params['labels_dim']), dtype=tf.dtypes.float32)

            # get current step
            step = self.g_optimizer.iterations.numpy()

            # d train step
            if step % self.d_opt['reg_interval'] == 0:
                d_loss, r1_reg = self.d_reg_train_step(z, real_images, labels)

                # update values for printing
                losses['d_loss'] = d_loss.numpy()
                losses['r1_reg'] = r1_reg.numpy()

                # update metrics
                metric_d_loss(d_loss)
                metric_r1_reg(r1_reg)
            else:
                d_loss = self.d_train_step(z, real_images, labels)

                # update values for printing
                losses['d_loss'] = d_loss.numpy()
                losses['r1_reg'] = 0.0

                # update metrics
                metric_d_loss(d_loss)

            # update g_clone
            self.g_clone.set_as_moving_average_of(self.generator)

            # g train step
            if step % self.g_opt['reg_interval'] == 0:
                g_loss, pl_reg = self.g_reg_train_step(z, labels)

                # update values for printing
                losses['g_loss'] = g_loss.numpy()
                losses['pl_reg'] = pl_reg.numpy()

                # update metrics
                metric_g_loss(g_loss)
                metric_pl_reg(pl_reg)
            else:
                g_loss = self.g_train_step(z, labels)

                # update values for printing
                losses['g_loss'] = g_loss.numpy()
                losses['pl_reg'] = 0.0

                # update metrics
                metric_g_loss(g_loss)

            # save to tensorboard
            with train_summary_writer.as_default():
                tf.summary.scalar('g_loss', metric_g_loss.result(), step=step)
                tf.summary.scalar('d_loss', metric_d_loss.result(), step=step)
                tf.summary.scalar('r1_reg', metric_r1_reg.result(), step=step)
                tf.summary.scalar('pl_reg', metric_pl_reg.result(), step=step)
                tf.summary.histogram('w_avg', self.generator.w_avg, step=step)

            # save every self.save_step
            if step % self.save_step == 0:
                self.manager.save(checkpoint_number=step)

            # save every self.image_summary_step
            if step % self.image_summary_step == 0:
                # add summary image
                summary_image = self.sample_images_tensorboard(real_images)
                with train_summary_writer.as_default():
                    tf.summary.image('images', summary_image, step=step)

            # print every self.print_steps
            if step % self.print_step == 0:
                elapsed = time.time() - t_start
                print(self.log_template.format(step, elapsed,
                                               losses['d_loss'], losses['g_loss'], losses['r1_reg'], losses['pl_reg']))

                # reset timer
                t_start = time.time()

            # check exit status
            if step >= self.max_steps:
                break

        # get current step
        step = self.g_optimizer.iterations.numpy()
        elapsed = time.time() - t_start
        print(self.log_template.format(step, elapsed,
                                       losses['d_loss'], losses['g_loss'], losses['r1_reg'], losses['pl_reg']))

        # save last checkpoint
        self.manager.save(checkpoint_number=step)
        return

    def sample_images_tensorboard(self, real_images):
        # prepare inputs
        reals = real_images[:self.n_samples, :, :, :]
        latents = tf.random.normal(shape=(self.n_samples, self.g_params['z_dim']), dtype=tf.dtypes.float32)
        dummy_labels = tf.ones((self.n_samples, self.g_params['labels_dim']), dtype=tf.dtypes.float32)

        # run networks
        fake_images_00, _ = self.g_clone([latents, dummy_labels], truncation_psi=0.0, training=False)
        fake_images_05, _ = self.g_clone([latents, dummy_labels], truncation_psi=0.5, training=False)
        fake_images_07, _ = self.g_clone([latents, dummy_labels], truncation_psi=0.7, training=False)
        fake_images_10, _ = self.g_clone([latents, dummy_labels], truncation_psi=1.0, training=False)

        # merge on batch dimension: [5 * n_samples, 3, out_res, out_res]
        out = tf.concat([reals, fake_images_00, fake_images_05, fake_images_07, fake_images_10], axis=0)

        # prepare for image saving: [5 * n_samples, out_res, out_res, 3]
        out = postprocess_images(out)

        # resize to save disk spaces: [5 * n_samples, size, size, 3]
        size = min(self.out_res, 256)
        out = tf.image.resize(out, size=[size, size])

        # make single image and add batch dimension for tensorboard: [1, 5 * size, n_samples * size, 3]
        out = merge_batch_images(out, size, rows=5, cols=self.n_samples)
        out = np.expand_dims(out, axis=0)
        return out

def filter_resolutions_featuremaps(resolutions, featuremaps, res):
    index = resolutions.index(res)
    filtered_resolutions = resolutions[:index + 1]
    filtered_featuremaps = featuremaps[:index + 1]
    return filtered_resolutions, filtered_featuremaps

def main():
    # global program arguments parser
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('--model_base_dir', default='./models', type=str)
    parser.add_argument('--tfrecord_dir', default='/mnt/vision-nas/data-sets/stylegan/ffhq-dataset/tfrecords/ffhq', type=str)
    parser.add_argument('--train_res', default=32, type=int)
    parser.add_argument('--shuffle_buffer_size', default=1000, type=int)
    parser.add_argument('--batch_size', default=4, type=int)
    args = vars(parser.parse_args())

    # network params
    train_res = args['train_res']
    resolutions = [  4,   8,  16,  32,  64, 128, 256, 512, 1024]
    featuremaps = [512, 512, 512, 512, 512, 256, 128,  64,   32]
    train_resolutions, train_featuremaps = filter_resolutions_featuremaps(resolutions, featuremaps, train_res)
    g_params = {
        'z_dim': 512,
        'w_dim': 512,
        'labels_dim': 0,
        'n_mapping': 8,
        'resolutions': train_resolutions,
        'featuremaps': train_featuremaps,
        'w_ema_decay': 0.995,
        'style_mixing_prob': 0.9,
    }
    d_params = {
        'labels_dim': 0,
        'resolutions': train_resolutions,
        'featuremaps': train_featuremaps,
    }

    # training parameters
    training_parameters = {
        # global params
        **args,

        # network params
        'g_params': g_params,
        'd_params': d_params,

        # training params
        'g_opt': {'learning_rate': 0.002, 'beta1': 0.0, 'beta2': 0.99, 'epsilon': 1e-08, 'reg_interval': 4},
        'd_opt': {'learning_rate': 0.002, 'beta1': 0.0, 'beta2': 0.99, 'epsilon': 1e-08, 'reg_interval': 16},
        'batch_size': args['batch_size'],
        'n_total_image': 25000000,
        'n_samples': 4,
        'lazy_regularization': True,
    }

    trainer = Trainer(training_parameters, name='stylegan2-ffhq')
    trainer.train()
    return

if __name__ == '__main__':
    main()


import cv2
import numpy as np
import sqlite3
# truy cap vao he thong de lay duoc duong dan cac thu muc trong may
import os

def insertOrUpdate(id, name, age, gender):

	conn = sqlite3.connect('D:\Final\AIdemo\AiDemo.db')

	query = "SELECT * FROM user WHERE id = " + str(id)
	rs = conn.execute(query)

	isExist = 0

	for row in rs:
		isExist = 1

	if(isExist == 0):
		query = "INSERT INTO user(id, name, age, gender) VALUES(" +str(id)+ ", '"+str(name)+"', "+str(age)+", '"+str(gender)+"')"
	else:
		query = "UPDATE user SET name = '" + str(name)+ "' ,age = " + str(age) + ", gender = '" + str(gender)+ "' WHERE id =" + str(id)

	conn.execute(query)
	conn.commit()
	conn.close()

# load thu vien
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

cap = cv2.VideoCapture(0)

# insert vao db
id = input("Nhap id: ")
name = input("Nhap ten: ")
age = input("Nhap tuoi: ")
gender = input("Nhap gioi tinh: ")

insertOrUpdate(id, name, age, gender)

num = 0

while(True):

	ret, frame = cap.read()

	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

	faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)

	for(x, y, w, h) in faces:

		cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 225, 0), 2)

        # tao folder luu anh vua cat duoc tu hinh vuong
		if not os.path.exists('facedata'):
			os.makedirs('facedata')

		num += 1

        # lu anh vao folder facedata
		cv2.imwrite('facedata/User.'+str(id)+'.'+str(num)+'.jpg', gray[y: y+h, x: x+w])

	cv2.imshow('frame', frame)
	cv2.waitKey(1)

	if num > 100:
		break

cap.release()
cv2.destroyAllWindows()

exec(open('trainData.py').read())


#396. Rotate Function
'''Given an array of integers A and let n to be its length.
Assume Bk to be an array obtained by rotating the array A k positions clock-wise,
we define a "rotation function" F on A as follow:
F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].
Calculate the maximum value of F(0), F(1), ..., F(n-1).
'''

'''
F(k) - F(k-1) = Bk[1] + Bk[2] + ... + Bk[n-1] + (1-n)Bk[0]
              = (Bk[0] + ... + Bk[n-1]) - nBk[0]
              = sum - nBk[0]
'''

def maxRotateFunction(A):
    s = sum(A)
    n = len(A)
    F = sum([A[i]*i for i in range(n)])
    maxF = F
    for i in range(1,n):
        F = F+s-n*A[n-i]
        maxF = max(F,maxF)
    return maxF


import os
import math
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.vq import *

class Utility:
  def clear():
    os.system('cls' if os.name == 'nt' else 'clear')

class EdgeDetection:
  def showResult(nrow = None, ncol = None, res_stack = None):
    plt.figure(figsize=(12, 12))

    for i, (label, image) in enumerate(res_stack):
      plt.subplot(nrow, ncol, i + 1)
      plt.imshow(image, cmap='gray')
      plt.title(label)
      plt.axis('off')

    plt.show()

  def start():
    image = cv2.imread('./Edge Detection/rickroll.jpg')
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    laplace8u = cv2.Laplacian(gray_image, cv2.CV_8U)
    laplace16s = cv2.Laplacian(gray_image, cv2.CV_16S)
    laplace32f = cv2.Laplacian(gray_image, cv2.CV_32F)
    laplace64f = cv2.Laplacian(gray_image, cv2.CV_64F)

    laplace_labels = ['8U', '16S', '32F', '64F']
    laplace_images = [laplace8u, laplace16s, laplace32f, laplace64f]

    EdgeDetection.showResult(2, 2, zip(laplace_labels, laplace_images))

class ShapeDetection:
  sides_to_shape = {
    '3': 'Triangle',
    '4': 'Rectangle',
    '5': 'Pentagon',
    '6': 'Hexagon',
    '7': 'Heptagon',
    '8': 'Octagon',
    '9': 'Nonagon',
    '10': 'Decagon'
  }

  def start():
    image = cv2.imread('./Shape Detection/shapes.png')
    new_image = cv2.resize(image, (800, 400))
    new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)

    _, threshold = cv2.threshold(new_image, 127, 255, cv2.THRESH_BINARY)
    contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    first = False
    for contour in contours:
      if first == False:
        first = True
        continue

      approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
      M = cv2.moments(contour)
      x = int(M['m10'] / M['m00'])
      y = int(M['m01'] / M['m00'])

      if (len(approx) >= 3 and len(approx) <= 10):
        cv2.putText(new_image, ShapeDetection.sides_to_shape[str(len(approx))], (x, y), cv2.FONT_HERSHEY_PLAIN, 0.6, (0, 0, 0))
      else:
        area = cv2.contourArea(contour)
        perimeter = cv2.arcLength(contour, True)
        circularity = 4 * math.pi * area / (perimeter * perimeter)
        if circularity > 0.8:
          cv2.putText(new_image, 'Circle', (x, y), cv2.FONT_HERSHEY_PLAIN, 0.6, (0, 0, 0))

    cv2.imshow('Shape Detection', new_image)
    cv2.waitKey()

class PatternDetection:
  def start():
    classifier = cv2.CascadeClassifier('./Pattern Recognition/haarcascade_frontalface_default.xml')

    train_path = './Pattern Recognition/train'
    tdir = os.listdir(train_path)

    face_list = []
    class_list = []

    for idx, train_dir in enumerate(tdir):
      for image_path in os.listdir(f'{train_path}/{train_dir}'):
        path = f'{train_path}/{train_dir}/{image_path}'
        gray = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        faces = classifier.detectMultiScale(gray, scaleFactor = 1.2, minNeighbors = 5)

        if len(faces) < 1:
          continue
        else:
          for face_rect in faces:
            x, y, w, h = face_rect
            face_image = gray[y:y+w, x:x+h]
            face_list.append(face_image)
            class_list.append(idx)

    face_recognizer = cv2.face.LBPHFaceRecognizer_create()
    face_recognizer.train(face_list, np.array(class_list))

    test_path = './Pattern Recognition/test'

    for path in os.listdir(test_path):
      fullpath = f'{test_path}/{path}'
      image = cv2.imread(fullpath)
      igray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

      faces = classifier.detectMultiScale(igray, scaleFactor = 1.2, minNeighbors = 5)

      if len(faces) < 1:
        continue

      for face_rect in faces:
        x, y, w, h = face_rect
        face_image = igray[y:y+w, x:x+h]
        res, conf = face_recognizer.predict(face_image)

        conf = math.floor(conf * 100) / 100
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 1)

        image_text = f'{tdir[res]} : {str(conf)}%'

        cv2.putText(image, image_text, (x, y - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 0, 0), 1)
        cv2.imshow('Result', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    cv2.destroyAllWindows()

class FaceDetectionAndRecognition:
  def start():
    face_cascade = cv2.CascadeClassifier('./Face Recognition/haarcascade_frontalface_default.xml')
    cap = cv2.VideoCapture(0)

    while True:
      ret, frame = cap.read()
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

      for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
        detected = frame[y:y + h, x: x + w]
        detected = cv2.GaussianBlur(detected, (23, 23), 30)
        frame[y:y + h, x: x + w] = detected

      cv2.imshow('Face Detection', frame)

      if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    cv2.imwrite('image.jpg', frame)
    cap.release()
    cv2.destroyAllWindows()

class MenuFacade:
  def displayMenu():
    print('Computer Vision Qualification')
    print('=============================')
    print('1. Edge Detection')
    print('2. Shape Detection')
    print('3. Pattern Recognition')
    print('4. Face Detection')
    print('5. Exit')

  def exitScreen():
    Utility.clear()
    input('Thank You! Press Enter to Exit')
    exit(0)

  def route(input):
    if (input == '1'):
      EdgeDetection.start()
    elif (input == '2'):
      ShapeDetection.start()
    elif (input == '3'):
      PatternDetection.start()
    elif (input == '4'):
      FaceDetectionAndRecognition.start()
    elif (input == '5'):
      MenuFacade.exitScreen()

while (True):
  Utility.clear()
  MenuFacade.displayMenu()
  option = input('>> ')
  MenuFacade.route(option)


import csv

jobs=[{'name':'IU','company':'loen','location':'kr','popularity':'new'},
{'name':'IU','company':'EDAM','location':'world','popularity':'celebrity'}]

'''
open: 파일을 열어준다, 없으면 생성해서 연다.
mode:
  'w': 쓰기전용, 파일이 존재하면 내용을 비우고 연다.
  'r': 읽기전용, default
  'x': 새로운 파일을 생성, 이미 존재한다면 IOError 예외
  'a': Append모드, 파일이 존재하지 않으면 새로 생성(존재하면?)
  '+': 읽고 쓰기용으로 열기
  'b': binary mode로 열기
  't': text mode로 열기, default
'''

def save_to_file(jobs):
  file=open('jobs.csv',mode='w')
  writer=csv.writer(file)
  writer.writerow(["job","company","location","link"])
  for job in jobs:
    writer.writerow(job.values())
  print(file)

save_to_file(jobs)


import sys
import pkg_resources

f = open("my_version.txt", 'w')
f.write(sys.version)
for d in pkg_resources.working_set:
    line = d.project_name + ' = ' + str(d.version) + "\n"
    f.write(line)
f.close()

print("Done")


import asyncio
import websockets

# Define the server's IP address and port
HOST = "localhost"
PORT = 8000

# Define the WebSocket server handler
async def server_handler(websocket, path):
    try:
        # Receive messages from the client
        async for message in websocket:
            print("Received message from client:", message)

            # Send the received message back to the client
            await websocket.send(message)

    except (websockets.exceptions.ConnectionClosedError, websockets.exceptions.ConnectionClosedOK) as e:
        print("WebSocket connection closed:", str(e))

# Start the WebSocket server
async def start_server():
    server = await websockets.serve(server_handler, HOST, PORT)
    print("WebSocket server started.")

    # Run the event loop to start the server
    await server.wait_closed()

# Create and run a new event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(start_server())

# import asyncio
# import websockets

# # Define the server's IP address and port
# HOST = "localhost"
# PORT = 8000

# # Define the WebSocket server handler
# async def server_handler(websocket, path):
#     try:
#         # Receive messages from the client
#         async for message in websocket:
#             print("Received message from client:", message)

#             # Process the message as needed
#             # ...

#             # Send a response back to the client
#             response_message = "Message received successfully!"
#             await websocket.send(response_message)

#     except websockets.exceptions.ConnectionClosedError:
#         print("WebSocket connection closed.")

# # Start the WebSocket server
# start_server = websockets.serve(server_handler, HOST, PORT)

# # Run the event loop to start the server
# asyncio.get_event_loop().run_until_complete(start_server)
# asyncio.get_event_loop().run_forever()

# import asyncio
# import websockets

# # Define the server's IP address and port
# HOST = "localhost"
# PORT = 8000

# # Define the WebSocket server handler
# async def server_handler(websocket, path):
#     # Receive messages from the client
#     async for message in websocket:
#         print("Received message:", message)

#         # Process the message as needed
#         # ...

#         # Send a response back to the client
#         response_message = "Message received successfully!"
#         await websocket.send(response_message)

# # Start the WebSocket server
# start_server = websockets.serve(server_handler, HOST, PORT)

# # Run the event loop to start the server
# asyncio.get_event_loop().run_until_complete(start_server)
# asyncio.get_event_loop().run_forever()

# import http.server
# import socketserver
# import json

# # Define the server's IP address and port
# HOST = "localhost"
# PORT = 8000

# # Create a custom handler by extending the BaseHTTPRequestHandler
# class MyHandler(http.server.BaseHTTPRequestHandler):
#     def do_GET(self):
#         # Customize the response if needed
#         # For example, you can modify headers or process the request data
#         # before sending the response.
#         print("Received GET request")
#         self.send_response(200)
#         self.send_header("Content-type", "text/plain")
#         self.end_headers()
#         response_message = "GET request received successfully!"
#         self.wfile.write(response_message.encode("utf-8"))

#     def do_POST(self):
#         content_length = int(self.headers["Content-Length"])
#         post_data = self.rfile.read(content_length)

#         try:
#             # Convert the received data to a Python object
#             message_data = json.loads(post_data.decode("utf-8"))
#             message = message_data["message"]

#             # Process the message as needed
#             print("Received message:", message)

#             # Send a response back to the client
#             self.send_response(200)
#             self.send_header("Content-type", "text/plain")
#             self.end_headers()
#             response_message = "Message received successfully!"
#             self.wfile.write(response_message.encode("utf-8"))

#         except json.JSONDecodeError:
#             self.send_response(400)
#             self.send_header("Content-type", "text/plain")
#             self.end_headers()
#             response_message = "Invalid JSON data received!"
#             self.wfile.write(response_message.encode("utf-8"))

# # Create a server instance with the custom handler
# server = socketserver.TCPServer((HOST, PORT), MyHandler)

# # Start the server
# print(f"Server started on http://{HOST}:{PORT}")
# server.serve_forever()


# coding:utf8

from pyspark import SparkConf, SparkContext

if __name__ == '__main__':
    conf = SparkConf().setAppName("test").setMaster("local[*]")
    sc = SparkContext(conf=conf)

    rdd = sc.parallelize([1, 3, 2, 4, 7, 9, 6], 3)

    # rdd有几个分区, 就会写几个文件part-00000 .. part-0000N
    # 原因是因为, 这些文件有executor直接写. 不是通过driver写.
    # warn: 目录必须不存在!
    rdd.saveAsTextFile("../data/output/out1")


"""Yolo v3 detection script.

Saves the detections in the `detection` folder.

Usage:
    python detect.py <images/video> <iou threshold> <confidence threshold> <filenames>

Example:
    python detect.py images 0.5 0.5 data/images/dog.jpg data/images/office.jpg
    python detect.py video 0.5 0.5 data/video/shinjuku.mp4

Note that only one video can be processed at one run.
"""
## TODO:
    # add dress path in the input
    # add in the application
            # detect if img or video
            # set dress path
            # call main(images/video, personPath)

## billboard TODO:
    # try with video input
    # cut video in pictures

import tensorflow as tf
import sys
import cv2

# from yoloV3.yolo_v3 import Yolo_v3
# from yoloV3.utils import load_images, load_class_names, draw_boxes, draw_frame

from yoloV3.yolo_v3 import Yolo_v3
from yoloV3.utils import load_images, load_class_names, draw_boxes, draw_frame

sys.version

import numpy as np
import os
import matplotlib.pyplot as plt
from PIL import Image,ImageOps

import itertools

tf.compat.v1.disable_eager_execution()

_MODEL_SIZE = (416, 416)
_CLASS_NAMES_FILE = './yoloV3/data/labels/obj.names'
_MAX_OUTPUT_SIZE = 20

class videoConverter:
    inPath = ''
    outPath = ''
    picAmt = 0

    def countFrames():
        FPS = 25
        cap = cv2.VideoCapture(videoConverter.inPath)
        cap.set(cv2.CAP_PROP_FPS, FPS)

        currentFrame = 0
        while (True):
            ret, frame = cap.read()
            if (not ret):
                break
            else:
                currentFrame += 1
        cap.release()
        cv2.destroyAllWindows()
        return currentFrame

    def splitVid():
        framesN = videoConverter.countFrames() // videoConverter.picAmt
        framesAdd = 0
        ImgName = 1
        currentFrame = 0

        FPS = 25
        cap = cv2.VideoCapture(videoConverter.inPath)
        cap.set(cv2.CAP_PROP_FPS, FPS)

        while (True):
            ret, frame = cap.read()
            if (not ret):
                break
            else:
                if(currentFrame == framesAdd):
                    name = videoConverter.outPath + str(ImgName) + '.png'
                    cv2.imwrite(name, frame)
                    framesAdd += framesN
                    ImgName += 1
                    currentFrame += 1
        cap.release()
        cv2.destroyAllWindows()

    def videoList():
        videoConverter.splitVid()
        imageList = []
        for picture in range(videoConverter.picAmt):
            imageList.append(videoConverter.outPath + str(picture + 1) + '.png')
        return imageList

    def preparelists(picAmt, cInput, cOutput, pInput, pOutput):
        videoConverter.picAmt = picAmt

        print('converting clothing video into images...')

        videoConverter.inPath = cInput
        videoConverter.outPath = cOutput
        clothList = videoConverter.videoList()

        print('converting your video into images...')

        videoConverter.inPath = pInput
        videoConverter.outPath = pOutput
        personList = videoConverter.videoList()

        return clothList, personList

class combineImages:
    clothingImg  = ''
    imagePath = ''
    err_mar = 0

    def makeBlack(clothingImg, row, p):
        clothingImg[row][p][0] = 0.
        clothingImg[row][p][1] = 0.
        clothingImg[row][p][2] = 0.
        clothingImg[row][p][3] = 1.
        return clothingImg

    def colorCheck(rgb, clothingImgp):
        for x in range(4):
            if clothingImgp[x] >= rgb[x] - 1*combineImages.err_mar and  clothingImgp[x] <= rgb[x] + 1*combineImages.err_mar:
                return True
            else:
                return False

    def blackPixelCheck(row, p):
        if combineImages.clothingImg[row][p][0] == 0. and combineImages.clothingImg[row][p][1] == 0. and combineImages.clothingImg[row][p][2] == 0. and combineImages.clothingImg[row][p][3] == 1.:
            return True
        else:
            return False

    def prepClothingImg(clothingImg):

        plt.imshow(clothingImg)

        r = clothingImg[0][0][0]
        g = clothingImg[0][0][1]
        b = clothingImg[0][0][2]
        a = clothingImg[0][0][3]

        rgb = [r, g, b, a]

        clothingImg[0][0][0] = 0.
        clothingImg[0][0][1] = 0.
        clothingImg[0][0][2] = 0.
        clothingImg[0][0][3] = 0.

        rown = -1
        ppcounter = -1

        top = 1000000000
        right = 0
        left = 1000000000
        bottom = 0
        topCon = True

        for row in range(len(clothingImg)):
            for p in range(len(clothingImg[row])):
                if p != 0:
                    if combineImages.blackPixelCheck(row - 1, p) or combineImages.blackPixelCheck(row, p - 1):
                        if combineImages.colorCheck(rgb, clothingImg[row][p]):
                            clothingImg = combineImages.makeBlack(clothingImg, row, p)
                else:
                    if row != 0:
                        if combineImages.blackPixelCheck(row - 1, p):
                            if combineImages.colorCheck(rgb, clothingImg[row][p]):
                                clothingImg = combineImages.makeBlack(clothingImg, row, p)

            for p in range(len(clothingImg[row])):
                if combineImages.colorCheck(rgb, clothingImg[row][p]):
                    if not(combineImages.blackPixelCheck(row, p) and combineImages.blackPixelCheck(row, p + 1)):
                        clothingImg = combineImages.makeBlack(clothingImg, row, p)
                        for pn in range(p, 0, -1):
                            if not(combineImages.blackPixelCheck(row, p)) and combineImages.blackPixelCheck(row, p + 1):
                                clothingImg = combineImages.makeBlack(clothingImg, row, p)

        for row in range(len(clothingImg)):
            for p in range(len(clothingImg[row])):
                try:
                    if row != 0:
                        if not(combineImages.blackPixelCheck(row, p)) and combineImages.blackPixelCheck(row - 1, p):
                            if row < top:
                                top = row
                    if row != 0:
                        if not(combineImages.blackPixelCheck(row, p)) and combineImages.blackPixelCheck(row, p - 1):
                            if p < left:
                                left = p
                    if not(combineImages.blackPixelCheck(row, p)) and combineImages.blackPixelCheck(row, p + 1):
                        if p > right:
                            right = p
                    if not(combineImages.blackPixelCheck(row, p)) and combineImages.blackPixelCheck(row + 1, p):
                        if row > bottom:
                            bottom = row
                except:
                    pass

        croppedclothingImg = clothingImg[top:bottom, left:right, :]

        fig = plt.imshow(croppedclothingImg)
        plt.axis('off')
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.savefig(combineImages.imagePath + 'steps/dressTest.png', bbox_inches='tight', pad_inches = 0)

    def combineImg(input_names, clothList):
        print('detecting body...')
        iou_threshold = 0.5
        confidence_threshold = 0.1
        class_names = load_class_names(_CLASS_NAMES_FILE)
        n_classes = len(class_names)

        model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE,
                        max_output_size=_MAX_OUTPUT_SIZE,
                        iou_threshold=iou_threshold,
                        confidence_threshold=confidence_threshold)

        batch_size = len(input_names)
        batch = load_images(input_names, model_size=_MODEL_SIZE)
        inputs = tf.compat.v1.placeholder(tf.float32, [batch_size, *_MODEL_SIZE, 3])
        detections = model(inputs, training=False)
        saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables(scope='yolo_v3_model'))

        with tf.compat.v1.Session() as sess:
            saver.restore(sess, './yoloV3/weights/model.ckpt')

            detection_result = sess.run(detections, feed_dict={inputs: batch})
        draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE)
        personArr = draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE)

        nameCounter = 1
        imgResultList = []
        print('combining images...')
        for x in range(len(personArr)):

            combineImages.clothingImg = plt.imread(clothList[x])
            combineImages.prepClothingImg(combineImages.clothingImg)

            personImg = Image.open(input_names[x])
            preppedClothingImg = Image.open(combineImages.imagePath + 'steps/dressTest.png')

            preppedClothingImg = preppedClothingImg.convert("RGBA")
            datas = preppedClothingImg.getdata()

            newData = []
            for item in datas:
                if item[0] == 0 and item[1] == 0 and item[2] == 0:
                    newData.append((1, 1, 1, 0))
                else:
                    newData.append(item)

            preppedClothingImg.putdata(newData)

            try:
                # person=0 body=1 legs=2 head=3
                bodyLeft = (personArr[x][1][0] - personArr[x][0][0]) // 3
                bodyRight = (personArr[x][0][2] - personArr[x][1][2]) // 3

                height = (personArr[x][1][3] - personArr[x][1][1]) * 2
                width = (personArr[x][1][2] - personArr[x][1][0])

                PreppedClothingImg = preppedClothingImg.resize((int(width), int(height)))

                left = personArr[x][1][0]

                try:
                    top = personArr[x][1][1] - ((personArr[x][1][1] - personArr[x][3][3]))
                except:
                    top = personArr[x][1][1] - ((personArr[x][1][3] - personArr[x][1][1]) // 20)

                backimg = personImg.copy()
                backimg.paste(PreppedClothingImg, (int(left), int(top)), mask=PreppedClothingImg)
                returnPath = combineImages.imagePath + 'result/combinedImg' + str(nameCounter) + '.png'
                backimg.save(returnPath)
                nameCounter += 1
                return returnPath
            except:
                print('could not locate body')
        print('saved')

    def getCombination(pInp, cInp):
        q, ext = pInp.split('.')
        if ext == 'mp4':
            # inputs = amt of wanted pictures out of vid/ video paths for clothing(c) and person(p)/ output path for images
            picAmt = 10
            cInpath = cInp
            cOutPath = combineImages.imagePath + 'dress/'
            pInPath = pInp
            pOutPath = combineImages.imagePath + 'person/'

            cList, pList = videoConverter.preparelists(picAmt, cInpath, cOutPath, pInPath, pOutPath)
            # outputs = output of all clothing images (cList)/ output for all person images (pList)
        else:
            im_rgb = Image.open(cInp)
            im_rgba = im_rgb.copy()
            im_rgba.putalpha(255)
            im_rgba.save('./images/steps/transformed.png')

            pList = [pInp]
            cList = ['./images/steps/transformed.png']

        combineImages.imagePath = './images/'
        combineImages.err_mar = 0.055
        return combineImages.combineImg(pList, cList)

#white


"""like2

Revision ID: f2dad65d8839
Revises: 26fd16a11708
Create Date: 2018-04-22 11:16:54.692216

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = 'f2dad65d8839'
down_revision = '26fd16a11708'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('video', 'like')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('video', sa.Column('like', mysql.BIGINT(display_width=20), autoincrement=False, nullable=True))
    # ### end Alembic commands ###