import tensorflow as tf

class CNN:
  def __init__(self, imgWidth, imgHeight, numClasses):
    self.x = tf.placeholder(tf.float32, shape=[None, imgWidth * imgHeight])
    self.y_ = tf.placeholder(tf.float32, shape=[None, numClasses])
    self.imgWidth = imgWidth
    self.imgHeight = imgHeight
    self.numClasses = numClasses
    self.W_conv1 = []
    self.b_conv1 = []
    self.W_conv2 = []
    self.b_conv2 = []
    self.W_fc1 = []
    self.b_fc1 = []
    self.W_fc2 = []
    self.b_fc2 = []
    self.model = []
    self.keep_prob = []
    self.accuracy = []
    self.session = tf.InteractiveSession()
    self.create()

  def weight_variable(self, shape):
  	initial = tf.truncated_normal(shape, stddev=0.1)
  	return tf.Variable(initial)

  def bias_variable(self, shape):
  	initial = tf.constant(0.1, shape = shape)
  	return tf.Variable(initial)

  def conv2d(self, x, W):
  	return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

  def max_pool_2x2(self, x):
  	return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

  def defineLayers(self):
    # first layer
    x_image = tf.reshape(self.x, [-1, self.imgWidth, self.imgHeight, 1])
    h_conv1 = tf.nn.relu(self.conv2d(x_image, self.W_conv1) + self.b_conv1)
    h_pool1 = self.max_pool_2x2(h_conv1)

    # second layer
    h_conv2 = tf.nn.relu(self.conv2d(h_pool1, self.W_conv2) + self.b_conv2)
    h_pool2 = self.max_pool_2x2(h_conv2)

    # fully connected layer
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, self.W_fc1) + self.b_fc1)

    # dropout
    self.keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, self.keep_prob)

    # readout
    self.model = tf.matmul(h_fc1_drop, self.W_fc2) + self.b_fc2

  def create(self):
    self.W_conv1 = self.weight_variable([5, 5, 1, 32])
    self.b_conv1 = self.bias_variable([32])
    self.W_conv2 = self.weight_variable([5, 5, 32, 64])
    self.b_conv2 = self.bias_variable([64])
    self.W_fc1 = self.weight_variable([7 * 7 * 64, 1024])
    self.b_fc1 = self.bias_variable([1024])
    self.W_fc2 = self.weight_variable([1024, self.numClasses])
    self.b_fc2 = self.bias_variable([self.numClasses])

    self.defineLayers()

  def load(self, filename):
    # load saved session
    saver = tf.train.Saver()
    saver.restore(self.session, filename)

    self.defineLayers()

    # accuracy
    correct_prediction = tf.equal(tf.argmax(self.model, 1), tf.argmax(self.y_, 1))
    self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  def train(self, data, numIterations, filename):
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.model, self.y_))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(self.model, 1), tf.argmax(self.y_, 1))
    self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    self.session.run(tf.global_variables_initializer())

    for i in range(numIterations):
      batch = data.train.next_batch(50)

      if i%100 == 0:
        train_accuracy = self.accuracy.eval(feed_dict={self.x:batch[0], self.y_: batch[1], self.keep_prob: 1.0})
        print("step %d, training accuracy %g"%(i, train_accuracy))

      train_step.run(feed_dict={self.x: batch[0], self.y_: batch[1], self.keep_prob: 0.5})

    # save the trained net
    saver = tf.train.Saver([self.W_conv1, self.b_conv1, self.W_conv2, self.b_conv2, self.W_fc1, self.b_fc1, self.W_fc2, self.b_fc2])
    saver.save(self.session, filename)

  def eval(self, xTest, yTest):
    return self.accuracy.eval(feed_dict={self.x: xTest, self.y_: yTest, self.keep_prob: 1.0})

  def classify(self, image):
    return self.session.run(tf.argmax(self.model, 1), feed_dict={self.x: [image], self.keep_prob: 1.0})


from Gutzwiller import GutzwillerSolver
from Explorer2D import Explorer2D
import h5py
import numpy as np

np.set_printoptions(precision=4, linewidth=200, suppress=True)

def get_dos(filename=None):
    if filename == None:
        W = 1
        n_points = 1000
        de = W / n_points
        energies = [-W / 2 + (n + 0.5) * de for n in range(n_points)]
        weights = [1 / W * de] * n_points
        return energies, weights
    else:
        dos = np.load('dos/' + filename)
        return dos[0].tolist(), dos[1].tolist()

def hf_lambda_and_eta():
    lam = [1] * 12 + [0] * 6
    eta = [0] * 5
    return lam, eta

solver = GutzwillerSolver()

# Set the accuracy and other parameters of the solver

solver.set_solver_params(ga_error=1e-5, ga_x = 0.5, rho_error=1e-7, rho_x=0.5, lambda_error=1e-7, com_error=1e-7, n_error=0.0001)

# Set the density of states from file or a constant one by default

energies, weights = get_dos()
solver.set_dos(energies, weights)

# Set the model parameters

n = 3.25
U = 1.0
J = 0.5
e_f = -0.5
V = 0.5
T = 0.00001

params = dict(n=n, e_f=e_f, V=V, U=U, U_p=(U - 2 * J), J=J, J_c=J, T=T)

for name, value in params.items():
    solver.set_model_param(value, name)

# Choose a phase

phase = 'FMSC'

# Set the initial values for rho, lambda eta and mu

rho_init =  [ 0.8,  0.2 , 0. , 0.05, -0.4, -0.3,  0,  -0.05]
lambda_init, eta_init =  hf_lambda_and_eta()
mu_init = -0.35

solver.set_rho(rho_init)
solver.set_model_param(mu_init, 'mu')
solver.set_lambda(lambda_init)
solver.set_eta(eta_init)

# First, find Hartree-Fock solution

solver.set_phase(phase, hartree_fock=True)

solver.solve(mode='rho')

print('Rho:    ', repr(np.array(solver.get_rho())))
print('Mu:     ', round(solver.get_model_param('mu'), 4))
print('Lambda: ', repr(np.array(solver.get_lambda())))
print('Eta:    ', repr(np.array(solver.get_eta())))
print('Energy:    ', round(solver.get_energy('full'), 4))

# Then, find the solution of Gutzwiller approximation

solver.set_phase(phase, hartree_fock=False)

solver.solve(mode='full')

print('Rho:    ', repr(np.array(solver.get_rho())))
print('Mu:     ', round(solver.get_model_param('mu'), 4))
print('Lambda: ', repr(np.array(solver.get_lambda())))
print('Eta:    ', repr(np.array(solver.get_eta())))
print('Energy:    ', round(solver.get_energy('full'), 4))

# Make sure it corresponds to energy minimum - all the minors listed below should be negative

print(solver.lambda_hessian_minors())

# Write the result to the file

meta = 'funny_name'

with h5py.File('DATA/' + Explorer2D.filename(phase, params, [], meta=meta, hf=False), 'w') as file:
    Explorer2D.save_solution(solver, file)


import math

#int |Boolean
#Constraint: True if Boolean is a power of two, else false.
#Strat1: Take log base 2, True if whole number, else False.

class Solution:
  def isPowerOfTwo(self, n):
    if (math.log(n, 2.0) % 1) == 0:
      return True
    else:
      return False

test = Solution()
print(test.isPowerOfTwo(16))


from editor.character import file_utils
from editor.character.character_info import CharacterInfo
from editor.character.kingdom_info import KingdomInfo

class PartyInfo:
    # pylint: disable=too-many-instance-attributes
    def __init__(self, path):
        self._temp_path = path
        self._party_json_name = 'party.json'
        self._player_json_name = 'player.json'
        self._header_json_name = 'header.json'
        self._party = file_utils.load_json(path, self._party_json_name)
        self._main = file_utils.load_json(path, self._player_json_name)
        self.main_character = CharacterInfo(self._party,
                                            self._main['m_MainCharacter'])
        self.kingdom = KingdomInfo(self._main)

    def money(self):
        return str(self._main['Money'])

    def update_money(self, money):
        if self._main['Money'] != int(money):
            self._main['Money'] = int(money)

    def save(self):
        header = file_utils.load_json(self._temp_path, self._header_json_name)
        header['Name'] = 'Edited - ' + header['Name']
        file_utils.save_json(self._temp_path,
                             self._player_json_name,
                             self._main)
        file_utils.save_json(self._temp_path,
                             self._party_json_name,
                             self._party)
        file_utils.save_json(self._temp_path,
                             self._header_json_name,
                             header)


import os
from unittest.mock import call, patch

import subscription_manager

TEST_WORKER_ARN = 'test-worker-arn'

def test_lambda_handler(tables):
    items = [
        {'subscription_id': 'sub1', 'enabled': True},
        {'subscription_id': 'sub2', 'enabled': False},
        {'subscription_id': 'sub3', 'enabled': True},
        {'subscription_id': 'sub4', 'enabled': True},
    ]
    for item in items:
        tables.subscriptions_table.put_item(Item=item)

    with patch('subscription_manager.invoke_worker') as mock_invoke_worker, \
            patch.dict(os.environ, {'SUBSCRIPTION_WORKER_ARN': TEST_WORKER_ARN}):
        mock_invoke_worker.return_value = {'StatusCode': None}

        subscription_manager.lambda_handler(None, None)

        assert mock_invoke_worker.mock_calls == [
            call(TEST_WORKER_ARN, items[0]),
            call(TEST_WORKER_ARN, items[2]),
            call(TEST_WORKER_ARN, items[3]),
        ]


__author__ = 'KoicsD'

def checkio(num1, num2):
    str1 = bin(num1)[2:]
    str2 = bin(num2)[2:]
    len1 = len(str1)
    len2 = len(str2)
    diff = len2 - len1
    if diff > 0:  # 2nd longer
        str1 = '0' * diff + str1
    elif diff < 0:  # 1st longer
        str2 = '0' * -diff + str2
    count = 0
    for i in range(len(str1)):
        if str1[i] != str2[i]:
            count += 1
    return count

"""
def checkio(num1, num2):
    return bin(num1 ^ num2)[2:].count('1')
"""

if __name__ == "__main__":
    assert checkio(117, 17) == 3, "1st example"
    assert checkio(1, 2) == 2, "2nd example"
    assert checkio(16, 15) == 5, "3rd example"


from models import *
from agents import *
import matplotlib.pyplot as plt
import numpy as np

model = PowerGridModel(10, 10)
for i in range(100):
    model.schedule.step()

print(model.schedule.agents[1].pos)

key = True

agent_counts = np.zeros((model.grid.width, model.grid.height))

for cell in model.grid.coord_iter():
    cell_content, x, y = cell
    agent_count = list(cell_content)[0].energy
    agent_counts[x, y] = agent_count
plt.imshow(agent_counts, interpolation='nearest')
plt.colorbar()
plt.show()

print(sum(sum(agent_counts)))


"""
Michael S. Emanuel
Sat Apr 21 22:14:03 2018

Ordered fractions
Problem 71

Consider the fraction, n/d, where n and d are positive integers.
If n<d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7,
3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that 2/5 is the fraction immediately to the left of 3/7.

By listing the set of reduced proper fractions for d ≤ 1,000,000 in ascending order of size,
find the numerator of the fraction immediately to the left of 3/7.

Solution Outline:
The fraction p/q that is immediately to the left of 3/7 will minimize the distance
eps = 3/7 - p/q
Simplifying this expression,
eps = (3q -7p) / 7q
This will be minimized when the numerator is 1, i.e. p can be found s/t 3q - 7p = 1
1,000,000 is congruent to 1 (7), so the best denominator is 3 lower, i.e.
q = 999,997.

"""

