import hashlib
import hmac
import os
import struct
import sys
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, ec
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.utils import int_to_bytes
from esp_secure_cert.esp_secure_cert_helper import load_private_key

supported_targets_rsa_ds = ['esp32s2', 'esp32s3', 'esp32c3',
                            'esp32c6', 'esp32h2']
supported_key_size_rsa = {'esp32s2': [1024, 2048, 3072, 4096],
                          'esp32c3': [1024, 2048, 3072],
                          'esp32s3': [1024, 2048, 3072, 4096],
                          'esp32c6': [1024, 2048, 3072],
                          'esp32h2': [1024, 2048, 3072]}

supported_targets_ecdsa = ['esp32h2']
supported_key_size_ecdsa = {'esp32h2': [256]}

idf_path = os.getenv('IDF_PATH')
if not idf_path or not os.path.exists(idf_path):
    raise Exception('IDF_PATH not found')

def number_as_bytes(number, pad_bits=None):
    """
    Given a number, format as a little endian array of bytes
    """
    result = int_to_bytes(number)[::-1]
    while pad_bits is not None and len(result) < (pad_bits // 8):
        result += b'\x00'
    return result

# @return
#       c               : ciphertext_c
#       iv              : initialization vector
#       key_size        : key size of the RSA private key in bytes.
# @input
#       privkey         : path to the RSA private key
#       priv_key_pass   : path to the RSA privaete key password
#       hmac_key        : HMAC key value ( to calculate DS params)
#       idf_target      : The target chip for the script (e.g. esp32c3)
# @info
#       The function calculates the encrypted private key parameters.
#       Consult the DS documentation (available for the ESP32-S2)
#       in the esp-idf programming guide for more details
#       about the variables and calculations.
def calculate_rsa_ds_params(privkey, priv_key_pass, hmac_key, idf_target):
    private_key_data = load_private_key(privkey, priv_key_pass)
    private_key = private_key_data["key_instance"]
    if not isinstance(private_key, rsa.RSAPrivateKey):
        print('ERROR: Only RSA private keys are supported')
        sys.exit(-1)
    if hmac_key is None:
        print('ERROR: hmac_key cannot be None')
        sys.exit(-2)

    priv_numbers = private_key.private_numbers()
    pub_numbers = private_key.public_key().public_numbers()
    Y = priv_numbers.d
    M = pub_numbers.n
    key_size = private_key.key_size
    if key_size not in supported_key_size_rsa[idf_target]:
        print('ERROR: Private key size {0} not supported for the target {1},'
              '\nthe supported key sizes are {2}'
              .format(key_size, idf_target,
                      str(supported_key_size_rsa[idf_target])))
        sys.exit(-1)

    iv = os.urandom(16)

    rr = 1 << (key_size * 2)
    rinv = rr % pub_numbers.n
    mprime = - rsa._modinv(M, 1 << 32)
    mprime &= 0xFFFFFFFF
    length = key_size // 32 - 1

    # get max supported key size for the respective target
    max_len = max(supported_key_size_rsa[idf_target])
    aes_key = hmac.HMAC(hmac_key, b'\xFF' * 32, hashlib.sha256).digest()

    md_in = number_as_bytes(Y, max_len) + \
        number_as_bytes(M, max_len) + \
        number_as_bytes(rinv, max_len) + \
        struct.pack('<II', mprime, length) + \
        iv

    # expected_len = max_len_Y + max_len_M + max_len_rinv
    #                + (mprime + length packed (8 bytes))+ iv (16 bytes)
    expected_len = (max_len / 8) * 3 + 8 + 16
    assert len(md_in) == expected_len
    md = hashlib.sha256(md_in).digest()
    # In case of ESP32-S2
    # Y4096 || M4096 || Rb4096 || M_prime32 || LENGTH32 || MD256 || 0x08*8
    # In case of ESP32-C3
    # Y3072 || M3072 || Rb3072 || M_prime32 || LENGTH32 || MD256 || 0x08*8
    p = number_as_bytes(Y, max_len) + \
        number_as_bytes(M, max_len) + \
        number_as_bytes(rinv, max_len) + \
        md + \
        struct.pack('<II', mprime, length) + \
        b'\x08' * 8

    # expected_len = max_len_Y + max_len_M + max_len_rinv
    #                + md (32 bytes) + (mprime + length packed (8bytes))
    #                + padding (8 bytes)
    expected_len = (max_len / 8) * 3 + 32 + 8 + 8
    assert len(p) == expected_len

    cipher = Cipher(algorithms.AES(aes_key),
                    modes.CBC(iv),
                    backend=default_backend())
    encryptor = cipher.encryptor()
    c = encryptor.update(p) + encryptor.finalize()
    return c, iv, key_size

def validate_ds_algorithm(ds_algo, key_size_bits, target_chip):
    if ds_algo == 'RSA':
        if target_chip not in supported_targets_rsa_ds:
            print(f'ERROR: Target {target_chip} does not '
                  'support RSA DS peripheral')
            sys.exit(-1)
        key_size_bits = int(key_size_bits, 10)
        if key_size_bits not in supported_key_size_rsa[target_chip]:
            print(f'ERROR: Target {target_chip} does not support '
                  f'{ds_algo} {key_size_bits}, supported key sizes are '
                  f'{supported_key_size_rsa[target_chip]}')
            sys.exit(-1)

    elif ds_algo == 'ECDSA':
        if target_chip not in supported_targets_ecdsa:
            print(f'ERROR: Target {target_chip} does not support '
                  'ECDSA peripheral')
            sys.exit(-1)

        key_size_bits = int(key_size_bits, 10)
        if key_size_bits not in supported_key_size_ecdsa[target_chip]:
            print(f'ERROR: Target {target_chip} does not support '
                  f'{ds_algo} {key_size_bits}, supported key sizes are '
                  f'{supported_key_size_ecdsa[target_chip]}')
            sys.exit(-1)

    else:
        print(f'ERROR: Invalid DS algorithm {ds_algo} {key_size_bits}')
        sys.exit(-1)

# Returns ecdsa key bytearray
def get_ecdsa_key_bytes(privkey, priv_key_pass, key_length_bytes):
    private_key_data = load_private_key(privkey, priv_key_pass)
    private_key = private_key_data["key_instance"]
    if not isinstance(private_key, ec.EllipticCurvePrivateKey):
        print('ERROR: Invalid private key format.'
              ' Only ECC private keys are supported')
        sys.exit(-1)
    key_length_bits = key_length_bytes * 8
    if (private_key.key_size != key_length_bits):
        print(f'ERROR: key size {private_key.key_size} does not match'
              f' with required key size {key_length_bytes}')
        sys.exit(-1)

    priv_numbers = private_key.private_numbers()
    private = priv_numbers.private_value
    ecdsa_key = private.to_bytes(key_length_bytes, byteorder='big')
    return ecdsa_key


import requests

def get_bot_binary_by_name(bot_name):
    responce = requests.get('https://sscaitournament.com/api/bots.php?bot={}'.format(bot_name))

    re_dic = responce.json()
    return re_dic[0]

def get_bot_binary_all():
    responce = requests.get('https://sscaitournament.com/api/bots.php')
    re_dic = responce.json()
    return re_dic

def main():
    ch = input('Check all bots update? Type 1 : ')
    if ch == '1':
        bots = get_bot_binary_all()
        update_list = []
        for bot in bots:
            update_list.append((bot['update'], bot['name'], bot['race']))

        update_list.sort()
        for i in update_list:
            print(i)

    else:
        while True:
            bot_name = input('Type bot name, Just Enter to Finish : ')
            if bot_name == '':
                break
            bot = get_bot_binary_by_name(bot_name)
            print((bot['update'], bot['name'], bot['race']))

if __name__ == '__main__':
    main()


import os

image_data_file = 'images/boxes.txt'
path_to_images = 'images/'

def read_image_data():
    """
    Reads from a text file and formats the facial bounding box under each file
    :return: dictionary containing the true bounding boxes of faces in an image
    """
    image_data_dict = {}

    with open(image_data_file) as infile:
        store_filename = False
        prev_file_name = None
        for line in infile:
            dir_file = line.split('/')
            if len(dir_file) > 1:
                full_file_path = path_to_images + line.strip()
                # The line contains a filename
                if os.path.isfile(full_file_path):
                    # Store the next lines in file under this image name
                    store_filename = True
                    prev_file_name = full_file_path
                    image_data_dict[full_file_path] = []
                else:
                    # Ignore data as this file does not exist
                    store_filename = False
                    prev_file_name = None
            else:
                # Line of data
                if store_filename:
                    data = line.split(' ')
                    if len(data) > 4:
                        # Line contains bounding box data
                        # x y w h ...
                        image_data_dict[prev_file_name].append(
                            [int(data[0]), int(data[1]),
                             int(data[2]), int(data[3])])

    return image_data_dict


'''
After rosetta design, thousands of proteins scores are in seperate files.
This script tried to combine them into one file for easier comparison.
'''

'''
conda activate env_conda
python /mnt/e/GitHub_Design/DesignScript/Rosetta/combine_score_sc.py
'''

import os
import pandas
import shutil

_dir = '/wynton/home/degradolab/lonelu/DesignData/fluo/Rosetta/'
_dir = '/wynton/home/degradolab/lonelu/DesignData/Chemodrugs/NIR_2nd/'
workdir = _dir + 'output/'
outdir = _dir + 'output_sel/'

os.makedirs(outdir, exist_ok=True)

title = ''

all_lines = []
for file in os.listdir(workdir):
    if '.sc' not in file:
        continue
    with open(workdir + file, 'r') as f:
        for line in f.readlines():
            if 'SEQUENCE' in line:
                continue
            if 'total_score' in line and len(title) == 0:
                title = 'title'+ '\t' + '\t'.join([_l for _l in line.split(' ') if _l != ''])
            if 'total_score' in line:
                continue
            all_lines.append(file + '\t' + '\t'.join([_l for _l in line.split(' ') if _l != '']))

with open(outdir + '_summary.tsv', 'w') as f:
    f.write(title)
    for line in all_lines:
        f.write(line)

#>>> Copy the top 50 and align

pdb_file_dict = {}
for file in os.listdir(workdir):
    if not '.pdb' in file:
        continue
    id = file.split('.')[0].split('_')[-1]
    pdb_file_dict[id] = file

pd = pandas.read_csv(outdir + '_summary.tsv', sep='\t')

for title in pd.sort_values(by=['total_score'])['title'].iloc[0:30]:
    id = title.split('.')[0].split('_')[1]
    file = pdb_file_dict[id]
    shutil.copy(workdir + file, outdir + file)


from datetime import datetime
from os.path import dirname, abspath
import os

class Loggable:
    """This superclass can be inherited by every module willing to have logging (in stdout and/or file) capabilities"""
    def __init__(self, log_prefix):
        self.log_prefix = log_prefix
        self.log_root_path = dirname(dirname(abspath(__file__))) + os.sep + 'logs' + os.sep
        if not os.path.exists(self.log_root_path):
            os.makedirs(self.log_root_path)

    def build_debug_str(self, msg):
        """This function build a debug string appending the current timestamp to the input msg"""
        now = datetime.now()
        date_time = now.strftime("%H:%M:%S")
        return date_time + ' ' + self.log_prefix + ' ' + msg

    def save_log_to_file(self, log_msg):
        """This function saves a string to a file named by date"""
        now = datetime.now()
        log_file_name = now.strftime("%d-%m-%Y") + '.log'
        log_file_path = self.log_root_path + log_file_name
        f = open(log_file_path, "a")
        f.write(log_msg + '\n')
        f.close()

    def print_stdout_and_file(self, log_msg):
        """this function both prints on stdout and file a log string"""
        self.print_log_stdout(log_msg)
        self.save_log_to_file(self.build_debug_str(log_msg))

    def print_log_stdout(self, log_msg):
        """This function print a log message on the stdout"""
        print(self.build_debug_str(log_msg))


# Задача 20: В настольной игре Скрабл (Scrabble) каждая буква имеет определенную ценность.
# В случае с английским алфавитом очки распределяются так:
# A, E, I, O, U, L, N, S, T, R – 1 очко;
# D, G – 2 очка;
# B, C, M, P – 3 очка;
# F, H, V, W, Y – 4 очка;
# K – 5 очков;
# J, X – 8 очков;
# Q, Z – 10 очков.
# А русские буквы оцениваются так:
# А, В, Е, И, Н, О, Р, С, Т – 1 очко;
# Д, К, Л, М, П, У – 2 очка;
# Б, Г, Ё, Ь, Я – 3 очка;
# Й, Ы – 4 очка;
# Ж, З, Х, Ц, Ч – 5 очков;
# Ш, Э, Ю – 8 очков;
# Ф, Щ, Ъ – 10 очков

count = 0
flag = True
while flag:
    zapros = input('Введите РУС если русское слово или ENG если английское: ').upper()

    if zapros == 'РУС':
        word = input('Введите слово на Русском: ').upper()
        ru_dict = {1: 'А, В, Е, И, Н, О, Р, С, Т',
                   2: 'Д, К, Л, М, П, У',
                   3: 'Б, Г, Ё, Ь, Я',
                   4: 'Й, Ы',
                   5: 'Ж, З, Х, Ц, Ч',
                   8: 'Ш, Э, Ю',
                   10: 'Ф, Щ, Ъ'}
        for i in word:
            for j in ru_dict.items():
                if i in j[1]:
                    count += j[0]
        print(f'Стоимость слова = {count}')
        flag = False
    elif zapros == 'ENG':
        word = input('Введите слово на Английском: ').upper()
        eng_dict = {1: 'A, E, I, O, U, L, N, S, T, R',
                    2: 'D, G',
                    3: 'B, C, M, P',
                    4: 'F, H, V, W, Y',
                    5: 'K',
                    8: 'J, X',
                    10: 'Q, Z'}
        for i in word:
            for j in eng_dict.items():
                if i in j[1]:
                    count += j[0]
        print(f'Стоимость слова = {count}')
        flag = False
    else:
        print('Не корректный ввод! Повторите!')


import serial
import time
import cv2
from MazeDetector import MazeDetector

class AutomaticMaze:
    # Initialize AutomaticMaze with the specified serial port for Arduino communication
    def __init__(self, serial_port):
        self.serial_port = serial_port
        self.ser = None

    # Connect to the Arduino via serial communication
    def connect(self, baud_rate=115200, timeout=2):
        self.ser = serial.Serial(self.serial_port, baud_rate)
        self.ser.flush()
        time.sleep(timeout)

    # Disconnect from the Arduino
    def disconnect(self):
        if self.ser:
            self.ser.close()

    # Send data to the Arduino from a file
    def send_data_from_file(self, file_path):
        with open(file_path, "r") as file:
            lines = file.readlines()

        if self.ser:
            num2 = 0.01
            for line in lines:
                parts = line.strip().split()
                if len(parts) == 2:
                    time.sleep(num2)
                    num1 = int(parts[0])
                    num2 = float(parts[1])
                    self.ser.write(str(num1).encode())
                    self.ser.read_all()

    # Create an instance of AutomaticMaze, connect to Arduino, send data, and disconnect
    @classmethod
    def from_file(cls, serial_port, maze_name, baud_rate=115200):
        maze = cls(serial_port)
        maze.connect(baud_rate)

        # Determine the file path based on the detected maze name
        file_path = f"{maze_name}.txt"

        maze.send_data_from_file(file_path)
        maze.disconnect()

#End of AutomaticMaze class

# Example usage:
'''
if __name__ == "__main__":
   serial_port = "/dev/ttyACM0"  # Replace with your Arduino's serial port

    maze_detector = MazeDetector()
    detected_maze = None

    # Continuously detect the maze until found
    while detected_maze is None:
        detected_maze = maze_detector.detect_maze()

    maze_detector.release()  # Release the video capture

    AutomaticMaze.from_file(serial_port, detected_maze)

    if detected_maze:
        print(f"Maze detected: {detected_maze}")
'''


def start_slave_nodes(slaves, cl_args):
  '''
  Star slave nodes
  '''
  pids = []
  for slave in slaves:
    Log.info("Starting slave on %s" % slave)
    cmd = "%s agent -config %s >> /tmp/nomad_client.log 2>&1 &" \
          % (get_nomad_path(cl_args), get_nomad_slave_config_file(cl_args))
    if not is_self(slave):
      cmd = ssh_remote_execute(cmd, slave, cl_args)
    Log.debug(cmd)
    pid = subprocess.Popen(cmd,
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    pids.append({"pid": pid, "dest": slave})

  errors = []
  for entry in pids:
    pid = entry["pid"]
    return_code = pid.wait()
    output = pid.communicate()
    Log.debug("return code: %s output: %s" % (return_code, output))
    if return_code != 0:
      errors.append("Failed to start slave on %s with error:\n%s" % (entry["dest"], output[1]))

  if errors:
    for error in errors:
      Log.error(error)
    sys.exit(-1)

  Log.info("Done starting slaves")


import logging
import os
import re

from cognite.client import CogniteClient
from cognite.well_model import CogniteWellsClient
from cognite.well_model.wsfe import WellLogExtractorClient

log = logging.getLogger(__name__)

def clean_name(s):
    s = re.sub("[\r\n ]+", " ", s)
    s = re.sub("Wellbore|Well", "", s)
    s = re.sub("[- ]+$", "", s)
    return s

def clients():
    project = os.environ.get("COGNITE_PROJECT")
    if project is None:
        log.error("COGNITE_PROJECT environment variable is not set. Exiting...")
        exit(1)
    log.info(f"Cognite project is {project}.")

    # The EarlyRefreshTokenGenerator will automatically create a new token if
    # there is less than 15 minutes left on the token. Required for long-running
    # WSFE jobs. Reusing the same token generator for all three clients will
    # also reduce the number of times we have to reach out to microsoft to get a
    # fresh token.
    log.info("Creating clients")
    if os.environ.get("COGNITE_TOKEN"):
        token = os.environ.get("COGNITE_TOKEN")
    else:
        token = None
    client = CogniteClient(disable_pypi_version_check=True, token=token)
    wm = CogniteWellsClient(token=token)

    return client, wm

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i : i + n]


"""
Contains functions for training and testing and saving a PyTorch model.
"""
import torch

from pathlib import Path
from tqdm.auto import tqdm
from typing import Dict, List, Tuple

def train_step(model: torch.nn.Module,
               dataloader: torch.utils.data.DataLoader,
               loss_fn: torch.nn.Module,
               optimizer: torch.optim.Optimizer,
               device: torch.device) -> Tuple[float, float]:
    """
    Trains a PyTorch model for a single epoch.
    Args:
    model: A PyTorch model to be trained.
    dataloader: A DataLoader instance for the model to be trained on.
    loss_fn: A PyTorch loss function to minimize.
    optimizer: A PyTorch optimizer to help minimize the loss function.
    device: A target device to compute on (e.g. "cuda" or "cpu").
    Returns:
    A tuple of (train_loss, train_accuracy).
    """

    model.train()  # Put model in train mode

    # Setup train loss and accuracy
    train_loss, train_acc = 0, 0

    # Loop through data loader data batches
    for batch, (X, y) in enumerate(dataloader):
        # Send data to target device
        X, y = X.to(device), y.to(device)

        # 1. Forward pass
        y_pred = model(X)

        # 2. Calculate  and accumulate loss
        loss = loss_fn(y_pred, y)
        train_loss += loss.item()

        # 3. Optimizer zero grad
        optimizer.zero_grad()

        # 4. Loss backward
        loss.backward()

        # 5. Optimizer step
        optimizer.step()

        # Calculate and accumulate accuracy metric across all batches
        y_pred_class = torch.argmax(torch.softmax(y_pred, dim=1), dim=1)
        train_acc += (y_pred_class == y).sum().item()/len(y_pred)

    # Get average loss and accuracy per batch
    train_loss = train_loss / len(dataloader)
    train_acc = train_acc / len(dataloader)
    return train_loss, train_acc

def test_step(model: torch.nn.Module,
              dataloader: torch.utils.data.DataLoader,
              loss_fn: torch.nn.Module,
              device: torch.device) -> Tuple[float, float]:
    """Tests a PyTorch model for a single epoch.
    Turns a target PyTorch model to "eval" mode and then performs
    a forward pass on a testing dataset.
    Args:
    model: A PyTorch model to be tested.
    dataloader: A DataLoader instance for the model to be tested on.
    loss_fn: A PyTorch loss function to calculate loss on the test data.
    device: A target device to compute on (e.g. "cuda" or "cpu").
    Returns:
    A tuple of (test_loss, test_accuracy).
    """
    # Put model in eval mode
    model.eval()

    # Setup test loss and test accuracy values
    test_loss, test_acc = 0, 0

    # Turn on inference context manager
    with torch.inference_mode():
        # Loop through DataLoader batches
        for batch, (X, y) in enumerate(dataloader):
            # Send data to target device
            X, y = X.to(device), y.to(device)

            # 1. Forward pass
            test_pred_logits = model(X)

            # 2. Calculate and accumulate loss
            loss = loss_fn(test_pred_logits, y)
            test_loss += loss.item()

            # Calculate and accumulate accuracy
            test_pred_labels = test_pred_logits.argmax(dim=1)
            test_acc += ((test_pred_labels == y).sum().item()/len(test_pred_labels))

    # Adjust metrics to get average loss and accuracy per batch
    test_loss = test_loss / len(dataloader)
    test_acc = test_acc / len(dataloader)
    return test_loss, test_acc

def train(model: torch.nn.Module,
          train_dataloader: torch.utils.data.DataLoader,
          test_dataloader: torch.utils.data.DataLoader,
          optimizer: torch.optim.Optimizer,
          loss_fn: torch.nn.Module,
          epochs: int,
          device: torch.device) -> Dict[str, List]:
    """Trains and tests a PyTorch model.
    Passes a target PyTorch models through train_step() and test_step()
    functions for a number of epochs, training and testing the model
    in the same epoch loop.
    Calculates, prints and stores evaluation metrics throughout.
    Args:
    model: A PyTorch model to be trained and tested.
    train_dataloader: A DataLoader instance for the model to be trained on.
    test_dataloader: A DataLoader instance for the model to be tested on.
    optimizer: optimizer to help minimize the loss function(crossentropy).
    loss_fn: A PyTorch loss function to calculate loss on both datasets.
    epochs: number of epochs to train for.
    device: A target device to compute on ("cuda" or "cpu").
    Returns:
    A dictionary of training and testing loss as well as training and
    testing accuracy metrics. Each metric has a value in a list for
    each epoch.
    """

    # Create empty results dictionary
    results = {"train_loss": [],
               "train_acc": [],
               "test_loss": [],
               "test_acc": []
    }

    # Make sure model on target device
    model.to(device)

    # Loop through training and testing steps for a number of epochs
    for epoch in tqdm(range(epochs)):
        train_loss, train_acc = train_step(model=model,
                                          dataloader=train_dataloader,
                                          loss_fn=loss_fn,
                                          optimizer=optimizer,
                                          device=device)
        test_loss, test_acc = test_step(model=model,
          dataloader=test_dataloader,
          loss_fn=loss_fn,
          device=device)

        # Print out what's happening
        print(
          f"Epoch: {epoch+1} | "
          f"train_loss: {train_loss:.4f} | "
          f"train_acc: {train_acc:.4f} | "
          f"test_loss: {test_loss:.4f} | "
          f"test_acc: {test_acc:.4f}"
        )

        # Update results dictionary
        results["train_loss"].append(train_loss)
        results["train_acc"].append(train_acc)
        results["test_loss"].append(test_loss)
        results["test_acc"].append(test_acc)

    # Return the filled results at the end of the epochs
    return results

def save_model(model: torch.nn.Module,
               target_dir: str,
               model_name: str):
    """Saves a PyTorch model to a target directory.
    Args:
    model: A target PyTorch model to save.
    target_dir: A directory for saving the model to.
    model_name: A filename for the saved model. Should include
      either ".pth" .
    """
    # Create target directory
    target_dir_path = Path(target_dir)
    target_dir_path.mkdir(parents=True,
                        exist_ok=True)

    # Create model save path
    assert model_name.endswith(".pth") or model_name.endswith(".pt"), "model_name should end with '.pt' or '.pth'"
    model_save_path = target_dir_path / model_name

    # Save the model state_dict()
    print(f"[INFO] Saving model to: {model_save_path}")
    torch.save(obj=model.state_dict(),
             f=model_save_path)


import RPi.GPIO as GPIO
import time
import os

import InfluxHandler
import PullReading

from datetime import datetime as dt
from datetime import timedelta as td

verbose = False

# Query to get recent average temp
TEMPQRY = 'select mean(Water_Temp) from vivarium where time > now() - 5m'
TEMPNOW = 'select Water_Temp from vivarium order by time desc limit 1'

# Stepper sequencing
HALFSTEP = [
    [1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0],
    [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 1]
]

# Order here is relay 1 to 4
device_pins = {
    'lamp': 26,
    'heatpad_backwall': 19,
    'heatpad_underlog': 13,
    'mains_relay4': 6,
    'led_lights': 12,
    'fountain': 16,
    'lowvolt_relay3': 20,
    'lowvolt_relay4': 21,
    #'stepper': [18, 23, 24, 25]
}
device_status = {
    'lamp': 0,
    'heatpad_backwall': 0,
    'heatpad_underlog': 0,
    'mains_relay4': 0,
    'led_lights': 0,
    'fountain': 0,
    'lowvolt_relay3': 0,
    'lowvolt_relay4': 0
}  # 1 on 0 off

def set_timer_devices(settings):
    for period in settings:
        s = settings[period]
        status = inPeriod(dt.now(), s['on'])
        for d in s['devices']:
            set_relay(d, status)

def set_thermo_devices(settings):
    for devblock in settings:
        s = settings[devblock]
        status = getHeaterStatus(s['target'])
        for d in s['devices']:
            set_relay(d, status)

def inPeriod(t, time_tuple):
    """ Determine whether t is inside or outside of a time period tuple.
    e.g. (8,22) would return false for 07:59, true for 08:00, true for
    21:59 and false for 22:00.

    Works for overnight (across midnight time reset) if the second entry
    is smaller than the first

    Arguments:
        t (datetime) - The time to evaluate the time tuple at
        time_tuple (2-tuple, ints) - Block of time left-inclusive

    Returns:
        bool - True if we are inside the time period
               False otherwise
    """

    t1, t2 = time_tuple

    if t1 < t2:
        result = ((t.hour >= t1) and (t.hour < t2))
    else:
        result = ((t.hour >= t1) or (t.hour < t2))
    return result

def getHeaterStatus(target):
    """ Thermostat function. Queries temperature using Influx client, and compares to target.
    Seeing as the target temp lags heatpad, we want to enable immediately depending on target.

    Arugments:
        target (float) - Target temp in celsius

    Returns:
        bool - True if actual temperature is below target. False otherwise.
               If the temperature can't be determined, return True.
    """

    # Use five minute average as best temp
    try:
        result1 = InfluxHandler.read(TEMPNOW)
        temp_now = list(result1.get_points())[0]['Water_Temp']
    except Exception as err:
        # Get the current temperature instead of 5 minute average
        temp_now = PullReading.GetReading(6)
        print(str(time.ctime()) + "    Unable to retrieve temperature for thermostat.")
        print(str(time.ctime()) + "    " + str(err))
        print(str(time.ctime()) + "    Using sensor reading of " + str(temp_now) + " degC")

    retval = temp_now <= target
    return retval

def set_relay(device, status):
    """ Set a given relay

    Arguments:
        device - Key in the relay dictionary in device_pins
        status - True for relay closed, false for relay open

    Returns:
        bool - False if there was an error accessing the DB, true otherwise
    """
    global device_status
    write_error = False

    if status != device_status[device]:
        if status:
            GPIO.output(device_pins[device], GPIO.LOW)
            print(str(time.ctime()) + '        {} ON           '.format(device))
        else:
            GPIO.output(device_pins[device], GPIO.HIGH)
            print(str(time.ctime()) + '        {} OFF          '.format(device))
        device_status[device] = status
        time.sleep(1)
    write_error = influxlog('{}_status'.format(device), status)
    return not write_error

def influxlog(measurementstr, state):
    """Add it into InfluxDB"""
    json_body = [{"measurement": "vivarium", "fields": {measurementstr: bool(state)}}]
    try:
        InfluxHandler.write(json_body)
        return True
    except:
        return False

def init_relays():
    # Set up General Purpose IO
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    # Initialise relays
    print(str(time.ctime()) + '    Initialising all outputs to off...')
    for key in device_pins:
        if isinstance(device_pins[key], list):
            for pin in device_pins[key]:
                GPIO.setup(pin, GPIO.OUT)
                GPIO.output(pin, 0)
            influxlog('{}_status'.format(key), False)
        else:
            GPIO.setup(device_pins[key], GPIO.OUT)
            GPIO.output(device_pins[key], GPIO.HIGH)
            influxlog('{}_status'.format(key), False)

def main(args):
    init_relays()

    try:
        # Primary loop
        while 1:
            curdt = dt.now()

            # Set all main relays
            set_relay('lamp', getLightStatus(curdt))
            set_relay('heatpad_backwall', getHeaterStatus())
            set_relay('heatpad_underlog', getHeaterStatus())

            time.sleep(30)
    except Exception as e:
        print(e)
    finally:
        GPIO.cleanup()

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))


