"""
Evaluates Places365 models on a given directory of images.
"""

from __future__ import print_function

import argparse
import os
import pickle
from pprint import pprint
import time
import random

import numpy as np
import scipy.io as sio

import torch
import torch.nn as nn
import torch.nn.parallel
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import torchvision.models as models

import torch.nn.functional as F

from utils import Bar, Logger, AverageMeter, accuracy, mkdir_p, savefig

import sys

# Parse arguments
parser = argparse.ArgumentParser(description='Evaluation of Places365 models')

parser.add_argument('-o', '--output_dir', type=str, required=True, help='path to output passive attention heatmaps')
parser.add_argument('-m', '--method', type=str, required=True, help='passive attention method (guidedbp, guidedbpximage, smoothgradguidedbp, gradcam, scorecam, cameras)')

# Datasets
parser.add_argument('-d', '--data', type=str, required=True, help='path to dataset',)
parser.add_argument('-j', '--workers', default=1, type=int, metavar='N',
                    help='number of data loading workers (default: 1)')
# Architecture
parser.add_argument('--arch', '-a', metavar='ARCH', default='resnet')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
        help='path to latest checkpoint (default: none)')
# Miscs
parser.add_argument('--manualSeed', type=int, help='manual seed')

args = parser.parse_args()
state = {k: v for k, v in args._get_kwargs()}

# CUDA
#os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
#use_cuda = torch.cuda.is_available()
use_cuda = False
device = torch.device("cuda" if use_cuda else "cpu")

# Random seed
if args.manualSeed is None:
    args.manualSeed = random.randint(1, 10000)
random.seed(args.manualSeed)
torch.manual_seed(args.manualSeed)
if use_cuda:
    torch.cuda.manual_seed_all(args.manualSeed)

def main():
    # Data loading code
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])  # places normalization
    transform = transforms.Compose([
        transforms.Resize(224),  # For input into places models
        transforms.ToTensor(),
        normalize,
    ])

    dataset = datasets.ImageFolder(root=args.data, transform=transform)
    loader = torch.utils.data.DataLoader(dataset,
                                         batch_size=1, shuffle=False,
                                         num_workers=args.workers, pin_memory=True)

    # Model
    print("==> creating model '{}'".format(args.arch))

    if args.arch == "resnet50":
        sys.path.insert(0, './pytorch-cnn-visualizations-modified/src/')
        model = models.resnet50(num_classes=365)
    elif args.arch == "alexnet":
        sys.path.insert(0, './pytorch-cnn-visualizations/src/')
        model = models.alexnet(num_classes=365)
    else:
        raise Exception("Invalid architecture chosen")

    if use_cuda:
        model = model.cuda()
    else:
        model = model.cpu()
    print('    Total params: %.2fM' % (sum(p.numel() for p in model.parameters())/1000000.0))

    if args.resume:
        # Load checkpoint.
        print('==> Resuming from checkpoint..')
        assert os.path.isfile(args.resume), 'Error: no checkpoint directory found!'
        args.checkpoint = os.path.dirname(args.resume)
        checkpoint = torch.load(args.resume, map_location=torch.device('cpu'))
        state_dict = {str.replace(k,'module.',''): v for k,v in checkpoint['state_dict'].items()}
        model.load_state_dict(state_dict)
    else:
        raise Exception("Must provide --resume")

    print(model)

    batch_time = AverageMeter()

    # switch to evaluate mode
    model.eval()

    end = time.time()
    bar = Bar('Processing', max=len(loader))
    for i, (inputs, _) in enumerate(loader):
        if use_cuda:
            inputs = inputs.cuda()
        inputs = torch.autograd.Variable(inputs, requires_grad=True)

        targets = model(inputs).argmax()
        last_conv_layer = None

        if args.method == "guidedbp":
            from guided_backprop import GuidedBackprop
            gb = GuidedBackprop(model)
            the_map = gb.generate_gradients(inputs, targets)
            the_map = np.moveaxis(the_map, 0, -1)
        elif args.method == "guidedbpximage":
            from guided_backprop import GuidedBackprop
            gb = GuidedBackprop(model)
            the_map = gb.generate_gradients(inputs, targets)
            the_map *= inputs.detach().numpy()[0]
            the_map = np.moveaxis(the_map, 0, -1)
        elif args.method == "smoothgradguidedbp":
            from guided_backprop import GuidedBackprop
            from smooth_grad import generate_smooth_grad
            gb = GuidedBackprop(model)
            the_map = generate_smooth_grad(gb, inputs, targets, 30, 0.1)
            the_map = np.moveaxis(the_map, 0, -1)
        elif args.method == "gradcam":
            from gradcam import GradCam
            if args.arch == "alexnet":
                last_conv_layer = len(model.features._modules.items()) - 2
            else:
                last_conv_layer = list(model._modules.keys())[-3]
            gc = GradCam(model, last_conv_layer)
            the_map = gc.generate_cam(inputs, targets)
        elif args.method == "scorecam":
            from scorecam import ScoreCam
            if args.arch == "alexnet":
                last_conv_layer = len(model.features._modules.items()) - 2
            else:
                last_conv_layer = list(model._modules.keys())[-3]
            sc = ScoreCam(model, last_conv_layer)
            the_map = sc.generate_cam(inputs, targets)
        elif args.method == "cameras":
            sys.path.insert(0, './CAMERAS/')
            from CAMERAS import CAMERAS
            if args.arch == "alexnet":
                last_conv_layer = "features." + list(model.features._modules.keys())[-2]
            else:
                last_conv_layer = list(model._modules.keys())[-3]
            cameras = CAMERAS(model, last_conv_layer)
            the_map = cameras.run(inputs, targets).cpu().numpy()
        else:
            raise Exception("Invalid passive attention method")

        if the_map.ndim == 3:
            # Convert to 2D
            the_map = np.sum(np.abs(the_map), axis=2)

        name = os.path.basename(dataset.imgs[i][0])
        num = int(name[name.index("r_")+2:-4])
        subdir = os.path.join(args.output_dir, f"img{num}/")
        if not os.path.exists(subdir):
            os.mkdir(subdir)
        arch = args.arch.replace("_", "")
        if last_conv_layer is None:
            fn = f"img{num}_network_{arch}_method_{args.method}.mat"
        else:
            fn = f"img{num}_network_{arch}_method_{args.method}_selector_{last_conv_layer}.mat"
        outpath = os.path.join(subdir, fn)
        data = {args.method: the_map}
        sio.savemat(outpath, data)

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        # plot progress
        bar.suffix  = '({batch}/{size}) Batch: {bt:.3f}s | Total: {total:} | ETA: {eta:}'.format(
                    batch=i + 1,
                    size=len(loader),
                    bt=batch_time.avg,
                    total=bar.elapsed_td,
                    eta=bar.eta_td,
                    )
        bar.next()
    bar.finish()

if __name__ == '__main__':
    main()


#!/usr/bin/python3
# CLIent: Barebones user client for Amoskeag
#
# Commands:
# look: Lists everything you can look at (stuff in stage, and your inventory)
# look <at>: Looks at <at>
# move: Lists where you can move to
# move <dir>: Moves to that location
# talk: List every possible talk command
# talk <cmd>: Talks to that person, or says that response if already talking
# get: List everything you can get
# get <obj>: Gets that object
# status: Lists your current inventory
# Blank command: Displays room info again
# exit: Quits

import requests
import json
import time

server = 'http://localhost:9999'

def get_token():
  global server
  r = requests.get(server + '/users/token').json()
  if ('success' in r):
    return r['token']
  else:
    raise IOError

