import matplotlib.pyplot as plt

truelist =["A","B","C","D","E","F","G","H","I","J"]

L1=["A","L","B","N","O","P","Q","C","S","T","D","V","W","X","Y"]
L2=["K","A","M","N","O","B","Q","R","S","T","C","V","W","X","D"]

plist = []
rlist = []
i = 0
for item in L1:
    i = i + 1
    tempL1 = L1[0:i]
    sameitem = set(tempL1) & set(truelist)
    num = len(sameitem)
    tp = num
    # precision = tp /(tp+fp) precision是相对你自己的模型预测而言：true positive ／retrieved set。假设你的模型一共预测了100个正例，而其中80个是对的正例，那么你的precision就是80%。我们可以把precision也理解为，当你的模型作出一个新的预测时，它的confidence score 是多少，或者它做的这个预测是对的的可能性是多少。
    precision = tp / i
    # recall =tp/(tp+fn) recall 是相对真实的答案而言： true positive ／ golden set 。假设测试集里面有100个正例，你的模型能预测覆盖到多少，如果你的模型预测到了40个正例，那你的recall就是40%
    recall = tp / len(truelist)

    plist.append(precision)
    rlist.append(recall)

i=0
plist2=[]
rlist2=[]
for item in L2:
    i = i + 1
    tempL2 = L2[0:i]
    sameitem = set(tempL2) & set(truelist)
    num = len(sameitem)
    tp = num
    # precision = tp /(tp+fp)
    precision = tp / i
    # recall =tp/(tp+fn)
    recall = tp / len(truelist)

    plist2.append(precision)
    rlist2.append(recall)

print(plist2)
print(rlist2)
print(recall)
print(plist)

# dates = ['2015-09-12','2015-09-22','2015-12-10','2015-12-20','2015-12-22']
# PM_25 = [80, 55,100,45,56]
# dates = [pd.to_datetime(d) for d in dates]
# plt.xlabel(rlist)
# plt.ylabel(plist)
plt.plot(rlist2, plist2)
plt.show()


#!/usr/bin/python3
def convertor(value):
    if value == 'I':
        return 1
    if value == 'V':
        return 5
    if value == 'X':
        return 10
    if value == 'L':
        return 50
    if value == 'C':
        return 100
    if value == 'D':
        return 500
    if value == 'M':
        return 1000
    return 0

def roman_to_int(roman_string):
    i = 0
    result = 0
    if not roman_string:
        return 0
    if type(roman_string) is not str:
        return 0
    while (i < len(roman_string)):
        current = convertor(roman_string[i])

        if i + 1 < len(roman_string):
            next_s = convertor(roman_string[i + 1])

            if current >= next_s:
                result += current
                i += 1
            else:
                result += next_s - current
                i += 2
        else:
            result += current
            i += 1
    return result


"""
SystemLab-Design Version 19.02
Functional block script: 90 Deg Optical Hybrid
Version 1.0 (19.02 23 Feb 2019)

Refs:
1) Integrated 90deg Hybrid Balanced Receiver Datasheet, Optoplex Corporation, Fremont, CA
Source: http://www.optoplex.com/download/Optoplex%20Integrated%2090deg%20Balanced
%20Receiver%20Brochure%20Rev3.3.pdf (Accessed 21 Feb 2019)
"""

import config

def run(input_signal_data, parameters_input, settings):

    '''==PROJECT SETTINGS==================================================='''
    module_name = '90 deg optical hybrid'
    n = settings['num_samples']
    n = int(round(n))
    iteration = settings['current_iteration']
    fs = settings['sampling_rate']

    # Status message
    if config.sim_status_win_enabled == True:
        config.sim_status_win.textEdit.append('Running ' + module_name +
                                          ' - Iteration #: ' + str(iteration))
    config.status.setText('Running ' + module_name +
                                          ' - Iteration #: ' + str(iteration))
    config.app.processEvents()

    if config.sim_data_activate == True:
        config.sim_data_view.dataEdit.append('Data output for ' + module_name +
                                          ' - Iteration #: ' + str(iteration))

    '''==INPUT PARAMETERS========================================================='''

    '''==INPUT SIGNALS======================================================'''
    signal_type = 'Optical'
    time_array = input_signal_data[0][3]

    optical_in_sig = input_signal_data[0][4]
    wave_key = optical_in_sig[0][0]
    wave_freq = optical_in_sig[0][1]
    e_field_input_sig = optical_in_sig[0][3]
    jones_vector_sig = optical_in_sig[0][2]
    noise_array_sig = optical_in_sig[0][4]
    psd_array_sig = optical_in_sig[0][5]

    optical_in_ref = input_signal_data[1][4]
    wave_key = optical_in_ref[0][0]
    wave_freq = optical_in_ref[0][1]
    e_field_input_ref = optical_in_ref[0][3]
#    jones_vector_ref = optical_in_ref[0][2]
    noise_array_ref = optical_in_ref[0][4]
#    psd_array_ref = optical_in_ref[0][5]

    '''==CALCULATIONS======================================================='''

    # Calculate fields exiting ports 3-6
    # Ref 1 - Port 3: S+L, Port 4: S-L, Port 5: S+jL, Port 6: S-jL
    e_field_output_P3 = 0.5*(e_field_input_sig + e_field_input_ref)
    e_field_output_P4 = 0.5*(e_field_input_sig - e_field_input_ref)
    e_field_output_P5 = 0.5*(e_field_input_sig + 1j*e_field_input_ref)
    e_field_output_P6 = 0.5*(e_field_input_sig - 1j*e_field_input_ref)

    e_field_noise_P3 = 0.5*(noise_array_sig + noise_array_ref)
    e_field_noise_P4 = 0.5*(noise_array_sig - noise_array_ref)
    e_field_noise_P5 = 0.5*(noise_array_sig + 1j*noise_array_ref)
    e_field_noise_P6 = 0.5*(noise_array_sig - 1j*noise_array_ref)

    '''==OUTPUT PARAMETERS LIST============================================='''
    ninety_deg_hybrid_parameters = []
    ninety_deg_hybrid_parameters = parameters_input #If no changes were made to parameters

    '''==RESULTS============================================================'''
    ninety_deg_hybrid_results = [] #No results

    '''==RETURN (Output Signals, Parameters, Results)=========================='''

    optical_P3 = [[wave_key, wave_freq, jones_vector_sig, e_field_output_P3, e_field_noise_P3, psd_array_sig]]
    optical_P4 = [[wave_key, wave_freq, jones_vector_sig, e_field_output_P4, e_field_noise_P4, psd_array_sig]]
    optical_P5 = [[wave_key, wave_freq, jones_vector_sig, e_field_output_P5, e_field_noise_P5, psd_array_sig]]
    optical_P6 = [[wave_key, wave_freq, jones_vector_sig, e_field_output_P6, e_field_noise_P6, psd_array_sig]]

    return ([[3, signal_type, fs, time_array, optical_P3],
             [4, signal_type, fs, time_array, optical_P4],
             [5, signal_type, fs, time_array, optical_P5],
             [6, signal_type, fs, time_array, optical_P6]],
             ninety_deg_hybrid_parameters, ninety_deg_hybrid_results)


import simpy
import random

RANDOM_SEED = 42
SIM_TIME = 100

class BroadcastPipe(object):
    def __init__(self, env, capacity=simpy.core.Infinity):
        self.env = env
        self.capacity = capacity
        self.pipes = []

    def put(self, value):
        if not self.pipes:
            raise RuntimeError('No output pipes.')
        events = [store.put(value) for store in self.pipes]
        return self.env.all_of(events)

    def get_output_conn(self):
        pipe = simpy.FilterStore(self.env, capacity=self.capacity)
        self.pipes.append(pipe)
        return pipe

def message_generator(name, env, out_pipe):
    while True:
        yield env.timeout(random.randint(6, 10))

        if random.random() < 0.5:
            id = 0
        else:
            id = 1

        msg = (id,env.now, f"{name} says hello at {env.now}")
        out_pipe.put(msg)
        print("++++++++++++++++++++++++++++++++++++++++++++++")

def message_consumer(name, env, in_pipe, id):
    while True:
        #msg = yield in_pipe.get()
        msg = yield in_pipe.get(lambda msg_rxed:  msg_rxed[0]==id)

        if msg[1] < env.now:
            print(f"LATE Getting msg: at time {env.now} {name} received {msg[1]}")
        else:
            print(f"at time {env.now} {name} received {msg[1]}")

        yield env.timeout(random.randint(4, 8))

# Setup and start the simulation
print('Process communication')
random.seed(RANDOM_SEED)
# env = simpy.Environment()

# For one-to-one or many-to-one type pipes, use Store
# pipe = simpy.Store(env)
# env.process(message_generator('Generator A', env, pipe))
# env.process(message_consumer('Consumer A', env, pipe))
#
# print('\nOne-to-one pipe communication\n')
# env.run(until=SIM_TIME)

# For one-to many use BroadcastPipe
# (Note: could also be used for one-to-one,many-to-one or many-to-many)
env = simpy.Environment()
bc_pipe = BroadcastPipe(env)

env.process(message_generator('Generator A', env, bc_pipe))
for i in range(0, 100):
    env.process(message_consumer(f'Consumer-{i} ', env, bc_pipe.get_output_conn(), i % 2))

print('\nOne-to-many pipe communication\n')
env.run(until=SIM_TIME)


import os
import numpy as np
import cv2
from pathlib import Path
import matplotlib.pyplot as plt
path_from = str(Path().absolute()) + "/smoothing_input/"

def average_frames_vid(frames):
    # pad each side by an empty frame
    pad = np.zeros(frames[0].shape)
    pad_frames = np.array([pad] + frames + [pad])
    new_frames = np.uint8(pad_frames[:-2]/3 + pad_frames[1:-1]/3 + pad_frames[2:]/3)
    #f_new = cv2.cvtColor(f_new, cv2.COLOR_BGR2RGB)
    return new_frames

def average_frames(frames):
    h, w, _ = frames[-1].shape
    new_frame = np.zeros(frames[0].shape)
    for i, frame in enumerate(frames):
        new_frame = new_frame + frame

    new_frame = new_frame / (i+1)
    return new_frame