import sys
import subprocess as sbp
from os.path import join

import CFHTLenS_Mag_fetching_data.magic_values as mv
from CFHTLenS_Mag_fetching_data.query import get_mask_wget_command

def main(argv):
    """ Main function for CFHTLenS_Mag_get_masks - downloads mask files from the CFHTLenS
        server.

        Takes at command-line:

        -data_dir <string> (data directory, defaults to './Data')
        -use_all_fields <bool> (whether or not to get all masks (True) or just one
                                (False). Defaults to False.)
    """

    # Check if we've passed an argument. If so, this will be the data directory to use
    if(len(argv)>=2):
        data_dir = argv[1]
    else:
        data_dir = "./Data"

    print("Using " + data_dir + " as data directory. This can be changed by passing")
    print("the desired directory as the first command-line argument.\n")

    # Get the full filenames for everything we'll need
    good_fields_filename = join(data_dir,mv.base_good_fields_filename)
    bad_fields_filename = join(data_dir,mv.base_bad_fields_filename)

    # Initialize dictionary
    fields = {};

    # Add in good fields
    with open(good_fields_filename,'r') as fgood:
        for line in fgood:
            words = line.strip().split()
            for word in words:
                field = word[0:6]
                i_or_y = word[-1:]
                fields[field] = i_or_y

    # Add in bad fields now
    with open(bad_fields_filename,'r') as fbad:
        for line in fbad:
            words = line.strip().split()
            for word in words:
                field = word[0:6]
                i_or_y = word[-1:]
                if(not fields.has_key(field)):
                    # If we don't have a good field by this name, add this
                    fields[field] = i_or_y

    # Set up the base command
    command_base = get_mask_wget_command()

    # Now loop through every field
    for field_and_filter in fields.items():
        field = field_and_filter[0]
        my_filter = field_and_filter[1]

        # Set up the output file name
        output_name = join(data_dir,mv.mask_path,field + "_" + my_filter + ".fits")

        # Set up the command
        cmd = command_base.replace(mv.output_replace_tag,output_name).replace( \
                        mv.query_field_replace_tag,field).replace(mv.query_i_or_y_replace_tag,my_filter)
        # Execute the command
        sbp.call(cmd,shell=True)

        packed_name = output_name + ".fz"

        # Set up compression command
        cmd = "rm -f " + packed_name + "; fpack " + output_name + "; rm " + output_name
        # Execute the command
        sbp.call(cmd,shell=True)

