import os
import imageio
import atexit
import math
from multiprocessing import Process, Queue
from gym.spaces import Box
from gym import utils
from gym.utils import seeding
import numpy as np
import mujoco_py

class PushObjectEnv(utils.EzPickle):

    def __init__(self, frame_skip, max_timestep=3000, log_dir='', seed=None):
        self.frame_skip = frame_skip

        model_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'push_object.xml')
        self.model = mujoco_py.load_model_from_path(model_path)
        self.sim = mujoco_py.MjSim(self.model, nsubsteps=frame_skip)
        self.data = self.sim.data
        self.viewer = mujoco_py.MjViewer(self.sim)
        self.joint_names = list(self.sim.model.joint_names)
        self.joint_addrs = [self.sim.model.get_joint_qpos_addr(name) for name in self.joint_names]
        self.obj_name = 'cube'
        self.endeff_name = 'endeffector'
        self.goal_pos = np.array([0., 0.])
        self.radiuses = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.075]
        self.level = len(self.radiuses)
        self.dist_thresh = 0.01
        self.metadata = {
            'render.modes': ['human', 'rgb_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }
        self.t = 0
        self.max_timestep = max_timestep

        pos_actuators = [actuator for actuator in self.model.actuator_names if 'position' in actuator]
        vel_actuators = [actuator for actuator in self.model.actuator_names if 'velocity' in actuator]
        assert(len(pos_actuators) + len(vel_actuators) == len(self.model.actuator_names))
        self.pos_actuator_ids = [self.model.actuator_name2id(actuator) for actuator in pos_actuators]
        self.vel_actuator_ids = [self.model.actuator_name2id(actuator) for actuator in vel_actuators]
        self.actuator_ids = self.pos_actuator_ids
        self.act_dim = len(self.actuator_ids)

        # compute array: position actuator's joint ranges, in order of self.pos_actuator_ids
        pos_actuators_joints = self.model.actuator_trnid[self.pos_actuator_ids][:, 0]
        self.joint_ranges = [self.model.jnt_range[joint] for joint in pos_actuators_joints]
        self.joint_ranges = np.array(self.joint_ranges)

        # initial position/velocity of robot and box
        self.init_qpos = self.data.qpos.ravel().copy()
        self.init_qvel = self.data.qvel.ravel().copy()
        _ob, _reward, _done, _info = self.step(np.zeros(self.act_dim))
        assert not _done
        self.obs_dim = _ob.size

        bounds = self.model.actuator_ctrlrange[self.actuator_ids].copy()
        low = bounds[:, 0]
        high = bounds[:, 1]
        self.action_space = Box(low, high)

        high = np.inf*np.ones(self.obs_dim)
        low = -high
        self.observation_space = Box(low, high)
        self.reward_range = (-np.inf, np.inf)
        self.seed(seed)

        # set up videos
        self.video_idx = 0
        self.video_path = os.path.join(log_dir, "video/video_%07d.mp4")
        self.video_dir = os.path.abspath(os.path.join(self.video_path, os.pardir))
        self.recording = False
        os.makedirs(self.video_dir, exist_ok=True)
        print('Saving videos to ' + self.video_dir)

        # close on exit
        atexit.register(self.close)

    def __del__(self):
      self.close()

    def seed(self, seed=None):
        """Sets the seed for this env's random number generator(s).

        Note:
            Some environments use multiple pseudorandom number generators.
            We want to capture all such seeds used in order to ensure that
            there aren't accidental correlations between multiple generators.

        Returns:
            list<bigint>: Returns the list of seeds used in this env's random
              number generators. The first value in the list should be the
              "main" seed, or the value which a reproducer should pass to
              'seed'. Often, the main seed equals the provided 'seed', but
              this won't be true if seed=None, for example.
        """
        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    def step(self, action):
        """Run one timestep of the environment's dynamics. When end of
        episode is reached, you are responsible for calling `reset()`
        to reset this environment's state.

        Accepts an action and returns a tuple (observation, reward, done, info).

        Args:
            action (object): an action provided by the environment

        Returns:
            observation (object): agent's observation of the current environment
            reward (float) : amount of reward returned after previous action
            done (boolean): whether the episode has ended, in which case further step() calls will return undefined results
            info (dict): contains auxiliary diagnostic information (helpful for debugging, and sometimes learning)
        """
        self.do_simulation(action)
        ob = self._get_obs()
        obj_pos = self.get_body_com(self.obj_name)
        obj_pos_xy = obj_pos[:2]

        # distance between object and goal
        dist_sq_og = np.sum(np.square(obj_pos_xy - self.goal_pos))
        rew_obj_goal = 0.1 * (np.exp(-800. * dist_sq_og) - 1.)

        # distance between object and robot end-effector
        endeff_pos = self.get_body_com(self.endeff_name)
        dist_sq_eo = np.sum(np.square(endeff_pos - obj_pos))
        rew_endeff_obj = 0.05 * (np.exp(-50. * dist_sq_eo) - 1.)
        reward = rew_obj_goal + rew_endeff_obj

        # reward_ctrl = -np.square(action).mean()
        # reward = rew_obj_goal + reward_ctrl
        done = False
        info = dict()
        if self.t > self.max_timestep:
            done = True
            info['dist_goal'] = np.sqrt(dist_sq_og)
        self.t += 1
        return ob, reward, done, info

    def reset(self, rand_init_pos=False, init_pos=[0., 0.]):
        """Resets the state of the environment and returns an initial observation.

        Returns: observation (object): the initial observation of the
            space.
        """
        self.t = 0
        self.sim.reset()
        ob = self.reset_model(rand_init_pos, init_pos)
        return ob

    def render(self, mode='human', close=False):
        """Renders the environment.

        The set of supported modes varies per environment. (And some
        environments do not support rendering at all.) By convention,
        if mode is:

        - human: render to the current display or terminal and
          return nothing. Usually for human consumption.
        - rgb_array: Return an numpy.ndarray with shape (x, y, 3),
          representing RGB values for an x-by-y pixel image, suitable
          for turning into a video.
        - ansi: Return a string (str) or StringIO.StringIO containing a
          terminal-style text representation. The text can include newlines
          and ANSI escape sequences (e.g. for colors).

        Note:
            Make sure that your class's metadata 'render.modes' key includes
              the list of supported modes. It's recommended to call super()
              in implementations to use the functionality of this method.

        Args:
            mode (str): the mode to render with
            close (bool): close all open renderings

        Example:

        class MyEnv(Env):
            metadata = {'render.modes': ['human', 'rgb_array']}

            def render(self, mode='human'):
                if mode == 'rgb_array':
                    return np.array(...) # return RGB frame suitable for video
                elif mode is 'human':
                    ... # pop up a window and render
                else:
                    super(MyEnv, self).render(mode=mode) # just raise an exception
        """
        if not close: # then we have to check rendering mode
            modes = self.metadata.get('render.modes', [])
            if len(modes) == 0:
                raise Exception('{} does not support rendering (requested mode: {})'.format(self, mode))
            elif mode not in modes:
                raise Exception('Unsupported rendering mode: {}. (Supported modes for {}: {})'.format(mode, self, modes))
        if close:
            if self.viewer is not None:
                self.viewer.finish()
                self.viewer = None
            return

        if mode == 'rgb_array':
            self.viewer.render()
            data, width, height = self._get_viewer().get_image()
            return np.fromstring(data, dtype='uint8').reshape(height, width, 3)[::-1, :, :]
        elif mode == 'human':
            self.viewer.render()

    def close(self):
          """Override _close in your subclass to perform any necessary cleanup.

          Environments will automatically close() themselves when
          garbage collected or when the program exits.
          """
          # _closed will be missing if this instance is still
          # initializing.
          if not hasattr(self, '_closed') or self._closed:
            return

          self._close()
          self._closed = True

    def start_record_video(self, path=None):
        if self.recording:
            print('record video in progress. calling stop before start.')
            self.stop_record_video()
        self.recording = True
        self.viewer._record_video = True
        self.viewer._hide_overlay = True
        fps = (1 / self.viewer._time_per_render)
        path = path or (self.video_path % self.video_idx)
        self.video_process = Process(target=save_video,
                                     args=(self.viewer._video_queue, path, fps))
        self.video_process.start()

    def stop_record_video(self):
        self.viewer._video_queue.put(None)
        self.video_process.join()
        self.video_idx += 1
        self.recording = False
        print('finished recording video %d' % self.video_idx)

    # ----------------------------

    @property
    def spec(self):
        return None

    def reset_model(self, rand_init_pos, init_pos=[0., 0.]):
        """
        Reset the robot degrees of freedom (qpos and qvel).
        """
        init_qpos = self.init_qpos
        if rand_init_pos:
            # center around zero, with radius 0.03
            # obj_pos = np.random.uniform(size=[2,]) * 0.3 - 0.15
            max_radius = self.radiuses[self.level - 1]
            radius = np.random.uniform(0., max_radius)
            print('level: %d, max_radius: %f, radius: %f' % (self.level, max_radius, radius))
            angle = np.random.uniform(-math.pi, math.pi)
            x = np.cos(angle) * radius
            y = np.sin(angle) * radius
            obj_pos = np.array([x, y])
        else:
            obj_pos = init_pos
        init_qpos[:2] = obj_pos
        self.set_state(self.init_qpos, self.init_qvel)
        return self._get_obs()

    def level_up(self):
        self.level += 1
        n_levels = len(self.radiuses)
        self.level = np.minimum(self.level, n_levels)
        print('increasing level to: %d' % self.level)

    def viewer_setup(self):
        """
        This method is called when the viewer is initialized and after every reset
        Optionally implement this method, if you need to tinker with camera position
        and so forth.
        """
        pass

    # -----------------------------

    def set_state(self, qpos, qvel):
        assert qpos.shape == (self.model.nq,) and qvel.shape == (self.model.nv,)
        state = self.sim.get_state()
        state.qpos[:] = qpos
        state.qvel[:] = qvel
        self.sim.set_state(state)
        self.sim.step()
        self.sim.forward()
        # self.model._compute_subtree()  # pylint: disable=W0212
        # self.model.forward()

    @property
    def dt(self):
        return self.model.opt.timestep * self.frame_skip

    def do_simulation(self, ctrl):
        # compute end position given velocity control
        # qpos_ctrl = [self.sim.data.qpos[addr] + qvel * self.dt for  (addr, qvel) in zip(self.joint_addrs, ctrl)]
        # print(str(qpos_ctrl))

        # compute velocity control
        n_vel_actuators = len(self.vel_actuator_ids)
        vel_ctrl = np.zeros(shape=[n_vel_actuators])
        # clip by -1, 1
        ctrl = np.clip(ctrl, -1., 1.)
        # scale position control up to joint range
        pos_ctrl = self.denormalize_pos(ctrl)
        self.sim.data.ctrl[self.actuator_ids] = pos_ctrl
        self.sim.data.ctrl[self.vel_actuator_ids] = vel_ctrl  # set velocity to zero for damping
        self.sim.step()
        self.sim.forward()

    def denormalize_pos(self, pos_ctrl):
        low = self.joint_ranges[:, 0]
        high = self.joint_ranges[:, 1]
        pos_ctrl = low + (pos_ctrl + 1.) / 2. * (high - low)
        return pos_ctrl

    def normalize_pos(self, pos):
        low = self.joint_ranges[:, 0]
        high = self.joint_ranges[:, 1]
        pos = (pos - low) / (high - low) * 2. - 1.
        return pos

    # def _get_viewer(self):
    #     if self.viewer is None:
    #         self.viewer = mujoco_py.MjViewer()
    #         self.viewer.start()
    #         self.viewer.set_model(self.model)
    #         self.viewer_setup()
    #     return self.viewer

    # com: center of mass?
    def get_body_com(self, body_name):
        idx = self.model.body_name2id(body_name)
        return self.data.body_xpos[idx]

    def get_body_comvel(self, body_name):
        idx = self.model.body_name2id(body_name)
        return self.data.xvelp[idx]

    def get_body_xmat(self, body_name):
        idx = self.model.body_name2id(body_name)
        return self.data.body_xmat[idx]

    # def state_vector(self):
    #     return np.concatenate([
    #         self.model.data.qpos.flat,
    #         self.model.data.qvel.flat
    #     ])

    def _get_obs(self):
        actuator_pos = self.data.actuator_length[self.pos_actuator_ids]
        actuator_vel = self.data.actuator_velocity[self.vel_actuator_ids]
        # actuator velocity can be out of [-1, 1] range, clip
        # actuator_vel = actuator_vel.clip(-1., 1.)
        # normalize pos
        pos_cos = np.cos(actuator_pos)
        pos_sin = np.sin(actuator_pos)
        actuator_pos = self.normalize_pos(actuator_pos)
        cube_com = self.get_body_com("cube")
        return np.concatenate([
            cube_com,
            pos_cos,
            pos_sin,
            actuator_pos
        ])

# Separate Process to save video. This way visualization is
# less slowed down.
def save_video(queue, filename, fps):
    writer = imageio.get_writer(filename, fps=fps)
    while True:
        frame = queue.get()
        if frame is None:
            break
        writer.append_data(frame)
    writer.close()

if __name__ == '__main__':
    env = PushObjectEnv(frame_skip=1)
    env.reset()
    # zeros = np.zeros(shape=[6])
    # ones = np.ones(shape=[6])
    for j in range(3):
        # env.start_record_video()
        # for i in range(3000):
        #     acts = np.random.normal(zeros, ones)
        #     _, _, done, _ = env.step(acts)
        #     env.render()
        #     if done:
        #         env.reset()
        # env.stop_record_video()
        for i in range(1500):
            env.step([0., 0., 0., 0., 0., 0.])
            env.render()
        for i in range(1500):
            # env.step([1., 1., 1., 1., 1., 1.])
            env.step([0., 0., 1., 0., 0., 0.])
            env.render()
        # for i in range(1500):
        #     env.step([-1., -1., -1., -1., -1., -1.])
        #     # env.step([0., -1., -1., 0., 0., 0.])
        #     env.render()


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

import os
import test
import functools
from unittest.loader import TestLoader
from baseCase.case import BaseTest

class BaseLoader(TestLoader):
    def loadTestsFromTestCase(self, testCaseClass):
        def isTestMethod(arr, testClass=testCaseClass):
            return arr[:4].lower().startswith('test') and callable(
                getattr(testClass, arr)) or arr == "runTest" and callable(getattr(testClass, arr))
        testFnNames = list(filter(isTestMethod, dir(testCaseClass)))
        if self.sortTestMethodsUsing:
            testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
        loaded_suite = self.suiteClass(map(testCaseClass, testFnNames))
        return loaded_suite

    def loadTestsFromModule(self, module, *args, pattern=None, **kws):
        dir_name = os.listdir(os.path.dirname(module.__file__))
        tests1 = [n[:-3] for n in dir_name if n.lower().startswith("test")]
        [__import__("".join([str(module.__package__), '.', i]), fromlist=True) for i in tests1]
        itests = self.suiteClass(map(self.loadTestsFromTestCase, BaseTest.__subclasses__()))
        return itests

baseLoder = BaseLoader()

if __name__ == "__main__":
    b = BaseLoader()
    s = b.loadTestsFromModule(test)
    print(s)
    # unittest.TextTestRunner(verbosity=2).run(s)


import os
import numpy as np

import keras
from keras.engine.topology import Layer
from keras.models import Model
from keras.layers import Input, Flatten, Dense, Lambda, Reshape, Concatenate
from keras.layers import Activation, LeakyReLU, ELU
from keras.layers import Conv2D, Conv2DTranspose, UpSampling2D, BatchNormalization
from keras.optimizers import Adam
from keras import backend as K

from .cond_base import CondBaseModel
from .layers import *
from .utils import *

def sample_normal(args):
    z_avg, z_log_var = args
    batch_size = K.shape(z_avg)[0]
    z_dims = K.shape(z_avg)[1]
    eps = K.random_normal(shape=(batch_size, z_dims), mean=0.0, stddev=1.0)
    return z_avg + K.exp(z_log_var / 2.0) * eps

def zero_loss(y_true, y_pred):
    return K.zeros_like(y_true)

class ClassifierLossLayer(Layer):
    __name__ = 'classifier_loss_layer'

    def __init__(self, **kwargs):
        self.is_placeholder = True
        super(ClassifierLossLayer, self).__init__(**kwargs)

    def lossfun(self, c_true, c_pred):
        return K.mean(keras.metrics.categorical_crossentropy(c_true, c_pred))

    def call(self, inputs):
        c_true = inputs[0]
        c_pred = inputs[1]
        loss = self.lossfun(c_true, c_pred)
        self.add_loss(loss, inputs=inputs)

        return c_true

class DiscriminatorLossLayer(Layer):
    __name__ = 'discriminator_loss_layer'

    def __init__(self, **kwargs):
        self.is_placeholder = True
        super(DiscriminatorLossLayer, self).__init__(**kwargs)

    def lossfun(self, y_real, y_fake_f, y_fake_p):
        y_pos = K.ones_like(y_real)
        y_neg = K.zeros_like(y_real)
        loss_real = keras.metrics.binary_crossentropy(y_pos, y_real)
        loss_fake_f = keras.metrics.binary_crossentropy(y_neg, y_fake_f)
        loss_fake_p = keras.metrics.binary_crossentropy(y_neg, y_fake_p)
        return K.mean(loss_real + loss_fake_f + loss_fake_p)

    def call(self, inputs):
        y_real = inputs[0]
        y_fake_f = inputs[1]
        y_fake_p = inputs[2]
        loss = self.lossfun(y_real, y_fake_f, y_fake_p)
        self.add_loss(loss, inputs=inputs)

        return y_real

class GeneratorLossLayer(Layer):
    __name__ = 'generator_loss_layer'

    def __init__(self, **kwargs):
        self.is_placeholder = True
        super(GeneratorLossLayer, self).__init__(**kwargs)

    def lossfun(self, x_r, x_f, f_D_x_f, f_D_x_r, f_C_x_r, f_C_x_f):
        loss_x = K.mean(K.square(x_r - x_f))
        loss_d = K.mean(K.square(f_D_x_r - f_D_x_f))
        loss_c = K.mean(K.square(f_C_x_r - f_C_x_f))

        return loss_x + loss_d + loss_c

    def call(self, inputs):
        x_r = inputs[0]
        x_f = inputs[1]
        f_D_x_r = inputs[2]
        f_D_x_f = inputs[3]
        f_C_x_r = inputs[4]
        f_C_x_f = inputs[5]
        loss = self.lossfun(x_r, x_f, f_D_x_r, f_D_x_f, f_C_x_r, f_C_x_f)
        self.add_loss(loss, inputs=inputs)

        return x_r

class FeatureMatchingLayer(Layer):
    __name__ = 'feature_matching_layer'

    def __init__(self, **kwargs):
        self.is_placeholder = True
        super(FeatureMatchingLayer, self).__init__(**kwargs)

    def lossfun(self, f1, f2):
        f1_avg = K.mean(f1, axis=0)
        f2_avg = K.mean(f2, axis=0)
        return 0.5 * K.mean(K.square(f1_avg - f2_avg))

    def call(self, inputs):
        f1 = inputs[0]
        f2 = inputs[1]
        loss = self.lossfun(f1, f2)
        self.add_loss(loss, inputs=inputs)

        return f1

class KLLossLayer(Layer):
    __name__ = 'kl_loss_layer'

    def __init__(self, **kwargs):
        self.is_placeholder = True
        super(KLLossLayer, self).__init__(**kwargs)

    def lossfun(self, z_avg, z_log_var):
        kl_loss = -0.5 * K.mean(1.0 + z_log_var - K.square(z_avg) - K.exp(z_log_var))
        return kl_loss

    def call(self, inputs):
        z_avg = inputs[0]
        z_log_var = inputs[1]
        loss = self.lossfun(z_avg, z_log_var)
        self.add_loss(loss, inputs=inputs)

        return z_avg

def discriminator_accuracy(x_r, x_f, x_p):
    def accfun(y0, y1):
        x_pos = K.ones_like(x_r)
        x_neg = K.zeros_like(x_r)
        loss_r = K.mean(keras.metrics.binary_accuracy(x_pos, x_r))
        loss_f = K.mean(keras.metrics.binary_accuracy(x_neg, x_f))
        loss_p = K.mean(keras.metrics.binary_accuracy(x_neg, x_p))
        return (1.0 / 3.0) * (loss_r + loss_p + loss_f)

    return accfun

def generator_accuracy(x_p, x_f):
    def accfun(y0, y1):
        x_pos = K.ones_like(x_p)
        loss_p = K.mean(keras.metrics.binary_accuracy(x_pos, x_p))
        loss_f = K.mean(keras.metrics.binary_accuracy(x_pos, x_f))
        return 0.5 * (loss_p + loss_f)

    return accfun

class CVAEGAN(CondBaseModel):
    def __init__(self,
        input_shape=(64, 64, 3),
        num_attrs=40,
        z_dims = 128,
        name='cvaegan',
        **kwargs
    ):
        super(CVAEGAN, self).__init__(input_shape=input_shape, name=name, **kwargs)

        self.input_shape = input_shape
        self.num_attrs = num_attrs
        self.z_dims = z_dims

        self.f_enc = None
        self.f_dec = None
        self.f_dis = None
        self.f_cls = None
        self.enc_trainer = None
        self.dec_trainer = None
        self.dis_trainer = None
        self.cls_trainer = None

        self.build_model()

    def train_on_batch(self, x_batch):
        x_r, c = x_batch

        batchsize = len(x_r)
        z_p = np.random.normal(size=(batchsize, self.z_dims)).astype('float32')

        x_dummy = np.zeros(x_r.shape, dtype='float32')
        c_dummy = np.zeros(c.shape, dtype='float32')
        z_dummy = np.zeros(z_p.shape, dtype='float32')
        y_dummy = np.zeros((batchsize, 1), dtype='float32')
        f_dummy = np.zeros((batchsize, 8192), dtype='float32')

        # Train autoencoder
        self.enc_trainer.train_on_batch([x_r, c, z_p], [x_dummy, z_dummy])

        # Train generator
        g_loss, _, _, _, _, _, g_acc = self.dec_trainer.train_on_batch([x_r, c, z_p], [x_dummy, f_dummy, f_dummy])

        # Train classifier
        self.cls_trainer.train_on_batch([x_r, c], c_dummy)

        # Train discriminator
        d_loss, d_acc = self.dis_trainer.train_on_batch([x_r, c, z_p], y_dummy)

        loss = {
            'g_loss': g_loss,
            'd_loss': d_loss,
            'g_acc': g_acc,
            'd_acc': d_acc
        }
        return loss

    def predict(self, z_samples):
        return self.f_dec.predict(z_samples)

    def build_model(self):
        self.f_enc = self.build_encoder(output_dims=self.z_dims*2)
        self.f_dec = self.build_decoder()
        self.f_dis = self.build_discriminator()
        self.f_cls = self.build_classifier()

        # Algorithm
        x_r = Input(shape=self.input_shape)
        c = Input(shape=(self.num_attrs,))
        z_params = self.f_enc([x_r, c])

        z_avg = Lambda(lambda x: x[:, :self.z_dims], output_shape=(self.z_dims,))(z_params)
        z_log_var = Lambda(lambda x: x[:, self.z_dims:], output_shape=(self.z_dims,))(z_params)
        z = Lambda(sample_normal, output_shape=(self.z_dims,))([z_avg, z_log_var])

        kl_loss = KLLossLayer()([z_avg, z_log_var])

        z_p = Input(shape=(self.z_dims,))

        x_f = self.f_dec([z, c])
        x_p = self.f_dec([z_p, c])

        y_r, f_D_x_r = self.f_dis(x_r)
        y_f, f_D_x_f = self.f_dis(x_f)
        y_p, f_D_x_p = self.f_dis(x_p)

        d_loss = DiscriminatorLossLayer()([y_r, y_f, y_p])

        c_r, f_C_x_r = self.f_cls(x_r)
        c_f, f_C_x_f = self.f_cls(x_f)
        c_p, f_C_x_p = self.f_cls(x_p)

        g_loss = GeneratorLossLayer()([x_r, x_f, f_D_x_r, f_D_x_f, f_C_x_r, f_C_x_f])
        gd_loss = FeatureMatchingLayer()([f_D_x_r, f_D_x_p])
        gc_loss = FeatureMatchingLayer()([f_C_x_r, f_C_x_p])

        c_loss = ClassifierLossLayer()([c, c_r])

        # Build classifier trainer
        set_trainable(self.f_enc, False)
        set_trainable(self.f_dec, False)
        set_trainable(self.f_dis, False)
        set_trainable(self.f_cls, True)

        self.cls_trainer = Model(inputs=[x_r, c],
                                 outputs=[c_loss])
        self.cls_trainer.compile(loss=[zero_loss],
                                 optimizer=Adam(lr=2.0e-4, beta_1=0.5))
        self.cls_trainer.summary()

        # Build discriminator trainer
        set_trainable(self.f_enc, False)
        set_trainable(self.f_dec, False)
        set_trainable(self.f_dis, True)
        set_trainable(self.f_cls, False)

        self.dis_trainer = Model(inputs=[x_r, c, z_p],
                                 outputs=[d_loss])
        self.dis_trainer.compile(loss=[zero_loss],
                                 optimizer=Adam(lr=2.0e-4, beta_1=0.5),
                                 metrics=[discriminator_accuracy(y_r, y_f, y_p)])
        self.dis_trainer.summary()

        # Build generator trainer
        set_trainable(self.f_enc, False)
        set_trainable(self.f_dec, True)
        set_trainable(self.f_dis, False)
        set_trainable(self.f_cls, False)

        self.dec_trainer = Model(inputs=[x_r, c, z_p],
                                 outputs=[g_loss, gd_loss, gc_loss])
        self.dec_trainer.compile(loss=[zero_loss, zero_loss, zero_loss],
                                 optimizer=Adam(lr=2.0e-4, beta_1=0.5),
                                 metrics=[generator_accuracy(y_p, y_f)])

        # Build autoencoder
        set_trainable(self.f_enc, True)
        set_trainable(self.f_dec, False)
        set_trainable(self.f_dis, False)
        set_trainable(self.f_cls, False)

        self.enc_trainer = Model(inputs=[x_r, c, z_p],
                                outputs=[g_loss, kl_loss])
        self.enc_trainer.compile(loss=[zero_loss, zero_loss],
                                optimizer=Adam(lr=2.0e-4, beta_1=0.5))
        self.enc_trainer.summary()

        # Store trainers
        self.store_to_save('cls_trainer')
        self.store_to_save('dis_trainer')
        self.store_to_save('dec_trainer')
        self.store_to_save('enc_trainer')

    def build_encoder(self, output_dims):
        x_inputs = Input(shape=self.input_shape)
        c_inputs = Input(shape=(self.num_attrs,))

        c = Reshape((1, 1, self.num_attrs))(c_inputs)
        c = UpSampling2D(size=self.input_shape[:2])(c)
        x = Concatenate(axis=-1)([x_inputs, c])

        x = BasicConvLayer(filters=128, strides=(2, 2))(x)
        x = BasicConvLayer(filters=256, strides=(2, 2))(x)
        x = BasicConvLayer(filters=256, strides=(2, 2))(x)
        x = BasicConvLayer(filters=512, strides=(2, 2))(x)

        x = Flatten()(x)
        x = Dense(1024)(x)
        x = Activation('relu')(x)

        x = Dense(output_dims)(x)
        x = Activation('linear')(x)

        return Model([x_inputs, c_inputs], x)

    def build_decoder(self):
        z_inputs = Input(shape=(self.z_dims,))
        c_inputs = Input(shape=(self.num_attrs,))
        z = Concatenate()([z_inputs, c_inputs])

        w = self.input_shape[0] // (2 ** 4)
        x = Dense(w * w * 512)(z)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)

        x = Reshape((w, w, 512))(x)

        x = BasicDeconvLayer(filters=512, strides=(2, 2))(x)
        x = BasicDeconvLayer(filters=256, strides=(2, 2))(x)
        x = BasicDeconvLayer(filters=256, strides=(2, 2))(x)
        x = BasicDeconvLayer(filters=128, strides=(2, 2))(x)

        d = self.input_shape[2]
        x = BasicDeconvLayer(filters=d, strides=(1, 1), bnorm=False, activation='tanh')(x)

        return Model([z_inputs, c_inputs], x)

    def build_discriminator(self):
        inputs = Input(shape=self.input_shape)

        x = BasicConvLayer(filters=128, strides=(2, 2))(inputs)
        x = BasicConvLayer(filters=256, strides=(2, 2))(x)
        x = BasicConvLayer(filters=256, strides=(2, 2))(x)
        x = BasicConvLayer(filters=512, strides=(2, 2))(x)

        f = Flatten()(x)
        x = Dense(1024)(f)
        x = Activation('relu')(x)

        x = Dense(1)(x)
        x = Activation('sigmoid')(x)

        return Model(inputs, [x, f])

    def build_classifier(self):
        inputs = Input(shape=self.input_shape)

        x = BasicConvLayer(filters=128, strides=(2, 2))(inputs)
        x = BasicConvLayer(filters=256, strides=(2, 2))(x)
        x = BasicConvLayer(filters=256, strides=(2, 2))(x)
        x = BasicConvLayer(filters=512, strides=(2, 2))(x)

        f = Flatten()(x)
        x = Dense(1024)(f)
        x = Activation('relu')(x)

        x = Dense(self.num_attrs)(x)
        x = Activation('softmax')(x)

        return Model(inputs, [x, f])


from faker import Faker
from git_class.models_new.database import create_db, Session
from git_class.models_new.car import Car
from git_class.models_new.info_car import InfoCar

def create_database(load_fake_data=True):
    create_db()
    if load_fake_data:
        _load_fake_data(Session())

def _load_fake_data(session):
    car_names = ['BMW', 'Audi', 'Mercedes', 'Lada', 'Opel', 'Ford', 'Honda', 'Lexus']

    for i in car_names:
        cars_n = Car(model=i)
        session.add(cars_n)
    session.commit()
    faker = Faker('ru_RU')

    for i in range(len(car_names)):
        colors = faker.color()
        forces = faker.random.randint(110, 250)
        names = faker.name()
        info = InfoCar(colors, forces, names)
        session.add(info)
    session.commit()
    session.close()


import os
import torch
import torch.nn.functional as F
import glob
import imageio
import numpy as np
from utils.data_utils import get_image_to_tensor, get_mask_to_tensor

class DVRDataset(torch.utils.data.Dataset):
    def __init__(self,
                 args,
                 mode,
                 list_prefix="softras_",
                 image_size=None,
                 scale_focal=True,
                 max_imgs=100000,
                 z_near=1.2,
                 z_far=4.0):
        super().__init__()
        self.args = args
        self.mode = mode
        self.base_dir = args.datadir

        use_source_lut = True if args.src_view is not None else False
        if use_source_lut:
            print("Using views from list", args.src_view)
            with open(args.src_view, "r") as f:
                tmp = [x.strip().split() for x in f.readlines()]
            self.source_lut = {
                x[0] + "/" + x[1]: torch.tensor(list(map(int, x[2:])), dtype=torch.long)
                for x in tmp
            }

        categories = [x for x in glob.glob(os.path.join(self.base_dir, "*")) if os.path.isdir(x)]
        obj_lists = [os.path.join(x, list_prefix + '{}.lst'.format(mode)) for x in categories]

        all_objs = []
        for obj_list in obj_lists:
            if not os.path.exists(obj_list):
                continue
            category_dir = os.path.dirname(obj_list)
            category = os.path.basename(category_dir)
            with open(obj_list, "r") as f:
                objs = [(category, os.path.join(category_dir, x.strip())) for x in f.readlines()]
            all_objs.extend(objs)

        self.all_objs = all_objs

        self.image_to_tensor = get_image_to_tensor()
        self.mask_to_tensor = get_mask_to_tensor()
        print("[Info] Loading DVR dataset: {}, mode: {}, "
              "type: {}, {} objects in total".format(self.base_dir,
                                                     mode,
                                                     "ShapeNet",
                                                     len(self.all_objs)))

        self.image_size = image_size
        self._coord_trans = torch.diag(torch.tensor([1, -1, -1, 1], dtype=torch.float32))
        self.scale_focal = scale_focal
        self.max_imgs = max_imgs

        self.z_near = z_near
        self.z_far = z_far
        self.lindisp = False

    def __len__(self):
        return len(self.all_objs)

    def __getitem__(self, index):
        category, obj_dir = self.all_objs[index]

        rgb_paths = [x for x in glob.glob(os.path.join(obj_dir, "image", "*"))
                     if x.endswith(".jpg") or x.endswith(".png")]
        rgb_paths = sorted(rgb_paths)
        mask_paths = sorted(glob.glob(os.path.join(obj_dir, "mask", "*.png")))
        if len(mask_paths) == 0:
            mask_paths = [None] * len(rgb_paths)

        if len(rgb_paths) <= self.max_imgs:
            sel_indices = np.arange(len(rgb_paths))
        else:
            sel_indices = np.random.choice(len(rgb_paths), self.max_imgs, replace=False)
            rgb_paths = [rgb_paths[i] for i in sel_indices]
            mask_paths = [mask_paths[i] for i in sel_indices]

        cam_path = os.path.join(obj_dir, "cameras.npz")
        all_cam = np.load(cam_path)

        all_imgs = []
        all_poses = []
        all_masks = []
        all_bboxes = []
        focal = None

        for idx, (rgb_path, mask_path) in enumerate(zip(rgb_paths, mask_paths)):
            i = sel_indices[idx]
            img = imageio.imread(rgb_path)[..., :3]
            if self.scale_focal:
                x_scale = img.shape[1] / 2.0
                y_scale = img.shape[0] / 2.0
                xy_delta = 1.0
            else:
                x_scale = y_scale = 1.0
                xy_delta = 0.0

            if mask_path is not None:
                mask = imageio.imread(mask_path)
                if len(mask.shape) == 2:
                    mask = mask[..., None]
                mask = mask[..., :1]

            # ShapeNet
            wmat_inv_key = "world_mat_inv_" + str(i)
            wmat_key = "world_mat_" + str(i)
            if wmat_inv_key in all_cam:
                extr_inv_mtx = all_cam[wmat_inv_key]
            else:
                extr_inv_mtx = all_cam[wmat_key]
                if extr_inv_mtx.shape[0] == 3:
                    extr_inv_mtx = np.vstack((extr_inv_mtx, np.array([0, 0, 0, 1])))
                extr_inv_mtx = np.linalg.inv(extr_inv_mtx)

            intr_mtx = all_cam["camera_mat_" + str(i)]
            fx, fy = intr_mtx[0, 0], intr_mtx[1, 1]
            assert abs(fx - fy) < 1e-9
            fx = fx * x_scale
            if focal is None:
                focal = fx
            else:
                assert abs(fx - focal) < 1e-5
            pose = extr_inv_mtx

            pose = torch.tensor(pose, dtype=torch.float32) @ self._coord_trans

            img_tensor = self.image_to_tensor(img)
            if mask_path is not None:
                mask_tensor = self.mask_to_tensor(mask)

                rows = np.any(mask, axis=1)
                cols = np.any(mask, axis=0)
                rnz = np.where(rows)[0]
                cnz = np.where(cols)[0]
                if len(rnz) == 0:
                    raise RuntimeError("ERROR: Bad image at", rgb_path, "please investigate!")
                rmin, rmax = rnz[[0, -1]]
                cmin, cmax = cnz[[0, -1]]
                bbox = torch.tensor([cmin, rmin, cmax, rmax], dtype=torch.float32)
                all_masks.append(mask_tensor)
                all_bboxes.append(bbox)

            all_imgs.append(img_tensor)
            all_poses.append(pose)

        if mask_path is not None:
            all_bboxes = torch.stack(all_bboxes)

        all_imgs = torch.stack(all_imgs)
        all_poses = torch.stack(all_poses)
        if len(all_masks) > 0:
            all_masks = torch.stack(all_masks)
        else:
            all_masks = None

        if self.image_size is not None and all_imgs.shape[-2:] != self.image_size:
            scale = self.image_size[0] / all_imgs.shape[-2]
            focal *= scale

            if mask_path is not None:
                all_bboxes *= scale

            all_imgs = F.interpolate(all_imgs, size=self.image_size, mode="area")
            if all_masks is not None:
                all_masks = F.interpolate(all_masks, size=self.image_size, mode="area")

        H, W = all_imgs.shape[-2:]

        if self.args.src_view is None:
            src_view = np.random.choice(len(rgb_paths), 1)[0]
        else:
            # src_view = int(self.args.src_view)
            obj_id = category + "/" + os.path.basename(obj_dir)
            src_view = self.source_lut[obj_id]

        src_img = all_imgs[src_view:src_view+1]
        src_pose = all_poses[src_view:src_view+1]
        # src_mask = all_masks[src_view:src_view+1]
        # src_bbox = all_bboxes[src_view:src_view+1]
        src_path = rgb_paths[src_view]

        K = np.array([[focal, 0., W // 2, 0.],
                      [0., focal, H // 2, 0],
                      [0., 0, 1, 0],
                      [0, 0, 0, 1]]).astype(np.float32)
        intrinsics = torch.from_numpy(K).view(1, 4, 4).repeat(len(all_imgs), 1, 1)
        depth_range = torch.tensor([self.z_near, self.z_far], dtype=torch.float32).view(1, -1).repeat(len(all_imgs), 1)

        ret = {
            "obj_dir": obj_dir,
            "category": category,
            "index": index,
            "intrinsics": intrinsics,
            "rgb_paths": rgb_paths,
            "images": all_imgs,
            "poses": all_poses,
            "depth_range": depth_range,
            "src_view": src_view,
            "src_path": src_path,
            "src_image": src_img,
            # "src_mask": src_mask,
            "src_pose": src_pose,
            # "src_bbox": src_bbox
        }
        if all_masks is not None:
            ret["masks"] = all_masks

        ret["bboxes"] = all_bboxes
        return ret


# -*- coding: utf-8 -*-
from contextlib import contextmanager
try:
    from typing import Type
except ImportError:  # Python 2.x
    pass

import redis
import datetime
from bitmapist4 import events as ev

class Bitmapist(object):
    """
    Core bitmapist object
    """

    # Should hourly be tracked as default?
    # Note that this can have huge implications in amounts
    # of memory that Redis uses (especially with huge integers)
    track_hourly = False

    # Should unique events be tracked as default?
    track_unique = False

    def __init__(self,
                 connection_or_url=redis.StrictRedis(),
                 track_hourly=False,
                 track_unique=True,
                 finished_ops_expire=3600 * 24,
                 unfinished_ops_expire=60,
                 key_prefix='bitmapist_'):
        if isinstance(connection_or_url, redis.StrictRedis):
            self.connection = connection_or_url
        else:
            self.connection = redis.StrictRedis.from_url(connection_or_url)
        self.track_hourly = track_hourly
        self.track_unique = track_unique
        self.finished_ops_expire = finished_ops_expire
        self.unfinished_ops_expire = unfinished_ops_expire
        self.key_prefix = key_prefix
        self.pipe = None

        kw = {'bitmapist': self}
        self.UniqueEvents = type('UniqueEvents', (ev.UniqueEvents, ),
                                 kw)  # type: Type[ev.UniqueEvents]
        self.YearEvents = type('YearEvents', (ev.YearEvents, ),
                               kw)  # type: Type[ev.YearEvents]
        self.MonthEvents = type('MonthEvents', (ev.MonthEvents, ),
                                kw)  # type: Type[ev.MonthEvents]
        self.WeekEvents = type('WeekEvents', (ev.WeekEvents, ),
                               kw)  # type: Type[ev.WeekEvents]
        self.DayEvents = type('DayEvents', (ev.DayEvents, ),
                              kw)  # type: Type[ev.DayEvents]
        self.HourEvents = type('HourEvents', (ev.HourEvents, ),
                               kw)  # type: Type[ev.HourEvents]
        self.BitOpAnd = type('BitOpAnd', (ev.BitOpAnd, ),
                             kw)  # type: Type[ev.BitOpAnd]
        self.BitOpOr = type('BitOpOr', (ev.BitOpOr, ),
                            kw)  # type: Type[ev.BitOpOr]
        self.BitOpXor = type('BitOpXor', (ev.BitOpXor, ),
                             kw)  # type: Type[ev.BitOpXor]
        self.BitOpNot = type('BitOpNot', (ev.BitOpNot, ),
                             kw)  # type: Type[ev.BitOpNot]

    def mark_event(self,
                   event_name,
                   uuid,
                   timestamp=None,
                   track_hourly=None,
                   track_unique=None):
        """
        Marks an event as "happened" for a specific moment. The function
        stores the event for the day, week and month, and optionally
        for the hour, as well as the unique event.

        - event_name is the name of the event to track
        - uuid is the unique id of the subject (typically user id). The id
          should not be huge
        - timestamp is an optional moment of time which date should be used as
          a reference point, default is to `datetime.utcnow()`

        Examples:

            # Mark id 1 as active
            b.mark_event('active', 1)

            # Mark task completed for id 252
            b.mark_event('tasks:completed', 252)
        """
        self._mark(event_name, uuid, timestamp, 1, track_hourly, track_unique)

    def unmark_event(self,
                     event_name,
                     uuid,
                     timestamp=None,
                     track_hourly=None,
                     track_unique=None):
        """
        Marks an event as "not happened" for a specific moment. The function
        stores the event for the day, week and month, and optionally
        for the hour, as well as the unique event.
        """
        self._mark(event_name, uuid, timestamp, 0, track_hourly, track_unique)

    def _mark(self, event_name, uuid, timestamp, value, track_hourly,
              track_unique):
        if timestamp is None:
            timestamp = datetime.datetime.utcnow()
        if track_hourly is None:
            track_hourly = self.track_hourly
        if track_unique is None:
            track_unique = self.track_unique

        obj_classes = [self.MonthEvents, self.WeekEvents, self.DayEvents]
        if track_hourly:
            obj_classes.append(self.HourEvents)
        if track_unique:
            obj_classes.append(self.UniqueEvents)

        if self.pipe is None:
            pipe = self.connection.pipeline()
        else:
            pipe = self.pipe

        for obj_class in obj_classes:
            pipe.setbit(
                obj_class.from_date(event_name, timestamp).redis_key, uuid,
                value)

        if self.pipe is None:
            pipe.execute()

    def start_transaction(self):
        if self.pipe is not None:
            raise RuntimeError("Transaction already started")
        self.pipe = self.connection.pipeline()

    def commit_transaction(self):
        if self.pipe is None:
            raise RuntimeError("Transaction not started")
        self.pipe.execute()
        self.pipe = None

    def rollback_transaction(self):
        self.pipe = None

    @contextmanager
    def transaction(self):
        self.start_transaction()
        try:
            yield
            self.commit_transaction()
        except:
            self.rollback_transaction()
            raise

    def mark_unique(self, event_name, uuid):
        """
        Mark unique event as "happened with a user"

        Unique event (aka "user flag") is an event which doesn't depend on date.
        Can be used for storing user properties, A/B testing, extra filtering,
        etc.

        - event_name: The name of the event, could be "active" or "new_signups"
        - uuid:  a unique id, typically user id. The id should not be huge

        Example:

            # Mark id 42 as premium
            b.mark_unique('premium', 42)
        """
        self._mark_unique(event_name, uuid, value=1)

    def unmark_unique(self, event_name, uuid):
        """
        Mark unique event as "not happened with a user"

        Unique event (aka "user flag") is an event which doesn't depend on date.
        Can be used for storing user properties, A/B testing, extra filtering,
        etc.

        - event_name: The name of the event, could be "active" or "new_signups"
        - uuid:  a unique id, typically user id. The id should not be huge

        Example:

            # Mark id 42 as premium
            b.unmark_unique('premium', 42)
        """
        self._mark_unique(event_name, uuid, value=0)

    def _mark_unique(self, event_name, uuid, value):
        conn = self.connection if self.pipe is None else self.pipe
        redis_key = self.UniqueEvents(event_name).redis_key
        conn.setbit(redis_key, uuid, value)

    def get_event_names(self, prefix='', batch=10000):
        """
        Return the list of all event names, with no particular order. Optional
        `prefix` value is used to filter only subset of keys
        """
        expr = '{}{}*'.format(self.key_prefix, prefix)
        ret = set()
        for result in self.connection.scan_iter(match=expr, count=batch):
            result = result.decode()
            chunks = result.split('_')
            event_name = '_'.join(chunks[1:-1])
            if not event_name.startswith('bitop_'):
                ret.add(event_name)
        return sorted(ret)

    def delete_all_events(self):
        """
        Delete all events from the database.
        """
        keys = self.connection.keys('{}*'.format(self.key_prefix))
        if keys:
            self.connection.delete(*keys)

    def delete_temporary_bitop_keys(self):
        """
        Delete all temporary keys that are used when using bit operations.
        """
        keys = self.connection.keys('{}bitop_*'.format(self.key_prefix))
        if keys:
            self.connection.delete(*keys)

    def prefix_key(self, event_name, date):
        return '{}{}_{}'.format(self.key_prefix, event_name, date)


import flask
from flask import Flask,request , jsonify
from xyz import AddTwo as ad

app = Flask(__name__)

@ app.route('/')
def test():
    return jsonify({"status":"ok"})

@ app.route('/parsename',methods=['GET'])
def test_name():

    var_name = request.args.get("name")
    return jsonify({"Entered name = ": var_name})

@ app.route('/twopara',methods=['GET'])
def two_para():
    n=request.args.get('name')
    r=request.args.get('rollno')
    output="Roll No{}has the name:{}".format(r,n)
    return output

@ app.route('/addtwo',methods=['GET'])
def add_two_num():
    fno = request.args.get("fn")
    sno = request.args.get("sn")

    a = ad()
    s = a.add_Two(fno,sno)

    return "Sum of {} and {} is : {}".format(fno,sno,s)

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


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

import sys
import os
import json
import argparse

def print_rank_0(*args, **kwargs):
    rank = int(os.getenv("RANK", "0"))
    if rank == 0:
        print(*args, **kwargs)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("module_name", type=str)
    parser.add_argument("--output_preLen", type=int, default=20)
    parser.add_argument("--only-oneflow", action="store_true")
    parser.add_argument("--only-pytorch", action="store_true")
    args = parser.parse_args()

    with open("input_text.json", encoding="utf-8") as f:
        dict_input = json.load(f)

    input = {
        "txt": dict_input[str(args.output_preLen)],
        "preLen": args.output_preLen # 预测的长度
    }

    if not args.only_pytorch:
        sys.path.append('/path/to/writer/AI_Writer_Web')
        from AI_Writer_Web.infer import Writer as Writer_Oneflow
        writer_oneflow = Writer_Oneflow()
        output_oneflow = writer_oneflow.inference(input)
        oneflow_time = output_oneflow["time"]
        #print(output_oneflow["txt"])

        print_rank_0(
            f"OneFlow {args.module_name} time: {oneflow_time:.8f}s , output_preLen={args.output_preLen}"
        )

    if not args.only_oneflow:
        sys.path.append('/path/to/writer/AI_Writer')
        from AI_Writer.infer import Writer as Writer_Pytorch
        writer_pytorch = Writer_Pytorch()
        output_pytorch = writer_pytorch.inference(input)

        pytorch_time = output_pytorch["time"]
        #print(output_pytorch["txt"])

        print_rank_0(
            f"PyTorch {args.module_name} time: {pytorch_time:.8f}s , output_preLen={args.output_preLen}"
        )

    if not args.only_pytorch and not args.only_oneflow:
        relative_speed = pytorch_time / oneflow_time
        print_rank_0(
            f"Relative speed: {relative_speed:.8f} (= {pytorch_time:.8f}s / {oneflow_time:.8f}s)"
        )


import os
from flask import Blueprint, request, make_response
from werkzeug.utils import secure_filename
from uploadFileTask import handle_file

from ..models import db,Products
import pandas as pd
#from uploadFileTask import handle_file
from multiprocessing.pool import ThreadPool as Pool
upload_products = Blueprint('upload',  __name__,
                        template_folder='templates')

t = Pool(processes=20)

@upload_products.route('/upload', methods=['POST'])
def upload():
    # Remember the paramName was set to 'file', we can use that here to grab it
    file = request.files['file']
    filename = secure_filename(file.filename)
    print(filename)
    # secure_filename makes sure the filename isn't unsafe to save
    save_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), secure_filename(file.filename))
    current_chunk = int(request.form['dzchunkindex'])
    print("chunck ",current_chunk)
    print("chunk total count",)
    if os.path.exists(save_path) and current_chunk == 0:
            os.remove(save_path)
    with open(save_path, 'ab') as f:
        # Goto the offset, aka after the chunks we already wrote
        f.seek(int(request.form['dzchunkbyteoffset']))
        data=file.stream.read()
        f.write(data)
    if current_chunk ==(int(request.form['dztotalchunkcount'])-1):
         #t1 = threading.Thread(target=store_data_in_db, args=(save_path,db))
         #t1.start()
         # t1.join()
         #store_data_in_db(save_path)
         # t.join()
         # t.close()
         #handle_file.send(save_path)
         handle_file(save_path)
    return make_response(('Chunk', 200))

def handle_file(save_path):
    f = get_next_record(save_path)
    for i in f:
        t.map(store_in_db, (i,))

def get_next_record(file):
    df = pd.read_csv(file, sep=',')
    print(df.head(5))
    df=df.drop_duplicates(subset='sku', keep='first')
    inc=j=int(df.shape[0]/100)
    i=0
    for _ in range(100):
        yield df[i:j]
        i+=inc
        j+=inc

def store_in_db(dataset):
    products=[]
    #dataset.drop_duplicates(subset='sku', keep="last")
    for index, data in dataset.iterrows():
        products.append(Products(name=data[0], sku=data[1], description=data[2]))
        #db.session.merge(record)
    try:
        db.session.bulk_save_objects(products)
        db.session.commit()
    except Exception as e:
        print(e)

def store_data_in_db(file,db):
    Products.query.delete()
    db.session.commit()
    import pandas as pd
    df = pd.read_csv(file, sep=',', header=None)
    try:
          for index, row in df.iterrows():
                 record = Products(name=row[0], sku=row[1], description=row[2])
                 db.session.merge(record)
    except Exception as e :
                 print(e)
    db.session.commit()


import pygame

class Ship:
    def __init__(self, screen):
        self.character = pygame.image.load('hw_images/ship_0009.png')
        self.character_rect = self.character.get_rect()
        self.screen_rect = screen.get_rect()
        self.character_rect.center = self.screen_rect.center
        self.ship_speed = 1.0

        self.x = float(self.character_rect.x)
        self.y = float(self.character_rect.y)

        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def draw(self):
        screen.blit(self.character, self.character_rect)

    def update(self):
        if self.moving_right and self.character_rect.right < self.screen_rect.right:
            self.x += self.ship_speed
        if self.moving_left and self.character_rect.left > 0:
            self.x -= self.ship_speed
        if self.moving_up and self.character_rect.top > 0:
            self.y -= self.ship_speed
        if self.moving_down and self.character_rect.bottom < self.screen_rect.bottom:
            self.y += self.ship_speed

        self.character_rect.x = self.x
        self.character_rect.y = self.y

pygame.init()
screen = pygame.display.set_mode((500, 500))
ship = Ship(screen)

screen.fill((181, 255, 255))

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = True
            elif event.key == pygame.K_LEFT:
                ship.moving_left = True
            elif event.key == pygame.K_UP:
                ship.moving_up = True
            elif event.key == pygame.K_DOWN:
                ship.moving_down = True

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = False
            elif event.key == pygame.K_LEFT:
                ship.moving_left = False
            elif event.key == pygame.K_UP:
                ship.moving_up = False
            elif event.key == pygame.K_DOWN:
                ship.moving_down = False

    ship.update()
    screen.fill((181, 255, 255))
    ship.draw()
    pygame.display.flip()


from django.conf.urls import include, url
from django.views.generic import RedirectView

from . import views

person = [
    url(r'^$', views.PersonList.as_view(), name='person-list'),
    url(r'^new/$', views.PersonCreate.as_view(), name='person-create'),
    url(r'^(?P<pk>[^/]+)/$', views.PersonDetail.as_view(), name='person-detail'),
    url(r'^(?P<pk>[^/]+)/delete/$', views.PersonDelete.as_view(), name='person-delete'),
    url(r'^(?P<pk>[^/]+)/edit/$', views.PersonEdit.as_view(), name='person-edit'),
    url(r'^(?P<pk>[^/]+)/volunteer/$', views.VolunteerCreate.as_view(), name='volunteer-create'),
    url(r'^(?P<pk>[^/]+)/recipient/$', views.RecipientCreate.as_view(), name='recipient-create'),
    url(r'^(?P<pk>[^/]+)/member/$', views.MemberCreate.as_view(), name='member-create'),
    url(r'^(?P<pk>[^/]+)/membership/$', views.MembershipCreate.as_view(), name='membership-create'),
]

recipient = [
    url(r'^$', views.RecipientList.as_view(), name='recipient-list'),
    url(r'^(?P<pk>\d+)/$', views.RecipientDetail.as_view(), name='recipient-detail'),
    url(r'^(?P<pk>\d+)/delete/$', views.RecipientDelete.as_view(), name='recipient-delete'),
    url(r'^(?P<pk>\d+)/edit/$', views.RecipientEdit.as_view(), name='recipient-edit'),
    url(r'^(?P<pk>\d+)/custodian/$', views.CustodianCreate.as_view(), name='custodian-create'),
]

volunteer = [
    url(r'^$', views.VolunteerList.as_view(), name='volunteer-list'),
    url(r'^(?P<pk>\d+)/$', views.VolunteerDetail.as_view(), name='volunteer-detail'),
    url(r'^(?P<pk>\d+)/delete/$', views.VolunteerDelete.as_view(), name='volunteer-delete'),
    url(r'^(?P<pk>\d+)/edit/$', views.VolunteerEdit.as_view(), name='volunteer-edit'),
]

member = [
    url(r'^$', views.MemberList.as_view(), name='member-list'),
    url(r'^(?P<pk>\d+)/$', views.MemberDetail.as_view(), name='member-detail'),
    url(r'^(?P<pk>\d+)/edit/$', views.MemberEdit.as_view(), name='member-edit'),
]

custodian = [
    url(r'^(?P<pk>\d+)/$', views.CustodianDetail.as_view(), name='custodian-detail'),
    url(r'^(?P<pk>\d+)/edit/$', views.CustodianEdit.as_view(), name='custodian-edit'),
]

membership = [
    url(r'^(?P<pk>\d+)/$', views.MembershipDetail.as_view(), name='membership-detail'),
    url(r'^(?P<pk>\d+)/edit/$', views.MembershipEdit.as_view(), name='membership-edit'),
]

project = [
    url(r'^$', views.ProjectList.as_view(), name='project-list'),
    url(r'^(?P<pk>\d+)/$', views.ProjectDetail.as_view(), name='project-detail'),
    url(r'^(?P<pk>\d+)/edit/$', views.ProjectEdit.as_view(), name='project-edit'),
    url(r'^new/$', views.ProjectCreate.as_view(), name='project-create'),
]

group = [
    url(r'^$', views.GroupList.as_view(), name='group-list'),
    url(r'^(?P<pk>\d+)/$', views.GroupDetail.as_view(), name='group-detail'),
    url(r'^(?P<pk>\d+)/edit/$', views.GroupEdit.as_view(), name='group-edit'),
    url(r'^new/$', views.GroupCreate.as_view(), name='group-create'),
]

event = [
    url(r'^$', views.EventList.as_view(), name='event-list'),
    url(r'^(?P<pk>\d+)/$', views.EventDetail.as_view(), name='event-detail'),
    url(r'^(?P<pk>\d+)/edit/$', views.EventEdit.as_view(), name='event-edit'),
    url(r'^new/$', views.EventCreate.as_view(), name='event-create'),
]

ajax = [
    url(r'^person/', views.AjaxPersonList.as_view()),
]

urlpatterns = [
    url(r'^ajax/', include(ajax)),
    url(r'^person/', include(person)),
    url(r'^recipient/', include(recipient)),
    url(r'^volunteer/', include(volunteer)),
    url(r'^member/', include(member)),
    url(r'^custodian/', include(custodian)),
    url(r'^membership/', include(membership)),
    url(r'^group/', include(group)),
    url(r'^project/', include(project)),
    url(r'^event/', include(event)),

    url(r'^$', RedirectView.as_view(url='login/')),
    url(r'^missing_doc/$', views.missing_doc, name='missing_doc'),

    url(r'^login/$', views.Login.as_view(), name='login'),
    url(r'^logout/$', views.Logout.as_view(), name='logout'),
    url(r'^home/$', views.Home.as_view(), name='home'),
    url(r'^basicformnewperson/$', views.NewIndividualMember.as_view(), name='basicformnewperson'),
    url(r'^basicformnewfamily/$', views.NewFamilyMember.as_view(), name='basicformnewfamily'),
    url(r'^membership/$', views.MembershipList.as_view(), name='membership-list'),
    url(r'^membership/(?P<pk>[^/]+)/$', views.MembershipDetail.as_view(), name='membership-detail'),

]


from datetime import datetime, timedelta
from unittest.mock import AsyncMock

from framework.clients.cache_client import CacheClientAsync
from framework.di.service_collection import ServiceCollection

from clients.azure_gateway_client import AzureGatewayClient
from clients.email_gateway_client import EmailGatewayClient
from clients.twilio_gateway import TwilioGatewayClient
from tests.buildup import ApplicationBase

class TwilioGatewayClientTests(ApplicationBase):
    def configure_services(self, service_collection: ServiceCollection):
        self.mock_cache_client = AsyncMock()

        def get_mock_cache_client(services):
            return self.mock_cache_client

        service_collection.add_singleton(
            dependency_type=CacheClientAsync,
            factory=get_mock_cache_client)

    def get_client(
        self
    ) -> TwilioGatewayClient:
        self.mock_cache_client.get_cache.return_value = None
        return self.resolve(TwilioGatewayClient)

    # async def test_send_sms(self):
    #     client = self.get_client()

    #     response = await client.send_sms(
    #         recipient='+18563323608',
    #         message='test')

    #     self.assertIsNotNone(response)

class EmailGatewayClientTests(ApplicationBase):
    def configure_services(self, service_collection: ServiceCollection):
        self.mock_cache_client = AsyncMock()

        def get_mock_cache_client(services):
            return self.mock_cache_client

        service_collection.add_singleton(
            dependency_type=CacheClientAsync,
            factory=get_mock_cache_client)

    def get_client(
        self
    ) -> EmailGatewayClient:
        self.mock_cache_client.get_cache.return_value = None
        return self.resolve(EmailGatewayClient)

    async def test_send_email(self):
        client = self.get_client()

        response = await client.send_email(
            subject='Test',
            recipient='me@dan-leonard.com',
            message='Test')

        self.assertIsNotNone(response)

    async def test_send_datatable_email(self):
        client = self.get_client()

        data = [{'row': 'value'}]

        response = await client.send_datatable_email(
            subject='Test',
            recipient='me@dan-leonard.com',
            data=data)

        self.assertIsNotNone(response)

    async def test_send_json_email(self):
        client = self.get_client()

        data = [{'row': 'value'}]

        response = await client.send_json_email(
            subject='Test',
            recipient='me@dan-leonard.com',
            data=data)

        self.assertIsNotNone(response)

class AzureGatewayClientTests(ApplicationBase):
    def configure_services(self, service_collection: ServiceCollection):
        self.mock_cache_client = AsyncMock()

        def get_mock_cache_client(services):
            return self.mock_cache_client

        service_collection.add_singleton(
            dependency_type=CacheClientAsync,
            factory=get_mock_cache_client)

    def get_client(
        self
    ) -> AzureGatewayClient:
        self.mock_cache_client.get_cache.return_value = None
        return self.resolve(AzureGatewayClient)

    async def test_get_pod_images(self):
        client = self.get_client()

        response = await client.get_pod_images()

        self.assertIsNotNone(response)

    async def test_get_acr_manifests(self):
        client = self.get_client()

        response = await client.acr_get_manifests(
            repository_name='framework')

        self.assertIsNotNone(response)

    async def test_get_repositories(self):
        client = self.get_client()

        response = await client.acr_get_repositories()

        self.assertIsNotNone(response)

    async def test_get_cost_management_data(self):
        client = self.get_client()

        start_date = datetime.now() - timedelta(days=1)
        end_date = datetime.now()

        response = await client.get_cost_management_data(
            start_date=start_date.strftime('%Y-%m-%d'),
            end_date=end_date.strftime('%Y-%m-%d'))

        self.assertIsNotNone(response)

    async def test_get_pods(self):
        client = self.get_client()

        response = await client.get_pods()

        self.assertIsNotNone(response)

    async def test_get_logs(self):
        client = self.get_client()

        pods_response = await client.get_pods()
        pods = pods_response.get('pods')

        response = await client.get_logs(
            namespace=pods[0].get('name'),
            pod=pods[0].get('namespace'))

        self.assertIsNotNone(response)


import tensorflow as tf
import matplotlib.pyplot as plt

cifar10=tf.keras.datasets.cifar10
(x_train,y_train),(x_test,y_test)=cifar10.load_data()

plt.imshow(x_train[0]) #绘制图片
plt.show()

print("x_train[0]:\n",x_train[0])

print(y_train)

print(x_test.shape)


from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import glob
import os
import shutil

required_conan_version = ">=1.32.0"

class VulkanValidationLayersConan(ConanFile):
    name = "vulkan-validationlayers"
    description = "Khronos official Vulkan validation layers for Windows, Linux, Android, and MacOS."
    license = "Apache-2.0"
    topics = ("conan", "vulkan-validation-layers", "vulkan", "validation-layers")
    homepage = "https://github.com/KhronosGroup/Vulkan-ValidationLayers"
    url = "https://github.com/conan-io/conan-center-index"

    settings = "os", "arch", "compiler", "build_type"
    options = {
        "with_wsi_xcb": [True, False],
        "with_wsi_xlib": [True, False],
        "with_wsi_wayland": [True, False]
    }
    default_options = {
        "with_wsi_xcb": True,
        "with_wsi_xlib": True,
        "with_wsi_wayland": True
    }

    short_paths = True

    exports_sources = ["CMakeLists.txt", "patches/**"]
    generators = "cmake"
    _cmake = None

    @property
    def _source_subfolder(self):
        return "source_subfolder"

    def config_options(self):
        if self.settings.os != "Linux":
            del self.options.with_wsi_xcb
            del self.options.with_wsi_xlib
            del self.options.with_wsi_wayland

    def configure(self):
        if self.settings.compiler.get_safe("cppstd"):
            tools.check_min_cppstd(self, 11)
        if self.settings.compiler == "gcc" and tools.Version(self.settings.compiler.version) < "5":
            raise ConanInvalidConfiguration("gcc < 5 is not supported")

    def requirements(self):
        self.requires("spirv-tools/2020.5", private=True)
        self.requires("vulkan-headers/{}".format(self.version))
        if self.options.get_safe("with_wsi_xcb") or self.options.get_safe("with_wsi_xlib"):
            self.requires("xorg/system")
        if self.options.get_safe("with_wsi_wayland"):
            self.requires("wayland/1.18.0")

    def validate(self):
        if self.options["spirv-tools"].shared:
            raise ConanInvalidConfiguration("vulkan-validationlayers can't depend on shared spirv-tools")

    def source(self):
        tools.get(**self.conan_data["sources"][self.version])
        extracted_dir = glob.glob("Vulkan-ValidationLayers-*")[0]
        os.rename(extracted_dir, self._source_subfolder)

    def build(self):
        self._patch_sources()
        cmake = self._configure_cmake()
        cmake.build()

    def _patch_sources(self):
        for patch in self.conan_data.get("patches", {}).get(self.version, []):
            tools.patch(**patch)
        tools.replace_in_file(os.path.join(self._source_subfolder, "cmake", "FindVulkanHeaders.cmake"),
                              "HINTS ${VULKAN_HEADERS_INSTALL_DIR}/share/vulkan/registry",
                              "HINTS ${VULKAN_HEADERS_INSTALL_DIR}/res/vulkan/registry")

    def _configure_cmake(self):
        if self._cmake:
            return self._cmake
        self._cmake = CMake(self)
        self._cmake.definitions["VULKAN_HEADERS_INSTALL_DIR"] = self.deps_cpp_info["vulkan-headers"].rootpath
        self._cmake.definitions["USE_CCACHE"] = False
        if self.settings.os == "Linux":
            self._cmake.definitions["BUILD_WSI_XCB_SUPPORT"] = self.options.with_wsi_xcb
            self._cmake.definitions["BUILD_WSI_XLIB_SUPPORT"] = self.options.with_wsi_xlib
            self._cmake.definitions["BUILD_WSI_WAYLAND_SUPPORT"] = self.options.with_wsi_wayland
        self._cmake.definitions["BUILD_WERROR"] = False
        self._cmake.definitions["BUILD_TESTS"] = False
        self._cmake.definitions["INSTALL_TESTS"] = False
        self._cmake.definitions["BUILD_LAYERS"] = True
        self._cmake.definitions["BUILD_LAYER_SUPPORT_FILES"] = True
        self._cmake.configure()
        return self._cmake

    def package(self):
        self.copy("LICENSE.txt", dst="licenses", src=self._source_subfolder)
        cmake = self._configure_cmake()
        cmake.install()
        if self.settings.os == "Windows":
            # import lib is useless, validation layers are loaded at runtime
            lib_dir = os.path.join(self.package_folder, "lib")
            tools.remove_files_by_mask(lib_dir, "VkLayer_khronos_validation.lib")
            tools.remove_files_by_mask(lib_dir, "libVkLayer_khronos_validation.dll.a")
        else:
            # Move json files to res, but keep in mind to preserve relative
            # path between module library and manifest json file
            os.rename(os.path.join(self.package_folder, "share"), os.path.join(self.package_folder, "res"))

    def package_info(self):
        self.cpp_info.libs = ["VkLayer_utils"]

        manifest_subfolder = "bin" if self.settings.os == "Windows" else os.path.join("res", "vulkan", "explicit_layer.d")
        vk_layer_path = os.path.join(self.package_folder, manifest_subfolder)
        self.output.info("Appending VK_LAYER_PATH environment variable: {}".format(vk_layer_path))
        self.env_info.VK_LAYER_PATH.append(vk_layer_path)


"""
Flask: Using templates
"""
from asyncore import read
from re import M
from turtle import title
from setup_db import select_students, select_courses
import sqlite3
from sqlite3 import Error
from flask import Flask, render_template, request, redirect, url_for, g

app = Flask(__name__)

DATABASE = './database.db'

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

@app.route("/")
def index():
    # get the database connection
    conn = get_db()

    return render_template("index.html",
                            students=select_students(conn),
                            courses=select_courses(conn)
                        )

@app.route("/courses", methods=["POST", "GET"])
def courses():
    # get the database connection
    if request.method == 'GET':
     return render_template("courses_page.html"
                    # select_students executes SELECT SQL statement on database connetion
                    # returns list of students

    )
    else:
        conn = get_db()
        courseID=request.form['courseid']
        cur = conn.cursor()
        cur.execute("SELECT students.name, grades.course_id, courses.name, grades.grade FROM grades INNER JOIN students on students.student_no = grades.student_no INNER JOIN courses on grades.course_id = courses.id WHERE courses.id = ? ORDER BY grades.grade", (courseID,))
        select_courses_id = []
        for (students_name, course_id, coursesname, gradesgrade) in cur:
                select_courses_id.append({
                    "students.name": students_name,
                    "grades.course_id": course_id,
                    "courses.name": coursesname,
                    "grades.grade": gradesgrade
                })

        print(select_courses_id)
        return render_template("courses_page.html", select_courses_id=select_courses_id)

@app.route("/studentsP", methods=["POST", "GET"])
def studentsP():
    # get the database connection
    conn = get_db()
    studentNo=None

    if request.method == 'GET':

        return render_template("courses_page.html",

    )

    else:
            conn = get_db()
            studentNo=request.form['student_id']
            #studentNo=request.form['data']
            cur = conn.cursor()
            cur2 = conn.cursor()
            cur2.execute("SELECT student_no, name FROM students WHERE student_no = ? ", (studentNo,))
            select_student_name = []
    for (student_n, studentsname) in cur2:
            select_student_name.append({
               "student_no":student_n,
               "name": studentsname
            })
            break

    print("Profile: ",select_student_name)

    cur.execute("SELECT grades.course_id, courses.name, grades.grade FROM grades INNER JOIN students on students.student_no = grades.student_no INNER JOIN courses on grades.course_id = courses.id WHERE students.student_no = ?", (studentNo,))

    select_student_no = []
    for (coursid,coursesname,gradesgrade) in cur:
        select_student_no.append({
            "grades.course_id": coursid,
            "courses.name": coursesname,
            "grades.grade": gradesgrade
            })

        #print(select_student_no.index(coursesname))

    return render_template("students_profile.html",
                        studentss=select_student_no,
                        select_student_nam = select_student_name)

@app.route("/addStudent", methods=["POST", "GET"])
def addStudent():

    if request.method == 'GET':
        # send the form
        return render_template('add_student.html')

    else:
        student_no1 = request.form['student_id_no']
        name1 = request.form['name']
        conn = get_db()
        cur = conn.cursor()

    try:

            sql = ''' INSERT INTO students(student_no,name)
              VALUES(?,?) '''
            cur.execute(sql, (student_no1, name1))
            conn.commit(),

            return redirect(url_for('index'))

    except Error as e: # if error
            # then display the error in 'error.html' page

            return render_template('error.html', msg="Error adding new student.", error=e)
    finally:
            conn.close()

@app.route("/addGrade", methods=["POST", "GET"])
def addGrade():

    if request.method == 'GET':
        conn = get_db()
        grades = [("A"), ("B"), ("C"), ("D"), ("E"), ("F")]
        gra=grades
        cur = conn.cursor()
        #cur.execute("SELECT student_no FROM students WHERE name = ? ", (studentname,))
        cur.execute("SELECT student_no FROM students")
        select_student_name = []
        for (student_n) in cur:
                select_student_name.append({
               "student_no":student_n
            })
                break

        return render_template("add_grade.html",
                    students=select_students(conn),
                    courses=select_courses(conn),
                    gra = gra
                    )

    else:
        conn = get_db()
        cur = conn.cursor()
        studno = request.form['studno']
        course = request.form['course']
        grade = request.form['grade']

    try:

            sql = ''' INSERT INTO grades(student_no,course_id,grade)
              VALUES(?,?,?) '''
            cur.execute(sql, (studno, course, grade))
            conn.commit(),

            return redirect(url_for('index'))

    except Error as e: # if error
            # then display the error in 'error.html' page

            return render_template('error.html', msg="Error adding new grade.", error=e)
    finally:
            conn.close()

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


#!/usr/bin/env python3
from pydantic import BaseModel
from pydantic.schema import schema
from typing import Any

class dumClass(BaseModel):

    A : str = 'Hello'
    B : str = None
    C: bool = False

    def __init__(self, **data: Any):
        print("dumClass was called")
        super().__init__(**data)
        print("dumClass was initialized")
        if self.A == "Hello" :
            self.B = "Goodbye"

    def saySomething(self):
        if self.C is False :
            print("Shhhhhh......")
        else :
            print("A is " + self.A + "; B is " + self.B)

def troubleshoot() :

    print("Testing the dumClass!")
    thisJSON={"A":"Hello","C":True}
    try :
        thisproject = dumClass(**thisJSON)
    except ValidationError as e :
        print(e)

    thisproject.saySomething()

def generateSchema():
    import json
    print(dumClass.schema_json(indent=2))

if __name__ == "__main__":
  generateSchema()
  troubleshoot()