import imageio
import os

path = 'F:/11111'
filenames = []
for files in os.listdir(path):
    if files.endswith('jpg') or files.endswith('jpeg') or files.endswith('png'):
        file = os.path.join(path, files)
        filenames.append(file)

images = []
for filename in filenames:
    images.append(imageio.imread(filename))
imageio.mimsave('F:/my.gif', images, duration=0.5)


class Library:
    def __init__(self, listOfBooks):
       self.books = listOfBooks

    def displayAvalableBooks(self):
            print("The books present in the library are: ")
            for book in self.books:
                print("->" + book)
    def borrowBook(self, bookName):
        if bookName in self.books:
            print(f"You have been issued {bookName}. Please keep it safe and return it within 30 days.\nThank You! ")
            self.books.remove(bookName)
            return True
        else:
            print("Sorry, This book as been 'issued' to somebody else. Please wait until the book is returned")
            return False

    def returnBook(self, bookName):
        self.books.append(bookName)
        print("Thanks for returning the Book.\nHave a great Day!")

class Student:
    def requestBook(self):
        self.book = input("Enter the name of the Book you want: ")
        return self.book

    def returnBook(self):
        self.book = input("Enter the name of the Book you want to return: ")
        return self.book

if __name__ == "__main__":
    centralLibrary = Library(["Algorithms", "Django", "Clrs", "Python Notes", "The Jungle Book", "The Jurasic Park"])
    student = Student()
    # centralLibrary.displayAvalableBooks()
    while(True):
        welcomeMsg =  ''' =====Welcome to 'CENTRAL LIBRARY'=====
        Please choose the option:
        1. List all the books
        2. Request a book
        3. Return a book
        4. Exit the Menu
        '''
        print(welcomeMsg)

        a = int(input("Enter a choice: "))
        if a == 1:
            centralLibrary.displayAvalableBooks()
        elif a == 2:
            centralLibrary.borrowBook(student.requestBook())
        elif a == 3:
            centralLibrary.returnBook(student.returnBook())
        elif a == 4:
            print("Thanks for choosing CENTRAL LIBRARY.\nHave a great day!")
            exit()
        else:
            print("Invalid Choice!")


import numpy as np
import pandas as pd
from keras.models import load_model
import psutil
import ctypes

# Load model
model = load_model("lstm_model.h5")
# Get process ID of Minecraft.Windows.exe
for process in psutil.process_iter(['pid', 'name']):
    if process.info['name'] == 'Minecraft.Windows.exe':
        pid = process.info['pid']
        break
else:
    print("Minecraft.Windows.exe is not running!")
    exit()
xveladdr = 0x214002DF07C-4
yveladdr = 0x214002DF07C
zveladdr = 0x214002DF07C+4
ongroundaddr = 0x214136B40D0
process_handle = ctypes.windll.kernel32.OpenProcess(0x1F0FFF, False, pid)

while True:
    velocity_x = ctypes.c_float()
    velocity_y = ctypes.c_float()
    velocity_z = ctypes.c_float()
    onground = ctypes.c_int()
    ctypes.windll.kernel32.ReadProcessMemory(process_handle, ctypes.c_void_p(xveladdr), ctypes.byref(velocity_x), ctypes.sizeof(velocity_x), None)
    ctypes.windll.kernel32.ReadProcessMemory(process_handle, ctypes.c_void_p(yveladdr), ctypes.byref(velocity_y), ctypes.sizeof(velocity_y), None)
    ctypes.windll.kernel32.ReadProcessMemory(process_handle, ctypes.c_void_p(zveladdr), ctypes.byref(velocity_z), ctypes.sizeof(velocity_z), None)
    ctypes.windll.kernel32.ReadProcessMemory(process_handle, ctypes.c_void_p(ongroundaddr), ctypes.byref(onground), ctypes.sizeof(onground), None)
    #print(velocity_x,velocity_y,velocity_z,onground)
    # Normalize data
    data_to_predict = pd.DataFrame({
        "velocity_x": [velocity_x.value],
        "velocity_y": [velocity_y.value],
        "velocity_z": [velocity_z.value],
        "onground": [onground.value]
    })
    data_to_predict = (data_to_predict - data_to_predict.min()) / (data_to_predict.max() - data_to_predict.min())

    # Reshape input to be 3D [samples, timesteps, features]
    X = data_to_predict.values.reshape((data_to_predict.shape[0], 1, data_to_predict.shape[1]))

    # Predict labels
    predictions = model.predict(X,verbose=0)
    #if predictions >= 0.5:
        #print("Flying")
    #else:
        #print("Not flying")
    print(predictions)


import numpy as np
import quaternion as q
import math
import abc
from cassandra.physics.kinematics import EulerAngles, Dynamic

class PID:
  """
  A simple PID controller.

  Parameters
  ----------
  Kp : scalar, default=0
    The PID's proportional gain.
  Ki : scalar, default=0
    The PID's integral gain.
  Kd : scalar, default=0
    The PID's derivative gain.

  Attributes
  ----------
  Kp : scalar
    The PID's proportional gain.
  Ki : scalar
    The PID's integral gain.
  Kd : scalar
    The PID's derivative gain.
  integral : scalar
    The accumulated integral of the error.
  last_error : scalar
    The last error recorded.
  """
  def __init__(self, Kp=0, Ki=0, Kd=0):
    self.Kp = Kp
    self.Ki = Ki
    self.Kd = Kd

    self.integral = 0
    self.last_error = 0

  def calculate(dt, error):
    """
    Calculate the output from an error and an ellapsed time.

    Parameters
    ----------
    dt : scalar
      The ellapsed time since the last calculation.
    error : scalar
      The error input.

    Returns
    -------
    scalar
      The output of the PID.
    """
    # Proportional term
    proportional = error * self.Kp

    # Integral term
    self.integral += error * dt
    integral = self.integral * self.Ki

    # Derivative term
    derivative = (error - self.last_error) / dt * self.Kd
    self.last_error = error

    return proportional + integral + derivative

