import logging
from typing import List

import click

from spoons_scraper.postgres.client import PostgresClient
from spoons_scraper.settings import Settings
from spoons_scraper.types import SpoonsLocation
from spoons_scraper.website import get_locations_generator

def bootstrap_logger(log_debug: bool) -> logging.Logger:
    logging.basicConfig(
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        level=logging.DEBUG if log_debug else logging.INFO,
    )
    return logging.getLogger(__name__)

@click.command()
@click.option("--log-debug", default=False, is_flag=True)
def main(log_debug: bool):

    settings = Settings()
    logger = bootstrap_logger(log_debug)

    pg_client = PostgresClient.bootstrap_from_settings()
    existing_locations: List[SpoonsLocation] = pg_client.get_all_locations()

    locations_generator = get_locations_generator(
        base_url=settings.SPOONS_BASE_URL, locations_path=settings.SPOONS_LOCATIONS_PATH
    )

    for location in locations_generator:

        if location in existing_locations:
            logger.info(f"Already inserted: {location}")
            continue

        pg_client.save_locations([location])
        logger.info(f"Saved new location: {location}")

if __name__ == "__main__":
    main()


# -*- encoding: utf-8 -*-
# here put the import lib
import os
import argparse
import torch
from tqdm import tqdm

from generators.diffusion_generator import DiffusionGenerator
from trainers.diffusion_trainer import DiffusionTrainer

from utils.utils import set_seed, load_pretrained_model
from utils.logger import Logger

parser = argparse.ArgumentParser()

# Required parameters
parser.add_argument("--model_name",
                    default='diffusion',
                    choices=['diffusion'],
                    type=str,
                    required=False,
                    help="model name")
parser.add_argument("--dataset",
                    default="ml1m",
                    choices=['ml1m', 'beauty', 'steam', 'video', 'yelp'],
                    help="Choose the dataset")
parser.add_argument("--inter_file",
                    default="inter",
                    type=str,
                    help="the name of interaction file")
parser.add_argument("--guide_model",
                    default="none",
                    type=str,
                    choices=["none", "cg", "cf", "control"],   # cg = classifier guide, cf = classifier free, control = controlnet
                    help="the type of guided model")
parser.add_argument("--noise_model",
                    default="gru",
                    choices=["gru", "trm", "unet", "unet1d", "unet2d", "wave"],
                    type=str,
                    help="the noise prediction model")
parser.add_argument("--output_dir",
                    default='./saved/',
                    type=str,
                    required=False,
                    help="The output directory where the model checkpoints will be written.")
parser.add_argument("--check_path",
                    default='',
                    type=str,
                    help="the save path of checkpoints for different running")
parser.add_argument("--pretrain_dir",
                    type=str,
                    default="diffusion",
                    help="the path that pretrained model saved in")
parser.add_argument("--demo",
                    default=False,
                    action="store_true",
                    help="whether use the demo dataset")
parser.add_argument("--train",
                    default=False,
                    action="store_true",
                    help="whether run diffusion training")
parser.add_argument("--pretrain_item",
                    default=False,
                    action="store_true",
                    help="whether load the pretrained item embedding")
parser.add_argument("--freeze_item",
                    default=False,
                    action="store_true",
                    help="whether freeze the item embedding during diffusion training")
parser.add_argument("--rec_path",
                    default="bert4rec/normal",
                    type=str,
                    help="the path of well-trained recommendation model")
parser.add_argument("--guide_type",
                    default="item",
                    type=str,
                    choices=["item", "cond", "seq", "bpr"],
                    help="the type of classifier. 'item' uses the first item for guide; \
                    'guide' uses the condition vector for guide. \
                    'seq' uses all items in original sequences for guidance")
parser.add_argument("--classifier_scale",
                    default=1,
                    type=float,
                    help="the scale for classifier guided model")
parser.add_argument("--pref",
                    default=False,
                    action="store_true",
                    help="whether use a preference model")
parser.add_argument("--rounding",
                    default=False,
                    action="store_true",
                    help="whether add rounding loss")
parser.add_argument("--rounding_scale",
                    default=1,
                    type=float,
                    help="the weight for rounding loss")
parser.add_argument("--simple",
                    default=False,
                    action="store_true",
                    help="whether use the simple loss")
parser.add_argument("--clamp",
                    default=False,
                    action="store_true",
                    help="whether use the clamp")
parser.add_argument("--clamp_step",
                    default=10000,
                    type=int,
                    help="the timestep to start using the clamp")

parser.add_argument("--keepon",
                    default=False,
                    action="store_true",
                    help="whether keep on training based on a trained model")
parser.add_argument("--keepon_path",
                    type=str,
                    default="normal",
                    help="the path of trained model for keep on training")

# Model parameters
parser.add_argument("--hidden_size",
                    default=64,
                    type=int,
                    help="the hidden size of embedding")
parser.add_argument("--trm_num",
                    default=2,
                    type=int,
                    help="the number of transformer layer")
parser.add_argument("--num_heads",
                    default=1,
                    type=int,
                    help="the number of heads in Trm layer")
parser.add_argument("--dropout_rate",
                    default=0.5,
                    type=float,
                    help="the dropout rate")
parser.add_argument("--max_len",
                    default=200,
                    type=int,
                    help="the max length of input sequence")
parser.add_argument("--mask_prob",
                    type=float,
                    default=0.4,
                    help="the mask probability for training Bert model")
parser.add_argument("--aug",
                    default=False,
                    action="store_true",
                    help="whether augment the sequence data")
parser.add_argument("--aug_seq",
                    default=False,
                    action="store_true",
                    help="whether use the augmented data")
parser.add_argument("--aug_seq_len",
                    default=10,
                    type=int,
                    help="the augmented length for each sequence")
parser.add_argument("--train_neg",
                    default=1,
                    type=int,
                    help="the number of negative samples for training")
parser.add_argument("--test_neg",
                    default=100,
                    type=int,
                    help="the number of negative samples for test")
parser.add_argument("--suffix_num",
                    default=5,
                    type=int,
                    help="the suffix number for augmented sequence")

# Other parameters
parser.add_argument("--train_batch_size",
                    default=512,
                    type=int,
                    help="Total batch size for training.")
parser.add_argument("--lr",
                    default=0.001,
                    type=float,
                    help="The initial learning rate for Adam.")
parser.add_argument("--l2",
                    default=0,
                    type=float,
                    help='The L2 regularization')
parser.add_argument("--num_train_epochs",
                    default=100,
                    type=float,
                    help="Total number of training epochs to perform.")
parser.add_argument("--lr_dc_step",
                    default=1000,
                    type=int,
                    help='every n step, decrease the lr')
parser.add_argument("--lr_dc",
                    default=0,
                    type=float,
                    help='how many learning rate to decrease')
parser.add_argument("--patience",
                    type=int,
                    default=100,
                    help='How many steps to tolerate the performance decrease while training')
parser.add_argument("--watch_metric",
                    type=str,
                    default='NDCG@10',
                    help="which metric is used to select model.")
parser.add_argument('--seed',
                    type=int,
                    default=42,
                    help="random seed for different data split")
parser.add_argument("--no_cuda",
                    action='store_true',
                    help="Whether not to use CUDA when available")
parser.add_argument('--gpu_id',
                    default=0,
                    type=int,
                    help='The device id.')
parser.add_argument('--num_workers',
                    default=0,
                    type=int,
                    help='The number of workers in dataloader')
parser.add_argument("--log",
                    default=False,
                    action="store_true",
                    help="whether create a new log file")

# Augmentation Parameters
parser.add_argument("--aug_num",
                    default=10,
                    type=int,
                    help="the item number of augmentation")
parser.add_argument("--aug_file",
                    default="inter",
                    type=str,
                    help="the augmentation file name")
parser.add_argument("--no_discrete_user",
                    default=False,
                    action="store_true",
                    help="save augment for not discrete users")

# Parameters for Diffusion Model
parser.add_argument("--num_diffusion_timesteps",
                    default=1000,
                    type=int,
                    help="the number of timesteps")
parser.add_argument("--ema",
                    default=False,
                    action="store_true",
                    help="whether use the ema in DDIM")
parser.add_argument("--ema_rate",
                    default=0.9999,
                    type=float,
                    help="the rate of ema")
parser.add_argument("--guide",
                    default=False,
                    action="store_true",
                    help="whether add guide vector to the diffusion model")
parser.add_argument("--dm_scheduler",
                    default="linear",
                    choices=["linear", "quad", "sigmoid", "cosine"],
                    type=str,
                    help="the type of scheduler for diffusion steps")

args = parser.parse_args()
set_seed(args.seed) # fix the random seed
base_save_path = os.path.join('./saved/', args.dataset)
#base_save_path = os.path.join(base_save_path, args.model_name)
args.output_dir = os.path.join(args.output_dir, args.dataset)
args.output_dir = os.path.join(args.output_dir, args.model_name)
args.output_dir = os.path.join(args.output_dir, args.check_path)    # if check_path is none, then without check_path
args.pretrain_dir = os.path.join(base_save_path, args.pretrain_dir)
args.rec_path = os.path.join(base_save_path, args.rec_path)

def main():

    log_manager = Logger(args)  # initialize the log manager
    logger, writer = log_manager.get_logger()    # get the logger

    device = torch.device("cuda:"+str(args.gpu_id) if torch.cuda.is_available()
                          and not args.no_cuda else "cpu")

    # generator is used to manage dataset
    generator = DiffusionGenerator(args, logger, device)

    trainer = DiffusionTrainer(args, logger, writer, device, generator)

    if args.train:
        trainer.train()

    else:
        trainer.load_model()

        with torch.no_grad():
            trainer.augment()

if __name__ == "__main__":

    main()


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

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging

from django.contrib import messages
from django.core.urlresolvers import reverse
from django.forms.models import formset_factory
from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponseRedirect

from ralph_assets import models as assets_models
from ralph_assets.forms import AttachmentForm
from ralph_assets.views.base import (
    AssetsBase,
    ActiveSubmoduleByAssetMixin,
)
from ralph_assets.models_assets import Attachment, ASSET_TYPE2MODE

logger = logging.getLogger(__name__)

class AttachmentMixin(ActiveSubmoduleByAssetMixin):

    def get_object_class(self):
        return self.Parent

class BaseAttachment(AttachmentMixin, AssetsBase):

    def get_back_url(self, parent_name):
        mapping = {
            'asset': 'dc',
            'licence': 'licences_list',
            'support': 'support_list',
        }
        return reverse(mapping[parent_name])

    def get_parent_mode(self, parent, parent_pk):
        parent_obj = parent.objects.get(pk=parent_pk)
        mode = ASSET_TYPE2MODE[parent_obj.asset_type]
        return mode

    def get_parent_pk(self, request):
        if 'select' in request.GET:
            parent_pk = request.GET.get('select')
        elif 'parent_id' in request.POST:
            parent_pk = request.POST.get('parent_id')
        return parent_pk

    def set_mainmenu_selection(self, mode, parent):
        if parent in set(['licence', 'support']):
            self.mainmenu_selected = parent + 's'
        else:
            self.mainmenu_selected = mode

    def dispatch(self, request, parent, *args, **kwargs):
        if parent == 'license':
            parent = 'licence'
        self.Parent = getattr(assets_models, parent.title())
        self.parent_name = parent
        parent_pk = self.get_parent_pk(request)
        self.mode = self.get_parent_mode(self.Parent, parent_pk)
        self.set_mainmenu_selection(self.mode, parent)
        return super(BaseAttachment, self).dispatch(
            request, self.mode, *args, **kwargs
        )