from fractions import Fraction
from Euler.Utility import range_inc
from Euler.ContinuedFraction import fracToLeft

def solveBruteForce(alpha: Fraction, qMax: int) -> Fraction:
    """Find the closest fraction to the left of alpha with denominator <= qMax"""
    # Create list of candidates
    cf = [fracToLeft(alpha, q) for q in range_inc(qMax)]
    # Return the largest candidate fraction
    return max(cf)

def main() -> int:
    # This is the fraction we want to get to the left of
    alpha: Fraction = Fraction(3, 7)
    # Maximum denominator
    qMax: int = 10**6
    f: Fraction = solveBruteForce(alpha, qMax)
    p: int = f.numerator
    print(f'The answer is p = {p}.  Nearest fraction is {f}.')
    return p

if __name__ == '__main__':
    main()


from .transaction import Transaction
from .stock import Stock
from .base import Base
from .position import Position

class Account(Base):
    def __init__(self, account_id):
        self.account_id = account_id
        self.balance = 0
        self.positions = {}

    def update_balance(self, amount):
        self.balance += amount

    def execute_transaction(self, action, ticker, quantity):
        new_stock = Stock(ticker)
        current_price = new_stock.get_current_price()
        transaction_value = current_price * quantity

        new_transaction = Transaction(action, ticker, current_price, quantity)

        if action == 'BUY':
            if self.balance >= transaction_value:
                self.update_balance(-transaction_value)
                if self.check_portfolio(ticker):
                    self.positions[ticker].update_position(quantity, new_transaction.transaction_id)
                else:
                    new_position = Position(ticker, quantity, new_transaction.transaction_id)
                    self.positions[ticker] = new_position
            else:
                print("insufficient funds to complete transaction")

        elif action == 'SELL':
            # verify that stock exists in portfolio
            if self.check_portfolio(ticker):
                if new_transaction.quantity <= self.positions[ticker].quantity:
                    self.update_balance(transaction_value)
                    self.positions[ticker].update_position(-quantity, new_transaction.transaction_id)
                else:
                    print('trying to sell more shares than you own')
            else:
                print('trying to sell stock you do not own')

        print('---> transaction executed:')
        new_transaction.get_details()

        self.clean_positions()

    def check_portfolio(self, ticker):
        if ticker in self.positions.keys():
            return True
        return False

    def fund_account(self, amount):
        self.update_balance(amount)
        print('added {} to account, new balance: {}'.format(amount, self.balance))

    def view_portfolio(self):
        if len(self.positions) > 0:
            print('---Portfolio Details---')
            for ticker, position in self.positions.items():
                position.get_details()
        else:
            print('no positions to display')
        print('total investable cash: {}'.format(self.balance))

    def clean_positions(self):
        print('---> cleaning portfolio')
        new_positions = {ticker: position for (ticker, position) in self.positions.items() if self.positions[ticker].quantity > 0}
        self.positions = new_positions


#!/usr/bin/env/python3
# Author: David Jonietz
# Copyright 2019 Institute of Advanced Research in Artificial Intelligence (IARAI) GmbH.
# IARAI licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Simple seq2seq baseline in keras with convLSTM layers for NeurIPS 2019 Traffic4Cast competition.
NOTE: ALTERNATIVE TO BASELINE_SEQ2SEQ.PY, WHERE THE SAME MODEL IS TRAINED ONLY FOR THE PREDICTION
TIMES AS DEFINED IN THE TEST DATA SETS.

This script serves for training a baseline model with Traffic4Cast data for one city.
It includes methods for:
    - loading and preprocessing the training data for one day
    - building the model in keras
    - training the model and saving its parameters

**Summary**

*Model Structure*

- the expected inputs are of shape (None, 3, 3, 495, 436), referring to [t-3:t] time bin
    arrays previous to the prediction time t with 3 channels, 495 rows and 436 columns
- a two layer convLSTM serves as encoder, of which the memory state of the final cell are
    repeated three times (corresponding to the prediction horizon of 5, 10, and 15 minutes).
- a two layer convLSTM decoder produces the final predictions in the shape of (None, 3, 3, 495, 436).

*Data Loading and Preprocessing*

- the hdf5 file names in the training data directory are listed and filtered for used
    defined excluded dates (e.g., holidays, not used here)
- from the data, only the intervals [t-3 : t] before each t in indices are extracted, leading to
    5 samples of 6 time bins each (3 as input, 3 as ground truth)
- it is then transposed to the expected input shape of the model, and rescaled to 0-1.

*Model Training*

- the model is built and compiled with MSE loss and the ADAM optimizer (initial learning rate of 0.0001)
- each training epoch, the data for one day is loaded and preprocessed, and the model is
    trained using a batch size of 1. This process is repeated for all days before the next epoch begins.
- the loss is logged after each iteration and the model parameters are saved after each epoch.