class FlightSoftware(Dynamic, abc.ABC):
  """
  A flight software for controlling the rocket before, during and after flight.

  This class is meant to be subclassed to specify a specific program method, overriding the default one.

  Parameters
  ----------
  loop_time : scalar, default=0
    The time per loop of the program, used to create a more accurate simulation.

  Attributes
  ----------
  loop_time : scalar
    The time per loop of the program, used to create a more accurate simulation.
  last_execution : scalar
    Keeps track of the last execution of the program in the simulation to know when to run it again.
  """

  def __init__(self, loop_time=0):
    self.last_execution = 0
    self.loop_time = loop_time

  def update(self, timestep, rocket, measurements):
    """
    Update the software according to an ellapsed time, the controlled rocket and the physical measurements.

    Parameters
    ----------
    timestep : scalar
      The time ellapsed since the last update.
    rocket : Rocket
      The rocket being controlled by the software.
    measurements : dict
      The physical information available to the software at this point in time.
    """
    self.last_execution += timestep
    if self.last_execution > self.loop_time:
      self.program(rocket, measurements)
      self.last_execution = 0

  @staticmethod
  @abc.abstractmethod
  def program(rocket, measurements):
    """
    The program being run by the software in loop.

    Parameters
    ----------
    rocket : Rocket
      The rocket being controlled by the software.
    measurements : dict
      The physical information available to the software at this point in time.
    """
    pass

class DummySoftware(FlightSoftware):
  """
  Dummy test software that does absolutely nothing.
  """
  @staticmethod
  def program(rocket, measurements):
    pass

class RandomSoftware(FlightSoftware):
  """
  Basic flight software that controls the rocket randomly.
  """
  @staticmethod
  def program(rocket, measurements):
    roll = np.random.uniform(-rocket.mount.max_roll, rocket.mount.max_roll)
    pitch = np.random.uniform(-rocket.mount.max_pitch, rocket.mount.max_pitch)
    orientation = EulerAngles(roll, pitch, 0).to_quaternion()
    rocket.mount.set_target(orientation)


from django.core.exceptions import ImproperlyConfigured

from ..core.settings import CONFIGURATION, COOKIE_MAX_AGE

# Tries to get the Twitter configuration, if the configuration is not
# provided or is incorrect raises an ImproperlyConfigured exception.
TWITTER = CONFIGURATION.get('TWITTER', None)

if not TWITTER:
    raise ImproperlyConfigured(
        'You have socialnetworks.facebook in your ISTALLED_APPS, but you do '
        'not specify any "TWITTER" settings inside '
        'SOCIALNETWORK_CONFIGURATION.'
    )

else:
    APP_ID = TWITTER.get('APP_ID', None)
    APP_SECRET = TWITTER.get('APP_SECRET', None)
    SESSION_KEY = TWITTER.get('SESSION_KEY', 'dsntw')
    SESSION_FIELDS = TWITTER.get('SESSION_FIELDS', ['screen_name', 'name'])
    SETUP_URL_NAME = TWITTER.get('SETUP_URL_NAME', None)

    if not APP_ID:
        raise ImproperlyConfigured(
            'A proper "APP_ID" must be specified in order to use '
            'socialnetworks.facebook module.'
        )

    if not APP_SECRET:
        raise ImproperlyConfigured(
            'A proper "APP_SECRET" must be specified in order to use '
            'socialnetworks.facebook module.'
        )


# This file is licensed under the GNU General Public License (GPL) version 2.0.
# See the LICENSE file or https://www.gnu.org/licenses/gpl-2.0.html for more details.

from data_provider.data_factory import data_provider
from exp.exp_basic import Exp_Basic
from models import JTFT
from utils.tools import EarlyStopping, adjust_learning_rate, visual, test_params_flop
from utils.metrics import metric

import numpy as np
import torch
import torch.nn as nn
from torch import optim
from torch.optim import lr_scheduler

import os
import sys
import time

import warnings
import matplotlib.pyplot as plt
import numpy as np

from sklearn.preprocessing import StandardScaler
import pandas as pd
import copy

warnings.filterwarnings('ignore')