class AddAttachment(BaseAttachment):
    """
    Adding attachments to Parent.
    Parent can be one of these models: License, Asset, Support.
    """
    template_name = 'assets/add_attachment.html'

    def get_context_data(self, **kwargs):
        ret = super(AddAttachment, self).get_context_data(**kwargs)
        ret.update({
            'selected_parents': self.selected_parents,
            'formset': self.attachments_formset,
        })
        return ret

    def get(self, *args, **kwargs):
        url_parents_ids = self.request.GET.getlist('select')
        self.selected_parents = self.Parent.objects.filter(
            pk__in=url_parents_ids,
        )
        if not self.selected_parents.exists():
            messages.warning(self.request, _("Nothing to edit."))
            return HttpResponseRedirect(self.get_back_url(self.parent_name))

        AttachmentFormset = formset_factory(
            form=AttachmentForm, extra=1,
        )
        self.attachments_formset = AttachmentFormset()
        return super(AddAttachment, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        url_parents_ids = self.request.GET.getlist('select')
        self.selected_parents = self.Parent.objects.filter(
            id__in=url_parents_ids,
        )
        AttachmentFormset = formset_factory(
            form=AttachmentForm, extra=0,
        )
        self.attachments_formset = AttachmentFormset(
            self.request.POST, self.request.FILES,
        )
        if self.attachments_formset.is_valid():
            for form in self.attachments_formset.forms:
                attachment = form.save(commit=False)
                attachment.uploaded_by = self.request.user
                form.save()
                for parent in self.selected_parents:
                    parent.attachments.add(attachment)
            messages.success(self.request, _("Changes saved."))
            return HttpResponseRedirect(self.get_back_url(self.parent_name))
        messages.error(self.request, _("Please correct the errors."))
        return super(AddAttachment, self).get(*args, **kwargs)

class DeleteAttachment(BaseAttachment):

    def post(self, *args, **kwargs):
        parent_id = self.request.POST.get('parent_id')
        attachment_id = self.request.POST.get('attachment_id')
        try:
            attachment = Attachment.objects.get(pk=attachment_id)
        except Attachment.DoesNotExist:
            messages.error(
                self.request, _("Selected attachment doesn't exists.")
            )
            return HttpResponseRedirect(self.get_back_url(self.parent_name))
        try:
            self.parent = self.Parent.objects.get(pk=parent_id)
        except self.Parent.DoesNotExist:
            messages.error(
                self.request,
                _("Selected {} doesn't exists.").format(self.parent_name),
            )
            return HttpResponseRedirect(self.parent.url)
        delete_type = self.request.POST.get('delete_type')
        if delete_type == 'from_one':
            if attachment in self.parent.attachments.all():
                self.parent.attachments.remove(attachment)
                self.parent.save()
                msg = _("Attachment was deleted")
            else:
                msg = _(
                    "{} does not include the attachment any more".format(
                        self.parent_name.title()
                    )
                )
            messages.success(self.request, _(msg))

        elif delete_type == 'from_all':
            Attachment.objects.filter(id=attachment.id).delete()
            messages.success(self.request, _("Attachments was deleted"))
        else:
            msg = "Unknown delete type: {}".format(delete_type)
            messages.error(self.request, _(msg))
        return HttpResponseRedirect(self.parent.url)


from flask import Blueprint
from flask import flash
from flask import g
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for

from banker.auth import login_required
from banker.db import get_db
from banker.helper.util import validate_balance

bp = Blueprint("bank", __name__)

@bp.route("/bank")
@login_required
def index():
    bal = get_balance(g.user["username"])
    return render_template('bank/index.html', balance=bal)

'Get balance from user'
@bp.route("/balance", methods=["GET"])
@login_required
def get_balance_by_user():

    return get_balance(g.user["username"])

def get_balance(usrname):

    db = get_db()
    balance = db.execute("Select balance FROM user WHERE username = ?", (usrname,)).fetchone()
    if balance:
        return str(balance[0])

'Change balance based on operation'
@bp.route("/balance", methods=["POST"])
@login_required
def adjust_balance():

    usrname = g.user["username"]
    db = get_db()
    type = request.form["adjust_balance"]
    balance = db.execute("Select balance FROM user WHERE username = ?", (usrname,)).fetchone()

    error = None

    success = None

    amount = request.form["amount"]
    if not validate_balance(amount):
        error = "Balance should be between 0 to 4294967295.99."
    else:
        amount = float(amount)
    if error is None:
        if type == "deposit":
            new_amount = balance[0] + amount
            if new_amount > 4204967295.99:
                error = "Amount Overflow"
            else:
                success = "Deposit Successful"

        elif type == "withdraw":
            new_amount = balance[0] - amount
            if new_amount < 0:
                error = "Insufficient Balance"
            else:
                success = "Withdraw Successful"

    if error is None:
        new_amount = round(new_amount,2)
        db.execute("Update user SET balance = ? WHERE username = ?", (new_amount,usrname,))
        db.commit()
        flash(success)

    else:
        flash(error)

    return redirect(url_for("bank.index"))

'reset current account balanc '
@bp.route("/<usrname>/balance/reset", methods=["GET", "POST"])
@login_required
def reset_balance(usrname):
    error = None

    if g.user["username"] == "admin":
        db = get_db()
        db.execute("Update user SET balance = 0 WHERE username = ?", (usrname,))
        db.commit()
        return "Balance for the {} has been changed".format(usrname)
    else:
        return redirect(url_for("bank.index"))


from scrapy.spiders import Spider, CrawlSpider, Rule
from scrapy.selector import Selector
from scrapy.linkextractors import LinkExtractor

from crawlers.items import newsItem
import unicodedata

class economist(CrawlSpider):
    name = "economist"
    allowed_domains = ["economist.com"]
    start_urls = ["https://www.economist.com/", "https://www.economist.com/news/world-week/21600185-politics-week", "https://www.economist.com/business-finance", "https://www.economist.com/economics", "https://www.economist.com/science-technology", "https://www.economist.com/culture"]

    rules = (
        Rule(LinkExtractor(restrict_xpaths=['//*[@id="hero"]/li/div/a', '//*[@id="homepage-center-inner"]/section/article/a', '//*[@id="homepage-highlight-1"]/article/a', '']),callback='parse_item', follow=True),
        )
    #

    def parse_item(self, response):
        try:
            sel = Selector(response)
            #print response
            title = sel.xpath('/html/head/title/text()')
            #print '           '
            #print unicodedata.normalize('NFKD', title[0].extract()).encode('ascii', 'ignore')
            #print '           '
            author = sel.xpath('//*[@id="story-header"]/div/div/p/span/a/span/text()')
            #content = sel.xpath('//*[@id="story"]/p[1]/text()') #List
            content = sel.xpath('//*[@class="story-body-text story-content"]/text()')
            link = sel.xpath('/html/head/link[2]/@href')

            item = newsItem()

            try:
                date = sel.xpath('//*[@id="story-header"]/div/div/p/time/text()')
                item['date'] = unicodedata.normalize('NFKD', date[0].extract()).encode('ascii', 'ignore')
            except:
                item['date'] = -1

            item['title'] = unicodedata.normalize('NFKD', title[0].extract()).encode('ascii', 'ignore')
            item['link'] = unicodedata.normalize('NFKD', link[0].extract()).encode('ascii', 'ignore')
            item['author'] = unicodedata.normalize('NFKD', author[0].extract()).encode('ascii', 'ignore')
            item['publication'] = 'Economist'
            item['politicalScore'] = 0
            item['posNegScore'] = 0

            wholeBody = ''
            for part in content:
                wholeBody += unicodedata.normalize('NFKD', part.extract()).encode('ascii', 'ignore')
            item['body'] = unicodedata.normalize('NFKD', content[0].extract()).encode('ascii', 'ignore')
            item['body'] = wholeBody
            #print(link)

            return [item]
        except:
            return []


import logging
from flask import Flask
from flask_crontab import Crontab

from app.worker.orders_worker import OrdersWorker
from app.config import Config

app = Flask(__name__)
crontab = Crontab(app)

# Setup logging
handler = logging.FileHandler(Config.LOGGING_LOCATION)
handler.setLevel(Config.LOGGING_LEVEL)
formatter = logging.Formatter(Config.LOGGING_FORMAT)
handler.setFormatter(formatter)
app.logger.addHandler(handler)

@app.route("/")
def etl():
    return "ETL app!"

@crontab.job(minute="*/5")
def process_data():
    worker = OrdersWorker()
    worker.process()


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 10 08:01:41 2015

@author: Joe
"""

import numpy as np
import pandas as pd
import math
import matplotlib.pyplot as plt
import datetime as dt
import time
import re
from csv import reader
from collections import Counter
from matplotlib.pyplot import gca

tnrfont = {'fontname':'Helvetica'}

# This function turns a time stamp to several values
def timestamp2vals(entry):
    # Get date info
    just_date = entry[0:10]
    three_nums_date = just_date.split('/')
    year = int(three_nums_date[2])
    mon = int(three_nums_date[0])
    day = int(three_nums_date[1])

    # Get time info
    just_time = entry[11:19]
    three_nums_time = just_time.split(':')
    second = int(three_nums_time[2])
    minute = int(three_nums_time[1])
    hour = int(three_nums_time[0])

    ampm = entry[20:]
    if ampm is 'PM':
        hour = hour+12
    # Get seconds since epoch
    try:
        time_stamp = dt.datetime(year, mon, day, hour, minute, second)
        secs_since_epoch = time.mktime(time_stamp.timetuple())
    except:
        secs_since_epoch = 0

    out_var = (year, mon, day, hour, minute, second, secs_since_epoch)
    return out_var

#%% Read data

# Open the data file
f = open('Seattle_Collision_Data.csv')
# Get the header lines
headers = f.readline()
headers = headers.rstrip()
header_list = headers.split(',')

# Get the column indices of importance from header line
headers_of_interest = ['Shape','FATALITIES','INCDATE','INJURIES','ROADCOND','SEVERITYCODE','WEATHER']
indices = []
for name in headers_of_interest:
    indices.append(header_list.index(name))

# Get several lists of data from the file
time_list = []
loc_list = []
severity_list = []
weather_list = []

info = reader(f,delimiter=',',quotechar='"')
for row in info:
    # Row is a list of all info for the nth incident
    # Extract info for the location, date/time, severity, and weather
    loc_list.append(row[indices[0]])
    time_list.append(row[indices[2]])

    # Some of these are not integers
    try:
        severity_list.append(int(row[indices[5]]))
    except:
        item_to_add = int(row[indices[5]][0]) +.5
        severity_list.append(item_to_add)
    weather_list.append(row[indices[6]])

#%% Sort the data from past to future

time_info=[]
hours=[]
days=[]
mons=[]
blanks = []

counter = 0
for item in time_list:
    # Each element is a tuple with year, mon, day, hour, min, sec, secs since epoch
    counter = counter+1
    # Most of the entries are good, a few are blank
    try:
        time_info.append(timestamp2vals(item))
    except:
        blanks.append(counter-1)

for item in time_info:
    hours.append(item[3])
    days.append(item[2])
    mons.append(item[1])

#%% Get rid of the info for bad dates
for kk in range(len(blanks)-1,-1,-1):
    del(loc_list[kk])
    del(severity_list[kk])
    del(weather_list[kk])

counter = 0
zerolist= []
secs = []
for item in time_info:
    counter = counter+1
    # Some items may be before the epoch, throw these out
    if item[6] == 0:
        zerolist.append(counter-1)
    else:
        secs.append(item[6])

# Get rid of the info for more bad dates
for kk in range(len(zerolist)-1,-1,-1):
    del(loc_list[kk])
    del(severity_list[kk])
    del(weather_list[kk])

# Sort the events from past to future (the first couple data points seem weird, throw these out)
secs = np.array(secs)
sort_index=np.argsort(secs)
secs = secs[sort_index[2:]]
secs = secs - secs[0]

# Sort the other info
loc_list = [loc_list[i] for i in sort_index[2:]]
severity_list = [severity_list[i] for i in sort_index[2:]]
weather_list = [weather_list[i] for i in sort_index[2:]]

plt.figure(0)
plt.plot(secs)
plt.xlabel('Event', **tnrfont)
plt.ylabel('Time [s]', **tnrfont)
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.savefig('Fig0.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

# Convert seconds into the associated day
days1 = []
for sec in secs:
    days1.append(int(sec)//(60*60*24))

plt.figure(1)
plt.plot(days1)
plt.xlabel('Event', **tnrfont)
plt.ylabel('Time [days]', **tnrfont)
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.savefig('Fig1.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

#%% Total number of incidents associated with a particular weather condition

names=[]
com_weather_total = Counter(weather_list).most_common()
for name in com_weather_total:
    if name[0] == '':
        names.append('None')
    else:
        names.append(name[0])
vals = [x[1] for x in com_weather_total]

plt.figure(2)
plt.bar(range(0,len(names[0:7])),vals[0:7])
plt.xticks( np.arange(len(names[0:7])), names[0:7], rotation=45, **tnrfont)
plt.ylabel('Number of Occurances', **tnrfont)
#plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.savefig('Fig2.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

#%% Figure out how many events in a given day

start_val = 0
day_list = []
day_dict = dict()
for day in range(0,max(days1)+1):
    num_events = days1.count(day)
    if num_events>0:
        start_val = days1.index(day)
        stop_val = start_val+num_events
        weather = Counter(weather_list[start_val:stop_val]).most_common()[0][0]
        weather_consistency = Counter(weather_list[start_val:stop_val]).most_common()[0][1]/num_events
        severity = np.mean(severity_list[start_val:stop_val])
    else:
        weather = ''
        severity = 0
        weather_consistency = 0
    day_list.append((day,num_events,weather,severity,weather_consistency))

# Now day list is a list of tuples containing the elapsed day from the start day,
# the number of incidents on that day, and the most common weather

# Sorry the variable names get a little weird here
# Day index
x_val = [x[0] for x in day_list]
# Number of incidents
y_val = [x[1] for x in day_list]
# Weather condition on that day
z_val = [x[2] for x in day_list]
# Average severity for that day
w_val = [x[3] for x in day_list]
# Weather consistency (i.e. what fraction of events agree with most common)
c_val = [x[4] for x in day_list]

# Number and severity of incidents per day
plt.figure(3)
plt.plot(x_val,y_val)
plt.xlabel('Day', **tnrfont)
plt.ylabel('Number of Events', **tnrfont)
plt.savefig('Fig3.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

plt.figure(4)
plt.plot(x_val,w_val)
plt.xlabel('Day', **tnrfont)
plt.ylabel('Average Severity of Events', **tnrfont)
plt.savefig('Fig4.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

#%% How many days are most associated with a given weather condition

com_weather = Counter(z_val).most_common()
com_weather_states = [x[0] for x in com_weather]
com_weather_nums = [x[1] for x in com_weather]
for kk in range(0,len(com_weather)):
    if com_weather[kk][0] == 'Raining':
        index1 = kk
    if com_weather[kk][0] == '':
        com_weather_states[kk] = 'None'

#%% How many incidents (and average severity) are associated with a given weather condition

clear_incs = [y_val[i] for i in range(0,len(y_val)) if z_val[i] == com_weather_states[0]]
clear_sev = [w_val[i] for i in range(0,len(w_val)) if z_val[i] == com_weather_states[0]]
clear_cons = [c_val[i] for i in range(0,len(c_val)) if z_val[i] == com_weather_states[0]]
rain_incs = [y_val[i] for i in range(0,len(y_val)) if z_val[i] == com_weather_states[2]]
rain_sev = [w_val[i] for i in range(0,len(w_val)) if z_val[i] == com_weather_states[2]]
rain_cons = [c_val[i] for i in range(0,len(c_val)) if z_val[i] == com_weather_states[2]]

print('Mean number of incidents for: \n')
print('Clear Days (', 100*np.mean(clear_cons), '%): ', np.mean(clear_incs), ' with mean severity of ', np.mean(clear_sev))
print('Rainy Days (', 100*np.mean(rain_cons), '%): ', np.mean(rain_incs), ' with mean severity of ', np.mean(rain_sev))

plt.figure(5)
plt.bar(range(0,len(com_weather_states)),com_weather_nums)
plt.xticks( np.arange(len(com_weather_states)), com_weather_states, rotation=45, **tnrfont)
plt.ylabel('Number of Occurances', **tnrfont)
plt.savefig('Fig5.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

#%% Check days that have particularly many incidents

y_val_mean = np.mean(y_val)
y_val_std = np.std(y_val)
extreme_cases = []
counter = 0
for event in y_val:
    # Check events with more than 1 std greater than the mean number of incidents
    if event > y_val_mean+y_val_std:
        extreme_cases.append((z_val[counter], c_val[counter]))
    counter = counter+1

extreme_weather = [x[0] for x in extreme_cases]

counter_object = (Counter(extreme_weather).most_common())
for kk in range(0,len(counter_object)):
    if counter_object[kk][0] == 'Raining':
        index = kk

fraction = (Counter(extreme_weather).most_common()[index][1])/len(extreme_cases)
fraction1 = (com_weather[index1][1])/len(x_val)
#extreme_cons = [x[1] for x in extreme_cases]
print('On days with particularly many incidents, ', fraction*100, '% of those days were rainy')
print('Overall, ', fraction1*100, '% of days were rainy')

#%% Plots

plt.figure(6)
plt.hist(hours)
plt.xlabel('Hour', **tnrfont)
plt.ylabel('Number of Events', **tnrfont)
plt.savefig('Fig6.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

plt.figure(7)
plt.hist(days, bins=31)
plt.xlabel('Day', **tnrfont)
plt.ylabel('Number of Events', **tnrfont)
plt.savefig('Fig7.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')

plt.figure(8)
plt.hist(mons, bins=12)
plt.xlabel('Month', **tnrfont)
plt.ylabel('Percentange of Events', **tnrfont)
plt.savefig('Fig8.jpeg', format='jpeg', dpi=1000, bbox_inches='tight')


#%%

import openai
import os
import openai
from RotatingTextFile import RotatingTextFile
openai.organization = "org-HRqLWCACWqb2BZgAXGT6UtE8"
openai.api_key = os.environ["OPENAI_API_KEY"]
# openai.Model.list()
# %%
command = "python test.py"
code_file = "test.py"

# run code file and gather stdout and stderr
import subprocess
def run_and_get_output(command):
    # run command and capture both stdout and stderr
    process = subprocess.Popen(
        command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
    )

    # wait for the process to terminate
    stdout, stderr = process.communicate()

    return stdout, stderr

def make_prompt(code_contents, stdout, stderr, command):
    prompt_template = open("prompt.txt", "r").read()
    prompt = prompt_template.format(
        code=code_contents,
        stdout=stdout,
        stderr=stderr,
        command = command,
    )
    return prompt

#%%

def maktlo_iterate(command, code_file):
    stdout, stderr = run_and_get_output(command)
    code_contents = open(code_file, "r").read()

    prompt = make_prompt(code_contents, stdout, stderr, command)

    response = openai.Completion.create(
        model="code-davinci-002",
        prompt=prompt,
        temperature=0,
        max_tokens=182,
        top_p=1.0,
        frequency_penalty=0.0,
        presence_penalty=0.0,
        stop=["###"]
    )
    fixed = response['choices'][0]['text'].strip()

    def condition():
        return True #always rotate

    with RotatingTextFile(code_file,condition,100) as fp:
        fp.write(fixed)
    with RotatingTextFile(code_file+".stdout",condition,100) as fp:
        fp.write(str(stdout))
    with RotatingTextFile(code_file+".stderr",condition,100) as fp:
        fp.write(str(stderr))

# test fixed
for i in range(10):
    maktlo_iterate("python test.py", "test.py")
# %%


import vlc # pip install python-vlc
from datetime import datetime
import time
import os

default_length = 60 # Seconds
vlcInstance = vlc.Instance("--demux=ts",b"--rtsp-frame-buffer-size=800000",b"--network-caching=1000")
path = "Streams/"

#get Preset Path
def get_path():

    if(os.path.exists(path) == False):
        os.makedirs(path)
    return path

def get_stream(length = default_length):
    mediaName = datetime.now().strftime("%m-%d-%Y-%Hh%Mm%Ss") +".avi"
    media_record = vlcInstance.media_new("rtsp://iocldg:iocldg123123@14.241.197.248:2006/cam/realmonitor?channel=1&subtype=0")
    media_record.add_option("sout=#duplicate{dst=file{access=file,mux=avi,dst="+get_path()+ "/" + mediaName+"}}")
    media = vlcInstance.media_player_new()
    media.set_media(media_record)
    media.play()
    time.sleep(length)
    media.stop()
    media.release()

for i in range(10):
    get_stream(600)
    time.sleep(600)


from pathlib import Path
import asyncio
from aiogram import Dispatcher, types
from create_bot import dp, bot
from date_base import sqlite_db
from machine import machine_condition as machine
from games import arithmetic,cancel_bid
from keyboards import bits
from aiogram.types import InputFile
from aiogram.types import ReplyKeyboardRemove
from aiogram.dispatcher.filters import Text
from games import num_let

import random

import tracemalloc

# Включаем tracemalloc
tracemalloc.start()

# Получаем текущую директорию, где находится ваш скрипт
current_directory = Path(__file__).resolve().parent.parent

# Собираем путь к гифке, предполагая, что она находится в двух папках выше
GIF_PATH1 = (current_directory / 'material/rlt1.mp4').resolve()
GIF_PATH2 = (current_directory / 'material/rlt2.mp4').resolve()
gif = [GIF_PATH1, GIF_PATH2]
#список ставок
list_players = {}
numbers_player = 1

from_roulettez = ['Рулетка крутиться, бабосы мутятся', 'Вращайте барабан']
numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12]
colors = ["🔴", "💚", "⚫"]

id_bot_mess = []
async def send_message(callback, text):
    sent_message = await bot.send_message(chat_id=callback.message.chat.id, text=text)

    id_bot_mess.append(sent_message)

async def delete_message(callback):
    try:
        for sent_message in id_bot_mess:
            await bot.delete_message(chat_id=sent_message.chat.id, message_id=sent_message.message_id)
    except Exception as e:
        print(f"Failed to delete message: {e}")

async def send_and_delete_gif(callback):
    sent_message = await bot.send_animation(chat_id=callback.message.chat.id, animation=InputFile(random.choice(gif)))

    # Ждем 3 секунды
    await asyncio.sleep(3)

    # Пытаемся удалить сообщение с гифкой
    try:
        await bot.delete_message(chat_id=callback.message.chat.id, message_id=sent_message.message_id)
    except Exception as e:
        print(f"Failed to delete message: {e}")

def bet_countin_number(win_number, bit_pl, user, win_list, range_number, id):
    user_money = 5
    try:
        if win_number in range(int(bit_pl[0]), int(bit_pl[-1])):

            win_money = user_money*2
            arithmetic.winnings_money(sqlite_db, id, win_money)
            arithmetic.addition(sqlite_db, id, win_money)
            win_list += f"{user.first_name} выиграл {win_money}  на {range_number}\n"
        else:

            arithmetic.losting_money(sqlite_db, id, user_money)
            win_list += f"{user.first_name} на {range_number}\n"

    except:
        win_list = bet_countin_color(win_number, bit_pl, user, win_list, id)
    return win_list

def bet_countin_color(win_number, bit_pl, user, win_list, id):
    user_money = 5
    if num_let.num_let[win_number] == bit_pl[-1]:
        win_money = user_money*2
        arithmetic.addition(sqlite_db, id, win_money)
        arithmetic.winnings_money(sqlite_db, id, win_money)
        win_list += f"{user.first_name} выиграл {win_money}  на {bit_pl[-1]}\n"
    else:
        arithmetic.losting_money(sqlite_db, id, user_money)
        win_list += f"{user.first_name} на {bit_pl[-1]}\n"
    return win_list

def add_list_player(id_player, data):
    global numbers_player
    global numbers
    #добавления в список ставок (игроков)

    list_players[numbers_player] = {id_player : data}

    sqlite_db.cur.execute(f"SELECT money FROM players WHERE id={id_player}")
    user_money = sqlite_db.cur.fetchone()[0]
    return user_money

async def check_money(user_money, id_player, callback):

    #проверка на кол-во денег
    global numbers_player
    global numbers
    if user_money >= 5:
        arithmetic.subtraction(sqlite_db, id_player, 5)
        if callback.data != "Удвоить" and callback.data != 'Повторить':
            await send_message(callback, text = f"Ставка принята: {callback.from_user.first_name} {callback.data}")

        numbers_player += 1

    else:
        await callback.message.answer(f"Не хватает денег, у вас на счету: {user_money}")

#принятия ставок/работа кнопок от бота
@dp.callback_query_handler()
async def command(callback: types.CallbackQuery):
    #начальная ставка

    global numbers_player
    global numbers

    #проверка нажатия на ставки
    if (callback.data == "1 - 3" or callback.data == "4 - 6" or
        callback.data == "7 - 9" or callback.data=="10 - 12"
        or callback.data == "5 на 🔴" or callback.data == "5 на ⚫"
        or callback.data == "5 на 💚"):

        #добавления в список ставок (игроков)
        user_money = add_list_player(callback.from_user.id, callback.data)

        #проверка на кол-во денег
        await check_money(user_money, callback.from_user.id, callback)

    #Удвоить
    if callback.data == "Удвоить":
        id_player = callback.from_user.id
        double_list = {key: value for key, value in list_players.items()}
        for i in double_list:

            for id_for_list in double_list[i].keys():
                if id_for_list == id_player:
                    #добавления в список ставок (игроков)
                    user_money = add_list_player(id_player, double_list[i][id_player])
                    #проверка на кол-во денег
                    await check_money(user_money, id_player, callback)

        await send_message(callback, text = f"Все ставки {callback.from_user.first_name} удвоены")

    #вращать барабан
    if callback.data == "Крутить":
        if len(list_players) != 0:
            win_number = random.choice(numbers)

            win_list = f"Рулетка {win_number} {num_let.num_let[win_number]}\n"

            await send_message(callback, f"{callback.from_user.first_name} крутит рулетку")
            asyncio.ensure_future(send_and_delete_gif(callback))
            await asyncio.sleep(3)

            for n in list_players:
                for id in list_players[n]:
                    range_number = list_players[n][id]
                    bit_pl = range_number.split(" ")
                    for id in list_players[n]:
                        user = await bot.get_chat(id)
                        win_list = bet_countin_number(win_number, bit_pl, user, win_list, range_number, id)

            await callback.message.answer(text=win_list)
            list_players.clear()
            await delete_message(callback)
            id_bot_mess.clear()
        elif len(list_players) == 0:
            await callback.message.answer("Ставки еще не делались. Сперва сделайте ставку")

#запуск коммандлы рулетки
async def roulettez_start(message: types.Message):
    await message.answer(text="""Минирулетка
Угадайте число из:
0💚
1🔴 2⚫ 3🔴 4⚫ 5🔴 6⚫
7🔴 8⚫ 9🔴10⚫11🔴12⚫
Ставки можно текстом:
10 на красное | 5 на 12""", reply_markup=bits)

cancel_bid.register_hendlers_cancel(dp)

def register_hendlers_roulettez(dp : Dispatcher):
    dp.register_message_handler(roulettez_start, Text(equals='Рулетка', ignore_case=True), state="*")


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

from deepFM_model.deepfm import Deepfm
import pickle
import numpy as np
import logging
import torch
from config import Config

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.FileHandler('./logs/stdout.log')
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

'''
加载模型及模型参数
@param
    feature_columns_file:特征列文件
    checkpoint_file:模型权重参数
@return
    model:加载后的模型
'''
def load_model(feat_sizes_file,checkpoint_file,use_cuda = False):
    with open(feat_sizes_file,'rb') as f:
        feat_sizes=pickle.load(f)
    sparse_features = ["uid", "mid","gender", "occp",'genre']
    dense_features = ["age"]
    if use_cuda and torch.cuda.is_available():
        print('cuda ready...')
        device = 'cuda:0'
    else:
        device = 'cpu'
    model = Deepfm(feat_sizes ,sparse_feature_columns = sparse_features,dense_feature_columns = dense_features,
                   dnn_hidden_units=[400,400,400] , dnn_dropout=0.3 , ebedding_size = 8 ,
                   l2_reg_linear=1e-3, device=device)
    model.load_state_dict(torch.load(checkpoint_file))
    return model
'''
使用训练后的模型进行综合排序
@param
    model:加载后的模型
    pred_input:指定格式的模型输入数据
    cfg:配置参数对象
@return
    rank_item_ids:综合排序后的物品id
'''
def predict_rank(model,pred_input,count):
    pred_res=model.predict(pred_input,batch_size=256)
    pred_res=np.squeeze(pred_res,axis=1)
    index=np.flipud(np.argsort(pred_res))
    sorted_pred_res=np.flipud(np.sort(pred_res))
    rank_item_ids=pred_input['mid'][index]
    rank_items=[]
    for i in range(len(rank_item_ids)):
        if i<10:
            continue
        rank_items.append({'productId':str(rank_item_ids[i]),'score':str(sorted_pred_res[i])})
        if i-10>count:
            break
    return rank_items
'''
生成指定格式的模型输入数据
@param
    mongo:mongo连接对象
    userId:用户id
    item_ids:召回的物品id
@return
    pred_input:指定格式的模型输入数据
'''
def create_pred_input(mongo,userId,item_ids):
    user=mongo.db.User.find_one({"userId":userId})
    uid_list=[]
    mid_list=[]
    gender_list=[]
    occp_list=[]
    age_list=[]
    genre_list=[]
    sparse_features = ["uid", "mid","gender", "occp",'genre']
    for item_id in item_ids:
        item=mongo.db.Product.find_one({"productId":item_id})
        uid_list.append(user['userId'])
        mid_list.append(item_id)
        gender_list.append(user['gender'])
        occp_list.append(user['occupation'])
        age_list.append(user['age'])
        try:
            genre=int(item['tags'].split('|')[0])
        except Exception as e:
            logger.error(e)
            genre=0
        genre_list.append(genre)
    pred_input={'uid':np.array(uid_list),'mid': np.array(mid_list),'gender': np.array(gender_list),
                  'occp': np.array(occp_list),'genre': np.array(genre_list),'age': np.array(age_list)}
    for feat in sparse_features:
        with open('./weights/'+feat+'_label_encoder.pkl','rb') as f:
            lbe=pickle.load(f)
            pred_input[feat] = lbe.fit_transform(pred_input[feat])
    return pred_input
'''
cfg=Config()
model=load_model(cfg.feat_sizes_file,cfg.checkpoint_file)
pred_model_input={'uid':np.array([545]),'mid': np.array([221]),'gender': np.array([1]),
                      'occp': np.array([6]),'genre': np.array([1]),'age': np.array([20])}
pred_res=model.predict(pred_model_input)
print(pred_res)
'''


from datetime import date
from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app, origins=["http://localhost", "*"])

@app.route('/')
def index():
  return 'Backend is healthy'

@app.route('/hello_from_backend')
def hello_from_backend():
  print('backend called')
  return {"Hello":["Hello", "From", "Backend"]}

@app.route('/api/date')
def get_current_date():
  today = date.today()
  return {'date': today.strftime("%B %d, %Y")}

if __name__ == '__main__':
  app.run(debug=False, host='0.0.0.0')


from gurobipy import GRB, Model, quicksum

#Conjuntos
H = range(0,2) #Horarios
Dias = range(0,28) #Días
M = range(1,5) #Tipos de Trabajador
N = range(1, 33) #Trabajador

restaurant = Model()

#Variables
T_nmhd = restaurant.addVars(N, M, H, Dias, vtype=GRB.BINARY, name='T') #Si el trabajador n de tipo m trabaja en el turno h del día d.
z_d = restaurant.addVars(Dias,vtype=GRB.BINARY, name='Abrir Local') #si el restaurante abre el día d.
k_d = restaurant.addVars(Dias,vtype=GRB.BINARY, name='Comprar Insumos') #  si se compran insumos el día d
I_d = restaurant.addVars(Dias,vtype=GRB.INTEGER, lb = 0, name='Cantidad Insumos Comprados') #Cantidad de insumos comprados el día d
B_d = restaurant.addVars(Dias,vtype=GRB.INTEGER, lb = 0, name='Cantidad de Insumos en Bodega') #Cantidad de insumos en el inventario en el día d

restaurant.update()

#Parámetros
D_hd = [[40, 50],[50, 60],[80, 90],[100, 120],[130, 220],[180, 250],[0,0],
        [40, 50],[50, 60],[80, 90],[100, 120],[130, 220],[180, 250],[0,0],
        [40, 50],[50, 60],[80, 90],[100, 120],[130, 220],[180, 250],[0,0],
        [40, 50],[50, 60],[80, 90],[100, 120],[130, 220],[180, 250],[0,0]]
        # Demanda de clientes en el restaurante en el horario h el día d. **LISTA**
S = 24 #Espacio medido en metros cuadrados de la cocina
A = 300000 #Gasto de agua e higiene por persona.
CT_m = {1: 9000,
        2: 12000,
        3: 14000,
        4: 10000} #Costo de un Turno de un trabajador tipo m.
U = 8000 #Ganancia promedio entregada por persona.
L_f = 20000 #Gastos de luz diario promedio si el local est´a cerrado.
L_v = 40000 #Gastos de luz diario promedio si abre el local
B = 3075000 #Otros gastos mensuales fijos (internet, cable, etc.).
CM_m = 50 #Capacidad máxima de clientes por trabajador del tipo m.
I_p = 1 #Cantidad de insumos necesitados por persona.
C_i = 5000 #Costo promedio por insumo
v = 12000 #Volumen promedio de los insumos (cm^3)
V = 6000000 # Volumen de la bodega en cm^3.
C_k = 20000 #Costo de ir a comprar insumos.
X = 1000 # Ancho del espacio destinado a mesas.
Y =  1000#Largo del espacio destinado a mesas
r = 35 #cm #Radio de las mesas.
Q = 1000000000 #Número muy grande

#Restricciones

#R1.Personal suficiente atendiendo en el local

restaurant.addConstrs((quicksum(T_nmhd[n,m,h,d] for n in N)) * CM_m >= D_hd[d][h] for m in range(1,4) for h in H for d in Dias)
restaurant.addConstrs((quicksum(T_nmhd[n,4,h,d] for n in N)) == 1 for h in H for d in Dias if (d+1)%7!=0)

#R2.Cantidad máxima de personal en cocina (cada persona cuenta con al menos 4 metros cuadrados).

restaurant.addConstrs(S * 0.25 >= quicksum(T_nmhd[n,3,h,d] for n in N) for h in H for d in Dias) #Asumimos que cada trabajador ocupa 1m^2

#R3.Días domingo no se trabaja.

restaurant.addConstrs(z_d[d] == 0 for d in Dias if (d+1)%7==0  )

#R4.Deben haber suficientes insumos para los clientes.

restaurant.addConstrs(I_d[d] + B_d[d] >= quicksum(I_p * D_hd[d][h] for h in H) for d in Dias)

#R5.Cuando no hay suficientes insumos para satisfacer la demanda del dia se debe ir a comprar

restaurant.addConstrs(- (B_d[d] - quicksum(I_p * D_hd[d][h] for h in H)) <= k_d[d] * Q for d in Dias )
restaurant.addConstrs(- (B_d[d] - quicksum(I_p * D_hd[d][h] for h in H)) + Q  -1 >= k_d[d] * Q for d in Dias)

#R6.Lo que no se ocupa se guarda para el día siguiente.

restaurant.addConstrs(B_d[d] == B_d[d-1] - quicksum(I_p * D_hd[d][y]  for y in range(0,2))+ I_d[d-1] for d in Dias[1:28])

#R7.La bodega comieza vacía.

restaurant.addConstr(B_d[0] == 0)

#R8.Si no se va a comprar no pueden llegar insumos al restaurante

restaurant.addConstrs(I_d[d] <= k_d[d] * Q for d in Dias)

#R9.Los días domingo no se va a comprar.

for d in Dias:
    if (d+1)%7==0:
        restaurant.addConstr(I_d[d] == 0)
        restaurant.addConstr(k_d[d] <= I_d[d])

#R10.No se debe superar la capacidad máxima de almacenaje.

restaurant.addConstrs(v * B_d[d] <= V for d in Dias)

#R11.Cada trabajador puede trabajar en un turno por día como máximo

restaurant.addConstrs(quicksum(quicksum(T_nmhd[n,m,h,d] for m in M) for h in H) <= 1 for d in Dias for n in N)

#Función Objetivo

Ganancias = quicksum(quicksum(U * D_hd[d][h] * z_d[d] for h in H) for d in Dias)
Costos = A + B + quicksum(L_f + z_d[d] *(L_v - L_f) + k_d[d]*(C_k + C_i*I_d[d])   + quicksum(quicksum(z_d[d]* CT_m[m]*quicksum(T_nmhd[n,m,h,d] for n in N) for m in M) for h in H ) for d in Dias)
Objetivo = Ganancias - Costos

restaurant.setObjective(Objetivo, GRB.MAXIMIZE)

restaurant.update()

restaurant.optimize()

restaurant.printAttr("X")
print("\n-------------\n")


import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle
import sqlite3 as sql
import database

model = pickle.load(open('Desktop/RFModel.pkl','rb'))
vectorizer = pickle.load(open('Desktop/FVectorizer.pkl','rb'))
app = Flask(__name__)

@app.route('/')
@app.route('/home')
def home():
    return render_template('index.html')

@app.route('/result',methods =['POST'])
def prediction():
    if request.method == 'POST':
        try:
            review = request.form['review']
            print([review])
            test_bag = vectorizer.transform([review]).toarray()
            review_prediction = model.predict(test_bag)
            result = 'Positive review' if review_prediction[0] else 'Negative Review'
            feedback = 'no feedback'
            with sql.connect("database.db") as con:
                cur = con.cursor()
                cur.execute("INSERT INTO sentiment_analysis (review,prediction,feedback) VALUES (?,?,?)",(review,result,feedback))
                con.commit()
                print("Record successfully added")
        except:
            con.rollback()
            print("error in insert operation")

        finally:
            con.close()
            return render_template("result.html",pred = result,review=review)

@app.route('/data')
def list_all():
    con = sql.connect("database.db")
    con.row_factory = sql.Row

    cur = con.cursor()
    cur.execute("select * from sentiment_analysis")

    rows = cur.fetchall() # returns list of dictionaries
    return render_template("data.html",rows = rows)

@app.route('/feedback',methods =['POST'])
def feedback():
    try:
        rev = request.form['rev']
        feedback = request.form['feedbac']
        result = request.form['predict']
        with sql.connect("database.db") as con:
            cur = con.cursor()
            cur.execute("UPDATE sentiment_analysis SET feedback = ? WHERE review = ?;",(feedback,rev))
            print("Record successfully updated")
            con.commit()
    except:
        con.rollback()
        print("error in update operation")

    finally:
        con.close()
    return render_template("result.html",pred = result,review=rev)

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


#!/usr/bin/python3

#================================================================================================
#
#  FILE:
#      	plot.py
#
#  DESCRIPTION:
#	Plot path length graph
#
#  USAGE:
#      python3 plot.py
#
#================================================================================================

import numpy as num
import pandas as pan
import matplotlib.pyplot as plt
import seaborn as sea
import sys
import os

WORKDIR="graphs_path_length"
show=True

# write additional column to file1
file1=WORKDIR+"/lava_tracelen.txt_sorted"
data=pan.read_csv(file1,sep=" ")
data['Benchmark']="LAVA-1"
data.to_csv(file1 + "_2",sep=",", header=False,index=False)

# write additional column to file2
file2=WORKDIR+"/hyperput_tracelen.txt_sorted"
data2=pan.read_csv(file2,sep=" ")
data2['Benchmark']="HyperPUT"
data2.to_csv(file2 + "_2",sep=",", header=False,index=False)

# merge files
os.system("awk '{print $0}' lava_tracelen.txt_sorted_2 hyperput_tracelen.txt_sorted_2 > total_tracelen.csv")

file_merged=WORKDIR+"/total_tracelen.csv"
merged=pan.read_csv(file_merged,sep=",",header=None,names=["Trace_Length","Benchmark"])

## standard boxplot
figure= merged.boxplot(patch_artist=True, by="Benchmark")

## display
plt.title(None)
plt.suptitle(None)
plt.yscale('log')
#plt.xticks([])
plt.xticks(rotation=55)
plt.xlabel("Benchmark")
plt.ylabel("Trace length")
plt.savefig(WORKDIR + str("/merged_tracelength") + '.png',bbox_inches='tight')
plt.savefig(WORKDIR + str("/merged_tracelength") + '.jpg',bbox_inches='tight')
if show == True:
	plt.show()


from . import point
import vcs
import vtk
import vcs.vcs2vtk
from .font import FontEditor
from vcs.vtk_ui.text import contrasting_color

__valign_map__ = {
    0: 0,
    1: 0,
    2: 1,
    3: 2,
    4: 2,
}

__backend_actor_names__ = {
    "mean": "vtk_backend_Mean_text_actor",
    "min": "vtk_backend_Min_text_actor",
    "max": "vtk_backend_Max_text_actor",
}

def get_actor(member, dp):
    if member.member in __backend_actor_names__:
        actor = dp.backend[__backend_actor_names__[member.member]]
    elif "vtk_backend_%s_text_actor" % member.member in dp.backend:
        actor = dp.backend["vtk_backend_%s_text_actor" % member.member]
    else:
        actor = None
    return actor

class LabelEditor(point.PointEditor):

    """
    An editor for text items that have data-provided text (template.min, template.max, etc.)

    Draggable, provides a toolbar for config options.
    """

    def __init__(self, interactor, label, dp, configurator):
        self.label = label
        self.display = dp
        super(LabelEditor, self).__init__(interactor, label, configurator)

        self.toolbar = vcs.vtk_ui.Toolbar(
            self.interactor,
            "%s Options" %
            label.member)
        template = vcs.gettemplate(dp.template)

        self.actor = get_actor(self.label, self.display)

        tprop = self.actor.GetTextProperty()
        self.real_bg = tprop.GetBackgroundColor()
        self.real_bg_opacity = tprop.GetBackgroundOpacity()

        tprop.SetBackgroundColor(contrasting_color(*tprop.GetColor()))
        tprop.SetBackgroundOpacity(.85)

        text_types_name = template.name + "_" + label.member

        try:
            self.tt = vcs.gettexttable(text_types_name)
            self.to = vcs.gettextorientation(text_types_name)
        except ValueError:
            self.tt = vcs.createtexttable(text_types_name, label.texttable)
            self.to = vcs.createtextorientation(
                text_types_name,
                label.textorientation)

        self.height_button = self.toolbar.add_slider_button(
            self.to.height,
            1,
            100,
            "Height",
            update=self.update_height)

        halign = self.toolbar.add_button(
            ["Left Align", "Center Align", "Right Align"], action=self.halign)
        valign = self.toolbar.add_button(
            ["Top Align", "Half Align", "Bottom Align"], action=self.valign)

        halign.set_state(self.to.halign)
        valign.set_state(__valign_map__[self.to.valign])

        self.angle_button = self.toolbar.add_slider_button(
            self.to.angle,
            0,
            360,
            "Angle",
            update=self.update_angle)

        self.picker = None
        self.toolbar.add_button(["Change Color"], action=self.change_color)
        # Adds itself to self.toolbar automatically
        FontEditor(self.toolbar, self.set_font,
                   current_font=vcs.getfontname(self.tt.font))
        self.toolbar.show()
        self.label.texttable = self.tt.name
        self.label.textorientation = self.to.name

    def save(self):
        if self.actor:
            self.sync_actor()
        else:
            self.configurator.changed = True
            self.configurator.save()

    def sync_actor(self):
        if self.actor:
            p = self.actor.GetTextProperty()
            winSize = self.interactor.GetRenderWindow().GetSize()
            vcs.vcs2vtk.prepTextProperty(
                p,
                winSize,
                to=self.to,
                tt=self.tt,
                cmap=None)

    def set_font(self, font):
        self.tt.font = font
        self.save()

    def halign(self, state):
        self.to.halign = state
        self.save()

    def valign(self, state):
        if state == 0:
            self.to.valign = 0
        elif state == 1:
            self.to.valign = 2
        elif state == 2:
            self.to.valign = 3

        self.save()

    def update_height(self, value):
        self.to.height = int(value)
        self.height_button.set_value(int(value))
        self.save()

    def update_angle(self, value):
        self.to.angle = int(value)
        self.angle_button.set_value(int(value))
        self.save()

    def change_color(self, state):
        if self.picker:
            self.picker.make_current()
        else:
            self.picker = vcs.colorpicker.ColorPicker(
                500,
                500,
                vcs.getcolormap(),
                self.tt.color,
                parent_interactor=self.interactor,
                on_save=self.set_color,
                on_cancel=self.cancel_color)

    def set_color(self, cmap, color):
        self.tt.color = color
        self.picker = None
        self.save()
        tprop = self.actor.GetTextProperty()
        tprop.SetBackgroundColor(contrasting_color(*tprop.GetColor()))
        tprop.SetBackgroundOpacity(.85)

    def cancel_color(self):
        self.picker = None

    def get_text(self):
        return get_label_text(self.label, self.display)

    def place(self):
        pass

    def is_object(self, label):
        return self.label == label

    def in_bounds(self, x, y):
        w, h = self.interactor.GetRenderWindow().GetSize()
        x, y = x * w, y * h
        picker = vtk.vtkPropPicker()
        for ren in vcs.vcs2vtk.vtkIterate(
                self.interactor.GetRenderWindow().GetRenderers()):
            if ren.HasViewProp(self.actor):
                break
        else:
            return False

        if picker.PickProp(x, y, ren) and picker.GetViewProp() == self.actor:
            return True
        else:
            return False

    def delete(self):
        super(LabelEditor, self).delete()
        self.actor.SetVisibility(0)
        self.configurator.deactivate(self)

    def detach(self):
        super(LabelEditor, self).detach()
        self.toolbar.detach()
        tprop = self.actor.GetTextProperty()
        tprop.SetBackgroundColor(*self.real_bg)
        tprop.SetBackgroundOpacity(self.real_bg_opacity)

    def update_priority(self):
        maxLayers = self.interactor.GetRenderWindow().GetNumberOfLayers()
        new_layer = self.label.priority * 10000 + 1 + \
            self.configurator.displays.index(self.display)
        if new_layer + 1 > maxLayers:
            self.interactor.GetRenderWindow().SetNumberOfLayers(new_layer + 1)

        self.actor.SetLayerNumber(new_layer)

        self.render()

    def render(self):
        from vcs.vtk_ui.manager import get_manager
        m = get_manager(self.interactor)
        m.queue_render()

def get_label_text(label, display):
    actor = get_actor(label, display)

    if actor is not None:
        return actor.GetInput()
    else:
        s = label.member

        array = display.array[0]

        try:
            t = getattr(array, s)
        except AttributeError:
            t = ''
        return t


# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import json
import os
import subprocess
import threading

import psutil

from ai_exec import ai_call, ai_dialog_exec, ai_popen
from dlg import dialog
from gui import gui

class PartitionMenuItem():
    def __init__(self, name='', size=0, text='', size_text='', details_text='',
                 ops=None):
        self.name = name
        self.size = size
        self.text = text
        self.size_text = size_text
        self.details_text = details_text
        if ops is None:
            self.ops = []
        else:
            self.ops = ops

class PartitionLib():
    base = 1000
    boot_part_size = 1024 * 1024 * 512
    default_filesystem = 'ext4'
    default_luks_type = 'luks2'
    default_partitioning = 'gpt' if ai_call('efivar -l')[0] == 0 else 'msdos'
    part_granularity = 1024 * 1024
    part_granularity_unit = 'MiB'

    def __init__(self):
        self.boot_target = ''
        self.crypt_passphrase = ''
        self.crypt_target = ''
        self.install_target = ''
        self.swap_crypt_passphrase = ''
        self.swap_crypt_target = ''
        self.swap_target = ''
        self.parts = []
        self.scanned = False

    def __str__(self):
        return str(json.dumps(self.parts, sort_keys=True))

    def _action_autosetup(self, name, size, parttable, crypt=False,
                          passphrase=''):
        boot_start = self.part_granularity
        boot_end = boot_start + self.boot_part_size - 1
        root_start = boot_end + 1
        root_end = size - psutil.virtual_memory().total - \
            self.part_granularity
        root_end = self._to_gran_end(root_end) * self.part_granularity - 1
        swap_start = root_end + 1

        boot_target = self.get_part_from_disk_num(name, 1)
        crypt_passphrase = ''
        crypt_target = ''
        install_target = self.get_part_from_disk_num(name, 2)
        swap_crypt_passphrase = ''
        swap_crypt_target = ''
        swap_target = self.get_part_from_disk_num(name, 3)
        if crypt:
            crypt_passphrase = passphrase
            crypt_target = install_target
            install_target = '/dev/mapper/cryptroot'
            swap_crypt_passphrase = passphrase
            swap_crypt_target = swap_target
            swap_target = '/dev/mapper/swap'

        if crypt:
            with open('/tmp/alinstaller-keyfile', 'w', encoding='utf_8') as f:
                f.write(passphrase)

        cmd = 'parted -m -s \'' + name + '\' unit B mklabel \'' + \
            parttable + '\''
        if parttable == 'gpt':
            cmd += ' mkpart ESP fat32 ' + str(boot_start) + 'B ' + \
                str(boot_end) + 'B'
        else:
            cmd += ' mkpart primary fat32 ' + str(boot_start) + 'B ' + \
                str(boot_end) + 'B'
        cmd += ' mkpart primary ' + self.default_filesystem + ' ' + \
            str(root_start) + 'B ' + str(root_end) + 'B'
        cmd += ' set 1 boot on'
        cmd += ' mkpart primary linux-swap ' + str(swap_start) + 'B 100%'
        cmd += ' && mkfs.fat -F32 \'' + self.get_part_from_disk_num(name, 1) + '\''
        if not crypt:
            cmd += ' && mkfs.ext4 \'' + self.get_part_from_disk_num(name, 2) + '\''
        else:
            cmd += ' && cryptsetup -v -q --key-file /tmp/alinstaller-keyfile luksFormat ' + \
                '--type \'' + self.default_luks_type + '\' \'' + \
                self.get_part_from_disk_num(name, 2) + '\''
            cmd += ' && cryptsetup -q --key-file /tmp/alinstaller-keyfile open \'' + \
                self.get_part_from_disk_num(name, 2) + '\' cryptroot'
            cmd += ' && mkfs.ext4 /dev/mapper/cryptroot'
        if not crypt:
            cmd += ' && mkswap \'' + self.get_part_from_disk_num(name, 3) + '\''
        else:
            cmd += ' && cryptsetup -v -q --key-file /tmp/alinstaller-keyfile luksFormat ' + \
                '--type \'' + self.default_luks_type + '\' \'' + \
                self.get_part_from_disk_num(name, 3) + '\''
            cmd += ' && cryptsetup -q --key-file /tmp/alinstaller-keyfile open \'' + \
                self.get_part_from_disk_num(name, 3) + '\' swap'
            cmd += ' && mkswap /dev/mapper/swap'

        cmd += ' && echo Completed.'

        self._exec(cmd, linger=True, msg='Automatically setting up disk...')

        try:
            os.remove('/tmp/alinstaller-keyfile')
        except Exception:
            pass

        self.boot_target = boot_target
        self.crypt_passphrase = crypt_passphrase
        self.crypt_target = crypt_target
        self.install_target = install_target
        self.swap_crypt_passphrase = swap_crypt_passphrase
        self.swap_crypt_target = swap_crypt_target
        self.swap_target = swap_target

    def _action_boot(self, name):
        disk = self.get_disk_from_part(name)
        num = self.get_num_from_part(name)
        cmd = 'parted -m -s \'' + disk + '\' unit B toggle \'' + \
            str(num) + '\' boot && echo Completed.'

        self._exec(cmd, linger=True, msg='Toggling boot flag...')

    def _action_boot_target(self, name):
        self.boot_target = name
        self._msgbox(_('Successful.'))

    def _action_clear_boot_target(self, name):
        self.boot_target = ''
        self._msgbox(_('Successful.'))

    def _action_clear_crypt_target(self, name):
        self.crypt_passphrase = ''
        self.crypt_target = ''
        self._msgbox(_('Successful.'))

    def _action_clear_install_target(self, name):
        self.install_target = ''
        self._msgbox(_('Successful.'))

    def _action_clear_swap_crypt_target(self, name):
        self.swap_crypt_passphrase = ''
        self.swap_crypt_target = ''
        self._msgbox(_('Successful.'))

    def _action_clear_swap_target(self, name):
        self.swap_target = ''
        self._msgbox(_('Successful.'))

    def _action_cryptsetup(self, name, passphrase):
        with open('/tmp/alinstaller-keyfile', 'w', encoding='utf_8') as f:
            f.write(passphrase)
        self._exec('cryptsetup -v -q --key-file /tmp/alinstaller-keyfile luksFormat --type \'' +
                   self.default_luks_type + '\' \'' + name + '\' && echo Completed.',
                   linger=True, msg='Setting up encryption...')
        os.remove('/tmp/alinstaller-keyfile')

    def _action_cryptopen(self, name, passphrase):
        with open('/tmp/alinstaller-keyfile', 'w', encoding='utf_8') as f:
            f.write(passphrase)
        self._exec('cryptsetup -q --key-file /tmp/alinstaller-keyfile open \'' + name +
                   '\' cryptroot && echo Completed.',
                   linger=True, msg='Opening main encrypted partition...')
        self.crypt_passphrase = passphrase
        self.crypt_target = name
        os.remove('/tmp/alinstaller-keyfile')

    def _action_cryptclose(self, name):
        self._exec('cryptsetup close cryptroot && echo Completed.',
                   linger=True, msg='Closing main encrypted block...')
        self.crypt_passphrase = ''
        self.crypt_target = ''

    def _action_swap_cryptopen(self, name, passphrase):
        with open('/tmp/alinstaller-keyfile', 'w', encoding='utf_8') as f:
            f.write(passphrase)
        self._exec('cryptsetup -q --key-file /tmp/alinstaller-keyfile open \'' + name +
                   '\' swap && echo Completed.',
                   linger=True, msg='Opening swap encrypted partition...')
        self.swap_crypt_passphrase = passphrase
        self.swap_crypt_target = name
        os.remove('/tmp/alinstaller-keyfile')

    def _action_swap_cryptclose(self, name):
        self._exec('cryptsetup close swap && echo Completed.',
                   linger=True, msg='Closing swap encrypted block...')
        self.swap_crypt_passphrase = ''
        self.swap_crypt_target = ''

    def _action_format(self, name, fstype):
        cmd = ''
        if fstype == 'swap':
            cmd = 'mkswap \'' + name + '\''
        else:
            cmd = 'mkfs -t \'' + fstype + '\' \'' + name + '\''
        cmd += ' && echo Completed.'
        self._exec(cmd, linger=True, msg='Formatting partition...')

    def _action_install_target(self, name):
        self.install_target = name
        self._msgbox(_('Successful.'))

    def _action_parttable(self, name, parttable):
        self._exec('parted -m -s \'' + name + '\' unit B mklabel \'' +
                   parttable + '\' && echo Completed.', linger=True,
                   msg='Creating partition table...')

    def _action_part(self, name, start, end, fstype):
        self._exec('parted -m -s \'' + name.split('*')[1] +
                   '\' unit \'' + self.part_granularity_unit + '\' mkpart primary \'' +
                   fstype + '\' \'' + str(start) + '\' \'' + str(end) + '\' ' +
                   '&& echo Completed.', linger=True,
                   msg='Creating new partition...')

    def _action_refresh(self, name):
        pass

    def _action_remove(self, name):
        disk = self.get_disk_from_part(name)
        num = self.get_num_from_part(name)
        self._exec('parted -m -s \'' + disk + '\' unit B rm \'' + str(num)
                   + '\' && echo Completed.', linger=True,
                   msg='Removing partition...')

    def _action_swap_target(self, name):
        self.swap_target = name
        self._msgbox(_('Successful.'))

    def _add_disk_to_menu(self, menu, pref, name, item, size, *args, **kwargs):
        menu.append(PartitionMenuItem(
            name=name,
            size=size,
            text=pref + name,
            size_text=self._format_size(size),
            details_text=_('Size: ') + self._format_size(size) + '\n' +
            _('Partitioning: ') +
            (item['parttable'] if 'parttable' in item and item['parttable']
             is not None else _('unknown')),
            ops=['autosetup', 'parttable']
        ))

    def _add_free_space_to_menu(
            self, menu, pref, parent_name, start, end, *args, **kwargs):

        gran_start = self._to_gran_start(start)
        gran_end = self._to_gran_end(end)
        if gran_start <= 0:
            gran_start = 1
        if gran_end < gran_start:
            return

        start_str = str(gran_start) + self.part_granularity_unit
        end_str = str(gran_end) + self.part_granularity_unit

        menu.append(PartitionMenuItem(
            name='*' + parent_name + '*' + start_str + '*' + end_str,
            size=end - start + 1,
            text=pref + _('Free Space'),
            size_text=self._format_size(end - start + 1),
            details_text=_('Size: ') + self._format_size(end - start + 1) + '\n' +
            _('Start: ') + str(start) + '\n' +
            _('End: ') + str(end),
            ops=['part']
        ))

    def _add_other_to_menu(self, menu, pref, name, item, size, *args, **kwargs):
        menu.append(PartitionMenuItem(
            name=name,
            size=size,
            text=pref + name,
            size_text=self._format_size(size),
            details_text=_('Size: ') + self._format_size(size) + '\n' +
            _('Filesystem: ') +
            (item['fstype'] if 'fstype' in item and item['fstype']
             is not None else _('unknown')),
            ops=['format', 'boot-target', 'install-target',
                 'swap-target', 'cryptclose', 'swap-cryptclose']
        ))

    def _add_options_to_menu(self, menu, *args, **kwargs):
        menu.append(PartitionMenuItem(
            name='*show-targets',
            text=_('Show Configured Targets'),
            details_text=_('Boot target: ') + self.boot_target + '\n' +
            _('Main encryption target: ') + self.crypt_target + '\n' +
            _('Main installation target: ') + self.install_target + '\n' +
            _('Swap encryption target: ') + self.swap_crypt_target + '\n' +
            _('Swap target: ') + self.swap_target,
            ops=['clear-boot-target', 'clear-crypt-target',
                 'clear-install-target', 'clear-swap-crypt-target',
                 'clear-swap-target']
        ))
        menu.append(PartitionMenuItem(
            name='*refresh',
            text=_('Refresh'),
            ops=['refresh']
        ))

    def _add_part_to_menu(self, menu, pref, name, item, size, *args, **kwargs):
        menu.append(PartitionMenuItem(
            name=name,
            size=size,
            text=pref + name,
            size_text=self._format_size(size),
            details_text=_('Size: ') + self._format_size(size) + '\n' +
            _('Filesystem: ') +
            (item['fstype'] if 'fstype' in item and item['fstype'] is not None else _('unknown')) + '\n' +
            _('Flags: ') +
            (item['flags'] if 'flags' in item and item['flags'] is not None else '') + '\n' +
            _('Start: ') +
            (item['start'] if 'start' in item and item['start'] is not None else _('unknown')) + '\n' +
            _('End: ') +
            (item['end'] if 'end' in item and item['end']
             is not None else _('unknown')),
            ops=['boot', 'format', 'boot-target', 'install-target',
                 'swap-target', 'remove', 'cryptsetup', 'cryptopen',
                 'swap-cryptopen']
        ))

    def _exec(self, cmd, msg='', **kwargs):
        if not gui.started:
            ai_dialog_exec(cmd, msg=msg, **kwargs)
        else:
            from partition_gui import partition_gui
            p = ai_popen(
                cmd,
                stdin=subprocess.DEVNULL,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                universal_newlines=True
            )
            partition_gui.add_log_lib(msg)
            partition_gui.add_log_lib('- ' + cmd)
            for x in p.stdout:
                partition_gui.add_log_lib(x.rstrip('\n'))

    def _format_size(self, size):
        units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
        for x in units[:-1]:
            if size < self.base:
                return str(size) + ' ' + x
            size //= self.base
        return str(size) + ' ' + units[-1]

    def _get_layout_menu_noscan(self, parts=None, parent=None, level=0):
        if parts is None:
            parts = self.parts

        l = []
        last = -1
        pref = '    ' * level
        has_special_type = False

        for x in parts:
            start = 0
            if 'start' in x and x['start'] is not None:
                start = int(x['start'])

            if start > last + 1:
                self._add_free_space_to_menu(menu=l, pref=pref,
                                             parent_name=parent['name'], start=last + 1,
                                             end=start - 1)

            add_to_menu_func = self._add_other_to_menu
            if x['type'] == 'disk':
                add_to_menu_func = self._add_disk_to_menu
            elif x['type'] == 'part':
                add_to_menu_func = self._add_part_to_menu
            else:
                has_special_type = True

            add_to_menu_func(
                menu=l, pref=pref, name=x['name'], item=x,
                size=int(x['size']) if 'size' in x else 0)

            if 'children' in x:
                l += self._get_layout_menu_noscan(
                    parts=x['children'], parent=x, level=level + 1)
            elif level == 0 and 'parttable' in x and x['parttable'] is not None \
                    and x['parttable'] != 'unknown':
                # always consider a toplevel with a known partition table
                # to possibly have free space
                l += self._get_layout_menu_noscan(
                    parts=[], parent=x, level=level + 1)

            if 'end' in x:
                last = max(last, int(x['end']))

        if parent is not None and 'size' in parent and not has_special_type:
            parent_size = int(parent['size'])
            if parent_size > last + 1:
                self._add_free_space_to_menu(menu=l, pref=pref,
                                             parent_name=parent['name'], start=last + 1,
                                             end=parent_size - 1)

        if level == 0:
            self._add_options_to_menu(menu=l)

        return l

    def _msgbox(self, text):
        if not gui.started:
            dialog.msgbox(text)
        else:
            event = threading.Event()

            def show():
                from gi.repository import Gtk
                dialog2 = Gtk.MessageDialog(
                    gui.window,
                    Gtk.DialogFlags.DESTROY_WITH_PARENT,
                    Gtk.MessageType.INFO,
                    Gtk.ButtonsType.CLOSE,
                    text
                )
                dialog2.run()
                dialog2.destroy()
                event.set()
            gui.idle_add(show)
            event.wait()

    def _to_gran_end(self, x):
        return x // self.part_granularity

    def _to_gran_start(self, x):
        return (x + self.part_granularity - 1) // self.part_granularity

    def _convert_start_end_to_str(self, p):
        for x in p:
            if 'children' in x:
                self._convert_start_end_to_str(x['children'])
            if 'start' in x and isinstance(x['start'], int):
                x['start'] = str(x['start'])
            if 'end' in x and isinstance(x['end'], int):
                x['end'] = str(x['end'])

    def _remove_toplevel_mounted(self, p):
        rm = []
        for x in p:
            if 'mountpoint' in x and x['mountpoint'] is not None:
                rm.append(x)
        for x in rm:
            p.remove(x)

    def _scan_for_details(self, p):
        for x in p:
            if 'children' in x:
                self._scan_for_details(x['children'])

            __, layout = ai_call(
                'parted -m -s \'' + x['name'] + '\' unit B print')
            # sometimes parted returns error, but with useful information

            layout = layout.decode('utf-8').split('\n')

            if len(layout) <= 1:
                continue
            info = layout[1].split(':')
            if len(info) >= 6:
                x['parttable'] = info[5]

            if 'children' not in x or (not x['children']):
                continue

            if len(layout) <= 2:
                continue
            layout = layout[2:]

            part_map = {}
            for y in layout:
                if not y:
                    continue
                info = y[:-1].split(':')
                if len(info) < 7:
                    continue
                if (not info[1]) or (not info[2]):
                    continue
                part = self.get_part_from_disk_num(x['name'], info[0])
                info[1] = info[1][:-1]
                info[2] = info[2][:-1]
                part_map[part] = (info[1], info[2], info[6])

            for y in x['children']:
                name = y['name']
                if name in part_map:
                    y['start'], y['end'], y['flags'] = part_map[name]

    def _sort_parts(self, parts):
        parts.sort(key=lambda x: 0 if ('start' not in x or x['start'] is None) else int(x['start']))

        for x in parts:
            if 'children' in x:
                self._sort_parts(x['children'])

    def _scan_for_all_disks(self):
        ret, p = ai_call('lsblk -b -J -O -p')
        if ret != 0:
            raise RuntimeError('lsblk failed')
        p = json.loads(p)['blockdevices']
        self._convert_start_end_to_str(p)
        self._remove_toplevel_mounted(p)
        self._scan_for_details(p)
        self._sort_parts(p)
        self.parts = p
        self.scanned = True

    def action(self, op, name, *args, **kwargs):
        if op == 'autosetup':
            self._action_autosetup(name, *args, **kwargs)
        elif op == 'parttable':
            self._action_parttable(name, *args, **kwargs)
        elif op == 'part':
            self._action_part(name, *args, **kwargs)
        elif op == 'format':
            self._action_format(name, *args, **kwargs)
        elif op == 'boot':
            self._action_boot(name, *args, **kwargs)
        elif op == 'boot-target':
            self._action_boot_target(name, *args, **kwargs)
        elif op == 'install-target':
            self._action_install_target(name, *args, **kwargs)
        elif op == 'swap-target':
            self._action_swap_target(name, *args, **kwargs)
        elif op == 'remove':
            self._action_remove(name, *args, **kwargs)
        elif op == 'cryptsetup':
            self._action_cryptsetup(name, *args, **kwargs)
        elif op == 'cryptopen':
            self._action_cryptopen(name, *args, **kwargs)
        elif op == 'cryptclose':
            self._action_cryptclose(name, *args, **kwargs)
        elif op == 'swap-cryptopen':
            self._action_swap_cryptopen(name, *args, **kwargs)
        elif op == 'swap-cryptclose':
            self._action_swap_cryptclose(name, *args, **kwargs)
        elif op == 'clear-boot-target':
            self._action_clear_boot_target(name, *args, **kwargs)
        elif op == 'clear-crypt-target':
            self._action_clear_crypt_target(name, *args, **kwargs)
        elif op == 'clear-install-target':
            self._action_clear_install_target(name, *args, **kwargs)
        elif op == 'clear-swap-crypt-target':
            self._action_clear_swap_crypt_target(name, *args, **kwargs)
        elif op == 'clear-swap-target':
            self._action_clear_swap_target(name, *args, **kwargs)
        elif op == 'refresh':
            self._action_refresh(name, *args, **kwargs)

    def get_disk_from_part(self, name):
        i = len(name)
        if i == 0:
            return ''
        i -= 1
        while i >= 0 and name[i].isdigit():
            i -= 1
        if name.startswith('/dev/nvme'):
            return name[:i]
        return name[:i + 1]

    def get_layout_menu(self, scan=False):
        if scan:
            self.scan()
        return self._get_layout_menu_noscan()

    def get_num_from_part(self, name):
        i = len(name)
        if i == 0:
            return 0
        i -= 1
        if not name[i].isdigit():
            return 0
        while i >= 0 and name[i].isdigit():
            i -= 1
        return int(name[i + 1:])

    def get_part_from_disk_num(self, disk, num):
        if disk.startswith('/dev/nvme'):
            return disk + 'p' + str(num)
        return disk + str(num)

    def get_text_for_op(self, op):
        if op == 'autosetup':
            return _('Automatically Set Up This Disk')
        if op == 'parttable':
            return _('Create/Rewrite Partition Table')
        if op == 'part':
            return _('Create New Partition')
        if op == 'format':
            return _('Format Selected Partition')
        if op == 'boot':
            return _('Toggle Boot Flag')
        if op == 'boot-target':
            return _('Select as Boot Target')
        if op == 'install-target':
            return _('Select as Main Installation Target')
        if op == 'swap-target':
            return _('Select as Swap Target')
        if op == 'remove':
            return _('Remove Selected Partition')
        if op == 'cryptsetup':
            return _('Set Up Encryption')
        if op == 'cryptopen':
            return _('Open Main Encrypted Partition')
        if op == 'cryptclose':
            return _('Close Main Encrypted Block')
        if op == 'swap-cryptopen':
            return _('Open Swap Encrypted Partition')
        if op == 'swap-cryptclose':
            return _('Close Swap Encrypted Block')
        if op == 'clear-boot-target':
            return _('Clear Boot Target')
        if op == 'clear-crypt-target':
            return _('Clear Main Encryption Target')
        if op == 'clear-install-target':
            return _('Clear Main Installation Target')
        if op == 'clear-swap-crypt-target':
            return _('Clear Swap Encryption Target')
        if op == 'clear-swap-target':
            return _('Clear Swap Target')
        if op == 'refresh':
            return _('Refresh Partition List')
        return op

    def scan(self):
        self._scan_for_all_disks()

partition_lib = PartitionLib()

{'partition_gui': 'partition_gui.partition_gui', 'Gtk': 'gi.repository.Gtk'}


import logging
import numpy as np
from itertools import *
from tabulate import tabulate

import cwi
import utils

def setup_logger(args):
    import socket
    host = socket.gethostname().split('.')[0]
    logger = logging.getLogger()
    logger.setLevel(logging.CRITICAL)
    shandler = logging.StreamHandler()
    shandler.setLevel(logging.CRITICAL)
    logger.addHandler(shandler);

    if len(args['log']) > 0 and args['log'] != 'dont':
        ihandler = logging.FileHandler('logs/{}.log'.format(args['log']), mode='w')
        ihandler.setLevel(logging.CRITICAL)
        logger.addHandler(ihandler);

C_powers = [-20,+8]
C_values = np.power(np.zeros(sum(map(abs,C_powers)))+2, np.arange(*C_powers))

def opt(dset, emb, embs, e_context):
    dargs = {'e_context' : 0, 'feats' : '', 'percentile' : 0, 'n_fold' : 5,
            'cweights' : 1, 'clf' : 'svm', 'kerntype' : 'lin', 'C' : 1, 'kerngamma' : 1, 'kerncoef0' : 1, 'kerndegree' : 1}
    dargs['e_context'] = e_context
    dargs['embs'] = embs
    f1,f1std,C = c_opt(dset, emb, dargs)
    return {'e_context' :e_context, 'embs':' '.join(embs), 'f1':f1,'f1std':f1std, 'C':C}

def c_opt(dset, emb, dargs):
    logging.critical(tabulate([dargs],headers='keys'))
    f1s = []
    for C in C_values:
        targs = dargs.copy()
        targs['C'] = C
        f1, f1std = cwi.xvalidate(dset, targs, emb)
        f1s.append((f1,f1std,C))
        logging.critical('{}\t{}\t{}'.format(C, f1,f1std))
    logging.critical('\n')
    return max(f1s)

if __name__ == '__main__':
    setup_logger({'log':'road'})

    dset = utils.get_dset()

    emb = cwi.Emb(dset)

    infolist = []
    infolist.append(opt(dset,emb,['r50'],0))

    """
    for ec in range(4):
        infolist.extend(opt(dset,emb,['{}{}'.format(e,dim)],ec) for e,dim in product(['s','g'],[50,100,200]))
        # infolist.extend(opt(dset,emb,ename,ec) for ename in [['s50']])

    for ec in range(4):
        infolist.extend(opt(dset,emb,[e1,e2],ec) for e1,e2 in product(['s50','s100','s200'],['g50','g100','g200']))
    """

    logging.critical(tabulate(infolist,headers='keys'))


import mimetypes
from typing import NoReturn, BinaryIO
from os import path

import boto3
from botocore.exceptions import ClientError

from .Singleton import Singleton
from .exceptions import \
    EnvironmentIncorrect, \
    WorkerDoesNotStarted, \
    UploadFileFailed, \
    CantGetFile, \
    CantRemoveFile, \
    CantRemoveBucket, \
    NoSuchBucket, \
    BucketExist

from .config import \
    MINIO_ACCESS_KEY, \
    MINIO_SECRET_KEY, \
    MINIO_ENDPOINT_URL, \
    MINIO_EXPIRES_FILE_LINK_IN_SECONDS

class File:
    filename: str
    mimetype: str
    data: BinaryIO

    def __init__(self,
                 filename: str = None,
                 mimetype: str = None,
                 data: BinaryIO = None,
                 filepath: str = None):

        if filepath:
            mimetype = mimetype or mimetypes.guess_type(filepath)[0]
            filename = filename or path.basename(filepath)
            data = data or open(filepath, "rb")

        self.mimetype = mimetype
        self.filename = filename
        self.data = data

class S3Worker(Singleton):
    S3 = "s3"

    ENVIRONMENT_LIST = [
        MINIO_SECRET_KEY,
        MINIO_ACCESS_KEY,
        MINIO_ENDPOINT_URL
    ]

    def __init__(self):
        """
        Initializing the s3 client
        """
        for item in self.ENVIRONMENT_LIST:
            if not item:
                raise EnvironmentIncorrect
        self.s3 = boto3.client(
            self.S3,
            endpoint_url=MINIO_ENDPOINT_URL,
            aws_access_key_id=MINIO_ACCESS_KEY,
            aws_secret_access_key=MINIO_SECRET_KEY
        )
        if not self.s3:
            raise WorkerDoesNotStarted

    def new_bucket(self, bucket_name: str) -> NoReturn:
        """
        Creating the bucket
        :param bucket_name: name of the bucket
        """
        try:
            self.s3.create_bucket(Bucket=bucket_name)
        except ClientError:
            raise BucketExist

    def upload_file(self, bucket_name: str, file: File) -> str:
        """
        Uploading a file to bucket
        :param bucket_name: name of the bucket, where the file will be uploaded
        :param file: file, which would be uploaded
        """
        try:
            self.s3.upload_fileobj(
                file.data,
                bucket_name,
                file.filename,
                ExtraArgs={"ContentType": file.mimetype}
            )
            #import time
            #time.sleep(1)
            return self.s3.generate_presigned_url(
                "get_object",
                Params={"Bucket": bucket_name, "Key": file.filename},
                ExpiresIn=MINIO_EXPIRES_FILE_LINK_IN_SECONDS
            )
        except ClientError:
            raise UploadFileFailed

    def get_file_url(self, bucket_name: str, file_name: str):
        """
        :param bucket_name: name of the bucket, where the file will be uploaded
        :param file_name: name of the file
        """
        try:
            url = self.s3.generate_presigned_url(
                ClientMethod="get_object",
                Params={"Bucket": bucket_name, "Key": file_name},
                ExpiresIn=MINIO_EXPIRES_FILE_LINK_IN_SECONDS
            )
            return url
        except ClientError:
            raise CantGetFile

    def remove_file(self, bucket_name: str, file_name: str) -> NoReturn:
        """
        :param bucket_name: name of the bucket, where the file will be deleted
        :param file_name: name of the file
        """
        try:
            self.s3.delete_object(Bucket=bucket_name, Key=file_name)
        except ClientError:
            raise CantRemoveFile

    def remove_bucket(self, bucket_name: str) -> NoReturn:
        """
        :param bucket_name: name of the bucket to delete
        """
        try:
            for file_name in self.list_files(bucket_name):
                self.remove_file(bucket_name, file_name)

            self.s3.delete_bucket(Bucket=bucket_name)
        except ClientError:
            raise CantRemoveBucket

    def list_files(self, bucket_name: str) -> list[str]:
        """
        Return all objects of the bucket
        :param bucket_name: name of the bucket, where the file will be uploaded
        """
        try:
            objects = self.s3.list_objects(Bucket=bucket_name)

            if "Contents" in objects:
                return list(map(lambda x: x["Key"], objects["Contents"]))
            return []
        except ClientError:
            raise NoSuchBucket


import pytest
from django_swagger_utils.drf_server.exceptions import NotFound

from resource_management.presenters.presenter_implementation import \
    PresenterImplementation
from resource_management.constants.exception_message import \
    USER_DOES_NOT_EXISTS

def test_username_not_exist_raise_exception():

    #Arrange
    exception_message = USER_DOES_NOT_EXISTS[0]
    exception_response_status = USER_DOES_NOT_EXISTS[1]
    presenter = PresenterImplementation()

    #Act
    with pytest.raises(NotFound) as exception:
        presenter.username_not_exists()

    #Assert
    assert exception.value.message == exception_message
    assert exception.value.res_status == exception_response_status


# Thanks to Sipak bro and Aryan..
# animation Idea by @ItzSipak && @Hell boy_pikachu
# Made by @hellboi_atul ....and thanks to @Crackexy for the logos...
# Kang with credits else gay...
import asyncio
import os
import requests
import time
from PIL import Image
from io import BytesIO
from datetime import datetime
import random
from telethon import events, version
from userbot.utils import admin_cmd, sudo_cmd
from userbot import ALIVE_NAME, Lastupdate
from . import dcdef
from telethon.tl.types import ChannelParticipantsAdmins
# 🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
DEFAULTUSER = str(ALIVE_NAME) if ALIVE_NAME else "𝙲𝙾𝙵𝙵𝙸𝙽 𝚇 𝚄𝚂𝙴𝚁"

# Thanks to Sipak bro and Aryan..
# animation Idea by @ItzSipak && @Hell boy_pikachu
# Made by @hellboi_atul ....and thanks to @Crackexy for the logos...
# Kang with credits else gay...
# alive.py for DC(DARK COBRA)
# mod For Coffin With Credit
global ghanti
ghanti = borg.uid
edit_time = 5
""" =======================CONSTANTS====================== """
file1 = "https://telegra.ph/file/37ee8db6262b2ea00dbcf.jpg"
""" =======================CONSTANTS====================== """

@borg.on(admin_cmd(pattern=r"alive"))
@borg.on(sudo_cmd(pattern=r"alive", allow_sudo=True))

async def hmm(yes):
    chat = await yes.get_chat()
    global ghanti
    ghanti = borg.uid
    await yes.delete()
    uptime = await dcdef.get_readable_time((time.time() - Lastupdate))
    pm_caption = "** 𝙲𝙾𝙵𝙵𝙸𝙽 𝚇 𝙸𝚂 𝙾𝙽𝙻𝙸𝙽𝙴 **\n\n"
    pm_caption += "**Am Alive And Systems Are Working Perfectly As It Should Be...**\n\n"
    pm_caption += "✖ About My System ✖\n\n"
    pm_caption += f"➜ **𝙏𝙀𝙇𝙀𝙏𝙃𝙊𝙉 𝙑𝙀𝙍𝙎𝙄𝙊𝙉** ☞ {version.__version__}\n"
    pm_caption += "➜ **𝙎𝙐𝙋𝙋𝙊𝙍𝙏 𝘾𝙃𝘼𝙉𝙉𝙀𝙇** ☞ [ᴊᴏɪɴ](https://t.me/CoffinX_Updates)\n"
    pm_caption += "➜ **𝙇𝙄𝘾𝙀𝙉𝙎𝙀**  ☞ [𝚃𝙴𝙰𝙼 𝙲𝙾𝙵𝙵𝙸𝙽](https://github.com/theshashankk)\n"
    pm_caption += "➜ **𝘾𝙊𝙋𝙔𝙍𝙄𝙂𝙃𝙏 𝘽𝙔** ☞ [𝙲𝙾𝙵𝙵𝙸𝙽 𝚇 𝚄𝙱](https://github.com/theshashankk/CoffinX-UB)\n\n"
    pm_caption += f"➜ **𝘾𝙊𝙁𝙁𝙄𝙉 𝙐𝙋𝙏𝙄𝙈𝙀** ☞ {uptime}\n\n"
    pm_caption += f"➜ **𝙈𝙔 𝙋𝙀𝙍𝙊 𝙈𝘼𝙎𝙏𝙀𝙍** ☞ [{DEFAULTUSER}](tg://user?id={ghanti})\n"
    on = await borg.send_file(yes.chat_id, file=file1,caption=pm_caption)

    await asyncio.sleep(edit_time)
    ok = await borg.edit_message(yes.chat_id, on, file=file2)

    await asyncio.sleep(edit_time)
    ok2 = await borg.edit_message(yes.chat_id, ok, file=file3)

    await asyncio.sleep(edit_time)
    ok3 = await borg.edit_message(yes.chat_id, ok2, file=file1)

    await asyncio.sleep(edit_time)
    ok4 = await borg.edit_message(yes.chat_id, ok3, file=file3)

    await asyncio.sleep(edit_time)
    ok5 = await borg.edit_message(yes.chat_id, ok4, file=file2)

    await asyncio.sleep(edit_time)
    ok6 = await borg.edit_message(yes.chat_id, ok5, file=file1)

    await asyncio.sleep(edit_time)
    ok7 = await borg.edit_message(yes.chat_id, ok6, file=file4)