import tensorflow as tf
import logging
tf.get_logger().setLevel(logging.ERROR)
import numpy as np
from model_utils import biGRUCell, initial_point_interpolation

class mrnn ():
  """MRNN class with core functions.

  Attributes:
    - x: incomplete data
    - model_parameters:
      - h_dim: hidden state dimensions
      - batch_size: the number of samples in mini-batch
      - iteration: the number of iteration
      - learning_rate: learning rate of model training
  """

  def __init__(self, x, model_parameters):

    # Set Parameters
    self.no, self.seq_len, self.dim = x.shape

    self.h_dim = model_parameters['h_dim']
    self.batch_size = model_parameters['batch_size']
    self.iteration = model_parameters['iteration']
    self.learning_rate = model_parameters['learning_rate']

  def rnn_train (self, x, m, t, f):
    """Train RNN for each feature.

    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix
      - f: feature index
    """
    tf.compat.v1.reset_default_graph()

    with tf.compat.v1.Session() as sess:

      # input place holders
      target = tf.placeholder(tf.float32, [self.seq_len, None, 1])
      mask = tf.placeholder(tf.float32, [self.seq_len, None, 1])

      # Build rnn object
      rnn = biGRUCell(3, self.h_dim, 1)
      outputs = rnn.get_outputs()
      loss = tf.sqrt(tf.reduce_mean(tf.square(mask*outputs - mask*target)))
      optimizer = tf.compat.v1.train.AdamOptimizer(self.learning_rate)
      train = optimizer.minimize(loss)

      sess.run(tf.compat.v1.global_variables_initializer())

      # Training
      for i in range(self.iteration):
        # Batch selection
        batch_idx = np.random.permutation(x.shape[0])[:self.batch_size]

        temp_input = np.dstack((x[:,:,f], m[:,:,f], t[:,:,f]))
        temp_input_reverse = np.flip(temp_input, 1)

        forward_input = np.zeros([self.batch_size, self.seq_len, 3])
        forward_input[:,1:,:] = temp_input[batch_idx,
                                           :(self.seq_len-1), :]

        backward_input = np.zeros([self.batch_size, self.seq_len, 3])
        backward_input[:,1:,:] = temp_input_reverse[batch_idx,
                                                    :(self.seq_len-1),:]

        _, step_loss = \
        sess.run([train, loss],
                 feed_dict=
                 {mask: np.transpose(np.dstack(m[batch_idx,:,f]),[1, 2, 0]),
                  target: np.transpose(np.dstack(x[batch_idx,:,f]),[1, 2, 0]),
                  rnn._inputs: forward_input,
                  rnn._inputs_rev: backward_input})

      # Save model
      inputs = {'forward_input': rnn._inputs,
                'backward_input': rnn._inputs_rev}
      outputs = {'imputation': outputs}

      save_file_name = 'tmp/mrnn_imputation/rnn_feature_' + str(f+1) + '/'
      tf.compat.v1.saved_model.simple_save(sess, save_file_name,
                                           inputs, outputs)

  def rnn_predict (self, x, m, t):
    """Impute missing data using RNN block.

    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix

    Returns:
      - imputed_x: imputed data by rnn block
    """
    # Output Initialization
    imputed_x = np.zeros([self.no, self.seq_len, self.dim])

    # For each feature
    for f in range(self.dim):

      temp_input = np.dstack((x[:,:,f], m[:,:,f], t[:,:,f]))
      temp_input_reverse = np.flip(temp_input, 1)

      forward_input = np.zeros([self.no, self.seq_len, 3])
      forward_input[:,1:,:] = temp_input[:,:(self.seq_len-1), :]

      backward_input = np.zeros([self.no, self.seq_len, 3])
      backward_input[:,1:,:] = temp_input_reverse[:,:(self.seq_len-1),:]

      save_file_name = 'tmp/mrnn_imputation/rnn_feature_' + str(f+1)  + '/'

      # Load saved model
      graph = tf.Graph()
      with graph.as_default():
        with tf.compat.v1.Session() as sess:
          sess.run(tf.compat.v1.global_variables_initializer())
          tf.compat.v1.saved_model.loader.load(sess,
                                               [tf.compat.v1.saved_model.SERVING],
                                               save_file_name)
          fw_input = graph.get_tensor_by_name('inputs:0')
          bw_input = graph.get_tensor_by_name('inputs_rev:0')
          output = graph.get_tensor_by_name('map/TensorArrayStack/TensorArrayGatherV3:0')

          imputed_data = sess.run(output,
                                  feed_dict={fw_input: forward_input,
                                             bw_input: backward_input})

          imputed_x[:, :, f] = (1-m[:,:,f]) * np.transpose(np.squeeze(imputed_data)) + \
                               m[:,:,f] * x[:,:,f]

    # Initial poitn interpolation for better performance
    imputed_x = initial_point_interpolation (x, m, t, imputed_x)

    return imputed_x

  def fc_train(self, x, m, t):
    """Train Fully Connected Networks after RNN block.

    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix
    """
    tf.compat.v1.reset_default_graph()

    # rnn imputation results
    rnn_imputed_x = self.rnn_predict(x, m, t)

    # Reshape the data for FC train
    x = np.reshape(x, [self.no * self.seq_len, self.dim])
    rnn_imputed_x = np.reshape(rnn_imputed_x, [self.no * self.seq_len, self.dim])
    m = np.reshape(m, [self.no * self.seq_len, self.dim])

    # input place holders
    x_input = tf.placeholder(tf.float32, [None, self.dim])
    target = tf.placeholder(tf.float32, [None, self.dim])
    mask = tf.placeholder(tf.float32, [None, self.dim])

    # build a FC network
    U = tf.compat.v1.get_variable("U", shape=[self.dim, self.dim],
                                  initializer=tf.contrib.layers.xavier_initializer())
    V1 = tf.compat.v1.get_variable("V1", shape=[self.dim, self.dim],
                                   initializer=tf.contrib.layers.xavier_initializer())
    V2 = tf.compat.v1.get_variable("V2", shape=[self.dim, self.dim],
                                   initializer=tf.contrib.layers.xavier_initializer())
    b = tf.Variable(tf.random.normal([self.dim]))

    L1 = tf.nn.sigmoid((tf.matmul(x_input, tf.linalg.set_diag(U, np.zeros([self.dim,]))) + \
                        tf.matmul(target, tf.linalg.set_diag(V1, np.zeros([self.dim,]))) + \
                        tf.matmul(mask, V2) + b))

    W = tf.Variable(tf.random.normal([self.dim]))
    a = tf.Variable(tf.random.normal([self.dim]))
    hypothesis = W * L1 + a

    outputs = tf.nn.sigmoid(hypothesis)

    # reshape out for sequence_loss
    loss = tf.sqrt(tf.reduce_mean(tf.square(outputs - target)) )

    # Optimizer
    optimizer = tf.compat.v1.train.AdamOptimizer(self.learning_rate)
    train = optimizer.minimize(loss)

    # Sessions
    sess = tf.compat.v1.Session()
    sess.run(tf.compat.v1.global_variables_initializer())

    # Training step
    for i in range(self.iteration * 20):
      batch_idx = np.random.permutation(x.shape[0])[:self.batch_size]
      _, step_loss = sess.run([train, loss],
                              feed_dict={x_input: x[batch_idx, :],
                                         target: rnn_imputed_x[batch_idx, :],
                                         mask: m[batch_idx, :]})

    # Save model
    inputs = {'x_input': x_input,
              'target': target,
              'mask': mask}
    outputs = {'imputation': outputs}

    save_file_name = 'tmp/mrnn_imputation/fc_feature/'
    tf.compat.v1.saved_model.simple_save(sess, save_file_name,
                                         inputs, outputs)

  def rnn_fc_predict(self, x, m, t):
    """Impute missing data using RNN and FC.

    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix

    Returns:
      - fc_imputed_x: imputed data using RNN and FC
    """
    # rnn imputation results
    rnn_imputed_x = self.rnn_predict(x, m, t)

    # Reshape the data for FC predict
    x = np.reshape(x, [self.no * self.seq_len, self.dim])
    rnn_imputed_x = np.reshape(rnn_imputed_x, [self.no * self.seq_len, self.dim])
    m = np.reshape(m, [self.no * self.seq_len, self.dim])

    save_file_name = 'tmp/mrnn_imputation/fc_feature/'

    # Load saved data
    graph = tf.Graph()
    with graph.as_default():
      with tf.compat.v1.Session() as sess:

        sess.run(tf.compat.v1.global_variables_initializer())
        tf.compat.v1.saved_model.loader.load(sess, [tf.compat.v1.saved_model.SERVING],
                                             save_file_name)
        x_input = graph.get_tensor_by_name('Placeholder:0')
        target = graph.get_tensor_by_name('Placeholder_1:0')
        mask = graph.get_tensor_by_name('Placeholder_2:0')
        outputs = graph.get_tensor_by_name('Sigmoid_1:0')

        fc_imputed_x = sess.run(outputs, feed_dict={x_input: x,
                                                    target: rnn_imputed_x,
                                                    mask: m})

    # Reshape imputed data to 3d array
    fc_imputed_x = np.reshape(fc_imputed_x, [self.no, self.seq_len, self.dim])
    m = np.reshape(m, [self.no, self.seq_len, self.dim])
    x = np.reshape(x, [self.no, self.seq_len, self.dim])

    fc_imputed_x = fc_imputed_x * (1-m) + x * m
    fc_imputed_x = initial_point_interpolation (x, m, t, fc_imputed_x)

    return fc_imputed_x

  def fit(self, x, m, t):
    """Train the entire MRNN.

    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix
    """
    # Train RNN part
    for f in range(self.dim):
      self.rnn_train(x, m, t, f)
      print('Finish ' + str(f+1) + '-th feature training with RNN for imputation')
    # Train FC part
    self.fc_train(x, m, t)
    print('Finish M-RNN training with both RNN and FC for imputation')

  def transform(self, x, m, t):
    """Impute missing data using the entire MRNN.

    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix

    Returns:
      - imputed_x: imputed data
    """
    # Impute with both RNN and FC part
    imputed_x = self.rnn_fc_predict(x, m, t)

    return imputed_x


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

from flask import request, session, redirect, abort
from flask import current_app, Blueprint

from eduid_common.authn.utils import get_location
from eduid_common.authn.eduid_saml2 import get_authn_request, get_authn_response
from eduid_common.authn.eduid_saml2 import authenticate
from eduid_webapp.authn.acs_registry import get_action, schedule_action

import logging
logger = logging.getLogger(__name__)

authn_views = Blueprint('authn', __name__)

@authn_views.route('/login')
def login():
    """
    login view, redirects to SAML2 IdP
    """
    redirect_url = current_app.config.get('SAML2_LOGIN_REDIRECT_URL', '/')
    came_from = request.args.get('next', redirect_url)
    idp = session.get('idp', None)
    idp = request.args.get('idp', idp)
    authn_request = get_authn_request(current_app.config,
                                      session, came_from, idp)
    schedule_action('login-action')
    logger.debug('Redirecting the user to the IdP')
    return redirect(get_location(authn_request))