class Exp_Main_JTFT(Exp_Basic):
    def __init__(self, args):
        super(Exp_Main_JTFT, self).__init__(args)

    def _build_model(self):
        model_dict = {
            'JTFT': JTFT,
        }
        model = model_dict[self.args.model].Model(self.args).float()
        if self.args.use_multi_gpu and self.args.use_gpu:
            model = nn.DataParallel(model, device_ids=self.args.device_ids)
        return model

    def _get_data(self, flag):
        data_set, data_loader = data_provider(self.args, flag)
        return data_set, data_loader

    def _select_optimizer(self, model):
        model_optim = optim.Adam(model.parameters(), lr=self.args.learning_rate)
        return model_optim

    def _select_criterion(self):
        criterion = nn.MSELoss()
        return criterion

    def vali(self, vali_data, vali_loader, criterion, verbose=False):
        total_loss = []
        if verbose:
            preds = []
            trues = []
        self.model.eval()
        with torch.no_grad():
            for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(vali_loader):
                batch_x = batch_x.float().to(self.device)
                batch_y = batch_y.float().to(self.device)
                if self.args.use_mark:
                    batch_x_mark = batch_x_mark.float().to(self.device)
                    batch_y_mark = batch_y_mark.float().to(self.device)
                else:
                    batch_x_mark = None
                    batch_y_mark = None
                outputs = self.model(batch_x, z_mark=batch_x_mark, target_mark=batch_y_mark)
                f_dim = -1 if self.args.features == 'MS' else 0
                outputs = outputs[:, -self.args.pred_len:, f_dim:]
                batch_y = batch_y[:, -self.args.pred_len:, f_dim:]
                loss = criterion(outputs, batch_y)
                total_loss.append(loss.item())
                if verbose:
                    pred = outputs.detach().cpu()
                    true = batch_y.detach().cpu()
                    preds.append(pred.numpy())
                    trues.append(true.numpy())
        total_loss = np.average(total_loss)
        if verbose:
            preds = np.array(preds)
            trues = np.array(trues)
            preds = preds.reshape(-1, preds.shape[-2], preds.shape[-1])
            trues = trues.reshape(-1, trues.shape[-2], trues.shape[-1])
            mae, mse, rmse, mape, mspe, rse, corr = metric(preds, trues)
            print('ms/ma/rse:{:.4f}, {:.4f}, {:.4f}'.format(mse, mae, rse))
        self.model.train()
        return total_loss

    def train(self, setting):
        train_data, train_loader = self._get_data(flag='train')
        vali_data, vali_loader = self._get_data(flag='val')
        test_data, test_loader = self._get_data(flag='test')
        path = os.path.join(self.args.checkpoints, setting)
        if not os.path.exists(path):
            os.makedirs(path)
        train_steps = len(train_loader)
        print('Length of train/val/test loader', len(train_loader), len(vali_loader), len(test_loader))

        criterion = self._select_criterion()
        if self.args.use_huber_loss:
            print("Use huber loss for train and validation, test loss remains MSE")
            criterion_huber = nn.HuberLoss(delta=self.args.huber_delta)
        if not self.args.ini_with_low_freq:
            #Calculate the initial frequencies
            for batch_x, batch_y, batch_x_mark, batch_y_mark in train_loader:
                x = batch_x[:, -self.args.seq_len:, :].float().to(self.device)
                #Accumulate amplitude for frequecies
                if not self.args.use_multi_gpu:
                    self.model.model.accum_freq_amp(x)
                else:
                    self.model.module.model.accum_freq_amp(x)
            #Obtain initial frequencies
            if not self.args.use_multi_gpu:
                self.model.model.comp_ini_freq()
            else:
                self.model.module.model.comp_ini_freq()
        #Training
        model_optim = self._select_optimizer(self.model)
        early_stopping = EarlyStopping(patience=self.args.patience, verbose=True)
        scheduler = lr_scheduler.OneCycleLR(optimizer = model_optim,
                        steps_per_epoch = train_steps,
                        pct_start = self.args.pct_start,
                        epochs = self.args.train_epochs,
                        max_lr = self.args.learning_rate)
        if self.args.resume_after_epo != 0:
            print('loading model')
            self.model.load_state_dict(torch.load(path + '/checkpoint.pth'))
            for epoch in range(self.args.resume_after_epo):
                if self.args.lradj != 'TST':
                    adjust_learning_rate(model_optim, scheduler, epoch + 1, self.args)
                else:
                    for step in range(len(train_loader)):
                        adjust_learning_rate(model_optim, scheduler, epoch + 1, self.args, printout=False)
                        scheduler.step()

        #Training models, return the best result obtained on 'ctrl+c'
        try:
            for epoch in range(self.args.resume_after_epo, self.args.train_epochs):
                iter_count = 0
                train_loss = []
                self.model.train()
                epoch_time = time.time()
                for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(train_loader):
                    iter_count += 1
                    model_optim.zero_grad()
                    batch_x = batch_x.float().to(self.device)
                    batch_y = batch_y.float().to(self.device)
                    if self.args.use_mark:
                        batch_x_mark = batch_x_mark.float().to(self.device)
                        batch_y_mark = batch_y_mark.float().to(self.device)
                    else:
                        batch_x_mark = None
                        batch_y_mark = None
                    f_dim = -1 if self.args.features == 'MS' else 0
                    outputs = self.model(batch_x, z_mark=batch_x_mark, target_mark=batch_y_mark)
                    outputs = outputs[:, -self.args.pred_len:, f_dim:]
                    batch_y = batch_y[:, -self.args.pred_len:, f_dim:]
                    if self.args.use_huber_loss:
                        loss = criterion_huber(outputs, batch_y)
                    else:
                        loss = criterion(outputs, batch_y)
                    train_loss.append(loss.item())
                    loss.backward()
                    model_optim.step()
                    if self.args.lradj == 'TST':
                        adjust_learning_rate(model_optim, scheduler, epoch + 1, self.args, printout=False)
                        scheduler.step()

                val_time=time.time()
                print("Epoch: {} cost time: {:.2f}".format(epoch + 1, val_time - epoch_time), end=" ")
                train_loss = np.average(train_loss)
                if self.args.use_huber_loss:
                    vali_loss = self.vali(vali_data, vali_loader, criterion_huber)
                else:
                    vali_loss = self.vali(vali_data, vali_loader, criterion)
                verbose_test=False
                if epoch >= self.args.min_epochs:
                    early_stopping(vali_loss, self.model, path)
                    if early_stopping.counter==0:
                        verbose_test=True
                test_loss = self.vali(test_data, test_loader, criterion, verbose=verbose_test)
                print("batchs {}, val/test time {:.2f}".format(i, time.time() - val_time))
                print("Epoch: {}, Steps: {} | Train {:,.5f} Vali Loss: {:.5f} Test Loss: {:.5f}".format(epoch + 1, train_steps, train_loss, vali_loss, test_loss))
                if not self.args.use_multi_gpu:
                    self.model.model.show_freqs(n_disp=self.args.n_freq)
                else:
                    self.model.module.model.show_freqs(n_disp=self.args.n_freq)
                if early_stopping.early_stop:
                    print("Early stopping")
                    break
                if self.args.lradj != 'TST':
                    adjust_learning_rate(model_optim, scheduler, epoch + 1, self.args)
                else:
                    print('Updating learning rate to {}'.format(model_optim.state_dict()['param_groups'][0]['lr']))

        except KeyboardInterrupt:
            print("KeyboardInterrupt, return the current best model")
        self.model.load_state_dict(torch.load(path +'/checkpoint.pth', map_location=self.device))

        return self.model

    def test(self, setting, test=0):
        test_data, test_loader = self._get_data(flag='test')

        if test:
            print('loading model')
            path = os.path.join(self.args.checkpoints, setting)
            self.model.load_state_dict(torch.load(path + '/checkpoint.pth'))

        preds = []
        trues = []
        inputx = []
        folder_path = './test_results/' + setting + '/'
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)

        self.model.eval()
        with torch.no_grad():
            for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(test_loader):
                batch_x = batch_x.float().to(self.device)
                batch_y = batch_y.float().to(self.device)
                if self.args.use_mark:
                    batch_x_mark = batch_x_mark.float().to(self.device)
                    batch_y_mark = batch_y_mark.float().to(self.device)
                else:
                    batch_x_mark = None
                    batch_y_mark = None
                outputs = self.model(batch_x, z_mark=batch_x_mark, target_mark=batch_y_mark)
                f_dim = -1 if self.args.features == 'MS' else 0
                outputs = outputs[:, -self.args.pred_len:, f_dim:]
                batch_y = batch_y[:, -self.args.pred_len:, f_dim:]
                outputs = outputs.detach().cpu().numpy()
                batch_y = batch_y.detach().cpu().numpy()
                pred = outputs
                true = batch_y
                preds.append(pred)
                trues.append(true)
                inputx.append(batch_x.detach().cpu().numpy())
                if i % 20 == 0:
                    input = batch_x.detach().cpu().numpy()
                    gt = np.concatenate((input[0, :, -1], true[0, :, -1]), axis=0)
                    pd = np.concatenate((input[0, :, -1], pred[0, :, -1]), axis=0)
                    visual(gt, pd, os.path.join(folder_path, str(i) + '.pdf'))
        if self.args.test_flop:
            test_params_flop((batch_x.shape[1],batch_x.shape[2]))
            exit()
        preds = np.array(preds)
        trues = np.array(trues)
        inputx = np.array(inputx)
        preds = preds.reshape(-1, preds.shape[-2], preds.shape[-1])
        trues = trues.reshape(-1, trues.shape[-2], trues.shape[-1])
        inputx = inputx.reshape(-1, inputx.shape[-2], inputx.shape[-1])
        # result save
        folder_path = './results/' + setting + '/'
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        mae, mse, rmse, mape, mspe, rse, corr = metric(preds, trues)
        print('Test_loss(pl {}): mse:{:.5f}, mae:{:.5f}, rse:{:.5f}'.format(self.args.pred_len, mse, mae, rse))
        f = open("result.txt", 'a')
        f.write(setting + " \n")
        f.write('mse:{}, mae:{}, rse:{}'.format(mse, mae, rse))
        f.write('\n')
        f.write('\n')
        f.close()
        np.save(folder_path + 'pred.npy', preds)
        return

    def predict(self, setting, load=False):
        pred_data, pred_loader = self._get_data(flag='pred')

        if load:
            path = os.path.join(self.args.checkpoints, setting)
            best_model_path = path + '/' + 'checkpoint.pth'
            self.model.load_state_dict(torch.load(best_model_path))

        preds = []

        self.model.eval()
        with torch.no_grad():
            for i, (batch_x, batch_y, _, _) in enumerate(pred_loader):
                batch_x = batch_x.float().to(self.device)
                batch_y = batch_y.float()
                outputs = self.model(batch_x)
                pred = outputs.detach().cpu().numpy()  # .squeeze()
                preds.append(pred)

        preds = np.array(preds)
        preds = preds.reshape(-1, preds.shape[-2], preds.shape[-1])

        # result save
        folder_path = './results/' + setting + '/'
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)

        np.save(folder_path + 'real_prediction.npy', preds)

        return