class Game():
  def __init__(self, token):
    self.token = token
    self.inventory = []

  def _get(self, url):
    r = requests.get(server + url, headers = {
      'Authorization': 'Bearer ' + self.token
    }).json()
    if ('success' in r):
      if (r["success"] == True):
        return r["output"]
      else:
        raise IOError(r["message"])
    else:
      raise IOError()

  def _post(self, url, data):
    r = requests.post(server + url, headers = {
      'Authorization': 'Bearer ' + self.token
    }, json=data).json()
    if ('success' in r):
      if ('output' in r):
        return r['output']
      if ('message' in r):
        raise IOError(r["message"])
    raise IOError(str(r))

  def __update(self, r):
    out = r["desc"]
    if ("exit" in r):
      self.exits = r["exit"]
      if (len(self.exits) != 0):
        out = out + "\n\nExits are " + ','.join([e["label"] for e in self.exits])

    if ("talk" in r):
      self.talks = r["talk"]
      if (len(self.talks) != 0):
        say = []
        opt = 'abcdefghijklmnopqr'
        for idx, e in enumerate(r["talk"]):
          e["say"] = e["label"]
          e["label"] = opt[idx]

    if ("look" in r):
      self.looks = r["look"]
      self.looks.extend(
        ({"label" : e["label"],
          "id" : e["look"]} for e in self.inventory)
      )

    if ("get" in r):
      self.gets = r["get"]

    if ("endgame" in r):
      self.talks = []
      self.exits = []
      self.looks = []
      self.gets = []
      return r["desc"] + "\n\n **** GAME OVER ****"

    if ("title" in r):
      out = r["title"] + "\n\n" + out
    return out

  def __say(self, data):
    labels = [e["label"] for e in data]
    return ', '.join(labels)

  def __gopost(self, data, dest, label):
    try:
      target = next(i["id"] for i in data if i["label"] == dest)
    except StopIteration:
      return "Can't find " + dest
    r = self._post('/game/'+label, {"id": target})
    if (label is "move"):
      r = self._get('/game/look')
    return self.__update(r)

  def look(self):
    r = self._get('/game/look')
    return self.__update(r)

  def talk(self):
    out = ""
    if (len(self.talks) != 0):
      say = []
      opt = 'abcdefghijklmnopqr'
      for idx, e in enumerate(self.talks):
        say.append(opt[idx] + ") " + e["say"])
      out = '\n'.join(say)
    return out

  def lookat(self):
    return self.__say(self.looks)

  def get(self):
    return self.__say(self.gets)

  def move(self):
    return self.__say(self.exits)

  def talk_to(self, to):
    out = self.__gopost(self.talks, to, "talk")
    if (len(self.talks) != 0):
      out = out + "\n\n" + self.talk()
    return out

  def lookat_to(self, to):
    out = self.__gopost(self.looks, to, "look")
    if (len(self.talks) != 0):
      out = out + "\n\n" + self.talk()
    return out

  def get_to(self, to):
    out = self.__gopost(self.gets, to, "get")
    self.look()
    return out

  def move_to(self, to):
    return self.__gopost(self.exits, to, "move")

  def use(self, id, on):
    try:
      item = next(i["id"] for i in self.looks if i["label"] == id)
      target = next(i["id"] for i in self.looks if i["label"] == on)
    except StopIteration:
      return "Can't do that"

    r = self._post('/game/use', {"id":item, "target":target})
    return self.__update(r)

  def consume(self, to):
    return self.__gopost(self.looks, to, "use")

  def status(self):
    r = self._get('/game/status')
    self.inventory = r["inventory"]
    return "Inventory: " + ", ".join([e["label"] for e in r["inventory"]])

  def save(self):
    return str(self._get('/game/save'))

  def saves(self):
    return str(self._get('/game/saves'))

  def load(self, guid):
    return str(self._post('/game/load', {"id": guid}))

cond = True
game = Game(get_token())
time.sleep(0.5)
print("Connected to Amoskeag server at " + server + " with token " + game.token)
print(game.look())
while(cond):
  text = input('$ ')
  try:
    if (text == "exit"):
      cond = False
      print("Thanks for playing!")
    elif (text == "talk"):
      print(game.talk())
    elif (text == "look"):
      print(game.lookat())
    elif (text == "move"):
      print(game.move())
    elif (text == "get"):
      print(game.get())
    elif (text == "save"):
      print(game.save())
    elif (text == "saves"):
      print(game.saves())
    elif (text.startswith("load ")):
      print(game.load(text[5:]))
    elif (text.startswith("look ")):
      print(game.lookat_to(text[5:]))
    elif (text.startswith("talk ")):
      print(game.talk_to(text[5:]))
    elif (text.startswith("move ")):
      print(game.move_to(text[5: ]))
    elif (text.startswith("get ")):
      print(game.get_to(text[4: ]))
      game.status()
    elif (text.startswith("status")):
      print(game.status())
    elif (text.startswith("use ")):
      cmd = text[4:].split(" on ")
      if (len(cmd) < 2):
        print(game.consume(cmd[0]))
      else:
        print(game.use(cmd[0], cmd[1]))
    elif (text == ""):
      print(game.look())
    else:
      print("I don't understand that command.")
  except IOError as e:
    print("Error: " + str(e))


import itertools
from flask import Flask, app, request, jsonify
import json
from itertools import permutations
from itertools import combinations
import os
from flask_cors import CORS, cross_origin

# Importing Modules

import flask
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/index', methods=['GET', 'POST'])
@cross_origin()
def index():

    value = request.json
    # value
    a = value["Slot"]

    Crc = list(value["CourseList"])

    MorE = a
    NoC = len(list((Crc)))
    LC = []
    LL = []

    for i in range(NoC):
        c = Crc[i]
        LC.append(c)

    with open("WinterSem22Updated1.json") as json_file:
        data = json.load(json_file)

    for i in data:
        if i['slot'].count('L') > 3 and i['crcode'] in LC:
            LL.append(i['crcode'])
            LC.remove(i['crcode'])

    A1, B1, C1, D1, E1, F1, G1 = [], [], [], [], [], [], []
    A2, B2, C2, D2, E2, F2, G2 = [], [], [], [], [], [], []

    MS = ['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1']
    ES = ['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2']

    LabCourses = []
    LabCount = 0
    LabOnlyCourses = []

    for Course in data:
        if MorE == "Morning":
            if Course['crcode'] in LC or Course['crcode'] in LL:
                if 'A1' in Course['slot']:
                    A1.append(Course)
                if 'B1' in Course['slot']:
                    B1.append(Course)
                if 'C1' in Course['slot']:
                    C1.append(Course)
                if 'D1' in Course['slot']:
                    D1.append(Course)
                if 'E1' in Course['slot']:
                    E1.append(Course)
                if 'F1' in Course['slot']:
                    F1.append(Course)
                if 'G1' in Course['slot']:
                    G1.append(Course)
                if 'L' in Course['slot']:
                    val = Course['slot'][1:3]
                    if val.isdigit():
                        if int(val) in list(range(31, 61)):
                            LabCourses.append(Course)
                            if Course['slot'].count('L') > 3:
                                LabOnlyCourses.append(Course)
                                LabCount += 1

        if MorE == "Evening":
            if Course['crcode'] in LC or Course['crcode'] in LL:
                if 'A2' in Course['slot']:
                    A2.append(Course)
                if 'B2' in Course['slot']:
                    B2.append(Course)
                if 'C2' in Course['slot']:
                    C2.append(Course)
                if 'D2' in Course['slot']:
                    D2.append(Course)
                if 'E2' in Course['slot']:
                    E2.append(Course)
                if 'F2' in Course['slot']:
                    F2.append(Course)
                if 'G2' in Course['slot']:
                    G2.append(Course)
                if 'L' in Course['slot']:
                    S1 = Course['slot'][1:3]
                    S2 = Course['slot'][1:2]
                    if S1.isdigit():
                        if int(S1) in list(range(1, 31)):
                            LabCourses.append(Course)
                            LabCount += 1
                            if Course['slot'].count('L') > 3:
                                LabOnlyCourses.append(Course)
                                LabCount += 1
                    elif S2.isdigit():
                        if int(S2) in list(range(1, 31)):
                            LabCourses.append(Course)
                            LabCount += 1
                            if Course['slot'].count('L') > 3:
                                LabOnlyCourses.append(Course)
                                LabCount += 1

    LF = []

    LabCount = 0
    for i in LC:
        flage = False
        for j in data:
            if j['crcode'] == i and flage == False and 'L' in j['slot']:
                LabCount += 1
                LF.append(j['crcode'])
                flage = True

    LabCount += len(LL)
    LF = LF + LL

    Len1 = len(MS)
    Len2 = len(LC)

    for i in range(Len1-Len2):
        LC.append("NULL")

    AllCourseTracks = list(permutations(LC))  # ALL PATHS

    NewAllCourseTracks = []  # Final All paths

    n = len(AllCourseTracks)
    for i in AllCourseTracks:
        if i not in NewAllCourseTracks:
            NewAllCourseTracks.append(i)

    AllPossible = []

    if MorE == "Morning":

        for CourseTrack in NewAllCourseTracks:
            CheckList = [A1, B1, C1, D1, E1, F1, G1]
            MyList = [False for i in CourseTrack]
            CurPath = []
            count = 0

            for i, j in zip(CourseTrack, CheckList):
                if i == "NULL":
                    MyList[count] = True
                    CurPath.append("NULL")
                else:
                    if i in [m['crcode'] for m in j]:
                        val = []
                        for f in j:
                            if i == f['crcode']:
                                val.append(f)
                        MyList[count] = True
                        CurPath.append(val)
                        # print(CurPath)
                count += 1
            # print(MyList)

            if all(MyList):

                # CurPath.remove('NULL')
                Temp = []
                for i in CurPath:
                    if i != "NULL":
                        Temp.append(i)
                AllPossible.append(Temp)

    if MorE == "Evening":

        for CourseTrack in NewAllCourseTracks:
            CheckList = [A2, B2, C2, D2, E2, F2, G2]
            MyList = [False for i in CourseTrack]
            CurPath = []
            count = 0

            for i, j in zip(CourseTrack, CheckList):
                Fal = False
                if i == "NULL":
                    MyList[count] = True
                    CurPath.append("NULL")
                else:
                    if i in [m['crcode'] for m in j]:
                        val = []
                        for f in j:
                            if i == f['crcode']:
                                if i == f['crcode']:
                                    val.append(f)

                        MyList[count] = True
                        CurPath.append(val)
                count += 1

            if all(MyList):
                # CurPath.remove('NULL')
                Temp = []
                for i in CurPath:
                    if i != "NULL":
                        Temp.append(i)
                AllPossible.append(Temp)

    AllPaths = []

    for i in AllPossible:
        EachPossible = list(itertools.product(*i))
        for j in EachPossible:
            AllPaths.append(j)

    count = 0

    SuperFinalList = []

    for i in AllPaths:
        TempList = []
        for j in i:
            for k in LabCourses:
                if k['crcode'] == j['crcode'] and k['ename'] == j['ename']:
                    TempList.append(k)

        # print(TempList)

        LabCombo = list(combinations(LabOnlyCourses, len(LL)))
        for b in LabCombo:
            flage = True
            hen = []
            hen = TempList.copy()
            hen = hen + list(b)
            for l in range(len(hen)):
                for m in range(len(hen)):
                    if l != m and hen[l]['slot'] in hen[m]['slot']:
                        flage = False
            if flage == True:
                if count < 100:
                    SuperFinalList.append(list(i) + hen)
                    count += 1
                else:
                    break

