from django.shortcuts import render_to_response
from django.core.context_processors import csrf
from django.template import RequestContext # For CSRF
from django.forms.formsets import formset_factory, BaseFormSet
from django.http import HttpResponse, HttpResponseRedirect

from dynamicform.todo.forms import *

def index(request):
    # This class is used to make empty formset forms required
    # See http://stackoverflow.com/questions/2406537/django-formsets-make-first-required/4951032#4951032
    class RequiredFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            super(RequiredFormSet, self).__init__(*args, **kwargs)
            for form in self.forms:
                form.empty_permitted = False

    TodoItemFormSet = formset_factory(TodoItemForm, max_num=10, formset=RequiredFormSet)

    if request.method == 'POST': # If the form has been submitted...
        todo_list_form = TodoListForm(request.POST) # A form bound to the POST data
        # Create a formset from the submitted data
        todo_item_formset = TodoItemFormSet(request.POST, request.FILES)

        if todo_list_form.is_valid() and todo_item_formset.is_valid():
            todo_list = todo_list_form.save()
            for form in todo_item_formset.forms:
                todo_item = form.save(commit=False)
                todo_item.list = todo_list
                todo_item.save()

            return HttpResponseRedirect('thanks') # Redirect to a 'success' page
    else:
        todo_list_form = TodoListForm()
        todo_item_formset = TodoItemFormSet()

    # For CSRF protection
    # See http://docs.djangoproject.com/en/dev/ref/contrib/csrf/
    c = {'todo_list_form': todo_list_form,
         'todo_item_formset': todo_item_formset,
        }
    c.update(csrf(request))

    return render_to_response('todo/index.html', c)


import socket, pickle, argparse
import asyncio

async def client(args):
    host = args.host
    port = int(args.port)
    reader, writer = await asyncio.open_connection(
        host, port)

    while True:
        msg1 = input("> ")
        msg1 = pickle.dumps(msg1)
        writer.write(msg1)
        await writer.drain()

        msg2 = await reader.read(1024)
        msg2 = pickle.loads(msg2)

        if pickle.loads(msg1).lower() == "exit":
            print(f"Server: {msg2}")

            writer.close()
            await writer.wait_closed()
            exit()

        if msg2["error"] == "":
            print("OK\n", msg2["output"])
        else:
            print("ERROR\n", msg2["error"])

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-hs", '--host', required=True, help='host del sever')
    parser.add_argument("-p", '--port', required=True, help='Puerto de conexion')
    args = parser.parse_args()
    asyncio.run(client(args))


from collections import Counter

def solution(s):
    answer = []
    word = s.replace("{","").replace("}", "").split(",")
    count_word = Counter(word).most_common()
    for i in count_word:
        answer.append(int(i[0]))
    return answer

def solution2(s:str):
    word_list = list(map(int, s.replace("}", "").replace("{", "").split(",")))
    count_dict = Counter(word_list).most_common()
    answer = [item[0] for item in count_dict]
    return answer

if __name__ == '__main__':

    print(solution2("{{2},{2,1},{2,1,3},{2,1,3,4}}"))
    print(solution2("{{1,2,3},{2,1},{1,2,4,3},{2}}"))
    print(solution2("{{20,111},{111}}"))
    print(solution2("{{123}}"))
    print(solution2("{{4,2,3},{3},{2,3,4,1},{2,3}}"))

# print(solution(s))


from options.base_options import BaseOptions

class TestOptions(BaseOptions):
    def initialize(self, parser):
        BaseOptions.initialize(self, parser)
        parser.add_argument('--use_latest_checkpoint', action='store_true', help='if set, inference will use last saved epoch, otherwise it will use best validation performing model')

        parser.set_defaults(phase='test')
        self.isTrain = False
        return parser


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

#This software is distributed under the Creative Commons license (CC0) version 1.0. A copy of this license should have been distributed with this software.
#The license can also be read online: <https://creativecommons.org/publicdomain/zero/1.0/>. If this online license differs from the license provided with this software, the license provided with this software should be applied.

"""
This package contains the inner workings of Luna.

The core is kept as small as possible. Its main function is to manage the global
system that must be common knowledge across all plug-ins. This includes the
plug-in API itself, but could also include things like the internationalisation
system in the future.
"""

APPLICATION_AUTHOR = "${LUNA_AUTHOR}".replace("$" + "{LUNA_AUTHOR}", "Ghostkeeper")
"""
The author(s) of the application.

This is constructed in such a way that it can be formatted by CMake if necessary
and still have a default filled in by Python if CMake wasn't applied.
"""

APPLICATION_DESCRIPTION = "${LUNA_DESCRIPTION}".replace("$" + "{LUNA_DESCRIPTION}", "Pluggable application framework.")
"""
A description for the application.

This is constructed in such a way that it can be formatted by CMake if necessary
and still have a default filled in by Python if CMake wasn't applied.
"""

APPLICATION_NAME = "${LUNA_TITLE}".replace("$" + "{LUNA_TITLE}", "Luna")
"""
The name of the application.

This is constructed in such a way that it can be formatted by CMake if necessary
and still have a default filled in by Python if CMake wasn't applied.
"""

APPLICATION_VERSION_MAJOR = int("${LUNA_VERSION_MAJOR}".replace("$" + "{LUNA_VERSION_MAJOR}", "0"))
"""
The major version number of the application.

This is constructed in such a way that it can be formatted by CMake if necessary
and still have a default filled in by Python if CMake wasn't applied.
"""

APPLICATION_VERSION_MINOR = int("${LUNA_VERSION_MINOR}".replace("$" + "{LUNA_VERSION_MINOR}", "0"))
"""
The minor version number of the application, beneath the major version.

This is constructed in such a way that it can be formatted by CMake if necessary
and still have a default filled in by Python if CMake wasn't applied.
"""

APPLICATION_VERSION_PATCH = int("${LUNA_VERSION_PATCH}".replace("$" + "{LUNA_VERSION_PATCH}", "0"))
"""
The patch version number of the application, beneath the minor version.

This is constructed in such a way that it can be formatted by CMake if necessary
and still have a default filled in by Python if CMake wasn't applied.
"""