# 链接：https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/
# 题意：给定一个整型数组，从中选取 k 个数，求最大值减去最小值的差最小是多少？

# 数据限制：
#   1 <= k <= nums.length <= 1000
#   0 <= nums[i] <= 10 ^ 5

# 输入： nums = [90], k = 1
# 输出： 0
# 解释：
#       只能选 [90] ，最大值为 90, 最小值为 90 ，差为 0

# 输入： nums = [9,4,1,7], k = 2
# 输出： 2
# 解释：
#       选择 [9,7] ，最大值为 9, 最小值为 7, 差为 2

# 思路： 排序 + 贪心
#
#       当选择的数是按顺序连续的 k 个数时，才有可能取得最小差值。
#
#       假设选择了一个非连续的数，那么差值不会变小，结果不会更优。
#
#       所以我们可以对 nums 排序，然后枚举所有长度为 k 的子数组计算差值的最小值即可。
#
#
#       时间复杂度： O(nlogn)
#           1. 需要对 nums 中全部 O(n) 个数字排序，排序时间复杂度为 O(nlogn)
#           2. 需要遍历 nums 中全部 O(n - k) 个长度为 k 的子数组
#       空间复杂度： O(1)
#           1. 只需要维护常数个额外变量即可

class Solution:
    def minimumDifference(self, nums: List[int], k: int) -> int:
        # 先排序
        nums.sort()
        # 从所有长度为 k 的子数组中找到最小差值
        return min(
            nums[i] - nums[i - k + 1]
            for i in range(k - 1, len(nums))
        )

Python3/src/1901 ~ 2000/1984 - Minimum Difference Between Highest and Lowest of K Scores.py


from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Article
from .forms import BlogPost,Form
# Create your views here.

def index(request):
    data= Article.objects.all().values()
    context = {
        'data':data
    }
    return render(request,"home/index.html",context)

def articles(request):
    data= Article.objects.all().values()
    context = {
        'data':data
    }
    return render(request,"home/article.html",context)

def showArticle(request,id):
    data = Article.objects.get(id = id)
    article = Article.objects.all()
    print(data)
    context = {
        'data' : data,
        'article': article
    }
    return render(request, "home/show.html",context)

def search(request):
    keyword = ""
    context = {}
    if request.method == "POST":
        keyword = request.POST.get("search")
        data = Article.objects.filter(title=keyword)
        context = {
            "data":data
        }
    return render(request,'home/article.html',context)

def addArticle(request):
    form = Form()
    context ={'form':form}
    return render(request,"home/add.html",context)

def store(request):
    if request.method == "POST":
        form = Form(request.POST)
        if form.is_valid():
            form.save()
    return redirect("/")


# coding: utf-8
from __future__ import unicode_literals

import logging
from datetime import timedelta
from django.views.generic import TemplateView
from django.db import connection

from core import models
import pymorphy2

BORDERS = 50
RESULTS_PER_PAGE = 20

log = logging.getLogger(__name__)

