import pandas as pd
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from rosetta.parallel.pandas_easy import groupby_to_series_to_frame
from joblib import Parallel,delayed
import multiprocessing,re
#===============================================================================
#                     1. Merge scaffolds
#===============================================================================
fa = '/media/lewislab/Dropbox (UCSD SBRG)/LewisPub/Data/RefGenomes/hamster_2013/2015_PBJ_temp_assembly/ch_illumina_pbj.fasta'
outFile = '/data/shangzhong/LargeDeletion/hamster_pb_illu_super_scaffold.fa'
chr_junc_file = '/data/shangzhong/LargeDeletion/hamster_pb_super_scaffold_junc.txt'
"""
in_handle = open(fa,'r')
out_handle = open(outFile,'w')
chr_junc_handle = open(chr_junc_file,'w')
sequence = ''
n = 1
pos = 0
for record in SeqIO.parse(in_handle,'fasta'):
    sequence += str(record.seq)
    pos += len(record.seq)
    if len(sequence) >= 200000000:
        item = SeqRecord(Seq(sequence), id = 'chr'+str(n),description="")
        SeqIO.write(item,out_handle,'fasta')
        sequence = ''
        n += 1
        pos = 0
    else:
        sequence += 'N'*500
        chr_junc_handle.write('\t'.join(['chr'+str(n),str(pos),str(pos+500)]) + '\n')
        pos += 500
chr_junc_handle.close()
if sequence != '':
    item = SeqRecord(Seq(sequence[:-500]), id = 'chr'+str(n),description="")
    SeqIO.write(item,out_handle,'fasta')
    # remove last line of junctions
    df = pd.read_csv(chr_junc_file,sep='\t',header=None,names=['chr','start','end'])
    df = df[:-1]
    df.to_csv(chr_junc_file,sep='\t',index=False)
out_handle.close()
# check the length of each chromosome
handle = open(outFile)
for record in SeqIO.parse(handle,'fasta'):
    print len(record.seq)
# check the inserted 500 Ns
df = pd.read_csv(chr_junc_file,sep='\t',header=0)
for record in SeqIO.parse(open(outFile),'fasta'):
    chr_name = record.id
    chr_seq = str(record.seq)
    chr_df = df[df['chr'].values==chr_name]
    for start,end in zip(chr_df['start'],chr_df['end']):
        insert = chr_seq[int(start):int(end)]
        if insert != 'N'*500:
            raise ValueError('insert seqeunce pos extraction wrong')
"""
#===============================================================================
#                 2. filter largedel results (make sure the deletion doesn't span over scaffolds)
#===============================================================================
def del_overlap(chr_name,start,end,junc_df):
    """start,end are deletion start end"""
    junc_df = junc_df[junc_df['chr'].values=='chr' + str(chr_name)]
    filter_junc = junc_df[(junc_df['start']>=int(start)) & (junc_df['end']<=int(end))]
    if filter_junc.empty:
        return True
    else:
        return False
# large_del = '/data/shangzhong/LargeDeletion/01_largedel_res.txt'
# filter_large_del = '/data/shangzhong/LargeDeletion/02_filter_large_del.txt'
#
# junc_df = pd.read_csv(chr_junc_file,sep='\t',header=0,names=['chr','start','end'])
# lar_del_df = pd.read_csv(large_del,sep='\t',header=0,names=['chr','start','end','cov'])
# lar_del_df['chr'] = lar_del_df['chr'].astype('str')
# print(lar_del_df.shape)
# #del_overlap('1','783881','785184',junc_df)
# cri = lar_del_df.apply(lambda x: del_overlap(x['chr'],x['start'],x['end'],junc_df),axis=1)
# res_df = lar_del_df[cri]
# print(res_df.shape)
# res_df.to_csv(filter_large_del,sep='\t',index=False)
#===============================================================================
#                 3. get the overlap between the deletion and gene
#===============================================================================
def keep_map(single_df):
    """decide to keep the blast hits or not"""
    index = single_df.index
    if len(index) == 1:
        return single_df
    for i in range(len(index))[:-1]:
        if single_df.loc[index[i],'keep'] == 'F':
            continue
        max_index = max(single_df.loc[index[i],'sstart'],single_df.loc[index[i],'send'])
        min_index = min(single_df.loc[index[i],'sstart'],single_df.loc[index[i],'send'])
        fst_range = range(min_index,max_index+1)
        for j in range(i+1,len(index)):
            max_in = max(single_df.loc[index[j],'sstart'],single_df.loc[index[j],'send'])
            min_in = min(single_df.loc[index[j],'sstart'],single_df.loc[index[j],'send'])
            snd_range = range(min_in,max_in+1)
            if set(fst_range).intersection(snd_range) != set():
                single_df.loc[index[j],'keep'] = 'F'
    return single_df
#----------------- 1) filter blast results -------------------------------------
def applyParallel(dfGrouped,func,thread):
    retLst = Parallel(thread)(delayed(func)(group) for name,group in dfGrouped)
    return pd.concat(retLst)

# del_threshold = 500
# blast = '/data/shangzhong/LargeDeletion/05_del_blast2_hamster.txt'
# blst_df = pd.read_csv(blast,sep='\t',header=None,names=['qid','chr','iden','len',
#                     'mis','gap','qstart','qend','sstart','send','evalue','bit'])
# blst_df['keep'] = pd.Series(['T']*blst_df.shape[0])
# # remove overlapped regions
# res = applyParallel(blst_df.groupby(['qid','chr']),keep_map,10)
# result = res.sort_index()
# result = result[result['keep'].values=='T']
# result = result.drop('keep',1)
# result.to_csv('/data/shangzhong/del_blast2_hamster.txt',sep='\t',header=0,index=False)

#===============================================================================
#                     4. get overlap between deletion and genes
#===============================================================================
def gene_del_overlap(line,del_df):
    """check gff gene line overlap with deletion
    """
    g_start = line['start']
    g_end = line['end']
    chrom = line['chr']
    # only keep deletions in the same chromosome
    chr_del_df = del_df[del_df['chr'].values==chrom]
    chr_del_df['overlap'] = chr_del_df.apply(lambda x: set(range(min(x['sstart'],x['send']),max(x['sstart'],x['send'])+1)).intersection(range(g_start,g_end+1)),axis=1)
    overlap_df = chr_del_df[chr_del_df['overlap'].values!=set()]
    overlap_del = list(set(overlap_df['qid'].tolist()))
    line['del'] = ','.join(overlap_del)
    overlap_l = overlap_df['overlap'].tolist()  # overlap list
    if overlap_l == []:
        line['del_start'] = line['del_end'] = 0
    else:
        del_s = []; del_e = []
        for over in overlap_l:
            del_s.append(min(list(over)))
            del_e.append(max(list(over)))
        del_s = [str(i) for i in del_s]
        del_e = [str(i) for i in del_e]
        line['del_start'] = ','.join(del_s)
        line['del_end'] = ','.join(del_e)
    return line

def get_del_len(row):
    start = row['del_start'].split(',')
    end = row['del_end'].split(',')
    pos = []
    for m,n in zip(start,end):
        pos.extend(range(m,n+1))
    length = len(list(set(pos)))
    return length

blast = '/data/shangzhong/LargeDeletion/05_del_blast2_hamster_rm_overlap.txt'
blst_df = pd.read_csv(blast,sep='\t',header=None,names=['qid','chr','iden','len',
                    'mis','gap','qstart','qend','sstart','send','evalue','bit'])
# filter blast results
blst_df = blst_df[~((blst_df['iden']<90.00) & (blst_df['len']<200))]

del_chrs = blst_df['chr'].tolist()  # deletion mapped chromosomes

# read gff file and only keep annotation on the deletion mapping chromosomes
gff = '/data/genome/hamster/ncbi_refseq/hamster.gff'
gff_df = pd.read_csv(gff,sep='\t',header=None,comment='#',names=['chr','s','feature','start','end','dot','strand','none','anno'])
gff_df = gff_df.drop(['s','dot','none'],axis=1)
gff_df = gff_df[gff_df['chr'].isin(del_chrs)]
# only keep annotation lines that have gene= in description column
cri = gff_df['anno'].apply(lambda x: 'gene=' in x)
gff_df = gff_df[cri]
gff_df = gff_df.reset_index(drop=True)
# add geneid and gene symbol to dataframe and only extract line whose feature is gene
gff_df['gene'] = gff_df['anno'].apply(lambda x: re.search('(?<=gene=).+?(?=;|$)',x).group())
gff_df['geneid'] = gff_df['anno'].apply(lambda x: re.search('(?<=GeneID:).+?(?=[;,]|$)',x).group())
gff_df['gene_len'] = gff_df['end'] - gff_df['start'] + 1
gene_df = gff_df[gff_df['feature'].values=='gene'].copy()
gene_df = gene_df.drop(['anno'],1)
gene_df = gene_df.reset_index(drop=True)
# add del_start, del_end column to gene_df
row_num = gene_df.shape[0]
gene_df = gene_df.reindex(columns=gene_df.columns.tolist() + ['del_start','del_end','del'],fill_value=0)

### get gene deletion start and end
res = gene_df.apply(lambda row: gene_del_overlap(row,blst_df),axis=1)
res_df = res[res['del_start'].values!=0]
res_df['del_len'] = res_df.apply(lambda row: get_del_len(row),axis=1)
res_df['del_percent'] = res_df['del_len'].div(res_df['gene_len'])


import json
from os import path
import pyperclip

def lookup(code1, code2):
    codes_filename = path.join(path.expanduser("~"),'.ibsec_codes.json')
    with open(codes_filename) as codes_file:
        CODES = json.load(codes_file)
    response = CODES[code1 - 1] + CODES[code2 - 1]
    pyperclip.copy(response)
    print (response + ' has been copied to the clipboard!')


#!/usr/bin/env python

"""The setup script."""

from setuptools import find_packages, setup

with open("README.md") as readme_file:
    readme = readme_file.read()

requirements = ["numpy", "pandas", "xlrd", "openpyxl", "click", "fuzzywuzzy", "fuzzymatcher"]
test_requirements = ["pytest>=6.2.5"]
development_requirements = ["pre-commit", "matplotlib"]

setup(
    author="Lennart Damen",
    author_email="lennart.damen@bigdatarepublic.nl",
    python_requires="==3.7.5",
    classifiers=[
        "Development Status :: 2 - Pre-Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Natural Language :: English",
        "Programming Language :: Python :: 3",
    ],
    description="Matching LIS to TecDoc",
    install_requires=requirements,
    tests_require=test_requirements,
    extras_require={
        "dev": test_requirements + development_requirements,
        "test": test_requirements,
    },
    entry_points={"console_scripts": ["olyslager=oly_matching.cli:cli"]},
    license="MIT license",
    long_description=readme,
    include_package_data=True,
    name="olyslager",
    packages=find_packages(include=["oly_matching", "oly_matching.*"]),
    version="0.0.0",
    zip_safe=False,
)


# 유임승차 비율이 가장 높은 역 찾기

import csv
file = open('C:/Users/yoona/OneDrive/Desktop/4차산업혁명 빅데이터/2주차/subway.csv', 'r')
data = csv.reader(file)
next(data)

mx = -1
# 비율 설정(%)
rate = 0.0
# 역 이름
mx_station = ''

# 반복문
for row in data:
    # 4에서 7까지의 수를 i라는 변수에 대입할 때
    for i in range(4, 8):

        # row 4부터 7까지 쉼표 삭제
        row[i] = int(row[i].replace(',', ''))

    # 만약 유임승차 + 무임승차 값이 0이면 계산하지 말고 다음 줄로 넘기기
    if row[4] + row[6] == 0:
        continue

    # 유임승차 / 유임승차 + 무임승차 => 승차인원 대비 유임승차 비율이 가장 높은 곳을 파악하기 위함
    rate = row[4] / (row[4] + row[6])

    # rate 값이 mx보다 크면 rate값을 mx에 대입한다.
    if rate > mx:
        mx = rate
        # 역 이름 + 호선 명으로 mx_station 값에 대입
        mx_station = row[3] + ' ' + row[1]