"""create user table

Revision ID: 726f693370c6
Revises:
Create Date: 2021-01-14 19:13:38.354021

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '726f693370c6'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    op.create_table(
        'users',
        sa.Column('id', sa.Integer, primary_key=True, index=True),
        sa.Column('spotify_id', sa.String, unique=True, index=True),
        sa.Column('is_active', sa.Boolean, default=True),
        sa.Column('name', sa.String(255)),
        sa.Column('image_url', sa.String),
        sa.Column('refresh_token', sa.String),
        sa.Column('access_token', sa.String),
        sa.Column('token_expires', sa.Integer)
    )

def downgrade():
    op.drop_table('users')


# This Program to insert Random Data in all six tables
import cx_Oracle
import random

def connectdatabase():
    fp = open("database.txt", 'r')
    if fp:
        c = fp.read()
        fp.close()
        return c
    else:
        print("File is not opened!")

firstname = ['Dipak', 'Atharv', 'Bhupendra',  'Rohit', 'Saurabh', 'Jayesh', 'Shivam', 'Kalpesh',
             'Atul',  'Yash', 'Ashwini', 'Rina', 'Anuja', 'Diksha', 'Khushi', 'Neha', 'Sarika', 'Puja']

g = ['Ashwini', 'Rina', 'Anuja', 'Diksha', 'Khushi', 'Neha', 'Sarika', 'Puja']

lastname = ['Mali', 'Mahajan', 'Patil', 'Garud', 'Salunke', 'Yogi', 'Koli', 'Thakur', 'Magare',
            'Nikam', 'Hire', 'Wakade', 'Tawade', 'Deshmukh', 'More', 'Lokhande', 'Chauhan', 'Mahale']

a = [18, 19, 20, 21]

address = ['Chopda', 'Jalgaon', 'Dhule', 'Nundurbar', 'Shirpur', 'Chalisgaon']

c = cx_Oracle.connect(connectdatabase())
if c != None:
    print("Connected")
    cur = c.cursor()
    branch = ['com', 'ds', 'entc', 'cevil', 'mech', 'ele']
    for j in range(6):
        for i in range(1, 51):
            rn = i
            name = random.choice(firstname)+' '+random.choice(lastname)
            prn = 20110603+i
            age = random.choice(a)
            if name.split()[0] in g:
                gen = 'F'
            else:
                gen = 'M'
            d = random.randint(1, 28)
            m = random.randint(1, 12)
            y = 2021-age
            dob = f'{str(d)}/{str(m)}/{str(y)}'
            phn = '80555*****'
            email = f'{name.split()[0]}{str(rn)}@gmail.com'
            add = random.choice(address)
            cgpa = random.uniform(2.5, 10.0)
            q = f"INSERT INTO {branch[j]} VALUES({int(rn)}, '{name}', {int(prn)}, {int(age)}, '{gen}', '{dob}', '{phn}', '{email}', '{add}',{cgpa})"
            cur.execute(q)
    c.commit()
    c.close()
else:
    print(c)
    print('Failed...')
print('Random data of 50 students has been added for each branch :)')


import argparse
import os
import random
import sys

import numpy as np
import torch

from unityagents import UnityEnvironment

import DDPG
import mDDPG
import OurDDPG
import TD3
import utils

class UnityWrapper:
    def __init__(self, env, train_mode=True):
        self.env = env
        self.brain_name = env.brain_names[0]
        self.train_mode = train_mode

        brain = env.brains[self.brain_name]
        action_size = brain.vector_action_space_size

        self.action_space = action_size
        self.observation_space = brain.vector_observation_space_size
        self.num_agents = len(self.reset())

        self.action_space_low = -1
        self.action_space_high = 1
        self._max_episode_steps = 1000
        self.num_agents = 1

    def reset(self, train_mode=None):
        if train_mode is None:
            train_mode = self.train_mode
        env_info = self.env.reset(train_mode=train_mode)[self.brain_name]
        return env_info.vector_observations[0]

    def step(self, actions):
        env_info = self.env.step(vector_action=actions)[self.brain_name]
        rewards = env_info.rewards  # get reward (for each agent)
        dones = [1 if x else 0 for x in env_info.local_done]  # see if episode finished
        next_states = env_info.vector_observations  # get next state (for each agent)
        return next_states[0], rewards[0], dones[0], None

    def sample(self):
        return [random.uniform(self.action_space_low, self.action_space_high) for _ in range(self.action_space)]

    def close(self):
        return self.env.close()

    def seed(self, seed):
        pass

def executable():
    if sys.platform == "linux":
        return "Reacher_Linux_NoVis/Reacher.x86_64"
    if sys.platform == "darwin":
        return "Reacher.app"

# Runs policy for X episodes and returns average reward
def evaluate_policy(env, policy, eval_episodes=10):
    rewards = []
    for _ in range(eval_episodes):
        episode_reward = 0
        obs = env.reset()
        done = False
        while not done:
            action = policy.select_action(np.array(obs))
            obs, reward, done, _ = env.step(action)
            episode_reward += reward
        rewards.append(episode_reward)

    rewards = np.array(rewards)
    print("---------------------------------------")
    print("Evaluation over %d episodes: %f" % (eval_episodes, rewards.mean()))
    print("---------------------------------------")
    return rewards

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--policy_name", default="TD3")  # Policy name
    parser.add_argument("--env_name", default="Reacher-v2")
    parser.add_argument("--seed", default=0, type=int)  # Sets Gym, PyTorch and Numpy seeds
    parser.add_argument("--start_timesteps", default=1e3, type=int)  # How many time steps purely random policy is run for
    parser.add_argument("--eval_freq", default=5e3, type=float)  # How often (time steps) we evaluate
    parser.add_argument("--max_timesteps", default=1e6, type=float)  # Max time steps to run environment for
    parser.add_argument("--save_models", action="store_true")  # Whether or not models are saved
    parser.add_argument("--expl_noise", default=0.1, type=float)  # Std of Gaussian exploration noise
    parser.add_argument("--batch_size", default=100, type=int)  # Batch size for both actor and critic
    parser.add_argument("--discount", default=0.99, type=float)  # Discount factor
    parser.add_argument("--tau", default=0.005, type=float)  # Target network update rate
    parser.add_argument("--policy_noise", default=0.2, type=float)  # Noise added to target policy during critic update
    parser.add_argument("--noise_clip", default=0.5, type=float)  # Range to clip target policy noise
    parser.add_argument("--policy_freq", default=2, type=int)  # Frequency of delayed policy updates
    args = parser.parse_args()

    file_name = "%s_%s_%s" % (args.policy_name, args.env_name, str(args.seed))
    print("---------------------------------------")
    print("Settings: %s" % (file_name))
    print("---------------------------------------")

    for dirname in ("./results", "./rewards"):
        os.makedirs(dirname, exist_ok=True)
    if args.save_models:
        os.makedirs("./pytorch_models", exist_ok=True)

    unity = UnityEnvironment(file_name=executable(), no_graphics=True)
    env = UnityWrapper(unity, train_mode=True)

    # Set seeds
    env.seed(args.seed)
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)

    state_dim = env.observation_space
    action_dim = env.action_space
    max_action = 1

    # Initialize policy
    if args.policy_name == "TD3":
        policy = TD3.TD3(state_dim, action_dim, max_action)
    elif args.policy_name == "OurDDPG":
        policy = OurDDPG.DDPG(state_dim, action_dim, max_action)
    elif args.policy_name == "DDPG":
        policy = DDPG.DDPG(state_dim, action_dim, max_action)
    elif args.policy_name == "mDDPG":
        policy = mDDPG.DDPG(state_dim, action_dim, max_action)

    replay_buffer = utils.ReplayBuffer()

    # Evaluate untrained policy
    evaluations = [evaluate_policy(env, policy).mean()]

    total_timesteps = 0
    timesteps_since_eval = 0
    episode_num = 0
    episode_reward = 0
    episode_timesteps = 0
    done = True
    rewards = []

    while total_timesteps < args.max_timesteps:
        if done:
            if total_timesteps != 0:
                print(
                    ("Total T: %d Episode Num: %d Episode T: %d Reward: %f")
                    % (total_timesteps, episode_num, episode_timesteps, episode_reward)
                )
                rewards.append(episode_reward)
                if args.policy_name == "TD3":
                    policy.train(
                        replay_buffer,
                        episode_timesteps,
                        args.batch_size,
                        args.discount,
                        args.tau,
                        args.policy_noise,
                        args.noise_clip,
                        args.policy_freq,
                    )
                else:
                    policy.train(replay_buffer, episode_timesteps, args.batch_size, args.discount, args.tau)

            # Evaluate episode
            if timesteps_since_eval >= args.eval_freq:
                timesteps_since_eval %= args.eval_freq
                evaluations.append(evaluate_policy(env, policy).mean())

                if args.save_models:
                    policy.save(file_name, directory="./pytorch_models")
                np.save("./results/%s" % (file_name), evaluations)

            # Reset environment
            obs = env.reset()
            done = False
            episode_reward = 0
            episode_timesteps = 0
            episode_num += 1

        # Select action randomly or according to policy
        if total_timesteps < args.start_timesteps:
            action = env.sample()
        else:
            action = policy.select_action(np.array(obs))
            if args.expl_noise != 0:
                action = (action + np.random.normal(0, args.expl_noise, size=env.action_space)).clip(
                    env.action_space_low, env.action_space_high
                )

        # Perform action
        new_obs, reward, done, _ = env.step(action)
        done_bool = 0 if episode_timesteps + 1 == env._max_episode_steps else float(done)
        episode_reward += reward

        # Store data in replay buffer
        replay_buffer.add((obs, new_obs, action, reward, done_bool))

        obs = new_obs

        episode_timesteps += 1
        total_timesteps += 1
        timesteps_since_eval += 1

    # Final evaluation
    evaluations.append(evaluate_policy(env, policy, 100).mean())
    if args.save_models:
        policy.save("%s" % (file_name), directory="./pytorch_models")
    np.save("./results/%s" % (file_name), evaluations)
    np.save("./rewards/%s" % (file_name), rewards)

if __name__ == "__main__":
    main()


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

# # Forecasting exercise for SARIMA
# Feb 28th 2022

# In[1]:

import sys
print(sys.executable)
import numpy as np
import pandas as pd
import os
print(os.getcwd())
from IPython.display import display
import seaborn as sns
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.graphics.tsaplots import plot_pacf, plot_acf
from statsmodels.tsa.statespace.sarimax import SARIMAX
from sklearn.metrics import mean_absolute_percentage_error as mape
import pmdarima as pm #help us to select p,d,q
import warnings
warnings.filterwarnings('ignore')

# In[2]:

# Custom functions:
import src.colorsetup
from src.timeseriesFunctions import plot_time_series, get_years, dickey_fuller_test, mape, cross_validate

# ## Input data

# In[3]:

data_path = 'https://vincentarelbundock.github.io/Rdatasets/csv/datasets/sunspot.year.csv'
data= pd.read_csv(data_path, usecols= ['time', 'value'], index_col= 'time', parse_dates= ['time'])
data

# In[4]:

plot_time_series(data.index, data.value, title= "Yearly Sunpots", ylabel= "Sunspots")

# In[5]:

get_years(data)

# In[6]:

cutoff_1= [str(i) for i in range(1700,1800)]
cutoff_2= [str(i) for i in range(1800,1900)]
cutoff_3= [str(i) for i in range(1900,1988)]

print(np.std(data.loc[cutoff_1]['value']))
print(np.std(data.loc[cutoff_2]['value']))
print(np.std(data.loc[cutoff_3]['value']))

# In[7]:

# Return the natural logarithm of one plus the input array, element-wise
data['log_transformation']= np.log1p(data.value)
print(np.std(data.loc[cutoff_1]['log_transformation']))
print(np.std(data.loc[cutoff_2]['log_transformation']))
print(np.std(data.loc[cutoff_3]['log_transformation']))

# In[8]:

plot_time_series(data.index, data.log_transformation, title= "Yearly Sunpots", ylabel= "log(Sunspots)")

# In[9]:

seasonal_plot= seasonal_decompose(data.log_transformation, period= 11).plot()

# In[10]:

acf= plot_acf(data.log_transformation, zero= False)

# In[11]:

pacf= plot_pacf(data.log_transformation, zero= False)

# Differencing

# In[12]:

data['lag_11']= data.log_transformation.shift(11)
data['seasonal_diff'] = data.log_transformation - data['lag_11']

# In[ ]:

# data.head(12)

# In[13]:

dickey_fuller_test(data['seasonal_diff'].dropna())

# In[14]:

acf_seasonal_diff= plot_acf(data['seasonal_diff'].dropna(), zero= False)
pacf_seasonal_diff= plot_pacf(data['seasonal_diff'].dropna(), zero= False)

# ## Start with a very simple model

# In[15]:

start_model= SARIMAX(data.log_transformation, order= (0,0,0), seasonal_order= (0,1,0,12), trend= 'c').fit()

# In[17]:

pacf_residuals= plot_pacf(start_model.resid[start_model.loglikelihood_burn:], zero= False)

# In[18]:

auto_model= pm.auto_arima(data.log_transformation,
                          start_p= 0, max_p= 3,
                          d= 0,
                          start_q= 0, max_q= 3,
                          m= 11,
                          seasonal= True,
                          start_P= 0,
                          D= 1,
                          trace= True, error_action= 'ignore', suppress_warnings= True, stepwise= True)

# In[19]:

print('Order: {} Seasonal order: {}'.format(auto_model.order, auto_model.seasonal_order))

# In[ ]:

eda_model= SARIMAX(data.log_transformation, order= (2,0,0), seasonal_order= (2,1,0,11), trend= 'c').fit()

# In[21]:

auto_diag= eda_model.plot_diagnostics(figsize= (18,8))

# In[ ]:

warnings.filterwarnings("ignore")
series = data['log_transformation']
horizon = 3
start = int(len(data.value)*.75) # 75% for training_set
step_size = 1
order = auto_model.order
seasonal_order = auto_model.seasonal_order

log_cv1 = cross_validate(series,horizon,start,step_size,
                    order = order,
                    seasonal_order = seasonal_order)

# In[23]:

exp_cv1= np.expm1(log_cv1)

# In[24]:

display(data.tail())
exp_cv1.tail()

# In[33]:

image_folder= "../../../plots/time_series_analysis/"
plot_name= "sarima.forecast.png"

print("MAPE: {:.4f}".format(mape(exp_cv1)))
exp_cv1.actual.plot(color= "black")
exp_cv1.forecast.plot(color= "orange")

plt.title("SARIMA model with MAPE: {:.4f}".format(mape(exp_cv1)))
plt.ylabel("Time series")
plt.xlabel("Time (Years)")
plt.legend()
plt.grid(alpha= 0.4)
plt.savefig(image_folder+plot_name, transparent= True)
plt.show()

# In[34]:

# Zoom in
exp_cv1['actual'].loc['1970':'1988'].plot(color= "black")
exp_cv1['forecast'].loc['1970':'1988'].plot(color= "orange")

plt.legend()
plt.show()

# In[35]:

import session_info
session_info.show()

# In[ ]:


from flask import request, jsonify
from mysqlAccess import mysqlConnection, mysqlCloseConnection, mysqlQuery, mysqlInsert, mysqlUpdate
from configApp import app

@app.route('/equipas', methods=['GET'])
def equipas():
    accessPoints = ["all", "att", "describe", "insert", "remove"]
    return jsonify(accessPoints)

@app.route('/equipas/all', methods=['GET'])
def equipasAll():
    conn = mysqlConnection()
    query = "SELECT * FROM equipas;"
    records = mysqlQuery(conn, query)
    mysqlCloseConnection(conn)
    return jsonify(records)

@app.route('/equipas/att', methods=['GET'])
def equipasAtt():
    conn = mysqlConnection()
    cursor = conn.cursor()
    cursor.execute(f'SELECT * FROM equipas;')
    column_names = [i[0] for i in cursor.description]
    data = cursor.fetchall()
    rows = []
    for row in data:
        row_dict = {}
        for i in range(len(column_names)):
            row_dict[column_names[i]] = row[i]
        rows.append(row_dict)
    mysqlCloseConnection(conn)
    return jsonify(rows)

@app.route('/equipas/describe', methods=['GET'])
def equipasDescribe():
    if 'idEquipa' not in request.args:
        return "Error: No id field provided. Please specify an id."

    idEquipa = request.args['idEquipa']
    conn = mysqlConnection()
    query = "SELECT * FROM equipas where idEquipa = '"+idEquipa+"';"
    records = mysqlQuery(conn, query)
    mysqlCloseConnection(conn)
    return jsonify(records)

@app.route('/equipas/insert', methods=['GET'])
def equipasInsert():
    if 'idEquipa' not in request.args:
        return "Error: No id field provided. Please specify an id."

    idEquipa = request.args['idEquipa']
    nome = request.args['nome']
    numMembros = request.args['numMembros']

    conn = mysqlConnection()
    query = "INSERT INTO equipas ( idEquipa, nome, numMembros ) VALUES( '"+idEquipa+"', '"+nome+"' , '"+numMembros+"' );"
    print(query)
    records = mysqlInsert(conn, query)
    mysqlCloseConnection(conn)
    print(records)
    return jsonify(records)

@app.route('/equipas/remove', methods=['GET'])
def equipasRemove():
    if 'idEquipa' not in request.args:
        return "Error: No id field provided. Please specify an id."

    idEquipa = request.args['idEquipa']

    conn = mysqlConnection()
    query = "DELETE FROM equipas WHERE idEquipa='"+idEquipa+"';"
    records = mysqlUpdate(conn, query)
    mysqlCloseConnection(conn)
    return jsonify(records)

@app.route('/equipas/update', methods=['GET'])
def equipasUpdate():
    if 'idEquipa' not in request.args:
        return "Error: No id field provided. Please specify an id."

    conn = mysqlConnection()
    idEquipa = request.args['idEquipa']

    if 'nome' not in request.args:
        query = "SELECT nome FROM equipas WHERE idEquipa='"+idEquipa+"';"
        records = mysqlQuery(conn, query)
        nome = str(records[0][0])
    else:
        nome = request.args['nome']

    if 'numMembros' not in request.args:
        query = "SELECT numMembros FROM equipas WHERE idEquipa='"+idEquipa+"';"
        records = mysqlQuery(conn, query)
        numMembros = str(records[0][0])
    else:
        numMembros = request.args['numMembros']

    query = "UPDATE equipas SET nome='"+nome+"', numMembros='"+numMembros+"' WHERE idEquipa='"+idEquipa+"';"

    records = mysqlUpdate(conn, query)
    mysqlCloseConnection(conn)
    return jsonify(records)


import matplotlib.pyplot as plt

from dival.util.plot import plot_image
from dival.measure import PSNR, SSIM
from dival import DataPairs

from dliplib.utils import Params
from dliplib.reconstructors.dip import DeepImagePriorReconstructor
from dliplib.utils.helper import load_standard_dataset, set_use_latex

set_use_latex()

# load data
dataset = load_standard_dataset('ellipses')
test_data = dataset.get_data_pairs('validation', 5)

index = 3
obs, gt = test_data[index]
test_data = DataPairs([obs], [gt], name='test: %d' % index)

# forward operator
ray_trafo = dataset.ray_trafo
params = Params.load('ellipses_dip')

results = []

# Architecture-hyper-parameters
channels = [1, 8, 32, 64, 128, 128, 128, 128]
scales = [5, 5, 5, 5, 5, 4, 3, 2]
iters = [5000] * 8

# Compute reconstructions with each different architectures
for ch, sc, it in zip(channels, scales, iters):
    print('Channels: %d, Scales: %d' % (ch, sc))
    params.channels = (ch,) * sc
    params.skip_channels = (0,) * sc
    params.scales = sc
    params.iterations = it
    reconstructor = DeepImagePriorReconstructor(ray_trafo=ray_trafo, hyper_params=params.dict, name='DIP')
    result = reconstructor.reconstruct(obs)
    results.append(result)

ch = 128
sc = 5
skip_channels = [[0, 0, 0, 0, 4],
                 [0, 0, 0, 4, 4],
                 [0, 0, 4, 4, 4],
                 [0, 4, 4, 4, 4]]

for skip in skip_channels:
    print('Skip Channels:')
    print(skip)
    params.channels = (ch,) * sc
    params.skip_channels = skip
    params.scales = sc
    reconstructor = DeepImagePriorReconstructor(ray_trafo=ray_trafo, hyper_params=params.dict, name='DIP')
    result = reconstructor.reconstruct(obs)
    results.append(result)

fig = plt.figure(figsize=(9.1, 8.3))
for i in range(len(results)):
    ax = fig.add_subplot(3, 4, i+1)
    psnr = PSNR(results[i], gt)
    ssim = SSIM(results[i], gt)
    plot_image(results[i], ax=ax, xticks=[], yticks=[], cmap='pink')
    if i < 8:
        ax.set_title('Channels: %d, Scales: %d' % (channels[i], scales[i]))
    else:
        ax.set_title('Skip: {}'.format(skip_channels[i-8]))
    ax.set_xlabel('PSNR: %.2f, SSIM: %.4f' % (psnr, ssim))

plt.tight_layout()
plt.tight_layout()

plt.savefig('ellipses_architectures.pdf')
plt.savefig('ellipses_architectures.pgf')
plt.show()


from machine import Pin, Timer
from neopixel import NeoPixel
import time

numPixels = 23
pin = 13
np = NeoPixel(Pin(pin, Pin.OUT), numPixels)

color = (0, 0, 0)
glow_color = (0, 0, 0)
grow = False
tick = 0

timer = None

def stop_glow():
    global timer
    if timer:
        timer.deinit()
        timer = None

def glow(new_color):
    global timer
    global glow_color

    glow_color = new_color
    if not timer:
        timer = Timer(-1)
        timer.init(period=5000, mode=Timer.PERIODIC, callback=glow_cb)

def glow_cb():
    global color
    global glow_color
    if glow_color == color:
        setColor((0, 0, 0))
    else:
        setColor(glow_color)

def setColor(newColor):
    global color
    color = newColor

def draw():
    global tick

    tick += 1

    if grow:
        p = tick % numPixels
        if np[p] == color:
            np[p] = (0, 0, 0)
        else:
            np[p] = color
    else:
        for p in range(0, numPixels):
            np[p] = color
    np.write()

# def growShrinkRing(color):
#     for p in range(0, numPixels):
#         np[p] = [0, 0, 0]
#     np.write()
#     for p in range(0, numPixels):
#         np[p] = color
#         np.write()
#         time.sleep_ms(50)
#     for p in range(0, numPixels):
#         np[p] = [0, 0, 0]
#         np.write()
#         time.sleep_ms(50)


import os

import numpy as np
import tensorflow as tf

import mnist
from utils import dense_to_one_hot, sigmaprime, sigma

data_dir = 'data'
cifar10_path = os.path.join(data_dir, 'cifar10')
mnist_path = os.path.join(data_dir, 'mnist')

# check for existence
os.path.exists(data_dir)

# To stop potential randomness
seed = 128
rng = np.random.RandomState(seed)

# number of neurons in each layer
input_num_units = 28*28
hidden_num_units = 300
output_num_units = 10

# define placeholders
x = tf.placeholder(tf.float32, [None, input_num_units])
y = tf.placeholder(tf.float32, [None, output_num_units])

# set remaining variables
epochs = 5
batch_size = 128
learning_rate = 0.01

weights = {
    'hidden': tf.Variable(tf.random_normal([input_num_units, hidden_num_units], seed=seed)),
    'output': tf.Variable(tf.random_normal([hidden_num_units, output_num_units], seed=seed))
}

biases = {
    'hidden': tf.Variable(tf.random_normal([hidden_num_units], seed=seed)),
    'output': tf.Variable(tf.random_normal([output_num_units], seed=seed))
}

fully_connected_layer1 = tf.add(tf.matmul(x, weights['hidden']), biases['hidden'])
fully_connected_layer1_tanh = sigma(fully_connected_layer1)
# fully_connected_layer1_tanh = tf.nn.tanh(fully_connected_layer1)
# fully_connected_layer1_tanh = fully_connected_layer1

fully_connected_layer2 = tf.add(tf.matmul(fully_connected_layer1_tanh, weights['output']), biases['output'])
fully_connected_layer2_tanh = sigma(fully_connected_layer2)
# fully_connected_layer2_tanh = tf.nn.tanh(fully_connected_layer2)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=fully_connected_layer2_tanh, labels=y))
# optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

d_z_2 = tf.multiply(loss, sigmaprime(fully_connected_layer2))
d_b_2 = d_z_2
d_w_2 = tf.matmul(tf.transpose(fully_connected_layer1_tanh), d_z_2)

d_a_1 = tf.matmul(d_z_2, tf.transpose(weights['output']))
d_z_1 = tf.multiply(d_a_1, sigmaprime(fully_connected_layer1))
d_b_1 = d_z_1
d_w_1 = tf.matmul(tf.transpose(x), d_z_1)

eta = tf.constant(0.5)
step = [
    tf.assign(weights['hidden'], tf.subtract(weights['hidden'], tf.multiply(eta, d_w_1))),
    tf.assign(biases['hidden'], tf.subtract(biases['hidden'], tf.multiply(eta, tf.reduce_mean(d_b_1, axis=[0])))),
    tf.assign(weights['output'], tf.subtract(weights['output'], tf.multiply(eta, d_w_2))),
    tf.assign(biases['output'], tf.subtract(biases['output'], tf.multiply(eta, tf.reduce_mean(d_b_2, axis=[0]))))
]

acct_mat = tf.equal(tf.argmax(fully_connected_layer2_tanh, 1), tf.argmax(y, 1))
acct_res = tf.reduce_sum(tf.cast(acct_mat, tf.float32))

# load data
mnist = mnist.load_mnist(mnist_path)

# cifar10_train_images, \
# cifar10_train_labels, \
# cifar10_validation_images, \
# cifar10_validation_labels, \
# cifar10_test_images, \
# cifar10_test_labels = cifar10.maybe_download_and_extract(cifar10_path)

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

for i in range(10000):
    batch_xs, batch_ys = mnist.train.next_batch(10)
    batch_ys = dense_to_one_hot(batch_ys, 10)

    sess.run(step, feed_dict={x: batch_xs,
                                y: batch_ys})
    if i % 1000 == 0:
        mnist_labels = dense_to_one_hot(mnist.test.labels[:1000], 10)
        res = sess.run(acct_res, feed_dict =
                       {x: mnist.test.images[:1000],
                        y: mnist_labels})
        print (i, res)


from django.conf import settings
from celery_tasks.main import celery_app
# 导入Django内置发送邮件函数
from django.core.mail import send_mail

@celery_app.task(name='send_verify_email')
def send_verify_email(email, verify_url):
    # 设置邮件内容
    html_message = '
尊敬的用户您好！
' \
                   '
感谢您使用美多商城。
' \
                   '
您的邮箱为：%s 。请点击此链接激活您的邮箱：
' \
                   '
%s
' % (email, verify_url, verify_url)
    # 调用django.core.mail种的send_mail方法进行邮件发送
    send_mail(subject='注册激活', message='', from_email=settings.EMAIL_FROM, recipient_list=[email],
              html_message=html_message)


from PIL import Image
import os.path
from django.core.exceptions import ValidationError

def resolution_checker(image):
    '''check resolution of uploaded images, reject if too small or too big'''
    im = Image.open(image)
    width, height = im.size
    size_in_bytes = os.stat(image).st_size
    if width < 1200 or height < 800:
        return False
    if size_in_bytes > 2_621_400:
        return False
    return True

def file_size(value):
    '''check file size, this is important because of model field'''
    # file has to be less than 2.5MB, else
    # if gets handled by the TempFileUploader.
    # This code does not work with the TempFileUploader.
    limit = 2621400
    if value.size > limit:
        raise ValidationError('File too large. Size should not exceed 2 MiB.')

def image_resizer(image):
    '''image resizer utility to generate thumbnails'''
    # also creates the path to save thumbnails
    size = (480, 480)
    thumbnail_path = os.path.splitext(image)[0] + '_thumbnail'
    with Image.open(image) as im:
        im.thumbnail(size)
        im.save(thumbnail_path, 'PNG')
    return thumbnail_path


def find_network_with_name_label(session, name_label):
    networks = session.call_xenapi('network.get_by_name_label', name_label)
    if len(networks) == 1:
        return networks[0]
    elif len(networks) > 1:
        raise Exception(_('Found non-unique network for name_label %s') %
                        name_label)
    else:
        return None

def find_network_with_bridge(session, bridge):
    """
    Return the network on which the bridge is attached, if found.
    The bridge is defined in the nova db and can be found either in the
    'bridge' or 'name_label' fields of the XenAPI network record.
    """
    expr = ('field "name__label" = "%s" or field "bridge" = "%s"' %
            (bridge, bridge))
    networks = session.call_xenapi('network.get_all_records_where', expr)
    if len(networks) == 1:
        return networks.keys()[0]
    elif len(networks) > 1:
        raise Exception(_('Found non-unique network for bridge %s') % bridge)
    else:
        raise Exception(_('Found no network for bridge %s') % bridge)


"""Add ENABLE_NATIVE_ID_CHECK_VERBOSE_DEBUGGING feature flag