@authn_views.route('/saml2-acs', methods=['POST'])
def assertion_consumer_service():
    """
    Assertion consumer service, receives POSTs from SAML2 IdP's
    """
    if 'SAMLResponse' not in request.form:
        abort(400)
    xmlstr = request.form['SAMLResponse']
    session_info = get_authn_response(current_app.config, session, xmlstr)
    logger.debug('Trying to locate the user authenticated by the IdP')

    user = authenticate(current_app, session_info)
    if user is None:
        logger.error('Could not find the user identified by the IdP')
        raise HTTPUnauthorized("Access not authorized")

    action = get_action()
    return action(session_info, user)


#!/usr/bin/env python3

import os

COUNTER = 0

def automodule(module_path, module_name):
    result = module_name + "\n"

    for i in range(0, len(module_name)):
        result += "-"

    result += "\n\n.. automodule:: " + module_path + "\n"
    result += "\t:members:\n\t:undoc-members:\n\t:show-inheritance:\n\n"
    return bytes(result, "UTF-8")

def create_title(name):
    result = name + "\n"

    for i in range(0, len(name)):
        result += "="

    result += "\n\n"
    return bytes(result, "UTF-8")

def walk(file, url, rst_url):
    for f in os.listdir(url):
        if os.path.isfile(os.path.join(url, f)):
            if f.endswith(".py") and f != "__init__.py":
                name = f.split(".")[0]
                file.write(automodule(rst_url + "." + name, name))
        else:
            if not f == "__pycache__":
                walk(create_subsection(file, f), url + "/" + f, rst_url + "." + f)

def dir_autogen(file, title, url, rst_url):
    file.write(create_title(title))
    exact_url = "./../../" + url
    walk(file, exact_url, rst_url)

def create_subsection(file, title):
    file.write(bytes(".. toctree::\n\n", "UTF-8"))

    global COUNTER
    COUNTER += 1
    sub_file_name = "subsection" + str(COUNTER)
    file.write(bytes("\t" + sub_file_name + "\n\n", "UTF-8"))

    sub_file = open("rst_files/" + sub_file_name + ".rst", "wb+")

    sub_file.write(create_title(title))

    return sub_file

file = open("rst_files/Autogen.rst", "wb+")

dir_autogen(file, "model", "model", "model")
dir_autogen(file, "view", "view", "view")
dir_autogen(file, "controller", "controller", "controllers")

os.system("make clean")
os.system("make html")


import unittest
from answer.number_of_rectangles_that_can_form_the_largest_square import Solution

class TestSolution(unittest.TestCase):

    def setUp(self):
        self.rectangles = [
            [[5, 8], [3, 9], [5, 12], [16, 5]],
            [[2, 3], [3, 7], [4, 3], [3, 7]],
        ]
        self.answers = [
            3,
            3,
        ]

    def solution(self, i):
        s = Solution()
        result = s.countGoodRectangles(self.rectangles[i])
        self.assertEqual(self.answers[i], result)

    def test_solution0(self):
        self.solution(0)

    def test_solution1(self):
        self.solution(1)

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


# Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: miha@reciprocitylabs.com
# Maintained By: miha@reciprocitylabs.com

import unittest
from mock import patch

from ggrc import app  # noqa
from ggrc import notification

class TestNotificationsInit(unittest.TestCase):

  @patch("ggrc.notification.get_filter_data")
  def test_get_notification_data(self, get_filter_data):
    """ Test that data does not contain empty emails """

    get_filter_data.return_value = {
        "email@example.com": {},
        "": {},
    }
    notifications = notification.get_notification_data([1, 2])
    self.assertIn("email@example.com", notifications)
    self.assertNotIn("", notifications)


## Shark.py
#	Authors: Wouter Vrielink, Vincent Hagen
#	Date   : 15-01-2014
#
#	Implements the shark agent
##

from Model import Agent, norm
import numpy as np
import random

class Shark(Agent):
	""" Shark agent """
	sight = 15 # Sight in which to spot fish
	eatRange = 2 # The range in which fish get eaten
	colorCode = 3 # The color the shark has in the image
	speed = 1
	speed1 = 1 # normal speed
	speed2 = 2 # speed when a fish is in range
	speed2Range = 5 # how close needs a fish to be for the shark to speed up
	def __init__(self, m, pos, d=None):
		""" Initializes the shark. """
		super(Shark, self).__init__(m, m.sharkMap, pos)
		self.map = m

		# Set the direction
		if d == None:
			d = self.randomDirection()

		self.direction = norm(d)

		self.notMoved = 0

		self.map.allShark.append(self)

	def getFishes(self, n):
		""" Gets and uses a mask with fishes within a range given. """
		mask = self.model.getMask( (self.iX, self.iY), n, self.direction)

		return self.model.fishMap[mask]

	def step(self):
		""" Steps the shark, same as the fish but without the schooling
			and they hunt on fish instead of algae """

		if self.notMoved > 3:
			self._direction = self.randomDirection()
		else:
			d = self.direction + self.reactToWall() / 8

			n = [f for f in self.getFishes(self.sight) if f != 0]
			if (n != []):
				poss = [[f.x,f.y] for f in n]
				# degs = [f.direction for f in n]
				d += self.closest_neighbour_position(poss) * 2 # + \
									# self.mean_direction(degs)
			else:
				self.speed = 1
			self._direction = norm(d)

		while(True):
			dx, dy = self.speed * self._direction
			if(self.map.boundCheck(self.x + dx, self.y + dy)):
				break
			self._direction = self.randomDirection()

		self._x = self.x + dx
		self._y = self.y + dy

		self._iX = int(self._x)
		self._iY = int(self._y)

	def update(self):
		""" Update the fish. Make sure first that the fish can actually move to
			the square it wants to move to. """
		if not self.canUpdateTo(self._iX, self._iY):
			return False;

		m  = self.map.sharkMap

		m[self.iX, self.iY] = 0

		self.x = self._x
		self.y = self._y

		self.iX = self._iX
		self.iY = self._iY

		self.direction = self._direction

		m[self.iX, self.iY] = self
		self.notMoved = 0

		for r in range(0, self.eatRange + 1):
			fp = [f for f in self.getFishes(self.eatRange) if f != 0]

			length = len(fp)
			if length > 0:
				rand = random.randint(0, length - 1)
				self.model.data["fishEaten"] += 1
				fp[rand].destroy()
				return True

		return True
	def closest_neighbour_position(self, poss):
		closest = poss[0]
		closestDist = (poss[0][0] - self.x) ** 2 + (poss[0][1] - self.y) ** 2
		for pos in poss:
			dist = (pos[0] - self.x) ** 2 + (pos[1] - self.y) ** 2
			if (dist < closestDist):
				closestDist = dist
				closest = pos

		self.speed = self.speed2 if closestDist <= self.speed2Range ** 2 else self.speed1

		return norm(closest - np.array([self.x, self.y]))

	def mean_neighbours_position(self, poss):
		""" Calculates the average position of the neighbours. """
		avg_pos = np.mean(poss, axis=0)
		return norm(avg_pos - np.array([self.x, self.y]))
	def mean_direction(self, dirs):
		""" Sums the vectors of the given degrees and normalizes. """
		return norm(np.sum(dirs, axis=0))

	def destroy(self):
		""" Kills the shark """
		self.model.sharkMap[self.iX, self.iY] = 0
		self.model.allShark.remove(self)


from app.keys_generator.keys_manager.elgamal_keys import ElGamalKeysManager
from app.keys_generator.keys_manager.rsa_keys import RSAKeysManager
from app.keys_generator.prime import Prime
from os import path

path_data = path.join(path.abspath(path.dirname(__file__)), '../../data/')
path_primes = path.join(path_data, 'primes/')

def generate_elgamal_keys():
    """
    CLI to generate an ElGamal key pair
    :return:
    """

    print("Génération d'un couple de clés ElGamal")

    # Prime number
    print("Fichier du nombre premier à utiliser. Si inexistant, il sera généré. (prime_default.txt par défaut)")
    prime_filename = input()

    if prime_filename == 'q':
        return

    if prime_filename == '':
        prime_filename = 'prime_default.txt'

    # Keys
    print("Nom des fichiers des clés publique / privée à utiliser. Si inexistant, il sera généré. (key_default.txt "
          "par défaut)")
    keys_filename = input()

    if keys_filename == 'q':
        return

    if keys_filename == '':
        keys_filename = 'key_default.txt'

    prime = Prime(path.join(path_primes, prime_filename))
    key_mngmt = ElGamalKeysManager(keys_filename, prime.get_prime(), prime.get_generator())

    print("Clé publique")
    print(key_mngmt.get_public_key())
    print("Clé privée")
    print(key_mngmt.get_private_key())

def generate_rsa_keys():
    """
    CLI to generate an RSA key pair
    :return:
    """

    print("Génération d'un couple de clés RSA")

    # Prime number p
    print("Fichier du nombre premier p à utiliser. Si inexistant, il sera généré. (prime_p.txt par défaut)")
    prime_p_filename = input()

    if prime_p_filename == 'q':
        return

    if prime_p_filename == '':
        prime_p_filename = 'prime_p.txt'

    # Prime number q
    print("Fichier du nombre premier q à utiliser. Si inexistant, il sera généré. (prime_q.txt par défaut)")
    prime_q_filename = input()

    if prime_q_filename == 'q':
        return

    if prime_q_filename == '':
        prime_q_filename = 'prime_q.txt'

    # Keys
    print("Nom des fichiers des clés publique / privée à utiliser. Si inexistant, il sera généré. (key_rsa_default.txt "
          "par défaut)")
    keys_filename = input()

    if keys_filename == 'q':
        return

    if keys_filename == '':
        keys_filename = 'key_rsa_default.txt'

    prime_p = Prime(path.join(path_primes, prime_p_filename), with_generator=False)
    prime_q = Prime(path.join(path_primes, prime_q_filename), with_generator=False)
    key_mngmt = RSAKeysManager(keys_filename, [prime_p.get_prime(), prime_q.get_prime()])

    print("Clé publique")
    print(key_mngmt.get_public_key())
    print("Clé privée")
    print(key_mngmt.get_private_key())

def dh_key_exchange():
    """
    CLI to perform a Diffie-Hellman key exchange
    :return:
    """

    # Prime number
    print("Fichier du nombre premier à utiliser. Si inexistant, il sera généré. (prime_default.txt par défaut)")
    prime_filename = input()

    if prime_filename == 'q':
        return

    if prime_filename == '':
        prime_filename = 'prime_default.txt'

    # Keys Alice
    print("Nom des fichiers des clés publique / privée d'Alice. Si inexistant, il sera généré. (key_alice.txt "
          "par défaut)")
    keys_a_filename = input()

    if keys_a_filename == 'q':
        return

    if keys_a_filename == '':
        keys_a_filename = 'key_alice.txt'

    # Keys Bob
    print("Nom des fichiers des clés publique / privée de Bob. Si inexistant, il sera généré. (key_bob.txt "
          "par défaut)")
    keys_b_filename = input()

    if keys_b_filename == 'q':
        return

    if keys_b_filename == '':
        keys_b_filename = 'key_bob.txt'

    prime = Prime(path.join(path_primes, prime_filename))
    key_mgmt_a = ElGamalKeysManager(keys_a_filename, prime.get_prime(), prime.get_generator())
    key_mgmt_b = ElGamalKeysManager(keys_b_filename, prime.get_prime(), prime.get_generator())
    shared_key_a = key_mgmt_a.generate_shared_key(key_mgmt_b.get_public_key())
    shared_key_b = key_mgmt_b.generate_shared_key(key_mgmt_a.get_public_key())

    print("Alice:")
    print("Clé publique : ", key_mgmt_a.get_public_key())
    print("Clé privée : ", key_mgmt_a.get_private_key())
    print("Clé secrète partagée : ", shared_key_a)

    print("\nBob:")
    print("Clé publique : ", key_mgmt_b.get_public_key())
    print("Clé privée : ", key_mgmt_b.get_private_key())
    print("Clé secrète partagée : ", shared_key_b)

    if shared_key_a == shared_key_b:
        print("\nLes clés secrètes sont égales!")
    else:
        print("\nLes clés secrètes ne correspondent pas!")

