import heapq
import random
import time
from typing import Any, Callable, Iterator, List, NamedTuple, Set, Tuple

from ..core.models import Groups, UserKey

Genome = List[List[UserKey]]
Rating = float
RatingFunc = Callable[[Genome, Any], Rating]

class RatedGenome(NamedTuple):
    """A rating together with its genome."""

    rating: Rating
    genome: Genome

Population = List[RatedGenome]

def build_genome(users: List[UserKey], sizes: List[int]) -> Genome:
    """Build new genome from list of users."""
    genome = []
    pos = 0
    for size in sizes:
        genome.append(users[pos:pos+size])
        pos += size
    return genome

def iter_genome_count(genome: Genome, count: int) -> Iterator[UserKey]:
    """Iterate over all members until number of member is reached."""
    for group in genome:
        for member in group:
            if count < 1:
                return
            yield member
            count -= 1

def iter_genome_set(genome: Genome, ignore_members: Set[UserKey]) -> Iterator[UserKey]:
    """Iterate over all members that will not be ignored."""
    for group in genome:
        for member in group:
            if member not in ignore_members:
                yield member

def crossover_genomes(
        genome_1: Genome, genome_2: Genome, user_count: int) -> Tuple[bool, Genome]:
    """Combine two genomes to a new one."""
    if genome_1 == genome_2 or len(genome_1) < 2:
        return (False, genome_1)
    split_pos = random.randint(1, user_count - 1)  # nosec
    users = list(iter_genome_count(genome_1, split_pos))
    users.extend(iter_genome_set(genome_2, set(users)))
    genome = build_genome(users, [len(group) for group in genome_1])
    return (True, genome)

def mutate_genome(genome: Genome) -> Genome:
    """Return a new genome value that is a permutation of the given one."""
    group_1 = random.randint(0, len(genome) - 1)  # nosec
    group_2 = random.randint(0, len(genome) - 1)  # nosec
    if group_1 == group_2:
        group_2 = (group_1 + 1) % len(genome)

    pos_1 = random.randint(0, len(genome[group_1]) - 1)  # nosec
    user_1 = genome[group_1][pos_1]
    pos_2 = random.randint(0, len(genome[group_2]) - 1)  # nosec
    user_2 = genome[group_2][pos_2]
    new_genome = []
    for number, group in enumerate(genome):
        if number == group_1:
            new_group = list(group)
            new_group[pos_1] = user_2
            new_genome.append(new_group)
        elif number == group_2:
            new_group = list(group)
            new_group[pos_2] = user_1
            new_genome.append(new_group)
        else:
            new_genome.append(group)
    return new_genome

def build_initial_population_genomes(
        population_size: int, users: List[UserKey], sizes: List[int]) -> List[Genome]:
    """Build an initial population of genomes."""
    user_list = list(users)
    result = []
    for _ in range(population_size):
        random.shuffle(user_list)
        result.append(build_genome(user_list, sizes))
    return result

def build_initial_population(
        population_size: int, users: List[UserKey], sizes: List[int],
        rating_func: RatingFunc, rating_data: Any) -> Population:
    """Build an initial population, ordered by rating."""
    genome_list = build_initial_population_genomes(population_size, users, sizes)
    population = [
        RatedGenome(rating_func(genome, rating_data), genome)
        for genome in genome_list]
    heapq.heapify(population)
    return population

def mutate_population(
        population: Population, num_mutation: int) -> List[Tuple[Genome, Rating]]:
    """Perform a number of mutations on given population."""
    result = []
    for _ in range(num_mutation):
        rated_genome = random.choice(population)  # nosec
        mutated_genome = mutate_genome(rated_genome.genome)
        result.append((mutated_genome, rated_genome.rating))
    return result

def crossover_population(
        population: Population, num_crossover: int, user_count: int) -> List[Genome]:
    """Perform a number of crossover operations on given population."""
    result = []
    for _ in range(num_crossover):
        parent_genomes = random.sample(population, 2)
        gave_birth, child_genome = crossover_genomes(
            parent_genomes[0].genome, parent_genomes[1].genome, user_count)
        if gave_birth:
            result.append(child_genome)
    return result

def reduce_population(population: Population, population_size: int) -> None:
    """Reduce the population to be of given size."""
    while len(population) > population_size:
        genome_1_pos = random.randint(0, len(population) - 1)  # nosec
        genome_2_pos = random.randint(0, len(population) - 1)  # nosec
        if genome_1_pos == genome_2_pos:
            continue

        if population[genome_1_pos].rating < population[genome_2_pos].rating:
            del population[genome_2_pos]
        else:
            del population[genome_1_pos]

def trivial_groups(users: List[UserKey], num_groups) -> Groups:
    """There is only one groups, so the result is the trivial one."""
    result = [frozenset(users)]
    while len(result) < num_groups:
        result.append(frozenset())
    return tuple(result)

def best_groups(population: Population, num_groups: int) -> Groups:
    """Select the best genome and produces resulting groups."""
    result = [frozenset(group) for group in population[0].genome]
    while len(result) < num_groups:
        result.append(frozenset())
    return tuple(result)

class StopStrategy:  # pylint: disable=too-many-instance-attributes
    """Encapsulate the descision when to stop the algorithm."""

    def __init__(
            self, initial_rating: float,
            max_rounds: int, max_best_rounds: int, max_seconds: float):
        """Initialize the object."""
        self.initial_rating: float = initial_rating
        self.max_rounds: int = max_rounds
        self.max_best_rounds: int = max_best_rounds
        self.max_seconds: float = max_seconds
        self.start()

    def start(self) -> None:
        """Signal that algorithm starts."""
        self.best_rating: float = self.initial_rating
        self.best_rounds: int = 0
        self.rounds: int = 0
        self.time_start: float = time.monotonic()

    def should_continue(self, rating: float) -> bool:
        """Return True if algorithm should continue its work."""
        self.rounds += 1
        if self.max_rounds <= self.rounds:
            return False
        if rating < self.best_rating:
            self.best_rating = rating  # pylint: disable=attribute-defined-outside-init
            self.best_rounds = 0  # pylint: disable=attribute-defined-outside-init
        else:
            self.best_rounds += 1
            if self.max_best_rounds < self.best_rounds:
                return False
        return (time.monotonic() - self.time_start) < self.max_seconds

def generic_genetic_policy(
        users: List[UserKey], sizes: List[int],
        rating_func: RatingFunc, rating_data: Any,
        stop_strategy: StopStrategy) -> Groups:
    """Calculate a group that suits the rating at best."""
    num_groups = len(sizes)
    while sizes[-1] < 1:
        sizes = sizes[:-1]
    if len(sizes) < 2:
        return trivial_groups(users, num_groups)

    population_size = min(100, len(sizes) * len(users) * 2)

    population = build_initial_population(
        population_size, users, sizes, rating_func, rating_data)

    num_crossover = len(users)
    num_mutation = len(users) + population_size

    stop_strategy.start()
    while stop_strategy.should_continue(population[0].rating):
        for genome in crossover_population(population, num_crossover, len(users)):
            population.append(RatedGenome(rating_func(genome, rating_data), genome))

        successful_mutations = 0
        for genome, old_rating in mutate_population(population, num_mutation):
            new_rating = rating_func(genome, rating_data)
            if new_rating < old_rating:
                successful_mutations += 1
            population.append(RatedGenome(new_rating, genome))

        reduce_population(population, population_size)
        heapq.heapify(population)

    return best_groups(population, num_groups)


import matplotlib.pyplot as plt
import numpy as np
import torch

import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm

import torchvision.datasets as datasets

import argparse
import os

from torch.utils.tensorboard import SummaryWriter

parser = argparse.ArgumentParser(description='')
parser.add_argument('--epochs', default=300, type=int, metavar='N',
                    help='number of total epochs to run')
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
                    help='number of data loading workers (default: 4)')
parser.add_argument('-b', '--batch-size', default=128, type=int,
                    metavar='N', help='mini-batch size (default: 128)')
parser.add_argument('--lr', '--learning-rate', default=0.01, type=float,
                    metavar='LR', help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
                    help='momentum')
parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float,
                    metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('--modelName', type=str, default="prob1A",
                    help='Name of the desired model')

global args, best_prec1
args = parser.parse_args()

writer = SummaryWriter('runs/' + args.modelName)

class housingDataloader():
    def __init__(self, values, labels, train=True):
        if train:
            self.values = values[:435]
            self.labels = labels[:435]
        else:
            self.values = values[435:]
            self.labels = labels[435:]
    def getLoader(self):
        return zip(self.values, self.labels)

def main():

    from PIL.Image import new
    import numpy as np
    import pandas as pd
    housingDataset = pd.DataFrame(pd.read_csv("data/housing.csv"))
    housingDataset.head()
    trainingVars = ['area', 'bedrooms', 'bathrooms', 'stories', 'parking']

    trainingData = housingDataset[trainingVars]
    labels = housingDataset['price']
    t_u = torch.tensor(np.asarray(trainingData))
    t_c = torch.tensor(np.asarray(labels))

    t_un = t_u * .1
    norm = np.linalg.norm(t_c)
    t_cn = t_c/norm

    print(trainingData.shape)
    print(labels.shape)

    trainLoader = housingDataloader(t_un, t_cn, True)
    valLoader = housingDataloader(t_un, t_cn, False)
    # cudnn.benchmark = True
    # # Define the mean and std. devation for the dataset
    # normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
    #                                  std=[0.229, 0.224, 0.225])

    # Create the model and send it to the GPU
    if args.modelName == "prob1A":
        model = AModel()
    elif args.modelName == "prob1B":
        model = BModel()

    model

    criterion = nn.MSELoss()

    optimizer = torch.optim.SGD(model.parameters(), args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
                                                        milestones=[100,150])

    for epoch in range(1, args.epochs+1, 1):

        # Evaluate the network
        prec1 = validate(valLoader, model, criterion, epoch)

        # train for one epoch
        print('current lr {:.5e}'.format(optimizer.param_groups[0]['lr']))

        train(trainLoader, model, criterion, optimizer, epoch)

        # lr_scheduler.step()

def train(trainLoader, model, criterion, optimizer, epoch):
    # Swap model to train
    model.train()

    losses = AverageMeter()
    top1 = AverageMeter()
    epochAccuracy = []
    epochLoss = []

    for input, target in trainLoader.getLoader():
        # target = target.cuda()
        # input_var = input.cuda()
        input_var = input
        target_var = torch.tensor([target.float()])
        # Get network output
        output = model(input_var)
        loss = criterion(output, target_var)
        # SGD
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        # Make the output a float
        output = output.float()
        loss = loss.float()
        # Calculate accuracy and loss
        prec1 = accuracy(output.data, target)
        losses.update(loss.item(), input.size(0))
        top1.update(prec1, input.size(0))

        epochLoss.append(loss.item())
        epochAccuracy.append(prec1)

    trainLoss = sum(epochLoss)/len(epochLoss)
    trainAccuracy = sum(epochAccuracy)/len(epochAccuracy)
    writer.add_scalar('Housing Training loss', trainLoss, epoch)
    writer.add_scalar('Housing Training Accuracy', trainAccuracy, epoch)
    print('Training Accuracy: {top1.avg:.3f}'.format(top1=top1))

def validate(valLoader, model, criterion, epoch):
    # Swap to evaluation
    model.eval()

    with torch.no_grad():
        losses = AverageMeter()
        top1 = AverageMeter()
        epochAccuracy = []
        epochLoss = []
        for input, target in valLoader.getLoader():
            # target = target.cuda()
            # input_var = input.cuda()
            # target_var = target.cuda()
            input_var = input
            target_var = torch.tensor([target.float()])
            # compute output
            output = model(input_var)

            loss = criterion(output, target_var)
            output = output.float()
            loss = loss.float()
            # measure accuracy and record loss
            prec1 = accuracy(output.data, target)
            losses.update(loss.item(), input.size(0))
            top1.update(prec1, input.size(0))

            epochLoss.append(loss.item())
            epochAccuracy.append(prec1)

        valLoss = sum(epochLoss)/len(epochLoss)
        valAccuracy = sum(epochAccuracy)/len(epochAccuracy)

        print('Validation Accuracy: {top1.avg:.3f}'.format(top1=top1))
        writer.add_scalar('Housing Validation loss', valLoss, epoch)
        writer.add_scalar('Housing Validation Accuracy', valAccuracy, epoch)

    return top1.avg

class AModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(5, 8)
        self.fc2 = nn.Linear(8, 1)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = self.fc2(x)

        return x

class BModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(5, 8)
        self.fc2 = nn.Linear(8, 16)
        self.fc3 = nn.Linear(16, 4)
        self.fc4 = nn.Linear(4, 1)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = self.fc4(x)

        return x

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def accuracy(output, target):

    if output == 0:
        res = 0
    else:
        res = abs(abs((target-output)/output)-1)

    return float(res)

if __name__ == '__main__':
    main()


from contextlib import contextmanager
import logging
import os
import pytest
from greentype import core

core.TEST_MODE = True
core.LOG.setLevel(logging.DEBUG)

TEST_ROOT = os.path.dirname(__file__)
TEST_DATA_ROOT = os.path.join(TEST_ROOT, 'test_data')

class TestAnalyzer(core.GreenTypeAnalyzer):
    def __init__(self, target_path):
        super(TestAnalyzer, self).__init__(target_path)
        self.config['FOLLOW_IMPORTS'] = True
        self._inferred_types = False

    def assert_resolved(self, target_module, local_name, real_name):
        module = self.index_module(path=target_module)
        assert module is not None
        resolved = self.resolve_name(local_name, module)
        assert resolved is not None
        assert resolved.qname == real_name

    def invalidate_indexes(self):
        super(TestAnalyzer, self).invalidate_indexes()
        self._inferred_types = False

    def infer_parameter_types(self):
        super(TestAnalyzer, self).infer_parameter_types()
        self._inferred_types = True

    def assert_inferred(self, param_name, class_names):
        if not self._inferred_types:
            self.infer_parameter_types()

        param = self.indexes['PARAMETER_INDEX'][param_name]
        assert set(class_names) == set(c.qname for c in param.suggested_types)

    @contextmanager
    def roots(self, *roots):
        if not roots:
            roots = [os.getcwd()]
        old_roots = self.config['SOURCE_ROOTS']
        self.config['SOURCE_ROOTS'] = list(map(os.path.abspath, roots))
        old_cwd = os.getcwd()
        os.chdir(roots[0])
        try:
            yield
        finally:
            os.chdir(old_cwd)
            self.config['SOURCE_ROOTS'] = old_roots

@pytest.fixture(autouse=True)
def in_test_data_dir(request):
    old_cwd = os.getcwd()
    test_data_dir = getattr(request.module, 'TEST_DATA_DIR', None)
    if test_data_dir:
        os.chdir(os.path.join(TEST_DATA_ROOT, test_data_dir))
        request.addfinalizer(lambda: os.chdir(old_cwd))

@pytest.fixture()
def analyzer():
    return TestAnalyzer(os.getcwd())


import re
import numpy as np
from typing import Iterable

from kaldi_tflite.lib.io import KaldiObjReader

class KaldiNnet3Reader(KaldiObjReader):
    """
    Reader for kaldi nnet3 model files. It parses the model file and
    loads the components and their parameters.
    """

    def __init__(self, nnet3_path: str, binary: bool):
        """
        Initializes the KaldiNnet3Reader by reading in the data from the
        nnet3 model file (e.g. final.raw) at the path specified.

        Parameters
        ----------
        path : str
            Path to nnet3 model file.
        binary : bool
            If true, will read the file as a binary file. Otherwise will
            parse the file as a text file.
        """
        super(KaldiNnet3Reader, self).__init__(nnet3_path, binary)

        self.config = []
        self.components = []
        self.read()

    def read(self):
        """
        Reads the loaded data and parses the config and components of
        the nnet3 model along with their parameters.

        Raises
        ------
        ValueError
            If file is not in the expected format.
        """
        # Token denoting the start of the nnet3 model data.
        self.expectToken("<Nnet3>")

        # Reading nnet3 config section.
        line = self.readLine()
        if line.strip() != "":
            raise ValueError("expected model config following <Nnet3> token, got blank line")
        self.readConfigLines()

        # Reading number of components in the model.
        self.expectToken("<NumComponents>")
        numComponents = self.readInt()

        assert numComponents > 0 and numComponents < 100000, \
            f"expected between 1 and 9999 components, got {numComponents}"

        # Reading the components themselves.
        self.components = []
        for i in range(numComponents):
            self.expectToken("<ComponentName>")
            # Reading component name and type.
            compName = self.readToken()
            compType = self.readToken()

            # Reading component data.
            component = {}
            component["name"] = compName
            component["type"] = compType
            component.update(self.readComponent(compType))

            # Adding to component list.
            self.components.append(component)

        self.expectToken("</Nnet3>")

    def readConfigLines(self) -> list:
        """
        Reads the lines specifying the nnet3 model config at the start
        of the model file, just after the <Nnet3> tag. The read pointer
        incremented to just after end of the config section.

        Returns
        -------
        list
            Lines containing the nnet3 model config.
        """
        self.config = []
        line = self.readLine().strip()
        while line != "":
            self.config.append(line)
            line = self.readLine().strip()

    def readComponent(self, compType: str) -> dict:
        """
        Reads the component data of the given component type. Increments
        the read pointer to just before the next component.

        Parameters
        ----------
        compType : str
            Component type to read.

        Returns
        -------
        dict
            Component data read from the data stream.
        """
        # Tokens that indicate the end of this component in the model data.
        closingTok = "</" + compType[1:]
        closingTokens = {closingTok, '<ComponentName>'}

        # Getting format of the component and the read funcs to call for each
        # of its sub parts.
        compFormat = self.getComponentFormat(compType)

        # Reading component data.
        compData = {}

        if len(compFormat) == 0:
            return compData

        for (token, readFunc, key) in compFormat:
            if self.expectToken(token, closingTokens):
                compData[key] = readFunc()
            else:
                print(f"  - failed to find token {token}")

        return compData

    def getComponentFormat(self, compType: str) -> list:
        """
        Based on the given component type, this method returns a list of
        tuples (token, read_function, dict_key) where:

          - 'token' is the name of the token to expect.

          - 'read_function' is the function we should use to read the data
            following the token.

          -'dict_key' is the key under which the read data should stored in.

        For instance, an entry in the list may be:
            ('<ParameterMatrix>', self.readMat, 'params')

        This is adapted from the following files in the kaldi repository:
          - `egs/wsj/s5/steps/nnet3/report/convert_model.py`
          - `src/nnet3/nnet-simple-component.cc`
          - `src/nnet3/nnet-normalize-component.cc`

        Parameters
        ----------
        compType : str
            Component type to get the format for.

        Raises
        ------
        ValueError
            If component type does not have a format specified.

        Returns
        -------
        list
            List of tuples (token, read_function, dict_key) corresponding
            to the given component type.
        """
        comp = self.stripTagsAndSuffix(compType, suffix="Component")

        if comp in {'Sigmoid', 'Tanh', 'RectifiedLinear', 'Softmax', 'LogSoftmax', 'NoOp'}:
            return [
                ('<Dim>', self.readInt, 'dim'),
                #  ('<BlockDim>', self.readInt, 'block-dim'),
                ('<ValueAvg>', self.readVec, 'value-avg'),
                ('<DerivAvg>', self.readVec, 'deriv-avg'),
                ('<Count>', self.readDouble, 'count'),
                ('<OderivRms>', self.readVec, 'oderiv-rms'),
                ('<OderivCount>', self.readDouble, 'oderiv-count'),
            ]

        if comp in {'Affine', 'NaturalGradientAffine'}:
            return [
                ('<LinearParams>', self.readMat, 'params'),
                ('<BiasParams>', self.readVec, 'bias'),
            ]

        if comp == 'Linear':
            return [('<Params>', self.readMat, 'params')]

        if comp == 'BatchNorm':
            return [
                ('<Dim>', self.readInt, 'dim'),
                ('<BlockDim>', self.readInt, 'block-dim'),
                ('<Epsilon>', self.readFloat, 'epsilon'),
                ('<TargetRms>', self.readFloat, 'target-rms'),
                ('<TestMode>', self.readBool, 'test-mode'),
                ('<Count>', self.readDouble, 'count'),
                ('<StatsMean>', self.readVec, 'stats-mean'),
                ('<StatsVar>', self.readVec, 'stats-var'),
            ]

        # We don't parse any parameters for these components.
        if comp in {"StatisticsExtraction", "StatisticsPooling"}:
            return []

        raise ValueError(f"unsupported component type '{compType}'")

    def stripTagsAndSuffix(self, token: str, suffix: str = "") -> str:
        """
        Strips angle brackets and any specified suffix from the given
        token.

        Parameters
        ----------
        token : str
            Token to strip angle bracket tags and suffix from.
        suffix : str, optional
            Suffix to strip from the token, by default ""

        Returns
        -------
        str
            Cleaned up token.
        """
        # Striping angle brackets.
        if token.startswith("<"):
            token = token.lstrip("<")
        if token.endswith("/>"):
            token = token.rstrip("/>")
        if token.endswith(">"):
            token = token.rstrip(">")

        # Stripping any additional suffixes.
        if len(suffix) > 0:
            if token.endswith(suffix):
                token = token[:len(token) - len(suffix)]

        return token

    def getComponent(self, name: str) -> Iterable[dict]:
        """
        Returns the component data of the components that
        match the given name / pattern.

        Parameters
        ----------
        name : str
            Name of component to lookup. Could also be a regex pattern
            (e.g. "tdnn1.*"). If multiple components match the pattern,
            all their data are concatenated in returned list. The order
            is the same one they appear in the model.

        Returns
        -------
        Iterable[dict]
            Data of components matching given name / pattern.
        """
        matchingComponents = []
        for c in self.components:
            cName = c.get("name", None)
            if cName is None:
                continue
            if re.match(rf"{name}", cName):
                matchingComponents.append(c)

        return matchingComponents

    def getWeights(self, name: str) -> Iterable[np.ndarray]:
        """
        Returns the loaded weights of the component with the given
        name if it has any. If it's a component that does not have
        any weights, such as a "<StatisticsExtractionComponent>",
        an empty list is returned. If the given name does not exist
        in the nnet3 model, an exception is raised.

        Parameters
        ----------
        name : str
            Name of component to return weights for. Could also be
            a regex pattern (e.g. "tdnn1.*"). If multiple components
            match the pattern, all their weights are concatenated in
            the list. The order is the same one they appear in the
            model.

        Returns
        -------
        Iterable[np.ndarray]
            Weights returned as a list of numpy arrays.

        Raises
        ------
        KeyError
            If no components match the given name / pattern.
        """
        matchingComponents = self.getComponent(name)
        if len(matchingComponents) == 0:
            raise KeyError(f"no components with name matching '{name}'")

        weights = []
        for c in matchingComponents:
            t = c["type"]
            if t == "<NaturalGradientAffineComponent>":
                weights.extend([c["params"], c["bias"]])
            elif t == "<BatchNormComponent>":
                weights.extend([c["target-rms"], c["stats-mean"], c["stats-var"]])
            else:
                # Other parsed components don't have weights.
                continue

        return weights


from flask import Flask, redirect,render_template, request,redirect
from datetime import datetime
import logging
import sys
import re
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import random
import string
import nltk
import requests

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)

def greeting_response(text):
  text=text.lower()
  bot_greetings=['howdy','hi','hey','hello','hola']
  user_greetings=['hi','hey','hello','hola','greetings','wassup']
  for word in text.split():
    if word in user_greetings:
      return random.choice(bot_greetings)
def index_sort(list_var):
  length=len(list_var)
  list_index=list(range(0,length))
  x=list_var
  for i in range(length):
    for j in range(length):
      if x[list_index[i]] > x[list_index[j]]:
        temp=list_index[i]
        list_index[i]=list_index[j]
        list_index[j]=temp
  return list_index