# print(SuperFinalList)

    FinalAnswer = flask.jsonify({"ListOfListOfCourses": SuperFinalList})

    return FinalAnswer

if __name__ == '__main__':
    #app.run( debug = True )
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)


# #!/usr/bin/env python3
# import serial
#
# if __name__ == '__main__':
#     ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
#     ser.flush()
#
#     while True:
#        if ser.in_waiting > 0:
#
#             line = ser.readline()
#             print(line)

# import serial
# import time
# if __name__ == '__main__':
#     ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
#     ser.flush()
#     while True:
#         ser.write(b"2000\n")
#         line = ser.readline().decode('utf-8').rstrip()
#         print(line)
#         time.sleep(1)

#!/usr/bin/env python3
import serial
import time
if __name__ == '__main__':
    ser1 = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
#    ser2 = serial.Serial('/dev/ttyACM1', 9600, timeout=1)
    ser1.flush()
#    ser2.flush()
    centerX = -550;
    centerY = -500;
    centerX1= 550;
    centerY1= 500;
    while True:
        print("CenterXY")
        output = ser1.write((str(centerX) + 'x' + str(centerY) + 'y').encode('ascii'));
        #output1 = ser2.write((str(centerX1) + 'x' + str(centerY1) + 'y').encode('ascii'));
        #ser.write(centerXY.encode('ascii') + b'\n')
        #ser.write(b'2000')
        linex = ser1.readline().decode('utf-8').rstrip()
        #liney = ser2.readline().decode('utf-8').rstrip()
        print(linex)
        #print(liney)
        time.sleep(2)


from abc import abstractmethod
from typing import List;

from numpy import array as vector
from polynomialfiltering.PythonUtilities import virtual;
from polynomialfiltering.AbstractComponentFilter import AbstractComponentFilter
from polynomialfiltering.filters import ManagedFilterBase;
from polynomialfiltering.filters.controls.judge.BaseScalarJudge import BaseScalarJudge

class ManagedScalarRecursiveFilterSet(ManagedFilterBase):

    def __init__(self):
        super().__init__(None);

    '''workers : List<AbstractComponentFilter>'''
    '''SSRs    : List<float>'''
    '''judges  : List<IJudge>'''

    def setWorkers(self, workers : List[AbstractComponentFilter]) -> None:
        self.workers = workers;
        self.worker = workers[0];
        self.SSRs = [];
        self.judges = []
        for i in range(0,len(workers)) :
            self.SSRs[i] = self.SSR;
            self.judges[i] = BaseScalarJudge(self);

    def getWorkers(self) -> List[AbstractComponentFilter]:
        return self.workers;

    @virtual
    def addObservation(self, t:float, y:vector) -> bool:
        self.iR = self.errorModel.getPrecisionMatrix(self, t, y)
        minSSR = 0;
        for iW in range(0, len(self.workers)) :
            Zstar = self.workers[iW].predict(t)
            e = y[0] - Zstar[0]
            innovation = self.workers[iW].update(t, Zstar, e)
            if (self.judges[iW].scalarUpdate(t, y, e, self.iR)) :
                innovation = self.worker.update(t, Zstar, e)
                self.monitor.accepted(t, y, innovation );
                return True;
            else :
                self.monitor.rejected(t, y );
                return False;
        iBest = BaseScalarJudge.best(self.judges);
        if (iBest < 0) :
            iBest = 0;
        self.SSR = self.SSRs[iBest];
        self.worker = self.workers[iBest];


import streamlit as st
import time

## barras de progreso
st.header('st.progress')
st.caption('Display a progress bar')

def progress_bar():
    for pct_complete in range(1,121,20):
        time.sleep(0.5)
        pct_complete = min(pct_complete,100)
        # el numero de ingesta es la cantidad de progreso que lleva del [1-100]
        my_bar.progress(pct_complete)

## spinner
## poner mensaje de espera hasta que se termine de ejecuatar algo
with st.spinner("Something is processing .."):
    my_bar = st.progress(0) #esto es lo que se está ejecutando
    progress_bar()

################### mensajes de info exito o error ####################
# info color azul
st.subheader('st.info')
st.info('This is infomation message')

# success color verde
st.subheader('st.success')
st.success('This is success message')

#warning color amarillo
st.subheader('st.warning')
st.warning('This is warning')

# error color rojo
st.subheader('st.error')
st.error('this is error')

time.sleep(2)
#globitos
st.balloons()


import os
import argparse
from fm_index import FMIndex
from util import save_pickle, load_pickle, load_files, get_file_name_via_index
from flask import Flask, render_template, request, redirect, url_for

parser = argparse.ArgumentParser()
parser.add_argument('--f', type=str, metavar='PATH', help='text file')
parser.add_argument('--dir', type=str, metavar='PATH', help='text dir')
parser.add_argument('--dict', type=str, metavar='PATH', help='dict file')
parser.add_argument('--t', type=str, help='raw text')
parser.add_argument('--s', type=str, help='search text')
parser.add_argument('--d', type=str, metavar='PATH', help='dict file')
args = parser.parse_args()
app = Flask(__name__)

T, fmi, db = None, None, None

@app.route('/', methods=['GET'])
def index():
    if request.method == 'GET':
        # message = 'hi'
        pat = request.args.get('keyword')
        # request.form['keyword'])
        # return render_template('index.html', message=message)

        if pat:
            # pat = request.form['keyword']
            # pat = '日本の'
            match = fmi.search(pat)
            print('Results of search("{}")'.format(pat))
            print('match', match)
            rng = 15
            results = []
            for i, m in enumerate(match):
                beg = m[0]
                end = m[1]
                # print('(beg, end) = ({}, {})'.format(beg, end))
                f_name = get_file_name_via_index(db, beg)
                print('{} [{}]: ({}, {})'.format(i, f_name, beg, end))
                # print('---{}"{}"{}---'.format(dec[beg-rng: beg], dec[beg:end], dec[end:end+rng]))
                print('---{}"{}"{}---'.format(T[beg-rng: beg], T[beg:end], T[end:end+rng]))
                v = '{}{}{}'.format(T[beg-rng: beg], T[beg:end], T[end:end+rng])
                results.append((f_name, v))
                # print(decoded[m[0]:])
            return render_template('index.html', results=results)
        else:
            return render_template('index.html')
    else:
        # エラーなどでリダイレクトしたい場合はこんな感じで
        return redirect(url_for('index'))

def load_database():
    global T, fmi, db
    pat = args.s
    if args.dir is not None:
        T, db = load_files(args.dir)
        print('db', db)
        # print(T[:1000])
    elif args.f is not None:
        db = []
        with open(args.f, 'r') as f:
            T = ''.join(f.readlines())
            db.append((args.f, len(T)))
    else:
        T = args.t

    fmi = FMIndex()
    if args.dict and os.path.isfile(args.dict):
        saved_data = load_pickle(args.dict)
        bw = saved_data['bwt']
        fmi.set_dict(saved_data)
    else:
        print('encode text...')
        bw, sa = fmi.encode(T)
        ranks, ch_count = fmi.rank_bwt(bw)
        save_pickle({'bwt': bw, 'sa': sa, 'text_len': len(T), 'ch_count': ch_count}, 'index.dict')
        fmi.ch_count = ch_count
        print('encode done!')
    return
    # return T, fmi
    # print('decoding...')
    # dec = fmi.decode(bw)
    # print('decoding done')
    match = fmi.search(pat)
    print('Results of search("{}")'.format(pat))
    print('match', match)
    rng = 5
    for i, m in enumerate(match):
        beg = m[0]
        end = m[1]
        # print('(beg, end) = ({}, {})'.format(beg, end))
        f_name = get_file_name_via_index(db, beg)
        print('{} [{}]: ({}, {})'.format(i, f_name, beg, end))
        # print('---{}"{}"{}---'.format(dec[beg-rng: beg], dec[beg:end], dec[end:end+rng]))
        print('---{}"{}"{}---'.format(T[beg-rng: beg], T[beg:end], T[end:end+rng]))
        # print(decoded[m[0]:])

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


#!/usr/bin/env python
from django.core.management.base import BaseCommand, CommandError
import logging
import tempfile