def average_frames_warp(frames):
    #Image warping from: https://learnopencv.com/image-alignment-ecc-in-opencv-c-python/
    h, w, _ = frames[-1].shape
    new_frame = np.zeros(frames[0].shape)
    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 1000, 1e-10)

    numFrames = np.zeros(frames[-1][:,:,0].shape)
    for i, frame in enumerate(frames):
        (cc, warp_matrix) = cv2.findTransformECC(frame[:,:,0],frames[-1][:,:,0],np.eye(3, 3, dtype=np.float32), cv2.MOTION_HOMOGRAPHY, criteria,np.ones(frame[:,:,0].shape).astype(np.uint8),5)
        frame = cv2.warpPerspective(frame, warp_matrix, (w,h), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
        # plt.imshow(frame)
        # plt.show()
        new_frame = new_frame + frame
        numFrames[(frame!=0).all(axis=2)] +=1
    numFrames_3 = np.zeros(new_frame.shape)
    for c in range(3):
        numFrames_3[:,:,c] = numFrames
    numFrames_3[numFrames_3==0] = 1
    new_frame = new_frame / numFrames_3
    return new_frame

def video_frames(video):
    cap = cv2.VideoCapture(video)
    ret, frame = cap.read()
    frames = []
    while(1):
        frames.append(frame)
        ret, frame = cap.read()
        if ret==False :
            cap.release()
            cv2.destroyAllWindows()
            break
    return frames

def write_video(new_frames, vid_name):
    fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
    size = new_frames[0].shape[0:2]
    writer = cv2.VideoWriter(name, fourcc, 24, (size[1], size[0]))
    for frame in new_frames:
        writer.write(frame)

if __name__ == '__main__':
    for file in os.listdir(path_from):
        filename = file.split(".")
        if len(filename) == 2:
            name = 'smoothing_output/' + filename[0] + '_new.avi'
            frames = video_frames('smoothing_input/' + file)
            new_frames = average_frames_vid(frames)
            write_video(new_frames, name)


"""
Code for printing values as they are being computed by Theano.

This mode is inspired by nan_guard module in pylearn2.devtools.
"""

__authors__ = "Nicu Tofan"
__copyright__ = "Copyright 2015, Nicu Tofan"
__credits__ = ["Nicu Tofan"]
__license__ = "3-clause BSD"
__maintainer__ = "Nicu Tofan"
__email__ = "nicu.tofan@gmail.com"

import Image
import json
import logging
import os
import shutil
from xml.dom.minidom import parse

try:
    import pydot as pd
    if pd.find_graphviz():
        pydot_imported = True
    else:
        pydot_imported = False
except ImportError:
    pydot_imported = False

from pyl2extra.utils.script import setup_logging, make_argument_parser

class Txml2Html(object):
    """
    Generate html page from xml generated by XmlValMode.

    Parameters
    ----------
    xml_file : str
        Path to input file. It is expected that this file was generated by
        pyl2extra.devtools.printvalmode.XmlValMode.
    output : str
        Path to and base name for the output. ``/a/b/c/d`' will create
        output in ``/a/b/c`` directory; the files to be generated are
        ``d.html``, ``d.png``, ``d.cmapx``.
    Notes
    -----
    The constructor will attempt to create parent directory but not the entire
    path.
    """
    def __init__(self, xml_file, output,
                 html_prefix=None, html_suffix=None,
                 graphid=None):

        self.html_prefix = html_prefix
        self.html_suffix = html_suffix
        self.graphid = graphid

        output = os.path.abspath(output)
        self.xml_file = xml_file
        self.output = output

        logging.debug("input: %s", xml_file)

        self.out_path = os.path.split(output)[0]
        self.out_html = '%s.html' % output
        self.out_png = '%s.png' % output
        self.out_dot = '%s.dot' % output
        self.out_js = '%s.js' % output
        self.out_cmapx = '%s.cmapx' % output
        logging.debug("output: %s", self.out_html)
        logging.debug("output: %s", self.out_png)
        logging.debug("output: %s", self.out_cmapx)

        if not os.path.isdir(self.out_path):
            os.mkdir(self.out_path)

        self.graph = None
        self.nodes = {}
        self.edges = []

        self.reverse_nodes = []
        self.reverse_edges = []
        dom = parse(self.xml_file)
        self.domnodes = dom.getElementsByTagName('node')

        #: a view of the whole dataset; see `process()`
        self.view = None

        super(Txml2Html, self).__init__()

    def gen_full(self):
        """
        Generate the graph for the whole computation.

        This is similar to pydotprint from theano.ptinting in that it shows
        edges between variables and ops, with each variable and each op
        printed a exactly one time.
        """
        self.graphid = 'gfull' if self.graphid is None else self.graphid
        self.graph = pd.Dot(graph_type='digraph',
                            rankdir='TB',
                            graph_name=self.graphid)

        for i, node in enumerate(self.domnodes):
            hval = int(node.getAttribute('hash'))
            nval = '(%d) %s' % (i, node.getAttribute('name'))

            existing, hval = self._add_op(hval, node, True, nval)
            if existing:
                # not the first time we see you
                continue

            inputs = node.getElementsByTagName('inputs')[0]
            for lst in inputs.getElementsByTagName('list'):
                hvar = int(lst.getAttribute('hash'))
                nvar = lst.getAttribute('variable')
                existing, hvar = self._add_var(hvar, lst, True, nvar)
                existing, edge = self._add_edge(hvar, hval)
                if not existing:
                    self._create_edge(edge)

            outputs = node.getElementsByTagName('outputs')[0]
            for lst in outputs.getElementsByTagName('list'):
                hvar = int(lst.getAttribute('hash'))
                nvar = lst.getAttribute('variable')
                existing, hvar = self._add_var(hvar, lst, True, nvar)
                existing, edge = self._add_edge(hval, hvar)
                if not existing:
                    self._create_edge(edge)

        self._finalize()

    def gen_inputs(self):
        """
        Generate the graph for the opperations and the inputs to them.

        The graph has variables on the left side and operations on the
        right side. A variable may be shown multiple times but opperations
        are shown only once, in the order of execution, from top to bottom.
        """
        self.graphid = 'ginputs' if self.graphid is None else self.graphid
        self.graph = pd.Dot(graph_type='digraph',
                            rankdir='LR',
                            graph_name=self.graphid)
        for i, node in enumerate(self.domnodes):
            hval = int(node.getAttribute('hash'))
            nval = '(%d) %s' % (i, node.getAttribute('name'))

            existing, hval = self._add_op(hval, node, True, nval)
            if existing:
                # not the first time we see you
                continue

            inputs = node.getElementsByTagName('inputs')[0]
            for lst in inputs.getElementsByTagName('list'):
                hvar = int(lst.getAttribute('hash'))
                nvar = lst.getAttribute('variable')
                hvar = 'var%sx%d' % (hval, hvar)
                existing, hvar = self._add_var(hvar, lst, True, nvar)
                existing, edge = self._add_edge(hvar, hval)
                if not existing:
                    self._create_edge(edge)

        self._finalize()

    def gen_outputs(self):
        """
        Generate the graph for the opperations and the outputs to them.

        The graph has variables on the right side and operations on the
        left side. A variable may be shown multiple times but opperations
        are shown only once, in the order of execution, from top to bottom.
        """
        self.graphid = 'goutputs' if self.graphid is None else self.graphid
        self.graph = pd.Dot(graph_type='digraph',
                            rankdir='LR',
                            graph_name=self.graphid)
        for i, node in enumerate(self.domnodes):
            hval = int(node.getAttribute('hash'))
            nval = '(%d) %s' % (i, node.getAttribute('name'))

            existing, hval = self._add_op(hval, node, True, nval)
            if existing:
                # not the first time we see you
                continue

            outputs = node.getElementsByTagName('outputs')[0]
            for lst in outputs.getElementsByTagName('list'):
                hvar = int(lst.getAttribute('hash'))
                nvar = lst.getAttribute('variable')
                hvar = 'var%sx%d' % (hval, hvar)
                existing, hvar = self._add_var(hvar, lst, True, nvar)
                existing, edge = self._add_edge(hval, hvar)
                if not existing:
                    self._create_edge(edge)
        self._finalize()

    def gen_inouts(self):
        """
        Generate the graph for the opperations and the outputs to them.

        The graph has variables on the right side and operations on the
        left side. A variable may be shown multiple times but opperations
        are shown only once, in the order of execution, from top to bottom.
        """
        self.graphid = 'ginouts' if self.graphid is None else self.graphid
        self.graph = pd.Dot(graph_type='digraph',
                            rankdir='LR',
                            graph_name=self.graphid)
        for i, node in enumerate(self.domnodes):
            hval = int(node.getAttribute('hash'))
            nval = '(%d) %s' % (i, node.getAttribute('name'))

            existing, hval = self._add_op(hval, node, True, nval)
            if existing:
                # not the first time we see you
                continue

            inputs = node.getElementsByTagName('inputs')[0]
            for lst in inputs.getElementsByTagName('list'):
                hvar = int(lst.getAttribute('hash'))
                nvar = lst.getAttribute('variable')
                hvar = 'input%sx%d' % (hval, hvar)
                existing, hvar = self._add_var(hvar, lst, True, nvar)
                existing, edge = self._add_edge(hvar, hval)
                if not existing:
                    self._create_edge(edge)

            outputs = node.getElementsByTagName('outputs')[0]
            for lst in outputs.getElementsByTagName('list'):
                hvar = int(lst.getAttribute('hash'))
                nvar = lst.getAttribute('variable')
                hvar = 'output%sx%d' % (hval, hvar)
                existing, hvar = self._add_var(hvar, lst, True, nvar)
                existing, edge = self._add_edge(hval, hvar)
                if not existing:
                    self._create_edge(edge)

        self._finalize()

    def parse(self):
        """
        Constructs a view of the data from xml file.

        The structure is organized as follows:
        - at top level it is a dictionary mapping hashes to Op nodes;
        - each Op node has a name, op name, number of inputs and outputs
          and a list of epochs;
        - each epoch consist of a dictionary with two keys: input and output;
          these are on their turn list of variables;
        - variables are dictionaries with a name, hash, data type, shape
          and kind; associated data format depends on kind;
        - for integer, real and cplx types the values are used directly;
        - for vector and matrix they are lists
        - for ndarray they are dictionaries, with keys being the
          address of the matrix and the values matrices (last two dimensions)

        Example
        -------

.. code:

        {
            '12345': {
                    name: "some name",
                    op: "some op",
                    inputs: 3,
                    outputs, 1
                    epochs: [
                        {
                            input: [
                                {
                                    name: "var label"
                                    hash: "456789",
                                    dtype: "uint8",
                                    shape: [1,2,3,4,5]
                                    kind: integer, real, cplx, vector, matrix, ndarray
                                    data:
                                }
                            ],
                            output: [
                                {
                                    name: "var label"
                                    hash: "456789,
                                    dtype: "uint8",
                                    shape: [1,2,3,4,5]
                                    kind: integer, real, cplx, vector, matrix, ndarray
                                    data:
                                }
                            ]
                        }
                    ]
                }
        }

        """
        self.view = {}
        for i, node in enumerate(self.domnodes):
            hval = int(node.getAttribute('hash'))

            # get node data
            if self.view.has_key(hval):
                opnode = self.view[hval]
                epochs = opnode['epochs']
            else:
                epochs = []
                opnode = {'name': node.getAttribute('name'),
                          'op': node.getAttribute('op'),
                          'inputs': int(node.getAttribute('inputs')),
                          'outputs': int(node.getAttribute('outputs')),
                          'epochs': epochs}
                self.view[hval] = opnode

            # create a new epoch for this apparition
            inputs = []
            outputs = []
            epoc = {'input': inputs, 'output': outputs}
            epochs.append(epoc)

            def _get_text(node):
                """Get the text inside an element."""
                nodelist = node.childNodes
                result = []
                for node in nodelist:
                    if node.nodeType == node.TEXT_NODE:
                        result.append(node.data)
                return ''.join(result)

            def _get_part(node, dtype):
                """Get the text and convert it to an array of numbers."""
                txt = _get_text(node).split('|')
                if dtype.startswith('int') or dtype.startswith('uint'):
                    parts = [int(x) for x in txt]
                else:
                    parts = [float(x) for x in txt]
                return parts

            def _do_var(xmlnode, lst):
                """
                Reads a variable and  inserts it into the list.
                """
                hvar = int(xmlnode.getAttribute('hash'))
                varobj = {'name': xmlnode.getAttribute('variable'),
                          'hash': hvar}
                lst.append(varobj)

                valnodes = xmlnode.getElementsByTagName('value')
                if len(valnodes) > 0:
                    valnodes = valnodes[0]
                    val = _get_text(valnodes)
                    try:
                        val = int(val)
                        varobj['dtype'] = 'int'
                        varobj['kind'] = 'integer'
                    except ValueError:
                        val = float(val)
                        varobj['dtype'] = 'float'
                        varobj['kind'] = 'integer'
                    varobj['shape'] = []
                    varobj['data'] = val
                    return

                valnodes = xmlnode.getElementsByTagName('integer')
                if len(valnodes) > 0:
                    valnodes = valnodes[0]
                    varobj['dtype'] = valnodes.getAttribute('dtype')
                    varobj['shape'] = []
                    varobj['kind'] = 'integer'
                    varobj['data'] = int(valnodes.getAttribute('value'))
                    return

                valnodes = xmlnode.getElementsByTagName('real')
                if len(valnodes) > 0:
                    valnodes = valnodes[0]
                    varobj['dtype'] = valnodes.getAttribute('dtype')
                    varobj['shape'] = []
                    varobj['kind'] = 'real'
                    varobj['data'] = float(valnodes.getAttribute('value'))
                    return

                valnodes = xmlnode.getElementsByTagName('cplx')
                if len(valnodes) > 0:
                    valnodes = valnodes[0]
                    varobj['dtype'] = valnodes.getAttribute('dtype')
                    varobj['shape'] = []
                    varobj['kind'] = 'cplx'
                    varobj['data'] = [float(valnodes.getAttribute('r')),
                                      float(valnodes.getAttribute('i'))]
                    return

                valnodes = xmlnode.getElementsByTagName('ndarray')
                if len(valnodes) > 0:
                    valnodes = valnodes[0]
                    varobj['dtype'] = valnodes.getAttribute('dtype')
                    shape = valnodes.getAttribute('shape').split(',')
                    shape = [int(x) for x in shape]
                    varobj['shape'] = shape
                    assert len(shape) > 0

                    parts = xmlnode.getElementsByTagName('part')
                    if len(shape) == 1:
                        varobj['kind'] = 'vector'
                        assert len(parts) == 1
                        lpart = _get_part(parts[0], varobj['dtype'])
                        assert len(lpart) == shape[-1]
                        varobj['data'] = lpart

                    elif len(shape) == 2:
                        varobj['kind'] = 'matrix'
                        assert len(parts) == shape[0]
                        matrix = []
                        for partnode in parts:
                            lpart = _get_part(partnode, varobj['dtype'])
                            assert len(lpart) == shape[-1]
                            matrix.append(lpart)
                        assert len(matrix) == shape[0]
                        varobj['data'] = matrix
                    else:
                        varobj['kind'] = 'ndarray'
                        crt_idx = 0
                        matrix = []
                        ndarray = {}
                        ndaddress = None
                        for partnode in parts:
                            address = partnode.getAttribute('address')
                            address = address.split(',')
                            if not ndaddress:
                                ndaddress = address[:-1]
                            assert len(address) == len(shape) - 1
                            lpart = _get_part(partnode, varobj['dtype'])
                            assert len(lpart) == shape[-1]

                            if address[-2] == crt_idx:
                                matrix.append(lpart)
                            else:
                                ndarray[ndaddress] = matrix
                                crt_idx = address[-2]
                                matrix = [lpart]
                        ndarray[ndaddress] = matrix
                    return

                raise ValueError('Variable %d without value;\n%s' %
                                 (hvar, _get_text(xmlnode)))

            # get input variables
            innodelist = node.getElementsByTagName('inputs')[0]
            for lst in innodelist.getElementsByTagName('list'):
                _do_var(lst, inputs)

            # get output variables
            outnodelist = node.getElementsByTagName('outputs')[0]
            for lst in outnodelist.getElementsByTagName('list'):
                _do_var(lst, outputs)

    def _add_op(self, hval, obj, create_dot=True, label=None):
        """
        Adds a node to internal map and to dot graph.

        If the hash is already present then the object is added to the list,
        otherwise a new list is created.

        Parameters
        ----------
        hval : int or str
            The unique identifier for the node.
        obj : object
            The xml element
        create_dot : bool
            If the node does not exist should we create it?
        label : str
            The label for the node. If None (default) the name is extracted
            from xml node ``obj``.

        Returns
        -------
        existing, hval : bool and str
            ``existing`` is True if the has was seen before, False if
            this is new. ``hval`` has the final identifier.
        """
        existing, hval = self._add_node(hval, obj)
        if not existing:
            if create_dot:
                if label is None:
                    label = obj.getAttribute('name')
                self.reverse_nodes.append({'name': hval,
                                           'label': label,
                                           'style': 'filled',
                                           'fillcolor': '#ffffff',
                                           'shape': 'ellipse',
                                           'URL': '#\\N'})
        return existing, hval

    def _add_var(self, hval, obj, create_dot=True, label=None):
        """
        Adds a node to internal map and to dot graph.

        If the hash is already present then the object is added to the list,
        otherwise a new list is created.

        Parameters
        ----------
        hval : int or str
            The unique identifier for the node.
        obj : object
            The xml element
        create_dot : bool
            If the node does not exist should we create it?
        label : str
            The label for the node. If None (default) the name is extracted
            from xml node ``obj``.

        Returns
        -------
        existing, hval : bool and str
            ``existing`` is True if the has was seen before, False if
            this is new. ``hval`` has the final identifier.
        """
        existing, hval = self._add_node(hval, obj)
        if not existing:
            if create_dot:
                if label is None:
                    label = obj.getAttribute('name')
                self.reverse_nodes.append({'name': hval,
                                           'label': label,
                                           'style': 'filled',
                                           'fillcolor': '#ff0000',
                                           'shape': 'box',
                                           'URL': '#\\N'})
        return existing, hval

    def _add_node(self, hval, obj):
        """
        Adds a node to internal map.

        If the hash is already present then the object is added to the list,
        otherwise a new list is created.

        Parameters
        ----------
        hval : int or str
            The unique identifier for the node.
        obj : object
            The xml element

        Returns
        -------
        existing, hval : bool and str
            ``existing`` is True if the has was seen before, False if
            this is new. ``hval`` has the final identifier.
        """
        if isinstance(hval, int):
            hval = 'n%d' % hval
        if self.nodes.has_key(hval):
            self.nodes[hval].append(obj)
            existing = True
        else:
            self.nodes[hval] = [obj]
            existing = False
        return existing, hval

    def _add_edge(self, src, dst):
        """
        Adds a node to internal map.

        If the hash is already present then the object is added to the list,
        otherwise a new list is created.

        Parameters
        ----------
        src : int or str
            The unique identifier for the source node.
        dst : int or str
            The unique identifier for the destination node.

        Returns
        -------
        existing, edge : bool and tuple
            ``existing`` tells if the edge was seen before;
            ``edge`` has the canonical form for the edge as stored in
            `self.edges`.
        """
        if isinstance(src, int):
            src = 'n%d' % src
        if isinstance(dst, int):
            dst = 'n%d' % dst
        edge = (src, dst)
        if edge in self.edges:
            existing = True
        else:
            self.edges.append(edge)
            existing = False
        return existing, edge

    def _create_edge(self, edge):
        """
        Creates a dot edge.
        """
        self.reverse_edges.append({'src': edge[0], 'dst': edge[1]})
        #        graph.add_edge(pd.Edge(src='n%d' % src,
        #                               dst='n%d' % dst,
        #                               label=lbl,
        #                               labelaligned=True,
        #                               style='',
        #                               color='black'))

    def _finalize(self):
        """
        Create output files.
        """

        if not self.view:
            self.parse()

        for ndins in reversed(self.reverse_nodes):
            nw_node = pd.Node(**ndins)
            self.graph.add_node(nw_node)
        for edgins in reversed(self.reverse_edges):
            dote = pd.Edge(**edgins)
            self.graph.add_edge(dote)

        self.graph.write(self.out_dot, prog='dot', format='dot')
        self.graph.write(self.out_png, prog='dot', format='png')
        self.graph.write(self.out_cmapx, prog='dot', format='cmapx')

        img_file = os.path.split(self.out_png)[1]
        with open(self.out_cmapx, 'rt') as fhand:
            cmapx = fhand.read()

        img = Image.open(self.out_png)
        img_w, img_h = img.size
        img = None

        if not self.html_prefix:
            self.html_prefix = """<!DOCTYPE html>
<html>
<head>
    <title>Theano graph</title>
    <meta name="HandheldFriendly" content="True" />
    <meta name="MobileOptimized" content="320" />
    <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1" />
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width">

    <!-- Bootstrap core CSS -->
    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
    <link rel="stylesheet" href="http://cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css">

   <style>
    body {
        font-family: Helvetica, Arial, sans-serif;
    }
    h1 {
        font-size: 20px;
    }
    img[usemap] {
        border: none;
        height: auto;
        max-width: 100%;
        width: auto;
    }
    .ui-dialog{font-size: 75%;}
    th, tr {
        text-align: center;
    }
    .ui-dialog-titlebar .PinDialog, .ui-dialog-titlebar .DialogPinned {
    	float: left;
        height: 20px;
        margin: 0 5px 0 -10px;
        padding: 1px;
        top: 50%;
        width: 20px;
    }
    </style>

</head>
<body>
<div class="container">

    <h1>Theano graph</h1>

    <div class="row">
        <div class="col-md-12">
"""
        if not self.html_suffix:
            self.html_suffix = """
         </div>
    </div>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!--script src="jquery-1.11.3.js"></script-->
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="http://mattstow.com/experiment/responsive-image-maps/jquery.rwdImageMaps.min.js"></script>
<script src="http://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<!--script src="jquery.dataTables.js"></script-->
<script src="out.js"></script>
<script src="txml2html.js"></script>

<div id="dialoglist" class="hide">
<div id="nodedialog" title="Basic dialog">
    <dl class="dl-horizontal">
        <dt>Name</dt><dd id="ndd_node_name"></dd>
        <dt>Op</dt><dd id="ndd_node_op"></dd>
        <dt>Inputs</dt><dd id="ndd_node_inp"></dd>
        <dt>Outputs</dt><dd id="ndd_node_outp"></dd>
        <dt>Epochs</dt><dd id="ndd_node_ep"></dd>
    </dl>
    <div id="ndd_form"></div>
    <div id="ndd_var_brief"></div>
    <div id="ndd_var_value">
    </div>
</div>
</div>

<script>

</script>
</body>
"""

        with open(self.out_js, 'wt') as fhand:
            fhand.write('(function() { \n  window.%s_data = ' % self.graphid)
            json.dump(self.view, fhand, indent=2, separators=(',', ': '))
            fhand.write(';\n  window.tx2h_data = window.%s_data;\n' % self.graphid)
            fhand.write('\n})();\n')

        # js file with helper routines
        srcjs = os.path.join(os.path.split(os.path.abspath(__file__))[0],
                             'trunk',
                             'txml2html.js')
        dstjs = os.path.join(os.path.split(self.output)[0], 'txml2html.js')
        shutil.copy(srcjs, dstjs)

        with open(self.out_html, 'wt') as fhand:
            fhand.write(self.html_prefix)
            fhand.write('<img src="%s" '
                        'width="%d" '
                        'height="%d" '
                        'usemap="#%s" '
                        'id="img%s" '
                        'alt="" />' % (img_file,
                                       img_w,
                                       img_h,
                                       self.graphid,
                                       self.graphid))
            fhand.write(cmapx)
            fhand.write(self.html_suffix)

def parse_args():
    """
    Add arguments specific for this script to the parser; parse arguments.
    """
    parser = make_argument_parser("Debugger for pylearn2 models.")
    parser.add_argument('--mode',
                        type=str,
                        help='How to arrange nodes in the graph.',
                        choices=['full', 'inputs', 'outputs', 'inouts'],
                        default='full')
    parser.add_argument('xml_file',
                        type=str,
                        help='The file to convert.',
                        default=None)
    parser.add_argument('output',
                        type=str,
                        help='The output path and base file name.',
                        default=None)
    return parser.parse_args()

def main():
    """
    Module entry point.
    """
    args = parse_args()

    # prepare logging
    setup_logging(args)
    logging.debug("Application starting...")

    if not pydot_imported:
        logging.error('pydot could not be imported.')
    else:
        # run based on request
        x2h = Txml2Html(args.xml_file, args.output)
        if args.mode == 'full':
            x2h.gen_full()
        elif args.mode == 'inputs':
            x2h.gen_inputs()
        elif args.mode == 'outputs':
            x2h.gen_outputs()
        elif args.mode == 'inouts':
            x2h.gen_inouts()
        else:
            logging.error('Unknown mode: %s', args.mode)
    logging.debug("Application ended")

if __name__ == '__main__':
    main()


import json
from typing import Tuple

def json_to_bytes(message) -> bytes:
    """
    json deserialize a message and then decode it.
    Use this to convert your json message to bytes before publishing it over the socket.
    :param message: A json serializable list or a dict.
    :return: bytes
    """
    if not (type(message) is list or type(message) is dict):
        raise TypeError(f'Expected a list or dict but got {type(message)} instead.')

    d = json.dumps(message)

    return d.encode('utf-8')

def get_version() -> Tuple[str, str]:
    """
    Get the system firmware version.
    :return: tuple containing version and build number
    """

    with open('/etc/pineapple/version', 'r') as f:
        vers = [l.strip() for l in f.readlines()]
        return vers[0], vers[1]


import json
import re
from enum import Enum
from pathlib import Path
from typing import List
from typing import Pattern

from replayresizer.tools import color16_to_colour, colour_to_color16

def parse_int(value: str):
    try:
        return int(value)
    except ValueError:
        return 0

class CloseAction(Enum):
    CLOSE = 0
    CLOSE_AND_DELETE_SOURCE = 1
    CLOSE_AND_DELETE_RESIZE = 2
    CLOSE_AND_DELETE_ALL = 3

class AutoActionWhen(Enum):
    ON_COMPLETED = 0
    ON_DRAGGED = 1

class AppConfiguration(object):
    def __init__(self, path: Path):
        self._path = path
        # shadow
        self.setup = True
        self.first_start = True
        self.pause = False
        self.rename_format = "{name}_resized.{ext}"
        # generic
        self.input_directory = ""
        self.output_directory = ""
        self.ffmpeg_command = "ffmpeg"
        self.ffprobe_command = "ffprobe"
        self.listen_delay = 0
        self.color = 0x592e31
        self.thumbnail = True
        self.draw_media_info = False
        self.silent_popup = False
        self.ignore_error = False
        self.close_action = 2
        self.close_action_with_shift = 0
        self.auto_close_delay = 5
        self.auto_close_when = 0
        # encode
        self.size_limit = 8170
        self.hq_bitrate = 1200
        self.ulq_bitrate = 260
        self.normalized_volume = True
        self.volume_db = 2.0
        self.volume_normalize_limit_db = 20.0
        #   HQ
        self.hq_fps30 = True
        self.hq_no_audio = False
        self.hq_width = 1280
        self.hq_height = 0
        self.hq_size_adjust = 100.0
        self.hq_encoder_params = "-deadline realtime -cpu-used -8"
        #   LQ
        self.lq_fps30 = True
        self.lq_no_audio = False
        self.lq_width = 640
        self.lq_height = 0
        self.lq_size_adjust = 96.0
        self.lq_encoder_params = "-preset veryfast"
        #   ULQ
        self.ulq_fps16 = True
        self.ulq_no_audio = False
        self.ulq_width = 320
        self.ulq_height = 0
        self.ulq_size_adjust = 96.0
        self.ulq_encoder_params = "-preset veryfast"
        # target files
        self.targets = ["1:\\.mp4$"]
        self.ignores = ["1:^\\."]

        # cached
        self._active_targets = []  # type: List[Pattern]
        self._active_ignores = []  # type: List[Pattern]
        self.compile_targets()

    def load_from_panel(self, panel):
        """
        :type panel: replayresizer.settings_panel.SettingsFrame
        """
        self.input_directory = str(panel.tc_dir_input.GetValue())
        self.output_directory = str(panel.tc_dir_output.GetValue())
        self.ffmpeg_command = str(panel.tc_ffmpeg.GetValue())
        self.ffprobe_command = str(panel.tc_ffprobe.GetValue())
        self.listen_delay = int(panel.spin_listen_delay.GetValue())
        self.color = colour_to_color16(panel.btn_my_color.GetColour())
        self.thumbnail = bool(panel.check_thumbnail.GetValue())
        self.draw_media_info = bool(panel.check_draw_media_info.GetValue())
        self.silent_popup = bool(panel.check_silent_popup.GetValue())
        self.ignore_error = bool(panel.check_ignore_error.GetValue())
        self.close_action = int(panel.choice_close_action.GetSelection())
        self.close_action_with_shift = int(panel.choice_close_action_with_shift.GetSelection())
        self.auto_close_delay = int(panel.spin_auto_close_delay.GetValue())
        self.auto_close_when = int(panel.choice_auto_close_when.GetSelection())
        self.size_limit = int(panel.spin_size_limit.GetValue())
        self.hq_fps30 = bool(panel.check_hq_fps30.GetValue())
        self.hq_no_audio = bool(panel.check_hq_no_audio.GetValue())
        self.hq_width = parse_int(panel.tc_hq_scale_width.GetValue())
        self.hq_height = parse_int(panel.tc_hq_scale_height.GetValue())
        self.hq_size_adjust = float(panel.spin_hq_size_adjust.GetValue())
        self.hq_encoder_params = str(panel.tc_hq_encoder_params.GetValue())
        self.lq_fps30 = bool(panel.check_lq_fps30.GetValue())
        self.lq_no_audio = bool(panel.check_lq_no_audio.GetValue())
        self.lq_width = parse_int(panel.tc_lq_scale_width.GetValue())
        self.lq_height = parse_int(panel.tc_lq_scale_height.GetValue())
        self.lq_size_adjust = float(panel.spin_lq_size_adjust.GetValue())
        self.lq_encoder_params = str(panel.tc_lq_encoder_params.GetValue())
        self.ulq_fps16 = bool(panel.check_ulq_fps16.GetValue())
        self.ulq_no_audio = bool(panel.check_ulq_no_audio.GetValue())
        self.ulq_width = parse_int(panel.tc_ulq_scale_width.GetValue())
        self.ulq_height = parse_int(panel.tc_ulq_scale_height.GetValue())
        self.ulq_size_adjust = float(panel.spin_ulq_size_adjust.GetValue())
        self.ulq_encoder_params = str(panel.tc_ulq_encoder_params.GetValue())
        self.hq_bitrate = parse_int(panel.tc_hq_bitrate.GetValue()) or 1200
        self.ulq_bitrate = parse_int(panel.tc_ulq_bitrate.GetValue()) or 260
        self.normalized_volume = bool(panel.check_normalized_volume.GetValue())
        self.volume_db = float(panel.spin_volume_db.GetValue())
        self.volume_normalize_limit_db = float(panel.spin_volume_normalize_limit_db.GetValue())
        self.targets = [f"{int(panel.list_target.IsChecked(idx))}:{entry}"
                        for idx, entry in enumerate(panel.list_target.GetItems())]
        self.ignores = [f"{int(panel.list_ignore.IsChecked(idx))}:{entry}"
                        for idx, entry in enumerate(panel.list_ignore.GetItems())]
        self.compile_targets()

    def load_to_panel(self, panel):
        """
        :type panel: replayresizer.settings_panel.SettingsFrame
        """
        panel.tc_dir_input.SetValue(str(self.input_directory or ""))
        panel.tc_dir_output.SetValue(str(self.output_directory or ""))
        panel.tc_ffmpeg.SetValue(str(self.ffmpeg_command or ""))
        panel.tc_ffprobe.SetValue(str(self.ffprobe_command or ""))
        panel.spin_listen_delay.SetValue(max(0, int(self.listen_delay or 0)))
        panel.btn_my_color.SetColour(color16_to_colour(int(self.color or 0)))
        panel.check_thumbnail.SetValue(bool(self.thumbnail or False))
        panel.check_draw_media_info.SetValue(bool(self.draw_media_info or False))
        panel.check_silent_popup.SetValue(bool(self.silent_popup or False))
        panel.check_ignore_error.SetValue(bool(self.ignore_error or False))
        panel.choice_close_action.Select(max(0, min(4, int(self.close_action or 0))))
        panel.choice_close_action_with_shift.Select(max(0, min(4, int(self.close_action_with_shift or 0))))
        panel.spin_auto_close_delay.SetValue(max(0, int(self.auto_close_delay or 0)))
        panel.choice_auto_close_when.Select(max(0, min(1, int(self.auto_close_when or 0))))
        panel.spin_size_limit.SetValue(max(200, min(99999999, int(self.size_limit or 0))))
        panel.check_hq_fps30.SetValue(bool(self.hq_fps30 or False))
        panel.check_hq_no_audio.SetValue(bool(self.hq_no_audio or False))
        panel.tc_hq_scale_width.SetValue(str(max(0, int(self.hq_width or 0))))
        panel.tc_hq_scale_height.SetValue(str(max(0, int(self.hq_height or 0))))
        panel.spin_hq_size_adjust.SetValue(float(self.hq_size_adjust or 0))
        panel.tc_hq_encoder_params.SetValue(str(self.hq_encoder_params or ""))
        panel.check_lq_fps30.SetValue(bool(self.lq_fps30 or False))
        panel.check_lq_no_audio.SetValue(bool(self.lq_no_audio or False))
        panel.tc_lq_scale_width.SetValue(str(max(0, int(self.lq_width or 0))))
        panel.tc_lq_scale_height.SetValue(str(max(0, int(self.lq_height or 0))))
        panel.spin_lq_size_adjust.SetValue(float(self.lq_size_adjust or 0))
        panel.tc_lq_encoder_params.SetValue(str(self.lq_encoder_params or ""))
        panel.check_ulq_fps16.SetValue(bool(self.ulq_fps16 or False))
        panel.check_ulq_no_audio.SetValue(bool(self.ulq_no_audio or False))
        panel.tc_ulq_scale_width.SetValue(str(max(0, int(self.ulq_width or 0))))
        panel.tc_ulq_scale_height.SetValue(str(max(0, int(self.ulq_height or 0))))
        panel.spin_ulq_size_adjust.SetValue(float(self.ulq_size_adjust or 0))
        panel.tc_ulq_encoder_params.SetValue(str(self.ulq_encoder_params or ""))
        panel.tc_hq_bitrate.SetValue(str(int(self.hq_bitrate or 1200)))
        panel.tc_ulq_bitrate.SetValue(str(int(self.ulq_bitrate or 260)))
        panel.check_normalized_volume.SetValue(bool(self.normalized_volume or False))
        panel.spin_volume_db.SetValue(float(self.volume_db or 0))
        panel.spin_volume_normalize_limit_db.SetValue(float(self.volume_normalize_limit_db or 0))
        panel.tc_target_value.SetValue("")
        panel.list_target.Clear()
        for entry in self.targets:
            index = panel.list_target.Append(entry[2:])
            if entry.startswith("1:"):
                panel.list_target.Check(index)
        panel.tc_ignore_value.SetValue("")
        panel.list_ignore.Clear()
        for entry in self.ignores:
            index = panel.list_ignore.Append(entry[2:])
            if entry.startswith("1:"):
                panel.list_ignore.Check(index)

    def load_from_json_file(self, *, save_default=True):
        if not self._path.is_file():
            if save_default:
                self.save_to_json_file()
            return

        with self._path.open(encoding="utf-8") as file:
            data = json.load(file)

        for key, value in self.__dict__.items():
            if key.startswith("_") or key not in data:
                continue

            setattr(self, key, data.get(key))

    def save_to_json_file(self):
        Path(self._path.parent).mkdir(parents=True, exist_ok=True)

        if self._path.exists():
            with self._path.open(encoding="utf-8") as file:
                data = json.load(file)
        else:
            data = {}

        for key, value in self.__dict__.items():
            if key.startswith("_"):
                continue

            data[key] = value

        with self._path.open("w", encoding="utf-8") as file:
            json.dump(data, file, ensure_ascii=False, indent=2)

    @property
    def active_targets(self) -> List[Pattern]:
        return self._active_targets

    @property
    def active_ignores(self) -> List[Pattern]:
        return self._active_ignores

    @property
    def close_action_enum(self):
        try:
            return CloseAction(self.close_action)
        except ValueError:
            return CloseAction.CLOSE

    @property
    def close_action_shift_enum(self):
        try:
            return CloseAction(self.close_action_with_shift)
        except ValueError:
            return CloseAction.CLOSE

    @property
    def auto_close_when_enum(self):
        try:
            return AutoActionWhen(self.auto_close_when)
        except ValueError:
            return AutoActionWhen.ON_COMPLETED

    #

    def compile_targets(self):
        self._active_targets = [re.compile(entry[2:]) for entry in self.targets if entry.startswith("1:")]
        self._active_ignores = [re.compile(entry[2:]) for entry in self.ignores if entry.startswith("1:")]


# transforms.py中，Totensor 转化为tensor
# torchvision计算机视觉先关

from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

def transform_test():
    '''
    图片转tensor
    '''
    img_path=''
    img=Image.open(img_path)

    writer = SummaryWriter('./logs')
    # 输入PIL，输出tensor
    tensor_totensor = transforms.ToTensor()#将图片转化为tensor类型
    tensor_img = tensor_totensor(img)#tensor_img是一个tensor类型

    writer.add_image('tensor_test',tensor_img)
    writer.close()

def normalize_test():
    '''
        归一化处理
    '''
    img_path = ''
    img_pil=Image.open(img_path)
    writer = SummaryWriter('./logs')
    tensor_totensor = transforms.ToTensor()  # 将图片转化为tensor类型
    tensor_img = tensor_totensor(img_pil)  # tensor_img是一个tensor类型

    print(tensor_img[0][0][0])
    trans_normalize = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])#用平均值和标准偏差归一化张量图像。只支持输入tensor
    #归一化公式：output[channel] = (input[channel] - mean[channel]) / std[channel]
    #归一化是让数据在一个范围内，从而避免奇异样本数据的影响
    img_nor=trans_normalize(tensor_img)
    print(img_nor[0][0][0])

    writer.add_image('noramlize_test',img_nor)