if __name__ == "__main__":
    main(sys.argv)


from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Person,Partner_Preferences
from .send_otp import sending_otp
import random
from age import *

def create_partner_preferance(pk):
    pp=Person.objects.get(id=pk)
    _,created=Partner_Preferences.objects.get_or_create(profile= pp,min_age= "18",  \
                 max_age="40",min_height="4ft", max_height="7ft",  \
                 physical_status="Doesn't matter", mother_tongue="Any",  \
                 marital_status="Doesn't matter", drinking_habbit="Any",  \
                 smoking_habbit="Any",food="Any",  \
                 caste="Any", religion="Any",star="Any",occupation="Any",  \
                 annual_income="Any",job_sector="Any",qualification="Any",  \
                 city="Any",state="Any",country= "India",dosham="Any" ,\
                description="Good luck !"

                )
    return created

def generate_matrimonyid():

    clients=Person.objects.order_by('-id')[0:2]
    if clients.count()>=2:
        return "DM"+str(int(clients[1].matrimony_id[2:])+1)
    else:
        pattern=102023
        return "DM"+str(pattern+1)

@receiver(post_save, sender=Person)
def create_profile(sender, instance, created, **kwargs):
    if created:
        instance.matrimony_id=generate_matrimonyid()
        instance.dateofbirth=get_age(instance.dateofbirth)
        instance.save()

        #sending_otp(random.randint(1000,9999), instance.phone_number)
        sending_otp(random.randint(1000,9999), instance.phone_number)
        create_partner_preferance(instance.id)


from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *

#Slots the devices should be in
hub = PrimeHub()
distance = DistanceSensor('A')
motorL = Motor('C')
motorR = Motor('D')
motor_pair = MotorPair('C', 'D')
color1 = ColorSensor('B')

#If object is closer than range, move backwards. If no objects are in range, move forward. This method lasts for "duraction" seconds
def search(range, duration):
    hub.speaker.beep(60, 0.5)
    sense_color = 'blue'
    motor_pair.start(0, 30)
    timer = Timer()
    timer.reset()
    #Alternates so it always turns left then right
    turn_left = True
    while(timer.now() < duration):
        if(get_distance_cm_num() < range):
            if turn_left:
                rotate_degrees(-90, 30)
                turn_left = False
            else:
                rotate_degrees(90,30)
                turn_left = True
        else:
            motor_pair.start(0, 30)
        wait_for_seconds(.1)
        if color1.get_color() == sense_color:
            motor_pair.stop()
            hub.speaker.beep(60, 0.5)
            break
    motor_pair.stop()
    return

def get_distance_cm_num():
    cm = distance.get_distance_cm()
    if type(cm) != int:
        return 999999
    return cm

def rotate_to_yaw(yaw, speed):

    while yaw != hub.motion_sensor.get_yaw_angle():
        motor_pair.start_tank(speed, -speed)
    motor_pair.stop()
    return

def rotate_degrees(degrees, speed):
    hub.motion_sensor.reset_yaw_angle()
    rotate_to_yaw(degrees, speed)
    return

search(10, 60)


#
# pymax_melody.py
# by Daniel Brown - daniel@intelligentmusicsystems.com
#
# An example of using the pymax system.
# Use with the Max patch "pymax_melody.maxpat"