from pythonping import ping
from kvmcp.models import KvmInfo as KI

class Command(BaseCommand):
    """This command check state host use ping."""
    DN_KVM = 'msm.ru'
    help = 'Check available equipment in net.'
    logger = logging.getLogger(__name__)

    def handle(self, *args, **options):
        kvm_all = KI.objects.values('name')
        for kvm in kvm_all:
            tmp = tempfile.TemporaryFile(mode='w+t')
            try:
                ping('{kvm_name}.{domain}'.format(kvm_name=kvm['name'], domain=self.DN_KVM),verbose=True, count=3,timeout=1, out=tmp)
            except OSError:
                Command.logger.error("You don't have the permission to socket. Command check_state.")
                break
            tmp.seek(0)
            host = KI.objects.get(name=kvm['name'])
            if 'Request timed out' in tmp.read():
                host.state = False
            else:
                host.state = True
            host.save()
            tmp.close()


import subprocess
import sys
from pathlib import Path

directory_path = "/opt/homebrew/xcodegen"
command = "brew install xcodegen"

# Check for the directory that would exist isnide of the core architecture.
if Path(directory_path).exists():
    print(f"The directory '{directory_path}' exists.")
else:
    # install xcodegen
    print("xcodegen isnt installed")
    user_inp = input("would you like to install it? (y/n):\n")

    if user_inp == "y":
        print("installing xcodegen")
        try:
            output = subprocess.check_output(command, shell=True, text=True)
        except subprocess.CalledProcessError as e:
            print(f"Error running process: {e}")

        sys.exit(0)
    else:
        sys.exit(1)


from django.http.response import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views.decorators.csrf import csrf_exempt
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Mentors, Youtubeplayerlist
from .serializers import CourseSerializer, MentorsSerializer,YoutubeplayerlistSerializer
from .models import Course
from rest_framework.decorators import api_view
from rest_framework import status
from django.http import Http404

@csrf_exempt
@api_view(['POST'])
def course_post(request):
    try:
        if request.method == 'POST':
            cname = request.data.get('courname')
            cdur = request.data.get('courduration')
            ccc = request.data.get('courcontents')
            ccost = request.data.get('coursecost')
            payload = {
                'course_name':cname,
                'course_duration':cdur,
                'course_contents':ccc,
                'course_cost':ccost,
            }
            r = Course(**payload)
            r.save()
            return Response({'status':'success', 'statuscode':'200', 'message':'saved successfully'})
        else:
            return Response({'status':'Fail', 'statuscode':'400', 'message':'course list does not posted'})

    except Exception as e:
            return Response({'status':'Fail', 'statuscode':'400', 'message':'course list does not posted'})

class CourseDetail(APIView):
    def get_object(self, id):
        try:
            return Course.objects.get(id=id)
        except Course.DoesNotExist:
            raise Http404

    def get(self, request, id):
        try:
            course = self.get_object(id)
            serializer = CourseSerializer(course)
            return Response(serializer.data)
        except Exception as e:
            return Response({'status':'Fail', 'statuscode':'400', 'message':'course not found'})

    def delete(self,request,id):
        try:
            course = self.get_object(id)
            course.delete()
            return Response({'status':'success','statuscode':'204','messsage':'coursedeleted successfully'})
        except Exception as e:
            return Response({'status':'fail','statuscode':'400','messsage':'course not found'})

    def put(self, request, id,):
        update = self.get_object(id)
        serializer = CourseSerializer(update, data=request.data)
        try:
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)
            else:
                return Response({'status':'success','statuscode':'204','message':'Data updatefailed'})
        except Exception as e:
            return Response({'status':'success','statuscode':'204','message':'invalid data'})

@csrf_exempt
@api_view(['GET'])
def view_all_courses(request):
    if request.method == 'GET':
        view_all = Course.objects.all()
        view_list = []
        for view in view_all:
            payload = {
                'id': view.id,
                'course_name': view.course_name,
                'course_duration': view.course_duration,
                'course_contents': view.course_contents,
                'course_cost': view.course_cost,
            }
            view_list.append(payload)
        return JsonResponse({'status': 'success', 'code': '200', 'all_courses': view_list})
    else:
        return Response({'status': 'fail', 'code': '404', 'message': 'Inavalid cradentials'})

@csrf_exempt
@api_view(['GET','POST','PUT'])
def mentors_details(request):
    if request.method == 'POST':
        try:
            mname = request.data.get('mentorname')
            mdiscription =request.data.get('mendiscription')
            photo = request.data.get('photo')
            payload ={
                'name':mname,
                'discription':mdiscription,
                'photo':photo
            }
            m = Mentors(**payload)
            m.save()
            return Response({'status':'ok', 'statuscode':'200', 'message':'mentors added sucesfully'})
        except Exception as e:
            return Response({'status':'Fail', 'statuscode':'400', 'message':'mentor doesnot exists'})

class Mentors_Update(APIView):
    def get_object(self, id):
        try:
            return Mentors.objects.get(id=id)
        except Mentors.DoesNotExist:
            raise Http404

    def get(self, request, id):
        try:
            mentors = self.get_object(id=id)
            serializer = MentorsSerializer(mentors)
            return Response(serializer.data)
        except Exception as e:
            return  Response({'status':'Fail', 'statuscode':'400', 'message':'mentor doesnot exists'})
    def delete(self,request,id):
        try:
            mentors = self.get_object(id)
            mentors.delete()
            return Response({'status':'succes', 'statuscode':'200', 'message':'mentor deleted sucessfully'})
        except Exception as e:
            return Response({'status':'Fail', 'statuscode':'400', 'message':'mentor doesnot exists'})

    def put(self, request, id, format=None):
        try:
            update = self.get_object(id)
            serializer = MentorsSerializer(update, data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)
        except Exception as e:
            return Response({'status':'Fail', 'statuscode':'400', 'message':'mentorsupdated sucesfully'})

@api_view(['POST'])
def youtube_list(request):
    if request.method == 'POST':
        name = request.data.get('name')
        url = request.data.get('url')
        context ={
            'name':name,
            'urls':url
        }
        y = Youtubeplayerlist(**context)
        y.save()
        return Response({'status':'ok', 'code':'200', 'message':'list added sucesfully'})

class youtubedetail(APIView):
    def get_object(self, id):
        try:
            return Youtubeplayerlist.objects.get(id=id)
        except Youtubeplayerlist.DoesNotExist:
            raise Http404

    def get(self, request, id):
        try:
            Youtubeplayerlist= self.get_object(id)
            serializer = YoutubeplayerlistSerializer(Youtubeplayerlist)
            return Response(serializer.data)
        except Exception as e:
            return Response({'status':'FAil', 'code':'400', 'message':'not found'})

    def delete(self,request,id):
        try:
            Youtubeplayerlist = self.get_object(id)
            Youtubeplayerlist.delete()
            return Response({'status':'ok', 'code':'200', 'message':'vedio deleted sucesfully'})
        except Exception as e:
            return Response({'status':'Fail', 'code':'400', 'message':'vedio not found'})

    def put(self, request, id,):
        update = self.get_object(id)
        serializer = YoutubeplayerlistSerializer(update, data=request.data)
        try:
            if serializer.is_valid():
                serializer.save()
                return Response({'status':'ok', 'code':'200', 'message':'updated sucesfully'})
            else:
                return Response({'status':'error', 'code':'404', 'message':'update failed'})

        except:
             return Response({'status':'Fail', 'code':'400', 'message':'vedio not foound'})


import os
import cherrypy
import re
import random
import locale
import pycountry
import datetime
from json import dumps as json_dumps
from cherrypy.process.plugins import SimplePlugin
from cherrypy._cptools import HandlerWrapperTool
from cherrypy._cpdispatch import PageHandler
from jinja2 import Environment, FileSystemLoader, ModuleLoader, StrictUndefined
from jinja2_webpack import Environment as WebpackEnvironment
from jinja2_webpack.filter import WebpackFilter
import opmuse
from opmuse.security import is_granted as _is_granted
from opmuse.pretty import pretty_date as _pretty_date
from opmuse.library import TrackStructureParser, Library, Album, Track, Artist

VISIBLE_WS = "\u2423"

def _max(items):
    return max(items)

def ordinal(num):
    # stolen from http://codegolf.stackexchange.com/a/4712
    k = num % 10
    return "%d%s" % (num, "tsnrhtdd"[(num / 10 % 10 != 1) * (k < 4) * k::4])

def date(value, date_format='%Y-%m-%d'):
    if type(value) is str:
        date = datetime.datetime.fromtimestamp(int(value))
    elif type(value) is int:
        date = datetime.datetime.fromtimestamp(value)
    else:
        date = value

    return date.strftime(date_format)