#mx*100으로 정수를 나타낸 후 소숫점 뒷자리가 2자리 이상이면 반올림(round)해주기
print(mx_station, round(mx*100, 2))


from app import app, db
from flask import render_template, flash, redirect, url_for
from app.forms import LoginForm
from app.models import Podcast, User, Episode, Comment, userPodcast, userEpisode
from flask_login import current_user, login_user, login_required, logout_user
from flask import request
from werkzeug.urls import url_parse
from app.forms import RegistrationForm
from app.forms import EmptyForm
from flask import jsonify
# g variable provided by Flask is a place where the application can store data that needs to persist through the life of a request
from flask import g
from app.forms import SearchForm

@app.before_request
def before_request():
    if current_user.is_authenticated:
        g.search_form = SearchForm()

@app.route('/')
@app.route('/index')
@login_required
def index():
    podcasts_featured = Podcast.query.filter_by(category_id=1)
    podcasts_daily = Podcast.query.filter_by(category_id=2)
    podcasts_news = Podcast.query.filter_by(category_id=3)
    return render_template('index.html', title='Home',
                           podcasts_featured=podcasts_featured,
                           podcasts_daily=podcasts_daily,
                           podcasts_news=podcasts_news)

@app.route('/podcast/<id>')
@login_required
def podcast_detail(id):
    podcast = Podcast.query.get(id)
    episodes = podcast.get_all_episodes()
    episode_id = []
    for i in range(episodes.__len__()):
        episode_id.append(episodes[i].id)
    form = EmptyForm()
    form_podcast_details = EmptyForm()
    return render_template('podcast.html', title='Podcast', podcast=podcast, episodes=episodes, form=form,
                           form_podcast_details=form_podcast_details,
                           episode_id=episode_id)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('index')
        return redirect(next_page)
    return render_template('login.html', title='Sign In', form=form)

@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))

@app.route('/register', methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)

@app.route('/follow/<id>', methods=['POST'])
@login_required
def follow(id):
    podcast = Podcast.query.filter_by(id=id).first()
    if podcast is None:
        flash('Podcast {} not found.'.format(id))
        return jsonify({'status': 'error'})
    current_user.follow(podcast)
    db.session.commit()
    return jsonify({'status': 'success'})

@app.route('/unfollow/<id>', methods=['POST'])
@login_required
def unfollow(id):
    podcast = Podcast.query.filter_by(id=id).first()
    if podcast is None:
        flash('Podcast {} not found.'.format(id))
        return jsonify({'status': 'error'})
    current_user.unfollow(podcast)
    db.session.commit()
    return jsonify({'status': 'success'})

@app.route('/user/<username>')
@login_required
def user(username):
    user = User.query.filter_by(username=username).first()
    podcasts = user.get_liked_podcasts()
    listened_episodes = user.get_listened_episodes()

    return render_template('user.html', user=user, podcasts=podcasts, listened_episodes=listened_episodes)

@app.route('/listen', methods=['POST'])
@login_required
# @cross_origin()
def episode_listened():
    user_id = request.form['userId']
    episode_id = request.form['episodeId']
    user = User.query.filter_by(id=user_id).first()
    episode = Episode.query.filter_by(id=episode_id).first()
    user.listen(episode)
    db.session.commit()
    return jsonify({'status': 'success'})

@app.route('/episode/<id>')
@login_required
def episode_detail(id):
    episode = Episode.query.get(id)
    comments = Comment.query.filter_by(episode_id=id).order_by(Comment.timestamp.desc())
    comments_with_users = []
    comment_number = 0
    for c in comments.all():
        comment_number = comment_number + 1
        replies_with_users = []
        user = User.query.get(c.user_id)
        replies = c.reply.all()
        for r in replies:
            comment_number = comment_number + 1
            reply_user = User.query.get(r.user_id)
            o = {'reply': r, 'user': reply_user}
            replies_with_users.append(o)

        comments_with_users.append({'comment': {'original': c, 'replies': replies_with_users}, 'user': user})

    photo_path = app.config['PHOTO_PATH_STATIC']
    return render_template('episode.html', title='Episode', episode=episode,
                           comments=comments, comments_with_users=comments_with_users,
                           current_user=current_user, comment_number=comment_number, photo_path=photo_path)

@app.route('/comments', methods=['POST'])
@login_required
# @cross_origin()
def comments():
    user_id = current_user.id
    episode_id = request.form['episodeId']
    message = request.form['message']
    comment = Comment(user_id=user_id, episode_id=episode_id, message=message)
    db.session.add(comment)
    db.session.commit()
    return jsonify({'status': 'success', 'commentId': comment.id})

@app.route('/reply', methods=['POST'])
@login_required
# @cross_origin()
def reply():
    user_id = current_user.id
    id = request.form['commentId']
    message = request.form['message']
    comment = Comment.query.filter_by(id=id)[0]
    reply = Comment(user_id=user_id, message=message)
    db.session.add(reply)
    comment.reply.append(reply)
    db.session.commit()
    return jsonify({'status': 'success'})

from app.forms import EditProfileForm
from werkzeug.utils import secure_filename
import os

@app.route('/edit_profile', methods=['GET', 'POST'])
@login_required
def edit_profile():
    form = EditProfileForm()
    if form.validate_on_submit():

        f = form.photo.data
        filename = secure_filename(f.filename)
        f.save(os.path.join(
            app.config['PHOTO_PATH'], filename
        ))
        current_user.username = form.username.data
        current_user.photo = f.filename
        db.session.commit()
        return redirect(url_for('user', username=current_user.username))
    elif request.method == 'GET':
        form.username.data = current_user.username
    return render_template('edit_profile.html', title='Edit Profile',
                           form=form)

@app.route('/my_feed')
@login_required
def my_feed():
    page = request.args.get('page', 1, type=int)
    podcast_with_episodes = current_user.get_podcast_with_episodes(page)

    if page == 1:
        return render_template('my_feed.html', podcast_with_episodes=podcast_with_episodes)

    l = []
    for x in podcast_with_episodes:
        l.append({'user': x.User.to_dict(),
                  'podcast_id': x.podcast_id,
                  'podcast_title': x.body,
                  'timestamp': x.timestamp,
                  'podcast_id': x.podcast_id,
                  'episode_id': x.id,
                  'episode_title': x.title,
                  'audio_link': x.audio_link,
                  'image': x.image,
                  'description': x.description
                  })
    return jsonify(l)

@app.route('/search')
@login_required
def search():
    if not g.search_form.validate():
        return redirect(url_for('index'))
    page = request.args.get('page', 1, type=int)
    users, total = User.search(g.search_form.q.data, page,
                               app.config['USERS_PER_PAGE'])
    next_url = url_for('search', q=g.search_form.q.data, page=page + 1) \
        if total > page * app.config['USERS_PER_PAGE'] else None
    prev_url = url_for('search', q=g.search_form.q.data, page=page - 1) \
        if page > 1 else None
    return render_template('search.html', title='Search', users=users,
                           next_url=next_url, prev_url=prev_url)

@app.route('/follow_user/<user_id>', methods=['POST'])
@login_required
def follow_user(user_id):
    user = User.query.filter_by(id=user_id).first()
    current_user.follow_user(user)
    db.session.commit()
    return jsonify({'status': 'success'})

@app.route('/unfollow_user/<user_id>', methods=['POST'])
@login_required
def unfollow_user(user_id):
    user = User.query.filter_by(id=user_id).first()
    current_user.unfollow_user(user)
    db.session.commit()
    return jsonify({'status': 'success'})

@app.route('/retweet', methods=['POST'])
@login_required
def retweet():
    episode_id = request.form['episodeId']
    episode = Episode.query.filter_by(id=episode_id).first()
    current_user.retweet_episode(episode)
    db.session.commit()
    return jsonify({'status': 'success'})

@app.route('/cancel_retweet', methods=['POST'])
@login_required
def cancel_retweet():
    episode_id = request.form['episodeId']
    episode = Episode.query.filter_by(id=episode_id).first()
    current_user.cancel_retweet_episode(episode)
    db.session.commit()
    return jsonify({'status': 'success'})

@app.route('/social_feed')
@login_required
def social_feed():
    page = request.args.get('page', 1, type=int)
    podcast_with_episodes = current_user.get_friends_tweeted_episodes(page)

    if page == 1:
        return render_template('social_feed.html', podcast_with_episodes=podcast_with_episodes)

    l = []
    for x in podcast_with_episodes:
        l.append({'user': x.User.to_dict(),
                  'podcast_id': x.podcast_id,
                  'podcast_title': x.body,
                  'timestamp': x.timestamp,
                  'podcast_id': x.podcast_id,
                  'episode_id': x.id,
                  'episode_title': x.title,
                  'audio_link': x.audio_link,
                  'image': x.image,
                  'description': x.description
                  })
    return jsonify(l)


from models.conn import userDataCollections
from service.JsonResponse import JsonResponse
from service.checkers.apiChecker import apiChecker
from service.checkers.commonChecker import pageChecker
from models.config import Config as SETTING
from service.checkers.movieChecker import getMovieObj

def deleteMovieById(apiKey, movieId):
    response = JsonResponse()

    try:
        data = []

        userObj = apiChecker(apiKey, response)
        if userObj:
            movieObj = getMovieObj(response, movieId)
        if movieObj:
            result = userDataCollections.delete_one(
                {
                    "userId" : userObj.get("_id"),
                    "movieId" : movieObj.get("_id"),
                }
            )
            if result.deleted_count:
                response.setStatus(200)
                response.setMessage("Deleted from watched list")
            else:
                response.setStatus(404)
                response.setMessage("We did not find this movie in watched list")

        response.setData(data)
    except Exception as e:
        response.setStatus(500) # Internal error
        response.setError("Error in deleting movie from watched list Contact Mr. Grey => " + str(e))
        # logConfig.logError("Error in fetching a content  => " + str(e))
    finally:
        return response.returnResponse()


# -*- coding: utf-8 -*-
"""
Created on Tue May 02 10:50:18 2017

@author: Kun
"""

#trailpy

import random
def makeVset(n):
    N = set()
    for j in range(2,n+1):
            for i in range(1,j):
                if i<j:
                    N.add((i,j))

    R = set(range(4,n+1))
    return [R, N]

def get_pedigree(n):
	#R = set([range(4,n+1)])
    N = set(((1,2),(1,3),(2,3)))
    P = set()
    for i in xrange(4, n+1):
        random_node = random.sample(N,1)
        P.add((random_node[0], i))
        if i > random_node[0][0]:
            nodeInsert1 = (random_node[0][0], i)
        else:
            nodeInsert1 = (i, random_node[0][0])

        if i > random_node[0][1]:
            nodeInsert2 = (random_node[0][1], i)
        else:
            nodeInsert2 = (i, random_node[0][1])

        N.add(nodeInsert1)
        N.add(nodeInsert2)
        N.remove(random_node[0])

    return P, N

def get_slack(P_0, N, n):
    slack_set = set()
    for j in range(2,n+1):
        for i in range(1, n):
            if i<j and (i,j) not in N and (i,j) not in P_0:
                slack_set.add((i, j))
    return slack_set

def makeE_Tset(n):
    Pdummy = set()
    P, N = get_pedigree(n)
    for item in P:
        Pdummy.add(item[0])
    s = get_slack(Pdummy, N, n)
    E_T = set()
    E_T.update(P)
    for i in N:
        E_T.add((i, None))
    for i in s:
        E_T.add((i, None))

    return E_T, s, Pdummy, P

#this needs to be fixed

"""
THIS NEEDS TO BE FIXED

This does not consider all E

All e needs to include the Uij set as well

E = Xijk U uij

WILL WORK FOR NOW I GUESS
"""
def makeE_Xset(n, E_T, P):
    E_X = set()
    kth = set()
    ijth = set()
    for k in range(4,n+1):
        for j in range(2,n+1):
                for i in range(1,j):
                    if i<j and j<k:
                        if ((i,j),k) not in E_T and (i,j) not in P:
                            if len(E_X) < n - 3:
                                if k in kth:
                                    continue
                                else:
                                    if (i,j) in ijth:
                                        continue
                                    else:
                                        E_X.add(((i,j),k))
                                        kth.add(k)
                                        ijth.add((i,j))
                                        break
                            else:
                                return E_X