class SearchView(TemplateView):
    template_name = 'core/index.html'

    def get(self, request, *args, **kwargs):
        query = request.GET.get('query', '').strip()

        if not query:
            return self.render_to_response({})

        log.info('Search for <%s>', query)

        morph = pymorphy2.MorphAnalyzer()
        words = [morph.parse(word.strip())[0].normal_form for word in query.split(' ')]
        amount = len(words)

        log.info('Morphed to <%s>', ', '.join(words))

        if amount > 5:
            return self.render_to_response({'query': query, 'error': 'Слишком длинный запрос'})

        if amount == 1:
            q = '''SELECT n0.word_id
                   FROM core_wordnormal n0
                   JOIN core_word w ON n0.word_id=w.id
                   JOIN core_record r ON w.record_id=r.id
                   WHERE n0.normal=%s'''

        else:
            select = 'SELECT ' + ', '.join('n%s.word_id' % i for i in xrange(amount))
            where = ' WHERE ' + ' AND '.join('n%s.normal=%%s' % i for i in xrange(amount))
            join = ''' FROM core_wordnormal n0 JOIN core_word w ON n0.word_id=w.id
                   JOIN core_record r ON w.record_id=r.id  '''
            for i in xrange(1, amount):
                join += ' JOIN core_wordnormal n{curr} ' \
                        'ON n{prev}.record_id=n{curr}.record_id ' \
                        'AND n{prev}.position < n{curr}.position ' \
                        'AND n{curr}.position - n{prev}.position < 5 '.format(curr=i, prev=i-1)

            q = select + join + where

        q += ' ORDER BY r.dt DESC, w.position ASC'
        q += ' LIMIT %s' % (RESULTS_PER_PAGE + 1)

        page = 1
        if request.GET.get('page'):
            page = int(request.GET.get('page'))

        q += ' OFFSET %s' % (RESULTS_PER_PAGE * (page - 1))

        cursor = connection.cursor()
        cursor.execute(q, words)

        results = []
        data = cursor.fetchall()
        for row in data[:RESULTS_PER_PAGE]:
            res_words = [models.Word.objects.get(pk=word_id) for word_id in row]
            start_position = max(res_words[0].position - BORDERS, 0)
            finish_position = res_words[-1].position + BORDERS

            words = models.Word.objects.filter(
                position__gte=start_position,
                position__lte=finish_position,
                record=res_words[0].record_id,
            )

            phrase = ''
            prev = words[0].offset
            for word in words:
                diff = word.offset - prev
                prev = word.offset

                if word.block_start:
                    phrase += '. '

                if word.pk in row:
                    phrase += ' %s ' % word.word
                else:
                    phrase += ' ' + word.word

                if diff > 1:
                    phrase += ','

            results.append({
                'record': res_words[0].record,
                'phrase': phrase,
                'start': timedelta(seconds=words[0].offset),
            })

        return self.render_to_response({
            'query': query,
            'results': results,
            'page': page,
            'first': 1 if page > 1 else None,
            'prev': page - 1 if page > 1 else None,
            'next': page + 1 if len(data) > RESULTS_PER_PAGE else None,
        })


import math
from random import random as aleatorio

def main():
    colecao = [8, 1, 10, 5, 3, 6]
    # colecao = ['Mari', 'Ana', 'Pedrinhu',
    #            'Rogerio', 'Zenóbian', 'Ana Letíciab']

    print(colecao)

    # Coleção de Números
    colecao_ordenada = quicksort(colecao)
    # colecao_ordenada = quicksort(colecao, reverse=True)

    # Colecao de Nomes
    # colecao_ordenada = quicksort(colecao, chave=len)
    # colecao_ordenada = quicksort(colecao, chave=len, reverse=True)
    # colecao_ordenada = quicksort(colecao, chave=ultima_letra)

    print(colecao_ordenada)

def ultima_letra(nome):
    return nome[len(nome) - 1]

def quicksort(vetor, chave=lambda x: x, reverse=False):

    if len(vetor) <= 1:
        return vetor

    random_index = math.floor(aleatorio() * len(vetor))
    pivot = vetor[random_index]
    # sorry: using method (implemente remover_item_colecao(colecao, item))
    vetor.remove(pivot)

    if reverse:
        left = filtro_colecao(vetor, lambda x: chave(x) > chave(pivot))
        right = filtro_colecao(vetor, lambda x: chave(x) <= chave(pivot))
    else:
        left = filtro_colecao(vetor, lambda x: chave(x) <= chave(pivot))
        right = filtro_colecao(vetor, lambda x: chave(x) > chave(pivot))

    return quicksort(left, chave, reverse) + [pivot] + quicksort(right, chave, reverse)

# utils
def filtro_colecao(colecao, funcao_criterio):
    colecao_filtrada = []

    for item in colecao:
        if funcao_criterio(item):
            colecao_filtrada.append(item)

    return colecao_filtrada

def mapear_colecao(colecao, funcao_mapeadora):
    nova_colecao = []

    for item in colecao:
        nova_colecao.append(funcao_mapeadora(item))

    return nova_colecao

main()


import os
import sys
basedir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0,basedir+'/../..')

import dum_mats

a = dum_mats.row_offset_col_gain_zeroimag()
a.plot()

b = a.create_reduced_dim(0)
b.set_chart_title("Test title")
b.plot(logx=True, logy=True)

c = b.create_reduced_dim(1)
c.set_chart_parameters(leg_prefix="Test", xsize=10, ysize=10)
c.plot(imag=True)

d = a.trace()
d.plot()

e = a[1:].to_dKmat().eigenvalues()
e.plot()

f = a[1:].to_dXSmat().to_dXSsca()
f.plot()

g = a[1:].to_dXSsca()
g.plot()

h = a[1:].to_dEPhaseMat().to_dEPhaseSca()
h.plot()

i = a[1:].to_dEPhaseSca()
i.plot()

j = a[1:].to_dQmat()
j.plot()

k = j.eigenvalues()
k.plot()


# -*- coding: utf-8 -*-
"""
Created on Tue Dec  8 10:45:11 2020

@author: jrmfi
"""
from tf_agents.environments import py_environment
from tf_agents.specs import array_spec
from tf_agents.trajectories import time_step as ts
import numpy as np
import pandas as pd
from Trade import Trade

