from flask import Flask
from flask import redirect, url_for
from flask import render_template
from flask import request,session
import requests
from datetime import timedelta
from bs4 import BeautifulSoup

from bs4 import BeautifulSoup
import pandas as pd
import numpy as np

from time import sleep
from random import randint
from flask_table import Table, Col
from PIL import Image
import seaborn as sns
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
from werkzeug.utils import secure_filename
from werkzeug.datastructures import  FileStorage
import re
from PIL import Image
import gensim
from gensim import corpora

from nltk import FreqDist
import plotly
import plotly.graph_objs as go
from bokeh.io import output_file, show
from bokeh.plotting import figure
import json
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import nltk
nltk.download('wordnet')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
nltk.download('vader_lexicon')

cut_name = []
dates = []
rating = []
cust_review = []

source = requests.get('https://webscraper.netlify.com/').text
soup = BeautifulSoup(source, 'lxml')

app= Flask(__name__)
app.secret_key = 'hello'
app.permanent_session_lifetime = timedelta(seconds=2)

def get_profile_detail(user_handle):

    url = "https://www.flipkart.com/search?q={}".format(user_handle)

    page = requests.get(url)
    soup = BeautifulSoup(page.content,'html.parser')
    print(soup.prettify())
    final_link = []
    for a in soup.find_all('a',class_='_31qSD5', href=True):
       final_link.append("https://www.flipkart.com"+a['href'])

    price = soup.find_all("div",class_= "_1vC4OE _2rQ-NK")
    name = soup.find_all("div",class_= "col col-7-12")

    review_title = []
    price_pro = []
    for i in range(0,len(name)):
        review_title.append(name[i].get_text())

    review_title[:]= [name.lstrip("\n")for name in review_title]
    review_title[:]= [name.rstrip("\n")for name in review_title]

    for i in range(0,len(price)):
        price_pro.append(price[i].get_text())

    price_pro[:]= [price.lstrip("\n")for price in price_pro]
    price_pro[:]= [price.rstrip("\n")for price in price_pro]

    return review_title,price_pro,final_link

@app.route('/<user_handle>/')
def home(user_handle):
    review_title,price_pro,final_link = get_profile_detail(user_handle)
    amazon =pd.DataFrame({'price': price_pro, 'name': review_title, 'link': final_link})
    html = amazon.to_html()
    return html