import socket
import os

from recvall import recv_msg, send_msg

address = ('', 8787)
# sock = socket.socket()
# sock.bind(address)
# sock.listen(1)
with socket.create_server(address) as sock:
    conn, addr = sock.accept()
    print('Connected: ', addr)
    try:
        data = recv_msg(conn).decode()
        print(data)
        send_msg(conn, data.upper().encode())
        send_msg(conn, str(os.listdir('/_shared/')).encode())
    finally:
        conn.close()
        print("Connection closed: ", addr)


import pandas as pd
from pycirclize import Circos
import sys

num = int(sys.argv[1])

#means 3 sections will be feeding into 8 sections
french_names = ["Dillygence", "HIVE Electric", "Numalliance", "OSE Group",
             "SIC Marketing", "SpooN", "SYSGO", "TRA-C Industrie"]
us_names = ["Borgwarner", "Magna", "Forvia", "Eaton",
            "Steris", "Invacare", "LubriZol", "GoodYear"]

#3 x 8 matrix
matrix_data = [
    [1, 1, 1, 0, 1, 1, 1, 1],
    [1, 1, 1, 1, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 1],
    [1, 1, 1, 0, 0, 0, 1, 1],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
]

matrix_df = pd.DataFrame(matrix_data, index=us_names, columns=french_names)

#Initialize Circos from matrix for plotting Chord Diagram
circos = Circos.initialize_from_matrix(
    matrix_df,
    space = 8,
    cmap = "tab10",
    label_kws=dict(size=12),
    link_kws=dict(ec="black", lw=0.5, direction=-1),
)

circos.savefig("example0{}.png".format(num))


"""change pettifor_number type to int

Revision ID: 3fa9c7ae3440
Revises: 08ca43d1d700
Create Date: 2019-08-26 11:08:29.867332

"""

# revision identifiers, used by Alembic.
revision = "3fa9c7ae3440"
down_revision = "08ca43d1d700"
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa

def upgrade():

    with op.batch_alter_table("elements") as batch_op:
        batch_op.alter_column("pettifor_number", type_=sa.Integer)

def downgrade():

    with op.batch_alter_table("elements") as batch_op:
        batch_op.alter_column("pettifor_number", type_=sa.Float)


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="nansi",
    # SEE   https://www.python.org/dev/peps/pep-0440/
    version="0.1.2a1",
    author="nrser",
    author_email="neil@neilsouza.com",
    description="Nrser ANSIble",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://nrser.com/nansi",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: BSD License",
        "Operating System :: POSIX",
    ],
    python_requires='>=3.8',
    install_requires=[
        'ansible>=2.9.4,<3',
        'typeguard>=2.9.1,<3',
        'splatlog>=0.1.0',
    ],
    license = "BSD License",
    license_files=[
        "LICENSE",
    ],
    platforms = [
        "POSIX",
    ],
)


import os
import logging

from yaml import safe_load, safe_dump

LOG = logging.getLogger(__name__)

DEFAULT_KUBECONFIG = '~/.kube/config'
# the piece of kubeconfig we care about
KUBECONFIG_REPEATABLES = ['clusters', 'users', 'contexts']

class Kubeconfig(object):
    """ High level class to describe operations on kubeconfigs """
    def __init__(self, kcfg_path=None, kcfg_yaml=None, kcfg=None):
        self.kubeconfig_path = self.determine_location(kcfg_path)
        if kcfg:
            # User provided us with the kubeconfig
            LOG.debug('Using user provided Kubeconfig')
            self.kubeconfig = kcfg
        else:
            # Attempt to load from yaml or file
            existing_kubeconfig = self.read()

            # load the kubeconfig from string
            if kcfg_yaml:
                loaded_kcfg = safe_load(kcfg_yaml)
                if existing_kubeconfig != {}:
                    LOG.warn('kubeconfig already exists at %s. It will be overwritten', self.kubeconfig_path)
                LOG.debug('Using provided yaml as kubeconfig')
                self.kubeconfig = loaded_kcfg
            else:
                LOG.debug('Using kubeconfig as loaded from file')
                self.kubeconfig = existing_kubeconfig

    def __eq__(self, other):
        # TODO: This only checks that the NAMES are the same. This doesn't catch when the content is
        #       different but the names are the same.
        if isinstance(other, self.__class__):
            # check that both objects have the same clusters, contexts, & users
            for item in KUBECONFIG_REPEATABLES:
                names = [c['name'] for c in self.kubeconfig[item]]
                incoming_names = [c['name'] for c in other.kubeconfig[item]]
                if set(names) != set(incoming_names):
                    return False
            return True
        else:
            return False

    def read(self):
        """ Loads the current kubeconfig from file """
        if not os.path.isfile(self.kubeconfig_path):
            LOG.debug('Kubeconfig not found at %s', self.kubeconfig_path)
            return {}
        else:
            with open(self.kubeconfig_path) as kcfg_f:
                LOG.debug('Reading kubeconfig at %s', self.kubeconfig_path)
                return safe_load(kcfg_f)

    def save(self):
        """ Saves the current kubeconfig to file """
        kcfg_dir = os.path.dirname(self.kubeconfig_path)
        LOG.debug('saving to %s' % kcfg_dir)
        if kcfg_dir and not os.path.exists(kcfg_dir):
            os.makedirs(kcfg_dir)

        # File has not be created yet
        with open(self.kubeconfig_path, "w+") as kcfg_f:
            kcfg_f.write(safe_dump(self.kubeconfig))

    def determine_location(self, kcfg_path=None):
        """ Identifies which kubeconfig is currently to be used.

        This will load the kubeconfig from the following locations in this precedence order:
        - specified in '--kubeconfig' flag
        - specified in $KUBECONFIG environment variable
        - home directory location ~/.kube/config

        Returns:
            The identified kubeconfig file to use
        """
        kubeconfig_env = None
        try:
            kubeconfig_env = os.environ['KUBECONFIG']
        except KeyError:
            pass  # :its_fine:

        # Determine
        kubeconfig_path = None
        if kcfg_path:
            kubeconfig_path = kcfg_path
        elif kubeconfig_env:
            kubeconfig_path = kubeconfig_env
        else:
            kubeconfig_path = DEFAULT_KUBECONFIG

        # Clean it up
        kubeconfig_path = os.path.expanduser(kubeconfig_path)
        kubeconfig_path = os.path.expandvars(kubeconfig_path)

        return kubeconfig_path

    def organize_kubeconfig(self, cluster, cloud_fqdn, cloud_username):
        """ Organizes the KubeConfig so it can be safely merged with other KubeConfigs

        `user` is renamed to `fqdn-username` to allow for unique keystone environments

        `context` is renamed to `cluster_name`

        `cluster` is renamed to `cluster_uuid`

        Args:
            cluster: dictionary with 'uuid' and 'name' as keys
            cloud_fqdn: string representing the cloud's fqdn
            cloud_username: string representing the cloud user's username
        """
        # change cluster name to cluster_uuid
        self.kubeconfig['clusters'][0]['name'] = cluster['uuid']
        self.kubeconfig['contexts'][0]['context']['cluster'] = cluster['uuid']
        # change context name to cluster name
        self.kubeconfig['contexts'][0]['name'] = cluster['name']
        # change user to fqdn-username
        new_username = '{}-{}'.format(cloud_fqdn, cloud_username)
        self.kubeconfig['users'][0]['name'] = new_username
        self.kubeconfig['contexts'][0]['context']['user'] = new_username

    def merge_kubeconfigs(self, new_kubeconfig):
        """ Soft merges two kubeconfig files.
        If name matches for cluster, context, or user the new_kubeconfig will be preferred

        Args:
            new_kubeconfig: A Kubeconfig object to merge into this one

        Returns:
            The merged kubeconfig dictionary
        """

        LOG.debug('Current kubeconfig:\n%s', self.kubeconfig)
        LOG.debug('Incoming kubeconfig:\n%s', new_kubeconfig.kubeconfig)
        if not self.kubeconfig:
            LOG.debug('Source is empty, no merging required')
            # it's a fresh kubeconfig! no need to merge anything
            self.kubeconfig = new_kubeconfig.kubeconfig
            return self.kubeconfig

        result = self.kubeconfig
        for category in KUBECONFIG_REPEATABLES:
            incoming_list = new_kubeconfig.kubeconfig[category]

            # merge based on the key 'name'
            for inc in incoming_list:
                merged = False
                for index, item in enumerate(result[category]):
                    if item['name'] == inc['name']:
                        LOG.debug('Item %s found in %s. Overwriting', inc['name'], category)
                        result[category][index] = inc
                        merged = True
                if not merged:
                    LOG.debug('Item %s not found in %s. Appending', inc['name'], category)
                    result[category].append(inc)
        LOG.debug('After merge:\n%s', result)
        self.kubeconfig = result

        return self.kubeconfig


import argparse
import os

from adult_income import get_adult_income
from compas import get_compas
from default_credit import get_default_credit
from marketing import get_marketing

# parser initialization
parser = argparse.ArgumentParser(description='Script for datasets preprocessing')

parser.add_argument('--dataset', type=str, default='adult_income', help='Dataset: adult_income, compas, default_credit, marketing')
args = parser.parse_args()

dataset = args.dataset

if dataset == 'adult_income':
    get_adult_income(save_df=True)

elif dataset == 'compas':
    get_compas(save_df=True)

elif dataset == 'default_credit':
    get_default_credit(save_df=True)

elif dataset == 'marketing':
    get_marketing(save_df=True)

else:
    print('This dataset is not handled for the moment')


import sys
from tech_news.scraper import get_tech_news
from tech_news.analyzer.ratings import top_5_categories
from tech_news.analyzer.search_engine import (
    search_by_title,
    search_by_date,
    search_by_category,
)

# Requisitos 11 e 12
def option_0():
    print("Digite quantas notícias serão buscadas:")
    number = input()
    get_tech_news(int(number))

def option_1():
    print("Digite o título:")
    title = input()
    print(search_by_title(title))

def option_2():
    print("Digite a data no formato aaaa-mm-dd:")
    date = input()
    print(search_by_date(date))

def option_3():
    print("Digite a categoria:")
    category = input()
    print(search_by_category(category))

def option_4():
    print(top_5_categories())

def option_5():
    print("Encerrando script")

funcs = [option_0, option_1, option_2, option_3, option_4, option_5]

def analyzer_menu():
    print(
        "Selecione uma das opções a seguir:\n "
        "0 - Popular o banco com notícias;\n "
        "1 - Buscar notícias por título;\n "
        "2 - Buscar notícias por data;\n "
        "3 - Buscar notícias por categoria;\n "
        "4 - Listar top 5 categorias;\n "
        "5 - Sair.",
        end="",
    )
    option = input()

    try:
        funcs[int(option)]()
    except (IndexError, ValueError):
        print("Opção inválida", file=sys.stderr)


# pyright: strict
from typing import ClassVar

#  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Inheritance
#  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Inheritance allows us to inherit attributes and methods from a parent class
# We can create sub classes and get all the fuctionality of our parent class
# and we can overwrite or add new functionality without effecting our parent class.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Employee:

    raise_amt: ClassVar[float] = 1.04  # %4

    def __init__(self, first: str, last: str, pay: int) -> None:
        self.first: str = first
        self.last: str = last
        self.pay: int = pay
        self.email: str = f'{first}.{last}@company.com'

    def fullname(self) -> str:
        return f'{self.first} {self.last}'

    def apply_raise(self) -> None:
        self.pay = int(self.pay * self.raise_amt)

# Now, we want our developers get %10 raise
class Developer(Employee):
    # overwriting the parent
    raise_amt: ClassVar[float] = 1.10