def img_resize():
    '''
    图片缩放
    '''
    img_path=''
    img_pil=Image.open(img_path)
    trans_resize = transforms.Resize(512)
    # 随机裁剪
    trans_radom = transforms.RandomCrop(512)
    # 输入PIL 输出也是PIL
    resize_img = trans_resize(img_pil)
    # 之后就是转tensor
    tensor_totensor = transforms.ToTensor()

    resize_img = tensor_totensor(resize_img)

def compose_test():
    '''
    Compose中的参数是一个列表
    数据需要是transofroms类型的对象
    相当于就是Compose将操作整合，前面的输出会作为后面的输入（这里要注意前面输出的数据类型是否是后面正确的输入类型）
    '''
    img_path=''
    img=Image.open(img_path)
    trans_resiz=transforms.Resize(512)
    trans_totensor=transforms.ToTensor()
    # 实例化transforms.Compose
    trans_compose=transforms.Compose([trans_resiz,trans_totensor])
    img_tensor = trans_compose(img)
    # 结果查看
    writer = SummaryWriter('./logs')
    # 参数1：tage;参数2：tensor格式的图片数据；参数3：步数
    writer.add_image('Image resize and to tensor',img_tensor,0)

    writer.close()


from parameter import *
from wordvec import *
import data
import rnn_model
import train
import cifar100vgg as cfvg

