import urllib

import requests
from bs4 import BeautifulSoup

from node import Node

global_nodes = {}
max_count = 2

class WebSpider:

    def __init__(self, node, id):
        self.id = id
        self.node = node
        self.parse()

    def get_links(self):
        try:
            resp = requests.get(self.node.link)
            soup = BeautifulSoup(resp.content, 'html.parser')
        except Exception:
            return []
        links = []
        for a in soup.find_all('a', href=True):
            new_link = urllib.parse.urljoin(self.node.link, a['href'])
            if new_link.endswith("/"):
                new_link = new_link[:-1]
            links.append(new_link)
            links = list(set(links))
        return links

    def get_last_links(self, node):
        resp = requests.get(node.link)
        soup = BeautifulSoup(resp.content, 'html.parser')
        for a in soup.find_all('a', href=True):
            new_link = urllib.parse.urljoin(self.node.link, a['href'])
            if new_link.endswith("/"):
                new_link = new_link[:-1]
            if global_nodes.get(new_link) is not None:
                node.children.append(global_nodes[new_link])

    def parse(self):
        links = self.get_links()
        for link in links:
            if global_nodes.get(link) is None:
                if link.startswith("http"):
                    new_node = Node(link, len(global_nodes))
                    global_nodes[link] = new_node
                    if self.id < max_count:
                        WebSpider(new_node, self.id + 1)
                    else:
                        if self.id == max_count:
                            self.get_last_links(new_node)
                            continue
                    global_nodes[link].parents.append(self.node)
                    self.node.children.append(global_nodes[link])


from collections import deque

class TreeNode(object):
    def __init__(self, value):
        self.value = value
        self.left_node = None
        self.right_node = None

    def __str__(self):
        return str(self.value)

class BinarySearchTree(object):
    def __init__(self):
        self.root = None

    def add(self, value):
        if(self.root is None):
            self.root = TreeNode(value)
        else:
            inserted = False
            cur_node = self.root

            while not inserted:
                if(value <= cur_node.value):
                    if(cur_node.left_node):
                        cur_node = cur_node.left_node
                    else:
                        cur_node.left_node = TreeNode(value)
                        inserted = True
                elif(value > cur_node.value):
                    if(cur_node.right_node):
                        cur_node = cur_node.right_node
                    else:
                        cur_node.right_node = TreeNode(value)
                        inserted = True

    def search(self, value):
        cur_node = self.root
        found = False
        while not found:
            if(cur_node is None):
                return None
            elif(value < cur_node.value):
                cur_node = cur_node.left_node
            elif(value > cur_node.value):
                cur_node = cur_node.right_node
            elif(value == cur_node.value):
                return cur_node

    def list(self):
        elements = deque()
        self.trav_inorder(self.root, elements)
        return elements

    def trav_inorder(self, node, elements):
        if(node is not None):
            self.trav_inorder(node.left_node, elements)
            elements.append(node.value)
            self.trav_inorder(node.right_node, elements)


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time
import sqlite3

class SQLite(object):
    """docstring for SQLite"""
    def __init__(self, dbPath):
        self.dbPath = dbPath
        self.adl = None
        self.cursor = None

    def connectToDB(self):
        try:
            self.adl = sqlite3.connect(self.dbPath)
            self.cursor = self.adl.cursor()
        except Exception as e:
            fo = open("log.txt", "a")
            fo.write( time.asctime(time.localtime(time.time())) + str(e) + '\n' )
            fo.close()

    def insert(self, dataID):
        try:
            self.cursor.execute("INSERT into dataids (dataID) values (%s)" % dataID)
        except Exception as e:
            fo = open("log.txt", "a")
            fo.write( time.asctime(time.localtime(time.time())) + str(e) + '\n' )
            fo.close()

    def select(self, dataID):
        try:
            select_result = self.cursor.execute("SELECT dataID from dataids where dataID='%s'" % dataID)
            select_result = list(select_result)
            if len(select_result) == 0:
                return False
            else:
                return True
        except Exception as e:
            fo = open("log.txt", "a")
            fo.write( time.asctime(time.localtime(time.time())) + str(e) + '\n' )
            fo.close()
            return False


from enum import Enum

# label value => [isolated, end, middle, start]
class Letter(Enum):
    ا = [0,1]
    ب = [2,3,4,5]
    ت = [6,7,8,9]
    ث = [10,11,12,13]
    ج = [14,15,16,17]
    ح = [18,19,20,21]
    خ = [22,23,24,25]
    د = [26,27]
    ذ = [28,29]
    ر = [30,31]
    ز = [32,33]
    س = [34,35,36,37]
    ش = [38,39,40,41]
    ص = [42,43,44,45]
    ض = [46,47,48,49]
    ط = [50,51,52,53]
    ظ = [54,55,56,57]
    ع = [58,59,60,61]
    غ = [62,63,64,65]
    ف = [66,67,68,69]
    ق = [70,71,72,73]
    ك = [74,75,76,77]
    ل = [78,79,80,81]
    م = [82,83,84,85]
    ن = [86,87,88,89]
    ه = [90,91,92,93]
    و = [94,95]
    لا = [96,97]
    ي = [98,99,100,101]

terminal_characters = ["ا", "د", "ذ", "ر", "ز", "و"]

def get_labels(word):
    labels = []
    i = 0
    while i < len(word):
        if word[i] in Letter.__members__:
            label = Letter[word[i]].value
            if word[i] == "ل" and i+1 < len(word) and word[i+1] == "ا":   # special case "لا"
                comb = word[i] + word[i+1]
                if i == 0 or word[i-1] in terminal_characters:
                    label = Letter[comb].value[0]
                else:
                    label = Letter[comb].value[1]
                labels.append(label)
                i += 1
            elif i == 0:
                if len(word) == 1 or word[i] in terminal_characters:
                    labels.append(label[0])
                else:
                    labels.append(label[3])
            elif i == len(word)-1:
                if word[i-1] in terminal_characters:
                    labels.append(label[0])
                else:
                    labels.append(label[1])
            else:
                if word[i-1] in terminal_characters:
                    if word[i] in terminal_characters:
                        labels.append(label[0])
                    else:
                        labels.append(label[3])
                else:
                    if word[i] in terminal_characters:
                        labels.append(label[1])
                    else:
                        labels.append(label[2])
        else:
            print("unexpected text symbol!")
        i += 1
    return labels

def getCharFromLabel(label):
    for letter in Letter:
        if label in letter.value:
            return letter.name
    return ' '

if __name__ == "__main__":
    #document = "الاسلاميون"
    #print(get_labels(document)
    #print(getCharFromLabel(10))
    pass


import datetime
import logging
import os.path as osp
import os
import pandas as pd

# Check coordinates and return properly coordinates
#
# @ Param latitude1,latitude2,longitude1,longitude2 - geographical coordinates in WGS84 degrees
#
def check_coords(latitude1, latitude2, longitude1, longitude2):
    valid_coords = lambda y1,y2,x1,x2: -90 < y1 < 90 and -90 < y2 < 90 and -180 < x1 < 180 and -180 < x2 < 180
    if any([latitude1 is None, latitude2 is None, longitude1 is None, longitude2 is None]) or not valid_coords(latitude1,latitude2,longitude1,longitude2):
        return None,None,None,None
    if latitude1 < latitude2:
        lat1 = latitude1
        lat2 = latitude2
    else:
        lat1 = latitude2
        lat2 = latitude1
    if longitude1 < longitude2:
        lon1 = longitude1
        lon2 = longitude2
    else:
        lon1 = longitude2
        lon2 = longitude1
    return lat1,lat2,lon1,lon2

# Returns the time in Meso format
#
# @ Param utc_datetime - datetime in UTC
#
def meso_time(utc_datetime):
    year = utc_datetime.year
    month = utc_datetime.month
    day = utc_datetime.day
    hour = utc_datetime.hour
    return "{:04d}{:02d}{:02d}{:02d}{:02d}".format(year,month,day,hour,0)

# Tranform dictionary data from MesoWest request to pandas dataframe
#
# @ Param utc_datetime - datetime in UTC
#
def meso_data_2_df(mesowestData):
    if mesowestData is None:
        return pd.DataFrame([]),pd.DataFrame([])
    site_keys = ['STID','LONGITUDE','LATITUDE','ELEVATION','STATE']
    site_dic = {key: [] for key in site_keys}
    data_keys = ['STID','datetime','fm10']
    data_dic = {key: [] for key in data_keys}
    for stData in mesowestData['STATION']:
        for site_key in site_keys:
            site_dic[site_key].append(stData[site_key])
        data_dic['STID'] += [stData['STID']]*len(stData['OBSERVATIONS']['date_time'])
        data_dic['datetime'] += stData['OBSERVATIONS']['date_time']
        data_dic['fm10'] += stData['OBSERVATIONS']['fuel_moisture_set_1']

    data = pd.DataFrame.from_dict(data_dic)
    data['datetime'] = pd.to_datetime(data['datetime'])
    sites = pd.DataFrame.from_dict(site_dic).set_index('STID')
    return data,sites

# Ensure all directories in path if a file exist, for convenience return path itself.
#
# @ Param path - the path whose directories should exist
#
def ensure_dir(path):
    path_dir = osp.dirname(path)
    if not osp.exists(path_dir):
        os.makedirs(path_dir)
    return path

# Set UTC datetime from integers
#
# @ Param year - user specified year
# @ Param month - user specified month
# @ Param day - user specified day
# @ Param hour - user specified hour
#
def set_utc_datetime(year, month, day, hour = 0):
    userDatetime = datetime.datetime(year,month,day,hour,tzinfo=datetime.timezone.utc)
    # Checks if datetime given is valid (not a future date or a time before mesowest started collecting data)
    if userDatetime > datetime.datetime.now(datetime.timezone.utc) or userDatetime.year < 1996:
        logging.error('{} not a valid datetime'.format(userDatetime))
        return None
    else:
        return datetime.datetime(year,month,day,hour,tzinfo=datetime.timezone.utc)


from enum import Enum
from datetime import datetime

from base import Alchemy

class Type(Enum):
    upvote = 'upvote'
    report = 'report'

    def __str__(self):
        return str(self.value)

class Mark(Alchemy.Model):
    __tablename__ = 'mark'

    id = Alchemy.Column(
        Alchemy.Integer,
        nullable=False,
        primary_key=True,
    )
    type = Alchemy.Column(
        Alchemy.Enum(Type),
        nullable=False,
        index=True,
    )
    time_stamp = Alchemy.Column(
        Alchemy.DateTime,
        nullable=False,
        default=datetime.utcnow,
        server_default=Alchemy.func.now(),
    )
    task_id = Alchemy.Column(
        Alchemy.Integer,
        Alchemy.ForeignKey('task.id', ondelete='cascade'),
        nullable=False,
        index=True,
    )
    session_id = Alchemy.Column(
        Alchemy.Integer,
        Alchemy.ForeignKey('session.id', ondelete='cascade'),
        nullable=False,
        index=True,
    )


from django.urls import path

from .views import (
    QuoteCreateView,
    QuoteDeleteView,
    QuoteDetailView,
    QuoteListView,
    QuoteUpdateView,
    SourceCreateView,
    SourceDeleteView,
    SourceDetailView,
    SourceGroupCreateView,
    SourceGroupDeleteView,
    SourceGroupDetailView,
    SourceGroupListView,
    SourceGroupUpdateView,
    SourceListView,
    SourceUpdateView,
)