def bot_response(user_input,sent_list):
  user_input=user_input.lower()
  sent_list.append(user_input)
  bot_response=''
  cm=CountVectorizer().fit_transform(sent_list)
  similarity_scores=cosine_similarity(cm[-1],cm)
  similarity_scores_list=similarity_scores.flatten()
  index=index_sort(similarity_scores_list)
  index=index[1:]
  response_flag=0
  j=0
  for i in range(len(index)):
    if similarity_scores_list[index[i]]>0.0:
      bot_response=bot_response +' ' +sent_list[index[i]]
      response_flag=1
      j+=1
    if j>2:
      break
  if response_flag==0:
    bot_response=bot_response+' '+"I apologize , I don't understand."
  sent_list.remove(user_input)
  return bot_response
def searching(res,text):
   search1=re.search("\|\|.*\|\|", res)
   if search1:
      ans=(search1.group()).replace("|","")
      search_string="\|\|"+ans+"\|\|.*"+"[["+ans+"]]"
      search2 = re.search(search_string, text)
      if search2:
          msg=search2.group().replace("|","")
          msg=msg.replace("[","")
          msg=msg.replace("]","")
          msg=msg.replace(ans,"")
          return 1,msg
      else:
         return 0,""
   else:
      return 0,""

@app.route("/",methods=['GET','POST'])
def hello_world():
    nltk.download('punkt',quiet=True)
    # url = r'https://raw.githubusercontent.com/shahidul034/KUET-chatbot/main/Software-Project-Data.txt'
    # page = requests.get(url,verify=False)
    # corpus=page.text
    text=open(r"C:\Users\Inception\Desktop\python project dev\uttara chatbot\static\Software-Project-Data.txt","r",encoding="utf-8").read()
    sent_list=nltk.sent_tokenize(text)
    user_input=""
    msg=""
    print('Bot: I am bot. I will answer your queries about uttara university')
    exit_list=['exit','see you later','bye','quit','break']

    if request.method =='POST':
        user_input=(request.form['title'])
        if user_input.lower() in exit_list:
            msg="chat with you later !"
        else:
            if greeting_response(user_input)!=None:
                res=greeting_response(user_input)
                msg=res
            else:
                res=bot_response(user_input,sent_list)
                msg=res
                flag,out=searching(res,text)
                if flag:
                   msg=out
    return render_template("index.html",msg=msg)

if __name__ == "__main__":
    app.run(debug=True)


from resilient_circuits import AppFunctionComponent, app_function, FunctionResult
from fn_mcafee_epo.lib.epo_helper import init_client, PACKAGE_NAME
from resilient_lib import validate_fields

FN_NAME = "mcafee_epo_remove_user"

class FunctionComponent(AppFunctionComponent):
    """Component that implements function 'mcafee_epo_remove_user'"""

    def __init__(self, opts):
        super(FunctionComponent, self).__init__(opts, PACKAGE_NAME)

    @app_function(FN_NAME)
    def _app_function(self, fn_inputs):
        """
        Function: Delete a user from the ePO server
        Inputs:
            -   fn_inputs.mcafee_epo_username
        """

        yield self.status_message(f"Starting App Function: '{FN_NAME}'")

        # Validate parameters
        validate_fields(["mcafee_epo_username"], fn_inputs)

        # Log parameters
        self.LOG.info(str(fn_inputs))

        # Connect to ePO server
        client = init_client(self.opts, self.options)

        response = client.request(
            "core.removeUser",
            {"userName": fn_inputs.mcafee_epo_username}
        )

        yield self.status_message(f"Finished running App Function: '{FN_NAME}'")

        # Produce a FunctionResult with the results
        yield FunctionResult(response)


import re

input_text = '''homEwork:
    tHis iz your homeWork, copy these Text to variable.

    You NEED TO normalize it fROM letter CASEs point oF View. also, create one MORE senTENCE witH LAST WoRDS of each existING SENtence and add it to the END OF this Paragraph.

    it iZ misspeLLing here. fix“iZ” with correct “is”, but ONLY when it Iz a mistAKE.

    last iz TO calculate nuMber OF Whitespace characteRS in this Tex. caREFULL, not only Spaces, but ALL whitespaces. I got 87.'''

sentences = re.split(r'(?<=[.!?:])\s+', input_text)                                      # divide string by sentences if we see one of the [.!?:] characters and after it '\s+' one or more spaces
capitalized_first_letter = '\n'.join([sentence.capitalize() for sentence in sentences])  # for each sentence into list do UPPER CASE for first letter in each of the sentences and add all sentences into one string with Enter delimiter

output_like_input = ""                                                                   # create empty string for new text with paragraphs like in original text

sentences = re.split(r'(?<=[.!?:])\s+', capitalized_first_letter)                       # divide new string with UPPER letters by sentences if we see one of the [.!?:] characters and after it '\s+' one or more spaces
last_words = [sentence.split()[-1] if sentence else "" for sentence in sentences]       # divide each sentence by words (space separator) and choose the last word from each sentence and if sentence is not empty else empty result
new_sentence = ' '.join(last_words)                                                     # create new sentence and add each last word from the previous query
corrected_sentence = re.sub(r'[.:]+(?!$)', '', new_sentence)                            # delete all '.' and ':' from sentence if it is not the last word (not the end of the row $)

capitalized_first_letter = capitalized_first_letter + '\n' + corrected_sentence         # add sentence at the end of all text

paragraphs = capitalized_first_letter.split('\n')
paragraphs_array = [paragraphs[:1], paragraphs[1:2], paragraphs[2:4] + paragraphs[9:], paragraphs[4:6], paragraphs[6:9]]  # create array with sub-arrays with elements from paragraphs like slices (first with the element[0], the second with [1] and for third [2],[3] and [9] etc)

for row in paragraphs_array:                                    # loop for each sub-array
    list_to_str = ' '.join(map(str, row))                       # add each element in string format to the string with space separator
    output_like_input = output_like_input + list_to_str + '\n'  # add each text row with slice elements to the string with enter between each row

replace_one_iz = re.sub(r'“iz”', '“YYY“', output_like_input)                   # replace the right "iz" to avoid replication in future
replace_all_iz = replace_one_iz.replace("iz", "is").replace("“YYY“","“iz“")    # replace others iz with is and then replace the right "iz" which is "YYY" now like "iz" from the original row

whitespace_count = input_text.count(' ') + input_text.count('\n')            # count all spaces and enters into original text

print('1) Text in sensitive case and sentence with the last words: \n\n' + output_like_input )
print('2) Replace all wrong iz : \n\n' + replace_all_iz )
print('3) Count all whitespaces : ' + str(whitespace_count))


# -*- coding: utf-8 -*-
"""
Created on Thu Jan  2 11:35:06 2020

@author: haonan.liu
"""

from tkdocviewer import DocViewer
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
from gui_pkg.pdf_display_bespoke import display_pdf
import os
import cv2
import ocr_pkg.image_segmentation as img_seg
from gui_pkg.table_crop import get_PerspectiveTransform, getRowColumnLine
import gui_pkg.table_crop as table_crop
import numpy as np
from ocr_pkg.table_detect import detectTable
from tkinter import messagebox
import gui_pkg.exports as exp
from tkinter import ttk
import pytesseract
import time
from pandastable import Table
from gui_pkg.defaults import _C as cfg
from gui_pkg.img2table import img2df
#import tktable
#
#tktable.sample_test()
cur_dir = os.getcwd()
#fonts = font.Font(family = "San Francisco")
os.environ['PATH'] = os.environ['PATH'] + ';' + cur_dir +'\\poppler\\bin'
os.environ['PATH'] = os.environ['PATH'] + ';' + cur_dir +'\\ImageMagick-7.0.8-Q16'
os.environ['MAGIC_HOME'] = cur_dir +'\\ImageMagick-7.0.8-Q16'
os.environ['MAGICK_CODER_MODULE_PATH'] = cur_dir +'\\ImageMagick-7.0.8-Q16\\modules\\coders'
pytesseract.pytesseract.tesseract_cmd = cur_dir +'\\Tesseract-OCR\\tesseract.exe'

# some parameters
root = tk.Tk()
root.title(cfg.MAIN_TITLE)
rect = 10
img_canvas_multiplier = 1
ocr_indicator = ""

def get_filedialog(page_no_in = 0):
    global filename
    global page_num_total
    global page_no
    global page_dict
    global pdf_obj
    filepath = filedialog.askopenfile(initialdir =  "/", title = "Select A File", filetype =
        (("Portable Document Format","*.pdf"),("all files","*.*")))
    if filepath is None:
        return
    else:
        filename = filepath
    pdf_obj = display_pdf(filename.name)
    page_num_total = pdf_obj.count_pdf_pages()
    root.title(filename.name)
    #open_image()
    page_no = 0
    # Display some document
    v.display_file(filename.name, page_no+1)
    v.place(relx=0, rely=0, relwidth=1, relheight=1)
    page_img.place_forget()

    entry.delete(0, tk.END)
    entry.insert(0,"1")
    label_page['text'] = "/"+str(page_num_total) #it shows total num of page besides the current page num
    hide_buttons()
    page_dict = {}
    return filename

def img2icon(path_in, lenght = 20):
    icon_img = path_in
    icon_img = Image.open(icon_img)
    icon_img = icon_img.resize((lenght,lenght))
    icon_img = ImageTk.PhotoImage(icon_img)
    return icon_img

#===================pages operation==========================
def add_one():
    global page_no
    #global page_num_total
    global filename
    if page_no + 1 < page_num_total:
        page_no = page_no + 1
        v.display_file(filename.name, page_no+1)
        v.place(relx=0, rely=0, relwidth=1, relheight=1)
        page_img.place_forget()
        entry.delete(0, tk.END)
        entry.insert(0, str(page_no + 1))
        hide_buttons()
    else:
        pass

def minus_one():
    global page_no
    #global page_num_total
    global filename
    if page_no -1 >= 0:
        page_no = page_no - 1
        v.display_file(filename.name, page_no+1)
        v.place(relx=0, rely=0, relwidth=1, relheight=1)
        page_img.place_forget()
        entry.delete(0, tk.END)
        entry.insert(0, str(page_no + 1))
        hide_buttons()
    else:
        pass

def go_to_page(event):
    global page_no
    page_no = int(entry.get()) - 1
    v.display_file(filename.name, page_no+1)
    v.place(relx=0, rely=0, relwidth=1, relheight=1)
    page_img.place_forget()

#=================image processing=============================
def edit_mode():
    #global page_img
    global page_dict
    show_buttons()
    v.place_forget()
    pdf_obj.pdf_page_to_png(page_no)

    page_img.place(relx=0.1, rely=0, relwidth=1, relheight=1)

    open_image()
    page_dict["Page"+str(page_no)] = img_cur

def open_image():
    #size = int(lower_frame.winfo_height()*1.2)
    global img_array
    global img_cur
    global img_prev
    img_array = cv2.imread(cfg.temp_img_path)
    img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
    img_prev = img_array.copy()
    img_cur = img_array.copy()
    show_cur_img()

def show_cur_img():
    global img
    global ocr_indicator
    img = ImageTk.PhotoImage(img_resize(img_cur)) #img here is the tkinter format image
    page_img.delete("all")
    page_img.create_image(0,0, anchor='nw', image=img)
    page_img.image = img
    ocr_indicator = "img_cur"

def return_prev_img():
    global img
    global ocr_indicator
    global img_prev
    global img_cur
    img_cur = img_prev.copy()
    show_cur_img()

def img_resize(path_in):
    global img_shown_width
    global img_shown_height
    if isinstance(path_in, str):
        img_pil = Image.open(path_in)
    else:
        img_pil = Image.fromarray(path_in)
    wid, hei = img_pil.size
    if (hei > lower_frame.winfo_height()) | (wid > lower_frame.winfo_width()):
        img_shown_width = int(wid/hei*lower_frame.winfo_height()*img_canvas_multiplier)
        img_shown_height = int(lower_frame.winfo_height()*img_canvas_multiplier)
        if img_shown_width > lower_frame.winfo_width() :
            rat = lower_frame.winfo_width() / img_shown_width
            img_shown_width = img_shown_width * rat
            img_shown_height = img_shown_height * rat
        img_pil = img_pil.resize((int(img_shown_width), int(img_shown_height)))