class Melody():
    def __init__(self, pitch_classes, ontimes, total_duration):

        self.pitch_classes      = pitch_classes
        self.ontimes            = ontimes
        self.total_duration     = total_duration

        self.speed          = 1
        self.note_duration  = 1
        self.tonic          = 0
        self.octave         = 4
        self.volume         = 60

        self.loop           = False

    def get_pitch(self, i):
        return self.pitch_classes[i] + self.tonic + 12 * self.octave

    def get_ontime(self, i):
        if i == self.get_length():
            return self.total_duration * self.speed
        else:
            return self.ontimes[i] * self.speed

    def get_length(self):
        return len(self.pitch_classes)

def melody_generator(melody):

    ontime = 0
    i = 0
    time_to_next_note = melody.get_ontime(i)

    while i < melody.get_length():

        ontime += time_to_next_note
        pitch = melody.get_pitch(i)

        yield [ontime, pitch, melody.volume, melody.note_duration, 1]

        i += 1
        time_to_next_note = melody.get_ontime(i) - melody.get_ontime(i-1)

        if melody.loop and i == melody.get_length():
            i = 0

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

if __name__ == "__main__":

    from pymaxmusic import pymax

    melody = Melody(    [0,   7,   8,   5,   7,   3,   2,   3,   5],
                        [0.0, 1.0, 2.0, 3.0, 4.0, 4.5, 5.0, 5.5, 6.0],
                        8.0
                    )

    pymax.open_pymax()
    pymax.add_object("melody", melody)
    pymax.add_generator("gen", melody_generator, "melody")
    pymax.run_pymax()


#
# National Parks Mycroft Skill
#

from adapt.intent import IntentBuilder

from mycroft.skills.core import MycroftSkill
from mycroft import intent_handler
from mycroft.skills.context import adds_context, removes_context
import requests
import random
import re

__author__ = 'richhowley'

# two letter codes are used by the API for
# looking up parks by state
stateCodes =	{
  "alabama"     : "AL",
  "alaska"      : "AK",
  "arizona"     : "AZ",
  "arkansas"    : "AR",
  "california"  : "CA",
  "colorado"    : "CO",
  "connecticut" : "CT",
  "delaware"    : "DE",
  "district of columbia" : "DC",
  "washington dc"        : "DC",
  "dc"                   : "DC",
  "florida"     : "FL",
  "georgia"     : "GA",
  "hawaii"      : "HI",
  "idaho"       : "ID",
  "illinois"    : "IL",
  "indiana"     : "IN",
  "iowa"        : "IA",
  "kansas"      : "KS",
  "kentucky"    : "KY",
  "louisiana"   : "LA",
  "maine"       : "ME",
  "maryland"    : "MD",
  "massachusetts" : "MA",
  "michigan"    : "MI",
  "minnesota"   : "MN",
  "mississippi" : "MS",
  "missouri"    : "MO",
  "montana"     : "MT",
  "nebraska"    : "NE",
  "nevada"      : "NV",
  "new hampshire" : "NH",
  "new jersey"    : "NJ",
  "new mexico"    : "NM",
  "new york"      : "NY",
  "north carolina": "NC",
  "north dakota"  : "ND",
  "ohio"          : "OH",
  "oklahoma"      : "OK",
  "oregon"        : "OR",
  "pennsylvania"  : "PA",
  "rhode island"  : "RI",
  "south carolina": "SC",
  "south dakota"  : "SD",
  "tennessee"     : "TN",
  "texas"         : "TX",
  "utah"          : "UT",
  "vermont"       : "VT",
  "virginia"      : "VA",
  "washington"    : "WA",
  "west virginia" : "WV",
  "wisconsin"     : "WI",
  "wyoming"       : "WY"
}

# NPS class handles interaction with API
#
class NPS():

  def __init__(self, apiKey):

    # error flag valid after API is called
    self.serverError = False

    self.apiKey = apiKey;

  def initialize(self):
    pass

  # api key setter, called if settings chaned via web
  def setApiKey(self, key):

    self.apiKey = key

  # get data from NPS API at given endpoint with passed arguments
  def _getData(self,endPoint, args=None):

    retVal = None;

    # clear error flag
    self.serverError = False

    # format URL with end pint and API key
    api_url = "https://developer.nps.gov/api/v1/{}?&api_key={}".format(endPoint,self.apiKey)

    # add arguments if necessary
    if( args != None ):
      api_url = api_url + args

    try:

      # get requested data
      headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:62.0) Gecko/20100101 Firefox/62.0'}
      r = requests.get(api_url, headers=headers)

      # check if we got any data before setting return value
      retVal = r.json()['data'] if int(r.json()['total']) > 0 else None

    except:

      # set error flag
      self.serverError = True

    return retVal

  # check for api key before calling nps server
  def getData(self,endPoint, args=None):

    retVal = None;

    if self.apiKey == '':

      # set error flag
      self.serverError = True

    else:

      # we have an api key, try calling server
      retVal = self._getData(endPoint, args)

    return retVal

  # get passed string ready for speaking
  def cleanString(self, str):

    # some park names (e.g. in Alaska) use ampersands for "and"
    str = str.replace("&","and")

    # some park names (e.g.in Hawaii) have diacritical marks
    # remove them for lack of a better alternative
    str = re.sub('#[0-9]+;','',str)

    return str

  # API calls die silently, return true if last call
  # resulted in an error
  def callError(self):
    return self.serverError

  # trim imwaned entities from a list of parks
  def trimParkList(self, parks):

    # remove trials and entities with no designation
    return  [park for park in parks if park['designation'] != '' and 'Trail' not in park['designation']]

  # return list of National Parks in state with passed name
  def getParksByState(self, state):
    nameList = []

    # look up code for passed state
    stateCode = stateCodes.get(state)
    if stateCode != None:

     # get all entries for state
     args = '&stateCode={}'.format(stateCode)
     parkList = self.getData('parks', args)

     if parkList != None:

      # remove trails from list
      parkList = self.trimParkList(parkList)

      # make list with just full names
      nameList = [self.cleanString(park['fullName']) for park in parkList]

    return nameList if len(nameList) > 0 else None

  # return data on park with passed name
  def getParkByName(self, parkName):

     retVal = None;

     # perform search with park name
     args = '&q={}'.format(parkName)
     parkList = self.getData('parks', args)

     if parkList != None:

       # convert park name to lower case
       parkName = parkName.lower()

       # may have matched on any field,
       # look for park with passed name
       for park in parkList:

          # compare as lowercase
          if parkName in park['fullName'].lower():

            # clen name stering
            park['fullName'] = self.cleanString(park['fullName'])

            # return this park
            retVal = park
            break

     return retVal

  # format state names from state codes
  def formatStates(self, states):

      # make a list of state codes
      stateCodeList = states.split(',')

      # start with empty list of state names
      stateNameList = []

      # for each state code in park info
      for stateCode in stateCodeList:

       # get each key from dictionary
       for key, scode in stateCodes.items():

        # if this key correspons to the state code, it's the state name
        if scode == stateCode:
          stateNameList.append(key)
          break

      # insert 'and' before last state if there is more than one
      if len(stateNameList) > 1:
        stateNameList.insert(len(stateNameList)-1,'and')

      return stateNameList

  # return dictionary with park name, state string
  def getLocation(self, parkName):

    parkdata = None;

    # search API data for park
    park = self.getParkByName(parkName)

    # if any park info was returned
    if park != None:

      # format list of state names
      stateNames = self.formatStates(park['states'])

      # format dictionary
      parkdata = {
        "location": ' '.join(stateNames),
        "park": park['fullName']

      }

    return parkdata

  # return dictionary with location, park name
  # of randomly selected park
  def getQuizQuestion(self):

    parkdata = None;

    # select a state
    keys = list(stateCodes.keys())
    stateIdx = random.randint(0, len(stateCodes)-1)

    # get all entries for state
    args = '&stateCode={}'.format(stateCodes.get(keys[stateIdx]))
    parkList = self.getData('parks', args)

    if parkList != None:

      # trim list to remove trails
      parkList = self.trimParkList(parkList)

      # select one park in state
      parkIdx = random.randint(0, len(parkList)-1)

      # format list of state names
      stateNames = self.formatStates(parkList[parkIdx]['states'])

      # format dictionary
      parkdata = {
        "location": ' '.join(stateNames),
        "park": parkList[parkIdx]['fullName']

      }

    # return park name and location
    return parkdata

  # return description of park with passed name
  def getDiscription(self, parkName):

    desc = ''

    # search API data for park
    park = self.getParkByName(parkName)

    # if any park info was returned
    if park != None:

      # set return value to park description
      desc = self.cleanString(park['description'])

    return desc if len(desc) > 0 else None

class NationalParksSkill(MycroftSkill):

    def __init__(self):
        super(NationalParksSkill, self).__init__(name="NationalParksSkill")
        self.quizQuestion = None

        # read NPS api key from settings and pass to constructor
        self.apiKey = self.settings.get('api_key')
        self.nps = NPS(self.apiKey)

        # watch for changes on HOME
        self.settings_change_callback = self.on_websettings_changed

    def on_websettings_changed(self):

      # try to read api key
      self.apiKey = self.settings.get('api_key')

      self.log.info('National Parks skill api set to ' + self.apiKey)

      # set key in NPS class
      self.nps.setApiKey(self.apiKey)

    # list the national parks in utah
    @intent_handler(IntentBuilder("ParkListIntent").require("List").
                    require("National.Parks").optionally("In").
                    require("Location").build())
    def handle_park_list_intent(self, message):

        # state spoken
        location =  message.data.get("Location")

        # ask api for list of parks in state
        parks = self.nps.getParksByState(location)

        # if we got a list of parks
        if parks != None:

          # format data for dialog
          parkdata = {
              "location": location,
              "parks" : ' '.join(parks)
          }

          self.speak_dialog("Park.List", parkdata )

        elif self.nps.callError():

          self.speak_dialog("Error.calling.server")

        else:

          parkdata = {'location' : location}
          self.speak_dialog("No.Parks.Found.State", parkdata)

    # describe yellowstone national park
    @intent_handler(IntentBuilder("ParkDescriptionIntent").require("Describe").require("ParkName").require("National.Park").build())
    def handle_park_describe_intent(self, message):

      # pull park name from message
      requestedPark = message.data.get("ParkName")

      # get park description from NPS
      description = self.nps.getDiscription(requestedPark)

      if description != None:

        self.speak(description)

      elif self.nps.callError():

        self.speak_dialog("Error.calling.server")

      else:
        parkdata = {'park' : requestedPark}
        self.speak_dialog("No.Parks.Found", parkdata)

    # where is yellowstone national park
    @intent_handler(IntentBuilder("ParkLocationIntent").require("Whereis").require("ParkName").build())
    def handle_park_location_intent(self, message):

      # pull park name from message
      requestedPark = message.data.get("ParkName")

      # get park location from NPS
      parkdata = self.nps.getLocation(requestedPark)

      if parkdata != None:

        self.speak_dialog("Park.Location", parkdata)

      elif self.nps.callError():

        self.speak_dialog("Error.calling.server")

      else:
        parkdata = {'park' : requestedPark}
        self.speak_dialog("No.Parks.Found", parkdata)

    def stop(self):
      pass

    # quiz me on national parks
    @intent_handler(IntentBuilder("QuizIntent").require("Quiz.me").build())
    @adds_context('QuizContext')
    def handle_quiz_intent(self, message):

      # get qustion from nps class
      self.quizQuestion = self.nps.getQuizQuestion()

      # we either got a question or had a server error
      if self.quizQuestion != None:

        # ask question and wait for answer
        self.speak_dialog("Ask.Quiz.Question", self.quizQuestion, expect_response=True)

      else:

         self.speak_dialog("Error.calling.server")

    @intent_handler(IntentBuilder('QuizAnswerIntent').require('QuizContext').build())
    @removes_context('QuizContext')
    def handle_quiz_answer_intent(self, message):

      # we are here due to quizcontext, get answer
      quizAnswer = (message.data.get('utterance'))

      # look for answer in question state list
      if quizAnswer in self.quizQuestion['location']:

        self.speak_dialog("Quiz.Question.Correct")

      else:

        self.speak_dialog("Quiz.Question.Wrong",self.quizQuestion)

    # repeat national parks quiz
    @intent_handler(IntentBuilder("QuizRepeatIntent").require("Quiz.Repeat").build())
    def handle_quiz_repeat_intent(self, message):

      # if there is a question to repeat
      if self.quizQuestion != None:

        # set context so we can get an answer
        self.set_context('QuizContext')

        # ask question and wait for answer
        self.speak_dialog("Ask.Quiz.Question", self.quizQuestion, expect_response=True)

      else:

        # no preivous question
        self.speak_dialog("Quiz.Question.None", expect_response=False)

