import pygame
import time
import random

pygame.init()

crash_sound = pygame.mixer.Sound("Music & SF/SFX.wav")
pygame.mixer.music.load("Music & SF/gas gas gas final.wav")

scale = 1

display_width = 1000*scale
display_height = 600*scale

black = (0,0,0)
white = (225,225,225)
red = (150,0,0)
green = (0,150,0)
blue = (100,100,225)
bright_red = (225,0,0)
bright_green = (0,225,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('D')
clock = pygame.time.Clock()

carImg1 = pygame.transform.scale(pygame. image.load('car/car1.png'),(96*3*scale,31*3*scale))
carImg2 = pygame.transform.scale(pygame. image.load('car/car2.png'),(96*3*scale,31*3*scale))
carImg3 = pygame.transform.scale(pygame. image.load('car/car3.png'),(96*3*scale,31*3*scale))
carImg = [carImg1,carImg2,carImg3]

pause = False
game_over = False

def score(count1,count2):
    font = pygame.font.Font('freesansbold.ttf',25)
    text = font.render ("Score: " + str(count1) + "  Level: " + str(count2), True, white)
    gameDisplay.blit(text,(0,0))

def blocks(thingx,thingy,thingw,thingh,color):
    pygame.draw.rect(gameDisplay,color,[thingx,thingy,thingw,thingh])

def car(x,y,z):
    gameDisplay.blit(carImg[z],(x,y))

def text_objects(text,font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()

def message_display(text,score,level):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf,TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf,TextRect)

    pygame.display.update()

    time.sleep(2)

    game_over(score,level)

def crash(score,level):
    pygame.mixer.music.stop()
    pygame.mixer.Sound.play(crash_sound)
    message_display('You Crashed',score,level)

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            action()
#            if action == "play":
#               game_loop()
#            elif action == "quit":
#                pygame.quit()
#                quit()
    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ((x + w/2), (y + h/2))
    gameDisplay.blit(textSurf, textRect)

def unpause():
    global pause
    pause = False
    pygame.mixer.music.unpause()

def paused():

    pygame.mixer.music.pause()

    while pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        largeText = pygame.font.Font('freesansbold.ttf', 50)
        TextSurf, TextRect = text_objects("Paused", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2 - 150))
        gameDisplay.blit(TextSurf, TextRect)

        button('Resume', display_width/ 2 - 50, 250, 100, 50, green, bright_green,unpause)
        button('Restart', display_width / 2 - 50, 350, 100, 50, green, bright_green, game_loop)
        button('Quit', display_width/ 2 - 50, 450, 100, 50, red, bright_red,quit)

        pygame.display.update()
        clock.tick(15)

def game_over(score,level):

    while game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(black)
        largeText = pygame.font.Font('freesansbold.ttf', 50)
        TextSurf, TextRect = text_objects("GAME OVER", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2 - 150))
        gameDisplay.blit(TextSurf, TextRect)

        button('Score: ' + str(score) + '  Level: ' + str(level), display_width / 2 - 50, 200, 100, 50, black, black, None)
        button('Restart', display_width / 2 - 50, 350, 100, 50, green, bright_green, game_loop)
        button('Quit', display_width/ 2 - 50, 450, 100, 50, red, bright_red,quit)

        pygame.display.update()
        clock.tick(15)