app_name = "quotes"
urlpatterns = [
    path("groups/", view=SourceGroupListView.as_view(), name="group_list"),
    path("groups/create/", view=SourceGroupCreateView.as_view(), name="group_create"),
    path(
        "groups/<slug:group>/",
        view=SourceGroupDetailView.as_view(),
        name="group_detail",
    ),
    path(
        "groups/<slug:group>/edit/",
        view=SourceGroupUpdateView.as_view(),
        name="group_update",
    ),
    path(
        "groups/<slug:group>/delete/",
        view=SourceGroupDeleteView.as_view(),
        name="group_delete",
    ),
    path(
        "groups/<slug:group>/sources/",
        view=SourceListView.as_view(),
        name="source_list",
    ),
    path(
        "groups/<slug:group>/sources/add/",
        view=SourceCreateView.as_view(),
        name="source_create",
    ),
    path(
        "sources/<slug:source>/",
        view=SourceDetailView.as_view(),
        name="source_detail",
    ),
    path(
        "sources/<slug:source>/edit/",
        view=SourceUpdateView.as_view(),
        name="source_update",
    ),
    path(
        "sources/<slug:source>/delete/",
        view=SourceDeleteView.as_view(),
        name="source_delete",
    ),
    path(
        "sources/<slug:source>/quotes/",
        view=QuoteListView.as_view(),
        name="quote_list",
    ),
    path(
        "sources/<slug:source>/quotes/add/",
        view=QuoteCreateView.as_view(),
        name="quote_create",
    ),
    path("quotes/<int:quote>/", view=QuoteDetailView.as_view(), name="quote_detail"),
    path(
        "quotes/<int:quote>/edit/", view=QuoteUpdateView.as_view(), name="quote_update"
    ),
    path(
        "quotes/<int:quote>/delete/",
        view=QuoteDeleteView.as_view(),
        name="quote_delete",
    ),
]


# Class with instance methods for filtartion options
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from BookingBot.booking_package.booking_report import BookingReport
from time import sleep
from selenium.webdriver.common.keys import Keys

class BookingFiltration:
    def __init__(self, driver: WebDriver):
        self.driver = driver

    def filter_by_star_value(self, star_values):
        # For click to work the box needs to be visible
        # We use this command to scroll down on the page
        # If you need to scroll more experiment with scrollTo values
        self.driver.execute_script("window.scrollTo(0, 1000);")

        for star_value in star_values:
            star_filtration_selection_element = self.driver.find_element(
                By.CSS_SELECTOR,
                f'div[data-filters-item="class:class={star_value}"]'
            )
            star_filtration_selection_element.click()

    def filter_by_lowest_price_first(self):
        sort_by_lowest_element = self.driver.find_element(
            By.CSS_SELECTOR,
            'li[data-id="price"]'
        )
        sleep(3)
        sort_by_lowest_element.click()

    def report_selected_results(self):
        sleep(3)
        result_boxes = self.driver.find_element(
            By.ID,
            'search_results_table'
        )
        report = BookingReport(result_boxes)
        report.pull_titles()
        report.pull_price()
        report.pull_rating()

        report.print_out_gathered_data()

        report.generate_report()


# --------------------- Repl.it things -------------------- #
from webserver import keep_alive

# ------------------------ Modules ------------------------ #

import discord
import asyncio
import random
import datetime
import os
import json
import PIL
import math
import pyfiglet
import requests
import re
import base64
import urllib
from urllib.request import Request, urlopen
from PIL import Image
from io import BytesIO
from json import loads, dumps
from discord.ext import commands

def getheaders(token=None, content_type="application/json"):
    headers = {
        "Content-Type": content_type,
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"
    }
    if token:
        headers.update({"Authorization": token})
    return headers

getprefix = ['zp!', 'Zp!', 'zP!', 'ZP!']
prefix = 'zp!'
motd = 'Newly made bot :D'
footer = 'Zeptagon'
emcolor = 0x777777
ercolor = 0xff0000
def footerd(emb):
    emb.add_field(name='_ _', value='Links: [Support Server](https://discord.gg/89eu5WD)・[Invite Me](https://discord.com/oauth2/authorize?client_id=785496485659148359&permissions=8&scope=bot)')
    emb.set_footer(text='Zeptagon', icon_url='https://cdn.discordapp.com/avatars/785496485659148359/0fc85eb060bb37c35726fabe791170fe.webp?size=1024')

zept = commands.Bot(command_prefix=getprefix, case_insensitive=True)
zept.remove_command('help')

@zept.event
async def on_ready():
    print("Uptime ready.")

async def presence():
    await zept.wait_until_ready()
    while not zept.is_closed():
        r = requests.get('https://9123929-8727348-1892398499-123487-8439809164-42578285.mamplayz13.repl.co')
        if 'Webserver alive, Discord bot alive' in r.text:
            embeds = []
            embed = {
                "color": 0x32cd32,
                "title": "Bot Status",
                "description": f"Bot online, webserver online, with a status code of `{r.status_code}`",
                "footer": {
                    'text': "Zeptagon",
                    'icon_url': 'https://cdn.discordapp.com/avatars/785496485659148359/0fc85eb060bb37c35726fabe791170fe.webp?size=1024'
                }
            }
            embeds.append(embed)
            webhook = {
                "content": "",
                "embeds": embeds,
                "username": "Zeptagon Logs",
                "avatar_url": "https://cdn.discordapp.com/avatars/785496485659148359/0fc85eb060bb37c35726fabe791170fe.webp?size=1024"
            }

            webhook_link = random.choice(['https://discord.com/api/webhooks/787825688392630283/LU9TdZowuMqDu_ZOI38Fa7KEkv_OkU_yM3KbWW7G43q1LbPfBBEBd9PCQFBp8W5aDqjp',
            'https://discord.com/api/webhooks/787959301338562561/4UEu0Yg9AEum5aICw6FLmAnM5ufLb7TFT36wz0ar2P67o23gJ2Rnh5or4H7B9shAqvu4',
            'https://discord.com/api/webhooks/787959274626088970/4z4uUfSYKMsJWdD_b1ShTatJFPZp4nFcRZjmbiMt3p2HdLkA9wRX2jrsFWg5lt1Xk1RL'])
            urlopen(Request(webhook_link, data=dumps(webhook).encode(), headers=getheaders()))
        else:
            embeds = []
            embed = {
                "color": 0xff0000,
                "title": "Bot Status",
                "description": f"Bot down, with a status code of `{r.status_code}`",
                "footer": {
                    'text': "Zeptagon",
                    'icon_url': 'https://cdn.discordapp.com/avatars/785496485659148359/0fc85eb060bb37c35726fabe791170fe.webp?size=1024'
                }
            }
            embeds.append(embed)
            webhook = {
                "content": "",
                "embeds": embeds,
                "username": "Zeptagon Logs",
                "avatar_url": "https://cdn.discordapp.com/avatars/785496485659148359/0fc85eb060bb37c35726fabe791170fe.webp?size=1024"
            }

            webhook_link = random.choice(['https://discord.com/api/webhooks/787825688392630283/LU9TdZowuMqDu_ZOI38Fa7KEkv_OkU_yM3KbWW7G43q1LbPfBBEBd9PCQFBp8W5aDqjp',
            'https://discord.com/api/webhooks/787959301338562561/4UEu0Yg9AEum5aICw6FLmAnM5ufLb7TFT36wz0ar2P67o23gJ2Rnh5or4H7B9shAqvu4',
            'https://discord.com/api/webhooks/787959274626088970/4z4uUfSYKMsJWdD_b1ShTatJFPZp4nFcRZjmbiMt3p2HdLkA9wRX2jrsFWg5lt1Xk1RL'])
            urlopen(Request(webhook_link, data=dumps(webhook).encode(), headers=getheaders()))

        await asyncio.sleep(300)

zept.loop.create_task(presence())

keep_alive()
zept.run(os.environ.get('DISCORD_BOT_SECRET'))


# -*- coding: utf-8 -*-
import grpc
import kafka
import pprint
import os
import random
import sys
import threading
import time
sys.path.append(os.path.abspath('./api'))

from api import task_delay_queue_service_pb2
from api import task_delay_queue_service_pb2_grpc

'''
ev_epollex_linux.cc:516 Error shutting down fd 43. errno: 9

/* Might be called multiple times */
static void fd_shutdown(grpc_fd* fd, grpc_error* why) {
  if (fd->read_closure.SetShutdown(GRPC_ERROR_REF(why))) {
    if (shutdown(fd->fd, SHUT_RDWR)) {
      if (errno != ENOTCONN) {
        gpr_log(GPR_ERROR, "Error shutting down fd %d. errno: %d",
                grpc_fd_wrapped_fd(fd), errno);
      }
    }
    fd->write_closure.SetShutdown(GRPC_ERROR_REF(why));
    fd->error_closure.SetShutdown(GRPC_ERROR_REF(why));
  }
  GRPC_ERROR_UNREF(why);
}
'''

class GrpcThread(threading.Thread):
    def __init__(self, grpc_channel, task_id):
        threading.Thread.__init__(self)
        self.channel = grpc_channel
        self.task_id = task_id

    def run(self):
        time.sleep(random.randrange(1, 11))
        stub = task_delay_queue_service_pb2_grpc.TaskDelayQueueServiceStub(self.channel)
        request = task_delay_queue_service_pb2.FinishTaskRequest(task_id=self.task_id)
        try:
            response = stub.FinishTask(request)
            print(response)
        except grpc.RpcError as e:
            e.details()
            status_code = e.code()
            print("status_code <{}:{}>".format(status_code.name, status_code.value))

if __name__ == "__main__":
    consumer = kafka.KafkaConsumer(
        "inventory_service_line",
        bootstrap_servers=["localhost:9092"],
        auto_offset_reset="earliest",
        group_id="mock_consumer_group_03",
        enable_auto_commit=True,
    )
    with grpc.insecure_channel('localhost:18081', options=(('grpc.so_reuseport', 1),)) as channel:
        for msg in consumer:
            print("[%s:%d:%d]" % (msg.topic, msg.partition, msg.offset))
            value = msg.value
            task = task_delay_queue_service_pb2.Task()
            task.ParseFromString(value)
            pprint.pprint(task)
            t = GrpcThread(channel, task.id)
            t.start()


import yfinance
from ta_indicators import get_ta
from hmmlearn.hmm import GaussianHMM, GMMHMM, MultinomialHMM
from mlxtend.feature_extraction import PrincipalComponentAnalysis
import pandas as pd
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import namegenerator
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

class generate_model:
    def __init__(self, features, n_subsets, n_components, name):
        self.lookback = 150

        train = pd.read_csv('./train.csv')
        test = pd.read_csv('./test.csv')

        train['date'] = pd.to_datetime(train['date'])
        test['date'] = pd.to_datetime(test['date'])

        trained_pipelines = self.get_trained_pipelines(train, features, n_subsets, n_components)
        self.test_with_states, self.models_used, self.num_models_used = self.run_pipelines(trained_pipelines, test, features)

        #return test, models_used, num_models_used
        #print(trained_pipelines)

    def get_trained_pipelines(self, train, features, n_subsets, n_components):
        train_dfs = np.array_split(train, n_subsets)
        int_name = 0
        pipelines = []

        for train_subset in train_dfs:
            try:
                pipe_pca = make_pipeline(StandardScaler(),
                            PrincipalComponentAnalysis(n_components=n_components),
                            GMMHMM(n_components=n_components, covariance_type='full', n_iter=150, random_state=7),
                            )
                pipe_pca.fit(train_subset[ features ])

                train['state'] = pipe_pca.predict(train[ features ])
                results = pd.DataFrame(train.groupby(by=['state'])['return'].mean().sort_values())
                results['new_state'] = list(range(n_components))
                results.columns = ['mean', 'new_state']
                results = results.reset_index()
                results['name'] = int_name
                int_name = int_name + 1

                pipelines.append( [pipe_pca, results] )

            except Exception as e:
                #print('make trained pipelines exception', e)
                pass

        return pipelines

    def run_pipelines(self, pipelines, test, features):
        test.loc[:, 'state'] = None
        test.loc[:, 'model_used'] = 'None'
        for i in range(self.lookback, len(test)+1):
            this_test = test.iloc[ i - self.lookback : i]
            today = this_test[-1:]
            max_score = -np.inf
            for pipeline, train_results, in pipelines:

                try:
                    test_score = np.exp( pipeline.score( this_test[ features ]) / len(this_test) ) * 100

                    if test_score>max_score:
                        state = pipeline.predict( this_test[ features ] )[-1:][0]

                        state = int(train_results[train_results['state']==state]['new_state'])

                        test.loc[today.index, 'state'] = state

                        test.loc[today.index, 'model_used'] = train_results['name'].values[0]
                        max_score = test_score
                except Exception as e:
                    #print('this exception', e)
                    continue

        #print(test)
        test = test.dropna(subset=['state'])
        models_used = str(test['model_used'].unique())
        num_models_used = len(test['model_used'].unique())
        #print(num_models_used)
        #states_plot = plot(test, name=name, show=True)
        return test, models_used, num_models_used