class CardGameEnv(py_environment.PyEnvironment):

  def __init__(self,base,fim:int):
    self._action_spec = array_spec.BoundedArraySpec(
        shape=(1,), dtype=np.float32, minimum=0, maximum=2, name='action')
    self._observation_spec = array_spec.BoundedArraySpec(
        shape=(1,12), dtype=np.float32, minimum=[-20.0,-20.0,-20.0,-20.0,-20.0,-20.0,-20.0,-20.0,-20.0,-20.0,
                                                -20.0,-20.0],
                                        maximum=[20.0,20.0,20.0,20.0,20.0,20.0,20.0,20.0,20.0,20.0,
                                                 20.0,20.0], name='observation')

    self._episode_ended = False
    self.base = base
    self.cont = 0
    self.fim = fim
    self.contador = 0
    self.metal = False
    self.dados1,self.dados2 = self.tratamento(base,fim)
    self.trader = Trade()
    self._state =  self.dados2.values[0].tolist()#0 # self.dados2.values[0].tolist()
    print('--------------------')
    print(self._state)
    print(type(self._state))
    print('--------------------')
  #-----------------------------------------------------------------

  def action_spec(self):
    return self._action_spec

  def observation_spec(self):
    return self._observation_spec

  def _reset(self):
    self._state = self.dados2.values[self.cont]  #0 # self.dados2.values[0].tolist()
    self._episode_ended = False
    return ts.restart(np.array([self._state], dtype=np.float32))

  def _step(self, action):

    if self._episode_ended:
    #    # The last action ended the episode. Ignore the current action and start
    #    # a new episode.
       self.reset()

    # Make sure episodes don't go on forever.
    stop = -500
    gain = 500
    if len(self.dados1)-5 <= self.cont:
        self.contador +=1
        self.trader.reset()
        print('contador: ',self.contador)
        self._episode_ended = True
        self.cont = 0
    # dados1,dados2 = self.tratamento(self.base)
    compra,venda,neg,ficha,comprado,vendido,recompensa=self.trader.agente(self.dados1.values[self.cont],action,stop,gain,0)
    if comprado or vendido:
        self.metal = True
    if self.metal and (comprado == False and vendido == False):
        self.metal = False
        self._episode_ended = True

    # print('dentro da classe-----------------------')
    # print('cont :',self.cont)
    # print('acao: ',action)
    # print('comprado: ',comprado)
    # print('vendido: ',vendido)
    # print('recompensa: ',recompensa)
    # # print('dados1: ',self.dados1.values[self.cont][0]) #68084.0
    # # print('dados1: ',self.dados2.values[self.cont])
    # print('episodio: ',self._episode_ended )
    # print('-------------------------------------')

    self.state = self.dados2.values[self.cont].tolist()
    self.cont += 1

      # raise ValueError('`action` should be 0 or 1.')
    reward = recompensa
    if self._episode_ended:
      self.trader.reset()
      return ts.termination(np.array([self._state], dtype=np.float32), reward)
    else:
      return ts.transition(
          np.array([self._state], dtype=np.float32), reward=reward, discount=1.0)
  #-----------------------------------------------------------------
  def tratamento(self,base,fim):
    print('****************************')
    print('tratamento: ',self.cont)
    print('****************************')
    colunas = ['Hora','dif', 'retacao +','retracao -', 'RSI',
                 'M22M44', 'M22M66', 'M66M44', 'ADX', 'ATR',
                'Momentum', 'Force']

    colunas1 = ['Hora', 'open', 'high', 'low', 'close']
    dados1 = pd.DataFrame(data=base[-fim:-1].values,columns=base.columns)
    dados2 = pd.DataFrame(data=base[-fim:-1].values,columns=base.columns)
    dados1 = dados1[colunas1]
    dados2 = dados2[colunas]
    index = 0
    for i in dados2.values:
        base1 = i[0].split(':')
        dados2.at[index, 'Hora'] = float(base1[0])*100 + float(base1[1])
        index += 1
    train_mean = dados2.mean(axis=0)
    train_std = dados2.std(axis=0)
    dados2 = (dados2 - train_mean) / train_std
    return dados1,dados2


import logger
import pandas as pd
import os
import random
from pytorch_transformers import BertTokenizer
import torch
from pathlib import Path

from sklearn.model_selection import train_test_split

from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler, Dataset
from torch.utils.data.distributed import DistributedSampler
import en_core_web_md
from tqdm import tqdm, trange

class LMInputExample(object):
    """A single training/test example for the language model."""

    def __init__(self, guid, tokens_a, tokens_b=None, is_next=None, lm_labels=None):
        """Constructs a InputExample.
        Args:
            guid: Unique id for the example.
            tokens_a: string. The untokenized text of the first sequence. For single
            sequence tasks, only this sequence must be specified.
            tokens_b: (Optional) string. The untokenized text of the second sequence.
            Only must be specified for sequence pair tasks.
            label: (Optional) string. The label of the example. This should be
            specified for train and dev examples, but not for test examples.
        """
        self.guid = guid
        self.tokens_a = tokens_a
        self.tokens_b = tokens_b
        self.is_next = is_next  # nextSentence
        self.lm_labels = lm_labels  # masked words for language model

class LMInputFeatures(object):
    """A single set of features of data."""

    def __init__(self, input_ids, input_mask, segment_ids, is_next, lm_label_ids):
        self.input_ids = input_ids
        self.input_mask = input_mask
        self.segment_ids = segment_ids
        self.is_next = is_next
        self.lm_label_ids = lm_label_ids

def create_corpus(text_list, target_path):

    nlp = en_core_web_md.load(disable=['tagger', 'ner', 'textcat'])

    with open(target_path, 'w') as f:
        #  Split sentences for each document
        for text in tqdm(text_list):
            text_lines = [str(sent)
                          for i, sent in enumerate(nlp(str(text)).sents)]
            f.write('\n'.join(text_lines))
            f.write("\n  \n")