dev_1: Developer = Developer('Surush', 'Cyrus', 50000)
dev_2: Developer = Developer('Steve', 'Brown', 60000)

print(f'dev_1 --> First Name: {dev_1.first}, Pay: {dev_1.pay}')
dev_1.apply_raise()
print(f'dev_1 --> First Name: {dev_1.first}, Pay: {dev_1.pay}')

# Only dev_1 gets raise
print(f'dev_2 --> First Name: {dev_2.first}, Pay: {dev_2.pay}')


import xlsxwriter, json, os
import numpy as np
import itertools

"""Accept an array of tolsteps and write them as parallel columns in a table.
Labels is an array of additional information to include. """
def add_step_tolerances(book, sheet, st, row, col, labels):
    bold_format = book.add_format({'bold': True, 'align': 'right'})

    #add labels for this set of tolerance increments
    for (i, label) in enumerate(labels):
        sheet.write(row+i, col, label, bold_format)

    col += 1

    #add labels for data attributes
    for (i, label) in enumerate(["tolerance", "CPLEX Objective", "True Objective", "Infeasibility", "Solution status", "termination reason", "elapsed time", "gap"]):
        sheet.write(row+i, col, label + ":", bold_format)

    #write data
    for (i, trial_step) in enumerate(st):
        for (j, key) in enumerate(["tolerance", "cplex_objective", "objective", "infeasibility", "solution_status", "termination_status", "elapsed_time", "gap"]):
            sheet.write(row+j, col+i+1, trial_step[key])

    #return row and column moved to bottom right of where data was written
    return (row + 9, col + len(st))

"""
Accept a dictionary mapping method names to synced vectors of tolerance gaps.
This method will add a table to compare the gaps both naively, and scaled to
a base case.
"""
def add_method_gap_comparisons(book, sheet, method_results, base_case, title, row, col):
    bold_format = book.add_format({'bold': True, 'align': 'right'})

    scale_method = lambda special, base: (special - base) / base

    #write title
    sheet.write(row, col, title, bold_format)
    row += 1

    #write table headers
    sheet.write(row, col, "method", bold_format)
    sheet.write(row, col+1, "average", bold_format)
    sheet.write(row, col+2, "std dev", bold_format)
    sheet.write(row, col+3, "scaled average", bold_format)
    sheet.write(row, col+4, "scaled std dev", bold_format)

    i = 1
    for (method, results) in method_results.items():
        average = np.mean(results)
        deviation = np.std(results)
        scaled_results = [scale_method(result, method_results[base_case][i])
            for (i, result) in enumerate(results)]
        scaled_average = np.mean(scaled_results)
        scaled_std = np.std(scaled_results)

        sheet.write(row+i, col, method)
        sheet.write(row+i, col+1, str(average))
        sheet.write(row+i, col+2, str(deviation))
        sheet.write(row+i, col+3, str(scaled_average))
        sheet.write(row+i, col+4, str(scaled_std))
        i += 1
    return row + i, col + 4

def create_book(filename):
    return xlsxwriter.Workbook(filename)

def save_book(book):
    book.close()


from typing import List

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import (check_charity_project_before_delete,
                                check_charity_project_before_edit,
                                check_name_duplicate)
from app.core.db import get_async_session
from app.core.user import current_superuser
from app.crud.charity_project import charity_project_crud
from app.models import CharityProject
from app.schemas.charity_project import (CharityProjectCreate,
                                         CharityProjectDB,
                                         CharityProjectUpdate)
from app.services.transaction import transaction

router = APIRouter()

@router.post(
    '/',
    response_model=CharityProjectDB,
    response_model_exclude_none=True,
    dependencies=[Depends(current_superuser)],
)
async def create_new_charity_project(
        project: CharityProjectCreate,
        session: AsyncSession = Depends(get_async_session)
) -> CharityProject:
    await check_name_duplicate(project.name, session)
    new_project = await charity_project_crud.create(project, session)
    investment = await transaction(new_project, session)
    if investment:
        session.add_all([*investment, new_project])
        await session.commit()
        await session.refresh(new_project)
    return new_project

@router.get(
    '/',
    response_model=List[CharityProjectDB],
    response_model_exclude_none=True
)
async def get_all_charity_projects(
        session: AsyncSession = Depends(get_async_session)
) -> List[CharityProject]:
    return await charity_project_crud.get_multi(session)

@router.patch(
    '/{project_id}',
    response_model=CharityProjectDB,
    dependencies=[Depends(current_superuser)]
)
async def partially_update_project(
        project_id: int,
        obj_in: CharityProjectUpdate,
        session: AsyncSession = Depends(get_async_session)
) -> CharityProjectDB:
    new_project = await check_charity_project_before_edit(project_id, obj_in, session)
    if obj_in.name is not None:
        await check_name_duplicate(obj_in.name, session)
    new_project = await charity_project_crud.update(new_project, obj_in, session)
    return new_project

@router.delete(
    '/{project_id}',
    response_model=CharityProjectDB,
    dependencies=[Depends(current_superuser)]
)
async def remove_project(
        project_id: int,
        session: AsyncSession = Depends(get_async_session)
) -> CharityProject:
    project = await check_charity_project_before_delete(project_id, session)
    return await charity_project_crud.remove(project, session)


import json
import numpy as np

from src.utils import get_full_sample_paths, load_numpy, get_stratified_split
from src.preprocessing import apply_scaling_per_channel

import tensorflow as tf

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import CategoricalCrossentropy

from sklearn import metrics

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')  # !IMPORTANT

f = open('onehot_info.json')
label_index_info = json.load(f)
data_directory = 'data'

channels = ['Iz','Fp1', 'AF7', 'AF3', 'F1', 'F3', 'F5',
            'F7', 'FT7', 'FC5', 'FC3', 'FC1', 'C1',
            'C3', 'C5', 'T7', 'TP7', 'CP5', 'CP3',
            'CP1', 'P1', 'P3', 'P5', 'P7', 'P9', 'PO7',
            'PO3', 'O1', 'Oz', 'POz', 'Pz', 'CPz', 'Fpz',
            'Fp2', 'AF8', 'AF4', 'AFz', 'Fz', 'F2', 'F4', 'F6',
            'F8', 'FT8', 'FC6', 'FC4', 'FC2', 'FCz', 'Cz',
            'C2', 'C4', 'C6','T8', 'TP8', 'CP6', 'CP4', 'CP2',
            'P2', 'P4', 'P6', 'P8', 'P10', 'PO8', 'PO4']

indexes = []
keep = ['C3', 'Cz', 'C2', 'FC2']
for i in keep:
    indexes.append(channels.index(i))

if __name__ == '__main__':
    # Tuples with path and label index
    full_paths = get_full_sample_paths(data_directory)
    X, y = load_numpy(full_paths, label_index_info)
    X = X[:, indexes, :]

    dict_sets = get_stratified_split(X, y, 4,
                                     train_size=0.8,
                                     val_size=0.1,
                                     test_size=0.1,
                                     random_state=0,
                                     one_hot=True)

    out = list(map(lambda d: apply_scaling_per_channel(d[0], d[1], d[2]),
                   zip(np.swapaxes(dict_sets['train'][0], 0, 1),
                       np.swapaxes(dict_sets['val'][0], 0, 1),
                       np.swapaxes(dict_sets['test'][0], 0, 1))))

    out = list(zip(*out))
    dict_sets['train'][0] = np.swapaxes(np.array(out[0]), 0, 1).astype(np.float64)
    dict_sets['val'][0] = np.swapaxes(np.array(out[1]), 0, 1).astype(np.float64)
    dict_sets['test'][0] = np.swapaxes(np.array(out[2]), 0, 1).astype(np.float64)

    dddddd
    dict_sets['train'][0] = np.mean(dict_sets['train'][0], 1)
    dict_sets['val'][0] = np.mean(dict_sets['val'][0], 1)
    dict_sets['test'][0] = np.mean(dict_sets['test'][0], 1)
    print(dict_sets['train'][0].shape)
    gg
    batch_size = 48

    # Prepare the train dataset.
    train_dataset = tf.data.Dataset.from_tensor_slices((dict_sets['train'][0],
                                                        dict_sets['train'][1]))
    train_dataset = train_dataset.shuffle(buffer_size=21).batch(batch_size)

    # Prepare the validation dataset.
    val_dataset = tf.data.Dataset.from_tensor_slices((dict_sets['val'][0],
                                                      dict_sets['val'][1]))
    val_dataset = val_dataset.batch(batch_size)

    # Prepare the test dataset.
    test_dataset = tf.data.Dataset.from_tensor_slices((dict_sets['test'][0],
                                                       dict_sets['test'][1]))
    test_dataset = test_dataset.batch(batch_size)

    print('Final input shape ', dict_sets['train'][0].shape)

    model = keras.Sequential()
    model.add(
        keras.layers.Bidirectional(
          keras.layers.LSTM(
              units=128,
              input_shape=[dict_sets['train'][0].shape[1], dict_sets['train'][0].shape[2]]
          )
        )
    )
    model.add(keras.layers.Dropout(rate=0.5))
    model.add(keras.layers.Dense(units=128, activation='relu'))
    model.add(keras.layers.Dense(4, activation='softmax'))

    # # Instantiate an optimizer.
    optimizer = Adam(learning_rate=1e-3)

    # Instantiate a loss function.
    loss_fn = CategoricalCrossentropy(label_smoothing=0.001)
    model.compile(loss=loss_fn, optimizer=optimizer, metrics=['categorical_accuracy'])

    # Train the model
    model.fit(train_dataset, validation_data=val_dataset, epochs=100)

    outs = model.evaluate(test_dataset)
    print('Loss, Accuracy', outs)

    print('Classification report')
    print(model.predict(dict_sets['test'][0]))
    print(metrics.classification_report(np.argmax(dict_sets['test'][1], 1),
                                        np.argmax(model.predict(dict_sets['test'][0]), 1)
                                        ))

    cf = metrics.confusion_matrix(np.argmax(dict_sets['test'][1], 1),
                                  np.argmax(model.predict(dict_sets['test'][0]), 1))
    cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix=cf)

    cm_display.plot()
    plt.show()


from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal
from PyQt5.QtGui import QKeySequence
from PyQt5.QtNetwork import QHostInfo
from PyQt5.QtWidgets import QWidget, QMessageBox
from QCandyUi.CandyWindow import colorful

from aop.DialogAOP import DialogAOP
from comm.ICommunicateTCPIP import ICommunicateTCPIP
from comm.SerialPort import SerialPort, baudList
from comm.TcpServer import TcpServer
from comm.TcpSocketClient import TcpSocketClient
from comm.UdpSocket import UdpSocket
from help.DialogHelp import DialogHelp
from ui_tcpUdpSerialportTool import Ui_TcpUdpComTool
from util import global_const
from util.ConfigFile import ConfigFile
from util.DataOperate import *
from util.global_const import CONFIG_FILE_PATH

"""
AOP example:
    self.udpSocket.send('hello', '192.168.75.111', 7777)
    print(self.current_rec_data)

"""

# @装饰器pre_send_decorate
def pre_send_decorate(func):
    def send_new(self, *args, **kwargs):
        try:
            exec(self.pre_send_code)
        except Exception as e:
            print(e)
        func(self, *args, **kwargs)

    return send_new

# @装饰器post_rec_decorate
def post_rec_decorate(func):
    def rec_new(self, *args, **kwargs):
        func(self, *args, **kwargs)
        try:
            exec(self.post_rec_code)
        except Exception as e:
            print(e)

    return rec_new

class ConnectMode(Enum):
    UDP_MODE = 0
    TCP_CLIENT_MODE = 1
    TCP_SERVER_MODE = 2
    SERIAL_PORT_MODE = 3