Revision ID: 8bdc4dd58856
Revises: e8199ef92975
Create Date: 2021-05-31 09:54:01.457723

"""
from pcapi.models import feature

# revision identifiers, used by Alembic.
revision = "8bdc4dd58856"
down_revision = "e8199ef92975"
branch_labels = None
depends_on = None

FLAG = feature.FeatureToggle.ENABLE_NATIVE_ID_CHECK_VERBOSE_DEBUGGING

def upgrade():
    feature.add_feature_to_database(FLAG)

def downgrade():
    feature.remove_feature_from_database(FLAG)


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

import os
from twisted.internet import task, reactor
from scrapy.crawler import CrawlerRunner
from scrapy.utils.project import get_project_settings
from scrapy.utils.log import configure_logging

configure_logging()

def run_crawl():
    runner = CrawlerRunner(get_project_settings())

    if os.environ.get('NAROU_ID', '') != "" and os.environ.get('NAROU_PW', '') != "":
        runner.crawl('narou')
    if os.environ.get('HAMELN_ID', '') != "" and os.environ.get('HAMELN_PW', '') != "":
        runner.crawl('hameln')
    if os.environ.get('KAKUYOMU_ID', '') != "":
        runner.crawl('kakuyomu')

def _main():
    interval = int(os.environ.get('INTERVAL_MINUTES', '30')) * 60

    instance = task.LoopingCall(run_crawl)
    instance.start(interval)

    reactor.run()

if __name__ == '__main__':
    _main()


'''
Description: CMPT-371 Assignment2 - Q1(Client)
Author: Sen Lin, Mingjian Tang
Last Modified Date: June 24th, 2018
'''
import random
import time
from socket import *
#server info from sample
serverName = 'localhost'
serverPort = 50007
clientSocket = socket(AF_INET, SOCK_DGRAM)

#user input
seeds = int(input("Please provide me a random seed: "))
total_packets = int(input("How many packets do you want to send: "))
ackSeed = int(input("Please provide me seed for determining ACKs and NACKs has been corrupted: "))
probability = float(input("Please enter a propability between 0.0 to 1.0: "))

random.seed(ackSeed)
state1 = random.getstate() #random.uniform(0.0,1.0)
#random.seed(ackSeed)
random.seed(seeds)
state2 = random.getstate()

class packet:
	data = 0 #field 1
	sequence_number = 0
	ack	= 0
	nack = 0

currentPacket = packet()
currentPacket.data = random.randint(0,4294967295)
packetstr=str(currentPacket.data)+" "+str(currentPacket.sequence_number)+" "+str(currentPacket.ack)+" "+str(currentPacket.nack)
clientSocket.sendto( packetstr.encode(), (serverName, serverPort) )
expectedTime = time.time()
print("")
print("")
print("A packet with sequence number ", currentPacket.sequence_number," is about to be sent")
print("The sender is moving to state WAIT FOR ACK OR NACK")
print("Packet to send contains: data=",currentPacket.data," seq=",currentPacket.sequence_number," ack=",currentPacket.ack," nack=",currentPacket.nack)
print("")
currentPacketNumber = 0
modifiedMessage, severAddress = clientSocket.recvfrom(2048)
packetReceivedArray = modifiedMessage.decode().split(' ') #check if can be decoded
#print(probability_array)
message = None
waitingMessage = None

while currentPacketNumber < total_packets:
#check returning packet
	if time.time() >= expectedTime:
		random.setstate(state1)
		prob = random.uniform(0.0,1.0)
		state1 = random.getstate()
		if prob > probability and int(packetReceivedArray[2])==1:
			currentPacketNumber+=1

		if currentPacketNumber == total_packets:
			print("An ACK packet has just been received")
			print("All packages have been sent successfully. Byebye :) ")
			break
		if int (packetReceivedArray[2])==1 and currentPacket.sequence_number == 0 and prob>probability: #ack received and not corrupted, original sqnumber = 0
			currentPacket = packet()
			currentPacket.data = random.randint(0,4294967295)
			currentPacket.sequence_number = 1
			print("An ACK packet has just been received")
			print("A packet with sequence number ", currentPacket.sequence_number," is about to be sent")
			message = "The sender is moving to state WAIT FOR CALL 1 FROM ABOVE "
			waitingMessage = "The sender is moving to state WAIT FOR ACK OR NACK "
			#currentPacketNumber += 1
		elif int (packetReceivedArray[2])==1 and currentPacket.sequence_number == 1 and prob > probability: #ack received and not corrupted, original sqnumber = 1
			currentPacket = packet()
			currentPacket.data = random.randint(0,4294967295)
			currentPacket.sequence_number = 0
			print("An ACK packet has just been received")
			print("A packet with sequence number ", currentPacket.sequence_number," is about to be sent")
			message = "The sender is moving to state WAIT FOR CALL 0 FROM ABOVE "
			waitingMessage = "The sender is moving to state WAIT FOR ACK OR NACK "
			#currentPacketNumber += 1

		elif prob < probability: 										#corrupted
			print("A Corrupted ACK or NACK packet has just been received")
			if int(packetReceivedArray[2])==1:						#Ack and corrupted
				print("An ACK packet has just been received")
			else: 													#NACK and corrupted
				print("A NACK packet has just been received")
			if currentPacket.sequence_number == 1:
				message = "The sender is moving back to state WAIT FOR CALL 1 FROM ABOVE"
			else:
				message = "The sender is moving back to state WAIT FOR CALL 0 FROM ABOVE"
			waitingMessage = "The sender is moving back to state WAIT FOR ACK OR NACK"
			print("A packet with sequence number ", currentPacket.sequence_number," is about to be resent")

		elif int (packetReceivedArray[3])==1 and (prob > probability): 						#Nack and not corrupted
			if currentPacket.sequence_number == 1:
				message = "The sender is moving back to state WAIT FOR CALL 1 FROM ABOVE"
			else:
				message = "The sender is moving back to state WAIT FOR CALL 0 FROM ABOVE"
			waitingMessage = "The sender is moving back to state WAIT FOR ACK OR NACK"
			print("A NACK packet has just been received")
			print("A packet with sequence number ", currentPacket.sequence_number," is about to be resent")

		print("Packet to send contains: data=",currentPacket.data," seq=",currentPacket.sequence_number," ack=",currentPacket.ack," nack=",currentPacket.nack)
		random.setstate(state2)
		sleepTime = round(random.uniform(1.0,5.0))
		state2 = random.getstate()
		expectedTime+= sleepTime
		#time.sleep(sleepTime)
		print(message)
    	#send
		packetstr=str(currentPacket.data)+" "+str(currentPacket.sequence_number)+" "+str(currentPacket.ack)+" "+str(currentPacket.nack)
		clientSocket.sendto( packetstr.encode(), (serverName, serverPort))
		print(waitingMessage)
		#receive
		modifiedMessage, severAddress = clientSocket.recvfrom(2048)
		packetReceivedArray = modifiedMessage.decode().split(' ')
		print("")
		print("")

clientSocket.close()


import ast
import random
import threading

import numpy as np
import pandas as pd
import requests
import sys

from collections import deque
from pathlib import Path

from cement import Handler
from github import Github
from typing import Union, List, Tuple, Dict

from github.Commit import Commit
from github.GithubException import GithubException, RateLimitExceededException, UnknownObjectException
from github.PaginatedList import PaginatedList
from github.Repository import Repository

from tenet.core.diff_labeller.misc import safe_write
from tenet.core.exc import TenetError
from tenet.core.interfaces import HandlersInterface
from tenet.data.dataset import CommitMetadata, ChainMetadata
from tenet.data.diff import DiffBlock

class LocalGitFile:
    def __init__(self, url: str, path: Path, short: Path, content: str = None, tag: str = None):
        self.url = url
        self.path = path
        self.short = short
        self.content = content
        self.tag = tag

    def download(self):
        try:
            print(f'Requesting {self.tag}: {self.url}')
            request = requests.get(self.url)

            if request.status_code == 200:
                self.content = request.text
            else:
                print(f'Request code {request.status_code}')

        except ConnectionError:
            print("ConnectionError")

    def write(self) -> int:
        if not self.content:
            print(f"No file content for {self.path}")
            return -1
            # raise ValueError(f"No file content for {self.path}")

        self.path.parent.mkdir(exist_ok=True, parents=True)

        with self.path.open(mode='w') as f:
            print(f'Writing to  {self.path}')
            return f.write(self.content)

    def read(self):
        if not self.content:
            #print(f'Reading from {self.path}')

            if not self.path.exists() or self.path.stat().st_size == 0:
                if self.tag:
                    print(self.tag)
                self.download()
                self.write()
            else:
                with self.path.open(mode='r') as f:
                    self.content = f.read()

        return self.content

class PaginatedListRandomPicker:
    """
        Wrapper of PaginatedList that randomly picks pages
    """
    def __init__(self, paginated_list: PaginatedList, n_pages: int):
        self._paginated_list = paginated_list
        self._n_pages = n_pages
        self._visited = []

    def __call__(self, *args, **kwargs) -> Tuple[list, int]:
        if len(self._visited) >= self._n_pages:
            raise TenetError(f"PaginatedListRandomPicker visited all pages")

        rand_page = random.randint(0, self._n_pages - 1)

        while rand_page in self._visited:
            rand_page = random.randint(0, self._n_pages - 1)

        commits = self._paginated_list.get_page(rand_page)
        self._visited.append(rand_page)

        return commits, rand_page

class RandomCommitFilesLookup:
    def __init__(self, excluded_files: set, excluded_commits: set, target_extensions: list = None):
        self._excluded_files = excluded_files if excluded_files else {}
        self._excluded_commits = excluded_commits if excluded_files else {}
        self._target_extensions = target_extensions
        self._visited = []

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

    def coverage(self):
        coverage = set(self._visited)
        coverage.update(self._excluded_commits)

        return len(coverage)

    @property
    def excluded_files(self):
        return self._excluded_files

    @property
    def excluded_commits(self):
        return self._excluded_commits

    def check_file(self, file) -> bool:
        check = file.filename not in self._excluded_files and 'test' not in file.filename and file.status == 'modified'

        if self._target_extensions:
            return check and file.filename.split('.')[-1].lower() in self._target_extensions

        return check

    def get_commit_files(self, commit: Commit) -> list:
        return [file for file in commit.files if self.check_file(file)]

    def __call__(self, commits: List[Commit], files_limit: int = None) -> Dict[Commit, list]:
        # filter excluded commits
        commits = [commit for commit in commits if commit.sha not in self._excluded_commits]

        if len(commits) == 0:
            raise TenetError(f"No available commits after filtering excluded commits ")

        commit_files_pair = {}
        total_files = 0
        picked = []

        while len(picked) != len(commits):
            commit_idx = random.randint(0, len(commits) - 1)

            while commit_idx in picked:
                commit_idx = random.randint(0, len(commits) - 1)

            picked.append(commit_idx)
            random_commit = commits[commit_idx]
            commit_files_pair[random_commit] = self.get_commit_files(random_commit)
            total_files += len(commit_files_pair[random_commit])
            self._visited.append(random_commit.sha)

            if total_files > files_limit:
                break

        return commit_files_pair

class GithubHandler(HandlersInterface, Handler):
    """
        Github handler abstraction
    """

    class Meta:
        label = 'github'

    def __init__(self, **kw):
        super().__init__(**kw)
        self._git_api: Github = None
        self._tokens: deque = None
        self.lock = threading.Lock()
        # Size of the paginated list returned by get_commits, may change
        self.paginated_list_size = 30
        self._sw_types = None

    def has_rate_available(self):
        return self.git_api.get_rate_limit().core.remaining > 0

    @property
    def sw_types(self):
        if self._sw_types is None:
            self._sw_types = {}
            # normalize owner and project names
            for owner, projects in self.app.config.get_section_dict('sw_type').items():
                normalized_owner = owner.strip().lower()
                self._sw_types[normalized_owner] = {}

                for project, sw_type in projects.items():
                    normalized_project = project.strip().lower()
                    self._sw_types[normalized_owner][normalized_project] = sw_type

        return self._sw_types

    def get_sw_type(self, owner: str, project: str) -> Union[str, None]:
        if owner is None or pd.isna(owner):
            return 'unk'

        if project is None or pd.isna(project):
            return 'unk'

        normalized_owner = owner.strip().lower()
        normalized_project = project.strip().lower()

        if normalized_owner in self.sw_types and normalized_project in self.sw_types[normalized_owner]:
            return self.sw_types[normalized_owner][normalized_project]

        return 'unk'

    @property
    def git_api(self):
        with self.lock:
            if not self._tokens:
                tokens = self.app.pargs.tokens.split(',')
                self._tokens = deque(tokens, maxlen=len(tokens))

            if not self._git_api:
                self._git_api = Github(self._tokens[0])
                self._tokens.rotate(-1)

            count = 0
            while not self._git_api.get_rate_limit().core.remaining > 0:
                if count == len(self._tokens):
                    raise TenetError(f"Tokens exhausted")
                self._git_api = Github(self._tokens[0])
                self._tokens.rotate(-1)
                count += 1

            return self._git_api

    @git_api.deleter
    def git_api(self):
        with self.lock:
            self._git_api = None

    @staticmethod
    def parse_commit_sha(commit_sha: Union[list, str]) -> list:
        commit_hashes = []

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

        for cs in commit_sha:
            # Get diff only from the first parent if the fix is a merge commit
            commit_hash = ast.literal_eval(cs)[0]

            if isinstance(commit_hash, list):
                commit_hash = commit_hash[0]

            commit_hashes.append(commit_hash.split("#")[0].split("?")[0])

        return commit_hashes

    def get_commit(self, repo: Repository, commit_sha: str, raise_err: bool = False) -> Union[Commit, None]:
        # Ignore unavailable commits
        try:
            self.app.log.info(f"Getting commit {commit_sha}")
            return repo.get_commit(sha=commit_sha)
        except (ValueError, GithubException):
            err_msg = f"Commit {commit_sha} for repo {repo.name} unavailable: "
        except RateLimitExceededException as rle:
            err_msg = f"Rate limit exhausted: {rle}"
        #except Exception:
        #    err_msg = f"Unexpected error {sys.exc_info()}"

        if raise_err:
            raise TenetError(err_msg)

        self.app.log.error(err_msg)

        return None

    def get_repo(self, owner: str, project: str, raise_err: bool = False) -> Union[Repository, None]:
        repo_path = '{}/{}'.format(owner, project)

        try:
            self.app.log.info(f"Getting repo {repo_path}")
            return self.git_api.get_repo(repo_path)
        except RateLimitExceededException as rle:
            err_msg = f"Rate limit exhausted: {rle}"
        except UnknownObjectException:
            err_msg = f"Repo not found. Skipping {owner}/{project} ..."
        #except Exception:
        #    err_msg = f"Unexpected error {sys.exc_info()}"

        if raise_err:
            raise TenetError(err_msg)

        self.app.log.error(err_msg)

        return None

    def get_diff(self, commit: Commit, output_path: Path) -> str:

        if output_path.exists():
            self.app.log.info(f"{output_path} exists, reading...")

            with output_path.open(mode='r') as df:
                return df.read()

        self.app.log.info(f"Requesting {commit.raw_data['html_url']}.diff")
        diff_text = requests.get(f"{commit.raw_data['html_url']}.diff").text

        if output_path.exists() and output_path.stat().st_size != 0 and diff_text:
            # Save the diff string as a file

            with output_path.open(mode="w") as tmp_file:
                self.app.log.info(f"Writing diff to {output_path}")
                safe_write(tmp_file, diff_text, str(output_path.parents))

        return diff_text

    def get_file_from_commit(self, repo_file_path: str, commit: Commit, output_path: Path) -> Tuple[str, int]:
        if output_path.exists() and output_path.stat().st_size != 0:
            self.app.log.info(f"{output_path} exists, reading...")

            with output_path.open(mode='r') as f:
                f_str = f.read()
        else:
            url = f"{commit.html_url}/{repo_file_path}".replace('commit', 'raw')
            self.app.log.info(f"Requesting {url}")
            f_str = requests.get(url).text

            self.app.log.info(f"Writing {output_path}")
            output_path.parent.mkdir(exist_ok=True, parents=True)

            with output_path.open(mode="w") as f:
                f.write(f_str)

        return f_str, output_path.stat().st_size

    def get_blocks_from_diff(self, diff_text: str, extensions: list = None) -> List[DiffBlock]:
        """
        Parses the input diff string and returns a list of result entries.

        :param diff_text: The input git diff string in unified diff format.
        :param extensions: file to include from the diff based on extensions.
        :return: A list of entries resulted from the input diff to be appended to the output csv file.
        """

        if not diff_text:
            return []

        # Look for a_path
        lines = diff_text.splitlines()
        diff_path_bound = [line_id for line_id in range(len(lines)) if lines[line_id].startswith("--- ")]
        num_paths = len(diff_path_bound)
        diff_path_bound.append(len(lines))
        blocks = []
        # TODO: we want this too be more flexible, adapt
        extensions = extensions if extensions else self.app.get_config('proj_ext')

        for path_id in range(num_paths):
            # Only look for a_paths with the interested file extensions
            for ext in extensions:
                if lines[diff_path_bound[path_id]].endswith(ext):
                    # Only consider file modification, ignore file additions for now
                    block_start = diff_path_bound[path_id]
                    if not lines[block_start + 1].startswith("+++ "):
                        self.app.log.warning(f"Skipping block {block_start + 1} missing +++")
                        continue

                    # Ignore file deletions for now
                    if not lines[block_start + 1].endswith(" /dev/null"):
                        # Format of the "---" and "+++" lines:
                        # --- a/<a_path>
                        # +++ b/<b_path>
                        diff_block = DiffBlock(start=block_start, a_path=lines[block_start][len("--- a/"):],
                                               b_path=lines[block_start + 1][len("+++ b/"):])

                        # Do not include diff in the test files
                        if "test" in diff_block.a_path or "test" in diff_block.b_path:
                            continue

                        blocks.append(diff_block)

        return blocks

    def normalize_sha(self, chain: list):
        new_chain = []

        for commit_str in chain:

            # FIXME: WTF? Find why...
            if "//commit" in commit_str:
                commit_str = commit_str.replace("//commit", "/commit")

            if "pull/" in commit_str:
                owner, project, _, _, _, sha = commit_str.split("/")[3::]
            else:
                owner, project, _, sha = commit_str.split("/")[3:7]

            if len(sha) != 40:
                repo = self.get_repo(owner=owner, project=project)

                if not repo:
                    continue

                commit = self.get_commit(repo, sha.strip())

                if not commit:
                    continue

                new_chain.append(f"https://github.com/{owner}/{project}/commit/{commit.commit.sha}")
            else:
                new_chain.append(commit_str)

        return set(new_chain)

    def sort_chain(self, repo: Repository, chain: Union[str, set]) -> Tuple[Union[list, None], Union[list, None]]:
        if isinstance(chain, str):
            chain = list(eval(chain))

        df = pd.DataFrame()

        for commit_str in chain:
            commit = self.get_commit(repo, commit_sha=commit_str.split('/')[-1].strip())

            if not commit:
                return None, None

            author = commit.commit.author
            df = df.append({'commit': commit, 'datetime': author.date}, ignore_index=True)

        df = df.drop_duplicates()
        df = df.sort_values(by='datetime', ascending=True)

        # chain of commit and datetime
        return list(df['commit']), list(df['datetime'])

    @staticmethod
    def get_commit_parents(commit: Commit) -> set:
        return set([c.sha for c in commit.commit.parents])

    def get_commit_comments(self, commit: Commit, raise_err: bool = False) -> dict:
        comments, count = {}, 1
        err_msg = None

        try:
            for comment in commit.get_comments():
                comments[f'com_{count}'] = {
                    'author': comment.user.login,
                    'datetime': comment.created_at.strftime("%m/%d/%Y, %H:%M:%S"),
                    'body': comment.body.strip()
                }
                count += 1
        except RateLimitExceededException as rle:
            err_msg = f"Rate limit exhausted: {rle}"
        #except Exception:
        #    err_msg = f"Unexpected error {sys.exc_info()}"

        if err_msg:
            if raise_err:
                raise TenetError(err_msg)

            self.app.log.error(err_msg)

        return comments

    @staticmethod
    def get_commit_files(commit: Commit) -> dict:
        files = {}

        for f in commit.files:
            files[f.filename] = {
                'additions': f.additions,
                'deletions': f.deletions,
                'changes': f.changes,
                'status': f.status,
                'raw_url': f.raw_url,
                'patch': f.patch.strip() if f.patch else None
            }

        return files

    def get_commit_metadata(self, commit: Commit, include_comments: bool = True) -> CommitMetadata:
        comments = self.get_commit_comments(commit) if include_comments else {}
        files = self.get_commit_files(commit)
        stats = {'additions': commit.stats.additions, 'deletions': commit.stats.deletions, 'total': commit.stats.total}

        return CommitMetadata(author=commit.commit.author.name.strip(), message=commit.commit.message.strip(),
                              comments=str(comments) if len(comments) > 0 else np.nan, files=files, stats=stats)

    def get_chain_metadata(self, commit_sha: str, chain_ord: list, chain_datetime: list, raise_err: bool = False,
                           include_comments: bool = True) -> Union[ChainMetadata, None]:

        chain_ord_sha = []
        err_msg = None

        for commit in chain_ord:
            try:
                chain_ord_sha.append(commit.commit.sha)
            except RateLimitExceededException as rle:
                err_msg = f"Rate limit exhausted: {rle}"
                break
            #except Exception:
            #    err_msg = f"Unexpected error {sys.exc_info()}"
            #    break

        # self.app.log.info(f"Chain order: {chain_ord_sha}")

        if err_msg:
            if raise_err:
                raise TenetError(err_msg)

            self.app.log.error(err_msg)

        if len(chain_ord) != len(chain_ord_sha):
            return None

        try:
            chain_idx = chain_ord_sha.index(commit_sha)
        except ValueError as ve:
            self.app.log.error(f"{ve}")
            return None

        parents = list(self.get_commit_parents(chain_ord[-1]))

        commit_datetime = chain_datetime[chain_idx].strftime("%m/%d/%Y, %H:%M:%S")
        commit_metadata = self.get_commit_metadata(chain_ord[chain_idx], include_comments=include_comments)

        return ChainMetadata(commit_metadata=commit_metadata, chain_ord=chain_ord_sha, before_first_fix_commit=parents,
                             chain_ord_pos=chain_idx + 1, last_fix_commit=chain_ord[-1].commit.sha,
                             commit_sha=commit_sha, commit_datetime=commit_datetime)

    def get_project_metadata(self, project: str, chains: list, commits: list, indexes: list, save_path: Path,
                             include_comments: bool = True, drop_patch: bool = False) -> Union[pd.DataFrame, None]:

        # get owner and project
        if not pd.notna(project):
            return None

        owner, project_name = project.split('/')[3::]
        project_path = save_path / project_name

        if project_path.exists() and ChainMetadata.has_commits(project_path, commits):
            # in this case we do not need to query the api as the metadata has been saved previously
            self.app.log.info(f"Found metadata for {owner}/{project_name}")
            repo = None
        else:
            repo = self.get_repo(owner, project_name, raise_err=False)

            if not repo:
                return None

        self.app.log.info(f"Getting the metadata for {len(commits)} commits from project {project_name}...")
        chain_metadata_entries = []

        for idx, chain, commit_sha in zip(indexes, chains, commits):
            commit_metadata_path = project_path / f"{commit_sha}.json"
            commit_metadata_path.parent.mkdir(parents=True, exist_ok=True)
            chain_metadata_dict = ChainMetadata.load(path=commit_metadata_path)

            if not chain_metadata_dict:
                if repo is None:
                    repo = self.get_repo(owner, project_name, raise_err=False)

                    if repo is None:
                        break

                chain_ord, chain_datetime = self.sort_chain(repo, chain)

                if not chain_ord and not chain_datetime:
                    self.app.log.info(f"Skipping {commit_sha} ...")
                    continue

                chain_metadata = self.get_chain_metadata(commit_sha=commit_sha, chain_ord=chain_ord,
                                                         chain_datetime=chain_datetime, include_comments=include_comments)
                if chain_metadata is None:
                    continue

                chain_metadata_dict = chain_metadata.save(path=commit_metadata_path)

            if drop_patch and 'patch' in chain_metadata_dict:
                del chain_metadata_dict['patch']
            chain_metadata_dict.update({'index': idx})
            chain_metadata_entries.append(chain_metadata_dict)

        if chain_metadata_entries:
            return pd.DataFrame(chain_metadata_entries).set_index('index')

        return None

    def get_repo_commits(self, repo: Repository, raise_err: bool = False) \
            -> Union[Tuple[PaginatedList, int, int], Tuple[None, None, None]]:
        """
            Parses the input diff string and returns a list of result entries.

            :param repo: the Repository object
            :param raise_err: flag to raise caught exceptions
            :return: 2-sized-tuple with the PaginatedList commits and the numbers of pages.
        """
        try:
            repo_commits = repo.get_commits()
            total_commits = repo_commits.totalCount
            pages = (total_commits // self.paginated_list_size) + 1
            self.app.log.info(f"{repo.full_name} has {total_commits} commits (~{pages} pages).")
            return repo_commits, pages, total_commits
        except RateLimitExceededException as rle:
            err_msg = f"Rate limit exhausted: {rle}"
        except UnknownObjectException:
            err_msg = f"Failed to get {repo.full_name} repository commits..."
        #except Exception:
        #    err_msg = f"Unexpected error {sys.exc_info()}"

        if raise_err:
            raise TenetError(err_msg)

        self.app.log.error(err_msg)

        return None, None, None

    def repo_random_files_lookup(self, repo: Repository, target_files_count: int = 50, excluded_files: set = None,
                                 excluded_commits: set = None,  target_extensions: list = None,
                                 max_commits: int = None) \
            -> Tuple[pd.DataFrame, str]:
        self.app.log.info(f"Searching {repo.full_name} for {target_files_count} files...")
        paginated_commits, n_pages, total = self.get_repo_commits(repo=repo)

        if paginated_commits is None:
            return pd.DataFrame(), "error"

        paginated_list_random_picker = PaginatedListRandomPicker(paginated_commits, n_pages=n_pages)
        random_commit_file_lookup = RandomCommitFilesLookup(excluded_files=excluded_files,
                                                            target_extensions=target_extensions,
                                                            excluded_commits=excluded_commits)
        collected_files = []
        early_stopping = 0
        empty_commits = []

        while len(collected_files) < target_files_count:
            if max_commits is not None and early_stopping >= max_commits:
                self.app.log.warning(f"Early stopping after looking up {early_stopping} commits")
                return pd.DataFrame(collected_files + empty_commits), "stop"

            try:
                commits, rand_page = paginated_list_random_picker()
            except TenetError as sae:
                self.app.log.warning(sae)

                if random_commit_file_lookup.coverage() == total:
                    return pd.DataFrame(collected_files + empty_commits), "visited"
                else:
                    return pd.DataFrame(collected_files + empty_commits), "stop"

            self.app.log.info(f"Searching into page {rand_page}: {len(collected_files)}/{target_files_count}...")

            try:
                for commit, files in random_commit_file_lookup(commits, files_limit=target_files_count).items():
                    random_commit_file_lookup.excluded_commits.update({commit.sha})
                    early_stopping += 1

                    if len(files) == 0:
                        empty_commits.append({'sha': commit.sha, 'file_path': None, 'non_vuln_raw_url': None,
                                              'message': None})
                        continue

                    self.app.log.info(f"Found {len(files)} files for {commit.sha}...")

                    for file in files:
                        collected_files.append({'sha': commit.sha, 'file_path': file.filename,
                                                'non_vuln_raw_url': file.raw_url, 'message': commit.commit.message})
            except TenetError as sae:
                self.app.log.warning(sae)
                continue

        return pd.DataFrame(collected_files + empty_commits), "success"


#-----------------------------------------------------------------------------------#
#Chapter 5 - Bringing it all together

USA_edition_grouped = medals.loc[medals.NOC == 'USA'].groupby('Edition')

#Using .value_counts() for ranking

# Select the 'NOC' column of medals: country_names
country_names = medals['NOC']

# Count the number of medals won by each country: medal_counts
medal_counts = country_names.value_counts()

# Print top 15 countries ranked by medals
print(medal_counts.head(15))

#-----------------------------------------------------------------------------------#

#Using .pivot_table() to count medals by type
# Construct the pivot table: counted
counted = medals.pivot_table(index='NOC', values='Athlete', columns='Medal', aggfunc='count')

# Create the new column: counted['totals']
counted['totals'] = counted.sum(axis='columns')

# Sort counted by the 'totals' column
counted = counted.sort_values('totals', ascending=False)

# Print the top 15 rows of counted
print(counted.head(15))

#-----------------------------------------------------------------------------------#

#Applying .drop_duplicates()
# Select columns: ev_gen
ev_gen = medals[['Event_gender', 'Gender']]

# Drop duplicate pairs: ev_gen_uniques
ev_gen_uniques = ev_gen.drop_duplicates()

# Print ev_gen_uniques
print(ev_gen_uniques)

#-----------------------------------------------------------------------------------#
#Finding possible errors with .groupby()
# Group medals by the two columns: medals_by_gender
medals_by_gender = medals.groupby(['Event_gender', 'Gender'])

# Create a DataFrame with a group count: medal_count_by_gender
medal_count_by_gender = medals_by_gender.count()

# Print medal_count_by_gender
print(medal_count_by_gender)

#-----------------------------------------------------------------------------------#

#Locating suspicious data
# Create the Boolean Series: sus
sus = ((medals.Event_gender == 'W') & (medals.Gender == 'Men'))

# Create a DataFrame with the suspicious row: suspect
suspect = medals[sus]

# Print suspect
print(suspect)

#-----------------------------------------------------------------------------------#

#Using .nunique() to rank by distinct sports
# Group medals by 'NOC': country_grouped
country_grouped = medals.groupby('NOC')

# Compute the number of distinct sports in which each country won medals: Nsports
Nsports = country_grouped['Sport'].nunique()

# Sort the values of Nsports in descending order
Nsports = Nsports.sort_values(ascending=False)

# Print the top 15 rows of Nsports
print(Nsports.head(15))

#-----------------------------------------------------------------------------------#
#Counting USA vs. USSR Cold War Olympic Sports
# Extract all rows for which the 'Edition' is between 1952 & 1988: during_cold_war
during_cold_war = (medals.Edition>=1952) & (medals.Edition<=1988)

# Extract rows for which 'NOC' is either 'USA' or 'URS': is_usa_urs
is_usa_urs = medals.NOC.isin(['USA', 'URS'])

# Use during_cold_war and is_usa_urs to create the DataFrame: cold_war_medals
cold_war_medals = medals.loc[during_cold_war & is_usa_urs]

# Group cold_war_medals by 'NOC'
country_grouped = cold_war_medals.groupby('NOC')

# Create Nsports
Nsports = country_grouped['Sport'].nunique().sort_values(ascending=False)

# Print Nsports
print(Nsports)

#-----------------------------------------------------------------------------------#

#Counting USA vs. USSR Cold War Olympic Medals
# Create the pivot table: medals_won_by_country
medals_won_by_country = medals.pivot_table(index='Edition', columns='NOC', values='Athlete', aggfunc='count')

# Slice medals_won_by_country: cold_war_usa_usr_medals
cold_war_usa_usr_medals = medals_won_by_country.loc[1952:1988, ['USA','URS']]

# Create most_medals
most_medals = cold_war_usa_usr_medals.idxmax(axis='columns')

# Print most_medals.value_counts()
print(most_medals.value_counts())

#-----------------------------------------------------------------------------------#

#Visualizing USA Medal Counts by Edition: Line Plot
# Create the DataFrame: usa
usa = medals[medals.NOC == 'USA']

# Group usa by ['Edition', 'Medal'] and aggregate over 'Athlete'
usa_medals_by_year = usa.groupby(['Edition', 'Medal'])['Athlete'].count()

# Reshape usa_medals_by_year by unstacking
usa_medals_by_year = usa_medals_by_year.unstack(level='Medal')

# Plot the DataFrame usa_medals_by_year
usa_medals_by_year.plot()
plt.show()

#-----------------------------------------------------------------------------------#
#Visualizing USA Medal Counts by Edition: Area Plot

# Create the DataFrame: usa
usa = medals[medals.NOC == 'USA']

# Group usa by 'Edition', 'Medal', and 'Athlete'
usa_medals_by_year = usa.groupby(['Edition', 'Medal'])['Athlete'].count()

# Reshape usa_medals_by_year by unstacking
usa_medals_by_year = usa_medals_by_year.unstack(level='Medal')

# Create an area plot of usa_medals_by_year
usa_medals_by_year.plot.area()
plt.show()

#-----------------------------------------------------------------------------------#

#Visualizing USA Medal Counts by Edition: Area Plot with Ordered Medals
# Redefine 'Medal' as an ordered categorical
medals.Medal = pd.Categorical(values = medals.Medal,categories=['Bronze', 'Silver', 'Gold'],ordered=True)

# Create the DataFrame: usa
usa = medals[medals.NOC == 'USA']

# Group usa by 'Edition', 'Medal', and 'Athlete'
usa_medals_by_year = usa.groupby(['Edition', 'Medal'])['Athlete'].count()

# Reshape usa_medals_by_year by unstacking
usa_medals_by_year = usa_medals_by_year.unstack(level='Medal')

# Create an area plot of usa_medals_by_year
usa_medals_by_year.plot.area()
plt.show()

#-----------------------------------------------------------------------------------#

Manipulating DataFrames with pandas/Chapter 5 - Bringing it all together.py


import xlrd
import random
from random import shuffle
from random import randint

best_value_arr = []
best_route = []
savedIteration = 0
best_value = 10000
something = False
population = 10
cities_List = [2, 3, 4, 5, 6, 7, 8]
#creating a 2d array and filling it with 'x'
rand_pop = [['x' for i in range(7)] for j in range(10)]
#when getting the values the first item is row and the 2nd is column
data = xlrd.open_workbook('/Users/prajwalshrestha/Desktop/travelling_salesman/travelling_sales.xlsx')
worksheet = data.sheet_by_name('Sheet1')
#print(worksheet.cell(1,0).value)
#print(data.sheet_names())
for iteration in range(0,2000):
#function to calculate the initial total of the first generation population
	def calc_init_distance():
		#array to store the total distances calculated
		dist_Total = []
		#dict to store the total distance and array which holds the path it tool
		keyValue = {}
		#dict which is sorted according to the distances
		sortedKeyVal = {}
		#for loop calculate the distances from the array that is provided
		for i in range(0,population):
			total = 0
			for j in range (0,7):
				#to prevent Index Error
				if(j + 1 < len(rand_pop[1])):
					total = total + worksheet.cell(rand_pop[i][j],rand_pop[i][j + 1]).value
			#adding the distance of london to the first value of the rand_pop array because every journey starts from london and
			#also adding the distance from last destination to london
			lonToX = worksheet.cell(1, rand_pop[i][0]).value
			#storing the distance from x to london as the journey needs to end in london
			XtoLon = worksheet.cell(rand_pop[i][6],1).value
			#adding the distances from london to x and x to london
			total += (lonToX + XtoLon)
			keyValue[total] = rand_pop[i]
			#print(total)
			#creating an array to store the total distances calculated for each array in the rand_pop array
		topFiveDist = 0
		#loop that only gets the first five of the key:value pair list
		for key in sorted(keyValue.keys()):
			if(topFiveDist < 5):
				sortedKeyVal[key] = keyValue[key]
			else:
				break
			topFiveDist += 1
		best_value_arr.append(min(int(key) for key in sortedKeyVal))
		best_value = min(best_value_arr)
		if(iteration == 1999):
			print('best_value = ' + str(best_value))
		if(sortedKeyVal.get(best_value) != None):
			global string
			global best_route
			best_route = sortedKeyVal.get(best_value)
			print('Best route = ' + str(sortedKeyVal.get(best_value)))
		if iteration == 1999:
			print('The best route to take is: London -> ' + str(worksheet.cell(0, best_route[0]).value) + ' -> ' + str(worksheet.cell(0,best_route[1]).value) + ' -> ' + str(worksheet.cell(0,best_route[2]).value) + ' -> ' + str(worksheet.cell(0,best_route[3]).value) + ' -> ' + str(worksheet.cell(0,best_route[4]).value) + ' -> ' + str(worksheet.cell(0,best_route[5]).value) + ' -> ' + str(worksheet.cell(0,best_route[6]).value) + ' -> London')

	def mutation():
		counter = 0
		mutation_rate = 0.05
		mutated = []
		#the key:value pair list from stored in a different variable as a duplicate
		keyValuePair = calc_init_distance()
		#the values for the keyValuePair dict stored in a list
		for values in keyValuePair.values():
			mutated.append(values)
		#for loop to make sure that all the arrays go through a check for mutation
		for x in range(0,len(keyValuePair.values())-1):
			#random value created to check if the mutation is to be applied
			randomVal = random.random()
			#if statement that checks if the value of the randomVal is greater than the mutation rate and if it is then mutation is applied
			if(randomVal < mutation_rate):
				# two different random values that are the indexes that needs to be exchanged
				randomInList1 = randint(0,len(list(keyValuePair.values())[0])-1)
				randomInList2 = randint(0,len(list(keyValuePair.values())[0])-1)
				#print(list(keyValuePair.values())[x])
				#while loop so that the random indexes are not the same
				while(randomInList1 == randomInList2):
					randomInList2 = randint(0,len(list(keyValuePair.values())[0])-1)
				#performing the swap of random indexes
				list(keyValuePair.values())[x][randomInList1], list(keyValuePair.values())[x][randomInList2] = list(keyValuePair.values())[x][randomInList2], list(keyValuePair.values())[x][randomInList1]
				#items being added to the mutated list once the mutation is done
				mutated[x] = list(keyValuePair.values())[x]
				#print(list(keyValuePair.values())[x])
		return mutated

	def crossOver():
		#storing the array returned from the mutation function in a new array
		crossOvered = mutation()
		#making a new array with 0s inside
		newArray = [0] * 7
		#creating two random values between 0 and 4 for selecting random arrays on the Map variable
		randArr1 = randint(0,len(crossOvered)-1)
		randArr2 = randint(0,len(crossOvered)-1)
		#while loop so check and change if the two random values are the same as we dont want to select the same two arrays
		while(randArr1 == randArr2):
			randArr2 = randint(0,len(crossOvered)-1)
		array1 = crossOvered[randArr1]
		array2 = crossOvered[randArr2]
		#random index is picked from an array
		getRindex = randint(0,len(crossOvered[0])-2)
		#replacing the value of one of the 0s in the position getRindex to the value of array1[getRindex]
		newArray[getRindex] = array1[getRindex]
		#increasing the value of getRindex by one so that the 2nd 0 value can be replace by the next value of array1
		getRindex += 1
		newArray[getRindex] = array1[getRindex]
		#increasing the value again so that the replacing of the 0s starts from the index next to the value that has already been filled
		getRindex += 1
		#preventing index error
		if(getRindex > 6):
			getRindex = 0
		#skips valriable is used in the loop below
		skips = 0
		#while loop that continues until all the 0 values are filled
		while(0 in newArray):
			#while loop that checks if there are any duplicates given therere are no index errors
			while(array2[getRindex] in newArray):
				#preventing index error
				if(getRindex + 1 == 7):
					getRindex = 0
				else:
					getRindex += 1
				#skips variable is used to keep the index of the newArray the same when the index of the array2 changes
				skips += 1
			#replacing the 0s after the duplicate value has been skipped
			newArray[getRindex - skips] = array2[getRindex]
			#increasing the index so that other 0s can be replaced
			getRindex += 1
			if(getRindex > 6):
				getRindex = 0
		#print(crossOvered)
		#print('new Array = ' + str(newArray))
		crossOvered.append(newArray)
		return crossOvered

	# if the first iteration has finished then the new population needs to be allocated
	if something == False:
			#nested loop that runs to create a 2d array with shuffled numbers
		for x in range(population):
			#shuffling the list to find the first 10 inidividuals
			shuffle(cities_List)
			shuffle_it = cities_List
			for y in range(len(cities_List)):
				#creating 2d array to save all the random value list created so that the total distances can be calculated for each list
				rand_pop[x][y] = shuffle_it[y]
	#when the first iteration has finished create population of 10 with the ones that were the best from the last generation
	else:
		nextGen = crossOver()
		for x in range(0,3):
			#shuffling the list to find the first 10 inidividuals
			shuffle(cities_List)
			shuffle_it = cities_List
			nextGen.append(shuffle_it)
		rand_pop = nextGen
	calc_init_distance()


from django import forms
from django.contrib.auth import get_user_model

from orders.models import UserAddress

class GuestCheckoutForm(forms.Form):
    email = forms.EmailField()
    email2 = forms.EmailField(label="Verify email")

    def clean(self):
        email = self.cleaned_data.get('email')
        email2 = self.cleaned_data.get('email2')

        if email == email2:
            user = get_user_model()

            # Checking the mail already exist in the user database or not
            user_exist = user.objects.filter(email=email).count()
            if user_exist != 0:
                raise forms.ValidationError("This user already exist. Please login instead.")
        else:
            raise forms.ValidationError("Please confirm emails are the same.")

class AddressSelectForm(forms.Form):
    billing_address = forms.ModelChoiceField(
            queryset=UserAddress.objects.filter(type="billing"),
            widget=forms.RadioSelect,
            empty_label=None
            )
    shipping_address = forms.ModelChoiceField(
            queryset=UserAddress.objects.filter(type="shipping"),
            widget=forms.RadioSelect,
            empty_label=None
            )

# Address create form
class AddressCreateForm(forms.ModelForm):
    class Meta:
        model = UserAddress
        fields = ["street", "city", "state", "zipcode", "type"]


import tensorflow as tf

def train_input_fn(features, labels, batch_size):
    """An input function for training"""
    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
    # Shuffle, repeat, and batch the examples.
    dataset = dataset.shuffle(1000).repeat().batch(batch_size)
    # Build the Iterator, and return the read end of the pipeline.
    return dataset.make_one_shot_iterator().get_next()

def ffn_layer(name, inputs, hidden_units, activation=tf.nn.relu, seed=None):
    """Reusable code for making a simple neural net layer.

    It does a matrix multiply, bias add, and then uses relu to nonlinearize.
    It also sets up name scoping so that the resultant graph is easy to read,
    and adds a number of summary ops.
    """
    input_dim = inputs.get_shape().as_list()[1]
    # use xavier glorot intitializer as regular uniform dist did not work
    weight_initializer = tf.contrib.layers.xavier_initializer(uniform=True, seed=seed, dtype=tf.float32)
    with tf.variable_scope(name):
        # This Variable will hold the state of the weights for the layer
        with tf.name_scope('weights'):
            weights = tf.get_variable('W', [input_dim, hidden_units], initializer=weight_initializer)
            variable_summaries(weights)
        with tf.name_scope('biases'):
            biases = tf.get_variable('b', [hidden_units], initializer=tf.zeros_initializer)
            variable_summaries(biases)
        with tf.name_scope('Wx_plus_b'):
            preactivate = tf.matmul(inputs, weights) + biases
            tf.summary.histogram('pre_activations', preactivate)
        activations = activation(preactivate, name='activation')
        tf.summary.histogram('activations', activations)
        return activations
        # layer = tf.layers.dense(inputs=inputs, units=hidden_units, activation=activation)
        # return layer

def conv_layer_1d(name, inputs, num_filters=1, filter_size=(1, 1), strides=1, activation=None):
    """"A simple function to create a 1D conv layer"""
    with tf.variable_scope(name):
        # TensorFlow operation for convolution
        layer = tf.layers.conv1d(inputs=inputs,
                                 filters=num_filters,
                                 kernel_size=filter_size,
                                 strides=strides,
                                 padding='same',
                                 activation=activation)
        return layer