class BertLMDataBunch(object):

    @staticmethod
    def from_raw_corpus(data_dir, text_list,  tokenizer, bs=32, maxlen=512, multi_gpu=True, test_size=0.1):

        train_list, val_list = train_test_split(
            text_list, test_size=test_size, shuffle=True)
        # Create train corpus
        create_corpus(train_list, str(data_dir/'train.txt'))

        # Create val corpus
        create_corpus(val_list, str(data_dir/'val.txt'))

        return BertLMDataBunch(data_dir, tokenizer, bs=bs, maxlen=maxlen, multi_gpu=multi_gpu)

    def __init__(self, data_dir, tokenizer, train_file='train.txt', val_file='val.txt', test_data=None,
                 bs=32, maxlen=512, multi_gpu=True):

        self.tokenizer = tokenizer
        self.maxlen = maxlen
        self.bs = bs
        self.train_dl = None
        self.val_dl = None
        self.test_dl = None
        self.data_dir = data_dir

        if train_file:
            train_data = BERTLMDataset(
                str(self.data_dir/train_file), self.tokenizer, self.maxlen)
            if multi_gpu:
                train_sampler = RandomSampler(train_data)
            else:
                train_sampler = DistributedSampler(train_data)
            self.train_dl = DataLoader(
                train_data, sampler=train_sampler, batch_size=bs)

        if val_file:
            val_data = BERTLMDataset(
                str(self.data_dir/val_file), self.tokenizer, self.maxlen)

            val_sampler = SequentialSampler(val_data)
            self.val_dl = DataLoader(
                val_data, sampler=val_sampler, batch_size=bs)

class BERTLMDataset(Dataset):
    def __init__(self, corpus_path, tokenizer, seq_len, encoding="utf-8", corpus_lines=None, on_memory=True):
        self.vocab = tokenizer.vocab
        self.tokenizer = tokenizer
        self.seq_len = seq_len
        self.items = []
        self.on_memory = on_memory
        self.corpus_lines = corpus_lines  # number of non-empty lines in input corpus
        self.corpus_path = corpus_path
        self.encoding = encoding
        self.current_doc = 0  # to avoid random sentence from same doc

        # for loading samples directly from file
        self.sample_counter = 0  # used to keep track of full epochs on file
        # keep second sentence of a pair in memory and use as first sentence in next pair
        self.line_buffer = None

        # for loading samples in memory
        self.current_random_doc = 0
        self.num_docs = 0
        self.sample_to_doc = []  # map sample index to doc and line

        # load samples into memory
        if on_memory:
            self.all_docs = []
            doc = []
            self.corpus_lines = 0
            with open(corpus_path, "r", encoding=encoding) as f:
                for line in tqdm(f, desc="Loading Dataset", total=corpus_lines):
                    line = line.strip()
                    if line == "":
                        self.all_docs.append(doc)
                        doc = []
                        # remove last added sample because there won't be a subsequent line anymore in the doc
                        self.sample_to_doc.pop()
                    else:
                        # store as one sample
                        sample = {"doc_id": len(self.all_docs),
                                  "line": len(doc)}
                        self.sample_to_doc.append(sample)
                        doc.append(line)
                        self.corpus_lines = self.corpus_lines + 1

            # if last row in file is not empty
            if self.all_docs[-1] != doc:
                self.all_docs.append(doc)
                self.sample_to_doc.pop()

            self.num_docs = len(self.all_docs)
            self.items = self.all_docs

        # load samples later lazily from disk
        else:
            if self.corpus_lines is None:
                with open(corpus_path, "r", encoding=encoding) as f:
                    self.corpus_lines = 0
                    for line in tqdm(f, desc="Loading Dataset", total=corpus_lines):
                        if line.strip() == "":
                            self.num_docs += 1
                        else:
                            self.corpus_lines += 1

                    # if doc does not end with empty line
                    if line.strip() != "":
                        self.num_docs += 1

            self.file = open(corpus_path, "r", encoding=encoding)
            self.random_file = open(corpus_path, "r", encoding=encoding)

    def __len__(self):
        # last line of doc won't be used, because there's no "nextSentence". Additionally, we start counting at 0.
        return self.corpus_lines - self.num_docs - 1

    def __getitem__(self, item):
        cur_id = self.sample_counter
        self.sample_counter += 1
        if not self.on_memory:
            # after one epoch we start again from beginning of file
            if cur_id != 0 and (cur_id % len(self) == 0):
                self.file.close()
                self.file = open(self.corpus_path, "r", encoding=self.encoding)

        t1, t2, is_next_label = self.random_sent(item)

        # tokenize
        tokens_a = self.tokenizer.tokenize(t1)
        tokens_b = self.tokenizer.tokenize(t2)

        # combine to one sample
        cur_example = LMInputExample(
            guid=cur_id, tokens_a=tokens_a, tokens_b=tokens_b, is_next=is_next_label)

        # transform sample to features
        cur_features = convert_example_to_features(
            cur_example, self.seq_len, self.tokenizer)

        cur_tensors = {"input_ids": torch.tensor(cur_features.input_ids),
                       "input_mask": torch.tensor(cur_features.input_mask),
                       "segment_ids": torch.tensor(cur_features.segment_ids),
                       "lm_label_ids": torch.tensor(cur_features.lm_label_ids),
                       "is_next": torch.tensor(cur_features.is_next)}

        return cur_tensors

    def random_sent(self, index):
        """
        Get one sample from corpus consisting of two sentences. With prob. 50% these are two subsequent sentences
        from one doc. With 50% the second sentence will be a random one from another doc.
        :param index: int, index of sample.
        :return: (str, str, int), sentence 1, sentence 2, isNextSentence Label
        """
        t1, t2 = self.get_corpus_line(index)
        if random.random() > 0.5:
            label = 0
        else:
            try:
                t2_temp = self.get_random_line()
                t2 = t2_temp
                label = 1
            except:
                label = 0

        assert len(t1) > 0
        assert len(t2) > 0
        return t1, t2, label

    def get_corpus_line(self, item):
        """
        Get one sample from corpus consisting of a pair of two subsequent lines from the same doc.
        :param item: int, index of sample.
        :return: (str, str), two subsequent sentences from corpus
        """
        t1 = ""
        t2 = ""
        assert item < self.corpus_lines
        if self.on_memory:
            sample = self.sample_to_doc[item]
            t1 = self.all_docs[sample["doc_id"]][sample["line"]]
            t2 = self.all_docs[sample["doc_id"]][sample["line"]+1]
            # used later to avoid random nextSentence from same doc
            self.current_doc = sample["doc_id"]
            return t1, t2
        else:
            if self.line_buffer is None:
                # read first non-empty line of file
                while t1 == "":
                    t1 = self.file.__next__().strip()
                    t2 = self.file.__next__().strip()
            else:
                # use t2 from previous iteration as new t1
                t1 = self.line_buffer
                t2 = self.file.__next__().strip()
                # skip empty rows that are used for separating documents and keep track of current doc id
                while t2 == "" or t1 == "":
                    t1 = self.file.__next__().strip()
                    t2 = self.file.__next__().strip()
                    self.current_doc = self.current_doc+1
            self.line_buffer = t2

        assert t1 != ""
        assert t2 != ""
        return t1, t2

    def get_random_line(self):
        """
        Get random line from another document for nextSentence task.
        :return: str, content of one line
        """
        # Similar to original tf repo: This outer loop should rarely go for more than one iteration for large
        # corpora. However, just to be careful, we try to make sure that
        # the random document is not the same as the document we're processing.
        for _ in range(10):
            if self.on_memory:
                rand_doc_idx = random.randint(0, len(self.all_docs)-1)
                rand_doc = self.all_docs[rand_doc_idx]
                line = rand_doc[random.randrange(len(rand_doc))]
            else:
                rand_index = random.randint(
                    1, self.corpus_lines if self.corpus_lines < 1000 else 1000)
                # pick random line
                for _ in range(rand_index):
                    line = self.get_next_line()
            # check if our picked random line is really from another doc like we want it to be
            if self.current_random_doc != self.current_doc:
                break
        return line

    def get_next_line(self):
        """ Gets next line of random_file and starts over when reaching end of file"""
        try:
            line = self.random_file.__next__().strip()
            # keep track of which document we are currently looking at to later avoid having the same doc as t1
            if line == "":
                self.current_random_doc = self.current_random_doc + 1
                line = self.random_file.__next__().strip()
        except StopIteration:
            self.random_file.close()
            self.random_file = open(
                self.corpus_path, "r", encoding=self.encoding)
            line = self.random_file.__next__().strip()
        return line