@colorful('blueGreen')
class TcpUdpSerialPortTool(QWidget):
    WIN_WIDTH = 655
    WIN_HEIGHT = 486

    ClientReadData = pyqtSignal(int, str, int, bytearray)
    ClientDisConnect = pyqtSignal(int, str, int)
    send_data = None
    pre_send_code = str()
    post_rec_code = str()

    def __init__(self):
        QWidget.__init__(self)
        self.widgetui = Ui_TcpUdpComTool()
        self.widgetui.setupUi(self)
        self.myconfig = ConfigFile(CONFIG_FILE_PATH)
        self.comboboxInit()
        self.configDataInit()
        self.__init_aop_files()
        self.widgetui.pushButtonSend.setEnabled(False)
        self.widgetui.pushButtonSend.setShortcut(
            QKeySequence("Ctrl+Return"))  # 快捷键, 也可以在QtDesign直接填
        self.setFocusPolicy(Qt.ClickFocus)  # 随便点击便聚焦
        self.current_rec_data = ''
        self.init_all_connections()

    def closeEvent(self, e):
        self.configDataSave()

    def __init_aop_files(self):
        with open(global_const.PRE_SEND_FILE_PATH) as f1:
            self.pre_send_code = f1.read()
        with open(global_const.POST_REC_FILE_PATH) as f2:
            self.post_rec_code = f2.read()

    def configDataInit(self):
        # 发送状态读取
        if int(self.myconfig.getValue('sendStatus') or 0) == HexOrChar.CHAR_STYLE.value:
            self.widgetui.radioButtonString.click()
        else:
            self.widgetui.radioButton16.click()
        if self.myconfig.getValue('recStatus') == HexOrChar.CHAR_STYLE.value:
            self.widgetui.checkBoxHexRec.setChecked(False)
        else:
            self.widgetui.checkBoxHexRec.setChecked(True)
        self.widgetui.comboBoxBaud.setCurrentText(
            str(self.myconfig.getValue("baudValue")))
        self.oldcomNum = str(self.myconfig.getValue("comNum"))
        self.widgetui.lineEditIpAim.setText(
            str(self.myconfig.getValue("aimIp")))
        self.widgetui.lineEditPortLocal.setText(
            str(self.myconfig.getValue("srcPort")))
        self.widgetui.lineEditPortAim.setText(
            str(self.myconfig.getValue("dstPort")))

    def configDataSave(self):
        self.myconfig.setValue("sendStatus", self.getRadioButtStat().value)
        self.myconfig.setValue("recStatus", self.getCheckBoxStat().value)
        self.myconfig.setValue("baudValue", self.widgetui.comboBoxBaud.currentText())
        self.myconfig.setValue(
            "srcPort", self.widgetui.lineEditPortLocal.text())
        self.myconfig.setValue("dstPort", self.widgetui.lineEditPortAim.text())
        self.myconfig.setValue("aimIp", self.widgetui.lineEditIpAim.text())
        self.myconfig.setValue("hostIp", self.widgetui.comboBoxLocal.currentText())
        if None != self.widgetui.comboComNum.currentText():
            self.myconfig.setValue("comNum", self.widgetui.comboComNum.currentText())

    def comboboxInit(self):
        # 工具选择框combox
        self.widgetui.comboBoxStyle.insertItem(0, "UDP")
        self.widgetui.comboBoxStyle.insertItem(1, "TCP Client")
        self.widgetui.comboBoxStyle.insertItem(2, "TCP Server")
        self.widgetui.comboBoxStyle.insertItem(3, "COM Tool")
        self.connectModeList = list()
        self.connectModeList.append('UDP')
        self.connectModeList.append('TCP Client')
        self.connectModeList.append('TCP Server')
        self.connectModeList.append('COM Tool')
        self.widgetui.comboBoxStyle.activated.connect(self.comboxClicked)
        # 波特率combox
        self.widgetui.comboBoxBaud.insertItems(0, baudList)
        self.widgetui.comboBoxBaud.setCurrentText("9600")
        self.widgetui.comboBoxBaud.hide()
        self.widgetui.comboComNum.hide()
        self.widgetui.comboBoxClientList.hide()  # tcp的server专用的组合框
        # 本地IP combobox
        self.myhostInfo = QHostInfo.fromName(QHostInfo.localHostName())
        hostIpList = ICommunicateTCPIP.getLocalIpList()
        if '127.0.0.1' not in hostIpList:
            hostIpList.append('127.0.0.1')
        oldHostIp = str(self.myconfig.getValue("hostIp"))
        tempHostIp = str()
        for ip in hostIpList:
            self.widgetui.comboBoxLocal.addItem(ip)
            if oldHostIp == ip:
                tempHostIp = ip
        self.widgetui.comboBoxLocal.setCurrentText(tempHostIp)
        self.myaddress = self.widgetui.comboBoxLocal.currentText()

    def init_all_connections(self):
        self.udpSocket = UdpSocket(self)
        self.tcpSocketClient = TcpSocketClient(self)
        self.serialPort = SerialPort()
        self.tcpServer = TcpServer(self)

    def connectUDP(self):
        self.udpSocket = UdpSocket(self)
        self.udpSocket.createConnection(self.myaddress, int(
            self.widgetui.lineEditPortLocal.text()))
        self.udpSocket.connectRec(self.dataReceivedUDP)
        self.widgetui.pushButtonSend.setEnabled(True)
        self.widgetui.labelStatus.setText('Udp创建OK')

    def connectTCPclient(self):
        self.tcpSocketClient = TcpSocketClient(self)
        self.tcpSocketClient.connectRec(self.dataReceivedTCP_client)
        isConnectOK = self.tcpSocketClient.createConnection(self.widgetui.lineEditIpAim.text(),
                                                            int(self.widgetui.lineEditPortAim.text()))
        if isConnectOK:
            self.widgetui.lineEditIpAim.setEnabled(False)
            self.widgetui.lineEditPortAim.setEnabled(False)
            self.widgetui.labelStatus.setText("TCP客户端创建OK")
        else:
            QMessageBox.information(self, "error", "连接失败")
            return
        self.widgetui.pushButtonSend.setEnabled(True)

    def connectTCPserver(self):
        self.tcpServer = TcpServer(self)
        self.tcpServer.createConnection(
            int(self.widgetui.lineEditPortLocal.text()))
        # 有TCPclient连接上server时
        self.tcpServer.connectClientConnected(self.updateClientList)
        # 当server收到client的发来的数据时
        self.tcpServer.connectClientDisconnected(self.disconnectTCPlist)
        # 有TCPclient断开连接时
        self.tcpServer.connectRec(self.dataReceivedTCP_server)
        self.widgetui.lineEditIpAim.setEnabled(False)
        self.widgetui.lineEditPortAim.setEnabled(False)
        self.widgetui.pushButtonSend.setEnabled(False)
        self.widgetui.comboBoxClientList.show()
        self.widgetui.labelStatus.setText("TCP服务端创建OK")

    def connectCOM(self):
        self.serialPort = SerialPort()
        self.serialPort.connectRec(self.comReadData)
        isOpenSuccess = self.serialPort.openPort(self.widgetui.comboComNum.currentText(),
                                                 int(self.widgetui.comboBoxBaud.currentText()))
        if isOpenSuccess:
            self.widgetui.labelStatus.setText(
                self.serialPort.getCurPortName() + " OK")
            self.widgetui.pushButtonStart.setText("断开")
            self.widgetui.pushButtonSend.setEnabled(True)
            self.widgetui.comboComNum.setEnabled(False)
            self.widgetui.comboBoxBaud.setEnabled(False)
        else:
            self.widgetui.labelStatus.setText(
                self.serialPort.getCurPortName() + " Error")

    def getRadioButtStat(self):
        if self.widgetui.radioButton16.isChecked():
            return HexOrChar.HEX_STYLE
        if self.widgetui.radioButtonString.isChecked():
            return HexOrChar.CHAR_STYLE
        return HexOrChar.CHAR_STYLE

    def getCheckBoxStat(self):
        if self.widgetui.checkBoxHexRec.isChecked():
            return HexOrChar.HEX_STYLE
        else:
            return HexOrChar.CHAR_STYLE

    @pyqtSlot(str)
    def disconnectTCPlist(self, fullAddrStr):
        removeIndex = self.widgetui.comboBoxClientList.findText(
            fullAddrStr)  # 找出文字对应的index
        self.widgetui.comboBoxClientList.removeItem(removeIndex)  # 根据index移除该项
        if self.widgetui.comboBoxClientList.count() == 0:  # client列表为空时, 不允许发送
            self.widgetui.pushButtonSend.setEnabled(False)

    @pyqtSlot(str)
    def updateClientList(self, fullAddrStr):
        if self.widgetui.comboBoxClientList.count() == 0:
            self.tcpServer.setCurClient(fullAddrStr)
        self.widgetui.comboBoxClientList.addItem(fullAddrStr)
        self.widgetui.pushButtonSend.setEnabled(True)

    @pyqtSlot()
    def dataReceivedUDP(self):
        data = self.udpSocket.read()
        self.recByStyle(data)

    @pyqtSlot()
    def dataReceivedTCP_client(self):
        data = self.tcpSocketClient.read()
        self.recByStyle(data)

    @pyqtSlot(str)
    def dataReceivedTCP_server(self, recFromFullAddr):
        uiCurClientFullAddr = self.widgetui.comboBoxClientList.currentText()
        # tcpServer.setCurClient(uiCurClientFullAddr)
        # 过滤掉其他非comboBoxClientList当前Client的接收数据
        if uiCurClientFullAddr == recFromFullAddr:
            data = self.tcpServer.read()
            self.recByStyle(data)

    @pyqtSlot()
    def comReadData(self):
        data = self.serialPort.read()
        self.recByStyle(data)

    @pyqtSlot()
    def on_pushButtonStart_clicked(self):
        # -------------------连接------------------
        if self.widgetui.pushButtonStart.text() == "连接":
            self.widgetui.pushButtonStart.setText("断开")
            self.widgetui.comboBoxLocal.setEnabled(False)
            self.widgetui.lineEditPortLocal.setEnabled(False)
            self.widgetui.comboBoxStyle.setEnabled(False)
            curMode = self.connectModeList.index(
                self.widgetui.comboBoxStyle.currentText())
            if curMode == ConnectMode.UDP_MODE.value:
                self.connectUDP()
            elif curMode == ConnectMode.TCP_CLIENT_MODE.value:
                self.connectTCPclient()
            elif curMode == ConnectMode.TCP_SERVER_MODE.value:
                self.connectTCPserver()
            elif curMode == ConnectMode.SERIAL_PORT_MODE.value:
                self.connectCOM()
        # -------------------断开------------------
        elif self.widgetui.pushButtonStart.text() == "断开":
            curMode = self.connectModeList.index(
                self.widgetui.comboBoxStyle.currentText())
            if curMode == ConnectMode.UDP_MODE.value:
                self.udpSocket.close()
            elif curMode == ConnectMode.TCP_CLIENT_MODE.value:
                self.tcpSocketClient.close()
                self.widgetui.lineEditIpAim.setEnabled(True)
                self.widgetui.lineEditPortAim.setEnabled(True)
            elif curMode == ConnectMode.TCP_SERVER_MODE.value:
                self.tcpServer.close()
                self.widgetui.comboBoxClientList.clear()
                self.widgetui.comboBoxClientList.hide()
                self.widgetui.lineEditIpAim.setEnabled(True)
                self.widgetui.lineEditPortAim.setEnabled(True)
            elif curMode == ConnectMode.SERIAL_PORT_MODE.value:
                self.widgetui.comboComNum.setEnabled(True)
                self.widgetui.comboBoxBaud.setEnabled(True)
                self.serialPort.close()
            self.widgetui.pushButtonSend.setEnabled(False)
            self.widgetui.comboBoxLocal.setEnabled(True)
            self.widgetui.lineEditPortLocal.setEnabled(True)
            self.widgetui.pushButtonStart.setText("连接")
            self.widgetui.comboBoxStyle.setEnabled(True)
            self.widgetui.labelStatus.setText("未连接")

    @pyqtSlot()
    def on_pushButtonSend_clicked(self):
        """
        self.udpSocket.send()
        self.tcpSocketClient.send()
        self.tcpServer.send()
        self.serialPort.send()
        :return:
        """
        msg = self.widgetui.plainTextEditSend.toPlainText()
        aimIP = self.widgetui.lineEditIpAim.text()
        aimPort = int(self.widgetui.lineEditPortAim.text())
        hexORchar = self.getRadioButtStat()
        if hexORchar == HexOrChar.HEX_STYLE:  # 16进制形式
            data = DataOperate.hexStringTochars(msg)
        if hexORchar == HexOrChar.CHAR_STYLE:  # 字符串形式
            data = msg
        curMode = self.connectModeList.index(
            self.widgetui.comboBoxStyle.currentText())
        self.send_data = data
        self.method_name(aimIP, aimPort, curMode)

    @pre_send_decorate
    def method_name(self, aimIP, aimPort, curMode):
        if curMode == ConnectMode.UDP_MODE.value:
            self.udpSocket.send(self.send_data, aimIP, aimPort)
        elif curMode == ConnectMode.TCP_CLIENT_MODE.value:
            self.tcpSocketClient.send(self.send_data)
        elif curMode == ConnectMode.TCP_SERVER_MODE.value:
            self.tcpServer.setCurClient(
                self.widgetui.comboBoxClientList.currentText())
            self.tcpServer.send(self.send_data)
        elif curMode == ConnectMode.SERIAL_PORT_MODE.value:
            self.serialPort.send(self.send_data)

    @pyqtSlot()
    def on_toolButtonAOP_clicked(self):
        aopEditWindow = DialogAOP()
        # aop_dialog若accept则表示aop文件修改了: accept:1 reject:0
        exec_status = aopEditWindow.exec()
        if exec_status == 1:
            self.__init_aop_files()

    @pyqtSlot()
    def on_pushButtonClearRec_clicked(self):
        self.widgetui.plainTextEditRec.clear()

    @pyqtSlot()
    def on_toolButtonShortCut_clicked(self):
        shortCutWindow = DialogHelp()
        shortCutWindow.exec()

    @pyqtSlot(int)
    def comboxClicked(self, selectedIndex):
        if "COM Tool" == self.widgetui.comboBoxStyle.itemText(selectedIndex):
            self.widgetui.comboBoxBaud.show()
            self.widgetui.comboComNum.show()
            self.widgetui.comboComNum.clear()
            # 扫描电脑存在连接的串口com并将其portname加入combox
            for portinfo in SerialPort.getSerialPortList():
                self.widgetui.comboComNum.addItem(portinfo)
            if ('' != self.oldcomNum) and (-1 != self.widgetui.comboComNum.findText(self.oldcomNum)):
                self.widgetui.comboComNum.setCurrentText(self.oldcomNum)
            self.widgetui.groupBoxLocal.hide()
            self.widgetui.groupBoxAim.hide()
        else:
            self.widgetui.comboBoxBaud.hide()
            self.widgetui.comboComNum.hide()
            self.widgetui.groupBoxLocal.show()
            self.widgetui.groupBoxAim.show()

    @pyqtSlot(str)
    def on_comboBoxLocal_activated(self, arg1):
        self.myaddress = arg1

    def keyPressEvent(self, e):
        if (e.modifiers() == Qt.ControlModifier) and (e.key() == Qt.Key_1):
            self.widgetui.plainTextEditRec.clear()
        if (e.modifiers() == Qt.ControlModifier) and (e.key() == Qt.Key_2):
            self.widgetui.plainTextEditSend.clear()
        if (e.modifiers() == Qt.ControlModifier) and (e.key() == Qt.Key_QuoteLeft):
            self.widgetui.plainTextEditRec.clear()
            self.widgetui.plainTextEditSend.clear()

    @post_rec_decorate
    def recByStyle(self, bytesData):
        self.current_rec_data = bytesData
        if self.getCheckBoxStat() == HexOrChar.CHAR_STYLE:
            try:
                self.widgetui.plainTextEditRec.insertPlainText(
                    bytesData.decode('GBK'))
            except Exception as e:
                print('接收报文转字符串编码错误', e)
                return
        elif self.getCheckBoxStat() == HexOrChar.HEX_STYLE:
            self.widgetui.plainTextEditRec.insertPlainText(
                DataOperate.charsToHexString(bytesData))
        bar = self.widgetui.plainTextEditRec.verticalScrollBar()
        bar.setSliderPosition(bar.maximum())