# 记录在运行程序时后面的字符串，默认是default
def defineArgs():
    parser = argparse.ArgumentParser(description="Chinese_poem_generator.")
    parser.add_argument("-m", "--mode", help="select mode by 'train' or test or head",
                        choices=["train", "test", "head"], default="test")
    return parser.parse_args()

if __name__ == "__main__":
    args = defineArgs()
    # 初始化数据的对象
    traindata = data.poems_data(isEvaluate=False)
    # 进行训练或者生成
    if args.mode == "train":
        train.train(traindata)
    else:
        print("请输入图片的路径:")
        path = input()
        label = cfvg.pic_to_label(path, show_pictures=False)
        print(label)
        poems = train.label_poem(label)


import argparse

parser = argparse.ArgumentParser(add_help=False)

address_group = parser.add_mutually_exclusive_group()

address_group.add_argument('-i',
    nargs=2,
    action='store',
    dest='ip_and_subnetmask',
    help='Accepts an ip address with subnet mask in subnet notation.\
        e.g. ip_subnet -i 192.168.0.22 255.255.255.0'
    )

address_group.add_argument('-c',
    action='store',
    dest='network_prefix',
    help='Accepts an ip address with subnet mask in network prefix notation.\
        e.g. ip_subnet -c 192.168.0.22/24'
    )