def game_intro():

    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(black)
        largeText = pygame.font.Font('freesansbold.ttf', 115)
        TextSurf, TextRect = text_objects("D", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button('Go!', 200, 450, 100, 50, green, bright_green,game_loop)
        button('Quit', 700, 450, 100, 50, red, bright_red,quit)

        pygame.display.update()
        clock.tick(15)

def game_loop():

    global pause

    pygame.mixer.music.play(-1)
    x = (display_width * 0.34)
    y = (display_height * 0.75)
    x_change = 0
    z = 0

    thing_startx = display_width * 0.5
    thing_starty = display_height * 0.0
    thing_speed = 5
    thing_speedx = random.randrange(-5 * thing_speed - 2 * thing_speed - 30, 5 * thing_speed + 10) * 0.1
    thing_speedy = 1 * thing_speed
    thing_width = 0
    thing_height = 0
    thing_size = 0.2 * thing_speed

    dodged = 0
    level = 1

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    x_change = -7
                    z = 2
                elif event.key == pygame.K_d:
                    x_change = 7
                    z = 1
                elif event.key == pygame.K_p:
                    pause = True
                    paused()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    x_change = 0
                    z = 0

        x += x_change

        gameDisplay.fill(black)
        car(x, y, z)

        blocks(thing_startx,thing_starty,thing_width,thing_height,blue)
        thing_startx += thing_speedx
        thing_starty += thing_speedy
        thing_width += thing_size
        thing_height += thing_size

        score(dodged,level)

        if x < 0 - 28 * 3 or x > display_width -28 * 3 - 39 * 3:
            crash(dodged,level)

        if thing_starty > display_height:
            thing_startx = display_width * 0.5
            thing_starty = display_height * 0.0
            thing_speedx = random.randrange(-5 * thing_speed - 2 * thing_speed - 30, 5 * thing_speed + 10) * 0.1
            thing_speedy = 1 * thing_speed
            thing_width = 0
            thing_height = 0
            thing_size = 0.2 * thing_speed
            dodged += 1
            if dodged > 10:
                thing_speed = 10
                level = 2
                if dodged > 20:
                    thing_speed = 15
                    level = 3
                    if dodged > 30:
                        thing_speed = 20
                        level = 4
                        if dodged > 40:
                            thing_speed = 20
                            level = 4
                            if dodged > 50:
                                thing_speed = 25
                                level = 5

        if y < thing_starty + thing_height and y + 25*3 > thing_starty:
            if x+28*3 > thing_startx and x+28*3 < thing_startx + thing_width or x+28*3 + 39*3> thing_startx and x+28*3 + 39*3< thing_startx+thing_width:
                crash(dodged, level)

        pygame.display.update()
        clock.tick(60)

game_intro()

pygame.quit()
quit()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "版权所有@源码商城：https://shop530346312.taobao.com/?spm=a1z10.1-c.0.0.1c1f6daeeNP5VM"
__date__ = "2019/05/10 11:45"

# form表单验证

from django import forms
from .models import GetMessage

class GetMessageForm(forms.ModelForm):
    """读者留言验证,注意这里继承的是ModelForm"""

    class Meta:
        """继承GetMessage类"""
        model = GetMessage
        fields = ['name', 'email', 'subject', 'message']


from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import permission_classes
from django.http import JsonResponse
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from django.contrib.auth.models import User
from blockchain_project.models import DeployRequest
from blockchain_project.serializers.deploy_serializer import DeploymentRequestSerializer

from rest_framework import serializers
from blockchain_project.views.blockchain import deployOneFundraiseContract,CharityConfig
from web3 import Web3, HTTPProvider,eth
from blockchain_project.models import DeployedCharityContract
from web3.contract import ConciseContract

class DeployRequestView(APIView):
    '''
    用户请求代部署合约
    POST /api/fundraises
    ["name","owner_address","target_money","ipfs_hashes"]

    用户撤回请求代部署：
    DELETE /api/fundraises/delete/{id}

    用户查询代部署合约的申请记录
    GET /api/fundraises
    '''

    def post(self, request, format=None):
        # request.data 无法修改，重新复制一份data
        InfoDict=request.data.copy()
        InfoDict['state']=0
        InfoDict['owner']=User.objects.get(username=str(request.user)).id
        serializer = DeploymentRequestSerializer(data=InfoDict)

        if serializer.is_valid():
            serializer.save()
            return Response(status=status.HTTP_201_CREATED)
        else:
            print(serializer.errors)
            return Response(status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        deployments = DeployRequest.objects.filter(id=pk,owner=request.user.id)
        if len(deployments) == 0:
            return Response(status=status.HTTP_404_NOT_FOUND)
        deployment = deployments.get(id=pk)
        deployment.delete()
        return Response(status=status.HTTP_200_OK)

    def get(self, request, format=None):
        print(request.user)
        user = User.objects.get(username=str(request.user))
        deployments = DeployRequest.objects.filter(owner=user.id)
        if len(deployments) == 0:
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            InfoList = []
            for deployment in deployments:
                InfoList.append(deployment.id)
            return Response(InfoList, status=status.HTTP_200_OK)

class DeployRequestDetailView(APIView):
    '''
        用户查询单个代部署申请详情
        /api/fundraises/{id}
    '''
    def get(self,request,pk,format=None):
        deployments = DeployRequest.objects.filter(id=pk,owner=request.user.id)
        if len(deployments)==0:
            return Response(status=status.HTTP_404_NOT_FOUND)
        else:
            deployment=deployments[0]
            deploymentInfo={}
            deploymentInfo['id'] = deployment.id
            deploymentInfo['name'] = deployment.name
            deploymentInfo['owner_address'] = deployment.owner_address
            deploymentInfo['target_money'] = str(deployment.target_money)
            deploymentInfo['ipfs_hashes'] = deployment.ipfs_hashes.split(',')
            deploymentInfo['state'] = deployment.state

            return Response(data=deploymentInfo,status=status.HTTP_200_OK)

class DeploymentRejectView(APIView):
    '''
    管理员拒绝代部署合约
    POST /api/fundraises/request/{id}/reject
    '''
    permission_classes = (IsAdminUser,)

    def post(self, request, pk, format=None):
        deployments = DeployRequest.objects.filter(id=pk)
        if len(deployments) == 0:
            return Response(status=status.HTTP_404_NOT_FOUND)
        deployment = deployments.get(id=pk)
        deployment.state = 2
        deployment.save()
        return Response(status=status.HTTP_200_OK)

class AllDeployRequestView(APIView):
    '''
    管理员查询所有代部署申请
    GET /api/fundraises/request
    '''
    permission_classes = (IsAdminUser,)

    def get(self,request,format=None):
        # 查询所有代部署的申请
        deployments = DeployRequest.objects.all()
        if len(deployments) == 0:
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            InfoList = []
            for deployment in deployments:
                InfoList.append(deployment.id)
            return Response(InfoList, status=status.HTTP_200_OK)

'''
 管理员查询单个代部署申请的信息
 GET api/fundraises/request/<int:pk>

'''
class OneDeployRequestView(APIView):
    permission_classes = (IsAdminUser,)
    def get(self,request,pk,format=None):
        deployment = DeployRequest.objects.get(id=pk)
        deploymentInfo={}
        deploymentInfo['id'] = deployment.id
        deploymentInfo['name'] = deployment.name
        deploymentInfo['owner_address'] = deployment.owner_address
        deploymentInfo['target_money'] = str(deployment.target_money)
        deploymentInfo['ipfs_hashes'] = deployment.ipfs_hashes.split(',')
        deploymentInfo['state'] = deployment.state

        return Response(data=deploymentInfo,status=status.HTTP_200_OK)

class AgreeDeployView(APIView):
    '''
    管理员同意代部署合约***：
    POST /api/fundraises/request/agreement/{id}
    (部署deploy一份新的fundraise合约）
    '''
    def post(self,request,pk,format=None):
        # 获取代部署申请的对象和信息
        try:
            deployment = DeployRequest.objects.get(id=pk)
        except:
            print("找不到对应的代部署申请!!!")
            return Response(status=status.HTTP_404_NOT_FOUND)
        web3=Web3(HTTPProvider('http://localhost:7545'))
        # 读取机构钱包私钥
        with open('BlockchainManagement/config/keyfile','r') as f:
            encrypted_key=f.read()
            private_key=web3.eth.account.decrypt(encrypted_key,'blockchain')

        name=deployment.name
        try:
            # 获取管理员对应的机构地址
            charityAddr = DeployedCharityContract.objects.all()[0].contractAddr
        except:
            return Response(data={"找不到机构合约！！"},status=status.HTTP_404_NOT_FOUND)
        # 获取对应的机构合约
        charity_contract = web3.eth.contract(address=charityAddr, abi=CharityConfig['abi'],
                                             ContractFactoryClass=ConciseContract)
        targetMoney=deployment.target_money
        # 部署合约花费的gas由机构钱包出
        result=deployOneFundraiseContract(web3,web3.eth.accounts[0],private_key, name, charityAddr,deployment.owner_address , targetMoney)

        if True in result:
            # 机构合约添加fundraise合约地址
            res = charity_contract.addFundraise(result[1], transact={'from': web3.eth.accounts[0]})
            deployment.state=1
            deployment.save()
            return Response(status=status.HTTP_200_OK)
        else:
            return Response({'detail': '部署募捐项目出错'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


from flask import Flask, render_template, request
from joblib import load
from tensorflow.keras.models import load_model
from sklearn.preprocessing import StandardScaler
import pickle
import pandas as pd

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/submit', methods=['POST'])
def submit():

    def represents_int(s):
        try:
            int(s)
            return True
        except ValueError:
            return False

    if request.method == 'POST':
        postcode = request.form['postcode']
        state = request.form['state']
        policies = request.form['policies']
        age = request.form['age']
        coast = request.form['coast']
        form = request.form['form']
        fire = request.form['fire']
        fire_prox = request.form['fire_prox']
        payment = request.form['payment']
        burglar = request.form['burglar']
        portable = request.form['portable']

        with open('structure.pkl', 'rb') as f:
            structure = pickle.load(f)
        sc = load('scaler.joblib')
        model = load_model('oversample_model.h5')
        df_ = pd.DataFrame(columns=structure)
        final = []

        if state == '' or postcode == '' or policies == '' or age == '' or coast == '' or form == '' or fire == ''\
                or fire_prox == '' or payment == '' or burglar == '' or portable == '' or \
                represents_int(postcode) is False or represents_int(age) is False:
            return render_template('index.html', message='Please enter required fields')
        else:
            final.append(int(postcode))
            if fire == 'True':
                final.append(1)
            else:
                final.append(0)
            if burglar == 'True':
                final.append(1)
            else:
                final.append(0)
            if portable == 'True':
                final.append(1)
            else:
                final.append(0)
            final.append(int(age))
            final.append(int(policies))

            states = [i[i.index("_") + len("_"):] for i in structure if i.startswith('state_')]

            for j, item in enumerate(states):
                if item == state or state not in states and j == len(states) - 1:
                    final.append(1)
                elif j < len(states):
                    final.append(0)

            forms = [i[i.index("_") + len("_"):] for i in structure if i.startswith('form_')]

            for j, item in enumerate(forms):
                if item == form:
                    final.append(1)
                else:
                    final.append(0)

            coasts = [i[i.index("_") + len("_"):] for i in structure if i.startswith('coast_')]

            for j, item in enumerate(coasts):
                if item == coast or coast not in coasts and j == len(coasts) - 1:
                    final.append(1)
                elif j < len(coasts):
                    final.append(0)

            fires = [i[i.rfind("_") + len("_"):] for i in structure if i.startswith('fire_housing_proximity_')]

            for j, item in enumerate(fires):
                if item == fire_prox or fire_prox not in fires and j == len(fires) - 1:
                    final.append(1)
                elif j < len(fires):
                    final.append(0)

            policies_count = [i[i.rfind("_") + len("_"):] for i in structure if i.startswith('previous_policies_')]

            for j, item in enumerate(policies_count):
                if item == policies or policies not in policies_count and j == len(policies_count) - 1:
                    final.append(1)
                elif j < len(policies_count):
                    final.append(0)

            payments = [i[i.rfind("_") + len("_"):] for i in structure if i.startswith('card_type_')]

            for j, item in enumerate(payments):
                if item == payment:
                    final.append(1)
                else:
                    final.append(0)

            a_series = pd.Series(final, index=structure)
            df_ = df_.append(a_series, ignore_index=True)

            x_target = sc.transform(df_)

            prediction = model.predict_classes(x_target)[0]

            if prediction == 0:
                return render_template('index.html', message='Your submission has been processed!')
            else:
                return render_template('index.html', message='Oh! :( There has been an error with your submission. '
                                                             'Please, contact a Lemonade assistant')

if __name__ == '__main__':
    app.run()


import os, sys
import json
from os.path import abspath, dirname, join

sys.path.append(dirname(abspath(__file__)))
sys.path.append(dirname(dirname(abspath(__file__))))
#FAB_BASE_DIR = os.path.dirname(os.path.abspath(__file__))

os.environ.setdefault('TA2_STATIC_TEST_MODE',
                      'False')
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
                      'tworavensproject.settings.local_settings')

import django
try:
    django.setup()
except Exception as e:
    print("WARNING: Can't configure Django. %s" % e)

from tworaven_apps.datamart_endpoints.static_vals import \
    (DATAMART_ISI_NAME,)
from tworaven_apps.datamart_endpoints.materialize_util import \
    (MaterializeUtil,)
from tworaven_apps.datamart_endpoints.tasks import make_materialize_call

def test_materialize():
    """run materialize"""
    user_workspace_id = 89

    sr_fname = join('.', 'input', 'isi_materialize_req_01.json')

    datamart_params = json.loads(open(sr_fname, 'r').read())

    mu = MaterializeUtil(DATAMART_ISI_NAME,
                         user_workspace_id,
                         datamart_params)

    if mu.has_error():
        print('error: ', mu.get_error_message())
    else:
        for key, val in mu.materialize_result.items():
            if not val:
                print(f'\n{key}: (none)')
            else:
                print('\n%s: %s' % (key, str(val)[:150]))

        #print('materialize_result', mu.materialize_result)
    #    print('success: ', mu.materialize_result)
    #mu.show_info()

def test_celery_materialize():
    """run materialize"""
    user_workspace_id = 21

    sr_fname = join('.', 'input', 'isi_materialize_req_01.json')

    datamart_params = json.loads(open(sr_fname, 'r').read())

    mu_info = make_materialize_call(\
                     DATAMART_ISI_NAME,
                     user_workspace_id,
                     datamart_params,
                     **dict(websocket_id='dev_admin'))

    if not mu_info.success:
        print('error: ', mu_info.err_msg)
    else:
        print('mu_info msg', mu_info.result_obj)

if __name__ == '__main__':
    test_celery_materialize()


import functools
import sympy as sp

def bondgraph_to_equations(graph):

    coords, params, relations, labels = _build_coordinates(graph)

    system = _matricize(coords, params, relations)

    return system, coords, params, labels

def _build_coordinates(graph):
    # coordinates
    E = []
    F = []
    P = []
    Q = []
    U = []
    params = []
    ctrl_params = {}
    relations = []

    n = 0
    globals_map = {}
    an = 0
    bn = 0
    coord_labels = {}
    params_labels = {}
    globals_labels = {}

    for node_id, node in graph.nodes.items():

        subs = []

        one_port = len(node.ports) == 1

        for port_id in node.ports:
            if one_port:
                label = node.name
            else:
                label = "{};{}".format(node.name, port_id)
            en = sp.symbols("e_{{{}}}".format(label))
            fn = sp.symbols("f_{{{}}}".format(label))
            pn = sp.symbols("p_{{{}}}".format(label))
            qn = sp.symbols("q_{{{}}}".format(label))
            coord_labels[(node_id, port_id)] = (node.name, [en, fn, pn, qn])
            E.append(en)
            F.append(fn)
            P.append(pn)
            Q.append(qn)
            subs += [("e_{}".format(port_id), en),
                    ("f_{}".format(port_id), fn),
                    ("p_{}".format(port_id), pn),
                    ("q_{}".format(port_id), qn)]

            n += 1

        if node.control_variables:
            for v in node.control_variables:
                un = sp.symbols("u_{{{}}}".format(str(len(ctrl_params))))
                ctrl_params[node_id, v] = un
                subs += [(v, un)]
                U.append(un)

        if node.local_params:
            for param, v in node.local_params.items():
                if isinstance(v, int) or isinstance(v, float):
                    subs.append((param, v))
                else:
                    p_an = sp.symbols("{}_{{{}}}".format(param, node.name))
                    params_labels[(node.name, param)] = p_an
                    params.append(p_an)
                    subs.append((param, p_an))
                    an += 1

        if node.global_params:
            for param in node.global_params:
                try:
                    p_bn = globals_map[param]
                except KeyError:
                    p_bn = sp.symbols("{}".format(param))
                    globals_map[param] = p_bn

                subs.append((param, p_bn))

        node_cr = _build_relations(node)
        relations += [r.subs(subs) for r in node_cr]

    subs, (E, F, P, Q) = _construct_edge_subs(graph, coord_labels, (E, F, P, Q))

    relations = [r.subs(subs).simplify() for r in relations]
    params += list(globals_map.values())
    DX = F + E
    X = Q + P

    return (X, DX, U), params, relations, (coord_labels, params_labels, globals_map)

def _build_relations(node):
    rels = []
    for string in node.constitutive_relations:
        iloc = 0
        iloc = string.find("_i", iloc)

        if iloc < 0:
            # just a plain old string; sympy can take care of it
            rels.append(sp.sympify(string))
            continue

        sloc = string.rfind("sum(", 0, iloc)

        if sloc < 0:
            # we have a vector equation here.
            for port_id in node.ports:
                rels.append(
                    sp.sympify(
                        string.replace("_i", "_{}".format(port_id))))
            continue

        else:
            tiers = 0

            next_open = string.find("(", sloc + 4)
            eloc = string.find(")", sloc + 4)
            while next_open > 0 or tiers > 0:
                if next_open < eloc:
                    tiers += 1
                    next_open = string.find("(", next_open)
                else:
                    tiers -= 1
                    eloc = string.find(")", eloc)

            if eloc < 0:
                raise ValueError("Unbalanced brackets", string)

            substring = string[sloc+4: eloc]
            terms = [substring.replace("_i", "_{}".format(p))
                     for p in node.ports]
            symstr = string[0:sloc] + "(" + " + ".join(terms) + string[eloc:]
            rels.append(
                sp.sympify(symstr)
            )

    return [r for r in rels if r != 0]

def _matricize(coords, params, relations):

    x, dx, u = coords

    A_dict = {}
    B_dict = {}
    J_dict = {}
    G_dict = {}
    F_dict = {}

    de_row = 0
    j_row = 0
    for rel in relations:

        rel_is_linear = True
        a_terms = {}
        b_terms = {}

        coeffs = extract_coefficients(rel, constants=params)

        for c in coeffs:
            if c in dx:
                a_terms[dx.index(c)] = coeffs[c]
            elif c in x:
                b_terms[x.index(c)] = coeffs[c]
            elif c.atoms() & set(u):
                rel_is_linear = False
                try:
                    G_dict[(de_row, 0)] -= c*coeffs[c]
                except KeyError:
                    G_dict[(de_row, 0)] = -c * coeffs[c]
            else:
                rel_is_linear = False
                try:
                    F_dict[(de_row, 0)] -= c*coeffs[c]
                except KeyError:
                    F_dict[(de_row, 0)] = -c * coeffs[c]

        if rel_is_linear and (a_terms and not b_terms):
            for col, v in a_terms.items():
                J_dict[(j_row, col)] = v
            j_row += 1
        elif rel_is_linear and (b_terms and not a_terms):
            for col, v in b_terms.items():
                J_dict[(j_row, col)] = v
            j_row += 1

        else:
            for col, coeff in a_terms.items():
                A_dict[(de_row,col)] = coeff

            for col, coeff in b_terms.items():
                B_dict[(de_row,col)] = coeff
            de_row += 1

    A = sp.SparseMatrix(de_row, len(dx), A_dict)
    B = sp.SparseMatrix(de_row, len(x), B_dict)
    J = sp.SparseMatrix(j_row, len(x), J_dict)
    G = sp.SparseMatrix(de_row, 1, G_dict)
    F = sp.SparseMatrix(de_row, 1, F_dict)

    return A, B, J, G, F

def _matricize_v2(coords, params, relations):
    x, dx, u = coords
    n = len(x)
    m = len(u)
    matrix = {}
    de_row = 0

    for rel in relations:

        rel_is_linear = True
        a_terms = {}
        b_terms = {}
        g_terms = {}
        coeffs = extract_coefficients(rel, constants=params)
        for c in coeffs:

            if c in dx:
                a_terms[dx.index(c)] = coeffs[c]
            elif c in x:
                b_terms[x.index(c)] = coeffs[c]
            elif c in u:
                g_terms[u.index(c)] = coeffs[c]
            else:
                rel_is_linear = False
                try:
                    matrix[(de_row, 2 * n + m)] += c * coeffs[c]
                except KeyError:
                    matrix[(de_row, 2 * n + m)] = c * coeffs[c]
        if rel_is_linear and (b_terms and not a_terms):
            for col, v in a_terms.items():
                matrix[(de_row, col)] = v
            de_row += 1
        else:
            for col, coeff in a_terms.items():
                matrix[(de_row, col)] = coeff
            for col, coeff in b_terms.items():
                matrix[(de_row, col + n)] = coeff
            for col, coeff in g_terms.items():
                matrix[(de_row, 2*n + col)] = coeff

            de_row += 1

    M, _  = sp.SparseMatrix(de_row, 2*n+1 + m , matrix).rref()

    return _decompose(M, (dx,x,u))

def _decompose(matrix, coords):
    dx,x, u =coords
    n = len(x)
    A = matrix[:, 0:n]
    B = matrix[:, n: 2*n]
    G = -matrix[:, 2*n]
    F = -matrix[:, 2*n+1]

    Q = sp.eye(n)

    for row in range(A.rows):
        col = row
        while col < n and A[row, col] == 0:
            col += 1

        if row < col < n:
            A.col_swap(row, col)
            Q.col_swap(row, col)

        if A[row, row] != 0:
            Q[:, row] *= 1/A[row, row]

    return A, B*Q, G, F

def _generate_Q(matrix):

    n = matrix.cols
    Q = sp.SparseMatrix.eye(n)

    for row in range(matrix.rows):

        for col in range(row, n):
            if matrix[row, col] != 0:
                if col > row:
                    Q.col_swap(row, col)

                Q[:, row] /= matrix[row, col]
                break

    return Q

def null_space_basis(matrix):
    """
    Generates the basis vectors of the specified matrix
    Results are stored in a N x M matrix
    of the form :math:`(b_0 | b_1 | \ldots | b_M)` where :math:`b_j` is the jth
    basis vector of :math:`R^N`

    Args:
        matrix: The matrix to generate the basis from.

    Returns:
        Projector from nullspace :math:`P:R^M \rightarrow R^N`

    """
    R = smith_normal_form(matrix)
    P = R - sp.SparseMatrix.eye(R.rows)
    j = 0
    while j < P.cols:
        if P.col(j).is_zero:
            P.col_del(j)
        else:
            j += 1
    return P

def _construct_edge_subs(graph, coord_labels, coords, keep_dirs=False):
    """
    Generates the substitution list for the bond graph.

    When the bond graph comes in, it is specified in terms of the power across
    each port of each node. This method uses the bond wiring to determine a
    substitution list to chance the co-ordinate system so that it is in terms
    of the power balance for each bond; halving the co-ordinate space.

    Args:
        graph: The bond graph
        coord_labels: dictionary of co-ordinate information
        coords: The nodal co-ordinate space.
        keep_dirs (False): Set to true if to preserve arrowhead direciton

    Returns:
        a list of substition rules, and a new set of co-ordinates

    """

    E, F, P, Q = coords
    edge_list = [((n1, p1), (n2, p2)) for n1, n2, p1, p2 in graph.bonds]

    node_list = sorted([(k,v) for k,v in graph.nodes.items()],
                       key=lambda x: len(x[1].ports))
    subs = []

    while node_list:
        from_node, node = node_list.pop()
        edges = [e for e in edge_list if e[0][0] == from_node or e[1][0] == from_node]

        while edges:
            edge = edges.pop()
            (n1, po1), (n2, po2) = edge
            edge_list.remove(edge)

            # ed is  e-discard, ek is e-keep
            if n1 == from_node or keep_dirs:
                _,  (ed, fd, pd, qd) = coord_labels[(n1, po1)]
                _,  (ek, fk, pk, qk) = coord_labels[(n2, po2)]
            else:
                _, (ed, fd, pd, qd) = coord_labels[(n2, po2)]
                _, (ek, fk, pk, qk) = coord_labels[(n1, po1)]

            subs += [
                (ed, ek), (fd, -fk), (pd, pk), (qd, -qk)
            ]
            E.remove(ed)
            F.remove(fd)
            P.remove(pd)
            Q.remove(qd)

    return subs, (E, F, P, Q)

def smith_normal_form(matrix):
    """
    Assume n >= m
    Args:
        matrix:

    Returns:
    n x n smith normal form of the matrix.
    Particularly for projection onto the nullspace of M and the orthogonal
    complement
    that is, for a matrix M,
    P = _smith_normal_form(M) is a projection operator onto the nullspace of M
    """
    M, _ = matrix.rref()
    m, n = M.shape
    Mp = sp.MutableSparseMatrix(0, n, [])
    row = 0
    ins = 0

    while row < m:
        col = row
        while col + ins < n and M[row, col + ins] == 0:
            col += 1
        if col >= row + ins:
            Mp = Mp.col_join(sp.zeros(col - row, n))
            ins += col - row
        Mp = Mp.col_join(M.row(row))
        row += 1

    m, n = Mp.shape

    if m < n:
        Mp = Mp.col_join(sp.zeros(n-m, n))
    return Mp

def is_linear(equation, basis):
    """
    Check to see if an equation is linear.

    Args:
        equation: Sympy Equation
        basis: A vector of basis co-ordinates

    Returns:
        True if the equation is linear, false if not

    """
    if isinstance(equation, sp.Add):
        for arg in equation.args:
            if not is_linear(arg, basis):
                return False
    elif isinstance(equation, sp.Mul):
        if sum([a.atoms() & set(basis) != set() for a in equation.args]) > 1:
            return False
        for arg in equation.args:
            if not is_linear(arg, basis):
                return False
    elif isinstance(equation, sp.Number) or isinstance(equation, sp.Symbol):
        return True
    else:
        return False
    return True

def linearise(equation, X, X0=None):
    """

    Args:
        equation:
        X:
        X0:

    Returns:

    """
    if not X0:
        X0 = [0 for _ in X]

    grad = [sp.diff(equation, x) for x in X]
    return [functools.reduce(lambda eq, pair: eq.subs(*pair),
            zip(X, X0), partial) for partial in grad]

def extract_coefficients(equation, constants=None):

    coeff_dict = {}

    for term in equation.expand().args:

        products = flatten(term.as_coeff_mul())

        coeff = sp.S(1)
        base = sp.S(1)

        for factor in products:
            if factor.is_number or factor in constants:
                coeff = sp.Mul(coeff, factor)
            else:
                base = sp.Mul(base, factor)

        coeff_dict[base] = coeff
    return coeff_dict

def flatten(sequence):
    for item in sequence:
        if isinstance(item, (list, tuple)):
            for subitem in flatten(item):
                yield subitem
        else:
            yield item


'''
原始 PCM 的录音参数必须符合 8k/16k 采样率、
16bit 位深、单声道，支持的格式有：pcm（不压缩）、wav（不压缩，pcm编码）、amr（压缩格式）
http://yuyin.baidu.com/docs/asr/190
'''

from aip import AipSpeech
from pydub import AudioSegment
import os
import datetime
starttime = datetime.datetime.now()
currentdir = os.getcwd()

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
def save_to_file(file_name, contents):
    fh = open(file_name, 'w')
    fh.write(contents)
    fh.close()
def get_relative_time():
    return datetime.datetime.now()-starttime
def loop_and_asr_all(filenum,client):
    # filenum = return value of load_and_divide_wav function
    resultstr = str()
    for time in range(1,filenum+1):
        print('*****Trail',time,'Transforming*****')
        print('TIME:',get_relative_time())
        tempname = currentdir + '/TEMPWAV/seg' + str(time) + '.wav'
        tempresult = client.asr(get_file_content(tempname), 'wav', 16000, {'dev_pid': '1536',})
        print('Trail',time,'GETING:','\n',tempresult)
        if tempresult['err_msg'] != 'success.':
            print('ERROR:',tempresult['err_msg'])
            break
        resultstr = resultstr + tempresult['result'][0]
    return resultstr
def load_and_divide_wav(wavfilename,SecPerFile):#SecPerFile: MAX 59.9
    # WAV LOAD and Get LENGTH in seconds
    wavload = AudioSegment.from_wav(wavfilename) # converted to AudioSegment Object
    wavLenSEC = wavload.duration_seconds
    dividetimes = int(wavLenSEC/SecPerFile)+1
    print(' Wave Length[s]: ',wavLenSEC,'\n','Divide Times: ', dividetimes)
    # WAV FILE CUT: EACH 50s
    print('***DIVIDING WAV FILE***')
    print('Segments:',dividetimes)
    for time in range(1,dividetimes+1):
        tempname = currentdir + '/TEMPWAV/seg' +str(time) + '.wav'
        wavload[SecPerFile*(time-1)*1000:SecPerFile*time*1000].export(tempname,format='wav')
    print('***DIVIDING COMPLETE***')
    return dividetimes

def get_client():
# Input APP_ID & API_KEY & SECRET_KEY Here
    APP_ID = ''
    API_KEY = ''
    SECRET_KEY = ''
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    return client

# WAV FILENAME INPUT
wavname = input('[TYPE:WAV] FILENAME: ')
wavinput = wavname + '.wav'
if len(wavname)<1 : wavinput = 'test.wav'
print('Analysing WAV File:',wavinput)

# Divide File into small pieces
dividetimes = load_and_divide_wav(wavinput,50)
# Get ASR result as a string: resultstr
resultstr = loop_and_asr_all(dividetimes,get_client())

# Print result
print('TIME:',get_relative_time())
print('ASR Result: ',resultstr)
savename = 'RESULT'+ wavname +'.txt'
save_to_file(savename,resultstr)
print('File has been saved to',currentdir)


''' Web Routes '''
from masonite.helpers.routes import get, post, group

ROUTES = [
    get('/', 'HomeController@index').name('welcome'),
    get('/login', 'LoginController@show').name('login'),
    post('/login', 'LoginController@store'),
    get('/register', 'RegisterController@show'),
    post('/register', 'RegisterController@store'),
    get('/questions/@id', 'QuestionController@show'),

    # auth middleware
    get('/logout', 'LoginController@logout').middleware('auth'),
    get('/ask', 'QuestionController@create').middleware('auth'),
    group('/questions', [
        post('', 'QuestionController@store').middleware('auth'),
        post('/@id/answers', 'AnswerController@store').middleware('auth'),
        get('/@id/upvote', 'QuestionController@upvote').middleware('auth'),
        get('/@id/downvote', 'QuestionController@downvote').middleware('auth'),
    ]),
    get('/me/questions', 'QuestionController@questions').middleware('auth'),
    get('/me/answers', 'AnswerController@answers').middleware('auth'),
]


from  path import Path
import imageio
import cv2

root = Path('/home/roit/datasets/2019c/car/cut')
dump_root = Path('./car_cut_256512')
dump_root_dir = dump_root.makedirs_p()

folderName = Path(root)
if folderName.exists()==False:
	print('folder no exist')
files = folderName.files('*.{}'.format('png'))
files.sort()
size = (512,256)
frames =[]
for img_p in files:
    img = cv2.imread(img_p)
    img = cv2.resize(src=img,dsize=size,interpolation=cv2.INTER_AREA)
    cv2.imwrite(dump_root_dir/img_p.stem+'.jpg',img)
    frames.append(img)
    print('ok')


from decimal import Decimal
from django.conf import settings
from category.models import Item

class Order(object):
	def __init__(self, request):
		self.session = request.session
		order = self.session.get(settings.ORDER_SESSION_ID)
		if not order:
			order = self.session[settings.ORDER_SESSION_ID] = {}
		self.order = order

	def add(self, item, quantity=1, update_quantity=False):
		item_id = str(item.id)
		if item_id not in self.order:
			self.order[item_id] = {'quantity': 0, 'weight': str(item.weight)}
		if update_quantity:
			self.order[item_id]['quantity'] = quantity
		else:
			self.order[item_id]['quantity'] += quantity
		self.save()

	def save(self):
		self.session[settings.ORDER_SESSION_ID] = self.order
		self.session.modified = True

	def remove(self, item):
		item_id = str(item.id)
		if item_id in self.order:
			del self.order[item_id]
			self.save()

	def __iter__(self):
		item_ids = self.order.keys()
		items = Item.objects.filter(id__in=item_ids)
		for item in items:
			self.order[str(item.id)]['item'] = item

		for item in self.order.values():
			item['weight'] = Decimal(item['weight'])
			item['total_weight'] = item['quantity'] * item['weight']
			yield item

	def __len__(self):
		return sum(item['quantity'] for item in self.order.values())

	def get_total_weight(self):
		return sum(Decimal(item['weight'])*item['quantity'] for item in self.order.values())

	def clear(self):
		del self.session[settings.ORDER_SESSION_ID]
		self.session.modified = True


#!/usr/bin/env python
# -*- coding: utf-8 -*

# Coded for Python 3 (tested under Python 3.5 for Windows 64-bit)
#
# Code by genBTC. Created from scratch 10/6/2015.  Relies on other pre-requisite steps 1,2 that I scripted also.
#
# Version 0.1 - functional since 10/9/2015.
# Version 0.1a - now uses directory paths obtained from settings, ss.Preferences() 10/14/2015
# Version 0.1b - Changed code to target Python 3 @ 10/16/2015
#
# Script #3: Renames all your what.cd downloads to have a proper and uniform name.
#       All naming decisions happen here. You should/must edit this file to suit yourself, especially "Sorted_Record_Labels_List"
#       This program takes the results of Script #1,#2  and re-builds from scratch the desired filename.
#       This was built specific to my needs, the what and why of which needs explaining:
#
# FUNCTIONALITY notes:
#
#   1. The Sorted_Record_Labels_List is a list of record labels that I wish to store SORTED by the catalog number such as [DMZ001] (at the start of the filename)
#       this causes the program to omit output of a {Digital Mystikz} tag near the end, as its obvious to me because I have these stored in named folders.
#   1b. Any Record labels not part of this list will have the {Record Label} tag at the end BUT the catalog number omitted entirely.
#   1c. In the catalog number I removed all spaces, and converted it all to uppercase.
#   2. Much confusion arose from "Torrentobj.torrent.remastered" Edition which is a flag where different editions can have 4 modified fields: (year,name) and 2 of which (catalog,tor.group.recordLabel)
#       needed special handling.  Upon careful inspection, about 1% of these had flawed data in some way, and the code is very branched and unclear, although i did my best.
#       I handled as many edge cases as possible while trying to ascertain whether to use the original fields or the tor.torrent.remastered edition fields.
#       You would want to debug how these "Guesses" apply to your library and I am working on a way to delay changes until the end for confirmation or something.
#   3. The ReleaseType field (EP, Album) - fully listed below - I wanted to be handled so it would be displayed in [] on all files, but [Album] seemed redundant to me.
#       EP also got redundant as some albums are named as "The Something EP" [EP] so if the name ended in EP, i just omitted the bracketed [EP] as well.
#   3b. The (Blu-ray - FLAC - 24bit Lossless) seemed too long so i settled on just [FLAC 100%] for log FLACs or just [FLAC] and I dont have many 24-bits, so I skipped it.
#   3c. For MP3, I omitted the word MP3, and for VBR i omitted the words VBR. example: [WEB 320] [CD V0]
#   3d. For Torrentobj.torrent.scene releases, i just replaced the word [WEB] with [Torrentobj.torrent.scene] since its redundant to me, and there was no easy way to grab the Torrentobj.torrent.scene Group's name.
#   3e. For AAC, i searched the tor.torrent.description and tor.torrent.filepath (what the original torrent owner names it) for 'itunes' and if found, tagged it as [iTunes AAC].
#       tor.torrent.filePath is the field that goes into uTorrent, but is too unpredictable overall - even though it can contain valid info found nowhere else sometimes.
#       (actually, that field is what this program is attempting to correct, and there will be a followup program to re-import the new name back into uTorrent to seed)
#   4. The last thing the program does is re-copy all the tor.torrent.infoHash JSON files into the new-proper-name filenames, JUST FOR REFERENCE to have later. (easily deletable)
#       My advice to read these humanly, is to use Notepad++ with the plugin called JSON Viewer (http://nppjsonviewer.sourceforge.net/) or some other JSON viewer.
#       To use: CTRL+A (Select All) and CTRL+ALT+SHIFT+M (Torrentobj.torrent.format as JSON). Don't resave it, or there will be a ton of \t tabs and \n newlines in the saved file and will screw it up.
#   4b. Please note that simply renaming a .torrent file to the new-names will not affect the Bencoded tor.torrent.filePath inside and thus will not seed to that location, and there is no way
#       to modify the tor.torrent.filePath inside the torrent even with a BEncode Editor, because this will change the value of the tor.torrent.infoHash and will come up as "Torrent not recognized"
#
# PROGRAMMATICALLY, the following is explained:
#
#   1. Useful comments were left along the way as i went
#   2. My python is mediocre at best, so if you're wondering why I coded it Z way instead of X or Y its because I however it came to mind is how it got coded.
#       And step-by-step readability was preferred over abstraction.
#   3. I went through hell with the ascii vs unicode vs mbcs filesystem encodings and HTML-unescaped JSON, and this is just how I cobbled it all together to work.
#   4. While I NEEDED to grab the artist from the torrent filename, for some reason I got the album, year from it. This is for no reason.
#   5. The filenames needed to be sanitized from Windows's banned character list of \ / : * ? " < > | and have been replaced by Unicode looking clones
#       except the ? which I could only find a backwards looking replacement for.

# CODING TODO LIST:
# instead of  grabbing album,year from filename, grab from named hash JSON-dump file
# think of better naming concepts
# an idea: $1 $2 $3 beginning middle end, select which pieces of the program to write as the new name
# might be a good idea to create an undo.dat file
# *DONE*maybe sort the master list output file alphabetically
# save list of  files the program modified unusually
# wait till the end to display list of errors
# *DONE*refactor->create a class or wrapper function to hold the response[] (the shell of which can be borrowed from torrent-torrent.py,torrent-group.py)
# *DONE*(see bottom)profile performance and figure out what is the most cpu intensive part, and improve performance
# use multiple threads to process many things at once. (Caps out at 12% per Core) so 8 threads would be way faster, and SSD usage is at a minimum (1-5%)
# Figure out a better way to reduce the exponential nature of this file (first loop has X entries, nested loop has X entries, so thats X^2).
# Unify the encoding schemes and remove as many conversions as possible and just stick to unicode for everything.
# *DONE*create better way to interface with people other than myself / editing source code magic strings (added settings.ini Config file)

import bencode
import base64
import sys
import os
import hashlib
try:
    from urlparse import urlparse
except:
    import urllib.parse as urlparse
import time
import json
from enum import Enum
import ntpath
import re
import codecs
try:
    import HTMLParser
except:
    import html.parser as HTMLParser
import shutil
import difflib
from torrentclass import Torrent
from settings import Preferences

class ReleaseType(Enum):
    Unspecified = 0
    Album = 1
    Soundtrack = 3
    EP = 5
    Anthology = 6
    Compilation = 7
    DJ_Mix = 8
    Single = 9
    Live = 11
    Remix = 13
    Bootleg = 14
    Interview = 15
    Mixtape = 16
    Unknown = 21
    Concert = 22
    Demo = 23

Sorted_Record_Labels_List = ["abducted",
"abducted dub",
"abducted limited",
"abducted records",
"armada captivating",
"armada deep",
"big apple",
"butterz",
"buygore",
"chestplate",
"circus",
"deep medi",
"disciple",
"dmz",
"digital mystikz",
"dub police",
"eatbrain",
"firepower",
"hyperdub",
"kannibalen",
"kapsize",
"luckyme",
"mainstage music",
"mau5trap",
"monstercat",
"musical freedom",
"nest",
"never say die",
"nsd:",
"osiris music",
"owsla",
"play me",
"prime audio",
"revealed",
"rottun",
"smog",
"storming",
"sub soldiers",
"subhuman",
"swamp81",
"swamp 81",
"symbols",
"tempa",
"ultragore",
"zouk" ]

class TorrentEntry:
    def __init__(self):
        self.hash = None
        self.pathname = None
        self.filename = None
        self.artistalbum = None
        self.torrentid = None
        self.createdpropername = None
        self.fmttdMediaEncodeFormat = ""

def getUniqueWords(iterable):
    seen = set()
    result = []
    for item in iterable:
        if item.lower() not in seen:  # compare lowercases
            seen.add(item.lower())
            result.append(item)  # but preserve actual case
    return result  # return a list (in order)

def main():

    global fmttdcatalogueNumber     #to fix an issue with scope (line 330,333).

    ss = Preferences()
    hashtorrlistfile = ss.getwpath("outpath1")
    directory_path = ss.getwpath("script2destdir")   #as source dir (hash-grabs)
    allfiles = [os.path.join(directory_path, filename) for filename in next(os.walk(directory_path))[2]]  # gives absolute paths + names

    hashtofilenamefolder = ss.getwpath("script3destdir") #as dest dir (hash-grabs-as-filenames)
    writelistfile = codecs.open(ss.getwpath("outpath3"), 'wb', "utf-8")  # write-out a text file with one entry per line. main output file (3propernames.txt)

    writelistcontainer = []
    currentfilenumber = 1

    for hashidfilename in allfiles:  # iterate through filenames of what.cd JSON data

        with open(hashidfilename, 'r') as stringfile:  # open them
            needFixLabeltoNewEdition = False

            jsonresponse = json.load(stringfile)
            tor = Torrent(jsonresponse)

            if tor.group.categoryName != "Music":
                continue  # do not continue altering any non-music torrents.

            releaseTypeName = ReleaseType(tor.group.releaseType).name  # turn int. value into a string using the enum class above
            fmttdreleaseTypeName = "[" + releaseTypeName + "]"

            if tor.torrent.remastered:
                if tor.torrent.remasterTitle:
                    tor.group.name += " (" + tor.torrent.remasterTitle + ")"
                if tor.torrent.remasterYear > tor.group.year:
                    tor.group.year = tor.torrent.remasterYear
                if tor.torrent.remasterRecordLabel:
                    if tor.group.recordLabel.lower() != tor.torrent.remasterRecordLabel.lower():  # so not case sensitive
                        if not tor.group.recordLabel:
                            tor.group.recordLabel = tor.torrent.remasterRecordLabel
                        else:
                            # then things get complicated and we need to figure out which Label/catalog field is the best one to use, or combine them or both
                            needFixLabeltoNewEdition = True

                # if its been determined that its a remaster (new edition), process new label and catalog
                #  checking whether to combine with old, or which one to use, etc, etc.....
                if needFixLabeltoNewEdition == True:

                    score = difflib.SequenceMatcher(None, tor.group.recordLabel.lower(), tor.torrent.remasterRecordLabel.lower()).ratio()
                    if (score < 0.5):
                        # considered similar at 0.6 but this way is not that accurate. if they are lower than 0.5 similar, just use the new one
                        tor.group.recordLabel = tor.torrent.remasterRecordLabel
                        if tor.torrent.remasterCatalogueNumber:
                            tor.group.catalogueNumber = tor.torrent.remasterCatalogueNumber  # if tor.torrent.remasterCatalogueNumber is not blank, use it as the new tor.group.catalogueNumber

                    elif all(word in tor.torrent.remasterRecordLabel.lower() for word in tor.group.recordLabel.lower()) \
                            or \
                            all(word in tor.group.recordLabel.lower() for word in tor.torrent.remasterRecordLabel.lower()):
                        # If all the words in the old label is encompassed in the new one, use the new one.
                        # This would mean the new edition record label is most likely longer and is similar enough to use that,
                        # and preferred, since its more applicable to this specific release anyway.
                        # Even if the reverse is true, this code-block should only catch labels that differ in slight ways.(?)

                        # example 1: originallabel={Big Beat Records}  remasterlabel={Big Beat}          #elif new label in old label
                        #   result:     {Big Beat}                                                      #or
                        # example 2: originallabel={Big Beat}  remasterlabel={Big Beat Records}          #if old label in new label
                        #   result:     {Big Beat Records}                                              #then
                        tor.group.recordLabel = tor.torrent.remasterRecordLabel  # always choose new label.
                        if tor.torrent.remasterCatalogueNumber:
                            tor.group.catalogueNumber = tor.torrent.remasterCatalogueNumber  # if tor.torrent.remasterCatalogueNumber is not blank, use it as the new tor.group.catalogueNumber
                    else:
                        # This else-block is used when the above are not true, and we can't decide on which one to use, so we use both. Combined.

                        # example 1: originallabel={Island Records}  remasterlabel={Island Records / Lokal Legend}
                        #   result:     {Island Records / Lokal Legend}
                        # example 2: originallabel {Wall Recordings}  remasterlabel={Tiger Records}
                        #   result:     {Wall Recordings / Tiger Records}
                        new = ""
                        splitorig = re.sub("[(,),-]", " ",
                                           tor.group.recordLabel).split()  # remove delimiter chars that mess up stuff
                        sepchar = ["/"]
                        splitnew = re.sub("[(,),-]", " ", tor.torrent.remasterRecordLabel).split()  # turn everything into a list

                        new = " ".join(["%s" % (v) for v in getUniqueWords(
                            splitorig + sepchar + splitnew)])  # append unique words to the orig.

                        if tor.torrent.remasterCatalogueNumber:
                            if tor.group.catalogueNumber != tor.torrent.remasterCatalogueNumber:
                                if tor.group.catalogueNumber:
                                    tor.group.catalogueNumber += " / " + tor.torrent.remasterCatalogueNumber
                                else:
                                    tor.group.catalogueNumber = tor.torrent.remasterCatalogueNumber
                        tor.group.recordLabel = new

            # ntpath.basename was really slow so doing it manually.... (32 times faster)= 0.128 seconds vs 0.004 seconds
            # whats happening here is due to an exponential nested for loop, ie: 4129 results^2 = 17 million function calls of either basename or .rfind('\\')
            # there should be a better way to do this.
            hashfilesepidloc = hashidfilename.rfind("\\") + 1
            cmphashfn = hashidfilename[hashfilesepidloc:]
            iterhashfile = open(hashtorrlistfile, 'r',encoding='utf-8').readlines()  # read everything into memory
            for i in iterhashfile:  # read line
                splitline = i.strip().split(' / ')      # 0 torrentID / 1 Hash / 2 torrentfilename
                if splitline[1] == cmphashfn:  # if it matches, start processing
                    newEntry = TorrentEntry()  # instanciate class
                    newEntry.hash = splitline[1]  # store Hash for reference
                    newEntry.pathname = splitline[2]  # filename + extension
                    locextension = newEntry.pathname.find(".torrent")  # location of extension
                    locid = newEntry.pathname.rfind("-") + 1  # location of tor.torrent.id
                    newEntry.filename = newEntry.pathname[:locextension]  # chop the extension off (manually)
                    newEntry.artistalbum = newEntry.filename[:locid - 1]  # JUST the name (no ID#)
                    newEntry.torrentid = newEntry.filename[locid:locextension]  # grab ID for future reference (tor.torrent.id on what.cd)
                    # example : S-Type - Billboard (Lido Remix) - 2014 (WEB - MP3 - 320)
                    newEntry.artist = newEntry.artistalbum[:newEntry.artistalbum.find(" - ")]  # grab artist
                    tempalbum = newEntry.artistalbum[newEntry.artistalbum.find(" - ") + 3:]  # temp value helps with string processing
                    newEntry.album = tempalbum[:tempalbum.find(" - ")]  # not needed since it can be pulled from [group]
                    newEntry.year = tempalbum[tempalbum.find(" - ") + 3:tempalbum.find(" - ") + 7]  # not needed since it can be pulled from [group]

                    # ------------Recreate name------------#
                    # -------Special RULES SECTION---------#
                    newEntry.createdpropername = newEntry.artist + " - " + tor.group.name + " "
                    if tor.group.releaseType > 1:  # dont put it for Album or Unspecified
                        if tor.group.releaseType != 5:  # do something different for EP
                            newEntry.createdpropername += fmttdreleaseTypeName + " "
                        else:  # make a rule so [EP] doesnt come up if there is " EP " already
                            if tor.group.name[-2:] != "EP":
                                newEntry.createdpropername += fmttdreleaseTypeName + " "

                    newEntry.createdpropername += "(" + str(tor.group.year) + ")"

                    # written like this for easy humanreading
                    #           format = MP3, FLAC, AAC,
                    #          media = cd, web, vinyl, soundboard, dat
                    #        encoding = lossless,320,v0,256,v2,192
                    if tor.torrent.format == "FLAC":
                        newEntry.fmttdMediaEncodeFormat = "FLAC"
                        # log and logscore only applicable to flac.
                        if tor.torrent.hasLog:
                            newEntry.fmttdMediaEncodeFormat += " " + str(
                                tor.torrent.logScore) + "%"  # the % implies "log" so leave out the word log
                    if tor.torrent.format == "AAC":
                        if (any("itunes" in word.lower() for word in tor.torrent.description.split())) or (
                        any("itunes" in word.lower() for word in tor.torrent.filePath.split())):
                            newEntry.fmttdMediaEncodeFormat = "iTunes "
                        newEntry.fmttdMediaEncodeFormat += "AAC"
                    if tor.torrent.format == "MP3":
                        # dont actually write mp3
                        # only write Scene or WEB if it is an mp3
                        if tor.torrent.scene:
                            newEntry.fmttdMediaEncodeFormat += "Scene"
                        elif tor.torrent.media == "WEB":
                            newEntry.fmttdMediaEncodeFormat += "WEB"
                        else:
                            newEntry.fmttdMediaEncodeFormat += tor.torrent.media
                        if "VBR" in tor.torrent.encoding:
                            newEntry.fmttdMediaEncodeFormat += " " + tor.torrent.encoding[:2]
                        else:
                            newEntry.fmttdMediaEncodeFormat += " " + tor.torrent.encoding

                    newEntry.fmttdMediaEncodeFormat = "[" + newEntry.fmttdMediaEncodeFormat + "]"

                    newEntry.createdpropername += " " + newEntry.fmttdMediaEncodeFormat

                    # put catalog number in brackets
                    if tor.group.catalogueNumber:
                        fmttdcatalogueNumber = ("[" + tor.group.catalogueNumber + "]").replace(" ", "").upper()
                    elif tor.group.recordLabel:
                        fmttdcatalogueNumber = "[" + tor.group.recordLabel + "]"  # combines with next part to put recordLabel in the front if Cat# missing

                    if any(word in tor.group.recordLabel.lower() for word in Sorted_Record_Labels_List):  # so not case sensitive
                        newEntry.createdpropername = fmttdcatalogueNumber + " " + newEntry.createdpropername
                    elif tor.group.recordLabel:
                        newEntry.createdpropername += " " + "{" + tor.group.recordLabel + "}"
                        # if tor.group.catalogueNumber:                                                       #This will put [CATA###] after all releases, even labels not in your list
                        #     newEntry.createdpropername += " " + fmttdcatalogueNumber              #Gets kind of cumbersome for me. (uncomment to use it anyway)

                    # these 2 lines are a quick fix, for an oversight in my naming process
                    # if these are single file .mp3's (or a single .flac) they will need a .mp3 at the end of the filename
                    if not tor.torrent.filePath:
                        newEntry.createdpropername += "." + tor.torrent.format.lower()

                    try:
                        print(currentfilenumber, newEntry.createdpropername.encode('ascii', errors='ignore').decode())
                    except:
                        print("COULD NOT PRINT UNICODE FILENAME TO CONSOLE. HASH=", tor.torrent.infoHash)

                    ########-------------replace characters section----------------#########
                    newEntry.createdpropername = newEntry.createdpropername.replace("\\","＼")  # U+FF3C               FULLWIDTH REVERSE SOLIDUS
                    # these forward slashes are strange. "FullWidth" is very wide and would be too wide if theres already spaces around it.
                    newEntry.createdpropername = newEntry.createdpropername.replace(" / ","／")  # U+FFOF  (wide)       FULLWIDTH SOLIDUS
                    # "Division" slash is too narrow and needs spaces inserted surrounding it (and is still less width than the fullwidth)
                    newEntry.createdpropername = newEntry.createdpropername.replace("/"," ∕ ")  # U+2215  (narrow)     DIVISION SLASH
                    newEntry.createdpropername = newEntry.createdpropername.replace(":","꞉")  # U+A789               MODIFIER LETTER COLON
                    newEntry.createdpropername = newEntry.createdpropername.replace("*","※")  # U+203B               REFERENCE MARK
                    newEntry.createdpropername = newEntry.createdpropername.replace("?","؟")  # U+061F               ARABIC QUESTION MARK
                    newEntry.createdpropername = newEntry.createdpropername.replace('"',"ʺ")  # U+02BA               MODIFIER LETTER DOUBLE PRIME
                    newEntry.createdpropername = newEntry.createdpropername.replace("<","˂")  # U+02C2               MODIFIER LETTER LEFT ARROWHEAD
                    newEntry.createdpropername = newEntry.createdpropername.replace(">","˃")  # U+02C3               MODIFIER LETTER RIGHT ARROWHEAD
                    newEntry.createdpropername = newEntry.createdpropername.replace("|","ǀ")  # U+01C0               LATIN LETTER DENTAL CLICK
                    #####--windows filename banned chars replacement with unicode--#########

                    ######----------HashGrabs-as-Filenames--------########
                    # File output. Move all files named as hashes to a new dir as the proper name
                    if not os.path.exists(hashtofilenamefolder + newEntry.createdpropername):
                        shutil.copy(hashidfilename, hashtofilenamefolder + newEntry.createdpropername)

                    currentfilenumber += 1
                    #####------------make propernames.txt (has the hash in it also) ---------########
                    # Add it to the container (since this is in a loop)
                    writelistcontainer.append(newEntry.createdpropername + " / " + tor.torrent.infoHash + "\n")
    ##File Output. The Master List file of everything.##
    # when the loop exits, Sort it, and write it to the file.
    writelistcontainer.sort()
    for eachline in writelistcontainer:
        writelistfile.write(eachline)
    writelistfile.close()

if __name__ == "__main__":
    main()

    # PROFILING:
    # if __name__ == "__main__":
    #     import cProfile, pstats
    #     cProfile.run("main()", "{}.profile".format(__file__))
    #     s = pstats.Stats("{}.profile".format(__file__))
    #     s.strip_dirs()
    #     s.sort_stats("time").print_stats(25)

    # Sun Oct 11 00:19:57 2015    create-proper-names.py.profile
    #          35543736 function calls (35543709 primitive calls) in 28.724 seconds
    #    Ordered by: internal time
    #    List reduced from 171 to 10 due to restriction <10>
    #
    #    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    #         1   14.085   14.085   28.723   28.723 create-proper-names.py:175(main)
    #  17309720    4.716    0.000    4.716    0.000 {method 'rfind' of 'unicode' objects}
    #      4161    4.478    0.001    4.478    0.001 {method 'readlines' of 'file' objects}
    #  17313841    1.725    0.000    1.725    0.000 {method 'strip' of 'str' objects}
    #     16565    1.007    0.000    1.007    0.000 {open}
    #      4120    0.501    0.000    1.700    0.000 shutil.py:66(copyfile)
    #     16481    0.492    0.000    0.492    0.000 {nt.stat}
    #      4163    0.349    0.000    0.349    0.000 decoder.py:372(raw_decode)
    #     12418    0.251    0.000    0.251    0.000 {method 'read' of 'file' objects}
    #      4120    0.152    0.000    0.152    0.000 {nt.chmod}


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

import os
import sys
import time
import argparse
import logging
import requests
from BeautifulSoup import BeautifulSoup

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
loghandler = logging.StreamHandler(sys.stderr)
loghandler.setFormatter(logging.Formatter("[%(asctime)s] %(message)s"))
log.addHandler(loghandler)

base_url = "http://www.tripadvisor.com/"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"

parser = argparse.ArgumentParser(description='Scrape tripadvisor')
parser.add_argument('-datadir', type=str,
                    help='Directory to store raw html files',
                    default="data/")
parser.add_argument('-state', type=str,
                    help='State for which the hotel data is required.',
                    required=True)
parser.add_argument('-city', type=str,
                    help='City for which the hotel data is required.',
                    required=True)
args = parser.parse_args()

def get_city_page(city, state):
    """ Returns the URL of the list of the hotels in a city. Corresponds to
    STEP 1 & 2 of the slides.

    Parameters
    ----------
    city : str

    state : str

    Returns
    -------
    url : str
        The relative link to the website with the hotels list.

    """
    # Build the request URL
    url = base_url + "city=" + city + "&state=" + state
    # Request the HTML page
    headers = {'User-Agent': user_agent}
    response = requests.get(url, headers=headers)
    html = response.text.encode('utf-8')
    with open(os.path.join(args.datadir, city + '-tourism-page.html'), "w") as h:
        h.write(html)

    # Use BeautifulSoup to extract the url for the list of hotels in
    # the city and state we are interested in.

    # For example in this case we need to get the following href
    #

    # ...
    soup = BeautifulSoup(html)
    li = soup.find("li", {"class": "hotels twoLines"})
    city_url = li.find('a', href=True)
    return city_url['href']

def get_hotellist_page(city_url, page_count):
    """ Returns the hotel list HTML. The URL of the list is the result of
    get_city_page(). Also, saves a copy of the HTML to the disk. Corresponds to
    STEP 3 of the slides.

    Parameters
    ----------
    city_url : str
        The relative URL of the hotels in the city we are interested in.
    page_count : int
        The page that we want to fetch. Used for keeping track of our progress.

    Returns
    -------
    html : str
        The HTML of the page with the list of the hotels.
    """
    url = base_url + city_url
    # Sleep 2 sec before starting a new http request
    time.sleep(2)
    # Request page
    headers = { 'User-Agent' : user_agent }
    response = requests.get(url, headers=headers)
    html = response.text.encode('utf-8')
    # Save the webpage
    with open(os.path.join(args.datadir, args.city + '-hotelist-' + str(page_count) + '.html'), "w") as h:
        h.write(html)
    return html

def parse_hotellist_page(html):
    """Parses the website with the hotel list and prints the hotel name, the
    number of stars and the number of reviews it has. If there is a next page
    in the hotel list, it returns a list to that page. Otherwise, it exits the
    script. Corresponds to STEP 4 of the slides.

    Parameters
    ----------
    html : str
        The HTML of the website with the hotel list.

    Returns
    -------
    URL : str
        If there is a next page, return a relative link to this page.
        Otherwise, exit the script.
    """
    soup = BeautifulSoup(html)
    # Extract hotel name, star rating and number of reviews
    hotel_boxes = soup.findAll('div', {'class' :'listing wrap reasoning_v5_wrap jfy_listing p13n_imperfect'})
    if not hotel_boxes:
        log.info("#################################### Option 2 ######################################")
        hotel_boxes = soup.findAll('div', {'class' :'listing_info jfy'})
    if not hotel_boxes:
        log.info("#################################### Option 3 ######################################")
        hotel_boxes = soup.findAll('div', {'class' :'listing easyClear  p13n_imperfect'})

    for hotel_box in hotel_boxes:
        #print info for each hotel
        h  = hotel_box.find("a", {"target" : "_blank"})
        hotel_url = base_url + h['href']
        time.sleep(2)
        heads = { 'User-Agent' : user_agent }
        resp = requests.get(hotel_url, headers=heads)
        hml = resp.text.encode('utf-8')
        soup2 = BeautifulSoup(hml) #get the soup for the hotel's page so we can scrape the info
        rating = soup2.findAll('span', {'class' : 'compositeCount'})
        hotel_name = hotel_box.find("a", {"target" : "_blank"}).find(text=True)
        log.info("Hotel name: %s" % hotel_name.strip())

        #traveler rating
        ratings = []
        for r in rating:
            ratings.append(r.find(text = True))
        log.info("Traveler rating for excellent: %s" % ratings[0])
        log.info("Traveler rating for very good: %s" % ratings[1])
        log.info("Traveler rating for average: %s" % ratings[2])
        log.info("Traveler rating for poor: %s" % ratings[3])
        log.info("Traveler rating for terrible: %s" % ratings[4])

        #reviews
        review = soup2.findAll('div', {'class' : 'value'})
        reviews = []
        for r in review:
            reviews.append(r.find(text = True))

        log.info("See reviews for families: %s" % reviews[0])
        log.info("See reviews for couples: %s" % reviews[1])
        log.info("See reviews for solo: %s" % reviews[2])
        log.info("See reviews for business: %s" % reviews[3])

        #review summary
        sp = soup2.findAll('span', {'class' : 'rate sprite-rating_s rating_s'})
        summaries = []
        count = 1
        for item in sp:
            s = item.find('img', alt = True)
            rate = s['alt'].strip()[0:3]
            if rate[-1] == 'o':
                summaries.append(rate[0:1])
            else:
                summaries.append(rate)

        log.info("Rating summary for sleep quality: %s" % summaries[0])
        log.info("Rating summary for location: %s" % summaries[1])
        log.info("Rating summary for rooms: %s" % summaries[2])
        log.info("Rating summary for service: %s" % summaries[3])
        log.info("Rating summary for value: %s" % summaries[4])
        log.info("Rating summary for cleanliness: %s" % summaries[5])

    # Get next URL page if exists, otherwise exit
    div = soup.find("div", {"class" : "pgLinks"})
    # check if this is the last page
    if div.find('span', {'class' : 'guiArw pageEndNext'}):
        log.info("We reached last page")
        sys.exit()
    # If not, return the url to the next page
    hrefs = div.findAll('a', href= True)
    for href in hrefs:
        c = href['class'].encode('ascii', 'ignore')
        if c == 'guiArw sprite-pageNext ':
            log.info("Next url is %s" % href['href'])
            return href['href']

if __name__ == "__main__":
    # Get current directory
    current_dir = os.getcwd()
    # Create datadir if does not exist
    if not os.path.exists(os.path.join(current_dir, args.datadir)):
        os.makedirs(os.path.join(current_dir, args.datadir))

    # Get URL to obtaint the list of hotels in a specific city
    city_url = get_city_page(args.city, args.state)
    c=0
    while(True):
        c +=1
        html = get_hotellist_page(city_url,c)
        city_url = parse_hotellist_page(html)


#    Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import argparse
from batchgenerators.utilities.file_and_folder_operations import *
from nnunet.run.default_configuration import get_default_configuration_with_multiTask
from nnunet.paths import default_plans_identifier
from nnunet.training.cascade_stuff.predict_next_stage import predict_next_stage
from nnunet.training.network_training.nnUNetTrainer import nnUNetTrainer
from nnunet.training.network_training.nnUNetTrainerCascadeFullRes import nnUNetTrainerCascadeFullRes
from nnunet.training.network_training.nnUNetTrainerV2_CascadeFullRes import nnUNetTrainerV2CascadeFullRes
from nnunet.utilities.task_name_id_conversion import convert_id_to_task_name
from nnunet.paths import *

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("network")
    # parser.add_argument("network_trainer")
    parser.add_argument("fold", help='0, 1, ..., 5 or \'all\'')
    parser.add_argument("gpu", help='0, 1, ..., 5 or \'all\'')
    parser.add_argument("-val", "--validation_only", help="use this if you want to only run the validation",
                        action="store_true")
    parser.add_argument("-c", "--continue_training", help="use this if you want to continue a training",
                        action="store_true")
    parser.add_argument("-p", help="plans identifier. Only change this if you created a custom experiment planner",
                        default=default_plans_identifier, required=False)
    parser.add_argument("--use_compressed_data", default=False, action="store_true",
                        help="If you set use_compressed_data, the training cases will not be decompressed. Reading compressed data "
                             "is much more CPU and RAM intensive and should only be used if you know what you are "
                             "doing", required=False)
    parser.add_argument("--deterministic",
                        help="Makes training deterministic, but reduces training speed substantially. I (Fabian) think "
                             "this is not necessary. Deterministic training will make you overfit to some random seed. "
                             "Don't use that.",
                        required=False, default=False, action="store_true")
    parser.add_argument("--npz", required=False, default=False, action="store_true", help="if set then nnUNet will "
                                                                                "export npz files of "
                                                                                "predicted segmentations "
                                                                                "in the validation as well. "
                                                                                "This is needed to run the "
                                                                                "ensembling step so unless "
                                                                                "you are developing nnUNet "
                                                                                "you should enable this")
    parser.add_argument("--find_lr", required=False, default=False, action="store_true",
                        help="not used here, just for fun")
    parser.add_argument("--valbest", required=False, default=False, action="store_true",
                        help="hands off. This is not intended to be used")
    parser.add_argument("--fp32", required=False, default=False, action="store_true",
                        help="disable mixed precision training and run old school fp32")
    parser.add_argument("--val_folder", required=False, default="validation_raw",
                        help="name of the validation folder. No need to use this for most people")
    parser.add_argument("--interp_order", required=False, default=3, type=int,
                        help="order of interpolation for segmentations. Testing purpose only. Hands off")
    parser.add_argument("--interp_order_z", required=False, default=0, type=int,
                        help="order of interpolation along z if z is resampled separately. Testing purpose only. "
                             "Hands off")
    parser.add_argument("--force_separate_z", required=False, default="None", type=str,
                        help="force_separate_z resampling. Can be None, True or False. Testing purpose only. Hands off")

    args = parser.parse_args()

    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
    tasks = ["100", "101", "102", "103", "104"]
    fold = args.fold
    network = args.network
    network_trainer = "nnUNetMultiTrainerV2"
    validation_only = args.validation_only
    plans_identifier = args.p
    find_lr = args.find_lr

    use_compressed_data = args.use_compressed_data
    decompress_data = not use_compressed_data

    deterministic = args.deterministic
    valbest = args.valbest

    fp32 = args.fp32
    run_mixed_precision = not fp32

    val_folder = args.val_folder
    interp_order = args.interp_order
    interp_order_z = args.interp_order_z
    force_separate_z = args.force_separate_z

    classes_dict = {}
    for i, task in enumerate(tasks):
        if not task.startswith("Task"):
            task_id = int(task)
            task = convert_id_to_task_name(task_id)
        tasks[i] = task

        json_file = join(preprocessing_output_dir,task, "dataset.json")
        classes = []
        with open(json_file) as jsn:
            d = json.load(jsn)
            tags = d['labels']
            for i in tags:
                if not int(i) == 0:
                    classes.append(tags[i])
            classes_dict[task] = classes

    if fold == 'all':
        pass
    else:
        fold = int(fold)

    if force_separate_z == "None":
        force_separate_z = None
    elif force_separate_z == "False":
        force_separate_z = False
    elif force_separate_z == "True":
        force_separate_z = True
    else:
        raise ValueError(
            "force_separate_z must be None, True or False. Given: %s" % force_separate_z)

    plans_file, output_folder_names, dataset_directorys, batch_dice, stage, \
        trainer_class = get_default_configuration_with_multiTask(
            network, tasks, network_trainer, plans_identifier)

    if trainer_class is None:
        raise RuntimeError(
            "Could not find trainer class in nnunet.training.network_training")

    if network == "3d_cascade_fullres":
        assert issubclass(trainer_class, (nnUNetTrainerCascadeFullRes, nnUNetTrainerV2CascadeFullRes)), \
            "If running 3d_cascade_fullres then your " \
            "trainer class must be derived from " \
            "nnUNetTrainerCascadeFullRes"
    else:
        assert issubclass(trainer_class,
                          nnUNetTrainer), "network_trainer was found but is not derived from nnUNetMultiTrainer"

    trainer = trainer_class(plans_file, fold,tasks,tags=classes_dict, output_folder_dict=output_folder_names, dataset_directory_dict=dataset_directorys,
                            batch_dice=batch_dice, stage=stage, unpack_data=decompress_data,
                            deterministic=deterministic,
                            fp16=run_mixed_precision)

    trainer.initialize(not validation_only)

    if find_lr:
        trainer.find_lr()
    else:
        if not validation_only:
            if args.continue_training:
                trainer.load_latest_checkpoint()
            trainer.run_training()
        else:
            if valbest:
                trainer.load_best_checkpoint(train=False)
            else:
                trainer.load_latest_checkpoint(train=False)

        trainer.network.eval()

        # predict validation
        trainer.validate(save_softmax=args.npz, validation_folder_name=val_folder, force_separate_z=force_separate_z,
                         interpolation_order=interp_order, interpolation_order_z=interp_order_z)

        # if network == '3d_lowres':
        #     trainer.load_best_checkpoint(False)
        #     print("predicting segmentations for the next stage of the cascade")
        #     predict_next_stage(trainer, join(
        #         dataset_directory, trainer.plans['data_identifier'] + "_stage%d" % 1))

if __name__ == "__main__":
    main()


#Suppose, you are making a tree with a fasta file. The sequence names should have some information regarding the species/genus of the sequence.
#Most of the time,the sequence names are just GI/accession numbers - so after the tree is made, it is hard to tell which sequence belongs to which organism/virus etc. For this reason, we attempt to modify the fasta headers of the reference sequences to be used for tree building. We create a dictionary from two lists - one list contain the original fasta reference names. another list has the abbreviated species/virus name corresponding to this header. After that, we attach the 'value' of each of the keys in the dictionary to the corresponding fasta header in the reference fasta file. This code will take care of such scenario.

import sys, os, re
from Bio import SeqIO

# guide file looks like this:
# abbr1	AXXXXXX.1
# abbr2 ABXXTTR.1 etc.
guide=open("VLTF3_ref_abbr_names", "r")

#fasta file:
#>AXXXXX.1
#atgctatctcccc  etc.

fasta_file=open("VLTF3_ref_candidate_combined.faa", "r")

abbr=[]
seqname=[]

for i in guide.readlines():
	line=i.rstrip()
	tabs=line.split("\t")
	#print tabs
	abbr.append(tabs[0])
	seqname.append(tabs[1])
#print abbr
#print seqname

abbriviations_dict=dict(zip(seqname,abbr))
#print abbriviations_dict

seqList=[]
for ff in SeqIO.parse(fasta_file, "fasta"):
	if ff.id in abbriviations_dict:
		x=ff.id+"_"+abbriviations_dict[ff.id]
		ff.id=x
		seqList.append(ff)
	#print seqList
SeqIO.write(seqList, "test_seqs.faa", "fasta")


"""Required Modules for this program"""
import os
from ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from dotenv import load_dotenv

load_dotenv()

authenticator = IAMAuthenticator('rNNe1tsh3f7x8WRZeJw0aUh0mnpXAkO3jptTu4QSnOAH')
language_translator = LanguageTranslatorV3(
    version='2018-05-01',
    authenticator=authenticator
)

language_translator.set_service_url('https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/6e970e71-e943-4dba-b9bd-3b9b6287dd39')

def englishToFrench(englishText):
    """Translates English to French"""
    model_id='en-fr'
    fr_text = language_translator.translate(
    text=englishText,
    model_id=model_id).get_result()
    return(fr_text['translations'][0]['translation'])

def frenchToEnglish(frenchText):
    """Translates French to English"""
    model_id='fr-en'
    en_text = language_translator.translate(
    text=frenchText,
    model_id=model_id).get_result()
    return(en_text['translations'][0]['translation'])


import discord
from discord.ext.commands import Bot
from discord.ext import commands

import help_info
import config_vars

client = discord.Client()
bot = commands.Bot(command_prefix=">")
bot.remove_command('help')

extensions = ['ctf', 'ctftime', 'configuration', 'encoding', 'cipher', 'utility']
cool_names = ['nullpxl', 'Yiggles', 'JohnHammond', 'voidUpdate', 'Michel Ney', 'theKidOfArcrania', 'l14ck3r0x01', 'hasu', 'KFBI', 'mrFu', 'warlock_rootx', 'd347h4ck']

@bot.event
async def on_ready():
    print(f"{bot.user.name} - Online")
    print(f"discord.py {discord.__version__}\n")
    print("-------------------------------")

    await bot.change_presence(activity=discord.Game(name=">help | >report \"x\""))

@bot.command()
async def help(ctx, page=None):

    if page == 'ctftime':
        emb = discord.Embed(description=help_info.ctftime_help, colour=4387968)
        emb.set_author(name='CTFTime Help')
    elif page == 'ctf':
        emb = discord.Embed(description=help_info.ctf_help, colour=4387968)
        emb.set_author(name='CTF Help')
    elif page == 'config':
        emb = discord.Embed(description=help_info.config_help, colour=4387968)
        emb.set_author(name='Configuration Help')
    elif page == 'utility':
        emb = discord.Embed(description=help_info.utility_help, colour=4387968)
        emb.set_author(name='Utilities Help')

    else:
        emb = discord.Embed(description=help_info.help_page, colour=4387968)
        emb.set_author(name='NullCTF Help')

    await ctx.channel.send(embed=emb)

@bot.command()
async def source(ctx):
    await ctx.send(help_info.src)

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        return
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("Missing a required argument.  Do >help")
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You do not have the appropriate permissions to run this command.")
    if isinstance(error, commands.BotMissingPermissions):
        await ctx.send("I don't have sufficient permissions!")
    else:
        print("error not caught")
        print(error)

@bot.command()
async def request(ctx, feature):
    # Bot sends a dm to creator with the name of the user and their request.
    creator = await bot.fetch_user(230827776637272064)
    authors_name = str(ctx.author)
    await creator.send(f''':pencil: {authors_name}: {feature}''')
    await ctx.send(f''':pencil: Thanks, "{feature}" has been requested!''')

@bot.command()
async def report(ctx, error_report):
    # Bot sends a dm to creator with the name of the user and their report.
    creator = await bot.fetch_user(230827776637272064)
    authors_name = str(ctx.author)
    await creator.send(f''':triangular_flag_on_post: {authors_name}: {error_report}''')
    await ctx.send(f''':triangular_flag_on_post: Thanks for the help, "{error_report}" has been reported!''')

@bot.command()
async def amicool(ctx):
    authors_name = str(ctx.author)
    if authors_name in cool_names:
        await ctx.send('You are very cool :]')
    else:
        await ctx.send('lolno')
        await ctx.send('Psst, kid.  Want to be cool?  Find an issue and report it or request a feature!')

if __name__ == '__main__':
    for extension in extensions:
        bot.load_extension('cogs.' + extension)
    bot.run(config_vars.discord_token)


#!/usr/bin/env python
###############################################################################
# release.py
#
# This is a tool created to help create release builds of Reverie. This tool
# is currently poorly documented. Hopefully that will change in the future
# but for right now my best suggestion is to use the --help flag on the
# command line.
###############################################################################

import sys
import os
import argparse
import shutil
import fnmatch
import subprocess
import zipfile
import tempfile

# This is the compile script. It's fed into the Lua interpreter to generate lua bytecode files
comp_script = '''
VERBOSE= {Verbose_}
OUTDIR = "{Outdir_}/"
DEBUG  = {Debug_}
if jit then
    function dumpF(f)
        return string.dump(f, DEBUG)
    end
else
    dumpF = string.dump
end
if VERBOSE then
    function logfile(n)  end
else
    function logfile(n) end
end
for k,fname in ipairs(arg) do
    if fname ~= "-" then
        {Logfile_}
        local chunk = assert(loadfile(fname .. ".lua"))
        local outname = OUTDIR .. fname .. ".lua"
        local stream = assert(io.open(outname, "wb"))
        stream:write(dumpF(chunk))
        stream:close()
    end
end
'''

def log(message):
    sys.stdout.write(str(message))
    sys.stdout.write('\n')

class Release:
    """
    Defines a class which can be used to create release builds of Reverie
    """
    def __init__(self, lua_exe, debug, verbose):
        """
        lua_exe - the path to the lua executable file (does not have to be an exe
                if you are using a non-windows system.
        debug - True if lua files should be compiled with debug information
        verbose - True if verbose messages should be printed
        """
        self.lua_exe = lua_exe
        self.debug = debug
        self.verbose = verbose

    def fileMatchesList(self, fnlist, fname):
        """
        Test if a file matches any of the patterns in fnlist.
        Using the fnmatch function we test fname against each of the
        patterns in fnlist. If any matches succeed return True, otherwise
        return False.
        """
        for fpattern in fnlist:
            if (fnmatch.fnmatch(fname, fpattern)):
                return True
        return False

    def search(self, folder, patterns):
        """
        Find all files in the folder tree which match any of the pattern in patterns.
        This function uses fnmatch to perform pattern matching. See fnmatch for more
        details. This function returns an array
        """
        fileList = []
        for dirpath, dirnames, filenames in os.walk(folder):
            if self.outdir in dirnames:
                dirnames.remove(self.outdir)
            for fn in filenames:
                if self.fileMatchesList(patterns, fn):
                    fileList.append(os.path.relpath(os.path.join(dirpath,fn)))
        return fileList

    def luac(self, filenames):
        """
        Run Lua with the compile script to generate lua bytecode files for each file
        in filesnames.
        exe - the Lua executable
        filenames - list of filenames to compile to Lua bytecode
        """
        params = [self.lua_exe, '-']
        for fn in filenames:
            assert(fn[-4:] == '.lua')
            directory = os.path.dirname(fn)
            outd = os.path.join(self.outdir,directory)
            if not os.path.exists(outd):
                os.makedirs(outd)
            params.append(fn[:-4])

        # build Lua script
        scr = comp_script.format(
            Outdir_  = self.outdir,
            Debug_   = 'true' if self.debug else 'false',
            Verbose_ = 'true' if self.verbose else 'false',
            Logfile_ = 'print("Compiling",fname)' if self.verbose else '')
        # turn it into a binary buffer
        bscript = scr.encode('latin-1')
        # run lua
        proc = subprocess.Popen(params, stdin=subprocess.PIPE)
        proc.communicate(bscript)
        return proc.wait() == 0

    def build(self, output):
        """
        Actually perform the building, etc.
        output - zip file to write to
        """
        patterns = [
            '*.png',
            '*.bmp',
            '*.ogg',
            '*.mp3',
            '*.ttf',
            '*.py',
            '*.sh',
        ]
        try:
            self.outdir = tempfile.mkdtemp(prefix='build-', dir=os.getcwd()).replace('\\','/')

            log('Compiling lua files')
            luaList = self.search('.', ['*.lua'])
            if not self.luac(luaList):
                return False

            log('Building file list')
            finalList = self.search('.', patterns)
            finalList.extend([os.path.join(self.outdir,x) for x in luaList])

            log('Building zip file \'' + output + '\'')
            with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as mzip:
                for ifn in finalList:
                    if self.verbose:
                        log('Zipping ' + ifn)
                    if ifn.startswith(self.outdir):
                        mzip.write(ifn, ifn[len(self.outdir)+1:])
                    else:
                        mzip.write(ifn)

            return True
        finally:
            sys.stdout.write('Cleaning up...\t')
            shutil.rmtree(self.outdir)
            sys.stdout.write('Done cleaning\n')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(prog='release.py')
    parser.add_argument('lua', help='Lua interpreter')
    parser.add_argument('output', help='Output file')
    parser.add_argument('--verbose','-v', action='store_true', help='Be verbose')
    parser.add_argument('--debug','-d', action='store_true', help='Store debug information in Lua files')
    args = parser.parse_args()
    rel = Release(args.lua, args.debug, args.verbose)
    rel.build(args.output)


import setuptools

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

files = ["metadata/*", "metadata/*/*", "metadata/.*/*"]

setuptools.setup(
    name='fmfexporter',
    version='0.2',
    scripts=['bin/fmfexporter'],
    python_requires='>=3.6',
    install_requires=['fmf', 'requests', 'jira', 'urllib3'],
    setup_requires=['pytest-runner', 'fmf', 'requests', 'urllib3'],
    tests_require=['pytest'],
    author="Fernando Giorgetti, Dominik Lenoch",
    author_email="fgiorget@redhat.com, dlenoch@redhat.com",
    license="Apache-2.0",
    description="Flexible Metadata Format test-case exporter tool",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/rhmessagingqe/fmfexporter",
    packages=setuptools.find_packages(),
    package_data={'fmfexporter': files},
    provides=['fmfexporter'],
    classifiers=[
        "Programming Language :: Python :: 3.6",
        "License :: OSI Approved :: Apache Software License",
        "Operating System :: OS Independent",
    ],
)


import json
import corenlp
if __name__ == '__main__':
    with open('./nlp.txt', 'r') as f:
        text = f.read()
    corenlp_dir = "/usr/local/lib/stanford-corenlp-full-2013-06-20/"
    parser = corenlp.StanfordCoreNLP(corenlp_path=corenlp_dir)
    # 一度に処理すると、途中で処理が切れる
    for line in text.split('\n'):
        result = json.loads(parser.parse(line))
        for sentence_data in result['sentences']:
            for word_data in sentence_data['words']:
                word = word_data[0]
                tag = word_data[1]['NamedEntityTag']
                pos = word_data[1]['PartOfSpeech']
                if tag == 'PERSON' and pos == 'NNP':
                    print(f'{word}')