#        else:
#            img_shown_width  = int(lower_frame.winfo_width()*img_canvas_multiplier)
#            img_shown_height = int(hei/hei*lower_frame.winfo_width()*img_canvas_multiplier)
    return img_pil

def rotate_clockwise():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    img_cur = img_seg.rotate_back(cfg.rot_unit, img_cur)
    show_cur_img()

def rotate_counterclockwise():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    img_cur = img_seg.rotate_back(-cfg.rot_unit, img_cur)
    show_cur_img()

def hide_buttons():
    btn_list = [button_crop, button_return, button_rot, button_rot2,button_cut,
               button_line, button_exports, button_pdf, button_txt, button_wand, btn_undo]
    for btn in btn_list:
        btn.place_forget()

def show_buttons():
    button_crop.place(relx=0.37, relheight=1, relwidth=0.04)
    button_return.place(relx=0.42, relheight=1, relwidth=0.04)
    button_rot.place(relx=0.47, relheight=1, relwidth=0.04)
    button_rot2.place(relx=0.52, relheight=1, relwidth=0.04)
    button_cut.place(relx=0.57, relheight=1, relwidth=0.06)
    button_line.place(relx=0.625, relheight=1, relwidth=0.06)
    button_pdf.place(relx=0.83, relheight=1, relwidth=cfg.btn_width)
    button_exports.place(relx=0.785, relheight=1, relwidth=cfg.btn_width)
    button_txt.place(relx=0.73, relheight=1, relwidth=cfg.btn_width)
    button_wand.place(relx=0.675, relheight=1, relwidth=0.06)
    btn_undo.place(relx=0.3, relheight=1, relwidth=cfg.btn_width)

def return_img():
    global img_cur
    img_cur = img_array.copy()
    #size = int(lower_frame.winfo_height()*1.2)
    show_cur_img()

def crop_img():
    global img_cur
    global img_prev
    #size = int(lower_frame.winfo_height()*1.2)
    img_prev = img_cur.copy()
    img_cur = get_PerspectiveTransform(img_cur, 5, 100).main_fun()
    show_cur_img()

def draw_auxiliary_line():
    m_h = getRowColumnLine(img_cur).horizontal()
    m_v = getRowColumnLine(img_cur).vertical()
    img_w_line = table_crop.draw_auxiliary_line(img_cur,m_h,"h" )
    img_w_line = table_crop.draw_auxiliary_line(img_w_line,m_v,"v" )
    img = ImageTk.PhotoImage(img_resize(img_w_line))
    page_img.delete("all")
    page_img.create_image(0, 0, anchor='nw', image=img)
    page_img.image = img

def on_move_press(event):
    global curX, curY
    curX, curY = (event.x, event.y)
    page_img.coords(rect, start_x, start_y, curX, curY)

def on_button_release(event):
    print(start_x, start_y, curX, curY)
    pass

def on_button_press(event, **kwargs):
    global start_x
    global start_y
    global rect
    start_x = event.x
    start_y = event.y
    #rect = page_img.create_rectangle(0, 0, 1,1, fill = "", outline="#e01919")
    #page_img.delete(rect)

    if rect:
        page_img.delete(rect)

    #rect = page_img.create_rectangle(0, 0, 1,1, fill = "red", outline="#e01919", alpha = 0.5)
    rect = create_rectangle(0, 0, 1,1, fill = "red", outline="#e01919", alpha = 0.5)

def create_rectangle(x1, y1, x2, y2, **kwargs):
    if 'alpha' in kwargs:
        alpha = int(kwargs.pop('alpha') * 255)
        fill = kwargs.pop('fill')
        fill = root.winfo_rgb(fill) + (alpha,)
    return page_img.create_rectangle(x1, y1, x2, y2, **kwargs)

def crop_manul():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    if len(img_array)>0:
        hei,wid = img_cur.shape[0], img_cur.shape[1]
        print("img wid",wid, "hei", hei)
        print("img shown width", img_shown_width)
        print("img shown height", img_shown_height)

        rate = wid/img_shown_width
        start_x_img = rate * start_x
        start_y_img = rate * start_y
        cur_x_img = rate * curX
        cur_y_img = rate * curY
        if len(img_cur) == 3:
            img_cur = img_cur[int(start_y_img):int(cur_y_img),int(start_x_img):int(cur_x_img), :]
        else:
            img_cur = img_cur[int(start_y_img):int(cur_y_img),int(start_x_img):int(cur_x_img)]
        show_cur_img()
    else:
        pass

def magic_stick():
    global img_no_border
    global ocr_indicator
    mask, joint = detectTable(img_cur).run()
    kernel = np.ones((2, 2), np.uint8)
    mask = cv2.dilate(mask, kernel, iterations =2)
    gray_img = cv2.cvtColor(img_cur, cv2.COLOR_BGR2GRAY)
    img_no_border = cv2.add(gray_img, mask)
    img = ImageTk.PhotoImage(img_resize(img_no_border))
    page_img.delete("all")
    page_img.create_image(0,0, anchor='nw', image=img)
    page_img.image = img
    ocr_indicator = "img_no_border"
#===========================================================export functions============================================================
def img2txt():
    global ocr_indicator
    name = filedialog.asksaveasfile(mode='w',defaultextension=".txt")
    if name is None:
        return
    if ocr_indicator == "img_cur":
        print("ocr current img")
        exp.page_exports(img_cur, name.name)
    else:
        print("img remove border")
        exp.page_exports(img_no_border, name.name)
    messagebox.showinfo("title???", "Finish!")

def img2pdf():
    name = filedialog.asksaveasfile(mode='w',defaultextension=".pdf")
    if name is None:
        return
    #exp.page_exports(img_cur, name.name)
    pdf = pytesseract.image_to_pdf_or_hocr(img_cur, extension='pdf')
    f = open(name.name, 'w+b')
    f.write(pdf)
    f.close()
    messagebox.showinfo("", "Finish!")

def img2excel():
#    name = filedialog.asksaveasfile(mode='w',defaultextension=".xlsx")
#    if name is None:
#        return
#    time.sleep(10)
#    messagebox.showinfo("", "Finish!")
    global df
    global page_width

    df = exp.page_exports(img_cur, filename.name).img2df(filename.name,page_no)
    try:
        if df.empty:
            df = img2df(img_cur)
    except:
        if not df:
            df = img2df(img_cur)
    #page_width = pdf_obj.get_page_width(page_no)
    #df = exp.page_exports(img_cur).crop_page(filename.name, page_no, page_width,img_shown_width,start_x, start_y, curX, curY)
    pop_table = tk.Toplevel(height = 500, width = 700)
    pop_table.wm_title("Table output")

    #pop_frame = tk.Label(pop_table)
    pt = Table(pop_table, dataframe = df) #,showtoolbar=True
    pt.show()
    #pt.place(relx=0, rely=0, relwidth=1, relheight=0.8)

    return
#===================================pop up windows=============================================
def popup_entrybox():
    global output_txt
    popup_window = tk.Toplevel(height = 500, width = 700)
    popup_window.wm_title("OCR output")

    l = tk.Label(popup_window, text="OCR output:")
    l.place(relx=0.1, rely=0, relwidth=1, relheight=0.05, anchor='n')

    back = tk.Frame(master = popup_window, width = 800, height = 600)
    back.place(relx=0.5, rely=0.05, relwidth=1, relheight=0.95, anchor='n')
    if ocr_indicator == "img_cur":
        print("ocr current img")
        text = pdf_obj.extrac_text_from_page(page_no)
        if (len(text) < 5) or text.isspace():
            text = pytesseract.image_to_string(img_cur)
            if text.isspace():
                text = pytesseract.image_to_string(img_cur, config = '--psm 6')
    else:
        text = pdf_obj.extrac_text_from_page(page_no)
        if (len(text) < 5) or text.isspace():
            print("OCRing img remove border")
            text = pytesseract.image_to_string(img_no_border)
            if text.isspace():
                text = pytesseract.image_to_string(img_no_border, config = '--psm 6')

    output_txt = tk.Text(back)
    #output_txt.delete(0, tk.END)
    output_txt.insert(tk.END, text)
    output_txt.place(relx=0.5, rely=0, relwidth=0.9, relheight=0.9, anchor='n')
    save_out_btn = tk.Button(back, text = "Save", font =4, command= lambda: create_save())
    save_out_btn.place(relx = 0.5, rely=0.92, relwidth=0.15, relheight=0.06, anchor='n')
    remove_space_btn = tk.Button(back, text = "Trim", font = 4, command = lambda: remove_space())
    remove_space_btn.place(relx = 0.3, rely=0.92, relwidth=0.15, relheight=0.06, anchor='n')

def create_save():
    name = filedialog.asksaveasfile(mode='w',defaultextension=".txt")
    if name is None:
        return
    file = open(name.name, 'w', encoding = 'utf8')
    file.write(output_txt.get(1.0, "end-1c"))
    file.close()
    messagebox.showinfo("", "Finish!")

def remove_space():
    text_inEntry = output_txt.get(1.0, "end-1c").split("\n")
    text_inEntry = [substr for substr in text_inEntry if (substr != "") & (not substr.isspace())]
    text_inEntry = "\n".join(text_inEntry)
    output_txt.delete(1.0, "end-1c")
    output_txt.insert(1.0, text_inEntry)

#===========================filters and other img processing=========================
def GussianBlur():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    img_cur = cv2.GaussianBlur(img_cur,(5,5),0)
    show_cur_img()

def erode_img():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    kernel = np.ones((2,2),np.uint8)
    if len(img_cur.shape) == 3:
        img_cur = cv2.cvtColor(img_cur, cv2.COLOR_BGR2GRAY)
    img_cur = cv2.erode(img_cur,kernel,iterations = 1)
    show_cur_img()

def dilate_img():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    kernel = np.ones((2,2),np.uint8)
    if len(img_cur.shape) == 3:
        img_cur = cv2.cvtColor(img_cur, cv2.COLOR_BGR2GRAY)
    img_cur = cv2.dilate(img_cur,kernel,iterations = 1)
    show_cur_img()

#===========================eraser=============================
def eraser():
    global img_no_border
    global ocr_indicator
    if len(img_array)>0:
        hei,wid = img_cur.shape[0], img_cur.shape[1]
        print("img wid",wid, "hei", hei)
        print("img shown width", img_shown_width)
        print("img shown height", img_shown_height)

        rate = wid/img_shown_width
        start_x_img = rate * start_x
        start_y_img = rate * start_y
        cur_x_img = rate * curX
        cur_y_img = rate * curY
        if len(img_cur) == 3:
            img_no_border[int(start_y_img):int(cur_y_img),int(start_x_img):int(cur_x_img), :]=255
        else:
            img_no_border[int(start_y_img):int(cur_y_img),int(start_x_img):int(cur_x_img)]=255
        img = ImageTk.PhotoImage(img_resize(img_no_border))
        page_img.delete("all")
        page_img.create_image(0,0, anchor='nw', image=img)
        page_img.image = img
        ocr_indicator =  "img_no_border"
    else:
        pass

#===========================rotate img=========================
def orientation_det():
    orient = pytesseract.image_to_osd(img_cur)
    messagebox.showinfo("", orient)

def rot_clockwise():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    img_cur = cv2.rotate(img_cur,cv2.ROTATE_90_CLOCKWISE)
    show_cur_img()

def rot_Counterclockwise():
    global img_cur
    global img_prev
    img_prev = img_cur.copy()
    img_cur = cv2.rotate(img_cur,cv2.ROTATE_90_COUNTERCLOCKWISE)
    show_cur_img()

#=========================help index==========================================
def help_index():
    popup_window = tk.Toplevel(height = 753, width = 500)
    popup_window.wm_title("Help Index")

    l = tk.Label(popup_window, text="We Are Always Ready for Help")
    l.place(relx=0.1, rely=0, relwidth=1, relheight=0.05)#, anchor='n'

    help_pic_cns = tk.Canvas(popup_window, highlightthickness=0, bg="white") # bd=0,
    help_pic_cns.delete("all")

    img = ImageTk.PhotoImage(Image.open(cfg.icon_folder+"\\delight.jpg"))
    help_pic_cns.create_image(0,0, anchor='nw', image=img)
    help_pic_cns.image = img
    help_pic_cns.place(relx=0, rely=0, relwidth=1, relheight=1)#, anchor='n'
    #output_txt.delete(0, tk.END)

