# -*- coding: utf-8 -*-
"""
Exercise 3: Use urllib to replicate the previous exercise of (1) retrieving
the document from a URL, (2) displaying up to 3000 characters, and
(3) counting the overall number of characters in the document. Don’t
worry about the headers for this exercise, simply show the first 3000
characters of the document contents.

"""

import urllib.request, urllib.parse, urllib.error
#from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter the URL: ') # http://data.pr4e.org/romeo.txt
fhand = urllib.request.urlopen(url, context=ctx).read()
# Why here we need to transfer bytes format to Unicode format???
if type(fhand) is bytes:
    fhand = str(fhand, encoding = 'utf8')

count = 0
for line in fhand:
    for c in line:
        if count < 3000:
            print(c, end = '')
        count = count + 1

print('\n\nThere are {} characters in this documet.'.format(count))
# There are 8864 characters in this documet
# Means it ignores the header!


'''
This program runs the genetic algorithm in same-stats with a number of different parameters for optimization purposes.
Parameters to tune include:
    * number of iterations
    * population size
    * probability of crossover
    * probability of mutation
    * mutation standard deviation (magnitude)
'''

import dissimilarity as ds
import same_stats as ss
from itertools import product
from time import perf_counter
from functools import partial

# Not testing changes in mutation standard deviation, since mutations are rare occurrences anyway

test_iterations = (10,50)
test_population_sizes = (20,100,200)
test_p_crossover = (0.5,0.7,0.9,1)
test_p_mutation = (0,0.01,0.05)
mutation_sd = 0.25

performance = {}
measures_to_test = (ds.data_diff, ds.skewness_diff, ds.kurt_diff, ds.power_diff)
REFERENCE = ss.load_data('dataset1.txt')

for combination in product(test_iterations, test_population_sizes, test_p_crossover, test_p_mutation):
    start_time = perf_counter()
    populations = ss.run_genetic(*combination, mutation_sd,
        measures_to_test, REFERENCE)
    best_distributions = [pop[0] for pop in populations[1:]]
    max_fitnesses = [partial(pair[0], REFERENCE)(pair[1]) for pair in zip(measures_to_test, best_distributions)]
    end_time = perf_counter()

    performance[combination] = (max_fitnesses, end_time - start_time)

print(performance)


import cv2
import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))  # get directory root
image_dir = os.path.join(BASE_DIR, "images")  # get image directory path
# print(image_dir)
person_id = input("Enter the Id: ")  # receive the id input
image_dir_id = os.path.join(image_dir, person_id)  # create a path specific to the id

try:
    os.mkdir(image_dir_id)  # create a directory in the name of id
except FileExistsError:
    print('Directory already exist')

# print(image_dir_id)

cam = cv2.VideoCapture(0)  # using standard web-cam as input object
detector = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')  # set haarcascade frontalface model

sampleNum = 0
while (True):
    ret, img = cam.read()       # read image
    # print(ret)                # checking for camera read the image properly
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = detector.detectMultiScale(gray, 1.5, 5)
    for (x, y, w, h) in faces:
        sampleNum = sampleNum + 1   # incrementing sample number
        # print(os.path.join(image_dir_id,str(sampleNum))+ ".jpg")
        cv2.imwrite(os.path.join(image_dir_id, str(sampleNum)) + ".jpg", img=img)   # saving images to indexed directory
        cv2.imshow('frame', img)      # display the frame
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)    # setting the rectangle for ROI

    if cv2.waitKey(25) & 0xFF == ord('q'):  # delay 25 millisecond break if key 'q' pressed
        break
    elif sampleNum > 200:   # finish execution after 100 sample pictures
        break

cam.release()       # release the camera resource
cv2.destroyAllWindows()


import time
from tkinter import *

tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
mytriangle = canvas.create_polygon(10, 10, 10, 60, 50, 35, fill='red')
"""
for x in range(0, 60):
    canvas.move(1, 5, 0)
    tk.update()
    time.sleep(0.05)

    while x == 59:
        for j in range(0, 60):
          canvas.move(1, -5, 0)
           tk.update()
           time.sleep(0.05)
    """
def movetriangle(event):
    if event.keysym == 'Up':
        canvas.move(mytriangle, 0, -3)
    elif event.keysym == 'Down':               #key sm --> key symbol
        canvas.move(mytriangle, 0, 3)
    elif event.keysym == 'Left':
        canvas.move(mytriangle, -3, 0)
    else:
        canvas.move(mytriangle, 3, 0)

canvas.bind_all('<KeyPress-Up>', movetriangle)
canvas.bind_all('<KeyPress-Down>', movetriangle)
canvas.bind_all('<KeyPress-Left>', movetriangle)
canvas.bind_all('<KeyPress-Right>', movetriangle)

canvas.itemconfig(mytriangle, fill='blue', outline='red')        #changes the color of the triangle and the outline color

tk.mainloop()


#!/usr/bin/python
#-*- coding: utf-8 -*-

import sys
import itertools

import read
import utils
import solve

"""Główny program dla heurystyki sumy ważonych marginesów parami. Wczytuje dane
z pliku, odpowiednio je modyfikuje i uruchamia procedurę wyliczającą przepływy.
"""

def change_index_to_zero_based(successors):
    """Przekształca tablicę następników z tablicy o podstawie 1 do
    tablicy o podstawie 0.
    """
    result = successors[1:]

    for node in result:
        for i in range(len(node)):
            node[i] -= 1

    return result

if __name__ == "__main__":
    # wczytaj zawartość pliku
    filename = sys.argv[1]
    f = open(filename, "r")
    contents = "".join(f.readlines())

    # informacje o projekcie
    (jobs_count, start_time, duedate, tardcost) = read.read_project_information(contents)

    # wczytaj relacje zależnościowe
    successors = read.read_precedence_relations(contents, jobs_count)
    successors = change_index_to_zero_based(successors)

    # wczytaj czasy trwania
    durations = read.read_durations(contents, jobs_count)[1:]

    # przygotuj macierz kosztów
    cost_matrix = utils.cost_matrix(durations, successors)

    # zamień koszty na zyski
    for (i, j) in itertools.product(range(jobs_count + 2), repeat = 2):
        if cost_matrix[i][j] != None:
            cost_matrix[i][j] = - cost_matrix[i][j]

    # pokaż rozwiązanie
    solve.solve_wpf(jobs_count + 2, cost_matrix, duedate)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#   @autor: beauge_z, jussea_m

import socket
import sys
import os

buff_sz=4096

class player:
    inv=list()
    lvl=1

def moveTo(x, case):
    action = list()
    pos = tst = 0
    caseLen=3
    sz=3
    action.extend(cleanCase(case[pos]))
    for i in range(0, (3 + player.lvl - 1)):
        tst+=(int(caseLen/2) + 1 + i)
        if x >= sz and tst <= len(case):
            action.append('avance')
            pos+=(int(caseLen/2) + 1 + i)
            action.extend(cleanCase(case[pos]))
        caseLen+=2
        sz+=caseLen
    if pos == 0:
        action.append('avance')
        pos+=2
        action.extend(cleanCase(case[pos]))
    if pos < x:
        action.append('droite')
        while pos < x:
            action.append('avance')
            pos+=1
            action.extend(cleanCase(case[pos]))
    if pos > x:
        action.append('gauche')
        while pos > x:
            action.append('avance')
            pos-=1
            action.extend(cleanCase(case[pos]))
    return action

def checkInv(needed):
    for item in needed:
        if item not in player.inv:
            return False
    return True

def cleanCase(case):
    take=list()
    if case:
        for elem in case.split():
            if elem != 'joueur':
                take.append('prend ' + elem)
            if elem != 'nourriture' and elem != 'joueur':
                player.inv.append(elem)
    return take

def checkPlayer(case):
    if player.lvl == 1:
        return True
    nbplayer=0
    ret=0
    while ret != -1:
        ret = case.find('joueur', ret)
        if ret != -1:
            nbplayer+=1
    if (player.lvl == 2 or player.lvl == 3) and nbplayer != 2 :
       serv.send('broadcast lvl' + str(player.lvl))
       print('-->broadcast lvl' + str(player.lvl))
       return False
    if (player.lvl == 4 or player.lvl == 5) and nbplayer != 4 :
       serv.send('broadcast lvl' + str(player.lvl))
       print('-->broadcast lvl' + str(player.lvl))
       return False
    if (player.lvl == 5 or player.lvl == 7) and nbplayer != 6 :
       serv.send('broadcast lvl' + str(player.lvl))
       print('-->broadcast lvl' + str(player.lvl))
       return False
    return True

def getAction(mapseen, mytime):
    action=list()
    needed=list()
    newmap=mapseen.replace("{", "").replace("}", "")
    case=newmap.split(',')
    pos=0
    find=False
    action.clear()
    if player.lvl == 1:
        needed = ['linemate']
    if player.lvl == 2:
        needed = ['linemate', 'deraunere', 'sibur']
    if player.lvl == 3:
        needed = ['linemate', 'linemate', 'sibur', 'phiras', 'phiras']
    if player.lvl == 4:
        needed = ['linemate', 'deraunere', 'sibur', 'sibur', 'phiras']
    i=0
    for i in range(0, len(case)):
        if case[i].find('nourriture') != -1 and not find:
            action = moveTo(i, case)
            mytime+=126
            find=True
    i=0
    for elem in needed:
        for i in range(0, len(case)):
            if case[i].find(elem) != -1 and not find:
                action = moveTo(i, case)
                find=True
    find=False
    mytime-=(7*len(action))
    if (checkInv(needed) and checkPlayer(case[i]) and mytime - (7*len(needed))) > 400:
        action.extend(cleanCase(case[i]))
        for elem in needed:
            action.append('pose ' + elem)
            del player.inv[player.inv.index(elem)]
        action.append('incantation')
        player.lvl+=1
    if not action:
        action.append('droite')
    return action, mytime

def ai(serv, mytime):
    seen=False
    action=list()
    data=''
    while(data != 'mort\n'):
        if seen is False:
            action.clear()
            serv.send(bytes('voir\n', 'utf-8'))
            print('-->voir', flush=True)
            data=serv.recv(buff_sz).decode('utf-8')
            print('<--%s' % data, end="", flush=True)
            action, mytime=getAction(data, mytime)
            seen=True
        elif action:
            serv.send(bytes(action[0] + '\n', 'utf-8'))
            print('-->%s' % action[0], flush=True)
            data=serv.recv(buff_sz).decode('utf-8')
            print('<--%s' % data, end="", flush=True)
            if action[0] == 'incantation':
                data=serv.recv(buff_sz).decode('utf-8')
                print('<--%s' % data, end="", flush=True)
            del action[0]
            if not action:
                seen=False

def firstMsgs(serv, team):
    data=serv.recv(buff_sz).decode('utf-8')
    if (data == 'BIENVENUE\n'):
        print('<--%s' % data, end="", flush=True)
        serv.send(bytes(team + '\n', 'utf-8'))
        print('-->%s' % team, flush=True)
        data=serv.recv(buff_sz).decode('utf-8')
        print('<--%s' % data, end="", flush=True)
        numCl=int(data)
        data=serv.recv(buff_sz).decode('utf-8')
        print('<--%s' % data, end="", flush=True)
        sizeMap = data.split(' ')
        x = int(sizeMap[0])
        y = int(sizeMap[1])
    else:
        raise RuntimeError('Incompatible server. Maybe not a Zappy server?')
    return numCl

