import random
from math import log
from collections import defaultdict
from tqdm import tqdm
from sys import argv

random.seed(42)

class Topic_Model:
    def __init__(self, iteration_num, NUM_TOPICS, α, β, is_tqdm=True):
        self.iteration_num = iteration_num
        self.NUM_TOPICS = NUM_TOPICS
        self.α = α
        self.β = β
        self.is_tqdm = is_tqdm
        self.num_wordtype = 0
        self.xcorpus = []
        self.ycorpus = []
        self.xcounts = defaultdict(lambda: 0)
        self.ycounts = defaultdict(lambda: 0)

    def sampleone(self, probs):
        z = sum(probs)  # 確率の和(正規化項 ) を計算
        remaining = random.random() * z
        for i in range(len(probs)):
            remaining -= probs[i]
            if remaining <= 0:
                return i
        print('error')
        exit()

    def addCounts(self, word, topic, docid, amount):
        assert topic >= 0
        assert docid >= 0
        assert amount >= 0 or amount == -1

        self.xcounts[topic] += amount
        self.xcounts[f'{word}|{topic}'] += amount

        self.ycounts[docid] += amount
        self.ycounts[f'{topic}|{docid}'] += amount
        # バグチェック
        # < 0 の場合はエラー終了

    def init(self, in_path):
        wordtype = []
        for line in map(lambda x: x.rstrip(), open(in_path)):
            docid = len(self.xcorpus)
            words = line.split()
            topics = []
            for word in words:
                topic = random.randint(0, self.NUM_TOPICS - 1)
                topics.append(topic)
                self.addCounts(word, topic, docid, 1)  # カウントを追加
                if word not in wordtype:
                    wordtype.append(word)
            self.xcorpus.append(words)
            self.ycorpus.append(topics)
        self.num_wordtype = len(wordtype)

    def sampling(self, out_path='out.txt'):
        for _ in tqdm(range(self.iteration_num)):
            ll = 0
            for i in tqdm(range(len(self.xcorpus))):
                for j in range(len(self.xcorpus[i])):
                    probs = []
                    x = self.xcorpus[i][j]
                    y = self.ycorpus[i][j]
                    self.addCounts(x, y, i, -1)  # 各種カウントの減算 (-1)
                    for k in range(self.NUM_TOPICS):
                        p_x_k = (self.xcounts[f'{x}|{k}'] + self.α) / \
                            (self.xcounts[k] + self.α * self.num_wordtype)
                        p_k_y = (self.ycounts[f'{k}|{i}'] + self.β) / \
                            (self.ycounts[i] + self.β * self.NUM_TOPICS)
                        probs.append(p_x_k * p_k_y)      # トピック k の確率
                    new_y = self.sampleone(probs)
                    ll += log(probs[new_y])        # 対数尤度の計算
                    self.addCounts(x, new_y, i, 1)      # 各カウントの加算
                    self.ycorpus[i][j] = new_y
        #         print(ll)
        # print(self.xcounts)
        # print(self.ycounts)
        # print(self.xcorpus)
        # print(self.ycorpus)

        # word_topic = {i: [] for i in range(self.NUM_TOPICS)}
        word_topic_list = [[] for i in range(self.NUM_TOPICS)]
        # for i in range(len(self.xcorpus)):
        #     for j in range(len(self.xcorpus[i])):
        #         if self.xcorpus[i][j] not in word_topic:
        #             word_topic[self.xcorpus[i][j]] = self.ycorpus[i][j]
        for i in range(len(self.xcorpus)):
            for j in range(len(self.xcorpus[i])):
                word = self.xcorpus[i][j]
                topic = self.ycorpus[i][j]
                if word not in word_topic_list[topic]:
                    word_topic_list[topic].append(word)
        # with open(out_path, "w") as f_out:
        #     for k, v in word_topic.items():
        #         print(k, v, file=f_out)

        for i in range(len(word_topic_list)):
            random.shuffle(word_topic_list[i])
        with open(out_path, "w") as f_out:
            for i, values in enumerate(word_topic_list):
                print(i, file=f_out)
                for v in values:
                    print(v, end=' ', file=f_out)
                print(file=f_out)

if __name__ == '__main__':
    if argv[1:] == ['test']:
        in_path = '../../../nlptutorial/test/07-train.txt'
        is_tqdm = False
    else:
        in_path = '../../../nlptutorial/data/wiki-en-documents.word'
        is_tqdm = True
    topic_model = Topic_Model(20, 2, 0.01, 0.01, is_tqdm)  # iteration_num, NUM_TOPICS, α, β
    topic_model.init(in_path)
    topic_model.sampling('out_sep_shuffle_test.txt')


#(五)分离颜色通道&多通道图像混合
import cv2
'''
cv2.split(m, mv):将一个多通道数组分离成几个单通道数组
m：我们需要进行分离的多通道数组
mv：函数的输出数组或者输出的vector容器
'''
import  argparse
import numpy as np

img1 = cv2.imread("2.jpg")
(B,G,R) = cv2.split(img1)
cv2.imshow("Red", R)
cv2.imshow("Green", G)
cv2.imshow("Blue",B)
print("R_shape:",G.shape,"  G_shape:",G.shape,"  B_shape:",B.shape)

#通道扩展
zeros = np.zeros(img1.shape[:2], np.uint8)
img_B = cv2.merge([B,zeros,zeros])
cv2.imshow("B_", img_B)
img_G = cv2.merge([zeros,G,zeros])
cv2.imshow("G_", img_G)
img_R = cv2.merge([zeros,zeros,R])
cv2.imshow("R_", img_R)

cv2.waitKey()
cv2.destroyAllWindows()


import click
from renku_r_tools.packrat import Packrat

@click.group()
def cli():
    pass

@cli.command(help = 'Link packrat libraries to another location.')
@click.option('-p', '--proj_dir',
    help = 'R project main directory path. Use absolute path.',
    required = True)
@click.option('-s', '--source',
    default = '/home/rstudio/packrat',
    help = 'Main directory path of the new packrat libraries source. \
Use absolute path.',
    required = True,
    show_default = True)
@click.option('-f', '--force',
    is_flag = True,
    help = 'Overwrite existing libraries.')
@click.option('-v', '--verbose',
    is_flag = True,
    help = 'Print various messages.')
def ln_packrat_lib(proj_dir, source, force, verbose):
    packrat = Packrat(proj_dir = proj_dir)
    packrat.ln_lib(ln_source = source,
        force = force,
        verbose = verbose)

if __name__ == '__main__':
    cli()


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import sklearn.preprocessing as sklpre
import sklearn.model_selection as sklms

# TO DO: Make method for getting model data that is separate from regression method