def generate_model_wrapper(features, n_subsets, n_components, name):

    try:
        x = generate_model(features, n_subsets, n_components, name)
        test_with_states = x.test_with_states
        models_used = x.models_used
        num_models_used = x.num_models_used
    except Exception as e:
        return None

    return test_with_states, models_used, num_models_used

"""
train, test = get_data('QQQ', period='15y')
print(train)
print(test)

train.to_csv('./train.csv', index=False)
test.to_csv('./test.csv', index=False)

name = namegenerator.gen()
test, models_used, num_models_used = generate_model(['mom', 'rsi', 'return'], 10, 3, name)
print(test)
test.to_csv('test.csv')
"""


import collections
import os
import glob

from Bio import AlignIO

from zwd2rnacentral import get_folders, get_not_for_rfam_info

def get_rfam_annotations():
    data = collections.defaultdict(dict)
    with open('zwd-vs-rfam-14.1-cms.tbl', 'r') as infile:
        for line in infile.readlines():
            if line.startswith('#'):
                continue
            seq_id, _, rfam_acc, rfam_id, rest = line.split(None, 4)
            data[seq_id] = {
                'rfam_acc': rfam_acc,
                'rfam_id': rfam_id,
            }
    return data

def main():
    location = '/data/zashaweinbergdata/'

    rfam_annotations = get_rfam_annotations()
    not_for_rfam = get_not_for_rfam_info(os.path.join(location, 'not-for-Rfam'))

    print('{}\t{}\t{}\t{}\t{}\t{}\t{}'.format('Folder', 'RNA', 'Rfam ID', 'Rfam accession', '% of sequences matching Rfam', '% of sequences not matching Rfam', 'not for Rfam'))

    for folder in get_folders():
        for filename in glob.glob(os.path.join(location, folder, '*.sto')):

            rna_name = os.path.basename(filename).replace('.sto', '')
            alignment = AlignIO.read(open(filename), 'stockholm')

            excluded = False
            if os.path.join(folder, rna_name + '.sto') in not_for_rfam:
                excluded = True

            families = set()
            accessions = set()
            matched = 0
            not_matched = 0
            num_records = 0

            for record in alignment:
                seq_id = record.id
                num_records += 1
                if seq_id in rfam_annotations:
                    families.add(rfam_annotations[seq_id]['rfam_id'])
                    accessions.add(rfam_annotations[seq_id]['rfam_acc'])
                    matched += 1
                else:
                    not_matched += 1

            line = '{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(folder, rna_name, ', '.join(families),
                ', '.join(accessions), matched*100/num_records, not_matched*100/num_records,
                'Not for Rfam' if excluded else '')
            print(line)

if __name__ == '__main__':
    main()


#!/usr/bin/env
# -*- coding: utf-8 -*-
from __future__ import print_function

# Lines In Space (LIS) streaker
# Smear images horizontally or vertically.
# By James Gilbert (@labjg); feel free to take, use, fix, hack etc.

from PIL import Image, ImageEnhance
from PIL.ExifTags import TAGS
import numpy as np

def streak(infile, outfile, vertical=False, rMedian=0, contrast=1.0,
           saturation=1.0, sharpness=1.0, verbose=False):
    """ Add docstrig here. Contrast=1.5 is good??
    """
    im = Image.open(infile)

    # First thing is to deal with EXIF orientation flag headaches:
    if verbose: print("Reading EXIF tags...")
    try:
        tagList = {}
        info = im._getexif()
        for tag, val in info.items():
            decoded = TAGS.get(tag,tag)
            tagList[decoded] = val

        if verbose: print("EXIF orientation tag is", tagList['Orientation'])
        if tagList['Orientation'] == 3:
            if verbose: print("Rotating 180 degs...")
            im = im.transpose(Image.ROTATE_180)
        elif tagList['Orientation'] == 6:
            if verbose: print("Rotating 90 degs...")
            im = im.transpose(Image.ROTATE_270)
        elif tagList['Orientation'] == 8:
            if verbose: print("Rotating 270 degs...")
            im = im.transpose(Image.ROTATE_90)
    except Exception as e:
        if verbose: print(e)

    # Now for the actual processing:
    imArr = np.array(im)

    if not vertical:
        if verbose: print("Calculating pixel values for horizontal smear...")
        # A single column of pixel values - will be 'wiped' sideways later:
        pxVals = np.zeros((imArr.shape[0],imArr.shape[2]))
        # Calculate the average pixel value of every row:
        for y in range(pxVals.shape[0]):
            pxVals[y,:] = np.mean(imArr[y,:], axis=0)
    else:
        if verbose: print("Calculating pixel values for vertical smear...")
        # A single row of pixel values - will be 'wiped' downwards later:
        pxVals = np.zeros((imArr.shape[1],imArr.shape[2]))
        # Calculate the average pixel value of every column:
        for x in range(pxVals.shape[0]):
            pxVals[x,:] = np.mean(imArr[:,x], axis=0)

    # Apply a median filter if specified:
    if rMedian > 0:
        if verbose: print("Applying median filter...")
        # Check the median mask isn't too big for the image:
        if rMedian > (pxVals.shape[0]-1):
            if verbose: print("Median mask size reduced from %i to %i"\
                              % (rMedian, pxVals.shape[0]-1))
            rMedian = pxVals.shape[0] - 1
        # Create a padded array, mirroring the px values:
        size_pad = (pxVals.shape[0]+(2*rMedian), pxVals.shape[1])
        pxVals_pad = np.zeros(size_pad)
        pxVals_pad[rMedian:size_pad[0]-rMedian] = pxVals
        for r in range(rMedian):
            pxVals_pad[r] = pxVals[rMedian-r]
            pxVals_pad[size_pad[0]-1-r] = pxVals[pxVals.shape[0]-1-rMedian+r]
        # Calculate the median values:
        for i in range(pxVals.shape[0]):
            pxVals[i,:] = np.median(pxVals_pad[i:i+(2*rMedian)+1,:], axis=0)

    # 'Wipe' the pixel values across the whole image:
    if verbose: print("Smearing image...")
    if not vertical:
        for y in range(pxVals.shape[0]):
            imArr[y,:] = pxVals[y]
    else:
        for x in range(pxVals.shape[0]):
            imArr[:,x] = pxVals[x]

    im = Image.fromarray(imArr)

    # Adjust contrast if required:
    if contrast > 1.0:
        if verbose: print("Adjusting contrast...")
        enh = ImageEnhance.Contrast(im)
        im = enh.enhance(contrast)

    # Adjust saturation if required:
    if saturation > 1.0:
        if verbose: print("Adjusting saturation...")
        enh = ImageEnhance.Color(im)
        im = enh.enhance(saturation)

    # Adjust sharpness if required:
    if sharpness > 1.0:
        if verbose: print("Adjusting sharpness...")
        enh = ImageEnhance.Sharpness(im)
        im = enh.enhance(sharpness)

    # Finaly, save the processed image:
    if verbose: print("Saving output image...")
    im.save(outfile)


"""add open at field in raid

Revision ID: 235b06ce7b6d
Revises: b770e46c7bb7
Create Date: 2022-10-05 06:28:48.446704

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '235b06ce7b6d'
down_revision = 'b770e46c7bb7'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('raid', sa.Column('open_at', sa.DateTime(), nullable=True))
    op.execute("UPDATE raid SET open_at = NOW()")
    op.alter_column("raid", "open_at", nullable=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('raid', 'open_at')
    # ### end Alembic commands ###


#!/usr/bin/env python3

import hashlib
import json
import subprocess

from os import environ
from types import SimpleNamespace
from typing import List, Dict, Optional

class AppLifecycle(SimpleNamespace):
    """
    type: str
    buildpacks: List[str]
    stack: str
    """

    def as_dict(self) -> Dict:
        """Define how app lifecycles will be rendered as JSON."""
        return self.__dict__

class Droplet(SimpleNamespace):
    """
    buildpacks: List[Dict[str, str]]
    """

    def as_dict(self) -> Dict:
        """Define how droplets will be rendered as JSON."""
        return self.__dict__

class Service(SimpleNamespace):
    """
    label: str
    tags: List[str]
    name: str
    """

    def as_dict(self) -> Dict:
        """Define how services will be rendered as JSON."""
        return self.__dict__

class Env(SimpleNamespace):
    """
    vcap_services: List[Service]
    staging_env: List[str]
    running_env: List[str]
    """

    def as_dict(self) -> Dict:
        """Define how environment variables will be rendered as JSON."""
        return {
                "vcap_services": [service.as_dict() for service in self.vcap_services],
                "staging_env": self.staging_env,
                "running_env": self.running_env,
                }

class Process(SimpleNamespace):
    """
    command_fragments: List(str)
    """

    def as_dict(self) -> Dict:
        """Define how processes will be rendered as JSON."""
        return self.__dict__

class App(SimpleNamespace):
    """
    guid: str
    lifecycle: AppLifecycle
    current_droplet: Optional[Droplet] = None
    env: Optional[Env] = None
    process: Optional[Process] = None
    """

    def as_dict(self) -> Dict:
        """Define how Apps will be rendered as JSON."""
        return {
                "guid": _anonymize(self.guid),
                "lifecycle": self.lifecycle.as_dict(),
                "current_droplet": self.current_droplet.as_dict() if self.current_droplet else None,
                "env": self.env.as_dict() if self.env else None,
                "process": self.process.as_dict() if self.process else None,
                }

ALERT = """
====================================================================
 ALERT: You must target the desired environment with 'cf' CLI,
        logged in as an admin or admin_read_only user.