""" Hyperas (hyperopt for keras) : Automate hyperparameter optimization """

import silence_tensorflow.auto
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import uniform, choice

from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.models import Sequential

import pandas as pd
import numpy as np

def process_data(train, test, fillmissing=True):
    cols = list(train.columns)
    data_cat = train[cols[1:11]]
    data_cat = pd.get_dummies(data_cat)
    data_num = train[cols[11:-1]]
    y_train = train['target']
    x_train = data_cat.join(data_num)

    cols = list(test.columns)
    data_cat = test[cols[1:11]]
    data_cat = pd.get_dummies(data_cat)
    data_num = test[cols[11:]]
    x_test = data_cat.join(data_num)
    # missing some G category in cat6 testing data, adding it manually
    if fillmissing:
        x_test.insert(23, "cat6_G", 0)
    else:
        x_train.drop(['cat6_G'], axis=1, inplace=True)
    return np.asarray(x_train), np.asarray(y_train), np.asarray(x_test)

def data():
    train = pd.read_csv('../DataSets/TPSfeb2021/train.csv')
    cols = list(train.columns)
    data_cat = train[cols[1:11]]
    data_cat = pd.get_dummies(data_cat)
    data_num = train[cols[11:-1]]
    x_train = data_cat.join(data_num)
    x_train.drop(['cat6_G'], axis=1, inplace=True)
    y = np.asarray(train['target'], dtype='float64')
    X = np.asarray(x_train, dtype='float64')

    x_train, y_train = X[:200000], y[:200000]
    x_valid, y_valid = X[200000:], y[200000:]
    return x_train, y_train, x_valid, y_valid

def create_model(x_train, y_train, x_valid, y_valid):
    model = Sequential()
    model.add(Dense(512, input_shape=(69,), activation='relu'))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense({{choice([256, 512, 1024])}}, activation={{choice(['relu', 'sigmoid'])}}))
    model.add(Dropout({{uniform(0, 1)}}))

    if {{choice(['three', 'four'])}} == 'four':
        model.add(Dense({{choice([128, 256])}}, activation={{choice(['relu', 'sigmoid'])}}))

    model.add(Dense(1))

    model.compile(optimizer={{choice(['rmsprop', 'adam', 'sgd'])}}, loss='mse')

    history = model.fit(x_train, y_train, batch_size={{choice([64, 128])}},
                        epochs={{choice([10, 15, 20])}}, validation_split=.1)

    validation_loss = np.amax(history.history['val_loss'])
    print('Best validation loss of epochs:', validation_loss)
    return {'loss': -validation_loss, 'status': STATUS_OK, 'model': model}

if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=create_model, data=data, algo=tpe.suggest,
                                          max_evals=10, trials=Trials())
    best_model.save('tpsfeb2021_hyperas01.h5')

    print("Best pertforming model chosen hyper-parameters:")
    print(best_run)
    X_train, y_train, X_valid, y_valid = data()
    print("Evaluation of best performing model:")
    print(best_model.evaluate(X_valid, y_valid))

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

    X, y, x_test = process_data(train, test)

    prediction = best_model.predict(x_test)
    test_prediction = test[['id']].copy()
    test_prediction['target'] = prediction
    test_prediction.to_csv('test_prediction_dropmissing_7.csv', index=False)


import pygame
import random
import button

pygame.init()

clock = pygame.time.Clock()
fps = 60

#janela do jogo
bottom_panel = 150
screen_width = 800
screen_height = 400 + bottom_panel

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Luta contra o Covid-19')

current_fighter = 1
total_fighters = 3
action_cooldown = 0
action_wait_time = 90
attack = False
potion = False
potion_effect = 15
clicked = False
game_over = 0

font = pygame.font.SysFont('Times New Roman', 26)

#define colours
red = (255, 0, 0)
green = (0, 255, 0)

background_img = pygame.image.load('img/Background/background.png').convert_alpha()
#painel de imagem
panel_img = pygame.image.load('img/Icons/panel.png').convert_alpha()
#imagem dos icones
potion_img = pygame.image.load('img/Icons/potion.png').convert_alpha()
restart_img = pygame.image.load('img/Icons/restart.png').convert_alpha()
#vitoria e derrota
victory_img = pygame.image.load('img/Icons/victory.png').convert_alpha()
defeat_img = pygame.image.load('img/Icons/defeat.png').convert_alpha()
#imagem que aparece ao por em cima do inimigo
sword_img = pygame.image.load('img/Icons/seringa.png').convert_alpha()

def draw_text(text, font, text_col, x, y):
	img = font.render(text, True, text_col)
	screen.blit(img, (x, y))

def draw_bg():
	screen.blit(background_img, (0, 0))

def draw_panel():

	screen.blit(panel_img, (0, screen_height - bottom_panel))

	draw_text(f'{knight.name} HP: {knight.hp}', font, red, 100, screen_height - bottom_panel + 10)
	for count, i in enumerate(bandit_list):
		#nome e vida
		draw_text(f'{i.name} HP: {i.hp}', font, red, 550, (screen_height - bottom_panel + 10) + count * 60)

#lutador
class Fighter():
	def __init__(self, x, y, name, max_hp, strength, potions):
		self.name = name
		self.max_hp = max_hp
		self.hp = max_hp
		self.strength = strength
		self.start_potions = potions
		self.potions = potions
		self.alive = True
		self.animation_list = []
		self.frame_index = 0
		self.action = 0#0:idle, 1:attack, 2:hurt, 3:dead
		self.update_time = pygame.time.get_ticks()

		temp_list = []
		for i in range(8):
			img = pygame.image.load(f'img/{self.name}/Idle/{i}.png')
			img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
			temp_list.append(img)
		self.animation_list.append(temp_list)
		#atacando
		temp_list = []
		for i in range(8):
			img = pygame.image.load(f'img/{self.name}/Attack/{i}.png')
			img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
			temp_list.append(img)
		self.animation_list.append(temp_list)
		#machucado
		temp_list = []
		for i in range(3):
			img = pygame.image.load(f'img/{self.name}/Hurt/{i}.png')
			img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
			temp_list.append(img)
		self.animation_list.append(temp_list)
		#morte
		temp_list = []
		for i in range(10):
			img = pygame.image.load(f'img/{self.name}/Death/{i}.png')
			img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
			temp_list.append(img)
		self.animation_list.append(temp_list)
		self.image = self.animation_list[self.action][self.frame_index]
		self.rect = self.image.get_rect()
		self.rect.center = (x, y)

	def update(self):
		animation_cooldown = 100

		self.image = self.animation_list[self.action][self.frame_index]

		if pygame.time.get_ticks() - self.update_time > animation_cooldown:
			self.update_time = pygame.time.get_ticks()
			self.frame_index += 1

		if self.frame_index >= len(self.animation_list[self.action]):
			if self.action == 3:
				self.frame_index = len(self.animation_list[self.action]) - 1
			else:
				self.idle()

	def idle(self):

		self.action = 0
		self.frame_index = 0
		self.update_time = pygame.time.get_ticks()

	def attack(self, target):

		rand = random.randint(-5, 5)
		damage = self.strength + rand
		target.hp -= damage

		target.hurt()

		if target.hp < 1:
			target.hp = 0
			target.alive = False
			target.death()
		damage_text = DamageText(target.rect.centerx, target.rect.y, str(damage), red)
		damage_text_group.add(damage_text)

		self.action = 1
		self.frame_index = 0
		self.update_time = pygame.time.get_ticks()

	def hurt(self):

		self.action = 2
		self.frame_index = 0
		self.update_time = pygame.time.get_ticks()

	def death(self):

		self.action = 3
		self.frame_index = 0
		self.update_time = pygame.time.get_ticks()

	def reset (self):
		self.alive = True
		self.potions = self.start_potions
		self.hp = self.max_hp
		self.frame_index = 0
		self.action = 0
		self.update_time = pygame.time.get_ticks()

	def draw(self):
		screen.blit(self.image, self.rect)