def get_jinja_env():
    auto_reload = cherrypy.config['opmuse'].get('jinja.auto_reload')

    if auto_reload is None:
        auto_reload = True

    template_path = os.path.join(os.path.dirname(__file__), '..', 'templates')

    if os.path.exists(template_path):
        loader = FileSystemLoader(template_path)
    else:
        loader = ModuleLoader('/usr/share/opmuse/templates/')

    env = Environment(
        loader=loader,
        extensions=['jinja2.ext.loopcontrols', 'jinja2.ext.do', 'jinja2.ext.autoescape'],
        autoescape=guess_autoescape,
        undefined=StrictUndefined,
        auto_reload=auto_reload,
        cache_size=-1
    )

    cache_path = cherrypy.config['opmuse'].get('cache.path')
    webpack_manifest_path = os.path.join(cache_path, 'webpack-manifest.json')

    if os.path.exists(webpack_manifest_path):
        webpack_env = WebpackEnvironment(manifest=webpack_manifest_path, publicRoot='')
        env.filters['webpack'] = WebpackFilter(webpack_env)
    else:
        def webpack_filter(*args):
            raise Exception("webpack-manifest.json is missing, please run `yarn build:dev` and restart opmuse")

        env.filters['webpack'] = webpack_filter

    env.filters['nl2p'] = nl2p
    env.filters['format_seconds'] = format_seconds
    env.filters['format_seconds_alt'] = format_seconds_alt
    env.filters['pretty_date'] = pretty_date
    env.filters['format_number'] = format_number
    env.filters['show_ws'] = show_ws
    env.filters['format_bytes'] = format_bytes
    env.filters['json'] = json
    env.filters['startswith'] = startswith
    env.filters['track_path'] = track_path
    env.filters['round'] = round
    env.filters['pretty_format'] = pretty_format
    env.filters['country'] = country
    env.filters['date'] = date
    env.filters['ordinal'] = ordinal
    env.filters['max'] = _max

    env.globals['pagination_pages'] = pagination_pages
    env.globals['rand_id'] = rand_id
    env.globals['is_granted'] = is_granted
    env.globals['is_track'] = is_track
    env.globals['is_artist'] = is_artist
    env.globals['is_album'] = is_album
    env.globals['render'] = render
    env.globals['opmuse_version'] = opmuse.__version__

    return env

def country(code):
    try:
        data = pycountry.countries.get(alpha_2=code)
        return data.name
    except KeyError:
        return code

# implemented with the help of http://codereview.stackexchange.com/a/15239
def pagination_pages(page, pages, size):
    if pages <= 10:
        return list(range(1, pages + 1))
    else:
        return sorted(list(set(range(1, 2)) |
                      set(range(max(1, page - size + 1), min(page + size, pages + 1))) |
                      set(range(pages, pages + 1))))

def pretty_format(format):
    return Library.pretty_format(format)

def is_track(object):
    return isinstance(object, Track)

def is_artist(object):
    return isinstance(object, Artist)

def is_album(object):
    return isinstance(object, Album)

def is_granted(role):
    return _is_granted([role])

def rand_id():
    return str(random.randrange(1, 99999))

def replace_ws(string):
    match = re.search(r'\S', string)

    if match is not None:
        index = match.start()
        return "%s%s" % (index * VISIBLE_WS, string[index:])
    else:
        return len(string) * VISIBLE_WS

def show_ws(string):
    """
    Helper for replacing trailing whitespace with the unicode visible space
    character
    """
    if string is None or isinstance(string, StrictUndefined) or len(string) == 0:
        return "[MISSING]"

    return replace_ws(replace_ws(string)[::-1])[::-1]

def format_bytes(bytes, precision=2):
    if bytes is None:
        bytes = 0

    bytes = int(bytes)

    suffixes = ['B', 'KB', 'MB', 'GB', 'TB']
    suffixIndex = 0

    while bytes > 1024:
        suffixIndex += 1
        bytes = bytes / 1024.0

    return "%.*f %s" % (precision, bytes, suffixes[suffixIndex])

def track_path(track, artist=None):
    track_structure = TrackStructureParser(track, data_override={'artist': artist})
    path = track_structure.get_path()

    if path is not None:
        return path.decode('utf8', 'replace')
    else:
        return ''

def startswith(value, start):
    return value.startswith(start)

def json(value):
    return json_dumps(value)

def format_number(number):
    if number is None:
        return None

    return locale.format('%d', number, grouping=True)

def pretty_date(date, ago="ago"):
    return _pretty_date(date, ago=ago)

def nl2p(string):
    paragraphs = []

    for line in string.split('\n'):
        if line == "":
            continue

        paragraphs.append("<p>%s</p>" % line)

    return ''.join(paragraphs)

def format_seconds_alt(seconds):
    if seconds is None:
        seconds = 0

    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)

    if minutes > 0:
        minute_str = "%dm " % minutes
    else:
        minute_str = ""

    if hours > 0:
        hour_str = "%dh " % hours
    else:
        hour_str = ""

    if hours == 0 and minutes == 0 and seconds == 0 or seconds > 0:
        second_str = "%ds" % seconds
    else:
        second_str = ""

    return "%s%s%s" % (hour_str, minute_str, second_str)

def format_seconds(seconds):
    if seconds is None:
        seconds = 0

    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)

    if hours > 0:
        string = "%02d:" % (hours, )
    else:
        string = ""

    return "%s%02d:%02d" % (string, minutes, seconds)

def guess_autoescape(template_name):
    if template_name is None or '.' not in template_name:
        return False

    ext = template_name.rsplit('.', 1)[1]

    return ext in ('html', )

class JinjaPlugin(SimplePlugin):

    def __init__(self, bus):
        SimplePlugin.__init__(self, bus)
        self.env = None
        self.bus.subscribe("bind_jinja", self.bind_jinja)

    def start(self):
        self.env = get_jinja_env()

    start.priority = 130

    def bind_jinja(self):
        return self.env

    def stop(self):
        self.env = None

def render(path, params=[]):
    # this one fucks with cherrypy.request and stuff, but seeing as this should
    # only be run in a template after all that jazz is done, it should be safe.
    # i guess we'll notice if it doesn't though :/
    func, vpath = cherrypy.request.dispatch.find_handler(path)
    config = cherrypy.request.config

    if func:
        handler = PageHandler(func, *params)
        filename = config['tools.jinja.filename']
        response_dict = handler()
        html = render_template(filename, response_dict)
        return html
    else:
        return ''

def render_template(filename, dictionary):
    template = cherrypy.request.jinja.get_template(filename)

    template.globals['request'] = cherrypy.request
    template.globals['xhr'] = cherrypy.request.headers.get('X-Requested-With') == 'XMLHttpRequest'
    template.globals['current_url'] = cherrypy.url()

    return template.render(dictionary)

class Jinja(HandlerWrapperTool):

    def __init__(self):
        HandlerWrapperTool.__init__(self, self.jinja)

    def callable(self, filename=None, *args, **kwargs):
        HandlerWrapperTool.callable(self)

    def jinja(self, next_handler, *args, **kwargs):
        if cherrypy.request.jinja is None:
            return

        response_dict = next_handler(*args, **kwargs)
        conf = self._merged_args()

        if 'filename' not in conf:
            raise Exception('No template filename specified!')

        html = render_template(conf['filename'], response_dict)

        if 'text/html' == cherrypy.response.headers['Content-Type']:
            cherrypy.response.headers['Content-Type'] = 'text/html; charset=utf8'

        return html.encode('utf8', 'replace')

class JinjaEnvTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_start_resource',
                               self.bind_jinja, priority=10)

    def bind_jinja(self):
        binds = cherrypy.engine.publish('bind_jinja')
        cherrypy.request.jinja = binds[0]

class JinjaAuthenticatedTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'before_handler',
                               self.start, priority=20)

    def start(self):
        if cherrypy.request.jinja is None:
            return

        cherrypy.request.jinja.globals['authenticated'] = False
        cherrypy.request.jinja.globals['user'] = None

        if hasattr(cherrypy.request, 'user') and cherrypy.request.user is not None:
            cherrypy.request.jinja.globals['user'] = cherrypy.request.user
            cherrypy.request.jinja.globals['authenticated'] = True


import cv2

trained_face_data = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')

webcam = cv2.VideoCapture(0)