def conv_layer_2d(name, inputs, num_filters=1, filter_size=(1, 1), strides=(1,1), activation=None):
    """"A simple function to create a 2D conv layer"""
    with tf.variable_scope(name):
        # TensorFlow operation for convolution
        layer = tf.layers.conv2d(inputs=inputs,
                                 filters=num_filters,
                                 kernel_size=filter_size,
                                 strides=strides,
                                 padding='same',
                                 activation=activation)
        return layer

def max_pool_layer(inputs, name, pool_size=(1, 1), strides=(1, 1), padding='same'):
    """"A simple function to create a max pool layer"""
    with tf.variable_scope(name):
        # TensorFlow operation for max pooling
        layer = tf.layers.max_pooling2d(inputs=inputs,
                                        pool_size=pool_size,
                                        strides=strides,
                                        padding=padding)
        return layer

def max_pool_layer1d(inputs, name, pool_size=(1, 1), strides=(1, 1), padding='same'):
    """"A simple function to create a max pool layer"""
    with tf.variable_scope(name):
        # TensorFlow operation for max pooling
        layer = tf.layers.max_pooling1d(inputs=inputs,
                                        pool_size=pool_size,
                                        strides=strides,
                                        padding=padding)
        return layer
def dropout(inputs, keep_prob=1.0):
    with tf.name_scope('dropout'):
        # keep_prob = tf.placeholder(tf.float32)
        tf.summary.scalar('dropout_keep_probability', keep_prob)
        return tf.nn.dropout(inputs, keep_prob)