class HealthBar():
	def __init__(self, x, y, hp, max_hp):
		self.x = x
		self.y = y
		self.hp = hp
		self.max_hp = max_hp

	def draw(self, hp):

		self.hp = hp

		ratio = self.hp / self.max_hp
		pygame.draw.rect(screen, red, (self.x, self.y, 150, 20))
		pygame.draw.rect(screen, green, (self.x, self.y, 150 * ratio, 20))

class DamageText(pygame.sprite.Sprite):
	def __init__(self, x, y, damage, colour):
		pygame.sprite.Sprite.__init__(self)
		self.image = font.render(damage, True, colour)
		self.rect = self.image.get_rect()
		self.rect.center = (x, y)
		self.counter = 0

	def update(self):

		self.rect.y -= 1

		self.counter += 1
		if self.counter > 30:
			self.kill()

damage_text_group = pygame.sprite.Group()

knight = Fighter(200, 330, 'Medico', 30, 15, 3)
bandit1 = Fighter(550, 270, 'Covid', 20, 6, 1)
bandit2 = Fighter(700, 270, 'Covid', 20, 6, 1)

bandit_list = []
bandit_list.append(bandit1)
bandit_list.append(bandit2)

knight_health_bar = HealthBar(100, screen_height - bottom_panel + 40, knight.hp, knight.max_hp)
bandit1_health_bar = HealthBar(550, screen_height - bottom_panel + 40, bandit1.hp, bandit1.max_hp)
bandit2_health_bar = HealthBar(550, screen_height - bottom_panel + 100, bandit2.hp, bandit2.max_hp)

potion_button = button.Button(screen, 100, screen_height - bottom_panel + 70, potion_img, 64, 64)
restart_button = button.Button(screen, 330, 120, restart_img, 120, 30)

run = True
while run:

	clock.tick(fps)

	draw_bg()

	draw_panel()
	knight_health_bar.draw(knight.hp)
	bandit1_health_bar.draw(bandit1.hp)
	bandit2_health_bar.draw(bandit2.hp)

	knight.update()
	knight.draw()
	for bandit in bandit_list:
		bandit.update()
		bandit.draw()

	damage_text_group.update()
	damage_text_group.draw(screen)

	attack = False
	potion = False
	target = None

	pygame.mouse.set_visible(True)
	pos = pygame.mouse.get_pos()
	for count, bandit in enumerate(bandit_list):
		if bandit.rect.collidepoint(pos):

			pygame.mouse.set_visible(False)

			screen.blit(sword_img, pos)
			if clicked == True and bandit.alive == True:
				attack = True
				target = bandit_list[count]
	if potion_button.draw():
		potion = True

	draw_text(str(knight.potions), font, red, 150, screen_height - bottom_panel + 70)

	if game_over == 0:

		if knight.alive == True:
			if current_fighter == 1:
				action_cooldown += 1
				if action_cooldown >= action_wait_time:

					if attack == True and target != None:
						knight.attack(target)
						current_fighter += 1
						action_cooldown = 0
					#pocao
					if potion == True:
						if knight.potions > 0:
							if knight.max_hp - knight.hp > potion_effect:
								heal_amount = potion_effect
							else:
								heal_amount = knight.max_hp - knight.hp
							knight.hp += heal_amount
							knight.potions -= 1
							damage_text = DamageText(knight.rect.centerx, knight.rect.y, str(heal_amount), green)
							damage_text_group.add(damage_text)
							current_fighter += 1
							action_cooldown = 0
		else:
			game_over = -1

		for count, bandit in enumerate(bandit_list):
			if current_fighter == 2 + count:
				if bandit.alive == True:
					action_cooldown += 1
					if action_cooldown >= action_wait_time:

						if (bandit.hp / bandit.max_hp) < 0.5 and bandit.potions > 0:

							if bandit.max_hp - bandit.hp > potion_effect:
								heal_amount = potion_effect
							else:
								heal_amount = bandit.max_hp - bandit.hp
							bandit.hp += heal_amount
							bandit.potions -= 1
							damage_text = DamageText(bandit.rect.centerx, bandit.rect.y, str(heal_amount), green)
							damage_text_group.add(damage_text)
							current_fighter += 1
							action_cooldown = 0

						else:
							bandit.attack(knight)
							current_fighter += 1
							action_cooldown = 0
				else:
					current_fighter += 1

		if current_fighter > total_fighters:
			current_fighter = 1

	alive_bandits = 0
	for bandit in bandit_list:
		if bandit.alive == True:
			alive_bandits += 1
	if alive_bandits == 0:
		game_over = 1

	if game_over != 0:
		if game_over == 1:
			screen.blit(victory_img, (250, 50))
		if game_over == -1:
			screen.blit(defeat_img, (290, 50))
		if restart_button.draw():
			knight.reset()
			for bandit in bandit_list:
				bandit.reset()
			current_fighter = 1
			action_cooldown
			game_over = 0

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			run = False
		if event.type == pygame.MOUSEBUTTONDOWN:
			clicked = True
		else:
			clicked = False

	pygame.display.update()

pygame.quit()


import random
#import secrets
class encrypt:
	def __init__(self,data):
		self.data = data
		self.block_size = 8
		self.l = []
		self.key_list = []
		self.enc_list = []
	def convert_to_binary(self):
		#print(self.data,len(self.data))
		for i in self.data:
			self.l.append('{0:08b}'.format(ord(i)))
	def obtain_prn(self):
		'''
		num = secrets.randbelow(length)
		'''
		return random.randint(1,self.block_size)
	def crossover(self,a,b,type_crossover):
		if(type_crossover == 0):
			x = a[0:4] + b[4:8]
			y = b[0:4] + a[4:8]
		if(type_crossover == 1):
			x = a[0:2] + b[2:6] + a[6:8]
			y = b[0:2] + a[2:6] + b[6:8]
		if(type_crossover == 2):
			x = a[0:2] + b[2:4] + a[4:6] + b[6:8]
			y = b[0:2] + a[2:4] + b[4:6] + a[6:8]
		return x,y
	def mutate(self,a,b):
		temp1 = ''
		temp2 = ''
		for i in range(0,len(a)):
			if(a[i]=='0'):
				temp1 = temp1+'1'
			if(a[i]=='1'):
				temp1 = temp1+'0'
			if(b[i]=='0'):
				temp2 = temp2+'1'
			if(b[i]=='1'):
				temp2 = temp2+'0'
		return temp1,temp2
	def save_file(self):
		enc_text = ''
		for i in self.enc_list:
			enc_text = enc_text + chr(int(bin(int(i,2)),2))
		f = open("enc_file","w")
		f.write(enc_text)
		key_text = ''
		f.close()
		for i in self.key_list:
			key_text = key_text + '{0:08b}'.format(i)
		f = open("key","w")
		f.write(key_text)
		f.close()
	def start(self):
		self.convert_to_binary()
		i = 0
		while(i<len(self.l)-1):
			a = self.l[i]
			b = self.l[i+1]
			i = i + 2
			rand_no = self.obtain_prn()
			self.key_list.append(rand_no)
			type_crossover = rand_no % 3
			crsv_a,crsv_b = self.crossover(a,b,type_crossover)
			mut_a,mut_b = self.mutate(crsv_a,crsv_b)
			self.enc_list.append(mut_a)
			self.enc_list.append(mut_b)
		self.save_file()


from rsome import lp
from rsome import grb_solver as grb
import rsome as rso
import numpy as np
import pandas as pd
import pytest

def test_model():

    model = lp.Model()
    x = model.dvar()
    y = model.dvar(vtype='I')
    z = model.dvar(2)

    with pytest.raises(ValueError):
        model.min(z*5)

    with pytest.raises(ValueError):
        model.max(z*5)

    model.max(3*x + 4*y)
    model.st(x*2.5 + y <= 20)
    model.st(-5*x - 3*y >= -30)
    model.st(-16 + x + 2*y <= 0)
    model.st(y.abs() <= 2)

    with pytest.raises(RuntimeError):
        model.get()

    with pytest.raises(RuntimeError):
        x.get()

    primal = model.do_math()
    string = '=============================================\n'
    string += 'Number of variables:           5\n'
    string += 'Continuous/binaries/integers:  4/0/1\n'
    string += '---------------------------------------------\n'
    string += 'Number of linear constraints:  6\n'
    string += 'Inequalities/equalities:       6/0\n'
    string += 'Number of coefficients:        11\n'
    assert primal.__repr__() == string

    df = primal.show()
    assert type(df) == pd.DataFrame

    model.solve(grb)
    print(model.solution.status)
    assert abs(model.get() - 22.4) < 1e-4
    assert abs(x.get() - 4.8) < 1e-4
    assert abs(y.get() - 2) < 1e-4
    assert primal == model.do_math()

    model = lp.Model()
    x = model.dvar()
    y = model.dvar(vtype='I')
    z = model.dvar(2)
    model.min(-3*x + -4*y)
    model.st(x*2.5 + y <= 20)
    model.st(-5*x - 3*y >= -30)
    model.st(-16 + x + 2*y <= 0)
    model.st((y*2).abs() <= 4)
    model.solve(grb)
    assert abs(model.get() + 22.4) < 1e-4
    assert abs(x.get() - 4.8) < 1e-4
    assert abs(y.get() - 2) < 1e-4

    with pytest.warns(UserWarning):
        model.do_math(primal=False)

    model.st(y >= 2.5)
    with pytest.warns(UserWarning):
        model.solve(grb)

    with pytest.raises(RuntimeError):
        objval = model.get()
        print(objval)

    with pytest.raises(RuntimeError):
        x_sol = x.get()
        print(x_sol)

    with pytest.raises(SyntaxError):
        model.min(3*x + 5*y)

    with pytest.raises(SyntaxError):
        model.max(3*x + 5*y)

def test_model_match():

    m1, m2 = lp.Model('1st model'), lp.Model('2nd model')

    x1, x2 = m1.dvar(5), m2.dvar(5)
    xx1, xx2 = m1.dvar((2, 5)), m2.dvar((2, 5))

    with pytest.raises(ValueError):
        x1 + x2

    with pytest.raises(ValueError):
        x1 + xx2

    with pytest.raises(ValueError):
        m1.st(xx2 + 0 <= 1)

    with pytest.raises(ValueError):
        m2.st(xx1 + 0 >= 1)

    with pytest.raises(ValueError):
        m1.st(x2 >= 0)

    with pytest.raises(TypeError):
        m1.st(np.zeros(5) == 0)

    with pytest.raises(TypeError):
        m1.st(rso.norm(x1) <= 1)


"""
COMP.CS.100 Tehtävä opintopistelaskuri
Tekijä: Eetu Kuittinen
Opiskelijanumero: 150541820
"""

# Ohjelmassa käyttämäni lyhenne str tulee sanasta String eli merkkijono. Tämä on itseäni selkeyttämään, sillä taustani
# on ohjelmointikielissä, joissa muuttujatyyppi on esiteltävä sitä määriteltäessä

