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

import matplotlib as mtl
import matplotlib.pyplot as plt
import openpyxl as px
import csv
import platform
pf = platform.system()

fname = '/Users/yokooannosuke/Cording/Pine64-python--pulse/Pulsesimulator/pulse_simdata.xlsm'

wb = px.load_workbook(fname, keep_vba=True, data_only=True)
ws = wb.get_sheet_by_name('Pulseパラメータ')

#################################################
PLsc1 = []
PLec1 = []
i = 1
while ws.cell(row=i, column=1).value != None:
    PLsc1.append(ws.cell(row=i, column=1).value)
    PLec1.append(ws.cell(row=i, column=3).value)
    i += 1
del PLsc1[0:3]
del PLec1[0:3]
# print(PLsc1)
# print(PLec1)

x_pri = PLsc1 + PLec1
x_list = sorted(x_pri)
x_value1 = sorted(x_list + x_list)

y_list = [0, 1, 1, 0]
y_value1 = []
for i in range(len(PLsc1)):
    y_value1 += y_list

if x_value1[0] != 0:
    x_value1.insert(0, 0)
    y_value1.insert(0, 0)
else:
    pass
# print(x_value1)
# print(y_value1)

##################################################

ADsc1 = []
ADec1 = []
i = 1
while ws.cell(row=i, column=13).value != None:
    ADsc1.append(ws.cell(row=i, column=13).value)
    ADec1.append(ws.cell(row=i, column=15).value)
    i += 1
del ADsc1[0:3]
del ADec1[0:3]
# print(ADsc1)
# print(ADec1)

x_pri = ADsc1 + ADec1
x_list = sorted(x_pri)
x_value4 = sorted(x_list + x_list)

y_value4 = []
for i in range(len(ADsc1)):
    y_value4 += y_list

if x_value4[0] != 0:
    x_value4.insert(0, 0)
    y_value4.insert(0, 0)
else:
    pass
# print(x_value4)
# print(y_value4)

####################################################

dds_csv = open(
    '/Users/yokooannosuke/Cording/Pine64-python--pulse/Pulsesimulator/pulse_simDDSdata.csv')

DDS_sc1 = []
DDSsc1_int = []
DDS_data = []
DDS_list = []

for row in csv.reader(dds_csv):
    DDS_sc1.append(row[0])
    DDS_data.append(row[4])
del DDS_sc1[0:3]
del DDS_data[0:3]

# 読み込んだデータをint型に変換
for i in range(len(DDS_sc1)):
    DDSsc1_int.append(int(DDS_sc1[i]))

    # 40bitdataを一文字ずつにして配列に格納
    chars = list(DDS_data[i].strip())
    DDS_list.append(chars)
# 読み込んだ配列をint型に変換
for j in range(len(DDS_list)):
    for i in range(len(DDS_list[0])):
        if str == type(DDS_list[j][i]):
            DDS_list[j][i] = (int(DDS_list[j][i]))
print(DDS_list)
print(DDSsc1_int)

####################################################

x1, y1 = x_value1, y_value1
x4, y4 = x_value4, y_value4
fig = plt.figure()

ax1 = fig.add_subplot(211, title='PL1', yticks=[0, 1], yticklabels=["0", "1"])
ax4 = fig.add_subplot(212, title='AD1',  xlabel='counter', yticks=[
                      0, 1], yticklabels=["0", "1"], sharex=ax1)

ax1.plot(x1, y1)
ax4.plot(x4, y4)

fig.savefig('simulation', facecolor=fig.get_facecolor())


import os
import time
import typing
import uuid
from collections import Counter
from datetime import datetime, timedelta

import dateutil.parser
import pytest
from django.utils import timezone

import gamechannels.gameservice as gameservice
import gamechannels.score as score_service
from gamechannels.game import GameState, GameStatus
import gamechannels.gamealias as gamealias
from gamechannels.gameexceptions import *

game_service = gameservice.RedisGameService()

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eggwords.settings.local_settings")

TEST_WORDS = ['spiders', 'diss', 'redips', 'eds', 'per', 'pes', 'speirs', 'sped', 'pride', 'psi', 'pied', 'rise',
              'reps', 'spires', 'prides', 'press', 'spired', 'pier', 'pies', 'resid', 'reis', 'die', 'ers', 'pries', 'pried',
              'dip', 'side', 'dis', 'sipe', 'dries', 'ser', 'peds', 'prises', 'spider', 'sips', 'sei', 'ides', 'ess', 'res',
              'rep', 'spire', 'siped', 'redip', 'rei', 'psis', 'ped', 'sipes', 'ire', 'red', 'piss', 'pisser', 'spier',
              'sris', 'dire', 'spied', 'rises', 'pissed', 'rids', 'sri', 'ids', 'reds', 'sire', 'sirs', 'resids',
              'peri', 'speir', 'dips', 'drips', 'spiers', 'seis', 'sers', 'riped', 'ride', 'ripes', 'peris', 'piers',
              'ripe', 'spies', 'pie', 'rip', 'rips', 'rid', 'pis', 'dress', 'sis', 'sir', 'sip', 'sired', 'priss',
              'prise', 'prised', 'rides', 'sires', 'dies', 'ires', 'ired', 'sides', 'drip']

TEST_WORD_COUNTER = Counter((len(w) for w in TEST_WORDS))
TEST_WORD_COUNT = [0] * (max((len(w) for w in TEST_WORDS)) + 1)
for (w_len, w_count) in TEST_WORD_COUNTER.items():
    TEST_WORD_COUNT[w_len] = w_count

@pytest.fixture(autouse=True, scope='function')
def game_id():
    game_id = uuid.uuid4()
    yield game_id
    # import pdb; pdb.set_trace()
    game_service.clean_up_game(game_id)

def test_simple_game(game_id):
    print("Test game_id = " + str(game_id))

    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    p2_id = uuid.uuid4()
    p2_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)

    game_state = game_service.get_game_state(game_id)
    assert GameStatus.WAITING == game_service.get_game_status(game_id)
    assert 1 == len(game_state.player_ids)
    assert p1_id in game_state.player_ids
    assert game_state.created_time is not None
    assert dateutil.parser.parse(game_state.created_time) < datetime.utcnow()

    game_service.add_player(game_id, p2_id, p2_token)

    game_state = game_service.get_game_state(game_id)
    assert GameStatus.WAITING == game_service.get_game_status(game_id)
    assert 2 == len(game_state.player_ids)
    assert p1_id in game_state.player_ids
    assert p2_id in game_state.player_ids

    result = game_service.use_word(game_id, p1_id, 'spiders')
    assert not result

    did_start = game_service.start_game(game_id, TEST_WORDS, countdown=1)
    assert did_start

    words = game_service.get_game_words(game_id)
    assert words is not None
    for test_word in TEST_WORDS:
        assert test_word in words

    game_state = game_service.get_game_state(game_id)
    assert GameStatus.SCHEDULED == game_service.get_game_status(game_id)
    assert len('spiders') == len(game_state.letters)
    for l in 'spiders':
        assert l in game_state.letters

    time.sleep(1)
    assert GameStatus.PLAYING == game_service.get_game_status(game_id)

    result = game_service.use_word(game_id, p1_id, 'spiders')
    assert result == True

    result2 = game_service.use_word(game_id, p2_id, 'spiders')
    assert result2 == False

    game_state = game_service.get_game_state(game_id)

    p1_words = game_state.words_for(p1_id)
    assert len(p1_words) == 1
    assert 'spiders' in p1_words

    p2_words = game_state.words_for(p2_id)
    assert len(p2_words) == 0

    game_service.end_game(game_id)
    game_state = game_service.get_game_state(game_id)
    assert GameStatus.COMPLETED == game_service.get_game_status(game_id)

def test_score() -> None:
    p1_id = uuid.uuid4()
    p2_id = uuid.uuid4()

    game_state0 = GameState(
        uuid.uuid4(),
        {p1_id: {}, p2_id: {}},
        {},
        (timezone.now() - timedelta(seconds=45)).isoformat(),
        (timezone.now() + timedelta(seconds=45)).isoformat(),
        (timezone.now() - timedelta(seconds=70)).isoformat(),
        'spiders',
        TEST_WORD_COUNT,
        (timezone.now() + timedelta(minutes=10)).isoformat()
    )

    score0 = score_service.score_game(game_state0)
    assert score0[p1_id] == score0[p2_id] == 0

    game_state1 = GameState(
        uuid.uuid4(),
        {p1_id: {}, p2_id: {}},
        {  'spiders': p1_id,
           'diss': p1_id,
           'redips': p1_id,
           'eds': p1_id,
           'per': p1_id,
           'pes': p1_id,
           'speirs': p1_id,
           'sped': p1_id,
           'pride': p1_id,
           'psi': p2_id,
           'pied': p2_id,
           'rise': p2_id,
           'reps': p2_id,
           'spires': p2_id,
           'prides': p2_id,
           'press': p2_id
        },
        (timezone.now() - timedelta(seconds=45)).isoformat(),
        (timezone.now() + timedelta(seconds=45)).isoformat(),
        (timezone.now() - timedelta(seconds=70)).isoformat(),
        'spiders',
        TEST_WORD_COUNT,
        (timezone.now() + timedelta(minutes=10)).isoformat()
    )

    score1 = score_service.score_game(game_state1)
    assert score1[p1_id] > score1[p2_id]

def test_expire(game_id):
    game_service.set_expire(game_id, 180)

def test_get_missing_game():
    game_state = game_service.get_game_state(uuid.uuid4())
    assert not game_state.exists()