"""

import numpy as np
import h5py
import os, csv, re, datetime
import sys, getopt
from keras import models
from keras import layers
from keras import optimizers
from keras import losses
from keras import backend as K
from tensorflow import device

#set indices (correspond to prediction times in test sets)
utcPlus2 = [30, 69, 126, 186, 234]
utcPlus3 = [57, 114, 174, 222, 258]
#Data Loader Functions

def load_data(file_path, indices):
    """Load data for one test day, return as numpy array with normalized samples of each
        6 time steps in random order.

        Args.:
            file_path (str): file path of h5 file for one day
            indices (list): list with prediction times (as list indices in the interval [0, 288])

        Returns: numpy array of shape (5, 6, 3, 495, 436)
    """
    #load h5 file
    fr = h5py.File(file_path, 'r')
    a_group_key = list(fr.keys())[0]
    data = list(fr[a_group_key])

    #identify test cases and split in samples of each length 3 time bins
    data = [data[y - 3 : y + 3] for y in indices]
    data = np.stack(data, axis=0)

    #transpose to (samples, timesteps, channels, rows, columns)
    data = np.transpose(data, (0, 1, 4, 2, 3))

    #rescale and return data
    data = data.astype(np.float32)
    np.random.shuffle(data)
    data /= 255.
    return data

def list_filenames(directory, excluded_dates):
    """Auxilliary function which returns list of file names in directory in random order,
        filtered by excluded dates.

        Args.:
            directory (str): path to directory
            excluded_dates (list): list of dates which should not be included in result list,
                e.g., ['2018-01-01', '2018-12-31']

        Returns: list
    """
    filenames = os.listdir(directory)
    np.random.shuffle(filenames)

    #check if in excluded dates
    excluded_dates = [datetime.datetime.strptime(x, '%Y-%m-%d').date() for x in excluded_dates]
    filenames = [x for x in filenames if return_date(x) not in excluded_dates]
    return filenames

def return_date(file_name):
    """Auxilliary function which returns datetime object from Traffic4Cast filename.

        Args.:
            file_name (str): file name, e.g., '20180516_100m_bins.h5'

        Returns: date string, e.g., '2018-05-16'
    """

    match = re.search(r'\d{4}\d{2}\d{2}', file_name)
    date = datetime.datetime.strptime(match.group(), '%Y%m%d').date()
    return date

#Model Definition

def build_model():
    """Build keras model.

        Returns: keras model
    """

    #define model input
    data_shape = (3, 3, 495, 436)
    prev_frames = layers.Input(shape=data_shape, name='prev_frames')

    #define layers
    with device('/device:GPU:0'):
        convlstm_0 = layers.ConvLSTM2D(filters=32, kernel_size=(7, 7), padding='same', return_sequences=True, return_state=False,
                             activation='tanh', recurrent_activation='hard_sigmoid',
                             kernel_initializer='glorot_uniform', unit_forget_bias=True,
                             data_format='channels_first', name='convlstm_0')

        convlstm_1 = layers.ConvLSTM2D(filters=64, kernel_size=(7, 7), padding='same', return_sequences=False, return_state=True,
                             activation='tanh', recurrent_activation='hard_sigmoid',
                             kernel_initializer='glorot_uniform', unit_forget_bias=True,
                             data_format='channels_first', name='convlstm_1')

    with device('/device:GPU:1'):
        convlstm_2 = layers.ConvLSTM2D(filters=64, kernel_size=(7, 7), padding='same', return_sequences=True, return_state=False,
                             activation='tanh', recurrent_activation='hard_sigmoid',
                             kernel_initializer='glorot_uniform', unit_forget_bias=True,
                             data_format='channels_first', name='convlstm_2')

        convlstm_3 = layers.ConvLSTM2D(filters=3, kernel_size=(7, 7), padding='same', return_sequences=True, return_state=False,
                             activation='tanh', recurrent_activation='hard_sigmoid',
                             kernel_initializer='glorot_uniform', unit_forget_bias=True,
                             data_format='channels_first', name='convlstm_3')

    #define model structure
    #encoder
    x = convlstm_0(prev_frames)
    x = convlstm_1(x)[-1]

    #flatten, repeat and reshape
    x = layers.Flatten()(x)
    x = layers.RepeatVector(3)(x)
    x = layers.Reshape((3, 64, 495, 436))(x)

    #decoder
    x = convlstm_2(x)
    x = convlstm_3(x)

    #build and return model
    seq_model = models.Model(inputs=prev_frames, outputs=x)
    return seq_model

#Training Procedure

def model_train(data_dir, model_dir, log_path, indices, excl_dates=[]):
    """Build model, load data and train. Save model after each training epoch.

        Args.:
            data_dir (str): path to directory with training data
            model_dir (str): path to directory to store model
            log_path (str): path to log file
            indices (list): list with prediction times (as list indices in the interval [0, 288])
            excluded_dates (list): list of dates which should not be included in result list, e.g.,
                ['2018-01-01', '2018-12-31']

        Returns: None
    """
    #generate log file
    log_file = open(log_path, 'w', newline='' )
    log_writer = csv.writer(log_file)
    log_writer.writerow(['epoch', 'file', 'loss'])

    #build and compile model
    model = build_model()
    optimizer = optimizers.Adam(lr=0.0001)
    model.compile(optimizer=optimizer, loss=losses.mean_squared_error)

    #receive filenames of training days
    file_names = list_filenames(data_dir, excl_dates)

    #train model for each training data set
    epochs = 50
    for e in range(epochs):
        for f in file_names:
            data = load_data(data_dir + f, indices)

            #define first 3 time bins as input, last 3 as labels
            x = data[:, :3]
            y = data[:, 3:]

            #train for mini-batches
            with device('/device:GPU:3'):
                hist = model.fit(x, y, epochs=1, batch_size=3)
                loss = hist.history["loss"]

            #log loss
            log_writer.writerow([e, f, loss[0]])
            log_file.flush()

        #save model
        model.save(model_dir + r'model_ep_{}.h5'.format(e))

#Run Model
if __name__ == '__main__':

    data_dir = ''
    model_dir = ''
    log_path = ''
    city = ''
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hd:m:l:c:", ["data_dir","model_dir","log_path","city"])
    except getopt.GetoptError:
        print('usage: baseline_seq2seq_foc_train.py -d <data_dir> -m <model_dir> -l <log_path> -c <city>')
        sys.exit(2)
    for opt,arg in opts:
        if opt == '-h':
            print('usage: baseline_seq2seq_foc_train.py -d <data_dir> -m <model_dir> -l <log_path> -c <city>')
        elif opt in ("-d","--data_dir"):
            data_dir = arg
        elif opt in ("-m","--model_dir"):
            model_dir = arg
        elif opt in ("-l","--log_path"):
            log_path = arg
        elif opt in ("-c","--city"):
            city = arg
    if city in ("Berlin","Istanbul","Moscow"):
        training_file_dir = os.path.join(data_dir, city, city+"_test")
        training_indices = utcPlus3
        if city == "Berlin":
            training_indices = utcPlus2
        model_train(training_file_dir, model_dir, log_path, training_indices)
    else:
        print('invalid city provided')
        sys.exit(2)


# coding=utf-8
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
                        unicode_literals, with_statement)

import functools
import unittest

from pants.engine.nodes import Return

_WAITING = 'TODO: Waiting'

@unittest.skip('Skipped to expedite landing #3821; see: #4027.')
class GraphTest(unittest.TestCase):

  def setUp(self):
    super(GraphTest, self).setUp()
    self.pg = 'TODO: These tests need to be ported to native tests.'

  @classmethod
  def _mk_chain(cls, graph, sequence, states=[_WAITING, Return]):
    """Create a chain of dependencies (e.g. 'A'->'B'->'C'->'D') in the graph from a sequence."""
    for state in states:
      dest = None
      for src in reversed(sequence):
        if state is _WAITING:
          graph.add_dependencies(src, [dest] if dest else [])
        else:
          graph.complete_node(src, state([dest]))
        dest = src
    return sequence

  def test_disallow_completed_state_change(self):
    self.pg.complete_node('A', Return('done!'))
    with self.assertRaises('TODO: CompletedNodeException: These tests should be ported to native tests.'):
      self.pg.add_dependencies('A', ['B'])

  def test_disallow_completing_with_incomplete_deps(self):
    self.pg.add_dependencies('A', ['B'])
    self.pg.add_dependencies('B', ['C'])
    with self.assertRaises('TODO: IncompleteDependencyException: These tests should be ported to native tests.'):
      self.pg.complete_node('A', Return('done!'))

  def test_dependency_edges(self):
    self.pg.add_dependencies('A', ['B', 'C'])
    self.assertEquals({'B', 'C'}, set(self.pg.dependencies_of('A')))
    self.assertEquals({'A'}, set(self.pg.dependents_of('B')))
    self.assertEquals({'A'}, set(self.pg.dependents_of('C')))

  def test_cycle_simple(self):
    self.pg.add_dependencies('A', ['B'])
    self.pg.add_dependencies('B', ['A'])
    # NB: Order matters: the second insertion is the one tracked as a cycle.
    self.assertEquals({'B'}, set(self.pg.dependencies_of('A')))
    self.assertEquals(set(), set(self.pg.dependencies_of('B')))
    self.assertEquals(set(), set(self.pg.cyclic_dependencies_of('A')))
    self.assertEquals({'A'}, set(self.pg.cyclic_dependencies_of('B')))

  def test_cycle_indirect(self):
    self.pg.add_dependencies('A', ['B'])
    self.pg.add_dependencies('B', ['C'])
    self.pg.add_dependencies('C', ['A'])

    self.assertEquals({'B'}, set(self.pg.dependencies_of('A')))
    self.assertEquals({'C'}, set(self.pg.dependencies_of('B')))
    self.assertEquals(set(), set(self.pg.dependencies_of('C')))
    self.assertEquals(set(), set(self.pg.cyclic_dependencies_of('A')))
    self.assertEquals(set(), set(self.pg.cyclic_dependencies_of('B')))
    self.assertEquals({'A'}, set(self.pg.cyclic_dependencies_of('C')))

  def test_cycle_long(self):
    # Creating a long chain is allowed.
    nodes = list(range(0, 100))
    self._mk_chain(self.pg, nodes, states=(_WAITING,))
    walked_nodes = [node for node, _ in self.pg.walk([nodes[0]])]
    self.assertEquals(nodes, walked_nodes)

    # Closing the chain is not.
    begin, end = nodes[0], nodes[-1]
    self.pg.add_dependencies(end, [begin])
    self.assertEquals(set(), set(self.pg.dependencies_of(end)))
    self.assertEquals({begin}, set(self.pg.cyclic_dependencies_of(end)))

  def test_walk(self):
    nodes = list('ABCDEF')
    self._mk_chain(self.pg, nodes)
    walked_nodes = list((node for node, _ in self.pg.walk(nodes[0])))
    self.assertEquals(nodes, walked_nodes)

  def test_invalidate_all(self):
    chain_list = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
    invalidators = (
      self.pg.invalidate,
      functools.partial(self.pg.invalidate, lambda node, _: node == 'Z')
    )

    for invalidator in invalidators:
      self._mk_chain(self.pg, chain_list)

      self.assertTrue(self.pg.completed_nodes())
      self.assertTrue(self.pg.dependents())
      self.assertTrue(self.pg.dependencies())
      self.assertTrue(self.pg.cyclic_dependencies())

      invalidator()

      self.assertFalse(self.pg._nodes)

  def test_invalidate_partial(self):
    comparison_pg = 'TODO: These tests need to be ported to native tests.'
    chain_a = list('ABCDEF')
    chain_b = list('GHIJKL')

    # Add two dependency chains to the primary graph.
    self._mk_chain(self.pg, chain_a)
    self._mk_chain(self.pg, chain_b)

    # Add only the dependency chain we won't invalidate to the comparison graph.
    self._mk_chain(comparison_pg, chain_b)

    # Invalidate one of the chains in the primary graph from the right-most node.
    self.pg.invalidate(lambda node, _: node == chain_a[-1])

    # Ensure the final structure of the primary graph matches the comparison graph.
    pg_structure = {n: e.structure() for n, e in self.pg._nodes.items()}
    comparison_structure = {n: e.structure() for n, e in comparison_pg._nodes.items()}
    self.assertEquals(pg_structure, comparison_structure)

  def test_invalidate_count(self):
    self._mk_chain(self.pg, list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))
    invalidated_count = self.pg.invalidate(lambda node, _: node == 'I')
    self.assertEquals(invalidated_count, 9)

  def test_invalidate_partial_identity_check(self):
    # Create a graph with a chain from A..Z.
    chain = self._mk_chain(self.pg, list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))
    self.assertTrue(list(self.pg.completed_nodes()))

    # Track the pre-invaliation nodes (from A..Q).
    index_of_q = chain.index('Q')
    before_nodes = [node for node, _ in self.pg.completed_nodes() if node in chain[:index_of_q + 1]]
    self.assertTrue(before_nodes)

    # Invalidate all nodes under Q.
    self.pg.invalidate(lambda node, _: node == chain[index_of_q])
    self.assertTrue(list(self.pg.completed_nodes()))

    # Check that the root node and all fs nodes were removed via a identity checks.
    for node, entry in self.pg._nodes.items():
      self.assertFalse(node in before_nodes, 'node:\n{}\nwasnt properly removed'.format(node))

      for associated in (entry.dependencies, entry.dependents, entry.cyclic_dependencies):
        for associated_entry in associated:
          self.assertFalse(
            associated_entry.node in before_nodes,
            'node:\n{}\nis still associated with:\n{}\nin {}'.format(node, associated_entry.node, entry)
          )


import argparse
import os
import pickle
from skimage import io
import tqdm

from utils import get_refset_dirs, get_image_files

#
# Have to do this because cv2.imread only prints a warning
# for bad images instead of throwing an exception. So to make sure we don't
# have any in our referenceset we check first using scikit which does throw an
# exception.
#
def verify_images(images_dir, work_dir):
    good_cache_file = os.path.join(work_dir, "verified_images.p")
    if os.path.exists(good_cache_file):
        with open(good_cache_file, "rb") as file:
            good_images = pickle.load(file)
    else:
        good_images = []

    bad_images = []
    #
    # Create new_good_images in case some images don't exist anymore we don't
    # want them hanging out in the cache forever.
    #
    new_good_images = []
    for filepath in tqdm.tqdm(get_image_files(images_dir)):
        if filepath in good_images:
            new_good_images.append(filepath)
        else:
            try:
                img = io.imread(os.path.join(images_dir, filepath))
                new_good_images.append(filepath)
            except Exception as ex:
                bad_images.append((filepath, ex))
    if not os.path.exists(work_dir):
        print("Creating work directory [{}]".format(work_dir))
        os.mkdir(work_dir)
    with open(good_cache_file, "wb") as file:
        pickle.dump(new_good_images, file)

    return bad_images

def is_hw_image(image):
    dirname = os.path.basename(os.path.dirname(image))
    if dirname.startswith("hw_"):
        basename = os.path.basename(image)
        parts = basename.split(".")[0].split("-")
        # hw_5823/008a4557-ac79-4232-a20d-f2a85bea65af.jpg
        return len(parts) == 5 and len(parts[0]) == 8 and len(parts[1]) == 4 \
            and len(parts[2]) == 4 and len(parts[3]) == 4 \
            and len(parts[4]) == 12
    return False

if __name__ == '__main__':
    import warnings
    warnings.filterwarnings("ignore")

    parser = argparse.ArgumentParser(description="init_refset")
    parser.add_argument('--refset', dest="refset", type=str)
    parser.add_argument('--image', dest="image", type=str)
    args = parser.parse_args()

    if args.refset is not None:
        images_dir, work_dir = get_refset_dirs(args.refset)

        bad_images = verify_images(images_dir, work_dir)
        if len(bad_images) > 0:
            for image in bad_images:
                print(
                    "Image [{}] is bad due to [{}]".format(image[0], image[1])
                )
            print("\n\nRun the following to fix them up...\n\n")
            for image in bad_images:
                filepath = os.path.join(images_dir, image[0])
                print("convert {} {}".format(filepath, filepath))
            print("\n\nAnd in happywhale source also run...\n\n")
            for image in bad_images:
                filepath = image[0]
                if is_hw_image(filepath):
                    file = os.path.basename(filepath)
                    mediaasset = os.path.join("/mnt/data/media", file[0],
                                              file[1], file[2], file[3],
                                              file[4], file[5], file)
                    print("convert {} {}".format(mediaasset, mediaasset))
                else:
                    external = os.path.join(
                        "/mnt/data/media/idrefsets/humpbacks", filepath
                    )
                    print("convert {} {}".format(external, external))
    else:
        io.imread(args.image)
        print(is_hw_image(args.image))
        print("Image seems fine")


from flask_restful import Resource, abort
from flask import render_template, make_response, request

import config
from controller import experiment_store as es
from controller import worker_store as ws
from controller import experiment_summary_store as ess
from controller import assemble_task_store as ats
from controller import learn_task_store as lts
from controller import task_store as ts
from controller.task_scheduler import scheduler
from controller import strategy_config_store as scs
from model import AssembleTask, LearnTask, Task
from model.experiment_summary import ExperimentSummary
from model.experiment_summary_task import ExperimentSummaryTask

class Manage(Resource):
    def post(self, hash):
        try:
            experiment = es.get_experiment(hash)
            if request.json['command'] == 'generate_tasks':
                success = self.generate_tasks(experiment, hash)
                return make_response("generate_tasks_success" if success else "generate_tasks_need_assembling", 200)

            elif request.json['command'] == 'run_all':
                success = self.run_all_tasks(experiment)
                if success:
                    scheduler.add_experiment(hash, experiment.priority)

                return make_response("run_success" if success else "run_failed", 200)

            elif request.json['command'] == 'run_task':
                success = self.run_task(request.json['id'])
                if success:
                    scheduler.add_experiment(hash, experiment.priority)

                return make_response("run_success" if success else "run_failed", 200)

            elif request.json['command'] == 'rerun_all':
                success = self.rerun_all_tasks(experiment, hash)
                if success:
                    scheduler.add_experiment(hash, experiment.priority)

                return make_response("rerun_success" if success else "rerun_failed", 200)

            elif request.json['command'] == 'rerun_task':
                success = self.rerun_task(hash, request.json['id'])
                if success:
                    scheduler.add_experiment(hash, experiment.priority)

                return make_response("rerun_success" if success else "rerun_failed", 200)

            elif request.json['command'] == 'stop_all':
                success = self.stop_all_tasks(experiment)
                if success:
                    scheduler.remove_experiment(hash)

                return make_response("stop_success" if success else "stop_failed", 200)

            elif request.json['command'] == 'stop_task':
                success = self.stop_task(request.json['id'])
                scheduler.check_experiment(hash, experiment.priority)
                return make_response("stop_success" if success else "stop_failed", 200)

            elif request.json['command'] == 'reorder_task':
                success = self.reorder_task(request.json['id'], request.json['direction'] == "up")
                return make_response("reorder_success" if success else "reorder_failed", 200)

            elif request.json['command'] == 'delete_experiment':
                success = self.stop_all_tasks(experiment)
                if success:
                    scheduler.remove_experiment(hash)

                success = success and es.delete_experiment(hash)
                success = success and ts.delete_tasks_by_experiment(hash)
                if ess.get_experiment_summary(hash):
                    success = success and ess.delete_experiment_summary(hash)

                return make_response(f"delete_success" if success else f"delete_failed", 200)

            else:
                abort(400, message="Unknown command.")

        except Exception as e:
            if config.debug_mode:
                abort(400, message=str(e))

            else:
                abort(404, message="Page not found")

    def init_summary(self, exp_id, exp, generated_tasks):
        summary_tasks = {t_id: self.create_summary_task(a_task, l_task, t.created_ts) for t_id, t, a_task, l_task in generated_tasks}
        success = ess.new_experiment_summary(
            exp_id,
            ExperimentSummary(exp.name, exp.markdown, summary_tasks, exp.created_ts)
        )
        return bool(success)

    @staticmethod
    def create_summary_task(a_task, l_task, created_ts):
        a_conf = (l_task or a_task).assemble_config
        l_conf = None if not l_task else l_task.learn_config
        exp_sum = ExperimentSummaryTask(a_conf, l_conf, created_ts)
        if a_task.is_completed():
            exp_sum.add_assemble_result(a_task.result_file_path)

        if l_task and l_task.is_completed():
            exp_sum.add_learn_result(l_task.result)

        if a_conf['strategy_id'] == "manual_file_input":
            exp_sum.add_assemble_result(a_conf['strategy_parameters']['file_path'])

        return exp_sum

    def get(self, hash):
        try:
            experiment = es.get_experiment(hash)
            return make_response(
                render_template(
                    'experiment/manage.j2',
                    hash=hash,
                    experiment=self.load_experiment(experiment)
                ),
                200,
                {'Content-Type': 'text/html'}
            )

        except Exception as e:
            if config.debug_mode:
                abort(400, message=str(e))

            else:
                abort(404, message="Page not found")

    def load_experiment(self, exp):
        tasks = sorted([self.load_task_from_db(t_id) for t_id in exp.tasks], key=lambda t: t["order_in_exp"])
        learn_configs = [(c_id, scs.get_config_by_id(c_id)) for c_id in exp.learn_configs]
        runnable_tasks = [t for t in tasks if t['state'] == "generated"]
        stoppable_tasks = [t for t in tasks if t['state'] == "runnable" or t['state'] == "running"]
        result = {
            "name": exp.name,
            "markdown": exp.markdown,
            "state": exp.state,
            "assemble_configs": [(c_id, scs.get_config_by_id(c_id)) for c_id in exp.assemble_configs],
            "learn_configs": learn_configs,
            "tasks": tasks,
            "show_summary": learn_configs and [t for t in tasks if t['state'] == "completed"],
            "has_runnable_tasks": bool(runnable_tasks),
            "has_stoppable_tasks": bool(stoppable_tasks),
            "all_tasks_completed": not runnable_tasks and not stoppable_tasks,
        }
        return result

    @staticmethod
    def load_task_from_db(task_id):
        task = ts.get_task_by_id(task_id)
        result = {
            "id": task_id,
            "state": task.state,
            "order_in_exp": task.order_in_exp,
            "status_info": "Task Completed" if task.state == "completed" else "Run task",
        }
        assemble_task = ats.get_task_by_id(task.assemble_task_id)
        if task.learn_task_id:
            learn_task = lts.get_task_by_id(task.learn_task_id)
            result["learn_config"] = learn_task.learn_config
            if task.state == "runnable" or task.state == "running":
                if learn_task.state == "runnable":
                    result["status_info"] = "Learning is waiting for worker"

                elif learn_task.state == "running":
                    result["status_info"] = "Learning is in progress"

        result["assemble_config"] = assemble_task.assemble_config
        if task.state == "runnable" or task.state == "running":
            if assemble_task.state == "runnable":
                result["status_info"] = "Assembling is waiting for worker"

            elif assemble_task.state == "running":
                result["status_info"] = "Assembling is in progress"

        return result

    def rerun_task(self, exp_id, task_id):
        exp_sum = ess.get_experiment_summary(exp_id)
        success, task, a_task, l_task = self._rerun_task(task_id)
        if success:
            sum_changes = exp_sum.update_task(task_id, self.create_summary_task(a_task, l_task, task.created_ts))
            success = ess.update_experiment_summary(sum_changes, exp_id)

        return success

    def rerun_all_tasks(self, exp, exp_id):
        exp_sum = ess.get_experiment_summary(exp_id)
        sum_changes = None
        success = True
        for t_id in exp.tasks:
            t_success, task, a_task, l_task = self._rerun_task(t_id)
            if t_success:
                sum_changes = exp_sum.update_task(t_id, self.create_summary_task(a_task, l_task, task.created_ts))

        if sum_changes:
            success = success and ess.update_experiment_summary(sum_changes, exp_id)

        return success

    @staticmethod
    def _rerun_task(task_id):
        task = ts.get_task_by_id(task_id)

        if task.state != "completed":
            return None, None, None, None

        new_a_task_id, new_a_task = ats.rerun_task(task.assemble_task_id)
        if not new_a_task:
            return None, None, None, None

        if task.learn_task_id:
            new_l_task_id, new_l_task = lts.rerun_task(task.learn_task_id, new_a_task_id)
            task_changes = task.re_run(new_a_task_id, new_l_task_id)
            return ts.update_task(task_changes, task_id), task, new_a_task, new_l_task

        else:
            task_changes = task.re_run(new_a_task_id, None)
            return ts.update_task(task_changes, task_id), task, new_a_task, None

    def generate_tasks(self, exp, exp_id):
        changes = exp.set_state("generating_tasks")
        success = es.update_experiment(changes, exp_id)
        try:
            generated_tasks_data = self.generate_tasks_data(exp, exp_id)
            if generated_tasks_data:
                success = success and self.init_summary(exp_id, exp, generated_tasks_data)
                changes = exp.set_state("generated_tasks")
                changes.update(exp.add_task_list([t_id for t_id, _, _, _ in generated_tasks_data]))
                if success:
                    success = success and es.update_experiment(changes, exp_id)
                    return True

        except Exception as e:
            pass

        changes = exp.set_state("configuration")
        success = es.update_experiment(changes, exp_id)
        return False

    @staticmethod
    def generate_tasks_data(exp, exp_id):
        assemble_configs = [scs.get_config_by_id(c_id) for c_id in exp.assemble_configs]
        learn_configs = [scs.get_config_by_id(c_id) for c_id in exp.learn_configs]

        generated_tasks = []

        index = 1

        for a_conf in assemble_configs:
            a_conf_dict = a_conf.to_dict()
            a_conf_dict.pop('shared_parameters', None)
            a_task = AssembleTask(a_conf_dict)
            a_task_es_id, a_task = ats.new_assemble_task(a_task)
            if a_task_es_id:
                if not learn_configs:
                    if a_task.assemble_config['strategy_id'] != "manual_file_input":
                        task = Task(exp_id, a_task_es_id, None, index)
                        index += 1
                        if a_task.is_completed():
                            task.completed(a_task_es_id)

                        task_id = ts.new_task(task)
                        if task_id:
                            generated_tasks.append((task_id, task, a_task, None))

                for l_conf in learn_configs:
                    l_task = LearnTask(a_conf.to_dict(), a_task_es_id, l_conf.to_dict())
                    l_task_es_id, l_task = lts.new_learn_task(l_task)
                    if l_task_es_id:
                        task = Task(exp_id, a_task_es_id, l_task_es_id, index)
                        index += 1
                        if l_task.is_completed():
                            task.completed(l_task_es_id)

                        task_id = ts.new_task(task)
                        if task_id:
                            generated_tasks.append((task_id, task, a_task, l_task))

        return generated_tasks

    def run_all_tasks(self, exp):
        success = True
        for t_id in exp.tasks:
            success = self.run_task(t_id) or success

        return success

    @staticmethod
    def run_task(task_id):
        task = ts.get_task_by_id(task_id)
        if not task or task.state != "generated":
            return False

        t_change = task.make_runnable()
        a_task = ats.get_task_by_id(task.assemble_task_id)
        success = True
        if a_task.is_completed():
            t_change.update(task.completed(task.assemble_task_id))

        else:
            a_task_change = a_task.make_runnable()
            success = ats.update_task(a_task_change, task.assemble_task_id)

        if success and task.learn_task_id:
            l_task = lts.get_task_by_id(task.learn_task_id)
            if l_task.is_completed():
                t_change = task.completed(task.learn_task_id)

            else:
                l_task_change = l_task.make_runnable()
                success = lts.update_task(l_task_change, task.learn_task_id)

        if success:
            success = ts.update_task(t_change, task_id)

        return success

    def stop_all_tasks(self, exp):
        success = True
        for t_id in exp.tasks:
            success = self._stop_task(t_id) or success

        return success

    def stop_task(self, task_id):
        return self._stop_task(task_id)

    def _stop_task(self, task_id):
        task = ts.get_task_by_id(task_id)

        if task.state != "running":
            return None

        task_changes = task.stop()
        success = ts.update_task(task_changes, task_id)
        self._stop_sub_task(task.assemble_task_id, ats, True)
        if task.learn_task_id:
            self._stop_sub_task(task.learn_task_id, lts, False)

        return success

    @staticmethod
    def _stop_sub_task(sub_task_id, task_store, assembling):
        sub_task = task_store.get_task_by_id(sub_task_id)
        worker_id = sub_task.assigned_to
        parents = ts.get_assembling_parents(sub_task_id) if assembling else ts.get_learning_parents(sub_task_id)
        if not parents:
            return

        sub_task_changes = sub_task.stop()
        if sub_task_changes:
            task_store.update_task(sub_task_changes, sub_task_id)
            if not sub_task.assigned_to:
                worker = ws.get_worker(worker_id)
                if worker:
                    worker_changes = worker.clear_task()
                    ws.update_worker(worker_changes, worker_id)

    @staticmethod
    def reorder_task(task_id, direction):
        try:
            task = ts.get_task_by_id(task_id)
            task_order = task.order_in_exp
            swap_order = task_order + (-1 if direction else 1)
            swap_task_id, swap_task = ts.search_task_by_dict({
                'order_in_exp': swap_order,
                'experiment_id': task.experiment_id,
            })
            task_changes = task.set_order_in_exp(swap_order)
            success = ts.update_task(task_changes, task_id)
            swap_task_changes = swap_task.set_order_in_exp(task_order)
            return ts.update_task(swap_task_changes, swap_task_id) and success

        except Exception as e:
            pass

        return False


#! /usr/bin/python3
#coding=utf-8

from urllib.parse import urlencode, quote_plus
from urllib.request import urlopen
import time
import temperature_url_get
def fetch_thing(url,params,method):

    #urlencode(params) 中文轉碼
    params = urlencode(params)
    #params = urllib.parse.urlencode(params) #此註解為python2.7的用法
    if method == ("POST"):
        f = urlopen(url,params)
    else:
        f = urlopen(url+"?"+params)
    return(f.read().decode('utf-8'),f.close)

#================================================

url = 'http://phpmyadmin-t106362512project.a3c1.starter-us-west-1.openshiftapps.com/examples/status.php'
while True:
    content,response_code = fetch_thing(url,{},'GET')
    time.sleep(3)
    stco = str(content)
    if stco.find("1") == 319: #content的第319字元
        print("1")
        temperature_url_get.RPI()
    elif stco.find("0") == 319:
        print("0")
    else:
        print(stco)


#!/usr/bin/python
# coding=utf-8
__author__ = "Chichi Weng"
__copyright__ = "Copyright 2015 Python Snippets. All rights reserved."
__license__ = "MIT"
__email__ = "949409306@qq.com"

import unittest
from constant import const

class TestCase(unittest.TestCase):
    def test_constant(self):
        self.assertEqual(const.FIRST,1)


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# 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/py3o/renderserver/server.py
# Compiled at: 2018-07-04 04:43:43
import logging
from pyf.transport.packets import Packet
from pyf.station import FlowServer
from pyf.station.utils import base64encoder
from pyf.station.utils import base64decoder
from pyf.station.utils import file_to_packets
from pyf.station.utils import packets_to_file
from pyjon.utils import get_secure_filename
import json, os, sys, optparse
from py3o.formats import Formats
from py3o.renderers.juno import Convertor
from twisted.internet.threads import deferToThread
logging.basicConfig(level=logging.DEBUG)
formats = Formats()
all_formats = formats.get_known_format_names()

class DataHandler(object):
    soffice_port = None
    soffice_host = None

    def __init__(self, flow, client):
        self.flow = flow
        self.client = client

    def render(self, header, flow, callback):
        source_format = header.get('source_format', 'odt')
        pdf_options = None
        pdf_options_str = header.get('pdf_options')
        if pdf_options_str:
            pdf_options = json.loads(pdf_options_str)
        input_filename = get_secure_filename(suffix='.%s' % source_format.lower())
        output_filename = get_secure_filename()
        try:
            packets_to_file(flow, input_filename, callback)
            format_ = formats.get_format(header.target_format.lower())
            convertor = Convertor(self.soffice_host, self.soffice_port)
            logging.info('Connecting to libreoffice %s:%s to convert document to %s', self.soffice_host, self.soffice_port, format_.odfname)
            if pdf_options:
                logging.info('PDF export options: %s', pdf_options)
            convertor.convert(input_filename, output_filename, format_.odfname, pdf_options)
        finally:
            os.unlink(input_filename)

        return output_filename

    def rendered(self, output_filename, callback):
        callback('render_status', 'ok', filename=output_filename)
        for data in base64encoder(file_to_packets(open(output_filename, 'rb'))):
            self.client.message(data)

        self.client.success('Process ended successfully')
        logging.info('Process ended successfully')
        os.unlink(output_filename)

    def renderfailed(self, err):
        self.client.error(err)
        logging.error('Error received from libreoffice: %s', err)

    def handle_data(self):
        header = self.flow.next()
        if header.action == 'render':
            callback = lambda key, value, **kwargs: self.client.message(Packet(dict(type='appinfo', key=key, value=value, **kwargs)))
            if header.target_format.lower() in all_formats:
                d = deferToThread(self.render, header, base64decoder(self.flow), callback)
                d.addCallback(self.rendered, callback)
                d.addErrback(self.renderfailed)
            else:
                callback('render_status', 'nok', client=self.client)
                raise ValueError('Unsupported format %s' % header.target_format)
        else:
            callback('render_status', 'nok', client=self.client)
            raise ValueError('Unsupported action %s' % header.action)
        logging.info('end of flow...')

def cli_server():
    optparser = optparse.OptionParser()
    optparser.add_option('-a', '--sofficehost', dest='soffice_host', help='specify the open office hostname/ip address ADDR', metavar='ADDR', default='127.0.0.1')
    optparser.add_option('-p', '--sofficeport', dest='soffice_port', help='specify the open office port PORT', metavar='PORT', default='8997')
    optparser.add_option('-l', '--listenport', dest='listen_port', help='specify the PORT on which our service will listen', metavar='PORT', default=8994)
    optparser.add_option('-i', '--listeninterface', dest='listen_interface', help='specify the INTERFACE on which our service will listen (default: all interfaces)', metavar='INTERFACE', default=None)
    optparser.add_option('-d', '--driver', dest='driver', help='choose a driver between juno & pyuno', default='juno')
    optparser.add_option('-j', '--java', dest='javalib', help='choose a jvm.dll/jvm.so to use if you are using the juno driver', default=None)
    optparser.add_option('-u', '--ure', dest='ure_base', help='choose a ure base dir, ex: /usr/lib on ubuntu/libreoffice', default=None)
    optparser.add_option('-o', '--office', dest='office_base', help='choose a office base dir, ex: /usr/lib64/libreoffice on ubuntu64/libreoffice', default=None)
    optparser.add_option('-m', '--maxmem', dest='maxmem', help='how much memory to give to the JVM if you are using juno driver, default is 150Mb', default=150)
    optparser.add_option('-t', '--office-type', dest='office_type', help='Windows only option! By default we try to find LibreOffice, but if you need to use OpenOffice, set this option to --office-type=openoffice', default='libreoffice')
    options, args = optparser.parse_args()
    start_server(options)
    return

def start_server(options):
    if os.name in ('nt', 'os2', 'ce'):
        if options.office_type == 'openoffice':
            base_ooregkey = 'SOFTWARE\\OpenOffice.org\\OpenOffice.org'
        else:
            if options.office_type == 'libreoffice':
                base_ooregkey = 'SOFTWARE\\Wow6432Node\\LibreOffice\\LibreOffice'
            else:
                raise ValueError('%s is not a valid office type' % options.office_type)
            import _winreg
            base_key = _winreg.HKEY_LOCAL_MACHINE
            try:
                key = _winreg.OpenKey(base_key, base_ooregkey)
                version = _winreg.EnumKey(key, 0)
                office_version = version
                key = _winreg.OpenKey(base_key, '%s\\%s' % (base_ooregkey, version))
            except WindowsError:
                raise ValueError('OpenOffice is not installed on this Windows host')

        soffice_bin, key_type = _winreg.QueryValueEx(key, 'Path')
        soffice_dir = os.path.dirname(soffice_bin)
        soffice_rootdir = os.path.split(soffice_dir)[0]
        ure_base = soffice_rootdir
        sys.path.insert(0, soffice_dir)
        if options.javalib is None and options.driver == 'juno':
            try:
                base_java_key = 'SOFTWARE\\JavaSoft\\Java Runtime Environment'
                key = _winreg.OpenKey(base_key, base_java_key)
                numversions = _winreg.QueryInfoKey(key)[0]
                last_version = _winreg.EnumKey(key, numversions - 1)
                if numversions > 1:
                    logging.warning('Multiple Java versions found, using the highest: %s' % last_version)
                javakey = _winreg.OpenKey(base_key, '%s\\%s' % (
                 base_java_key, last_version))
                jvm, key_type = _winreg.QueryValueEx(javakey, 'RuntimeLib')
            except WindowsError:
                raise ValueError('Java is not installed on this Windows host')

        else:
            jvm = options.javalib
    else:
        jvm = options.javalib
        soffice_rootdir = options.office_base
        ure_base = options.ure_base
    if not jvm:
        raise ValueError("You must specify a JVM library (run 'config-py3oservice')")
    jvm_path = os.path.abspath(jvm)
    if not os.path.exists(jvm_path):
        raise ValueError('The specified JVM library does not exist, can not proceed')
    if options.driver == 'juno':
        from py3o.renderers.juno import start_jvm
        start_jvm(jvm, soffice_rootdir, ure_base, int(options.maxmem))
    else:
        raise ValueError('Renderer not yet supported')
    DataHandler.soffice_host = options.soffice_host
    DataHandler.soffice_port = options.soffice_port
    factory = FlowServer(DataHandler)
    from twisted.internet import selectreactor
    selectreactor.install()
    from twisted.internet import reactor
    logging.info('listening on %s:%s', options.listen_interface or '*', options.listen_port)
    if options.listen_interface:
        reactor.listenTCP(int(options.listen_port), factory, interface=options.listen_interface)
    else:
        reactor.listenTCP(int(options.listen_port), factory)
    reactor.run(installSignalHandlers=1)
    return


import re
from base64 import b64decode
from datetime import datetime, timezone
from urllib.parse import urlparse

import marshmallow as ma

from app.objects.interfaces.i_object import FirstClassObjectInterface
from app.objects.secondclass.c_link import Link, LinkSchema
from app.objects.secondclass.c_fact import OriginType
from app.utility.base_object import BaseObject
from app.utility.base_planning_svc import BasePlanningService
from app.utility.base_service import BaseService

class AgentFieldsSchema(ma.Schema):

    paw = ma.fields.String(allow_none=True)
    sleep_min = ma.fields.Integer()
    sleep_max = ma.fields.Integer()
    watchdog = ma.fields.Integer()
    group = ma.fields.String()
    architecture = ma.fields.String()
    platform = ma.fields.String()
    server = ma.fields.String()
    upstream_dest = ma.fields.String(allow_none=True)
    username = ma.fields.String()
    location = ma.fields.String()
    pid = ma.fields.Integer()
    ppid = ma.fields.Integer()
    trusted = ma.fields.Boolean()
    executors = ma.fields.List(ma.fields.String())
    privilege = ma.fields.String()
    exe_name = ma.fields.String()
    host = ma.fields.String()
    contact = ma.fields.String()
    proxy_receivers = ma.fields.Dict(keys=ma.fields.String(), values=ma.fields.List(ma.fields.String()),
                                     allow_none=True)
    proxy_chain = ma.fields.List(ma.fields.List(ma.fields.String()), allow_none=True)
    origin_link_id = ma.fields.String()
    deadman_enabled = ma.fields.Boolean(allow_none=True)
    available_contacts = ma.fields.List(ma.fields.String(), allow_none=True)
    host_ip_addrs = ma.fields.List(ma.fields.String(), allow_none=True)

    display_name = ma.fields.String(dump_only=True)
    created = ma.fields.DateTime(format=BaseObject.TIME_FORMAT, dump_only=True)
    last_seen = ma.fields.DateTime(format=BaseObject.TIME_FORMAT, dump_only=True)
    links = ma.fields.List(ma.fields.Nested(LinkSchema), dump_only=True)
    pending_contact = ma.fields.String()

    @ma.pre_load
    def remove_nulls(self, in_data, **_):
        return {k: v for k, v in in_data.items() if v is not None}

    @ma.pre_load
    def remove_properties(self, data, **_):
        data.pop('display_name', None)
        data.pop('created', None)
        data.pop('last_seen', None)
        data.pop('links', None)
        return data

class AgentSchema(AgentFieldsSchema):

    @ma.post_load
    def build_agent(self, data, **kwargs):
        return None if kwargs.get('partial') is True else Agent(**data)

class Agent(FirstClassObjectInterface, BaseObject):

    schema = AgentSchema()
    load_schema = AgentSchema(partial=['paw', 'origin_link_id'])

    RESERVED = dict(server='#{server}', group='#{group}', agent_paw='#{paw}', location='#{location}',
                    exe_name='#{exe_name}', upstream_dest='#{upstream_dest}',
                    payload=re.compile('#{payload:(.*?)}', flags=re.DOTALL))

    @property
    def unique(self):
        return self.hash(self.paw)

    @property
    def display_name(self):
        return '{}${}'.format(self.host, self.username)

    @classmethod
    def is_global_variable(cls, variable):
        if variable.startswith('payload:'):
            return True
        if variable == 'payload':
            return False
        if variable in cls.RESERVED:
            return True
        return False

    def __init__(self, sleep_min=30, sleep_max=60, watchdog=0, platform='unknown', server='unknown', host='unknown',
                 username='unknown', architecture='unknown', group='red', location='unknown', pid=0, ppid=0,
                 trusted=True, executors=(), privilege='User', exe_name='unknown', contact='unknown', paw=None,
                 proxy_receivers=None, proxy_chain=None, origin_link_id='', deadman_enabled=False,
                 available_contacts=None, host_ip_addrs=None, upstream_dest=None, pending_contact=None):
        super().__init__()
        self.paw = paw if paw else self.generate_name(size=6)
        self.host = host
        self.username = username
        self.group = group
        self.architecture = architecture
        self.platform = platform.lower()
        url = urlparse(server)
        self.server = '%s://%s:%s' % (url.scheme, url.hostname, url.port)
        self.location = location
        self.pid = pid
        self.ppid = ppid
        self.trusted = trusted
        self.created = datetime.now(timezone.utc)
        self.last_seen = self.created
        self.last_trusted_seen = self.created
        self.executors = executors
        self.privilege = privilege
        self.exe_name = exe_name
        self.sleep_min = int(sleep_min)
        self.sleep_max = int(sleep_max)
        self.watchdog = int(watchdog)
        self.contact = contact
        self.links = []
        self.access = self.Access.BLUE if group == 'blue' else self.Access.RED
        self.proxy_receivers = proxy_receivers if proxy_receivers else dict()
        self.proxy_chain = proxy_chain if proxy_chain else []
        self.origin_link_id = origin_link_id
        self.deadman_enabled = deadman_enabled
        self.available_contacts = available_contacts if available_contacts else [self.contact]
        self.pending_contact = pending_contact if pending_contact else contact
        self.host_ip_addrs = host_ip_addrs if host_ip_addrs else []
        if upstream_dest:
            upstream_url = urlparse(upstream_dest)
            self.upstream_dest = '%s://%s:%s' % (upstream_url.scheme, upstream_url.hostname, upstream_url.port)
        else:
            self.upstream_dest = self.server
        self._executor_change_to_assign = None
        self.log = self.create_logger('agent')

    def store(self, ram):
        existing = self.retrieve(ram['agents'], self.unique)
        if not existing:
            ram['agents'].append(self)
            return self.retrieve(ram['agents'], self.unique)
        existing.update('group', self.group)
        existing.update('trusted', self.trusted)
        existing.update('sleep_min', self.sleep_min)
        existing.update('sleep_max', self.sleep_max)
        existing.update('watchdog', self.watchdog)
        existing.update('pending_contact', self.pending_contact)
        return existing

    async def calculate_sleep(self):
        return self.jitter('%d/%d' % (self.sleep_min, self.sleep_max))

    async def capabilities(self, abilities):
        """Get abilities that the agent is capable of running
        :param abilities: List of abilities to check agent capability
        :type abilities: List[Ability]
        :return: List of abilities the agents is capable of running
        :rtype: List[Ability]
        """
        capabilities = []
        for ability in abilities:
            if self.privileged_to_run(ability) and ability.find_executors(self.executors, self.platform):
                capabilities.append(ability)
        return capabilities

    async def get_preferred_executor(self, ability):
        """Get preferred executor for ability
        Will return None if the agent is not capable of running any
        executors in the given ability.
        :param ability: Ability to get preferred executor for
        :type ability: Ability
        :return: Preferred executor or None
        :rtype: Union[Executor, None]
        """
        potential_executors = ability.find_executors(self.executors, self.platform)
        if not potential_executors:
            return None

        preferred_executor_name = self._get_preferred_executor_name()
        for executor in potential_executors:
            if executor.name == preferred_executor_name:
                return executor
        return potential_executors[0]

    async def heartbeat_modification(self, **kwargs):
        now = datetime.now(timezone.utc)
        self.last_seen = now
        if self.trusted:
            self.last_trusted_seen = now
        self.update('pid', kwargs.get('pid'))
        self.update('ppid', kwargs.get('ppid'))
        self.update('server', kwargs.get('server'))
        self.update('exe_name', kwargs.get('exe_name'))
        self.update('location', kwargs.get('location'))
        self.update('privilege', kwargs.get('privilege'))
        self.update('host', kwargs.get('host'))
        self.update('username', kwargs.get('username'))
        self.update('architecture', kwargs.get('architecture'))
        self.update('platform', kwargs.get('platform'))
        self.update('proxy_receivers', kwargs.get('proxy_receivers'))
        self.update('proxy_chain', kwargs.get('proxy_chain'))
        self.update('deadman_enabled', kwargs.get('deadman_enabled'))
        self.update('contact', kwargs.get('contact'))
        self.update('host_ip_addrs', kwargs.get('host_ip_addrs'))
        self.update('upstream_dest', kwargs.get('upstream_dest'))
        if not self._executor_change_to_assign:
            # Don't update executors if we're waiting to assign an executor change to the agent.
            self.update('executors', kwargs.get('executors'))

    async def gui_modification(self, **kwargs):
        loaded = AgentFieldsSchema(only=('group', 'trusted', 'sleep_min', 'sleep_max', 'watchdog', 'pending_contact')).load(kwargs)
        for k, v in loaded.items():
            self.update(k, v)

    async def kill(self):
        self.update('watchdog', 1)
        self.update('sleep_min', 60 * 2)
        self.update('sleep_max', 60 * 2)

    def replace(self, encoded_cmd, file_svc):
        decoded_cmd = b64decode(encoded_cmd).decode('utf-8', errors='ignore').replace('\n', '')
        decoded_cmd = decoded_cmd.replace(self.RESERVED['server'], self.server)
        decoded_cmd = decoded_cmd.replace(self.RESERVED['group'], self.group)
        decoded_cmd = decoded_cmd.replace(self.RESERVED['agent_paw'], self.paw)
        decoded_cmd = decoded_cmd.replace(self.RESERVED['location'], self.location)
        decoded_cmd = decoded_cmd.replace(self.RESERVED['exe_name'], self.exe_name)
        decoded_cmd = decoded_cmd.replace(self.RESERVED['upstream_dest'], self.upstream_dest)
        decoded_cmd = self._replace_payload_data(decoded_cmd, file_svc)
        return decoded_cmd

    def privileged_to_run(self, ability):
        if not ability.privilege or self.Privileges[self.privilege].value >= self.Privileges[ability.privilege].value:
            return True
        return False

    async def bootstrap(self, data_svc):
        abilities = []
        for i in self.get_config(name='agents', prop='bootstrap_abilities'):
            for a in await data_svc.locate('abilities', match=dict(ability_id=i)):
                abilities.append(a)
        await self.task(abilities, obfuscator='plain-text')

    async def deadman(self, data_svc):
        abilities = []
        deadman_abilities = self.get_config(name='agents', prop='deadman_abilities')
        if deadman_abilities:
            for i in deadman_abilities:
                for a in await data_svc.locate('abilities', match=dict(ability_id=i)):
                    abilities.append(a)
        await self.task(abilities, obfuscator='plain-text', deadman=True)

    async def task(self, abilities, obfuscator, facts=(), deadman=False):
        if not self.executors:
            return []

        bps = BasePlanningService()
        preferred_executor_name = self._get_preferred_executor_name()

        links = []
        for ability in await self.capabilities(abilities):
            executors = ability.find_executors(self.executors, self.platform)
            executors = sorted(executors, key=lambda ex: ex.name == preferred_executor_name, reverse=True)

            for executor in executors:
                ex_links = [Link.load(dict(command=self.encode_string(executor.test), paw=self.paw, ability=ability,
                                           executor=executor, deadman=deadman))]
                valid_links = await bps.add_test_variants(links=ex_links, agent=self, facts=facts, trim_unset_variables=True)
                if valid_links:
                    links.extend(valid_links)
                    break

        links = await bps.obfuscate_commands(self, obfuscator, links)
        knowledge_svc_handle = BaseService.get_service('knowledge_svc')
        for fact in facts:
            fact.source = self.paw
            fact.origin_type = OriginType.SEEDED
            await knowledge_svc_handle.add_fact(fact)
        self.links.extend(links)
        return links

    async def all_facts(self):
        knowledge_svc_handle = BaseService.get_service('knowledge_svc')
        return await knowledge_svc_handle.get_facts(dict(source=self.paw))

    @property
    def executor_change_to_assign(self):
        return self._executor_change_to_assign

    def set_pending_executor_removal(self, executor_name):
        """Mark specified executor to remove.
        :param executor_name: name of executor for agent to remove
        :type executor_name: str
        """
        if executor_name and isinstance(executor_name, str):
            if executor_name in self.executors:
                # Remove the executor server-side so planners can generate appropriate links immediately.
                self.executors.remove(executor_name)
                self._executor_change_to_assign = dict(action='remove', executor=executor_name)
        else:
            self.log.error('Paw %s: Invalid executor name. Please provide non-empty string. Provided value: %s',
                           self.paw, executor_name)

    def set_pending_executor_path_update(self, executor_name, new_binary_path):
        """Mark specified executor to update its binary path to the new path.
        :param executor_name: name of executor for agent to update binary path
        :type executor_name: str
        :param new_binary_path: new binary path for executor to reference
        :type new_binary_path: str
        """
        if executor_name and new_binary_path and isinstance(executor_name, str) and isinstance(new_binary_path, str):
            if executor_name in self.executors:
                self._executor_change_to_assign = dict(action='update_path', executor=executor_name,
                                                       value=new_binary_path)
        else:
            self.log.error('Paw %s: Invalid format for executor name or new binary path. '
                           'Please provide non-empty strings. Provided values: %s, %s',
                           self.paw, executor_name, new_binary_path)

    def assign_pending_executor_change(self):
        """Return the executor change dict and remove pending change to assign.
        :return: Dict representing the executor change that is assigned.
        :rtype: dict(str, str)
        """
        executor_change = self.executor_change_to_assign
        self._executor_change_to_assign = None
        return executor_change

    def _replace_payload_data(self, decoded_cmd, file_svc):
        for uuid in re.findall(self.RESERVED['payload'], decoded_cmd):
            if self.is_uuid4(uuid):
                _, display_name = file_svc.get_payload_name_from_uuid(uuid)
                decoded_cmd = decoded_cmd.replace('#{payload:%s}' % uuid, display_name)
        return decoded_cmd

    def _get_preferred_executor_name(self):
        if 'psh' in self.executors:
            return 'psh'
        elif 'sh' in self.executors:
            return 'sh'
        return self.executors[0]


class CustomException(Exception):
    def __init__(self, message, value):
        super().__init__()
        self.message = message
        self.value = value

    def __str__(self):
        return self.message

    def print(self):
        print("##### 오류 정보 #####")
        print("메시지:", self.message)
        print("값:", self.value)

try:
    raise CustomException("딱히 이유는 없음", 273)
except CustomException as e:
    e.print()


from typing import List

def range_with_sum(all_numbers: List[int],
                   target: int,
                   sum: int = 0,
                   numbers_slice: List[int] = None,
                   ):
    """
    >>> range_with_sum([1,2,3,4,5,6], 10)
    [1, 2, 3, 4]

    >>> range_with_sum([1,2,3,4,5,6], 9)
    [2, 3, 4]

    >>> range_with_sum([1,2,3,4,5,6], 50)
    []

    """
    # print(numbers_slice, all_numbers)
    if not numbers_slice:
        numbers_slice = []

    if sum == target:
        # print('found')
        return numbers_slice
    elif not all_numbers and sum < target:
        # print('all num empty')
        return []
    elif sum < target:
        # print('sum < target: ', sum, '<', target)
        return range_with_sum(all_numbers=all_numbers[1:],
                              target=target,
                              sum=sum + all_numbers[0],
                              numbers_slice=[*numbers_slice, all_numbers[0]])
    else:  # sum > target:
        # print('---')
        return range_with_sum(all_numbers=[*numbers_slice, *all_numbers][1:],
                              target=target)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

    print(range_with_sum([1, 2, 3, 4, 5, 6], 30))


import pty
import os
import select
import serial
import sys
import re
import time

def mkpty():
    #打开伪终端
    master1, slave = pty.openpty()
    slave_name1 = os.ttyname(slave)
    master2, slave = pty.openpty()
    slave_name2 = os.ttyname(slave)
    print(slave_name1, slave_name2)
    sys.stdout.flush()
    return master1, master2

if __name__ == "__main__":
    master1, master2 = mkpty()
    ser = serial.Serial('/dev/ttyS0', 115200, timeout=0.05)
    ser.write(b'config com1 115200\r\n')
    ser.write(b'config com3 9600\r\n')
    ser.write(b'UNLOGALL COM1\r\n')
    ser.write(b'UNLOGALL COM2\r\n')
    ser.write(b'UNLOGALL COM3\r\n')
    ser.write(b'mode rover\r\n')
    #ser.write(b'log com1 rtkposb ontime 1\r\n')
    #ser.write(b'log com1 rtkvelb ontime 1\r\n')
    ser.write(b'log com1 bestposb ontime 0.2\r\n')
    ser.write(b'log com1 bestvelb ontime 0.2\r\n')
    ser.write(b'log com1 bestxyzb ontime 0.2\r\n')
    ser.write(b'log com1 headingb ontime 0.2\r\n')
    ser.write(b'GPGGA COM1 1\r\n')
    ser.write(b'GPRMC COM3 1\r\n')
    ser.write(b'GPGGA COM3 1\r\n')
    ser.write(b'SAVECONFIG\r\n')
    time.sleep(1)

    while True:
        rl, wl, el = select.select([ser, master2], [], [], 1)
        for master in rl:
            if master == master2:
                data = os.read(master, 1024)
                ser.write(data)
            if master == ser:
                data = ser.read(1024)
                pattern = re.compile(b'\$GP\S*\\r\\n')
                os.write(master1, pattern.sub(b'', data))
                for gps_data in pattern.findall(data):
                    os.write(master2, gps_data)


#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

import numpy as np
def softmax(x):
    """
    Compute the softmax function for each row of the input x.

    Arguments:
    x -- A N dimensional vector or M x N dimensional numpy matrix.

    Return:
    x -- You are allowed to modify x in-place
    """
    orig_shape = x.shape

    if len(x.shape) > 1:
        # Matrix
        exp_minmax = lambda x: np.exp(x - np.max(x))
        denom = lambda x: 1.0 / np.sum(x)
        x = np.apply_along_axis(exp_minmax,1,x)
        denominator = np.apply_along_axis(denom,1,x)

        if len(denominator.shape) == 1:
            denominator = denominator.reshape((denominator.shape[0],1))

        x = x * denominator
    else:
        # Vector
        x_max = np.max(x)
        x = x - x_max
        numerator = np.exp(x)
        denominator =  1.0 / np.sum(numerator)
        x = numerator.dot(denominator)

    assert x.shape == orig_shape
    return x

def convert_to_one_hot(y, C):
    return np.eye(C)[y.reshape(-1)].T

y = np.array([1,2,3,4])
convert_to_one_hot(y,5)

# array([[ 0.,  0.,  0.,  0.],#        [ 1.,  0.,  0.,  0.],#        [ 0.,  1.,  0.,  0.],#        [ 0.,  0.,  1.,  0.],#        [ 0.,  0.,  0.,  1.]])


# 정점 탐색

def dfs(m, vi, idx, v):
    if idx == len(vi):
        return

    vi[v] = True
    print(v)

    for i in range(0, len(m)):
        if m[v][i] == 1 and (not vi[i]):
            dfs(m, vi, idx+1, i)

def main():
    test_case = int(input())

    for t in range(test_case):
        line = input().split()
        dot = int(line[0])
        edge = int(line[1])
        m = [[0]*(dot+1) for _ in range(dot+1)]

        line2 = input().split()
        i = 0
        for e in range(edge):
            a = int(line2[i])
            b = int(line2[i+1])
            m[a][b] = 1
            m[b][a] = 1
            i += 2

        vi = [False]*(dot+1)
        dfs(m, vi, 0, 1)

if __name__ == '__main__':
    main()


#TMXLoader.py

from . import ResourceCache
from .. import Log
import tmx, os

class TMXLoader(ResourceCache.Loader):
    """This class uses the simple TMX library to load a tilemap file into