available_cmds = {
    1: generate_elgamal_keys,
    2: generate_rsa_keys,
    3: dh_key_exchange
}

def generate_keys():
    """
    CLI to choose which action to do in Keys generator
    :return:
    """
    while True:
        print("Génération de couples de clés publiques / privées")
        print("->1<- Générer un couple de clés ElGamal")
        print("->2<- Générer un couple de clés RSA")
        print("->3<- Partage de clé privé Diffie-Hellman")
        cmd = input()

        if cmd == 'q':
            break

        cmd_nb = 0
        try:
            cmd_nb = int(cmd)
        except ValueError:
            print('La commande entrée doit être un nombre')

        if cmd_nb in available_cmds:
            func_to_run = available_cmds.get(cmd_nb)
            func_to_run()
            break
        else:
            print("La commande n'est pas disponible")

def all():
    """
    Performs all possible options of keys generator
    :return:
    """

    # Keys generation
    print("Génération d'un couple de clés ElGamal publique / privée")
    prime = Prime(path.join(path_primes, "prime_default.txt"))
    key_mngmt = ElGamalKeysManager("key_default.txt", prime.get_prime(), prime.get_generator())
    print("\nClé publique :")
    print(key_mngmt.get_public_key())
    print("Clé privée :")
    print(key_mngmt.get_private_key())

    print("\nGénération d'un couple de clés RSA publique / privée")
    prime_p = Prime(path.join(path_primes, "prime_p.txt"))
    prime_q = Prime(path.join(path_primes, "prime_q.txt"))
    key_mngmt = RSAKeysManager("key_rsa_default.txt", [prime_p.get_prime(), prime_q.get_prime()])
    print("\nClé publique :")
    print(key_mngmt.get_public_key())
    print("Clé privée :")
    print(key_mngmt.get_private_key())

    # Diffie-Hellman key exchange
    print("\nPartage de clé secrète Diffie-Hellman")
    key_mgmt_a = ElGamalKeysManager("key_alice.txt", prime.get_prime(), prime.get_generator())
    key_mgmt_b = ElGamalKeysManager("key_bob.txt", prime.get_prime(), prime.get_generator())
    shared_key_a = key_mgmt_a.generate_shared_key(key_mgmt_b.get_public_key())
    shared_key_b = key_mgmt_b.generate_shared_key(key_mgmt_a.get_public_key())

    print("Alice:")
    print("Clé publique : ", key_mgmt_a.get_public_key())
    print("Clé privée : ", key_mgmt_a.get_private_key())
    print("Clé secrète partagée : ", shared_key_a)

    print("\nBob:")
    print("Clé publique : ", key_mgmt_b.get_public_key())
    print("Clé privée : ", key_mgmt_b.get_private_key())
    print("Clé secrète partagée : ", shared_key_b)

    if shared_key_a == shared_key_b:
        print("\nLes clés secrètes sont égales!\n")
    else:
        print("\nLes clés secrètes ne correspondent pas!\n")


import os
import sys
import unittest

import pytorch_lightning as pl
import pytorch_lightning.loggers
from pytorch_lightning.utilities.parsing import AttributeDict

from deep_depth_transfer import PoseNetResNet, DepthNetResNet, UnsupervisedCriterion
from deep_depth_transfer.data import KittiDataModuleFactory
from deep_depth_transfer.models import ScaledUnsupervisedDepthModel
from test.data_module_mock import DataModuleMock

if sys.platform == "win32":
    WORKERS_COUNT = 0
else:
    WORKERS_COUNT = 4

class TestUnsupervisedDepthModel(unittest.TestCase):
    def setUp(self) -> None:
        current_folder = os.path.dirname(os.path.abspath(__file__))
        dataset_folder = os.path.join(os.path.dirname(current_folder), "datasets", "kitti")
        data_module_factory = KittiDataModuleFactory(range(0, 301, 1), directory=dataset_folder)
        self._data_module = data_module_factory.make_dataset_manager(
            final_image_size=(128, 384),
            transform_manager_parameters={"filters": True},
            batch_size=1,
            num_workers=WORKERS_COUNT,
            split=(0.8, 0.1, 0.1)
        )
        self._data_module = DataModuleMock(self._data_module)

        pose_net = PoseNetResNet()
        depth_net = DepthNetResNet()
        criterion = UnsupervisedCriterion(self._data_module.get_cameras_calibration(), 1, 1)

        params = AttributeDict(
            lr=1e-3,
            beta1=0.99,
            beta2=0.9,
            scale_lr=1e-3,
            initial_log_scale=0.,
            initial_log_min_depth=0.,
            initial_log_pose_scale=0.,
        )
        self._model = ScaledUnsupervisedDepthModel(params, pose_net, depth_net, criterion).cuda()

    def test_unsupervised_depth_model(self):
        tb_logger = pl.loggers.TensorBoardLogger('logs/')
        trainer = pl.Trainer(logger=tb_logger, max_epochs=1, gpus=1, progress_bar_refresh_rate=20)
        trainer.fit(self._model, self._data_module)


import py5

CUT_STROKE, FOLD_STROKE = py5.color(255, 0, 0), py5.color(0, 0, 255)

def frame_box(w, h, d, thick=0):
    """ draw the 3D version of the box with rectangular holes """
    mw, mh, md = w / 2., h / 2., d / 2.
    py5.translate(0, 0, -md)  # base
    face(0, 0, w, h, thick)
    py5.translate(0, 0, d)  # top
    face(0, 0, w, h, thick)
    py5.translate(0, 0, -md)  # back to 0
    py5.rotate_y(py5.HALF_PI)
    py5.translate(0, 0, -mw)  # left side
    face(0, 0, d, h, thick)
    py5.translate(0, 0, w)  # right side
    face(0, 0, d, h, thick)
    py5.translate(0, 0, -mw)  # back to middle
    py5.rotate_y(-py5.HALF_PI)  # back to 0 rotation
    py5.rotate_x(py5.HALF_PI)
    py5.translate(0, 0, -mh)  # lateral e
    face(0, 0, w, d, thick)
    py5.translate(0, 0, h)  # lateral d
    face(0, 0, w, d, thick)
    py5.translate(0, 0, -mw)  # reset translate
    py5.rotate_x(-py5.HALF_PI)  # reset rotate

def face(x, y, w, h, thick):
    mw, mh = w / 2., h / 2.
    py5.push_matrix()
    py5.translate(x, y)
    py5.begin_shape()
    py5.vertex(-mw, -mh)
    py5.vertex(+mw, -mh)
    py5.vertex(+mw, +mh)
    py5.vertex(-mw, +mh)
    if thick > 0 and mw - thick > 0 and mh - thick > 0:
        mw -= thick
        mh -= thick
        py5.begin_contour()  # counterclockwise hole
        py5.vertex(-mw, -mh)
        py5.vertex(-mw, +mh)
        py5.vertex(+mw, +mh)
        py5.vertex(+mw, -mh)
        py5.end_contour()
    py5.end_shape(py5.CLOSE)
    py5.pop_matrix()

def unfolded_frame_box(w, h, d, thick=0, draw_main=True):
    mw, mh, md = w / 2., h / 2., d / 2.
    unfolded_face(0, -h - md, w, d, "aaan", thick, draw_main)
    unfolded_face(0, -mh, w, h, "vvvv", thick, draw_main)
    unfolded_face(0, -mh + mh + md, w, d, "cncv", thick, draw_main)
    unfolded_face(0, +mh + d, w, h, "cncc", thick, draw_main)
    unfolded_face(-mw - md, -mh, d, h, "acna", thick, draw_main)
    unfolded_face(mw + md, -mh, d, h, "ncaa", thick, draw_main)

def unfolded_face(x, y, w, h, edge_types, thick=0, draw_main=True):
    e0, e1, e2, e3 = edge_types
    mw, mh = w / 2., h / 2.
    py5.push_matrix()
    py5.translate(x, y)
    if draw_main:
        edge(-mw, +mh, -mw, -mh, e0)
        edge(-mw, -mh, +mw, -mh, e1)
        edge(+mw, -mh, +mw, +mh, e2)
        edge(+mw, +mh, -mw, +mh, e3)
    if thick > 0 and mw - thick > 0 and mh - thick > 0:
        unfolded_face(0, 0, w - thick * 2, h - thick * 2, "cccc")
    py5.pop_matrix()

def edge(x0, y0, x1, y1, edge_type):
    if edge_type == "n":  # no edge is drawn
        return
    elif edge_type == "c":  # cut stroke selected
        py5.stroke(CUT_STROKE)
    else:
        py5.stroke(FOLD_STROKE)  # fold stroke selected for "v" and "a"
    py5.line(x0, y0, x1, y1)    # line drawn here
    if edge_type == "a":    # tab (note a fold-stroke line was already drawn)
        py5.stroke(CUT_STROKE)
        py5.no_fill()
        glue_tab((x0, y0), (x1, y1), 10)

def glue_tab(p1, p2, tab_w, cut_ang=py5.QUARTER_PI / 3):
    """
    draws a trapezoidal or triangular glue tab along edge defined by p1 and p2,
    with width tab_w and cut angle a
    """
    al = py5.atan2(p1[0] - p2[0], p1[1] - p2[1])
    a1 = al + cut_ang + py5.PI
    a2 = al - cut_ang
    # calculate cut_len to get the right tab width
    cut_len = tab_w / py5.sin(cut_ang)
    f1 = (p1[0] + cut_len * py5.sin(a1),
          p1[1] + cut_len * py5.cos(a1))
    f2 = (p2[0] + cut_len * py5.sin(a2),
          p2[1] + cut_len * py5.cos(a2))
    edge_len = py5.dist(p1[0], p1[1], p2[0], p2[1])

    if edge_len > 2 * cut_len * py5.cos(cut_ang):    # 'normal' trapezoidal tab
        py5.begin_shape()
        py5.vertex(*p1)    # vertex(p1[0], p1[1])
        py5.vertex(*f1)
        py5.vertex(*f2)
        py5.vertex(*p2)
        py5.end_shape()
    else:    # short triangular tab
        fm = ((f1[0] + f2[0]) / 2, (f1[1] + f2[1]) / 2)
        py5.begin_shape()
        py5.vertex(*p1)
        py5.vertex(*fm)    # middle way of f1 and f2
        py5.vertex(*p2)
        py5.end_shape()