#======================Windows settings==============================================
canvas = tk.Canvas(root, height=cfg.HEIGHT, width=cfg.WIDTH, bg = "white")
canvas.pack()
#====================================================================================

frame = tk.Frame(root, bg='white', bd=10)
frame.place(relx=0.5, rely=0, relwidth=1, relheight=0.07, anchor='n')

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

menubar = tk.Menu(root)
filemenu = tk.Menu(menubar,  background= "#e0ecde",selectcolor = "#68b2a0", tearoff=0)
open_icon = img2icon(cfg.OPEN_ICON)
exit_icon = img2icon(cfg.EXIT_ICON)

save_icon = cfg.ICON_DIR+"\\save icon.png"
save_icon = img2icon(save_icon)

filemenu.add_command(label=" Open", background= "white", font = 5, image = open_icon, compound="left")
filemenu.add_command(label=" Save", background= "#e0ecde", font = 5, image = save_icon, compound="left",)
filemenu.add_separator()
filemenu.add_command(label=" Exit", background= "#e0ecde", font = 5, image = exit_icon, compound="left", command=root.quit)
#filemenu['font'] = fonts
menubar.add_cascade(label="File", background= "#e0ecde", font = 5, menu=filemenu)
#====================================================================================
helpmenu = tk.Menu(menubar, tearoff=0,   background= "#f5f5f5")
helpmenu.add_command(label="Help", command= lambda: help_index())
helpmenu.add_command(label="More Services")
menubar.add_cascade(label="Help", menu=helpmenu)
#helpmenu['font'] = fonts
#====================================================================================
filtermenu = tk.Menu(menubar, tearoff=0)
filtermenu.add_command(label="MediumBlur")
filtermenu.add_command(label="GussianBlur", command = lambda : GussianBlur())
filtermenu.add_command(label="Erosion", command = lambda : erode_img())
filtermenu.add_command(label="Dilation", command = lambda : dilate_img())

menubar.add_cascade(label="Filters", menu=filtermenu)
#====================================================================================
operationmenu = tk.Menu(menubar, tearoff=0)
operationmenu.add_command(label="Eraser", command = lambda: eraser())
operationmenu.add_command(label="Fine Liner")
operationmenu.add_command(label="Corner Picker")
operationmenu.add_command(label="Border Removal")

menubar.add_cascade(label="Operations", menu=operationmenu)
#====================================================================================
page_orgmenue = tk.Menu(menubar, tearoff=0)
page_orgmenue.add_command(label = "Rorate Clockwise(90°)", command= lambda:rot_clockwise())
page_orgmenue.add_command(label = "Rorate Counter Clockwise(90°)", command = lambda: rot_Counterclockwise())
page_orgmenue.add_command(label="Orientation Detection", command = lambda: orientation_det())

menubar.add_cascade(label="Page Orgnize", menu=page_orgmenue)
menubar.config(selectcolor = "#68b2a0", bd=0)

#=======================================buttons on top=============================================

get_file = img2icon(cfg.FOLDER_ICON, 32)
file_load_button = tk.Button(frame, text="", font=8, border="0", image = get_file,command= lambda: get_filedialog(0), bg = "white")
file_load_button.place(relx=0, relheight=1, relwidth=cfg.btn_width)
file_load_button['border'] = "0"

minus = img2icon(cfg.MINUS_ICON, 32)
button_minues = tk.Button(frame, text = "", font = 8,bg = "white", border= "0",image = minus, command = lambda: minus_one())
button_minues.place(relx=0.035, relheight=1, relwidth=cfg.btn_width)

entry = tk.Entry(frame, font=40)
entry.place(relx=0.07,relwidth=0.03, relheight=1)
entry.bind("<Return>", go_to_page)

label_page = tk.Label(frame, bg = "white")
label_page.config(font=6)
label_page.place(relx = 0.105, relwidth=0.03, relheight=1)

plus_icon = img2icon(cfg.PLUS_ICON, 32)
button_plus = tk.Button(frame, text = "", font = 8, bg = "white", bd = 0.5, border= "0",image = plus_icon,command = lambda: add_one())
button_plus.place(relx=0.14, relheight=1, relwidth=cfg.btn_width)

edit_mode_icon = img2icon(cfg.EDIT_MODE_ICON, 32)
button_editmode = tk.Button(frame, text = "", font = 8, bg = "white", bd = 0.5, border= "0",image = edit_mode_icon, command = lambda: edit_mode())
button_editmode.place(relx=0.175, relheight=1, relwidth=cfg.btn_width)

crop = img2icon(cfg.CROP_ICON,32)
button_crop = tk.Button(frame, text = "", font = 8, bg = "white", image = crop, border = "0",command = lambda: crop_img())
button_crop.place(relx=0.21, relheight=1, relwidth=cfg.btn_width)

return_icon = img2icon(cfg.RETURN_ICON, 32)
button_return = tk.Button(frame, text = "", image = return_icon, bg = "white",font = 8, command = lambda: return_img())
button_return.place(relx=0.245, relheight=1, relwidth=cfg.btn_width)
button_return['border'] = "0"

undo_icon = img2icon(cfg.UNDO_ICON, 32)
btn_undo = tk.Button(frame, text="", image = undo_icon, bg="white", font=8, border="0",command=lambda: return_prev_img())
btn_undo.place(relx=0.3, relheight=1, relwidth=cfg.btn_width)

rot1 = img2icon(cfg.ROTATE1,32)
button_rot = tk.Button(frame, text = "↷", font = 9, bg = "white",border="0", image = rot1, command = lambda: rotate_clockwise())
button_rot.place(relx=0.47, relheight=1, relwidth=cfg.btn_width)

rot2 = img2icon(cfg.ROTATE2,32)
button_rot2 = tk.Button(frame, text = "", font = 9, bg = "white", border="0", image=rot2 ,command = lambda: rotate_counterclockwise())
button_rot2.place(relx=0.52, relheight=1, relwidth=cfg.btn_width)

cut_icon = img2icon(cfg.SCISSORS,32)
button_cut = tk.Button(frame, text = "cut", font = 9, bg = "white", image = cut_icon,border="0", command = lambda: crop_manul())
button_cut.place(relx=0.57, relheight=1, relwidth=cfg.btn_width)

lines = img2icon(cfg.PENCIL,32)
button_line = tk.Button(frame, text = "", font = 9, bg = "white", image = lines,border = "0", command = lambda :draw_auxiliary_line())
button_line.place(relx=0.625, relheight=1, relwidth=cfg.btn_width)

magic_wand = img2icon(cfg.WAND,32)
button_wand = tk.Button(frame, text = "", font = 9, bg = "white", image = magic_wand, border = "0", command = lambda :magic_stick())
button_wand.place(relx=0.675, relheight=1, relwidth=0.06)

exports_txt = img2icon(cfg.TXT_ICON, 32)
button_txt = tk.Button(frame, text = "", font = 9, bg = "white", image = exports_txt, border = "0", command = lambda: popup_entrybox())
button_txt.place(relx=0.73, relheight=1, relwidth=cfg.btn_width)

exports_excel = img2icon(cfg.excel_icon,32)
button_exports = tk.Button(frame, text = "", font = 9, bg = "white", image = exports_excel, border = "0", command = lambda :img2excel())
button_exports.place(relx=0.785, relheight=1, relwidth=cfg.btn_width)

exports_pdf = img2icon(cfg.PDF_ICON, 32)
button_pdf = tk.Button(frame, text = "", font = 9, bg = "white", image = exports_pdf, border = "0", command=lambda: img2pdf())
button_pdf.place(relx=0.83, relheight=1, relwidth=cfg.btn_width)
#====================================================================================

lower_frame = tk.Frame(root, bg='green',  border= "0",colormap="new")
lower_frame.place(relx=0.5, rely=0.06, relwidth=1, relheight=0.90, anchor='n')

v = DocViewer(lower_frame)
v.place(relx=0, rely=0, relwidth=1, relheight=1)

page_img = tk.Canvas(lower_frame, bg="green", highlightthickness=0, bd = "0.5") # bd=0,
page_img.bind("<ButtonPress-1>", on_button_press)
page_img.bind("<B1-Motion>", on_move_press)
page_img.bind("<ButtonRelease-1>", on_button_release)
# Start Tk's event loop
root.config(menu=menubar, background = 'white', border = "0") #, cursor = "mouse"
root.mainloop()


import torch
nn = torch.nn

class emb_permute(nn.Module):
    def __init__(self, args):
        super(emb_permute, self).__init__()
        self.emb = nn.Embedding(args['VOCAB_SIZE'] * 2, args['EMB_DIM'])

    def forward(self, indices):
        return self.emb(indices).permute(1, 0, 2)

class Encoder(nn.Module):
    def __init__(self, args):
        super().__init__()
        self.device = args['DEVICE']
        self.emb = emb_permute(args=args)
        self.enc = nn.TransformerEncoder(
            nn.TransformerEncoderLayer(
                d_model=args['EMB_DIM'],
                nhead=args['ATTN_HEAD'],
                dim_feedforward=args['FEED_DIM']
            ),
            num_layers=args['ENC_LAYERS']
        )

        self.positional = nn.Parameter(torch.randn(2 * args['SEQ_LENGTH'] + 4, 1, args['EMB_DIM']))

    def forward(self, inputs):
        # Obtian the batch number

        B = inputs.shape[0]
        seq_length = inputs.shape[1]

        inputs = self.emb(inputs)
        inputs += self.positional[0:seq_length].repeat(1, B, 1)

        outputs = self.enc(inputs)
        return outputs # Not return the hidden states here

class RNN_encoder(nn.Module):
    def __init__(self, args):
        super().__init__()
        # self.emb = emb_permute(args=args)
        # self.emb = nn.Linear(args['VOCAB_SIZE'] * 2, args['EMB_DIM'])
        if args['RNN_TYPE'] == 'LSTM':
            self.enc = nn.LSTM(
                input_size=args['EMB_DIM'],
                hidden_size=args['HIDDEN'],
                num_layers=args['LAYERS'],
                dropout=args['DROPOUT'],
                bidirectional=args['BIDIRECTION']
            )

        elif args['RNN_TYPE'] == 'GRU':
            self.enc = nn.GRU(
                input_size=args['EMB_DIM'],
                hidden_size=args['HIDDEN'],
                num_layers=args['LAYERS'],
                dropout=args['DROPOUT'],
                bidirectional=args['BIDIRECTION']
            )

        self.dropout = nn.Dropout(p=args['DROPOUT'])

    def forward(self, x, **kwargs):
        # x = self.dropout(self.emb(x))
        x, h_c = self.enc(x)
        return x