while (True):

    sucess, frame = webcam.read()
    gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    face_coordinates = trained_face_data.detectMultiScale(gray_img)

    for (x, y, w, h) in face_coordinates:
        cv2.rectangle(frame, (x,y), (x + w, y + h), (0, 255, 0), 2)

    cv2.imshow('face detected', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

print("Done")


#! /usr/bin/env python3

import os
import json

def compute_dir_index(path, extensions):
    """ path: path to a website's root directory.
        extensions: allowed file extensions.
        Returns a dictionary with 'file: last modified time'. """
    index = {}
    # traverse the website's dir
    for root, dirs, filenames in os.walk(path):
        # loop through the files in the current directory
        for f in filenames:
            # if a file ends with a desired extension
            if f.endswith(extensions):
                # get the file path relative to the website's dir
                file_path = os.path.relpath(os.path.join(root, f), path)
                # get the last modified time of that file
                mtime = os.path.getmtime(os.path.join(path, file_path))
                # put them in the index
                index[file_path] = mtime

    # return a dict of files as keys and
    # last modified time as their values
    return index

if __name__ == "__main__":
    # file extensions to track (basically all the static files)
    extensions = ('.jpg', '.jpeg', '.png', '.gif', '.svg',
                  '.css', '.js', '.json', '.ico', '.xml')

    # path to the website's root directory
    path = '/var/www/example.com/htdocs/'

    # compute the new index
    new_index = compute_dir_index(path, extensions)

    # the old index json file
    json_file = '/var/www/example.com/index.json'

    # try to read the old json file
    try:
        with open(json_file, 'r') as f:
            old_index = json.load(f)
    # if there's no such file the old_index is an empty dict
    except IOError:
        old_index = {}

    # if there's a difference
    if new_index != old_index:
        # run the s3-sync script
        os.system('/path/to/s3-sync.sh')
        # save/overwrite the json file with the new index
        with open(json_file, 'w') as f:
            json.dump(new_index, f, indent=4)


import time
import os
from tqdm import tqdm
print("Loading things up")
time.sleep(2)
print("Loading sOS.py")
for i in tqdm (range (101),
			desc="Loading bOS.py",
			ascii=False, ncols=75):
	time.sleep(0.1)

print("Complete.")


#!/usr/bin/env python3

# A tool to convert any data files using ParaView
#
# Ivan Markin, @unkaktus 2023

import paraview.simple as pv
import argparse
import os

parser = argparse.ArgumentParser('pvconvert')
parser.add_argument("-i", type=str, help="Input filename")
parser.add_argument("-o", type=str, help="Output filename")

args = parser.parse_args()

reader = pv.OpenDataFile(args.i)
writer = pv.CreateWriter(args.o, reader)
writer.WriteAllTimeSteps = 1
writer.UpdatePipeline()


# Dependencies
# SSL certificate verification
# pip libraries : pandas, requests, bs4, yfinance, openpyxl

import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup

# List of S&P500 index's constituents from Wikipedia
# ---------------------------------------------------------------------------------------------------#
# data_frame = pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0]
# Get the ticker symbols from the DataFrame
# tickers = data_frame["Symbol"].tolist()

#                                            Note
# I HAVE COMMENTED OUT THE ABOVE CODE TO FETCH THE SYMBOLS OF THE ASSETS OF THE S&P500 INDEX AUTOMATICALLY
# BECAUSE THERE CAN BE POTENTIAL ISSUES WITH SSL CERTIFICATION ON DIFFERENT MACHINES AND SERVERS
# THESE ISSUES CAN BE A BIT LENGTHY TO DEBUG SO FOR NOW THE TICKERS ARE COPIED MANUALLY
# THE AUTOMATION CAN BE DONE BY GIVING IT SOME TIME
# ---------------------------------------------------------------------------------------------------#

tickers = ['MMM','AOS','ABT','ABBV','ACN','ATVI','ADM','ADBE','ADP','AAP','AES',
'AFL','A','APD','AKAM','ALK','ALB','ARE','ALGN','ALLE','LNT','ALL','GOOGL','GOOG',
'MO','AMZN','AMCR','AMD','AEE','AAL','AEP','AXP','AIG','AMT','AWK','AMP','ABC','AME',
'AMGN','APH','ADI','ANSS','AON','APA','AAPL','AMAT','APTV','ACGL','ANET','AJG','AIZ','T',
'ATO','ADSK','AZO','AVB','AVY','AXON','BKR','BALL','BAC','BBWI','BAX','BDX','WRB','BRK.B',
'BBY','BIO','TECH','BIIB','BLK','BK','BA','BKNG','BWA','BXP','BSX','BMY','AVGO','BR','BRO',
'BF.B','BG','CHRW','CDNS','CZR','CPT','CPB','COF','CAH','KMX','CCL','CARR','CTLT','CAT','CBOE',
'CBRE','CDW','CE','CNC','CNP','CDAY','CF','CRL','SCHW','CHTR','CVX','CMG','CB','CHD','CI','CINF',
'CTAS','CSCO','C','CFG','CLX','CME','CMS','KO','CTSH','CL','CMCSA','CMA','CAG','COP','ED','STZ',
'CEG','COO','CPRT','GLW','CTVA','CSGP','COST','CTRA','CCI','CSX','CMI','CVS','DHI','DHR','DRI',
'DVA','DE','DAL','XRAY','DVN','DXCM','FANG','DLR','DFS','DISH','DIS','DG','DLTR','D','DPZ','DOV',
'DOW','DTE','DUK','DD','DXC','EMN','ETN','EBAY','ECL','EIX','EW','EA','ELV','LLY','EMR','ENPH','ETR',
'EOG','EPAM','EQT','EFX','EQIX','EQR','ESS','EL','ETSY','RE','EVRG','ES','EXC','EXPE','EXPD','EXR',
'XOM','FFIV','FDS','FICO','FAST','FRT','FDX','FITB','FSLR','FE','FIS','FISV','FLT','FMC','F','FTNT',
'FTV','FOXA','FOX','BEN','FCX','GRMN','IT','GEHC','GEN','GNRC','GD','GE','GIS','GM','GPC','GILD','GL',
'GPN','GS','HAL','HIG','HAS','HCA','PEAK','HSIC','HSY','HES','HPE','HLT','HOLX','HD','HON','HRL','HST',
'HWM','HPQ','HUM','HBAN','HII','IBM','IEX','IDXX','ITW','ILMN','INCY','IR','PODD','INTC','ICE','IFF',
'IP','IPG','INTU','ISRG','IVZ','INVH','IQV','IRM','JBHT','JKHY','J','JNJ','JCI','JPM','JNPR','K','KDP',
'KEY','KEYS','KMB','KIM','KMI','KLAC','KHC','KR','LHX','LH','LRCX','LW','LVS','LDOS','LEN','LNC','LIN',
'LYV','LKQ','LMT','L','LOW','LYB','MTB','MRO','MPC','MKTX','MAR','MMC','MLM','MAS','MA','MTCH','MKC',
'MCD','MCK','MDT','MRK','META','MET','MTD','MGM','MCHP','MU','MSFT','MAA','MRNA','MHK','MOH','TAP',
'MDLZ','MPWR','MNST','MCO','MS','MOS','MSI','MSCI','NDAQ','NTAP','NFLX','NWL','NEM','NWSA','NWS','NEE',
'NKE','NI','NDSN','NSC','NTRS','NOC','NCLH','NRG','NUE','NVDA','NVR','NXPI','ORLY','OXY','ODFL','OMC',
'ON','OKE','ORCL','OGN','OTIS','PCAR','PKG','PARA','PH','PAYX','PAYC','PYPL','PNR','PEP','PFE','PCG',
'PM','PSX','PNW','PXD','PNC','POOL','PPG','PPL','PFG','PG','PGR','PLD','PRU','PEG','PTC','PSA','PHM',
'QRVO','PWR','QCOM','DGX','RL','RJF','RTX','O','REG','REGN','RF','RSG','RMD','RVTY','RHI','ROK','ROL',
'ROP','ROST','RCL','SPGI','CRM','SBAC','SLB','STX','SEE','SRE','NOW','SHW','SPG','SWKS','SJM','SNA',
'SEDG','SO','LUV','SWK','SBUX','STT','STLD','STE','SYK','SYF','SNPS','SYY','TMUS','TROW','TTWO','TPR',
'TRGP','TGT','TEL','TDY','TFX','TER','TSLA','TXN','TXT','TMO','TJX','TSCO','TT','TDG','TRV','TRMB','TFC',
'TYL','TSN','USB','UDR','ULTA','UNP','UAL','UPS','URI','UNH','UHS','VLO','VTR','VRSN','VRSK','VZ','VRTX',
'VFC','VTRS','VICI','V','VMC','WAB','WBA','WMT','WBD','WM','WAT','WEC','WFC','WELL','WST','WDC','WRK','WY',
'WHR','WMB','WTW','GWW','WYNN','XEL','XYL','YUM','ZBRA','ZBH','ZION','ZTS']

# Stocks of interest as per the problem statement - apple, amazon, google
target_tickers = ['AAPL', 'AMZN', 'GOOGL']

# Storing the total market capitalization of the entire S&P500 index (^GSPC)
marketCap_gspc = 0 # initialising, will be calculated later

# storing all the required metrics for the stocks of interests in a dictionary
# the elements in the data dictionary can be automated by dedicating a class for it.
# since the required metrics are very few, i'm going with the manual approach here
data = {
    "AAPL" : {
        'marketCap' : None,
        's&pweight' : None,
        'operationMargin' : None,
        'lastClose' : None,
        'ytdPerformance' : None,
        'revenue' : None,
        'netIncome' : None
    },
    "AMZN" : {
        'marketCap' : None,
        's&pweight' : None,
        'operationMargin' : None,
        'lastClose' : None,
        'ytdPerformance' : None,
        'revenue' : None,
        'netIncome' : None
    },
    "GOOGL" : {
        'marketCap' : None,
        's&pweight' : None,
        'operationMargin' : None,
        'lastClose' : None,
        'ytdPerformance' : None,
        'revenue' : None,
        'netIncome' : None
    }
}
# 1. starting off by calculating the net market capitalisation of s&p500 index
for ticker in tickers:
    try:
        marketCap = yf.Ticker(ticker).info['marketCap']
        # this marketCap represents the marketCap of the currently iterated stock
        marketCap_gspc += marketCap
        # updating the marketCaps of the targets to calculate the S&P weights ahead
        if(ticker in target_tickers):
            data[ticker]['marketCap'] = marketCap
    except:
        # we can also print the error received here, for now i'll just skip
        continue

# 2. Doing the Main calculation for all the metrics
for ticker in target_tickers:
    #fetching all the information of our target ticker in a variable(dicitionary) called stock
    stock = yf.Ticker(ticker).info

    # 1 S&P500 weight of the stock
    marketCap = data[ticker]['marketCap']
    weight = ((marketCap)/(marketCap_gspc))*100
    # #updating the weight of this stock
    data[ticker]['s&pweight'] = weight

    # 2 Operating Margin
    operatingMargin = stock['operatingMargins']
    data[ticker]['operatingMargin'] = operatingMargin

    # 3  Last Close Date
    lastClose = stock['previousClose']
    data[ticker]['lastClose'] = lastClose

    # 4

    # 5 Stocks' YTD performance
    previousClose = stock['previousClose']
    currentPrice = stock['currentPrice']
    ytd = ((currentPrice - previousClose)/(previousClose))*100
    data[ticker]['ytdPerformance'] = ytd

    # 6 Revenue
    totalRevenue = stock['totalRevenue']
    fiftyTwoWeekChange = stock['52WeekChange']
    # revenue = ((totalRevenue - fiftyTwoWeekChange)/(fiftyTwoWeekChange))*100
    revenue = totalRevenue
    data[ticker]['revenue'] = revenue

    # 7 Net Income
    netIncomeToCommon = stock['netIncomeToCommon']
    fiftyTwoWeekLow = stock['fiftyTwoWeekLow']
    netIncome = netIncomeToCommon
    data[ticker]['netIncome']=netIncome

    # 8

# Printing all the Stock Data on the screen
for stock in data:
    print(stock)
    print("S&P 500 weight of the stock : %f " % data[stock]['s&pweight'])
    print("Operatin Margin : %f" % data[stock]['operatingMargin'])
    print("Last Closing Price : %f " % data[stock]['lastClose'])
    print("YTD Performance : %f " % data[stock]['ytdPerformance'])
    print("Annual Revenue : %d " % data[stock]['revenue'])
    print("Net Annual Income : %d " % data[stock]['netIncome'])
    print()

# Declare the file where you want the output to be stored here
output_file = "output.xlsx"
# Convert your data to a pandas Data Frame
df = pd.DataFrame(data)
# Write the Data to the output file
df.to_excel(output_file)

print(f"Output Saved to {output_file}")


import pandas as pd
import glob
import os.path
import re
import datetime as dt
import warnings

from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer, TfidfTransformer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline

from main_serv import authors, str_TD_FileDir

def read_source_txt(strAuthIndex):
    if not authors.check_inUse(strAuthIndex):
        return None

    asr=authors.loc[strAuthIndex]

    lst_files=glob.glob(os.path.join(asr['strFileDir'], asr['file_mask']))
    lst_params=[]

    for fl in lst_files:
        try:
            with open(fl, 'r') as f:
                dct={'text': f.read().strip(),
                     'flag':asr['fire'],
                     'name':os.path.split(fl.replace('.txt', ''))[1],
                     'auth':asr['strRuName'] }

                lst_params.append(dct)

        except IsADirectoryError:
            continue

    pdf=pd.DataFrame(lst_params)
    #print(pdf['name'].tolist())
    return  pdf

def read_TD(strDir=str_TD_FileDir, file_mask='*'):

    lst_files=glob.glob(os.path.join(strDir, file_mask))
    lst_params=[]
    #print(strDir)
    for fl in lst_files:
        try:
            with open(fl, 'r') as f:
                dct={'text': f.read().strip(),
                     'flag':'?',
                     'name':os.path.split(fl.replace('.txt', ''))[1],
                     'auth':strDir}

                lst_params.append(dct)

        except IsADirectoryError:
            continue
    pdf=pd.DataFrame(lst_params)

    #print(pdf['name'].tolist())
    return  pdf

def train_model(pdf, train_with_test=True):
    vectoriser = TfidfVectorizer()
    classifier = MLPClassifier(hidden_layer_sizes=(480, 50,), activation='relu', tol=5e-5,
                               n_iter_no_change=5, max_iter=250, alpha=1e-5, verbose=True,
                               warm_start=False)

    print('Train model...', end='')
    X_train, X_test, y_train, y_test=train_test_split(pdf['text'], pdf['flag'])

    #print(X_train)
    clf_pp=Pipeline([('tfidf', vectoriser), ('clf_n', classifier)])

    if train_with_test:
        clf_pp.fit(X_train, y_train)
        res=clf_pp.predict(X_test)

        res_prob=clf_pp.predict_proba(X_test)

        print('Training done. Accurancy score = ', clf_pp.score(X_test, y_test), end='\n')
    else:
        clf_pp.fit(pdf['text'], pdf['flag'])
        print('Train comlete - without test', end='\n')
    return clf_pp

def make_result_pdf(pdf):
    pdf_res=pdf[['name', 'text']]
    pdf_res['short_text']=pdf_res['text'].str[:150]
    return pdf_res[['name', 'short_text']]

def sort_result(pdf):
    lstID = ['book', 'part', 'head']
    #pdf_res=pdf
    pdf[lstID] = pdf['name'].str.extract(r'b(?P<book>\d+)_p(?P<part>\d+)_h(?P<head>\d+)')
    pdf[lstID] = pdf[lstID].astype(int)
    pdf.sort_values(lstID, inplace=True)
    return pdf.drop(lstID, axis=1)

def modeling(strIndex, lstPredict, pdfSrc):
    def set_flag(src):
        return 1 if src['auth']==authors.main_author['strRuName'].values[0] else 0

    def get_make_dir(strName):
        strResultDir = re.sub('predict', 'result', strName)

        try:
            os.mkdir(strResultDir)
        except:
            pass
        return strResultDir

    def get_res_data_frame(strSourceDir, pdf_src):
        if strSourceDir == str_TD_FileDir:
            pdf_result = sort_result(make_result_pdf(pdf_src))
        else:
            pdf_result = make_result_pdf(pdf_src)
        return pdf_result

    authors.main_author = strIndex
    pdfSrc['flag']=pdfSrc.apply(set_flag, axis=1)

    print('Train model for ', strIndex)
    #print(pdfSrc.groupby(['flag', 'auth']).count())
    #return 0

    model = train_model(pdfSrc)
    print('o.k.', end='\n')
    #res = model.predict(pdf_td['text'])
    for l in lstPredict:
        print('?' * 20, '\t', l, '\t', '?' * 20)
        print('\n{1} ВОПРОС : автор "кучи непонятных текстов" -- {0}? --- {2}'.format(
                authors.main_author['strRuName'].values[0],
                dt.datetime.now().strftime('%d.%m.%Y %H:%M:%S'), authors.index.tolist()))

        pdf_td = read_TD(strDir=l)  # read ASK sample
        pdf_result=get_res_data_frame(l, pdf_td)
        res_prob = model.predict_proba(pdf_td['text'])

        pdf = pd.DataFrame({'prob2_0': [j for _, j in res_prob]}, index=pdf_td.index.tolist())
        pdf_result = pdf_result.join(pdf)
        prob2_mean = pdf_result['prob2_0'].mean()
        print('DONE. Probability for YES - {0:.3f}'.format(prob2_mean))
        strResFile = os.path.join(get_make_dir(l),
                                  'result_prob_{}.csv'.format(authors.main_author['strFileDir'].values[0]))
        pdf_result.to_csv(strResFile, sep=';', index=False)
        print('writing result to file ', strResFile)
        print('?' * 80)

    print('All done for', strIndex)

lst_predict=['predict/f1', 'predict/ka1', 'predict/pl1', 'predict/at1',
                 'predict/ga1', 'predict/pa1', 'predict/o1', 'predict/ilf1',
                 'predict/lt1', 'predict/mm', 'predict/sh', str_TD_FileDir]

def main():
    #lst_predict=['predict/f1', 'predict/ka1', 'predict/pl1', 'predict/at1', 'predict/ba1',
    #             'predict/ga1', 'predict/pa1', 'predict/o1', 'predict/ilf1',
    #             'predict/lt1', 'predict/mm', str_TD_FileDir]
    #lst_predict = ['predict/f1']

    # TD
    #'predict/f1'  # Fadeev's texts
    #'predict/ka1'  # Kataev's texts
    #'predict/pl1'  # Platonov's texts
    #'predict/lt1'  # L. Tolstoj's texts
    #'predict/at1'  # A. Tolstoj's texts
    #'predict/Ba1'  # I. Babel's texts
    #'predict/mm'  # Bulgakov's texts

    authors.use(val=False)
    authors.use(index=['kru', 'sho', 'sera', 'fad', 'ltol', 'pla', 'kat', 'blg', 'atol', 'bab'], val=True)
    #print(authors)

    pdfX = pd.concat([pdf for pdf in authors.index.map(read_source_txt)])

    print('loading sourses (files):')
    print(pdfX.groupby(by='auth').size())
    print('=' * 40)

    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        for n, auth in authors.iterrows():
            if auth['in_use']:
                modeling(n, lst_predict, pdfX)

if __name__ == "__main__":
    main()
    #modeling('bab', lst_predict)


'''
Created on 3 maj 2011

@author: Yasin
'''

from PyQt5 import QtCore, QtGui, QtWidgets

from .modal_items import QColorDialog, QFontDialog

class PlotModel(QtCore.QAbstractItemModel):
    '''
    This model was created to allow support of the
    complexity of the multiCanvasItem. it allows a
    treeview approach of the
    '''
    sortRole   = QtCore.Qt.UserRole
    filterRole = QtCore.Qt.UserRole + 1

    """INPUTS: Node, QObject"""
    def __init__(self, root, parent=None, col_count = 2):
        super(PlotModel, self).__init__(parent)
        self._rootNode = root
        self._col_count = col_count
        self.color_picker = QColorDialog()
        self.font_picker = QFontDialog()
        self.referenceModel()

    def referenceModel(self):
        self._rootNode.referenceModel(self)

    def root(self):
        return self._rootNode

    """INPUTS: QModelIndex"""
    """OUTPUT: int"""
    def rowCount(self, parent):
        if not parent.isValid():
            parentNode = self._rootNode
        else:
            parentNode = parent.internalPointer()

        return parentNode.childCount()

    """INPUTS: QModelIndex"""
    """OUTPUT: int"""
    def columnCount(self, parent):
        return self._col_count

    """INPUTS: QModelIndex, int"""
    """OUTPUT: QVariant, strings are cast to QString which is a QVariant"""
    def data(self, index, role):

        if not index.isValid():
            return None

        node = index.internalPointer()

        if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            return node.data(index.column())

        if role == QtCore.Qt.DecorationRole:
            if index.column() == 0:
                resource = node.resource()
                return QtGui.QIcon(QtGui.QPixmap(resource))

        if role == PlotModel.sortRole:
            return node.typeInfo()

        if role == PlotModel.filterRole:
            return node.typeInfo()

        if role == QtCore.Qt.BackgroundRole:
            if type(node.data(index.column())) == QtGui.QColor:
                return QtGui.QBrush(node.data(index.column()))

        if role == QtCore.Qt.FontRole:
            if type(node.data(index.column())) == QtGui.QFont:
                return node.data(index.column())

    """INPUTS: QModelIndex, QVariant, int (flag)"""
    def setData(self, index, value, role=QtCore.Qt.EditRole):

        if index.isValid():
            node = index.internalPointer()
            if role == QtCore.Qt.EditRole:
                node.setData(index.column(), value)
                self.dataChanged.emit(index, index)
                return True

        return False

    """INPUTS: int, Qt::Orientation, int"""
    """OUTPUT: QVariant, strings are cast to QString which is a QVariant"""
    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole:
            if section == 0:
                return "Properties"
            else:
                return ""

    """INPUTS: QModelIndex"""
    """OUTPUT: int (flag)"""
    def flags(self, index):
        item = self.getNode(index)
        return item.flags(index)

    """INPUTS: QModelIndex"""
    """OUTPUT: QModelIndex"""
    """Should return the parent of the node with the given QModelIndex"""
    def parent(self, index):
        node = self.getNode(index)
        parentNode = node.parent()

        if parentNode == self._rootNode or parentNode == None or parentNode.row() == None:
            return QtCore.QModelIndex()

        return self.createIndex(parentNode.row(), 0, parentNode)

    """INPUTS: int, int, QModelIndex"""
    """OUTPUT: QModelIndex"""
    """Should return a QModelIndex that corresponds to the given row, column and parent node"""
    def index(self, row, column, parent):
        parentNode = self.getNode(parent)
        childItem = parentNode.child(row)
        if childItem:
            return self.createIndex(row, column, childItem)
        else:
            return QtCore.QModelIndex()

    """CUSTOM"""
    """INPUTS: QModelIndex"""
    def getNode(self, index):
        node = index.internalPointer()
        if node:
            return node

        return self._rootNode

    def itemAt(self,index):
        '''
        returns the ite, associated to the model index
        '''
        if not index.parent().internalPointer() is None:
            return index.parent().internalPointer().child(index.row())
        else:
            return None

    """INPUTS: int, int, QModelIndex"""
    def insertRows(self, position, rows, items, parentNode):
        success = False
        self.beginInsertRows(
            parentNode.index(), position,
            position + rows - 1)

        for row in range(rows):
            items[row].referenceModel(self)
            success = parentNode.insertChild(position, items[row])

        self.endInsertRows()
        self.referenceModel()

        return success

    # """INPUTS: int, int, QModelIndex"""
    def removeRows(self, position, rows, parentNode):
        success = False
        self.beginRemoveRows(
            parentNode.index(), position,
            position + rows - 1)

        for row in range(rows):
            success = parentNode.removeChild(position)

        self.endRemoveRows()

        return success


from mighty.backends import TaskBackend
from mighty.backends.schedulers.celery import start_task
from celery import uuid

class TaskBackend(TaskBackend):
    def start(self):
        task_id = uuid()
        start_task.apply_async(
            (self.ct, self.pk, self.task, task_id)+self.extra_args,
            self.extra_kwargs,
            task_id=task_id)


import re

from rest_framework import serializers

from yugioh_django.cards.models import Card

class CardSerializer(serializers.ModelSerializer):
    class Meta:
        model = Card
        fields = ('id', 'name', 'attribute', 'attack', 'defense', 'lore', 'effect')

    id = serializers.UUIDField(read_only=True)
    name = serializers.CharField(required=True)
    attribute = serializers.CharField(required=True)
    attack = serializers.CharField(allow_null=True, required=False)
    defense = serializers.CharField(allow_null=True, required=False)

    def _validate_battle_value(self, value):
        return re.match('[0-9]+|\?', value)

    def validate_attack(self, attack):
        if attack is None:
            return attack
        if not self._validate_battle_value(attack):
            raise serializers.ValidationError('{} is an invalid attack value'.format(attack))
        return attack

    def validate_defense(self, defense):
        if defense is None:
            return defense
        if not self._validate_battle_value(defense):
            raise serializers.ValidationError('{} is an invalid defense value'.format(defense))
        return defense

    def create(self, validated_data):
        validated_data['author'] = self.context['request'].user
        return super().create(validated_data)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import mglearn

print("""
İbrahim Halil Bayat
Department of Electronics and Communication Engineering
İstanbul Technical University
İstanbul, Turkey
\n---------- Classifying Iris Species ----------
Species: Setosa, Versicolor, Virginica.
""")

data = load_iris()
x_train, x_test, y_train, y_test = train_test_split(data['data'], data['target'], random_state=0)

print("\n--------- Converting NumPy into Pandas ---------------")
df = pd.DataFrame(x_train, columns=data['feature_names'])
grr = pd.plotting.scatter_matrix(df, c=y_train, figsize=(15, 15), marker='o', hist_kwds={'bins': 20}, s=60, alpha=0.8,
                        cmap=mglearn.cm3)
plt.show()


from salon.salon import *
from salon.equipment import *
from itertools import groupby

class Cupboard(Equipment):
    numb_jewelry = {}

    def __init__(self, types_jewelry):
        super().__init__(types_jewelry)
        Equipment.__approach_time = 0.6
        self.numb_jewelry = self.__sort_jewelry()

    def __sort_jewelry(self):
        numb = []
        for i in range(self.count_jewelry()):
            numb.append(len(self.types_jewelry[i]))
        no_dif_numb = [el for el, _ in groupby(numb)]
        numb_jewelry = {key: [] for key in sorted(no_dif_numb)}
        for i in range(self.count_jewelry()):
            numb_jewelry[numb[i]].append(self.types_jewelry[i])
        return numb_jewelry

    def __generate_jewelry(self):
        keys_jewelry = list(self.numb_jewelry.keys())
        count_jewelry = len(keys_jewelry)
        interval_a = 1
        interval_b = count_jewelry / self.level if count_jewelry / self.level > 1 else 1
        number_random = random.randint(interval_a, interval_b) - 1
        random_list_jewelry = self.numb_jewelry.get(keys_jewelry[number_random])
        return random_list_jewelry[random.randint(1, len(random_list_jewelry)) - 1]

    def play(self, person):
        random_jewelry = self.__generate_jewelry()
        random_concat_word = sorted(list(random_jewelry), key=lambda a: random.random())
        start_play_time = time.time()
        while True:
            if (time.time() - start_play_time) >= person.max_mood_time:
                return False
            try:
                print('Составьте слово из букв:')
                for i in random_concat_word:
                    print(i, end=" ")
                print('\r')
                word = str(input()).replace(' ', '')
                if word == random_jewelry:
                    print('Верно! ', end="")
                    return True
                else:
                    print('Неверно!')
                time.sleep(0.4)
            except ValueError:
                print('Вы ввели не слово')
                time.sleep(0.4)

    def type_and_level(self):
        return 'Шкаф с одеждой ' + str(self.level) + ' уровня'