import pandas as pd
import os
import matplotlib.pyplot as plt
import datetime

class Picture(object):

    def __init__(self, path):
        self.id_list=self.getFileNames(path)
        self.draw(self.id_list)

    def getFileNames(self,path):
        list = os.listdir(path)
        return list

    def draw(self, id_list):
        for id in id_list:
            df = self.getDataframe(id)
            start=df.index[0]
            start = start + datetime.timedelta(days=10)
            end= start + datetime.timedelta(days = 10)
            plt.figure()
            df.plot(title=id.split(".")[0],xlim=[start,end])
            plt.savefig("pictures/" + id.split(".")[0])
            plt.close()

    def getDataframe(self, id):
        df = pd.read_csv("parts/" + id)
        df["timestamp"]=pd.to_datetime(df["timestamp"]*1000000000)
        df.set_index("timestamp", inplace=True)
        df1 = df["value"]
        return df1

if __name__=='__main__':

    interval=Picture("parts")


from django.conf.urls import url
from . import views

urlpatterns = [
	url(r'^$', views.parliament, name='parliament'),
    url(r'^Committee/$', views.committee, name='committee'),
    url(r'^Elections/$', views.elections, name='elections'),
    url(r'^Minutes/$', views.minutes, name='minutes'),
    url(r'^Feedback/$', views.feedback, name='feedback'),
]


from rest_framework import serializers

from departments.models import Employee, Department

class DepartmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Department
        fields = (
            'name',
        )

class EmployeeSerializer(serializers.ModelSerializer):
    department = DepartmentSerializer(many=False, read_only=True)

    class Meta:
        model = Employee
        fields = (
            'id',
            'department',
            'full_name',
            'email',
        )


#-------------------------------------------------------------------------------
# Name:        POXLSParser
# Purpose:     JS Wright Purchase Order Parser for Tradex Update
#
# Author:      Raju.Surapuraju
#
# Created:     04/01/2019
# Copyright:   (c) Raju.Surapuraju 2018
# Licence:     Causeway Technologies Limited
#-------------------------------------------------------------------------------

import re
import os
import configparser

settings = configparser.ConfigParser()
settings._interpolation = configparser.ExtendedInterpolation()
settings.read('C:\\config\\config.ini')
varciteXMLInput = settings.get('InputData', 'citeXMLInput')
varciteCSV = settings.get('InputData', 'citeCSV')

citeXML = varciteXMLInput
citeCSV = varciteCSV

if os.path.exists(citeCSV):
  os.remove(citeCSV)
else:
  print("citeCSV file does not exist")

def parseXML():
    with open(citeXML) as cxFH:
        while True:
            line = cxFH.readline()
            line = str(line)
            invoiceNumStr = re.search('DocNumber="(.+?)" DocType=', line)
            catalogueNumberStr = re.search('E.7140.ItemNumber.D="(.+?)"/', line)
            catalogueLineNumberStr = re.search('E.1082.LineItemNumber.D="(.+?)">', line)
            catalogueLineQuantityStr = re.search('E.6060.Quantity.D="(.+?)"', line)
            if invoiceNumStr:
                invoiceNum = invoiceNumStr.group(1)
                print ("Invoice Number:" + invoiceNum)

            if catalogueNumberStr:
                catalogueNumber = catalogueNumberStr.group(1)
                print (" Catelogue Number:" + catalogueNumber)

            if catalogueLineNumberStr:
                catalogueLineNumber = catalogueLineNumberStr.group(1)
                print (" Catalogue Line Number:" + catalogueLineNumber)

            if catalogueLineQuantityStr:
                catalogueLineQuantity = catalogueLineQuantityStr.group(1)
                print (" Catalogue Line Quantity:" + catalogueLineQuantity)
                print ()

                csvString =  (invoiceNum + ',' +  catalogueNumber + ','  + catalogueLineNumber + ',' + catalogueLineQuantity)
                csvFH = open(citeCSV, "a")
                csvFH.write(csvString + '\n')
                csvFH.close()

            if not line:
                break
        cxFH.close()

def main():
    parseXML()

if __name__ == '__main__':
    main()


import time
import random
import os
import requests
from colorama import *

init(autoreset=True)

class jg:
	names = ["Миша", "Аимбет", "Хакеры из ананимус", "Дова", "Клайд", "Влад гей","когда миксед", "я люблю сосать член","пуська", "бля", "твоя мама гей", "сука", "бля"]
	def helloWorld():
		print(Fore.CYAN + 'WEBHOOK FLOOD')
		print("github.com/jackglow/webhookspam")
	def run(urmsg):
		hf = open("hooks.txt", "r")
		hooks = hf.read()
		hf.close()
		totalhooks = 0
		hooks = hooks.split('\n')
		for hook in hooks:
			totalhooks += 1
			print(Fore.YELLOW+"Loaded "+str(totalhooks)+" webhook in total...")
		print(Fore.GREEN + "Webhooks loaded.")
		print(Fore.YELLOW + "Running message (30) send...")
		for x in range(0,30):
			for hook in hooks:
				r = requests.post(hook, data={'username': random.choice(jg.names), 'content': urmsg, "avatar_url": "https://avatars3.githubusercontent.com/u/13962537?s=460&v=4"})

jg.helloWorld()
print(Fore.YELLOW + "Enter message> ", end="")
f = input()
jg.run(f)


from detect_text import parse_screenshot, compress
from google_search import run_query_all
import os
import glob
import time
from watchdog.events import FileSystemEventHandler

compressed = True

class ParseSearchHandler(FileSystemEventHandler):

    def on_created(self, event):
        start = time.time()
        # by default, mac saves ss on desktop
        all_img = glob.glob('/Users/jpchen/Desktop/*.png')
        # get the last ss
        latest_img = max(all_img, key=os.path.getctime)
        if compressed:
            latest_img = compress(latest_img)
        try:
            q_and_a = parse_screenshot(latest_img, should_launch=True, compressed=compressed)
            question, answer = q_and_a['question'], q_and_a['answers']
            negative_q = ' not ' in question.lower() or "isn ' t " in question.lower() or ' never ' in question.lower()
            answer = run_query_all(question, answer, is_negative=negative_q)
        except:
            pass
        end = time.time()
        try:
            # read aloud the answer
            os.system('say "{} is the most likely answer."'.format(answer))
            print('Elapsed wall time: {} seconds', end - start)
        except:
            # something went wrong
            os.system('say fuck if i know')


# -*- coding:utf-8 _*-
"""
@author:Administrator
@file: predict_data_process.py
@time: 2018/9/11
"""
import pandas as pd
pd.set_option('display.column',100)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
import missingno as msno
import matplotlib.pyplot as plt

data = pd.read_csv('./predict_data.csv')

# msno.bar(data)
# plt.show()

print(data.shape)
data = data.dropna()
print(data.shape)

def process_bedrooms(data):
    list_month = list(data['bedrooms'].astype('str'))
    list_month_process = []
    for i in list_month:
        if i != 'nan':
            list_month_process.append(eval(i))
        else:
            list_month_process.append(i)
    data['bedrooms'] = pd.Series(list_month_process)
    data['bedrooms'] = data['bedrooms'].astype('float')
    return data

# 处理bedrooms(eval)
data = process_bedrooms(data)
print(data.head(100))
data = data.drop()

data.to_csv('predict_data_dropna.csv',index=False)


from tdigest import TDigest

def calculateQuantiles(col, min_tdigest_items):
    digest = TDigest()

    for i in range(min_tdigest_items):
     digest.update(col[i])

    quantiles = []
    for i in range(len(col)):
        val = col[i]
        c = digest.cdf(val)
        quantiles.append(c)
        if i >= min_tdigest_items:
            digest.update(val)

    return quantiles

def calculateQuantilesForDf(df, min_tdigest_items, cols_to_skip):
    cols = list(df.columns)
    cols_to_process = [x for x in cols if (x in cols_to_skip) == False]
    for x in cols_to_process:
        print("Processing " + x)
        df[x + "_q"] = calculateQuantiles(df[x], min_tdigest_items)


from Tabuleiro import createTab
from Tabuleiro import createTabInterno
from Tabuleiro import showTab
from Controle import entrada
from Regras import inverteTurno
from Regras import quemJoga
from Regras import checkTrilha
from Regras import contaPontos
import Ranking
import RegrasUsuario
def startGame():
	vencedor = False
	tab = createTab()
	tabInterno = createTabInterno()
	player = quemJoga()
	saudacao(player)
	#temporario
	#player = "joseildo"
	jogadores = ["PC",player]
	turno = "PLAYER"

	while not vencedor:
		showTab(tab)
		vencedor = checkTrilha(tabInterno)

		if not vencedor:
			tab = entrada(tab,turno,tabInterno)
			turno = inverteTurno(turno,jogadores)
			if tab.upper() == "SAIR":
				vencedor = "NIGUEM"
		elif vencedor == "NIGUEM":
			print("OS DOIS VENCERAM")
		else:
			pontos = contaPontos(vencedor,tab)
			chamaVencedor(vencedor[0],pontos,jogadores)

def saudacao(nome):
	print("Seja bem vindo(a) "+nome)
def chamaVencedor(vencedor,pontos,jogadores):
	if vencedor == "■":
		venceu = jogadores[0]
	else:
		venceu = jogadores[1]
	Ranking.gravaEmRanking(venceu,pontos)
	print("Parabens, "+venceu+" voce venceu com ",pontos," pontos")
def menu():
	opcoes = ["jogar","regras","ranking","sair"]

	escolha = input("""\nomeEscolha entre :
	Entre : JOGAR--REGRAS--RANKING--SAIR : """)

	while (escolha.lower() != opcoes[3]):
		if (escolha.lower() == opcoes[0]):
			startGame()
		elif(escolha.lower() == opcoes[1]):
			RegrasUsuario.regras()
		elif(escolha.lower() == opcoes[2]):
			Ranking.exibeRanking()
		if(escolha.lower() not in opcoes)and(escolha.lower != opcoes[3]):
			escolha = input("""\nVoce fez uma opçao que nao é possivel porfavor faça outra
	Entre : JOGAR--REGRAS--RANKING--SAIR : """)
		elif(escolha.lower() in opcoes)and(escolha.lower != opcoes[3]):
			escolha = input("""\nEscolha uma das opções:
	Entre : JOGAR--REGRAS--RANKING--SAIR : """)


"""
### CODE OWNERS: Shea Parkes, Kyle Baird

### OBJECTIVE:
  Prove I can connect.

### DEVELOPER NOTES:
  <none>
"""

from fhirclient import client

# =============================================================================
# LIBRARIES, LOCATIONS, LITERALS, ETC. GO ABOVE HERE
# =============================================================================

settings = {
    'app_id': 'prm_analytics',
    'api_base': 'https://open-ic.epic.com/FHIR/api/FHIR/DSTU2'
}
epic = client.FHIRClient(settings=settings)

import fhirclient.models.patient as p
patient = p.Patient.read('Tbt3KuCY0B5PSrJvCu2j-PlK.aiHsu2xUjUM8bWpetXoB', epic.server)
patient.birthDate.isostring

epic.human_name(patient.name[0])


import os
import sys
import numpy as np
from datetime import datetime
import time