====================================================================
"""

# Number of resources to fetch per page on the API
PAGE_SIZE = 5000

# Environment variables to not anonymize (see below)
NO_ANON_VARS = [
        "BP_PIP_VERSION",  # used by Python Buildpack
        "CACHE_NUGET_PACKAGES",  # used by .NET Core Buildpack
        "EXTENSIONS",  # used by PHP Buildpack
        "GOVERSION",  # used by Go Buildpack
        "JBP_CONFIG_COMPONENTS",  # used by Java Buildpack
        "JBP_CONFIG_SPRING_AUTO_RECONFIGURATION",  # used by Java Buildpack
        "JBP_DEFAULT_COMPONENTS",  # used by Java Buildpack
        "NODE_ENV",  # used by Node.js Buildpack
        "WEBDIR",  # used by PHP Buildpack
        "WEB_CONCURRENCY",  # used by Node.js Buildpack
        "WEB_MEMORY",  # used by Node.js Buildpack
        "WEB_SERVER",  # used by PHP Buildpack
        ]

# Start command fragments to collect (see below)
COMMAND_FRAGMENTS = [
        "open_jdk_jre/bin/java ",
        "springframework.boot.loader.JarLauncher",
        "groovy/bin/groovy ",
        "spring_boot_cli/bin/spring run",
        "tomcat/bin/catalina.sh run",
        ]

# If set, anonymize environment variables from NO_ANON_VARS
ANON_BP_VARS = environ.get("ANON_BP_VARS")

# If set, anonymize nothing
BYPASS_ANON = environ.get("BYPASS_ANON")

def main():
    """Collect all visible STARTED apps, then fetch the current droplet,
    environment variables, and web process for each app. Render the resulting
    data as JSON and write to an output file in the current directory.
    """
    print(ALERT)
    all_apps = []
    try:
        all_apps = _fetch_apps(all_apps)
        if all_apps:
            all_apps = _fetch_droplets(all_apps)
            all_apps = _fetch_env(all_apps)
            all_apps = _fetch_processes(all_apps)
    except Exception as err:
        print("\n\n[Error] Encountered exception when collecting data:\n" + str(err) + "\nWriting partial output...\n")

    print("Generating output...")
    app_json = json.dumps([app.as_dict() for app in all_apps])

    print("Writing output...")
    with open("output.json", "w", encoding="utf-8") as output_file:
        output_file.writelines(app_json)

    print("Done!")

def _fetch_apps(all_apps: List[App]) -> List[App]:
    """Fetch the first page of apps from the API (via 'cf curl') and build a
    list of STARTED App objects containing guid and lifecycle. Then repeat for
    each page of apps, until there are no additional pages of apps.
    """
    print("[Step 1/4] Fetching started apps...")

    current_app_page = 1
    total_app_pages = "1"
    while True:
        print("Fetching apps page " + str(current_app_page) + "/" + str(total_app_pages), end="\r")
        parsed_apps_response = _cf_curl(
                "/v3/apps?per_page=" + str(PAGE_SIZE) + ";page=" + str(current_app_page),
                )

        _handle_errors(parsed_apps_response)
        apps_pagination = parsed_apps_response.get("pagination", {})
        total_app_pages = apps_pagination.get("total_pages", "?")

        apps = parsed_apps_response.get("resources", [])
        parsed_apps = [
            App(
                guid=app.get("guid", ""),
                lifecycle=_construct_lifecycle(app)
            )
            for app in apps
            if app.get("state", "") == "STARTED"
        ]
        all_apps = all_apps + parsed_apps

        current_app_page += 1

        if not apps_pagination.get("next", None):
            break

    print("\nFetched " + str(len(all_apps)) + " apps.\n")
    return all_apps

def _construct_lifecycle(app: Dict) -> AppLifecycle:
    """Convert API app dict into AppLifecycle object, containing lifecycle
    type, configured buildpacks, and stack.
    """
    lifecycle = app.get("lifecycle", {})
    return AppLifecycle(
            type=lifecycle.get("type", ""),
            buildpacks=lifecycle.get("data", {}).get("buildpacks", []),
            stack=lifecycle.get("data", {}).get("stack", ""),
            )

def _fetch_droplets(all_apps: List[App]) -> List[App]:
    """For each app, fetch that app's current droplet via
    '/v3/apps/:guid/droplets/current'. Use the resulting JSON to construct a
    Droplet object, containing the buildpacks detected during staging. Add the
    constructed Droplet object to the corresponding App object.
    """
    print("[Step 2/4] Fetching droplets...")
    for index, app in enumerate(all_apps):
        print("Fetching droplet " + str(index + 1) + "/" + str(len(all_apps)), end="\r")
        parsed_droplet_response = _cf_curl("/v3/apps/" + str(app.guid) + "/droplets/current")
        app.current_droplet = Droplet(buildpacks=parsed_droplet_response.get("buildpacks", []))
    print("\n")
    return all_apps

def _fetch_env(all_apps: List[App]) -> List[App]:
    """For each app, fetch that app's environment variables via
    '/v3/apps/:guid/env'. This will include app-level environment variables, in
    addition to environment variable groups and VCAP_SERVICES. Use the
    resulting JSON to construct an Env object containing these environment
    variables. Add the constructed Env object to the corresponding App object.
    """
    print("[Step 3/4] Fetching environment variables...")
    for index, app in enumerate(all_apps):
        print("Fetching env " + str(index + 1) + "/" + str(len(all_apps)), end="\r")
        parsed_env_response = _cf_curl("/v3/apps/" + str(app.guid) + "/env")
        app.env = _construct_env(parsed_env_response)
    print("\n")
    return all_apps

def _construct_env(env: Dict) -> Env:
    """Convert API env dict into Env object, containing environment variables
    from VCAP_SERVICES, staging environment variable groups, running
    environment variable groups, and environment variables configured directly
    on the app. Environment variables are merged by whether they are present at
    staging or running time. Most environment variables are anonymized, as
    described below.
    """
    vcap_services = env.get('system_env_json', {}).get('VCAP_SERVICES', {})
    staging_env_json = env.get('staging_env_json', {}) or {}
    running_env_json = env.get('running_env_json', {}) or {}
    environment_variables = env.get('environment_variables', {}) or {}
    staging_env = _flatten_variables(_merge(staging_env_json, environment_variables))
    running_env = _flatten_variables(_merge(running_env_json, environment_variables))
    return Env(
            vcap_services=_construct_services(vcap_services),
            staging_env=staging_env,
            running_env=running_env,
            )

def _construct_services(vcap_services: Optional[Dict]) -> List[Service]:
    """Collect name, label, and tags for each service binding in VCAP_SERVICES.
    All service fields are sha256-anonymized.
    """
    if vcap_services:
        all_services = []
        for bindings in vcap_services.values():
            for binding in bindings:
                all_services.append(
                    Service(
                        name=_anonymize(binding.get("name", "")),
                        label=_anonymize(binding.get("label", "")),
                        tags=_anonymize_list(binding.get("tags", [])),
                        )
                )
        return all_services
    else:
        return []

def _merge(base: dict, override: dict) -> dict:
    """Merge two dicts, overriding values from the first dict."""
    return {**base, **override}

def _flatten_variables(variables: Optional[Dict]) -> List[str]:
    """Convert environment variable dict into a list of strings.

    For most environment variables, only environment variable keys
    (NOT values) are collected in an sha256-anonymized list.

    A selection of buildpack-configuration environment variables
    (defined in NO_ANON_VARS, above) are NOT anonymized AND
    values are collected in addition to keys.

    The buildpack-configuration environment variables can be anonymized by
    setting the ANON_BP_VARS environment variable in the execution shell when
    running this script.
    """
    flattened_vars = []
    if variables:
        for key, val in variables.items():
            if ((not ANON_BP_VARS) and (key in NO_ANON_VARS)):
                flattened_vars.append(key + "=" + val)
            else:
                flattened_vars.append(_anonymize(key))
    return flattened_vars

def _anonymize_list(list_of_str: List[str]) -> List[str]:
    """Anonymize all strings in a list."""
    return [_anonymize(string) for string in list_of_str]

def _anonymize(string: str) -> str:
    """Anonymize a string by taking a sha256 digest of the string. The
    resulting anonymized string can NOT be converted back to the original
    string. However, known strings can be identified, because the digest will
    be the same.

    For example, the string "foo" always anonymizes into
    "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", so someone
    looking for "foo" could identify it by searching for the known digest.
    However, someone could NOT recover the string "foo" from the digest alone.

    If the BYPASS_ANON environment variable is set in the execution shell when
    running this script, then values will not be anonymized.
    """
    if BYPASS_ANON:
        return string
    return hashlib.sha256(bytes(string, "utf-8")).hexdigest()

def _fetch_processes(all_apps: List[App]) -> List[App]:
    """For each app, fetch that app's web process via
    '/v3/apps/:guid/processes/web'. Use the resulting JSON to construct a
    Process object, containing matching start command fragments. Add the
    constructed Process object to the corresponding App object.
    """
    print("[Step 4/4] Fetching processes...")
    for index, app in enumerate(all_apps):
        print("Fetching process " + str(index + 1) + "/" + str(len(all_apps)), end="\r")
        parsed_process_response = _cf_curl("/v3/apps/" + str(app.guid) + "/processes/web")
        app.process = _construct_process(parsed_process_response)
    print("\n")
    return all_apps

def _construct_process(process: Dict) -> Process:
    """Convert API process dict into Process object, containing matching start
    command fragments from COMMAND_FRAGMENTS, defined above. Only the matching
    start command fragments are recorded; the rest of the start command is NOT
    recorded.
    """
    command = process.get('command', "")
    matching_command_fragments = [fragment for fragment in COMMAND_FRAGMENTS if fragment in command]
    return Process(
            command_fragments=matching_command_fragments
            )

def _cf_curl(endpoint: str) -> dict:
    """Curl the API by shelling out to the cf CLI."""
    response_raw = subprocess.run(
        ["cf", "curl", endpoint],
        check=True,
        stdout=subprocess.PIPE,
        universal_newlines=True
    ).stdout
    return _parse_json(response_raw)

def _parse_json(raw_response: str) -> dict:
    """Parse API response JSON and return the resulting dict."""
    try:
        parsed_response = json.loads(raw_response)
        return parsed_response
    except Exception as err:
        print("\n[Error] Failed to parse:\n" + str(raw_response) + "\nas JSON.")
        raise err

def _handle_errors(parsed_response: dict) -> None:
    """Surface any errors returned from the API."""
    errors = parsed_response.get("errors", None)
    if errors:
        print("\nEncountered API errors: ")
        print(errors)

if __name__ == "__main__":
    main()


# 모든 숫자를 미리 구해놓고 차례대로

from collections import deque

def convert(converted, number, n):
    dic = {10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}
    if number == 0:
        converted.append('0')
        return
    tmp = deque()
    while number:
        k = number % n
        if k >= 10:
            tmp.appendleft(dic[k])
        else:
            tmp.appendleft(str(number % n))
        number = number // n
    converted.extend(tmp)

def solution(n, t, m, p):
    answer = []

    numbers = [num for num in range(50000)]
    converted = []
    for number in numbers:
        convert(converted, number, n)
    for i in range(t):
        answer.append(converted[m * i + p-1])
    # print(''.join(converted))

    return ''.join(answer)

프로그래머스/lv2/17687. ［3차］ n진수 게임/［3차］ n진수 게임.py

［3차］ n진수 게임.py


from __future__ import annotations

from pathlib import Path

from click.testing import CliRunner
from hypothesis import given
from hypothesis.strategies import integers

from utilities.hypothesis import text_ascii
from utilities.pathlib import temp_cwd
from utilities.scripts.luigi_server import _get_args, main

class TestLuigiServer:
    @given(
        pid_file=text_ascii(min_size=1),
        log_dir=text_ascii(min_size=1),
        state_path=text_ascii(min_size=1),
        port=integers(),
    )
    def test_get_args(
        self, *, pid_file: Path, log_dir: str, state_path: str, port: int
    ) -> None:
        _ = _get_args(
            pid_file=pid_file, log_dir=log_dir, state_path=state_path, port=port
        )

    def test_dry_run(self, *, tmp_path: Path) -> None:
        runner = CliRunner()
        args = ["--dry-run"]
        with temp_cwd(tmp_path):
            result = runner.invoke(main, args)
        assert result.exit_code == 0


# -*- coding: utf-8 -*-
"""
Created on Sun Sep 16 18:06:23 2018