parser.add_argument('-v',
    '--version',
    action='version',
    version='v{}'.format('0.9')
    )


def armstrong_number(arr):

    results = []

    for i in range(len(arr)):
        string_num = str(arr[i])
        n = len(string_num)
        answer = 0

        for num in range(len(string_num)):
            new_num = int(string_num[num])
            answer += new_num ** n

        if answer == arr[i]:
            results.append(answer)

    return results

def new_arm(arr):
    results = []

    for num in arr:
        total = 0
        for i in str(num):
            total += int(i) ** len(str(num))

        if total == int(num):
            results.append(total)

    return results

print(armstrong_number((list(range(0,999)))))
print(new_arm((list(range(0,999)))))


import torch
import clip
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import numpy as np
import os
import pickle
os.environ["CUDA_VISIBLE_DEVICES"] = "7"
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

def get_label(ID):
    # opening the file in read mode
    my_file = open('/data2/lzq/Object_Detection/dataset/train/labels/'+ID+'.txt', "r")

    # reading the file
    data = my_file.read()

    # replacing end of line('/n') with ' ' and
    # splitting the text it further when '.' is seen.
    # print(data)
    data_into_list = data.replace('\n', ' ').split(" ")
    # print(data_into_list)
    return data_into_list[0]

total_image_features=[]
total_labels = []
img_names=[]
for image_i in os.listdir('/data2/lzq/Object_Detection/dataset/train/images/'):
    ID_i = image_i[:-4]
    # print(ID_i)
    img_i='/data2/lzq/Object_Detection/dataset/train/images/'+image_i
    image = preprocess(Image.open(img_i)).unsqueeze(0).to(device)
    image_features = model.encode_image(image)
    image_features=image_features.squeeze().detach().cpu().numpy()
    total_image_features.append(image_features.reshape((1,len(image_features))))
    label_i = get_label(ID_i)
    # print(label_i)
    total_labels.append(label_i)
    img_names.append(ID_i)