"gzip file must be read and write in binary/bytes"
def myopenfile(fnm, mode):
    f = None
    if 'w' in mode:
        if isgzfile(fnm):
            import gzip
            mode = mode+'b' if 'b' != mode[-1] else mode
            f = gzip.open(fnm, mode)
        else:
            f = open(fnm, mode)
    else:
        if 'r' not in mode and 'a' not in mode:
            mode = 'r' + mode
        if os.path.isfile(fnm):
            if not isgzfile(fnm):
                f = open(fnm, mode)
            else:
                import gzip
                mode = 'rb'
                f = gzip.open( fnm, mode )
        elif os.path.isfile(fnm+'.gz'):
            'file @fnm does not exists, use fnm.gz instead'
            print(
                '==file {} does not exists, read {}.gz instead'.format(fnm,
                                                                       fnm))
            import gzip
            mode = 'rb'
            f = gzip.open(fnm+'.gz', mode)
        else:
            print('file: {} or its zip file does NOT exist'.format(fnm))
            sys.exit(1)
    return f

def isgzfile( filename ):
    return filename.endswith(".gz")

# delim must be str
def saveSimpleDictData(simdict, outdata, delim=':', mode='w'):
    if isgzfile(outdata):
        mode = mode + 'b' if mode[-1] != 'b' else mode
    # write bytes
    ib = True if 'b' in mode else False

    with myopenfile(outdata, mode) as fw:
        for k, val in simdict.items():
            k = k.decode() if isinstance(k, bytes) else str(k)
            val = val.decode() if isinstance(val, bytes) else str(val)
            outstr = "{}{}{}\n".format(k, delim,  val)
            x = outstr.encode() if ib else outstr
            fw.write(x)
        fw.close()

def loadSimpleDictData(indata, delim=':', mode='r', dtypes=[int, int]):
    if isgzfile(indata):
        mode = 'rb'
    # bytes
    ib = True if 'b' in mode else False

    simdict={}
    with myopenfile(indata, mode) as fr:
        for line in fr:
            line = line.decode() if ib else str(line)
            line = line.strip().split(delim)
            ktype, vtype = dtypes
            simdict[ktype(line[0])] = vtype(line[1])
        fr.close()
    return simdict

def saveDictListData(dictls, outdata, delim=':', mode='w'):
    if isgzfile(outdata):
        'possible mode is ab'
        mode = mode + 'b' if mode[-1] != 'b' else mode
    # write bytes
    ib = True if 'b' in mode else False

    with myopenfile(outdata, mode) as fw:
        i=0
        for k, l in dictls.items():
            if not isinstance(l,(list, np.ndarray)):
                print("This is not a dict of value list.", type(l))
                break
            if len(l)<1:
                continue
            k = k.decode() if isinstance(k, bytes) else str(k)
            ostr = "{}{}".format(k,delim)
            if len(l)<1:
                i += 1
                continue
            l = [ x.decode() if isinstance(x,bytes) else str(x) for x in l ]
            ostr = ostr + " ".join(l) + '\n'
            fw.write(ostr.encode() if ib else ostr)
        fw.close()
        if i > 0:
            print( "Warn: total {} empty dict lists are removed".format(str(i)) )

def loadDictListData(indata, ktype=str, vtype=str, delim=':', mode='r'):
    if isgzfile(indata):
        mode = 'rb'
    # bytes
    ib = True if 'b' in mode else False

    dictls={}
    with myopenfile(indata, mode) as fr:
        for line in fr:
            line = line.decode() if ib else line
            line = line.strip().split(delim)
            lst=[]
            for e in line[1].strip().split(' '):
                lst.append(vtype(e))
            dictls[ktype(line[0].strip())]=lst
        fr.close()
    return dictls

def convert_to_db_type(basic_type):
    basic_type_dict = {
        int: "INT",
        str: "TEXT",
        float: "REAL"
    }
    if basic_type in basic_type_dict.keys():
        return basic_type_dict[basic_type]
    else:
        return "TEXT"

def renumberids(indir, outdir, fnm, ofnm, delimeter=' ', comments='#', nodetype=str):
    users, msgs = {}, {}
    if not os.path.exists(outdir):
        os.makedirs(outdir, exist_ok=True)
    filepath = os.path.join(indir, fnm)
    ofilepath = os.path.join(outdir, ofnm)
    with myopenfile(filepath, 'r') as f, myopenfile(ofilepath, 'w') as outf:
        for line in f:
            line = line.strip()
            if line.startswith(comments):
                continue
            elems = line.split(delimeter)
            uid = nodetype(elems[0])
            bid = nodetype(elems[1])
            if uid not in users:
                users[uid] = len(users)
            if bid not in msgs:
                msgs[bid] = len(msgs)
            #t = int(elems[2])
            #label = int(elems[3])
            pos = line.find(delimeter)
            pos = line.find(delimeter, pos+1)
            outf.write('{} {} {}\n'.format(
                users[uid], msgs[bid], line[pos+1:]))
        outf.close()
        f.close()

    with open(os.path.join(outdir, 'userid.dict'), 'w') as f1,\
            open(os.path.join(outdir, 'msgid.dict'), 'w') as f2:
        for u, val in users.items():
            f1.write("{},{}\n".format(u, val))
        for m, val in msgs.items():
            f2.write("{},{}\n".format(m, val))
        f1.close()
        f2.close()
    return len(users), len(msgs)

'''
  @nodetype is the assumed type of node id, which used for saving the space of
  dict keys
  hdfs default delimeter is '\x01'
  If dicts exists , then reuse this dicts, and append new kyes when necessary.
'''
def renumberids2(infiles, outdir, delimeter =' ', isbyte=False,
        comments ='#', nodetype=str, colidx =[0, 1], dicts=None):
# for python >= 3.7
#def renumberids2(infiles, outdir, delimeter:str =' ', isbyte=False,
#        comments:str='#', nodetype=str, colidx:list =[0, 1], dicts:list=None):
    mode = 'b' if isbyte else ''
    numids = len(colidx)
    nodes = []
    if dicts is  None:
        nodes = [ {} for i in range(numids) ]
    elif len(dicts)==numids:
        for i in range(numids):
            if type(dicts[i]) is str:
                nodes.append(loadSimpleDictData(dicts[i], mode=mode,
                        dtypes=[nodetype, int]))
            elif type(dicts[i]) is dict:
                nodes.append(dicts[i])
            else:
                print("Error:\tincorrect type of dicts element: str or dict.")
                print("\tuse empty dict instead")
                nodes.append({})
    else:
        print("Error: incorrect input of dicts, {}".format(dicts))
        sys.exit(1)

    delimeter = delimeter.decode() if type(delimeter) is bytes else \
            str(delimeter)
    comments = comments.decode() if type(comments) is bytes else \
            str(comments)

    #=users, msgs = {}, {}
    if not os.path.exists(outdir):
        os.makedirs(outdir, exist_ok=True)

    #import ipdb; ipdb.set_trace()
    import glob
    files = glob.glob(infiles)
    for filepath in files:
        fnm = os.path.basename(filepath)
        #print('\tprocessing file {}'.format(fnm), flush=True)
        print('\tprocessing file {}'.format(fnm))
        sys.stdout.flush()
        j = fnm.find('.')
        nfnm = fnm[:j]+'.reid'+fnm[j:] if j!=-1 else fnm+'.reid'
        ofilepath = os.path.join(outdir, nfnm)
        with myopenfile(filepath, 'r'+mode) as f, myopenfile(ofilepath,
                                                             'w'+mode) as outf:
            for line in f:
                line = line.decode() if 'b' in mode else line
                line = line.strip()
                if line.startswith(comments):
                    continue
                elems = line.split(delimeter)
                nelems = elems.copy()
                for i in range(numids):
                    nodedict = nodes[i] # e.g. users
                    elemidx = colidx[i]
                    orgid = elems[elemidx].encode() if nodetype is bytes \
                            else nodetype(elems[elemidx])
                    if orgid not in nodedict:
                        nodedict[orgid] = len(nodedict)
                    nelems[elemidx] = str(nodedict[orgid])
                outstr = delimeter.join(nelems) + '\n'
                outx = outstr.encode() if 'b' in mode else outstr
                outf.write(outx)
            outf.close()
            f.close()

    for i in range(numids):
        nodedict = nodes[i]
        elemidx = colidx[i]
        outnm = os.path.join(outdir, "col{}ids.dict".format(elemidx))
        saveSimpleDictData(nodedict, outnm, delim=':', mode='b')
    return [ len(nodes[i]) for i in range(numids) ]

'''
    extract time stamps in log files or edgelist tensor
    @groupids the group col idx used for aggregating timestamps
    '\x01'
'''
def extracttimes(infile, outfile, timeidx=0, timeformat='%Y-%m-%d %H:%M:%S', delimeter=' ',
        isbyte=False, comments='#', nodetype=str, groupids=[]):
    mode = 'b' if isbyte else ''
    aggts = {} # final dict list for aggregating time series.
    import glob
    files = glob.glob(infile)
    for filepath in files:
        fnm = os.path.basename(filepath)
        #print('\tprocessing file {}'.format(fnm), flush=True)
        print('\tprocessing file {}'.format(fnm))
        sys.stdout.flush()
        with myopenfile(filepath, 'r'+mode) as f:
            for line in f:
                line = line.decode() if isinstance(line, bytes) else line
                if line.startswith(comments):
                    continue
                elems = line.split(delimeter)
                # todo: convert time string to ts
                if timeformat != 'int':
                    date = datetime.strptime(elems[timeidx], timeformat)
                    ts = int(time.mktime(date.timetuple()))
                # group by groupid
                if len(groupids) == 1:
                    key = elems[groupids[0]]
                else:
                    key = ','.join(np.array(elems)[groupids])
                if key not in aggts:
                    aggts[key] = []
                aggts[key].append(ts)
    if outfile is not None:
        saveDictListData(aggts, outfile)
    return aggts

'''
time\x01uid\x01...
e.g.: uid:t1,t2,t3
e.g.: uid,msg:t1,t2,t3
'''


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

UNITS = 256
CELL = LSTM
N_LAYERS = 2
DROPOUT = 0.3
LOSS = "mean_absolute_error"
OPTIMIZER = "rmsprop"

def create_model(input_length, units=UNITS, cell=CELL, n_layers=N_LAYERS, dropout=DROPOUT, loss=LOSS,
                 optimizer=OPTIMIZER):

    model = Sequential()

    for i in range(n_layers):
        # define the first layer
        if i == 0:
            model.add(cell(units, return_sequences=True, input_shape=(None, input_length)))
        # define the first layer
        elif i == n_layers - 1:
            model.add(cell(units, return_sequences=False))
        else:
            model.add(cell(units, return_sequences=True))
        # add dropout
        model.add(Dropout(dropout))

    model.add(Dense(1, activation="linear"))
    model.compile(loss=loss, metrics=[loss], optimizer=optimizer)

    return model

# model = create_model(50)
# model.summary()


import os
import tkinter as tk
from datetime import datetime, date
from tkinter import ttk

from tkcalendar import DateEntry

from Database.Sheet import Sheet
from Generator.Module import Module