class cp_2_k_mask(nn.Module):
    def __init__(self, args, out_channels):
        super(cp_2_k_mask, self).__init__()
        self.out_channels = out_channels
        self.rotor_num = len(args['ROTOR'].split(' '))

        self.networks = nn.ModuleList()
        self.linear_projectors = nn.ModuleList()

        # Embedding (zeros if there exists LSTM layers)
        if args['LAYERS'] == 0:
            self.position_emb = nn.Parameter(torch.randn(60, 1, args['EMB_DIM'] * 2)).to(args['DEVICE'])
        else:
            self.position_emb = torch.zeros(60, 1, args['EMB_DIM']).to(args['DEVICE'])

        self.emb = nn.Linear(args['VOCAB_SIZE'] * 2, args['EMB_DIM'] if args['LAYERS'] > 0 else args['EMB_DIM'] * 2)

        # Dropout
        self.dropout = nn.Dropout(p=args['DROPOUT'])

        # Adding LSTM layers
        if args['LAYERS'] > 0:
            self.networks.append(RNN_encoder(args))

        # Adding Transformer layers
        if args['ENC_LAYERS'] > 0:
            self.networks.append(nn.TransformerEncoder(
                    nn.TransformerEncoderLayer(
                        d_model=args['HIDDEN'] * 2,
                        nhead=args['ATTN_HEAD'],
                        dim_feedforward=args['FEED_DIM'],
                        dropout=args['DROPOUT']
                    ),
                    num_layers=args['ENC_LAYERS']
                ))

        # linear projectors for predictions
        self.linear_projectors = nn.Linear(args['HIDDEN'] * 2, out_channels * 3)

        # Further experiments projector
        self.num_rand_rotor_type = args['Random_rotors_num']
        self.random_reflector =  args['Random_reflector']
        self.num_rand_ringsetting = args['Random_ringsetting_num']
        self.num_rand_plug = args['Random_plugboard_num']

        # self.further_experiment_proj = nn.ModuleList()
        # random rotors (8 different types of rotors) shape : [1 - 3, 8]
        if args['Random_rotors_num'] is not None:
            # shape [batch, feats] -> [batch, rotor_num, rotor_type(8)]
            self.rotor_type_proj = nn.Linear(args['HIDDEN'] * 2, self.num_rand_rotor_type * 8)

        # random reflector (B and C) shape : [1]
        if args['Random_reflector'] is not None:
            # Shape: [batch, feats] -> [batch]
            self.reflector_proj = nn.Linear(args['HIDDEN'] * 2, 2)

        # random ring setting (26 ringsetting for each rotor) shape : [1 - 3, 26]
        if args['Random_ringsetting_num'] is not None:
            # shape: [batch, feats] -> [batch, ringsetting_num, 26]
            self.ringsetting_proj = nn.Linear(args['HIDDEN'] * 2, self.num_rand_ringsetting * 26)

        # Random plugboard( 26x 26 plugboard setting) shape : [26, 26]
        if args['Random_plugboard_num'] is not None:
            # shape: [batch, feats] -> [batch, 26, 26]
            self.plugboard_proj = nn.Linear(args['HIDDEN'] * 2, 26 * 26)

    def forward(self, x, masks):
        # Embedding
        x = self.dropout(self.emb(x))

        # Obtain the length of dimension
        seq, batch, features = x.shape

        # Adding the position embedding
        x = x + self.position_emb[:seq].repeat(1, batch, 1)

        # forwarding
        for layer in self.networks:
            x = layer(x, src_key_padding_mask=masks)

        # predictions
        position_pred = self.linear_projectors(x).reshape(seq, batch, 3, self.out_channels).permute(2, 0, 1, 3)

        # Further prediction
        # From here we place the features on the second dimension because the Cross Entropy loss works on the second dim
        further = []
        x = torch.mean(x, dim=0)
        if self.num_rand_rotor_type is not None:
            further.append(self.rotor_type_proj(x).reshape(batch, 8, self.num_rand_rotor_type))

        if self.random_reflector is not None:
            further.append(self.reflector_proj(x))

        if self.num_rand_ringsetting is not None:
            further.append(self.ringsetting_proj(x).reshape(batch, 26, self.num_rand_ringsetting))

        if self.num_rand_plug is not None:
            further.append(self.plugboard_proj(x).reshape(batch, 26, 26))

        return position_pred, further
        # return torch.stack([proj(x) for proj in self.linear_projectors])  # -> [3, seq, batch, out_channels]

class cp_2_k_onnx(cp_2_k_mask):
    def __init__(self, args, out_channels):
        super().__init__(args, out_channels)

    def forward(self, x):
        # Embedding
        x = self.dropout(self.emb(x))

        seq, batch, features = x.shape

        # forwarding
        for layer in self.networks:
            x = layer(x)

        # predictions
        return self.linear_projectors(x).view(seq, batch, 3, self.out_channels).permute(2, 0, 1, 3)

if __name__ == "__main__":
    from config import args
    sample = torch.randn(args['SEQ_LENGTH'][1], 1, args['VOCAB_SIZE'] * 2)
    print(sample.shape)
    print(sample.repeat(1, 5, 1).shape)


#!/usr/bin/env python3

from shared import argparse
from shared import setup

parser = argparse.ArgumentParser(description='Publish a payload to a topic')
parser.add_argument('topic')
parser.add_argument('payload')
args = parser.parse_args()
topic = args.topic
payload = args.payload
print('Publishing payload to ' + topic)

mqtt_client = setup()
mqtt_client.connect()
mqtt_client.publish(topic, payload, 0)
mqtt_client.disconnect()


import cv2
import numpy as np
import os

cap = cv2.VideoCapture(0)
#capWebcam.set(cv2.CAP_PROP_FRAME_WIDTH, 320.0)              # change resolution to 320x240 for faster processing
#capWebcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 240.0)

while(True):

    ret,frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    #img = cv2.medianBlur(frame,5)
    #cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

    #circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=10,maxRadius=20)

    #circles = np.uint16(np.around(circles))
    #for i in circles [0,:]:
            #cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
            #cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

    cv2.imshow('hh',gray)
    if cv2.waitKey(1)& 0xFF == ord('q'):
        break
cap.release()
cv2.destoryAllWindows()


def solution(board, moves):
    doll_board = [[] for _ in range(len(board[0]))]  # 0은 빈 공간이므로 0을 없애고 인형만 있는 칸만 냅둬서 인형만 있는 배열로 재생성
    for j in range(len(board)):
        for i in range(len(board[0])):
            if board[i][j] == 0: # 0인 칸은 없애기
                pass
            else:
                doll_board[j].append(board[i][j]) # 인형만 추가
    count = 0 # 인형이 없어진 횟수
    basket = [] # 인형을 넣을 바구니 배열
    for i in range(len(moves)):
        if len(doll_board[moves[i]-1]) == 0: # 각 번호의 열이 비어있을 경우 아무일도 일어나지 않는다고 했으므로 continue 처리
            continue
        else:
            basket.append(doll_board[moves[i]-1][0]) # moves에 따라 뽑은 인형을 basket에 넣기
            del doll_board[moves[i]-1][0] # 인형 배열에 있는 뽑은 인형 제거

        if len(basket) >= 2 and basket[-1] == basket[-2]: # basket배열에 들어온 인형과 바로 전 인형이 같을 경우
            for i in range(2): # 인형 2개를 없애고
                basket.pop()
            count += 1 # 인형이 없어진 횟수 증가
    result = count * 2 # 인형이 터진 횟수 = 인형 뽑은 횟수 * 터진 인형 개수

    return result # 인형이 터진 개수 반환

프로그래머스/lv1/64061. 크레인 인형뽑기 게임/크레인 인형뽑기 게임.py

크레인 인형뽑기 게임.py


import PySimpleGUI as sg
import sys
from serial import Serial

min_temp = [sg.Text(text="Min Temp\n\n _ _ _ °F", justification="center", border_width=3, key="Min Temp",
                    background_color="white", font=("Helvetica", 40), size=(14, 5), text_color="black")]

avg_temp = [sg.Text(text="Avg Temp\n\n _ _ _ °F", justification="center", border_width=3, key="Avg Temp",
                    background_color="white", font=("Helvetica", 40), size=(14, 5), text_color="black")]

max_temp = [sg.Text(text="Max Temp\n\n _ _ _ °F", justification="center", border_width=3, key="Max Temp",
                    background_color="white", font=("Helvetica", 40), size=(14, 5), text_color="black")]

total_temp = [max_temp, avg_temp, min_temp]

buttons = [sg.Button("Start/Stop", font=("Helvetica", 25), pad=(40, 40)),
           sg.Button("°C/°F", font=("Helvetica", 25), pad=(40, 40)),
           sg.Button("Quit", font=("Helvetica", 25), pad=(40, 40))]

current_temp = [sg.Text(text="Current Temp\n\n _ _ _ °F", justification="center", border_width=3, key="Current Temp",
                        background_color="white", font=("Helvetica", 100), size=(14, 5), text_color="black")]

layout = [
    [sg.Column([current_temp, buttons], element_justification="center"),
     sg.Column(total_temp, vertical_alignment="top")]]

maxT = -sys.maxsize - 1
minT = sys.maxsize
current_temp = None

total_temp = 0
num_temps = 0
avgT = 0

in_celsius = False
poll = False

def update_value(key: str, value: float, celsius: bool):
    if celsius:
        window[key].update(f"{key}\n\n {value} °C")
    else:
        window[key].update(f"{key}\n\n {value} °F")
    window.refresh()

def change_unit(value, celsius):
    if celsius:
        return round((value * 9 / 5) + 32, 2)
    return round((value - 32) * 5 / 9, 2)

ser = Serial("COM4", 9600)
ser.reset_input_buffer()

window = sg.Window(title="Temperature Readings", layout=layout)
while True:
    event, values = window.read(timeout=0)
    if poll:
        if ser.inWaiting() > 1:
            current_temp = float(ser.readline().decode())
            if in_celsius:
                current_temp = change_unit(current_temp, not in_celsius)
            update_value("Current Temp", current_temp, in_celsius)
            total_temp += current_temp
            num_temps += 1
            if current_temp > maxT:
                maxT = current_temp
                update_value("Max Temp", maxT, in_celsius)
            if current_temp < minT:
                minT = current_temp
                update_value("Min Temp", minT, in_celsius)
            avgT = round(total_temp / num_temps, 2)
            update_value("Avg Temp", avgT, in_celsius)
    if event == "Start/Stop":
        poll = not poll
    if event == "°C/°F" and current_temp is not None:
        current_temp = change_unit(current_temp, in_celsius)
        minT = change_unit(minT, in_celsius)
        maxT = change_unit(maxT, in_celsius)
        avgT = change_unit(avgT, in_celsius)
        in_celsius = not in_celsius
        update_value("Current Temp", current_temp, in_celsius)
        update_value("Max Temp", maxT, in_celsius)
        update_value("Min Temp", minT, in_celsius)
        update_value("Avg Temp", avgT, in_celsius)
    if event in (sg.WIN_CLOSED, "Quit"):
        break


import http.client
import json, login

from suite_functions import check_reply

params = """{
"id" ::""/
}""";

expected_response = json.loads("""
{"status":"error", "reason":"JSON request malformed"}
""")

def exec_test(debug=False):
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", "Cookie" : "_pdt_session_id_="+login.cookie_id+""}
    conn = http.client.HTTPConnection("localhost", 8080)
    conn.request("POST", "/projects/1/sprintlogs/create", params, headers)

    if not check_reply(conn, expected_response, debug):
        return False

    conn.request("POST", "/projects/1/sprintlogs/1/addpbi", params, headers)

    if not check_reply(conn, expected_response, debug):
        return False

    conn.request("POST", "/projects/1/sprintlogs/1/removepbi", params, headers)

    if not check_reply(conn, expected_response, debug):
        return False

    conn.request("POST", "/projects/1/pbis/1/createtask", params, headers)

    if not check_reply(conn, expected_response, debug):
        return False

    conn.request("POST", "/projects/1/pbis/1/edit", params, headers)

    if not check_reply(conn, expected_response, debug):
        return False

    return True


# l = list(range(100))
# l1 = l[:5]
# for i in l1:
#     print(i)
# print(l1)

import sys, time

for i in range(5):
    sys.stdout.write('{0}/5\r'.format(i + 1))
    sys.stdout.flush()
    time.sleep(1)


# pedir el nombre y la edad de los compañeros que vinieron a clase

def obtener_compañeros(cantidad):
    compañeros = []

    for i in range(cantidad):
        nombre = input("Ingrese el nombre del compañero: ")
        edad = int(input("Ingrese la edad del compañero: "))

        compañeros.append(tuple((nombre, edad)))

    compañeros.sort(key= lambda x: x[1])

    asistente = compañeros[0][0]
    profesor = compañeros[-1][0]

    return asistente, profesor

asistente, profesor  = obtener_compañeros(5)
print(f"El profesor es: {profesor} y su asistente es {asistente}")


# Import
import tensorflow as tf

# Criando um node no grafo computacional
node = tf.Variable(tf.zeros([2,2]))

# Executando o grafo computacional
sess = tf.Session()

# Iniciando as variaveis
sess.run(tf.global_variables_initializer())

# Avaliando o node
print('\nTensor original:\n ', sess.run(node))

# Adicionando element-wise no tensor
node = node.assign(node + tf.ones([2,2]))

# Avaliando o node novamente
print('\nTensor depois da edição:\n ', sess.run(node))

# Encerra a sessão
sess.close()


import logging