output_features=np.concatenate(total_image_features,axis=0)

print(np.shape(output_features))
print(len(total_labels))

with open('/data/cheryl/fuwai/card/features.pkl', 'wb') as f:
    pickle.dump(output_features, f)

with open('/data/cheryl/fuwai/card/labels.pkl', 'wb') as f:
    pickle.dump(np.array(total_labels), f)


import sys
sys.stdin = open("C:\\Users\\wlgns\\pythonprogramming\\samsung\\input.txt","r")

n  = int(input())
timeList = []
moneyList = []
dp = [0]*n
for i in range(n):
    t, m = map(int, input().split())
    timeList.append(t)
    if i+t<=n:
        moneyList.append(m)
    else:
        moneyList.append(0)
for i in range(n):
    maxMoney = 0
    for j in range(i):
        if j+ timeList[j]<=i:
            maxMoney = max(maxMoney, dp[j])
    dp[i] = max(dp[i], maxMoney + moneyList[i])

print(max(dp))


from .read_dir import read_dir
from .write_file import write_file
import cv2
import math
import json
from threading import Thread
import numpy as np

def create_test_by_folder_thread(folder, items_per_class, base_dir, path, classId, video_cache):
    subdir = read_dir("{}\\{}".format(base_dir, folder))
    print("[{}] started".format(folder))
    usable_videos = []
    for image in subdir:
        if image[-4:] == ".png":
            if len(usable_videos) < items_per_class:
                file = video_cache[image]  # "{}\{}".format(path,image[:-4])
                print("[{}] {}%".format(folder, round((len(usable_videos) + 1) * 100 / items_per_class, 2)))
                vidcap = cv2.VideoCapture(file)
                vidcap.set(cv2.CAP_PROP_POS_MSEC, (30 * 1000))
                success, img = vidcap.read()
                offset = 1
                while success:
                    cropped_image = img[40:500, 20:480]
                    number_of_gray_pix = np.sum(cropped_image == 126)
                    if number_of_gray_pix > 10_000:
                        cv2.imwrite("data\\test\\{}-{}.png".format(classId, len(usable_videos)), cropped_image)
                        write_file(
                            "data\\test\\{}-{}.txt".format(classId, len(usable_videos)),
                            "{} 0.5 0.5 1 1".format(classId),
                        )
                        usable_videos.append(img[:-4])
                        success = False
                    else:
                        vidcap.set(cv2.CAP_PROP_POS_MSEC, ((30 + 10 * offset) * 1000))
                        success, img = vidcap.read()
                        offset = offset + 1
    print("[{}] done".format(folder))

def generate_testing_images(max_items):
    path = "G:\Videos\Valorant"
    base_dir = "data\\all_images"
    dir = read_dir(base_dir)

    # print(dir)

    video_cache = {}
    with open("data\\all_images.json", "r") as file:
        video_cache = json.loads(file.read().replace("\n", ""))

    items_per_class = math.ceil(max_items / len(dir))

    my_threads = []
    for folder in dir:
        my_threads.append(
            Thread(
                target=create_test_by_folder_thread,
                args=[folder, items_per_class, base_dir, path, len(my_threads), video_cache],
            )
        )
        my_threads[len(my_threads) - 1].start()

    for t in my_threads:
        t.join()
    print("done")


from django.conf.urls import  url
from . import views
app_name = 'tests'
urlpatterns = [
    url(r'^$', views.IndexView.as_view(),name='index'),
    # ex: /test/5/
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    # ex: /test/5/results/
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultView.as_view(), name='results'),
    # ex: /test/5/vote/
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

]


import json
import os
import datetime

from django.views import View
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt

from django.shortcuts import render, redirect
from django.http import HttpResponse

# import urllib2
from django.urls import reverse

# from consts.request_params import RequestParams
from utility import UUID
from utility import send_now

from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth import update_session_auth_hash

from utility.session import Session

from fixkori_api.models import UserClient
from fixkori_api.models import UserServiceProvider
from fixkori_api.models import UserAdmin
from fixkori_api.models import Order
from fixkori_api.models import Area
from fixkori_api.models import Item

import constants

class Home(View):

    @staticmethod
    def get(request):
        if 'token' not in request.session:
            return render(request, 'customer/index.html')

        logged_in_user = Session.get_user_by_session(request.session['token'])
        if logged_in_user is None:
            return render(request, 'customer/index.html')

        if logged_in_user.user_type == constants.USER_TYPE_CLIENT:
            logged_in_user_object = UserClient.objects.get(user=logged_in_user)
            return render(request, 'customer/index.html', {'client': True,
                                                           'logged_in_user_object': logged_in_user_object})

        elif logged_in_user.user_type == constants.USER_TYPE_SERVICE_PROVIDER:
            '''service provider'''
            return render(request, 'admin_vendor/vendor_index.html')

        elif logged_in_user.user_type == constants.USER_TYPE_ADMIN:
            '''admin'''
            yesterday = datetime.date.today() - datetime.timedelta(days=1)
            total_order = Order.objects.all().count()
            pending_order = Order.objects.filter(status=constants.ORDER_STATUS_PENDING).count()
            total_user = UserClient.objects.all().count()
            new_users = UserClient.objects.filter(created_at__gt=yesterday).count()
            total_vendor = UserServiceProvider.objects.all().count()
            service_today = Order.objects.filter(status=constants.ORDER_STATUS_COMPLETED,
                                                 last_updated__gt=yesterday).count()
            last_5_orders = Order.objects.filter(status=constants.ORDER_STATUS_PENDING)[:5]
            last_5_users = UserClient.objects.all()[:5]
            last_5_vendors = UserServiceProvider.objects.all()[:5]
            logged_in_user_object = UserAdmin.objects.get(user=logged_in_user)
            return render(request, 'admin_vendor/admin_index.html', {'admin': True,
                                                                     'total_order': total_order,
                                                                     'logged_in_user_object': logged_in_user_object,
                                                                     'pending_order': pending_order,
                                                                     'total_user': total_user,
                                                                     'new_users': new_users,
                                                                     'total_vendor': total_vendor,
                                                                     'service_today': service_today,
                                                                     'last_5_orders': last_5_orders,
                                                                     'last_5_users': last_5_users,
                                                                     'last_5_vendors': last_5_vendors})

class PlaceOrder(View):

    @staticmethod
    @login_required(login_url='/login/')
    def post(request):
        logged_in_user = Session.get_user_by_session(request.session['token'])
        client = UserClient.objects.get(user=logged_in_user)
        area = Area.objects.get(area_name=request.POST['area'])
        item = Item.objects.get(item_name=request.POST['item'])
        new_order = Order(customer=client,
                          area=area,
                          item=item,
                          brand=request.POST['brand'],
                          model=request.POST['model'],
                          service_type=int(request.POST['service_type']),
                          date=request.POST['date'],
                          address=request.POST['address'],
                          instruction=request.POST['instruction'],
                          description=request.POST['description'])
        new_order.save()
        greeting_text = 'We received your order. Allow us some time, and we will contact you as soon as possible.' \
                        ' Happy Fixing!'
        send_now(client.phone_number, greeting_text)
        send_now('01737233902', 'New order! Please check fixkori!')
        return redirect(reverse('home'))

    @staticmethod
    @login_required(login_url='/login/')
    def get(request):
        logged_in_user = Session.get_user_by_session(request.session['token'])
        client = UserClient.objects.get(user=logged_in_user)
        # todo have to add a order taking page with client info
        return redirect(reverse('home'))

class OrderList(View):

    @staticmethod
    @login_required(login_url='/login/')
    def get(request):
        logged_in_user = Session.get_user_by_session(request.session['token'])
        client = UserClient.objects.get(user=logged_in_user)
        all_orders = Order.objects.filter(customer=client)
        # todo have to add a order taking page with client info and all_order List
        return redirect(reverse('home'))

class OrderDetails(View):

    @staticmethod
    @login_required(login_url='/login/')
    def get(request, oid):
        logged_in_user = Session.get_user_by_session(request.session['token'])
        client = UserClient.objects.get(user=logged_in_user)
        order = Order.objects.get(ok=oid)
        if order.customer == client:
            # send data
            pass
        else:
            # do not send data
            pass
        # todo have to add a order taking page with client info and order details
        return redirect(reverse('home'))

class CancelOrder(View):

    @staticmethod
    @login_required(login_url='/login/')
    def get(request, oid):
        logged_in_user = Session.get_user_by_session(request.session['token'])
        client = UserClient.objects.get(user=logged_in_user)
        order = Order.objects.get(ok=oid)
        if order.customer == client:
            # change order state
            pass
        else:
            # do not change order state
            pass
        # todo have to add a order taking page with client info and order details
        return redirect(reverse('home'))

class UserProfile(View):

    @staticmethod
    @login_required(login_url='/login/')
    def get(request, oid):
        logged_in_user = Session.get_user_by_session(request.session['token'])
        client = UserClient.objects.get(user=logged_in_user)
        # todo have to send user object
        return redirect(reverse('home'))


import random

def compareLottoNum(input,set):
    rightNumberCounter = 0
    for i in range(0,6):
        if input[i] in set:
            rightNumberCounter += 1
    return rightNumberCounter

def inputLottoNum():
    inputNumList = []
    while len(inputNumList) != 6:
        inputNum = int(input("Eingabe der "+str(len(inputNumList)+1)+". Lottozahl: "))
        if inputNum not in inputNumList and 50 > inputNum and inputNum > 0:
            inputNumList.append(inputNum)
        else:
            print("Invalid number "+str(inputNum))
    inputNumList.sort()
    return inputNumList

def randLottoNum():
    lottoNumList = [random.randint(1,49)]
    while len(lottoNumList) != 6:
        randNum = random.randint(1,49)
        if randNum not in lottoNumList:
            lottoNumList.append(randNum)
    lottoNumList.sort()
    return lottoNumList

def lotto():
    inputNumList = inputLottoNum()
    randNumList = randLottoNum()
    print()
    print(F"You numbers are: ",inputNumList)
    print(F"The right numbers are: ",randNumList)
    print(F"You got ",compareLottoNum(inputNumList,randNumList)," right!")