class MediaSpace(Module):
    def __init__(self, root):
        super(MediaSpace, self).__init__(root)
        self.root.title("Invoice Generator - Media Space")
        self.module_name = 'MediaSpace'
        self.root.geometry('460x635')

    def load_database(self):
        self.SPACES = Sheet('mediaspace_spaces')
        self.ITEMS = Sheet('mediaspace_items')
        self.EXTRAS = Sheet('mediaspace_extras')

    def build_window(self):
        super(MediaSpace, self).place_admin_fields()
        super(MediaSpace, self).place_customer_fields()
        self.place_space_reservation_details()
        super(MediaSpace, self).place_item_reservation_fields()
        super(MediaSpace, self).place_notes()
        self.clear()

    def place_space_reservation_details(self):
        frame = tk.LabelFrame(self.master, text='Reservation Details', font=('Arial', 14), padx=10, pady=10,
                              borderwidth=0, highlightthickness=0)

        for i, text in enumerate(['Selection', 'Day', 'Timings']):
            tk.Label(frame, text=text, font=('Arial', 12), padx=10, width=6).grid(row=0, column=i, padx=10)

        options = []
        for i in range(12):
            str_num = str(i)

            if len(str_num) == 1:
                str_num = '0' + str_num

            options.append(str_num + ':' + '00 am')
            options.append(str_num + ':' + '30 am')

        options.append('12' + ':' + '00 pm')
        options.append('12' + ':' + '30 pm')

        for i in range(1, 12):
            str_num = str(i)

            if len(str_num) == 1:
                str_num = '0' + str_num

            options.append(str_num + ':' + '00 pm')
            options.append(str_num + ':' + '30 pm')

        self.reservation_details = dict()
        for i in range(3):
            item_field = ttk.Combobox(frame, values=self.SPACES.list_items(), width=22)
            item_field.grid(row=i + 1, column=0)

            date_field = DateEntry(frame, width=8, background='black', foreground='white', borderwidth=2)
            date_field.grid(row=i + 1, column=1)

            timing_frame = tk.LabelFrame(frame, borderwidth=0, highlightthickness=0)

            start_time = ttk.Combobox(timing_frame, values=options, width=8)
            end_time = ttk.Combobox(timing_frame, values=options, width=8)

            start_time.grid(row=0, column=1)
            end_time.grid(row=0, column=2)
            timing_frame.grid(row=i + 1, column=2)
            timings = (start_time, end_time)
            self.reservation_details[item_field] = (date_field, timings)

        discount_frame = tk.LabelFrame(frame, borderwidth=0, highlightthickness=0)
        tk.Label(discount_frame, text="Discount", font=('Arial', 10), pady=10, width=6).pack(side=tk.LEFT)

        self.discount = tk.Spinbox(discount_frame, from_=0, to=25, increment=5, wrap=True, width=3)
        self.discount.pack(side=tk.RIGHT, padx=20)

        discount_frame.grid(row=i + 2, column=0, sticky=tk.W)

        frame.pack(padx=20, pady=2, anchor=tk.W)

    def read_inputs(self, *args):

        def convert_to_num(str, isFloat=True):
            test_str = str
            if isFloat:
                test_str = str.replace('.', '', 1)

            if test_str == '':
                return 0
            elif test_str.isnumeric():
                return float(str)
            else:
                return -1

        def reformat(string_date, char='/'):
            date = string_date.split('/')
            for i, entry in enumerate(date):
                if len(entry) == 1:
                    date[i] = '0' + entry

            return char.join(date)

        def generate_col_ids(chars):
            res = []

            for num in range(25, 36):
                entry = []
                for char in chars:
                    entry.append(char + str(num))
                res.append(entry)

            return res

        def separate(string_date, split_char='/'):
            string_date = string_date.split(split_char)
            m, d, y = [int(num) for num in string_date]
            return y, m, d

        admin, customer, company = self.admin.get(), self.customer.get(), self.company.get()
        credit = convert_to_num(self.credit.get())

        discount = convert_to_num(self.discount.get()) / 100

        current_file_count = str(len(os.listdir('Invoices/MediaSpace/')) + 1)
        zeros = ''.join(['0'] * (5 - len(current_file_count.split())))
        invoice_number = zeros + current_file_count

        invoice_date = datetime.date(datetime.now()).strftime("%m/%d/%y")
        days = {'mon': 0, 'tue': 1, 'wed': 2, 'thu': 3, 'fri': 4, 'sat': 5, 'sun': 6}

        billables, reservation_descriptions = [], []
        for space, info in self.reservation_details.items():
            if space.get():
                name = space.get()
                _date, timings = info

                start, end = [timing.get() for timing in timings]

                # seperate time from am/pm
                start_time, start_meridiem = start.split(' ')
                end_time, end_meridiem = end.split(' ')

                start_hr, start_min = [int(num) for num in start_time.split(':')]
                end_hr, end_min = [int(num) for num in end_time.split(':')]

                # reformat timing to 24-hr format for calculations
                if start_meridiem == 'pm' and start_hr != 12:
                    start_hr += 12

                if end_meridiem == 'pm' and end_hr != 12:
                    end_hr += 12

                duration = end_hr - start_hr + end_min / 60 - start_min / 60
                time = start + ' - ' + end

                # Extract quantities
                _, weekday_rate, weekend_rate, unit = self.SPACES[name]

                # Choose rate based on weekday
                display_name = name
                dt = date(*separate(_date.get()))
                if dt.weekday() == days['sun'] or dt.weekday() == days['sat']:
                    rate = weekend_rate
                    display_name = name + ' - Weekend'
                else:
                    rate = weekday_rate

                booking_date = reformat(_date.get())

                # Save info
                billables += [(display_name, duration, unit, float(rate))]
                reservation_descriptions += [(booking_date, time)]

        reserved_items = []
        for item, info in self.table_fields.items():
            if item.get() != '':

                _rate, _qty = info

                rate = convert_to_num(_rate.get())
                qty = convert_to_num(_qty.get(), isFloat=False)

                if rate <= 0 or qty <= 0:
                    raise Exception("Invalid rate or quanitity used")

                item_name = item.get()
                if item_name in self.ITEMS:
                    _, _, unit = self.ITEMS[item_name]
                else:
                    unit = "Item"

                reserved_items += [(item_name, qty, unit, rate)]

        billables += reserved_items

        try:
            if admin == "":
                raise Exception("Insert Admin Name")

            if customer == "":
                raise Exception("Insert Customer Name")

            if company == "":
                raise Exception("Insert Company Name")

            if credit < 0:
                raise Exception("Credit is a positive number")

            if discount < 0 or discount > 1:
                raise Exception("Discount is a number between 0 and 100")

            for name, qty, unit, rate in reserved_items:
                if qty <= 0:
                    raise Exception("Invalid quantity used in item reservation")
                if rate <= 0:
                    raise Exception("Invalid rate used in item reservation")

        except Exception as e:
            return e

        target = 'Invoices/MediaSpace/' + invoice_number + "_" + customer

        cells = {
            'admin': 'A6',
            'name': 'A13',
            'company': 'A14',
            'invoice_date': 'G12',
            'invoice_number': 'G11',
            'due_date': 'G13',
            'reservation_details': [['A18', 'B18'], ['A19', 'B19'], ['A20', 'B20']],
            'table': (generate_col_ids(['A', 'C', 'D', 'F'])),
            'discount': 'F40',
            'credit': 'G43',
            'notes': 'A48'
        }

        # place info
        entries = [(cells['admin'], admin),
                   (cells['name'], customer),
                   (cells['company'], company),
                   (cells['invoice_date'], invoice_date),
                   (cells['invoice_number'], invoice_number),
                   (cells['due_date'], reservation_descriptions[0][0]),
                   (cells['notes'], self.notes.get("1.0", 'end-1c'))]

        for placement, entry in zip(cells['table'], billables):
            for cell, info in zip(placement, entry):
                entries.append((cell, info))

        for i, entry in enumerate(zip(cells['reservation_details'], reservation_descriptions)):
            placement, description = entry
            for cell, info in zip(placement, description):
                entries.append((cell, info))

        hidden_entries = []
        for cell1, cell2 in cells['reservation_details'][i + 1:]:
            hidden_entries.append((cell1, 0))

        hidden_entries.append((cells['credit'], credit))
        hidden_entries.append((cells['discount'], discount))

        return target, entries, hidden_entries

    def clear(self, *args):
        self.admin.delete(0, 'end')
        self.admin.insert(0, 'Omar Eddin')

        self.customer.delete(0, 'end')

        self.company.delete(0, 'end')
        self.company.insert(0, 'Individual')

        self.credit.delete(0, 'end')

        for selection, info_fields in self.reservation_details.items():
            # day, start, end =
            for field in [selection, info_fields[0], *info_fields[1]]:
                field.delete(0, 'end')

        # self.discount.delete(0, 'end')

        for item_field, info_fields in self.table_fields.items():
            for field in [item_field, *info_fields]:
                field.delete(0, 'end')

        self.notes.delete(1.0, 'end')
        self.notes.insert(1.0, 'Please make your payment through Paypal to info@makeplus.us')

if __name__ == '__main__':
    window = tk.Tk()
    module = MediaSpace(window)
    module.start()


from pymongo import MongoClient
from faker import Factory
fake = Factory.create('ru_RU')

dataset1 = []
dataset2 = []

for _ in range(0,150):
  dataset1.append(
    {"name": fake.color_name(),
     "description": fake.text(max_nb_chars=50)}
  )
  dataset2.append(
    {"name": fake.name(),
     "description": fake.text(max_nb_chars=100),
     "tags": []}
  )

client = MongoClient('localhost', 27017)

db = client['boojom']

db.tags.remove()
db.objects.remove()

db.tags.insert(dataset1)
db.objects.insert(dataset2)


import tensorflow as tf
import os
import xml.etree.ElementTree as etree
import numpy as np
import csv
from datetime import datetime
import math
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

NUM_PER_EPOCH_FOR_TRAIN = 24147
NUM_PER_EPOCH_FOR_VAL = 7984
NUM_PER_EPOCH_FOR_TEST = 8007

BATCH_SIZE = 32
IMAGE_SIZE = 112
MOMENTUM = 0.9

MAX_ITERATIONS = 1000000
DISPLAY_STEP = 10
VALIDATION_STEP = 200

NUM_CHECKPOINTS = 144

def input(dataset_path,csv_path,type,dataset_type):
    csv_file = dataset_type+'.csv'

    queue = tf.train.string_input_producer([csv_path + csv_file])
    reader = tf.TextLineReader(skip_header_lines = True)
    _,row = reader.read(queue)
    record_defaults = [[""],[0],[0],[0]]
    image_path,width,height,label = tf.decode_csv(row,record_defaults,field_delim=',')

    image_path = dataset_path + tf.constant('/') + image_path
    label = tf.cast(label,tf.int64)
    width = tf.cast(width,tf.int64)
    height = tf.cast(height,tf.int64)

    image = tf.image.convert_image_dtype(tf.image.decode_jpeg(tf.read_file(image_path),channels = 3),dtype = tf.float32)
    image.set_shape([IMAGE_SIZE,IMAGE_SIZE,3])

    if type == 'train':
        images,labels = tf.train.shuffle_batch([image,label],batch_size = BATCH_SIZE,num_threads = 8,capacity = NUM_PER_EPOCH_FOR_TRAIN + 3*BATCH_SIZE,min_after_dequeue = NUM_PER_EPOCH_FOR_TRAIN)
    elif type == 'validation' or type == 'test':
        num_per_epoch = NUM_PER_EPOCH_FOR_VAL
        if type == 'test':
            num_per_epoch = NUM_PER_EPOCH_FOR_TEST
        images,labels = tf.train.batch([image,label],batch_size = BATCH_SIZE, num_threads = 8, capacity = num_per_epoch)

    return images,labels