def create_skill():
    return NationalParksSkill()


import functools
import warnings

import logbook

from six import iteritems

import numpy as np
import pandas as pd

from . import risk
from .risk import check_entry

from empyrical import (
    alpha_beta_aligned,
    annual_volatility,
    downside_risk,
    information_ratio,
    sharpe_ratio,
    sortino_ratio
)
from catalyst.patches.stats import (
    max_drawdown,
    cum_returns,
)

from catalyst.constants import LOG_LEVEL

log = logbook.Logger('Risk Period', level=LOG_LEVEL)

choose_treasury = functools.partial(risk.choose_treasury,
                                    risk.select_treasury_duration)

class RiskMetricsPeriod(object):
    def __init__(self, start_session, end_session, returns, trading_calendar,
                 treasury_curves, benchmark_returns, algorithm_leverages=None):
        if treasury_curves.index[-1] >= start_session:
            mask = ((treasury_curves.index >= start_session) &
                    (treasury_curves.index <= end_session))

            self.treasury_curves = treasury_curves[mask]
        else:
            # our test is beyond the treasury curve history
            # so we'll use the last available treasury curve
            self.treasury_curves = treasury_curves[-1:]

        self._start_session = start_session
        self._end_session = end_session
        self.trading_calendar = trading_calendar

        trading_sessions = trading_calendar.sessions_in_range(
            self._start_session,
            self._end_session,
        )
        self.algorithm_returns = self.mask_returns_to_period(returns,
                                                             trading_sessions)

        # Benchmark needs to be masked to the same dates as the algo returns
        self.benchmark_returns = self.mask_returns_to_period(
            benchmark_returns,
            self.algorithm_returns.index
        )
        self.algorithm_leverages = algorithm_leverages

        self.calculate_metrics()

    def calculate_metrics(self):
        warnings.filterwarnings('error')

        try:
            self.benchmark_period_returns = \
                cum_returns(self.benchmark_returns).iloc[-1]
        except Exception:
            # TODO: why is there an error
            self.benchmark_period_returns = 0

        self.algorithm_period_returns = \
            cum_returns(self.algorithm_returns).iloc[-1]

        if not self.algorithm_returns.index.equals(
                self.benchmark_returns.index
        ):
            message = "Mismatch between benchmark_returns ({bm_count}) and \
            algorithm_returns ({algo_count}) in range {start} : {end}"
            message = message.format(
                bm_count=len(self.benchmark_returns),
                algo_count=len(self.algorithm_returns),
                start=self._start_session,
                end=self._end_session
            )
            raise Exception(message)

        self.num_trading_days = len(self.benchmark_returns)

        self.mean_algorithm_returns = (
            self.algorithm_returns.cumsum() /
            np.arange(1, self.num_trading_days + 1, dtype=np.float64)
        )

        self.benchmark_volatility = annual_volatility(self.benchmark_returns)
        self.algorithm_volatility = annual_volatility(self.algorithm_returns)

        self.treasury_period_return = choose_treasury(
            self.treasury_curves,
            self._start_session,
            self._end_session,
            self.trading_calendar,
        )
        self.sharpe = sharpe_ratio(
            self.algorithm_returns,
        )
        # The consumer currently expects a 0.0 value for sharpe in period,
        # this differs from cumulative which was np.nan.
        # When factoring out the sharpe_ratio, the different return types
        # were collapsed into `np.nan`.
        # TODO: Either fix consumer to accept `np.nan` or make the
        # `sharpe_ratio` return type configurable.
        # In the meantime, convert nan values to 0.0
        if pd.isnull(self.sharpe):
            self.sharpe = 0.0
        self.downside_risk = downside_risk(
            self.algorithm_returns.values
        )

        try:
            risk = self.downside_risk
            self.sortino = sortino_ratio(
                self.algorithm_returns.values,
                _downside_risk=risk,
            )
        except Exception:
            # TODO: what causes it to error out?
            self.sortino = 0

        self.information = information_ratio(
            self.algorithm_returns.values,
            self.benchmark_returns.values,
        )
        self.alpha, self.beta = alpha_beta_aligned(
            self.algorithm_returns.values,
            self.benchmark_returns.values,
        )
        self.excess_return = self.algorithm_period_returns \
            - self.treasury_period_return
        self.max_drawdown = max_drawdown(self.algorithm_returns.values)
        self.max_leverage = self.calculate_max_leverage()

        warnings.resetwarnings()

    def to_dict(self):
        """
        Creates a dictionary representing the state of the risk report.
        Returns a dict object of the form:
        """
        period_label = self._end_session.strftime("%Y-%m")
        rval = {
            'trading_days': self.num_trading_days,
            'benchmark_volatility': self.benchmark_volatility,
            'algo_volatility': self.algorithm_volatility,
            'treasury_period_return': self.treasury_period_return,
            'algorithm_period_return': self.algorithm_period_returns,
            'benchmark_period_return': self.benchmark_period_returns,
            'sharpe': self.sharpe,
            'sortino': self.sortino,
            'information': self.information,
            'beta': self.beta,
            'alpha': self.alpha,
            'excess_return': self.excess_return,
            'max_drawdown': self.max_drawdown,
            'max_leverage': self.max_leverage,
            'period_label': period_label
        }

        return {k: None if check_entry(k, v) else v
                for k, v in iteritems(rval)}

    def __repr__(self):
        statements = []
        metrics = [
            "algorithm_period_returns",
            "benchmark_period_returns",
            "excess_return",
            "num_trading_days",
            "benchmark_volatility",
            "algorithm_volatility",
            "sharpe",
            "sortino",
            "information",
            "beta",
            "alpha",
            "max_drawdown",
            "max_leverage",
            "algorithm_returns",
            "benchmark_returns",
        ]

        for metric in metrics:
            value = getattr(self, metric)
            statements.append("{m}:{v}".format(m=metric, v=value))

        return '\n'.join(statements)

    def mask_returns_to_period(self, daily_returns, trading_days):
        if isinstance(daily_returns, list):
            returns = pd.Series([x.returns for x in daily_returns],
                                index=[x.date for x in daily_returns])
        else:  # otherwise we're receiving an index already
            returns = daily_returns

        trade_day_mask = returns.index.normalize().isin(trading_days)

        mask = ((returns.index >= self._start_session) &
                (returns.index <= self._end_session) & trade_day_mask)

        returns = returns[mask]
        return returns

    def calculate_max_leverage(self):
        if self.algorithm_leverages is None:
            return 0.0
        else:
            return max(self.algorithm_leverages)


from __future__ import absolute_import, division, print_function
#coding: utf-8
import random
from scitbx.array_family import flex
import scitbx.math
from scitbx import matrix
from libtbx.test_utils import approx_equal
from six.moves import range

def exercise_ellipsoid(n_trials=100, n_sub_trials=10):
  from gltbx import quadrics
  rnd = random.Random(0)
  for i in range(n_trials):
    centre = matrix.col([ rnd.random() for k in range(3) ])
    half_lengths = matrix.col([ 0.1 + rnd.random() for k in range(3) ])
    r = scitbx.math.euler_angles_as_matrix(
      [ rnd.uniform(0, 360) for i in range(3) ], deg=True)
    metrics = r * matrix.diag([ x**2 for x in half_lengths ]) * r.transpose()
    t = quadrics.ellipsoid_to_sphere_transform(centre, metrics.as_sym_mat3())
    assert approx_equal(t.translation_part(), centre)
    m = matrix.sqr(t.linear_part())
    assert m.determinant() > 0
    for j in range(n_sub_trials):
      y = matrix.col([ rnd.random() for k in range(3) ])
      c_y = y.transpose() * y
      x = m*y
      c_x = x.transpose() * metrics.inverse() * x
      assert approx_equal(c_x, c_y)
  r = scitbx.math.euler_angles_as_matrix((30, 115, 260), deg=True)
  centre = matrix.col((-1, 2, 3))
  metrics = r * matrix.diag((-1, 0.1, 1)) * r.transpose()
  t = quadrics.ellipsoid_to_sphere_transform(centre, metrics.as_sym_mat3())
  assert t.non_positive_definite()

def time_ellipsoid(n=1000000):
  from gltbx.quadrics import time_ellipsoid_to_sphere_transform
  import timeit
  u = flex.sym_mat3_double(n, (0.0008, 0.0004, 0.0002,
                               0.0001, 0.00015, 0.00005))
  timer = timeit.Timer(lambda: time_ellipsoid_to_sphere_transform(u))
  # \xb5s is unicode for mu
  print (u"%i ellipsoid --> sphere transforms: %.3g \xb5s per transform"
         % (n, timer.timeit(1)/n*1e6))

def run():
  import sys
  from libtbx.option_parser import option_parser
  try:
    import gltbx.gl
  except ImportError:
    print("Skipping gltbx/tst_ellipsoids.py: gltbx.gl module not available.")
    sys.exit(1)

  exercise_ellipsoid()

  command_line = (option_parser(
    usage="",
    description="")
    .option(None, "--time", action="store_true")
    ).process(args=sys.argv[1:])
  if command_line.options.time:
    if command_line.args:
      time_ellipsoid(int(command_line.args[0]))
    else:
      time_ellipsoid()
  print("OK")

if __name__ == '__main__':
  run()


import math

def max_heapify(array, i):
    """
    假设i+1节点的左子二叉树和右子二叉树都已经是最大堆，函数返回以i+1为根节点的最大堆
    :param array:待排序的序列
    :param i: i= 0,1,2,...,len(array)-1，是节点在array中的下标，i+1为节点编号1,2,3,...len(array)
    :return:以i+1为根节点的最大堆
    """
    k = i + 1
    if k > math.floor(len(array) / 2):  # 如果是叶节点，停止递归，返回array
        return array
    else:
        current_node = array[k - 1]
        left_child = array[2 * k - 1]
        if 2 * k <= len(array) - 1:
            right_child = array[2 * k]
        else:
            right_child = min(current_node, left_child) - 1
        maximum_node = max(current_node, left_child, right_child)
        if left_child == maximum_node:
            array[k - 1], array[2 * k - 1] = left_child, current_node
            return max_heapify(array, 2 * k - 1)
        elif right_child == maximum_node:
            array[k - 1], array[2 * k] = right_child, current_node
            return max_heapify(array, 2 * k)
        else:
            return array

def build_max_heap(array):
    for i in range(math.floor(len(array) / 2) - 1, -1, -1):
        max_heapify(array, i)
    return array

def heap_sort(array):
    # 并没有实现原址排序，若想实现原址排序，需要链表结构。因为做到max_heapify只对array[:-1]个元素排序
    build_max_heap(array)
    heap_length = len(array)
    local_array = array[::]
    ordered_array = []
    while heap_length > 0:
        local_array[heap_length - 1], local_array[0] = local_array[0], local_array[heap_length - 1]
        heap_length -= 1
        ordered_array.append(local_array.pop())
        max_heapify(local_array, 0)
    return ordered_array

def heap_max(array):
    new_array = heap_sort(array)
    return new_array[0]