def convert_example_to_features(example, max_seq_length, tokenizer):
    """
    Convert a raw sample (pair of sentences as tokenized strings) into a proper training sample with
    IDs, LM labels, input_mask, CLS and SEP tokens etc.
    :param example: InputExample, containing sentence input as strings and is_next label
    :param max_seq_length: int, maximum length of sequence.
    :param tokenizer: Tokenizer
    :return: InputFeatures, containing all inputs and labels of one sample as IDs (as used for model training)
    """
    tokens_a = example.tokens_a
    tokens_b = example.tokens_b
    # Modifies `tokens_a` and `tokens_b` in place so that the total
    # length is less than the specified length.
    # Account for [CLS], [SEP], [SEP] with "- 3"
    _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3)

    t1_random, t1_label = random_word(tokens_a, tokenizer)
    t2_random, t2_label = random_word(tokens_b, tokenizer)
    # concatenate lm labels and account for CLS, SEP, SEP
    lm_label_ids = ([-1] + t1_label + [-1] + t2_label + [-1])

    # The convention in BERT is:
    # (a) For sequence pairs:
    #  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
    #  type_ids: 0   0  0    0    0     0       0 0    1  1  1  1   1 1
    # (b) For single sequences:
    #  tokens:   [CLS] the dog is hairy . [SEP]
    #  type_ids: 0   0   0   0  0     0 0
    #
    # Where "type_ids" are used to indicate whether this is the first
    # sequence or the second sequence. The embedding vectors for `type=0` and
    # `type=1` were learned during pre-training and are added to the wordpiece
    # embedding vector (and position vector). This is not *strictly* necessary
    # since the [SEP] token unambigiously separates the sequences, but it makes
    # it easier for the model to learn the concept of sequences.
    #
    # For classification tasks, the first vector (corresponding to [CLS]) is
    # used as as the "sentence vector". Note that this only makes sense because
    # the entire model is fine-tuned.
    tokens = []
    segment_ids = []
    tokens.append("[CLS]")
    segment_ids.append(0)
    for token in tokens_a:
        tokens.append(token)
        segment_ids.append(0)
    tokens.append("[SEP]")
    segment_ids.append(0)

#     assert len(tokens_b) > 0
    for token in tokens_b:
        tokens.append(token)
        segment_ids.append(1)
    tokens.append("[SEP]")
    segment_ids.append(1)

    input_ids = tokenizer.convert_tokens_to_ids(tokens)

    # The mask has 1 for real tokens and 0 for padding tokens. Only real
    # tokens are attended to.
    input_mask = [1] * len(input_ids)

    # Zero-pad up to the sequence length.
    while len(input_ids) < max_seq_length:
        input_ids.append(0)
        input_mask.append(0)
        segment_ids.append(0)
        lm_label_ids.append(-1)

    assert len(input_ids) == max_seq_length
    assert len(input_mask) == max_seq_length
    assert len(segment_ids) == max_seq_length
    assert len(lm_label_ids) == max_seq_length

    if example.guid < 5:
        logger.info("*** Example ***")
        logger.info("guid: %s" % (example.guid))
        logger.info("tokens: %s" % " ".join(
            [str(x) for x in tokens]))
        logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
        logger.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
        logger.info(
            "segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
        logger.info("LM label: %s " % (lm_label_ids))
        logger.info("Is next sentence label: %s " % (example.is_next))

    features = LMInputFeatures(input_ids=input_ids,
                               input_mask=input_mask,
                               segment_ids=segment_ids,
                               lm_label_ids=lm_label_ids,
                               is_next=example.is_next)
    return features

def _truncate_seq_pair(tokens_a, tokens_b, max_length):
    """Truncates a sequence pair in place to the maximum length."""

    # This is a simple heuristic which will always truncate the longer sequence
    # one token at a time. This makes more sense than truncating an equal percent
    # of tokens from each, since if one sequence is very short then each token
    # that's truncated likely contains more information than a longer sequence.
    while True:
        total_length = len(tokens_a) + len(tokens_b)
        if total_length <= max_length:
            break
        if len(tokens_a) > len(tokens_b):
            tokens_a.pop()
        else:
            tokens_b.pop()