class RegressionClass:
    def __init__(self, degree=5, stddev=1, step=0.05):
        self.x = np.arange(0, 1, step)
        self.y = np.arange(0, 1, step)
        self.stddev = stddev
        self.n = len(self.x)
        self.degree = degree
        self.modeled = False
        self.z_ = self.noise_function(self.x, self.y)

    def generate_data(self, x, y):
        """
        Creates the Franke function
        """
        term1 = 0.75 * np.exp(-(0.25 * (9 * x - 2) ** 2) - 0.25 * ((9 * y - 2) ** 2))
        term2 = 0.75 * np.exp(-((9 * x + 1) ** 2) / 49.0 - 0.1 * (9 * y + 1))
        term3 = 0.5 * np.exp(-(9 * x - 7) ** 2 / 4.0 - 0.25 * ((9 * y - 3) ** 2))
        term4 = -0.2 * np.exp(-(9 * x - 4) ** 2 - (9 * y - 7) ** 2)
        return term1 + term2 + term3 + term4

    def noise_function(self, x, y):
        """
        Adds Gaussian noise to the function f,  ~ N(0,stddev)
        """
        f = self.generate_data(x, y)
        noise = np.random.normal(0, self.stddev, size=f.shape)
        return f + noise

    def plot_franke(self):
        """
        3D plot of the Franke function
        """
        fig = plt.figure()
        xx, yy = np.meshgrid(self.x, self.y)
        ax = fig.gca(projection="3d")
        z = self.noise_function(xx, yy)
        # Plot the surface.
        surf = ax.plot_surface(
            self.x, self.y, z, cmap=cm.coolwarm, linewidth=0, antialiased=False
        )
        # Customize the z axis.
        ax.set_zlim(-0.10, 1.40)
        ax.zaxis.set_major_locator(LinearLocator(10))
        ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f"))
        # Add a color bar which maps values to colors.
        fig.colorbar(surf, shrink=0.5, aspect=5)
        plt.show()

    def design_matrix(self):
        """
        Creates the design matrix
        """
        X = np.zeros((2, self.n))
        X[0, :] = self.x
        X[1, :] = self.y
        X = X.T
        poly = sklpre.PolynomialFeatures(self.degree)
        return poly.fit_transform(X)

    def ordinary_least_squares(self):
        """
        Calculates ordinary least squares regression and the variance of
        estimated parameters
        """
        X = self.design_matrix()
        XTX = X.T @ X
        XTz = X.T @ self.z_
        beta = np.linalg.solve(XTX, XTz)  # solves XTXbeta = XTz
        beta_variance = self.stddev ** 2 * np.linalg.pinv(XTX)
        self.beta, self.beta_variance_ = beta, np.diag(beta_variance)
        self.modeled = True

    @property
    def eval_model(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")
        return self.design_matrix() @ self.beta

    @property
    def mean_squared_error(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")

        return np.sum((self.z_ - self.eval_model) ** 2) / self.n

    @property
    def r_squared(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")
        z = self.z_
        return 1 - np.sum((z - self.eval_model) ** 2) / np.sum((z - np.mean(z)) ** 2)

    @property
    def beta_variance(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")
        return self.beta_variance_

if __name__ == "__main__":
    np.random.seed(100)
    test = RegressionClass(degree=5, stddev=1, step=0.05)
    test.ordinary_least_squares()
    print(f"MSE {test.mean_squared_error}")
    print(f"R2 score {test.r_squared}")
    print(f"Beta variance {test.beta_variance}")
    plt.plot(test.eval_model, color="blue")
    plt.plot(test.z_, color="red")
    plt.show()


import httplib2
import json

from flask import  current_app, session
from sqlalchemy import alias, and_, or_

from models.base import db_session
from models.auth import Auth
from models.block import Block
from models.follow import Follow
from models.user import User
from models.user_auth import UserAuth

from utils.config_utils import load_config
from utils.get_from import get_from

class UserController:
  #TODO: Make sure that you can't set your username to a number to get another person's account
  def _get_user(self, identifier):
    if identifier.__class__ is int:
      user = User.query.filter(User.user_id==identifier).first()
    else:
      user = User.query.filter(User.username==identifier).first()
    return user

  def _logout(self):
    from app import oauth2
    if 'user' in session:
      del session['user']
    session.modified = True
    oauth2.storage.delete()

  def _request_user_info(self, credentials):
    http = httplib2.Http()
    credentials.authorize(http)
    resp, content = http.request('https://www.googleapis.com/plus/v1/people/me')

    if resp.status != 200:
      current_app.logger.error(
        "Error while obtaining user profile: \n%s: %s",
        resp,
        content
      )
      return None

    profile = json.loads(content.decode('utf-8'))

    google_auth_id = profile['id']
    email = profile['emails'][0]['value']
    user = {
      'username': email.split("@")[0],
      'email': email,
      'display_name': profile['displayName'],
      'first_name': get_from(profile, ['name', 'givenName']),
      'last_name': get_from(profile, ['name', 'familyName']),
      'image_url': get_from(profile, ['image', 'url']),
    }

    row_user_auth = UserAuth.query.filter(
      and_(
        UserAuth.auth_key==Auth.GOOGLE,
        UserAuth.auth_id==google_auth_id
      )
    ).first()
    if not row_user_auth:
      row_user = User(**user)
      db_session.add(row_user)
      db_session.commit()

      row_user_auth = UserAuth(
        user_id=row_user.user_id,
        auth_key=Auth.GOOGLE,
        auth_id=google_auth_id
      )
      db_session.add(row_user_auth)
      db_session.commit()
    else:
      row_user = row_user_auth.user
      for k,v in user.items():
        setattr(row_user, k, v)
      db_session.merge(row_user)
      db_session.commit()

    session['user'] = row_user.to_json()

  def block_user(self, identifier, active):
    current_user = self.current_user
    user = self._get_user(identifier)

    row_block = Block(
      user_id = current_user.user_id,
      block_id = user.user_id,
      active = active
    )
    db_session.merge(row_block)
    db_session.commit()

    return self._get_user(identifier)

  @property
  def current_user_id(self):
    user_id = None
    if 'user' in session:
      user_id = session['user']['user_id']
    return user_id

  @property
  def current_user(self):
    user_id = self.current_user_id
    user = None
    if user_id:
      user = User.query.filter(User.user_id == user_id).first()
    return user

  def follow_user(self, identifier, active):
    current_user = self.current_user
    user = self._get_user(identifier)

    row_follow = Follow(
      user_id = current_user.user_id,
      follow_id = user.user_id,
      active = active
    )
    db_session.merge(row_follow)
    db_session.commit()

    return self._get_user(identifier)

  def get_blocking(self, user=None):
    if user is None: user = self.current_user
    return user.blocked_users.filter(Block.active).all()

  def get_followers(self, user=None):
    if user is None: user = self.current_user
    return user.follower_users.filter(Follow.active).all()

  def get_following(self, user=None):
    if user is None: user = self.current_user
    return user.following_users.filter(Follow.active).all()

  # TODO: Add ranking algorithm
  def get_following_recommended(self, user=None):
    if user is None: user = self.current_user

    already_following_user_ids = alias(
      db_session.query(
        Follow.follow_id
      ).select_from(
        Follow
      ).filter(
        and_(
          Follow.user_id == user.user_id,
          Follow.active
        )
      ),
      'already_following_user_ids'
    )

    blocking_user_ids = alias(
      db_session.query(
        Block.block_id
      ).select_from(
        Block
      ).filter(
        and_(
          Block.user_id == user.user_id,
          Block.active
        )
      ),
      'blocking_user_ids'
    )

    query = db_session.query(
      User
    ).filter(
      and_(
        User.user_id != user.user_id,
        ~User.user_id.in_(already_following_user_ids),
        ~User.user_id.in_(blocking_user_ids)
      )
    ).limit(
      5
    ).all()

    return query

  def get_user(self, identifier):
    return self._get_user(identifier)

  def get_users(self):
    return User.query.all()


import json

import requests
from django.conf import settings

from core.models import Configuration

def newsletter_registration(email):
    configuration = Configuration.get_solo()

    headers = {
        "accept": "application/json",
        "content-type": "application/json",
        "api-key": settings.SIB_API_KEY,
    }

    data = {
        "email": email,
        # "attributes": {
        #     "FIRSTNAME": "",
        #     "LASTNAME": ""
        # },
        "includeListIds": [int(settings.SIB_NEWSLETTER_LIST_ID)],
        "templateId": int(settings.SIB_NEWSLETTER_DOI_TEMPLATE_ID),
        "redirectionUrl": configuration.application_frontend_url + "?newsletter=confirmed",
        "updateEnabled": True,
    }

    return requests.post(settings.SIB_CONTACT_DOI_ENDPOINT, headers=headers, data=json.dumps(data))


#!/usr/bin/env python3
import re
import argparse

class HostNameCleaner:
    def __init__(self, orig_file):
        self.raw_file = orig_file
        self.hostnames = []
        self.clean_hostnames()

    def clean_hostnames(self):
        """
            Passed an Opened file
            Check if each line is a valid Hostname
            if line fails, return don't return that line
        """
        file_by_lines = self.raw_file.read().split("\n")
        for line in file_by_lines:
            if len(line) > 0 and line[0] != '#':
                host_sanitize_l1 = HostNameCleaner.remove_wildcard(line)
                host_sanitize_l2 = HostNameCleaner.is_valid_hostname(host_sanitize_l1)
                if host_sanitize_l2 is not None:
                    self.hostnames.append(host_sanitize_l2)

    @staticmethod
    def remove_wildcard(hostname):
        if hostname[0:2] == "*.":
            hostname = hostname[2:]
            hostname = HostNameCleaner.remove_wildcard(hostname)
        return hostname

    @staticmethod
    def is_valid_hostname(hostname):
        hostname_regex = re.compile("(?!-)[A-Z\d-]{5,63}(?<!-)$", re.IGNORECASE)
        num_periods = re.findall(r'\.', hostname)
        num_non_alpha = re.findall(r'\W', hostname)
        if len(hostname) > 255:
            return None
        if len(num_periods) == 0 or len(num_periods) > 4 or len(num_non_alpha) > len(num_periods):
            return None
        if len(re.findall(r'\s+', hostname)) > 0:    # remove hostnames with whitespace ( spaces / tabs )
            return None
        if all(hostname_regex.match(x) for x in hostname.split(".")) is not True:
            return hostname
        return None

if __name__ == '__main__':
    RE = re.compile(' +')
    parser = argparse.ArgumentParser(description="PyOpenSSL")
    parser.add_argument('--infile', type=argparse.FileType('r', encoding='UTF-8'), required=True)
    args = parser.parse_args()

    with args.infile as file:
        cleaner = HostNameCleaner(file)
        print(cleaner.hostnames)

########### input ###########
# no.commas,please
# stackoverflow.com
# httpbin.org
# weirddtstst
# GGorge. loves apples.
# *.google.com
# tab should  be  removed
# ....%%%%%....@@£££

########### Result ###########
# stackoverflow.com
# httpbin.org
# google.com


##############################################################################################
# PURPOSE
#   Measures the flux of the emission lines of the spectra by integrating the adjusted Gaussian function on these lines
#
# CREATED BY:
#   Felícia Palacios
#
# ADAPTED BY:
#   Vitor Eduardo Buss Bootz
#
# CALLING SEQUENCE
#   python Felícia_Palácios_Flux_Calculator.py     --> In terminal
#
# INPUT PARAMETERS
#   output_dir            --> Output directory name
#   output_file           --> Output filename
#   starlight_input_dir   --> Directory of input files
#   starlight_output_dir  --> Directory of input files
#
# OUTPUT
#   sample_flux_lines.csv (flag_correction == 'off')
#   corrections.csv (flag_correction == 'on')
#   PNG images showing the adjusts of the function to check its quality
#
# REQUIRED SCRIPTS
#
#
# COMMENTS
#   Vacuum wavelengths used
#   The 'flag_correction' flag allows the modification of problematic spectra without modifying the well-ajusted spectra
#
##############################################################################################

##############################################################################################
# INPUTS
##############################################################################################

def interp(xin,xout,yin): #Tentar arrumar isso no futuro.. problemas na conversão entre R e Python
    Ninterpol = len(xout)
    yout = np.array([])

    for k in range(Ninterpol):
        t = xin[xin < xout[k]]
        tind = len(t)

        if tind <= 0: tind = 1
        if tind >= len(xin): tind = len(xin) - 1
        t1 = xin[tind - 1]
        t2 = xin[tind]
        t3 = xin[tind + 1]
        tx = xout[k]

        A = (tx - t1) / (t3 - t1)
        B = (tx - t2) / (t3 - t2)
        C = (tx - t3) / (t2 - t1)
        D = (tx - t1) / (t3 - t2)
        E = (tx - t2) / (t3 - t1)

        G = (tx - t2) / (t3 - t2)
        H = (tx - t1) / (t2 - t1)

        if (t1 != t2) & (t2 != t3):
            yout = np.append(yout, yin[tind+1] * A * B - yin[tind] * D * C + yin[tind-1] * E * C)

        if (t1 == t2):
            yout = np.append(yout, yin[tind+1] - yin[tind]) * G + yin[tind]

        if(t2 == t3):
            yout = np.append(yout, yin[tind] - yin[tind-1]) * H + yin[tind-1]

    return(yout)

def interp1(xin,xout,yin):
    Ninterpol = len(xout)
    yout = np.array([])

    for k in range(Ninterpol):
        t = xin[xin < xout[k]]
        tind = len(t) -1 #VERIFICAR SE ESTE -1 NÃO INTERFERE NOS RESULTADOS

        if tind <= 0: tind = 1
        if tind >= len(xin): tind = len(xin) - 1
        t1 = xin[tind - 1]
        t2 = xin[tind]
        t3 = xin[tind + 1]
        tx = xout[k]

        A = (tx - t1) / (t3 - t1)
        B = (tx - t2) / (t3 - t2)
        C = (tx - t3) / (t2 - t1)
        D = (tx - t1) / (t3 - t2)
        E = (tx - t2) / (t3 - t1)

        G = (tx - t2) / (t3 - t2)
        H = (tx - t1) / (t2 - t1)

        if (t1 != t2) & (t2 != t3):
            yout = np.append(yout, yin[tind+1] * A * B - yin[tind] * D * C + yin[tind-1] * E * C)

        if (t1 == t2):
            yout = np.append(yout, yin[tind+1] - yin[tind]) * G + yin[tind]

        if(t2 == t3):
            yout = np.append(yout, yin[tind] - yin[tind-1]) * H + yin[tind-1]

    return(yout)

def interp2(xin,xout,yin):
    Ninterpol = len(xout)
    yout = np.array([])

    for k in range(Ninterpol):
        t = xin[xin < xout[k]]
        tind = len(t) -2 #VERIFICAR SE ESTE -1 NÃO INTERFERE NOS RESULTADOS

        if tind <= 0: tind = 1
        if tind >= len(xin): tind = len(xin) - 1
        t1 = xin[tind - 1]
        t2 = xin[tind]
        if k != Ninterpol:
            t3 = xin[tind + 1]
        tx = xout[k]

        A = (tx - t1) / (t3 - t1)
        B = (tx - t2) / (t3 - t2)
        C = (tx - t3) / (t2 - t1)
        D = (tx - t1) / (t3 - t2)
        E = (tx - t2) / (t3 - t1)

        G = (tx - t2) / (t3 - t2)
        H = (tx - t1) / (t2 - t1)

        if (t1 != t2) & (t2 != t3):
            yout = np.append(yout, yin[tind+1] * A * B - yin[tind] * D * C + yin[tind-1] * E * C)

        if (t1 == t2):
            yout = np.append(yout, yin[tind+1] - yin[tind]) * G + yin[tind]

        if(t2 == t3):
            yout = np.append(yout, yin[tind] - yin[tind-1]) * H + yin[tind-1]

    return(yout)

import numpy as np
import pandas as pd
import astropy.io.fits as fits
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import math
import copy
from matplotlib.backends.backend_pdf import PdfPages
import datetime

#declara as função gaussianas e os contínuos que serão ajustados aos dados
def gauss(x, a, b, c, d, cs, e, f, g, h, i, j, k, l, s1, s2):
    step_min_oiii = np.heaviside(lam_final - min_oiii, 0)
    step_max_oiii = np. heaviside(lam_final - max_oiii, 0)
    step_min_hbeta = np.heaviside(lam_final - min_hbeta, 0)
    step_max_hbeta = np.heaviside(lam_final - max_hbeta, 0)
    step_min_halpha = np.heaviside(lam_final - min_nii, 0)
    step_max_halpha = np.heaviside(lam_final - max_nii, 0)
    step_min_oii = np.heaviside(lam_final - min_oii, 0)
    step_max_oii = np.heaviside(lam_final - max_oii, 0)
    step_min_sii = np.heaviside(lam_final - min_sii, 0)
    step_max_sii = np.heaviside(lam_final - max_sii, 0)

    r1 = a*(step_min_oiii - step_max_oiii)
    r2 = b*(step_min_halpha - step_max_halpha)
    r3 = c*(step_min_oii - step_max_oii)
    r4 = d*(step_min_hbeta - step_max_hbeta)
    r5 = cs*(step_min_sii - step_max_sii)

    w_oiii1 = (x - e)/f
    alpha_oiii1 = (1./math.sqrt(2.*math.pi))*np.exp((-w_oiii1**2.)/2.)*g

    cenoiii2 = lam_oiii2 + (lam_oiii2/lam_oiii1)*(e - lam_oiii1)
    sigoiii2 = (lam_oiii2/lam_oiii1)*f
    ampoiii2 = g/3.
    w_oiii2 = (x - cenoiii2)/sigoiii2
    alpha_oiii2 = (1./math.sqrt(2.*math.pi))*np.exp((-w_oiii2**2.)/2.)*ampoiii2

    cenhbeta = lam_hbeta + (lam_hbeta/lam_oiii1)*(e - lam_oiii1)
    sighbeta = (lam_hbeta/lam_oiii1)*f
    w_hbeta = (x - cenhbeta)/sighbeta
    alpha_hbeta = (1./math.sqrt(2.*math.pi))*np.exp((-w_hbeta**2.)/2.)*h

    cenhalpha = lam_halpha + (lam_halpha/lam_oiii1)*(e - lam_oiii1)
    sighalpha = (lam_halpha/lam_oiii1)*f
    w_halpha = (x - cenhalpha)/sighalpha
    alpha_halpha = (1./math.sqrt(2.*math.pi))*np.exp((-w_halpha**2.)/2.)*i

    cennii1 = lam_nii1 + (lam_nii1/lam_oiii1)*(e - lam_oiii1)
    signii1 = (lam_nii1/lam_oiii1)*f
    w_nii1 = (x - cennii1)/signii1
    alpha_nii1 = (1./math.sqrt(2.*math.pi))*np.exp((-w_nii1**2.)/2.)*j

    cennii2 = lam_nii2 + (lam_nii2/lam_oiii1)*(e - lam_oiii1)
    signii2 = (lam_nii2/lam_oiii1)*f
    ampnii2 = j/3.
    w_nii2 = (x - cennii2)/signii2
    alpha_nii2 = (1./math.sqrt(2.*math.pi))*np.exp((-w_nii2**2.)/2.)*ampnii2

    cenoii1 = lam_oii1 + (lam_oii1/lam_oiii1)*(e - lam_oiii1)
    sigoii1 = (lam_oii1/lam_oiii1)*f
    w_oii1 = (x - cenoii1)/sigoii1
    alpha_oii1 = (1./math.sqrt(2.*math.pi))*np.exp((-w_oii1**2.)/2.)*k

    cenoii2 = lam_oii2 + (lam_oii2/lam_oiii1)*(e - lam_oiii1)
    sigoii2 = (lam_oii2/lam_oiii1)*f
    w_oii2 = (x - cenoii2)/sigoii2
    alpha_oii2 = (1./math.sqrt(2.*math.pi))*np.exp((-w_oii2**2.)/2.)*l

    censii1 = lam_sii1 + (lam_sii1/lam_oiii1)*(e - lam_oiii1)
    sigsii1 = (lam_sii1/lam_oiii1)*f
    w_sii1 = (x - censii1)/sigsii1
    alpha_sii1 = (1./math.sqrt(2.*math.pi))*np.exp((-w_sii1**2.)/2.)*s1

    censii2 = lam_sii2 + (lam_sii2/lam_oiii1)*(e - lam_oiii1)
    sigsii2 = (lam_sii2/lam_oiii1)*f
    w_sii2 = (x - censii2)/sigsii2
    alpha_sii2 = (1./math.sqrt(2.*math.pi))*np.exp((-w_sii2**2.)/2.)*s2

    return (r1 + r2 + r3 + r4 + r5 + (alpha_oiii1/f) + (alpha_oiii2/sigoiii2) + (alpha_hbeta/sighbeta)
+ (alpha_halpha/sighalpha) + (alpha_nii1/signii1) + (alpha_nii2/signii2) + (alpha_oii1/sigoii1)
+ (alpha_oii2/sigoii2) + (alpha_sii1/sigsii1) + (alpha_sii2/sigsii2))

#comprimentos de onda de laboratório
lam_oiii1 = 5008.239
lam_oiii2 = 4960.295
lam_hbeta = 4862.721
lam_halpha = 6564.61
lam_nii1 = 6585.27
lam_nii2 = 6549.86
lam_oii1 = 3727.092
lam_oii2 = 3729.875

lam_sii1 = 6718.29
lam_sii2 = 6732.68

###
flag_correction = 'off'
###

### FILES & FOLDERS ###
starlight_input_dir = '/home/vitorbootz/research/files_input_starlight/'
starlight_output_dir = '/home/vitorbootz/research/files_output_starlight/'
if flag_correction == 'on':  spectra_z_file = '/home/vitorbootz/research/aux_files/to_correct.csv'
if flag_correction == 'off': spectra_z_file = '/home/vitorbootz/research/aux_files/galaxy_list.csv'
output_dir = '/home/vitorbootz/research/flux_measurements/'

if flag_correction == 'on':  output_file = 'corrections.csv'
if flag_correction == 'off': output_file = 'sample_flux_lines.csv'

### OUTPUT FILE HEADER ###
header = pd.DataFrame(['lcgID','extension','ra','dec','OIII_5008','OIII_4959', 'Hb', 'Ha', 'NII_6583', 'NII_6548', 'OII1', 'OII2', 'SII_6718', 'SII_6732']).T
header.to_csv(output_dir+output_file, index=False, mode='w', header=False)

### SELECTION ###
data = pd.read_csv(spectra_z_file, sep=',')  # Lista de spectros e redshifts
selection = (data['onoff'] == 1) & (data['extension'] < 100) #Seleciona apenas as lcgs do sdss e as do gemini com abertura = 3 arcsec
data = data[selection]
data.index = range(len(data))

for i in range(len(data)):

    if data.flag_sdss[i] == 0:
        file_vis = (starlight_input_dir + 'input_starlight_gemini_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.csv')
        file_syn = (starlight_output_dir + 'output_starlight_gemini_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.csv')

    if data.flag_sdss[i] == 1:
        file_vis = (starlight_input_dir + 'input_starlight_sdss_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.csv')
        if (data.extension[i] > 10) & (data.extension[i] < 20):
            file_syn = (starlight_output_dir + 'output_starlight_sdss_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]-10) + '.csv')
        else:
            file_syn = (starlight_output_dir + 'output_starlight_sdss_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.csv')

    hdu = pd.read_csv(file_vis, delim_whitespace=True, engine='python', header = None)
    starlight = pd.read_csv(file_syn, skiprows = 306, delim_whitespace=True, engine='python', header = None)

### O range escolhido para o starlight pega o menor e o maior lambda entre os n espectros de cada grupo ###
### Portanto, tem casos de lambdas iniciais e finais maiores e menores nos inputs em relação ao output e vice e versa ###
### Por isso aplico estas correções, para que tanto o input quando o output do starlight fiquem com o mesmo lambda range ###
    if data.flag_sdss[i] == 0:
        if (hdu[0][0] <= starlight[0][0]) & (hdu[0][len(hdu[0])-1] >= starlight[0][len(starlight[0])-1]):
            hdu = hdu[(hdu[0] >= starlight[0][0]) & (hdu[0] <= starlight[0][len(starlight[0])-1])]
            hdu.index = range(len(hdu))
        elif (hdu[0][0] > starlight[0][0]) & (hdu[0][len(hdu[0])-1] <= starlight[0][len(starlight[0])-1]):
            starlight = starlight[(starlight[0] >= hdu[0][0]) & (starlight[0] <= hdu[0][len(hdu[0])-1])]
            starlight.index = range(len(starlight))
        elif (hdu[0][0] >= starlight[0][0]) & (hdu[0][len(hdu[0])-1] >= starlight[0][len(starlight[0])-1]):
            starlight = starlight[starlight[0] >= hdu[0][0]]
            starlight.index = range(len(starlight))
            hdu = hdu[hdu[0] <= starlight[0][len(starlight[0])-1]]
            hdu.index = range(len(hdu))
        elif (hdu[0][0] <= starlight[0][0]) & (hdu[0][len(hdu[0])-1] <= starlight[0][len(starlight[0])-1]):
            hdu = hdu[hdu[0] >= starlight[0][0]]
            hdu.index = range(len(hdu))
            starlight = starlight[starlight[0] <= hdu[0][len(hdu[0])-1]]
            starlight.index = range(len(starlight))

        l_obs = hdu[0]
        f_obs = hdu[1]
        sig = hdu[2]
        f_syn = starlight[2]

#### Os espectros de input do sloan estão bem feios, então aplico uma interpolação aqui ###
    if data.flag_sdss[i] == 1:

        lamb = hdu[0]
        flux = hdu[1]
        l_starlight = starlight[0]
        f_syn = starlight[2]
        sig   = hdu[2]

        try:
            f_obs = interp(lamb, l_starlight, flux)
            sig = interp(lamb, l_starlight, sig)
            l_obs = l_starlight
        except:
            try:
                f_obs = interp1(lamb, l_starlight, flux)
                sig = interp1(lamb, l_starlight, sig)
                l_obs = l_starlight
            except:
                try:
                    f_obs = interp2(lamb, l_starlight, flux)
                    sig = interp2(lamb, l_starlight, sig)
                    l_obs = l_starlight
                except:
                    print('Erro na interpolação: LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]))
                    continue
################
### Exceções: aqui foi necessário aplicar a correção feita nos arquivos do gemini E interpolar o espectro ###
    if (data.lcgID[i] == 2764) & (data.extension[i] == 1):
        hdu = pd.read_csv(file_vis, delim_whitespace=True, engine='python', header = None)
        starlight = pd.read_csv(file_syn, skiprows = 306, delim_whitespace=True, engine='python', header = None)
        starlight = starlight[starlight[0] >= hdu[0][0]]
        starlight.index = range(len(starlight))
        hdu = hdu[hdu[0] <= starlight[0][len(starlight[0])-1]]
        hdu.index = range(len(hdu))
        lamb = hdu[0]
        flux = hdu[1]
        l_starlight = starlight[0]
        f_syn = starlight[2]
        sig   = hdu[2]
        f_obs = interp1(lamb, l_starlight, flux)
        sig = interp1(lamb, l_starlight, sig)
        l_obs = l_starlight
  #  if (data.lcgID[i] == 2023) & (data.extension[i] == 1):
  #      hdu = pd.read_csv(file_vis, delim_whitespace=True, engine='python', header = None)
  #      starlight = pd.read_csv(file_syn, skiprows = 306, delim_whitespace=True, engine='python', header = None)
  #
  #      interp2023 = pd.read_csv('/home/vitorbootz/research/aux_files/interp_2023.csv')
  #      interp2023.columns = ['n','flux_out']
  #      interp2023 = interp2023[interp2023.n>118].dropna()
  #
  #      interpsig2023 = pd.read_csv('/home/vitorbootz/research/aux_files/interp_sig_2023.csv')
  #      interpsig2023.columns = ['n','sig_out']
  #      interpsig2023 = interpsig2023[interpsig2023.n>118].dropna()
  #
  #      starlight = starlight[starlight[0] >= hdu[0][0]]
  #      starlight.index = range(len(starlight))
  #      hdu = hdu[hdu[0] <= starlight[0][len(starlight[0])-1]]
  #      hdu.index = range(len(hdu))
  #      lamb = hdu[0]
  #      flux = hdu[1]
  #      l_starlight = starlight[0]
  #      f_syn = starlight[2]
  #      sig   = hdu[2]
  #      f_obs = interp2023.flux_out
  #      sig = interpsig2023.sig_out
  #      l_obs = l_starlight
################

    cube = (f_obs - f_syn)
    #cube = (f_obs)
    cube.dropna(inplace=True)

    n = len(l_obs)
    l0 = l_obs[0]     #primeiro valor de lambda
    step = l_obs[7] - l_obs[6]          #intervalo delta lambda
    unit = 'Angstrom' #unidade

    #cria um vetor com comprimentos de onda
    if unit == 'Angstrom':
        lam = np.arange(n)*step + l0
    else:
        if unit == 'nm':
            lam = np.arange(n)*step*10 + l0*10

    #centro das linhas observadas
    cen_oiii1 = 5006.843
    cen_oiii2 = lam_oiii2 + (lam_oiii2/lam_oiii1)*(cen_oiii1 - lam_oiii1)
    cen_hbeta = lam_hbeta + (lam_hbeta/lam_oiii1)*(cen_oiii1 - lam_oiii1)
    cen_halpha = lam_halpha + (lam_halpha/lam_oiii1)*(cen_oiii1 - lam_oiii1)
    cen_nii1 = lam_nii1 + (lam_nii1/lam_oiii1)*(cen_oiii1 - lam_oiii1)
    cen_nii2 = lam_nii2 + (lam_nii2/lam_oiii1)*(cen_oiii1 - lam_oiii1)
    cen_oii1 = lam_oii1 + (lam_oii1/lam_oiii1)*(cen_oiii1 - lam_oiii1)
    cen_oii2 = lam_oii2 + (lam_oii2/lam_oiii1)*(cen_oiii1 - lam_oiii1)

    cen_sii1 = lam_sii1 + (lam_sii1/lam_oiii1)*(cen_oiii1 - lam_oiii1)
    cen_sii2 = lam_sii2 + (lam_sii2/lam_oiii1)*(cen_oiii1 - lam_oiii1)

    #cria janela do OIII
    min_oiii = cen_oiii2 - 20
    max_oiii = cen_oiii1 + 20

    imin_oiii = (np.abs(lam - min_oiii)).argmin()
    imax_oiii = (np.abs(lam - max_oiii)).argmin()

    lam1 = lam[imin_oiii : imax_oiii]
    cube1 = cube[imin_oiii : imax_oiii]
    sig1 = sig[imin_oiii : imax_oiii]

    #cria janela do H beta
    min_hbeta = cen_hbeta - 20
    max_hbeta = cen_hbeta + 20

    imin_hbeta = (np.abs(lam - min_hbeta)).argmin()
    imax_hbeta = (np.abs(lam - max_hbeta)).argmin()

    lam2 = lam[imin_hbeta : imax_hbeta]
    cube2 = cube[imin_hbeta : imax_hbeta]
    sig2 = sig[imin_hbeta : imax_hbeta]

    #cria janela do NII
    min_nii = cen_nii2 - 20
    max_nii = cen_nii1 + 20

    imin_nii = (np.abs(lam - min_nii)).argmin()
    imax_nii = (np.abs(lam - max_nii)).argmin()

    lam3 = lam[imin_nii : imax_nii]
    cube3 = cube[imin_nii : imax_nii]
    sig3 = sig[imin_nii : imax_nii]

    #cria janela do SII
    min_sii = cen_sii1 - 30
    max_sii = cen_sii2 + 30

    imin_sii = (np.abs(lam - min_sii)).argmin()
    imax_sii = (np.abs(lam - max_sii)).argmin()

    lam5 = lam[imin_sii : imax_sii]
    cube5 = cube[imin_sii : imax_sii]
    sig5 = sig[imin_sii : imax_sii]

    #cria janela do OII
    min_oii = cen_oii1 - 20
    max_oii = cen_oii2 + 20

    #abre o arquivo do espectro visual se as linhas de OII cairem fora do infravermelho
    if min_oii < 10000:
        #head_vis = hdu_vis[0].header
        #data_vis = hdu_vis[0].data
        #sig_vis = hdu_vis[1].data/1e-19

        #cube_vis = data_vis/1e-19

        cube_vis = cube
        sig_vis = sig

        n_vis = len(cube_vis)
        l0_vis = l_obs[0]
        step_vis = l_obs[7] - l_obs[6]
        unit_vis = 'Angstrom'

        if unit_vis == 'Angstrom':
            lam_vis = np.arange(n_vis)*step_vis + l0_vis
        else:
            if unit_vis == 'nm':
                lam_vis = np.arange(n_vis)*step_vis*10 + l0_vis*10

        imin_oii = (np.abs(lam_vis - min_oii)).argmin()
        imax_oii = (np.abs(lam_vis - max_oii)).argmin()

        lam4 = lam_vis[imin_oii : imax_oii]
        cube4 = cube_vis[imin_oii : imax_oii]
        sig4 = sig_vis[imin_oii : imax_oii]

    else:

        imin_oii = (np.abs(lam - min_oii)).argmin()
        imax_oii = (np.abs(lam - max_oii)).argmin()

        lam4 = lam[imin_oii : imax_oii]
        cube4 = cube[imin_oii : imax_oii]
        sig4 = sig[imin_oii : imax_oii]

    #junta as janelas
    lam_fin1 = np.append(lam1,lam2)
    cube_fin1 = np.append(cube1,cube2)
    sig_fin1 = np.append(sig1,sig2)
    lam_fin2 = np.append(lam_fin1, lam3)
    cube_fin2 = np.append(cube_fin1, cube3)
    sig_fin2 = np.append(sig_fin1, sig3)
    lam_fin = np.append(lam_fin2, lam4)
    cube_fin = np.append(cube_fin2, cube4)
    sig = np.append(sig_fin2, sig4)

    lam_final = np.append(lam_fin, lam5)
    cube_final = np.append(cube_fin, cube5)
    sig_final = np.append(sig, sig5)

    #chute inicial dos parâmetros
    if flag_correction == 'off': guess = np.array([10, 10, 10, 10, 10, cen_oiii1, 60*cen_oiii1/(2.99792*1e5), 100, 100, 100, 100, 100, 100, 100, 100])
    if flag_correction == 'on':  guess = np.array([10, 10, 10, 10, 10, cen_oiii1, 60*cen_oiii1/(2.99792*1e5), 100, 100, 100, 100, 100, 100, 100, 100])

    #ajuste das funções aos dados
    try:
        if (data.lcgID[i] == 3866) & (data.extension[i] == 13):
            p, pv = curve_fit(gauss, lam_final, cube_final, guess, sigma=None, method='lm')
        else:
            p, pv = curve_fit(gauss, lam_final, cube_final, guess, sigma=sig_final, method='lm')
    except:
        print('Erro no curve_fit: LCG'+str(data.lcgID[i])+'_'+str(data.extension[i]))
        continue

    #parâmetros do ajuste
    a1 = p[0] #continuo 1
    a2 = p[1] #continuo 2
    a3 = p[2] #continuo 3
    a4 = p[3] #continuo 4
    a5 = p[4] #continuo 5
    center_oiii1 = p[5]
    sig_oiii1 = copy.deepcopy(p[6])
    amp_oiii1 = p[7]
    amp_hbeta = p[8]
    amp_halpha = p[9]
    amp_nii1 = p[10]
    amp_oii1 = p[11]
    amp_oii2 = p[12]
    amp_sii1 = p[13]
    amp_sii2 = p[14]

    #outros parâmetros
    center_oiii2 = lam_oiii2 + (lam_oiii2/lam_oiii1)*(center_oiii1 - lam_oiii1)
    center_hbeta = lam_hbeta + (lam_hbeta/lam_oiii1)*(center_oiii1 - lam_oiii1)
    center_halpha = lam_halpha + (lam_halpha/lam_oiii1)*(center_oiii1 - lam_oiii1)
    center_nii1 = lam_nii1 + (lam_nii1/lam_oiii1)*(center_oiii1 - lam_oiii1)
    center_nii2 = lam_nii2 + (lam_nii2/lam_oiii1)*(center_oiii1 - lam_oiii1)
    center_oii1 = lam_oii1 + (lam_oii1/lam_oiii1)*(center_oiii1 - lam_oiii1)
    center_oii2 = lam_oii2 + (lam_oii2/lam_oiii1)*(center_oiii1 - lam_oiii1)

    center_sii1 = lam_sii1 + (lam_sii1/lam_oiii1)*(center_oiii1 - lam_oiii1)
    center_sii2 = lam_sii2 + (lam_sii2/lam_oiii1)*(center_oiii1 - lam_oiii1)

    sigma_oiii2 = (lam_oiii2/lam_oiii1)*sig_oiii1
    sigma_hbeta = (lam_hbeta/lam_oiii1)*sig_oiii1
    sigma_halpha = (lam_halpha/lam_oiii1)*sig_oiii1
    sigma_nii1 = (lam_nii1/lam_oiii1)*sig_oiii1
    sigma_nii2 = (lam_nii2/lam_oiii1)*sig_oiii1
    sigma_oii1 = (lam_oii1/lam_oiii1)*sig_oiii1
    sigma_oii2 = (lam_oii2/lam_oiii1)*sig_oiii1

    sigma_sii1 = (lam_sii1/lam_oiii1)*sig_oiii1
    sigma_sii2 = (lam_sii2/lam_oiii1)*sig_oiii1

    amp_oiii2 = amp_oiii1/3.
    amp_nii2 = amp_nii1/3.

    vel = p[5]*(299792.458/p[4])

    #plot dos espectros e ajustes
    fig = plt.figure(figsize=(6,20))
    axis1 = fig.add_subplot(511)
    axis1.plot(lam1,cube1)
    x = lam1
    y = (a1 + amp_oiii1*(1./math.sqrt(2.*math.pi))*np.exp((-((x - center_oiii1)/sig_oiii1)**2.)/2.)/sig_oiii1
         + amp_oiii2*(1./math.sqrt(2.*math.pi))*np.exp((-((x - center_oiii2)/sigma_oiii2)**2.)/2.)/sigma_oiii2)
    axis1.plot(x,y, color = 'red')
    plt.title('LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i])+': OIII Window')

    axis2 = fig.add_subplot(512)
    axis2.plot(lam2, cube2)
    x1 = lam2
    y1 = (a4 + amp_hbeta*(1./math.sqrt(2.*math.pi))*np.exp((-((x1 - center_hbeta)/sigma_hbeta)**2.)/2.)/sigma_hbeta )
    axis2.plot(x1,y1, color = 'red')
    plt.title('LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i])+': H Beta Window')

    axis3 = fig.add_subplot(513)
    axis3.plot(lam3, cube3)
    x2 = lam3
    y2 = (a2 + amp_halpha*(1./math.sqrt(2.*math.pi))*np.exp((-((x2 - center_halpha)/sigma_halpha)**2.)/2.)/sigma_halpha
         + amp_nii1*(1./math.sqrt(2.*math.pi))*np.exp((-((x2 - center_nii1)/sigma_nii1)**2.)/2.)/sigma_nii1
         + amp_nii2*(1./math.sqrt(2.*math.pi))*np.exp((-((x2 - center_nii2)/sigma_nii2)**2.)/2.)/sigma_nii2)
    axis3.plot(x2,y2, color = 'red')
    plt.title('LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i])+': NII Window')

    axis4 = fig.add_subplot(514)
    axis4.plot(lam4,cube4)
    x3 = lam4
    y3 = (a3 + amp_oii1*(1./math.sqrt(2.*math.pi))*np.exp((-((x3 - center_oii1)/sigma_oii1)**2.)/2.)/sigma_oii1
         + amp_oii2*(1./math.sqrt(2.*math.pi))*np.exp((-((x3 - center_oii2)/sigma_oii2)**2.)/2.)/sigma_oii2)
    axis4.plot(x3,y3, color = 'red')
    plt.title('LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i])+': OII Window')

    axis5 = fig.add_subplot(515)
    axis5.plot(lam5,cube5)
    x4 = lam5
    y4 = (a5 + amp_sii1*(1./math.sqrt(2.*math.pi))*np.exp((-((x4 - center_sii1)/sigma_sii1)**2.)/2.)/sigma_sii1
         + amp_sii2*(1./math.sqrt(2.*math.pi))*np.exp((-((x4 - center_sii2)/sigma_sii2)**2.)/2.)/sigma_sii2)
    axis5.plot(x4,y4, color = 'red')

    plt.title('LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i])+': SII Window')

    if flag_correction == 'off':
        if data.flag_sdss[i] == 1:
            fig.savefig('/home/vitorbootz/research/flux_measurements/log_images/' + 'sdss_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.png')
        if data.flag_sdss[i] == 0:
            fig.savefig('/home/vitorbootz/research/flux_measurements/log_images/' + 'gemini_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.png')

    if flag_correction == 'on':
        if data.flag_sdss[i] == 1:
            fig.savefig('/home/vitorbootz/research/flux_measurements/to_correct/' + 'sdss_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.png')
        if data.flag_sdss[i] == 0:
            fig.savefig('/home/vitorbootz/research/flux_measurements/to_correct/' + 'gemini_LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]) + '.png')

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

    ### OUTPUT DATA ###

    amp_oiii1 = round(amp_oiii1,5)
    amp_oiii2 = round(amp_oiii2,5)
    amp_hbeta = round(amp_hbeta,5)
    amp_halpha = round(amp_halpha,5)
    amp_nii1 = round(amp_nii1,5)
    amp_oii1 = round(amp_oii1,5)
    amp_oii2 = round(amp_oii2,5)
    amp_sii1 = round(amp_sii1,5)
    amp_sii2 = round(amp_sii2,5)

    fluxes = pd.DataFrame([data.lcgID[i], data.extension[i], data.ra[i], data.dec[i], amp_oiii1, amp_oiii2, amp_hbeta, amp_halpha, amp_nii1, amp_nii2, amp_oii1, amp_oii2, amp_sii1, amp_sii2]).T
    fluxes[0] = int(fluxes[0])
    fluxes[1] = int(fluxes[1])
    print('LCG' + str(data.lcgID[i]) + '_' + str(data.extension[i]))
    if flag_correction == 'on': fluxes.to_csv(output_dir+output_file, index=False, header=None, mode='a')
    if flag_correction == 'off': fluxes.to_csv(output_dir+output_file, index=False, header=None, mode='a')


from collective.transmogrifier import transmogrifier
from ftw.builder import session
from ftw.builder.testing import BUILDER_LAYER
from ftw.builder.testing import set_builder_session_factory
from ftw.testing import ComponentRegistryLayer
from ftw.testing.quickinstaller import snapshots
from opengever.base import model
from opengever.base.model import create_session
from opengever.meeting.interfaces import IMeetingSettings
from opengever.ogds.base.setup import create_sql_tables
from opengever.ogds.models import BASE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PLONE_FIXTURE
from plone.app.testing import PloneSandboxLayer
from plone.app.testing import ploneSite
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.browserlayer.utils import unregister_layer
from plone.dexterity.schema import SCHEMA_CACHE
from plone.registry.interfaces import IRegistry
from plone.testing import Layer
from plone.testing import z2
from Products.CMFCore.utils import getToolByName
from Testing.ZopeTestCase.utils import setupCoreSessions
from z3c.saconfig import EngineFactory
from z3c.saconfig import GloballyScopedSession
from z3c.saconfig.interfaces import IEngineFactory
from z3c.saconfig.interfaces import IScopedSession
from zope.component import getUtility
from zope.component import provideUtility
from zope.configuration import xmlconfig
from zope.sqlalchemy import datamanager
import transaction

snapshots.disable()

def clear_transmogrifier_registry():
    transmogrifier.configuration_registry._config_info = {}
    transmogrifier.configuration_registry._config_ids = []

def setup_sql_tables():
    # Create opengever.ogds.base SQL tables
    create_sql_tables()

    # Create opengever.globalindex SQL tables
    model.Base.metadata.create_all(create_session().bind)

    # Activate savepoint "support" for sqlite
    # We need savepoint support for version retrieval with CMFEditions.
    if 'sqlite' in datamanager.NO_SAVEPOINT_SUPPORT:
        datamanager.NO_SAVEPOINT_SUPPORT.remove('sqlite')

def truncate_sql_tables():
    tables = BASE.metadata.tables.values() + \
        model.Base.metadata.tables.values()

    session = create_session()

    for table in tables:
        session.execute(table.delete())

class AnnotationLayer(ComponentRegistryLayer):
    """Loads ZML of zope.annotation.
    """

    def setUp(self):
        super(AnnotationLayer, self).setUp()
        import zope.annotation
        self.load_zcml_file('configure.zcml', zope.annotation)

ANNOTATION_LAYER = AnnotationLayer()

class OpengeverFixture(PloneSandboxLayer):

    defaultBases = (PLONE_FIXTURE, BUILDER_LAYER)

    def testSetUp(self):
        super(OpengeverFixture, self).testSetUp()
        setup_sql_tables()

    def testTearDown(self):
        truncate_sql_tables()
        from opengever.testing.sql import reset_ogds_sync_stamp
        with ploneSite() as portal:
            reset_ogds_sync_stamp(portal)
        super(OpengeverFixture, self).testTearDown()

    def setUpZope(self, app, configurationContext):
        # do not install pas plugins (doesnt work in tests)
        from opengever.ogds.base import hooks
        hooks._setup_scriptable_plugin = lambda *a, **kw: None

        xmlconfig.string(
            '<configure xmlns="http://namespaces.zope.org/zope">'

            '  <include package="z3c.autoinclude" file="meta.zcml" />'
            '  <includePlugins package="plone" />'
            '  <includePluginsOverrides package="plone" />'

            '  <include package="opengever.ogds.base" file="tests.zcml" />'
            '  <include package="opengever.setup.tests" />'

            '</configure>',
            context=configurationContext)

        z2.installProduct(app, 'plone.app.versioningbehavior')

        setupCoreSessions(app)

        # Set max subobject limit to 0 -> unlimited
        # In tests this is set to 100 by default
        transient_object_container = app.temp_folder.session_data
        transient_object_container.setSubobjectLimit(0)

    def setUpPloneSite(self, portal):
        self.installOpengeverProfiles(portal)
        self.createMemberFolder(portal)

    def tearDown(self):
        super(OpengeverFixture, self).tearDown()
        clear_transmogrifier_registry()

    def installOpengeverProfiles(self, portal):
        # Copied from metadata.zxml of opengever.policy.base:default
        # The aim is to use the opengever.policy.base:default here, but it
        # changes some things such as the language which will result in
        # lots of failing tests.
        applyProfile(portal, 'plone.app.dexterity:default')
        applyProfile(portal, 'plone.app.registry:default')
        applyProfile(portal, 'opengever.globalindex:default')
        applyProfile(portal, 'opengever.ogds.base:default')
        applyProfile(portal, 'opengever.base:default')
        applyProfile(portal, 'opengever.document:default')
        applyProfile(portal, 'opengever.mail:default')
        applyProfile(portal, 'opengever.dossier:default')
        applyProfile(portal, 'opengever.repository:default')
        applyProfile(portal, 'opengever.journal:default')
        applyProfile(portal, 'opengever.task:default')
        applyProfile(portal, 'opengever.tabbedview:default')
        applyProfile(portal, 'opengever.trash:default')
        applyProfile(portal, 'opengever.inbox:default')
        applyProfile(portal, 'opengever.tasktemplates:default')
        applyProfile(portal, 'opengever.portlets.tree:default')
        applyProfile(portal, 'opengever.contact:default')
        applyProfile(portal, 'opengever.advancedsearch:default')
        applyProfile(portal, 'opengever.sharing:default')
        applyProfile(portal, 'opengever.latex:default')
        applyProfile(portal, 'opengever.meeting:default')
        applyProfile(portal, 'ftw.datepicker:default')
        applyProfile(portal, 'plone.formwidget.autocomplete:default')
        applyProfile(portal, 'plone.formwidget.contenttree:default')
        applyProfile(portal, 'ftw.contentmenu:default')

    def createMemberFolder(self, portal):
        # Create a Members folder.
        setRoles(portal, TEST_USER_ID, ['Manager'])
        portal.invokeFactory('Folder', 'Members')
        portal['Members'].invokeFactory('Folder', TEST_USER_ID)
        setRoles(portal, TEST_USER_ID, ['Member'])

class MemoryDBLayer(Layer):
    """A Layer which only set up a test sqlite db in to the memory
    """

    def testSetUp(self):
        super(MemoryDBLayer, self).testSetUp()
        setup_sql_tables()
        self.session = create_session()

    def testTearDown(self):
        truncate_sql_tables()
        transaction.abort()

def integration_session_factory():
    sess = session.BuilderSession()
    sess.session = create_session()
    return sess

def functional_session_factory():
    sess = session.BuilderSession()
    sess.auto_commit = True
    sess.session = create_session()
    return sess

def memory_session_factory():
    engine_factory = EngineFactory('sqlite:///:memory:')
    provideUtility(
        engine_factory, provides=IEngineFactory, name=u'opengever_db')

    scoped_session = GloballyScopedSession(engine=u'opengever_db')
    provideUtility(
        scoped_session, provides=IScopedSession, name=u'opengever')

    return functional_session_factory()

MEMORY_DB_LAYER = MemoryDBLayer(
    bases=(BUILDER_LAYER,
           set_builder_session_factory(memory_session_factory)),
    name='opengever:core:memory_db')

OPENGEVER_FIXTURE = OpengeverFixture()

OPENGEVER_INTEGRATION_TESTING = IntegrationTesting(
    bases=(OPENGEVER_FIXTURE,
           set_builder_session_factory(integration_session_factory)),
    name="opengever.core:integration")

OPENGEVER_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(OPENGEVER_FIXTURE,
           set_builder_session_factory(functional_session_factory)),
    name="opengever.core:functional")

def activate_filing_number(portal):
    applyProfile(portal, 'opengever.dossier:filing')
    transaction.commit()

def inactivate_filing_number(portal):
    unregister_layer('opengever.dossier.filing')

    portal_types = getToolByName(portal, 'portal_types')
    fti = portal_types.get('opengever.dossier.businesscasedossier')
    fti.behaviors = [behavior for behavior in fti.behaviors
                     if not behavior.endswith('IFilingNumber')]

    SCHEMA_CACHE.invalidate('opengever.dossier.businesscasedossier')

class FilingLayer(PloneSandboxLayer):

    defaultBases = (OPENGEVER_FUNCTIONAL_TESTING,)

    def setUpPloneSite(self, portal):
        activate_filing_number(portal)

    def tearDownPloneSite(self, portal):
        inactivate_filing_number(portal)

OPENGEVER_FUNCTIONAL_FILING_LAYER = FilingLayer()

class MeetingLayer(PloneSandboxLayer):

    def setUpPloneSite(self, portal):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMeetingSettings)
        settings.is_feature_enabled = True
        transaction.commit()

    def tearDownPloneSite(self, portal):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMeetingSettings)
        settings.is_feature_enabled = False
        transaction.commit()

    defaultBases = (OPENGEVER_FUNCTIONAL_TESTING,)

OPENGEVER_FUNCTIONAL_MEETING_LAYER = MeetingLayer()

{'reset_ogds_sync_stamp': 'opengever.testing.sql.reset_ogds_sync_stamp', 'hooks': 'opengever.ogds.base.hooks'}


#!flask/bin/python
from flask import Flask, request, jsonify, abort
from keras.models import load_model
import numpy as np
from keras.preprocessing import image

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello, World!"

@app.route('/predict', methods=['POST'])
def create_task():
#    image_path = 'img_dataset/prediction_img/pipeorhome17.jpg'
    image_path =  request.files['file']
    Prediction_output = []

    product_model = load_model('product.h5')
    test_image = image.load_img(image_path,
                                target_size = (64, 64))

    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
    product_output = product_model.predict(test_image)
    print(product_output)

    if product_output[0][0] == 1 :
        Prediction_output.append("Door")
    elif product_output[0][1] == 1:
         Prediction_output.append("Pipe")

    test_image = image.load_img(image_path,
                                target_size = (64, 64))
    model = load_model('material.h5')
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
    output = model.predict(test_image)
    print(output)

    if output[0][0] == 1 :
        Prediction_output.append("Glass")
    elif output[0][1] == 1:
         Prediction_output.append("Plastic")
    elif output[0][2] == 1:
         Prediction_output.append("Steel")
    elif output[0][3] == 1:
         Prediction_output.append("Wood")

    test_image = image.load_img(image_path,
                                target_size = (64, 64))
    damage_model = load_model('damageOrNot.h5')
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
    damage_output = damage_model.predict(test_image)
    print(damage_output)

    if damage_output[0][0] == 1 :
        Prediction_output.append("Damage")
    elif damage_output[0][1] == 1:
         Prediction_output.append("Not Damage")
    return jsonify({"data" : {
                              "Product":Prediction_output[0],
                              "Meterial":Prediction_output[1],
                              "Condition":Prediction_output[2]
                              }})

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


from typing import List
import subprocess
import os
import random

from .IngestorInterface import IngestorInterface
from .Quote import Quote

class PdfIngestor(IngestorInterface):
    allowed_extensions = ['pdf']

    @classmethod
    def parse(cls, path: str) -> List[Quote]:
        if not cls.can_ingest(path):
            raise Exception('cannot ingest exception')

        tmp = f'.{random.randint(0,100000000)}.txt'
        call = subprocess.call(['pdftotext', path, tmp])

        file_ref = open(tmp, "r")
        quotes = []

        for line in file_ref.readlines():
            line = line.strip('\n\r').strip()
            if len(line) > 0:
                parse = line.split('-')
                new_quote = Quote(parse[0], parse[1])
                quotes.append(new_quote)

        file_ref.close()
        os.remove(tmp)
        return quotes


import urllib.request,json
from .models import NewsSource, NewsArticle
import datetime#wanted to implement search article:later though

# Getting api key
api_key = None
# Getting the movie base url
base_url = None

def configure_request(app):
    global api_key,base_url
    api_key = app.config['API_KEY']
    base_url = app.config['API_BASE_URL']
def get_sources():
    '''
    Function gets the json response to url request
    '''
    get_sources_url = base_url.format(api_key)

    with urllib.request.urlopen(get_sources_url) as url:
        get_sources_data = url.read()
        get_sources_response = json.loads(get_sources_data)

        sources_results = None

        if get_sources_response['sources']:
            sources_results_list = get_sources_response['sources']
            sources_results = process_sources_results(sources_results_list)

    return sources_results

def process_sources_results(sources_list):
    '''
    Function  processes  sources result and transform them to a list of Objects
    Args:
        sources_list: A list of dictionaries that contain sources details
    Returns :
        sources_results: A list of newssources objects
    '''
    sources_results = []
    for source_item in sources_list:
        id = source_item.get('id')
        name = source_item.get('name')
        description = source_item.get('description')
        url = source_item.get('url')

        sources_object = NewsSource(id,name,description,url)
        sources_results.append(sources_object)

    return sources_results

def get_articles(source_name):
    search_movie_url = 'https://newsapi.org/v2/top-headlines?sources={}&apiKey={}'.format(source_name,api_key)
    with urllib.request.urlopen(search_movie_url) as url:
        search_articles_data = url.read()
        search_articles_response = json.loads(search_articles_data)

        search_articles_results = None

        if search_articles_response['articles']:
            search_articles_list = search_articles_response['articles']
            search_articles_results = process_articles_results(search_articles_list)

    return search_articles_results

def process_articles_results(articles_list):
    '''
    Function  processes  articles result and transform them to a list of Objects
    Args:
        articles_list: A list of dictionaries that contain articles details
    Returns :
        articles_results: A list of newssources objects
    '''
    articles_results = []
    for item in articles_list:
        name = item.get('source')
        author=item.get("author")
        title=item.get("title")
        description = item.get('description')
        url = item.get('url')
        urlToImage = item.get('urlToImage')
        publishedAt= item.get("publishedAt")

        articles_object = NewsArticle(name,author,title, description, url, urlToImage, publishedAt)
        articles_results.append(articles_object)

    return articles_results


import os, time, glob, shutil
from pytube import YouTube
import moviepy.editor as mp

def downloadYouTube(videourl, path):
    try:
        yt = YouTube(videourl)
        yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
        if not os.path.exists(path):
            os.makedirs(path)
        yt.download(path, filename=str(round(time.time() * 1000)))
    except Exception as e:
        print("Erro ao baixar link: {}".format(videourl))
        print("[Error]: {}".format(e))

def convertMp3(video):
    try:
        clip = mp.VideoFileClip(video)
        clip.audio.write_audiofile("{}.{}".format(str(round(time.time() * 1000)), "mp3"))
    except Exception as e:
        print("Erro ao converter: {}".format(video))
        print("[Error]: {}".format(e))

def moveToDirectory():
    dst = "./songs_{}".format(str(round(time.time())))
    songs = glob.glob("./*.mp3")

    if(len(songs) == 0):
         return False

    try:
        os.makedirs(dst)
    except:
        print("Erro ao criar pasta")

    for s in songs:
        shutil.move(s, dst)

# Pega lista de vídeos e quebra em arrays
videos = input("Cole os links separado por vírgula: ")
videos = videos.split(",")

path_video = "./videos_{}".format(str(round(time.time())))
for video in videos:
    downloadYouTube(video, path_video)

videos = glob.glob(path_video + "/*.mp4")

# percorre vídeos e converte em música
for video in videos:
    print(video)
    convertMp3(video)

# Move músicas para um diretorio gerado
moveToDirectory()


"""Support for Verisure Smartplugs."""
from __future__ import annotations

from time import monotonic
from typing import Callable, Iterable

from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import VerisureDataUpdateCoordinator

async def async_setup_entry(
    hass: HomeAssistant,
    entry: ConfigEntry,
    async_add_entities: Callable[[Iterable[Entity]], None],
) -> None:
    """Set up Verisure alarm control panel from a config entry."""
    coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
    async_add_entities(
        VerisureSmartplug(coordinator, serial_number)
        for serial_number in coordinator.data["smart_plugs"]
    )

class VerisureSmartplug(CoordinatorEntity, SwitchEntity):
    """Representation of a Verisure smartplug."""

    coordinator: VerisureDataUpdateCoordinator

    def __init__(
        self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
    ) -> None:
        """Initialize the Verisure device."""
        super().__init__(coordinator)
        self.serial_number = serial_number
        self._change_timestamp = 0
        self._state = False

    @property
    def name(self) -> str:
        """Return the name or location of the smartplug."""
        return self.coordinator.data["smart_plugs"][self.serial_number]["area"]

    @property
    def unique_id(self) -> str:
        """Return the unique ID for this alarm control panel."""
        return self.serial_number

    @property
    def is_on(self) -> bool:
        """Return true if on."""
        if monotonic() - self._change_timestamp < 10:
            return self._state
        self._state = (
            self.coordinator.data["smart_plugs"][self.serial_number]["currentState"]
            == "ON"
        )
        return self._state

    @property
    def available(self) -> bool:
        """Return True if entity is available."""
        return (
            super().available
            and self.serial_number in self.coordinator.data["smart_plugs"]
        )

    def turn_on(self, **kwargs) -> None:
        """Set smartplug status on."""
        self.coordinator.verisure.set_smartplug_state(self.serial_number, True)
        self._state = True
        self._change_timestamp = monotonic()

    def turn_off(self, **kwargs) -> None:
        """Set smartplug status off."""
        self.coordinator.verisure.set_smartplug_state(self.serial_number, False)
        self._state = False
        self._change_timestamp = monotonic()


from __future__ import print_function
from espressomd import System, electrostatics, electrostatic_extensions
from espressomd.shapes import Wall
import espressomd
import numpy as np
from threading import Thread
from math import *
from vtf_files import _writevsf, _writevcf, _vtf_pid_map

required_features = ["EXTERNAL_FORCES", "MASS", "ELECTROSTATICS", "LENNARD_JONES"]
espressomd.assert_features(required_features)

def pprint(message):
    print('\n')
    print(str(message).center(80, '.'))

'''-----------------'''
'''GLOBAL PARAMETERS'''
'''-----------------'''

name = "z_density_test"      #z_density, z_density_no_Ez, z_density_long, z_density_long_no_Ez, z_density_short
# System parameters
scale = 5   #scale # no of particles
simulate = True
#simulate = False
num_steps_equilibration = 20  #relax steps
num_configs = 200              # integration steps
integ_steps_per_config = 500   #steps per integration step
Ez = 10     # capacitor on, of
e = 1
e = 1.602e-19

n_part = int(5000)    #Total number of particles, # of Cl + # of Na
n_ionpairs = int(n_part / 2)
density = 1.1138      # Define density, timestep and temperature
time_step = 0.001823
temp = 1198.3
temp = 1
gamma = 1
#l_bjerrum = 0.885^2 * e^2/(4*pi*epsilon_0*k_B*T) #Bjerrum length
l_bjerrum = 130878.0 / temp
wall_margin = 0.5

# Particle parameters
types       = {"Cl":          0, "Na": 1, "Electrode": 2}
numbers     = {"Cl": n_ionpairs, "Na": n_ionpairs}
charges     = {"Cl":       -1.0*e, "Na": 1.0*e}
lj_sigmas   = {"Cl":       3.85, "Na": 2.52,  "Electrode": 3.37}
lj_epsilons = {"Cl":     192.45, "Na": 17.44, "Electrode": 24.72}

WCA_cut = 2.**(1. / 6.)
lj_cuts     = {"Cl":        WCA_cut * lj_sigmas["Cl"],
               "Na":        WCA_cut * lj_sigmas["Na"],
               "Electrode": WCA_cut * lj_sigmas["Electrode"]}

masses      = {"Cl":  35.453, "Na": 22.99, "Electrode": 12.01}

'''-----------------'''
'''  SYSTEM CONFIG  '''
'''-----------------'''
# Setup System
box_l = (n_ionpairs * sum(masses.values()) / density)**(1. / 3.)    # Use density and mass of particles to calculate boxsize
box_z = box_l + 2.0 * (lj_sigmas["Electrode"] + wall_margin)
elc_gap = box_z * 0.15     #Electrostatic Layer Correction, gap=height of the empty region between the system box and the neighboring artificial images
system = System(box_l=[box_l, box_l, box_z + elc_gap])
system.seed = 42
box_volume = np.prod([box_l, box_l, box_z])

system.periodicity = [1, 1, 1]    # p3m only with periodic boundary
system.time_step = time_step
system.cell_system.skin = 0.3
system.thermostat.set_langevin(kT=temp, gamma=gamma)

# Walls
system.constraints.add(shape=Wall(dist=wall_margin, normal=[0, 0, 1]),
                       particle_type=types["Electrode"])
system.constraints.add(shape=Wall(dist=-(box_z - wall_margin), normal=[0, 0, -1]),
                       particle_type=types["Electrode"])

'''-----------------'''
''' PLACE PARTICLES '''
'''-----------------'''

for i in range(int(n_ionpairs)):
    p = np.random.random(3) * box_l
    p[2] += lj_sigmas["Electrode"]
    system.part.add(id=len(system.part), type=types["Cl"],
                    pos=p, q=charges["Cl"], mass=masses["Cl"])
for i in range(int(n_ionpairs)):
    p = np.random.random(3) * box_l
    p[2] += lj_sigmas["Electrode"]
    system.part.add(id=len(system.part), type=types["Na"],
                    pos=p, q=charges["Na"], mass=masses["Na"])

# Lennard-Jones interactions parameters
def combination_rule_epsilon(eps1, eps2):
    '''Lorentz-Berthelot combination rule'''
    return (eps1 * eps2)**0.5

def combination_rule_sigma(sig1, sig2):
    '''Lorentz-Berthelot combination rule'''
    return (sig1 + sig2) * 0.5

'''-----------------'''
'''  Calculate  lj  '''
'''-----------------'''
for s in [["Cl", "Na"], ["Cl", "Cl"], ["Na", "Na"],
          ["Na", "Electrode"], ["Cl", "Electrode"]]:
    lj_sig = combination_rule_sigma(lj_sigmas[s[0]], lj_sigmas[s[1]])
    lj_cut = combination_rule_sigma(lj_cuts[s[0]], lj_cuts[s[1]])
    lj_eps = combination_rule_epsilon(lj_epsilons[s[0]], lj_epsilons[s[1]])

    system.non_bonded_inter[types[s[0]], types[s[1]]].lennard_jones.set_params(
        epsilon=lj_eps, sigma=lj_sig, cutoff=lj_cut, shift="auto")

if simulate == True:
    energy = system.analysis.energy()
    pprint("Before Minimization: E_total={}".format(energy['total']))
    system.minimize_energy.init(f_max=10, gamma=10, max_steps=2000,
                                max_displacement=0.1)
    system.minimize_energy.minimize()
    energy = system.analysis.energy()
    pprint("After Minimization: E_total={}".format(energy['total']))

    pprint("Tuning Electrostatics")
    # Tuning Electrostatics
    p3m = electrostatics.P3M(prefactor=l_bjerrum * temp,
                            accuracy=1e-2)
    system.actors.add(p3m)
    elc = electrostatic_extensions.ELC(gap_size=elc_gap,
                                    maxPWerror=1e-3)
    system.actors.add(elc)
    pprint("Done tuning electrostatics")

    '''---------------------'''
    ''' add capacitor field '''
    '''---------------------'''
    for p in system.part:
        p.ext_force = [0, 0, Ez * p.q]

    '''-----------------'''
    '''  relax system   '''
    '''-----------------'''
    pprint("Relax system")
    # Temperature Equilibration
    system.time = 0.0
    for i in range(int(num_steps_equilibration)):
        energy = system.analysis.energy()
        temp_measured = energy['kinetic'] / ((3.0 / 2.0) * n_part)
        print("progress={:.0f}%,\tt={:.1f}\tE_total={:.2f}\tE_coulomb={:.2f}\tT={:.4f}\r"
            .format(i * 100. / int(num_steps_equilibration - 1), system.time,
                    energy['total'], energy['coulomb'], temp_measured))
        system.integrator.run(integ_steps_per_config)

    pprint("Done relaxing system")

    # Integration
    bins = 100
    z_dens_na = np.zeros(bins)
    z_dens_cl = np.zeros(bins)
    system.time = 0.0
    cnt = 0

    f_vst = open("out_vst.dat", "w")
    f_vcf = open("out_vcf.dat", "w") #only one file

    '''-----------------'''
    '''Integrate System '''
    '''-----------------'''

    pprint("Integrate")
    energy = system.analysis.energy()
    _writevsf(system, f_vst, types='all', radiuses={}) #only once

    for i in range(num_configs):
        print('progress: {:3.0f}%\tEnergy total: {:4.3f}\tnon_bonded Energy: {:4.3}\tCoulomb Energy: {:4.3}'.format(
                i * 100. / num_configs, energy['total'], energy['non_bonded'], energy['coulomb']))
        energy = system.analysis.energy()
        temp_measured = energy['kinetic'] / ((3.0 / 2.0) * n_part)
        system.integrator.run(integ_steps_per_config)

        _writevcf(system, f_vcf, types='all',folded=True)

        for p in system.part:
            bz = int(p.pos[2] / box_z * bins)
            if p.type == types["Na"]:
                z_dens_na[bz] += 1.0
            elif p.type == types["Cl"]:
                z_dens_cl[bz] += 1.0
        cnt += 1

    f_vst.close()
    f_vcf.close()

    print('progress: 100%')

    # Analysis

    #radial distr fct
    rdf_bins = 100
    r_min = 0.0
    r_max = system.box_l[2] / 2.0
    r, rdf = system.analysis.rdf(rdf_type='rdf', type_list_a=[0], type_list_b=[1],
                            r_min=r_min, r_max=r_max, r_bins=rdf_bins)
    r_rdf = np.column_stack((r, rdf))
    np.savetxt(name+"_rdf.data", r_rdf, header="#r rdf")
    # Average / Normalize with Volume
    z_dens_na /= (cnt * box_volume / bins)
    z_dens_cl /= (cnt * box_volume / bins)
    z_values = np.linspace(0, box_l, num=bins)
    res = np.column_stack((z_values, z_dens_na, z_dens_cl))
    np.savetxt(name+".data", res,
                header="#z rho_na(z) rho_cl(z)")
else:
    res = np.loadtxt(name+".data")
    r_rdf = np.loadtxt(name+"_rdf.data")
    r = r_rdf[:, 0]
    rdf = r_rdf[:, 1]
    z_values = res[:, 0]
    z_dens_na = res[:, 1]
    z_dens_cl = res[:, 2]
pprint("Start plotting")
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6), dpi=80)
plt.subplot(211)
plt.plot(z_values, z_dens_na, label='Na')
plt.plot(z_values, z_dens_cl, label='Cl')
plt.plot([wall_margin+lj_sigmas['Electrode']/2, wall_margin+lj_sigmas['Electrode']/2], [0, np.max(z_dens_cl)])
plt.plot([np.max(z_values) - (wall_margin+lj_sigmas['Electrode']/2),
            np.max(z_values) - (wall_margin+lj_sigmas['Electrode']/2)], [0, np.max(z_dens_cl)])
plt.xlabel('$z$-axis $(\\mathrm{\\AA})$', fontsize=20)
plt.ylabel('Density $(\\mathrm{u\\AA}^{-3})$', fontsize=20)
plt.legend(fontsize=16)

plt.subplot(212)
plt.plot(r, rdf)
plt.xlabel('$r$ ', fontsize=20)
plt.ylabel('radial distr function g(r)', fontsize=20)
plt.show()

pprint('Done Plotting')


# -*- coding: utf-8 -*-
# Copyright © IBM Corporation 2010, 2020
# (c) Copyright IBM Corp. 2010, 2020. All Rights Reserved.
# pragma pylint: disable=unused-argument, no-self-use
"""Function implementation"""
import json
import logging
import re

from resilient_circuits import ResilientComponent, function, handler, StatusMessage, FunctionResult, FunctionError
from resilient_lib import validate_fields, ResultPayload

from fn_guardium_insights_integration.lib.insights_services import InsightsServices
from fn_guardium_insights_integration.lib.resilient_incident_operations import ResilientIncidentHelper
from fn_guardium_insights_integration.lib.time_conversions import compute_start_stop_times

class FunctionComponent(ResilientComponent):
    """Component that implements Resilient function 'function_guardium_insights_populate_breach_data_types"""

    def __init__(self, opts):
        """constructor provides access to the configuration options"""
        super(FunctionComponent, self).__init__(opts)
        self.options = opts.get("fn_guardium_insights_integration", {})

    @handler("reload")
    def _reload(self, event, opts):
        """Configuration options have changed, save new values"""
        self.options = opts.get("fn_guardium_insights_integration", {})

    @function("function_guardium_insights_populate_breach_data_types")
    def _function_guardium_insights_populate_breach_data_types_function(self, event, *args, **kwargs):
        """Function: A function to populate the incident breach data types."""
        try:
            # Get the function parameters:
            incident_id = kwargs.get("incident_id")  # number
            input_field_guardium_insights_who = kwargs.get("input_field_guardium_insights_who")  # text
            input_field_guardium_insights_what = kwargs.get("input_field_guardium_insights_what")  # text

            log = logging.getLogger(__name__)
            log.info("incident_id: %s", incident_id)
            log.info("input_field_guardium_insights_who: %s", input_field_guardium_insights_who)
            log.info("input_field_guardium_insights_what: %s", input_field_guardium_insights_what)

            yield StatusMessage("populating breach data types on incident: {}".format(incident_id))

            # Validating app.config settings
            validate_fields(["report_period", "report_fetch_size"], self.options)

            # Reading app.config settings
            report_period = self.options.get("report_period")
            report_fetch_size = self.options.get("report_fetch_size")

            # Initialize GI service
            gi_services = InsightsServices(self.options, log)

            # Initialize resilient service
            res_service = ResilientIncidentHelper(self.rest_client(), log)

            # Initialize Result payload.
            res_pay = ResultPayload("fn_guardium_insights_integration", **kwargs)
            res_status = False
            reason = ""

            _from_date, _to_date = compute_start_stop_times(report_period)

            # Converting string json to json object
            try:
                input_field_guardium_insights_what = json.loads(input_field_guardium_insights_what)
            except json.decoder.JSONDecodeError as err_msg:
                log.info("error converting what information to json object: {}".format(err_msg))
                input_field_guardium_insights_what = dict()

            _datasourceip = _port = None
            if input_field_guardium_insights_what:
                _datasourceip = input_field_guardium_insights_what.get("server_ip")
                _port = input_field_guardium_insights_what.get("server_port")
            if _datasourceip or _port:
                report_payload = gi_services.generate_report_payload(_from_date, _to_date, report_fetch_size,
                                                                     _datasourceip, _port)
                gi_list_reports = gi_services.list_all_reports()
                report_id = gi_services.get_report_id(gi_list_reports, "Classification")
                if report_id:
                    # Run classification report.
                    report_data = gi_services.run_report(report_payload)

                    # Correcting received response, if its not correct.
                    if isinstance(report_data, str):
                        report_data = re.sub(r"{.result.*\{.total_number_of_rows.*\}", "", report_data)
                        report_data = json.loads(report_data)

                    # Populating breach data types
                    res_service.populate_breach_data_types(incident_id, report_data)

                    reason = "populating breach data types completed: incident ID{}".format(incident_id)
                else:
                    reason = "--------Classification report not found in Guardium Insight system---------"
                    log.info(reason)
            else:
                reason = "--------Data SourceIP and Port Information is not available in the incident---------"
                log.info(reason)
            yield StatusMessage("populating breach data types completed: incident ID{}".format(incident_id))

            fun_result = res_pay.done(res_status, {}, reason)
            # Produce a FunctionResult with the results
            yield FunctionResult(fun_result)
        except Exception as fn_err:
            yield FunctionError(fn_err)


import json
import pandas as pd

def export_json(result):
    """ Serializing json """
    with open('src/sample/output.json', 'w') as f:
        # json_object = json.dumps(result, ensure_ascii=False, indent=4)
        json.dump(result, f, ensure_ascii=False, indent=4)

def export_exel(result):
    """ Export exel with new format """
    excel_result = []
    for r in result:
        for comment in r['Comments']:
            r_dict = {
                'ProductPageLink': r['ProductPageLink'],
                'ProductName': r['ProductName'],
                'Productcode': r['Productcode'],
                'BrandNameFa': r['BrandNameFa'],
                'BrandNameEn': r['BrandNameEn'],
                'Group': r['Group'],
                'CommentOwnerId': comment['CommentOwnerId'],
                'CommentDate': comment['CommentDate'],
                'CommentDescription': comment['CommentDescription'],
            }
            excel_result.append(r_dict)
    df = pd.DataFrame.from_dict(excel_result)
    print(df)
    df.to_excel('src/sample/sample.xlsx')


import datetime

now = datetime.datetime.now()

# c)
msgNum = 0

def logg():
    navn = input("Oppgi navn : ")
# g    tid = input("Oppgi tid : ")
    tid = "{:02d}:{:02d}".format(now.hour, now.minute) #! [FIXED] Gir kun ett siffer. F.eks 17:3 for 17:03
    melding = input("Melding : ")
# g    print('Klokken {} sendte {} følgende melding: "{}"'.format(tid, navn, melding))

    # c) utvid programmet:
    global msgNum
    msgNum += 1
    print('Msg {}, {} sendte {} følgende melding: "{}"'.format(msgNum, tid, navn, melding))

def main ():
    while msgNum < 6:
        logg()

main()


# import ipdb
# import os
# from glob import glob
# import pandas as pd
# import numpy as np
# import cv2
import datetime
from model import *
from util import *
from data.stl10_input import read_all_images, get_file

sys.stdout = Logger(filename=os.path.join(os.getcwd(), "stl-10_%s.log" % datetime.datetime.now().strftime("%Y-%m-%d_%H%M")))

n_epochs = 10000
learning_rate_val = 0.002
weight_decay_rate = 0.00001
momentum = 0.9
batch_size = 100
lambda_recon = 0.999
lambda_adv = 0.001

overlap_size = 7
hiding_size = 64
image_size = 128

result_path = '../results/stl-10/'
model_path = '../models/stl-10/'
pretrained_model_path = None

if not os.path.exists(model_path):
    os.makedirs(model_path)

if not os.path.exists(result_path):
    os.makedirs(result_path)

GRAYSCALE = False
# print "Loading Pretraining data..."
# X_pretrain = get_unlabeled_data(gray=GRAYSCALE)
print("Loading labeled data...")
# X_train, Y_train = get_training_data(gray=GRAYSCALE)
# X_test, Y_test = get_test_data(gray=GRAYSCALE)
# X_train = read_all_images(path_to_data=get_file("stl10_binary/stl_Xunlabeled_128px.bin"), img_size=image_size)
X_train = read_all_images(path_to_data=get_file("stl10_binary/stl_Xtrain_128px.bin"), img_size=image_size)
X_test = read_all_images(path_to_data=get_file("stl10_binary/stl_Xtest_128px.bin"), img_size=image_size)
# X_train = np.rollaxis(X_train, 3, 1)
# X_test = np.rollaxis(X_test, 3, 1)
# convert class vectors to binary class matrices, index from file starts at 1
# Y_train = np_utils.to_categorical(Y_train - 1, nb_classes)
# Y_test = np_utils.to_categorical(Y_test - 1, nb_classes)
print(X_train.shape)

is_train = tf.placeholder(tf.bool)

learning_rate = tf.placeholder(tf.float32, [])
images_tf = tf.placeholder(tf.float32, [batch_size, image_size, image_size, 3], name="images")

labels_D = tf.concat(0, [tf.ones([batch_size]), tf.zeros([batch_size])])
labels_G = tf.ones([batch_size])
images_hiding = tf.placeholder(tf.float32, [batch_size, hiding_size, hiding_size, 3], name='images_hiding')

model = Model()

bn1, bn2, bn3, bn4, bn5, bn6, debn4, debn3, debn2, debn1, reconstruction_ori, reconstruction = model.build_reconstruction(
    images_tf, is_train)
adversarial_pos = model.build_adversarial(images_hiding, is_train)
adversarial_neg = model.build_adversarial(reconstruction, is_train, reuse=True)
adversarial_all = tf.concat(0, [adversarial_pos, adversarial_neg])

# Applying bigger loss for overlapping region
mask_recon = tf.pad(tf.ones([hiding_size - 2 * overlap_size, hiding_size - 2 * overlap_size]),
                    [[overlap_size, overlap_size], [overlap_size, overlap_size]])
mask_recon = tf.reshape(mask_recon, [hiding_size, hiding_size, 1])
mask_recon = tf.concat(2, [mask_recon] * 3)
mask_overlap = 1 - mask_recon

# TODO plug in GCS loss here
loss_recon_ori = tf.square(images_hiding - reconstruction)
loss_recon_center = tf.reduce_mean(
    tf.sqrt(1e-5 + tf.reduce_sum(loss_recon_ori * mask_recon, [1, 2, 3])))  # Loss for non-overlapping region
loss_recon_overlap = tf.reduce_mean(
    tf.sqrt(1e-5 + tf.reduce_sum(loss_recon_ori * mask_overlap, [1, 2, 3]))) * 10.  # Loss for overlapping region
loss_recon = loss_recon_center + loss_recon_overlap

loss_adv_D = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(adversarial_all, labels_D))
loss_adv_G = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(adversarial_neg, labels_G))

loss_G = loss_adv_G * lambda_adv + loss_recon * lambda_recon
loss_D = loss_adv_D  # * lambda_adv

var_G = filter(lambda x: x.name.startswith('GEN'), tf.trainable_variables())
var_D = filter(lambda x: x.name.startswith('DIS'), tf.trainable_variables())

W_G = filter(lambda x: x.name.endswith('W:0'), var_G)
W_D = filter(lambda x: x.name.endswith('W:0'), var_D)

loss_G += weight_decay_rate * tf.reduce_mean(tf.pack(map(lambda x: tf.nn.l2_loss(x), W_G)))
loss_D += weight_decay_rate * tf.reduce_mean(tf.pack(map(lambda x: tf.nn.l2_loss(x), W_D)))

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
sess = tf.InteractiveSession(config=tf.ConfigProto(gpu_options=gpu_options))

optimizer_G = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5)
grads_vars_G = optimizer_G.compute_gradients(loss_G, var_list=var_G)
# grads_vars_G = map(lambda gv: [tf.clip_by_value(gv[0], -10., 10.), gv[1]], grads_vars_G)
train_op_G = optimizer_G.apply_gradients(grads_vars_G)

optimizer_D = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5)
grads_vars_D = optimizer_D.compute_gradients(loss_D, var_list=var_D)
# grads_vars_D = map(lambda gv: [tf.clip_by_value(gv[0], -10., 10.), gv[1]], grads_vars_D)
train_op_D = optimizer_D.apply_gradients(grads_vars_D)

saver = tf.train.Saver(max_to_keep=100)

tf.initialize_all_variables().run()

if pretrained_model_path is not None and os.path.exists(pretrained_model_path):
    saver.restore(sess, pretrained_model_path)

iters = 0

loss_D_val = 0.
loss_G_val = 0.

for epoch in range(n_epochs):
    # trainset.index = range(len(trainset))
    # trainset = trainset.ix[np.random.permutation(len(trainset))]

    for start, end in zip(
            range(0, len(X_train), batch_size),
            range(batch_size, len(X_train), batch_size)):

        images_ori = X_test[start:end]

        if iters % 2 == 0:
            images_ori = map(lambda img: img[:, ::-1, :], images_ori)

        is_none = np.sum(map(lambda x: x is None, images_ori))
        if is_none > 0: continue

        images_crops = map(lambda x: crop_random(x, x=32, y=32), images_ori)
        images, crops, _, _ = zip(*images_crops)

        # Printing activations every 10 iterations
        if iters % 100 == 0:
            test_images_ori = X_test[:batch_size]

            test_images_crop = map(lambda x: crop_random(x, x=32, y=32), test_images_ori)
            test_images, test_crops, xs, ys = zip(*test_images_crop)

            reconstruction_vals, recon_ori_vals, bn1_val, bn2_val, bn3_val, bn4_val, bn5_val, bn6_val, debn4_val, debn3_val, debn2_val, debn1_val, loss_G_val, loss_D_val = sess.run(
                [reconstruction, reconstruction_ori, bn1, bn2, bn3, bn4, bn5, bn6, debn4, debn3, debn2, debn1, loss_G,
                 loss_D],
                feed_dict={
                    images_tf: test_images,
                    images_hiding: test_crops,
                    is_train: False
                })

            # Generate result every 1000 iterations
            if iters % 100 == 0:
                ii = 0
                for rec_val, img, x, y in zip(reconstruction_vals, test_images, xs, ys):
                    rec_hid = (255. * (rec_val + 1) / 2.).astype(int)
                    rec_con = (255. * (img + 1) / 2.).astype(int)

                    rec_con[y:y + 64, x:x + 64] = rec_hid
                    # cv2.imwrite(os.path.join(result_path, 'img_' + str(ii) + '.' + str(int(iters / 100)) + '.jpg'),
                    #             rec_con)
                    ii += 1

                if iters == 0:
                    for test_image in test_images_ori:
                        test_image = (255. * (test_image + 1) / 2.).astype(int)
                        test_image[32:32 + 64, 32:32 + 64] = 0
                        # cv2.imwrite(os.path.join(result_path, 'img_' + str(ii) + '.ori.jpg'), test_image)

            print("========================================================================")
            print(bn1_val.max(), bn1_val.min())
            print(bn2_val.max(), bn2_val.min())
            print(bn3_val.max(), bn3_val.min())
            print(bn4_val.max(), bn4_val.min())
            print(bn5_val.max(), bn5_val.min())
            print(bn6_val.max(), bn6_val.min())
            print(debn4_val.max(), debn4_val.min())
            print(debn3_val.max(), debn3_val.min())
            print(debn2_val.max(), debn2_val.min())
            print(debn1_val.max(), debn1_val.min())
            print(recon_ori_vals.max(), recon_ori_vals.min())
            print(reconstruction_vals.max(), reconstruction_vals.min())
            print(loss_G_val, loss_D_val)
            print("=========================================================================")

            if np.isnan(reconstruction_vals.min()) or np.isnan(reconstruction_vals.max()):
                print("NaN detected!!")
                ipdb.set_trace()

        # Generative Part is updated every iteration
        _, loss_G_val, adv_pos_val, adv_neg_val, loss_recon_val, loss_adv_G_val, reconstruction_vals, recon_ori_vals, bn1_val, bn2_val, bn3_val, bn4_val, bn5_val, bn6_val, debn4_val, debn3_val, debn2_val, debn1_val = sess.run(
            [train_op_G, loss_G, adversarial_pos, adversarial_neg, loss_recon, loss_adv_G, reconstruction,
             reconstruction_ori, bn1, bn2, bn3, bn4, bn5, bn6, debn4, debn3, debn2, debn1],
            feed_dict={
                images_tf: images,
                images_hiding: crops,
                learning_rate: learning_rate_val,
                is_train: True
            })

        _, loss_D_val, adv_pos_val, adv_neg_val = sess.run(
            [train_op_D, loss_D, adversarial_pos, adversarial_neg],
            feed_dict={
                images_tf: images,
                images_hiding: crops,
                learning_rate: learning_rate_val / 10.,
                is_train: True
            })

        print("Iter:", iters, "Gen Loss:", loss_G_val, "Recon Loss:", loss_recon_val, "Gen ADV Loss:", loss_adv_G_val, "Dis Loss:", loss_D_val, "||||", adv_pos_val.mean(), adv_neg_val.min(), adv_neg_val.max())

        iters += 1

    saver.save(sess, model_path + 'model', global_step=epoch)
    learning_rate_val *= 0.99


from django import template
register = template.Library()

#$ def (value, arg):
# var|foo:"bar"    var is the variable/value, bar is the arg
@register.filter(name='streakBoo')
def streakBoo(value):
    if (value[0] == 'W'):
        return True
    else:
        return False

@register.filter(name='win')
def win(value, args):
    if (value > args):
        return True
    else:
        return False

@register.filter(name='getAddress')
def getAddress(value):
    dict  = {
    "ATL": '1 Philips Dr. Atlanta, GA 30303',
    "BKN": '620 Atlantic Ave, Brooklyn, NY 11217',
    "BOS": '100 Legends Way, Boston, MA 02114',
    "CHA": '333 E Trade St, Charlotte, NC 28202',
    "CHI": '1901 W Madison St, Chicago, IL 60612',
    "CLE": '1 Center Ct, Cleveland, OH 44115',
    "DAL": '2500 Victory Ave, Dallas, TX 75219',
    "DEN": '1000 Chopper Cir, Denver, CO 80204',
    "DET": '6 Championship Dr, Auburn Hills, MI 48326',
    "GS": '7000 Coliseum Way, Oakland, CA 94621',
    "HOU": '1510 Polk St, Houston, TX 77002',
    "IND": '125 S Pennsylvania St, Indianapolis, IN 46204',
    "LAC": '1111 S Figueroa St, Los Angeles, CA 90015',
    "LAL": '1111 S Figueroa St, Los Angeles, CA 90015',
    "MEM": '191 Beale St, Memphis, TN 38103',
    "MIA": '601 Biscayne Blvd, Miami, FL 33132',
    "MIL": '1001 N 4th St, Milwaukee, WI 53203',
    "MIN": '600 First Avenue North, Minneapolis, MN 55403',
    "NOP": '1501 Dave Dixon Dr, New Orleans, LA 70113',
    "NYK": '4 Pennsylvania Plaza, New York, NY 10001',
    "OKC": '100 W Reno Ave, Oklahoma City, OK 73102',
    "ORL": '400 W Church St #200, Orlando, FL 32801',
    "PHI": '3601 S Broad St, Philadelphia, PA 19148',
    "PHX": '201 E Jefferson St, Phoenix, AZ 85004',
    "POR": '1 N Center Ct St, Portland, OR 97227',
    "SAC": '1 Sports Pkwy, Sacramento, CA 95834',
    "SA": '1 AT&T Center, San Antonio, TX 78219',
    "TOR": '40 Bay Street Toronto, Ontario, Canada',
    "UTA": '301 W South Temple, Salt Lake City, UT 84101',
    "WAS": '601 F St Nw, Washington, DC 20004',
    }
    return dict[value]

@register.filter(name='teamName')
def teamName(value):
    dict = {
    "ATL": 'Atlanta Hawks',
    "BKN": 'Brooklyn Nets',
    "BOS": 'Boston Celtics',
    "CHA": 'Charlotte Hornets',
    "CHI": 'Chicago Bulls',
    "CLE": 'Cleveland Cavaliers',
    "DAL": 'Dallas Mavericks',
    "DEN": 'Denver Nuggets',
    "DET": 'Detriot Pistons',
    "GS": 'Golden State Warriors',
    "HOU": 'Houston Rockets',
    "IND": 'Indiana Pacers',
    "LAC": 'LA Clippers',
    "LAL": 'LA Lakers',
    "MEM": 'Memphis Grizzlies',
    "MIA": 'Miami Heat',
    "MIL": 'Milwaukee Bucks',
    "MIN": 'Minnesota Timberwolves',
    "NOP": 'New Orleans Pelicans',
    "NYK": 'New York Knicks',
    "OKC": 'Oklahoma City Thunder',
    "ORL": 'Orlando Magic',
    "PHI": 'Philadelphia 76ers',
    "PHX": 'Phoenix Suns',
    "POR": 'Portland TrailBlazers',
    "SAC": 'Sacramento Kings',
    "SA": 'San Antonio Spurs',
    "TOR": 'Toronto Raptors',
    "UTA": 'Utah Jazz',
    "WAS": 'Washington Wizards',
    }
    return dict[value]


from django.urls import path

from . import views

urlpatterns = [
    path('', views.VeiculosList.as_view(), name='listar-veiculos'),
    path(r'novo/', views.VeiculosNew.as_view(), name='novo-veiculo'),
    path(r'<int:pk>/', views.VeiculosEdit.as_view(), name='editar-veiculo'),
    path(r'excluir/<int:pk>/', views.VeiculosDelete.as_view(), name='deletar-veiculo'),
]


#! python3
#  coding=utf-8
from urllib.parse import urljoin

import scrapy

import Columbus.utils.datetime_util as datetime_util
from Columbus.items import OfficialListItem
from Columbus.repository.weixin_dao.keyword_monitor_dao import KeywordMonitorDao

class AccountCrawlSpider(scrapy.Spider):
    name = "account_crawl"
    allowed_domains = ['sogou.com']

    keyword_monitor = KeywordMonitorDao()
    keyword_monitors = keyword_monitor.query_by_crawl_date(datetime_util.curDate())

    keywords = []
    for keyword_monitor in keyword_monitors:
        keywords.append(keyword_monitor[1])

    keyword_count = 0
    keyword = keywords[keyword_count]

    page_count = 1
    base_url = ""
    if page_count == 1:
        base_url = "http://weixin.sogou.com/weixin?type=1&s_from=input&query=" + keyword + "&ie=utf8&_sug_=n&_sug_type_="
    else:
        base_url = "http://weixin.sogou.com/weixin?query=" + keyword + "&_sug_type_=&s_from=input&_sug_=n&type=1&page=" \
                   + str(page_count) + "&ie=utf8"

    start_urls = [base_url]

    def parse(self, response):
        node_list = response.xpath("//div[@class='news-box']/ul/li")
        print("response node length:" + str(len(node_list)))

        # 解析当前页面获取的所有列表
        for node in node_list:
            item = OfficialListItem()
            name = node.xpath("./div/div[2]/p[1]/a//text()").extract()
            code = node.xpath("./div/div[2]/p[2]/label[@name='em_weixinhao']/text()").extract()[0]
            icon = node.xpath("./div/div[1]/a/img/@src").extract()[0]
            description = node.xpath("./dl[1]/dd//text()").extract()
            href = node.xpath("./div/div[2]/p[1]/a/@href").extract()[0]

            item['name'] = "".join(name)
            item['code'] = code
            item['icon'] = icon
            item['description'] = "".join(description)
            item['href'] = href

            yield item

        self.page_count = self.page_count + 1

        # 查看是否有下一页的按钮
        if self.page_count < 10 and len(response.xpath("//div[@class='news-box']/div/a[@id='sogou_next']")):
            next_url = urljoin(self.base_url,
                               response.xpath("//div[@class='news-box']/div/a[@id='sogou_next']/@href").extract()[0])

            yield scrapy.Request(next_url, callback = self.parse)
        else:
            if len(self.keywords) - 1 > self.keyword_count:
                self.keyword_count += 1
                self.keyword = self.keywords[self.keyword_count]

                self.page_count = 0
                self.base_url = "http://weixin.sogou.com/weixin?query=" \
                                + self.keyword + "&_sug_type_=&s_from=input&_sug_=n&type=1&page=" \
                                + str(self.page_count) + "&ie=utf8"

                yield scrapy.Request(self.base_url, callback = self.parse)


from rest_framework import serializers

from bank.models import BankUserAdministration
from core import models

class BankCustomersSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.BankCustomer
        fields = '__all__'
        read_only_fields = (
            'id',
            'user',
            'created_at',
            'updated_at',
        )

class BankTransferSerializer(serializers.ModelSerializer):
    iban_from_username = serializers.SerializerMethodField('get_iban_from_username')
    iban_to_username = serializers.SerializerMethodField('get_iban_to_username')

    def get_iban_to_username(self, obj):
        return str(obj.iban_to.account_owned_by)

    def get_iban_from_username(self, obj):
        return str(obj.iban_from.account_owned_by)

    class Meta:
        model = models.BankTransfer
        fields = (
            'id',
            'iban_from',
            'iban_from_username',
            'iban_to',
            'iban_to_username',
            'is_success',
            'is_open',
            'use_case',
            'executionlog',
            'execute_datetime',
            'amount',
            'created_at',
            'updated_at',
            'created_by',
        )

        # fields = '__all__'

        read_only_fields = (
            'id',
            'created_at',
            'updated_at',
            'executionlog',
            'is_open',
            'is_success',
            'created_by',
            # 'iban_from'
        )

    def create(self, validated_data):
        request = self.context.get('request', None)
        user = request.user
        adminstating_ibans = [x.iban for x in BankUserAdministration(user).adminstrating_accounts]
        if not validated_data['iban_from'].iban in adminstating_ibans:
            raise serializers.ValidationError({"detail": "Not an account from user"})
        validated_data['created_by'] = request.user.bank_customer
        return super(BankTransferSerializer, self).create(validated_data=validated_data)

class BankAccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.BankAccount
        fields = (
            "name",
            "iban",
            "account_owned_by",
            "balance",
            "updated_at",
            "created_at",
        )
        read_only_fields = (
            "id",
            "iban",
            "account_owned_by",
            "balance",
            'created_at',
            'updated_at',
        )


# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import itertools

def get_numbers_from_file(file_path="day15_input.txt"):
    with open(file_path) as f:
        for l in f:
            return [int(s) for s in l.split(",")]

def yield_game(numbers):
    turns = dict()
    for turn, n in enumerate(numbers):
        turns.setdefault(n, []).append(turn)
        yield n
    for turn in itertools.count(start=turn + 1):
        lst = turns[n]
        n = (lst[-1] - lst[-2]) if len(lst) > 1 else 0
        turns.setdefault(n, []).append(turn)
        yield n

def n_th(n, it):
    for i, v in enumerate(it):
        if i == n:
            return v

def play_game(numbers, turn):
    return n_th(turn - 1, yield_game(numbers))

def run_tests():
    example1 = [0, 3, 6]
    assert list(itertools.islice(yield_game(example1), 10)) == [
        0,
        3,
        6,
        0,
        3,
        3,
        1,
        0,
        4,
        0,
    ]
    assert play_game(example1, 1) == 0
    assert play_game(example1, 9) == 4
    assert play_game(example1, 10) == 0
    assert play_game(example1, 2020) == 436
    assert play_game([1, 3, 2], 2020) == 1
    assert play_game([2, 1, 3], 2020) == 10
    assert play_game([1, 2, 3], 2020) == 27
    assert play_game([2, 3, 1], 2020) == 78
    assert play_game([3, 2, 1], 2020) == 438
    assert play_game([3, 1, 2], 2020) == 1836
    # assert play_game(example1, 30000000) == 175594 # Slow

def get_solutions():
    numbers = get_numbers_from_file()
    print(play_game(numbers, 2020) == 273)
    # print(play_game(numbers, 30000000) == 47205) # Slow

if __name__ == "__main__":
    begin = datetime.datetime.now()
    run_tests()
    get_solutions()
    end = datetime.datetime.now()
    print(end - begin)