import random
import sys
import numpy as np
import pygame as pg
from pygame.locals import *

#setup display
display_width = 800
display_height = 400
#setup colors
RED = (0,139,139)
GREEN = (0,128,0)
WHITE = (255,255,255)
#specify circle properties
circle_centre_x = 400
circle_centre_y = 50
circle_radius = 20
circle_y_step_falling = 40
#specify rectangle properties
rectangle_left = 400
rectangle_top = 350
rectangle_width = 200
rectangle_height = 50
#set hyperparameters
lr = 0.85
y = 0.99
#set constants
score = 0
missed = 0
reward = 0

class State:
    def __init__(self,rect,circle_centre_x,circle_centre_y):
        self.rect = rect
        self.circle_centre_x = circle_centre_x
        self.circle_centre_y = circle_centre_y

def score_gain_missed_count():
    font = pg.font.SysFont(None,25)
    text = font.render('Score:' + str(score),True,(238,58,140))
    text1 = font.render('Missed:' + str(missed),True,(238,58,140))
    gameDisplay.blit(text,(display_width - 120,10))
    gameDisplay.blit(text1,(display_width - 280,10))

def calculate_score(rect,s):
    if rect.left <= s <= rect.right:
        return 1
    else:
        return -1

def circle_falling(circle_radius):
    new_x = 100 - circle_radius
    multiplier = random.randint(1,8)
    new_x *= multiplier
    return new_x

rct = pg.Rect(rectangle_left,rectangle_top,rectangle_width,rectangle_height)
Q_Dic = {}
Q = np.zeros([300,3])

def state_to_numbers(s):
    r = s.rect.left #you can also use s.rect.right
    cx = circle_centre_x #circle x position
    cy = circle_centre_y
    n = int(str(r) + str(cx) + str(cy))

    if n in Q_Dic:
        return Q_Dic[n]
    else:
        if len(Q_Dic):
            maximum = max(Q_Dic,key = Q_Dic.get)
            Q_Dic[n] = Q_Dic[maximum] + 1
        else:
            Q_Dic[n] = 1
    return Q_Dic[n]

def get_best_action(s):
    return np.argmax(Q[state_to_numbers(s),:])

def new_state_after_action(s,act):
    if act == 2:
        if s.rect.right + s.rect.width > display_width:
            rct = s.rect
        else:
            rct = pg.Rect(s.rect.left + s.rect.width,s.rect.top,s.rect.width,s.rect.height)

    elif act == 1:
        if s.rect.left - s.rect.width < 0:
            rct  = s.rect
        else:
            rct = pg.Rect(s.rect.left - s.rect.width,s.rect.top,s.rect.width,s.rect.height)

    else:
        rct = s.rect

    return State(rct,circle_centre_x,circle_centre_y+circle_y_step_falling)

def new_rect_after_action(rect,act):
        if act == 2:
            if s.rect.right + s.rect.width > display_width:
                return rect
            else:
                return pg.Rect(s.rect.left + s.rect.width,s.rect.top,s.rect.width,s.rect.height)

        elif act == 1:
            if s.rect.left - s.rect.width < 0:
                return rect
            else:
                return pg.Rect(s.rect.left - s.rect.width,s.rect.top,s.rect.width,s.rect.height)

        else:
            return rect

#initialize pygame
FPS = 20
clock = pg.time.Clock()
pg.init()
gameDisplay = pg.display.set_mode((display_width,display_height))
pg.display.set_caption('Catch The Ball')

while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()
    gameDisplay.fill(WHITE)
    pg.draw.rect(gameDisplay,GREEN,rct)
    pg.draw.circle(gameDisplay,RED,(circle_centre_x,circle_centre_y),circle_radius)

    if circle_centre_y == display_height - rectangle_height - circle_radius:
        reward = calculate_score(rct,circle_centre_x)
        circle_centre_x = circle_falling(circle_radius)
        circle_centre_y = 50

    else:
        reward = 0
        circle_centre_y += circle_y_step_falling

    s = State(rct,circle_centre_x,circle_centre_y)
    act = get_best_action(s)
    r0 = calculate_score(s.rect,circle_centre_x)
    s1 = new_state_after_action(s,act)

    Q[state_to_numbers(s),act] += lr*(r0 + y*np.max(Q[state_to_numbers(s1),:]) - Q[state_to_numbers(s),act])
    rct = new_rect_after_action(s.rect,act)
    score_gain_missed_count()
    if reward == 1:
        score += reward
    elif reward == -1:
        missed += reward
    pg.display.update()
    clock.tick(FPS)


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

"""
This file is used to test some small pytrajectory examples.

"""

import unittest
import time
import sympy as sp
import numpy as np
import pytest
from pytrajectory import TransitionProblem
from pytrajectory import log
from pytrajectory import auxiliary as aux

from ipydex import IPS, activate_ips_on_exception
activate_ips_on_exception()

def rhs_di(x, u, uref, t, p):
    x1, x2 = x
    u1, = u

    ff = [x2, u1]

    return ff

def rhs_di_time_scaled(x, u, uref, t, p):
    x1, x2 = x
    u1, = u

    # one additional free parameter
    k, = p

    ff = [k*x2, k*u1]

    return ff

# this does not yet work
def rhs_di_time_scaled_with_penalties(x, u, uref, t, p):
    x1, x2 = x
    u1, = u

    # one additional free parameter
    k, = p

    ff = [k*x2, k*u1, (k-1)**2*100]

    return ff

# system state boundary values for a = 0.0 [s] and b = 2.0 [s]
xa_di = [0.0, 0.0]
xb_di = [1.0, 0.0]

xa_br = [0.0, 0.0, 0.0]
xb_br = [0.0, 0.0, 1.0]

def rhs_di_penalties(x, u, uref, t, p):
    x1, x2 = x
    u1, = u

    ff = [x2, u1, 0]

    return ff

def rhs_inv_pend(x, u, uref, t, p):
    x1, x2, x3, x4 = x  # system variables
    u1, = u  # input variable

    l = 0.5  # length of the pendulum
    g = 9.81  # gravitational acceleration

    # this is the vectorfield
    ff = [x2,
          u1,
          x4,
          (1/l)*(g*sp.sin(x3) + u1*sp.cos(x3))]

    return ff

def rhs_brockett_system(x, u, uref, t, p):
    x1, x2, x3 = x  # system variables
    u1, u2 = u  # input variables
    # this is the vectorfield

    ff = [u1,
          u2,
          x2*u1-x1*u2]

    return ff

# a = 0.0
xa_inv_pend = [0.0, 0.0, np.pi, 0.0]
# b = 3.0
xb_inv_pend = [0.0, 0.0, 0.0, 0.0]

logfname = "test_log.txt"

with open(logfname, "w") as txtfile:
    txtfile.write("\n")

def log_tst_case(name):
    with open(logfname, "a") as txtfile:
        logstr = f"{time.ctime()} {name}\n"
        txtfile.write(logstr)