the resource cache. It then will also load all the images and tilesets into the
resourcecache that are not already there"""
    def __init__(self):
        self._tilemap = None

    def GetExtension(self):
        return [".tmx"]

    def Load(self, AbsoluteFilepath):
        filename = os.path.split(AbsoluteFilepath)
        #Load the tilemap
        self._tilemap = tmx.TileMap.load(filename)
        TSXLoaderPtr = ResourceCache.ResourceCache.gResourceCache.GetLoader(".tsx")
        if (TSXLoaderPtr == None):
            Log.LogError("Can't load tilemap object without TSXLoader")

        for tileset in self._tilemap.tilesets:
            if tileset.source != None:
                TSXLoaderPtr.Load(tileset.source)

        #If we need to preload objects I guess we should load those?


def gcd(a: int, b: int) -> int:
    while a != 0:
        a, b = b % a, a
    return b

def find_mod_inverse(a: int, m: int) -> int:
    if gcd(a, m) != 1:
        msg = f"mod inverse of {a!r} and {m!r} does not exist"
        raise ValueError(msg)
    u1, u2, u3 = 1, 0, a
    v1, v2, v3 = 0, 1, m
    while v3 != 0:
        q = u3 // v3
        v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
    return u1 % m


import csv

def lerCSV(caminho):
    matriz = []
    with open(caminho) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        csv_reader.__next__()
        for row in csv_reader:
            matriz = matriz + [row[1:]]

    matriz = [[int(j) for j in i] for i in matriz]
    return matriz

def calcularGraus(matriz):
    graus = []
    for v in range(len(matriz)):
        graus.append(0)
        for a in range(len(matriz[v])):
            if(matriz[v][a] == 1):
                graus[v] += 1
    return graus

def TeoremaOre(matriz, graus):
    graus = calcularGraus(matriz)
    if(len(matriz) < 3):
        return False

    for v1 in range(len(graus)-1):

        for v2 in range(v1+1, len(matriz[v1])):
            if(graus[v1] + graus[v2] < len(matriz)):
                return False
    return True

def bondyAndChvatal(matriz, graus):
    if(len(matriz)<3):
        return False
    matrizAux = matriz.copy()
    for v1 in range(len(graus)-1):
        for v2 in range(v1+1, len(matriz[v1])):
            if(matrizAux[v1][v2] == 0):
                if(graus[v1] + graus[v2] >= len(matriz)):
                    matrizAux[v1][v2] = 1
                    matrizAux[v2][v1] = 1
                else:
                    return False
    return True

def checarTeorema(teorema, resultado):
    print("Teorema: " + teorema + "\nResultado: " + str(resultado) + "\n------------------------------------------------------------------------------------------------------")

def TeoremaDirac(matriz, graus):

    if(len(matriz) < 3):
        return False

    for g in range(len(graus)):
        if(graus[g] < len(matriz)/2):
            return False
    return True

def checarGrafos(caminho):
    matriz = lerCSV(caminho)
    print("Matriz:" + str(matriz) + "\n")
    grausMatriz = calcularGraus(matriz)
    checarTeorema("Ore", TeoremaOre(matriz, grausMatriz))
    checarTeorema("Bondy And Chvatal", bondyAndChvatal(matriz, grausMatriz))
    checarTeorema("Dirac", TeoremaDirac(matriz, grausMatriz))

#------------------------  Checagem dos Grafos -------------------------------------
print("")
print("Grafo 01 :")
checarGrafos(r"C:\Users\Aptare Developer 02\Desktop\unifor\Grafos\Trabalho3\Grafos\Grafo01.csv")
print("Grafo 02 :")
checarGrafos(r"C:\Users\Aptare Developer 02\Desktop\unifor\Grafos\Trabalho3\Grafos\Grafo02.csv")
print("Grafo 03 :")
checarGrafos(r"C:\Users\Aptare Developer 02\Desktop\unifor\Grafos\Trabalho3\Grafos\Grafo03.csv")
print("Grafo 04 :")
checarGrafos(r"C:\Users\Aptare Developer 02\Desktop\unifor\Grafos\Trabalho3\Grafos\Grafo04.csv")


# -*- coding: utf-8 -*-
"""
Created on Fri Feb 28 14:47:20 2020