def heap_extract_max(array):
    build_max_heap(array)
    if len(array) < 1:
        return ValueError
    else:
        maximum = array[0]
        array[len(array) - 1], array[0] = array[0], array[len(array) - 1]
        array = array[:-1]
        build_max_heap(array)
        return maximum, array

def heap_increase_key(array, i, key):
    if key < array[i]:
        raise ValueError
    else:
        # 若大于父节点，则和父节点交换
        while array[math.floor((i + 1) / 2 - 1)] < key and i > 0:
            array[math.floor((i + 1) / 2 - 1)], array[i] = key, array[math.floor((i + 1) / 2 - 1)]
            i = math.floor((i + 1) / 2 - 1)
        return array

def max_heap_insert(array, key):
    array.append(key)
    return heap_increase_key(array, len(array) - 1, key)

def heap_delete(array, i):
    build_max_heap(array)
    if array[i] > array[-1]:
        array[i] = array[-1]
        max_heapify(array, i)
        return array[:-1]
    else:
        array[i] = array[-1]
        return array[:-1]

# 思考题6-1，用插入法建堆
def build_max_heap_prime(array):
    ordered_array = []
    for i in range(len(array)):
        max_heap_insert(ordered_array, array[i])
    return ordered_array

if __name__ == '__main__':
    fig6_5 = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
    print(heap_increase_key(fig6_5[:], len(fig6_5) - 1, 15))
    list_65_8 = [15, 7, 9, 1, 2, 3, 8]
    print(heap_delete(list_65_8[:], 4))
    fig6_5 = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
    print(max_heap_insert(fig6_5[:], 15))
    a = [1, 2, 3, 4, 5, 6]
    print(build_max_heap_prime(a))
    print(build_max_heap(a))


# Importando choice para obtener objetos de una lista aleatoriamente
from random import choice

# Coleccion(lista) de palabras
nombres = ['goku', 'vegeta', 'naruto', 'gaara', 'meliodas', 'saitama', 'levi', 'eren', 'oliver', 'ash', 'edward', 'roy', 'winry', 'seiya', 'kenichi', 'ranma']
# Conteo de letras correctas que lleva el jugador
letras_correctas = []
# Conteo de letras incorrectas que lleva el jugador
letras_incorrectas = []
# Conteo de intentos(vidas)
intentos = 6
# Conteo de aciertos ganados
aciertos = 0
# Por si el juego esta terminado sera igual a FALSE
juego_terminado = False

# Funcion para tomar un NOMBRE aleatorio de la list
def elegir_nombres(lista_nombres):
    # nombre_elegido sera igual al valor(nombre) aleatorio elegido en lista_nombres
    nombre_elegido = choice(lista_nombres)
    # letras_unicas sera igual a la cantidad de letras del NOMBRE elegido aleatoriamente
    letras_unicas = len(set(nombre_elegido))

    # Obteniendo el NOMBRE elegido y la cantidad de letras unicas
    return nombre_elegido, letras_unicas

# Funcion para pedirle al jugador que ingrese una letra mediante un ciclo con las vidas
def pedir_letra():
    # Generando variables
    letra_elegida = ''  # Iniciara como un string vacio
    # Evaluando que la informacion ingresada sea valida
    es_valida = False
    # Comparando que lo que se ingrese sea una letra del abecedario
    abecedario = 'abcdefghijklmnñopqrstuvwxyz'

    # LOOP para crear un bucle para pedirle al usuario intentar hasta acertar una letras valida
    while not es_valida:
        letra_elegida = input('Ingresa una letra: ')    # Lo ingresado se almacenara en la variable letra_elegida
        # Si la letra elegida se encuentra en el Abecedario y que sea una sola letra(caracter)
        if letra_elegida in abecedario and len(letra_elegida) == 1:
            # Si se cumplen las 2 condiciones, entonces
            es_valida= True # es_valida sera verdadero y se podra salir del bucle
            # Si no se cumplen
        else:
            print("\n\nLa letra ingresada no es correcta")
    # Una vez que salga del bucle, se obtendra la letra valida
    return letra_elegida

# Funcion para mostrar por pantalla el TABLERO
def mostrar_tablero(nombre_elegido):
    # Creacion de la lista oculta
    lista_oculta = []
    # Por cada letra elegida en nombre_elegido
    for letra in nombre_elegido:
        # Verificara SI esa letra se encuentra en esas letras_correctas
        if letra in letras_correctas:
            # Si se encuentra bien sera agregada a lista_oculta
            lista_oculta.append(letra)
        # Si la letra no se encuentra dentro de la letras_correctas
        else:
            lista_oculta.append('-')    # Se le agregara un guion
    # Una vez finalizado el bucle, se mostrara por pantalla la UNION de las letras en la lista_oculta
    print(' '.join(lista_oculta))

# Funcion para confirmar si la letra ingresada por el usuario se encuentra en
def confirmar_letra(letra_elegida, palabra_oculta, vidas, coincidencias):
    fin = False # Confirmacion de  que el juego aun no termina
    # Si la letra elegida se encuentra dentro de de palabra_oculta
    if letra_elegida in palabra_oculta:
        # Si se encuentra entonces, a letras_correctas se le agregara la letra_elegida
        letras_correctas.append(letra_elegida)
        coincidencias += 1  # Aumentando la cantidad de coincidencias se leagregara 1
    # Si el jugador no acerto
    else:
        letras_incorrectas.append(letra_elegida)  # De igual manera se le agregra la letra_eleiga a letras_incorrectas
        vidas -= 1  # Tambien se le restara una vida

    # Verificando si aun tiene vidas
    if vidas == 0:
        fin = perder() # El fin del juego sera verdadero y se invocara el metodo perder()
    # Si las coincidencias son iguales a la cantidad de letras
    elif coincidencias == letras_unicas:
        # Si son iguales entonces, fin sera = al metodo ganar() con la palabra ocultas
        fin = ganar(palabra_oculta)

    # Devolviendo los estados de las vidas, fin y las coincidencias
    return vidas, fin, coincidencias

# Funcion para verificar si el jugador a perdido el juego
def perder():
    print("\nDesafortunadamente te has quedado sin vidas :(")
    print("\nEl nombre oculto era " + nombre)

    return True # Terminando el juego

# Funcion para verificar si el jugador a ganado el juego
def ganar(nombre_descubierto):
    # Mostrando el estado del tablero
    mostrar_tablero(nombre_descubierto)
    print("\n\nFelicidades, has encontrado el nombre")

    return True # Se termina el juego

# Invocando las funciones
nombre, letras_unicas = elegir_nombres(nombres) # Invocando la funcion elegir_nombres y agregando como parametro nombres

# Condicion para terminar el juego
while not juego_terminado:
    print('\n' + '*' * 20 + '\n')   # Separando el tablero
    # Mostrando el tablero
    mostrar_tablero(nombre)
    print('\n')
    print("\nLetras incorrectas: " + ' | '.join(letras_incorrectas))
    print(f"Cantidad de vidas: {intentos}")
    print('\n' + '*' * 20 + '\n')   # Separando el tablero

    # Alamacenado lo capturado en funcion perdir_letra en la variable letra
    letra = pedir_letra()
    intentos, terminado, aciertos = confirmar_letra(letra, nombre, intentos, aciertos)
    juego_terminado = terminado


import codecs
import copy
import logging
import os
import re

import asstosrt
import chardet
from hanziconv import HanziConv
from pysrt import SubRipTime, SubRipFile
import pysrt
import Segment

ad_key_words = ["人人影视","人人视频APP", "字幕组", "资源Q群", "YYeTs", "原创翻译","网易见外", "招募Q号", "小愿8压制组", "火鸟字幕合并器", "圣城家园"]
one_line_ad_key_words = ["翻译", "字幕", "made by", "Made by"]

def get_encoding(file):
    encoding_detect_result = chardet.detect(open(file, 'rb').read())
    encoding = encoding_detect_result['encoding']
    if not encoding:
        return "utf-8"
    if 'gb2312' == encoding.lower():
        return 'gb18030'
    if 'ascii' == encoding.lower():
        return 'utf-8'
    if 'iso-8859-1' == encoding.lower():
        return 'utf-8'
    if 'iso-8859-7' == encoding.lower():
        return 'utf-8'
    return encoding

def is_one_line(sub):
    return len(sub.text.split('\n', 1)) < 2

def is_one_language(subs):
    return ascii_ratio(subs) > 0.9 or (len(list(filter(is_one_line, subs))) * 1.0 / len(subs) > 0.5)

def ascii_ratio(subs):
    c = 0
    l = 0
    for sub in subs:
        for w in sub.text:
            if ord(w) < 128:
                c += 1
        l += len(sub.text)
    r = c * 1.0 / l
    return r

def erase(subs, first):
    for sub in subs:
        lines = sub.text.split('\n')
        if len(lines) < 2:
            sub.text = '...'
        elif first:
            sub.text = lines[-1]
        else:
            sub.text = "\n".join(lines[0:-1])
    return subs

def remove_styles(text):
    sub = re.sub(r'\{[^\}]+\}', '', text)
    if sub != text:
        return sub
    sub = re.sub(r'<[^>]+>', '', text)
    if sub != text:
        return sub
    # not very sure of the below patterns
    sub = re.sub(r'\([^\)]+\)', '', text)
    if sub != text:
        return sub
    sub = re.sub(r'\[[^\]]+\]', '', text)
    if sub != text:
        return sub
    return text

def english_first(subs1, subs2):
    if ascii_ratio(subs1) > ascii_ratio(subs2):
        return subs1, subs2
    else:
        return subs2, subs1

def preprocess_subtitle(subs):

    encoding = get_encoding(subs)
    print('code {}'.format(encoding))

    subs = pysrt.open(subs, encoding)
    for sub in subs:
        sub.text = remove_styles(sub.text)

    subs.data = sorted(subs.data, key=lambda a: a.start)
    new_subs = [subs.data[0]]
    for sub in subs.data[1:]:
        if sub.start == new_subs[-1].start:
            new_subs[-1].text = new_subs[-1].text + '\n' + sub.text
        else:
            new_subs.append(sub)
    subs.data = new_subs
    subs.clean_indexes()

    mix_language = not is_one_language(subs)
    # remove faked subtitle
    if mix_language:
        new_subs = list(filter(lambda a: not is_one_line(a), subs))
        subs.data = new_subs
        subs.clean_indexes()

    sub1 = copy.deepcopy(subs)
    sub2 = copy.deepcopy(subs)

    if mix_language:
        sub1 = erase(sub1, True)
        sub2 = erase(sub2, False)

    en_subs, cn_subs = english_first(sub1, sub2)

    # reformat for cn first
    for en, cn, sub in zip(en_subs, cn_subs, subs):
        if cn.text != en.text:
            sub.text = en.text + "\n" + cn.text
    return en_subs, subs

VTT_PATTERN = re.compile("(\\d{2}:\\d{2}:\\d{2})\\.(\\d{3})")
def vtt_to_srt(lines):
    results = []
    for line in lines:
        find = VTT_PATTERN.findall(line)
        if len(find) == 2:
            results.append(line.replace(".", ","))
            continue
        else:
            results.append(line)
    return results

def replace_chinese_character(source):
    return source.replace("，", ",")\
        .replace("–", "-")\
        .replace("—", "-")\
        .replace("。", ".")\
        .replace("“", "\"")\
        .replace("”", "\"")\
        .replace("‘", "'")\
        .replace("’", "'")\
        .replace("？", "?")\
        .replace("！", "!")\
        .replace("…", "...")\
        .replace("：", ":")

def check_contain_chinese(check_str):
    is_contained = False
    for c in check_str :
        if '\u4e00' <= c <= '\u9fa5':
            is_contained = True
            break
    return is_contained

def is_english(content):
    return not check_contain_chinese(content)