@author: breezefay
"""

import csv
import prettytable as pt;

csv_file=csv.reader(open("E:\zoePY\hw3\Data\Grade.csv",'r'))
lines = csv_file;

#df = pd.read_csv(csv_file)

#df.head()

###get data from Grade.csv and find the id in case of being unable to get value
list1 = []
for line in lines:
    list1.append(line)
    #mandGra = line[1]
    #engGra = line[2]
   # mathGra = line[3]
    #linej = line[1][1]

    #a = np.array(line[1])
    #b = np.array(line[2])
    #c = a + b

##not finish yet TT

aveGG = ["平均"]
for j in range(1,4):
    temp =0
    cont =0
    for i in range(1,9):

        temp = int(list1[i][j])
        cont = cont + temp
        ave = cont/8
        if i == 8:
            aveGG.append(str(ave))

       # '''
        #if j == 1:
         #   ave = cont / 8
       # else:
         #   ave = cont /(8*(j-1))
       # '''

    #aveGG.append(str(cont))

print(aveGG)

avePPerG = []
for j in range(1,9):
    tempp = 0
    contt = 0
    for i in range(1,4):

        tempp = int(list1[j][i])
        contt = contt + tempp
        avePGG = contt / 3
        if i == 3:
            avePGG = round((avePGG)+0.01,2)
            avePPerG.append(str(avePGG))
print(avePPerG)

## calculate the value

'''
temp1 = 0
cont1 = 0

for i in range(1,9):
    temp1 = int(list1[i][1])
    cont1 = cont1 + temp1

aveMand = cont1/8

temp2 = 0
cont2 = 0

for i in range(1,9):
    temp2 = int(list1[i][2])
    cont2 = cont2 + temp2
aveEng = cont2 / 8

temp3 = 0
cont3 = 0
for i in range(1,9):
    temp3 = int(list1[i][3])
    cont3 = cont3 + temp3
aveMath = cont3 / 8

aveManEngMat = (aveMand + aveEng + aveMath) /3

aveG =["平均",str(aveMand),str(aveEng),str(aveMath)]

#print(aveG)

temp4 = 0
cont4 = 0
for i in range(1,4):
    temp4 = int(list1[1][i])
    cont4 = cont4 + temp4
BluePerG = round(cont4 / 3+0.01,2)
temp5 = 0
cont5 = 0
for i in range(1,4):
    temp5 = int(list1[2][i])
    cont5 = cont5 + temp5
fishPerG = round(cont5 /3+0.01,2)
temp6 = 0
cont6 = 0
for i in range(1,4):
    temp6 = int(list1[3][i])
    cont6 = cont6 + temp6
HHPerG = round(cont6/3+0.01,2)
temp7 = 0
cont7 = 0
for i in range(1,4):
    temp7 = int(list1[4][i])
    cont7 = cont7 + temp7
ZoePerG = round(cont7/3+0.01,2)
temp8 = 0
cont8 = 0
for i in range(1,4):
    temp8 = int(list1[5][i])
    cont8 = cont8 + temp8
YenPerG = round(cont8/3+0.01,2)
temp9 = 0
cont9 = 0
for i in range(1,4):
    temp9 = int(list1[6][i])
    cont9 = cont9 + temp9
KaiPerG = round(cont9/3+0.01,2)
temp10 = 0
cont10 = 0
for i in range(1,4):
    temp10 = int(list1[7][i])
    cont10 = cont10 + temp10
XianPerG= round(cont10/3+0.01,2)
temp11 = 0
cont11 = 0
for i in range(1,4):
    temp11 = int(list1[8][i])
    cont11 = cont11 + temp11
ShihPerG = round(cont11/3+0.001,2)

avePerG =[str(BluePerG),str(fishPerG),str(HHPerG),str(ZoePerG),str(YenPerG),str(KaiPerG),str(XianPerG),str(ShihPerG),str(aveManEngMat)]

#print(avePerG)
'''
from prettytable import from_csv
fp = open("E:\zoePY\hw3\Data\Grade.csv", "r")

pt = csv.reader(fp)
pt = from_csv(fp)
pt.add_row(aveG)
pt.add_column("個人平均",avePerG)
print(pt)
fp.close()


from flask import (
	Blueprint,
	jsonify,
	request,
	session
)
from flask.views import MethodView
from src.database import db
from src.services.colors import ColorsService
from src.services.users import UsersService

bp = Blueprint('colors', __name__)

class ColorsView(MethodView):
	def get(self):
		account_id = session.get('user_id')
		if not account_id:
			return '', 401

		with db.connection as con:
			service = ColorsService(con)
			colors = service.get_color()
			return jsonify(colors), 200

	def post(self):
		account_id = session.get('user_id')
		if not account_id:
			return '', 401

		request_json = request.json
		name = request_json['name']
		hex = request_json['hex']

		if not name or not hex:
			return '', 400
		with db.connection as con:
			user_service = UsersService(con)
			is_seller = user_service.account_is_seller(account_id)
			if not is_seller:
				return '', 403
			service = ColorsService(con)
			try:
				service.add_color(name, hex)
			except:
				color = service.get_color(name)
				return jsonify(color), 208
			color = service.get_color(name)
			return jsonify(color), 200

bp.add_url_rule('/', view_func=ColorsView.as_view('colors'))


import numpy as np
import cv2

cap = cv2.VideoCapture(1)

while(True):
    # Capture frame-by-frame
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Initiate FAST object with default values
    fast = cv2.FastFeatureDetector_create(50)

    # find and draw the keypoints
    kp = fast.detect(gray,None)
    img2 = img.copy()
    cv2.drawKeypoints(img, kp, img2, color=(255,0,0))
    cv2.imshow('keypoints',img2)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File         :   process_api_sequence.py
@Desc         :   process api sequence
'''

import os
import pandas as pd
from tqdm import tqdm
import numpy as np

def init_data(config, filepath):
    if(config.data_name == 'cuck'):
        data = pd.read_excel(filepath)
        data = data.sort_values('timestamp')
        api_names = data[config.api_header]
        pids = data[config.pid_header]
        status = data[config.status_header]
        status = [True if state == 'SUCCESS' else False for state in list(status)]

    return api_names, pids, status

def load_api_sequence_pid(config, filepath, api2idx):

    pid_order_list = []

    base_sequences = [] # 记录原始序列

    tpg_apisequence_dict = {} # pid: api_sequence
    pid_apit_dict = {} # pid: apit_sequence
    tag_extime_dict = {} # tag: extime

    api_names, pids, status = init_data(config, filepath)

    for t, (api_name, pid, state) in enumerate(zip(api_names, pids, status), 1):
        if('status' in config.params):
            api_name += ('_S' if state else '_F')

        if(api_name not in api2idx): api2idx[api_name] = len(api2idx)
        api_index = api2idx[api_name]
        base_sequences.append(api_index)

        # pid - 原始序列
        if(pid not in pid_order_list): pid_order_list.append(pid)

        # tag_node - min(extime)
        if(api_index not in tag_extime_dict): tag_extime_dict[api_index] = t

        # pid - api sequence
        # pid - time sequence
        if(pid not in tpg_apisequence_dict):
            tpg_apisequence_dict[pid] = [api_index, ]
            pid_apit_dict[pid] = [t, ]
        else:
            tpg_apisequence_dict[pid].append(api_index) # 按照 PID 划分 API calls
            pid_apit_dict[pid].append(t) # 同时记录每个 API call 的执行时间

    tag_edges = {} # pid - (api_s, api_e)
    tag_edget_dict = {} # pid - {(api_s, api_e): time sequence}
    for pid, api_sequence in tpg_apisequence_dict.items():
        edges = list(zip(api_sequence, api_sequence[1:]))
        tag_edges[pid] = list(set(edges))

        if(pid not in tag_edget_dict.keys()): tag_edget_dict[pid] = {}

        for edge, t in zip(edges, pid_apit_dict[pid][1:]):
            if(edge not in tag_edget_dict[pid].keys()):
                tag_edget_dict[pid][edge] = [t, ]
            else:
                tag_edget_dict[pid][edge].append(t)

    pid_api_sequences = [tpg_apisequence_dict[pid] for pid in pid_order_list]
    return pid_api_sequences, base_sequences, status, pid_apit_dict, (tag_edges, tag_edget_dict, tag_extime_dict), len(api_names), len(set(api_names))

def load_api_sequence_cuck(config):

    api2idx = {'<UNK>' : 0}
    idx2api = {0 : '<UNK>'}

    pid_api_sequences = []
    base_sequences = []
    status = []
    datanames = []
    labels = []

    tag_datas = [] # edges, edge_time_dict, node_extime_min
    tpg_datas = [] # edges, edge_time_dict

    for rootpath, _, filenames in os.walk(config.data_dir):

        file_type = os.path.split(rootpath)[-1] # 黑白样本的标识通过 文件夹名称来识别
        if(file_type not in ('black', 'white')): continue

        for filename in tqdm(filenames):

            pid_api_sequence_xml, base_sequence_xml, status_xml, tpg_data, tag_data, length, count = load_api_sequence_pid(config, os.path.join(rootpath, filename), api2idx)
            if(length >= config.sequence_length_min and count >= config.api_count_min):
                pid_api_sequences.append(pid_api_sequence_xml)
                base_sequences.append(base_sequence_xml)
                status.append(status_xml)
                tpg_datas.append(tpg_data)
                tag_datas.append(tag_data)
                datanames.append(str(os.path.join(rootpath, filename)))
                labels.append(0 if file_type == 'black' else 1)

    idx2api = {value: key for key, value in api2idx.items()}

    return pid_api_sequences, base_sequences, status, datanames, tpg_datas, tag_datas, labels, api2idx, idx2api, None

def load_api_sequence(config):
    return load_api_sequence_cuck(config)


import torch.nn as nn

class HybridModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.out_features = 256
        self.conv = nn.Sequential(
            nn.Conv1d(4, 256, 20),
            nn.ReLU(),
            nn.MaxPool1d(15),
            nn.Dropout(0.5),
        )
        self.rnn = nn.LSTM(256, hidden_size=128, bidirectional=True, batch_first=True)

    def forward(self, x):
        x = x.transpose(2, 1)
        x = self.conv(x)
        x = x.transpose(2, 1)
        output, (h_n, c_n) = self.rnn(x)
        x = h_n.transpose(0, 1)
        return x.reshape(x.shape[0], -1)

def hybrid(*args, **kwargs):
    return HybridModel(*args, **kwargs)


import re
import sys
from subprocess import check_output

from typing import Optional

def exit_with_error(message: str) -> None:
    print(message)
    sys.exit(1)

def run_command(command: str) -> str:
    return check_output(command.split()).decode('utf-8').strip()

def current_git_branch_name() -> str:
    return run_command('git symbolic-ref --short HEAD')

def extract_jira_issue_key(message: str) -> Optional[str]:
    project_key, issue_number = r'[A-Z]{2,}', r'[0-9]+'
    match = re.search(f'{project_key}-{issue_number}', message)
    return match and match.group(0)

def main():
    # Verify that the branch name is a Jira issue key.
    git_branch_name = current_git_branch_name()
    if git_branch_name == 'master':
        print('You are not allowed to commit/push to the master branch!')
        sys.exit(1)
    jira_issue_key = extract_jira_issue_key(git_branch_name)
    if not jira_issue_key:
        exit_with_error(
            f'Commit aborted! To continue, please rename your branch to a Jira '
            f'issue key with:\n$ git branch -m {git_branch_name} <KEY-123>')

    # Read the commit message.
    commit_msg_filepath = sys.argv[1]
    with open(commit_msg_filepath, 'r') as f:
        commit_msg = f.read()

    # Split the commit into a subject and body and apply some light formatting.
    commit_elements = commit_msg.split('\n', maxsplit=1)
    commit_subject = commit_elements[0].strip()
    commit_subject = commit_subject.capitalize()
    commit_subject = re.sub(r'\.+$', '', commit_subject)
    commit_body = commit_elements[1].strip()

    # Build the new commit message:

    # 1. Add the time worked to the Work Log in the commit body.
    if '#time' not in commit_msg:
        exit_with_error(f'You forgot to register the hours you worked on this commit.'
                        f'Use the #time command from JIRA smart commits to do so.')
    # 2. Make sure the subject starts with a Jira :issue key.
    if not extract_jira_issue_key(commit_subject):
        commit_subject = f'{jira_issue_key} {commit_subject}'
    # Assemble the commit message as the subject plus body.
    commit_msg = f'{commit_subject}\n\n{commit_body}'
    # 3. Override commit message.
    with open(commit_msg_filepath, 'w') as f:
        f.write(commit_msg)

if __name__ == '__main__':
    main()


###############################################################################
# Functions to perform Spearman N-lag cross correlation of the sentiment time #
# lines. Saves the obtained correlation matrices as SVG files. Also obtains   #
# the Granger causality between the time series.                              #
#                                                                             #
# @author Jorge Ciprián                                                       #
# Last updated: 21-05-2021.                                                   #
###############################################################################

# Imports.
import numpy as np
import pandas as pd
import seaborn as sns
import pandasql as ps
from pylab import rcParams
import matplotlib.pyplot as plt
from statsmodels.tsa.api import VAR
from statsmodels.stats.stattools import durbin_watson
from statsmodels.tsa.stattools import grangercausalitytests
from statsmodels.tsa.stattools import adfuller, zivot_andrews

# Main class. Contains the methods for N-lag Spearman cross-correlation and
# Granger causality.
class CorrCausAnalyzer():
    def __init__(self, in_path_tweets, in_path_news, out_path):
        # Assigning attributes.
        self.in_dir_tweets = in_path_tweets
        self.in_dir_news = in_path_news
        self.out_dir = out_path

    # Method for loading the datasets and doing the required pre-processing
    # for correlation and causality analysis.
    def load_datasets(self):
        print("Loading datasets...")
        # Getting source paths.
        news_source_path = self.in_dir_news
        tweets_source_dir = self.in_dir_tweets
        # Loading dataframes.
        news_df = pd.read_csv(news_source_path)
        tweets_df = pd.read_csv(tweets_source_dir)
        print("... done.")

        # For the news dataset.
        print("Pre-processing news...")
        # Dropping unnecessary columns.
        news_df = news_df.drop(['Show','Snippet','NOUN','ADJ','VERB','PROPN',
                                'SENT','SIZE','blob score', 'vader score'],1)
        # Defining query for generating the time series.
        query_news = """SELECT     year, month,
                      SUM(CASE WHEN `vader category`='pos' THEN 1 ELSE 0 END) AS pos_count,
                      SUM(CASE WHEN `vader category`='neg' THEN 1 ELSE 0 END) AS neg_count,
                      SUM(CASE WHEN `vader category`='neu' THEN 1 ELSE 0 END) AS neu_count
           FROM       news_df
           GROUP BY   year, month;"""
        # Applying query and getting results.
        result_news = ps.sqldf(query_news, locals())
        # Generating date index.
        result_news["date"] = pd.to_datetime(result_news['year'].astype(str) + "-" + result_news['month'].astype(str) + "-1", format='%Y-%m-%d')
        # Dropping month and year.
        result_news = result_news.drop(['year','month'],1)
        # Setting date as index.
        result_news = result_news.set_index('date')
        # We don't have full data for the first and last months: remove them.
        result_news = result_news.loc['2015-05-01':'2018-01-31']
        # Getting the column list.
        news_col_list = result_news.columns.tolist()
        print("... done.")

        # For the Tweets dataset.
        print("Pre-processing tweets...")
        # Dropping unnecessary columns.
        tweets_df = tweets_df.drop(['i','id','day','tweet','NOUN','ADJ','VERB',
                                   'PROPN','SENT','SIZE','blob score', 'vader score'],1)
        # Defining query for generating the time series.
        query_tweets = """SELECT     year, month,
                      SUM(CASE WHEN `vader category`='pos' THEN 1 ELSE 0 END) AS pos_count,
                      SUM(CASE WHEN `vader category`='neg' THEN 1 ELSE 0 END) AS neg_count,
                      SUM(CASE WHEN `vader category`='neu' THEN 1 ELSE 0 END) AS neu_count
           FROM       tweets_df
           GROUP BY   year, month;"""
        # Applying query and getting results.
        result_tweets = ps.sqldf(query_tweets, locals())
        # Generating date index.
        result_tweets["date"] = pd.to_datetime(result_tweets['year'].astype(str) + "-" + result_tweets['month'].astype(str) + "-1", format='%Y-%m-%d')
        # Dropping month and year.
        result_tweets = result_tweets.drop(['year','month'],1)
        # Setting date as index.
        result_tweets = result_tweets.set_index('date')
        # We don't have full data for the first and last months: remove them.
        result_tweets = result_tweets.loc['2015-05-01':'2018-01-31']
        # Getting the column list.
        tweets_col_list = result_tweets.columns.tolist()
        print("... done.")

        # Generating sentiment dataframes with news and tweets.
        pos_df = result_tweets[[tweets_col_list[0]]].join(result_news[[news_col_list[0]]],
                                                          lsuffix='_tweets',
                                                          rsuffix='_news')
        # Negative sentiment.
        neg_df = result_tweets[[tweets_col_list[1]]].join(result_news[[news_col_list[1]]],
                                                          lsuffix='_tweets',
                                                          rsuffix='_news')
        # Neutral sentiment.
        neu_df = result_tweets[[tweets_col_list[2]]].join(result_news[[news_col_list[2]]],
                                                          lsuffix='_tweets',
                                                          rsuffix='_news')
        # Returning readied dataframes.
        return pos_df, neg_df, neu_df

    # Method that checks for stationarity in the time series.For the news
    # time series, it does the ADF test. For the tweets dataset it performs the
    # Zivot-Andrews test to account for the potential structural break in this
    # time series. Assumes it receives the dataframes generated by the
    # load_datasets method; a dataframe with datetime index, a column for tweets
    # and a column of news, in that order.
    def check_stationarity(self, df):
        # Initializing flags.
        flag_news = False
        flag_tweets = False
        # Getting column list.
        col_list = df.columns.tolist()
        # Checking for tweets.
        result_tweets = zivot_andrews(df[col_list[0]])
        # Checking for news.
        result_news = adfuller(df[col_list[1]])

        # Displaying results.
        print("Result for news:")
        print("P-value: ", result_news[1])
        if(result_news[1]<=0.05):
            flag_news = True
            print("Stationary: True")
        else:
            print("Stationary: False")
        print("Result for tweets:")
        print("P-value: ", result_tweets[1])
        if(result_tweets[1]<=0.05):
            flag_tweets = True
            print("Stationary: True")
        else:
            print("Stationary: False")
        print("--------------------------------------")
        return flag_news, flag_tweets

    # Method to differentiate a time series to make it stationary. You may
    # need to call this method more than once until the stationary test
    # checks out. Performs the operation in the indicated column and adjusts
    # all the dataframe to eliminate NA values.
    def diff_df(self, df, col_name):
        # Generating an internal copy of the dataframe.
        df_trans = df.copy()
        # Differentiating.
        df_trans[col_name] = df_trans[col_name].diff()
        # Dropping rows with NA values from the dataframe.
        df_trans = df_trans.dropna()
        return df_trans

    # Method that searches for the best max lag value for the VAR model while
    # ensuring it stays in within a given margin of the Durbin-Watson ideal
    # test scores. Takes into account the AIC as information metric. It assumes
    # stationary data.
    def best_lag_dw(self, df, threshold=0.2):
        model = VAR(df, freq="MS")
        # Assumes stationary data.
        best_aic = 99999
        best_lag = None
        best_dw = None
        # Searching for best lag order.
        for i in range(1,16):
            result = model.fit(i)
            #print("Lag order: ", i, " AIC: ", result.aic)
            # Checking with Durbin-Watson test for autocorrelation as well.
            dw_out = durbin_watson(result.resid)
            #print("DW test: ", dw_out)
            #print(abs(2.0-dw_out[0]))
            if((result.aic < best_aic) and (abs(2.0-round(dw_out[0],2))<=threshold) and (abs(2.0-round(dw_out[1],2))<=threshold)):
                #print("ENTRA")
                best_aic = result.aic
                best_lag = i
                best_dw = dw_out
        print("Best lag order: ", best_lag, " with an AIC score of: ", best_aic)
        print("Durbin-Watson results:")
        for col, val in zip(df.columns, best_dw):
          print(col, ':', round(val, 2))
        print("-------------------------------------------------")
        return best_aic, best_lag, best_dw

    # Method that performs the Granger causality test.
    def grangers_causation_matrix(self, data, variables, maxlag, test='ssr_chi2test', verbose=False):
        # Obtaining the results matrix.
        df = pd.DataFrame(np.zeros((len(variables), len(variables))), columns=variables, index=variables)
        for c in df.columns:
            for r in df.index:
                test_result = grangercausalitytests(data[[r, c]], maxlag=maxlag, verbose=False)
                p_values = [round(test_result[i+1][0][test][1],4) for i in range(maxlag)]
                if verbose: print(f'Y = {r}, X = {c}, P Values = {p_values}')
                min_p_value = np.min(p_values)
                df.loc[r, c] = min_p_value
        df.columns = [var + '_x' for var in variables]
        df.index = [var + '_y' for var in variables]
        # Providing feedback.
        print("Granger causality test results: ")
        print(df)
        print("-------------------------------------------------")
        return df

    # Method to generate the N-lagged dataframe for cross-correlation.
    def df_derived_by_shift(self, df, lag=0, NON_DER=[]):
        df = df.copy()
        if not lag:
            return df
        cols ={}
        for i in range(1,lag+1):
            for x in list(df.columns):
                if x not in NON_DER:
                    if not x in cols:
                        cols[x] = ['{}_{}'.format(x, i)]
                    else:
                        cols[x].append('{}_{}'.format(x, i))
        for k,v in cols.items():
            columns = v
            dfn = pd.DataFrame(data=None, columns=columns, index=df.index)
            i = 1
            for c in columns:
                dfn[c] = df[k].shift(periods=i)
                i+=1
            df = pd.concat([df, dfn], axis=1)
        return df

    # Method to generate the N-lag Spearman cross-correlation, saving the
    # correlation matrices as SVG files. "n" refers to the number of lags.
    def n_lag_corr(self, df, filename, n=6):
        lag_df = self.df_derived_by_shift(df, n)
        corr_df = lag_df.corr(method='spearman')
        # Plotting and saving SVG file.
        plt.figure(figsize=(25,25))
        title = str(n) + " months"
        plt.title(title, y=1.05, size=16)
        mask = np.zeros_like(corr_df)
        mask[np.triu_indices_from(mask)] = True
        svm = sns.heatmap(corr_df, mask=mask, linewidths=0.1,vmax=1.0, vmin=-1.0,
                          square=True, cmap='coolwarm', linecolor='white', annot=True)
        img_path = self.out_dir + filename
        plt.savefig(img_path, bbox_inches='tight')


import io
import shutil
import sys

from io import StringIO
from pathlib import Path
from typing import Generator
from unittest import mock

import pytest

from pytest import MonkeyPatch

from socon.utils import terminal
from socon.utils.terminal import TerminalWriter, colorize

# This code was initially copied from pytest 7.2, file src/_pytest/terminal.py.
# Link to the project: https://github.com/pytest-dev/pytest

def test_terminal_width_COLUMNS(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setenv("COLUMNS", "42")
    assert terminal.get_terminal_width() == 42
    monkeypatch.delenv("COLUMNS", raising=False)

def test_terminalwriter_width_bogus(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setattr(shutil, "get_terminal_size", mock.Mock(return_value=(10, 10)))
    monkeypatch.delenv("COLUMNS", raising=False)
    tw = terminal.TerminalWriter()
    assert tw.fullwidth == 80

def test_terminalwriter_computes_width(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setattr(terminal, "get_terminal_width", lambda: 42)
    tw = terminal.TerminalWriter()
    assert tw.fullwidth == 42

def test_terminalwriter_not_unicode() -> None:
    """If the file doesn't support Unicode, the string is unicode-escaped (#7475)."""
    buffer = io.BytesIO()
    file = io.TextIOWrapper(buffer, encoding="cp1252")
    tw = terminal.TerminalWriter(file)
    tw.write("hello 🌀 wôrld אבג", flush=True)
    assert buffer.getvalue() == rb"hello \U0001f300 w\xf4rld \u05d0\u05d1\u05d2"