def create_variable(name,shape,initializer):
    return tf.get_variable(name,shape,initializer = initializer,dtype = tf.float32)

def conv_layer(name,bottom,shape):
    with tf.variable_scope(name):
        kernal = create_variable('weights',shape,tf.contrib.layers.xavier_initializer_conv2d())
        conv = tf.nn.conv2d(bottom,kernal,[1,1,1,1],padding = 'SAME')
        biases = create_variable('biases',[shape[-1]],tf.contrib.layers.xavier_initializer())
        preactivation = tf.nn.bias_add(conv,biases)
        relu = tf.nn.relu(preactivation,name = name)

        return relu

def ip_layer(name,bottom,top):
    with tf.variable_scope(name):
        shape = bottom.get_shape().as_list()
        dim = np.prod(shape[1:])
        reshape = tf.reshape(bottom,[-1,dim])
        weights = create_variable('weights',[dim,top],tf.contrib.layers.xavier_initializer(uniform = False))
        biases = create_variable('biases',[top],tf.contrib.layers.xavier_initializer())

        return tf.nn.relu(tf.matmul(reshape,weights)+biases,name = name)

def pool_layer(name,bottom):
    return tf.nn.max_pool(bottom,ksize=[1,2,2,1],strides=[1,2,2,1],padding = 'SAME',name=name)

def inference(image):
    conv1_1 = conv_layer('conv1_1',image,[3,3,3,64])
    conv1_2 = conv_layer('conv1_2',conv1_1,[3,3,64,64])
    pool1 = pool_layer('pool1',conv1_2)
    conv2_1 = conv_layer('conv2_1',pool1,[3,3,64,128])
    conv2_2 = conv_layer('conv2_2',conv2_1,[3,3,128,128])
    pool2 = pool_layer('pool2',conv2_2)
    conv3_1 = conv_layer('conv3_1',pool2,[3,3,128,256])
    conv3_2 = conv_layer('conv3_2',conv3_1,[3,3,256,256])
    conv3_3 = conv_layer('conv3_3',conv3_2,[3,3,256,256])
    pool3 = pool_layer('pool3',conv3_3)
    conv4_1 = conv_layer('conv4_1',pool3,[3,3,256,512])
    conv4_2 = conv_layer('conv4_2',conv4_1,[3,3,512,512])
    conv4_3 = conv_layer('conv4_3',conv4_2,[3,3,512,512])
    pool4 = pool_layer('pool4',conv4_3)
    conv5_1 = conv_layer('conv5_1',pool4,[3,3,512,512])
    conv5_2 = conv_layer('conv5_2',conv5_1,[3,3,512,512])
    conv5_3 = conv_layer('conv5_3',conv5_2,[3,3,512,512])
    pool5 = pool_layer('pool5',conv5_3)
    fc1 = ip_layer('fc1',pool5,4096)
    fc2 = ip_layer('fc2',fc1,4096)
    fc3 = ip_layer('fc3',fc2,20)

    return fc3

def loss_op(logits,labels):
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels,logits = logits,name = 'cross_entropy_per_batch')
    return tf.reduce_mean(cross_entropy,name = 'cross_entropy')

def train():

    with tf.Graph().as_default():
        global_step = tf.contrib.framework.get_or_create_global_step()

        with tf.device('/cpu:0'):
            images,labels = input('./train_images','./',type = 'train',dataset_type = 'train')

        decay_steps = int(50*NUM_PER_EPOCH_FOR_TRAIN/BATCH_SIZE)
        lr = tf.train.exponential_decay(0.01,global_step*BATCH_SIZE,decay_steps,0.95,staircase=True)

        opt = tf.train.MomentumOptimizer(lr,MOMENTUM)

        logits = inference(images)
        loss = loss_op(logits,labels)
        train_op = opt.minimize(loss,global_step = global_step)

        init = tf.global_variables_initializer()

        saver = tf.train.Saver(max_to_keep = 1000)

        var_list = [var for var in tf.global_variables() if not '2_2' in var.name and not '1_2' in var.name and  not '3_3' in var.name and not '4_3' in var.name and not '5_3' in var.name and not 'fc' in var.name]
        restore_saver = tf.train.Saver(var_list)

        with tf.Session() as sess:

            sess.run(init)
            ckpt = tf.train.get_checkpoint_state('./','checkpoint1')
            restore_saver.restore(sess,ckpt.model_checkpoint_path)

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess,coord=coord)

            time_start = datetime.now()
            time_end = 0

            loss_average = 0

            for step in range(MAX_ITERATIONS):
                _,batch_loss = sess.run([train_op,loss])
                loss_average += batch_loss

                if step%DISPLAY_STEP == 0:
                    time_end = datetime.now()
                    if step !=0:
                        loss_average /= 10
                    print("step:{},duration:{},average loss:{}".format(step,time_end-time_start,loss_average))
                    record_file = open('./log','a')
                    record_file.write('{},{}\n'.format(step,loss_average))
                    record_file.close()
                    loss_average = 0
                    time_start = time_end
                if step%VALIDATION_STEP ==0:
                    saver.save(sess,"./vgg16_4096.ckpt",global_step = step)
                    print('checkpoint saved')

        coord.request_stop()
        coord.join(threads)