def is_chinese(content):
    return check_contain_chinese(replace_chinese_character(content))

def count_digital(content):
    count = 0
    for i in range(len(content)):
        c = content[i]
        if (c >= '0' and c<= '9') :
            count += 1
    return count

def is_comment(subtitle_array):
    if len(subtitle_array) == 1:
        content = subtitle_array[0]
        return content.startswith("（") and content.endswith("）")
    return False

def is_advertisement(subtitles):
    for word in ad_key_words:
        for line in subtitles:
            if word in line:
                return True
    if len(subtitles) == 1:
        for word in one_line_ad_key_words:
            if word in line:
                return True
    return False

def to_simple_chinese(content):
    return HanziConv.toSimplified(content)

class SubtitleException(Exception):
    def __init__(self, message):
        Exception.__init__(self, message)

class SubtitleFile:

    def __init__(self, subtitles):
        self.subtitles = subtitles

    @staticmethod
    def load(file_path):
        ext = file_path.split('.')[-1].lower()
        encoding = get_encoding(file_path)
        logging.info('{} code {}'.format(file_path, encoding))

        lines = []
        with open(file_path, 'r', encoding=encoding) as f:
            if ext == 'ass' or ext == 'ssa':
                lines = asstosrt.convert(f).split("\n")
                ext = 'srt'
            else:
                for line in f:
                    lines.append(line.strip())
        file = SubtitleFile.load_lines(lines, ext, encoding)
        logging.info("load {} success ".format(file_path))
        return file

    @staticmethod
    def loads(content, ext='srt', encoding='utf-8'):
        lines = content.split("\n")
        return SubtitleFile.load_lines(lines, ext, encoding)

    @staticmethod
    def load_lines(lines, ext, encoding):
        if ext == 'srt':
            lines = SubtitleFile.__fix_srt_lines(lines)
            subtitle_file = SubtitleFile(SubtitleFile.__srt_to_subtitle_file(lines, encoding))
            return subtitle_file
        elif ext == 'vtt':
            lines = vtt_to_srt(lines)
            subtitle_file = SubtitleFile(SubtitleFile.__srt_to_subtitle_file(lines, encoding))
            return subtitle_file
        else:
            raise SubtitleException("wrong file subtitle content")

    @staticmethod
    def __fix_srt_lines(lines):
        pattern = re.compile('\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}')
        fixed_lines = []
        group = [lines[0]]
        have_time = False
        for line in lines[1:]:
            group.append(line)
            if not line:
                if len(group) > 2:
                    fixed_lines.extend(group)
                group = []
                have_time = False
            elif pattern.match(line):
                if have_time:
                    fixed_lines.extend(group[:-2])
                    fixed_lines.append("")
                    group = group[-2:]
                have_time = True
        if group:
            fixed_lines.extend(group)
        return fixed_lines

    @staticmethod
    def __srt_to_subtitle_file(lines, encoding):
        subs = pysrt.from_string("\n".join(lines), encoding=encoding, eol='\n')
        return SubtitleCleaner.fix_subtitle(subs)

    def __is_sentence_ends(self, subtitle_content):
        for line in subtitle_content.split("\n"):
            if line.strip() and line.strip()[-1] in ('.', '。', '!', '！'):
                return True
        return False

    def calc_segments(self, min_duration_mills=140000, minimal_duration=90000, min_sentence_number=None):
        assert(min_duration_mills > minimal_duration)
        subs = self.subtitles
        segments = []
        start_index = 0
        end_index = 0
        part_sentence_number = 0
        end_mills = subs[-1].end.ordinal
        while end_index < len(subs.data):
            part_duration = subs[end_index].end.ordinal - subs[start_index].start.ordinal
            part_sentence_number += 1
            end_sub = subs[end_index]
            end_index += 1
            if end_mills - subs[start_index].start.ordinal < min_duration_mills + minimal_duration:
                min_duration_mills = minimal_duration
            if part_duration >= min_duration_mills \
                    and (not min_sentence_number or part_sentence_number > min_sentence_number)\
                    and self.__is_sentence_ends(end_sub.text):
                segments.append(Segment(subs[start_index].start.ordinal, subs[end_index - 1].end.ordinal))
                start_index = end_index
                part_sentence_number = 0

        if not segments:
            logging.warning("find empty segments when clip subtitle")
            return segments

        last_segment = segments[-1]
        segments.remove(last_segment)
        segments.append(Segment(last_segment.start, subs[end_index - 1].end.ordinal))
        return segments

    def save(self, file):
        subs = self.subtitles
        subs = [str(s) for s in subs]
        new_subs = []
        for sub in subs:
            fields = sub.split('\n')
            fields[0] = ''
            new_subs.append('\n'.join(fields))
        subs = new_subs
        subs = "".join(subs)
        subs = "WEBVTT\n\n" + subs
        content = re.sub("(\d{2}:\d{2}:\d{2}),(\d{3})", lambda m: m.group(1) + '.' + m.group(2), subs)
        with codecs.open(file, 'w', 'utf-8') as outfile:
            outfile.write(content)

    #TODO save by file extension
    def save_srt(self, file):
        self.subtitles.save(path=file, encoding='utf-8')

    #TODO optimize later
    def save_en(self, file):
        subs = self.subtitles
        subs = [str(s) for s in subs]
        new_subs = []
        for sub in subs:
            fields = sub.split('\n')
            fields[0] = ''
            new_subs.append('\n'.join(fields[:3]) + '\n')
        subs = new_subs
        subs = "".join(subs)
        subs = "WEBVTT\n\n" + subs
        content = re.sub("(\d{2}:\d{2}:\d{2}),(\d{3})", lambda m: m.group(1) + '.' + m.group(2), subs)
        with codecs.open(file, 'w', 'utf-8') as outfile:
            outfile.write(content)

    def slice(self, segment):
        subs = self.subtitles
        start_time = SubRipTime(milliseconds=segment.start)
        end_time = SubRipTime(milliseconds=segment.end)
        slices = copy.deepcopy(subs.slice(starts_before=end_time, ends_after=start_time))
        slices.shift(milliseconds=-segment.start)
        slices.clean_indexes()
        return SubtitleFile(slices)

    def shift(self, hours=0, minutes=0, seconds=0, mills=0):
        self.subtitles.shift(hours=hours, minutes=minutes, seconds=seconds, milliseconds=mills)

    def combine_cn_en(self, cn_subtitle_path, en_subtitle_path, output_folder):
        cn_subs = pysrt.open(cn_subtitle_path)
        en_subs = pysrt.open(en_subtitle_path)
        cn_dict = {}
        new_subtitle = []
        for cn_sub in cn_subs:
            cn_dict[cn_sub.start.__str__()] = cn_sub.text
        index = 1
        for en_sub in en_subs:
            if en_sub.start.__str__() in cn_dict:
                subRipItem = copy.deepcopy(en_sub)
                subRipItem.text = en_sub.text + "\n" + cn_dict[en_sub.start.__str__()]
                subRipItem.index = index
                check_contain_chinese
                new_subtitle.append(subRipItem)
                index += 1
            else:
                logging.warning("[combine_cn_en] not found in cn dict, {}".format(en_sub.start.__str__()))
        output_file_path = os.path.join(output_folder, "cn_en_subtitle.vtt")
        self.save(output_file_path)
        return output_file_path

    def length(self):
        return len(self.subtitles)

    def __text(self, sub):
        txs = sub.text.split('\n')
        txt = txs[0]
        return txt

    def word_count(self):
        words = list(map(lambda t: self.__text(t).split(), self.subtitles))
        return sum(map(len, words))

class SubtitleCleaner:

    @staticmethod
    def fix_subtitle_file(subtitile_file):
        logging.info('deal with:{}'.format(subtitile_file))
        subtitile_file.subtitles = SubtitleCleaner.fix_subtitle(subtitile_file.subtitles)
        return subtitile_file

    @staticmethod
    def __do_fix(subtitle, subtitle_lines, chinese_first):
        if len(subtitle_lines) == 2 and is_english(subtitle_lines[0]) and is_english(subtitle_lines[1]):
            if chinese_first:
                subtitle.text = subtitle_lines[1] + '\n' + subtitle_lines[0]
            return

        chinese = []
        english = []
        for line in subtitle_lines:
            if is_chinese(line):
                chinese.append(line)
            else:
                english.append(line)
        english_line = replace_chinese_character(" ".join(english))
        chinese_line = to_simple_chinese("".join(chinese))
        subtitle.text = english_line + '\n' + chinese_line

    @staticmethod
    def is_chinese_first(subtitles):
        single_type_lines = []
        type = []
        for s in subtitles:
            subtitle_array = s.text.split('\n')
            types = [is_chinese(t) for t in subtitle_array]
            if len(set(types)) == 2:
                type.append(types[0])
            else:
                single_type_lines.append('\n' + str(s))
        if len(single_type_lines) == 0:
            return type[0]
        raise SubtitleException("必须包含中英文字幕:" + "\n".join(single_type_lines))

    @staticmethod
    def __fix_single_line(subtitle, subtitle_lines, chinese_first):
        if len(subtitle_lines) == 1:
            if is_chinese(subtitle_lines[0]):
                subtitle.text = 'no english subtitle' + '\n' + subtitle_lines[0]
            elif is_english(subtitle_lines[0]):
                subtitle.text = subtitle_lines[0] + '\n' + '无中文字幕'
            else:
                subtitle.text = 'no english subtitle\n无中文字幕'

    @staticmethod
    def fix_subtitle(subtitles):
        subs = subtitles
        for sub in subs:
            sub.text = remove_styles(sub.text)

        subs.data = sorted(subs.data, key=lambda a: a.start)
        subs.clean_indexes()
        new_subtitles = []
        chinese_first = SubtitleCleaner.is_chinese_first(subs)

        count = 0
        for subtitle in subs:
            subtitle_array = subtitle.text.split('\n')
            if is_advertisement(subtitle_array):
                logging.warning("ignore advertisement only {} ".format(subtitle_array))
                continue
            subtitle_array = [s for s in subtitle_array if s.strip()]
            if len(subtitle_array) > 4:
                logging.warning(" error subtitle {}".format(subtitle_array))
                raise SubtitleException("subtitle exception " + str(subtitle_array))

            if not subtitle_array:
                continue
            #may have problem later
            elif len(subtitle_array) == 1:
                count += 1
                continue
            switch_count = 0
            prev_english = is_english(subtitle_array[0])
            for i in range(1, len(subtitle_array)):
                l = subtitle_array[i]
                current_english = is_english(l)
                if current_english != prev_english:
                    switch_count += 1
                prev_english = current_english
            if len(subtitle_array) == 4 and switch_count != 1:
                raise SubtitleException("\n{}".format(subtitle))
            SubtitleCleaner.__do_fix(subtitle, subtitle_array, chinese_first)
            new_subtitles.append(subtitle)
        if count > len(subtitles) * 0.95:
            raise SubtitleException("too much single line content")
        return SubRipFile(new_subtitles)

if __name__ == '__main__':
    subs = SubtitleFile.load("C:\\Users\\zhongjianbin\\Desktop\\50.srt")


#!/usr/bin/env python
# coding=utf-8
##############################################################
 # File Name : LF_refocusing.py
 # Purpose : Refocus the Light Field Image
 # Creation Date : Wed 16 Aug 2017 02:04:54 PM CST
 # Last Modified : Sun 20 Aug 2017 01:15:39 AM CST
 # Created By : SL Chung
##############################################################
#Usage:
#    python3 LF_refocusing.py ./Img_Directory
import cv2
import numpy as np
import math
import sys
import os
from matplotlib import pyplot as plt

from python_tools import file_io

#def imshow(window_name, img, start_x, start_y):
#    cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
#    cv2.resizeWindow(window_name, 400, 400)
#    cv2.moveWindow(window_name, start_x, start_y)
#    cv2.imshow(window_name, center_img)