def main():
    # Flag
    onko_opintoja_liian_vahan = False

    # Arvojen säilyttäjät, alustetaan liukuluvuiksi
    edellisen_kuukauden_pisteet = 0.0
    pisteet_yhteensa = 0.0
    nykyisen_kuukauden_pisteet = 0.0
    opintopisteiden_maara_tassa_kuussa = 0.0
    keskiarvo = 0.0

    # Kysytään kuukaudet
    str_kuukaudet = input("Enter the number of months: ")
    # Muunetaan kuukaudet kokonaisluvuksi
    kuukausien_maara = int(str_kuukaudet)

    for i in range(1, kuukausien_maara + 1):

        print("Enter the number of credits in month ", i, ":", sep="", end=' ')
        str_opintopisteet = input("")
        opintopisteiden_maara_tassa_kuussa = int(str_opintopisteet)
        pisteet_yhteensa += opintopisteiden_maara_tassa_kuussa

        # Ensimmäisessä kuussa molempien arvoiksi asetetaan nykyiset kysytyt opintopisteet
        if i == 1:
            edellisen_kuukauden_pisteet = opintopisteiden_maara_tassa_kuussa
            nykyisen_kuukauden_pisteet = opintopisteiden_maara_tassa_kuussa

        # Toisella kierroksella ei kosketa edellisiin, vaan vain nykyiset korvataan
        elif i == 2:
            nykyisen_kuukauden_pisteet = opintopisteiden_maara_tassa_kuussa

        # Edellisen kuukauden pisteisiin siirretään se arvo, joka oli edellisellä kierroksella nykyisen kuukauden
        # pisteissä. Tämän jälkeen nykyisen arvo päivitetään ajan tasalle
        else:
            edellisen_kuukauden_pisteet = nykyisen_kuukauden_pisteet
            nykyisen_kuukauden_pisteet = opintopisteiden_maara_tassa_kuussa

        # Erisuuri estää tilanteen, jossa ensimmäisellä kierroksella nollan syöttäminen lopettaisi ohjelman.
        # Muuten kuukaudet lasketaan yhteen, ja jos summa on nolla, niin molemmat arvot olivat nollia. Tällöin ohjelma
        # suljetaan heti returnilla
        if i != 1 and edellisen_kuukauden_pisteet + nykyisen_kuukauden_pisteet == 0:
            print("\nYou did have too many study breaks!")
            return

        i += 1

    keskiarvo = pisteet_yhteensa / kuukausien_maara

    if keskiarvo < 5.0:
        print(f'\nYour monthly credit point average {keskiarvo:.1f} does not classify you as a full time '
              f'student.')

    else:
        print(f'\nYou are a full time student and your monthly credit point average is {keskiarvo:.1f}.')

if __name__ == "__main__":
    main()


from aws_cdk import Duration, Stack, CfnOutput
from aws_cdk import aws_glue as glue
from aws_cdk import aws_glue_alpha as glue_alpha
from aws_cdk import aws_iam as iam
from aws_cdk import aws_s3 as s3
from aws_cdk import aws_sns as sns
from aws_cdk import aws_sns_subscriptions as subscriptions
from aws_cdk import aws_sqs as sqs
from aws_cdk import custom_resources as cr
from constructs import Construct

RAW_TABLE = "db"
NEW_TABLE = "new_table"
# The S3 path where you will upload your data will be "s3://{bucket.bucket_name}/

class CrawlerStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        bucket = s3.Bucket(self, "bucket")

        glue_database = glue_alpha.Database(self, "GlueDB", database_name=RAW_TABLE)

        crawler_role = iam.Role(
            self,
            "crawler_role",
            assumed_by=iam.ServicePrincipal("glue.amazonaws.com"),
        )
        bucket.grant_read(crawler_role)
        crawler_role.add_to_policy(
            iam.PolicyStatement(actions=["glue:*"], resources=["*"])
        )
        crawler_role.add_to_policy(
            iam.PolicyStatement(actions=["cloudwatch:PutMetricData"], resources=["*"])
        )
        crawler_role.add_to_policy(
            iam.PolicyStatement(
                actions=[
                    "logs:CreateLogGroup",
                    "logs:CreateLogStream",
                    "logs:PutLogEvents",
                ],
                resources=["arn:aws:logs:*:*:/aws-glue/*"],
            )
        )
        crawler_event_queue = sqs.Queue(self, "Crawler Queue")

        crawler_role.add_to_policy(
            iam.PolicyStatement(
                actions=["*"],
                resources=[crawler_event_queue.queue_arn],
            )
        )

        crawler_event_queue.grant_consume_messages(crawler_role)
        crawler_event_topic = sns.Topic(self, "Topic crawler")
        crawler_event_topic.add_subscription(
            subscriptions.SqsSubscription(crawler_event_queue)
        )

        topic_policy = sns.TopicPolicy(
            self, "TopicPolicy", topics=[crawler_event_topic]
        )

        topic_policy.document.add_statements(
            iam.PolicyStatement(
                actions=["sns:Publish"],
                principals=[iam.ServicePrincipal("s3.amazonaws.com")],
                resources=[crawler_event_topic.topic_arn],
                conditions={"ArnLike": {"aws:SourceArn": bucket.bucket_arn}},
            )
        )

        crawler = glue.CfnCrawler(
            self,
            "crawler",
            targets=glue.CfnCrawler.TargetsProperty(
                s3_targets=[
                    glue.CfnCrawler.S3TargetProperty(
                        path=f"s3://{bucket.bucket_name}/",
                        event_queue_arn=crawler_event_queue.queue_arn,
                    )
                ]
            ),
            role=crawler_role.role_arn,
            database_name=glue_database.database_name,
            recrawl_policy=glue.CfnCrawler.RecrawlPolicyProperty(
                recrawl_behavior="CRAWL_EVENT_MODE"
            ),
        )
        crawler.node.add_dependency(
            crawler_role
        )  # Important as the dependency is not integrated natively in CDK/Cloudformation

        job = glue_alpha.Job(
            self,
            "Job",
            executable=glue_alpha.JobExecutable.python_etl(
                glue_version=glue_alpha.GlueVersion.V3_0,
                python_version=glue_alpha.PythonVersion.THREE,
                script=glue_alpha.Code.from_asset("glue_job/job.py"),
            ),
            timeout=Duration.minutes(15),
            max_capacity=2,
            default_arguments={
                "--job-bookmark-option": "job-bookmark-enable",
                "--bucket": bucket.bucket_name,
                "--db": glue_database.database_name,
                "--raw_table": RAW_TABLE,
                "--repartitioned_table": NEW_TABLE,
            },
        )
        bucket.grant_read_write(job)

        workflow = glue.CfnWorkflow(self, "Workflow")

        trigger_crawler = glue.CfnTrigger(
            self,
            "Trigger Crawler",
            actions=[glue.CfnTrigger.ActionProperty(crawler_name=crawler.ref)],
            type="SCHEDULED",
            workflow_name=workflow.ref,
            schedule="cron(0 */6 * * ? *)",  # every 6 hours
            start_on_creation=True,
        )

        trigger_job = glue.CfnTrigger(
            self,
            "Trigger Job",
            actions=[glue.CfnTrigger.ActionProperty(job_name=job.job_name)],
            type="CONDITIONAL",
            predicate=glue.CfnTrigger.PredicateProperty(
                conditions=[
                    glue.CfnTrigger.ConditionProperty(
                        crawler_name=crawler.ref,
                        crawl_state="SUCCEEDED",
                        logical_operator="EQUALS",
                    )
                ],
            ),
            start_on_creation=True,
            workflow_name=workflow.ref,
        )

        aws_custom = cr.AwsCustomResource(
            self,
            "bucket notification",
            on_create=cr.AwsSdkCall(
                service="S3",
                action="putBucketNotificationConfiguration",
                parameters={
                    "Bucket": bucket.bucket_name,
                    "NotificationConfiguration": {
                        "TopicConfigurations": [
                            {
                                "Events": ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"],
                                "TopicArn": crawler_event_topic.topic_arn,
                                "Id": crawler.ref,  # important part if you want the crawler to read the event
                            }
                        ],
                    },
                },
                physical_resource_id=cr.PhysicalResourceId.of("notif-" + crawler.ref),
            ),
            on_update=cr.AwsSdkCall(
                service="S3",
                action="putBucketNotificationConfiguration",
                parameters={
                    "Bucket": bucket.bucket_name,
                    "NotificationConfiguration": {
                        "TopicConfigurations": [
                            {
                                "Events": ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"],
                                "TopicArn": crawler_event_topic.topic_arn,
                                "Id": crawler.ref,  # important part if you want the crawler to read the event
                            }
                        ],
                    },
                },
                physical_resource_id=cr.PhysicalResourceId.of("notif-" + crawler.ref),
            ),
            on_delete=cr.AwsSdkCall(
                service="S3",
                action="putBucketNotificationConfiguration",
                parameters={
                    "Bucket": bucket.bucket_name,
                    "NotificationConfiguration": {},  # Be careful, it will delete all of your bucket notifs
                },
                physical_resource_id=cr.PhysicalResourceId.of("notif-" + crawler.ref),
            ),
            policy=cr.AwsCustomResourcePolicy.from_statements(
                statements=[
                    iam.PolicyStatement(
                        actions=["s3:PutBucketNotification*"], resources=["*"]
                    ),
                ]
            ),
        )
        CfnOutput(self, "crawlerName", value=crawler.ref)
        CfnOutput(self, "workflowName", value=workflow.ref)
        CfnOutput(self, "bucketName", value=bucket.bucket_name)


import requests
import re
from bs4 import BeautifulSoup
import json,sys,os

class downloader(object):
    def get_contents(self):
        headers1={
            #use a special cookies
			'User-Agent':"xxxx"

        }
        try:
            req = requests.get(url=self.url, headers=headers1)
            html = req.text
            div_bf = BeautifulSoup(html, "lxml")
            div = div_bf.find_all('div', class_="cc-cd")
        except :
            return 1 # requests error
        else:
            example = []

            for content in div:
                text = self.get_text(str(content))
                temp = re.split('\n\n \n\n|\n\n\n\n\n\n\n\n', text)
                example.append(temp)
            state = self.store_content(example)
            return state

    def get_text(self, content):
        # 去除<>
        reg = re.compile('<[^>]*>')
        texts = reg.sub('', content)
        return texts

    def store_content(self,contents):
        # 存储路径获取
        file_path = sys.argv[0]
        path=os.path.dirname(file_path)
        for i in contents:
            title=i[0].split("\n\n\n\n\n")[1]
            title=title.replace(' ','')
            content=i[1:]
            # name the file
            file_name=path+"/"+title+".txt"
            try:
                with open(file_name, 'w', encoding='utf-8') as f:
                    f.write(str(content))
            except IOError:
                return 2  # 文件存储出错
            else:
                continue
        return 0

def hot_craw_test(url):
    dl=downloader()
    dl.url = url
    state=dl.get_contents()
    return state


import time
import unittest
from constant import ExecState
from models.job import TestJob, TestJobCase
from models.base import db

class TestJobModule(unittest.TestCase):

    def test_job_name(self):
        job = TestJob.filter(id=1).first()
        self.assertEqual("test001", job.name)

class TestDagModule(unittest.TestCase):

    def test_overwrite_save(self):
        test_job_case = TestJobCase.get(id=1)
        test_job_case.state = ExecState.RUNNING
        test_job_case.save()
        test_job_case = TestJobCase.get(id=1)
        self.assertEqual(ExecState.RUNNING, test_job_case.state)

    def test_overwrite_update(self):
        TestJobCase.update(state=ExecState.RUNNING).where(
            TestJobCase.id == 1,
        ).execute()
        test_job_case = TestJobCase.get(id=1)
        self.assertEqual(ExecState.RUNNING, test_job_case.state)

class TestDbSession(unittest.TestCase):

    def db_operation1(self, job_case_id):
        self.db_operation2(job_case_id)
        # raise Exception("error")

    def db_operation2(self, job_case_id):
        self.db_operation3(job_case_id)
        # raise Exception("error")

    @staticmethod
    def db_operation3(job_case_id):
        TestJobCase.update(state=ExecState.FAIL).where(
            TestJobCase.id == job_case_id,
        ).execute()
        print("sleep 30...")
        time.sleep(30)
        raise Exception("error")

    # @db.atomic()
    def db_session(self, job_case_id):
        self.db_operation1(job_case_id)
        # raise Exception("db operation has error, rollback...")

    @db.atomic()
    def test_db_session(self):
        job_case_id = 4
        try:
            TestJobCase.update(state="running", note="xxx").where(
                TestJobCase.id == job_case_id,
            ).execute()
            self.db_session(job_case_id)
        except Exception as error:
            print(error)
        finally:
            self.assertNotEqual(
                ExecState.FAIL,
                TestJobCase.get_by_id(job_case_id).state
            )

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