def evaluate():

    with tf.Graph().as_default():

        global_step = tf.contrib.framework.get_or_create_global_step()

        with tf.device('/cpu:0'):
            images,labels = input('./validation_images','./',type = 'validation',dataset_type = 'validation')

        logits = inference(images)
        loss = loss_op(logits,labels)
        top_k = tf.nn.in_top_k(logits,labels,5)
        init = tf.global_variables_initializer()

        saver = tf.train.Saver()

        with tf.Session() as sess:

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess,coord=coord)

            num_iter = int(math.ceil(NUM_PER_EPOCH_FOR_VAL/BATCH_SIZE))
            num_sample = num_iter*BATCH_SIZE

            for checkpoint_id in range(NUM_CHECKPOINTS):
                checkpoint_id += 1
                file_name = 'checkpoint{}'.format(checkpoint_id)
                ckpt = tf.train.get_checkpoint_state('./',file_name)

                print('evaluating {}...'.format(file_name))

                sess.run(init)
                saver.restore(sess,ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('./')[-1].split('-')[-1]

                accuracy = 0
                loss_average = 0
                top_k_accuracy = 0
                class_accuracy = {}
                class_num = {}
                for i in range(20):
                    class_accuracy[i] = 0
                    class_num[i] = 0

                for step in range(num_iter):
                    top_k_bool,prediction,loss_batch,label_batch = sess.run([top_k,logits,loss,labels])
                    prediction = np.argmax(prediction,axis=1)
                    accuracy_batch = BATCH_SIZE - np.count_nonzero(prediction-label_batch)
                    top_k_accuracy_batch = np.count_nonzero(top_k_bool)

                    print('step:{},batch accuracy:{},top 5:{},batch loss:{}'.format(step,accuracy_batch,top_k_accuracy_batch,loss_batch))
                    accuracy += accuracy_batch
                    loss_average += loss_batch
                    top_k_accuracy += top_k_accuracy_batch
                    for i in range(BATCH_SIZE):
                        class_num[label_batch[i]] += 1
                        if label_batch[i] == prediction[i]:
                            class_accuracy[label_batch[i]] += 1

                accuracy /= num_iter
                loss_average /= num_iter
                top_k_accuracy /= num_iter
                print('{},loss:{},accuracy:{},top 5:{}'.format(file_name,loss_average,accuracy,top_k_accuracy))
                for i in range(20):
                    class_accuracy[i] /= class_num[i]
                print(class_accuracy)
                record_file = open('./evaluation_validation_log','a')
                record_file.write('{},{},{},{}\n{}\n'.format(file_name,loss_average,accuracy,top_k_accuracy,class_accuracy))
                record_file.close()

        coord.request_stop()
        coord.join(threads)

def main():
    print('Begin...')

if __name__ == '__main__':
    #train()
    evaluate()


#!/usr/bin/python3

import sys
import glob
import os
import natsort
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import re
import numpy

data = pd.DataFrame(dtype='float64', columns=["Omega", "Implementation", "Time"])
# cublas_data =
# cucosma_data = pd.DataFrame(dtype='float64')
# cutlass_data = pd.DataFrame(dtype='float64')
# omega_data = pd.DataFrame(dtype='float64')
# time_unit_data = pd.DataFrame(dtype='float64')

configs = "/home/neville/git/cucosma/Benchmarks/128x128x8_Benchmarks/square*/config*"
nvprofs = "/home/neville/git/cucosma/Benchmarks/cosmaBenchmarks/square*/nvprof*"
configs = "/home/neville/git/cucosma/Benchmarks/cosmaBenchmarks/square*/config*"

configs_array = natsort.natsorted(glob.glob(configs))
nvprofs_array = natsort.natsorted(glob.glob(nvprofs))
# print(configs_array)

for i in range(len(configs_array)):
    # print(configs_array[i])
    # print(nvprofs_array[i])
    # print(nvprofs_cosma_array[i])
    # print()
    config_df = pd.read_csv(configs_array[i])
    nvporf_df = pd.read_csv(nvprofs_array[i], skiprows=3)

    M = int(re.findall(r'\d+', config_df.iloc[10, 0])[0])
    N = int(re.findall(r'\d+', config_df.iloc[11, 0])[0])
    K = int(re.findall(r'\d+', config_df.iloc[12, 0])[0])
    THREADBLOCK_TILE_M = re.findall(r'\d+', config_df.iloc[13, 0])[0]
    THREADBLOCK_TILE_N = re.findall(r'\d+', config_df.iloc[14, 0])[0]
    THREADBLOCK_TILE_K = re.findall(r'\d+', config_df.iloc[15, 0])[0]
    LOAD_K = re.findall(r'\d+', config_df.iloc[16, 0])[0]
    WARP_TILE_M = re.findall(r'\d+', config_df.iloc[17, 0])[0]
    WARP_TILE_N = re.findall(r'\d+', config_df.iloc[18, 0])[0]
    THREAD_TILE_M = re.findall(r'\d+', config_df.iloc[19, 0])[0]
    THREAD_TILE_N = re.findall(r'\d+', config_df.iloc[20, 0])[0]

    # print(config_df)
    # print(nvporf_df)
    # omega_data = omega_data.append({'Omega': M}, ignore_index=True)
    # time_unit_data = time_unit_data.append({'Unit': nvporf_df['Duration'][0]}, ignore_index=True)

    col_cosma = nvporf_df[nvporf_df["Name"].str.contains("cosmaSgemm", na=False)]['Duration'].astype(float)
    col_cosma_reduction = nvporf_df[nvporf_df["Name"].str.contains("cosmaSplitK", na=False)]['Duration'].astype(float)
    col_cosma.reset_index(drop=True, inplace=True)
    col_cosma_reduction.reset_index(drop=True, inplace=True)

    if len(col_cosma_reduction) != 0:
        col_cosma = col_cosma + col_cosma_reduction

    col_cosma.reset_index(drop=True, inplace=True)
    col_cutlass = nvporf_df[nvporf_df["Name"].str.contains("cutlass", na=False)]['Duration'].astype(float)
    col_cutlass.reset_index(drop=True, inplace=True)

    #  col_cutlass = nvporf_df[nvporf_df["Name"].str.contains("cutlass", na=False)]['Duration'].astype(float)
    # col_cutlass.reset_index(drop=True, inplace=True)
    # cutlass_data[i] = col_cutlass

    cublas_kernel = nvporf_df["Name"][1]
    cublas_reduction = nvporf_df["Name"][2]

    if cublas_kernel == "[CUDA memset]":
        cublas_kernel = nvporf_df["Name"][2]
        cublas_reduction = nvporf_df["Name"][3]

    if cublas_reduction == "[CUDA memcpy HtoD]":
        cublas_reduction = ""

    cublas_real_name = cublas_kernel

    col_cublas_kernel = nvporf_df[nvporf_df["Name"] == cublas_kernel]['Duration'].astype(float)
    col_cublas_reduction = nvporf_df[nvporf_df["Name"] == cublas_reduction]['Duration'].astype(float)

    col_cublas_kernel.reset_index(drop=True, inplace=True)
    col_cublas_reduction.reset_index(drop=True, inplace=True)

    # print(col_cublas_kernel)
    # print(col_cublas_reduction)

    if len(col_cublas_reduction) != 0:
        col_cublas_kernel = col_cublas_kernel + col_cublas_reduction
        cublas_real_name = cublas_kernel + "+" + cublas_reduction

    # cublas_data[i] = col_cublas_kernel
    cublas_real_name = cublas_real_name.replace('volta_sgemm_', '')
    cublas_real_name = cublas_real_name.replace('_tt', '')
    cublas_real_name = cublas_real_name.replace(
        '<int, int, float, float, float, int=128, int=16, int=4, int=4, bool=0, cublasGemvParams<cublasGemvTensorStridedBatched<float const >, cublasGemvTensorStridedBatched<float>, float>>(float const , float, float)',
        '')
    cublas_real_name = cublas_real_name.replace('void', '')
    cublas_real_name = cublas_real_name.replace(' ', '')
    cublas_real_name = cublas_real_name.replace('_kernel<float,float,float>(cublasSplitKParams<float>,floatconst*,floatconst*,float*,floatconst*,floatconst*)', '')

    print(M, ": ", cublas_real_name)

    if nvporf_df['Duration'][0] == "ms":
        col_cosma = col_cosma * 1000
        col_cutlass = col_cutlass * 1000
        col_cublas_kernel = col_cublas_kernel * 1000

    curr_df_cublas = pd.DataFrame(dtype='float64', columns=["Omega", "Implementation", "Time"])
    curr_df_cutlass = pd.DataFrame(dtype='float64', columns=["Omega", "Implementation", "Time"])
    curr_df_cosma = pd.DataFrame(dtype='float64', columns=["Omega", "Implementation", "Time"])

    cublas_mean = numpy.mean(col_cublas_kernel)

    #print((M * N * (2 * K + 3)) / (col_cublas_kernel * 10e3))

    curr_df_cublas['Omega'] = numpy.full(100, M)
    curr_df_cublas['Implementation'] = numpy.full(100, cublas_real_name)
    curr_df_cublas['Time'] = (M * N * (2 * K + 3)) / (col_cublas_kernel * 10e5)

    curr_df_cutlass['Omega'] = numpy.full(100, M)
    curr_df_cutlass['Implementation'] = numpy.full(100, "CUTLASS")
    curr_df_cutlass['Time'] = (M * N * (2 * K + 3)) / (col_cutlass * 10e5)

    curr_df_cosma['Omega'] = numpy.full(100, M)
    curr_df_cosma['Implementation'] = numpy.full(100, "cuCOSMA")
    curr_df_cosma['Time'] = (M * N * (2 * K + 3)) / (col_cosma * 10e5)

    data = data.append(curr_df_cutlass, ignore_index=True).append(curr_df_cublas, ignore_index=True).append(curr_df_cosma, ignore_index=True)

# cuBLAS_mean_df = cuBLAS_mean_df.append({'Omega': M, 'Mean': cublas_mean}, ignore_index=True)

# print(cuBLAS_mean_df)

print(data)
# print(time_unit_data)

data.to_csv("data_square_flops.csv", index=False)
# cuBLAS_mean_df.to_csv("cuBLAS_mean_square.csv", index=False)
# print(cuBLAS_mean_df)

# time_unit_data.to_csv("time_unit_data_square.csv", index=False)

exit(1)


# 14.01.2013
# @hugo_dc

# Download code from SAP Server

import wx
import Toolbars
import Utils
import sap
import Messages
import db

def Show(self):
    f = DownloadCode(self, id=-1, title="Download ABAP Sourcecode")
    f.Show()

class DownloadCode(wx.Frame):
    fields = []
    def __init__(self, parent, id, title):
        self.parent = parent
        wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE & ~ (  wx.RESIZE_BORDER | wx.RESIZE_BOX | wx.MINIMIZE_BOX ) , size=(250, 190) )
        panel = wx.Panel(self, -1)

        self.CreateStatusBar()
        toolbar = self.CreateToolBar()
        toolbar = Toolbars.getToolbar(self, toolbar, 'downloadwindow')
        toolbar.Realize()
        self.fields = []
        self.servers = db.getServers()

        servers = []
        for server in self.servers:
            servers.append(server[0])

        wx.StaticText(panel, -1, "SAP Server: ", pos=(20, 55))
        self.choice = wx.Choice(panel, -1, pos=(120, 50), choices=servers )

        y = 20

        y = Utils.createField(self, panel, 'Program name:', 100, y)

    def OnDownload(self, event):
        message = Utils.setParams(self)
        if message != None:
            Messages.messageError(message, 'Error downloading code')
        else:
            ix = self.choice.GetCurrentSelection()

            if ix < 0:
                Messages.messageError('Select a server', 'Download ABAP Source code')
            else:
                abap = sap.getCode(self.servers[ix], self.params[0])
                if abap != "":
                    self.parent.code = abap
                    self.parent.codetext.SetValue(abap)
                    self.parent.ReloadHighlight()
                    Messages.messageInformation('Source Code downloaded successfully!', 'Download')
                    self.parent.local = False
                    self.parent.ChangeRootName(self.params[0].upper(), True)
                    self.parent.sap_program = self.params[0].upper()
                    self.parent.sap_server  = self.servers[ix]
                    self.parent.tree.SetBackgroundColour('blue violet')
                    self.parent.SetTitle('Zorse - SAP:\\' + self.params[0].upper())
                    self.Destroy()
                else:
                    Messages.messageError('Error downloading program', 'Error downloading code')


"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

import copy

class Solution:
    """
    @param: root: the root of binary tree
    @param: target: An integer
    @return: all valid paths
    """
    def __init__(self, paths=[], path=[]):
        self.paths = []
        self.path = []

    def binaryTreePathSum(self, root, target):
        self.pathsHelper(root, target)
        return self.paths

    def pathsHelper(self, root, target):
        if root is None:
            return
        if target == root.val and root.left is None and root.right is None:
            self.path.append(root.val)
            path1 = copy.copy(self.path)
            self.paths.append(path1)
            self.path.pop()
        else:
            self.path.append(root.val)
            self.pathsHelper(root.left, target - root.val)
            self.pathsHelper(root.right, target - root.val)
            self.path.pop()


# -*- coding: utf-8 -*-
# @File : getExcelData.py
# _author_=feng
# date: 2021/1/13
# openpyxl(xlsx表格复制)
from openpyxl import load_workbook
import openpyxl
import xlrd
from xlutils.copy import copy

# 1- 打开excel表
def get_excelData(sheetName, startRow, endRow):
    dataList = []
    # 1-excel表路径
    excelDir = 'D:/Auto_test_python/TP_Api_Test/data/TP_接口自动化测试用例V1.1.xls'
    # 2- 打开excel对象--formatting_info=True  保持样式
    workBook = xlrd.open_workbook(excelDir, formatting_info=True)
    # workBook = openpyxl.load_workbook(excelDir)
    # workSheetNames = workBook.sheet_names()#获取所有的表名
    # print(workSheetNames)
    # 3- 获取某一个指定的表
    workSheet = workBook.sheet_by_name(sheetName)
    # 4- 读取单元格---返回是字符串---cell(行号，列号)  从0开始
    for one in range(startRow - 1, endRow):
        reqBodyData = workSheet.cell(one, 9).value  # 请求body
        respData = workSheet.cell(one, 11).value  # 响应数据
        dataList.append((reqBodyData, respData))  # 封装一个列表里嵌套元组

    return dataList

# #可以自动识别用例数
# def get_excelData2(sheetName,caseName):
#     '''
#     :param sheetName: 表名
#     :param caseName: 某一个接口的用例名称
#     :return:
#     '''
#     resList = []
#     # 1-excel表路径
#     excelDir = r'D:\Auto_test_python\TP_Api_Test\data\TP_接口自动化测试用例V1.0.xls'
#     # 2- 打开excel对象--formatting_info=True  保持样式
#     workBook = xlrd.open_workbook(excelDir,formatting_info=True)
#     # workSheetNames = workBook.sheet_names()#获取所有的表名
#     # print(workSheetNames)
#     # 3- 获取某一个指定的表
#     workSheet = workBook.sheet_by_name(sheetName)
#     # 4- 读取一列数据
#     # print(workSheet.col_values(0))
#     idx=0 # 开始的下标
#     for one in workSheet.col_values(0):
#         if caseName in one:
#             reqBodyData = workSheet.cell(idx,9).value #请求body
#             respData =  workSheet.cell(idx,11).value #响应数据
#             resList.append((json.loads(reqBodyData),json.loads(respData))) # 封装一个列表里嵌套元组
#         idx += 1#
#     return resList
#
#     # for one in range(startRow-1,endRow):
#     #     reqBodyData = workSheet.cell(one,9).value#请求body
#     #     respData =  workSheet.cell(one,11).value#响应数据
#     #     resList.append((reqBodyData,respData))#封装一个列表里嵌套元组
#     # return resList
#
#
def set_excelData():
    # 1-excel表路径
    excelDir = '../data/TP_接口自动化测试用例V1.1.xls'
    # 2- 打开excel对象--formatting_info=True  保持样式
    workBook = xlrd.open_workbook(excelDir, formatting_info=True)
    workBookNew = copy(workBook)  # 复制一个新excel文件对象
    # workSheetNew = workBookNew.get_sheet(n)
    # 取复制出来的新excel文件对象的第一个子表
    workBookNew.save(r'../report/res.xls')
    return workBookNew
    # 复制出来的excel对象，复制出来excel对象的第一个子表

if __name__ == '__main__':
    a = get_excelData('1登录模块', 3, 3)
    # b=json.loads(a)
    print(a)
    print(type(a))