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

from datetime import timedelta, datetime

import threading
import socket
import struct
import json
import traceback
import logging
import sys

logger = logging.getLogger('pyShelly')

try:
    import http.client as httplib
except:
    import httplib

import sys
if sys.version_info < (3,):
    def ba2c(x): #Convert bytearra to compatible string
        return str(x)
    def b(x):
        return bytearray(x)
    def s(x):
        return str(x)
else:
    def ba2c(x): #Convert bytearra to compatible bytearray
        return x
    def b(x):
        return x
    def s(x):
        return str(x, 'cp1252')

name = "pyShelly"

COAP_IP = "224.0.1.187"
COAP_PORT = 5683

__version__ = "0.0.19"
VERSION = __version__

SHELLY_TYPES = {
    'SHSW-1'     : { 'name': "Shelly 1" },
    'SHSW-21'    : { 'name': "Shelly 2" },
    'SHSW-22'    : { 'name': "Shelly HD Pro" },
    'SHSW-25'    : { 'name': "Shelly 2.5" },
    'SHSW-44'    : { 'name': "Shelly 4 Pro" },
    'SHPLG-1'    : { 'name': "Shelly Plug" },
    'SHPLG2-1'   : { 'name': "Shelly Plug" },
    'SHRGBWW-01' : { 'name': "Shelly RGBWW" },
    'SHBLB-1'    : { 'name': "Shelly Bulb" },
    'SHHT-1'     : { 'name': "Shelly H&T" },
    'SHRGBW2'    : { 'name': "Shelly RGBW2" },
    'SHEM-1'     : { 'name': "Shelly EM" },
    'SHCL-255'   : { 'name': "Shelly Bulb" },
    'SH2LED-1'   : { 'name': "Shelly 2LED" },
    'SHSK-1'     : { 'name': "Shelly Socket" },
}
#SHSEN-1    Sense
#SHSM-01    Smoke

class pyShellyBlock():
    def __init__(self, parent, id, type, ipaddr, code):
        self.id = id
        self.type = type
        self.parent = parent
        self.ipaddr = ipaddr
        self.devices = []
        self.code = code    #DEBUG
        self._setup()

    def update(self, data, ip):
        #logger.debug("BlockUpdate %s", data)
        self.ipaddr = ip    #If changed ip
        for dev in self.devices:
            dev.ipaddr = ip
            dev.update(data)

    def _httpGet(self, url):
        conn = httplib.HTTPConnection(self.ipaddr)
        conn.request("GET", url)
        resp = conn.getresponse()
        body = resp.read()
        respJson = json.loads(body)
        conn.close()
        return respJson

    def _setup(self):
        if self.type == 'SHBLB-1' or self.type == 'SHCL-255':
            self._addDevice( pyShellyBulb(self) )
        elif self.type == 'SHSW-21':
            settings = self._httpGet("/settings")
            if settings['mode'] == 'roller':
                self._addDevice( pyShellyRoller(self) )
            else:
                self._addDevice( pyShellyRelay(self,1,0,2) )
                self._addDevice( pyShellyRelay(self,2,1,2) )
            self._addDevice( pyShellyPowerMeter(self,0,2) )
        elif self.type == 'SHSW-25':
            settings = self._httpGet("/settings")
            if settings['mode'] == 'roller':
                self._addDevice( pyShellyRoller(self) )
            else:
                self._addDevice( pyShellyRelay(self,1,0,1) )
                self._addDevice( pyShellyRelay(self,2,2,3) )
                self._addDevice( pyShellyPowerMeter(self,1,1) )
                self._addDevice( pyShellyPowerMeter(self,2,3) )
        elif self.type == 'SHSW-22':
            self._addDevice( pyShellyRelay(self,1,0,1) )
            self._addDevice( pyShellyRelay(self,2,2,3) )
            self._addDevice( pyShellyPowerMeter(self,1,1) )
            self._addDevice( pyShellyPowerMeter(self,2,3) )
        elif self.type == 'SH2LED-1':
            self._addDevice( pyShellyRGBW2W(self, 0) )
            self._addDevice( pyShellyRGBW2W(self, 1) )
        elif self.type == 'SHEM-1':
            self._addDevice( pyShellyRelay(self,1,0,1) )
        elif self.type == 'SHSW-1' or self.type == 'SHSK-1':
            self._addDevice( pyShellyRelay(self,0 , 0) )
        elif self.type == 'SHSW-44':
            for ch in range(4):
                self._addDevice( pyShellyRelay(self, ch+1, ch*2+1, ch*2) )
        elif self.type == 'SHRGBWW-01':
            self._addDevice( pyShellyRGBWW(self) )
        elif self.type == 'SHPLG-1' or self.type == 'SHPLG2-1':
            self._addDevice( pyShellyRelay(self, 0, 1, 0) )
        elif self.type == 'SHHT-1':
            self._addDevice( pyShellySensor(self) )
        elif self.type == 'SHRGBW2':
            settings = self._httpGet("/settings")
            if settings['mode'] == 'color':
                self._addDevice( pyShellyRGBW2C(self) )
            else:
                for ch in range(4):
                    self._addDevice( pyShellyRGBW2W(self, ch+1) )
        else:
            self._addDevice( pyShellyUnknown(self) )

    def _addDevice(self, dev):
        self.devices.append( dev )
        self.parent._addDevice( dev, self.code )
        return dev

    def _removeDevice(self, dev):
        self.devices.remove( dev )
        self.parent._removeDevice( dev, self.code )
        if len(self.devices)==0:
            self._setup()

class pyShellyDevice(object):
    def __init__(self, block):
        self.block = block
        self.id = block.id
        self.type = block.type
        self.ipaddr = block.ipaddr
        self.cb_updated = []
        self.lastUpdated = None
        self.isDevice = True
        self.isSensor = False
        self.subName = None
        self._unavailableAfterSec = 60
        self.stateValues = None

    def typeName(self):
        try:
            name = SHELLY_TYPES[self.type]['name']
        except:
            name = self.type
        if self.subName is not None:
            name = name + " (" + self.subName + ")"
        return name

    def _sendCommand(self, url):
        conn = httplib.HTTPConnection(self.block.ipaddr)
        conn.request("GET", url)
        resp = conn.getresponse()
        conn.close()

    def available(self):
        if self.lastUpdated is None:
            return False
        diff = datetime.now()-self.lastUpdated
        return diff.total_seconds() < self._unavailableAfterSec

    def _update(self, newState=None, newStateValues=None, newValues=None):
        logger.debug("Update state:%s stateValue:%s values:%s", newState, newStateValues, newValues)
        self.lastUpdated = datetime.now()
        needUpdate = False
        if newState is not None:
            if self.state != newState:
                self.state = newState
                needUpdate = True
        if newStateValues is not None:
            if self.stateValues != newStateValues:
                self.stateValues = newStateValues
                needUpdate = True
        if newValues is not None:
            self.sensorValues = newValues
            needUpdate = True
        if needUpdate:
            self._raiseUpdated()

    def _raiseUpdated(self):
        for callback in self.cb_updated:
            callback()

    def _removeMySelf(self):
        self.block._removeDevice(self)

class pyShellyUnknown(pyShellyDevice):
    def __init__(self, block):
        super(pyShellyUnknown, self).__init__(block)
        self.devType = "UNKNOWN"
    def update(self,data):
        pass

class pyShellyRelay(pyShellyDevice):
    def __init__(self, block, channel, pos, power=None):
        super(pyShellyRelay, self).__init__(block)
        self.id = block.id;
        if channel>0:
            self.id += '-' + str(channel)
            self._channel = channel-1
        else:
            self._channel = 0
        self._pos = pos
        self._power = power
        self.state = None
        self.devType = "RELAY"
        self.isSensor = power is not None

    def update(self,data):
        newState = data['G'][self._pos][2]==1
        newValues = None
        if self._power is not None:
            watt = data['G'][self._power][2]
            newValues = { 'watt' : watt }
        self._update(newState, None, newValues)

    def turnOn(self):
        self._sendCommand( "/relay/" + str(self._channel) + "?turn=on" )

    def turnOff(self):
        self._sendCommand( "/relay/" + str(self._channel) + "?turn=off" )

class pyShellyPowerMeter(pyShellyDevice):
   def __init__(self, block, channel, pos):
       super(pyShellyPowerMeter, self).__init__(block)
       self.id = block.id
       if channel>0:
         self.id += "-" + str(channel)
         self._channel = channel-1
       else:
         self._channel = 0
       self._pos = pos
       self.sensorValues = None
       self.devType = "POWERMETER"

   def update(self,data):
       watt = data['G'][self._pos][2]
       self._update(None, None, { 'watt' : watt })

class pyShellyRoller(pyShellyDevice):
    def __init__(self, block):
        super(pyShellyRoller, self).__init__(block)
        self.id = block.id
        self.devType = "ROLLER"
        self.state = None
        self.position = None
        self.isSensor = True
        self.subName = "Roller"
        self.upsideDown = True

    def update(self,data):
        states = data['G']
        settings = self.block._httpGet("/roller/0")
        self.position = settings['current_pos']
        watt = data['G'][2][2]
        #if not self.invert:
        state = self.position!=0
        #else:
        #   state = self.position==0
        self._update(state, None, { 'watt' : watt } )

    def up(self):
        self._sendCommand( "/roller/0?go=" + ( "open" if not self.upsideDown else "close" ) )

    def down(self):
        self._sendCommand( "/roller/0?go="  + ( "close" if not self.upsideDown else "open" ) )

    def stop(self):
        self._sendCommand( "/roller/0?go=stop" )

class pyShellyLight(pyShellyDevice):
    def __init__(self, block):
        super(pyShellyLight, self).__init__(block)
        self.id = block.id
        self.state = None
        self.devType = "LIGHT"
        self.url = "/light/0"

        self.mode = None
        self.brightness = None
        self.rgb = None
        self.temp = None

        self.supportEffects = True
        self.allowSwitchMode = True
        self.supportColorTemp = False

    def update(self,data):

        settings = self.block._httpGet(self.url)
        logger.debug(settings)

        newState = data['G'][4][2]==1   #151
        mode = settings['mode']

        if mode != self.mode:
            if not self.allowSwitchMode and self.mode is not None:
                self._removeMySelf()
                return
            self.mode = mode

        if self.mode=='color':
            self.brightness = int(settings['gain'])
        else:
            self.brightness = int(settings['brightness'])

        self.rgb = [ data['G'][0][2], data['G'][1][2], data['G'][2][2] ]

        self.temp = int(settings.get('temp',0))

        values = { 'mode' : self.mode, 'brightness': self.brightness, 'rgb' : self.rgb, 'temp':self.temp }
        self._update(newState, values)

    def _sendData(self, state, brightness=None, rgb=None, temp=None, mode=None, effect=None):
        url = self.url + "?"

        if state is not None:
            if not state or brightness==0:
                url += "turn=off"
                self._sendCommand( url )
                return

            url += "turn=on&"

        if mode is not None:
            self._sendCommand( "/settings/?mode=" + mode )
        else:
            mode = self.mode

        if effect is not None:
            self._sendCommand( "/settings/light/0/?effect=" + str(effect) )

        if brightness is not None:
            if mode == "white":
                url += "brightness=" + str(brightness) + "&"
            else:
                url += "gain=" + str(brightness) + "&"

        if rgb is not None:
            url += "red=" + str(rgb[0]) + "&"
            url += "green=" + str(rgb[1]) + "&"
            url += "blue=" + str(rgb[2]) + "&"

        if temp is not None:
            url += "temp=" + str(temp) + "&"

        self._sendCommand( url )

    def turnOn(self, rgb=None, brightness=None, temp=None, mode=None, effect=None):
        self._sendData(True, brightness, rgb, temp, mode, effect)

    def setValues(self, rgb=None, brightness=None, temp=None, mode=None, effect=None):
        self._sendData(None, brightness, rgb, temp, mode, effect)

    def turnOff(self):
        self._sendData(False)

    def getDimValue(self):
        return self.brightness

    def setDimValue(self, value):
        self._sendData(True, value)