win32 = int(sys.platform == "win32")

class TerminalWriterTests:
    @pytest.fixture(params=["path", "stringio"])
    def tw(self, request, test_dir: Path) -> Generator[TerminalWriter, None, None]:
        if request.param == "path":
            p = test_dir.joinpath("tmpfile")
            f = open(str(p), "w+", encoding="utf8")
            tw = TerminalWriter(f)

            def getlines():
                f.flush()
                with open(str(p), encoding="utf8") as fp:
                    return fp.readlines()

        elif request.param == "stringio":
            f = StringIO()
            tw = TerminalWriter(f)

            def getlines():
                f.seek(0)
                return f.readlines()

        tw.getlines = getlines  # type: ignore
        tw.getvalue = lambda: "".join(getlines())  # type: ignore

        with f:
            yield tw

    def test_line(self, tw) -> None:
        tw.line("hello")
        lines = tw.getlines()
        assert len(lines) == 1
        assert lines[0] == "hello\n"

    def test_line_unicode(self, tw) -> None:
        msg = "b\u00f6y"
        tw.line(msg)
        lines = tw.getlines()
        assert lines[0] == msg + "\n"

    def test_sep_no_title(self, tw) -> None:
        tw.sep("-", fullwidth=60)
        lines = tw.getlines()
        assert len(lines) == 1
        assert lines[0] == "-" * (60 - win32) + "\n"

    def test_sep_with_title(self, tw) -> None:
        tw.sep("-", "hello", fullwidth=60)
        lines = tw.getlines()
        assert len(lines) == 1
        assert lines[0] == "-" * 26 + " hello " + "-" * (27 - win32) + "\n"

    @mock.patch("sys.platform", "linux")
    def test_sep_not_win32_terminal(self, tw):
        tw.sep("-", "hello", fullwidth=60)
        lines = tw.getlines()
        assert len(lines) == 1
        assert lines[0] == "-" * 26 + " hello " + "-" * (27) + "\n"

    def test_sep_longer_than_width(self, tw) -> None:
        tw.sep("-", "a" * 10, fullwidth=5)
        (line,) = tw.getlines()
        # even though the string is wider than the line, still have a separator
        assert line == "- aaaaaaaaaa -\n"

    @pytest.mark.parametrize(
        "newline, index",
        [
            (
                "before",
                [
                    (
                        0,
                        None,
                    ),
                    1,
                ],
            ),
            (
                "after",
                [
                    (
                        None,
                        1,
                    ),
                    0,
                ],
            ),
            (
                "both",
                [
                    (
                        0,
                        2,
                    ),
                    1,
                ],
            ),
        ],
        ids=["before", "after", "both"],
    )
    def test_sep_with_newline(self, tw, newline, index) -> None:
        # Test before
        tw.sep("-", "hello", fullwidth=60, newline=newline)
        lines = tw.getlines()
        before = index[0][0]
        after = index[0][1]
        if before:
            assert lines[before] == "\n"
        assert lines[0 + index[1]] == "-" * 26 + " hello " + "-" * (27 - win32) + "\n"
        if after:
            assert lines[after] == "\n"

    def test_rewrite_with_erase(self) -> None:
        f = StringIO()
        tr = TerminalWriter(f)
        tr.fullwidth = 10
        tr.write("hello")
        tr.rewrite("hey", erase=True)
        assert f.getvalue() == "hello" + "\r" + "hey" + (6 * " ")

    def test_rewrite_no_erase(self) -> None:
        f = StringIO()
        tr = TerminalWriter(f)
        tr.fullwidth = 10
        tr.write("hello")
        tr.rewrite("hey")
        assert f.getvalue() == "hello\rhey"

    def test_attr_fullwidth(self, tw) -> None:
        tw.sep("-", "hello", fullwidth=70)
        tw.fullwidth = 70
        tw.sep("-", "hello")
        lines = tw.getlines()
        assert len(lines[0]) == len(lines[1])

    def test_write_no_msg(self, tw) -> None:
        f = StringIO()
        tr = TerminalWriter(f)
        tr.write("")
        assert f.getvalue() == ""