def variable_summaries(var):
  """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
  with tf.name_scope('summaries'):
    mean = tf.reduce_mean(var)
    tf.summary.scalar('mean', mean)
    with tf.name_scope('stddev'):
      stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
    tf.summary.scalar('stddev', stddev)
    tf.summary.scalar('max', tf.reduce_max(var))
    tf.summary.scalar('min', tf.reduce_min(var))
    tf.summary.histogram('histogram', var)

#
# mnist_data = input_data.read_data_sets('MNIST_data',
#                                        one_hot=True,   # Convert the labels into one hot encoding
#                                        dtype='float32', # rescale images to `[0, 1]`
#                                        reshape=False, # Don't flatten the images to vectors
#                                        )

# # Simple MNIST test of the layers
#
# tf.reset_default_graph()
# num_classes = 10
# cm = conf.ConfusionMatrix(num_classes)
# height, width, nchannels = 28, 28, 1
# gpu_opts = tf.GPUOptions(per_process_gpu_memory_fraction=0.45)
# filters_1 = 16
# kernel_size_1 = (5,5)
# pool_size_1 = (2,2)
# x_pl = tf.placeholder(tf.float32, [None, height, width, nchannels], name='xPlaceholder')
# y_pl = tf.placeholder(tf.float64, [None, num_classes], name='yPlaceholder')
# y_pl = tf.cast(y_pl, tf.float32)
#
# x = conv_layer_2d('layer1', x_pl, filters_1, kernel_size_1, activation=tf.nn.relu)
# x = max_pool_layer(x, 'max_pool', pool_size = pool_size_1, strides=pool_size_1)
# x = tf.contrib.layers.flatten(x)
#
# y = ffn_layer('output_layer', x, hidden_units=num_classes, activation=tf.nn.softmax)
#
# with tf.variable_scope('loss'):
#     # computing cross entropy per sample
#     cross_entropy = -tf.reduce_sum(y_pl * tf.log(y + 1e-8), reduction_indices=[1])
#
#     # averaging over samples
#     cross_entropy = tf.reduce_mean(cross_entropy)
#
# with tf.variable_scope('training'):
#     # defining our optimizer
#     optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
#
#     # applying the gradients
#     train_op = optimizer.minimize(cross_entropy)
#
# with tf.variable_scope('performance'):
#     # making a one-hot encoded vector of correct (1) and incorrect (0) predictions
#     correct_prediction = tf.equal(tf.argmax(y, axis=1), tf.argmax(y_pl, axis=1))
#
#     # averaging the one-hot encoded vector
#     accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#
#
# # Training Loop
# batch_size = 100
# max_epochs = 10
#
# valid_loss, valid_accuracy = [], []
# train_loss, train_accuracy = [], []
# test_loss, test_accuracy = [], []
#
# with tf.Session() as sess:
#     sess.run(tf.global_variables_initializer())
#     print('Begin training loop')
#
#     try:
#         while mnist_data.train.epochs_completed < max_epochs:
#             _train_loss, _train_accuracy = [], []
#
#             ## Run train op
#             x_batch, y_batch = mnist_data.train.next_batch(batch_size)
#             fetches_train = [train_op, cross_entropy, accuracy]
#             feed_dict_train = {x_pl: x_batch, y_pl: y_batch}
#             _, _loss, _acc = sess.run(fetches_train, feed_dict_train)
#
#             _train_loss.append(_loss)
#             _train_accuracy.append(_acc)
#
#             ## Compute validation loss and accuracy
#             if mnist_data.train.epochs_completed % 1 == 0 \
#                     and mnist_data.train._index_in_epoch <= batch_size:
#                 train_loss.append(np.mean(_train_loss))
#                 train_accuracy.append(np.mean(_train_accuracy))
#
#                 fetches_valid = [cross_entropy, accuracy]
#
#                 feed_dict_valid = {x_pl: mnist_data.validation.images, y_pl: mnist_data.validation.labels}
#                 _loss, _acc = sess.run(fetches_valid, feed_dict_valid)
#
#                 valid_loss.append(_loss)
#                 valid_accuracy.append(_acc)
#                 print(
#                     "Epoch {} : Train Loss {:6.3f}, Train acc {:6.3f},  Valid loss {:6.3f},  Valid acc {:6.3f}".format(
#                         mnist_data.train.epochs_completed, train_loss[-1], train_accuracy[-1], valid_loss[-1],
#                         valid_accuracy[-1]))
#
#         test_epoch = mnist_data.test.epochs_completed
#         while mnist_data.test.epochs_completed == test_epoch:
#             x_batch, y_batch = mnist_data.test.next_batch(batch_size)
#             feed_dict_test = {x_pl: x_batch, y_pl: y_batch}
#             _loss, _acc = sess.run(fetches_valid, feed_dict_test)
#             y_preds = sess.run(fetches=y, feed_dict=feed_dict_test)
#             y_preds = tf.argmax(y_preds, axis=1).eval()
#             y_true = tf.argmax(y_batch, axis=1).eval()
#             cm.batch_add(y_true,y_preds)
#             test_loss.append(_loss)
#             test_accuracy.append(_acc)
#         print('Test Loss {:6.3f}, Test acc {:6.3f}'.format(
#             np.mean(test_loss), np.mean(test_accuracy)))
#
#
#     except KeyboardInterrupt:
#         pass
#
#
# print(cm.accuracy())
# epoch = np.arange(len(train_loss))
# plt.figure()
# plt.plot(epoch, train_accuracy,'r', epoch, valid_accuracy,'b')
# plt.legend(['Train Acc','Val Acc'], loc=4)
# plt.xlabel('Epochs'), plt.ylabel('Acc'), plt.ylim([0.75,1.03])
# plt.show()