logging.basicConfig(level=logging.DEBUG #设置日志输出格式
                    ,filename="test.log" #log日志输出的文件位置和文件名
                    ,filemode="w" #文件的写入格式，w为重新写入文件，默认是追加
                    ,format="%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s" #日志输出的格式
                    # -8表示占位符，让输出左对齐，输出长度都为8位
                    ,datefmt="%Y-%m-%d %H:%M:%S" #时间输出的格式
                    )

############## 测试某个功能 ###################

from crossref import CrossRef

def test(doi):
    a = CrossRef(doi)
    a.connect()
    print(a.get_title())

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

if __name__=='__main__':
    try:
        doi = '10.1088/1361-6420/aa72b2'  # 一个无效的doi
        test(doi)
    except Exception as e:
        logging.exception(e)


# Problem
# Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

# Examples
# pig_it('Pig latin is cool') # igPay atinlay siay oolcay
# pig_it('Hello world !')     # elloHay orldway !

import string

def pig_it(text):
    words = text.split()
    result = ""
    for word in words:
        if word not in string.punctuation:
            result += word[1:] + word[0] + "ay "
        else:
            result += word + " "
    return result[:-1]


import board
import rotaryio
import digitalio
import time
import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

##i don't know what this does but this is the only way it works; arguably the most important
##line of code in the entire program
cc = ConsumerControl(usb_hid.devices)

##sets up the rotary encoder to handle directional control
knob = rotaryio.IncrementalEncoder(board.D3, board.D4)
last_pos = 0

##sets up various buttons
playPause = digitalio.DigitalInOut(board.D0)
playPause.direction = digitalio.Direction.INPUT
playPause.switch_to_input(pull = digitalio.pull.DOWN)
playPausePrevState = playPause.value

##cont'd button setup
nextTrack = digitalio.DigitalInOut(board.D1)
nextTrack.direction = digitalio.Direction.INPUT
nextTrack.switch_to_input(pull = digitalio.pull.DOWN)
nextTrackPrevState = nextTrack.value

##cont'd button setup
previousTrack = digitalio.DigitalInOut(board.D2)
previousTrack.direction = digitalio.Direction.INPUT
previousTrack.switch_to_input(pull = digitalio.pull.DOWN)
previousTrackPrevState = previousTrack.value

print("now reading!")

##dictionary #1 to set up what two states we have
stateList = [
    {
        "Playing...",
    },
    {
        "Pausing..."
    }
]

##whichever state we are NOT in gets put in this list
notCurrState = []
##moves the state we are not utilizing away from action
notCurrState.append(stateList.pop(0))

while True:
##checks to see if variation of rotary encoder reading
    current_pos = knob.position
    if last_pos == 0 or current_pos != last_pos:
        print(current_pos)

        ##volume up
        if current_pos > last_pos:
            for i in range(5):
                cc.send(ConsumerControlCode.VOLUME_INCREMENT)
        ##volume down
        if current_pos < last_pos:
            for i in range(5):
                cc.send(ConsumerControlCode.VOLUME_DECREMENT)
    ##sets the current position relative to itself
    last_pos = current_pos

##will play or pause track
    playPauseCurrState = playPause.value
    if playPauseCurrState != playPausePrevState:
        if playPauseCurrState:
            ##prints the state we are currently in
            print(stateList[0])
            cc.send(ConsumerControlCode.PLAY_PAUSE)
            ##sets up a temporary list to move states back and forth
            temp = []
            ##moves the just utilized state into the temporary list
            temp.append(stateList.pop(0))
            ##moves the unused state into the to-be-used list
            stateList.append(notCurrentState.pop(0))
            ##moves the just used state into the do-not-use list
            notCurrentState.append(temp.pop(0))
            playPausePrevState = playPauseCurrState
        else:
            playPausePrevState = False
            pass

##will skip or go back one track
    nextTrackCurrState = nextTrack.value
    if nextTrackCurrState != nextTrackPrevState:
        if nextTrackCurrState:
            print("skipping onto next track")
            cc.send(ConsumerControlCode.SCAN_NEXT_TRACK)
            nextTrackPrevState = nextTrackCurrState
        else:
            nextTrackPrevState = False
            pass

    previousTrackCurrState = previousTrack.value
    if previousTrackCurrState != previousTrackPrevState:
        if previousTrackCurrState:
            ##go back one track
            print("going back one track")
            cc.send(ConsumerControlCode.SCAN_PREVIOUS_TRACK)
            previousTrackPrevState = previousTrackCurrState
        else:
            previousTrackPrevState = False
            pass


"""
This Module defines a Node and a Binary Tree
"""

class Node:
  def __init__ (self,data,left=None,right=None):
    self.data = data
    self.left = left
    self.right = right

class BinaryTree:

    def __init__(self):
        self.root = None

    def pre_order(self):
        """
        A binary tree method which returns a list of items that it contains in pre_order

        input: None

        output: tree items

        sub method : walk () to make the recursion staff
        """
        list_of_items = []
        def walk(node):
            if node:
                list_of_items.append(node.data)
                if node.left:
                    walk(node.left)
                if node.right:
                    walk(node.right)

        walk(self.root)
        return list_of_items

    def add(self,value):
        if not self.root:
            self.root= Node(value)
        else :
            temp = self.root
            while temp:
                if value < temp.data:
                    if not temp.left:
                        temp.left = Node(value)
                        break
                    temp = temp.left
                else:
                    if not temp.right:
                        temp.right = Node(value)
                        break
                    temp = temp.right


def bar(vols, speeds):
    vols_ = [0]
    for vol1, vol2 in zip(vols, vols[1:]): # becouse volume's step can be less than first value
        vols_.append(vol1 + (vol2 - vol1) / 2)

    times = [0]
    for vol1, vol2, speed in zip(vols_, vols_[1:], speeds):
        times.append(times[-1] + (vol2 - vol1) / speed)

    return vols_, times


import sys
import InstaBotV2

def main():
    hdLess = False
    noOfLikesToGive = 1
    percentageOfUsersToCover = 0.501

    if len(sys.argv) > 1:
        print(sys.argv)
        if sys.argv[1] == 'True':
            hdLess = True
        try:
            noOfLikesToGive = int(sys.argv[2])
            percentageOfUsersToCover = float(sys.argv[3])
        except Exception as e:
            print(e)

    print([hdLess, type(hdLess), noOfLikesToGive, type(noOfLikesToGive), percentageOfUsersToCover, type(percentageOfUsersToCover)])
    print('\n')

    try:
        v2Bot = InstaBotV2.InstaBot(hdLess)
        # v2Bot.delayOps()
        v2Bot.getBrowser()
        v2Bot.logIn()

        theDailyResponse = v2Bot.love_Service('daily', noOfLikesToGive, percentageOfUsersToCover)
        if 'busted' in theDailyResponse:
            print("Busted!")
            return

        theExtraResponse = v2Bot.love_Service('extra', noOfLikesToGive, percentageOfUsersToCover)
        if 'busted' in theExtraResponse:
            print("Busted!")
            return
    except:
        # v2Bot.shutDown()
        pass

if __name__ == "__main__": main()


import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from scipy.optimize import curve_fit
import datetime as dt
import pylab
from uncertainties import ufloat
from uncertainties import unumpy

# phi-Winkel
wellenlaenge, phiLinks, phiRechts = np.genfromtxt('messung_phi.txt', unpack = True)

phi = .5 * (phiRechts - phiLinks)

for i in range(0, np.size(phi)):
	print("phi = " + str(round(phi[i], 1)))

phiGesamt = np.sum(phi) / np.size(phi)
phiError = np.sqrt(1. / (np.size(phi) - 1.) * np.sum((phiGesamt - phi) ** 2))

#print("phiGesamt = " + str(round(phiGesamt, 2)) + "\nphiError = " + str(round(phiError, 2)))

phiGesamtOptimal = 60.

# mu-Winkel
wellenlaenge, omegaLinks, omegaRechts = np.genfromtxt('messung_mu.txt', unpack = True)

mu = 180. - (omegaRechts - omegaLinks)
muOptimal = - 180. + (omegaRechts - omegaLinks)

for i in range(0, np.size(phi)):
	print("muOptimal = " + str(round(muOptimal[i], 2)))

n = np.sin((muOptimal + phiGesamt) * (np.pi / 180.) / 2.) / np.sin(phiGesamt * (np.pi / 180.) / 2.)
deltaN = np.sqrt(( np.sin(muOptimal / 2. * (np.pi / 180.)) / (np.cos(phiGesamt * (np.pi / 180.)) - 1) )**2) * phiError * (np.pi / 180.)

for i in range(0, np.size(n)):
	print(str(wellenlaenge[i]) + ": n = " + str(round(n[i], 3)) + "+-" + str(round(deltaN[i], 5)))

nOptimal = np.sin((muOptimal + phiGesamt) * (np.pi / 180.) / 2.) / np.sin(phiGesamt * (np.pi / 180.) / 2.)

for i in range(0, np.size(n)):
	print(str(wellenlaenge[i]) + ": nOptimal = " + str(round(nOptimal[i], 3)))

dispersionsgleichung1 = lambda x, A0, A2: A0 - A2 * x
vorschlagswerte1 = np.array([1., 1.])

dispersionsgleichung2 = lambda x, A0, A2: A0 + A2 * x
vorschlagswerte2 = np.array([1., 1.])

#nOptimal = nOptimal[::-1]
wellenlaenge *= 1e-9

koeffizienten1, varianzen1 = curve_fit(dispersionsgleichung1, wellenlaenge ** 2, nOptimal ** 2, p0 = vorschlagswerte1, maxfev = 1000)

koeffizienten2, varianzen2 = curve_fit(dispersionsgleichung2, 1 / wellenlaenge ** 2, nOptimal ** 2, p0 = vorschlagswerte2, maxfev = 1000)

print("A0' = " + str(koeffizienten1[0]) + " +- " + str(np.sqrt(varianzen1[0][0])))
print("A2' = " + str(koeffizienten1[1]) + " +- " + str(np.sqrt(varianzen1[1][1])))

s1quadrat = 1. / (np.size(wellenlaenge) - 2.) * np.sum((nOptimal ** 2 - koeffizienten1[0] - koeffizienten2[1] / wellenlaenge ** 2) ** 2)
print("s'^2 = " + str(s1quadrat))

s2quadrat = 1. / (np.size(wellenlaenge) - 2.) * np.sum((nOptimal ** 2 - koeffizienten2[0] + koeffizienten1[1] * wellenlaenge ** 2) ** 2)
print("s^2 = " + str(s2quadrat))

print("A0 = " + str(koeffizienten2[0]) + " +- " + str(np.sqrt(varianzen2[0][0])))
print("A2 = " + str(koeffizienten2[1]) + " +- " + str(np.sqrt(varianzen2[1][1])))

#print(koeffizienten2)
#print(varianzen2)

wellenlaengeTheorie = np.arange(np.min(wellenlaenge), np.max(wellenlaenge), 1e-12)
dispersionsgleichungOptimal = lambda x: np.sqrt(koeffizienten2[0] + koeffizienten2[1] / x**2)

plt.plot(wellenlaenge ** 2, nOptimal ** 2, "kx")
plt.plot(wellenlaengeTheorie ** 2, dispersionsgleichung1(wellenlaengeTheorie ** 2, koeffizienten1[0], koeffizienten1[1]), "r-")
plt.ylabel(r'$n^2$')
plt.xlabel(r'$1 / \lambda^2 \, [1 / \mathrm{m}^2]$')
plt.legend(["Messwerte", "Dispersionsgleichung 2"])

fig = plt.gcf() #Gibt Referent auf die aktuelle Figur - "get current figure"
fig.set_size_inches(10 * .7, 7 * .7)

plt.grid(which = 'both')
plt.savefig('../img/dispersion2falsch.pdf')
plt.clf()

fig = plt.gcf() #Gibt Referent auf die aktuelle Figur - "get current figure"

plt.plot(1 / wellenlaenge ** 2, nOptimal ** 2, "kx")
plt.plot(1 / wellenlaengeTheorie ** 2, dispersionsgleichung2(1 / wellenlaengeTheorie ** 2, koeffizienten2[0], koeffizienten2[1]), "b-")