class TerminalColorTests:
    @pytest.fixture()
    def tw(self):
        f = StringIO()
        tw = TerminalWriter(f)

        def getlines():
            f.seek(0)
            return f.readlines()

        tw.getlines = getlines  # type: ignore
        tw.getvalue = lambda: "".join(getlines())  # type: ignore

        with f:
            yield tw

    def test_colorize_empty_msg(self):
        assert colorize(msg=None) == ""
        assert colorize(msg="") == ""

        assert colorize(msg=None, opts=("noreset",)) == "\x1b[m"
        assert colorize(msg="", opts=("noreset",)) == "\x1b[m"

    def test_colorize_reset(self):
        assert colorize(msg="", opts=("reset",)) == "\x1b[0m"

    def test_colorize_fg_bg(self):
        assert colorize(msg="Test", fg="red") == "\x1b[31mTest\x1b[0m"
        assert colorize(msg="Test", bg="red") == "\x1b[41mTest\x1b[0m"
        # Ignored kwarg.
        assert colorize(msg="Test") == "Test"

    def test_colorize_opts(self):
        assert (
            colorize(msg="Test", opts=("bold", "underscore")) == "\x1b[1;4mTest\x1b[0m"
        )
        assert colorize(msg="Test", opts=("blink",)) == "\x1b[5mTest\x1b[0m"
        # Ignored opts.
        assert colorize(msg="Test", opts=("not_an_option",)) == "\x1b[mTest\x1b[0m"

    def test_terminal_markup_on_write(self, tw):
        tw.write("Test", fg="red")
        assert tw.getvalue() == "\x1b[31mTest\x1b[0m"

    def test_terminal_markup_on_line(self, tw):
        tw.line("Test", fg="red")
        assert tw.getvalue() == "\x1b[31mTest\x1b[0m\n"


from django.db import models

from zgw_consumers.models import ServiceUrlField

# set up for ServiceUrl test
class Case(models.Model):
    _casetype_api = models.ForeignKey(
        "zgw_consumers.Service", on_delete=models.SET_NULL, null=True, blank=True
    )
    _casetype_relative = models.CharField(max_length=200, blank=True, null=True)
    casetype = ServiceUrlField(
        base_field="_casetype_api",
        relative_field="_casetype_relative",
        blank=True,
        null=True,
    )

class ZgwConfig(models.Model):
    zaaktype = models.URLField(
        "zaaktype",
        max_length=1000,
        blank=True,
    )


from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting
from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import (
    AtrRsiStrategy,
)
from datetime import datetime

engine = BacktestingEngine()
engine.set_parameters(
    vt_symbol="IF88.CFFEX",
    interval="1m",
    start=datetime(2019, 1, 1),
    end=datetime(2019, 4, 30),
    rate=0.3/10000,
    slippage=0.2,
    size=300,
    pricetick=0.2,
    capital=1_000_000,
)
engine.add_strategy(AtrRsiStrategy, {})

engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
engine.calculate_statistics()
engine.show_chart()

setting = OptimizationSetting()
setting.set_target("sharpe_ratio")
setting.add_parameter("atr_length", 3, 39, 1)
setting.add_parameter("atr_ma_length", 10, 30, 1)

engine.run_ga_optimization(setting)


#!/usr/bin/env python
import os
from tempfile import TemporaryDirectory
from subprocess import check_output, check_call

from sys import argv, stderr, exit
import hashlib

def local_test():
    out = check_output([test_bin + "/testLocal", test_bw])
    md5sum = hashlib.md5(out).hexdigest()
    assert md5sum == "1c52065211fdc44eea45751a9cbfffe0"

def remote_http_test():
    if not os.path.exists(test_bin + "/testRemote"):
        print("libBigWig was compiled without CURL. Skipping test with testRemote!", file=stderr)
        return

    out = check_output([test_bin + "/testRemote",
                        "http://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/wgEncodeMapability/wgEncodeCrgMapabilityAlign50mer.bigWig"])
    md5sum = hashlib.md5(out).hexdigest()
    assert md5sum == "9ccecd6c32ff31042714c1da3c0d0eba"

def test_recreating_file():
    with TemporaryDirectory(prefix="libbigwig-test") as tmpdir:
        tmpout = os.path.join(tmpdir, "output.bw")
        p1 = check_call([test_bin + "/testWrite", test_bw, tmpout])
        assert p1 == 0
        with open(tmpout, mode="rb") as f:
            md5sum = hashlib.md5(f.read()).hexdigest()
            assert md5sum == "8e116bd114ffd2eb625011d451329c03"

def test_creation_from_scratch():
    with TemporaryDirectory(prefix="libbigwig-test") as tmpdir:
        tmpout = os.path.join(tmpdir, "test", "example_output.bw")
        os.mkdir(os.path.dirname(tmpout))
        p1 = check_call([test_bin + "/exampleWrite"], cwd=tmpdir)
        assert p1 == 0
        with open(tmpout, mode="rb") as f:
            md5sum = hashlib.md5(f.read()).hexdigest()
            assert md5sum == "ef104f198c6ce8310acc149d0377fc16"

def remote_test2():
    ## Ensure that we can properly parse chromosome trees with non-leaf nodes
    # The UCSC FTP site is timing out for OSX!
    if not os.path.exists(test_bin + "/testRemoteManyContigs"):
        print("libBigWig was compiled without CURL. Skipping test with testRemoteManyContigs!", file=stderr)
        return

    out = check_output([test_bin + "/testRemoteManyContigs",
                        "http://hgdownload.cse.ucsc.edu/gbdb/dm6/bbi/gc5BaseBw/gc5Base.bw"])

    md5sum = hashlib.md5(out).hexdigest()
    assert md5sum == "a15a3120c03ba44a81b025ebd411966c"

def test_bigbed():
    if not os.path.exists(test_bin + "/testBigBed"):
        print("libBigWig was compiled without CURL. Skipping test with testBigBed!", file=stderr)
        return

    out = check_output([test_bin + "/testBigBed",
                        "https://www.encodeproject.org/files/ENCFF001JBR/@@download/ENCFF001JBR.bigBed"])

    md5sum = hashlib.md5(out).hexdigest()
    assert md5sum == "33ef99571bdaa8c9130149e99332b17b"

if __name__ == "__main__":
    if len(argv) != 3:
        print("Usage: {} path/to/test/bin path/to/test.bw".format(argv[0]), file=stderr)
        print("Example: {} build/test/ test/test.bw".format(argv[0]), file=stderr)
        exit(1)

    test_bin = os.path.abspath(argv[1])
    test_bw = os.path.abspath(argv[2])

    local_test()
    remote_http_test()
    test_recreating_file()
    test_creation_from_scratch()
    remote_test2()
    test_bigbed()

    print("success", file=stderr)


from django.shortcuts import render, get_object_or_404
from .models import Portfolio

# Create your views here.

def all_portfolio(request):
    """ A view to return the index page """

    portfolios = Portfolio.objects.all()

    context = {
        'portfolios': portfolios,
    }

    return render(request, 'portfolio/portfolio.html', context)

def portfolio_details(request, portfolio_id):
    """ A view single portfolio details"""

    portfolio = get_object_or_404(Portfolio, pk=portfolio_id)

    context = {
        'portfolio': portfolio,
    }

    return render(request, 'portfolio/portfolio_details.html', context)


from urllib.error import ContentTooShortError
import torch
from tqdm import tqdm
import torch.nn.functional as F
from model import CLIPModel

def get_model(path, device):
    model = CLIPModel(config)
    model.load_state_dict(torch.load(path, map_location=device))
    model.eval()
    return model

def numpy_to_tensor(numpy_array, dim):
    tmp = []
    for tensor in numpy_array:
        tmp.append(tensor)
    return torch.stack(tmp, dim=dim)

def dict_to_tensor(indices, dict, trained_model, content_type, clip_version='clip-ViT-L-14'):
    collection = []
    for id in tqdm(indices):
        try:
            emb = torch.from_numpy(dict['data'][id][clip_version])
            emb = torch.unsqueeze(emb, 0)
            if trained_model:
                if content_type == 'text':
                    emb = trained_model.text_projection_head(emb)
                elif content_type == 'image':
                    emb = trained_model.image_projection_head(emb)
                else:
                    raise NotImplementedError
            collection.append(emb)
        except Exception as e:
            print('Problem with ', id)
            print('Exception: ', e)
            break
    # final_tensor = numpy_to_tensor(collection, dim=dim)
    final_tensor = torch.stack(collection, dim=1)
    final_tensor = torch.squeeze(final_tensor, dim=0)
    print('Final tensor shape:', final_tensor.shape)
    return final_tensor