lotto()


from bs4 import BeautifulSoup
from bs4.dammit import EncodingDetector
import requests
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
import matplotlib.pyplot as plt
import sqlite3

# helps render image
from PIL import Image

# path functions to help access the image
from os import path, getcwd

# render image correctly for our function
import numpy as np

# import nltk
# nltk.download('all')

# import scipy.sparse.linalg as sp
# id = np.eye(13)
# vals, vecs = sp.eigsh(id, k=6)
# vals

# natural language processing toolkit to help process our data
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

# create function so that it is more generalized - can be used for other scenarios
def get_soup(html):
    """ get data for web page"""
    resp = requests.get(film_list)
    # if charcleacterset is in the header of content type then we'll convert to lower case or else we will not include it
    http_encoding = resp.encoding if 'charset' in resp.headers.get('content-type', '').lower() else None
    html_encoding = EncodingDetector.find_declared_encoding(resp.content, is_html=True)
    # encoding variable to include if it is http or html
    encoding = html_encoding or http_encoding
    # we'll get content of response from the request we made (see line 23) and we'll use this type of encoding to return soup object, which includes all html content for the website
    soup = BeautifulSoup(resp.content, features="html.parser")
    return soup

def get_links(soup):
    """ Get links from a web page """
    http_link_list = []
    for link in soup.find_all('a', href=True):
        if link['href']!= '/':
            http_link_list.append(link['href'].strip("'"))
    return http_link_list

def get_ps(soup):
    """ get <h1> tags from web page"""
    http_link_list = []
    for link in soup.find_all('h3'):
        http_link_list.append(link.get_text())
    return http_link_list

def get_text(text_array):
    """ get text from an array"""
    text = " ".join(text_array)
    return text

def get_film_text(film_list):
    """get text from all film in list"""
    text_return = []
    for i in film_list:
        print(i)
        soup = get_soup(i)
        text_array = get_ps(soup)
        full_text = get_text(text_array)
        text_return.append(full_text)
    return text_return

def punctuation_stop(text):
    """remove punctuation and stop words"""
    filtered = []
    stop_words = set(stopwords.words('english'))
    word_tokens = word_tokenize(text)
    for w in word_tokens:
        if w not in stop_words and w.isalpha():
            filtered.append(w.lower())
    return filtered

#webpage
film_list = "https://www.timeout.com/film/best-horror-films"

#gets word soup from website
soupout = get_soup(film_list)
soupout

#gets links from website
h_links = get_links(soupout)

html_links = h_links[0:30]

#return list of all episode text
text_return_list = get_film_text(html_links)
all_text = get_text(text_return_list)

#removed punctuation and stop words
filteredlst = punctuation_stop(all_text)

# #list of unwanted words
unwanted = ['noble']

#remove unwanted words
text = " ".join([ele for ele in filteredlst if ele not in unwanted])

#get the working directory
d = getcwd()

#numpy image file of mask image
mask = np.array(Image.open("halloween-logo.jpg"))

#create the word cloud object
wc= WordCloud(background_color="black", max_words=2000, max_font_size=90, random_state=1, mask=mask, stopwords=STOPWORDS)
wc.generate(text)

image_colors = ImageColorGenerator(mask)

plt.figure(figsize=[10,10])
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis('off')
plt.show()


# Program to print this pattern
# *
# **
# ***
# ****
# *****
# ****
# ***
# **
# *

def main():
    k=0
    for i in range(0,5):
        for j in range(0,5):
            if i==j:
                print("*"*i)
                k+=1
            elif i != j:
                print(end="")
        if k == 5:
            break
    for x in range(5,0,-1):
        print("*"*x)

if __name__ == "__main__":
    main()


import keras
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense, LSTM
from keras.optimizers import Adam
from keras import regularizers
from keras import initializers
from keras.layers import *
from keras.models import *
from keras import layers, models, optimizers
from keras import backend as K
import numpy as np
import random
from collections import deque

class Agent:
    def __init__(self, state_size, is_eval=False, model_name=""):
        self.state_size = state_size # normalized previous days
        self.action_size = 3 # sit, buy, sell
        self.memory = deque(maxlen=1000)
        self.inventory = []
        self.model_name = model_name
        self.is_eval = is_eval

        self.gamma = 0.95
        self.epsilon = 1.0
        self.epsilon_min = 0.01
        self.epsilon_decay = 0.995

        self.model = load_model("/models/" + model_name) if is_eval else self._model()

    def _model(self):
        input_tensor = Input((self.state_size,1,))
        x = input_tensor
        x = LSTM(8,return_sequences = True)(x)
        x = LSTM(16,return_sequences = True)(x)
        #x = LSTM(32*2**2,return_sequences = True)(x)
        x = Dropout(0.5)(x)
        x = Flatten()(x)
        x = Dense(self.action_size, activation='linear', kernel_initializer = 'normal')(x)
        model = Model(inputs=input_tensor, outputs=x)
        model.compile(loss="mse", optimizer=Adam(lr=0.001))

        return model

    def act(self, state):
        if not self.is_eval and random.random() <= self.epsilon:
            return random.randrange(self.action_size)

        options = self.model.predict(state)
        return np.argmax(options[0])

    def expReplay(self, batch_size):
        mini_batch = []
        l = len(self.memory)
        for i in range(l - batch_size + 1, l):
            mini_batch.append(self.memory[i])
            #print("mini_batch")

        for state, action, reward, next_state, done in mini_batch:
            target = reward
            #print('brfe if')
            if not done:
                #print('in if before target')
                target = reward + self.gamma * np.amax(self.model.predict(np.reshape(next_state,(next_state.shape[0],next_state.shape[1],1)))[0])
            #print('out if after target')
            target_f = self.model.predict(np.reshape(state,(state.shape[0],state.shape[1],1)))
            target_f[0][action] = target
            self.model.fit(np.reshape(state,(state.shape[0],state.shape[1],1)), target_f, epochs=1, verbose=0)
            #print('after if')
            if self.epsilon > self.epsilon_min:
                self.epsilon *= self.epsilon_decay


#! /Users/xiaotongli/anaconda3/bin/python
# -*- coding: utf-8 -*-
# @Time    : 9/28/18 10:57 PM
# @Author  : Xiaotong Li
# @School  : University of California, Santa Cruz
# @FileName: autocomplete_System.py
# @Software: PyCharm