def test_reinit_game(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    p2_id = uuid.uuid4()
    p2_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.add_player(game_id, p2_id, p2_token)
    game_service.start_game(game_id, TEST_WORDS)
    game_service.end_game(game_id)
    assert GameStatus.COMPLETED == game_service.get_game_status(game_id)

    game_service.reinit_game(game_id)
    gstate = game_service.get_game_state(game_id)
    assert GameStatus.WAITING == gstate.status()
    assert gstate.start_time is None
    assert gstate.end_time is None
    assert gstate.letters is None
    assert 0 == len(gstate.word_count)
    assert all(0 == v for k, v in gstate.score().items())
    assert 0 == len(gstate.used_words)

def test_player_info(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.update_player_name(game_id, p1_id, 'TestName', p1_token)
    gstate = game_service.get_game_state(game_id)
    assert 'TestName' == gstate.player_info[p1_id]['name']
    assert 'color' in gstate.player_info[p1_id]

def test_full_game(game_id):
    game_service.init_game(game_id, uuid.uuid4(), uuid.uuid4())
    for _ in range(gameservice.MAX_PLAYERS - 1):
        game_service.add_player(game_id, uuid.uuid4(), uuid.uuid4())
    with pytest.raises(GameFull):
        game_service.add_player(game_id, uuid.uuid4(), uuid.uuid4())
        game_service.add_player(game_id, uuid.uuid4(), uuid.uuid4())

def test_player_colors(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.update_player_name(game_id, p1_id, 'TestName', p1_token)

    p2_id, p2_token = uuid.uuid4(), uuid.uuid4()
    p3_id, p3_token = uuid.uuid4(), uuid.uuid4()
    p4_id, p4_token = uuid.uuid4(), uuid.uuid4()
    p5_id, p5_token = uuid.uuid4(), uuid.uuid4()

    game_service.add_player(game_id, p2_id, p2_token)
    game_service.add_player(game_id, p3_id, p3_token)
    game_service.add_player(game_id, p4_id, p4_token)
    game_service.add_player(game_id, p5_id, p5_token)

    gstate = game_service.get_game_state(game_id)

    # Assert all colors are unique
    used_colors = [pi['color'] for pi in gstate.player_info.values()]
    assert len(set(used_colors)) == len(used_colors)

    # Remove / Add player, then test again
    game_service.remove_player(game_id, p3_id)
    new_id, new_token = uuid.uuid4(), uuid.uuid4()
    game_service.add_player(game_id, new_id, new_token)

    gstate = game_service.get_game_state(game_id)
    used_colors = [pi['color'] for pi in gstate.player_info.values()]
    assert len(set(used_colors)) == len(used_colors)

def test_player_info_unauthorized(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    with pytest.raises(UnauthorizedAction):
        game_service.update_player_name(game_id, p1_id, 'TestName', None)
    with pytest.raises(UnauthorizedAction):
        game_service.update_player_name(game_id, p1_id, 'TestName', uuid.uuid4())

def test_game_alias(game_id) -> None:
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    aliased = gamealias.create_game_alias(game_id)
    dealiased = gamealias.dealias_game(aliased)

    assert str(dealiased) == str(game_id)

def test_game_alias_missing(game_id) -> None:
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    with pytest.raises(GameAliasNotFound):
        gamealias.dealias_game('abcdefg')

def test_start_missing_game(game_id) -> None:
    with pytest.raises(GameDoesNotExist):
        game_service.start_game(game_id, TEST_WORDS)

def test_double_start_game(game_id) -> None:
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.start_game(game_id, TEST_WORDS)

    with pytest.raises(GameStatusException):
        game_service.start_game(game_id, TEST_WORDS)


import time

class Node:
    def __init__(self, telephone, abonent):
        self.telephone = int(telephone)  # 1
        self.abonent = abonent           # 0

class HashTable:
    nodes = dict()

    def add(self, line):
        if self.nodes.get(self.hash_func(line[1])) is None:
            self.nodes[self.hash_func(line[1])] = [line]
        else:
            self.nodes[self.hash_func(line[1])].append(line)

    def hash_func(self, val):
        val = int(val)
        return val % 100

    def find_del(self, val, delete = False):
        if self.nodes.get(self.hash_func(val)) is not None:
            for string in self.nodes.get(self.hash_func(val)):
                if string[1] == val:
                    if delete:
                        self.nodes.get(self.hash_func(val)).remove(string)
                    else:
                        return string

    def print(self):
        for node in self.nodes:
            print(node)
            for str in self.nodes[node]:
                print('\t' + (str[1]) + ' - ' + (str[0]))

file = open('Table 22.txt')
file_lines = file.readlines()
lines = []
''''''
for i in range(1, len(file_lines)):
    file_lines[i] = file_lines[i].replace('\n', '').split('\t')
    lines.append(file_lines[i])

hash_table = HashTable()
telephones = []

for line in lines:
    hash_table.add(line)
    telephones.append(line[1])

start_time = time.time()
#for i in range(8000):
#    id = random.randrange(0, len(telephones))
#    hash_table.find_delete(telephones[id], True)
end_time = time.time() - start_time

hash_table.print()

print('seconds: ' + str(end_time))

input()
hash_table.find_del('7192830', True)
hash_table.print()
print()


import multiprocessing
import exifread
import rawpy
import os
import shutil
import PIL
from PIL import Image
from datetime import datetime
from timeit import default_timer
from time import sleep
from SUASSystem import GCSSettings
from SUASSystem.suas_logging import log
from ImgProcessingCLI.Runtime.RuntimeTarget import RuntimeTarget
from ImgProcessingCLI.DataMine.OrientationSolver import OrientationSolver
from ImgProcessingCLI.DataMine import KMeansCompare
from ImgProcessingCLI.Runtime import TargetCropper3
from EigenFit.DataMine import Categorizer
from ImgProcessingCLI.Runtime.GeoStamp import GeoStamp
from ImgProcessingCLI.Runtime.GeoStamps import GeoStamps
from EigenFit.Load import *
from ImgProcessingCLI.Runtime.TargetCrop import TargetCrop

def run_img_proc_process(logger_queue, img_proc_status, location_log, targets_to_submit):
    SUASSystem.logging.logger_worker_configurer(logger_queue)
    logger_name = multiprocessing.current_process().name

    log(logger_name, "Instantiating letter categorizer")
    eigenvectors = load_numpy_arr(GCSSettings.BASE_LETTER_CATEGORIZER_PCA_PATH + "/Data/Eigenvectors/eigenvectors 0.npy")
    projections_path = GCSSettings.BASE_LETTER_CATEGORIZER_PCA_PATH + "/Data/Projections"
    mean = load_numpy_arr(GCSSettings.BASE_LETTER_CATEGORIZER_PCA_PATH + "/Data/Mean/mean_img 0.npy")
    num_dim = 20
    letter_categorizer = Categorizer(eigenvectors, mean, projections_path, KMeansCompare, num_dim)
    log(logger_name, "Letter categorizer instantiated successfully")

    log(logger_name, "Instantiating orientation solver")
    orientation_eigenvectors = load_numpy_arr(GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH + "/Data/Eigenvectors/eigenvectors 0.npy")
    orientation_projections_path = GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH + "/Data/Projections"
    orientation_mean = load_numpy_arr(GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH + "/Data/Mean/mean_img 0.npy")
    orientation_num_dim = 50
    orientation_solver = OrientationSolver(orientation_eigenvectors, orientation_mean, GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH, orientation_num_dim)
    log(logger_name, "Orientation solver instantiated")

    if os.path.exists(GCSSettings.GENERATED_DATA_LOCATION):
        shutil.rmtree(GCSSettings.GENERATED_DATA_LOCATION)

    os.mkdir(GCSSettings.GENERATED_DATA_LOCATION)
    os.mkdir(os.path.join(GCSSettings.GENERATED_DATA_LOCATION, "object_file_format"))

    sd_path = os.path.join("/Volumes", GCSSettings.SD_CARD_NAME, "DCIM")
    gps_coords = []

    while True:
        if "enabled" in str(img_proc_status.value).lower():
                if len(location_log) != 0:
                    try:
                        gps_coords.append(location_log[0])
                        gps_coords = gps_coords[1:]
                    except:
                        pass

                log(logger_name, "Waiting for path to exist...")

                # Wait for SD card to be loaded
                if not os.path.exists(sd_path):
                    sleep(1)

                    continue

                # Once SD card is loaded, begin processing
                # For every image on the SD card
                # 1. Load the image
                # 2. Process the crops, determine the ones that are targets
                # 3. Save the targets to GENERATED_DATA_LOCATION
                # 4. Upload the targets to interop server
                log(logger_name, "Beginning image processing...")
                geo_stamps = GeoStamps([GeoStamp([38.38875, -77.27532], datetime.now())])#gps_coords)
                crop_index = 0

                all_target_crops = []
                for pic_folder in os.listdir(sd_path):
                    pictures_dir_path = os.path.join(sd_path, pic_folder)

                    for pic_name in os.listdir(pictures_dir_path):
                        if ".SRW" in pic_name:
                            start_time = default_timer()
                            log(logger_name, "Loading image " + pic_name)
                            pic_path = os.path.join(pictures_dir_path, pic_name)
                            img = rawpy.imread(pic_path).postprocess()
                            rgb_image = Image.fromarray(numpy.roll(img, 1, axis=0))
                            image_timestamp = get_image_timestamp(pic_path)

                            target_crops = TargetCropper3.get_target_crops_from_img(rgb_image, image_timestamp, geo_stamps, 6)
                            target_crops = TargetCrop.get_non_duplicate_crops(all_target_crops, target_crops, GCSSettings.MIN_DIST_BETWEEN_TARGETS_KM)
                            all_target_crops.extend(target_crops)
                            log(logger_name, "Finished processing " + pic_name + " in " + str(default_timer() - start_time) + " seconds")

                            for target_crop in target_crops:
                                try:
                                    log(logger_name, "Identifying target characteristics of target #" + str(crop_index))
                                    runtime_target = RuntimeTarget(target_crop, letter_categorizer, orientation_solver)
                                    target_json_output = runtime_target.get_competition_json_output()

                                    log(logger_name, "Saving target characteristics of target #" + str(crop_index))
                                    output_pic_name = os.path.join(GENERATED_DATA_LOCATION, "object_file_format", str(crop_index) + ".png")
                                    output_json_name = os.path.join(GENERATED_DATA_LOCATION, "object_file_format", str(crop_index) + ".txt")
                                    save_json_data(output_json_name, {"target_json_output" : "Testing"})#target_json_output)
                                    target_crop.get_crop_img().save(output_pic_name)

                                    crop_index += 1
                                except:
                                    log(logger_name, "ERROR: Could not process a target crop")

        sleep(0.5)

def get_image_timestamp(filename):
    """
    Returns an image's timestamp
    """
    opened_file = open(filename, 'rb')
    tags = exifread.process_file(opened_file)
    image_raw_time = tags['Image DateTime']
    converted_time = datetime.strptime(str(image_raw_time), "%Y:%m:%d %H:%M:%S")

    return converted_time


#!/usr/bin/env python3

import psycopg2

DBNAME = "news"

def main():
    """Connect to the PostgreSQL database and return a connection object."""
    try:
        db = psycopg2.connect(database=DBNAME)
        c = db.cursor()
    except (UnboundLocalError):
        print("Unable to connect to database.")

    # 1. What are the most popular three articles of all time?
    sql_popular_articles = """
        SELECT articles.title, COUNT(*) AS num
        FROM articles
        JOIN log
        ON log.path LIKE concat('/article/%', articles.slug)
        GROUP BY articles.title
        ORDER BY num DESC
        LIMIT 3;
    """
    c.execute(sql_popular_articles)
    articles = c.fetchall()

    print("MOST POPULAR ARTICLES:\n")
    for article in articles:
        formatted_response = "{} - {} views".format(article[0], article[1])
        print(formatted_response)

    print("\n=================\n")

    # 2. Who are the most popular article authors of all time?
    sql_popular_authors = """
        SELECT authors.name, COUNT(articles.author) as views
        FROM articles, log, authors
        WHERE log.path = concat('/article/',articles.slug) and
        articles.author = authors.id
        GROUP by authors.name
        ORDER by views desc
    """
    c.execute(sql_popular_authors)
    authors = c.fetchall()

    print("MOST POPULAR AUTHORS:\n")
    for author in authors:
        clean_title = author[0]
        formatted_response = "{} - {} views".format(clean_title, author[1])
        print(formatted_response)

    print("\n=================\n")

    # 3. On which days did more than 1% of requests lead to errors?
    sql_request_errors = """
        SELECT Date, Total, Error, (Error::float*100)/Total::float
        as Percent
        FROM (select time::timestamp::date as Date, COUNT(status) as Total,
        SUM(case when status = '404 NOT FOUND' then 1 else 0 end) as Error
        FROM log
        GROUP by time::timestamp::date) as result
        WHERE (Error::float*100)/Total::float > 1.0 ORDER by Percent desc;
    """
    c.execute(sql_request_errors)
    error = c.fetchall()

    print("DAY WITH MOST ERRORS:\n")
    print("{} - {}%".format(error[0][0], round(error[0][3], 1)))

    db.close()

if __name__ == "__main__":
    main()


import numpy as np
import pandas as pd
import csv

import sys
xTstPath = sys.argv[1]
outputPath = sys.argv[2]

tst_x = pd.read_csv(xTstPath)
tst_x = tst_x['feature']
pixels = tst_x.values
X = np.zeros((pixels.shape[0], 48*48))
for ix in range(X.shape[0]):
    p = pixels[ix].split(' ')
    for iy in range(X.shape[1]):
        X[ix, iy] = int(p[iy])

# X -= np.mean(X, axis=0)
# X /= np.std(X, axis=0)
X /= 255

X = X.reshape((X.shape[0], 48, 48, 1))

# print(X[0:10].shape)
from keras.models import load_model
model = load_model('./b_model.179-0.6742.h5')
pred = model.predict(X)

# print(type(pred))
# print(len(pred))
# print(pred[0])
# print(pred)

ans = []
for i in range(len(pred)):
    ans.append([str(i)])
    a = np.argmax(pred[i])
    ans[i].append(a)

# print(ans)

text = open(outputPath, "w+")
s = csv.writer(text,delimiter=',',lineterminator='\n')
s.writerow(["id","label"])
for i in range(len(ans)):
    s.writerow(ans[i])
text.close()


#
# @lc app=leetcode id=934 lang=python3
#
# [934] Shortest Bridge
#
# https://leetcode.com/problems/shortest-bridge/description/
#
# algorithms
# Medium (44.19%)
# Likes:    305
# Dislikes: 23
# Total Accepted:    11.1K
# Total Submissions: 25.1K
# Testcase Example:  '[[0,1],[1,0]]'
#
# In a given 2D binary array A, there are two islands.  (An island is a
# 4-directionally connected group of 1s not connected to any other 1s.)
#
# Now, we may change 0s to 1s so as to connect the two islands together to form
# 1 island.
#
# Return the smallest number of 0s that must be flipped.  (It is guaranteed
# that the answer is at least 1.)
#
#
#
# Example 1:
#
#
# Input: [[0,1],[1,0]]
# Output: 1
#
#
#
# Example 2:
#
#
# Input: [[0,1,0],[0,0,0],[0,0,1]]
# Output: 2
#
#
#
# Example 3:
#
#
# Input: [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
# Output: 1
#
#
#
#
#
# Note:
#
#
# 1 <= A.length = A[0].length <= 100
# A[i][j] == 0 or A[i][j] == 1
#
#
#
#
#
#
#
#
class Solution:
    def shortestBridge(self, A: List[List[int]]) -> int:
        n=len(A)
        m=len(A[0])
        if n==0 or m==0:
            return 0
        found=False
        q=collections.deque()
        for i in range(n):
            for j in range(m):
                if A[i][j]==1 and found==False:
                    self.dfs(A, i, j, q)
                    found=True
                    #print(A)
                    #dfs遍历一个岛屿完毕
        #再bfs，找下一个岛屿
        step=0
        dirs=[0,1,0,-1,0]
        #print(q)
        while q:
            qlen=len(q)
            for _ in range(qlen):
                dx, dy=q.popleft()
                for i in range(len(dirs)-1):
                    x=dx+dirs[i]
                    y=dy+dirs[i+1]
                    if x>=0 and x<n and y>=0 and y<m:
                        if A[x][y]==0:
                            A[x][y]=2
                            q.append([x,y])
                        elif A[x][y]==1:
                            return step
            step+=1

        return -1

    def dfs(self, A, i, j, q):
        if i<0 or j<0 or i>=len(A) or j>=len(A[0]) or A[i][j]!=1:
            return
        A[i][j]=2
        q.append([i,j])
        self.dfs(A, i-1, j, q)
        self.dfs(A, i+1, j, q)
        self.dfs(A, i, j-1, q)
        self.dfs(A, i, j+1, q)


from string import Template
import os

clang_mandatory =  "-Wno-c99-extensions"
expand = {
  "O2": "-O2",
  "warn": "-Wall -Wextra"
}
types = ["c++", "gnu++"]
extras = ["O2", "warn"]
libcxx = {
    "manual": {
        "compile": "-stdlib=libc++ -isystem/usr/include/libcxxabi",
        "link": "-stdlib=libc++ -lc++abi"
    }
}

convert_dir = {"clang-4.0": "clang-4", "clang-5.0": "clang-5", "clang-6.0": "clang-6"}
compiler_cmd = {"clang":"clang++", "gcc":"g++"}

compilers = {
    "clang-2.9":{"distro": "trusty", "configs": ["98", "11"]},
    "clang-3.0":{"distro": "trusty", "configs": ["98", "11"]},
    "clang-3.1":{"distro": "trusty", "configs": ["98", "11"]},
    "clang-3.2":{"distro": "trusty", "configs": ["98", "11", "1y"]},
    "clang-3.3":{"distro": "trusty", "configs": ["98", "11", "1y"]},
    "clang-3.4":{"distro": "trusty", "configs": ["98", "11", "14", "1z"]},
    "clang-3.5":{"distro": "trusty", "configs": ["98", "11", "14", "1z"]},
    "clang-3.6":{"distro": "trusty", "configs": ["98", "11", "14", "1z"]},
    "clang-3.7":{"distro": "trusty", "configs": ["98", "11", "14", "1z"]},
    "clang-3.8":{"distro": "trusty", "configs": ["98", "11", "14", "1z"]},
    "clang-3.9":{"distro": "xenial", "configs": ["98", "11", "14", "1z"], "libcxx":"manual"},
    "clang-4.0":{"distro": "xenial", "configs": ["98", "11", "14", "1z"], "libcxx":"manual"},
    "clang-5.0":{"distro": "xenial", "configs": ["98", "11", "14", "1z"], "libcxx":"manual"},
    "clang-6.0":{"distro": "xenial", "configs": ["98", "11", "14", "17"], "libcxx":"manual"},
    "clang-7":{"distro": "bionic", "configs": ["98", "11", "14", "17", "2a"], "libcxx":"manual"},
    "clang-8":{"distro": "bionic", "configs": ["98", "11", "14", "17", "2a"], "libcxx":"manual"},
    "clang-9":{"distro": "bionic", "configs": ["98", "11", "14", "17", "2a"], "libcxx":"manual"},
    "gcc-4.4":{"distro": "precise", "configs": ["98", "0x"]},
    "gcc-4.5":{"distro": "precise", "configs": ["98", "0x"]},
    "gcc-4.6":{"distro": "precise", "configs": ["98", "0x"]},
    "gcc-4.7":{"distro": "precise", "configs": ["98", "11", "1y"]},
    "gcc-4.8":{"distro": "trusty", "configs": ["98", "11", "1y"]},
    "gcc-4.9":{"distro": "trusty", "configs": ["98", "11", "1y"]},
    "gcc-5":{"distro": "trusty", "configs": ["98", "11", "14", "1z"]},
    "gcc-6":{"distro": "trusty", "configs": ["98", "11", "14", "1z"]},
    "gcc-7":{"distro": "xenial", "configs": ["98", "11", "14", "1z"]},
    "gcc-8":{"distro": "xenial", "configs": ["98", "11", "14", "17", "2a"]}
}

def make_toolset(compiler, version, config, cfgtype, extra=None, libcxx_cfg=None):
    conf = cfgtype + config
    short_conf = conf
    if cfgtype == "gnu++":
        short_conf = "gnu" + config

    ver = version + "~" + short_conf
    if libcxx_cfg:
        ver += "~lc"
    if extra:
        ver += "~" + extra

    t = "using {compiler} : {ver} : {cmd}-{version} : ".format(
        compiler=compiler, ver=ver, version=version, cmd=compiler_cmd[compiler])
    t += '<cxxflags>"'
    if compiler == "clang":
        t += clang_mandatory + " "
    t += "-std=" + conf + " "
    if extra:
        t += expand[extra] + " "
    if libcxx_cfg:
        t += libcxx[libcxx_cfg]["compile"] + " "
    t = t[:-1]
    t += '" '
    if libcxx_cfg:
        t += '<linkflags>"'
        t += libcxx[libcxx_cfg]["link"]
        t += '" '
    t += ";\n"
    return t

def build_configs():
    for compiler, options in compilers.items():
        cdir = compiler
        if compiler in convert_dir:
            cdir = convert_dir[compiler]

        compiler_type, version = compiler.split("-")

        toolsets = "using {compiler} : {version} : {cmd}-{version} : ".format(
            compiler=compiler_type, version=version, cmd=compiler_cmd[compiler_type])
        if compiler_type == "clang":
            toolsets += '<cxxflags>"-Wno-c99-extensions" '
        toolsets += ";\n"

        for config in options["configs"]:
            for cfgtype in types:
                toolsets += make_toolset(compiler_type, version, config, cfgtype)
                for extra in extras:
                    toolsets += make_toolset(compiler_type, version, config, cfgtype, extra)

                if "libcxx" in options:
                    toolsets += make_toolset(compiler_type, version, config, cfgtype, libcxx_cfg=options["libcxx"])
                    for extra in extras:
                        toolsets += make_toolset(
                            compiler_type, version, config, cfgtype, extra, libcxx_cfg=options["libcxx"])

        template_file = "user-config.jam.{distro}.template".format(distro=options["distro"])
        with open(template_file, "r") as f:
            ts = f.read()

        t = Template(ts)
        cfg = t.safe_substitute(toolsets=toolsets)

        with open(os.path.join(cdir, "user-config.jam"), "w") as f:
            f.write(cfg)

if __name__ == "__main__":
    build_configs()


import unittest

import pandas as pd

from tests.test_utils import get_sample_pdf_with_labels, get_sample_pdf, get_sample_sdf, get_sample_pdf_with_extra_cols, \
    get_sample_pdf_with_no_text_col, get_sample_spark_dataframe
from nlu import *

class TestNer(unittest.TestCase):

    def test_zh_ner(self):
        pipe = nlu.load('zh.ner')
        data = '您的生活就是矩阵编程固有的不平衡方程的剩余部分之和。您是异常的最终结果，尽管做出了我最大的努力，但我仍然无法消除数学精度的和谐。尽管仍然不遗余力地避免了负担，但这并不意外，因此也不超出控制范围。这无可避免地将您引向了这里。'
        df = pipe.predict([data], output_level='document')
        for c in df.columns: print(df[c])

    def test_aspect_ner(self):
        pipe = nlu.load('en.ner.aspect_sentiment')
        data = 'We loved our Thai-style main which amazing with lots of flavours very impressive for vegetarian. But the service was below average and the chips were too terrible to finish.'
        df = pipe.predict([data], output_level='document')
        for c in df.columns: print(df[c])

    def test_ner_pipe_confidences(self):
        #
        df = nlu.load('en.ner.onto.glove.6B_100d', verbose=True).predict(
            'Donald Trump from America and Angela Merkal from Germany dont share many oppinions.', output_level='token',
            metadata=True)
        for c in df.columns: print(df[c])

if __name__ == '__main__':
    unittest.main()


# http://community.topcoder.com/stat?c=problem_statement&pm=1593&rd=4494

class Circuits(object):
    def __init__(self, connects, costs):
        self.ncomponents = len(connects)
        self.graph = self.build_graph(connects, costs)

    @classmethod
    def how_long(cls, connects, costs):
        circuit = cls(connects, costs)
        return circuit.run()

    def build_graph(self, connects, costs):
        graph = [x[:] for x in [[0] * self.ncomponents] * self.ncomponents]
        for i, comps in enumerate(connects):
            weight = costs[i].split()
            for j, c in enumerate(comps.split()):
                graph[i][int(c)] = int(weight[j])
        return graph

    def run(self):
        self.max_path = 0
        self.visited = [False] * self.ncomponents
        for i in xrange(self.ncomponents):
            if not self.visited[i]:
                self.dfs(i, 0)
        return self.max_path

    def dfs(self, vertex, path):
        self.visited[vertex] = True
        for i in xrange(self.ncomponents):
            if self.graph[vertex][i]:
                self.dfs(i, path + self.graph[vertex][i])
        self.max_path = max(self.max_path, path)


import sys

import pygame as pg
from pygame.locals import *

from display import window
from game import snake_sprite, apple_sprite
from constants import *

pg.init()

def run():
    screen= window.createWindow()
    clock = pg.time.Clock()

    snake = snake_sprite.Snake()
    apple = apple_sprite.Apple()

    pg.display.flip()

    while True:
        clock.tick(15)

        for event in pg.event.get():
            if event.type == pg.QUIT:
                sys.exit()

            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    sys.exit()

                elif event.key >= pg.K_UP and event.key <= pg.K_LEFT:
                    snake.change_dir(event.key)

                break

        snake_updates = snake.update()
        ate = snake_updates.pop(0)

        apple_rect = []
        if ate:
            apple_rect = apple.place_apple()

        pg.display.update(snake_updates + apple_rect)


import click
from acoustid.script import Script
from acoustid.uwsgi_utils import run_web_app, run_api_app
from acoustid.cron import run_cron
from acoustid.scripts.import_submissions import run_import

@click.group()
def cli():
    pass

@cli.group()
def run():
    pass

@run.command('web')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
@click.option('-w', '--workers', type=int, envvar='ACOUSTID_WEB_WORKERS')
def run_web_cmd(config, workers):
    """Run production uWSGI with the website."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_web_app(script.config, workers=workers)

@run.command('api')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
@click.option('-w', '--workers', type=int, envvar='ACOUSTID_API_WORKERS')
def run_api_cmd(config, workers):
    """Run production uWSGI with the API."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_api_app(script.config, workers=workers)

@run.command('cron')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def run_cron_cmd(config):
    """Run cron."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_cron(script)

@run.command('import')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def run_import_cmd(config):
    """Run import."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_import(script)

def main():
    cli()


import re
from typing import Optional

import yaml
from cfn_tools import load_json, load_yaml, ODict, CfnYamlLoader
from cfn_tools.yaml_dumper import TAG_MAP
from cloudrail.knowledge.context.cloud_provider import CloudProvider
from cloudrail.knowledge.context.iac_type import IacType
from cloudrail.knowledge.utils.file_utils import read_file
from cloudrail.knowledge.utils.string_utils import StringUtils

ELEMENT_POSITION_KEY: str = 'cfn_resource_block_position'
NODE_KEY_LINE_REGEX: str = '"{}"\\s*:'

# pylint: disable=R1710
class CustomCloudformationLoader(CfnYamlLoader):
    pass

def construct_mapping(self, node, deep=False):
    """
    Use ODict for maps
    """

    mapping = ODict()
    for key_node, value_node in node.value:
        key = self.construct_object(key_node, deep=deep)
        value = self.construct_object(value_node, deep=deep)
        mapping[key] = value

    if 'Type' in mapping and ELEMENT_POSITION_KEY not in mapping:
        mapping[ELEMENT_POSITION_KEY] = (node.start_mark.line, node.end_mark.line - 1)

    return mapping

CustomCloudformationLoader.add_constructor(TAG_MAP, construct_mapping)

class CloudformationUtils:

    EXTRA_PARAMETERS_KEY: str = 'cr_extra_params'

    @classmethod
    def validate_required_cfn_template_parameters(cls, cfn_template_file: str, cfn_template_parameters: dict):
        cfn_template_content: dict = cls.load_cfn_template(cfn_template_file)
        template_parameters: dict = cfn_template_content.get('Parameters', {})
        for param_name in template_parameters:
            if param_name not in cfn_template_parameters and 'Default' not in template_parameters[param_name]:
                raise Exception(f'Missing required template parameter "{param_name}"')

    @classmethod
    def create_cfn_template_extra_parameters(cls, cfn_stack_name: str, iac_type: IacType, cloud_provider: CloudProvider,
                                             cfn_stack_region: str, account_id: str, cfn_template_file_name: str,
                                             account_name: Optional[str] = None, cfn_parameters: dict = None) -> dict:
        extra_parameters = {'stack_name': cfn_stack_name,
                            'iac_type': iac_type,
                            'cloud_provider': cloud_provider,
                            'region': cfn_stack_region,
                            'account_id': account_id,
                            'cfn_template_file_name': cfn_template_file_name}

        if account_name:
            extra_parameters['account_name'] = account_name

        if cfn_parameters:
            extra_parameters.update(cfn_parameters)
        return extra_parameters

    @classmethod
    def load_cfn_template(cls, cfn_template_file: str) -> dict:
        if cfn_template_file.endswith('.json'):
            return cls.file_to_cfn_json(cfn_template_file)
        elif cfn_template_file.endswith('.yaml'):
            return cls.file_to_cfn_yaml(cfn_template_file)
        else:
            cls.raise_invalid_cfn_template_file()

    @classmethod
    def cfn_template_str_to_dict(cls, cfn_template_content: str) -> dict:
        if StringUtils.is_json(cfn_template_content):
            return load_json(cfn_template_content)
        elif StringUtils.is_yaml(cfn_template_content):
            return load_yaml(cfn_template_content)
        else:
            cls.raise_invalid_cfn_template_file()

    @classmethod
    def file_to_cfn_json(cls, file_path: str) -> dict:
        try:
            cfn_template_content: str = read_file(file_path)
            cfn_template_as_dict: dict = load_json(cfn_template_content)
            cls._set_cfn_resources_block_position(cfn_template_as_dict, file_path)
            return cfn_template_as_dict
        except Exception as ex:
            message = f'Error while reading CloudFormation template JSON file {file_path}. {ex}'
            raise Exception(message)

    @staticmethod
    def file_to_cfn_yaml(file_path: str) -> dict:
        try:
            return yaml.load(stream=read_file(file_path), Loader=CustomCloudformationLoader)
        except Exception as ex:
            message = f'error while reading CloudFormation template yaml file {file_path}. {ex}'
            raise Exception(message)

    @staticmethod
    def to_odict(ordinary_dict: dict) -> ODict:
        return ODict([[key, val] for key, val in ordinary_dict.items()])

    @staticmethod
    def raise_invalid_cfn_template_file():
        raise Exception('Invalid CloudFormation template file format')

    @classmethod
    def _set_cfn_resources_block_position(cls, cfn_template_as_dict: dict, file_path: str):
        json_doc: str = read_file(file_path)
        cfn_resources: dict = cfn_template_as_dict.get('Resources', {})
        for resource_name, cfn_resource in cfn_resources.items():
            if ELEMENT_POSITION_KEY not in cfn_resource:
                key_pattern: str = NODE_KEY_LINE_REGEX.format(resource_name)
                pattern_occurrences: list = re.split(key_pattern, json_doc)
                if len(pattern_occurrences) > 1:
                    start_line: int = pattern_occurrences[0].count('\n') + 1
                    end_line: int = start_line + cls._end_element_line_number(pattern_occurrences[-1])
                    cfn_resource[ELEMENT_POSITION_KEY] = (start_line, end_line)

    @staticmethod
    def _end_element_line_number(pattern_occurrence: str):
        open_braces_counter: int = 0
        already_opened: bool = False
        index: int = 0
        for character in pattern_occurrence:
            if character == '{':
                already_opened = True
                open_braces_counter += 1
            elif character == '}':
                open_braces_counter -= 1

            if already_opened and open_braces_counter == 0:
                break
            index += 1
        sub_str: str = pattern_occurrence[0: index]
        return sub_str.count('\n')


#!/usr/bin/env python
"""
Script to prepare nextstrain alpha files
"""
###TODO
# Assumptions : Second column sample_name is unique by itself and only the latest run for the sample is retained in submission manifest
#1) Get files in the submission manifest from seq_path
#2) Exclude submitted files as GISAID will already have it
#3) Add submitted files to local build only
#4) If metadata exists use metadata
#5) Else create fake metadata for the non existing samples

import pandas as pd
import numpy as np
import json , os
import argparse
import random , time
from datetime import datetime , date , timedelta
from pandas.io.json import json_normalize
import glob ,re , tempfile ,shutil
import random , time
from datetime import datetime , date , timedelta
from pyfaidx import Fasta
from datetime import date
import warnings
from pytz import timezone
from Bio import SeqIO

warnings.simplefilter(action='ignore', category=FutureWarning)
pd.options.mode.chained_assignment = None  # default='warn'

class PrepareAlphaNextstrain:
    """
    Prepare data for Nextstrain
    """

    def __init__(self,sub_manifest,SEQ_PATH,FASTA_PATH,MASTER_META,SUBMITTED_SEQ,OUTPUT_DIR):
        """
        Class to prepare alpha data consensus data for nextstrain

        :param sub_manifest: Submission Manifest file for a sequencing run
        :param SEQ_PATH: path to sequencing runs
        :param FASTA_PATH: path to fasta file within sequencing runs
        :param MASTER_META: path to master metadata excel sheet
        :param SUBMITTED_SEQ: path to fasta file of already submitted sequences
        :param OUTPUT_DIR: path to alpha input files
        """
        self.id_mapping_file = sub_manifest
        self.SEQ_PATH = SEQ_PATH
        self.FASTA_PATH = FASTA_PATH
        self.MASTER_META = MASTER_META
        self.SUBMITTED_SEQ = SUBMITTED_SEQ
        self.OUTPUT_DIR = OUTPUT_DIR
        self.CONSENSUS_FASTA = "*.nanopolish.consensus.fasta"
        self.DATE_FMT ="%Y%m%d"
        self.SUB_FMT ="%Y-%m-%d"

        self.REF_LENGTH = 29903
        self.count_Ns = 5000

        self.CUR_DATE = datetime.now(timezone('US/Eastern'))
        self.SUB_DATE = self.CUR_DATE.strftime(self.SUB_FMT)
        self.CUR_DATE = self.CUR_DATE.strftime(self.DATE_FMT)

        self.ALPHA_PREFIX= os.path.join(self.OUTPUT_DIR, self.CUR_DATE)
        self.STATE_PREFIX = "MD"
        self.header_pat = re.compile('([A-Z]+-[0-9]*_[A-Z]+[0-9]+)')

        ### Setting default metadata for all samples
        self.loc_default = "North America / USA / Maryland"
        self.host = "Human"
        self.specimen_source = "Nasopharyngeal swab"
        self.seq_tech = "Nanopore MinION"
        self.assembly_method = "ARTIC 1.0.0"
        self.dep = "Johns Hopkins Hospital Department of Pathology"
        self.address = "600 N. Wolfe St; Baltimore, MD 21287"
        self.authors = "Peter M. Thielen, Thomas Mehoke, Shirlee Wohl, Srividya Ramakrishnan, Melanie Kirsche, Amanda Ernlund, Oluwaseun Falade-Nwulia, Timothy Gilpatrick, Paul Morris, Norah Sadowski, Nídiá Trovao, Victoria Gniazdowski, Michael Schatz, Stuart C. Ray, Winston Timp, Heba Mostafa"
        self.submitter = "JHU"
        self.next_fields = [ "Virus name",
                             "Type",
                             "Passage details/history",
                             "Collection date",
                             "Location",
                             "Additional location information",
                             "Host",
                             "Additional host information",
                             "Gender",
                             "Patient age",
                             "Patient status",
                             "Specimen source",
                             "Outbreak",
                             "Last vaccinated",
                             "Treatment",
                             "Sequencing technology",
                             "Assembly method",
                             "Coverage",
                             "Originating lab",
                             "Address",
                             "Sample ID given by the sample provider",
                             "Submitting lab",
                             "Address.1",
                             "Sample ID given by the submitting laboratory",
                             "Authors",
                             "Submitter",
                             "Submission date",
                             "Run date",
                             "Run folder" ]

    def log(self,message):
        """ Log messages to standard output. """
        print(time.ctime() + ' --- ' + message, flush=True)

    def get_fasta_lengths(self,fasta):
        fa_lens = []
        N_counts = []
        for seq_record in SeqIO.parse(fasta, "fasta"):
               A_count = seq_record.seq.count('A')
               C_count = seq_record.seq.count('C')
               G_count = seq_record.seq.count('G')
               T_count = seq_record.seq.count('T')
               N_count = seq_record.seq.count('N')
               fa_lens.append(len(seq_record))
               N_counts.append(N_count)
        return fa_lens, N_counts

    def getKeysByValues(self,dictOfElements, listOfValues):
        listOfKeys = list()
        listOfItems = dictOfElements.items()
        for item  in listOfItems:
            if item[1] in listOfValues:
                listOfKeys.append((item[1], item[0]))
        return  listOfKeys

    def get_file_list(self,file_list,file_pattern):
        """
        get file list of consensus fasta from SEQ_PATH and FASTA_PATH

        """
        c_list = []
        for i in file_list:
            cfile = glob.glob(i + self.CONSENSUS_FASTA)[0]
            c_list.append(cfile)
        return c_list

    def get_fasta_header(self,seq):
        """
         get header from fasta file
        """
        with open(seq,'r') as fd:
           header = []
           for line in fd:
               if line[0]=='>':
                   header.append(line.strip().split(">")[1].split(" ")[0])
        return header

    def map_submission_names(self):
        """
        Extract submission names from submission_manifest.txt
        """
        sub_file = self.id_mapping_file
        self.sname_dict= {}
        self.sheader_dict = {}
        self.only_sname_dict = {}
        self.only_sheader_dict = {}
        with open(sub_file,'r') as fs:
             for line in fs:
                 run_id, sample_name, sample_header , renamed_header, coveragex = line.strip().split("\t")
                 s_key = run_id + ":" + sample_name
                 h_key = run_id + ":" + sample_header
                 sn_key = sample_name
                 sh_key = sample_header
                 if  s_key not in self.sname_dict:
                     self.sname_dict[s_key] = renamed_header
                 if  h_key not in self.sheader_dict:
                     self.sheader_dict[h_key] = renamed_header
                 if  sn_key not in self.only_sname_dict:
                     self.only_sname_dict[sn_key] = renamed_header
                 if  sh_key not in self.only_sheader_dict:
                     self.only_sheader_dict[sh_key] = renamed_header
        return ( self.sname_dict , self.sheader_dict, self.only_sname_dict, self.only_sheader_dict )

    def concat_fasta_files(self,fa_list, out):
        """
        Function to concat fasta files from a list
        """
        filt_fnames = []
        with open(out, 'w') as outfile:
             for fname in fa_list:
                 ffile = glob.glob(fname)[0]
                 g_lengths, n_counts = self.get_fasta_lengths(ffile)
                 if g_lengths[0] == 29903 and n_counts[0] < 5000:
                    filt_fnames.append(ffile)
                    with open(ffile) as infile:
                         for line in infile:
                             outfile.write(line)
                 else:
                    self.log("INFO : Skipping {0} from alpha Genome Length :  {1} , N_counts : {2}".format(fname,g_lengths[0],n_counts[0]))
        return filt_fnames

    def filter_metadata(self,sample_list,out,fill_meta=None):
        """
        Function to filter metadata from a list
        """
        meta_tsv = pd.read_table(self.MASTER_META)
        existing_meta_list = meta_tsv["Virus name"].tolist()
        self.meta_columns = meta_tsv.columns.values.tolist()
        self.no_meta_ids = [ i for i in sample_list if i not in existing_meta_list ]
        meta_tsv = meta_tsv[ meta_tsv["Virus name"].isin(sample_list) ]
        # Change all unknown collection date to run_date
        for index, row in meta_tsv.iterrows():
            if meta_tsv.loc[index,'Collection date'] in ["", "?", "??","unknown", None] :
               #(meta_tsv.loc[index,'Collection date'])
               meta_tsv.loc[index,'Collection date'] = meta_tsv.loc[index,'Run date']
            if meta_tsv.loc[index,'Submission date'] in ["", "?", "??","unknown", None] :
               meta_tsv.loc[index,'Submission date'] = self.SUB_DATE
            #if meta_tsv.loc[index,'Location'].astype(str).str.contains("??|unknown") :
            #   meta_tsv.loc[index,'Location'].replace("??",)
        meta_tsv[['Collection date', 'Submission date']] = meta_tsv[['Collection date','Submission date']].fillna(value=self.SUB_DATE)

        # Change all unknown State to MT (  unknown )
        meta_tsv.to_csv(out, sep='\t', encoding='utf-8',  index=False, line_terminator='\r\n')
        return out

    def rename_submission_fasta(self,alpha_fa_file,rename_dict):
        """
        Get the GISAID submission names for all the files selected for submission

        """
        fa_file = Fasta(alpha_fa_file, key_function=lambda key: rename_dict[key])
        renamed_fa_file = alpha_fa_file + ".renamed"
        with open(renamed_fa_file, 'w') as renamed:
             for entry in fa_file:
                 print(">" + entry.name, file=renamed)
                 for line in entry:
                      print(line, file=renamed)
        shutil.copy(renamed_fa_file, alpha_fa_file)
        if os.path.isfile(renamed_fa_file):
            os.remove(renamed_fa_file)
        fa_file = Fasta(alpha_fa_file)
        return alpha_fa_file

    def prepare_fasta(self):
        """
        Function to the final Alpha nextstrain fasta files
        - Get sname_dict  , sheader_dict mappings from submission manifest
        - Get existing_seq , headers of the submitted files
        - If key in already submitted , remove the file from alpha_file_list; concat files
        - append submitted files to a new file all_jhu_sequences.fasta
        """
        self.sname_dict , self.sheader_dict , self.only_sname_dict , self.only_header_dict =  self.map_submission_names()
        alpha_tuple_list = [ (i.split(":")[0] , i.split(":")[1]) for i in self.sname_dict.keys() ]
        self.alpha_file_list = [x[1] for x in alpha_tuple_list]

        self.existing_seq = self.get_fasta_header(self.SUBMITTED_SEQ)
        self.existing_snames = [ name.split(":")[1] for name, rname in self.sname_dict.items() if rname in self.existing_seq ]
        self.alpha_file_list = [e for i, e in enumerate(self.alpha_file_list) if e not in self.existing_snames]
        alpha_file_path_list = [ self.SEQ_PATH + "/" + r + "/" + self.FASTA_PATH + "/" + s for (r,s) in alpha_tuple_list if s in self.alpha_file_list ]
        alpha_file_path_list = self.get_file_list(alpha_file_path_list, self.CONSENSUS_FASTA)

        self.outseq = self.ALPHA_PREFIX + "-jhu_sequences.fasta"
        self.all_outseq = self.ALPHA_PREFIX + "-all_local_jhu_sequences.fasta"
        filt_fnames = self.concat_fasta_files(alpha_file_path_list,self.outseq)

        ori_headers = self.get_fasta_header(self.outseq)
        #new_headers = [ i.rsplit("/",3)[1].split(".nanopolish")[0] for i in ori_headers ]
        new_headers = {}
        for header in ori_headers:
            m = re.search(self.header_pat,header)
            new_head = m.group(0)
            if header not in new_headers:
                new_headers[header] = new_head

        #new_headers = { i : re.search(self.header_pat,i).group(0) for i in ori_headers }
        #new_headers = [ i.split("4-draft-consensus/")[1] for i in new_headers ]
        self.rename_submission_fasta(self.outseq,new_headers)
        # Rename header if not in self.only_header_dict
        ### This part of the code is to deal with names of format MDHP-18 to be renamed to MDHP-00018
        #for k ,i in new_headers.items():
        #    if i not in self.only_header_dict:
        #        parts = i.split("-")
        #        if len(parts[1].split("_")[0]) < 5:
        #            new_value = "-".join([parts[0],parts[1].split("_")[0].zfill(5)+"_"+parts[1].split("_")[1]])
        #            new_headers[k] = new_value

        rename_dict =  { key : self.only_header_dict[key] if key in self.only_header_dict else key for key in new_headers.values() }
        self.rename_submission_fasta(self.outseq,rename_dict)
        self.all_jhu_list = [self.SUBMITTED_SEQ ,self.outseq ]
        self.concat_fasta_files(self.all_jhu_list,self.all_outseq)
        return ( self.outseq  , self.all_outseq )
        # return final fasta

    def prepare_metadata(self):
        """
        Function to get metadata for the fasta files
        - get all submitted seq metadata
        - Get strain ids , cut by sep "/" index 2, cut "-" [1] to get HP name
        - Append STATE prefix to HP name
        - If present. Filter rows based on ids
        - If not present, create fake metadata based on run_date, default location (Maryland); unknown for everything else
        """
        sample_names = self.get_fasta_header(self.outseq)
        all_sample_names = self.get_fasta_header(self.all_outseq)
        self.outmeta = self.ALPHA_PREFIX + "-jhu_metadata.tsv"
        self.all_outmeta = self.ALPHA_PREFIX + "-all_local_jhu_metadata.tsv"
        self.filter_metadata(sample_names,self.outmeta)
        self.filter_metadata(all_sample_names,self.all_outmeta)
        if len(self.no_meta_ids) > 0:
            self.log("INFO : No metadata for ids : {0}".format(",".join(self.no_meta_ids)))
            self.log("INFO : Adding metadata for the ids")
            no_meta_mapped_list = [ (k, i.split(":")[1] , i.split(":")[0]) for k,i in self.getKeysByValues(self.sname_dict,self.no_meta_ids) ]
            missing_df_lists = []
            missing_df = pd.DataFrame(columns = self.meta_columns)
            self.add_meta_cols = [ i for i in missing_df.columns.tolist() if i not in self.next_fields ]
            #print("\n".join(self.add_meta_cols))
            add_rows = []
            for i,j,k in no_meta_mapped_list:

                run_date = datetime.strptime(k.split("_")[0],self.DATE_FMT)
                run_date = run_date.strftime(self.SUB_FMT)
                row = [ i ,
                       "betacoronavirus",
                       "Original",
                       run_date ,
                       self.loc_default , "unknown" ,
                       self.host,
                       "unknown", "unknown" , "unknown" , "unknown" ,
                       self.specimen_source ,
                       "unknown", "unknown" , "unknown" ,
                       self.seq_tech , self.assembly_method ,
                       "unknown" , self.dep  , self.address , j , self.dep  , self.address, j,
                       self.authors , self.submitter , self.SUB_DATE, run_date , k ]
                row = row + ["unknown" for i in self.add_meta_cols]
                add_rows.append(row)
                #missing_df.loc[len(missing_df)] = row
                #missing_df = missing_df.append(row)
                #missing_df = pd.concat([missing_df, pd.DataFrame(row)], axis=0)
                #missing_df.loc[ missing_df.index.max() +  1 ] =  row
                #missing_df[self.meta_columns] = row

                #missing_df = missing_df.append(pd.Series(row),index = self.meta_columns )
            missing_df = pd.DataFrame(add_rows, columns=self.meta_columns )
            missing_df.to_csv(self.outmeta, mode='a', sep="\t",header=False)
            missing_df.to_csv(self.all_outmeta, mode='a', sep="\t",header=False)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Script to Parse new sequencing runs from alpha nextstrain")
    parser.add_argument("--submission_manifest",dest="sub_manifest",type=str, required=True, help="Path to the tsv file with run_id,sample_name,sample_header,rename_header,coveragex")
    parser.add_argument("--seq-path",dest="SEQ_PATH",type=str, required=True, help="Path to the sequencing runs folder")
    parser.add_argument("--fasta-path",dest="FASTA_PATH",type=str, required=True, help="Path to the draft consensus fasta folder")
    parser.add_argument("--submitted-fasta",dest="SUBMITTED_SEQ",type=str, required=True, help="Path to a single fasta of already submitted fasta files")
    parser.add_argument("--master-meta",dest="MASTER_META",type=str, required=True, help="File containing the master metadata for GISAID submission")
    parser.add_argument("-out",dest="OUTPUT_DIR",type=str, required=True, help="Path to output directory for alpha nextstrain")

    args = parser.parse_args()
    prepAlpha = PrepareAlphaNextstrain(args.sub_manifest,args.SEQ_PATH,args.FASTA_PATH,args.MASTER_META,args.SUBMITTED_SEQ,args.OUTPUT_DIR)
    try:
        prepAlpha.prepare_fasta()
        prepAlpha.prepare_metadata()
    except:
        raise


import os
import csv
import pickle as pkl
import tensorflow as tf

from operator import itemgetter

# Show warnings and errors only
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

tf.flags.DEFINE_string('output_directory', None, 'Restore the model from this run')
tf.flags.DEFINE_string('data_file', None, 'Data file path')
tf.flags.DEFINE_integer('mu_threshold', None, 'mu_threshold')
tf.flags.DEFINE_integer('beta_threshold', None, 'beta_threshold')
tf.flags.DEFINE_integer('phi_threshold', None, 'phi_threshold')

FLAGS = tf.app.flags.FLAGS

def categories_count(data_file_):
    with open(data_file_, 'r', encoding='utf-8', errors='ignore') as f:
        csv_reader = csv.reader(f)
        header = next(csv_reader)
        label_idx = header.index('label')
        counts_ = dict()

        for line in csv_reader:
            counts_[line[label_idx]] = counts_.setdefault([line[label_idx]], 0) + 1
    return counts_

# load the dictionaries
# load the importance values of the interpretation_features
with open(os.path.join(FLAGS.output_directory, 'IF.pkl'), 'rb') as f:
    IF = pkl.load(f)

# load the count of the interpretation_features
with open(os.path.join(FLAGS.output_directory, 'count_IF.pkl'), 'rb') as f:
    Freq = pkl.load(f)

# load the average important value of the interpretation_features
with open(os.path.join(FLAGS.output_directory, 'average_IF.pkl'), 'rb') as f:
    Ravg = pkl.load(f)

# final dictionary
final_IF = dict()

for category, sub_dict in Ravg.items():
    for word, value in sub_dict.items():
        if value >= FLAGS.mu_threshold and Freq[category][word] >= FLAGS.beta_threshold and len(word) >= 2:
            is_true = True
            for category_idx in range(len(Ravg)):
                if category_idx in Ravg and category_idx != category:
                    if word not in Ravg[category_idx] or value >= Ravg[category_idx][word] + FLAGS.phi_threshold:
                        continue
                    else:
                        is_true = False
                        break
            if is_true:
                final_IF.setdefault(category, dict())
                final_IF[category][word] = value

print(f'the length of the interpretation_features dict for all categories: {len(final_IF)}')

# write the sets of true features to files.
with open(os.path.join(FLAGS.output_directory, 'final_IF.pkl'), 'wb') as f:
    pkl.dump(final_IF, f, pkl.HIGHEST_PROTOCOL)

for key, value in final_IF.items():
    print(f'The interpretation features of class: {key} is :')
    for item in sorted(value.items(), key=itemgetter(1)):
        print('{} : {:.2f}'.format(item[0], item[1]))
    print(f'The length of the dictionary for class {key} is = {len(value)}')


# http://oj.leetcode.com/problems/combinations/
class Solution:
	def f(self, a, n):
		b = []
		for i in range (0, len(a)):
			for j in range(0, n+1):
				if j > a[i][len(a[i]) -1]:
					b.append(a[i] + [j])
		return b
	# @return a list of lists of integers
	def combine(self, n, k):
		a = []
		for i in range (1, n+1):
			a.append([i])
		for i in range (2, k+1):
			a = self.f(a, n)
		return a

n = 10
k = 4
s = Solution()
print(s.combine(n, k))


import pyspeckit
from spectral_cube import SpectralCube
from astropy import units as u

OneOneFile = '/home/monsch/Desktop/NH3/project/omc1_vla_11_JEP_clean_spw0_mscale_v1.fits'
cube11sc = SpectralCube.read(OneOneFile)
moment_0 = cube11sc.moment(order=0)
moment_1 = cube11sc.moment(order=1)

cube11 = pyspeckit.Cube(OneOneFile)

cube11.xarr.convert_to_unit(u.km/u.s, equivalencies=u.spectral())
cube11.xarr.refX = pyspeckit.spectrum.models.ammonia.freq_dict['oneone']

spf = pyspeckit.wrappers.fitnh3.fitnh3(cube11,vrange=[-100, 100], vrangeunit='km/s', quiet=False,Tex=20, Tkin=15, column=1000000000000000.0, fortho=1.0,tau=None)


from concurrent.futures import ThreadPoolExecutor
from work_with_db import get_data_from_table
from sql import SQL
import datetime
import logging

while True:
    db = input(
        '''
УКАЖИ СХЕМУ ДЛЯ ПОДКЛЮЧЕНИЯ НАПРИМЕР:

IBS/IBS_EXPOBANK1908@BARKDB.BARK_RPT
IBS/IBS@57_TEST_REP_OMSK
IBS/IBS@57_TEST_REP_TOMSK
IBS/IBS@57_TEST_REP_NSK1
IBS/IBS@57_TEST_REP_NSK2

СХЕМА:
''')
    logging.disable(logging.DEBUG)

    num = 0
    table_names = list(SQL.keys())
    for table_name in table_names: print(f'{num}.', table_name); num += 1

    tables = input('\nУКАЖИ НОМЕРА ВЫГРУЖАЕМЫХ ТАБЛИЦ В ФОРМАТЕ (12 или 0123):')
    id_realization_1 = str(input("ВВЕДИ id 1 РЕАЛИЗАЦИИ:"))
    id_realization_2 = str(input("ВВЕДИ id 2 РЕАЛИЗАЦИИ:"))

    startTime = datetime.datetime.today()
    print(startTime)
    Params = []
    for table in tables:
        table_name = table_names[int(table)]
        sql_query = SQL[table_name]
        if id_realization_2 != '':
            Params += [(table_name + '1', id_realization_1, id_realization_2, sql_query, db)]
            Params += [(table_name + '2', id_realization_2, id_realization_1, sql_query, db)]
        else:
            Params += [(table_name, id_realization_1, None, sql_query, db)]

    with ThreadPoolExecutor(max_workers=len(Params)) as executor:
        logging.debug('Запускаем потоки')
        list(map(lambda param: executor.submit(get_data_from_table, param), Params))

    endTime = datetime.datetime.today()
    print(f"ЗАВЕРШЕНО {endTime - startTime}")


from chainercv import transforms

import numpy as np
from skimage import transform as skimage_transform
import cv2 as cv

USE_OPENCV = False

def cv_rotate(img, angle):
    if USE_OPENCV:
        img = img.transpose(1, 2, 0) / 255.
        center = (img.shape[0] // 2, img.shape[1] // 2)
        r = cv.getRotationMatrix2D(center, angle, 1.0)
        img = cv.warpAffine(img, r, img.shape[:2])
        img = img.transpose(2, 0, 1) * 255.
        img = img.astype(np.float32)
    else:
        # scikit-image's rotate function is almost 7x slower than OpenCV
        img = img.transpose(1, 2, 0) / 255.
        img = skimage_transform.rotate(img, angle, mode='edge')
        img = img.transpose(2, 0, 1) * 255.
        img = img.astype(np.float32)
    return img

def random_erasing(x, p=0.5, s_base=(0.02, 0.4), r_base=(0.3, 3)):
    x = x.copy()
    size = x.shape[2]
    if np.random.uniform(0, 1) > p:
        while True:
            s = np.random.uniform(s_base[0], s_base[1]) * size * size
            r = np.random.uniform(r_base[0], r_base[1])
            w = int(np.sqrt(s / r))
            h = int(np.sqrt(s * r))
            left = np.random.randint(0, size)
            top = np.random.randint(0, size)
            if left + w < size and top + h < size:
                break
        c = np.random.randint(0, 256)
        x[:, top:top + h, left:left + w] = c
    return x

def transform_img(inputs, mean, std,
                  pca_sigma=0, random_angle=0, x_random_flip=False,
                  y_random_flip=False, expand_ratio=1., random_crop_size=(224, 224),
                  random_erase=False, output_size=(224, 224),
                  train=False):
    x, lab = inputs
    x = x.copy()
    # Color augmentation
    if train and pca_sigma != 0:
        x = transforms.pca_lighting(x, pca_sigma)
    x -= mean[:, None, None]
    x /= std[:, None, None]
    x = x[::-1]
    if train:
        # Random rotate
        if random_angle != 0:
            angle = np.random.uniform(-random_angle, random_angle)
            x = cv_rotate(x, angle)

        # Random flip
        if x_random_flip or y_random_flip:
            x = transforms.random_flip(x, x_random=x_random_flip, y_random=y_random_flip)

        # Random expand
        if expand_ratio > 1:
            x = transforms.random_expand(x, max_ratio=expand_ratio)

        if all(random_crop_size) > 0:
            x = transforms.random_crop(x, random_crop_size)
        else:
            if random_erase:
                x = random_erasing(x)

    if all(random_crop_size) > 0:
        x = transforms.resize(x, random_crop_size)
    else:
        x = transforms.resize(x, output_size)

    return x, lab


from pysimm import system, lmps, forcefield

def run(test=False):
  # use a smiles string to query the pubchem search database and read the mol file returned from the http request
  # if cannot get to internet, read local cached response from pubchem
  try:
    s = system.read_pubchem_smiles('c1=cc=cc=c1')
  except:
      import os
      s = system.read_mol(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, 'c1=cc=cc=c1.mol'))

  # the resulting system (benzene) has alternating double bonds
  # we want pysimm to recognize the ring as aromatic, so we define each bond in the ring to be bond order 'A'
  for b in s.bonds:
    if b.a.elem=='C' and b.b.elem=='C':
      b.order='A'

  # the resulting system has sufficient information to type with a forcefield, here we will use the Dreiding force field
  # we will also determine partial charges using the gasteiger algorithm
  s.apply_forcefield(forcefield.Dreiding(), charges='gasteiger')

  # we'll perform a 2 step energy minimization using the steepest decent and conjugate gradient algorithms in LAMMPS
  lmps.quick_min(s, min_style='sd', name='min_sd')
  lmps.quick_min(s, min_style='cg', name='min_cg')

  # write a few different file formats
  s.write_xyz('benzene.xyz')
  s.write_yaml('benzene.yaml')
  s.write_lammps('benzene.lmps')
  s.write_chemdoodle_json('benzene.json')

if __name__ == '__main__':
    run()


import feedparser

class PeopleOnTheISS:

    def __init__(self, agentConfig, checksLogger, rawConfig):
        self.agentConfig = agentConfig
        self.checksLogger = checksLogger
        self.rawConfig = rawConfig

    def run(self):
        url = "http://feeds.feedburner.com/SpaceRightNow"
        feed = feedparser.parse(url)
        current = feed[ "items" ][0]["title_detail"]["value"]
        return {"People on the ISS" : current}


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

import time
from pynput.keyboard import Controller
from deval.component.std.keyevent import KeyEventComponent

class MacKeyEventComponent(KeyEventComponent):

    def __init__(self, name):
        self._name = name
        self.keyboard = Controller()

    def keyevent(self, keyname):
        """
        Use pynput to simulate keyboard input

        Parameters:
            keyname - the keys
        """
        waittime = 0.05
        for c in keyname:
            self.keyboard.press(key=c)
            self.keyboard.release(key=c)
            time.sleep(waittime)

    def text(self, text, enter=False):
        """
        Use pynput to simulate keyboard input

        Parameters:
            keyname - the keys
        """
        waittime = 0.05
        for c in text:
            self.keyboard.press(key=c)
            self.keyboard.release(key=c)
            time.sleep(waittime)

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value


JSON_URL = "https://api.patreon.com/campaigns/157274/posts?filter%5Bis_by_creator%5D=true&page%5Bcount%5D=10&json-api-version=1.0"
FEED_FILE = "war_nerd.rss"
OWNER_NAME = "maskd"
OWNER_EMAIL = "none@example.com"
FEED_CATEGORY = "History"

ATOM = "http://www.w3.org/2005/Atom"
ITUNES = "http://www.itunes.com/dtds/podcast-1.0.dtd"
NSMAP = {
    "atom": ATOM,
    "itunes": ITUNES
}

FEED_ATTRIBUTES = {
    "title": "Radio War Nerd",
    "description": "War Nerd live updates",
    "link": JSON_URL,
    "language": "en-us",
    "{%s}subtitle" % (ITUNES): "Radio War Nerd",
    "{%s}author" % (ITUNES): "Mark Ames and Gary Brecher",
    "{%s}summary" % (ITUNES): "War Nerd live updates",
    "{%s}explicit" % (ITUNES): "Yes",
}


import socket
import os
import time
import smtplib
import email.message

SLEEP_INT = int(os.getenv('SLEEP_INT'))
TIMEOUT = int(os.getenv('TIMEOUT'))

SMTP_SERVER = os.getenv('SMTP_SERVER')
EMAIL_RECIPIENT = os.getenv('EMAIL_RECIPIENT')
EMAIL_SENDER = os.getenv('EMAIL_SENDER')

EMAIL_SUBJECT = 'Warning from Rancher-SocketTester'

def try_send_email(subject, body):
    try:
        msg = email.message.Message()
        msg['Subject'] = subject
        msg['From'] = EMAIL_SENDER
        msg['To'] = EMAIL_RECIPIENT
        msg.add_header('Content-Type', 'text/html')
        msg.set_payload(body)
        s = smtplib.SMTP(SMTP_SERVER)
        s.sendmail(EMAIL_SENDER, EMAIL_RECIPIENT, msg.as_string())
    except Exception as e:
        print('Warning: could not send email: ' + e)

def test_socket_connection(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(TIMEOUT)
    try:
        s.connect((host, port))
        print('Connected to socket: %s:%d' % (host, port))
    except socket.error as e:
        msg = "Could not connect to %s:%d - %s" % (host, port, e)
        print(msg)
        try_send_email(EMAIL_SUBJECT, msg)
    s.close()

def main():
    while True:
        host = os.getenv('HOST')
        port = int(os.getenv('PORT'))
        test_socket_connection(host, port)
        time.sleep(SLEEP_INT)

if __name__ == '__main__':
    main()


from flask import Flask,render_template,redirect ,request
from mysqlconnection import connectToMySQL
app = Flask(__name__)

##****************************Main Page ***********************************
@app.route('/')
def startup():
    return redirect("/users")

@app.route('/users')
def main():
    print("test")
    mysql=connectToMySQL("users")
    users= mysql.query_db("SELECT * FROM users;")
    return render_template("user.html", all_users=users)
@app.route('/users/new')
def newuser():
    return render_template("new_user.html")

##************************************CREATE USER ****************************
@app.route('/users/new/input', methods=["POST"])
def input():
    query = "INSERT INTO users (first_name,last_name,email,created_at,updated_at) VALUES (%(fname)s,%(lname)s,%(email)s,NOW(),NOW());"
    data = {
        "fname": request.form["f_name"],
        "lname": request.form["l_name"],
        "email": request.form["email"]
    }
    mysql=connectToMySQL("users")
    id = mysql.query_db(query,data)
    print(id)
    id = str(id)
    return redirect ("/users/"+id)

#*****************************SHOW USER *************************************
@app.route('/users/<id>')
def show(id):
    mysql=connectToMySQL("users")
    query = "SELECT* from users where user_id=%(id)s;"
    data = {
        "id" : id
    }
    data = mysql.query_db(query,data)
    person = data[0]
    return render_template("user_info.html", id=id, one_users = person)
##********************************EDIT PAGE *************************************
@app.route('/edit/<id>')
def edit(id):
    mysql=connectToMySQL("users")
    return render_template("edit_user.html", id=id)

@app.route('/edit/users/<id>', methods=['POST'])
def editusers(id):
    mysql=connectToMySQL("users")
    query = "Update users SET first_name=%(f_name)s, last_name=%(l_name)s, email=%(email)s where user_id=%(id)s;"
    data = {
        "f_name": request.form["f_name"],
        "l_name": request.form["l_name"],
        "email": request.form["email"],
        "id": id
    }
    mysql.query_db(query,data)
    return redirect ("/users", id=id)
#**********************************Delete **************************************
@app.route('/delete/<id>')
def delete():
    mysql=connectToMySQL("users")
    query = "Delete from users where user_id=%(id)s;"
    data = {
        "id" : id
    }
    mysql.query_db(query,data)
    return redirect("/users")

#***********************************End*************************************
if __name__ == "__main__":
    app.run(debug=True)


# -*- coding:utf-8 -*-
def rewrite():
    with open('haproxy.conf.bak','r',encoding='utf-8') as file ,\
        open('haproxy.conf','w',encoding='utf-8') as file2:
        file2.write(file.read())

def getbackerinfo(server):
    with open('haproxy.conf','r',encoding='utf-8') as file:
        conf_list = file.readlines()
        tag = False
        for line in conf_list:
            if line.strip() == 'backend %s' % server:
                backend_line = line.strip().split(' ')
                tag = True
                print('backend name: %s' %backend_line[1])
            if tag and line.strip().startswith('server'):
                server_line = line.strip().split(' ')
                print("serverid: %s\n"
                      "serverip: %s\n"
                      "weight: %s\n"
                      "maxconn: %s\n" % (server_line[1],server_line[2],server_line[4],server_line[6]))
                tag = False

def addbackendinfo(backend,server,weight='20',maxconn='3000'):
    with open('haproxy.conf','r',encoding='utf-8') as file:
        conf_list = file.readlines()
        add_backend = 'backend %s' %backend
        add_server = 'server %s %s weight %s maxconn %s' %(server,server,weight,maxconn)
        backend_tag = False
        server_tag = False
        for line in conf_list:
            compare_line = line.strip()
            if add_backend == compare_line:
                backend_tag = True
            elif add_server == compare_line:
                server_tag = True
        if backend_tag is True and server_tag is True:
            print("%s\n%s.\nAlready exited." %(add_backend,add_server))
        elif backend_tag is True and server_tag is False:
            print("backer %s.\nAlready exited." % add_backend)
        else:
            conf_list.append('\nbackend %s\n' % backend)
            conf_list.append('\t\tserver %s %s weight %s maxconn %s\n' %(server,server,weight,maxconn))
        with open('haproxy.conf.bak','w',encoding='utf-8') as file2:
            for line in conf_list:
                file2.write(line)
    rewrite()

def delbackendinfo(backend,server,weight='20',maxconn='3000'):
    with open('haproxy.conf', 'r', encoding='utf-8') as file:
        del_backend = 'backend %s' %backend
        del_server = 'server %s %s weight %s maxconn %s' %(server,server,weight,maxconn)
        conf_list = file.readlines()
        write_list = []
        backend_tag = False
        server_tag = False
        for line in conf_list:
            if line.strip() == del_backend:
                backend_tag = True
                del_backend = line
            elif line.strip() == del_server:
                server_tag = True
                del_server = line
            write_list.append(line)
            if backend_tag is True and server_tag is True:
                del write_list[write_list.index(del_backend)]
                del write_list[write_list.index(del_server)]
                backend_tag = False
                server_tag = False
        with open('haproxy.conf.bak','w',encoding='utf-8') as file2:
            for line in write_list:
                file2.write(line)
    rewrite()

menu_list = ["查看backend信息","添加backend信息","删除backend信息","按q退出程序"]

while True:
    for index, menu in enumerate(menu_list):
        print(index, menu)

    choice = input('请选择>>>：')
    if choice == '0':
        server = str(input("请输入您要查看的backend名："))
        getbackerinfo(server)
    elif choice == '1':
        print("范例：{'backend':'www.oldboy.org','record':{'server':'100.1.7.9','weight':20,'maxconn':3000}}")
        args = input("请输入您想要添加的信息：")
        arg = eval(args)
        backend = arg['backend']
        record_dict = arg['record']
        server = record_dict['server']
        weight = str(record_dict['weight'])
        maxconn = str(record_dict['maxconn'])
        addbackendinfo(backend,server,weight,maxconn)
    elif choice == '2':
        print("范例：{'backend':'www.oldboy.org','record':{'server':'100.1.7.9','weight':20,'maxconn':3000}}")
        args = input("请输入您想要删除的信息：")
        arg = eval(args)
        backend = arg['backend']
        record_dict = arg['record']
        server = record_dict['server']
        weight = str(record_dict['weight'])
        maxconn = str(record_dict['maxconn'])
        delbackendinfo(backend,server,weight,maxconn)
    elif choice == 'q':
        exit()


import abc
import sqlite3
from contextlib import contextmanager
from os import path
from typing import Any, Dict, Generator, List, Sequence, Sized

from movielog import humanize
from movielog.logger import logger

DB_FILE_NAME = "movie_db.sqlite3"
DB_DIR = "db"

Connection = sqlite3.Connection
Cursor = sqlite3.Cursor
Row = sqlite3.Row

DB_PATH = path.join(DB_DIR, DB_FILE_NAME)
DbConnectionOpts: Dict[str, Any] = {"isolation_level": None}

@contextmanager
def connect() -> Generator[Connection, None, None]:
    connection = sqlite3.connect(DB_PATH, **DbConnectionOpts)
    connection.row_factory = sqlite3.Row
    yield connection
    connection.close()

@contextmanager
def transaction(connection: Connection) -> Generator[None, None, None]:
    connection.execute("PRAGMA journal_mode = WAL;")
    connection.execute("BEGIN TRANSACTION;")
    yield
    connection.commit()
    connection.execute("PRAGMA journal_mode = DELETE")

def exec_query(query: str) -> Sequence[sqlite3.Row]:
    with connect() as connection:
        return connection.execute(query).fetchall()

class Table(abc.ABC):
    recreate_ddl: str
    table_name: str

    @classmethod
    def add_index(cls, column: str) -> None:
        script = """
            DROP INDEX IF EXISTS "index_{0}_on_{1}";
            CREATE INDEX "index_{0}_on_{1}" ON "{0}" ("{1}");
        """
        with connect() as connection:
            connection.executescript(script.format(cls.table_name, column))

    @classmethod
    def validate(cls, collection: Sized) -> None:
        with connect() as connection:
            inserted = connection.execute(
                "select count(*) from {0}".format(cls.table_name),  # noqa: S608
            ).fetchone()[0]

            assert collection  # noqa: S101

            expected = len(collection)
            assert expected == inserted  # noqa: S101

            logger.log("Inserted {} {}.", humanize.intcomma(inserted), cls.table_name)

    @classmethod
    def recreate(cls) -> None:
        formatted_ddl = cls.recreate_ddl.format(cls.table_name)
        with connect() as connection:
            logger.log("Recreating {} table...", cls.table_name)
            connection.executescript(formatted_ddl)

    @classmethod
    def insert(cls, ddl: str, parameter_seq: List[Dict[str, Any]]) -> None:
        logger.log("Inserting {}...", cls.table_name)

        with connect() as connection:
            with transaction(connection):
                connection.executemany(ddl, parameter_seq)

    @classmethod
    def delete(cls, key: str, ids: Sequence[str]) -> None:
        ddl = """
            DELETE FROM {0} WHERE {1} IN ({2});
        """

        with connect() as connection:
            with transaction(connection):
                connection.execute(ddl.format(cls.table_name, key, cls.format_ids(ids)))
                logger.log(
                    "Deleted {} rows from {}...",
                    humanize.intcomma(len(ids)),
                    cls.table_name,
                )

    @classmethod
    def format_ids(cls, ids: Sequence[str]) -> str:
        return ",".join('"{0}"'.format(db_id) for db_id in ids)


# this script was (probably?) written to find song filename with similar filenames
# To find possible duplicates, as filename were often renamed in various uncanny and incoherent and unpredictable manner, I split all filenames and tried to find filename with at least 2 identical words in them, apart from very often occuring words like the a of on in etc.

from sys import argv
from recur_scan_new import recur_scan
from htmlshow import *

if len(argv) != 2:
    exit('supply a path!')
path = argv[1]

filepaths, nope, dirs_unused = recur_scan(path)

filtered = ['the','a','of', '-', 'on', 'in', 'to', 'by']
def fingy(filename):
    return [a.lower() for a in filename[:-4].replace('-', ' - ').split() if a.lower() not in filtered and len(a)>1 and not a.isdigit()]
fingers = {}
freq = {}

html = open(argv[1]+"\\sim-names.hml",'w',encoding = 'utf-8')

html_start(html)
html.write(h2('similiar name LOL PUN'))

tbl = trth(['col1', 'col2', 'col3', 'col4'])
for path, folder, filename, filesize in filepaths:
    # print(repr((path, folder, filename, filesize)))
    # break
    fingered = fingy(filename)
    for w in fingered:
        if w not in freq: freq[w] = 0
        freq[w]+=1
    fingers[(filesize,filename)] = fingered
    # tbl+=tr((str(filesize), filename, folder.replace('C:\\musk\\',''), '|'.join(fingy(filename))))

# removing often occuring words

keepthose = ['theme']
most_occuring = [(fr,w) for w,fr in freq.items() if fr>4 and w not in keepthose]
# for fr,w in most_occuring:
    # print(fr,w)
for fr,w in most_occuring:
    # print(fr,w)
    print('removing', fr,w)
    for tup, fing in fingers.items():
        if w in fing: fing.remove(w)

matches = {}
for tup1, finger1 in fingers.items():
    for tup2, finger2 in fingers.items():
        # if tup1 == tup2 or tup1[0] == tup2[0]: continue
        if tup1[1] == tup2[1]: continue
        intersect = set(finger1) & set(finger2)
        mh = len(intersect)
        if mh > 0 and (tup2, tup1) not in matches and intersect != set(['theme']):
            # if (tup1, tup2) not in matches: matches[(tup1, tup2)] = 0
            matches[(tup1, tup2)] = (mh, '|'.join(list(intersect)))
tbl = trth(['col1', 'col2', 'col3', 'col4'])
sorted_matches = [(tups[0], tups[1], vals[0], vals[1]) for tups, vals in matches.items()]
# for tups, vals in matches.items():
#     tup1, tup2 = tups
#     k1,f1 = tup1
#     k2,f2 = tup2
#     mh, joins = vals
#     tbl+=tr((f1, f2, mh, joins))
sorted_matches.sort(key = lambda a: a[2], reverse=True)
for tup1, tup2, mh, joins in sorted_matches:
    k1,f1 = tup1
    k2,f2 = tup2
    tbl+=tr((f1, f2, mh, joins))

print(len(matches))

html.write(table(tbl))
html_finish(html)