class pyShellyBulb(pyShellyLight):
    def __init__(self, block):
        super(pyShellyBulb, self).__init__(block)
        self.supportColorTemp = True

class pyShellyRGBWW(pyShellyLight):
    def __init__(self, block):
        super(pyShellyRGBWW, self).__init__(block)
        self.supportColorTemp = True

class pyShellyRGBW2W(pyShellyLight):
    def __init__(self, block, channel):
        super(pyShellyRGBW2W, self).__init__(block)
        self.id = self.id + '-' + str(channel)
        self._channel = channel-1
        self.mode = "white"
        self.url = "/white/" + str(channel-1)
        self.supportEffects = False
        self.allowSwitchMode = False

    def update(self,data):
        if len(data['G'])==8:
            newState = data['G'][4+self._channel][2]==1
            self.brightness = data['G'][self._channel][2]
            values = { 'mode' : self.mode, 'brightness': self.brightness, 'rgb' : self.rgb, 'temp':self.temp }
            self._update(newState, values)
        else:
            self._removeMySelf()

class pyShellyRGBW2C(pyShellyLight):
    def __init__(self, block):
        super(pyShellyRGBW2C, self).__init__(block)
        self.mode = "color"
        self.url = "/color/0"
        self.supportEffects = False
        self.allowSwitchMode = False

class pyShellySensor(pyShellyDevice):
    def __init__(self, block):
        super(pyShellySensor, self).__init__(block)
        self.id = block.id
        self.state = None
        self.devType = "SENSOR"
        self.isSensor = True
        self.isDevice = False
        self._unavailableAfterSec = 3600*3  #TODO, read from settings

    def update(self,data):
        temp = float(data['G'][0][2])
        humidity = float(data['G'][1][2])
        battery = int(data['G'][2][2])
        if humidity==0:
            try:
                status = self.block._httpGet("/status")
                humidity = status['hum']['value']
            except Exception as e:
                logger.exception("Error receiving humidity: " + str(e))
        self._update(None, None, { 'temperature' : temp, 'humidity' : humidity, 'battery' : battery })

class pyShelly():
    def __init__(self):
        logger.info("Init pyShelly %s", VERSION)
        self.stopped = threading.Event()
        self.blocks = {}
        self.devices = []
        self.cb_deviceAdded = None
        self.cb_deviceRemoved = None
        self.igmpFixEnabled = False #Used if igmp packages not sent correctly

    def open(self):
        self.initSocket()
        self._udp_thread = threading.Thread(target=self._udp_reader)
        self._udp_thread.daemon = True
        self._udp_thread.start()

    def version(self):
        return VERSION

    def initSocket(self):
        s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 10)
        s.bind(('', COAP_PORT))
        mreq = struct.pack("=4sl", socket.inet_aton(COAP_IP), socket.INADDR_ANY)
        s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
        s.settimeout(15)
        self._socket = s

    def close(self):
        self.stopped.set()
        if self._udp_thread is not None:
            self._udp_thread.join()
        try:
            self._socket.shutdown(socket.SHUT_RDWR)
        except socket.error:
            pass
        self._socket.close()

    def discover(self):
        msg = bytes(b'\x50\x01\x00\x0A\xb3cit\x01d\xFF')
        self._socket.sendto(msg, (COAP_IP, COAP_PORT))

    def _addDevice(self, dev, code):
        logger.debug('Add device')
        self.devices.append (dev )
        if self.cb_deviceAdded is not None:
            self.cb_deviceAdded(dev, code)

    def _removeDevice(self, dev, code):
        logger.debug('Remove device')
        self.devices.remove(dev)
        if self.cb_deviceRemoved is not None:
            self.cb_deviceRemoved(dev, code)

    def _udp_reader(self):

        nextIGMPfix = datetime.now() + timedelta(minutes=1)

        while not self.stopped.isSet():

            try:

                #This fix is needed if not sending IGMP reports correct
                if self.igmpFixEnabled and datetime.now()>nextIGMPfix:
                    logger.debug("IGMP fix")
                    nextIGMPfix = datetime.now() + timedelta(minutes=1)
                    mreq = struct.pack("=4sl", socket.inet_aton(COAP_IP), socket.INADDR_ANY)
                    try:
                        self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, mreq)
                    except Exception as e:
                        logger.debug("Can't drop membership, " + str(e))
                    try:
                        self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
                    except Exception as e:
                        logger.debug("Can't add membership, " + str(e))

                logger.debug("Wait for UDP message")

                try:
                    dataTmp, addr = self._socket.recvfrom(500)
                except socket.timeout:
                    continue

                logger.debug("Got UDP message")

                data = bytearray(dataTmp)
                logger.debug(" Data: %s", data)

                byte = data[0]
                ver = byte >> 6
                typex = (byte >> 4) & 0x3
                tokenlen = byte & 0xF

                code = data[1]
                msgid = 256 * data[2] + data[3]

                pos = 4

                logger.debug(' Code: %s', code)

                if code == 30 or code == 69 :

                    byte = data[pos]
                    totDelta = 0

                    devType = "";
                    id = "";

                    while byte!=0xFF:
                        delta = byte >> 4
                        length = byte & 0x0F

                        if delta==13:
                            pos=pos+1
                            delta = data[pos]+13
                        elif delta==14:
                            pos=pos+2
                            delta = data[pos-1]*256 + data[pos] + 269

                        totDelta = totDelta + delta

                        if length==13:
                            pos=pos+1
                            length = data[pos]+13
                        elif length==14:
                            pos=pos+2
                            length = data[pos-1]*256 + data[pos] + 269

                        value = data[pos+1:pos+length]
                        pos = pos + length +1;

                        if totDelta==3332:
                            devType, id, _ = s(value).split('#',2)

                        byte = data[pos]

                    payload = s(data[pos+1:])

                    logger.debug(' Type %s, Id %s, Payload *%s*', devType, id, payload.replace(' ',''))

                    if id not in self.blocks:
                        self.blocks[id] = pyShellyBlock(self, id, devType, addr[0], code)

                    if code==30:
                        self.blocks[id].update(json.loads(payload), addr[0])

            except Exception as e:

                logger.exception("Error receiving UDP: " + str(e) + traceback.format_exc())


import cv2
import numpy as np

stream_ip=str('rtsp://admin:12345678@10.10.10.10/stream0/mobotix.mjpeg')