class Solution:
    def myPow(self, x, n):
        """
        :type x: float
        :type n: int
        :rtype: float
        """

        if n == 0:
            return 1
        if n < 0:
            n = -n
            x = 1/x
        # pow() is a built in function in python3
        # it will return the value of x^y
        if n%2 == 0:
            return pow(x*x, n//2)
        else:
            return pow(x*x, n//2) *x


import time

from cinder import context
from cinder.db.sqlalchemy import models
from cinder.db.sqlalchemy import session as sql_session
from cinder import exception
from cinder import flags
from cinder.openstack.common import log as logging
from cinder import test
from cinder.tests import fake_flags
from cinder.volume import volume_types

FLAGS = flags.FLAGS
LOG = logging.getLogger(__name__)

class VolumeTypeTestCase(test.TestCase):
    """Test cases for volume type code."""
    def setUp(self):
        super(VolumeTypeTestCase, self).setUp()

        self.ctxt = context.get_admin_context()
        self.vol_type1_name = str(int(time.time()))
        self.vol_type1_specs = dict(type="physical drive",
                                    drive_type="SAS",
                                    size="300",
                                    rpm="7200",
                                    visible="True")

    def test_volume_type_create_then_destroy(self):
        """Ensure volume types can be created and deleted."""
        prev_all_vtypes = volume_types.get_all_types(self.ctxt)

        type_ref = volume_types.create(self.ctxt,
                                       self.vol_type1_name,
                                       self.vol_type1_specs)
        new = volume_types.get_volume_type_by_name(self.ctxt,
                                                   self.vol_type1_name)

        LOG.info(_("Given data: %s"), self.vol_type1_specs)
        LOG.info(_("Result data: %s"), new)

        for k, v in self.vol_type1_specs.iteritems():
            self.assertEqual(v, new['extra_specs'][k],
                             'one of fields doesnt match')

        new_all_vtypes = volume_types.get_all_types(self.ctxt)
        self.assertEqual(len(prev_all_vtypes) + 1,
                         len(new_all_vtypes),
                         'drive type was not created')

        volume_types.destroy(self.ctxt, type_ref['id'])
        new_all_vtypes = volume_types.get_all_types(self.ctxt)
        self.assertEqual(prev_all_vtypes,
                         new_all_vtypes,
                         'drive type was not deleted')

    def test_get_all_volume_types(self):
        """Ensures that all volume types can be retrieved."""
        session = sql_session.get_session()
        total_volume_types = session.query(models.VolumeTypes).count()
        vol_types = volume_types.get_all_types(self.ctxt)
        self.assertEqual(total_volume_types, len(vol_types))

    def test_get_default_volume_type(self):
        """Ensures default volume type can be retrieved."""
        type_ref = volume_types.create(self.ctxt,
                                       fake_flags.def_vol_type,
                                       {})
        default_vol_type = volume_types.get_default_volume_type()
        self.assertEqual(default_vol_type.get('name'),
                         fake_flags.def_vol_type)

    def test_default_volume_type_missing_in_db(self):
        """Ensures proper exception raised if default volume type
        is not in database."""
        session = sql_session.get_session()
        default_vol_type = volume_types.get_default_volume_type()
        self.assertEqual(default_vol_type, {})

    def test_non_existent_vol_type_shouldnt_delete(self):
        """Ensures that volume type creation fails with invalid args."""
        self.assertRaises(exception.VolumeTypeNotFound,
                          volume_types.destroy, self.ctxt, "sfsfsdfdfs")

    def test_repeated_vol_types_shouldnt_raise(self):
        """Ensures that volume duplicates don't raise."""
        new_name = self.vol_type1_name + "dup"
        type_ref = volume_types.create(self.ctxt, new_name)
        volume_types.destroy(self.ctxt, type_ref['id'])
        type_ref = volume_types.create(self.ctxt, new_name)

    def test_invalid_volume_types_params(self):
        """Ensures that volume type creation fails with invalid args."""
        self.assertRaises(exception.InvalidVolumeType,
                          volume_types.destroy, self.ctxt, None)
        self.assertRaises(exception.InvalidVolumeType,
                          volume_types.get_volume_type, self.ctxt, None)
        self.assertRaises(exception.InvalidVolumeType,
                          volume_types.get_volume_type_by_name,
                          self.ctxt, None)

    def test_volume_type_get_by_id_and_name(self):
        """Ensure volume types get returns same entry."""
        volume_types.create(self.ctxt,
                            self.vol_type1_name,
                            self.vol_type1_specs)
        new = volume_types.get_volume_type_by_name(self.ctxt,
                                                   self.vol_type1_name)

        new2 = volume_types.get_volume_type(self.ctxt, new['id'])
        self.assertEqual(new, new2)

    def test_volume_type_search_by_extra_spec(self):
        """Ensure volume types get by extra spec returns correct type."""
        volume_types.create(self.ctxt, "type1", {"key1": "val1",
                                                 "key2": "val2"})
        volume_types.create(self.ctxt, "type2", {"key2": "val2",
                                                 "key3": "val3"})
        volume_types.create(self.ctxt, "type3", {"key3": "another_value",
                                                 "key4": "val4"})

        vol_types = volume_types.get_all_types(
            self.ctxt,
            search_opts={'extra_specs': {"key1": "val1"}})
        LOG.info("vol_types: %s" % vol_types)
        self.assertEqual(len(vol_types), 1)
        self.assertTrue("type1" in vol_types.keys())
        self.assertEqual(vol_types['type1']['extra_specs'],
                         {"key1": "val1", "key2": "val2"})

        vol_types = volume_types.get_all_types(
            self.ctxt,
            search_opts={'extra_specs': {"key2": "val2"}})
        LOG.info("vol_types: %s" % vol_types)
        self.assertEqual(len(vol_types), 2)
        self.assertTrue("type1" in vol_types.keys())
        self.assertTrue("type2" in vol_types.keys())

        vol_types = volume_types.get_all_types(
            self.ctxt,
            search_opts={'extra_specs': {"key3": "val3"}})
        LOG.info("vol_types: %s" % vol_types)
        self.assertEqual(len(vol_types), 1)
        self.assertTrue("type2" in vol_types.keys())

    def test_volume_type_search_by_extra_spec_multiple(self):
        """Ensure volume types get by extra spec returns correct type."""
        volume_types.create(self.ctxt, "type1", {"key1": "val1",
                                                 "key2": "val2",
                                                 "key3": "val3"})
        volume_types.create(self.ctxt, "type2", {"key2": "val2",
                                                 "key3": "val3"})
        volume_types.create(self.ctxt, "type3", {"key1": "val1",
                                                 "key3": "val3",
                                                 "key4": "val4"})

        vol_types = volume_types.get_all_types(
            self.ctxt,
            search_opts={'extra_specs': {"key1": "val1",
                                         "key3": "val3"}})
        LOG.info("vol_types: %s" % vol_types)
        self.assertEqual(len(vol_types), 2)
        self.assertTrue("type1" in vol_types.keys())
        self.assertTrue("type3" in vol_types.keys())
        self.assertEqual(vol_types['type1']['extra_specs'],
                         {"key1": "val1", "key2": "val2", "key3": "val3"})
        self.assertEqual(vol_types['type3']['extra_specs'],
                         {"key1": "val1", "key3": "val3", "key4": "val4"})


import requests
import json
import boto3
import botocore.auth
import botocore.awsrequest
import logging
import datetime

from main import DEV_MODE

def get_image_url(url):
  if url.startswith("s3://"):
    return convert_s3_to_http_url(url)
  elif not url.lower().endswith(('.jpg', '.png')):
    return download_and_upload_to_s3(url)
  else:
    return url

def convert_s3_to_http_url(s3_url):
    # Split the S3 URL into bucket and key
    parts = s3_url.replace("s3://", "").split("/")
    bucket = parts[0]
    key = "/".join(parts[1:])

    # Construct and return the HTTP URL
    http_url = f"http://{bucket}.s3.amazonaws.com/{key}"
    return http_url

def download_and_upload_to_s3(image_url):
    """
    Download image from a URL and upload it to Amazon S3 bucket.

    Parameters:
    image_url (str): URL of the image to be downloaded and uploaded.
    bucket_name (str): Name of the S3 bucket.
    s3_file_name (str): Desired file name to be used in S3.
    """
    # Initialize boto3 S3 client
    s3 = boto3.client('s3')

    # Get the current timestamp and convert it to a string
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")

    bucket_name = "bookmark-scrapper-bucket"
    s3_file_name = f"{DEV_MODE.lower()}/uploaded_image_{timestamp}.jpg"
    logging.debug(f"download and upload, url: {image_url}")

    try:
        # Get image from URL
        response = requests.get(image_url, stream=True)
        response.raise_for_status()

        # Ensure the image is treated as binary data
        response.raw.decode_content = True

        # Upload image to S3
        s3.upload_fileobj(response.raw, bucket_name, s3_file_name)
        print(f"Successfully uploaded {s3_file_name} to {bucket_name}")

        public_url = f"https://{bucket_name}.s3.amazonaws.com/{s3_file_name}"
        return public_url

    except requests.exceptions.RequestException as e:
        print(f"Error getting image from URL: {e}")

    except Exception as e:
        print(f"An error occurred: {e}")


from selenium import webdriver
import sys

def test_scores_service():
    driver = webdriver.Chrome()
    driver.get('http://127.0.0.1:5000')
    score_value = driver.find_element('xpath', '//*[@id="score"]')
    score = score_value.text
    if int(score) > 1 and int(score) < 1000:
        return True
    else:
        return False

def main_function():
    if test_scores_service():
        return sys.exit(0)
    else:
        return sys.exit(1)

if __name__ == "__main__":
    main_function()


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

from django.utils.translation import gettext_lazy as _
from horizon import exceptions
from horizon import tabs
from horizon import utils as horizon_utils

from tacker_horizon.openstack_dashboard import api
from tacker_horizon.openstack_dashboard.dashboards.nfv.nsmanager import tables
from tacker_horizon.openstack_dashboard.dashboards.nfv import utils

class NSManagerTab(tabs.TableTab):
    name = _("NSManager Tab")
    slug = "nsmanager_tab"
    table_classes = (tables.NSManagerTable,)
    template_name = ("horizon/common/_detail_table.html")
    preload = False

    def has_more_data(self, table):
        return self._has_more

    def get_nsmanager_data(self):
        try:
            tables.NSManagerItemList.clear_list()
            nss = api.tacker.ns_list(self.request)

            if len(nss) > horizon_utils.functions.get_page_size(
                    self.request):
                self._has_more = True
            else:
                self._has_more = False

            for ns in nss:
                try:
                    ns_desc_str = ns['description']
                except KeyError:
                    ns_desc_str = ""

                vim = ns['vim_id']
                obj = tables.NSManagerItem(
                    ns['name'],
                    ns_desc_str,
                    vim,
                    ns['status'],
                    ns['id'],
                    ns['error_reason'])
                tables.NSManagerItemList.add_item(obj)
            return tables.NSManagerItemList.NSLIST_P
        except Exception:
            self._has_more = False
            error_message = _('Unable to get instances')
            exceptions.handle(self.request, error_message)

            return []

class NSManagerTabs(tabs.TabGroup):
    slug = "nsmanager_tabs"
    tabs = (NSManagerTab,)
    sticky = True

class NSEventsTab(tabs.TableTab):
    name = _("Events Tab")
    slug = "events_tab"
    table_classes = (utils.EventsTable,)
    template_name = ("horizon/common/_detail_table.html")
    preload = False

    def has_more_data(self, table):
        return self._has_more

    def get_events_data(self):
        try:
            utils.EventItemList.clear_list()
            events = api.tacker.events_list(self.request,
                                            self.tab_group.kwargs['ns_id'])

            if len(events) > horizon_utils.functions.get_page_size(
                    self.request):
                self._has_more = True
            else:
                self._has_more = False

            for event in events:
                evt_obj = utils.EventItem(
                    event['id'], event['resource_state'],
                    event['event_type'],
                    event['timestamp'],
                    event['event_details'])
                utils.EventItemList.add_item(evt_obj)
            return utils.EventItemList.EVTLIST_P
        except Exception:
            self._has_more = False
            error_message = _('Unable to get events.')
            exceptions.handle(self.request, error_message)
            return []

class NSDetailsTabs(tabs.TabGroup):
    slug = "NS_details"
    tabs = (NSEventsTab,)
    sticky = True


# -*- coding: utf-8 -*-
"""
Spyder Editor

"""

import os
import re
import zipfile
import tkinter
from tkinter import filedialog, messagebox

def show_error(text):
    tkinter.messagebox.showerror('Error',text)

def show_results(text):
    tkinter.messagebox.showinfo('Results',text)

def count(files):
    #define a dictionary to hold the filename and a count of number of slides:
    decks = {}
    results = ""
    #for each file check if it is a pptx file and set count to 0 or show error message
    for file in files:
        if os.path.abspath(file).endswith('.pptx'):
            decks[(os.path.abspath(file))] = 0
        else:
            show_error('The file %s is not a .pptx file and it will be ignored'%(file))
    #counting slides in each deck
    for deck, count in decks.items():
        try:
            #try to read the file as a zipfile since pptx works like a zipfile
            archive = zipfile.ZipFile(deck,'r')
            contents = archive.namelist()
            #We cannot directly count the len of contents to get the number of slides as the list contains elemetns like 'ppt','slides','docprops',etc.
        except Exception as e:
            #error in reading file
            show_error('Error reading %s (%s). Hence, count set to 0'%(os.path.abspath(deck),e))
        #if no exeption occurred
        else:
            for name in contents:
                if(re.findall('ppt/slides/slide',name)):
                    decks[deck] += 1
    results += ('Slides\tDeck\n')

    for deck, count in decks.items():
        results += ('%s\t\t%s\n'%(count,os.path.basename(deck)))

    if show_summary.get():
        results += ('\n- - - - -\n')
        total = 0
        for count in decks.values():
            total += count
        results += ('Total slides in %s decks: %s'%(len(decks),total))
    show_results(results)

#Creating the GUI to read the pptx files
root = tkinter.Tk()
root.geometry('275x175')
root.title("Slide Counter")

#create file dialogbox to select files
def openfilebutton():
    t = tkinter.filedialog.askopenfilenames()
    count(t)

#Design the dialog box
header = tkinter.Label(root,text='Welcome to Slide Counter!', fg='blue', font=('Times Bold', 18))
header.pack(side='top',ipady = 10)

text = tkinter.Label(root,text='Select some .pptx files, and the\n app will count how many slides\n are contained within them.')
text.pack()

show_summary = tkinter.BooleanVar() #create a variable
show_summary.set(True)
summary = tkinter.Checkbutton(root, text='Show summary', var=show_summary) #create a checkbutton and link it to the bool var
summary.pack(ipady = 10)

openfiles = tkinter.Button(root, text='Choose files', command=openfilebutton)
openfiles.pack(fill="x")

# Initialize Tk window.
root.mainloop()