def get_review_detail(url_handle,pd_handle):
    header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    cust_review = []
    html=home(pd_handle)
    dfs = pd.read_html(html)
    df = dfs[0]
    def num(url_handle):
        return f'{url_handle}'
    orange=int(num(url_handle))
    trng=df.iloc[orange,3]
    res = trng.split('srno', 1)[0]

    reep=str(res).replace('/p/', '/product-reviews/')
    reep
    urr=reep+'page='

    rev_num = requests.get(trng, headers=header)

    soup = BeautifulSoup(rev_num.text, 'html.parser')
    list_header=[]
    pa = soup.find_all('span', class_='_38sUEc')
    ss=str(pa).split('&amp', 1)[1]
    ss
    su=str(ss).split('Reviews', 1)[0]
    su
    l=su[15:]
    int_b = int(l.replace(',',''))
    print ("The integer value",int_b)
    srinivas=(int_b//10)+1
    srinivas
    pages = np.arange(1, srinivas)
    for items in header:
        try:
            list_header.append(items.get_text())
        except:
            continue

    for page in pages:

      page = requests.get(urr + str(page) + "&ref_=adv_nxt", headers=header)

      soup = BeautifulSoup(page.text, 'html.parser')

      review_div = soup.find_all('div', class_='col _390CkK _1gY8H-')

      sleep(randint(1,8))
      for container in review_div:
            # runtime
            review = container.find('div', class_='qwjRop').text
            mod_review= review.rstrip("READ MORE")
            cust_review.append(mod_review)
            dataFrame = pd.DataFrame(data = cust_review)
            dataFrame.to_csv('Geeks.csv')

    return cust_review

@app.route('/homme/<url_handle>/<pd_handle>/')
def homme(url_handle,pd_handle):
    cust_review=get_review_detail(url_handle,pd_handle)
    flipkart = pd.DataFrame({'Review': cust_review})
    Review_tbe=flipkart.to_html()
    return Review_tbe

@app.route('/download/<url_handle>/<pd_handle>/')
def download(url_handle,pd_handle):
    cust_review=get_review_detail(url_handle,pd_handle)
    flipkart = pd.DataFrame({'Review': cust_review})
    df= flipkart.to_html()
    return df

@app.route('/king')
def king():
    return redirect(url_for('home', user_handle='mobile'))

@app.route('/',methods=['POST','GET'])
def login():
    if request.method == 'POST':
        session.permanent = True
        user = request.form['nm']
        session['user']=user
        return redirect(url_for('user'))
    else:
        if 'user' in session:
            return redirect(url_for('user'))
        return render_template('login.html')

@app.route('/user',methods=['POST','GET'])
def user(user_handle='sumit'):
    if request.method == 'POST':
        session.permanent = True
        usr = request.form['url']
        session['user']=usr
        return redirect(url_for('home', user_handle= usr))

    if 'user' in session:
        user = session['user']
        return render_template("user.html", user=user)

    else:
        return redirect(url_for('login'))

@app.route('/logout.html/',methods=['POST','GET'])
def logout():
    if request.method == 'POST':
        session.permanent = True
        ussr = request.form['urll']
        usssr = request.form['urlll']
        session['user']=ussr
        return render_template('redirect.html',number=ussr,pd=usssr)
    else:
        return render_template('logout.html')

@app.route('/logout1.html/')
def upload():
    return render_template("logout 1.html")

@app.route('/success', methods = ['POST'])
def success():
    if request.method == 'POST':
        f = request.files['file']
        f.save(f.filename)
        return render_template("success.html", name = f.filename)

from textblob import TextBlob

def fetch_sentiment_using_textblob(text):
    sentiment = []
    for i in text:
        analysis = TextBlob(i)
        # set sentiment
        if analysis.sentiment.polarity >= 0:
            sentiment.append('positive')
        else:
            sentiment.append('negative')
    return sentiment

@app.route('/polarity')
def polarity():
    data1=pd.read_html('datalog.html')
    df=data1[0]
    final_data=pd.DataFrame(df)
    corpus = []
    for i in range(0,len(final_data)):
        review = re.sub('[^a-zA-Z]', ' ', final_data['Review'][i])
        review = review.lower()
        review = review.split()
        ps = WordNetLemmatizer()
        review = [ps.lemmatize(word) for word in review if not word in set(stopwords.words('english'))]
        review = ' '.join(review)
        corpus.append(review)
    review_list = final_data['Review'].tolist()
    final_data['sentiment']= fetch_sentiment_using_textblob(review_list)
    final_data['sentiment'].value_counts()
    pos=final_data.groupby('sentiment')
    pos_values=pos.get_group('positive')
    pos_values
    pos_values['Review']
    sid = SentimentIntensityAnalyzer()
    sentiment_summary = dict()
    # for readme in readmes:
    #     sentences = nltk.tokenize.sent_tokenize(readme)
    #     for sentence in sentences:
    #         sentiment_score = sid.polarity_scores(sentence)messages = pos_list
    messages = pos_values['Review']
    summary = {"positive":0,"neutral":0,"negative":0}
    for x in messages:
        ss = sid.polarity_scores(x)
        if ss["compound"] == 0.0:
            summary["neutral"] +=1
        elif ss["compound"] > 0.0:
            summary["positive"] +=1
        else:
            summary["negative"] +=1
    sen=[]

    sid = SentimentIntensityAnalyzer()
    sentiment_summary = dict()
    # for readme in readmes:
    #     sentences = nltk.tokenize.sent_tokenize(readme)
    #     for sentence in sentences:
    #         sentiment_score = sid.polarity_scores(sentence)messages = pos_list
    messages = pos_values['Review']
    summary = {"positive":0,"neutral":0,"negative":0}
    for x in messages:
        ss = sid.polarity_scores(x)
        if ss["compound"] == 0.0:
            sen.append('Neutral')
        elif ss["compound"] > 0.0:
            sen.append('Positive')
        else:
            sen.append('negative')
    pd.DataFrame(sen).count()

    pos_values.index

    positive=pd.concat([pos_values.drop('sentiment',axis=1),pd.DataFrame(sen,index=pos_values.index)],axis=1)

    pos.get_group('negative')

    sds=positive.rename(columns={0: "sentiment"})

    sumit=pd.concat([sds,pos.get_group('negative')])

    sns.countplot('sentiment',data=sumit)
    lp=sumit['sentiment'].value_counts()
    kp=pd.DataFrame(lp)
    kp.reset_index(inplace=True)
    kp['sentiment']
    output_file('bars.html')
    colors = ["#c9d9d3", "#718dbf", "#e84d60"]
    p=figure(x_range= list(kp['index']), plot_height=250, title="polarity",
           toolbar_location=None, tools="")
    p.vbar(
         x= list(kp['index']),
        top= list(kp['sentiment']),color=colors,
        width=0.9
    )

    p.xgrid.grid_line_color = None
    p.y_range.start = 0
    show(p)

    return sumit.to_html()

def par():
    data1=pd.read_html('datalog.html')
    df=data1[0]
    final_data=pd.DataFrame(df)
    corpus = []
    for i in range(0,len(final_data)):
        review = re.sub('[^a-zA-Z]', ' ', final_data['Review'][i])
        review = review.lower()
        review = review.split()
        ps = WordNetLemmatizer()
        review = [ps.lemmatize(word) for word in review if not word in set(stopwords.words('english'))]
        review = ' '.join(review)
        corpus.append(review)
    review_list = final_data['Review'].tolist()
    final_data['sentiment']= fetch_sentiment_using_textblob(review_list)
    final_data['sentiment'].value_counts()
    pos=final_data.groupby('sentiment')
    pos_values=pos.get_group('positive')
    pos_values
    pos_values['Review']
    sid = SentimentIntensityAnalyzer()
    sentiment_summary = dict()
    # for readme in readmes:
    #     sentences = nltk.tokenize.sent_tokenize(readme)
    #     for sentence in sentences:
    #         sentiment_score = sid.polarity_scores(sentence)messages = pos_list
    messages = pos_values['Review']
    summary = {"positive":0,"neutral":0,"negative":0}
    for x in messages:
        ss = sid.polarity_scores(x)
        if ss["compound"] == 0.0:
            summary["neutral"] +=1
        elif ss["compound"] > 0.0:
            summary["positive"] +=1
        else:
            summary["negative"] +=1
    sen=[]

    sid = SentimentIntensityAnalyzer()
    sentiment_summary = dict()
    # for readme in readmes:
    #     sentences = nltk.tokenize.sent_tokenize(readme)
    #     for sentence in sentences:
    #         sentiment_score = sid.polarity_scores(sentence)messages = pos_list
    messages = pos_values['Review']
    summary = {"positive":0,"neutral":0,"negative":0}
    for x in messages:
        ss = sid.polarity_scores(x)
        if ss["compound"] == 0.0:
            sen.append('Neutral')
        elif ss["compound"] > 0.0:
            sen.append('Positive')
        else:
            sen.append('negative')
    pd.DataFrame(sen).count()

    pos_values.index

    positive=pd.concat([pos_values.drop('sentiment',axis=1),pd.DataFrame(sen,index=pos_values.index)],axis=1)

    pos.get_group('negative')

    sds=positive.rename(columns={0: "sentiment"})

    sumit=pd.concat([sds,pos.get_group('negative')])
    return sumit
@app.route('/word')
def word():
    data=par()
    wc = WordCloud(background_color="black",
                   width = 800, height = 800)

    wc.generate(str(data['Review']))
    wc.to_file("wordcloud.png")

    filename = Image.open("wordcloud.png")
    filename.show()
    return '
see the wordcloud in the PNG FILE BELOW ðŸ‘‡
'

@app.route('/positive')
def positive():
    data=par()
    sent=data.groupby('sentiment')
    positive_values=sent.get_group('Positive')
    wc = WordCloud(background_color="black",
                   width = 800, height = 800)

    wc.generate(str(positive_values['Review']))
    wc.to_file("wordcloud.png")

    filename = Image.open("wordcloud.png")
    filename.show()
    return '
see the wordcloud in the PNG FILE BELOW ðŸ‘‡
'

@app.route('/negative')
def negative():
    data=par()
    sent=data.groupby('sentiment')
    positive_values=sent.get_group('negative')
    wc = WordCloud(background_color="black",
                   width = 800, height = 800)

    wc.generate(str(positive_values['Review']))
    wc.to_file("wordcloud.png")

    filename = Image.open("wordcloud.png")
    filename.show()
    return '
see the wordcloud in the PNG FILE BELOW ðŸ‘‡
'

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

    app.run(host='0.0.0.0', port=5000)


class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        stack, store = [], {}

        for num in nums2:
            while stack and stack[-1] < num:
                store[stack.pop()] = num
            stack.append(num)

        return [store.get(n, -1) for n in nums1]


# https://www.acmicpc.net/problem/7576

import sys
from collections import deque
m, n = map(int, sys.stdin.readline().strip().split())
arr = [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

# print(arr)

count = -1
lst = []
for i in range(n):
    for k in range(m):
        if arr[i][k] == 1:
            lst.append([i, k])
dq = deque([lst])
while dq:
    nl = []
    l = dq.popleft()
    if len(l) == 0:
        break

    count += 1
    for i, k in l:
        for x, y in [[0, 1], [1, 0], [0, -1], [-1, 0]]:
            xx, yy = i+x, k+y
            if 0 <= xx < n and 0 <= yy < m and arr[xx][yy] == 0:
                arr[xx][yy] = 1
                nl.append([xx, yy])
    dq.append(nl)

flag = True
for i in arr:
    if 0 in i:
        flag = False
        break

if flag:
    print(count)
else:
    print(-1)


from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView
# from django.contrib.auth.mixins import LoginRequiredMixin

from .models import Author, Book, Review

class IndexView(ListView):
    template_name = 'books/index.html'
    context_object_name = 'books'

    def get_queryset(self):
        return Book.objects.all()

class BookDetailView(DetailView):
    template_name = 'books/detail.html'
    model = Book

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['reviews'] = context['book'].review_set.order_by('-created_at')
        context['authors'] = context['book'].authors.all()
        return context

def author(request, author):
    books = Book.objects.filter(authors__name=author)
    context_dict = {
        'books': books,
    }
    return render(request, 'books/author_books.html', context_dict)

def review(request, id):
    if request.user.is_authenticated:
        body = request.POST['review']
        new_review = Review(body=body, book_id=id, user=request.user)
        new_review.save()
    return redirect('home')


from math import *
import matplotlib.pyplot as plt
import time

def mplot(x, y1, y2):
    fig = plt.figure(figsize=(15, 5))
    ax1 = fig.add_subplot(1, 2, 1)
    ax2 = fig.add_subplot(1, 2, 2)

    ax1.plot(x, y1)
    ax1.set_title('Quadratic')
    ax1.set_xlabel('x axis')
    ax1.set_ylabel('y label')

    ax2.plot(x, y2)
    ax2.set_title('Cubic')
    ax2.set_xlabel('x axis')
    ax2.set_ylabel('y axis')

    return fig

x = [i for i in range(101)]
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

print(mplot(x, y1, y2))
time.sleep(5)


import logging
import os
import argparse
import sys
from string import Template
from c3.pythonscript import Pythonscript
from c3.utils import convert_notebook
from c3.create_operator import create_operator
from c3.templates import grid_wrapper_template, cos_grid_wrapper_template, gw_component_setup_code, dockerfile_template

def wrap_component(component_path,
                   component_description,
                   component_dependencies,
                   component_interface,
                   component_inputs,
                   component_process,
                   cos,
                   ):
    # get component name from path
    component_name = os.path.splitext(os.path.basename(component_path))[0]

    gw_template = cos_grid_wrapper_template if cos else grid_wrapper_template
    grid_wrapper_code = gw_template.substitute(
        component_name=component_name,
        component_description=component_description,
        component_dependencies=component_dependencies,
        component_inputs=component_inputs,
        component_interface=component_interface,
        component_process=component_process,
    )

    # Write edited code to file
    grid_wrapper_file = f'cgw_{component_name}.py' if cos else f'gw_{component_name}.py'
    grid_wrapper_file_path = os.path.join(os.path.dirname(component_path), grid_wrapper_file)
    # remove 'component_' from gw path
    grid_wrapper_file_path = grid_wrapper_file_path.replace('component_', '')
    with open(grid_wrapper_file_path, 'w') as f:
        f.write(grid_wrapper_code)

    logging.info(f'Saved wrapped component to {grid_wrapper_file_path}')

    return grid_wrapper_file_path

def get_component_elements(file_path):
    # get required elements from component code
    py = Pythonscript(file_path)
    # convert description into a string with a single line
    description = (py.get_description().replace('\n', ' ').replace('"', '\''))
    inputs = py.get_inputs()
    outputs = py.get_outputs()
    dependencies = py.get_requirements()

    # combine inputs and outputs
    interface_values = {}
    interface_values.update(inputs)
    interface_values.update(outputs)

    # combine dependencies list
    dependencies = '\n# '.join(dependencies)

    # generate interface code from inputs and outputs
    interface = ''
    type_to_func = {'String': '', 'Boolean': 'bool', 'Integer': 'int', 'Float': 'float'}
    for variable, d in interface_values.items():
        interface += f"# {d['description']}\n"
        interface += f"component_{variable} = {type_to_func[d['type']]}(os.getenv('{variable}', {d['default']}))\n"

    # generate kwargs for the subprocesses
    process_inputs = ', '.join([f'{i}=component_{i}' for i in inputs.keys()])
    # use log level from grid wrapper
    process_inputs = process_inputs.replace('component_log_level', 'log_level')

    return description, interface, process_inputs, dependencies

# Adding code
def edit_component_code(file_path):
    file_name = os.path.basename(file_path)
    if file_path.endswith('.ipynb'):
        logging.info('Convert notebook to python script')
        target_file = convert_notebook(file_path)
        file_path = target_file
        file_name = os.path.basename(file_path)
    else:
        # write edited code to different file
        target_file = os.path.join(os.path.dirname(file_path), 'component_' + file_name)

    target_file_name = os.path.basename(target_file)

    with open(file_path, 'r') as f:
        script = f.read()
    # Add code for logging and cli parameters to the beginning of the script
    script = gw_component_setup_code + script
    # replace old filename with new file name
    script = script.replace(file_name, target_file_name)
    with open(target_file, 'w') as f:
        f.write(script)

    if '__main__' not in script:
        logging.warning('No __main__ found in component code. Grid wrapper will import functions from component, '
                        'which can lead to unexpected behaviour without using __main__.')

    logging.info('Saved component python script in ' + target_file)

    return target_file

def apply_grid_wrapper(file_path, component_process, cos, *args, **kwargs):
    assert file_path.endswith('.py') or file_path.endswith('.ipynb'), \
        "Please provide a component file path to a python script or notebook."

    file_path = edit_component_code(file_path)

    description, interface, inputs, dependencies = get_component_elements(file_path)

    component_elements = dict(
        component_path=file_path,
        component_description=description,
        component_dependencies=dependencies,
        component_interface=interface,
        component_inputs=inputs,
        component_process=component_process
    )

    logging.debug('Wrap component with parameters:')
    for component, value in component_elements.items():
        logging.debug(component + ':\n' + str(value) + '\n')

    logging.info('Wrap component')
    grid_wrapper_file_path = wrap_component(cos=cos, **component_elements)
    return grid_wrapper_file_path, file_path

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('file_path', type=str,
                        help='Path to python script or notebook')
    parser.add_argument('additional_files', type=str, nargs='*',
                        help='List of paths to additional files to include in the container image')
    parser.add_argument('-p', '--component_process', type=str, required=True,
                        help='Name of the component sub process that is executed for each batch.')
    parser.add_argument('--cos', action=argparse.BooleanOptionalAction, default=False,
                        help='Creates a grid wrapper for processing COS files')
    parser.add_argument('-r', '--repository', type=str, default=None,
                        help='Container registry address, e.g. docker.io/<username>')
    parser.add_argument('-v', '--version', type=str, default=None,
                        help='Container image version. Auto-increases the version number if not provided (default 0.1)')
    parser.add_argument('-l', '--log_level', type=str, default='INFO')
    parser.add_argument('--dockerfile_template_path', type=str, default='',
                        help='Path to custom dockerfile template')
    parser.add_argument('--test_mode', action='store_true')
    args = parser.parse_args()

    # Init logging
    root = logging.getLogger()
    root.setLevel(args.log_level)
    handler = logging.StreamHandler(sys.stdout)
    formatter = logging.Formatter('%(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    handler.setLevel(args.log_level)
    root.addHandler(handler)

    grid_wrapper_file_path, component_path = apply_grid_wrapper(**vars(args))

    if args.repository is not None:
        logging.info('Generate CLAIMED operator for grid wrapper')

        # Add component path and init file path to additional_files
        args.additional_files.append(component_path)

        # Update dockerfile template if specified
        if args.dockerfile_template_path != '':
            logging.info(f'Uses custom dockerfile template from {args.dockerfile_template_path}')
            with open(args.dockerfile_template_path, 'r') as f:
                _dockerfile_template = Template(f.read())
        else:
            _dockerfile_template = dockerfile_template

        create_operator(
            file_path=grid_wrapper_file_path,
            repository=args.repository,
            version=args.version,
            dockerfile_template=_dockerfile_template,
            additional_files=args.additional_files,
            log_level=args.log_level,
            test_mode=args.test_mode,
        )

        logging.info('Remove local component file')
        os.remove(component_path)

if __name__ == '__main__':
    main()


"""
Author: Alpha Team Group Project
Date: March 2023
Purpose: Deep learning embedding module contains functions for deep learning embedding
"""

# ----------------------------------------- Imports ----------------------------------------- #

import random

import numpy as np
# External imports
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv, GATConv, SAGEConv
from torch_geometric.utils import from_networkx

# ----------------------------------------- CONSTANT ----------------------------------------- #

# ------------------------------------------- MODEL ------------------------------------------ #

class GCN(torch.nn.Module):
    """
    Graph Convolutional Network
    """
    def __init__(self, num_features, hidden_dim, embed_dim):
        super(GCN, self).__init__()
        self.encoder = GCNConv(num_features, hidden_dim)
        self.decoder = torch.nn.Linear(hidden_dim, embed_dim)

    def forward(self, data):
        x, edge_index = data.x, data.edge_index
        z = self.encoder(x, edge_index)
        x = self.decoder(z)
        return x

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

class GAT(torch.nn.Module):
    """
    Graph Attention Network
    """
    def __init__(self, num_features, hidden_dim, embed_dim):
        super(GAT, self).__init__()
        self.encoder = GATConv(num_features, hidden_dim)
        self.decoder = torch.nn.Linear(hidden_dim, embed_dim)

    def forward(self, data):
        x, edge_index = data.x, data.edge_index
        z = self.encoder(x, edge_index)
        x = self.decoder(z)
        return x

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

class SAGE(torch.nn.Module):
    """
    GraphSAGE
    """
    def __init__(self, num_features, hidden_dim, embed_dim):
        super(SAGE, self).__init__()
        self.encoder = SAGEConv(num_features, hidden_dim)
        self.decoder = torch.nn.Linear(hidden_dim, embed_dim)

    def forward(self, data):
        x, edge_index = data.x, data.edge_index
        z = self.encoder(x, edge_index)
        x = self.decoder(z)
        return x

# ------------------------------------------- LOSS ------------------------------------------- #

def unsupervised_loss(recon_x, x):
    """
    :Function: Unsupervised loss function for deep learning embedding
    :param recon_x: reconstructed x
    :type recon_x: torch.Tensor
    :param x: original x
    :type x: torch.Tensor
    :return: loss
    :rtype: torch.Tensor
    """
    mse_loss = F.mse_loss(recon_x, x)
    return mse_loss

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

def contrastive_loss(embeddings, positive_pairs, negative_pairs, margin=1.0):
    """
    :Function: Contrastive loss function for unsupervised learning
    :param embeddings: embeddings
    :type embeddings: torch.Tensor
    :param positive_pairs: positive pairs
    :type positive_pairs: torch.Tensor
    :param negative_pairs: negative pairs
    :type negative_pairs: torch.Tensor
    :param margin: margin
    :type margin: float
    :return: loss
    :rtype: torch.Tensor
    """
    positive_distances = torch.norm(embeddings[positive_pairs[:, 0]] - embeddings[positive_pairs[:, 1]], dim=1)
    negative_distances = torch.norm(embeddings[negative_pairs[:, 0]] - embeddings[negative_pairs[:, 1]], dim=1)

    positive_loss = torch.mean(torch.square(positive_distances))
    negative_loss = torch.mean(torch.square(torch.clamp(margin - negative_distances, min=0.0)))

    loss = 0.5 * (positive_loss + negative_loss)
    return loss

# ----------------------------------------- TRAINING ----------------------------------------- #

def train(model, optimizer, data, device, proximity=False):
    """
    :Function: Train model for deep learning embedding
    :param model: Deep learning model
    :type model: torch.nn.Module
    :param optimizer: Optimizer
    :type optimizer: torch.optim.Optimizer
    :param data: Data
    :type data: torch_geometric.data.Data
    :param device: Device
    :type device: torch.device
    :param proximity: Proximity
    :type proximity: bool
    :return: loss
    :rtype: float
    """
    model.train()
    optimizer.zero_grad()
    out = model(data.to(device))
    if proximity:
        loss = contrastive_loss(out, data.positive_pairs, data.negative_pairs)
    else:
        loss = unsupervised_loss(out[data.train_mask], data.x[data.train_mask])
    loss.backward()
    optimizer.step()
    return loss.item()

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

def test(model, data, device, proximity=False):
    """
    :Function: Test model for deep learning embedding
    :param model: Deep learning model
    :type model: torch.nn.Module
    :param data: Data
    :type data: torch_geometric.data.Data
    :param device: Device
    :type device: torch.device
    :param proximity: Proximity
    :type proximity: bool
    :return: loss
    :rtype: float
    """
    model.eval()
    out = model(data.to(device))
    if proximity:
        loss = contrastive_loss(out, data.positive_pairs, data.negative_pairs)
    else:
        loss = unsupervised_loss(out[data.test_mask], data.x[data.test_mask])
    return loss.item()

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

def train_model(model, optimizer, data, device, epochs, proximity=False):
    """
    :Function: Train model for deep learning embedding
    :param model: Deep learning model
    :type model: torch.nn.Module
    :param optimizer: Optimizer
    :type optimizer: torch.optim.Optimizer
    :param data: Data
    :type data: torch_geometric.data.Data
    :param device: Device
    :type device: torch.device
    :param epochs: Epochs
    :type epochs: int
    :param proximity: Proximity
    :type proximity: bool
    :return: loss
    :rtype: float
    """
    best_loss = float('inf')
    best_weights = None
    for epoch in range(1, epochs + 1):
        loss = train(model, optimizer, data, device, proximity=proximity)
        test_loss = test(model, data, device, proximity=proximity)
        if test_loss < best_loss:
            best_loss = test_loss
            best_weights = model.state_dict()
        if test_loss < 0.01:  # early stopping
            break
        print('Epoch: {:03d}, Loss: {:.5f}, Test Loss: {:.5f}'.format(epoch, loss, test_loss))
    model.load_state_dict(best_weights)
    return model

# ---------------------------------------- PREPROCESS ---------------------------------------- #

def generate_pairs(networkx_graph, num_negative_pairs=None):
    """
    :Function: Generate positive and negative pairs
    :param networkx_graph: networkx graph
    :type networkx_graph: networkx.Graph
    :param num_negative_pairs: number of negative pairs
    :type num_negative_pairs: int
    :return: positive pairs, negative pairs
    :rtype: np.array, np.array
    """
    nodes = list(networkx_graph.nodes())

    positive_pairs = np.array([[u, v] for u, v in networkx_graph.edges])

    if num_negative_pairs is None:
        num_negative_pairs = len(positive_pairs)

    negative_pairs = []
    while len(negative_pairs) < num_negative_pairs:
        u, v = random.sample(nodes, 2)
        if not networkx_graph.has_edge(u, v):
            negative_pairs.append([u, v])
    negative_pairs = np.array(negative_pairs)

    return positive_pairs, negative_pairs

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

def pairs_to_indices(data, positive_pairs, negative_pairs):
    """
    :Function: Convert pairs to indices
    :param data: Data
    :type data: torch_geometric.data.Data
    :param positive_pairs: positive pairs
    :type positive_pairs: np.array
    :param negative_pairs: negative pairs
    :type negative_pairs: np.array
    :return: positive indices, negative indices
    :rtype: np.array, np.array
    """
    node_to_index = {node: i for i, node in enumerate(data.mapping)}

    positive_indices = np.array([[node_to_index.get(u), node_to_index.get(v)] for u, v in positive_pairs if
                                 u in node_to_index and v in node_to_index])
    negative_indices = np.array([[node_to_index.get(u), node_to_index.get(v)] for u, v in negative_pairs if
                                 u in node_to_index and v in node_to_index])

    return positive_indices, negative_indices

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

def preprocess_data(networkx_graph, node_features):
    """
    :Function: Preprocess data for deep learning embedding
    :param networkx_graph: Networkx graph
    :type networkx_graph: networkx.Graph
    :param node_features: Node features
    :type node_features: np.array
    :return: data
    :rtype: torch_geometric.data.Data
    """
    # Convert to torch_geometric.data.Data
    data = from_networkx(networkx_graph)

    # Add node features
    data.x = torch.tensor(node_features, dtype=torch.float)

    # Add train and test mask
    data.train_mask = torch.zeros(data.num_nodes, dtype=torch.uint8)
    data.train_mask[:data.num_nodes // 2] = 1
    data.test_mask = torch.zeros(data.num_nodes, dtype=torch.uint8)
    data.test_mask[data.num_nodes // 2:] = 1
    data.mapping = {node: i for i, node in enumerate(networkx_graph.nodes)}

    return data

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

def get_embeddings(model, data):
    """
    :Function: Get final embeddings
    :param model: Deep learning model
    :type model: torch.nn.Module
    :param data: Data
    :type data: torch_geometric.data.Data
    :return: embeddings
    :rtype: np.array
    """
    model.eval()
    with torch.no_grad():
        x, edge_index = data.x, data.edge_index
        embeddings = model.encoder(x, edge_index)
    return embeddings.detach().cpu().numpy()


import scrapy
import json

class JustwatchSPider(scrapy.Spider):
    name = 'jwspider'
    start_urls = ['https://www.justwatch.com/uk/movies']

    def parse(self, response):
        for link in response.css('div.title-list-grid__item a::attr(href)'):
            yield response.follow(link.get(), callback=self.parse_movie)

    def parse_movie(self, response):
        for film in response.css('div.title-info.visible-xs.visible-sm'):
            genres = film.css('div.detail-infos__value span::text').getall()
            popravljeni = []
            for genre in genres:
                if genre != ', ':
                    popravljeni.append(genre.strip())
            try:
                yield {
                    'name': response.css('div.title-block__container div.title-block h1::text').get().strip(),
                    'leto': response.css('div.title-block__container div.title-block span::text').get().replace('(','').replace(')','').strip(),
                    'rating': film.css('div.jw-scoring-listing__rating a.jw-scoring-listing__rating--no-link::text').get().strip(),
                    'genres': popravljeni,
                    'cas_trajanja': film.css('div.detail-infos div.detail-infos__value::text').getall()[0],
                    'age_rating': film.css('div.detail-infos div.detail-infos__value::text').getall()[1],
                    'director': film.css('div.detail-infos__value a.title-credit-name::text').get().strip()
                }
            except:
                yield {
                    'name': response.css('div.title-block__container div.title-block h1::text').get().strip(),
                    'leto': response.css('div.title-block__container div.title-block span::text').get().replace('(','').replace(')', '').strip(),
                    'rating': film.css('div.jw-scoring-listing__rating a.jw-scoring-listing__rating--no-link::text').get().strip(),
                    'genres': popravljeni,
                    'director': 'NIMA GLAVNEGA DIREKTORJA'
                }


from classes import conjuntoDominanteMinimo as CDM
from classes import grafo

class BBconjuntoDominanteMinimo:
	def __init__(self):
		self.Q = []

	#Retorna a solução ótima utilizando Bracnh and Bound dos diagramas de decisão
	def encontrarNoOtimoBB(self, largura_diagrama, instancia):
		cdm = CDM.DDconjuntoDominanteMinimo() #Cria um objeto do diagrama de decisão
		cdm.gf = grafo.Grafo(instancia) #Carrega a intancia e cria um grafo dele
		self.Q = [cdm.criarRaiz()] #Adiciona a raiz inicial ao vetor de raizes
		solução = cdm.criarRaiz() #Cria um no pra solução inicial
		solução.custo = 10000000000000000000000 #O custo da solução inicial recebe um valor grande
		while len(self.Q) > 0: #Percorre até que o vetor esteja vazio
			restrito = cdm.encontrarNoRestritoCutSet(largura_diagrama, self.Q[0], self.Q[0].camada)
			if(solução.custo > restrito[0].custo): #Testa se o diagrama restrito obteve uma solução melhor, pra subtituir a atual
				solução = restrito[0]
			restrito.pop(0) #Apaga solução do diagrama restrito, e mantém apenas o CutSet
			if(len(restrito) > 0): #Se o Cuset não é vazio calcula o diagrama relaxado
				relaxado = cdm.encontrarNoRelaxado(largura_diagrama, self.Q[0], self.Q[0].camada)
				if(solução.custo > relaxado[0].custo): #Testa se o diagrama relaxado é pior que a solução atual para poda
					self.Q.extend(restrito)
			self.Q.pop(0)
		return solução


from wrappers import roundAll, timer
import random
import math

class Population:
	def __init__(self, target, fit, ind, dnaLen, popSize, geneSet = None):
		self.target = target
		self.fit = fit
		self.ind = ind
		self.dnaLen = dnaLen
		self.popSize = popSize
		self.geneSet = geneSet

		self.mutationChance = .05

		self.genPopulation()

	def genPopulation(self):
		population = []
		for i in range(self.popSize):
			population.append(self.ind(self.dnaLen, self.geneSet))
		self.population = population

	def weighted_population(self):
		self.weightedPop = []
		targetFit = self.fit(self.target, self.target)
		for individual in self.population:
			try:
				weight = 1/abs((targetFit - self.fit(individual, self.target)) / targetFit)
			except ZeroDivisionError:
				weight = 100
			self.weightedPop.append([individual, weight])
		return self.weightedPop

	def weighted_choice(self, items):
		weight_total = sum((item[1] for item in items))
		n = random.uniform(0, weight_total)
		for item, weight in items:
			if n < weight:
				return item
			n = n - weight
		return item

	def createChildren(self, dna1, dna2):
		pos = int(random.random()*self.dnaLen)
		return (dna1[:pos]+dna2[pos:], dna2[:pos]+dna1[pos:])

	def randomFromGeneSet(self):
		pos = int(random.random()*len(self.geneSet))
		return self.geneSet[pos]

	def mutate(self, individual):
		for i in range(len(individual)):
			if(random.random() < self.mutationChance):
				individual[i] = self.randomFromGeneSet()
		return individual

	def avg_ind(self):
		sortedPop = self.getInd()
		halfway = math.ceil(self.popSize / 2)
		return sortedPop[0][halfway][0]

	def evolve(self, iterations):
		for i in range(iterations):
			self.wPop = self.weighted_population()
			self.population = []
			for x in range(int(self.popSize/2)):
				ind1 = self.weighted_choice(self.wPop)
				ind2 = self.weighted_choice(self.wPop)

				ind1, ind2 = self.createChildren(ind1,ind2)

				self.population.append(self.mutate(ind1))
				self.population.append(self.mutate(ind2))
		avg = self.avg_ind()
		return avg, self.fit(avg, self.target)

	def bestInd(self):
		ind = self.getInd()[0][0][0]
		return ind, self.fit(ind, self.target)

	def getInd(self):
		return sorted([self.wPop], key=lambda x: x[1])

	def __str__(self):
		best = self.bestInd()
		return ''.join([val for val in best[0]]) + ': '+str(best[1])


# This Cog contains code from Arbitlog, which was made by Roadcrosser.
# https://github.com/Roadcrosser/archiver
import discord
import json
import os
import httplib2
import re
import datetime
import asyncio
import zipfile
import textwrap

from io import BytesIO

from discord.ext import commands
from discord.ext.commands import Cog
from helpers.checks import check_if_staff

class Arbitlog(Cog):
    def __init__(self, bot):
        self.bot = bot

    def textify_embed(self, embed, limit=40, padding=0, pad_first_line=True):
        text_proc = []
        title = ""
        if embed.title:
            title += embed.title
            if embed.url:
                title += " - "
        if embed.url:
            title += embed.url
        if not title and embed.author:
            title = embed.author.name
        if title:
            text_proc += [title, ""]
        if embed.description:
            text_proc += [embed.description, ""]
        if embed.thumbnail:
            text_proc += ["Thumbnail: " + embed.thumbnail.url, ""]
        for f in embed.fields:
            text_proc += [
                f.name
                + (
                    ":"
                    if not f.name.endswith(
                        ("!", ")", "}", "-", ":", ".", "?", "%", "$")
                    )
                    else ""
                ),
                *f.value.split("\n"),
                "",
            ]
        if embed.image:
            text_proc += ["Image: " + embed.image.url, ""]
        if embed.footer:
            text_proc += [embed.footer.text, ""]

        text_proc = [textwrap.wrap(t, width=limit) for t in text_proc]

        texts = []

        for tt in text_proc:
            if not tt:
                tt = [""]
            for t in tt:
                texts += [t + " " * (limit - len(t))]

        ret = " " * (padding * pad_first_line) + "╓─" + "─" * limit + "─╮"

        for t in texts[:-1]:
            ret += "\n" + " " * padding + "║ " + t + " │"

        ret += "\n" + " " * padding + "╙─" + "─" * limit + "─╯"

        return ret

    async def log_whole_channel(self, channel, zip_files=False):
        st = ""

        if zip_files:
            b = BytesIO()
            z = zipfile.ZipFile(b, "w", zipfile.ZIP_DEFLATED)
            zipped_count = 0

        async for m in channel.history(limit=None):
            blank_content = True
            ts = "{:%Y-%m-%d %H:%M} ".format(m.created_at)
            padding = len(ts) + len(m.author.name) + 2
            add = ts
            if m.type == discord.MessageType.default:
                add += "{0.author.name}: {0.clean_content}".format(m)
                if m.clean_content:
                    blank_content = False
            else:
                add += m.system_content

            for a in m.attachments:
                if not blank_content:
                    add += "\n"
                add += (
                    " " * (padding * (not blank_content)) + "Attachment: " + a.filename
                )
                if zip_files:
                    fn = "{}-{}-{}".format(m.id, a.id, a.filename)
                    async with self.bot.session.get(a.url) as r:
                        f = await r.read()

                    z.writestr(fn, f)
                    add += " (Saved as {})".format(fn)
                    zipped_count += 1

                blank_content = False

            for e in m.embeds:
                if e.type == "rich":
                    if not blank_content:
                        add += "\n"
                    add += self.textify_embed(
                        e, limit=40, padding=padding, pad_first_line=not blank_content
                    )
                    blank_content = False

            if m.reactions:
                if not blank_content:
                    add += "\n"
                add += " " * (padding * (not blank_content))
                add += " ".join(
                    ["[{} {}]".format(str(r.emoji), r.count) for r in m.reactions]
                )
                blank_content = False

            add += "\n"
            st = add + st

        ret = st
        if zip_files:
            if zipped_count:
                z.writestr("log.txt", st)
                b.seek(0)
                ret = (ret, b)
            else:
                ret = (ret, None)

        return ret

    @commands.guild_only()
    @commands.check(check_if_staff)
    @commands.command()
    async def arbitlog(self, ctx, *, args=""):
        channels = args.split()
        to_log = []
        for ch in channels:
            ch = ctx.guild.get_channel_or_thread(int(ch.strip("<#>")))
            if ch:
                if type(ch) == discord.CategoryChannel:
                    to_log += ch.channels
                else:
                    to_log += [ch]
        channels = to_log
        await ctx.channel.send(f"Logging: {' '.join([c.mention for c in channels])}")
        async with ctx.channel.typing():
            for ch in channels:
                out = await self.log_whole_channel(ch, zip_files=True)
                zipped_files = out[1]
                out = out[0]

                f = BytesIO()
                f.write(out.encode("utf-8"))
                f.seek(0)

                fn = "#{}-{}-{}".format(
                    ch.name, ch.id, int(ctx.message.created_at.timestamp())
                )

                reply = "Saved to disk as `{}.txt`.\n".format(fn)

                if zipped_files:
                    if not os.path.isdir("arbitlogs"):
                        os.mkdir("arbitlogs")

                    with open("arbitlogs/" + fn + ".zip", "wb+") as o:
                        o.write(zipped_files.getvalue())
                    reply += (
                        "Also saved files to disk as `{}.zip` ({:,.2f} MB).".format(
                            fn, int(len(zipped_files.getvalue()) / (1024 * 1024))
                        )
                    )

                with open("arbitlogs/" + fn + ".txt", "wb+") as o:
                    o.write(f.read())

                await ctx.channel.send(reply)

            return True

async def setup(bot):
    await bot.add_cog(Arbitlog(bot))


"""
This is the main script to train the SDCRegression model implemented in `model.py`

Usage:

1. To build a model from scratch, run
```cmd
python -m sdc.train_regression --train --nb_epoch 6
```
where `sdc.train_regression` build a vgg16-based regression model from CenterImage to Steer. `--nb_epoch` is the number of training epoches.

2. To continuously train a model from a previous one, run
```cmd
python -m sdc.train_regression --train --restore --nb_epoch 6
```

3. To load a trained model to evaluate on test data without any training, simply run
```cmd
python -m sdc.train_regression --restore
```
"""

import numpy as np

import argparse
import tensorflow as tf
import keras.backend as K
import pandas as pd

from . import model
from . import config
from . import process
from . import data

from .config import xycols, processors

# parse command line parameters
parser = argparse.ArgumentParser()
parser.add_argument("--train", dest="train", action="store_true")
parser.add_argument("--restore", dest="restore", action="store_true")
parser.add_argument("--nb_epoch", dest="nb_epoch", type=int)
args = parser.parse_args()

nb_epoch = args.nb_epoch

# load training data
dataset = data.DataSet(config.train_data).preprocess()
# split to train, validation and test
trainset, others = dataset.split(test_size=20000)
valset, testset = others.split(test_size=10000)

train_size = trainset.size()
val_size = valset.size()
test_size = testset.size()

print("training size %d, validation size %d, test size %d" % (train_size, val_size, test_size))

# make batch_generator out of the corresonponding datasets
train_generator = trainset.make_batch_generator(batch_size=config.batch_size,
						col_grps = xycols,
						process_fns = processors)
val_generator = valset.make_batch_generator(batch_size=config.batch_size,
						col_grps = xycols,
						process_fns = processors)
test_generator = testset.make_batch_generator(batch_size=config.batch_size,
						col_grps = xycols,
						process_fns = processors)

# training the model
with tf.Session() as sess:
	# create the model
	K.set_session(sess)
	img_steer_model = model.SteerRegressionModel(input_shape=config.image_size, model=config.model_name)
	# model information
	print("inspect model:")
	img_steer_model.inspect()
	# restore from previous training if instructed to do so
	if args.restore:
		print("restore model from", config.model_prefix)
		img_steer_model.restore(config.model_prefix)

	if args.train:
		img_steer_model.fit_generator(nb_epoch, train_generator,
			train_size//config.batch_size*config.batch_size,
			val_generator, val_size)
		# save the model
		print("save model to", config.model_prefix)
		img_steer_model.save(config.model_prefix)
	# evaluation on test data for future inspection
	print("evaluate on test data")
	testset.reset()
	test_y = testset.log.SteeringAngle
	test_yhat = img_steer_model.predict_generator(test_generator, test_size)
	mse = np.mean((test_yhat - test_y) * (test_yhat - test_y))
	print("test mse:", mse)
	# save the test result
	print("save test result for inspection")
	test_result = pd.DataFrame({"steer": test_y,
								"prediction": test_yhat,
								"image": testset.log.CenterImage})
	test_result.to_csv("tmp/test_result.csv", header=True, index=False)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('Users', '0003_auto_20160627_1522'),
    ]

    operations = [
        migrations.AlterField(
            model_name='users',
            name='userType',
            field=models.CharField(default=b'U', max_length=2),
        ),
    ]


from create_html_page import ALL_CENTERS, CITIES, get_source_html
from parse_data import get_page_in_html, making_list_of_items, parse_to_json

def main():
    url = (
        'https://online.metro-cc.ru/category/'
        'chaj-kofe-kakao/chay/cherniy-chay'
    )

    for city in CITIES:
        for center in ALL_CENTERS[city]:
            get_source_html(
                url,
                center,
                city,
            )

            soup = get_page_in_html(
                f'../html_pages/{city}/index_{center}.html'
            )
            parse_to_json(making_list_of_items(soup), city, center)

if __name__ == '__main__':
    main()


from .models import Pupil
from .serializers import PupilSerializer, RuSerializer, UzSerializer, EngSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status, permissions
from django.utils.translation import ugettext_lazy as _

class PupilList(APIView):

    """
    List all pupils, or create a new pupil.
    """
    def get(self, request, format=None):
        try:
            pupils = Pupil.objects.all()
            serializer = PupilSerializer(pupils, many=True)
            return Response(serializer.data)
        except:
            return Response(
                {"detail": _("Pupil not found.")},
                status=status.HTTP_404_NOT_FOUND
            )

    def post(self, request, format=None):
        serializer = PupilSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class PupilDetail(APIView):
    """
    Retrieve, update or delete a pupil instance.
    """
    def get_object(self, pk):
        try:
            return Pupil.objects.get(pk=pk)
        except Pupil.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        try:
            pupil = self.get_object(pk)
            serializer = PupilSerializer(pupil)
            return Response(serializer.data)
        except:
            return Response(
                {"detail": _("pupil not found.")},
                status=status.HTTP_404_NOT_FOUND
            )

    def put(self, request, pk, format=None):
        try:
            pupil = self.get_object(pk)
            serializer = PupilSerializer(pupil, data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        except:
            return Response(
                {"detail": _("Bad Request.")},
                status=status.HTTP_400_BAD_REQUEST
            )

    def delete(self, request, pk, format=None):
        try:
            pupil = self.get_object(pk)
            pupil.delete()
            return Response({"Data was deleted."},status=status.HTTP_204_NO_CONTENT)
        except:
            return Response(
                {"detail": _("Bad Request.")},
                status=status.HTTP_400_BAD_REQUEST
            )

class PupilLanguage(APIView):

    permission_classes = (permissions.AllowAny,)

    def get(self, request, language):
        try:
            pupils = Pupil.objects.all()
            if language=="uz":
                serializer = UzSerializer(pupils, many=True)
            elif language=="eng":
                serializer = EngSerializer(pupils, many=True)
            else:
                serializer = RuSerializer(pupils, many=True)

            return Response(serializer.data)
        except:
            return Response(
                {"detail": _("Pupil not found.")},
                status=status.HTTP_404_NOT_FOUND
            )


import torch
import numpy as np
from scipy.io.wavfile import write

# Load the Tacotron 2 model
tacotron2 = torch.hub.load('nvidia/DeepLearningExamples:main',
                           'nvidia_tacotron2')
tacotron2 = tacotron2.to('cpu')
tacotron2.eval()

# Load the WaveGlow model
waveglow = torch.hub.load('nvidia/DeepLearningExamples:main',
                          'nvidia_waveglow')
waveglow = waveglow.to('cpu')
waveglow.eval()

# Set the text to be synthesized
text = "Hello world, I missed you so much."

# Load the TTS utilities
utils = torch.hub.load('nvidia/DeepLearningExamples:main', 'nvidia_tts_utils')

# Prepare the input sequence
sequences, lengths = utils.prepare_input_sequence([text])

# Generate the mel spectrogram with Tacotron 2
with torch.no_grad():
    mel, _, _ = tacotron2.infer(sequences, lengths)

# Synthesize audio with WaveGlow
with torch.no_grad():
    audio = waveglow.infer(mel)

# Convert audio to NumPy array and write to file
audio_numpy = audio[0].data.cpu().numpy()
rate = 22050
write("audio.wav", rate, audio_numpy)


#!/usr/bin/env python3
"""
Script covering the entire histogram workflow process.
"""
import os
import datetime
import library as lib

data_dir_0  = 'data/histograms/0-10000km_dx30min_dy500km'

run_name    = 'test_filter'
data_dir    = os.path.join('data/histograms',run_name)
plot_dir    = os.path.join('output/galleries/histograms',run_name)
params      = ['spot_density']
xkeys       = ['ut_hrs','slt_mid']
sTime       = datetime.datetime(2017,9,1)
eTime       = datetime.datetime(2017,9,3)

geo_env     = lib.GeospaceEnv()

# Create histogram NetCDF Files ################################################
rd  = {}
rd['input_dir']             = data_dir_0
rd['output_dir']            = data_dir
rd['geospace_env']          = geo_env
rd['symh_min']              = -25
rd['symh_max']              =  25
rd['kp_min']                = None
rd['kp_max']                = 3
lib.select_histograms.main(rd)


from functools import reduce
from operator import mul
lista_numero = []
while True:
    quantidade = int(input("Informe um número (número zero para sair): "))
    if quantidade != 0:
        lista_numero.append(quantidade)
        #Soma da lista
        soma = sum(lista_numero)
        #multiplicação da lista
        mult = reduce(mul, lista_numero,1)
        #Maior valor de uma lista
        max_value = max(lista_numero)
        #Menor valor de uma lista
        min_value = min(lista_numero)
    elif quantidade == 0:
        break
for posicao, numero in enumerate(lista_numero, start=1):
    print(f"#{posicao} - {numero}")
print(f'Soma total:{soma} \n Multiplicação Total:{mult} \n Valor Maximo:{max_value} \n Valor Minimo:{min_value}')

Exercicio Enumerate and List/exercicio01.py


import bpy
import sys
#import mathutils
from mathutils import *
import bgl
import blf
import gpu
from gpu_extras.batch import batch_for_shader
from ..math.vecmath import *

from enum import Enum

from .graphics import DrawContext2D

#----------------------------------
class Size2D:
    def __init__(width = 0, height = 0):
        self.width = width
        self.height = height

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

class Rectangle2D:
    def __init__(self, x = 0, y = 0, width = 0, height = 0):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def contains(self, x, y):
        return x >= self.x and x < self.x + self.width and y >= self.y and y < self.y + self.height

    def __str__(self):
        return str(self.x) + ", " + str(self.y) + ", " + str(self.width) + ", " + str(self.height)

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

class Layout:

    def __init__(self):
        self.parent = None
        self.window = None

    def get_parent(self):
        return self.parent

    def set_parent(self, parent):
        self.parent = parent

    def get_window(self):
        return self.window

    def set_window(self, window):
        self.window = window

    def layout_components(self, bounds):
        pass

    def draw(self, ctx):
        pass

    def handle_event(self, context, event):
        return False

    def get_screen_position(self):
        if self.parent != None:
            return self.parent.get_screen_position()
        elif self.window != None:
            return self.window.get_screen_position()
        return Vector((0, 0))

    def mouse_pressed(self, event):
        return False

    def mouse_released(self, event):
        return False

    def mouse_moved(self, event):
        return False

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

class LayoutAxis(Enum):
    X = 1
    Y = 2
#----------------------------------

class ExpansionType(Enum):
    EXPAND = 1
    PREFERRED = 2
#    MIN = 3
#    MAX = 4

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

class LayoutBox(Layout):
    class Info:
        def __init__(self):
            self.span = 0

    def __init__(self, axis = Axis.X):
        super().__init__()

        self.axis = axis
        self.children = []

    def dump(self, indent = ""):
        print(indent + "LayoutBox " + str(self.axis))

        for child in self.children:
            child.dump(indent + " ")

    def add_child(self, child):
        self.children.append(child)
        child.set_parent_layout(self)

    def handle_event(self, context, event):
        for child in self.children:
            result = child.handle_event_dispatch(context, event)
            if result:
                return True

        return False

    def calc_minimum_size(self):
        span_x = 0
        span_y = 0

        for child in self.children:
            size = child.calc_minimum_size()

            if self.axis == Axis.X:
                span_x += size.x
                span_y = max(span_y, size.y)
            else:
                span_x = max(span_x, size.x)
                span_y += size.y

        return Vector((span_x, span_y))

    def calc_preferred_size(self):
        span_x = 0
        span_y = 0

        for child in self.children:
            size = child.calc_preferred_size()

            if self.axis == Axis.X:
                span_x += size.x
                span_y = max(span_y, size.y)
            else:
                span_x = max(span_x, size.x)
                span_y += size.y

        return Vector((span_x, span_y))

    def calc_maximum_size(self):
        span_x = 0
        span_y = 0

        for child in self.children:
            size = child.calc_maximum_size()

            if self.axis == Axis.X:
                span_x += size.x
                span_y = max(span_y, size.y)
            else:
                span_x = max(span_x, size.x)
                span_y += size.y

        return Vector((span_x, span_y))

    def layout_components(self, bounds):
        local_span = bounds.width if self.axis == Axis.X else bounds.height

        infoList = []
        total_span = 0

        #Allocate min sizes first
#        print("calc min")
        for i in range(len(self.children)):
            child = self.children[i]
            size = child.calc_minimum_size()

            info = self.Info()
            infoList.append(info)
            info.span = size.x if self.axis == Axis.X else size.y
            total_span += info.span

#            print("child %d: span %d" % (i, info.span))

        #Expand to preferred sizes if there is room
#        print("calc pref")
        for i in range(len(self.children)):
            if total_span < local_span:
                child = self.children[i]
                size = child.calc_preferred_size()
                pref_span = size.x if self.axis == Axis.X else size.y
                span_remaining = local_span - total_span

#                print("span_remaining " + str(span_remaining))
#                print("pref_span " + str(pref_span))

                info = infoList[i]
#                print("info.span " + str(info.span))
                span_to_add = min(span_remaining, pref_span - info.span)
                info.span += span_to_add
                total_span += span_to_add

#                print("child %d: span %d" % (i, info.span))

        #If there is space left, expand children with expand set
        # print("calc max")
        # print("local_span " + str(local_span))
        # print("total_span " + str(total_span))
        for i in range(len(self.children)):
            if total_span < local_span:
                child = self.children[i]
                info = infoList[i]
#                print("info.span " + str(info.span))

                if (self.axis == Axis.X and child.expansion_type_x == ExpansionType.EXPAND) or (self.axis == Axis.Y and child.expansion_type_y == ExpansionType.EXPAND):
                    size = child.calc_maximum_size()
                    max_span = size.x if self.axis == Axis.X else size.y

                    expand_to = min(max_span, local_span - total_span + info.span)

                    # print("max_span " + str(max_span))
                    # print("expand_to " + str(expand_to))

                    # info = infoList[i]
                    # print("info.span " + str(info.span))

                    span_to_add = expand_to - info.span
                    info.span += span_to_add
                    total_span += span_to_add

#                print("child %d: span %d" % (i, info.span))

        #Apply sizes
        cursor_offset = 0
        for i in range(len(self.children)):
            child = self.children[i]
            info = infoList[i]

            if self.axis == Axis.X:
                child.position.x = cursor_offset
                child.position.y = 0
                child.size.x = info.span
                child.size.y = bounds.height
            else:
                child.position.x = 0
                child.position.y = cursor_offset
                child.size.x = bounds.width
                child.size.y = info.span

            child.layout_components()

            cursor_offset += info.span

    def draw(self, ctx):
        for child in self.children:
            child.draw(ctx)

    def pick_panel_stack(self, pos):
        for child in self.children:
            child_pos = child.position

            #Bounds relative to parent
            bounds = child.bounds()

#            print ("testing bounds " + str(bounds))
            if bounds.contains(pos[0], pos[1]):
#                print ("bounds test passed")

                rel_pos = pos - child_pos
                result = child.pick_panel_stack(rel_pos)

                # if result:
                    # return True
                result.insert(0, child)
                return result

        return []

    def mouse_pressed(self, event):
        # print ("layout mouse_pressed")
        # print ("in event " + str(event))

        for child in self.children:
            pos = child.position

            #Bounds relative to parent
            bounds = child.bounds()

#            print ("testing bounds " + str(bounds))
            if bounds.contains(event.pos[0], event.pos[1]):
#                print ("bounds test passed")

                evt = event.copy()
                evt.pos -= pos
                result = child.mouse_pressed(evt)

                if result:
                    return True

        return False

    def mouse_released(self, event):
        for child in self.children:
            pos = child.position

            #Bounds relative to parent
            bounds = child.bounds()
            if bounds.contains(event.pos[0], event.pos[1]):

                evt = event.copy()
                evt.pos -= pos
                result = child.mouse_released(evt)
                if result:
                    return True

        return False

    def mouse_moved(self, event):
        for child in self.children:
            pos = child.position

            #Bounds relative to parent
            bounds = child.bounds()
            if bounds.contains(event.pos[0], event.pos[1]):

                evt = event.copy()
                evt.pos -= pos
                result = child.mouse_moved(evt)
                if result:
                    return True

        return False


from __future__ import print_function
import os.path
from time import sleep
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

# The ID and range of a sample spreadsheet.

row = 2

SAMPLE_SPREADSHEET_ID = '1TrBjFW-bFRMBze4ShWvYtB9eFvEyvyW10J_Aoqfw4fQ' #Add the ID of the spreadsheet with you urls and then where you
SAMPLE_RANGE_NAME_SHEET1 = 'Sheet1!A2:A' # Where your URLS begin on the sheets

def main():
    service = get_google_service() # connects to your google sheets
    data = get_data(service) # getting the url from the sheets
    data = run_selenium(data)

    append_values(data, service)

def get_google_service():
    """Shows basic usage of the Sheets API.
    Prints values from a sample spreadsheet.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    return build('sheets', 'v4', credentials=creds)

def get_data(service):
    try:
         # Call the Sheets API
        sheet = service.spreadsheets()
        result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                    range=SAMPLE_RANGE_NAME_SHEET1).execute()
        values = result.get('values', [])

        if not values:
            print('No data found.')
            return
        print(values)
        return values
    except HttpError as err:
        print(err)

def run_selenium(data):
    service = Service(executable_path="/path/to/chromedriver")
    driver = webdriver.Chrome(service=service)
    driver.get("https://wordcounter.net/website-word-count")
    input = driver.find_element(By.XPATH, '//*[@id="url"]')
    return_data = []
    for row in data:
        input = driver.find_element(By.XPATH, '//*[@id="url"]')
        input.send_keys(row[0])
        form = driver.find_element(By.XPATH, '/html/body/div[6]/div/form/div/div[2]/input')
        form.click()
        top_word = driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[1]/div/table/tbody/tr[1]/td[1]')
        top_word_inner_html = top_word.get_attribute('innerHTML')
        top_word_count = driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[1]/div/table/tbody/tr[1]/td[2]')
        top_word_count_inner_html = top_word_count.get_attribute("innerHTML")
        total_word_count = driver.find_element(By.XPATH, '//*[@id="word_count_block"]/span')
        total_word_count_inner_html = total_word_count.get_attribute('innerHTML')
        index_to_remove = total_word_count_inner_html.index('<')
        total_word_count_inner_html = total_word_count_inner_html[:index_to_remove]
        arr_to_append = [total_word_count_inner_html, top_word_inner_html, top_word_count_inner_html]
        return_data.append(arr_to_append)
    driver.close()
    return return_data

def append_values(values, service):
    range = f'Sheet1!B2'
    result = service.spreadsheets().values().update(
        spreadsheetId=SAMPLE_SPREADSHEET_ID,
        range=range,
        valueInputOption='RAW',
        body={'values': values}
    ).execute()
    # Return the result of the API call
    return result

if __name__ == '__main__':
    main()


"""
Stack Sequence

We can replay the pushes, and whenever we can pop the value at the start of pops, we do it cause why not.

"""
class Solution:
    def solve(self, pushes, pops):
        s=[]
        for p in pushes:
            s.append(p)
            while s and pops and s[-1] == pops[0]:
                s.pop()
                pops.pop(0)
        return s == pops == []


import math
from collections.abc import Iterable
import types
import torch

all_checks = True

def sign_bernoulli(x):
    u = torch.empty_like(x).uniform_()
    return (x.detach() - u).sign_()

def to_tuple(z):
    if isinstance(z, tuple):
        return z
    elif isinstance(z, types.GeneratorType):
        return tuple(z)
    else:
        return (z,)

def untuple(z):
    if isinstance(z, tuple) and len(z) == 1:
        return z[0]
    else:
        return z

class NumericalProblem(BaseException):
    pass

def check_var(x):
    """
    Check variance values are elligible
    """
    # print(torch.min(x.data))
    if all_checks:
        if not (x.data >= 0).all():
            raise NumericalProblem('variance is negative or nan')

def check_real(x):
    """
       Check for NaN / Inf
    """
    if all_checks:
        if not (x == x).all():
            raise NumericalProblem('NaN encountered')
        if not (x.abs() < float('inf')).all():
            raise NumericalProblem('+-Inf encountered')

def nan_to_zero(x):
    if math.isnan(x):
        x = 0.0
    return x


import requests
import re

def crawl_subdomains(domain):
  """Crawls all subdomains of a domain."""
  subdomains = []
  response = requests.get(f"https://{domain}")
  for link in response.text.split("/"):
    if re.match(r"^[a-z0-9-]+$", link):
      subdomains.append(link)
  return subdomains

if __name__ == "__main__":
  subdomains = crawl_subdomains("https://autolazy.vercel.app")
  print(subdomains)


import socket
import select

import json
import time
import globals
import threading

# s.setblocking(0)

def bytefixstrip(inp):
    inp = str(inp)
    output = inp[2:]
    output = output[:len(output) - 1]
    return output

count = 0

class ClientThread(threading.Thread):
    def __init__(self):
        super().__init__()
        self.daemon = True
        self.name = "Client thread"
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.bind(("", 5469))
        self.connected = False
        if not self.connected and globals.checkIfFirstAcceptance():
            while not self.connected:
                try:
                    # pass
                    self.s.listen(5)
                    #  Listening completes the connection and starts the game for the person who does NOT listen first
                    connection, addr = self.s.accept()
                    # print(addr)
                    # TODO: maybe log address of connections? or log sent data that is unusual?
                    self.s = connection
                    #  Search for and accept the new connection, which will be used to receive data.
                    #  One connection object for each client
                    connectHeader = self.s.recv(4)
                    if int(bytefixstrip(connectHeader)) == 0:
                        self.connected = True
                except Exception as e:
                    print("Client first connect: " + str(e))

    def run(self):
        print("Starting client...")
        while True:
            try:
                if not self.connected and not globals.checkIfFirstAcceptance():
                    while not self.connected:
                        try:
                            self.s.listen(5)
                            connectHeader = self.s.recv(4)
                            if int(bytefixstrip(connectHeader)) == 0:
                                self.connected = True
                        except Exception as e:
                            print("Client second connect: " + str(e))
                            # This is trying to fix OSError 10060, which states no response is made
                            # So sending an empty header upon initialization should get the server on the other end as it comes up.
                            # (The server does seem to become alive even when this is in a while loop as it causes 10060)
                        # except Exception as e:
                        #     print("Client init: " + str(e))
                        #  Possible solution above is known to cause OSError 10057 as it constantly tries to send
                        #  Before the server is opened. Theory for test is that as the server for the other side
                        #  Receives this, the socket will be connected and messages can be sent as normal
                # time.sleep(0.05)
                # while True:
                #     clientConnection = s.accept()
                #     print(clientConnection)
                # s.connect((socket.gethostname(), 5469))
                # print(str(socket.gethostname()))
                # print(str(type(socket.gethostname())))

                header = self.s.recv(4)  # Original cause of 10057
                if header != b'':
                    header = int(bytefixstrip(header))
                    # print(int(str(header)))
                    # print("Message header shows {0} bytes of data.".format(str(header)))
                    if header > 0:
                        msg = self.s.recv(header)
                        jsonned = json.loads(msg)

                    globals.p2Pos = jsonned["position"]
                    if "spawn" in jsonned:
                        globals.online_received["spawn"] = jsonned["spawn"]
                    if "build" in jsonned:
                        globals.online_received["build"] = jsonned["build"]
                    if "move" in jsonned:
                        globals.online_received["move"] = jsonned["move"]
                    if "mapfile" in jsonned:
                        globals.online_received["mapfile"] = jsonned["mapfile"]
                    if "HQSpawn" in jsonned:
                        globals.online_received["HQData"] = jsonned["HQData"]

                    #  Syncing usernames
                    if not globals.recievedName:
                        if "username" in jsonned:
                            globals.p2_name = jsonned["username"]
                            globals.online_sending["confirm_u"] = jsonned["username"]
                        if "confirm_u" in jsonned:
                            if jsonned["confirm_u"] == globals.p1_name:
                                globals.recievedName = True
            except Exception as e:
                # raise e
                print("Client: " + str(e))

    def kill(self):
        self.s.close()
        del self

# if __name__ == "__main__":
#     pass


import player
import users

class QueueTrack:
    def __init__(self, track, owner):
        self.track, self.owner, self.skip_votes = track, owner, []

    def to_JSON(self):
        return {"name": self.track.name, "artist": self.track.artist, "album": self.track.album, "uri": self.track.uri,
                "owner": self.owner.name, "skip_votes": len(self.skip_votes)}

class SongQueue:
    def __init__(self):
        self._queue = []
        player.register_end_of_track_listener(self._end_of_track_listener)

    def add_track(self, user, track, play_next=False):
        track = QueueTrack(track, user)

        if play_next and len(self._queue) > 1:
            if user.tokens >= 2:
                self._queue.insert(1, track)
                user.tokens -= 2

                if len(self._queue) == 1:
                    player.play_song(self._queue[0].track.uri)

                return "success"

            return "fail"
        else:
            if user.tokens >= 1:
                self._queue.append(track)
                user.tokens -= 1

                if len(self._queue) == 1:
                    player.play_song(self._queue[0].track.uri)

                return "success"

            return "fail"

    def next(self):
        if len(self._queue) == 0:
            return None

        self._queue.pop(0)

        if len(self._queue) > 0:
            player.play_song(self._queue[0].track.uri)

            return self._queue[0]

        player.stop_playback()

    def skip(self, user=None):
        if len(self._queue) > 0 and (user is None or user.tokens >= 1):
            self.next()

            if user:
                user.tokens -= 1

            return "success"

        return "fail"

    def vote_skip(self, index, user):
        if index < len(self._queue):
            if user.uid not in self._queue[index].skip_votes:
                self._queue[index].skip_votes.append(user.uid)

                if len(self._queue[index].skip_votes) >= round(len(users.users) * (2/3)):
                    if index == 0:
                        self.skip()
                    else:
                        self.remove(index)

                return "success"

        return "fail"

    def remove(self, index, user=None):
        if 0 < index < len(self._queue) and (user is None or user.tokens >= 1):
            self._queue.pop(index)

            if user:
                user.tokens -= 1

            return "success"

        return "fail"

    def _end_of_track_listener(self, session):
        session.player.play(False)
        self.next()

    def to_JSON(self):
        return [s.to_JSON() for s in self._queue]

queue = SongQueue()


import os
import torch
import argument_parser
import constants
from utils.saver import Saver
from utils.trainer import Trainer
from dataloader.indoor_scenes import IndoorScenes
from dataloader.indoor_scenes import get_active_dataset
from dataloader import dataset_base
from active_selection import get_active_selector
from model.deeplab import DeepLab
from utils.summary import TensorboardSummary
from utils.calculate_weights import calculate_weights_labels

def main():

    args = argument_parser.parse_args()
    print(args)
    torch.manual_seed(args.seed)
    torch.backends.cudnn.benchmark = True
    # hardcoding scannet

    # get handle to lmdb dataset
    lmdb_handle = dataset_base.LMDBHandle(os.path.join(constants.HDD_DATASET_ROOT, args.dataset, "dataset.lmdb"), args.memory_hog)

    # create train val and test sets
    train_set = get_active_dataset(args.active_selection_mode)(args.dataset, lmdb_handle, args.superpixel_dir, args.base_size, 'seedset_0')
    val_set = IndoorScenes(args.dataset, lmdb_handle, args.base_size, 'val')
    test_set = IndoorScenes(args.dataset, lmdb_handle, args.base_size, 'test')

    class_weights = None
    if args.use_balanced_weights:
        class_weights = calculate_weights_labels(get_active_dataset(args.active_selection_mode)(args.dataset, lmdb_handle, args.superpixel_dir, args.base_size, 'train'))

    saver = Saver(args)
    saver.save_experiment_config()
    summary = TensorboardSummary(saver.experiment_dir)
    writer = summary.create_summary()

    # get active selection method
    active_selector = get_active_selector(args, lmdb_handle, train_set)

    # for each active selection iteration
    for selection_iter in range(args.max_iterations):

        fraction_of_data_labeled = int(round(train_set.get_fraction_of_labeled_data() * 100))

        if os.path.exists(os.path.join(constants.RUNS, args.dataset, args.checkname, f'runs_{fraction_of_data_labeled:03d}', "selections")):
            # resume: load selections if this is a rerun, and selections are available from a previous run
            train_set.load_selections(os.path.join(constants.RUNS, args.dataset, args.checkname, f'runs_{fraction_of_data_labeled:03d}', "selections"))
        elif os.path.exists(os.path.join(constants.RUNS, args.dataset, args.checkname, f'runs_{fraction_of_data_labeled:03d}', "selections.txt")):
            # resume: load selections if this is a rerun, and selections are available from a previous run
            train_set.load_selections(os.path.join(constants.RUNS, args.dataset, args.checkname, f'runs_{fraction_of_data_labeled:03d}', "selections.txt"))
        else:
            # active selection iteration

            train_set.make_dataset_multiple_of_batchsize(args.batch_size)
            # create model from scratch
            model = DeepLab(num_classes=train_set.num_classes, backbone=args.backbone, output_stride=args.out_stride, sync_bn=args.sync_bn,
                            mc_dropout=((args.active_selection_mode.startswith('viewmc')) or(args.active_selection_mode.startswith('vote')) or args.view_entropy_mode == 'mc_dropout'))
            model = model.cuda()
            # create trainer
            trainer = Trainer(args, model, train_set, val_set, test_set, class_weights, Saver(args, suffix=f'runs_{fraction_of_data_labeled:03d}'))

            # train for args.epochs epochs
            lr_scheduler = trainer.lr_scheduler
            for epoch in range(args.epochs):
                trainer.training(epoch)
                if epoch % args.eval_interval == (args.eval_interval - 1):
                    trainer.validation(epoch)
                if lr_scheduler:
                    lr_scheduler.step()

            train_set.reset_dataset()
            epoch = trainer.load_best_checkpoint()

            # get best val miou / metrics
            _, best_mIoU, best_mIoU_20, best_Acc, best_Acc_class, best_FWIoU = trainer.validation(epoch, test=True)

            trainer.evaluator.dump_matrix(os.path.join(trainer.saver.experiment_dir, "confusion_matrix.npy"))

            writer.add_scalar('active_loop/mIoU', best_mIoU, train_set.get_fraction_of_labeled_data() * 100)
            writer.add_scalar('active_loop/mIoU_20', best_mIoU_20, train_set.get_fraction_of_labeled_data() * 100)
            writer.add_scalar('active_loop/Acc', best_Acc, train_set.get_fraction_of_labeled_data() * 100)
            writer.add_scalar('active_loop/Acc_class', best_Acc_class, train_set.get_fraction_of_labeled_data() * 100)
            writer.add_scalar('active_loop/fwIoU', best_FWIoU, train_set.get_fraction_of_labeled_data() * 100)

            # make active selection
            active_selector.select_next_batch(model, train_set, args.active_selection_size)
            # save selections
            trainer.saver.save_active_selections(train_set.get_selections(), args.active_selection_mode.endswith("_region"))
            trainer.train_writer.close()
            trainer.val_writer.close()

        print(selection_iter, " / Train-set length: ", len(train_set))

    writer.close()

if __name__ == '__main__':
    main()


from openupgradelib import openupgrade

@openupgrade.migrate()
def migrate(env, version):
    if not openupgrade.column_exists(
        env.cr, "product_product", "commingled_policy"
    ) and openupgrade.column_exists(env.cr, "product_template", "commingled_policy"):

        openupgrade.logged_query(
            env.cr,
            """
            ALTER TABLE product_product
            ADD COLUMN commingled_policy varchar,
            ADD COLUMN commingled_prefer_homogenous boolean
            """,
        )

        openupgrade.logged_query(
            env.cr,
            """
            UPDATE
                product_product
            SET
                commingled_policy = product_template.commingled_policy,
                commingled_prefer_homogenous = product_template.commingled_prefer_homogenous
            FROM product_template
            WHERE
                product_template.id = product_product.product_tmpl_id
            """,
        )


#coding: utf-8
import time
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
'''
यहां हम फेसबुक सम्बन्धी अस्त्रों का उल्लेख करेंगे
'''
#from .astra import *
import astra
import karma

#Fire and Forget Astras, to be run with {'msg':'run','func':function_object,'args':(),'kwargs':{}}
def fb_login(browser,fbconfig,logger=astra.baselogger):
		fbusr=fbconfig.get("Facebook","fbusername")
		fbpwd=fbconfig.get("Facebook","fbpassword")
		# or you can use Chrome(executable_path="/usr/bin/chromedriver")
		logger.info("Trying to log into FB in browser...")
		try:
			browser.get("http://www.facebook.com")
			assert "Facebook" in browser.title
			elem = browser.find_element_by_id("email")
			elem.send_keys(fbusr)
			elem = browser.find_element_by_id("pass")
			elem.send_keys(fbpwd)
			elem.send_keys(Keys.RETURN)
			time.sleep(10)
			browser.get("http://facebook.com/profile.php")
			time.sleep(10)
			logger.info("Successfully logged into FB")
		except Exception as exception:
			logger.error("Could not log into FB.."+ repr(exception))

def fb_search(fbbrowser,searchstring,logger=astra.baselogger):
		logger.info("Searching FB for " + searchstring)
		searchbar=fbbrowser.find_element_by_name("q")
		searchbar.clear()
		searchbar.send_keys(searchstring)
		searchbar.send_keys(Keys.ENTER)
		time.sleep(10)

def fb_get_postlinks_from_timeline(fbbrowser,url="https://facebook.com",count=10,logger=astra.baselogger):
    fbbrowser.get(url)
    karma.wait()
    postlinks=[]

    while len(postlinks)<count:
        posts=[]
        postcontents=[]
        posts=posts+fbbrowser.find_elements_by_class_name("userContentWrapper")
        for post in posts:
            postcontents.append(BeautifulSoup(post.get_property("innerHTML")))
        for postcontent in postcontents:

            pc=list(postcontent.find_all("a",{"class":"_5pcq"}))
            if len(pc)>0:
                pc=pc[0]
            else:
                continue
            url="https://facebook.com"+pc.get("href")
            postlinks.append(url)
        logger.info("Got %s posts " %(len(postlinks)))
        postlinks=list(set(postlinks))
        if "https://facebook.com#" in postlinks:
            postlinks.pop(postlinks.index("https://facebook.com#"))
        karma.scroll_page(fbbrowser)
        karma.wait()
    return postlinks[:count]


# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import csv
import logging

from cliff import lister

from goal_tools import governance
from goal_tools import sponsors

LOG = logging.getLogger(__name__)

class ContributionsReportBase(lister.Lister):
    "Base class for commands that report about contributions."

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            '--role',
            default=[],
            action='append',
            help='filter to only include specific roles (may be repeated)',
        )
        sponsor_group = parser.add_mutually_exclusive_group()
        sponsor_group.add_argument(
            '--highlight-sponsors',
            default=False,
            action='store_true',
            help=('highlight sponsor organizations and '
                  'combine stats for others'),
        )
        sponsor_group.add_argument(
            '--only-sponsors',
            default=False,
            action='store_true',
            help=('show only stats for sponsor organizations'),
        )
        parser.add_argument(
            '--sponsor-level',
            default='all',
            choices=('all', 'platinum', 'gold'),
            help='limit sponsor highlights to a subset of sponsors',
        )
        parser.add_argument(
            '--ignore-team',
            default=[],
            action='append',
            help='do not show stats for the named team (may be repeated)',
        )
        parser.add_argument(
            '--only-team',
            default=[],
            action='append',
            help='only show stats for the named team (may be repeated)',
        )
        parser.add_argument(
            '--ignore-tag',
            dest='ignore_tag',
            default=[],
            action='append',
            help=('do not show stats for projects with the tag '
                  '(may be repeated)'),
        )
        parser.add_argument(
            '--only-tag',
            dest='only_tag',
            default=[],
            action='append',
            help=('only show stats for projects with the tag '
                  '(may be repeated)'),
        )
        parser.add_argument(
            '--ignore-single-vendor',
            action='append_const',
            const='team:single-vendor',
            dest='ignore_tag',
            help='do not show stats for single-vendor projects',
        )
        parser.add_argument(
            '--governance-project-list',
            default=governance.PROJECTS_LIST,
            help='location of governance project list',
        )
        parser.add_argument(
            'contribution_list',
            nargs='+',
            help='name(s) of files containing contribution details',
        )
        return parser

    def get_contributions(self, parsed_args):

        def rows():
            for filename in parsed_args.contribution_list:
                LOG.debug('reading %s', filename)
                with open(filename, 'r', encoding='utf-8') as f:
                    reader = csv.DictReader(f)
                    yield from reader

        data = rows()

        roles = parsed_args.role
        if roles:
            data = (d for d in data if d['Role'] in roles)

        ignore_teams = set(t.lower() for t in parsed_args.ignore_team)
        if ignore_teams:
            data = (d for d in data if d['Team'].lower() not in ignore_teams)

        only_teams = set(t.lower() for t in parsed_args.only_team)
        if only_teams:
            data = (d for d in data if d['Team'].lower() in only_teams)

        if parsed_args.only_sponsors:
            sponsor_map = sponsors.Sponsors(parsed_args.sponsor_level)

            data = (
                d
                for d in data
                if d['Organization'] in sponsor_map
            )

        ignore_tags = set(parsed_args.ignore_tag)
        if ignore_tags:
            team_data = governance.Governance(
                url=parsed_args.governance_project_list)

            data = (
                d
                for d in data
                if not team_data.get_repo_tags(d['Project']).intersection(
                    ignore_tags)
            )

        only_tags = set(parsed_args.only_tag)
        if only_tags:
            team_data = governance.Governance(
                url=parsed_args.governance_project_list)

            data = (
                d
                for d in data
                if only_tags.issubset(team_data.get_repo_tags(d['Project']))
            )

        if parsed_args.highlight_sponsors:
            sponsor_map = sponsors.Sponsors(parsed_args.sponsor_level)

            def filter_sponsors(row):
                row['Organization'] = sponsor_map[row['Organization']]
                return row

            data = (filter_sponsors(d) for d in data)

        return data


#!/usr/bin/python3
import tensorflow as tf
import json
import inspect

mnist = tf.keras.datasets.mnist
input("Checkpoint: 01 - Define dataset complete")
(x_train, y_train),(x_test, y_test) = mnist.load_data()
input("Checkpoint: 02 - dataset load complete ")
x_train, x_test = x_train / 255.0, x_test / 255.0
input("Checkpoint: 03 - train and test data modified complete")

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])
input("Checkpoint: 04 - Model definition complete")

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
input("Checkpoint: 05 - Model compile complete")

model.fit(x_train, y_train, epochs=5)
i = 1
input("Checkpoint: 06 - Model training complete")
model.evaluate(x_test, y_test)


class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if not strs:
            return ""
        m, n = len(strs), len(strs[0])
        for j in range(n):
            for i in range(1, m):
                if j == len(strs[i]) or strs[i][j] != strs[0][j]:
                    return strs[0][:j]
        return strs[0]


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

from __future__ import print_function
import os, sys

if sys.version_info[:2] < (2, 7) or (3, 0) <= sys.version_info[:2] < (3, 4):
    raise RuntimeError("Python version 2.7 or >= 3.4 required.")

try:
    from setuptools import setup, find_packages
except ImportError:
    print('# Error: you don\'t have "setuptools" installed!')
    print('# Before install: $ pip install setuptools')
    print('# Or visit https://pypi.org/project/setuptools/')

def rd(filename):
    f = open(filename)
    r = f.read()
    f.close()
    return r

if sys.argv[-1] == "publish":
    os.system("python setup.py sdist upload")
    sys.exit()

setup(name='beautytext',
    version='0.1',
    description=('A tool to fits a text'),
    url='https://github.com/dbednarski/desafios/tree/master/strings',
    author='Daniel Bednarski',
    author_email='daniel.bednarski.ramos@gmail.com',
    license='GNU GPLv3.0',
    packages=['beautytext'],
    scripts=[os.path.join('scripts', f) for f in os.listdir('scripts')],
    long_description=rd('README.md'),
    long_description_content_type="text/markdown",
    install_requires=['numpy'],
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Intended Audience :: Others",
        "License :: OSI Approved :: GNU General Public License v3 or later" + \
        " (GPLv3+)",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 3",
    ],
    keywords=['text', 'string', 'justify'],
    zip_safe=True
)


import os
import time
import copy

U = (-1,0)  #UP
R = (0,1)   #RIGHT
D = (1,0)   #DOWN
L = (0,-1)  #LEFT
U_L = tuple(map(lambda a,b:a+b, U, L))
U_R = tuple(map(lambda a,b:a+b, U, R))
D_R = tuple(map(lambda a,b:a+b, D, R))
D_L = tuple(map(lambda a,b:a+b, D, L))
DIR = [U_L, U, U_R, R, D_R, D, D_L, L]

def solve(input):
    seat_Layout_1 = simulate(input, False, 4)
    print(sum([l.count("#") for l in seat_Layout_1]))

    seat_Layout_2 = simulate(input, True, 5)
    print(sum([l.count("#") for l in seat_Layout_2]))

def simulate(seat_layout, view_further, threshold):
    curr_seats = copy.deepcopy(seat_layout)
    changed = True
    while changed:
        changed = False
        new_seats = copy.deepcopy(curr_seats)
        for row in range(len(curr_seats)):
            for col in range(len(curr_seats[row])):
                occ = get_occupied_seats(curr_seats, row, col, view_further)
                if curr_seats[row][col] == "L" and occ == 0:
                    new_seats[row][col] = "#"
                    changed = True
                if curr_seats[row][col] == "#" and occ >= threshold:
                    new_seats[row][col] = "L"
                    changed = True
        curr_seats = new_seats
    return new_seats

def get_occupied_seats(seat_layout, row, col, view_further=False):
    row_l = len(seat_layout)
    col_l = len(seat_layout[0])

    steps = 1
    if view_further:
        steps = max(row_l, col_l)

    c = 0
    for pos in DIR:
        for s in range(1, steps+1):
            row_ = row + s*pos[0]
            col_ = col + s*pos[1]

            if 0 <= row_ < row_l and 0 <= col_ < col_l:
                seat = seat_layout[row_][col_]
                if seat == '#':
                    c += 1
                    break
                elif seat == 'L':
                    break
            else:
                break
    return c

script_dir = os.path.dirname(__file__)
with open(script_dir + "/input", "r") as myInput:
    start_time = time.time()
    input = [list(line.strip()) for line in myInput]
    solve(input)
    print("--- Execution time %s s ---" % (time.time() - start_time))