plt.ylabel(r'$n^2$')
plt.xlabel(r'$\lambda^2 \, [\mathrm{m}^2]$')
plt.legend(["Messwerte", "Dispersionsgleichung 1"], "lower right")

fig.set_size_inches(10 * .7, 6.5 * .7)

plt.grid(which = 'both')
plt.savefig('../img/dispersion1falsch.pdf')
plt.clf()

fig = plt.gcf() #Gibt Referent auf die aktuelle Figur - "get current figure"

#nOptimal = nOptimal[::-1]

plt.plot(wellenlaenge, nOptimal, "kx")
plt.plot(wellenlaengeTheorie, dispersionsgleichungOptimal(wellenlaengeTheorie), "b-")

plt.ylabel(r'$n$')
plt.xlabel(r'$\lambda \, [\mathrm{m}]$')
plt.legend(["Messwerte", "Dispersionsgleichung 1"], "lower right")

fig.set_size_inches(10 * .7, 6.5 * .7)

plt.grid(which = 'both')
plt.savefig('../img/dispersionNichtLinear.pdf')
plt.clf()

lambdaC = 656e-9
lambdaD = 589e-9
lambdaF = 486e-9

c = np.sqrt(dispersionsgleichung2(1/lambdaC ** 2, koeffizienten1[0], koeffizienten2[1]))
deltaC = .5 * np.sqrt(1/(koeffizienten2[0] + koeffizienten2[1] / lambdaC**2) * (varianzen2[0][0] + varianzen2[1][1] / lambdaC**4))
d = np.sqrt(dispersionsgleichung2(1/lambdaD ** 2, koeffizienten1[0], koeffizienten2[1]))
deltaD = .5 * np.sqrt(1/(koeffizienten2[0] + koeffizienten2[1] / lambdaD**2) * (varianzen2[0][0] + varianzen2[1][1] / lambdaD**4))
f = np.sqrt(dispersionsgleichung2(1/lambdaF ** 2, koeffizienten1[0], koeffizienten2[1]))
deltaF = .5 * np.sqrt(1/(koeffizienten2[0] + koeffizienten2[1] / lambdaF**2) * (varianzen2[0][0] + varianzen2[1][1] / lambdaF**4))
print("nC = " + str(round(c, 5)) + "+-" + str(round(deltaC,5)))
print("nD = " + str(round(d, 5)) + "+-" + str(round(deltaD,5)))
print("nF = " + str(round(f, 5)) + "+-" + str(round(deltaF,5)))

abbe = (d - 1.) / (f - c)

print("abbe = " + str(round(abbe, 4)) + "+-" + str(round(np.sqrt((1/(f-c) * deltaD) ** 2 + ((1-d)/((f-c)**2)*deltaF) ** 2 + ((d-1)/((f-c)**2)*deltaC) ** 2),4)))

b = 3e-2
A0 = koeffizienten1[0]
deltaA0 = np.sqrt(varianzen2[0][0])
A2 = koeffizienten2[1]
deltaA2 = np.sqrt(varianzen2[1][1])
aufloesungC = b * (A2 / lambdaC**3) * 1. / np.sqrt(A0 + A2 / lambdaC**2)
deltaAufloesungC = np.sqrt((b * A2 / (2. * lambdaC**3 * (A0 + A2 / lambdaC**2) ** 1.5))**2 * deltaA0**2 + (b / (lambdaC**3 *np.sqrt(A0 + A2 / lambdaC ** 2)) - b * A2 / (lambdaC ** 5 * np.sqrt(A0 + A2 / lambdaC ** 2) ** 3))**2 * deltaA2**2)
aufloesungF = b * (A2 / lambdaF**3) * 1. / np.sqrt(A0 + A2 / lambdaF**2)
deltaAufloesungF = np.sqrt((b * A2 / (2. * lambdaF**3 * (A0 + A2 / lambdaF**2) ** 1.5))**2 * deltaA0**2 + (b / (lambdaF**3 *np.sqrt(A0 + A2 / lambdaF ** 2)) - b * A2 / (lambdaF ** 5 * np.sqrt(A0 + A2 / lambdaF ** 2) ** 3))**2 * deltaA2**2)

print("AC = " + str(round(aufloesungC)) + "+-" + str(round(deltaAufloesungC)))
print("AF = " + str(round(aufloesungF)) + "+-" + str(round(deltaAufloesungF)))

print(np.sqrt(A2 / (A0 - 1)))

print("lambda1 = " + str(np.sqrt(- A2 / (A0 - 1.))) + "+-" + str(.5 * np.sqrt(- A2 / (A0 - 1.) ** 3 * deltaA0 ** 2 + 1. / (- A2 * (A0 - 1.)) * deltaA2 ** 2)))


#!/usr/bin/env python
# -*- coding: utf-8 -*-

class TermColors(object):

    """
    class :TermColors
        A helper class for colorizing output.
    """

    # color table
    color_dict = {
                  "purple": '\033[95m',
                  "blue": '\033[94m',
                  "green": '\033[92m',
                  "yellow": '\033[93m',
                  "red": '\033[91m',
                  "END": '\033[0m',
                  "bold": '\033[1m',
                  "underline": '\033[4m',
                 }

    def colorize(self, text, *styles):

        """
        Method: colorize
            wrapper for colorful output
        Params:
            text
              :: str
              :: text to be printed
            styles
              :: str
              :: style token
        """

        style_comb = ""
        for style in styles:
            style_comb += self.color_dict[style]
        return style_comb + text + self.color_dict["END"]


from flask import Flask, request
import hashlib
import hmac
import secrets
import os
import threading

from utils import *

app = Flask(__name__)
config_path = os.environ['DEPLOY_CONFIG'] if os.environ.get('DEPLOY_CONFIG') else 'config.toml'
conf = Config.parse(config_path)
repo = Repo(conf)
builder = Builder(conf)
builder.gen_static()
builder.deploy()

mutex = threading.Lock()

@app.route(conf.listen_url, methods=['POST'])
def up2date():
    if not secrets.compare_digest(request.headers.get('X-Hub-Signature'), verify_signature(request.data, conf.secret_key)):
        return 'bad signature', 401
    def thread_task():
        mutex.acquire()

        repo.update()
        builder.gen_static()
        builder.deploy()

        mutex.release()
    threading.Thread(target=thread_task).start()
    return 'ok', 200

def verify_signature(data: bytes, secret_key: bytes):
    signature = 'sha1=' + hmac.new(secret_key, msg=data, digestmod=hashlib.sha1).hexdigest()
    return signature

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


#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = "Gabriel Nativel-Fontaine"
__date__ = "21-01-04"
__usage__ = "Class for problem modelisation"
__version__ = "1.0"

from math import sqrt
import numpy as np

from src.SearchNode import SearchNode

class XYProblem:
    """ Used to create problem modelisation
    Create N random point in a 2D space
    """

    def __init__(self, nbPoints, start, end=None):
        self._start = SearchNode(start, 0)
        self._end = end

        self._cities = {}
        for i in range(nbPoints):
            x = np.random.randint(0, 50)
            y = np.random.randint(0, 50)
            self._cities[i] = (x, y)

    def __len__(self):
        return len(self._cities)

    @property
    def Start(self):
        return self._start

    @property
    def End(self):
        return self._end.Value

    def GetMax(self):
        """ Get min and max value of position (for display)
        """
        start = True
        minX = 0
        minY = 0
        for val in self._cities.values():
            if start:
                maxX, maxY = val
                minX, minY = val
                start = False
            else:
                x, y = val
                if maxX < x: maxX = x
                if maxY < y: maxY = y

                if minX > x: minX = x
                if minY > y: minY = y

        return maxX, maxY, minX, minY

    def GetNext(self, prev):
        next = []
        prev_pos = self._cities[prev.Value]
        for name, position in self._cities.items():
            if name != prev.Value:
                next.append(SearchNode(name, self._distance(prev_pos, position)))

        return next

    def _distance(self, pos1, pos2):
        """
        Euclidian distance
        :param pos1: tuple of (x, y) position
        :param pos2: tuple of (x, y) position
        :return: distance
        """
        return sqrt((pos2[0] - pos1[0]) ** 2 + (pos2[1] - pos1[1]) ** 2)

    def isEnd(self, node):
        return node == self._end


"""
Update the OMOP json files in-place to CDMv5.3.1 based on the CommonDataModel v5.3.1_fixes branch
(see https://github.com/OHDSI/CommonDataModel/blob/v5.3.1_fixes/OMOP_CDMv5.3.1_Field_Level.csv).

Minimizes formatting changes to reduce diff noise. Descriptions from current schemas are
preferred over updated ones. Note that running this script will overwrite
`resource_files/schemas/cdm/*.json`
"""

import csv
from collections import OrderedDict
from itertools import groupby
import json
import os

from resources import cdm_fields_path, resource_files_path

CDM_CSV = os.path.join(resource_files_path, 'OMOP_CDMv5.3.1_Field_Level.csv')
"""Downloaded from
   https://github.com/OHDSI/CommonDataModel/blob/v5.3.1_fixes/OMOP_CDMv5.3.1_Field_Level.csv"""

def find_current_path(filename):
    """
    Get the path to current schema file, if it exists
    :param filename: name of the JSON file
    :return:
    """
    for root, dirs, files in os.walk(cdm_fields_path):
        if filename in files:
            return os.path.join(root, filename)

def convert_type(in_type):
    """
    Convert the CDM data type to a BQ type
    :param in_type: value specified in cdmDatatype column of csv
    :return: a BQ type (str)
    """
    column_type = in_type.lower().split('(')[0]
    if (column_type == "bigint"):
        t = "integer"
    elif (column_type == "integer"):
        t = "integer"
    elif (column_type in ("timestamp", "datetime", "datetime2")):
        t = "timestamp"
    elif (column_type == "clob"):
        t = "string"
    elif (column_type == "character"):
        t = "string"
    elif (column_type == "varchar"):
        t = "string"
    elif (column_type == "text"):
        t = "string"
    elif (column_type == "character"):
        t = "string"
    elif (column_type == "boolean"):
        t = "string"  # todo - translate "t" and "f" to something else
    elif (column_type == "double"):
        t = "float"
    elif (column_type == "numeric"):
        t = "float"
    elif (column_type == "float"):
        t = "float"
    elif (column_type == "date"):
        t = "date"
    elif (column_type == "datetime"):
        t = "datetime"
    else:
        raise AssertionError("Unknown type: %s" % column_type)
    return t

def read():
    with open(CDM_CSV, encoding='utf8') as csvfile:
        reader = csv.DictReader(csvfile)
        in_fields = list(reader)
    for in_field in in_fields:
        yield dict(
            table=in_field['cdmTableName'].lower(),
            type=convert_type(in_field['cdmDatatype']),
            name=in_field['cdmFieldName'].lower(),
            mode='required' if in_field['isRequired'] == 'Yes' else 'nullable',
            description=in_field['userGuidance'])

def get_table(row):
    return row['table']

def update_fields():
    """
    Update schema files to version 5.3.1
    :return:
    """
    csv_rows = list(read())
    for table, fields in groupby(csv_rows, key=get_table):
        filename = f'{table}.json'
        old_schema = dict()
        current_path = find_current_path(filename)
        if current_path:
            with open(current_path, 'r', encoding='utf8') as schema_fp:
                old_fields = json.load(schema_fp)
                for old_field in old_fields:
                    old_schema[old_field['name']] = old_field

        field_items = []
        for field in fields:
            field_item = OrderedDict(type=field['type'],
                                     name=field['name'],
                                     mode=field['mode'])
            # prefer old description, if one exists
            description = field['description']
            old_field = old_schema.get(field['name'])
            if old_field:
                description = old_field.get('description', field['description'])
            # do not add a description property if empty
            if description:
                field_item['description'] = description
            field_items.append(field_item)

        # overwrite existing files, wherever they are
        # and write new schema files to cdm directory
        output_path = find_current_path(filename) or os.path.join(
            cdm_fields_path, filename)
        print(f'Writing to {output_path}...')
        with open(output_path, 'w', encoding='utf8') as fp:
            json.dump(field_items, fp, indent=2, ensure_ascii=False)

if __name__ == '__main__':
    update_fields()