while True:
    cap = cv2.VideoCapture(stream_ip)
    ret, frame = cap.read()
    if ret == False:
        print("Frame is empty")
        break
    else:
        cv2.imshow('Recording',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
            break
print('Video OK')
cap.release()
cv2.destroyAllWindows()


#!/usr/bin/python3
""" webstatic fabric """
import time
from fabric.api import local
import os.path

def do_pack():
    """ pack my static"""
    timestamp = time.strftime("%Y%m%d%H%M%S")
    path = "versions/web_static_{}.tgz".format(timestamp)
    try:
        if not os.path.exists('versions'):
            local("mkdir -p versions")
        local("tar -cvzf {} web_static".format(path))
        return path
    except Exception:
        return None


#!/usr/bin/env python
import os
import argparse
import urlparse

import requests

base_url = "https://raw.githubusercontent.com/rahulunair/attack_payloads/"
attack_strings = {
    "null": b"\x00",
    "crafted": b"\x01\x23\x42" + "/etc/passwd" + b"\x00\x45\x43\x00" * 2**2,
    "tiny": b"\x00" * 2,
    "bill_xml": "",
    "depth_json": ""
}

def giant_null_file(as_file=False, filename=None, size=0):
    """Create a giant binary null file"""
    filename = filename or "giant.bin"
    size = size or 24
    if as_file:
        with open(filename, "wb") as gf:
            gf.write(bytearray(attack_strings["null"]) * (2**size))
        return os.path.abspath("null.bin")
    print(attack_strings["null"] * (2**size))

def tiny_null_files(number=0):
    """Create number of tiny files"""
    number = number or 10
    for num in range(number):
        file_list = []
        with open(str(num) + "_file.bin", "wb") as fh:
            fh.write(attack_strings["tiny"])
        file_list.append(os.path.abspath(fh.name))
    print(file_list)

def crafted_binary(as_file=False):
    if as_file:
        with open("crafted.bin", "wb") as fh:
            fh.write(attack_strings["crafted"])
        return os.path.abspath("crafted.bin")
    print(attack_strings["crafted"])

def billion_laughs():
    """A billion laughs xml"""
    attack_strings["bill_xml"] = requests.get(urlparse.urljoin(
        base_url, "master/data/bill_laughs.xml")).text
    print(attack_strings["bill_xml"])

def crazy_json(as_file=False):
    """A deep nested json file generator"""
    attack_strings["depth_json"] = '{"id":' * 1001 + '42' + '}' * 1001
    if as_file:
        with open("depth_limit.json", "wb") as fh:
            fh.write(attack_strings["depth_json"])
            return os.path.abspath(fh.name)
    print(attack_strings["depth_json"])

def main():
    """Entry point, calls different functions based on option selected."""
    parser = argparse.ArgumentParser()
    parser.add_argument("--as_file",
                        help="Content will be saved as file if present",
                        action="store_true")
    parser.add_argument("--filename",
                        help="Name of the file")
    parser.add_argument("--number",
                        type=int,
                        help="Number of files")
    parser.add_argument("--size",
                        type=int,
                        help="Size of the file")
    group = parser.add_mutually_exclusive_group()
    group.add_argument("--crazy_json",
                       help="A deep nested json",
                       action="store_true")
    group.add_argument("--bill_laughs",
                       help="Billion laughs",
                       action="store_true")
    group.add_argument("--crafted",
                       help="A binary file with /etc/passwd",
                       action="store_true")
    group.add_argument("--tiny_files",
                       help="A set of tiny files",
                       action="store_true")
    group.add_argument("--giant_file",
                       help="A giant null file",
                       action="store_true")
    args = parser.parse_args()

    as_file = args.as_file
    if args.giant_file:
        giant_null_file(as_file, filename=args.filename, size=args.size)
    elif args.tiny_files:
        tiny_null_files(number=args.number)
    elif args.crafted:
        crafted_binary(as_file)
    elif args.bill_laughs:
        billion_laughs()
    elif args.crazy_json:
        crazy_json(as_file)

if __name__ == "__main__":
    main()


import os,stat
import subprocess

def show_status(path='.'):
	output_bytes = subprocess.check_output(['ls','-l',path])
	output_text = output_bytes.decode('utf-8')
	print(output_text)

show_status()
#找当前文件夹下的Python文件，并为文件的拥有者以及相同用户组的成员添加可执行权限
files = os.listdir('.')
for file in files:
	if file.endswith('py'):
		os.chmod(file,os.stat(file).st_mode | stat.S_IXGRP | stat.S_IXUSR)

show_status()


"""Tests for cleverhans.experimental.certification.optimization."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

from cleverhans.experimental.certification import dual_formulation
from cleverhans.experimental.certification import nn
from cleverhans.experimental.certification import optimization

class OptimizationTest(tf.test.TestCase):
  # pylint: disable=missing-docstring

  def prepare_dual_object(self):
    # Function to prepare dual object to be used for testing optimization.
    net_weights = [[[2, 2], [3, 3], [4, 4]], [[1, 1, 1], [-1, -1, -1]]]
    net_biases = [
        np.transpose(np.matrix([0, 0, 0])),
        np.transpose(np.matrix([0, 0]))
    ]
    net_layer_types = ['ff_relu', 'ff']
    nn_params1 = nn.NeuralNetwork(net_weights, net_biases, net_layer_types)

    test_input = np.transpose(np.matrix([0, 0]))
    true_class = 0
    adv_class = 1
    input_minval = 0
    input_maxval = 0
    epsilon = 0.1

    # Creating dual variables to use for optimization
    lambda_pos = [tf.get_variable('lambda_pos0',
                                  initializer=np.random.uniform(
                                      0, 0.1, size=(2, 1)).astype(np.float32)),
                  tf.get_variable('lambda_pos1',
                                  initializer=np.random.uniform(
                                      0, 0.1, size=(3, 1)).astype(np.float32))]
    lambda_neg = [tf.get_variable('lambda_neg0',
                                  initializer=np.random.uniform(
                                      0, 0.1, size=(2, 1)).astype(np.float32)),
                  tf.get_variable('lambda_neg1',
                                  initializer=np.random.uniform(
                                      0, 0.1, size=(3, 1)).astype(np.float32))]
    lambda_quad = [tf.get_variable('lambda_quad0',
                                   initializer=np.random.uniform(
                                       0, 0.1, size=(2, 1)).astype(np.float32)),
                   tf.get_variable('lambda_quad1',
                                   initializer=np.random.uniform(
                                       0, 0.1, size=(3, 1)).astype(np.float32))]
    lambda_lu = [tf.get_variable('lambda_lu0',
                                 initializer=np.random.uniform(
                                     0, 0.1, size=(2, 1)).astype(np.float32)),
                 tf.get_variable('lambda_lu1',
                                 initializer=np.random.uniform(
                                     0, 0.1, size=(3, 1)).astype(np.float32))]
    nu = tf.reshape(tf.get_variable('nu', initializer=200.0,
                                    dtype=tf.float32), shape=(1, 1))
    dual_var = {'lambda_pos': lambda_pos, 'lambda_neg': lambda_neg,
                'lambda_quad': lambda_quad, 'lambda_lu': lambda_lu, 'nu': nu}
    sess = tf.Session()
    dual_formulation_object = dual_formulation.DualFormulation(sess,
                                                               dual_var,
                                                               nn_params1,
                                                               test_input,
                                                               true_class,
                                                               adv_class,
                                                               input_minval,
                                                               input_maxval,
                                                               epsilon)
    return sess, dual_formulation_object

  def test_init(self):
    """ Function to test initialization of OptimizationTest. """
    sess, dual_formulation_object = self.prepare_dual_object()
    dual_formulation_object.set_differentiable_objective()
    sess.run(tf.global_variables_initializer())
    optimization_params = {
        'init_learning_rate': 0.1,
        'learning_rate_decay': 0.9,
        'eig_num_iter': 10,
        'eig_learning_rate': 0.01,
        'init_smooth': 0.5,
        'smooth_decay': 0.9,
        'inner_num_steps': 10,
        'optimizer': 'adam',
        'momentum_parameter': 0.9,
        'eig_type': 'TF'
    }
    optimization_object = optimization.Optimization(dual_formulation_object,
                                                    sess, optimization_params)
    self.assertIsNotNone(optimization_object)

  def test_get_min_eig_vec_proxy(self):
    """ Function test computing min eigen value using matrix vector products."""
    sess, dual_formulation_object = self.prepare_dual_object()
    _, matrix_m = dual_formulation_object.get_full_psd_matrix()
    optimization_params = {
        'init_learning_rate': 0.1,
        'learning_rate_decay': 0.9,
        'eig_num_iter': 2000,
        'eig_learning_rate': 0.01,
        'init_smooth': 0.0,
        'smooth_decay': 0.9,
        'inner_num_steps': 10,
        'optimizer': 'adam',
        'momentum_parameter': 0.9,
        'eig_type': 'TF'
    }
    sess.run(tf.global_variables_initializer())
    optimization_object = optimization.Optimization(dual_formulation_object,
                                                    sess, optimization_params)
    eig_vec = optimization_object.get_min_eig_vec_proxy()
    tf_eig_vec = optimization_object.get_min_eig_vec_proxy(use_tf_eig=True)
    self.assertIsNotNone(eig_vec)

    # Running the graphs and checking that minimum eigen value is correct
    # ** No smoothing
    tf_eig_vec_val, eig_vec_val, matrix_m_val = sess.run(
        [tf_eig_vec, eig_vec, matrix_m],
        feed_dict={
            optimization_object.eig_init_vec_placeholder:
                np.random.rand(6, 1),
            optimization_object.eig_num_iter_placeholder:
                2000,
            optimization_object.smooth_placeholder:
                0.0
        })

    # Eigen value corresponding to v is v^\top M v
    eig_val = np.matmul(
        np.transpose(eig_vec_val), np.matmul(matrix_m_val, eig_vec_val))
    tf_eig_val = np.matmul(
        np.transpose(tf_eig_vec_val), np.matmul(matrix_m_val, tf_eig_vec_val))
    [np_eig_values, _] = np.linalg.eig(matrix_m_val)
    self.assertLess(np.abs(np.min(np_eig_values) - eig_val), 1E-5)
    self.assertLess(np.abs(np.min(np_eig_values) - tf_eig_val), 1E-5)

    # Running the graphs and checking that minimum eigen value is correct
    # **Smoothing
    optimization_params['init_smooth'] = 0.0001
    optimization_object = optimization.Optimization(dual_formulation_object,
                                                    sess, optimization_params)
    eig_vec = optimization_object.get_min_eig_vec_proxy()
    tf_eig_vec = optimization_object.get_min_eig_vec_proxy(use_tf_eig=True)

    tf_eig_vec_val, eig_vec_val, matrix_m_val = sess.run(
        [tf_eig_vec, eig_vec, matrix_m],
        feed_dict={
            optimization_object.eig_init_vec_placeholder:
                np.random.rand(6, 1),
            optimization_object.smooth_placeholder:
                0.1,
            optimization_object.eig_num_iter_placeholder:
                2000
        })

    # Eigen value corresponding to v is v^\top M v
    eig_val = np.matmul(
        np.transpose(eig_vec_val), np.matmul(matrix_m_val, eig_vec_val))
    tf_eig_val = np.matmul(
        np.transpose(tf_eig_vec_val), np.matmul(matrix_m_val, tf_eig_vec_val))
    [np_eig_values, _] = np.linalg.eig(matrix_m_val)
    self.assertLess(np.abs(np.min(np_eig_values) - eig_val), 1E-5)
    # In general, smoothed version can be far off
    self.assertLess(np.abs(np.min(np_eig_values) - tf_eig_val), 1E-1)

  def test_optimization(self):
    """Function to test optimization."""
    sess, dual_formulation_object = self.prepare_dual_object()
    optimization_params = {
        'init_penalty': 10000,
        'large_eig_num_steps': 1000,
        'small_eig_num_steps': 500,
        'inner_num_steps': 10,
        'outer_num_steps': 2,
        'beta': 2,
        'smoothness_parameter': 0.001,
        'eig_learning_rate': 0.01,
        'optimizer': 'adam',
        'init_learning_rate': 0.1,
        'learning_rate_decay': 0.9,
        'momentum_parameter': 0.9,
        'print_stats_steps': 1,
        'stats_folder': None,
        'projection_steps': 200,
        'eig_type': 'TF'
    }
    sess.run(tf.global_variables_initializer())
    optimization_object = optimization.Optimization(dual_formulation_object,
                                                    sess, optimization_params)
    is_cert_found = optimization_object.run_optimization()
    self.assertFalse(is_cert_found)

if __name__ == '__main__':
  tf.test.main()


# pip install eve
from eve import Eve

app = Eve()
# config나 debug 속성에 접근하면 알 수 있겠지만, 아주 철저히 Flask 기반으로 설계되어 있다

if __name__ == '__main__':
    # app이 run하기 위해서는 settings.py에 DOMAIN이라는 이름의 딕셔너리가 선언되어 있어야 한다
    # DOMAIN = {'user': {}}로 선언해 두었는데, 'user'를 위한 자원이 있음을 뜻한다
    app.run(debug=True)
    # Flask와 동일하게 localhost:5000으로 run
    # localhost:5000/에 요청해 보면 아래의 응답을 볼 수 있다
    '''
    {
        "_links": {
            "child": [
                {
                    "href": "user",
                    "title": "user"
                }
            ]
        }
    }
    '''
    # 특정 API 리소스를 호출하지 않았으므로 사용 가능한 모든 리소스가 표시됨
    # localhost:5000/user에 요청을 해보면 이에 대한 응답을 볼 수 있다
    '''
    {
        "_items": [],
        "_links": {
            "parent": {
                "title": "home",
                "href": "/"
            },
            "self": {
                "title": "user",
                "href": "user"
            }
        },
        "_meta": {
            "page": 1,
            "max_results": 25,
            "total": 0
        }
    }
    '''


# Python program for implementation of MergeSort
def mergeSort(arr):

  #write your code here
  def helper(arr, left, right):
    if left<right:
      mid = (left+right)//2
      helper(arr, left, mid)
      helper(arr, mid+1, right)
      merge(arr, left, mid, right)

  def merge(arr, left, mid, right):
    L = arr[left:mid+1]
    R = arr[mid+1:right+1]
    L.append(float('inf'))
    R.append(float('inf'))
    i=j=0

    for x in range(left, right+1):
      if L[i]<=R[j]:
        arr[x] = L[i]
        i+=1
      else:
        arr[x] = R[j]
        j+=1

  helper(arr, 0, len(arr)-1)

# Code to print the list
def printList(arr):

    #write your code here
    print(arr)

# driver code to test the above code
if __name__ == '__main__':
    arr = [12, 11, 13, 5, 6, 7]
    print ("Given array is", end="\n")
    printList(arr)
    mergeSort(arr)
    print("Sorted array is: ", end="\n")
    printList(arr)


from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User

class Classroom(models.Model):
	teacher = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
	name = models.CharField(max_length=120)
	subject = models.CharField(max_length=120)
	year = models.PositiveSmallIntegerField()

	def get_absolute_url(self):
		return reverse('classroom-detail', kwargs={'classroom_id':self.id})

	def __str__(self):
		return self.name

class Student(models.Model):
	MALE = 'M'
	FEMALE = 'F'
	first_name = models.CharField(max_length=60)
	last_name = models.CharField(max_length=60)
	date_of_birth = models.DateField()
	GENDER_CHOICES = [
		(MALE,'Male'),
		(FEMALE, 'Female')
	]
	gender = models.CharField(
		max_length=1,
		choices=GENDER_CHOICES,
		default=MALE,
	)
	exam_grade = models.PositiveSmallIntegerField()
	classroom = models.ForeignKey(Classroom, default=1, on_delete=models.CASCADE)

	def get_absolute_url(self):
		return reverse('classroom-detail', kwargs={'student_id':self.id})


from cosapp.systems import System
from simple_beam.systems import BeamGeo, BeamMeca

class Main(System):
    def setup(self):
        self.add_inward("maintest",0)
        self.add_child(BeamGeo("geo"))
        self.add_child(BeamMeca("meca"))
        self.connect(self.geo.outwards, self.meca.inwards, ["I", "grid"])


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

dxList = []
dyList = []
dxBin = []
dyBin = []
colorList = []
ALPHA = .3
BINSIZE = 1

IMAGEOUT = 'dxdy.png'

#number of chip 2 rows = 40274

if len(sys.argv)==2:
    IMAGEOUT = sys.argv[1]

matchedStarsFile = open('matchedStarsBranches.out')

print("----------------------------------------")
print("Running Script: Average, bin, and plot")

print("\nLoading dx and dy.............")
totalMatches = 0
for line in matchedStarsFile:
    args = line.split()
    dxList.append(float(args[2]))
    dyList.append(float(args[3]))
    colorList.append(float(args[4]))
    totalMatches+=1

#matlab program - chip1
dxAverage1 = -3.278
dyAverage1 = 1.283

#matlab program - chip2
dxAverage2 = -3.192
dyAverage2 = 1.384

c="""
#IRD Python Program  Chip1
dxAverage1=-3.37293888426
dyAverage1=1.28905678601

#IRD Python Program Chip 2
dxAverage2=-3.07407676245
dyAverage2= 1.35405712439"""

print("\nBinning.............")
numBins = int(totalMatches/BINSIZE)
radius = 1
for i in range(numBins):
    sys.stdout.write("\r{0}%".format(100*i/numBins+1))
    sys.stdout.flush()
    curdx = sum(dxList[i*BINSIZE:(i+1)*BINSIZE])/BINSIZE
    curdy = sum(dyList[i*BINSIZE:(i+1)*BINSIZE])/BINSIZE
    if (i < 527/BINSIZE):
        plotX = curdx-dxAverage2
        plotY = curdy-dyAverage2
        if plotX**2+plotY**2<radius:
            dxBin.append(plotX/.08)
            dyBin.append(plotY/.08)
        else:
            colorList.pop(i)
    else:
        plotX = curdx-dxAverage1
        plotY = curdy-dyAverage1
        if plotX**2+plotY**2<radius:
            dxBin.append(plotX/.08)
            dyBin.append(plotY/.08)
        else:
            colorList.pop(i)
    #if dxBin[-1] < -7 or dxBin[-1]>7:
    #    dxBin.pop()
    #    dyBin.pop()
dx1Centroid = 0.0
dy1Centroid = 0.0
dx2Centroid = 0.0
dy2Centroid = 0.0

diskCount = 0 #left side - dx1/dy1
bulgeCount = 0 #right side - dx2/dy2

numBins = int(len(dxBin))

if BINSIZE == 1:
    for i in range(numBins):
        if colorList[i] < 2.05:
            dx1Centroid+=dxBin[i]
            dy1Centroid+=dyBin[i]
            diskCount+=1
            colorList[i] = .75#blue
        else:
            dx2Centroid+=dxBin[i]
            dy2Centroid+=dyBin[i]
            bulgeCount+=1
            colorList[i] = .25#red
    dx1Centroid = dx1Centroid /diskCount
    dy1Centroid = dy1Centroid /diskCount
    dx2Centroid = dx2Centroid /bulgeCount
    dy2Centroid = dy2Centroid /bulgeCount

    print("\nCentroid values......")
    print(dx1Centroid)
    print(dy1Centroid)
    print(dx2Centroid)
    print(dy2Centroid)

print("\n\nCreating plot.............")
#GENERATE AND DISPLAY SCATTER PLOT
if BINSIZE == 1:
    c1 = plt.scatter(dxBin, dyBin,c=colorList, s=30, alpha=ALPHA)
else:
    c1 = plt.scatter(dxBin, dyBin, s=30, alpha=ALPHA)
ax = plt.gca() #get current axis
ax.set_ylabel('dy')
ax.set_xlabel('dx')

#ADD LINES
ax.axhline(dy1Centroid, linestyle='-', color='b') # horizontal lines disk
ax.axvline(dx1Centroid, linestyle='-', color='b') # vertical lines disk

ax.axhline(dy2Centroid, linestyle='-', color='r') # horizontal lines bulge
ax.axvline(dx2Centroid, linestyle='-', color='r') # vertical lines bulge

ax.invert_xaxis()
plt.savefig(IMAGEOUT)
plt.show()


# Write a program that takes two strings and computes the minimum
#  number of edits needed to transform the first string into
# the second string.

def levenshteinDistance(str1, str2):
    # builds array of arrays
    edits = [[x for x in range(len(str1) + 1)] for y in range(len(str2) + 1)]
    # inits the first row with 0, 1,2 3,4
    for i in range(1, len(str2) + 1):
        edits[i][0] = edits[i - 1][0] + 1

    for i in range(1, len(str2) + 1):
        for j in range(1, len(str1) + 1):
            if str2[i - 1] == str1[j-1]:
                edits[i][j] = edits[i - 1][j-1]
            else:
                edits[i][j] = 1 + min(edits[i-1][j-1],
                                      edits[i-1][j], edits[i][j-1])
    return edits[-1][-1]

print(levenshteinDistance("abc", "yabd"))  # 2


from xai.brain.wordbase.nouns._sulfur import _SULFUR

#calss header
class _SULFURED(_SULFUR, ):
	def __init__(self,):
		_SULFUR.__init__(self)
		self.name = "SULFURED"
		self.specie = 'nouns'
		self.basic = "sulfur"
		self.jsondata = {}


# Copyright (C) 2015 Brad Cowie, Christopher Lorier and Joe Stringer.
# Copyright (C) 2015 Research and Education Advanced Network New Zealand Ltd.
#
# 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.

import ipaddr

from conf import Conf

class VLAN(Conf):

    tagged = None
    untagged = None
    vid = None
    controller_ips = None
    ipv4_routes = None
    ipv6_routes = None
    arp_cache = None
    nd_cache = None
    host_cache = None
    bgp_as = None
    bgp_port = None
    bgp_routerid = None
    bgp_neighbor_address = None
    bgp_neighbour_address = None
    bgp_neighbor_as = None
    bgp_neighbour_as = None
    routes = None
    max_hosts = None

    defaults = {
        'name': None,
        'description': None,
        'controller_ips': None,
        'unicast_flood': True,
        'bgp_as': 0,
        'bgp_port': 9179,
        'bgp_routerid': '',
        'bgp_neighbour_address': '',
        'bgp_neighbor_address': None,
        'bgp_neighbour_as': 0,
        'bgp_neighbor_as': None,
        'routes': None,
        'max_hosts': None,
        }

    def __init__(self, _id, dp_id, conf=None):
        if conf is None:
            conf = {}
        self._id = _id
        self.dp_id = dp_id
        self.update(conf)
        self.set_defaults()
        self._id = _id
        self.tagged = []
        self.untagged = []
        self.ipv4_routes = {}
        self.ipv6_routes = {}
        self.arp_cache = {}
        self.nd_cache = {}
        self.host_cache = {}

        if self.controller_ips:
            self.controller_ips = [
                ipaddr.IPNetwork(ip) for ip in self.controller_ips]

        if self.bgp_as:
            assert self.bgp_port
            assert ipaddr.IPv4Address(self.bgp_routerid)
            assert ipaddr.IPAddress(self.bgp_neighbor_address)
            assert self.bgp_neighbor_as

        if self.routes:
            self.routes = [route['route'] for route in self.routes]
            for route in self.routes:
                ip_gw = ipaddr.IPAddress(route['ip_gw'])
                ip_dst = ipaddr.IPNetwork(route['ip_dst'])
                assert ip_gw.version == ip_dst.version
                if ip_gw.version == 4:
                    self.ipv4_routes[ip_dst] = ip_gw
                else:
                    self.ipv6_routes[ip_dst] = ip_gw

    def set_defaults(self):
        for key, value in self.defaults.iteritems():
            self._set_default(key, value)
        self._set_default('vid', self._id)
        self._set_default('name', str(self._id))
        self._set_default('controller_ips', [])
        self._set_default('bgp_neighbor_as', self.bgp_neighbour_as)
        self._set_default('bgp_neighbor_address', self.bgp_neighbour_address)

    def __str__(self):
        port_list = [str(x) for x in self.get_ports()]
        ports = ','.join(port_list)
        return 'vid:%s ports:%s' % (self.vid, ports)

    def get_ports(self):
        return self.tagged + self.untagged

    def mirrored_ports(self):
        return [port for port in self.get_ports() if port.mirror]

    def mirror_destination_ports(self):
        return [port for port in self.get_ports() if port.mirror_destination]

    def flood_ports(self, configured_ports, exclude_unicast):
        ports = []
        for port in configured_ports:
            if not port.running:
                continue
            if exclude_unicast:
                if not port.unicast_flood:
                    continue
            ports.append(port)
        return ports

    def tagged_flood_ports(self, exclude_unicast):
        return self.flood_ports(self.tagged, exclude_unicast)

    def untagged_flood_ports(self, exclude_unicast):
        return self.flood_ports(self.untagged, exclude_unicast)

    def port_is_tagged(self, port_number):
        for port in self.tagged:
            if port.number == port_number:
                return True
        return False

    def port_is_untagged(self, port_number):
        for port in self.untagged:
            if port.number == port_number:
                return True
        return False

    def ip_in_controller_subnet(self, ip):
        for controller_ip in self.controller_ips:
            if ip in controller_ip:
                return True
        return False


from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(r'C:\Users\chinnareddy\OneDrive\Desktop\chromedriver.exe')

driver.maximize_window()

#Replace the file path with html
#for windows it will be like file://C:/Desktop/first.html

driver.get('file:/Users/chinnaredy/Desktop/html & css/selenium_scripts/html/first.html')

#current_url function will not take any arguments and return the current url of the tab
print(driver.current_url)

"""
Switching the focus selenium from one tab to other
in the html on clicking the anchor tag it will open gmail in other window and but the focus of
selenium will with the first tab only
to change focus from one tab to other selenium provide some functions to handle the windows
"""
#current_window_handle will return the window which the selenium is focused on
print(driver.current_window_handle)
#on click the anchor tag it will open the gmail in other tab
driver.find_element_by_tag_name('a').click()
time.sleep(5)
#after opening the new tab selenium will focused on the parent/first one only that you can see
#line number 25 and 31 will print that same because the focus of selenium is not changed
print(driver.current_window_handle)

#windows_handles will return the list with all the tab refernces as string
print(driver.window_handles)

time.sleep(5)
#switch_to_windows will return None but switch the focus of selenium to the specified tab
# here i was focusing to the newly opned tab i.e., second one in the window_hadles list.
driver.switch_to.window(driver.window_handles[1])
time.sleep(5)
#send_keys() method take two arguments one madidate and other is optional string that we enter in the input box.
#Other argument Kyes.ENTER will be discussed later.
#the below line in google.com we typing the text india in searchbox and hit enter of keyword with selenium
driver.find_element_by_name('q').send_keys('india',Keys.ENTER)
time.sleep(5)
#execute_script() function will take the javascript function as argument and execute it
#the below line it executing the function to scroll the window to bottom.
driver.execute_script('window.scrollTo(0,document.body.scrollHeight);')
time.sleep(3)
#this line click the next button in the pagination
#about find_element_by_xpath will be discussed later
driver.find_element_by_xpath("//span[normalize-space()='Next']").click()


import os
import secrets
from distutils.util import strtobool

import pytest
from dotenv import load_dotenv

import threescale_api
from tests.integration.common import HttpClient
from threescale_api.resources import (Service, ApplicationPlan, Application,
                                      Proxy)

load_dotenv()

def get_suffix() -> str:
    return secrets.token_urlsafe(8)

@pytest.fixture(scope='session')
def url() -> str:
    return os.getenv('THREESCALE_PROVIDER_URL')

@pytest.fixture(scope='session')
def token() -> str:
    return os.getenv('THREESCALE_PROVIDER_TOKEN')

@pytest.fixture(scope='session')
def master_url() -> str:
    return os.getenv('THREESCALE_MASTER_URL')

@pytest.fixture(scope='session')
def master_token() -> str:
    return os.getenv('THREESCALE_MASTER_TOKEN')

@pytest.fixture(scope="session")
def ssl_verify() -> bool:
    ssl_verify = os.getenv('THREESCALE_SSL_VERIFY', 'false')
    return bool(strtobool(ssl_verify))

@pytest.fixture(scope='session')
def api_backend() -> str:
    return os.getenv('TEST_API_BACKEND', 'http://www.httpbin.org:80')

@pytest.fixture(scope='session')
def api(url: str, token: str,
        ssl_verify: bool) -> threescale_api.ThreeScaleClient:
    return threescale_api.ThreeScaleClient(url=url, token=token,
                                           ssl_verify=ssl_verify)

@pytest.fixture(scope='session')
def master_api(master_url: str, master_token: str,
               ssl_verify: bool) -> threescale_api.ThreeScaleClient:
    return threescale_api.ThreeScaleClient(url=master_url, token=master_token,
                                           ssl_verify=ssl_verify)

@pytest.fixture(scope='module')
def apicast_endpoint(proxy):
    endpoint = proxy['sandbox_endpoint']
    return endpoint

@pytest.fixture(scope='module')
def apicast_http_client(apicast_endpoint, apicast_params,  ssl_verify):
    http_client = HttpClient(apicast_endpoint, ssl_verify,
                             params=apicast_params)
    return http_client

@pytest.fixture(scope='module')
def apicast_params(application, proxy):
    user_key = application['user_key']
    user_key_param = proxy['auth_user_key']
    return {user_key_param: user_key}

@pytest.fixture(scope='module')
def service_params():
    suffix = get_suffix()
    return dict(name=f"test-{suffix}")

@pytest.fixture(scope='module')
def service(service_params, api) -> Service:
    service = api.services.create(params=service_params)
    yield service
    service.delete()
    assert not service.exists()

@pytest.fixture(scope='module')
def account_params():
    suffix = get_suffix()
    name = f"test-{suffix}"
    return dict(name=name, username=name, org_name=name)

@pytest.fixture(scope='module')
def account(account_params, api):
    entity = api.accounts.create(params=account_params)
    yield entity
    entity.delete()
    assert not entity.exists()

@pytest.fixture(scope='module')
def application_plan_params() -> dict:
    suffix = get_suffix()
    return dict(name=f"test-{suffix}")

@pytest.fixture(scope='module')
def application_plan(api, service, application_plan_params) -> ApplicationPlan:
    resource = service.app_plans.create(params=application_plan_params)
    yield resource

@pytest.fixture(scope='module')
def application_params(application_plan):
    suffix = get_suffix()
    name = f"test-{suffix}"
    return dict(name=name, description=name, plan_id=application_plan['id'])

@pytest.fixture(scope='module')
def application(account, application_plan, application_params) -> Application:
    resource = account.applications.create(params=application_params)
    yield resource
    resource.delete()
    assert not resource.exists()

@pytest.fixture(scope='module')
def proxy(service, application, api_backend) -> Proxy:
    params = {
        'api_backend': api_backend,
        'credentials_location': 'query',
        'api_test_path': '/get',
    }
    proxy = service.proxy.update(params=params)
    return proxy

@pytest.fixture(scope='module')
def metric_params(service):
    suffix = get_suffix()
    friendly_name = f'test-metric-{suffix}'
    system_name = f'{friendly_name}'.replace('-', '_')
    return dict(service_id=service['id'], friendly_name=friendly_name,
                system_name=system_name, unit='count')

@pytest.fixture
def updated_metric_params(metric_params):
    suffix = get_suffix()
    friendly_name = f'test-updated-metric-{suffix}'
    metric_params['friendly_name'] = f'/anything/{friendly_name}'
    metric_params['system_name'] = friendly_name.replace('-', '_')
    return metric_params

@pytest.fixture(scope='module')
def metric(service, metric_params):
    resource = service.metrics.create(params=metric_params)
    yield resource
    resource.delete()
    assert not resource.exists()

@pytest.fixture(scope='module')
def hits_metric(service):
    return service.metrics.read_by(system_name='hits')

@pytest.fixture(scope='module')
def method_params(service):
    suffix = get_suffix()
    friendly_name = f'test-method-{suffix}'
    system_name = f'{friendly_name}'.replace('-', '_')
    return dict(friendly_name=friendly_name, system_name=system_name,
                unit='hits')

@pytest.fixture
def updated_method_params(method_params):
    suffix = get_suffix()
    friendly_name = f'test-updated-method-{suffix}'
    method_params['friendly_name'] = friendly_name
    method_params['system_name'] = f'{friendly_name}'.replace('-', '_')
    return method_params

@pytest.fixture(scope='module')
def method(hits_metric, method_params):
    resource = hits_metric.methods.create(params=method_params)
    yield resource
    resource.delete()
    assert not resource.exists()

def get_mapping_rule_pattern():
    suffix = get_suffix()
    pattern = f'test-{suffix}'.replace('_', '-')
    return pattern

@pytest.fixture(scope='module')
def mapping_rule_params(hits_metric):
    return dict(http_method='GET', pattern='/', metric_id=hits_metric['id'],
                delta=1)

@pytest.fixture
def updated_mapping_rules_params(mapping_rule_params):
    pattern = get_mapping_rule_pattern()
    params = mapping_rule_params.copy()
    params['pattern'] = f'/anything/{pattern}'
    return params

@pytest.fixture(scope='module')
def mapping_rule(proxy, mapping_rule_params):
    resource = proxy.mapping_rules.create(params=mapping_rule_params)
    yield resource
    resource.delete()
    assert not resource.exists()

@pytest.fixture
def create_mapping_rule(service):
    rules = []
    proxy = service.proxy.list()

    def _create(metric, http_method, path):
        params = dict(service_id=service['id'],
                      http_method=http_method,
                      pattern=f'/anything{path}',
                      delta=1, metric_id=metric['id'])
        rule = proxy.mapping_rules.create(params=params)
        rules.append(rule)
        return rule

    yield _create

    for rule in rules:
        if rule.exists():
            rule.delete()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re

from utils.reExpression import replace_quotes_1, replace_brackets_1, replace_brackets_2 \
    , replace_brackets_3, replace_brackets_4, replace_brackets_5, replace_brackets_6, replace_brackets_7 \
    , replace_brackets_8, replace_brackets_9, replace_clock_time, replace_brackets_1, replace_quotes_2, \
    replace_quotes_3, replace_brackets_10
"""
各场景语料命中率
"""
regex4 = re.compile(
    '[\±+_\-—–·&‰%¢$£¥₱€#@†*‡★؟\‚\:;¿?/,….~`|♪•♣♠♥♦√πΠ÷×§¶∆≠=≈∞°↑^←‚\:;¿?/,….~`|♪•♣♠♥♦√πΠ÷×§¶←↓→\\©®™℅,ـًٌٍَُِّْٰٖٕٓٔ¹½⅓¼⅛²⅔³¾⅜⅝⁴⅞@#¢₱€£¥%٪‰&_\-—–·+±\\﴾*★٭\‚›‹:;؛?؟∆≠=≈!∞°،↑)}\]’’>›»”’↓→\±+_\-—–·&‰٪%¢£¥₱€#@†*‡★؟„\©®™℅]')  # ar

regex = re.compile('\\|#\\|')
regex2 = re.compile('\s+')
regex3 = re.compile('##')
total_vocab = set()
total_unigrams = set()

def goodpercentofdata(data):
    total = 0
    i = 0
    j = 0
    with open(data, 'r', encoding='utf-8') as f_in:
        for lin in f_in:
            fileds = regex.split(lin)
            line = fileds[1].strip()
            # print(line)
            # print(line)
            # line = re.sub('\s+', ' ', line)
            # line = replace_brackets_1(line)
            # line = replace_brackets_9(line)
            # line = replace_brackets_8(line)
            # line = replace_brackets_7(line)
            # line = replace_brackets_6(line)
            # line = replace_brackets_5(line)
            # line = replace_brackets_4(line)
            # line = replace_brackets_3(line)
            # line = replace_brackets_2(line)
            # line = replace_brackets_10(line)
            # line = replace_quotes_2(line)
            # line = replace_quotes_3(line)
            # line = replace_clock_time(line)
            # line = replace_quotes_1(line)
            # print(line)

            # line.replace()
            # words=re.find(regex,line)
            # words = re.findall(regex4, line)
            # for w in words:
            #     # line=line.replace(' ','')
            #     # 先去空格
            #     line = re.sub('\s+', ' ', line)
            #     line = line.replace(' ' + w, w)

            words = line.strip().split('\t')
            for w in words:
                total += 1
                if w.strip() in total_vocab:
                    i += 1
                if w.strip() in total_unigrams:
                    j += 1
                    # print("w", w.strip())187744 在2w词表中数： 134794

    print("训练测试集总词数：", total, "在2w词表中数：", i, "正确率：", float(i / total))
    print("训练测试集总词数：", total, "在大词表中数：", j, "正确率：", float(j / total))

def goodpercentofsentense(data, s):
    names = []
    for dir_path, subpaths, files in os.walk(data):
        for name in filter(lambda x: x.endswith('.txt'), files):  # 文件夹下的所有文件
            file_path = os.path.join(dir_path, name)
            names.append(name)

    total_facebook = 0
    total_data=0
    total_facebook_huawei=0
    total_im=0
    total_twitter=0
    total_web=0
    i_data = 0
    j_data = 0
    i_facebook = 0
    j_facebook = 0
    i_im = 0
    j_im = 0
    i_facebook_huawei = 0
    j_facebook_huawei = 0
    i_web = 0
    j_web = 0
    i_twitter = 0
    j_twitter = 0
    for name in names:
        file_path = os.path.join(data, name)
        # print(file_path)

        with open(file_path, 'r', encoding='utf-8') as f_in:
            print(file_path)
            if file_path.endswith('_data.txt'):
                for line in f_in:
                    fileds = regex2.split(line)
                    for f in fileds:
                        total_data += 1
                        if f in total_vocab:
                            i_data += 1
                        if f in total_unigrams:
                            j_data += 1
            if file_path.endswith('_facebook.txt'):
                for line in f_in:
                    fileds = regex2.split(line)
                    for f in fileds:
                        total_facebook += 1
                        if f in total_vocab:
                            i_facebook += 1
                        if f in total_unigrams:
                            j_facebook += 1
            if file_path.endswith('_im.txt'):
                for line in f_in:
                    fileds = regex2.split(line)
                    for f in fileds:
                        total_im += 1
                        if f in total_vocab:
                            i_im += 1
                        if f in total_unigrams:
                            j_im += 1
            if file_path.endswith('-facebook.txt'):
                for line in f_in:
                    fileds = regex2.split(line)
                    for f in fileds:
                        total_facebook_huawei += 1
                        if f in total_vocab:
                            i_facebook_huawei += 1
                        if f in total_unigrams:
                            j_facebook_huawei += 1
            if file_path.endswith('-web.txt'):
                for line in f_in:
                    fileds = regex2.split(line)
                    for f in fileds:
                        total_web += 1
                        if f in total_vocab:
                            i_web += 1
                        if f in total_unigrams:
                            j_web += 1
            if file_path.endswith('-twitter.txt'):
                for line in f_in:
                    fileds = regex2.split(line)
                    for f in fileds:
                        total_twitter += 1
                        if f in total_vocab:
                            i_twitter += 1
                        if f in total_unigrams:
                            j_twitter += 1

    print("2w词表", str(float(i_facebook_huawei / total_facebook_huawei)) + "\t" + str(float(i_facebook / total_facebook)) + '\t'
          + str(float(i_im / total_im)) + '\t' + str(float(i_twitter / total_twitter)) + '\t' + str(float(i_web / total_web))+'\t'+str(float(i_data/ total_data)))
    print("大词表", str(float(j_facebook_huawei / total_facebook_huawei)) + "\t" + str(float(j_facebook / total_facebook))
          + '\t' + str(float(j_im / total_im)) + '\t' + str(float(j_twitter / total_twitter)) + '\t' + str(float(j_web / total_web))+'\t'+str(float(j_data / total_data)))

def mathc(s):
    words = regex2.split(s.strip())
    for w in words:
        if w in total_vocab:
            print(w)
        else:
            print("error:", w)

def total_voacab_words(data):
    total = 0

    with open(data, 'r', encoding='utf-8') as f:
        for line in f:
            fileds = regex3.split(line)
            if fileds[0].strip() not in total_vocab:
                total += 1
                total_vocab.add(fileds[0])
    print("词表总数：", total)

def total_unigram(data):
    total_u = 0
    with open(data, 'r', encoding='utf-8') as f:
        for line in f:
            # fileds = regex3.split(line)
            fileds = line.strip().split('\t')
            if fileds[0].strip() not in total_unigrams:
                # print(fileds[0])
                total_u += 1
                total_unigrams.add(fileds[0].strip())
    print("一元词表总数：", total_u)

def main():
    s = "th"
    arpath = '/Users/ff/Desktop/train_data/' + s + '/' + s + '_user_web_train/train_data_th_user_web_no_emoji_mapletters/'
    ardevdata = arpath + 'dev_data'
    arvocabdata = arpath + 'vocab_in_words'
    ar_unigram = '/Users/ff/Desktop/train_data/' + s + '/' + s + '_unigram'
    arsendata = '/Users/ff/Desktop/train_data/' + s + '/测评语料/华为测评/'
    total_voacab_words(arvocabdata)
    total_unigram(ar_unigram)
    goodpercentofdata(ardevdata)
    goodpercentofsentense(arsendata, s)

if __name__ == "__main__":
    main()


import re
import string
import sys
frequency = {}
document_text = open(sys.argv[1], 'r')
text_string = document_text.read().lower()
match_pattern = re.findall(r'\b[a-z]{4,15}\b', text_string)

for word in match_pattern:
    count = frequency.get(word,0)
    frequency[word] = count + 1

k1 = sorted(frequency, key=frequency.get, reverse=True)

for words in k1:
    print(words, frequency[words])


strong_light = -1

while strong_light < 0 or strong_light > 4:
    print('Type 1 for strong coffee, 2 for normal coffee, 3 for light coffee')

    strong_light = int(input('What type of coffee do you want: '))

#this is in grams
weightOfCoffeeBean = 0.1325

#water to be poured per 5 grams of coffee
pour_water = 88.7206

#define make strong coffee (line 75)
def make_strong_coffee():
    strong_coffee = int(input('How many beans did you put [STRONG COFFEE]: '))

    if strong_coffee <= 45:
        #strong coffee for one person
        print('ML of water ', pour_water * weightOfCoffeeBean * strong_coffee, ' strong coffee for one person')
    elif strong_coffee <= 60:
        #strong coffee for two people
        print('ML of water ', pour_water * weightOfCoffeeBean * strong_coffee, ' strong coffee for two people')
    elif strong_coffee <= 75:
        #strong coffee for three people
        print('ML of water ', pour_water * weightOfCoffeeBean * strong_coffee, ' strong coffee for three people')
    elif strong_coffee <= 90:
        #strong coffee for four people
        print('ML of water ', weightOfCoffeeBean * pour_water * strong_coffee, ' strong coffee for four people')
    else:
        print('Please restart the program')

#define normal coffee (line 78)
def make_normal_coffee():
    normal_coffee = int(input('How many beans did you put [NORMAL COFFEE]: '))

    if normal_coffee <= 30:
        #normal coffee for one person
        print('ML of water ', pour_water *weightOfCoffeeBean * normal_coffee, ' normal coffee for one person')
    elif normal_coffee <= 40:
        #normal coffee for two people
        print('ML of water ', pour_water * weightOfCoffeeBean * normal_coffee, 'normal coffee for two people')
    elif normal_coffee <= 50:
        #normal coffee for three people
        print('ML of water ', pour_water * weightOfCoffeeBean * normal_coffee, ' normal coffee for three people')
    elif normal_coffee <= 60:
        #normal coffee for four people
        print('ML of water ', pour_water * weightOfCoffeeBean * normal_coffee, ' normal coffee for four people')
    else:
        print('Please restart the program')

#define make light coffee (line 81)
def make_light_coffee():
    light_coffee = int(input('How many beans did you put [LIGHT COFFEE]: '))

    if light_coffee <= 20:
        #light coffee for one person
        print('ML of water ', pour_water * weightOfCoffeeBean * light_coffee, ' light coffee for one person only')
    elif light_coffee <= 25:
        #light coffee for two people
        print('ML of water ', pour_water * weightOfCoffeeBean * light_coffee, ' light coffee for two people only')
    elif light_coffee <= 30:
        #light coffee for three people
        print('ML of water ', pour_water * weightOfCoffeeBean * light_coffee, ' light coffee for three people only')
    elif light_coffee <= 40:
        #light coffee for four people
        print('ML of water ', pour_water * weightOfCoffeeBean * light_coffee, ' light coffee for four people only')

    else:
        print('Please restart the program')

if strong_light == 1:
    #make strong coffee
    make_strong_coffee()
elif strong_light == 2:
    #make normal coffee
    make_normal_coffee()
elif strong_light == 3:
    #make light coffee
    make_light_coffee()
else:
    print('Please restart the program')


import SimpleITK as sitk
import skimage.io as io
from itkwidgets import view
import itk
from scipy import ndimage
import numpy as np
import os
from tqdm import tqdm

def distance_transform(path, transformed_path):
    path_list = os.listdir(path)
    for pa in tqdm(path_list):
        img = sitk.ReadImage(path + pa)
        data = sitk.GetArrayFromImage(img)
        #nonzero_data = np.nonzero(data)
        #print('data', data[nonzero_data])
        transformed_data = ndimage.morphology.distance_transform_edt(data)
        #print('transformed_data', transformed_data)
        #nonzero_transformed_data = np.nonzero(transformed_data)
        #print('transformed_data', transformed_data[nonzero_transformed_data])
        out = sitk.GetImageFromArray(transformed_data)
        # out.SetSpacing(transformed_data.GetSpacing())
        # out.SetOrigin(transformed_data.GetOrigin())
        sitk.WriteImage(out, transformed_path + pa)

def show_data(path):
    path_list = os.listdir(path)
    print('start')
    for pa in tqdm(path_list):
        img = sitk.ReadImage(path + pa)
        data = sitk.GetArrayFromImage(img)
        data = np.unique(data)
        if 255 in data or len(data) != 3:
            print(data,img)
    print('end')

def show_da(path):
    print(path)
    img = sitk.ReadImage(path)
    data = sitk.GetArrayFromImage(img)
    print(data.shape)
    data = np.unique(data)
    #if 255 in data or len(data) != 3:
    print(data)

path = '/mnt/BrainDataNFS/dataset/ccta/db_update/others/crop_cardiac_zoom/vessel/'
transformed_path = '/mnt/BrainDataNFS/dataset/ccta/db_update/others/crop_cardiac_zoom/transformed_vessel_20201110/0987885_0002.nii.gz'
#img = itk.imread(path)
#view(img)
#distance_transform(path, transformed_path)
show_da(transformed_path)
#img = itk.imread(transformed_path)
#view(img)
#data = read_img(path)
#show_imgs(data)


import csv
import re
import os
from datetime import date, datetime
import time

FILENAME = 'work_log.csv'

def main_menu():
    clear()
    print('--- MAIN MENU ---')
    valid_answer = False

    while not valid_answer:
        answer = input(
            'Please choose [A]dd entry, [S]earch entry, [Q]uit: ').lower()
        if answer == 'a':
            valid_answer = True
            add_entry()
        elif answer == 's':
            valid_answer = True
            search_menu()
        elif answer == 'q':
            valid_answer = True
            print('Bye Bye!')
            quit()
        else:
            print(
                'Please Enter [A] for Add entry, [S] for Search entry or [Q] '
                'to Quit program')
            continue

def setup():
    try:
        with open(FILENAME, 'a') as csvfile:
            fieldnames = ['ID', 'date', 'taskname', 'timespent', 'detail']
            entry_writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            entry_writer.writeheader()
    except IOError:
        print("Error accessing file.")

def clear():
    print('\033c', end='')

def id_generator():
    max_ID = 0
    try:
        with open(FILENAME, 'r') as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                max_ID += 1
        return max_ID
    except IOError:
        print("Error accessing file.")

def add_entry():
    clear()
    print('--- ADD ENTRY ---')
    taskname = input('Enter Task Name: ')

    while True:
        try:
            timespent = int(
                input('How many minute(s) did you finish the task?: '))
            break

        except (ValueError, AttributeError):
            print('Please enter only number!')
            time.sleep(1.5)
            continue

    detail = input('Enter detail of the Task: ')

    try:
        with open(FILENAME, 'a') as csvfile:
            entry_writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            entry_writer.writerow({
                'ID': id_generator(),
                'date': date.today().strftime('%Y%m%d'),
                'taskname': taskname,
                'timespent': timespent,
                'detail': detail
            })
    except IOError:
        print("Error adding entry to file.")

    print('SUCCESSFULLY ADD NEW ENTRY')
    time.sleep(1.5)

def search_menu():
    valid_answer = False

    while not valid_answer:
        clear()
        print('--- SEARCH MENU ---')
        answer = input(
            'Search by [T]ext, [P]attern, [M]inute spent, [D]ate of entry or '
            '[Q]uit to main menu: ').lower()
        if answer == 't':
            search_text()
        elif answer == 'm':
            search_minute()
        elif answer == 'p':
            search_pattern()
            pass
        elif answer == 'd':
            search_date()
        elif answer == 'q':
            valid_answer = True
        else:
            print('You enter invalid command!')
            continue

def search_text():
    navigate_list = []

    while True:
        clear()
        try:
            print('--- SEARCH BY TEXT ---')
            answer = input('Please enter keyword or phrase: ')
            break
        except ValueError:
            continue
    try:
        with open(FILENAME) as csvfile:
            entry_reader = csv.DictReader(csvfile, fieldnames=fieldnames)
            for row in entry_reader:
                if row['date'] == 'date':
                    pass
                elif (
                            re.search(answer, row['taskname'], re.IGNORECASE) or
                            re.search(answer, row['detail'], re.IGNORECASE)
                ):
                    navigate_list.append({
                        'ID': row['ID'],
                        'date': row['date'],
                        'taskname': row['taskname'],
                        'timespent': row['timespent'],
                        'detail': row['detail']
                    })
    except IOError:
        print("Error accessing file to search.")

    if not navigate_list:
        print('No entry found with that keyword!')
        time.sleep(1.5)
        clear()
    else:
        display_entry(navigate_list)

def search_pattern():
    navigate_list = []

    while True:
        clear()
        try:
            print('--- SEARCH BY REGEX PATTERN ---')
            print('Please enter REGEX pattern')
            print(
                "No r', No \" or ' or any symbol, just regex escape or text "
                "you want, Example: \d\w")
            answer = str(input(': '))
            break
        except ValueError:
            continue

    regex = "r'" + answer + "'"

    try:
        with open(FILENAME) as csvfile:
            entry_reader = csv.DictReader(csvfile, fieldnames=fieldnames)
            for row in entry_reader:
                if row['date'] == 'date':
                    pass
                elif (
                            re.search(answer, row['taskname']) or
                            re.search(answer, row['detail'])
                ):
                    navigate_list.append({
                        'ID': row['ID'],
                        'date': row['date'],
                        'taskname': row['taskname'],
                        'timespent': row['timespent'],
                        'detail': row['detail']
                    })
    except IOError:
        print("Error accessing file to search pattern")

    if not navigate_list:
        print('No entry found with that pattern!')
        time.sleep(1.5)
        clear()
    else:
        display_entry(navigate_list)

def search_date():
    navigate_list = []

    while True:
        clear()
        try:
            print('--- SEARCH BY DATE RANGE ---')
            print('Please enter a range of entry date you want to search')
            print('INPUT FORMAT: YYYYMMDD. Only number, no space or symbol')
            min_date = int(input('Search Entry from date: '))
            max_date = int(input('Search Entry to date: '))
            break
        except ValueError:
            continue

    try:
        with open(FILENAME) as csvfile:
            entry_reader = csv.DictReader(csvfile, fieldnames=fieldnames)
            for row in entry_reader:
                if row['date'] == 'date':
                    pass
                elif min_date <= int(row['date']) <= max_date:
                    navigate_list.append({
                        'ID': row['ID'],
                        'date': row['date'],
                        'taskname': row['taskname'],
                        'timespent': row['timespent'],
                        'detail': row['detail']
                    })
    except IOError:
        print("Search_date file read/write error.")

    if not navigate_list:
        print('Cound not find any entry in given date range')
        time.sleep(1.5)
        clear()
    else:
        display_entry(navigate_list)

def search_minute():
    navigate_list = []

    while True:
        clear()
        try:
            print('--- SEARCH BY MINUTE RANGE ---')
            print('Please enter range of task duration you want to look for')
            print('INPUT FORMAT: Only number, no space or symbol')
            min_minute = int(input('Minimum task duration: '))
            max_minute = int(input('Maximum task duration: '))
            break
        except ValueError:
            continue

    try:
        with open(FILENAME) as csvfile:
            entry_reader = csv.DictReader(csvfile, fieldnames=fieldnames)
            for row in entry_reader:
                if row['date'] == 'date':
                    pass
                elif min_minute <= int(row['timespent']) <= max_minute:
                    navigate_list.append({
                        'ID': row['ID'],
                        'date': row['date'],
                        'taskname': row['taskname'],
                        'timespent': row['timespent'],
                        'detail': row['detail']
                    })
    except IOError:
        print("Search_minute file read/write error.")

    if not navigate_list:
        print('Could not find any entry in given duration range')
        time.sleep(1.5)
        clear()
    else:
        display_entry(navigate_list)

def display_entry(navigate_list):
    navigate = True
    navigate_index = 0

    while navigate:
        clear()
        print('Displaying entry {}/{}'.format(navigate_index + 1,
                                              len(navigate_list)))
        print('ID : {}'.format(navigate_list[navigate_index]['ID']))
        print('==========================================')
        print('Task Name: {}'.format(navigate_list[navigate_index]['taskname']))
        date_object = datetime.strptime(navigate_list[navigate_index]['date'],
                                        '%Y%m%d')
        print('Date of Entry: {}'.format(
            datetime.strftime(date_object, '<%A> %B %d, %Y')))
        print('Duration: {} Minute(s)'.format(
            navigate_list[navigate_index]['timespent']))
        print('==========================================')
        print('Detail of Entry:')
        print(navigate_list[navigate_index]['detail'])
        answer = input(
            '\n\nView [N]ext entry, '
            '[P]revious entry or '
            '[B]ack to search menu: ').lower()

        if navigate_index == 0 and answer == 'p':
            continue
        elif navigate_index == len(navigate_list) - 1 and answer == 'n':
            continue
        elif answer == 'p':
            navigate_index -= 1
            continue
        elif answer == 'n':
            navigate_index += 1
            continue
        elif answer == 'b':
            navigate = False
            clear()

fieldnames = ['ID', 'date', 'taskname', 'timespent', 'detail']

if not os.path.isfile(FILENAME):
    setup()

while True:
    main_menu()


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

DELTASFORVERSIONS = 5

import os, glob, time, markdown
from ynlib.files import WriteToFile, ReadFromFile
from ynlib.system import Execute

version = Execute('cat /Users/yanone/Code/py/git/typeWorld/guiapp/currentVersion.txt')
dmgFolder = os.path.expanduser('~/Code/TypeWorldApp/dmg')
dsaFolder = os.path.expanduser('~/Code/TypeWorldApp/dsa')

def getDSA(file):
	path = '~/Code/Sparkle/bin/sign_update %s ~/Code/dsa_priv.pem' % file
	dsa = Execute(path).decode()
	dsa = dsa.replace(' ', '').replace('\n', '')
	print(path)
	return dsa

for OS in ('windows', 'mac'):

	if OS == 'windows':
		appcastFilename = 'appcast_windows.xml'
	if OS == 'mac':
		appcastFilename = 'appcast.xml'

	# Delete files that are older than half a year, and delete appcast.xml
	print('Deleting old builds...')
	if os.path.exists(os.path.join(dmgFolder, appcastFilename)):
		os.remove(os.path.join(dmgFolder, appcastFilename))
	Execute('find ~/Code/TypeWorldApp/dmg/* -type f -mtime +30 -delete')

	if False:
#	if OS == 'mac':
		# Create deltas
		os.chdir(os.path.expanduser("~/Code/TypeWorldApp/apps/Mac"))
		versions = []
		for file in glob.glob("*"):
			version = file.replace('Type.World.', '').replace('.app', '')
			versions.append(version)
		# Sort descending
		versions.sort(key=lambda s: list(map(int, s.split('.'))), reverse=True)
		for version in versions[1:DELTASFORVERSIONS+1]:

			deltaPath = os.path.expanduser("~/Code/TypeWorldApp/dmg/TypeWorldApp.%s-%s.delta" % (versions[0], version))
			if not os.path.exists(deltaPath):
				print('Create delta between %s and %s' % (versions[0], version))
				Execute('~/Code/Sparkle/bin/BinaryDelta create ~/Code/TypeWorldApp/apps/Type.World.%s.app ~/Code/TypeWorldApp/apps/Type.World.%s.app %s' % (version, versions[0], deltaPath))

	# Write new appcast.xml

	lines = []
	lines.append('<?xml version="1.0" standalone="yes"?>')
	lines.append('<rss xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle" version="2.0">')
	lines.append('<channel>')
	lines.append('<title>Type.World</title>\n')

	os.chdir(os.path.expanduser("~/Code/TypeWorldApp/dmg"))
	for file in reversed(sorted(glob.glob("*"))):

		if OS == 'mac' and '.dmg' in file or OS == 'windows' and '.exe' in file:

			if OS == 'windows':
				ending = '.exe'
			if OS == 'mac':
				ending = '.dmg'

			path = os.path.expanduser("~/Code/TypeWorldApp/dmg/" + file)
			version = file.replace('TypeWorldApp.', '').replace(ending, '')
			length = str(int(os.stat(path).st_size))
			dsa = getDSA(os.path.join(dmgFolder, file))

			lines.append('<item>')
			lines.append('\t<title>' + version + '</title>')

			# Release notes
			md_path = os.path.expanduser("~/Code/TypeWorldApp/changelog/" + version + '.md')
			if os.path.exists(md_path):
				notes = ReadFromFile(md_path)
				if notes:
					notes += '\n\nPrevious release notes at [https://type.world/app/](https://type.world/app/)'
					lines.append('\t<description><![CDATA[' + markdown.markdown(notes) + ']]></description>')

			lines.append('\t<pubDate>' + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(os.path.getmtime(path))) + '</pubDate>')

			if OS == 'mac':
				lines.append('\t<sparkle:minimumSystemVersion>10.7</sparkle:minimumSystemVersion>')

			if OS == 'windows':
				sparkleOS = ' sparkle:installerArguments="/SILENT /SP- /NOICONS /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS" sparkle:os="windows" '
			else:
				sparkleOS = ''

			lines.append('\t<enclosure url="https://type.world/downloadlink?ID=guiapp&platform=' + OS + '&version=' + version + '" ' + sparkleOS + 'sparkle:version="' + version + '" sparkle:shortVersionString="' + version + '" length="' + length + '" sparkle:dsaSignature="' + dsa + '" type="application/octet-stream" />')

			if OS == 'mac':
				# Deltas
				deltas = []
				os.chdir(os.path.expanduser("~/Code/TypeWorldApp/dmg"))
				for delta in glob.glob("*"):
					if '.delta' in delta and 'TypeWorldApp.' + version + '-' in delta:

						path = os.path.expanduser("~/Code/TypeWorldApp/dmg/" + delta)
						previousVersion = delta.replace('TypeWorldApp.' + version + '-', '').replace('.delta', '')
						length = str(int(os.stat(path).st_size))
						dsa = getDSA(os.path.join(dmgFolder, delta))
						deltas.append('\t\t<enclosure url="https://type.world/downloadlink?ID=guiapp&platform=' + OS + '&version=' + version + '&deltaFrom=' + previousVersion + '" sparkle:version="' + version + '" sparkle:shortVersionString="' + version + '" sparkle:deltaFrom="' + previousVersion + '" length="' + length + '" sparkle:dsaSignature="' + dsa + '" type="application/octet-stream" />')

				if deltas:
					lines.append('\t<sparkle:deltas>')
					lines.extend(deltas)
					lines.append('\t</sparkle:deltas>')

			lines.append('</item>\n')

	lines.append('</channel>')
	lines.append('</rss>')

	appcastPath = os.path.join(dmgFolder, appcastFilename)

	f = open(appcastPath, 'w')
	f.write('\n'.join(lines))
	f.close()


# -*- coding: utf-8 -*-
# File: coco.py

import numpy as np # numpy module
import os # os module
from termcolor import colored # 텍스트에 칼라를 주는 모듈
from tabulate import tabulate # 리스트 이런거는 표로 만들어주는 모듈
import tqdm # 어느정도 진행됬는지 GUI 모듈.

from tensorpack.utils import logger
from tensorpack.utils.rect import FloatBox
from tensorpack.utils.timer import timed_operation
from tensorpack.utils.argtools import log_once

from config import config as cfg

__all__ = ['COCODetection', 'COCOMeta'] # 이것은 무엇을 의미하는 코드인가요.@@@@@

class _COCOMeta(object): # 인풋으로 오브젝트를 받아온다. 아웃풋은 무언가 시작할때, 클래스가 그런거 만들어주는 것같다.@@@@@
    INSTANCE_TO_BASEDIR = {  # 인스턴드투베이스디렉토리 라는 이름으로 딕셔너리를 하나 만든다.
        'train2014': 'train2014',
        'val2014': 'val2014',
        'valminusminival2014': 'val2014',
        'minival2014': 'val2014',
        'test2014': 'test2014'
    }

    def valid(self):  # 유효성을 판단해주는 그런 함수인가요? @@@@@
        return hasattr(self, 'cat_names') # 이 함수는 또 뭐지:

    def create(self, cat_ids, cat_names): # 생성해주는 함수이다. 리스트에 있는 아이디와 이름들 중에서 고양이 아이디와, 고양이 이름을 생성해준다.
        """
        cat_ids: list of ids
        cat_names: list of names
        """
        assert not self.valid() # assert 는 디버깅할때만 효과가 있고 릴리즈에서는 안쓴다. 의미는 valid하면 에러를 발생해줘! 라는 의미.
        assert len(cat_ids) == cfg.DATA.NUM_CATEGORY and len(cat_names) == cfg.DATA.NUM_CATEGORY # 고양이의 이름과 아이디가 cfg에 있는 데이터와 같지 않으면 에러가 발생한다.
        self.cat_names = cat_names # 고양이 이름 초기화
        self.class_names = ['BG'] + self.cat_names # BG를 붙혀서 클래스이름 초기화.

        # background has class id of 0
        self.category_id_to_class_id = { # 카테고리 아이디를 클래스 아이디로 dictionary 하나 만든다.
            v: i + 1 for i, v in enumerate(cat_ids)} # 여기서 i 는 0부터 시작하는 인덱스 v는 고양이 이름이다.
        self.class_id_to_category_id = { # 클래스 -> 카테고리 dictionary 하나 만든다.
            v: k for k, v in self.category_id_to_class_id.items()}
        cfg.DATA.CLASS_NAMES = self.class_names

COCOMeta = _COCOMeta() # 세션하나 만들어 주는 것인가?@@@@@

class COCODetection(object): # 코코디텍션이라는 클래스 만든다.
    def __init__(self, basedir, name): # 초기화 함수- 베이스 디렉토리와 이름이 필요하다.
        assert name in COCOMeta.INSTANCE_TO_BASEDIR.keys(), name # 인스턴스투베이스디렉토리 딕셔너리 안에 이름이 없으면 예외처리해준다. 있어야한다.
        self.name = name # 이름 초기화
        self._imgdir = os.path.realpath(os.path.join(  # 이미지가 있는 디렉토리와 관련된 라인
            basedir, COCOMeta.INSTANCE_TO_BASEDIR[name]))
        assert os.path.isdir(self._imgdir), self._imgdir # 이미지가 없으면  self._imgdir  로 예외를 처리해준다.
        annotation_file = os.path.join( # 파일 패스를 조인해준다. 무슨 파일 패스? @@@@@
            basedir, 'annotations/instances_{}.json'.format(name))
        assert os.path.isfile(annotation_file), annotation_file # 패스에 파일이 없으면 annotation_file로 예외처리

        from pycocotools.coco import COCO # cocodataset에 있는 coco 모듈을 쓴다.
        self.coco = COCO(annotation_file) # 파일을 연다.

        # initialize the meta
        cat_ids = self.coco.getCatIds() # coco 모듈의 getcatids()를 쓴다. 그래서 cat_ids 를 초기화한다.
        cat_names = [c['name'] for c in self.coco.loadCats(cat_ids)]  # cat_names 리스트를 coco 모듈에 의해 각각 초기화한다.
        if not COCOMeta.valid(): # 유효하지 않다면
            COCOMeta.create(cat_ids, cat_names) # 하나 생성한다.
        else: # 유효하다면
            assert COCOMeta.cat_names == cat_names # 이름이 같은지 예외처리 해준다.

        logger.info("Instances loaded from {}.".format(annotation_file)) # log 를 남겨준다.

    def load(self, add_gt=True, add_mask=False): # 로딩 함수 (mask는 알겠는데 ground truth가 뭐지?)@@@@@
        """
        Args:
            add_gt: whether to add ground truth bounding box annotations to the dicts
            add_mask: whether to also add ground truth mask

        Returns:
            a list of dict, each has keys including:
                'height', 'width', 'id', 'file_name',
                and (if add_gt is True) 'boxes', 'class', 'is_crowd', and optionally
                'segmentation'.
        """
        if add_mask: # 만약 마스크가 있다면
            assert add_gt # ground truth가 없으면 예외처리
        with timed_operation('Load Groundtruth Boxes for {}'.format(self.name)): # timed_operation은 어디서 온거지?@@@@@
            img_ids = self.coco.getImgIds() # 코코를 이용한 이미지 아이디 초기화
            img_ids.sort() # 이미지 아이디 정렬
            # list of dict, each has keys: height,width,id,file_name
            imgs = self.coco.loadImgs(img_ids) # 이미지 아이디를 인덱스로 한 이미지 로딩

            for img in tqdm.tqdm(imgs): # tqdm으로 진행상황을 보여준다.
                self._use_absolute_file_name(img) # _use_absuloute_file_name 은 어디서 온거지? 바로 아래에서 온다.
                if add_gt: # groung truth가 있다면
                    self._add_detection_gt(img, add_mask) # _add_detection_gt함수를 수행하라. 근데 이 함수 뭐지? 아래에서 온다.
            return imgs # 로딩함수의 리턴값은 이미지 객체이다.

    def _use_absolute_file_name(self, img): # 이미지값을 받아와서 이미지 이름을 파일의 패스로 지정해준다.
        """
        Change relative filename to abosolute file name.
        """
        img['file_name'] = os.path.join(
            self._imgdir, img['file_name'])
        assert os.path.isfile(img['file_name']), img['file_name'] # 파일이 없으면 예외처리해준다.

    def _add_detection_gt(self, img, add_mask): # 디텍션을 위해 박스와 클래스와 is_crowd를 만든다. 이것인 ground truth인가?@@@@@
        """
        Add 'boxes', 'class', 'is_crowd' of this image to the dict, used by detection.
        If add_mask is True, also add 'segmentation' in coco poly format.
        """
        # ann_ids = self.coco.getAnnIds(imgIds=img['id'])
        # objs = self.coco.loadAnns(ann_ids)
        objs = self.coco.imgToAnns[img['id']]  # equivalent but faster than the above two lines # id 값을 통해 이미지 객체 만든다.

        # clean-up boxes
        valid_objs = [] # 리스트 하나 만들어서
        width = img['width'] # 이미지 객체에 대한 width 값 초기화
        height = img['height'] # height 값 초기화
        for obj in objs: # 객체들 중에서
            if obj.get('ignore', 0) == 1: # ignore 값이 있는 딕셔너리가 있으면 뛰어넘는다. 보지 않는다.
                continue
            x1, y1, w, h = obj['bbox'] # 객체에서 bbox 정보를 초기화한다.
            # bbox is originally in float
            # x1/y1 means upper-left corner and w/h means true w/h. This can be verified by segmentation pixels.
            # But we do assume that (0.0, 0.0) is upper-left corner of the first pixel
            box = FloatBox(float(x1), float(y1), # float 박스를 만든다.(네모난 박스를 만든다.)
                           float(x1 + w), float(y1 + h))
            box.clip_by_shape([height, width]) # clip_by_shape함수가 뭐지?@@@@@
            # Require non-zero seg area and more than 1x1 box size
            if obj['area'] > 1 and box.is_box() and box.area() >= 4: # 객체의 너비가 1보다 크고 박스가 있고 박스의 너비가 4이상이면
                obj['bbox'] = [box.x1, box.y1, box.x2, box.y2] # 객체의 bbox는 x1,x2,y1,y2로 지정해준다.
                valid_objs.append(obj) # 그리고 객체를 유효한 객체들의 리스트에 넣는다.

                if add_mask: # 그리고 여기서 마스크가 있으면(mask r cnn 일때를 말한다.)
                    segs = obj['segmentation']  # 객체의 segmentation 부분을 가지고 segs 라는 변수를 초기화한다.
                    if not isinstance(segs, list): # segs 라는 변수가 리스트가 아닐때,
                        assert obj['iscrowd'] == 1 # 객체에 iscrowd가 1이면 예외처리해준다.
                        obj['segmentation'] = None # 객체의 segmentation이 없다? @@@@@
                    else:
                        valid_segs = [np.asarray(p).reshape(-1, 2).astype('float32') for p in segs if len(p) >= 6] # segs라는 리스트에서 유효한 것들만 뽑는다.
                        if len(valid_segs) < len(segs): # 근데 segs들에서 유효한 segs들이 별로 없다면
                            log_once("Image {} has invalid polygons!".format(img['file_name']), 'warn') # 로그를 띄운다. 별로 없어서 warning이라고

                        obj['segmentation'] = valid_segs # 유효한 segs들은 객체의 segmentation에 다시 넣어준다.

        # all geometrically-valid boxes are returned
        boxes = np.asarray([obj['bbox'] for obj in valid_objs], dtype='float32')  # (n, 4) # 유효한 객체들의 bbox를 np를 통해 만든어 준다.
        cls = np.asarray([ # 유효한 객체들로 클래스를 만든어 cls라는 변수를 만들어 준다.
            COCOMeta.category_id_to_class_id[obj['category_id']]
            for obj in valid_objs], dtype='int32')  # (n,)
        is_crowd = np.asarray([obj['iscrowd'] for obj in valid_objs], dtype='int8') # 유효한 객체에서 각 객체의 is_crowd 를 가지고 is_crowd라는 똑같은 이름의 변수를 초기화해준다.

        # add the keys
        img['boxes'] = boxes        # nx4  # 박스들을 이미지 객체의 박스에 넣는다.  여기서 boxes는 아마 유효한 것들의 boxes 일것이다.
        img['class'] = cls          # n, always >0   # 클래스들을 이미지 객체의 클래스에 넣는다.
        img['is_crowd'] = is_crowd  # n, # is_crowd를 이미지 객체의 그것에 넣는다.
        if add_mask: # 만약 마스크 rcnn 이라면
            # also required to be float32
            img['segmentation'] = [ # 세그멘테이션도 이와 동일한 맥락이다.
                obj['segmentation'] for obj in valid_objs]

    def print_class_histogram(self, imgs): # 이미지 객체 리스트를 가지고와서 클래스 히스토그램을 그린다.
        nr_class = len(COCOMeta.class_names)  # 클래스 이름의 길이를 nr_class라는 변수에 초기화해준다.
        hist_bins = np.arange(nr_class + 1) # nr_class 에 1일 더하여 hist_bins라는 np를 만든다.

        # Histogram of ground-truth objects
        gt_hist = np.zeros((nr_class,), dtype=np.int) # ground truth 히스토그램 변수를 만든다.
        for entry in imgs: # 이미지 리스트에서 entry 라는 이름으로,
            # filter crowd?
            gt_inds = np.where( # numpy의 where 을 통해서 클래스가 0보다 크고, is_crowd가 0인 것의 위치를 찾는다.)
                (entry['class'] > 0) & (entry['is_crowd'] == 0))[0]
            gt_classes = entry['class'][gt_inds] # 그래서 찾은 것을 gt_classes라는 것에 넣어준다.
            gt_hist += np.histogram(gt_classes, bins=hist_bins)[0] # np histogram이라는 것을 사용해서 gt_hist를 만들어 준다. 이것을 반복한다.
        data = [[COCOMeta.class_names[i], v] for i, v in enumerate(gt_hist)] # data 라는 곳에 유용한 것들을 모아놓는다. 결국 보여줄 것.
        data.append(['total', sum([x[1] for x in data])]) # 데이터에 total 과 data의 있는 값들을 더 추가한다.
        table = tabulate(data, headers=['class', '#box'], tablefmt='pipe') # 표를 만들어 준다.
        logger.info("Ground-Truth Boxes:\n" + colored(table, 'cyan')) # 로그를 띄워준다.

    @staticmethod
    def load_many(basedir, names, add_gt=True, add_mask=False):  # 여러개를 로드하고 합병해서 특정 인스턴스파일들을 뭉친다.
        """
        Load and merges several instance files together.

        Returns the same format as :meth:`COCODetection.load`. # cocodetection.load 라는 것과 같은 방식으로 리턴한다.
        """
        if not isinstance(names, (list, tuple)): # 인스턴드가 list, tuple이 아니면, 이렇게 해석하는것 맞나?  @@@@@
            names = [names] # 이름들을 names에 초기화한다.
        ret = [] # 리스트 하나 만들고,
        for n in names: # 이름들을 가지고,
            coco = COCODetection(basedir, n) # 코코 변수 하나를 만든다. 코코 변수는 무엇일까? @@@@@
            ret.extend(coco.load(add_gt, add_mask=add_mask)) # ret에 coco.load를 해서 가지고 온 것을 계속 넣어준다. 그리고 반환한다.
        return ret

if __name__ == '__main__': # 메인 함수 시작
    c = COCODetection(cfg.DATA.BASEDIR, 'train2014') # 코코 디텍션에서 cfg라는 곳의 베이스폴더 이름과 train2014를 가지고 c라는 이름에 넣어준다.
    gt_boxes = c.load(add_gt=True, add_mask=True) # ground truth 박스도 c에서 넣어준다.
    print("#Images:", len(gt_boxes)) # gt_boxes의 길이를 출력
    c.print_class_histogram(gt_boxes) # 그림을 그려준다. gt_bexes의 길이이다.


import cv2
import numpy as np
import os

class matchers:

		def detectAndDescribe(self, image):
		# convert the image to grayscale
			gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

			# check to see if we are using OpenCV 3.X
				# detect and extract features from the image
			orb = cv2.ORB_create(nfeatures=5000)
			kp = orb.detect(image,None)
			(kps, features) = orb.compute(image, kp)

			kps = np.float32([kp.pt for kp in kps])

			# return a tuple of keypoints and features
			return (kps, features)

		def match(self, im1,im2, direction=None, method='homography'):
			# compute the raw matches and initialize the list of actual
			# matches
			print("Direction : ", direction)
			kpsA, featuresA = self.detectAndDescribe(im1)
			kpsB, featuresB = self.detectAndDescribe(im2)
			matcher = cv2.DescriptorMatcher_create("BruteForce")
			rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
			matches = []

			# loop over the raw matches
			for m in rawMatches:
				# ensure the distance is within a certain ratio of each
				# other (i.e. Lowe's ratio test)
				if len(m) == 2 and m[0].distance < m[1].distance * 0.65:
					matches.append((m[0].trainIdx, m[0].queryIdx))

			print("matches selected: ",len(matches))
			# computing a homography requires at least 4 matches
			if len(matches) > 4:
				# construct the two sets of points
				ptsA = np.float32([kpsA[i] for (_, i) in matches])
				ptsB = np.float32([kpsB[i] for (i, _) in matches])

				# compute the homography between the two sets of points

				if method == 'affine':
					partial_homo, ma = cv2.estimateAffine2D(ptsA, ptsB, cv2.RANSAC, ransacReprojThreshold=5.0)
					H = np.array([[0.0, 0, 0], [0.0, 0, 0], [0.0, 0, 1]])
					H[0:partial_homo.shape[0],0:partial_homo.shape[1]] = partial_homo
					print(H)
					print(H.shape)
        			#M = np.append(M, [[0,0,1]], axis=0)

				if method == 'homography':
					H, mask = cv2.findHomography(ptsA, ptsB, cv2.RANSAC,5.0)

				# return the matches along with the homograpy matrix
				# and status of each matched point
			return H

			# otherwise, no homograpy could be computed
			return None

# for file1 in os.listdir('/Users/sharutgupta/Dropbox/COL780/Ass2/Images/1'):
# 	if file1=='.DS_Store':
# 		continue
# 	max_match = 0
# 	for file2 in os.listdir('/Users/sharutgupta/Dropbox/COL780/Ass2/Images/1'):
# 		if file2=='.DS_Store':
# 			continue
# 		print(file1+", "+file2)
# 		if(file1==file2):
# 			continue

# 		img1 = cv2.resize(cv2.imread('./Images/1/'+file1),(480, 320))
# 		img2 = cv2.resize(cv2.imread('./Images/1/'+file2), (480, 320))

# 		count_img1 = file1.split('.')[0]
# 		count_img2 = file2.split('.')[0]
# 		my_match= matchers();
# 		my_match.match(img1,img2,None)