def server_connect(team_name, port, host):
    mytime=0
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    numCl = firstMsgs(s, team_name)
    newpid=-1
    if numCl > 0:
        newpid=os.fork()
        if (newpid == 0):
            os.system(' '.join(sys.argv))
            os._exit(0)
    ai(s, mytime)
    if newpid == 0:
        os.waitpid(newpid, 0)
    s.close()

def getArg():
    host='localhost'
    port=''
    team_name=''
    for i in range(1, len(sys.argv)):
        if i % 2 is not 0 and i+1 <= len(sys.argv):
            if sys.argv[i] == '-n':
                team_name=str(sys.argv[i+1])
            elif sys.argv[i] == '-p':
                port=int(sys.argv[i+1])
            elif sys.argv[i] == '-h':
                host=str(sys.argv[i+1])
    return (team_name, port, host)

def chckArg():
    try:
        host='localhost'
        port=''
        team_name=''
        for i in range(1, len(sys.argv)):
            if i % 2 is not 0 and i+1 <= len(sys.argv):
                if sys.argv[i] == '-n':
                    team_name=str(sys.argv[i+1])
                elif sys.argv[i] == '-p':
                    port=int(sys.argv[i+1])
                elif sys.argv[i] == '-h':
                    host=str(sys.argv[i+1])
        if not port or not team_name:
            raise RuntimeError('Client must have a team name and a port specified to run.')
    except ValueError:
        raise RuntimeError('Bad argument type.')
    return True

def main():
    try:
        if len(sys.argv) < 5 or len(sys.argv) > 7:
            print('Usage: %s -n team_name -p port [-h machine_name]' % str(sys.argv[0]), flush=True)
        else:
            if chckArg() is True:
                team_name, port, host = getArg()
                server_connect(team_name, port, host)
        return True
    except Exception as err:
        sys.stderr.write('[\033[1;31mError\033[0m]%s\nProgram is closing.\n' % str(err))
        return False

if __name__ == '__main__':
    sys.exit(main())


import os
import sys
no = {
    "quantidade" : 0,
    "caracter" : "",
    "direita": None,
    "esquerda": None
}

import json

'''
Calcular quantos bits sao necessarios
Juntar os bits
Substituir os bits
ArmezanrCodificação
'''
#
# print(os.path.getsize("a.ale"))
# print(os.path.getsize("a.txt"))
#

#
arq = open("imgJogador.ale","wb")
# b = ord("é")
# a = chr(b)
# c = bytes(a,encoding="utf-8")
# d = c.decode()
# print(a,b,c,d,sep="\n")
# arq.write(bytes(169))
# arq.close()
from Ferramentas.ferramentasArquivos import *

# print("texto", texto)
'''
# print(chr(int.from_bytes(b'\xc3\xa9\n', byteorder=sys.byteorder) ))# => 17
print(ord("é"))
arq.write(b'\xc3\xa9\n')
arq.close()

print("Tamanho compac:", os.path.getsize("a.ale"))
print("tamanho Origin:", os.path.getsize("a.txt"))
print("tamanho Gerado:", os.path.getsize("aGer.txt"))

arq1 = open("a.txt", "rb")
arq2 = open("aGer.txt", "rb")

linhas1 = []
for i in arq1:
    linhas1 += [i]

igual = True
cont = 0
for n in arq2:
    if(n != linhas1[cont]):
        print("N", n)
        print("Lista", linhas1[cont])
        print("COnt", cont)
        print("Nao e igual")
        igual = False
        break

    cont += 1

bits ['0110', '1111', '1110', '1101', '1100', '1011', '1010', '1001', '1000', '00111', '00110', '00101', '00100', '00011', '00010', '00001', '00000', '01011', '01010', '01001', '01000', '0111']

0110 1110 11110110111001011101001001100000111001100010100100000110001000000100000001011010100100101000010000011

if(igual):
    print("Igual")
'''
a ="bHAm3}\]&	!%"
for c in a:
    print(bin(ord(c)))


# -*- coding: utf-8 -*-
"""
面向对象最重要的概念就是类（Class）和实例（Instance），必须牢记类是抽象的模板，比如Student类，
而实例是根据类创建出来的一个个具体的“对象”，每个对象都拥有相同的方法，但各自的数据可能不同。
"""
'''
注意到__init__方法的第一个参数永远是self，表示创建的实例本身，因此，在__init__方法内部，
就可以把各种属性绑定到self，因为self就指向创建的实例本身。
有了__init__方法，在创建实例的时候，就不能传入空的参数了，必须传入与__init__方法匹配的参数，
但self不需要传，Python解释器自己会把实例变量传进去：
'''