@author: jnk5kor
"""
#program to print the jumping numbers :- whose adjacent no. digits difference is 1 or -1
x=int(input("Print any positive number"))
for num in range(10,x):
    a=str(num)
    for i in range(0,len(a)-1):
        diff=int(a[i])-int(a[i+1])
        if abs(diff)==1:
            if i==len(a)-2:
                print(a)
        else:
            break


import random

artists = ['dipaka', 'jalsib', 'chain']

class TrapArtist:
    print(0)

    def __init__(self, name):
        print(1)
        self._name = name

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

    @name.setter
    def name(self, name):
        print(43)
        if name not in artists:
            raise ValueError('dflk')
        self._name = name

    @staticmethod
    def randomartist():
        print(5)
        return TrapArtist(random.choice(artists))

a = TrapArtist.randomartist()
print(a.name)


from django.shortcuts import render,redirect
from .models import Paciente
from .forms import pacienteForm
# Create your views here.
# def listado_pacientes(request):
#     pacientes=Paciente.objects.all()
#     contexto = {'paciente':pacientes}
#     return render(request, 'users/listado_pacientes.html',contexto)

def listado_pacientes(request):
    pacientes=Paciente.objects.all()
    data={
        'lista_pacientes':pacientes
    }
    return render(request, 'users/listado_pacientes.html',data)

def modificar_paciente(request,correo): #con este parametro correo haces la busqueda en la bd y obtenes
    paciente = Paciente.objects.get(correo=correo) # el paciente que va a modificar
    if request.method == 'GET':
        form = pacienteForm(instance=paciente)
    else:
        form = pacienteForm(request.POST,instance=paciente)
        if form.is_valid():
            form.save()
        return redirect('listado_pacientes')
    return render(request,'paciente/ingresar_paciente.html', {'form': form})
    #return render(request,'home.html')  #aca cambiale el home.html por la pagina de modificar

def ingresar_paciente(request):
    form = pacienteForm(request.POST)
    #if request.method == 'POST':
    if form.is_valid():
        form.save()
        #return redirect('pages:listado_pacientes')
        pacientes=Paciente.objects.all()
        data={
            'lista_pacientes':pacientes
        }
        return render(request, 'users/listado_pacientes.html',data)

    else:
        form = pacienteForm()
    return render(request, 'paciente/ingresar_paciente.html', {'form': form})

    #form = pacienteForm(request.POST or None)
   # if form.is_valid():
   #     form.save()
   #     form = pacienteForm()
   # context = {
   #     'form': form
   # }
   # return render(request, "paciente/ingresar_paciente.html", context)

def detalle_paciente(request, correo):
    paciente = Paciente.objects.get(correo=correo)
    if request.method == 'GET':
        form = pacienteForm(instance=paciente)
    return render(request,'paciente/detalle_paciente.html',{'form':form})


import io
import math
import nltk
import string
import lxml.html as html
topics=['business','politics']
all_text=[]
corpus={}
for i in topics:

    page=html.parse('http://www.gazeta.ru/'+i+'/')

    urls = page.getroot().xpath('//div[@class="pl20 pr20"]//a[@href]/@href')

    for j in urls:
        #print ('http://www.gazeta.ru/'+j)
        new_page = html.parse('http://www.gazeta.ru/'+j)

        texts = new_page.getroot().xpath('//p/text()')
        texts = [''.join(t).encode('utf-8', 'ignore').decode('utf-8') for t in texts]
        page_texts = ''.join(texts)

        page_texts = page_texts.replace('\n', ' ').replace('\r', '')
        page_texts = " ".join(page_texts.split())
        all_text.append(page_texts)
    corpus[i]=all_text
    all_text=[]
        #print  page_texts

def tokenize(corpus):
    news_list = corpus
    words = []
    for text in news_list:
        text_str = ''.join(text)
        line = nltk.word_tokenize(text_str)
        line = [word.encode("utf-8").decode("utf-8").lower() for word in line if word not in string.punctuation
                        and word not in [u'\u2012', u'\u2013', u'\u2014', u'\u2015', u'``', u"''"]]
        words += line
    return words
#arr= tokenize(all_text)
#print type(corpus['politics'])
#print (tokenize(corpus['politics']))

def load_ngrams(in_file):
    input_f = io.open(in_file, "r", encoding='utf8')
    n_dict = {}
    for line in input_f:
        line = line.replace('\n', '').split('\t')
        n_dict[tuple(line[0:])] = int(line[0])
    input_f.close()
    return n_dict

def compute_perplexity(category, n3_gram, n2_gram):
    sum_probability = 0
    unknown_words = 0
    min_probability = 1
    sum_log = 0

    current_probability = {}

    for i in range(2, len(category)):
        try:
            current_probability[i] = (n3_gram[(category[i - 2], category[i - 1], category[i])] + 0.0)/ n2_gram[(category[i - 2], category[i - 1])]
            sum_probability += current_probability
            if current_probability[i] < min_probability:
                min_probability = current_probability[i]
        except KeyError:
            unknown_words += 1
    print (unknown_words, len(category), sum_probability)
    known_words = len(category) - unknown_words
    min_probability /= 10.0
    probability_to_substract = (unknown_words * min_probability + 0.0) / known_words
    for i in range(2, len(category)):
        try:
            sum_log += math.log(current_probability[i] - probability_to_substract, 2)
        except KeyError:
            sum_log += math.log(min_probability, 2)

    return sum_log

n_two_gram=load_ngrams("C://valentina//17.07.2016//2grams-3(1)//2grams-3.txt")
#print (len(n_two_gram.keys()))
n_three_gram=load_ngrams("C://valentina//17.07.2016//3grams-3//3grams-3.txt")
#print (len(n_three_gram.keys()))
arr_business=(tokenize(corpus['business']))
arr_politics=tokenize(corpus['politics'])

business_per=compute_perplexity(arr_business, n_three_gram, n_two_gram)
print ("Perplexity for business = ",2**(-business_per / len(arr_business)))
print ("Perplexity for politics = ",2**(-business_per / len(arr_politics)))


print('--GERADOR DE P.A--')
#ENTRADAS
primeiro_termo = int(input('Qual o primeiro termo da P.A? >> '))
razao = int(input('Qual vai ser a razão da P.A? >> '))

#PROCESSAMENTO
qtd_de_termos = 1                               #Quantidade de termos totais
termo1 = primeiro_termo                         #Primeiro termo
mais = 10                                       #Simulação de que foi escolhido 10 termos a mais para ver (pq o programa em si ja mostra 1o termos)
total = 0

while mais != 0:
    total = total + mais
    while qtd_de_termos <= total(10):
        print('{} >> '.format(termo1), end='')
        termo1 += razao
        qtd_de_termos += 1                       #Se chegou neste ponto e não satisfez o qtd_de_termos <= total ele volta pro inicio desse bloco de while
    print('Pausa')
    mais = int(input('Qual termo você gostaria de ver a mais? [se não, digite 0] >> '))

#SAIDAS
print('--TOTAL--')
print('O total final ficou em {} termos'.format(qtd_de_termos))
print('--FIM--')


#-*-coding:utf-8-*-
import os
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

_loop = 100

path = "D://iflytek/stp/client/nw/chromedriver"
chrome_options = Options()

chrome_options.add_argument( "nwapp=D:\iflytek\stp\client\stp-page-client")
print("01 initial done")

#在功能模块出现的时候点击“讲话速记”
driver = webdriver.Chrome( executable_path=path, chrome_options=chrome_options)
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/div[2]/div[3]/div/div[2]/ul/li[1]/div'))
    )
    element.click()
    print("02 Open real-time")
finally:
    time.sleep(10)

#在音频选择页选择本地内置麦克风
driver.find_element_by_xpath('/html/body/div[8]/div/div/div/div[2]/div[1]/form/ul/li[3]/label').click()
time.sleep(5)
print("03 select mic")

#在音频选择页点击“下一步”
driver.find_element_by_xpath('/html/body/div[8]/div/div/div/div[2]/div[2]/div/a').click()
time.sleep(5)
print("04 click next")

#开始速记
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[1]/div[1]/div[1]').click()
time.sleep(5)
print("05 begin record")

count = 0
while (count < _loop):
    #编辑区域获取焦点，输入hello...
    element01 = driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[3]/div/div[1]')
    element01.click()
    time.sleep(50)
    print("06 get focus")
    element01.send_keys("hello,we will input something in the editor")
    time.sleep(5)
    print("07 input hello...")

    #暂停速记
    driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[1]/div[1]/div[3]').click()
    time.sleep(10)
    print("08 pause")

    #编辑区域获取焦点，输入你好...
    element02 = driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[3]/div/div[1]')
    element02.click()
    time.sleep(10)
    print("09 get focus")
    element02.send_keys(u'哈哈哈哈哈哈！！！你好，我会在编辑器中添加一些汉字！！！哈哈哈哈哈')
    time.sleep(5)
    print(u'10 input 你好...')

    #继续速记
    driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[1]/div[1]/div[2]').click()
    time.sleep(10)
    print("11 continue")

    time.sleep(500)
    count = count + 1

#点击查找替换
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[1]/ul/li[1]/a').click()
time.sleep(10)
print("12 find&replace")

#替换"你"为"我"
element03 = driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[2]/div/div[1]/div/input')
element03.click()
time.sleep(5)
print("13 find content")
element03.send_keys(u'你')

element04 = driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/div/input')
element04.click()
time.sleep(5)
print("14 replace content")
element04.send_keys(u'我')

#点击替换
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/a[1]').click()
time.sleep(10)
print("15 replace")

#点击X关闭查找替换栏
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[2]/div/a').click()
time.sleep(10)
print("16 find&replace")

#点击查找替换
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[1]/ul/li[1]/a').click()
time.sleep(10)
print("17 find&replace")

#替换"我"为"你"
element05 = driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[2]/div/div[1]/div/input')
element05.click()
time.sleep(5)
print("18 find content")
element05.send_keys(u'我')

element06 = driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/div/input')
element06.click()
time.sleep(5)
print("19 replace content")
element06.send_keys(u'你')

#点击全部替换
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/a[2]').click()
time.sleep(10)
print("20 replace ALL")


# Importing Python Libraries
from datetime import datetime as dt
import  time

print(time.localtime(time.time()))

date = dt.now()
if dt(date.year,date.month,date.day,8) < date < dt(date.year,date.month,date.day,18):
    print("I am in Office now, Kindly Call me Later")
else:
    print("Fun Hours")

import calendar
cal = calendar.prcal(2000)

print(cal)


from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from flask_moment import Moment
from datetime import datetime
from wtforms import FloatField,SubmitField
from wtforms.validators import DataRequired, NumberRange
from geopy.distance import geodesic

app = Flask(__name__)
bootstrap = Bootstrap(app)
moment  = Moment(app)
app.config['SECRET_KEY'] = 'hard to guess string'

class NameForm(FlaskForm):
    Alat = FloatField('What is A\'s latitude?', validators=[DataRequired(), NumberRange(min=-90, max=90, message='Latitude is in between -90 to 90')])
    Along = FloatField('What is A\'s longtitude?', validators=[DataRequired(), NumberRange(min=-180, max=180, message='Longitude is in between -180 to 180')])
    Blat = FloatField('What is B\'s latitude?', validators=[DataRequired(), NumberRange(min=-90, max=90, message='Latitude is in between -90 to 90')])
    Blong = FloatField('What is B\'s longtitude?', validators=[DataRequired(), NumberRange(min=-180, max=180, message='Longitude is in between -180 to 180')])
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        Alat = form.Alat.data
        Along = form.Along.data
        Blat = form.Blat.data
        Blong = form.Blong.data
        if Alat == Along == Blat == Blong and Alat != 0 and Along != 0 and Blat != 0 and Blong != 0:
            session['A'] = Alat, Along
            session['B'] = Blat, Blong
            session['dis'] = str(0)
        else:
            session['A'] = Alat, Along
            session['B'] = Blat, Blong
            session['dis'] = geodesic(session['A'], session['B']).km

        # old_dis = session.get('dis')
        # if old_dis is not None and old_dis != geodesic(A, B).km:
        #     flash('Looks like you have changed your location!')
        # session['dis'] = geodesic(A,B).km

        return redirect(url_for('index'))
    return render_template('main.html', form=form, dis=session.get('dis'), current_time=datetime.utcnow())

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