def dict_to_list(indices, dict, trained_model, content_type, clip_version='clip-ViT-L-14'):
    list = []
    for id in tqdm(indices):
        try:
            emb = torch.from_numpy(dict['data'][id][clip_version])
            emb = torch.unsqueeze(emb, 0)
            if trained_model:
                if content_type == 'text':
                    emb = trained_model.text_projection_head(emb)
                elif content_type == 'image':
                    emb = trained_model.image_projection_head(emb)
                else:
                    raise NotImplementedError
            list.append(emb)
        except Exception as e:
            print('Problem with ', id)
            print('Exception: ', e)
    assert len(indices) == len(list)
    print(f'Got a list of {len(list)} embeddings')
    return list

def get_top_k_indices(query_emb, index, target_embeddings, k=5, normalized=True):
    """
    Given a query embedding, an index, a tensor of target embeddings, and a k
    Retrieve indices of top-k items in target embeddings
    """
    if normalized:
        # normalize query and image embeddings
        query_embedding_n = F.normalize(query_emb, p=2, dim=-1)
        target_embeddings_n = F.normalize(target_embeddings, p=2, dim=-1)
    else:
        query_embedding_n = query_emb
        target_embeddings_n = target_embeddings

    # find top-k image embeddings
    dot_similarity = query_embedding_n @ target_embeddings_n.T
    values, indices = torch.topk(dot_similarity.squeeze(0), k)

    # map matches to indices
    matches = [index[idx] for idx in indices]

    return matches, values

def get_top_k_matches_for_queries(indices, queries_embs, target_tensor, k, normalized):
    retrieved_results = []
    for id, query_emb in tqdm(zip(indices, queries_embs)):
        matches, values = get_top_k_indices(
            query_emb=query_emb,
            index=indices,
            k=k,
            normalized=normalized,
            target_embeddings=target_tensor
        )
        retrieved_results.append(matches)
    assert len(indices) == len(queries_embs) == len(target_tensor)
    return retrieved_results

def recall_at_k(target, predictions, k=5, total_in_collection=1):
    if len(predictions) < k:
        print(f'Watch out, len(predictions) < k: {len(predictions)} < {k}')
    recall_at_k = predictions[:k].count(target) / total_in_collection
    return 100 * recall_at_k

def get_recalls(target, predictions, k_list=[1,5,10,50,100]):
    recalls = []
    for k in k_list:
        tmp_recall = recall_at_k(
            target=target,
            predictions=predictions,
            k=k,
            total_in_collection=1
            )
        recalls.append(tmp_recall)
    return recalls

def iterate_over_results_to_compute_recalls(ids, results, k_list=[1, 5, 10, 50, 100]):
    query_ids = []
    top_k_results = []
    recalls_at_1 = []
    recalls_at_5 = []
    recalls_at_10 = []
    recalls_at_50 = []
    recalls_at_100 = []
    for id, result in tqdm(zip(ids, results)):
        r_at_1, r_at_5, r_at_10, r_at_50, r_at_100 = get_recalls(
            id,
            result,
            k_list=k_list
            )
        query_ids.append(id)
        top_k_results.append(result)
        recalls_at_1.append(r_at_1)
        recalls_at_5.append(r_at_5)
        recalls_at_10.append(r_at_10)
        recalls_at_50.append(r_at_50)
        recalls_at_100.append(r_at_100)
    return query_ids, top_k_results, recalls_at_1, recalls_at_5, recalls_at_10, recalls_at_50, recalls_at_100

def get_mrr(target, predictions):
    if target in predictions:
        index = predictions.index(target) + 1
        mrr = 1 / index
        return mrr
    else:
        return 0.0


from collections import deque
def solution(s):
    if s[0]==")": return False

    s = deque(list(s))
    temp = 0
    for i in range(len(s)):
        s_left = s.popleft()
        if temp < 0: return False
        if s_left=="(": temp += 1
        elif s_left==")": temp -= 1

    if temp != 0: return False
    else: return True

# 개선 코드
def is_pair(s):
    pair = 0
    for x in s:
        if pair < 0: break
        pair = pair + 1 if x == "(" else pair - 1 if x == ")" else pair
        # x=="("면 pair + 1 그 반대면 pair -1
        # 중간에 pair가 음수가 되면 잘못된 괄호 이므로 반복문 종료 이때 반복문 종료시 pair != 0 이므로 return 값은 자연스럽게 False가 됨.
    return pair == 0
        # pair가 0인 경우 = 괄호가 정상인 경우 = True / 그 외에는 전부 False로 출력

프로그래머스/lv2/12909. 올바른 괄호/올바른 괄호.py


"""Add product table

Revision ID: 434a7905b168
Revises: ceff866606d9
Create Date: 2020-05-24 20:19:01.064978

"""
import salsa
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '434a7905b168'
down_revision = 'ceff866606d9'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('product',
    sa.Column('id', salsa.db.types.GUID(), nullable=False),
    sa.Column('active', sa.Boolean(), nullable=True),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('description', sa.Text(), nullable=False),
    sa.Column('image_urls', sa.ARRAY(sa.String(length=255)), nullable=False),
    sa.Column('user_id', salsa.db.types.GUID(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['user_account.id'], name=op.f('fk_product_user_id_user_account')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_product'))
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('product')
    # ### end Alembic commands ###


import base64
import os

"""
Both path & path_2 will need to be changed based on users local environment.

The deeply nested set of for loops below is sequentially finding each and only each .jpg file
of the student's written stories, converting the .jpg file to base64, and storing both the base64
and the name of the file as a .json object. That .json object is then being stored in path_2

"""

path = "C:/Users/micha/Desktop/Lambda/DS-Unit-5_Labs/Story_Squad_Dataset/Transcribed_Stories/"
path_2 = "C:/Users/micha/Desktop/Lambda/DS-Unit-5_Labs/story-squad-ds/testing/json_files"

for root, dirs, files in os.walk(path):
    for dir in dirs:
        input_path = os.path.join(path, dir)
        for root, dirs, files in os.walk(input_path):
            for dir_2 in dirs:
                input_path_2 = os.path.join(input_path, dir_2)
                for root, dirs, files in os.walk(input_path_2):
                    for name in files:
                        if name.endswith((".jpg")):
                            input_path_3 = os.path.join(input_path_2, name)
                            with open(input_path_3, "rb") as img_file:
                                my_string = base64.b64encode(img_file.read())
                                my_string = my_string.decode()

                                file_name = os.path.splitext(name)[0].replace(" ", "_")
                                json = f"{{\"images\": [\"data:image/jpg;base64,{my_string}\"], \"file_name\": \"{file_name}\"}}"

                                file_name = os.path.join(path_2, file_name+".json")
                                json_file = open(f"{file_name}", "w")
                                json_file.write(json)
                                json_file.close()


import logging
import isceobj

from iscesys.ImageUtil.ImageUtil import ImageUtil as IU
from mroipac.filter.Filter import Filter
from mroipac.icu.Icu import Icu
import os

logger = logging.getLogger('isce.insar.runFilter')

def runFilter(self, filterStrength, igramSpectrum = "full"):
    logger.info("Applying power-spectral filter")

    if igramSpectrum == "full":
        logger.info("Filtering the full-band interferogram")
        ifgDirname = self.insar.ifgDirname

    elif igramSpectrum == "low":
        if not self.doDispersive:
            print('Estimating dispersive phase not requested ... skipping sub-band interferograms')
            return
        logger.info("Filtering the low-band interferogram")
        ifgDirname = os.path.join(self.insar.ifgDirname, self.insar.lowBandSlcDirname)

    elif igramSpectrum == "high":
        if not self.doDispersive:
            print('Estimating dispersive phase not requested ... skipping sub-band interferograms')
            return
        logger.info("Filtering the high-band interferogram")
        ifgDirname = os.path.join(self.insar.ifgDirname, self.insar.highBandSlcDirname)

    topoflatIntFilename = os.path.join(ifgDirname , self.insar.ifgFilename)

    img1 = isceobj.createImage()
    img1.load(topoflatIntFilename + '.xml')
    widthInt = img1.getWidth()

    intImage = isceobj.createIntImage()
    intImage.setFilename(topoflatIntFilename)
    intImage.setWidth(widthInt)
    intImage.setAccessMode('read')
    intImage.createImage()

    # Create the filtered interferogram
    filtIntFilename = os.path.join(ifgDirname , 'filt_' + self.insar.ifgFilename)
    filtImage = isceobj.createIntImage()
    filtImage.setFilename(filtIntFilename)
    filtImage.setWidth(widthInt)
    filtImage.setAccessMode('write')
    filtImage.createImage()

    objFilter = Filter()
    objFilter.wireInputPort(name='interferogram',object=intImage)
    objFilter.wireOutputPort(name='filtered interferogram',object=filtImage)
    if filterStrength is not None:
        self.insar.filterStrength = filterStrength

    objFilter.goldsteinWerner(alpha=self.insar.filterStrength)

    intImage.finalizeImage()
    filtImage.finalizeImage()
    del filtImage

    #Create phase sigma correlation file here
    filtImage = isceobj.createIntImage()
    filtImage.setFilename(filtIntFilename)
    filtImage.setWidth(widthInt)
    filtImage.setAccessMode('read')
    filtImage.createImage()

    phsigImage = isceobj.createImage()
    phsigImage.dataType='FLOAT'
    phsigImage.bands = 1
    phsigImage.setWidth(widthInt)
    phsigImage.setFilename(os.path.join(ifgDirname , self.insar.coherenceFilename))
    phsigImage.setAccessMode('write')
    phsigImage.setImageType('cor')#the type in this case is not for mdx.py displaying but for geocoding method
    phsigImage.createImage()

    resampAmpImage = os.path.join(ifgDirname , self.insar.ifgFilename)

    if '.flat' in resampAmpImage:
        resampAmpImage = resampAmpImage.replace('.flat', '.amp')
    elif '.int' in resampAmpImage:
        resampAmpImage = resampAmpImage.replace('.int', '.amp')
    else:
        resampAmpImage += '.amp'

    ampImage = isceobj.createAmpImage()
    ampImage.setWidth(widthInt)
    ampImage.setFilename(resampAmpImage)
    #IU.copyAttributes(self.insar.resampAmpImage, ampImage)
    #IU.copyAttributes(resampAmpImage, ampImage)
    ampImage.setAccessMode('read')
    ampImage.createImage()

    icuObj = Icu(name='stripmapapp_filter_icu')
    icuObj.configure()
    icuObj.unwrappingFlag = False

    icuObj.icu(intImage = filtImage, ampImage=ampImage, phsigImage=phsigImage)

    filtImage.finalizeImage()
    phsigImage.finalizeImage()
    phsigImage.renderHdr()
    ampImage.finalizeImage()

    # Set the filtered image to be the one geocoded
    # self.insar.topophaseFlatFilename = filtIntFilename


import typing as ty
import reapy_boost as rpr

def get_bounds(midi: ty.Iterable[rpr.MIDIEventDict]) -> ty.Tuple[float, float]:
    start, end = float('inf'), 0
    for event in midi:
        if not event['selected']:
            continue
        if start > event['ppq']:
            start = event['ppq']
        if end < event['ppq']:
            end = event['ppq']
    return start, end

def spread_times(notes: ty.List[rpr.Note], start: float,
                 end: float) -> ty.List[int]:
    step = (end - start) / (len(notes))
    times = []
    for i in range(len(notes) + 1):
        times.append(int(start + i * step))
    return times

@rpr.inside_reaper()
def spread() -> None:
    take = rpr.Project().selected_items[0].active_take
    midi = take.get_midi()
    # filtered = ty.cast(
    #     ty.Tuple[rpr.MIDIEventDict],
    #     tuple(
    #         filter(
    #             lambda event: event['selected'] and event['buf'][0] == 144,
    #             midi
    #         )
    #     )
    # )
    start_ppq, end_ppq = get_bounds(midi)
    notes = list(filter(lambda note: note.selected, take.notes))
    times = spread_times(notes, start_ppq, end_ppq)
    times_on_i, times_off_i = 0, 1
    for note in notes:
        note.start = take.ppq_to_time(times[times_on_i])
        note.end = take.ppq_to_time(times[times_off_i])
        times_on_i += 1
        times_off_i += 1

spread()