class Student(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def print_info(self):
        print('Name:', self.name, 'Age:', self.age)

stu = Student('Tom', 23)
stu.print_info()
# 可以自由地给一个实例变量绑定属性
stu.score = 98
print(stu.score)

'''
类是创建实例的模板，而实例则是一个一个具体的对象，各个实例拥有的数据都互相独立，互不影响；
方法就是与实例绑定的函数，和普通函数不同，方法可以直接访问实例的数据；
通过在实例上调用方法，我们就直接操作了对象内部的数据，但无需知道方法内部的实现细节。
和静态语言不同，Python允许对实例变量绑定任何数据，也就是说，对于两个实例变量，
虽然它们都是同一个类的不同实例，但拥有的变量名称都可能不同：
'''


#!/usr/bin/env python

import pygame, os
from pygame.locals import *
from pygame import Rect, Surface

from settings import LEVEL_SIZE

class Level(object):
    def __init__(self):
        self.bounds = Rect((0,0), LEVEL_SIZE)
        self.render_background()

    def render_background(self):
        self.background = Surface(self.bounds.size)

        rows = self.bounds.height/20
        cols = self.bounds.width/20

        self.background.fill((0, 0, 0))
        for y in range(rows):
            for x in range(cols):
                self.background.fill((80, 80, 80), (x*20 +2, y*20 +2, 18, 18))


import os

# Implement a trie with insert, search, and startsWith methods.
# Note:
# You may assume that all inputs are consist of lowercase letters a-z

class TrieNode(object):
    def __init__(self):
        self.frequency = 0
        self.nodes = [None for _ in range(26)]

class Trie(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self._root = TrieNode()

    def insert(self, word):
        """
        Inserts a word into the trie.
        :type word: str
        :rtype: void
        """
        node, aNum = self._root, ord('a')
        for ch in word:
            num = ord(ch) - aNum
            if not node.nodes[num]:
                node.nodes[num] = TrieNode()

            node = node.nodes[num]
        node.frequency += 1

    def search(self, word):
        """
        Returns if the word is in the trie.
        :type word: str
        :rtype: bool
        """
        node, aNum = self._root, ord('a')
        for i in range(len(word)):
            num = ord(word[i]) - aNum
            if not node.nodes[num]:
                return False
            node = node.nodes[num]

        return False if not node else node.frequency > 0

    def startsWith(self, prefix):
        """
        Returns if there is any word in the trie that starts with the given prefix.
        :type prefix: str
        :rtype: bool
        """
        node, aNum = self._root, ord('a')
        for ch in prefix:
            num = ord(ch) - aNum
            if not node.nodes[num]:
                return False
            node = node.nodes[num]
        return True

# leetcode 211. Add and Search Word - Data structure design
# Design a data structure that supports the following two operations:
# search(word) can search a literal word or a regular expression string
# containing only letters a-z or .. A . means it can represent any one letter.

class WordDictionary(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self._root = TrieNode()

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        node, aNum = self._root, ord('a')
        for ch in word:
            num = ord(ch) - aNum
            if not node.nodes[num]:
                node.nodes[num] = TrieNode()

            node = node.nodes[num]
        node.frequency += 1

    def search(self, word):
        """
        Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
        :type word: str
        :rtype: bool
        """
        def subSearch(word, s, node):
            if not node:
                return False

            if s == len(word):
                return node.frequency > 0

            if word[s] == '.':
                for item in node.nodes:
                    if item and subSearch(word, s+1, item):
                        return True
                return False
            else:
                return subSearch(word, s+1, node.nodes[ord(word[s])-ord('a')])

        return subSearch(word, 0, self._root)

class WordDictionary2(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.word_dict = collections.defaultdict(list)

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        if word:
            self.word_dict[len(word)].append(word)

    def search(self, word):
        """
        Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
        :type word: str
        :rtype: bool
        """
        if not word:
            return False
        if '.' not in word:
            return word in self.word_dict[len(word)]
        for v in self.word_dict[len(word)]:
            for i, ch in enumerate(word):
                if ch != v[i] and ch != '.':
                    break
            else:
                return True
        return False

if __name__=="__main__":

    obj3 = Trie()
    res = obj3.search('a')

    obj2 = Trie()
    obj2.insert("word")
    param_2 = obj2.search("word")
    param_3 = obj2.startsWith("wor")

    obj1 = Trie()
    obj1.insert("ab")
    param_2 = obj1.search("a")
    param_3 = obj1.startsWith("a")

    obj = Trie()
    res = obj.insert('abc')
    res = obj.search('abc')

    res = obj.search('ab')
    res = obj.insert('ab')

    res = obj.search('ab')
    res = obj.insert('ab')
    res = obj.search('ab')

    obj = WordDictionary()
    res = obj.addWord("bad")
    res = obj.addWord("dad")
    res = obj.addWord("mad")
    res = obj.search("pad") #-> false
    res = obj.search("bad") #-> true
    res = obj.search(".ad") #-> true
    res = obj.search("b..") #-> true

    pass


class Alignment:
    def __init__(self, primary, secondary):
        # Alignments have 2 parts - the faction, and purpose within it
        self.primary = primary
        self.secondary = secondary

        # Alignments are usually just abbreviated
        self.shorthand = self.primary[0] + self.secondary[0]

    def __str__(self):
        return "{primary} ({secondary})".format(primary=self.primary, secondary=self.secondary)

# Not really a nice way to handle this IMO but I can't be bothered when this works
TI = Alignment("Town", "Investigative")
TP = Alignment("Town", "Protective")
TK = Alignment("Town", "Killing")
TS = Alignment("Town", "Support")

MD = Alignment("Mafia", "Deception")
MK = Alignment("Mafia", "Killing")
MS = Alignment("Mafia", "Support")

NB = Alignment("Neutral", "Benign")
NE = Alignment("Neutral", "Evil")
NK = Alignment("Neutral", "Killing")
NC = Alignment("Neutral", "Chaos")

CE = Alignment("Coven", "Evil")


# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# 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.

"""Command for removing an interface from a Google Compute Engine router."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from apitools.base.py import encoding
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute.routers import flags
from googlecloudsdk.core import exceptions

class InterfaceNotFoundError(exceptions.Error):
  """Raised when an interface is not found."""

  def __init__(self, name):
    self.name = name
    msg = 'interface `{0}` not found'.format(name)
    super(InterfaceNotFoundError, self
         ).__init__(msg)

class RemoveBgpPeer(base.UpdateCommand):
  """Remove an interface from a Google Compute Engine router.

  *{command}* removes an interface from a Google Compute Engine router.
  """

  ROUTER_ARG = None

  @classmethod
  def Args(cls, parser):
    cls.ROUTER_ARG = flags.RouterArgument()
    cls.ROUTER_ARG.AddArgument(parser, operation_type='update')

    parser.add_argument(
        '--interface-name',
        required=True,
        help='The name of the interface being removed.')

  def GetGetRequest(self, client, router_ref):
    return (client.apitools_client.routers,
            'Get',
            client.messages.ComputeRoutersGetRequest(
                router=router_ref.Name(),
                region=router_ref.region,
                project=router_ref.project))

  def GetSetRequest(self, client, router_ref, replacement):
    return (client.apitools_client.routers,
            'Patch',
            client.messages.ComputeRoutersPatchRequest(
                router=router_ref.Name(),
                routerResource=replacement,
                region=router_ref.region,
                project=router_ref.project))

  def Modify(self, args, existing, cleared_fields):
    """Mutate the router and record any cleared_fields for Patch request."""
    replacement = encoding.CopyProtoMessage(existing)

    # remove interface if exists
    interface = None
    for i in replacement.interfaces:
      if i.name == args.interface_name:
        interface = i
        replacement.interfaces.remove(interface)
        if not replacement.interfaces:
          cleared_fields.append('interfaces')
        break

    if interface is None:
      raise InterfaceNotFoundError(args.interface_name)

    return replacement

  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client

    router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
    get_request = self.GetGetRequest(client, router_ref)

    objects = client.MakeRequests([get_request])

    # Cleared list fields need to be explicitly identified for Patch API.
    cleared_fields = []
    new_object = self.Modify(args, objects[0], cleared_fields)

    with client.apitools_client.IncludeFields(cleared_fields):
      # There is only one response because one request is made above
      result = client.MakeRequests(
          [self.GetSetRequest(client, router_ref, new_object)])
      return result


from __future__ import absolute_import, with_statement

from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import NoReverseMatch

from django_hosts.tests.base import override_settings, HostsTestCase
from django_hosts.reverse import (get_hostconf_module, get_host_patterns,
    get_host, reverse_host, reverse_full)

class ReverseTest(HostsTestCase):

    @override_settings(ROOT_HOSTCONF='django_hosts.tests.hosts.simple')
    def test_reverse_host(self):
        self.assertRaises(ValueError,
            reverse_host, 'with_kwargs', ['spam'], dict(eggs='spam'))
        self.assertRaises(NoReverseMatch,
            reverse_host, 'with_kwargs', ['spam', 'eggs'])
        self.assertRaises(NoReverseMatch,
            reverse_host, 'with_kwargs', [], dict(eggs='spam', spam='eggs'))
        self.assertEqual('johndoe',
            reverse_host('with_kwargs', None, dict(username='johndoe')))
        self.assertEqual(reverse_host('with_args', ['johndoe']), 'johndoe')
        with self.settings(PARENT_HOST='spam.eggs'):
            self.assertEqual(reverse_host('with_args', ['johndoe']),
                             'johndoe.spam.eggs')

    @override_settings(
        ROOT_HOSTCONF='django_hosts.tests.hosts.simple',
        PARENT_HOST='spam.eggs')
    def test_reverse_full(self):
        self.assertEqual(reverse_full('static', 'simple-direct'),
                         '//static.spam.eggs/simple/')

    @override_settings(
        ROOT_HOSTCONF='django_hosts.tests.hosts.simple',
        PARENT_HOST='example.com')
    def test_reverse_full_without_www(self):
        self.assertEqual(reverse_full('without_www', 'simple-direct'),
                         '//example.com/simple/')

    @override_settings(
        ROOT_HOSTCONF='django_hosts.tests.hosts.simple',
        PARENT_HOST='spam.eggs')
    def test_reverse_custom_scheme(self):
        self.assertEqual(reverse_full('scheme', 'simple-direct'),
                         'https://scheme.spam.eggs/simple/')

class UtilityTests(HostsTestCase):

    @override_settings(ROOT_HOSTCONF='django_hosts.tests.hosts.simple')
    def test_get_hostconf_module(self):
        from django_hosts.tests.hosts import simple
        self.assertEqual(get_hostconf_module(), simple)

    def test_get_hostconf_module_no_default(self):
        from django_hosts.tests.hosts import simple
        self.assertEqual(
            get_hostconf_module('django_hosts.tests.hosts.simple'), simple)

    def test_missing_host_patterns(self):
        self.assertRaisesWithMessage(ImproperlyConfigured,
            'Missing ROOT_HOSTCONF setting', get_host_patterns)

    @override_settings(ROOT_HOSTCONF='django_hosts.tests.hosts')
    def test_missing_host_patterns_in_module(self):
        self.assertRaisesWithMessage(ImproperlyConfigured,
            "Missing host_patterns in 'django_hosts.tests.hosts'",
            get_host_patterns)

    @override_settings(ROOT_HOSTCONF='django_hosts.tests.hosts.simple')
    def test_get_working_host_patterns(self):
        from django_hosts.tests.hosts import simple
        self.assertEqual(get_host_patterns(), simple.host_patterns)

    @override_settings(ROOT_HOSTCONF='django_hosts.tests.hosts.simple')
    def test_get_host(self):
        self.assertEqual(get_host('static').name, 'static')
        self.assertRaisesWithMessage(NoReverseMatch,
            "No host called 'non-existent' exists", get_host, 'non-existent')

    @override_settings(ROOT_HOSTCONF='django_hosts.tests.hosts.appended')
    def test_appended_patterns(self):
        self.assertEqual(get_host('special').name, 'special')


import sqlite3 as dbapi

def main():
    bbdd = dbapi.connect("bbdd.dat")
    c = bbdd.cursor() #siempre se usa un cursor
    #c.execute("""create table empleados (dni text, nombre text, departamento text)""")
    #c.execute("""insert into empleados
    #                values ('12345678-A', 'Manuel Gil', 'Contabilidad')""")

    #bbdd.commit()
    #c.execute("""select * from empleados
    #              where departamento='Contabilidad'""")
    depto = 'Contabilidad'
    nombre = 'Manuel Gil'
    c.execute("""select * from empleados
                  where departamento=? and nombre =?""",(depto,nombre,))

    for tupla in c.fetchall():
        print (tupla)

    c.close()
    bbdd.close()
if __name__ == '__main__':
    main()


""" class added for automatic report generation """
import sys
from threading import RLock

from crawler.hasher import SimHash

def to_milli(full_time):
    return int(round(full_time * 1000))

class Report:
    def __init__(self):
        self.lock_report = RLock()
        self.simhash = SimHash()
        self.unique_pages = 0
        self.longest_page = ('', 0)
        self.common_words = dict()
        self.sub_domains = dict()

        self.scraped_domains = [
            '.ics.uci.edu',
            '.cs.uci.edu',
            '.informatics.uci.edu',
            '.stat.uci.edu',
            'today.uci.edu'
        ]
        self.domain_locks = []
        for i in range(5):
            self.domain_locks.append(RLock())

    def update_unique(self, num_pages):
        self.lock_report.acquire()
        self.unique_pages += num_pages
        self.lock_report.release()

    def update_longest(self, new_longest_page):
        self.lock_report.acquire()
        self.longest_page = new_longest_page
        self.lock_report.release()

    def update_common(self, new_words):
        self.lock_report.acquire()
        for word in new_words:
            if self.common_words.get(word):
                self.common_words[word] += new_words[word]
            else:
                self.common_words[word] = new_words[word]
        self.lock_report.release()

    def write_common_words(self, file):
        fifty_words = sorted(self.common_words.items(),
                             key=lambda x: x[1],
                             reverse=True)
        if len(fifty_words) >= 50:
            fifty_words = fifty_words[:50]
        i = 1
        file.write("50 most common words:\n")
        for word, freq in fifty_words:
            file.write("\t" + str(i) + ") " + str(word) + " -> " + str(freq) + "\n")
            i += 1
        file.write("\n\n\n")

    def update_domains(self, domain):
        self.lock_report.acquire()
        if self.sub_domains.get(domain[0]):
            self.sub_domains[domain[0]] += domain[1]
        else:
            self.sub_domains[domain[0]] = domain[1]
        self.lock_report.release()

    def write_domains(self, file):
        domains = sorted(self.sub_domains.items())
        i = 1
        file.write("Sub-domains for ics.uci.edu:\n")
        for dom, freq in domains:
            if type(dom) is bytes:
                dom = dom.decode('utf-8')
            file.write("\t" + str(i) + ") " + str(dom) + " -> " + str(freq) + "\n")
            i += 1

    def lock_domain(self, url):
        i = 0
        for domain in self.scraped_domains:
            if url.find(domain) != -1:
                self.domain_locks[i].acquire()
                break
            i += 1

    def release_domain(self, url):
        i = 0
        for domain in self.scraped_domains:
            if url.find(domain) != -1:
                self.domain_locks[i].release()
                break
            i += 1

    def generate_report(self):
        try:
            with open("hw2_report.txt", 'w') as report_file:
                report_file.write("Unique pages found:\t" + str(self.unique_pages) + "\n")
                report_file.write("Longest page by words:\n\t"
                                  + str(self.longest_page[0]) + "\n\t" + str(self.longest_page[1])
                                  + "\n\n\n")
                self.write_common_words(report_file)
                self.write_domains(report_file)
        except IOError:
            print("Report Error: could not write to hw2_report.txt file", file=sys.stderr)


import cProfile

def test_fib(func):
    lst = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
    for i, item in enumerate(lst):
        assert item == func(i)
        print(f'Test {i} OK')

def fib_loop(n):

    if n < 2:
        return n

    first, second = 0, 1
    for i in range(2, n + 1):
        first, second = second, first + second

    return second

cProfile.run('fib_loop(1000)')

# test_fib(fib_loop)

# lesson_04>python -m timeit -n 1000 -s "import Task_06" "Task_06.fib_loop(10)"
# 1000 loops, best of 5: 702 nsec per loop

# python -m timeit -n 1000 -s "import Task_06" "Task_06.fib_loop(100)"
# 1000 loops, best of 5: 6.59 usec per loop

# python -m timeit -n 1000 -s "import Task_06" "Task_06.fib_loop(500)"
# 1000 loops, best of 5: 30.2 usec per loop

# python -m timeit -n 1000 -s "import Task_06" "Task_06.fib_loop(50000)"
# 1000 loops, best of 5: 27 msec per loop


import os, sys, subprocess, numpy as np, time

def count_gpu():

    command = 'nvidia-smi'
    out = (subprocess.check_output(command, shell=True)).decode('utf-8')
    flag = cnt = 0
    out = out.split('\n')
    for line in out:
        line = line.strip()
        if line == "|===============================+======================+======================|":
            flag = 1
            continue
        if line == "":
            break
        if flag == 1:
            cnt += 1
    cnt /= 3
    return int(cnt)

threashhold = 3
consumed_memory = []
limit = 1
tot_gpu = count_gpu()
# print(tot_gpu)

while True:
    gpu_set = set()
    command = 'nvidia-smi'
    out = (subprocess.check_output(command, shell=True)).decode('utf-8')
    out = out.split('\n')
    out.reverse()
    cnt = 0
    for i in out:
        sp = i.split()
        # print(sp)
        if len(sp) > 1:
            if sp[3] == "G":
                continue
            gpu_id = sp[1]
            # print(gpu_id)
            gpu_set.add(int(gpu_id))
        if i == '|=============================================================================|':
            break
        # print(i)
        cnt += 1

    for j in range(tot_gpu):
        if j not in gpu_set:
            print("Runnign on GPU", j)
            command = ''
            (subprocess.check_output(command, shell=True)).decode('utf-8')
            consumed_memory.append((j, 'all'))


#This is a sketchy Ripoff of pong using TK graphics
#There will be two players, each controlled by a different set of keys, and there will be at least one ball
import tkinter # built-in Python graphics library
import os
import random

balls = []
players = []

class Thing():
    def __init__(self,x,y):
        self.x = x
        self.y = y

class Ball(Thing):
    """docstring for Ball."""
    def __init__(self, x,y):
        Thing.__init__(self,x,y)
        self.sizeX = 5
        self.sizeY = 5
        self.speedX = random.choice([-2,2])
        self.speedY = random.randint(-2,2)
        self.color = '#{0:0>6x}'.format(random.randint(00,16**6))

    def move(self):
        self.x += self.speedX
        self.y += self.speedY
    def collideY(self):
        #Makes ball bounce off upper or lower ends of screen
        if self.y + self.speedY <= 0 or self.y + self.speedY + self.sizeY >= 400:
            self.speedY = self.speedY * -1
    def drawBall(self,canvas):
        canvas.create_oval(self.x, self.y, self.x + self.sizeX, self.y + self.sizeY,
                           fill=self.color, outline="black")

#Generic Player
class Player(Thing):
    """docstring for Player."""
    def __init__(self,x,y):
        Thing.__init__(self,x,y)
        self.score = 0
        self.sizeX = 10
        self.sizeY = 40
        self.score = 0
        self.color = '#{0:0>6x}'.format(00)

    def moveDown(self):
        if self.y + self.sizeY < 400:
            self.y += 10

    def moveUp(self):
        if self.y > 0:
            self.y -= 10

    def drawPlayer(self,canvas):
        canvas.create_rectangle(self.x, self.y, self.x + self.sizeX, self.y + self.sizeY,
                           fill="black", outline="black")

#Specific Player
class Player1(Player):
    def __init__(self,x,y):
        Player.__init__(self,x,y)

    def score(self,ball):
        if ball.x + ball.sizeX >= 800:
            self.score += 1
            ball.x = 400
            ball.y = 200

    def hitBall(self,ball):
        if ball.x + ball.speedX <= self.sizeX+10:
            #might need to switch this
            if ball.y >= self.y and ball.y + ball.sizeY <= self.y + self.sizeY:
                ball.speedX = ball.speedX * -1
                ball.speedY = (((ball.y + ball.sizeY/2) - (self.y + self.sizeY/2))/20)*5

#Specific Player
class Player2(Player):
    def __init__(self,x,y):
        Player.__init__(self,x,y)
    def score(self,ball):
        if ball.x <= 0:
            self.score += 1
            ball.x = 400
            ball.y = 200

    def hitBall(self,ball):
        if ball.x + ball.sizeX + ball.speedX >= 790:
            if ball.y >= self.y and ball.y + ball.sizeY <= self.y + self.sizeY:
                ball.speedX = ball.speedX * -1
                ball.speedY = (((ball.y + ball.sizeY/2) - (self.y + self.sizeY/2))/20)*5

def setup():
    global balls, players
    players.append(Player1(10,180))
    players.append(Player2(790,180))
    balls.append(Ball(400,200))

def player1Up(event):
    global players
    players[0].moveUp()
def player1Down(event):
    global players
    players[0].moveDown()
def player2Up(event):
    global players
    players[1].moveUp()
def player2Down(event):
    global players
    players[1].moveDown()

def draw(canvas):
    '''Clear the canvas, have all game objects update and redraw, then set up the next draw.'''
    delay = 15 # milliseconds, so about 30 frames per second
    global balls, players
    canvas.delete(tkinter.ALL)
    for player in players:
        player.drawPlayer(canvas)
        for ball in balls:
            player.hitBall(ball)
            #player.score(ball)
    for ball in balls:
        ball.collideY()
        ball.move()
        ball.drawBall(canvas)

    canvas.after(delay, draw, canvas) # call this draw function with the canvas argument again after the delay

def keydown(event):
    global pressState
    presState = 1
    print(event.char)
def keyup(event):
    global pressState
    presState = 1
    print("hi")
    print(event.char)

if __name__ == '__main__':

    os.system('xset r off')

    # create the graphics root and a 400x400 canvas
    root = tkinter.Tk()
    setup()
    canvas = tkinter.Canvas(root, width=800, height=400)
    canvas.pack()

    #canvas.bind("<KeyPress>", keydown)
    #canvas.bind("<KeyRelease>", keyup)
    # if the user presses a key, call our handlers
    pressState = 0

    root.bind('<KeyRelease-w>', player1Up)
    root.bind('<Key-s>', player1Down)
    root.bind('<Up>', player2Up)
    root.bind('<Down>', player2Down)

    # start the draw loop
    draw(canvas)

    root.mainloop() # keep the window open


from django.conf import settings
from django.core.management import BaseCommand
from entity_subscription.models import Source

from entity_emailer.utils import constants
from entity_emailer.models import EmailTemplate

class Command(BaseCommand):
    def handle(self, *args, **options):
        admin_source_name = getattr(
            settings, 'ENTITY_EMAILER_ADMIN_SOURCE_NAME', constants['default_admin_source_name']
        )
        admin_template_name = getattr(
            settings, 'ENTITY_EMAILER_ADMIN_TEMPLATE_NAME', constants['default_admin_template_name']
        )
        Source.objects.get_or_create(
            name=admin_source_name,
            display_name=admin_source_name,
            description='Admin notifications.',
        )
        EmailTemplate.objects.get_or_create(
            template_name=admin_template_name,
            html_template='{{ html|safe }}',
        )


import matplotlib
matplotlib.use("Agg")

from BlogTutorials.pyimagesearch.dogs_vs_cats.config import dogs_vs_cats_config as config

from BlogTutorials.pyimagesearch.preprocessing.imagetoarraypreprocessor import ImageToArrayPreprocessor
from BlogTutorials.pyimagesearch.preprocessing.simplepreprocessor import SimplePreprocessor
from BlogTutorials.pyimagesearch.preprocessing.patchpreprocessor import PatchPreprocessor
from BlogTutorials.pyimagesearch.preprocessing.meanpreprocessor import MeanPreprocessor

from BlogTutorials.pyimagesearch.callbacks.trainingmonitor import TrainingMonitor
from BlogTutorials.pyimagesearch.io_module.hdf5datasetgenerator import HD5FDatasetGenerator
from BlogTutorials.pyimagesearch.nn.conv.alexnet import Alexnet

from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
import json
import os

# data augmentation training
aug = ImageDataGenerator(rotation_range=20,
                         zoom_range=0.15,
                         width_shift_range=0.2,
                         height_shift_range=0.2,
                         shear_range=0.15,
                         horizontal_flip=True,
                         fill_mode="nearest")

# load RGB mean
means = json.loads(open(config.DATA_SET_MEAN).read())

# preprocessors
sp = SimplePreprocessor(227, 227) # used only on validation data
pp = PatchPreprocessor(227, 227)
mp = MeanPreprocessor(means["R"], means["G"], means["B"])
iap = ImageToArrayPreprocessor()

# training and validation dataset generatros
trainGen = HD5FDatasetGenerator(config.TRAIN_HDF5, 32, aug=None,
                                preprocessor=[pp, mp, iap], classes=2)
valGen = HD5FDatasetGenerator(config.VAL_HDF5, 32,
                              preprocessor=[sp, mp, iap], classes=2)

# Optimizer
print("[info] compiling model...")
opt = Adam(lr=1e-3)
model = Alexnet.build(width=227,
                      height = 227,
                      depth=3,
                      classes=2,
                      reg=0.0002)
model.compile(loss="binary_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])

# callabacks
path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(os.getpid())])
# To save model at each epoch if using ctrl+C method
# monitor optios are val_loss, val_acc, train_loss, train_acc, etc. mode="max/min"
# checkpoint = ModelCheckpoint(fname, monitor="val_loss", mode="min", save_best_only=True, verbose=1)
# callbacks = [checkpoint]
callbacks = [TrainingMonitor(path)]

# train
model.fit_generator(
    trainGen.generator(),
    steps_per_epoch=trainGen.numImages // 32,
    validation_data=valGen.generator(),
    validation_steps=valGen.numImages // 32,
    epochs=40,
    max_queue_size=32*2,
    callbacks=callbacks,
    verbose=1)

# save model
print("Saving model...")
model.save(config.MODEL_PATH, overwrite=True)

trainGen.close()
valGen.close()


import discord, base64
from discord.ext import commands, tasks

class UtilsCog(commands.Cog, name="Some general utils"):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='hex2ascii', description="Hex to ascii\nTakes a hex string 0x123456 and returns ascii")
    async def hex2ascii(self, ctx, target: str):
        try:
            if target[:2].lower() == '0x':
                await ctx.send(bytes.fromhex(target[2:]).decode('ASCII'))
            else:
                await ctx.send(bytes.fromhex(target).decode('ASCII'))
        except Exception as e:
            await ctx.send(str(e))

    @commands.command(name="b64", description="decode or encode strings with base64")
    async def b64(self, ctx, operation: str, message: str):
        if operation[:1].lower() == 'e':
            await ctx.send(str(base64.b64encode(message.encode('ASCII'))))
        elif operation[:1].lower() == 'd':
            await ctx.send(str(base64.b64decode(message)))

def setup(bot):
    bot.add_cog(UtilsCog(bot))


import os
import sys
import time
import utils
import torch
import model
import dataloader
import numpy as np
import torch.nn as nn
from copy import deepcopy

os.environ["CUDA_VISIBLE_DEVICES"] = "2"

device = "cuda"
dataset_name = "facescrub"
root_path = "./result/align"
save_model_path = os.path.join(root_path, "target_models")
save_log_path = os.path.join(root_path, "target_logs")
save_img_path = os.path.join(root_path, "target_imgs")
os.makedirs(save_img_path, exist_ok=True)
os.makedirs(save_log_path, exist_ok=True)
os.makedirs(save_model_path, exist_ok=True)

model_name = "SimpleCNN"
model_v = sys.argv[1]
n_epochs = 300
noise = float(sys.argv[2])
dp_lr = 1e-2
momentum = 0.9
weight_decay = 1e-4

microbatch_size = 1
g_clip_thres = 1

target_path = os.path.join(root_path, "target_models/{}_{}.tar".format(model_name, model_v))

def eval_net(net, dataloader):
    net.eval()
    cnt, acc, loss_tot = 0, 0, 0
    for img, label in dataloader:
        img, label = img.to(device), label.to(device)
        label = label.view(-1)
        out_prob = net(img)
        out_label = torch.argmax(out_prob, dim=1).view(-1)
        acc += torch.sum(out_label == label).item()
        cnt += img.size(0)

    return acc * 100.0 / cnt

def main(args, trainloader, testloader, noise_multiplier):
    net = utils.get_model(args, model_name, model_v)
    net = torch.nn.DataParallel(net).to(device)

    if os.path.isfile(target_path):
        print('Load Existing Model')
        utils.load_state_dict(net, target_path)
    else:
        print('Create New Model')

    optimizer = torch.optim.SGD(net.parameters(),
                                lr=dp_lr,
                                momentum=momentum,
                                weight_decay=weight_decay)
    criterion = nn.CrossEntropyLoss(reduction='none').cuda()
    best_ACC = 0

    print("Start Training!")
    for e in range(n_epochs):
        tf = time.time()
        net.train()
        loss_tot, avg_norm, cnt, step = 0.0, 0.0, 0, 0

        for imgs, labels in trainloader:
            imgs, labels = imgs.to(device), labels.to(device)
            bs = imgs.size(0)
            outputs = net(imgs)
            loss = criterion(outputs, labels)

            grad = [torch.zeros_like(param) for param in net.parameters()]
            num_microbatch = bs / microbatch_size
            for j in range(0, bs, microbatch_size):
                optimizer.zero_grad()
                torch.autograd.backward(loss[j:j+microbatch_size], retain_graph=True)

                L2_norm = 0.0
                for param in net.parameters():
                    L2_norm += (param.grad * param.grad).sum()
                L2_norm = torch.sqrt(L2_norm)
                avg_norm = avg_norm * 0.95 + L2_norm * 0.05

                coef = float(g_clip_thres) / max(g_clip_thres, L2_norm.item())
                grad = [g + param.grad * coef for param, g in zip(net.parameters(), grad)]

            for param, g in zip(net.parameters(), grad):
                if noise_multiplier > 0:
                    param.grad.data = g + torch.cuda.FloatTensor(g.size()).normal_(0, noise_multiplier * float(g_clip_thres))
                else:
                    param.grad.data = g
                param.grad.data /= num_microbatch

            optimizer.step()
            loss_tot += torch.sum(loss).item()
            cnt += imgs.shape[0]
            step += 1

        train_loss = loss_tot / cnt
        train_accuracy = eval_net(net, trainloader)
        test_accuracy = eval_net(net, testloader)
        interval = time.time() - tf
        print("Epoch:{}\tTime:{:.2f}\tTrain Loss:{:.2f}\tTrain Acc:{:.2f}\tTest Acc:{:.2f}".format(e, interval, train_loss, train_accuracy, test_accuracy))

        if test_accuracy > best_ACC:
            best_ACC = test_accuracy
            best_model = deepcopy(net)
            torch.save({'state_dict':best_model.state_dict()}, os.path.join(save_model_path, "{}_{}.tar".format(model_name, model_v)))

    print("The best accuracy is %.03f." % (best_ACC))
    print("==============================================================================")

if __name__ == '__main__':
    file = dataset_name + ".json"
    args = utils.load_params(file)
    log_file = "train_" + args['dataset']['model_name'] + '_{}.txt'.format(model_v)
    logger = utils.Tee(os.path.join(save_log_path, log_file), 'w')

    train_file = args['dataset']['train_file']
    test_file = args['dataset']['test_file']
    trainloader = utils.init_dataloader(args, train_file, mode="train")
    testloader = utils.init_dataloader(args, test_file, mode="test")
    main(args, trainloader, testloader, noise)


__author__ = 'Vlad'

import math as m
import numpy as np

w = [1,1]

def partial_x(x,y):
    return 2*x

def partial_y(x,y):
    return 2*y

def norm_gradient(u,v):

    gradient_vector = [partial_x(x,y),partial_y(x,y)]

    vector_norm = np.linalg.norm(gradient_vector, ord=1)
    normalized_vector = [-1.0*x/vector_norm for x in gradient_vector]
    return normalized_vector
#
# def e_in(u,v):
#     return (u*m.exp(v)-2*v*m.exp(-u))**2
#
# delta_e = 1
# w_0 = [1,1]
# eta = 0.00002
# i=0
#
# while m.fabs(delta_e) >= 10e-14 and i<60000:
#     norm = norm_gradient(*w_0)
#     eta_times_v = np.multiply(eta,norm)
#     update = np.add(w_0,eta_times_v)
#     e_in_new = e_in(*update)
#     e_in_old = e_in(*w_0)
#     delta_e = e_in_new - e_in_old
#     w_0 = update
#     i+=1

x = 10
eta = 0.1
eps = 20
i=0

while eps > 10e-14:
    upd_x = x - 2*x*eta
    eps = m.fabs(x-upd_x)
    x = upd_x
    i+=1

print((x,i))
# print(delta_e)
# print(norm_gradient(1,1))
# print(np.linalg.norm(norm_gradient(1,1), ord=1))


#!/usr/bin/env python3

from math import log

def flip(S):
    # Reverse the string.
    Sr = reversed(S)

    # Start on +, since we'll need happy side up at the end.
    current = '+'

    # Iterate through the (reversed) string, counting the number of times
    # the current character changes.
    # e.g. '+++' -> 1; '---' -> 1, since we start on +.
    # '-+-' -> 3; '--+' -> 2; '++-' -> 1.
    nflips = 0
    for face in Sr:
        if face != current:
            current = face
            nflips += 1

    # Return the number of changes.
    return nflips

def oracle(S):
    assert(flip(S) < len(S)+1)
    if '-' in S:
        assert(flip(S) > 0)
        if '+-' in S:
            assert(flip(S) > 1)
            if '-+' not in S:
                assert(flip(S) == 2)
        else:
            assert(flip(S) == 1)
    else:
        assert(flip(S) == 0)

if __name__ == '__main__':

    T = int(input())
    for i in range(1, T+1):
        S = input()
        oracle(S)

        print("Case #{}: {}".format(i, flip(S)))


"""Computes ground-state and time-dependent charge density in the Hybrid
Hartree-Fock-LDA approximation.

The code outputs the ground-state charge density, the energy of the
system and the single-quasiparticle orbitals.
"""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import copy
import pickle
import numpy as np
import scipy as sp
import scipy.sparse as sps
import scipy.linalg as spla
from iDEA.input import Input
from . import results as rs
import iDEA.LDA
import iDEA.HF
import iDEA.NON

def hamiltonian(pm, eigf, density, alpha, occupations, perturb=False):
   r"""Compute HF Hamiltonian.

   Computes HYB Hamiltonian from a given set of single-particle states.

   .. math:: H_{\alpha}(x,y) = \delta(x-y)\hat{T} + \delta(x-y)v_{\text{ext}}(y) + \delta(x-y)v_{\text{H}}(y) + \alpha\Sigma_{\text{x}}(x,y) + (1-\alpha)\delta(x-y)v_{\text{xc}}^{\text{LDA}}(y)

   parameters
   ----------
   eigf  array_like
      single-particle states
   density  array_like
      electron density
   alpha  float
      HF-LDA mixing parameter (1 = all HF, 0 = all LDA)
   occupations: array_like
      orbital occupations
   perturb: bool
      If True, add perturbation to external potential (for time-dep. runs)

   returns array_like
      Hamiltonian matrix
    """

   # Construct kinetic energy.
   sd = pm.space.second_derivative
   sd_ind = pm.space.second_derivative_indices
   T = -0.5*sps.diags(sd, sd_ind, shape=(pm.sys.grid,pm.sys.grid), format='csr', dtype=complex).toarray()

   # Construct external potential.
   Vext = sps.diags(pm.space.v_ext, 0, shape=(pm.sys.grid,pm.sys.grid), format='csr', dtype=complex).toarray()
   if perturb:
      Vext += sps.diags(pm.space.v_pert, 0, shape=(pm.sys.grid,pm.sys.grid), format='csr', dtype=complex).toarray()

   # Construct Hartree potential.
   Vh = iDEA.HF.hartree(pm, density)

   # Construct LDA Vxc.
   if not pm.hyb.seperate:
      Vxc_LDA = iDEA.LDA.xc_potential(pm, density, pm.hyb.seperate)
   else:
      Vxc_LDA, Vx_LDA, Vc_LDA = iDEA.LDA.xc_potential(pm, density, pm.hyb.seperate)

   # Construct the Fock operator.
   if alpha != 0:
      F = iDEA.HF.fock(pm, eigf)
      F *= pm.space.delta
   else:
      F = np.zeros((pm.sys.grid, pm.sys.grid))

   # Construct hybrid Vxc.
   if not pm.hyb.seperate:
      Vxc = alpha*F + (1-alpha)*np.diag(Vxc_LDA)
   else:
      Vxc = alpha*F + (1-alpha)*np.diag(Vx_LDA) + np.diag(Vc_LDA)

   # Construct H.
   H = T + Vext + np.diag(Vh) + Vxc
   if not pm.hyb.seperate:
      return H, Vh, Vxc_LDA, F
   else:
      return H, Vh, Vxc_LDA, Vx_LDA, Vc_LDA, F

def calc_with_alpha(pm, alpha, occupations):
   r"""Calculate with given alpha.

   Perform hybrid calculation with given alpha.

   parameters
   ----------
   alpha  float
      HF-LDA mixing parameter (1 = all HF, 0 = all LDA)
   occupations: array_like
      orbital occupations

   returns density, eigf, eigv, E
      Hybrid Density, orbitals and total energy
    """

   # Initialise self-consistency.
   counter = 0
   convergence = 1.0
   H, Vh, Vxc_LDA, F = hamiltonian(pm, np.zeros((pm.sys.grid,pm.sys.grid)), np.zeros(pm.sys.grid), alpha, occupations)
   density, eigf, eigv = iDEA.HF.groundstate(pm, H)
   E = 0

   # Convergence boolean:
   converged = False

   # Perform self-consistency.
   while not converged and counter < pm.hyb.max_iter:
      # Keep copies to check convergence.
      density_old = copy.deepcopy(density)
      H_old =  copy.deepcopy(H)

      # Construct hybrid Hamiltonian.
      if not pm.hyb.seperate:
         H, Vh, Vxc_LDA, F = hamiltonian(pm, eigf, density, alpha, occupations)
      else:
         H, Vh, Vxc_LDA, Vx_LDA, Vc_LDA, F = hamiltonian(pm, eigf, density, alpha, occupations)

      # Mix for stability.
      H = pm.hyb.mix*H + (1.0-pm.hyb.mix)*H_old

      # Solve single-particle Shrodinger equation.
      density, eigf, eigv = iDEA.HF.groundstate(pm, H)

      if pm.sys.NE > 0:
         # Get a list of occupied wavefunctions.
         occupied = copy.deepcopy(eigf[:, :pm.sys.NE])

         # Scale HOMO orbital by its occupation.
         # Only this orbital can have fractional occupation.
         occupied[:,-1] = occupied[:,-1]*np.sqrt(occupations[-1])

         # Calculate density associated with occupied orbitals.
         # This takes into account fractional occupation.
         density = np.sum(occupied*occupied.conj(), axis=1).real

      # Test for convergance.
      convergence = sum(abs(density - density_old))
      converged = convergence < pm.hyb.tol and counter > 20

      pm.sprint('HYB: computing GS density with alpha = {:05.4f}, convergence = {:06.5e}'.format(alpha, convergence), 1, newline=False)
      counter += 1

      E_H = -0.5*np.dot(Vh, density)*pm.sys.deltax
      E_SYS = 0
      E_F = 0
      for i in range(pm.sys.NE):
         # Calculate system energy.
         E_SYS += eigv[i]*occupations[i]
         # Calculate exchange energy.
         orb = copy.deepcopy(eigf[:,i]) * np.sqrt(occupations[i])
         E_F -= 0.5 * np.dot(orb.conj().T, np.dot(F, orb)) * pm.sys.deltax

      # LDA energy:
      if pm.hyb.seperate:
          Exc_LDA, Ex_LDA, Ec_LDA = iDEA.LDA.xc_energy(pm, density)
          Evx_LDA = Ex_LDA - np.dot(density, Vx_LDA)*pm.sys.deltax
          Evc_LDA = Ec_LDA - np.dot(density, Vc_LDA)*pm.sys.deltax
          E = (E_SYS + E_H + alpha*E_F + (1.0 - alpha)*Evx_LDA + Evc_LDA).real
      else:
          Evxc_LDA = iDEA.LDA.xc_energy(pm, density) - np.dot(density, Vxc_LDA)*pm.sys.deltax
          E = (E_SYS + E_H + alpha*E_F + (1.0 - alpha)*Evxc_LDA).real

      # Calculate charges on grid.
      grid_charge = np.sum(density)*pm.sys.deltax

   # Print iteration values.
   pm.sprint('\nHYB: Total charge on grids: {:10.9f}'.format(grid_charge), 1, newline=True)
   pm.sprint('HYB: total energy = {0} converged in {1} iterations'.format(E, counter), 1, newline=True)
   pm.sprint('HYB: HOMO-LUMO gap = {0}\n'.format(eigv[pm.sys.NE]-eigv[pm.sys.NE-1]), 1, newline=True)
   return density, eigf, eigv, E

def save_results(pm, results, density, E, eigf, eigv, alpha):
   r"""Saves hybrid results to outputs directory
   """
   results.add(density,'gs_hyb{:05.3f}_den'.format(alpha).replace('.','_'))
   results.add(E,'gs_hyb{:05.3f}_E'.format(alpha).replace('.','_'))
   results.add(eigf.T,'gs_hyb{:05.3f}_eigf'.format(alpha).replace('.','_'))
   results.add(eigv,'gs_hyb{:05.3f}_eigv'.format(alpha).replace('.','_'))
   if pm.run.save:
      results.save(pm)

def optimal_alpha(pm, results, alphas, occupations):
   r"""Calculate optimal alpha.

   Calculate over range of alphas to determine optimal alpha.

   parameters
   ----------
   results  Results Object
      object to add results to
   alphas: array_like
      range of alphas to use
   occupations: array_like
      orbital occupations
   """
   pm.sprint('HYB: Finding optimal value of alpha', 1, newline=True)

   # Run E(N) calculations.
   nElect = pm.sys.NE
   pm.sprint('HYB: Starting calculations for N electrons (N={})'.format(pm.sys.NE), 1, newline=True)
   energies, eigsHOMO = n_run(pm, results, alphas, occupations)

   # Run E(N-1) calculations.
   pm.sys.NE = nElect - 1
   pm.sprint('HYB: Starting calculations for N-1 electrons (N-1={})'.format(pm.sys.NE), 1, newline=True)
   energies_minus_one, eigsLUMO = n_minus_one_run(pm, results, alphas, occupations)

   # Save all results.
   results.add(alphas,'gs_hyb_alphas')
   results.add(energies,'gs_hyb_enN')
   results.add(energies_minus_one,'gs_hyb_enNm')
   results.add(eigsLUMO,'gs_hyb_eigL')
   results.add(eigsHOMO,'gs_hyb_eigH')
   if pm.run.save:
      results.save(pm)

def n_run(pm, results, alphas, occupations):
   r"""Calculate for :math:`N` electron run.

   Calculate total energy and HOMO eigenvalue of N electron system.

   parameters
   ----------
   results  Results Object
      object to add results to
   alphas: array_like
      range of alphas to use
   occupations: array_like
      orbital occupations
   """
   alphas_length = alphas.shape[0]
   eigsHOMO  = np.empty(alphas_length)
   energies  = np.empty(alphas_length)
   for i in range(alphas_length):
      density, eigf, eigv, energy = calc_with_alpha(pm, alphas[i], occupations)
      eigsHOMO[i] = eigv[pm.sys.NE - 1]
      energies[i] = energy
      save_results(pm, results, density, energy, eigf, eigv, alphas[i])
   return energies, eigsHOMO

def n_minus_one_run(pm, results, alphas, occupations):
   r"""Calculate for :math:`N-1` electron run.

   Calculate total energy and LUMO eigenvalue of :math:`N-1` electron
   system.

   parameters
   ----------
   results  Results Object
      object to add results to
   alphas: array_like
      range of alphas to use
   occupations: array_like
      orbital occupations
   """
   alphas_length = alphas.shape[0]
   eigsLUMO  = np.empty(alphas_length)
   energies  = np.empty(alphas_length)
   for i in range(alphas_length):
      density, eigf, eigv, energy = calc_with_alpha(pm, alphas[i], occupations)
      eigsLUMO[i] = eigv[pm.sys.NE]
      energies[i] = energy
   return energies, eigsLUMO

def fractional_run(pm, results, occupations, fractions):
   energies = np.array([])
   eigsHOMO = np.array([])
   eigsLUMO = np.array([])
   pm.sprint('\nHYB: Running fractional numbers of electrons from {} to {}\n'.format(fractions[0], fractions[-1]), 1, newline=True)
   for num_electrons in fractions:
      pm.sprint('HYB: Current total number of electrons = {:05.4f}'.format(num_electrons), 1, newline=True)
      pm.sys.NE = int(np.ceil(num_electrons))
      if pm.sys.NE == 0:
         pm.sys.NE = 1
         occupations = np.zeros(pm.sys.NE)
      else:
         occupations = np.ones(pm.sys.NE)
         occupations[-1] = num_electrons - int(np.ceil(num_electrons)) + 1
      # Run a normal calculation.
      density, eigf, eigv, energy = calc_with_alpha(pm, pm.hyb.alpha, occupations)
      eigsHOMO = np.append(eigsHOMO, eigv[pm.sys.NE - 1])
      eigsLUMO = np.append(eigsLUMO, eigv[pm.sys.NE])
      energies = np.append(energies, energy)
   results.add(fractions,'gs_hyb_frac{:05.3f}'.format(pm.hyb.alpha).replace('.','_'))
   results.add(eigsHOMO, 'gs_hyb_frac{:05.3f}_HOMO'.format(pm.hyb.alpha).replace('.','_'))
   results.add(eigsLUMO, 'gs_hyb_frac{:05.3f}_LUMO'.format(pm.hyb.alpha).replace('.','_'))
   results.add(energies, 'gs_hyb_frac{:05.3f}_en'.format(pm.hyb.alpha).replace('.','_'))
   if (pm.run.save):
      results.save(pm)

def main(parameters):
   r"""Performs Hybrid calculation.

   parameters
   ----------
   parameters : object
      Parameters object

   returns object
      Results object
   """
   # Set up parameters.
   pm = parameters
   pm.setup_space()
   results = rs.Results()

   # Choose type of LDA to be run.
   if pm.hyb.seperate:
      pm.lda.NE = 'heg'
   else:
      pm.lda.NE = 3

   # Occupations of all the orbitals.
   # Only the last value should be fractional.
   occupations = np.ones(pm.sys.NE)

   # Run code to find optimal alpha.
   # No fractional occupation in this part.
   if pm.hyb.functionality == 'o':
      alphas = np.linspace(pm.hyb.of_array[0], pm.hyb.of_array[1], pm.hyb.of_array[2])
      optimal_alpha(pm, results, alphas, occupations)

   # Run code to get fractional numbers of electrons.
   elif pm.hyb.functionality == 'f':
      fractions = np.linspace(pm.hyb.of_array[0], pm.hyb.of_array[1], pm.hyb.of_array[2])
      #fractions = np.array([1.0, 0.9])
      fractional_run(pm, results, occupations, fractions)

   # Run code for one given alpha.
   elif pm.hyb.functionality == 'a':
      occupations[pm.sys.NE - 1] = 1.0
      density, eigf, eigv, E = calc_with_alpha(pm, pm.hyb.alpha, occupations)
      save_results(pm, results, density, E, eigf, eigv, pm.hyb.alpha)

   # Print an error message.
   else:
      pm.sprint('HYB: Functionality chosen is not valid. Please choose from a, o, and f.', 1, newline=True)

   return results


"""
Base class for equipment that is connected through a serial port.
"""
import time

import serial

from .connection_message_based import ConnectionMessageBased
from .constants import (
    Parity,
    StopBits,
    DataBits,
    REGEX_SERIAL,
    REGEX_PROLOGIX,
)

class ConnectionSerial(ConnectionMessageBased):

    def __init__(self, record):
        """Base class for equipment that is connected through a serial port.

        The :attr:`~msl.equipment.record_types.ConnectionRecord.properties`
        for a serial connection supports the following key-value pairs in the
        :ref:`connections-database` (see also :class:`serial.Serial` for more
        details about each parameter)::

            'read_termination': str or None, read until this termination sequence is found [default: '\\n']
            'write_termination': str or None, termination sequence appended to write messages [default: '\\r\\n']
            'termination': shortcut for setting both 'read_termination' and 'write_termination' to this value
            'max_read_size': int, the maximum number of bytes that can be read [default: 2**16]
            'encoding': str, the encoding to use [default: 'utf-8']
            'encoding_errors': str, encoding error handling scheme, e.g. 'strict', 'ignore' [default: 'strict']
            'timeout': float or None, the timeout (in seconds) for read and write operations [default: None]
            'baud_rate': int, the baud rate [default: 9600]
            'parity': str or None, parity checking, e.g. 'even', 'odd' [default: None]
            'data_bits': int, the number of data bits, e.g. 5, 6, 7, 8 [default: 8]
            'stop_bits': int or float, the number of stop bits, e.g. 1, 1.5, 2 [default: 1]
            'inter_byte_timeout': float or None, the inter-character timeout [default: None]
            'dsr_dtr': bool, enable hardware (DSR/DTR) flow control [default: False]
            'rts_cts': bool, enable hardware (RTS/CTS) flow control [default: False]
            'xon_xoff': bool, enable software flow control [default: False]

        The :data:`~msl.equipment.record_types.ConnectionRecord.backend`
        value must be equal to :data:`~msl.equipment.constants.Backend.MSL`
        to use this class for the communication system. This is achieved by
        setting the value in the **Backend** field for a connection record
        in the :ref:`connections-database` to be ``MSL``.

        Do not instantiate this class directly. Use the
        :meth:`~.EquipmentRecord.connect` method to connect to the equipment.

        Parameters
        ----------
        record : :class:`~.EquipmentRecord`
            A record from an :ref:`equipment-database`.

        Raises
        ------
        ~msl.equipment.exceptions.MSLConnectionError
            If the serial port cannot be opened.
        """
        self._serial = serial.Serial()
        super(ConnectionSerial, self).__init__(record)

        info = ConnectionSerial.parse_address(record.connection.address)
        if info is None:
            self.raise_exception('Invalid address {!r}'.format(record.connection.address))
        self._serial.port = info['port']

        props = record.connection.properties
        self._serial.parity = props.get('parity', Parity.NONE).value
        self._serial.inter_byte_timeout = props.get('inter_byte_timeout', None)

        try:
            self._serial.baudrate = props['baud_rate']
        except KeyError:
            self._serial.baudrate = props.get('baudrate', 9600)

        try:
            self._serial.bytesize = props['data_bits'].value
        except KeyError:
            self._serial.bytesize = props.get('bytesize', DataBits.EIGHT).value

        try:
            self._serial.stopbits = props['stop_bits'].value
        except KeyError:
            self._serial.stopbits = props.get('stopbits', StopBits.ONE).value

        try:
            self._serial.xonxoff = props['xon_xoff']
        except KeyError:
            self._serial.xonxoff = props.get('xonxoff', False)

        try:
            self._serial.rtscts = props['rts_cts']
        except KeyError:
            self._serial.rtscts = props.get('rtscts', False)

        try:
            self._serial.dsrdtr = props['dsr_dtr']
        except KeyError:
            self._serial.dsrdtr = props.get('dsrdtr', False)

        try:
            self._serial.open()
        except serial.serialutil.SerialException as err:
            # don't raise SerialException, use self.raise_exception below
            error_msg = str(err)
        else:
            error_msg = None

        if error_msg:
            self.raise_exception(error_msg)

        self.log_debug('Connected to {}'.format(record.connection))

    @property
    def serial(self):
        """:class:`serial.Serial`: The reference to the serial object."""
        return self._serial

    @property
    def baud_rate(self):
        """:class:`int`: The baud rate setting."""
        return self._serial.baudrate

    @property
    def data_bits(self):
        """:class:`~.constants.DataBits`: The number of data bits."""
        return DataBits(self._serial.bytesize)

    @property
    def stop_bits(self):
        """:class:`~.constants.StopBits`: The stop bit setting."""
        return StopBits(self._serial.stopbits)

    @property
    def parity(self):
        """:class:`~.constants.Parity`: The parity setting."""
        return Parity(self._serial.parity)

    def _set_backend_timeout(self):
        self._serial.timeout = self._timeout
        self._serial.write_timeout = self._timeout

    def disconnect(self):
        """Close the serial port."""
        try:
            # if the subclass raised an error in the constructor before
            # the this class is initialized then self._serial won't exist
            if self._serial.is_open:
                self._serial.close()
                self.log_debug('Disconnected from {}'.format(self.equipment_record.connection))
        except AttributeError:
            pass

    def write(self, msg):
        """Write a message over the serial port.

        Parameters
        ----------
        msg : :class:`str`
            The message to write.

        Returns
        -------
        :class:`int`
            The number of bytes sent over the serial port.
        """
        data = self._encode(msg)
        return self._serial.write(data)

    def read(self, size=None):
        """Read a message from the serial port.

        Parameters
        ----------
        size : :class:`int`, optional
            The number of bytes to read. If `size` is :data:`None` then read until:

            1. :obj:`~msl.equipment.connection_message_based.ConnectionMessageBased.read_termination`
               characters are read (only if
               :obj:`~msl.equipment.connection_message_based.ConnectionMessageBased.read_termination`
               is not :data:`None`)
            2. :obj:`~msl.equipment.connection_message_based.ConnectionMessageBased.max_read_size`
               bytes have been read (raises :exc:`~msl.equipment.exceptions.MSLConnectionError` if occurs)
            3. :exc:`~msl.equipment.exceptions.MSLTimeoutError` occurs
               (only if :obj:`~msl.equipment.connection_message_based.ConnectionMessageBased.timeout`
               is not :data:`None`)

            This method will block until at least one of the above conditions is fulfilled.

        Returns
        -------
        :class:`str`
            The message from the serial port.
        """
        if size:
            if size > self._max_read_size:
                self.raise_exception('max_read_size is {} bytes, requesting {} bytes'.format(
                    self._max_read_size, size)
                )

            out = self._serial.read(size)
            if len(out) != size:
                self.raise_exception('received {} bytes, requested {} bytes'.format(len(out), size))

        else:
            t0 = time.time()
            out = bytearray()
            while True:
                out.extend(self._serial.read(1))

                if self._read_termination and out.endswith(self._read_termination):
                    out = out[:-len(self._read_termination)]
                    break

                if len(out) > self._max_read_size:
                    self.raise_exception('len(bytes_read) [{}] > max_read_size [{}]'.format(
                        len(out), self._max_read_size)
                    )

                if self._timeout and time.time() - t0 >= self._timeout:
                    self.raise_timeout()

        return self._decode(size, out)

    @staticmethod
    def parse_address(address):
        """Get the serial port from an address.

        Parameters
        ----------
        address : :class:`str`
            The address of a :class:`~msl.equipment.record_types.ConnectionRecord`

        Returns
        -------
        :class:`dict` or :data:`None`
            The serial port in a format that is valid for PySerial (i.e., ``ASRL3`` becomes ``COM3``)
            or :data:`None` if the port cannot be determined from `address`.
        """
        match = REGEX_SERIAL.match(address)
        if match:
            d = match.groupdict()
            prefix = 'COM' if d['dev'] is None else d['dev']
            return {'port': prefix + d['number']}

        match = REGEX_PROLOGIX.match(address)
        if match:
            d = match.groupdict()
            prefix = 'ASRL' if d['name'].startswith('/') else ''
            return ConnectionSerial.parse_address(prefix + d['name'])


from PhysicsTools.Heppy.analyzers.core.TreeAnalyzerNumpy import TreeAnalyzerNumpy
from CMGTools.H2TauTau.proto.analyzers.ntuple import *
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle

class H2TauTauTreeProducerTauMu( TreeAnalyzerNumpy ):
    '''Tree producer for the H->tau tau analysis.

    Some of the functions in this class should be made available to everybody.'''

    def declareVariables(self, setup):

        tr = self.tree

        var( tr, 'run', int)
        var( tr, 'lumi', int)
        var( tr, 'evt', int)
        var( tr, 'NUP', int)
        var( tr, 'rho')

        bookDiLepton(tr)

        var( tr, 'pfmet')

        bookParticle(tr, 'diTau')
        bookTau(tr, 'l1')
        bookGenParticle(tr, 'l1_gen')
        var(tr, 'l1_gen_vis_pt')
        var(tr, 'l1_gen_vis_eta')
        var(tr, 'l1_gen_vis_phi')
        bookMuon(tr, 'l2')
        bookGenParticle(tr, 'l2_gen')
        bookParticle(tr, 'l1Jet')
        bookParticle(tr, 'l2Jet')

        var( tr, 'nJets')
        var( tr, 'nJets20')
        bookJet(tr, 'jet1')
        bookJet(tr, 'jet2')

        # b jets
        var( tr, 'nBJets')
        var(tr, 'nCSVLJets')
        bookJet(tr, 'bjet1')

        bookVBF( tr, 'VBF' )
        #bookJet(tr, 'cjet') # leading central veto jet from VBF

        var( tr, 'weight')
        var( tr, 'vertexWeight')
        var( tr, 'embedWeight')
        var( tr, 'hqtWeight')
        var( tr, 'hqtWeightUp')
        var( tr, 'hqtWeightDown')
        var( tr, 'NJetWeight')

        var( tr, 'tauFakeRateWeight')
        var( tr, 'tauFakeRateWeightUp')
        var( tr, 'tauFakeRateWeightDown')

        var( tr, 'nVert')

        var( tr, 'isFake')
        var( tr, 'isSignal')
        var( tr, 'leptonAccept')
        var( tr, 'thirdLeptonVeto')

        var(tr, 'genMass')
        var(tr, 'genMet')
        var(tr, 'genMex')
        var(tr, 'genMey')

        self.maxNGenJets = 4
        for i in range(0, self.maxNGenJets):
            bookGenParticle(tr, 'genQG_{i}'.format(i=i))

    def declareHandles(self):
        super(H2TauTauTreeProducerTauMu, self).declareHandles()
        self.handles['pfmetraw'] = AutoHandle(
                'slimmedMETs',
                'std::vector<pat::MET>'
                )

    def process(self, event):
        self.readCollections( event.input )

        tr = self.tree
        tr.reset()

        fill( tr, 'run', event.run)
        fill( tr, 'lumi',event.lumi)
        fill( tr, 'evt', event.eventId)

        # This is only relevant for the W/Z+N-jet samples
        if hasattr(event, 'NUP'):
            fill( tr, 'NUP', event.NUP)
        fill( tr, 'rho', event.rho)

        fillDiLepton( tr, event.diLepton )

        # import pdb; pdb.set_trace()
        pfmet = self.handles['pfmetraw'].product()[0]
        fill(tr, 'pfmet', pfmet.pt())

        fillParticle(tr, 'diTau', event.diLepton)
        fillTau(tr, 'l1', event.diLepton.leg1() )
        genTau = event.diLepton.leg1().genParticle()
        if genTau:
            fillGenParticle(tr, 'l1_gen', genTau)

            def getAllDaughters(p, l):
                for i in range(0, p.numberOfDaughters()):
                    d = p.daughter(i)
                    l.append(d)
                    getAllDaughters(d, l)

            p4vis = genTau.p4()
            daughters = []
            getAllDaughters(genTau, daughters)
            invisDaughters = [p for p in daughters if abs(p.pdgId()) in [12, 14, 16]]
            for d in invisDaughters:
                p4vis -= d.p4()

            fill(tr, 'l1_gen_vis_pt', p4vis.pt())
            fill(tr, 'l1_gen_vis_eta', p4vis.eta())
            fill(tr, 'l1_gen_vis_phi', p4vis.phi())

        fillMuon(tr, 'l2', event.diLepton.leg2() )
        if event.diLepton.leg2().genParticle():
            fillGenParticle(tr, 'l2_gen', event.diLepton.leg2().genParticle())
        fillParticle(tr, 'l1Jet', event.diLepton.leg1().jet )
        fillParticle(tr, 'l2Jet', event.diLepton.leg2().jet )

        fill(tr, 'nJets20', len(event.cleanJets) )
        nJets30 = len(event.cleanJets30)
        fill(tr, 'nJets', nJets30 )
        nJets = len(event.cleanJets)
        if nJets>=1:
             fillJet(tr, 'jet1', event.cleanJets[0] )
        if nJets>=2:
             fillJet(tr, 'jet2', event.cleanJets[1] )

        nBJets = len(event.cleanBJets)
        if nBJets>0:
             fillJet(tr, 'bjet1', event.cleanBJets[0] )
        fill(tr, 'nBJets', nBJets)

        # JAN - directly count CSVL jets as done in
        # other groups. May apply SFs as for CSVM jets
        # after rewriting BTagSF module in the future
        nCSVLJets = 0
        for jet in event.cleanJets:
            if jet.btag('combinedSecondaryVertexBJetTags') > 0.244:
                    nCSVLJets += 1
        fill(tr, 'nCSVLJets', nCSVLJets)

        if hasattr( event, 'vbf'):
             fillVBF( tr, 'VBF', event.vbf )
             #if len(event.vbf.centralJets) > 0:
             #        fillJet(tr, 'cjet', event.vbf.centralJets[0])

        fill(tr, 'weight', event.eventWeight)
        fill(tr, 'embedWeight', event.embedWeight)
        fill(tr, 'hqtWeight', event.higgsPtWeight)
        fill(tr, 'hqtWeightUp', event.higgsPtWeightUp)
        fill(tr, 'hqtWeightDown', event.higgsPtWeightDown)
        if hasattr( event, 'NJetWeight'):
            fill(tr, 'NJetWeight', event.NJetWeight)

        fill(tr, 'tauFakeRateWeightUp', event.tauFakeRateWeightUp)
        fill(tr, 'tauFakeRateWeightDown', event.tauFakeRateWeightDown)
        fill(tr, 'tauFakeRateWeight', event.tauFakeRateWeight)

        if hasattr( event, 'vertexWeight'):
            fill(tr, 'vertexWeight', event.vertexWeight)
            fill(tr, 'nVert', len(event.vertices) )

        fill(tr, 'isFake', event.isFake)
        fill(tr, 'isSignal', event.isSignal)
        fill(tr, 'leptonAccept',        event.leptonAccept)
        fill(tr, 'thirdLeptonVeto', event.thirdLeptonVeto)

        if hasattr(event, 'genParticles'):
            # Get Higgs/Z mass
            for p in event.genParticles:
                if p.pdgId() in [23, 25, 35, 36, 37]:
                    fill(tr, 'genMass', p.mass())
                    break

            # Calculate gen MET
            neutrinos = [p for p in event.genParticles if abs(p.pdgId()) in (12, 14, 16)]
            if neutrinos:
                genMet = neutrinos[0].p4()
                for p in neutrinos[1:]:
                    genMet += p.p4()
                fill(tr, 'genMet', p.pt())
                fill(tr, 'genMex', p.px())
                fill(tr, 'genMey', p.py())
            else:
                fill(tr, 'genMet', 0.)
                fill(tr, 'genMex', 0.)
                fill(tr, 'genMey', 0.)

            # Fill hard quarks/gluons
            quarksGluons = [p for p in event.genParticles if abs(p.pdgId()) in (1, 2, 3, 4, 5, 21) and p.status() == 3 and (p.numberOfDaughters() == 0 or p.daughter(0).status() != 3)]
            quarksGluons.sort(key=lambda x: -x.pt())
            for i in range(0, min(self.maxNGenJets, len(quarksGluons))):
                fillGenParticle(tr, 'genQG_{i}'.format(i=i), quarksGluons[i])

        self.tree.tree.Fill()


import os
import logging
from rango.base.core import RangoHandler

LOGGER = logging.getLogger()
logging.basicConfig()

try:
    version_filepath = os.path.join(__path__[0], "VERSION")
    with open(version_filepath) as version_file:
        __version__ = version_file.read().strip()
except IOError:
    LOGGER.warning("Could not find 'VERSION' file in package directory. The __version__ attribute will be empty!")

def getHandler():
    return RangoHandler()


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 19 17:35:09 2016
@author: yxl
"""
from imagepy.core.engine import Tool
import numpy as np
import wx
from numba import jit

@jit(nopython=True)
def floodfill(img, x, y):
    buf = np.zeros((131072,2), dtype=np.uint16)
    color = img[int(y), int(x)]
    img[int(y), int(x)] = 0
    buf[0,0] = x; buf[0,1] = y;
    cur = 0; s = 1;

    while True:
        xy = buf[cur]
        for dx in (-1,0,1):
            for dy in (-1,0,1):
                cx = xy[0]+dx; cy = xy[1]+dy
                if cx<0 or cx>=img.shape[1]:continue
                if cy<0 or cy>=img.shape[0]:continue
                if img[cy, cx]!=color:continue
                img[cy, cx] = 0
                buf[s,0] = cx; buf[s,1] = cy
                s+=1
                if s==len(buf):
                    buf[:len(buf)-cur] = buf[cur:]
                    s -= cur; cur=0
        cur += 1
        if cur==s:break

def cut(img, lines):
    if len(lines)<2:return
    lines = np.array(lines).round()
    ox,oy = lines[0]
    for i in lines[1:]:
        cx, cy = i
        dx, dy = cx-ox, cy-oy
        n = max(abs(dx), abs(dy)) + 1
        xs = np.linspace(cx, ox, n).round().astype(np.int16)
        ys = np.linspace(cy, oy, n).round().astype(np.int16)
        for x,y in zip(xs, ys):
            for dxy in [(1,0),(0,1)]:
                xx = x + dxy[0]
                yy = y + dxy[1]
                if xx<0 or xx>img.shape[1]: continue
                if yy<0 or yy>img.shape[0]: continue
                if img[yy,xx] == 0: continue
                floodfill(img, xx, yy)
        ox, oy = i

class Mark():
    def __init__(self, line):
        self.line = line

    def draw(self, dc, f, **key):
        dc.SetPen(wx.Pen((255,0,0), width=2, style=wx.SOLID))
        dc.DrawLines([f(*i) for i in self.line])

class Plugin(Tool):
    title = 'Graph Cut'
    def __init__(self):
        self.status = 0

    def mouse_down(self, ips, x, y, btn, **key):
        if btn==1:
            ips.snapshot()
            self.status = 1
            self.cur = [(x, y)]
            ips.mark = Mark(self.cur)
            ips.update()

    def mouse_up(self, ips, x, y, btn, **key):
        ips.mark = None
        self.status = 0
        cut(ips.img, self.cur)
        ips.update()

    def mouse_move(self, ips, x, y, btn, **key):
        if self.status==1:
            self.cur.append((x, y))
            ips.update()

    def mouse_wheel(self, ips, x, y, d, **key):
        pass


#!/usr/bin/python3
""" """

def number_of_lines(filename=""):
    """ """
    lines = 0
    with open(filename, encoding='utf-8') as filet:
        while True:
            line = filet.readline()
            if not line:
                break
            lines += 1
    return lines


from joern.all import JoernSteps
from igraph import *

def properties2label(node):
    labelStr = 'code:'+str(node.properties['code']) + '\n'
    labelStr += 'type:'+ str(node.properties['type'])
    return labelStr

#connect to database
j = JoernSteps()
j.setGraphDbURL('http://localhost:7474/db/data')
j.connectToDatabase()
#get function name
##func_name = raw_input('please input the target function name:')
func_name = 'GetAoutBuffer'
#query function id
query_str = 'getFunctionsByName("' + func_name + '").id'
res = j.runGremlinQuery(query_str)
func_id = res[2]
#get nodes
query_str = 'g.v('+ str(func_id) + ').functionToAST().astNodes()'
astNodes = j.runGremlinQuery(query_str)
#get edges
query_str = 'g.v(' + str(func_id) + ').functionToAST().astNodes().outE("IS_AST_PARENT")'
astEdges = j.runGremlinQuery(query_str)
#create igraph astTree
g = Graph()
#add root node
#add left node
for astNode in astNodes :
    g.add_vertex(astNode._id)
#init attribute--info
g.vs["info"] = ["None" for astNode in astNodes]
#alert attribute--info
for astNode in astNodes :
    nodeId = g.vs.find(name=astNode._id).index
    g.vs[nodeId]['info'] = properties2label(astNode)

srcDstEdgePairs = [(g.vs.find(name=e.start_node._id).index, g.vs.find(name=e.end_node._id).index) for e in astEdges]
g.add_edges(srcDstEdgePairs)

##draw picture
layout = g.layout("kk")
visual_style = {}
visual_style["vertex_size"] = 20
visual_style["vertex_label"] = g.vs["info"]
visual_style["layout"] = layout
visual_style["bbox"] = (4000, 4000)
visual_style["margin"] = 100
plot(g, **visual_style)


import os
import socket
from struct import *
import os

def store_file(connection, storage_dir):
    size_data = connection.recv(2)
    size = unpack('h', size_data)
    file_name = connection.recv(size[0])
    with open("{}/{}".format(storage_dir, file_name.decode()), 'wb') as f:
        data = connection.recv(1024)
        while data:
            f.write(data)
            data = connection.recv(1024)
    print('Successfully get the file')

def send_file(connection, storage_dir):
    size_data = connection.recv(2)
    size = unpack('h', size_data)
    file_name = connection.recv(size[0])
    if os.path.exists("{}/{}".format(storage_dir, file_name.decode())):
        # envio una e representando que existe el archivo
        connection.send(b'e')
        with open("{}/{}".format(storage_dir, file_name.decode()), 'rb') as f:
            data = f.read(1024)
            while data:
                connection.send(data)
                data = f.read(1024)
        print('Successfully sent the file')
    else:
        # envio una i representando un inexistente
        connection.send(b'i')
        print("The file doesn't exist")

def start_server(server_address, storage_dir):
    print(os.getcwd())
    print('TCP: start_server({}, {})'.format(server_address, storage_dir))
    close_connections = False
    if not os.path.exists(storage_dir):
        os.makedirs(storage_dir)
    (server_host, server_port) = server_address
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind((server_host, server_port))
        sock.listen()
        while not close_connections:
            print('waiting for a connection')
            connection, client_address = sock.accept()
            try:
                print('client connected:', client_address)
                client_action = connection.recv(1)
                if client_action == b'u':
                    store_file(connection, storage_dir)
                elif client_action == b'd':
                    send_file(connection, storage_dir)
            except Exception as e:
                print("Internal Error data")
            finally:
                print('connection closed')
                connection.close()


import osa
import math

"""
Задача №1

Дано: семь значений температур по Фаренгейту в файле temps.txt.
Необходимо вывести среднюю за неделю арифметическую температуру по Цельсию.

"""

def temps_file_to_list(file_path):
    with open(file_path) as f:
        content = f.read().split(' F')
    content = content[:-1]
    content = [int(x.strip()) for x in content]
    return(content)

def farenheit_to_celsius(fr_temperature):
    URL1 = 'http://www.webservicex.net/ConvertTemperature.asmx?WSDL'
    client1 = osa.client.Client(URL1)
    response1 = client1.service.ConvertTemp(Temperature=fr_temperature, FromUnit='degreeFahrenheit', ToUnit='degreeCelsius')
    return(response1)

def list_avg(list):
    avg_list = sum(fr_temps_list) / float(len(fr_temps_list))
    return(avg_list)

file_path_temps = 'Homework\\temps.txt'

fr_temps_list = temps_file_to_list(file_path_temps)
avg_week_fr_temperature = list_avg(fr_temps_list)
avg_week_cs_temperature = round(farenheit_to_celsius(avg_week_fr_temperature), 2)
print("Task 1:", avg_week_cs_temperature)

"""
Задача №2

Вы собираетесь отправиться в путешествие и начинаете разрабатывать маршрут
и выписывать цены на перелеты. Даны цены на билеты в местных валютах
(файл currencies.txt). Формат данных в файле:

<откуда куда>: <стоимость билета> <код валюты>
Пример:
MOSCOW-LONDON: 120 EUR
Посчитайте, сколько вы потратите на путешествие денег в рублях (без копеек, округлить в большую сторону).

"""

def cur_file_to_list_of_lists(cur_file_path):
    with open(cur_file_path) as f:
        content = f.readlines()
    content = [x.strip() for x in content]
    content = [x.split(' ') for x in content]
    return(content)

def convert_to_rub(from_cur, amount_in_cur):
    URL4 = 'http://fx.currencysystem.com/webservices/CurrencyServer4.asmx?WSDL'
    client4 = osa.client.Client(URL4)
    response4 = client4.service.ConvertToNum(toCurrency='RUB', fromCurrency=from_cur,
                                             amount=amount_in_cur, rounding=True)
    return(response4)

def prices_rub(data_cur):
    result_rub = []
    for route in data_cur:
        result_rub.append(convert_to_rub(route[2], route[1]))
    return(result_rub)

cur_file_path = 'Homework\\currencies.txt'
data_cur = cur_file_to_list_of_lists(cur_file_path)
result_sum = sum(prices_rub(data_cur))
result_sum_round_up = math.ceil(result_sum)
print("Task 2:", result_sum_round_up)

"""
Задача №3

Дано: длина пути в милях, название пути (файл travel.txt) в формате:

<название пути>: <длина в пути> <мера расстояния>
Пример:
MOSCOW-LONDON: 1,553.86 mi
Необходимо посчитать суммарное расстояние пути в километрах с точностью до сотых.

"""

def travel_file_to_list(travel_file_path):
    with open(travel_file_path) as f:
        content = f.readlines()
    content = [x.replace(',', '') for x in content]
    content = [x.strip() for x in content]
    content = [x.split(' ') for x in content]
    content = [float(x[1]) for x in content]
    return(content)

def miles_to_km(miles_value):
    URL5 = 'http://www.webservicex.net/length.asmx?WSDL'
    client5 = osa.client.Client(URL5)
    response5 = client5.service.ChangeLengthUnit(fromLengthUnit='Miles', toLengthUnit='Kilometers',
                                                 LengthValue=miles_value)
    return(response5)

travel_file_path = 'Homework\\travel.txt'

list_distance_in_miles = travel_file_to_list(travel_file_path)
sum_distance_in_miles = round(sum(list_distance_in_miles), 2)
print("Task 3:", sum_distance_in_miles)