# noinspection PyPep8Naming
class TestExamples(unittest.TestCase):

    def setUp(self):
        log_tst_case(self._testMethodName)

    def test_di_integrator_pure(self):
        S1 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               show_ir=False,
                               ierr=None,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    def test_di_integrator_pure_seed(self):
        S1 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               show_ir=False,
                               ierr=None,
                               use_chains=False,
                               maxIt=1,
                               seed=0)
        S1.solve()

        S2 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               show_ir=False,
                               ierr=None,
                               use_chains=False,
                               maxIt=1,
                               seed=1141)

        S2.solve()

        # assert that the different seed has taken effect
        assert S1.eqs.solver.res_list[0] != S2.eqs.solver.res_list[0]
        assert S2.eqs._first_guess == {"seed": 1141}

        assert S1.reached_accuracy
        assert S2.reached_accuracy

    def test_di_integrator_pure_with_random_guess(self):
        first_guess = {'seed': 20}
        S1 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               show_ir=False,
                               ierr=None,
                               first_guess=first_guess,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    def test_di_integrator_pure_with_complete_guess(self):

        # solve Problem for the first time
        first_guess = {'seed': 20}
        S1 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               show_ir=False,
                               ierr=None,
                               first_guess=first_guess,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

        first_guess2 = {'complete_guess': S1.eqs.sol,
                        'n_spline_parts': aux.Container(x=S1.eqs.trajectories.n_parts_x,
                                                        u=S1.eqs.trajectories.n_parts_u)}
        S2 = S1.create_new_TP(first_guess=first_guess2)
        S2.solve()

        assert S2.reached_accuracy

        # now test changed boundary conditions

        S3 = S2.create_new_TP(first_guess=first_guess2, xb=[1.5, 0.0])
        S3.solve()
        assert S3.reached_accuracy

    def test_di_integrator_pure_with_penalties(self):
        S1 = TransitionProblem(rhs_di_penalties, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               show_ir=False,
                               ierr=None,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    def test_di_constraint_x2_projective(self):
        con = {'x2': [-1, 10]}
        con = {'x2': [-0.1, 0.65]}
        S1 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0, constraints=con,
                               show_ir=False,
                               ierr=None,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    def test_di_con_u1_projective_integrator(self):
        con = {'u1': [-1.2, 1.2]}
        S1 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0, constraints=con,
                               show_ir=False,
                               ierr=None,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    def test_di_con_u1_x2_projective_integrator(self):
        con = {'u1': [-1.3, 1.3], 'x2': [-.1, .8],}
        S1 = TransitionProblem(rhs_di, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               constraints=con,
                               show_ir=False,
                               accIt=0,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    def test_di_timescaled(self):
        """The double integrator with an additional free parameter for time scaling"""
        con = {'u1': [-1.3, 1.3], 'x2': [-.1, .8],}
        S1 = TransitionProblem(rhs_di_time_scaled, a=0.0, b=2.0, xa=xa_di, xb=xb_di, ua=0, ub=0,
                               constraints=con,
                               show_ir=False,
                               accIt=0,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    @pytest.mark.xfail(reason="yet to implement", strict=True)
    @unittest.expectedFailure
    def test_di_timescaled_with_penalties(self):
        """The double integrator with an additional free parameter for time scaling"""
        con = {'u1': [-1.3, 1.3], 'x2': [-.1, .8],}
        S1 = TransitionProblem(rhs_di_time_scaled_with_penalties, a=0.0, b=2.0,
                               xa=xa_di, xb=xb_di, ua=0, ub=0,
                               constraints=con,
                               show_ir=False,
                               accIt=0,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

        # the penalties are not handled correctly yet..
        assert False

    def test_brockett_system(self):
        S1 = TransitionProblem(rhs_brockett_system, a=0.0, b=2.0, xa=xa_br, xb=xb_br,
                               ua=None, ub=None,
                               show_ir=False,
                               ierr=None,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    @pytest.mark.slow
    def test_pure_inv_pendulum(self):
        con = None
        eps = 7e-2  # increase runtime-speed (prevent additional run with 80 spline parts)
        S1 = TransitionProblem(rhs_inv_pend, a=0.0, b=3.0, xa=xa_inv_pend, xb=xb_inv_pend,
                               ua=0, ub=0, constraints=con,
                               show_ir=False,
                               accIt=0,
                               eps=eps,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

    @pytest.mark.slow
    def test_constr_inv_pendulum(self):
        con = { 'x1': [-0.8, 0.3], 'x2': [-2.0, 2.0], 'u1': [-7.0, 7.0] }
        eps = 7e-2  # increase runtime-speed (prevent additional run with 80 spline parts)
        S1 = TransitionProblem(rhs_inv_pend, a=0.0, b=3.0, xa=xa_inv_pend, xb=xb_inv_pend,
                               ua=0, ub=0, constraints=con,
                               show_ir=False,
                               accIt=0,
                               eps=eps,
                               use_chains=False)
        S1.solve()
        assert S1.reached_accuracy

# noinspection PyPep8Naming
class TestExamplesParallel(unittest.TestCase):

    def setUp(self):
        log_tst_case(self._testMethodName)

    def test_di_integrator_pure_parallel(self):

        # only one run
        results = aux.parallelizedTP(ff=rhs_di, xa=xa_di, xb=xb_di, ua=0, ub=0, use_chains=False)

        assert len(results) == 1
        assert results[0].reached_accuracy

        # now vary two parameters
        results = aux.parallelizedTP(ff=rhs_di, xa=xa_di, xb=xb_di, ua=0, ub=0, use_chains=False,
                                     seed=[0, 1, 2], b=[1, 2])

        assert len(results) == 6
        assert [r.reached_accuracy for r in results] == [True]*len(results)

if __name__ == "__main__":
    print(("\n"*2 + r"   please run py.test -s -k-slow %filename.py"+ "\n"))
    # or: py.test -s --pdbcls=IPython.terminal.debugger:TerminalPdb %filename

    tests = TestExamples()
    tests2 = TestExamplesParallel()

    log.console_handler.setLevel(10)

    # tests.test_di_integrator_pure()
    # print "-"*10
    # tests.test_di_constraint_x2_projective()
    # print "-"*10
    # tests.test_di_con_u1_x2_projective_integrator()
    # tests.test_di_integrator_pure_with_penalties()
    # tests.test_di_integrator_pure_with_random_guess()
    print("-"*10)
    tests2.test_di_integrator_pure()
    # tests.test_di_timescaled()


# -*- coding: utf-8 -*-
"""
Created on Wed May 30 15:01:24 2018

@author: sugino0708
"""

import gym
import numpy as np
import matplotlib.pyplot as plt

# 動画の描画関数の宣言
# 参考URL http://nbviewer.jupyter.org/github/patrickmineault
# /xcorr-notebooks/blob/master/Render%20OpenAI%20gym%20as%20GIF.ipynb
from JSAnimation.IPython_display import display_animation
from matplotlib import animation
from IPython.display import display

def display_frames_as_gif(frames):
    """
    Displays a list of frames as a gif, with controls
    """
    plt.figure(figsize=(frames[0].shape[1]/72.0, frames[0].shape[0]/72.0),
               dpi=72)
    patch = plt.imshow(frames[0])
    plt.axis('off')

    def animate(i):
        patch.set_data(frames[i])

    anim = animation.FuncAnimation(plt.gcf(), animate, frames=len(frames),
                                   interval=50)

    anim.save('movie_cartpole.mp4')  # 動画のファイル名と保存です
    display(display_animation(anim, default_mode='loop'))

# 定数の設定
ENV = 'CartPole-v0'

NUM_DIZITIZED = 6 #各状態の離散値への分割数
GAMMA = 0.99 #時間割引率
ETA  =0.5 #学習率

MAX_STEPS = 200 #1試行のstep数
NUM_EPISODES = 1000 #最大試行回数

"""
Agent Class

行動の主体となるエージェントの定義
"""

class Agent:
    def __init__(self, num_states, num_actions):
        """
        課題の状態と行動の数を初期化する
        """
        self.num_states = num.states #carpoleの場合、状態数は4種ある
        self.num_actions = num_actions #carpoleの場合、アクション数は2種ある
        self.brain = Brain(num_states, num_actions) #エージェントが行動を決定するための頭脳を生成

    def update_q_function(self, observation, action, reward, observation_next):
        """
        Q関数の更新
        """
        self.brain.update_Qtable(observation, action, reward, observation_next)

    def get_action(self, observation, step):
        """
        行動の決定
        """
        action = self.brain.decide_action(observation, step)
        return action

"""
Brain Class

エージェントが行動を判断するための頭脳
"""

class Brain:

    def __init__(self, num_states, num_actions):
        self.num_states = num_states  # CartPoleは状態数4を取得
        self.num_actions = num_actions  # CartPoleの行動（右に左に押す）の2を取得
        # 状態を分割数^（4変数）にデジタル変換したQ関数（表）を作成
        self.q_table = np.random.uniform(low=0, high=1, size=(NUM_DIZITIZED**self.num_states, self.num_actions))

    def bins(self, clip_min, clip_max, num):
        """
        観測した状態（連続値）を離散値にデジタル変換する
        """
        return np.linspace(clip_min, clip_max, num + 1)[1:-1]

    def digitize_state(self, observation):
        """
        観測したobservation状態を、離散値に変換する
        """
        cart_pos, cart_v, pole_angle, pole_v = observation
        digitized = [
        np.digitize(cart_pos, bins=self.bins(-2.4, 2.4, NUM_DIZITIZED)),
        np.digitize(cart_v, bins=self.bins(-3.0, 3.0, NUM_DIZITIZED)),
        np.digitize(pole_angle, bins=self.bins(-0.5, 0.5, NUM_DIZITIZED)),
        np.digitize(pole_v, bins=self.bins(-2.0, 2.0, NUM_DIZITIZED))
        ]
        return sum([x * (NUM_DIZITIZED**i) for i, x in enumerate(digitized)])

    def update_Qtable(self, observation, action, reward, observation_next):
        """
        Qテーブルを学習により更新
        観察を離散化
        """
        state = self.digitize_state(observation)
        state_next = self.digitize_state(observation_next)
        Max_Q_next = max(self.q_table[state_next][:])
        self.q_table[state, action] = self.q_table[state, action] + ETA * (reward + GAMMA * Max_Q_next - self.q_table[state, action])


#!/usr/bin/env python3
from __future__ import print_function
from math import trunc
from time import sleep
import sys
import codecs

from periphery import I2C

TCA9548_U93_ADDR = 0x70 #1110_000X  0XE0
SENSOR_IIC_BUS   = 0x01

def set_i2c_mux(dev_addr,channel):
  i2c = I2C("/dev/i2c-1")
  i2c.transfer(dev_addr, [I2C.Message([channel])])
  i2c.close()

def ad7414_reg_write(dev_addr,reg_addr,reg_value):
  i2c = I2C("/dev/i2c-1")
  i2c.transfer(dev_addr, [I2C.Message([reg_addr, reg_value])]) # SENSOR_IIC_BUS is selected
  i2c.close()

def ad7414_reg_read(dev_addr,reg_addr,nbits):
  i2c = I2C("/dev/i2c-1")
  read = I2C.Message([0x0]*nbits, read=True)
  i2c.transfer(dev_addr, [I2C.Message([reg_addr]), read]) # reg for read
  i2c.close()
  return codecs.encode(bytes(bytearray(read.data)), 'hex')

def ad7414_mon(dev_addr):
  #set the I2C Mux to SENSOR_IIC_BUS 0x01
  set_i2c_mux(TCA9548_U93_ADDR,SENSOR_IIC_BUS)

  ad7414_reg_write(dev_addr,0x1,0x48) #alert active low
  ad7414_reg_write(dev_addr,0x2,0x3F) #up limit is 63 Degree
  ad7414_reg_write(dev_addr,0x3,0x80) #low limit is 0 degree
  temperature=ad7414_reg_read(dev_addr,0x0,1)#read the temperature value
  return int(temperature,16)

def ltc2499_temp_mon(dev_addr,reg_addr0,reg_addr1):
  #set the I2C Mux to SENSOR_IIC_BUS 0x01 PUT INSIDE METHOD
  set_i2c_mux(TCA9548_U93_ADDR,SENSOR_IIC_BUS)

  # two delays as the chip is slow
  sleep(0.5)
  i2c = I2C("/dev/i2c-1")
  i2c.transfer(dev_addr, [I2C.Message([reg_addr1,reg_addr0])])# Reg for read
  sleep(0.5)

  read = I2C.Message([0x0]*4, read=True)
  i2c.transfer(dev_addr, [read])
  i2c.close()
  adc_code=int(codecs.encode(bytes(bytearray(read.data)),'hex'), 16)

  resolution=2500./0x80000000
  amplitude=(adc_code-0x40000000)*resolution
  if(adc_code==0x3FFFFFFF): amplitude=-1

  temperature= 326-0.5*amplitude
  return temperature


# -*- coding: utf-8 -*-
from rdflib import OWL, RDF, RDFS
from rdflib.namespace import Namespace

SH = Namespace('http://www.w3.org/ns/shacl#')

# Classes
RDF_Property = RDF.term('Property')
RDF_List = RDF.term('List')
RDFS_Resource = RDFS.term('Resource')
RDFS_Class = RDFS.term('Class')
OWL_Ontology = OWL.term("Ontology")
OWL_Class = OWL.term("Class")
OWL_DatatypeProperty = OWL.term("DatatypeProperty")
SH_NodeShape = SH.term('NodeShape')
SH_PropertyShape = SH.term('PropertyShape')
SH_ValidationResult = SH.term('ValidationResult')
SH_ValidationReport = SH.term('ValidationReport')
SH_Violation = SH.term('Violation')
SH_Info = SH.term('Info')
SH_Warning = SH.term('Warning')
SH_IRI = SH.term('IRI')
SH_BlankNode = SH.term('BlankNode')
SH_Literal = SH.term('Literal')
SH_BlankNodeOrIRI = SH.term('BlankNodeOrIRI')
SH_BlankNodeORLiteral = SH.term('BlankNodeOrLiteral')
SH_IRIOrLiteral = SH.term('IRIOrLiteral')
SH_ConstraintComponent = SH.term('ConstraintComponent')
SH_SHACLFunction = SH.term('SHACLFunction')
SH_SPARQLFunction = SH.term('SPARQLFunction')
SH_SPARQLRule = SH.term('SPARQLRule')
SH_TripleRule = SH.term('TripleRule')
SH_SPARQLTarget = SH.term('SPARQLTarget')
SH_SPARQLTargetType = SH.term('SPARQLTargetType')
SH_JSTarget = SH.term('JSTarget')
SH_JSTargetType = SH.term('JSTargetType')
SH_JSFunction = SH.term('JSFunction')

# predicates
RDF_type = RDF.term('type')
RDF_first = RDF.term('first')
RDF_rest = RDF.term('rest')
RDF_object = RDF.term('object')
RDF_predicate = RDF.term('predicate')
RDF_subject = RDF.term('subject')
RDFS_subClassOf = RDFS.term('subClassOf')
RDFS_comment = RDFS.term('comment')
SH_path = SH.term('path')
SH_deactivated = SH.term('deactivated')
SH_message = SH.term('message')
SH_name = SH.term('name')
SH_description = SH.term('description')
SH_property = SH.term('property')
SH_node = SH.term('node')
SH_target = SH.term('target')  # from advanced spec
SH_targetClass = SH.term('targetClass')
SH_targetNode = SH.term('targetNode')
SH_targetObjectsOf = SH.term('targetObjectsOf')
SH_targetSubjectsOf = SH.term('targetSubjectsOf')
SH_focusNode = SH.term('focusNode')
SH_resultSeverity = SH.term('resultSeverity')
SH_resultPath = SH.term('resultPath')
SH_resultMessage = SH.term('resultMessage')
SH_sourceConstraint = SH.term('sourceConstraint')
SH_sourceConstraintComponent = SH.term('sourceConstraintComponent')
SH_sourceShape = SH.term('sourceShape')
SH_severity = SH.term('severity')
SH_value = SH.term('value')
SH_conforms = SH.term('conforms')
SH_result = SH.term('result')
SH_inversePath = SH.term('inversePath')
SH_alternativePath = SH.term('alternativePath')
SH_zeroOrMorePath = SH.term('zeroOrMorePath')
SH_oneOrMorePath = SH.term('oneOrMorePath')
SH_zeroOrOnePath = SH.term('zeroOrOnePath')
SH_prefixes = SH.term('prefixes')
SH_prefix = SH.term('prefix')
SH_namespace = SH.term('namespace')
SH_rule = SH.term('rule')
SH_condition = SH.term('condition')
SH_order = SH.term('order')
SH_construct = SH.term('construct')
SH_subject = SH.term('subject')
SH_predicate = SH.term('predicate')
SH_object = SH.term('object')
SH_parameter = SH.term('parameter')
SH_ask = SH.term('ask')
SH_select = SH.term('select')
SH_this = SH.term('this')
SH_filterShape = SH.term('filterShape')
SH_nodes = SH.term('nodes')
SH_union = SH.term('union')
SH_intersection = SH.term('intersection')
SH_datatype = SH.term('datatype')
SH_nodeKind = SH.term('nodeKind')
SH_optional = SH.term('optional')
SH_js = SH.term('js')
SH_jsFunctionName = SH.term('jsFunctionName')
SH_jsLibrary = SH.term('jsLibrary')


#! /usr/bin/env python
# Copyright (c) 2010 Art & Logic Software Development, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import cStringIO
import cgi
import re
import urllib
import sys

# match a percent, followed by two hex digits
kEntityPattern = re.compile(r'%([0-9A-Fa-f]{2})')

class AHttpException(Exception):
   def __init__(self, code, str):
      self.code = code
      self.str = str

class ARequest(object):
   '''
      Class to represent the incoming client request and our reponse to that
      request.

      @ivar command: HTTP verb for this request.
      @ivar path: list of path components (IOW, path/to/file is represented as
            ['path', 'to', 'file']
      @ivar query: dict mapping each keys in the query string to a list of
            values
      @ivar headers: dict containing HTTP headers received from the client
      @ivar client: string containing client IP address
      @ivar postData: dict containing un-encoded post data (key, [list-of-values])
      @ivar responseCode: HTTP response code sent by our code.
      @ivar responseHeaders: dict of HTTP headers to return to the client
      @ivar output: list of strings to return as body of response message
      @ivar outputLength: sum of the lengths of the strings in the output list
      @ivar outputFile: open file object to return to the client as the output
            body -- note that we cannot use both C{output} and C{outputFile}
            at the same time.

      @ivar rawPath: full path to the page currently being displayed
      @ivar basePath: portion of path preceding any '?' that may be present
   '''
   def __init__(self, cmd, path=None, headers=None, client=None, postData=None):
      '''
         Create a new request object, using as many of the input parameters
         as we are provided with. We will use the same request object to hold
         the data that we would like to have sent back to the client.

      '''
      self.command = cmd
      if headers:
         self.headers = dict([(k.lower(), v) for (k, v) in headers.items()])
      self.client = client
      self.postData = postData
      self.responseCode = 200
      self.responseHeaders = {}
      self.output = []
      self.outputLength = None
      self.outputFile = None
      self.rawPath = path
      self.basePath = ''
      self.ParsePath(path)

   def SetHeader(self, key, value):
      self.responseHeaders[key] = value

   def GetRequestHeader(self, key):
      try:
         return self.headers[key]
      except KeyError:
         return None

   def SetResponseCode(self, code):
      self.responseCode = code

   def Redirect(self, location):
      '''
         @param location: path that the browser is told to redirect to. We
               serve up  a '302' (temporary) redirect.
      '''
      self.SetResponseCode(302)
      self.SetHeader("Location", location)

   def GetQueryData(self, key, fullList=False):
      ''' if the specified key is present in the query data dict, returns it.
          if the 'fullList' parameter is set to TRUE, returns the data as a
          list. otherwise returns a single element.
          If the key isn't present in the query returns None.
      '''
      val = None
      if self.query:
         val = self.query.get(key)
         if val:
            if not fullList:
               val = val[0]
      return val

   def GetPostData(self, key, fullList = False):
      ''' if the specified key is present in the post data dict, returns it.
          if the 'fullList' parameter is set to TRUE, returns the data as a
          list. otherwise returns a single element. If the requested key isn't
          present in the post data, returns None.
      '''
      val = None
      if self.postData:
         val = self.postData.get(key)
         if val:
            if not fullList:
               val = val[0]
      return val

   def Write(self, txt):
      ''' add 'txt' to the list of strings that we're going to send back to
      the client, also updating the output length as we go.
      '''
      self.outputFile = None
      self.output.append(txt)
      if not self.outputLength:
         self.outputLength = 0
      self.outputLength += len(txt)

   def SetOutputFile(self, outputFile, fileLength):
      ''' used to return a file of data, instead of a string. '''
      self.outputFile = outputFile
      self.output = []
      if not self.outputLength:
         self.outputLength = 0
      self.outputLength = fileLength

   def ParsePath(self, pathStr):
      # break off query string if it's there...
      components = pathStr.split("?")
      self.query = {}
      if len(components) > 1:
         # parse the query string into a dict (retaining any keys that have
         # empty values.
         queryVals = cgi.parse_qs(components[1], True)
         for key, val in queryVals.items():
            self.query[key.lower()] = val

      else:
         pass
      self.basePath = components[0]
      pathComponents = components[0].split('/')
      # the first path component will always be '' because we get a leading
      # slash -- there can't be anything preceding it -- just throw that one
      # away (similarly, throw away anything that comes after a trailing slash)
      self.path = [urllib.unquote_plus(p).lower() for p in pathComponents if len(p)]


import time, random
from datetime import datetime

# data = [2, 4, 33, 44, 55, 66, 77, 88, 99, 100]
data1 = list(range(100000))

def call_time(func):
    def wrapper(*args, **kwargs):
        t1 = datetime.now()
        x = func(*args, **kwargs)
        t2 = datetime.now()
        print('%s Time Costed:%s' % (func.__name__, t2-t1))
        return x
    return wrapper

@call_time
def linear_search(data_set, value):
    for i in range(len(data_set)):
        if data_set[i] == value:
            return print(i)

linear_search(data1, 234232)

@call_time
def bin_search(data_set, value):
    '''

    :param data_set: 所查找的数组
    :param value: 所查找的值
    :return: 所查找值的下标
    '''
    low = 0
    high = len(data_set) - 1 # 下标只到len-1
    while low <= high: # 常规判断
        mid = (low+high)//2
        if data_set[mid] == value:
            return print(mid)
        elif data_set[mid] > value:
            high = mid - 1
        else:
            low = mid + 1
    return print(False)

bin_search(data, 1035)

# def random_list(n):
#     result = []
#     ids = list(range(1001, 1001+n))
#     a1 = ['李', '郑', '刘', '林', '王', ]
#     a2 = ['爱', '光', '习', '龙', ]
#     a3 = ['娟', '丽', '刚', '龙', ]
#     for i in range(n):
#         age = random.randint(18,60)
#         id = ids[i]
#         name = random.choice(a1)+random.choice(a2)+\
#                random.choice(a3)
#         dict_ran = {'id':id, 'name':name, 'age':age}
#         result.append(dict_ran)
#     return result
#
#
# data = random_list(50)
# print(data)

# def bin_search(data_set, value):
#     '''
#
#     :param data_set: 所查找的数组
#     :param value: 所查找的值
#     :return: 所查找值的下标
#     '''
#     low = 0
#     high = len(data_set) - 1 # 下标只到len-1
#     while low <= high: # 常规判断
#         mid = (low+high)//2
#         if data_set[mid].get('id') == value:
#             return print(data_set[mid])
#         elif data_set[mid].get('id') > value:
#             high = mid - 1
#         else:
#             low = mid + 1
#     return print(False)
#
# bin_search(data, 1035)


from PyQt5.QtCore import QRectF
from enum import Enum
import re

class Tile(Enum):
    grass = 0
    wall = 1
    stone = 2
    sand = 3
    dark_sand = 4
    sand_stone = 5
    snow = 6
    ice = 7
    stonewall = 8
    fire = 9

    def walkable(self):
        return self in [Tile.grass, Tile.sand, Tile.dark_sand, Tile.snow, Tile.stone]
    def shootable(self):
        return self in [Tile.grass, Tile.sand, Tile.dark_sand, Tile.snow, Tile.stone, Tile.ice]

class LevelLoader:

    LEVEL_SIZE = 100
    TILE_SIZE = 10

    META_DATA_TAGS = ['single_spawn', 'duel_spawns', 'chaser_spawns']
    META_DATA_DEFAULT_VALUES = {'single_spawn' : (50,50), 'duel_spawns' : [(10,50),(90,50)], 'chaser_spawns' : [(50,20),(20,50),(70,50)]}

    # Reads a txt file and creates an 2 dimensional array representing a level
    # A '0' represents free space, a '1' represents a wall
    @classmethod
    def loadLevel(self, filePath):
        levelMatrix = []
        rects = []
        metadata = {}

        # construct regex pattern
        pattern = '(?P<name>'
        for tag in self.META_DATA_TAGS:
            pattern += tag + '|'
        pattern = pattern[:-1] # remove last '|'
        pattern += ')'
        pattern += ':\s*(?P<data>.*)'

        with open(filePath, "r") as fileObject:
            for line in fileObject:
                if line.startswith('\n'):
                    continue

                match = re.match(pattern, line)
                if match:
                    # if it matches, the line contains metadata
                    tag = match.group('name')
                    if tag in self.META_DATA_TAGS:
                        metadata[tag] = eval(match.group('data'))
                    else:
                        raise Exception('Unknown tag name: "' + tag + '" in level "' + filePath + '"')
                else:
                    # else it contains level data
                    row = []
                    for c in line:
                        if c == '\n':
                            continue
                        try:
                            tileNum = int(c)
                            row.append(Tile(tileNum))
                        except:
                            raise Exception('Unknown symbol: "' + c + '" in level "' + filePath + '"')
                    levelMatrix.append(row)

        # construct obstacle list
        for i in range(self.LEVEL_SIZE):
            for j in range(self.LEVEL_SIZE):
                if levelMatrix[i][j] == Tile.wall:
                    new_rect = QRectF(j * self.TILE_SIZE, i * self.TILE_SIZE,
                                          self.TILE_SIZE, self.TILE_SIZE)

                    # Check if the rect would already be covered by one of our rects
                    if any(rect.contains(new_rect) for rect in rects):
                        continue

                    # Explore right and down to cover more walls in one square
                    n=0
                    while all(all(x == Tile.wall for x in column[j:j+n]) for column in levelMatrix[i:i+n]):
                        n += 1

                    rects.append(QRectF(j * self.TILE_SIZE, i * self.TILE_SIZE,
                                        (n-1) * self.TILE_SIZE, (n-1) * self.TILE_SIZE))

        # fill in missing metadata with default values
        for tag in self.META_DATA_TAGS:
            if tag not in metadata:
                metadata[tag] = self.META_DATA_DEFAULT_VALUES[tag]

        return levelMatrix, rects, metadata


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/src/plugins/john.py
# Compiled at: 2020-01-19 08:13:30
# Size of source mod 2**32: 2034 bytes
from .dependency import Dependency
from ..config import Configuration
from ..handlers.color import Color
from ..handlers.process import Process
from ..plugins.hashcat import HcxPcapTool
import os

class John(Dependency):
    __doc__ = ' Wrapper for John program. '
    dependency_required = False
    dependency_name = 'john'
    dependency_url = 'http://www.openwall.com/john/'

    @staticmethod
    def crack_handshake(handshake, show_command=False):
        john_file = HcxPcapTool.generate_john_file(handshake,
          show_command=show_command)
        formats_stdout = Process(['john', '--list=formats']).stdout()
        if 'wpapsk-opencl' in formats_stdout:
            john_format = 'wpapsk-opencl'
        else:
            if 'wpapsk-cuda' in formats_stdout:
                john_format = 'wpapsk-cuda'
            else:
                john_format = 'wpapsk'
        command = [
         'john',
         '--format=%s' % john_format,
         '--wordlist', Configuration.wordlist,
         john_file]
        if show_command:
            Color.pl('{+} {D}Running: {W}{P}%s{W}' % ' '.join(command))
        else:
            process = Process(command)
            process.wait()
            command = [
             'john', '--show', john_file]
            if show_command:
                Color.pl('{+} {D}Running: {W}{P}%s{W}' % ' '.join(command))
            process = Process(command)
            stdout, stderr = process.get_output()
            if '0 password hashes cracked' in stdout:
                key = None
            else:
                for line in stdout.split('\n'):
                    if handshake.capfile in line:
                        key = line.split(':')[1]
                        break

        if os.path.exists(john_file):
            os.remove(john_file)
        return key


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

import flask, json

app = flask.Flask(__name__)

posts = [
			{
				u"userId": 1,
				u"id": 1,
				u"title": u"Título 1",
				u"body": u"Mensaje 1"
			},
			{
				u"userId": 1,
				u"id": 2,
				u"title": u"Título 2",
				u"body": u"Mensaje 2"
			},
			{
				u"userId": 1,
				u"id": 3,
				u"title": u"Título 3",
				u"body": u"Mensaje 3"
			},
			{
				u"userId": 1,
				u"id": 4,
				u"title": u"Título 4",
				u"body": u"Mensaje 4"
			},
			{
				u"userId": 1,
				u"id": 5,
				u"title": u"Título 5",
				u"body": u"Mensaje 5"
			},
			{
				u"userId": 2,
				u"id": 6,
				u"title": u"Título 6",
				u"body": u"Mensaje 6"
			},
			{
				u"userId": 2,
				u"id": 7,
				u"title": u"Título 7",
				u"body": u"Mensaje 7"
			},
			{
				u"userId": 2,
				u"id": 8,
				u"title": u"Título 8",
				u"body": u"Mensaje 8"
			},
			{
				u"userId": 2,
				u"id": 9,
				u"title": u"Título 9",
				u"body": u"Mensaje 9"
			},
			{
				u"userId": 2,
				u"id": 10,
				u"title": u"Título 10",
				u"body": u"Mensaje 10"
			},
			{
				u"userId": 3,
				u"id": 11,
				u"title": u"Título 11",
				u"body": u"Mensaje 11"
			},
			{
				u"userId": 3,
				u"id": 12,
				u"title": u"Título 12",
				u"body": u"Mensaje 12"
			},
			{
				u"userId": 3,
				u"id": 13,
				u"title": u"Título 13",
				u"body": u"Mensaje 13"
			},
			{
				u"userId": 3,
				u"id": 14,
				u"title": u"Título 14",
				u"body": u"Mensaje 14"
			},
			{
				u"userId": 3,
				u"id": 15,
				u"title": u"Título 15",
				u"body": u"Mensaje 15"
			}
		]

users = [
			{
				u"id": 1,
				u"name": u"Leanne Graham",
				u"username": u"Bret"
			},
			{
				u"id": 2,
				u"title": u"Ervin Howell",
				u"username": u"Antonette"
			},
			{
				u"id": 3,
				u"name": u"Clementine Bauch",
				u"username": u"Samantha"
			}
		]

def getPostsUserId(userId):
	postsUser = []
	try:
		id = int(userId)
		for post in posts:
			if post["userId"] == id:
				postsUser.append(post)
		return json.dumps(postsUser)
	except:
		return json.dumps(postsUser), 404

@app.route("/")
def index():
	return u"Bienvenido a mi servidor REST"

@app.route("/posts")
def getPosts():
	query_string = flask.request.args
	if "userId" in query_string:
		return getPostsUserId(query_string["userId"])
	else:
		return json.dumps(posts)

@app.route("/posts/<id>")
def getPostsId(id):
	try:
		if (int(id) > len(posts)) or (int(id) < 1):
			return json.dumps({}), 404
		else:
			return json.dumps(posts[int(id) - 1])
	except:
		return json.dumps({}), 404

@app.route("/users")
def getUsers():
	return json.dumps(users)

@app.route("/users/<id>")
def getUsersId(id):
	try:
		if (int(id) > len(users)) or (int(id) < 1):
			return json.dumps({}), 404
		else:
			return json.dumps(users[int(id) - 1])
	except:
		return json.dumps({}), 404

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


from pyld import jsonld
import json
import re
import requests
import grequests
from collections import defaultdict
from subprocess import Popen, PIPE, STDOUT
from joblib import Parallel, delayed
import multiprocessing
import logging

logger = logging.getLogger(__name__)

from .utils import readFile

class JSONLDHelper:
    def __init__(self):
        self.processor = jsonld.JsonLdProcessor()
        self.temp_attr_id = None

    def jsonld2nquads_helper(self, jsonld_doc):
        """
        Given a JSONLD document, return its nquads format

        Params
        ======
        jsonld_doc: jsonld document containing both JSON and the context file

        TODO
        ======
        Currently it relies on the JSONLD ruby client to convert to nquads
        When the JSONLD Python client is ready to adapt to 1.1,
        should switch to the Python client
        """
        """
        No longer need JSON-LD Ruby Client
        PyLD for 1.1 json-ld version is available
        # the following 6 lines use JSON-LD Ruby client to convert
        # JSON-LD document into NQuads format
        cmd = 'jsonld --validate --format nquads'
        p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
        p.stdin.write(doc.encode('utf-8'))
        stdout_data = p.communicate()[0]
        p.stdin.close()
        _response = stdout_data.decode()
        """
        # convert from jsonld doc to nquads format
        nquads = jsonld.to_rdf(jsonld_doc, {'format': "application/nquads"})
        """
        No longer need to deal with ruby error message
        if _response.startswith(('Error', 'ERROR')):
            logger.error("An error occured when JSON-LD Ruby client tries to parse the JSON-LD. \
                         The first 100 chars of the JSON document is %s", jsonld_doc[:100])
            return None
        # deal with cases when JSON-LD Ruby client returns empty resutls
        elif _response.startswith('\nParsed 0 statements'):
            logger.warning("0 statements is found when JSON-LD Ruby client tries to parse the JSON-LD input.\
                           The first 100 chars of the JSON document is %s", jsonld_doc[:100])
        else:
        """
        try:
            return self.processor.parse_nquads(nquads)
        except Exception as e:
            logger.error("Something Unexpected happend when JSON-LD Python client tries to parse the JSON-LD. \
                         The first 100 chars of the JSON document is %s", json.dumps(jsonld_doc[:100]))
            logger.error(e, exc_info=True)
            return None

    def jsonld2nquads(self, jsonld_docs):
        """
        Given a JSON-LD annotated document,
        Fetch it's corresponding NQUADs file from JSON-LD playground
        'http://jsonld.biothings.io/?action=nquads'

        TODO: Currently, PyLD hasn't been updated to match JSON-LD v 1.1
        So we are using the JSON-LD playground API, which is built upon
        JSON-LD ruby client for 1.1 version. When PyLD has been updated to
        match 1.1, we should switch back to PyLD.

        Params
        ======
        jsonld_doc: (dict)
            JSON-LD annotated document
        """
        # handle cases where input is a list of JSON documents
        # in this case, the results will also be a list of NQuads parsing results
        if type(jsonld_docs) == list and type(jsonld_docs[0]) == dict:
            #results = Parallel(n_jobs=multiprocessing.cpu_count())(delayed(self.jsonld2nquads_helper)(_doc) for _doc in jsonld_docs)
            results = []
            for _doc in jsonld_docs:
                results.append(self.jsonld2nquads_helper(_doc))
            if len(results) == 1:
                return results[0]
            else:
                return results
        # handle cases where input is a single JSON object document
        # in this case, the results will be a single NQuads parsing result
        elif type(jsonld_docs) == dict:
            """
            jsonld_docs = [jsonld_docs]
            results = Parallel(n_jobs=multiprocessing.cpu_count())(delayed(self.jsonld2nquads_helper)(_doc) for _doc in jsonld_docs)
            return results[0]
            """
            return self.jsonld2nquads_helper(jsonld_docs)
        # if the input is neither list of json_docs nor single json_doc
        # log error message and return None
        else:
            logger.warning("The input of the jsonld2nquads function should be a list of JSON docs or a single JSON dictionary doc. \
                           You input is %s. The first 100 chars of the input is %s", type(jsonld_docs), jsonld_doc[:100])
            return None

    def json2jsonld(self, json_docs, jsonld_context_path):
        """
        Given a JSON document and the endpoint where the doc comes from
        Fetch the JSON-LD context file for the endpoint
        Apply the JSON-LD context to JSON file to construct the JSON-LD document

        Return
        ======
        JSON-LD document
        """
        jsonld_context = readFile(jsonld_context_path)
        if type(json_docs) == list and type(json_docs[0]) == dict:
            jsonld_docs = [json_doc.update(jsonld_context) for json_doc in json_docs]
            return json_docs
        elif type(json_docs) == dict:
            json_docs.update(jsonld_context)
            return json_docs
        else:
            logger.warning("The input of the json2jsonld function should be a list of JSON docs or a single JSON dictionary doc. \
                           You input is %s. The first 100 chars of the input is %s", type(jsonld_docs), jsonld_doc[:100])
            return None

    def json2nquads(self, json_docs, context_file_path):
        """
        Given a JSON document, perform the following actions
        1) Find the json-ld context file based on endpoint_name
        2) Add JSON-LD context file to JSON doc
        3) Convert the JSON-LD doc into N-quads format

        Params
        ======
        json_doc: (dict)
            JSON document fetched from API calls
        endpoint_name: (str)
            the endpoint which the JSON doc comes from
        output: (str)
            URI subject
        predicate:
            NQUADS predicate, default is None
        """
        jsonld_docs = self.json2jsonld(json_docs, context_file_path)
        nquads = self.jsonld2nquads(jsonld_docs)
        return nquads

    def find_object_properties_in_jsonld(self, _dict):
        """
        extract the @base field corresponding to "attr:id" in a nested JSON-LD context file
        """
        for k,v in _dict.items():
            # check if @id startswith "attr:" or "rel:"
            if isinstance(v, dict) and "@id" in v and v["@id"] == "attr:id":
                # @base should be in the child level of @context
                if "@base" in v["@context"]:
                    self.temp_attr_id = v["@context"]["@base"]
                else:
                    print('@base should be included here! Something wrong with the JSON-LD context file!!')
            # otherwise, recall this function to look into the child level
            elif isinstance(v, dict):
                self.find_object_properties_in_jsonld(v)

    def jsonld_parser_helper(self, _dict, relation=defaultdict(set)):
        """
        extract relationship information from "@id" which startsfrom "assoc:"
        extract output information from "@base"
        """
        for k, v in _dict.items():
            # First, looking for value of @id startswith "assoc"
            # this represents an association in the nested structure
            if isinstance(v, dict) and "@id" in v and v["@id"].startswith("assoc:"):
                # Next, looking for whether "@base" exists in the direct child level
                if "@context" in v and "@base" in v["@context"]:
                    relation[v["@context"]["@base"]].add(v["@id"])
                # If "@base" not exists in direct child level, look for levels deeper
                elif "@context" in v:
                    self.temp_attr_id = None
                    self.find_object_properties_in_jsonld(v["@context"])
                    if self.temp_attr_id:
                        relation[self.temp_attr_id].add(v["@id"])
                    else:
                        print("attr:id is missing in the object properties!")
            elif isinstance(v, dict):
                self.jsonld_parser_helper(v, relation=relation)
        return relation

    def jsonld_relation_parser(self, jsonld_context):
        """
        Given a JSON-LD context file, reorganize the file
        so that the key would be the attr id,
        the rest of the information would be wrapped in the value

        Example Outputs:

        >>> jsonld_helper = JSONLDHelper()
        >>> jsonld_context_file = {"hits": {"@id": "http://bt.d2g/", "@type": "@id", "@context": {"gene": {"@id": ""}}}
        }}

        """
        if type(jsonld_context) != dict or '@context' not in jsonld_context:
            logging.error("Invalid JSON-LD context file!")
            return
        return self.jsonld_parser_helper(jsonld_context, relation=defaultdict(set))

    def fetch_object_value_by_predicate_value_in_nquads(self, nquads, predicate_value):
        """
        Given a nquads parsing results and a predicate_value
        find the corresponding object value(s)
        """
        object_values = []
        if '@default' in nquads:
            nquads = nquads['@default']
        for _nquad in nquads:
            if _nquad['predicate']['value'] == predicate_value:
                object_values.append(_nquad['object']['value'])
        return object_values

    def fetch_object_and_predicate_value_by_subject_value_in_nquads(self, nquads, subject_value, results=None):
        """
        Given a nquads parsing results and a subject_value
        find the corresponding object and predicate value
        """
        if not results:
            results = defaultdict(list)
        if '@default' in nquads:
            nquads = nquads['@default']
        for _nquad in nquads:
            if _nquad['subject']['value'] == subject_value:
                current_predicate_value = _nquad['predicate']['value']
                current_object_value = _nquad['object']['value']
                if current_predicate_value != 'http://biothings.io/pass/':
                    results[current_predicate_value].append(_nquad['object']['value'])
                else:
                    results = self.fetch_object_and_predicate_value_by_subject_value_in_nquads(nquads, _nquad['object']['value'], results)
        return results

    def fetch_properties_by_association_in_nquads(self, nquads, association_list):
        results = {}
        for _association in association_list:
            results[_association] = []
            object_values = self.fetch_object_value_by_predicate_value_in_nquads(nquads, _association)
            for _object_value in object_values:
                if _object_value.startswith('_:'):
                    object_predicate_dict = self.fetch_object_and_predicate_value_by_subject_value_in_nquads(nquads, _object_value)
                    if object_predicate_dict:
                        results[_association].append(object_predicate_dict)
                    else:
                        print("Could not fetch any properties from the given association: {}".format(_object_value))
                else:
                    results[_association].append({'http://biothings.io/explorer/vocab/attributes/id': [_object_value]})
        return results

    def fetch_properties_by_association_and_prefix_in_nquads(self, nquads, association, prefix):
        association_results = self.fetch_properties_by_association_in_nquads(nquads, [association])
        association_and_prefix_results = [_doc for _doc in association_results[association] if _doc['http://biothings.io/explorer/vocab/attributes/id'][0].startswith(prefix)]
        return association_and_prefix_results

t = jsonld.JsonLdProcessor()

def process_jsonld(doc):
    # cmd = 'ruby jsonld_test_cli.rb -a compact'
    doc = json.dumps(doc)
    logger.debug('The JSONLD file after json.dumps is %s', doc)
    RUBY_JSONLD_CMD = 'jsonld'
    cmd = RUBY_JSONLD_CMD + ' '
    cmd += '--validate --format nquads'
    p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
    p.stdin.write(doc.encode('utf-8'))
    # stdout_data = p.communicate(input=doc.encode('utf-8'))[0]
    stdout_data = p.communicate()[0]
    p.stdin.close()
    _response = stdout_data.decode()
    if 'Parsed' in _response:
        _nquad = re.sub('Parsed .*second.\n', '', _response)
        return t.parse_nquads(_nquad)
    else:
        return None

def json2jsonld(json_doc, jsonld_context_path):
    """

    """
    doc = json.dumps(doc).replace(' ', '')
    cmd = 'jsonld --validate --format nquads'
    p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
    p.stdin.write(doc.encode('utf-8'))
    stdout_data = p.communicate()[0]
    p.stdin.close()
    _response = stdout_data.decode()
    # check if startswith 'ERROR'
    # check if return nquads
    # check if the nquads is empty
    # if parsing error
    if 'Parsed' in _response:
        _nquad = re.sub('Parsed .*second.\n', '', _response)
        return t.parse_nquads(_nquad)
    else:
        return None

'''
def jsonld2nquads(jsonld_doc, mode='batch'):
    """
    Given a JSON-LD annotated document,
    Fetch it's corresponding NQUADs file from JSON-LD playground
    'http://jsonld.biothings.io/?action=nquads'

    TODO: Currently, PyLD hasn't been updated to match JSON-LD v 1.1
    So we are using the JSON-LD playground API, which is built upon
    JSON-LD ruby client for 1.1 version. When PyLD has been updated to
    match 1.1, we should switch back to PyLD.

    Params
    ======
    jsonld_doc: (dict)
        JSON-LD annotated document
    """
    # need to skip html escapes
    if mode != 'batch':
        nquads = requests.post('http://jsonld.biothings.io/?action=nquads', data={'doc': json.dumps(jsonld_doc).replace('>', ">").replace(' ', '')})
        if nquads.status_code != 413:
            # remove the log line from the nquads
            nquads = re.sub('Parsed .*second.\n', '', nquads.json()['output'])
            return t.parse_nquads(nquads)
    elif mode == 'batch':
        responses = []
        for _jsonld_doc in jsonld_doc:
            responses.append(grequests.post('http://jsonld.biothings.io/?action=nquads', data={'doc': json.dumps(_jsonld_doc).replace('>', ">").replace(' ', '')}))
        responses = grequests.map(iter(responses))
        results = []
        for _response in responses:
            if _response.status_code != 413:
                nquads = re.sub('Parsed .*second.\n', '', _response.json()['output'])
                results.append(t.parse_nquads(nquads))
            else:
                results.append(None)
        return results
'''
def jsonld2nquads(jsonld_docs):
    """
    Given a JSON-LD annotated document,
    Fetch it's corresponding NQUADs file from JSON-LD playground
    'http://jsonld.biothings.io/?action=nquads'

    TODO: Currently, PyLD hasn't been updated to match JSON-LD v 1.1
    So we are using the JSON-LD playground API, which is built upon
    JSON-LD ruby client for 1.1 version. When PyLD has been updated to
    match 1.1, we should switch back to PyLD.

    Params
    ======
    jsonld_doc: (dict)
        JSON-LD annotated document
    """
    results = []
    """
    for _doc in jsonld_docs:
        _response = process_jsonld(_doc)
        if 'Parsed' in _response:
            _nquad = re.sub('Parsed .*second.\n', '', _response)
            results.append(t.parse_nquads(_nquad))
        else:
            results.append(None)
    """
    num_cores = multiprocessing.cpu_count()
    results = Parallel(n_jobs=num_cores)(delayed(process_jsonld)(_doc) for _doc in jsonld_docs)
    return results

def fetchvalue(nquads, object_uri, predicate=None):
    """
    Given a NQUADS together with (URI/subject, predicate) pair
    Extract the object value

    Params
    ======
    nquads: (list)
        NQUADS doc
    object_uri: (str)
        URI subject
    predicate:
        NQUADS predicate. If None is specified, return all objects matching the subject
    """
    results = []
    # check if it's a valid nquads
    if nquads and '@default' in nquads:
        for _nquad in nquads['@default']:
            if predicate and object_uri in _nquad['object']['value'] and _nquad['predicate']['value'].split('/')[-1] == predicate.split(':')[-1]:
                results.append((_nquad['object']['value'].split(object_uri)[1], _nquad['predicate']['value'].split('/')[-1]))
            elif not predicate and object_uri in _nquad['object']['value']:
                results.append((_nquad['object']['value'].split(object_uri)[1], _nquad['predicate']['value'].split('/')[-1]))
    elif nquads:
        print('This is a invalid nquads, missing "@default"!!!')
    else:
        print('The nquads is empty')
    # if results is empty, it could be either nquads is empty or object_uri could not be found in nuqads
    if results:
        return list(set(results))
    else:
        return

def find_base(d, relation=defaultdict(set)):
    """
    Iterative function
    Given a JSON-LD context file as Python Dictionary,
    return all bio-entity URIs in that context file
    together with the relationship(s) as a set
    e.g. {'http://identifiers.org/pdb/': {'ont:has3DStructure'}}

    Params
    ======
    d: (dict)
        JSON-LD context
    relation: (dict)
        temporarily store relation info
    """
    for k, v in d.items():
        if isinstance(v, dict) and "@context" in v and "@base" in v["@context"]:
            relation[v["@context"]["@base"]].add(v["@id"])
        # if v is a dict and doesnt have @base, then reiterative the process
        elif isinstance(v, dict):
            find_base(v, relation=relation)
    return relation

def json2nquads(json_doc, context_file_path, output_type, predicate=None):
    """
    Given a JSON document, perform the following actions
    1) Find the json-ld context file based on endpoint_name
    2) Add JSON-LD context file to JSON doc
    3) Convert the JSON-LD doc into N-quads format

    Params
    ======
    json_doc: (dict)
        JSON document fetched from API calls
    endpoint_name: (str)
        the endpoint which the JSON doc comes from
    output: (str)
        URI subject
    predicate:
        NQUADS predicate, default is None
    """
    context_file = readFile(context_file_path)
    for _json_doc in json_doc:
        _json_doc.update(context_file)
    nquads = jsonld2nquads(json_doc)
    results = []
    for _nquad in nquads:
        output = fetchvalue(_nquad, output_type, predicate=predicate)
        if output:
            outputs = list(set(output))
            results.append(outputs)
        else:
            results.append(None)
    return results


# -*- coding: utf-8 -*-
'''
TopQuant-简称TQ极宽智能量化回溯分析系统，培训课件-配套教学python课件程序

Top极宽量化(原zw量化)，Python量化第一品牌
by Top极宽·量化开源团队 2017.10.1 首发

网站： www.TopQuant.vip      www.ziwang.com
QQ群: Top极宽量化1群，124134140
      Top极宽量化2群，650924099
      Top极宽量化3群，450853713

'''

import pandas as pd
import plotly as py
import plotly.figure_factory as pyff
#from plotly.tools import FigureFactory as pyff
#
import zsys
import ztools as zt
import ztools_str as zstr
import ztools_draw as zdr
import zpd_talib as zta
#
#-----------------
print('''
      本案例取消
      因为pd,mpl,ts，openCV，sklearn,tf部分基础模块库，api函数接口改了
      有兴趣的用户，请自行根据提示信息，修改相关源码

      开源项目，函数API接口，参数变化，属于很正常的现象
      每次大的版本升级，都会有个别模块库函数API接口变化，
      这种因为版本变化，引发的程序代码冲突，称为：版本冲突
      所以，使用开源软件，要养成多动手搜索/查看最新版本的软件文档/函数接口餐宿
      ''')
#-----------------

#================================
#1 预处理

pd.set_option('display.width', 450)
pyplt=py.offline.plot
#---------------

#2
xcod='600663'
fss='data/'+xcod+'.csv'
df=pd.read_csv(fss,index_col=0)
df=df.sort_index()
print('\n#2,df.tail()')
print(df.tail())

#3
print('\n#3,plot-->tmp/tmp_.html')
hdr,fss='k线图-'+xcod,'tmp/tmp_.html'
df2=df.tail(100)

#
# plotly函数接口变化，取消了vol成交量图形
#
zdr.drDF_cdl(df2,ftg=fss,m_title=hdr)

#4
print('\n#4,ok')


import pytest
from tinybaker import Transform, fileset, sequence
from tinybaker.exceptions import FileSetError, BakerError, BakerUnsupportedError

def test_filesets_work_for_inputs():
    class Concat(Transform):
        input_tags = {fileset("files")}
        output_tags = {"concatted"}

        def script(self):
            content = ""
            for ref in self.input_files[fileset("files")]:
                with ref.open() as f:
                    content = content + f.read()

            with self.output_files["concatted"].open() as f:
                f.write(content)

    Concat(
        input_paths={
            fileset("files"): ["./tests/__data__/foo.txt", "./tests/__data__/bar.txt"],
        },
        output_paths={"concatted": "/tmp/concatted"},
        overwrite=True,
    ).run()

    with open("/tmp/concatted", "r") as f:
        assert f.read() == "foo contentsbar contents"

def test_filesets_work_for_outputs():
    class Concat(Transform):
        input_tags = {"source"}
        output_tags = {fileset("copied")}

        def script(self):
            with self.input_files["source"].open() as f:
                content = f.read()

            for ref in self.output_files[fileset("copied")]:
                with ref.open() as f:
                    f.write(content)

    outpaths = ["/tmp/copy1", "/tmp/copy2", "/tmp/copy3"]
    Concat(
        input_paths={
            "source": "./tests/__data__/foo.txt",
        },
        output_paths={fileset("copied"): outpaths},
        overwrite=True,
    ).run()

    for path in outpaths:
        with open(path, "r") as f:
            assert f.read() == "foo contents"

def test_filesets_dont_work_for_sequences():
    class One(Transform):
        input_tags = {"foo"}
        output_tags = {fileset("files")}

        def script(self):
            pass

    class Two(Transform):
        input_tags = {fileset("files")}
        output_tags = {"bar"}

        def script(self):
            pass

    with pytest.raises(BakerUnsupportedError):
        sequence([One, Two])(
            input_paths={"foo": "./tests/__data__/foo.txt"},
            output_paths={"bar": "./tests/__data__/bar.txt"},
            overwrite=True,
        ).run()


import argparse
import cmath
import logging
import math
import pathlib
import random
import shutil

import h5py
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.cuda as cuda
import torch.nn as nn
from skimage.measure import compare_ssim as ssim
from tensorboardX import SummaryWriter
from torch.autograd import Variable
from torch.nn import functional as F
from torch.utils.data import Dataset

import utils
from data import transforms
from anet_model import AnetModel
from args import get_args

args = get_args()
train_loader, dev_loader = utils.create_data_loaders(args)

# ### Custom dataset class

def build_model(args):
    model = AnetModel(
        in_chans=2,
        out_chans=2,
        chans=args.num_chans,
        num_pool_layers=args.num_pools,
        drop_prob=args.drop_prob
    ).to(args.device)
    return model

# def build_optim(args, params):
#     optimizer = torch.optim.RMSprop(params, args.learning_rate, weight_decay=args.weight_decay)
#     return optimizer

# def load_model(checkpoint_file):
#     checkpoint = torch.load(checkpoint_file)
#     args = checkpoint['args']
#     model = build_model(args)
#     model.load_state_dict(checkpoint['model'])

#     optimizer = build_optim(args, model.parameters())
#     optimizer.load_state_dict(checkpoint['optimizer'])
#     return checkpoint, model, optimizer

# ### Image normalized

loss_func = nn.MSELoss()
best_val_loss = 1e9
writer = SummaryWriter(log_dir=args.exp_dir+'/summary')
valid_loss=[]
train_loss=[]
print('Total number of epochs:', args.epoch)
print('Total number of training iterations: ',len(train_loader))
print('Total number of validation iterations: ',len(dev_loader))

if args.resume:
    checkpoint, model, optimizer = utils.load_model(args.exp_dir + "/best_model.pt", build_model(args))
    best_dev_loss = checkpoint['best_dev_loss']
    start_epoch = checkpoint['epoch']
    if checkpoint['state']=='train':
        train = False
    del checkpoint
else:
    model = build_model(args)
    # if args.data_parallel:
        # model = torch.nn.DataParallel(model)
    optimizer = utils.build_optim(args, model.parameters())
    best_dev_loss = 1e9
    start_epoch = 0
    train = True

print(model)

# if args.resume:
#     checkpoint, model optimizer = load_model(args.checkpoint)
#     #best_val_loss = checkpoint['best_val_loss']
#     start_epoch = checkpoint['epoch']
#     if checkpoint['state']=='train':
#         train = False
#     del checkpoint
# else:
#     encoder = Encoder().cuda()
#     decoder = Decoder().cuda()
#     parameters = list(encoder.parameters())+ list(decoder.parameters())
#     optimizer = torch.optim.Adam(parameters, lr=args.learning_rate)
#     start_epoch = 0
#     train = True

for i in range(start_epoch, args.epoch):
    print("Epoch: ",i)
    global_step = i * len(train_loader)
    ##########################################TRAINING PHASE######################################################
    if train:
        print("Training Phase")
        total_loss_kspace = total_loss_image = 0.0
        model.train()
        for j, data in enumerate(train_loader):
            original_kspace, masked_kspace, mask, target, fname, slice_index = data

            # preprocessing the kspace (input)
            if args.preprocess == "unitize":
                nmasked_kspace, divisor = utils.unitize(masked_kspace)
                noriginal_kspace, divisor = utils.unitize(original_kspace, divisor)
            elif args.preprocess == "standardize":
                nmasked_kspace, mean, std = utils.standardize(masked_kspace)
                noriginal_kspace, mean, std = utils.standardize(original_kspace, mean, std)

            # transforming the input according to dimension and type
            noriginal_kspace, nmasked_kspace = utils.transformshape(noriginal_kspace), utils.transformshape(nmasked_kspace)

            nmasked_kspace = Variable(nmasked_kspace).to(args.device)
            noriginal_kspace = Variable(noriginal_kspace).to(args.device)

            # forward pass
            outputkspace = model(nmasked_kspace)

            # finding the kspace loss
            loss_kspace = loss_func(outputkspace, noriginal_kspace)
            loss_image = loss_func(utils.kspaceto2dimage(utils.transformback(outputkspace), args.polar), utils.kspaceto2dimage(utils.transformback(noriginal_kspace), args.polar))

            # setting up all the gradients to zero
            optimizer.zero_grad()

            #backward pass
            (loss_kspace + 3.0*loss_image).backward()
            optimizer.step()

            total_loss_kspace += loss_kspace.data.item()
            total_loss_image += loss_image.data.item()
            if j % 100 == 0:
                avg_loss_kspace, avg_loss_image = total_loss_kspace/(j + 1), total_loss_image/(j + 1)
                print(j+1, ': AVG TRAINING LOSS: Kspace:', avg_loss_kspace, 'Image', avg_loss_image, 'ITR LOSS: Kspace', loss_kspace.data.item(), 'Image', loss_image.data.item())

                if j % 500 == 0:
                    utils.compareimageoutput(original_kspace, masked_kspace, outputkspace, mask, writer, global_step + j + 1, 0, args.polar)
            writer.add_scalar('TrainKspaceLoss', loss_kspace.data.item(), global_step + j+1)
            writer.add_scalar('TrainImageLoss', loss_image.data.item(), global_step + j+1)
        utils.save_model(args, args.exp_dir, i+1 , model, optimizer, best_val_loss, False, 'train')
        # train_loss.append(total_loss_kspace/len(train_loader))
    train = True

    ################################VALIDATION#######################################################
    print("Validation Phase")
    # validation loss
    total_val_loss = 0.0
    model.eval()
    for j,data in enumerate(dev_loader):
        original_kspace, masked_kspace, mask, target, fname, slice_index = data

        # preprocessing the kspace (input)
        if args.preprocess == "unitize":
            nmasked_kspace, divisor = utils.unitize(masked_kspace)
            noriginal_kspace, divisor = utils.unitize(original_kspace, divisor)
        elif args.preprocess == "standardize":
            nmasked_kspace, mean, std = utils.standardize(masked_kspace)
            noriginal_kspace, mean, std = utils.standardize(original_kspace, mean, std)

        # transforming the input according to dimension and type
        noriginal_kspace, nmasked_kspace = utils.transformshape(noriginal_kspace), utils.transformshape(nmasked_kspace)

        nmasked_kspace = Variable(nmasked_kspace).to(args.device)
        noriginal_kspace = Variable(noriginal_kspace).to(args.device)

        # forward pass
        outputkspace = model(nmasked_kspace)

        # finding the kspace loss
        loss_kspace = loss_func(outputkspace, noriginal_kspace)
        loss_image = loss_func(utils.kspaceto2dimage(utils.transformback(outputkspace), args.polar), utils.kspaceto2dimage(utils.transformback(noriginal_kspace), args.polar))

        loss_itr = loss_kspace.data.item() + loss_image.data.item()

        total_val_loss += loss_itr

        if j % 100 == 0:
            avg_loss = total_val_loss/(j+1)
            print(j+1, ': AVG VALIDATION LOSS:', avg_loss, 'ITR LOSS:', loss_itr)
            if j % 200 == 0:
                utils.compareimageoutput(original_kspace,masked_kspace,outputkspace,mask,writer,global_step + j+1, 0, args.polar)

        writer.add_scalar('ValidationLoss', loss_itr, global_step + j+1)

    valid_loss.append(total_val_loss / len(dev_loader))

    print('saving')
    is_new_best = valid_loss[-1] < best_val_loss
    best_val_loss = min(best_val_loss, valid_loss[-1])
    print("best val loss :",best_val_loss)
    utils.save_model(args, args.exp_dir, i+1, model, optimizer, best_val_loss, is_new_best, 'valid')
writer.close()


"""BOJ Q5376 - 소수를 분수로 (https://www.acmicpc.net/problem/5376)
"""

from fractions import gcd

n = int(input())
for i in range(n):
    num = input()
    divisor = dividend = 0
    if '(' in num:
        # ex) num: '0.12(34)'
        # ==> a: '12', b: '34', divisior = 9900, dividend = 1234
        a, b = num[2:-1].split('(')
        divisor = (10 ** len(a)) * (10 ** len(b) - 1)
        dividend = int(a + b) - (int(a) if a else 0)
    else:
        a = num[2:]
        divisor = 10 ** len(a)
        dividend = int(a)

    gcd_value = gcd(divisor, dividend)
    print("%d/%d" % (dividend // gcd_value, divisor // gcd_value))


'''
Created on Jan 21, 2018

@author: andrewself
'''
import bs4 as bs
import urllib.request

BASE_URL = 'https://www.reg.uci.edu/perl/WebSoc?'

COURSE_CODE_INDEX = 0
MAX_INDEX = 8
ENR_INDEX = 9
WL_INDEX = 10
REQ_INDEX = 11

#sauce = urllib.request.urlopen('https://pythonprogramming.net/parsememcparseface/').read()
#soup = bs.BeautifulSoup(sauce,'lxml')

'''Gets list of names of all departments to creatre url for each department'''
def getDepts():
    Depts = set()
    with urllib.request.urlopen(BASE_URL) as sauce:
        soup = bs.BeautifulSoup(sauce,'lxml')
        for option in soup.find_all('select'):
            #print(option.get('name'))
            if (option.get('name') == 'Dept'):
                for name in option.find_all('option'):
                    Depts.add(name.get('value'))
        return Depts

'''creates url for each department'''
def getURL(depts):
    urls = set()
    for i in sorted(depts):
        #print(i)
        fields=[('YearTerm','2018-14'),('ShowFinals','1'),('ShowComments','1'),('Dept',i)]
        url = BASE_URL + urllib.parse.urlencode(fields)
        #print(url)
        urls.add(url)
    return urls

'''for each url, creates a dictionary {coursecode: (max, enrolled, waitlisted, requests)}'''
def UrlToDict(url):
    codes={}
    sauce = urllib.request.urlopen(url).read()
    soup = bs.BeautifulSoup(sauce, 'lxml')
    for tr in soup.find_all('tr'):
        #print(tr)
        classes = [td.string for td in tr.find_all('td')]
        if (len(classes)==17 and classes[3] != '0'):
            #print(classes)
            code = classes[COURSE_CODE_INDEX]
            cap = classes[MAX_INDEX]
            enr = classes[ENR_INDEX]
            if '/' in enr:
                enr = enr[enr.find('/ ')+2:]
            wl = classes[WL_INDEX]
            req = classes[REQ_INDEX]
            codes[code] = (cap,enr,req,wl)
    #print(codes)
    #print(url)
    return codes

'''combines dictionary of every department'''
def getAllInfo(urls):
    all_info = {}
    for url in urls:
        info= UrlToDict(url)
        for k,v in info.items():
            all_info[k]=v
    return all_info

'''prints master dictionary'''
def print_dict(info):
    for k,v in info.items():
        print(k,v)
    print(len(info))

if __name__ == '__main__':
    departments = getDepts()
    urls = getURL(departments)
    master_dict=getAllInfo(urls)
    print_dict(master_dict)


#!/usr/bin/env python
#
# colourbarcanvas.py - Render a colour bar using OpenGL and matplotlib.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This module provides the :class:`ColourBarCanvas`.

The :class:`ColourBarCanvas` contains logic to draw a colour bar (with
labels), and then renders said colour bar as a texture using OpenGL.

See the :mod:`~fsleyes_widgets.colourbarbitmap` module for details on how
the colour bar is created.
"""

import logging

import OpenGL.GL as gl
import numpy     as np

import fsleyes_props                         as props
import fsleyes_widgets.utils.colourbarbitmap as cbarbmp
import fsleyes.gl.textures                   as textures

log = logging.getLogger(__name__)

class ColourBarCanvas(props.HasProperties):
    """Contains logic to render a colour bar as an OpenGL texture.
    """

    cmap = props.ColourMap()
    """The :mod:`matplotlib` colour map to use."""

    negativeCmap = props.ColourMap()
    """Negative colour map to use, if :attr:`useNegativeCmap` is ``True``."""

    useNegativeCmap = props.Boolean(default=False)
    """Whether or not to use the :attr:`negativeCmap`.
    """

    cmapResolution = props.Int(minval=2, maxval=1024, default=256)
    """Number of discrete colours to use in the colour bar. """

    invert = props.Boolean(default=False)
    """Invert the colour map(s). """

    vrange = props.Bounds(ndims=1)
    """The minimum/maximum values to display."""

    label = props.String()
    """A label to display under the centre of the colour bar."""

    orientation = props.Choice(('horizontal', 'vertical'))
    """Whether the colour bar should be vertical or horizontal. """

    labelSide = props.Choice(('top-left', 'bottom-right'))
    """Whether the colour bar labels should be on the top/left, or bottom/right
    of the colour bar (depending upon whether the colour bar orientation is
    horizontal/vertical).
    """

    textColour = props.Colour(default=(1, 1, 1, 1))
    """Colour to use for the colour bar label. """

    bgColour = props.Colour(default=(0, 0, 0, 1))
    """Colour to use for the background. """

    def __init__(self):
        """Adds a few listeners to the properties of this object, to update
        the colour bar when they change.
        """

        self._tex  = None
        self._name = '{}_{}'.format(self.__class__.__name__, id(self))

        self.addGlobalListener(self._name, self.__updateTexture)

    def __updateTexture(self, *a):
        self._genColourBarTexture()
        self.Refresh()

    def _initGL(self):
        """Called automatically by the OpenGL canvas target superclass (see the
        :class:`.WXGLCanvasTarget` and :class:`.OSMesaCanvasTarget` for
        details).

        Generates the colour bar texture.
        """
        self._genColourBarTexture()

    def destroy(self):
        """Should be called when this ``ColourBarCanvas`` is no longer needed.
        Destroys the :class:`.Texture2D` instance used to render the colour
        bar.
        """
        self.removeGlobalListener(self._name)
        self._tex.destroy()
        self._tex = None

    def _genColourBarTexture(self):
        """Generates a texture containing an image of the colour bar,
        according to the current property values.
        """

        if not self._setGLContext():
            return

        w, h = self.GetSize()

        if w < 50 or h < 50:
            return

        if self.orientation == 'horizontal':
            if  self.labelSide == 'top-left': labelSide = 'top'
            else:                             labelSide = 'bottom'
        else:
            if  self.labelSide == 'top-left': labelSide = 'left'
            else:                             labelSide = 'right'

        if self.cmap is None:
            bitmap = np.zeros((w, h, 4), dtype=np.uint8)
        else:

            if self.useNegativeCmap:
                negCmap    = self.negativeCmap
                ticks      = [0.0, 0.49, 0.51, 1.0]
                ticklabels = ['{:0.2f}'.format(-self.vrange.xhi),
                              '{:0.2f}'.format(-self.vrange.xlo),
                              '{:0.2f}'.format( self.vrange.xlo),
                              '{:0.2f}'.format( self.vrange.xhi)]
                tickalign  = ['left', 'right', 'left', 'right']
            else:
                negCmap    = None
                ticks      = [0.0, 1.0]
                tickalign  = ['left', 'right']
                ticklabels = ['{:0.2f}'.format(self.vrange.xlo),
                              '{:0.2f}'.format(self.vrange.xhi)]

            bitmap = cbarbmp.colourBarBitmap(
                cmap=self.cmap,
                negCmap=negCmap,
                invert=self.invert,
                ticks=ticks,
                ticklabels=ticklabels,
                tickalign=tickalign,
                width=w,
                height=h,
                label=self.label,
                orientation=self.orientation,
                labelside=labelSide,
                textColour=self.textColour,
                cmapResolution=self.cmapResolution)

        if self._tex is None:
            self._tex = textures.Texture2D('{}_{}'.format(
                type(self).__name__, id(self)), gl.GL_LINEAR)

        # The bitmap has shape W*H*4, but the
        # Texture2D instance needs it in shape
        # 4*W*H
        bitmap = np.fliplr(bitmap).transpose([2, 0, 1])

        self._tex.setData(bitmap)
        self._tex.refresh()

    def _draw(self):
        """Renders the colour bar texture using all available canvas space."""

        if self._tex is None or not self._setGLContext():
            return

        width, height = self.GetSize()

        # viewport
        gl.glViewport(0, 0, width, height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()
        gl.glOrtho(0, 1, 0, 1, -1, 1)
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glLoadIdentity()

        gl.glClearColor(*self.bgColour)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        gl.glShadeModel(gl.GL_FLAT)

        self._tex.drawOnBounds(0, 0, 1, 0, 1, 0, 1)


from rest_framework.response import Response
class APIResponse(Response):
    def __init__(self, status=0, msg='ok', results=None, http_status=None,
                 headers=None, exception=False, content_type=None, **kwargs):
        # 将status、msg、results、kwargs格式化成data
        data = {
            'status': status,
            'msg': msg,
        }
        # results只要不为空都是数据：False、0、'' 都是数据 => 条件不能写if results
        if results is not None:
            data['results'] = results
        # 将kwargs中额外的k-v数据添加到data中
        data.update(**kwargs)

        super().__init__(data=data, status=http_status, headers=headers, exception=exception, content_type=content_type)


from django.forms import ModelForm
from .models import *
from django.shortcuts import render, redirect, get_object_or_404

# Create your views here.

class LivroForm(ModelForm):
    class Meta:
        model = Livro
        fields = ['autor', 'editora', 'isbn', 'numeroPaginas', 'titulo', 'anoPublicacao','emailEditora']

def livro_list(request, template_name='livro_list.html'):
    livro = Livro.objects.all()
    livros = {'lista': livro}
    return render(request, template_name, livros)

def livro_new(request, template_name='livro_form.html'):
    form = LivroForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('livro_list')
    return render(request, template_name, {'form':form})

def livro_edit(request, pk,template_name='livro_form.html'):
    # Consultar um livro para obtê-lo através da sua chave primária
    livro = get_object_or_404(Livro, pk=pk)
    # Verificar se o formulário é do tipo “post’,
    # caso positivo,
    # criar um formulário com a instância do livro obtida do banco de dados e salvar as alterações.
    if request.method == "POST" or None:
        form = LivroForm(request.POST,instance=livro)
        if form.is_valid():
            livro =form.save()
            return redirect('livro_list')
        # Caso o formulário não seja do tipo ‘post’,
        # renderizar, novamente, o formulário de edição dos livros
    else:
        form = LivroForm(instance=livro)
    return render(request, template_name, {'form': form})

def livro_remove(request, pk):
    livro = Livro.objects.get(pk=pk)
    if request.method == "POST":
        livro.delete()
        return redirect('livro_list')
    return render(request,'livro_delete.html',{'livro': livro})


import requests
from bs4 import BeautifulSoup
from datetime import datetime
import re
import json
import pandas

def getNewsdetial(newsurl):
     res = requests.get(newsurl)
     res.encoding = 'utf-8'
     soup = BeautifulSoup(res.text,'html.parser')
     newsTitle = soup.select('.page-header h1')[0].text.strip()
     nt = datetime.strptime(soup.select('.time-source')[0].contents[0].strip(),'%Y %m %d %H:%M')
     newsTime = datetime.strftime(nt,'%Y-%m-%d %H:%M')
     newsArticle = getnewsArticle(soup.select('.article p'))
     newsAuthor = newsArticle[-1]
     return newsTitle,newsTime,newsArticle,newsAuthor
def getnewsArticle(news):
     newsArticle = []
     for p in news:
          newsArticle.append(p.text.strip())
     return newsArticle

def getCommentCount(newsurl):
     m = re.search('doc-i(.+).shtml',newsurl)
     newsid = m.group(1)
     commenturl = 'http://comment5.news.sina.com.cn/page/info?version=1&format=js&channel=gn&newsid=comos-{}&group=&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20'
     comment = requests.get(commenturl.format(newsid))
     jd = json.loads(comment.text.lstrip('var data='))
     return jd['result']['count']['total']

def getNewsLinkUrl():
     urlFormat = 'http://api.roll.news.sina.com.cn/zt_list?channel=news&cat_1=gnxw&cat_2==gdxw1||=gatxw||=zs-pl||=mtjj&level==1||=2&show_ext=1&show_all=1&show_num=22&tag=1&format=json&page={}&callback=newsloadercallback&_=1501000415111'
     url = []
     for i in range(1,10):
         res = requests.get(urlFormat.format(i))
         jd = json.loads(res.text.lstrip('  newsloadercallback(').rstrip(');'))
         url.extend(getUrl(jd))
     return url

def getUrl(jd):
    url = []
    for i in jd['result']['data']:
         url.append(i['url'])
    return url

def getNewsDetial():
     title_all = []
     author_all = []
     commentCount_all = []
     article_all = []
     time_all = []
     url_all = getNewsLinkUrl()
     for url in url_all:
         title_all.append(getNewsdetial(url)[0])
         time_all.append(getNewsdetial(url)[1])
         article_all.append(getNewsdetial(url)[2])
         author_all.append(getNewsdetial(url)[3])
     commentCount_all.append(getCommentCount(url))
     total_2 = {'a_title':title_all,'b_article':article_all,'c_commentCount':commentCount_all,'d_time':time_all,'e_editor':author_all}
     return total_2

df = pandas.DataFrame(getNewsDetial())
df.to_excel('news2.xlsx')


import pandas as pd
import numpy
from sklearn import model_selection
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from random import randint

text_file = open("Output.csv", "w")
text_file.write("x1,x2,result\n")

for x in range(0, 10000):
    a = randint(1,100)
    b = randint(1,100)
    c = a + b
    text_file.write("%d, %d, %d\n" % (a, b, c))

text_file.close()

df = pd.read_csv('Output.csv', sep=',', header=0)

train, test = model_selection.train_test_split(df,test_size=0.01, random_state=0)

#clf = GaussianNB()
clf = SVC()
train_features = train.ix[:,0:2]
train_label = train.iloc[:,2]

clf.fit(train_features, train_label)

test_features = test.ix[:,0:2]
test_label = test.iloc[:,2]

test_data = pd.concat([test_features, test_label], axis=1)
test_data["prediction"] = clf.predict(test_features)

print(test_data)

print ("Naive Bayes Accuracy:", clf.score(test_features,test_label))


import discord

from Util import Configuration

class DMMessages:

    def __init__(self, bot):
        self.bot = bot

    async def on_message(self, message: discord.Message):
        if message.guild is not None or len(message.content) > 1800 or message.author.id == self.bot.user.id:
            return
        if not message.content.startswith("!"):
            channel = self.bot.get_channel(Configuration.get_master_var("inbox", 0))
            if channel is not None:
                await channel.send(f"[`{message.created_at.strftime('%c')}`] {message.author} (`{message.author.id}`) said: {message.clean_content}")
            for attachement in message.attachments:
                await channel.send(attachement.url)

def setup(bot):
    bot.add_cog(DMMessages(bot))


#!/usr/bin/python
# -*- coding: utf-8 -*-
''' kubeclient_ca ansible module '''

import base64
import yaml
from ansible.module_utils.basic import AnsibleModule

DOCUMENTATION = '''
---
module: kubeclient_ca
short_description: Modify kubeclient certificate-authority-data
author: Andrew Butcher
requirements: [ ]
'''
EXAMPLES = '''
- kubeclient_ca:
    client_path: /etc/origin/master/admin.kubeconfig
    ca_path: /etc/origin/master/ca-bundle.crt

- slurp:
    src: /etc/origin/master/ca-bundle.crt
  register: ca_data
- kubeclient_ca:
    client_path: /etc/origin/master/admin.kubeconfig
    ca_data: "{{ ca_data.content }}"
'''

def main():
    ''' Modify kubeconfig located at `client_path`, setting the
        certificate authority data to specified `ca_data` or contents of
        `ca_path`.
    '''

    module = AnsibleModule(  # noqa: F405
        argument_spec=dict(
            client_path=dict(required=True),
            ca_data=dict(required=False, default=None),
            ca_path=dict(required=False, default=None),
            backup=dict(required=False, default=True, type='bool'),
        ),
        supports_check_mode=True,
        mutually_exclusive=[['ca_data', 'ca_path']],
        required_one_of=[['ca_data', 'ca_path']]
    )

    client_path = module.params['client_path']
    ca_data = module.params['ca_data']
    ca_path = module.params['ca_path']
    backup = module.params['backup']

    try:
        with open(client_path) as client_config_file:
            client_config_data = yaml.safe_load(client_config_file.read())

        if ca_data is None:
            with open(ca_path) as ca_file:
                ca_data = base64.standard_b64encode(ca_file.read())

        changes = []
        # Naively update the CA information for each cluster in the
        # kubeconfig.
        for cluster in client_config_data['clusters']:
            if cluster['cluster']['certificate-authority-data'] != ca_data:
                cluster['cluster']['certificate-authority-data'] = ca_data
                changes.append(cluster['name'])

        if not module.check_mode:
            if len(changes) > 0 and backup:
                module.backup_local(client_path)

            with open(client_path, 'w') as client_config_file:
                client_config_string = yaml.dump(client_config_data, default_flow_style=False)
                client_config_string = client_config_string.replace('\'\'', '""')
                client_config_file.write(client_config_string)

        return module.exit_json(changed=(len(changes) > 0))

    # ignore broad-except error to avoid stack trace to ansible user
    # pylint: disable=broad-except
    except Exception as error:
        return module.fail_json(msg=str(error))

if __name__ == '__main__':
    main()


#!/usr/bin/env python

#Copyright (c) 2013, Eduard Broecker
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that
# the following conditions are met:
#
#    Redistributions of source code must retain the above copyright notice, this list of conditions and the
#    following disclaimer.
#    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
#    following disclaimer in the documentation and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
#WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
#DAMAGE.

#
# this script imports json-files from a canmatrix-object
# json-files are the can-matrix-definitions of the CANard-project (https://github.com/ericevenchick/CANard)

from builtins import *
from .canmatrix import *
import codecs
import json
import sys

def importJson(filename, **options):
    db = CanMatrix()

    f = open(filename, "r")
    jsonData = json.load(f)

    if "messages" in jsonData:
        for frame in jsonData["messages"]:
#            newframe = Frame(frame["id"],frame["name"],8,None)
            newframe = Frame(frame["name"],
                              Id=frame["id"],
                              dlc=8)

            if "is_extended_frame" in frame and frame["is_extended_frame"]:
                newframe._extended = 1
            else:
                newframe._extended = 0

            for signal in frame["signals"]:
                if signal["is_big_endian"]:
                    is_little_endian = False
                else:
                    is_little_endian = True
                if signal["is_signed"]:
                    is_signed = True
                else:
                    is_signed = False
                newsignal = Signal(signal["name"],
                                startBit=signal["start_bit"],
                                signalSize=signal["bit_length"],
                                is_little_endian=is_little_endian,
                                is_signed = is_signed,
                                factor=signal["factor"],
                                offset=signal["offset"])

                if newsignal._is_little_endian == False:

                    newsignal.setStartbit(newsignal._startbit, bitNumbering = 1, startLittle = True)
                newframe.addSignal(newsignal)
            db._fl.addFrame(newframe)
    f.close()
    return db


import datetime
import enum
from inspect import getframeinfo, stack

import termcolor

class State(enum.Enum):
    WARN = 'yellow'
    ERROR = 'red'
    INFO = 'green'

class Log:

    @staticmethod
    def __print(state: State, msg: str, filename: str):
        """
        Printmethod that shows message with state on the console.
        :param state: state
        :param msg: message to print
        """
        print(termcolor.colored('{}\t{}\t{}\t{}'.format(datetime.datetime.now(),
                                                        filename,
                                                        state.name,
                                                        msg), str(state.value)))

    @staticmethod
    def i(msg: str):
        """
        Logging info.
        :param msg: message to print
        """
        caller = getframeinfo(stack()[1][0])
        filename: str = caller.filename.split(sep='/')[-1]
        Log.__print(state=State.INFO, msg=msg, filename=filename)

    @staticmethod
    def e(msg: str):
        """
        Logging error.
        :param msg: message to print
        """
        caller = getframeinfo(stack()[1][0])
        filename: str = caller.filename.split(sep='/')[-1]
        Log.__print(state=State.ERROR, msg=msg, filename=filename)

    @staticmethod
    def w(msg: str):
        """
        Logging warning.
        :param msg: message to print
        """
        caller = getframeinfo(stack()[1][0])
        filename: str = caller.filename.split(sep='/')[-1]
        Log.__print(state=State.WARN, msg=msg, filename=filename)


import moai.nn.utils as miu

import torch
import functools
import logging
import typing

log = logging.getLogger(__name__)

__all__ = [
    "make_upsample",
]

__UPSAMPLE_FACTORY__ = {
    "none":                 torch.nn.Identity,
}

#NOTE: need to implement:
#  1) upscale->project,
#  2) project->upscale,
#  3) transpose conv with spatial and feature size reduction

def _update_upsample_op(name: str, type: typing.Type):
    if name not in __UPSAMPLE_FACTORY__.keys():
        __UPSAMPLE_FACTORY__.update({name: type})
    else:
        log.error(f"Trying to add an already existing key {name} in the convolution operation factory.")

def make_upsample(
    upscale_type: str,
    features: int,
    kernel_size: int=4,
    stride: int=2,
    **kwargs
) -> torch.nn.Module:
    if upscale_type in __UPSAMPLE_FACTORY__.keys():
        return miu.instantiate(__UPSAMPLE_FACTORY__[upscale_type],
        **{
            **locals(),
            **kwargs
        })
    else:
        log.error(f"Upscale type ({upscale_type}) not found.")

import moai.nn.sampling.spatial.upsample.deconv as mids

if "deconv2d" not in __UPSAMPLE_FACTORY__.keys():
    _update_upsample_op("deconv2d", mids.StridedDeconv2d)

del mids

import moai.nn.sampling.spatial.upsample.interpolate as miup

if "upsample2d" not in __UPSAMPLE_FACTORY__.keys():
    _update_upsample_op("upsample2d", miup.Upsample2d)

del miup