def gen_shifted_img(params, light_field, delta_x, delta_y):
    shifted_img = np.zeros((params['height'], params['width'], 3))
    center_coordinate = (params['num_cams_x']//2 + 1, params['num_cams_y']//2 + 1)

    for i in range(params['num_cams_x']):
        for j in range(params['num_cams_y']):
            start_x = (center_coordinate[0] - i) * delta_x
            start_y = (center_coordinate[1] - j) * delta_y
            temp = np.concatenate((light_field[i, j, start_x:, :, :], light_field[i, j, 0:start_x, :, :]), 0)
            temp = np.concatenate((temp[:, start_y:, :], temp[:, 0:start_y, :]), 1)
            shifted_img = shifted_img + temp

    shifted_img = shifted_img / ( params['num_cams_x'] * params['num_cams_y'])
    shifted_img = shifted_img.astype('uint8')
    return shifted_img

if len(sys.argv) < 2:
    print('Error: need input directory of source of GIF!')
    sys.exit()

LF_directory = sys.argv[1]

params = file_io.read_parameters(sys.argv[1])
light_field = file_io.read_lightfield(sys.argv[1])

center_img = light_field[ math.ceil(params['num_cams_x'] / 2),
                          math.ceil(params['num_cams_y'] / 2), :, :, : ]
center_img = center_img.astype('uint8')

average_img = np.sum(light_field, (0, 1)) / ( params['num_cams_x'] * params['num_cams_y'])
average_img = average_img.astype('uint8')

shifted_img = gen_shifted_img(params, light_field, 1, 1);

window=[]
window.append('Original Center Img')
window.append('Average Img')
window.append('Shifted Img')

#start_x = 0
#start_y = 0
#imshow(window[0], center_img, start_x, start_y)
#
#start_x = start_x + 400;
#imshow(window[1], average_img, start_x, start_y)
#
#cv2.waitKey(0)
#cv2.destroyAllWindows()

plt.subplot(1,3,1),plt.imshow(cv2.cvtColor( center_img, cv2.COLOR_BGR2RGB))
plt.title(window[0]), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2),plt.imshow(cv2.cvtColor(average_img, cv2.COLOR_BGR2RGB))
plt.title(window[1]), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3),plt.imshow(cv2.cvtColor(shifted_img, cv2.COLOR_BGR2RGB))
plt.title(window[2]), plt.xticks([]), plt.yticks([])

plt.show()

if not os.path.exists('./shifted/'):
    os.makedirs('./shifted')

for i in range(11):
    i = i - 5
    s = gen_shifted_img(params, light_field, i, i);
    cv2.imwrite('./shifted/shift_' + str(i) + '.jpg', s)


import numpy as np

def one_roi(img, margin=0, *, ignore=[], threshold=10, return_index=False):
    allzeros = np.where((img < threshold) & (img != 0))

    if type(margin) == int:
        margin = [margin for _ in range(img.ndim)]
    idx = ()
    for i in range(len(allzeros)):
        if i in ignore or len(allzeros[i]) == 0:
            idx += (np.s_[:],)
        else:
            idx += (np.s_[max(0, allzeros[i].min() - margin[i]):min(img.shape[i], allzeros[i].max() + margin[i] + 1)],)
    if return_index:
        return idx
    return img[idx]


import pandas as pd
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
from nltk import download
from collections import Counter

# Download NLTK
download('punkt')
download('stopwords')

# Initialize the PorterStemmer
stemmer = PorterStemmer()

# Get stopwords
stop_words = set(stopwords.words('english'))

# Function to clean text
def clean_text(text):
    # Remove punctuations and numbers
    text = re.sub('[^a-zA-Z]', ' ', text)

    # Convert to lowercase
    text = text.lower()

    # Tokenize
    word_tokens = word_tokenize(text)

    # Stopword removal and stemming
    text = [stemmer.stem(word) for word in word_tokens if word not in stop_words]

    text = ' '.join(text)

    return text

# Load the data
data = pd.read_csv('~/newworkspace/amazon_review_polarity_csv/train.csv', header=None, names=['sentiment', 'title', 'review'])

# Fill NaNs with an empty string
data = data.fillna("")

# Apply the function to the 'title' and 'review' columns
data['title'] = data['title'].apply(clean_text)
data['review'] = data['review'].apply(clean_text)

# Select only negative reviews
data = data[data['sentiment'] == 1]

# Remove null values
data = data.dropna()

# Keep only the cleaned 'review' column
data = data['review']

# Reset the index of the DataFrame
data = data.reset_index(drop=True)

# Split the reviews into words and count the occurrences of each word
word_counts = Counter(' '.join(data).split())

# Convert the word counts to a DataFrame and reset the index
word_counts_df = pd.DataFrame.from_dict(word_counts, orient='index').reset_index()

# Rename the columns
word_counts_df.columns = ['Word', 'Count']

# Sort the DataFrame by the counts in descending order
word_counts_df = word_counts_df.sort_values(by='Count', ascending=False)

# Print the top 10 words
print(word_counts_df.head(10))

# Save output to a CSV file
word_counts_df.to_csv('~/newworkspace/word_counts.csv', index=False)


import json
import os
from pathlib import Path

from . import logger
from .models.configs import CONFIG_CLASSES
from .models.image import ImageEntry

PATH_TO_CONFIGS = Path(os.environ.get("PATH_TO_CONFIGS"))
CONFIG_CLASS = os.environ.get("CONFIG_CLASS", "BasicConfig")
GLOB_PATTERN = os.environ.get("GLOB_PATTERN", "photos.csv")
OUTPUT_FOLDER = Path(os.environ.get("OUTPUT_FOLDER", "images"))
CROPPED_SUBFOLDER = os.environ.get("CROPPED_SUBFOLDER", "_cropped")
SPLIT_BY_CATEGORY = bool(int(os.environ.get("SPLIT_BY_CATEGORY", 1)))
CROP_DIMENSIONS = json.loads(os.environ.get("CROP_DIMENSIONS", "[[10, 15]]"))
DPCM = int(os.environ.get("DPCM", 120))

ConfigClass = CONFIG_CLASSES[CONFIG_CLASS]
config = ConfigClass.from_csvs(PATH_TO_CONFIGS, GLOB_PATTERN)

for i, entry in enumerate(config.entries):
    logger.info("%s/%s", i + 1, len(config.entries))
    output_dir = OUTPUT_FOLDER
    if SPLIT_BY_CATEGORY and entry.category is not None:
        output_dir /= entry.category
    image = ImageEntry.from_url(url=entry.url,
                                prompt=entry.prompt,
                                output_dir=output_dir)
    image.save()
    for width, height in CROP_DIMENSIONS:
        cropped_image = image.crop_image(width, height, DPCM, CROPPED_SUBFOLDER)
        if cropped_image.prompt:
            cropped_image.embed_prompt()
        cropped_image.save()


from django.test import TestCase
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
from django.apps import apps
from itertools import chain
from streamwebs.models import CameraPoint, Site
import datetime

class CameraPointTestCase(TestCase):

    def setUp(self):
        self.expected_fields = {
            'school': models.ForeignKey,
            'school_id': models.ForeignKey,
            'site': models.ForeignKey,
            'site_id': models.ForeignKey,
            'letter': models.CharField,
            'cp_date': models.DateField,
            'location': models.PointField,
            'map_datum': models.CharField,
            'description': models.TextField,
            'created': models.DateTimeField,
            'id': models.AutoField,

            'camera_point': models.ManyToOneRel
        }

        self.optional_fields = {
            'map_datum': models.CharField,
            'description': models.TextField
        }

        self.site = Site.test_objects.create_site('test site a')

    def test_fields_exist(self):
        model = apps.get_model('streamwebs', 'camerapoint')
        for field, field_type in self.expected_fields.items():
            self.assertEqual(
                field_type, type(model._meta.get_field(field)))

    def test_no_extra_fields(self):
        cp_fields = list(set(chain.from_iterable(
            (field.name, field.attname) if hasattr(field, 'attname') else
            (field.name,) for field in CameraPoint._meta.get_fields()
            if not (field.many_to_one and field.related_model is None)
            )))
        self.assertEqual(sorted(cp_fields),
                         sorted(self.expected_fields.keys()))

    def test_optional_fields(self):
        for field in self.optional_fields:
            self.assertEqual(
                CameraPoint._meta.get_field(field).blank, True)

    def test_CameraPoint_ManyToOneSite(self):
        """
        A camera point should correspond to a single specified site.
        """
        cp_date = datetime.date.today()

        camera_point = CameraPoint.test_objects.create_camera_point(
            self.site, cp_date, 'POINT(-121.393401 44.061437)'
        )

        self.assertEqual(camera_point.site.site_name, 'test site a')

    def test_obj_exists_req_fields(self):
        """
        Tests that the CameraPoint object can be created successfully when the
        required fields are provided.
        """
        camera_point = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-07', 'POINT(-121.393401 44.061437)'
        )

        # Required fields
        self.assertEqual(camera_point.site.site_name, 'test site a')
        self.assertEqual(camera_point.cp_date, '2016-07-07')

        # Optional fields
        self.assertEqual(camera_point.location.coords,
                         (-121.393401, 44.061437))
        self.assertEqual(camera_point.map_datum, '')
        self.assertEqual(camera_point.description, '')

    def test_obj_exists_opt_fields(self):
        """
        Tests that the CameraPoint object can be created successfully when the
        required and optional fields are provided.
        """
        camera_point = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-08', Point(-120.2684184, 44.3910532), 'WGS84',
            'Notes on this camera point for test site a'
        )

        # Required fields
        self.assertEqual(camera_point.site.site_name, 'test site a')
        self.assertEqual(camera_point.cp_date, '2016-07-08')

        # Optional fields
        self.assertEqual(camera_point.location.coords,
                         (-120.2684184, 44.3910532))
        self.assertEqual(camera_point.map_datum, 'WGS84')
        self.assertEqual(camera_point.description,
                         'Notes on this camera point for test site a')

    def test_letter_assign_for_first_cp(self):
        camera_point = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-08', Point(-120.2684184, 44.3910532),
        )
        self.assertEqual(camera_point.letter, 'A')

    def test_letter_assign_for_regular_cp(self):
        camera_point = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-08', Point(-120.2684184, 44.3910532),
        )
        self.assertEqual(camera_point.letter, 'A')

        camera_point_2 = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-08', Point(-120.2684184, 44.3910532),
        )
        self.assertEqual(camera_point_2.letter, 'B')

    def test_letter_assign_for_end_of_alphabet(self):
        camera_point = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-08', Point(-120.2684184, 44.3910532),
        )
        camera_point.letter = 'ZZ'
        camera_point.save()

        self.assertEqual(camera_point.letter, 'ZZ')

        camera_point_2 = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-08', Point(-120.2684184, 44.3910532)
        )
        self.assertEqual(camera_point_2.letter, 'AAA')

    def test_restart_lettering_for_new_site(self):
        camera_point = CameraPoint.test_objects.create_camera_point(
            self.site, '2016-07-08', Point(-120.2684184, 44.3910532),
        )
        self.assertEqual(camera_point.letter, 'A')

        site_2 = Site.test_objects.create_site('Second site')
        camera_point_2 = CameraPoint.test_objects.create_camera_point(
            site_2, '2016-07-08', Point(-120.2684184, 44.3910532),
        )
        self.assertEqual(camera_point_2.letter, 'A')


print("Digite dois numeros: ")
numero1 = int(input())
numero2 = int(input())

if numero1 > numero2:
    troca = numero1
    numero1 = numero2
    numero2 = troca

soma = 0
for i in range(numero1+1, numero2):
    if i % 2 != 0:
        soma = soma + i

print(f"SOMA DOS IMPARES = {soma}")