import os, sys, time, item, monster
from math import sqrt, log

def clear():
    os.system('cls' if os.name == 'nt' else 'clear')
class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
get = _GetchUnix()

#the symbol represetations of characters and items in game
hero = '@'
boss = '%'
merchant = '$'
door = '!'
chest = '&'

#readmap method helps to read the level.txt file and show them and transfer strings to self.levelmap (a list of strings)
#readmap change strings in each line to a list and return a list of lines lists
def readmap(filename, levelnum):
    mapFile = open(filename, 'r')
    content = mapFile.readlines() + ['\r\n']
    mapFile.close()
    start = 0
    end = 0
    l = levelnum % 6 + 1

    for i in range(len(content)):
        line = content[i].rstrip('\r\n')
        if ';'+str(l) in line:
            start = i
        if start and line == '':
            end = i
            break
    levelMap = content[start+1:end]
    return levelMap

class Player:
    def __init__(self):
        self.levelnum = 1
        self.level = readmap('levels.txt', self.levelnum)
        self.maxhealth = 50
        self.health = 50
        self.maxmagic = 30
        self.magic = 10
        self.weapon = None
        self.armor = None
        self.attack = 5
        self.maxattack = 20
        self.nattack = 5
        self.defense = 5
        self.maxdefense = 20
        self.ndefense = 5
        self.inventory = []
        self.invsize = 0
        self.invmax = 20
        self.money = 30
        self.exp = 0
        self.expmax = 50
        self.getLocation()
        self.recipe = None

    #change map to next level
    def moveToNext(self):
        self.levelnum += 1
        self.level = readmap('levels.txt', self.levelnum)

    #adds experience to the player
    def addexp(self,n):
        if self.exp + n >= self.expmax:
            self.exp = self.expmax - self.exp
            self.expmax = int(self.expmax * 1.5 + 0.5)
            self.levelnum += 1
            self.attack = int(self.attack * 1.2)
            self.na()
            self.magic = int(self.magic * 1.2)
            self.defense = int(self.defense * 1.2)
            self.nd()
            self.health = int(self.health * 1.1)
            print("You have leveled up!")
            time.sleep(1)
        else:
            self.exp += n

    #calculate the attack of player with weapon
    def na(self):
        if self.weapon == None:
            self.nattack = self.attack
        else:
            self.nattack = int(self.attack * (1 + sqrt(self.weapon.atkboost)))
        if self.nattack > self.maxattack:
            self.nattack = self.maxattack

    #calculate the defense of player with armor
    def nd(self):
        if self.armor == None:
            self.ndefense = self.defense
        else:
            self.ndefense = int(self.defense * (1 + sqrt(self.armor.defboost)))
        if self.ndefense > self.maxdefense:
            self.ndefense = self.maxdefense

    #the order number of the character symber in the list returned by readmap is self.y
    #and the number of the string in that list is self.x
    #we treats the position of every character as coordinates.
    def move(self, direction):
        self.getLocation()
        right = True
        if direction == 'w':
            y1 = self.y -1
            x1 = self.x
        elif direction == 's':
            y1 = self.y +1
            x1 = self.x
        elif direction == 'a':
            x1 = self.x -1
            y1 = self.y
        elif direction == 'd':
            x1 = self.x +1
            y1 = self.y
        if y1 in range(len(self.level)):
            if x1 in range(len(self.level[y1])):
                if self.level[y1][x1]==' ':
                    self.level[y1] = self.level[y1][:x1]+hero+self.level[y1][x1+1:]
                    self.level[self.y] = self.level[self.y][:self.x]+' '+self.level[self.y][self.x+1:]
        self.getLocation()

    def notdead(self):
        return self.health>0

    #refresh the location of character
    def getLocation(self):
        for i in range(len(self.level)):
            line = self.level[i].rstrip('\r\n')
            if hero in line:
                self.x = line.index(hero)
                self.y = i
                break

    #using coordinate to check if the player is around any other things like chest or boss.
    def around(self, c):
        bx = None
        for i in range(len(self.level)):
            line = self.level[i].rstrip('\r\n')
            if c in line:
                bx = line.index(c)
                by = i
        if bx == None:
            return False
        if self.x + 1 == bx or self.x - 1 == bx or self.x == bx:
            if self.y + 1 == by or self.y - 1 == by or self.y == by:
                return True

    #after the chest is used or the boss beaten, the symbol in the map needs to disappear
    def disappear(self, c):
        for i in range(len(self.level)):
            line = self.level[i].rstrip('\r\n')
            if c in line:
                bx = line.index(c)
                by = i
        if bx == None:
            return False
        self.level[by] = self.level[by][:bx]+' '+self.level[by][bx+1:]

    #get the status of the player
    def status(self):
        print('Health: '+str(self.health)+'/'+str(self.maxhealth))
        print('Attack: '+str(self.nattack)+'/'+str(self.maxattack))
        print('Magic:  '+str(self.magic)+'/'+str(self.maxmagic))
        print('Armor:  '+str(self.ndefense)+'/'+str(self.maxdefense))
        print('INV:    '+str(self.invsize)+'/'+ str(self.invmax))
        print('Money:  '+str(self.money))
        print('LV:     '+str(self.levelnum)+'  '+str(self.exp)+'/'+str(self.expmax))
        print()

    #Helps the player to inspect its inventory
    #apnum number is the appointed item the player currently choose in its inventory
    def inspect(self):
        still = True
        apnum = 0
        while still:
            namelist = []
            for item in self.inventory:
                if item not in namelist:
                    namelist.append(item)
            limit = len(namelist)
            if apnum >= limit:
                apnum = 0
            elif apnum < 0:
                apnum = limit-1

            if limit == 0:
                print('You have nothing in inventory.')
                print()
                print("Press c to continue")
                command = get()
                if command == 'c':
                    still = False
                else:
                    print('Not a valid command, try again')
            else:
                clear()
                inv = {}
                for item in self.inventory:
                    if item in inv:
                        inv[item] += 1
                    else:
                        inv[item] = 1
                self.status()
                print()
                print("Now you have: ")
                for item in namelist:
                    if item == namelist[apnum]:
                        print('>   ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                    else:
                        print('    ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                print()
                print()
                print('    ' + namelist[apnum].desc)
                namelist[apnum].describe()
                print()
                print()
                print('Press d to drop, u to use, c to craft, or x to leave')
                print()
                command = get()
                if command == 'w':
                    apnum -=1
                elif command == 's':
                    apnum +=1
                elif command == 'c':
                    self.craft(self.recipe)
                elif command == 'd':
                    print('Press y to Drop this Item or n to Cancel')
                    command = get()
                    if command == 'y':
                        self.drop(namelist[apnum])
                    elif command == 'n':
                        still == True
                elif command == 'u':
                    print('Press y to Use this Item or n to Cancel')
                    command = get()
                    if command == 'y':
                        namelist[apnum].use(self)
                    elif command == 'n':
                        still == True
                elif  command == 'x':
                    print('Press Y to Leave or N to Stay')
                    command = get()
                    if command == 'y':
                        still = False
                    elif command == 'n':
                        still == True
                else:
                    print('Not a valid Command, try again')

    #Similar to inspect, but shows the items monster drops and allows the player to pick up these items
    def monsterdrop(self, droplist):
        still = True
        apnum = 0
        while still:
            namelist = []
            for item in droplist:
                if item not in namelist:
                    namelist.append(item)
            limit = len(namelist)
            if apnum >= limit:
                apnum = 0
            elif apnum < 0:
                apnum = limit-1

            if limit == 0:
                clear()
                self.status()
                print()
                print('There is Nothing Else.')
                time.sleep(1)
                still = False
            else:
                clear()
                inv = {}
                for item in droplist:
                    if item in inv:
                        inv[item] += 1
                    else:
                        inv[item] = 1
                clear()
                self.status()
                print()
                print("Monster Drops: ")
                for item in namelist:
                    if item == namelist[apnum]:
                        print('>   ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                    else:
                        print('    ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                print()
                print()
                print()
                print('    ' + namelist[apnum].desc)
                namelist[apnum].describe()
                print()
                print()
                print('Press P to Pick, I to Check Inventory, or X to leave')
                print()
                command = get()
                if command == 'w':
                    apnum -=1
                elif command =='i':
                    self.inspect()
                elif command == 's':
                    apnum +=1
                elif command == 'p':
                    print('Press Y to Take this Item or N to Cancel')
                    command = get()
                    if command == 'y':
                        self.additem(namelist[apnum])
                        droplist.remove(namelist[apnum])
                    elif command == 'n':
                        still == True
                elif  command == 'x':
                    print('Press Y to Leave or N to Stay')
                    command = get()
                    if command == 'y':
                        still = False
                    elif command == 'n':
                        still == True

    #Similar to inspect. allows the player to pick up the items in chest
    def chestopen(self, chest):
        still = True
        apnum = 0
        while still:
            namelist = []
            for item in chest.items:
                if item not in namelist:
                    namelist.append(item)
            limit = len(namelist)
            if apnum >= limit:
                apnum = 0
            elif apnum < 0:
                apnum = limit-1

            if limit == 0:
                clear()
                self.status()
                print()
                print('There is Nothing Else.')
                time.sleep(2)
                still = False
            else:
                clear()
                inv = {}
                for item in chest.items:
                    if item in inv:
                        inv[item] += 1
                    else:
                        inv[item] = 1
                clear()
                self.status()
                print()
                print("Chest Has: ")
                for item in namelist:
                    if item == namelist[apnum]:
                        print('>   ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                    else:
                        print('    ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                print()
                print()
                print()
                print('    ' + namelist[apnum].desc)
                namelist[apnum].describe()
                print()
                print()
                print('Press P to Pick, I to inspect inventory, or X to Leave.')
                print()
                command = get()
                if command == 'w':
                    apnum -=1
                elif command =='i':
                    self.inspect()
                elif command == 's':
                    apnum +=1
                elif command == 'p':
                    print('Press Y to Take This Item or N to Cancel')
                    command = get()
                    if command == 'y':
                        self.additem(namelist[apnum])
                        chest.items.remove(namelist[apnum])
                    elif command == 'n':
                        still == True
                elif  command == 'x':
                    print('Press Y to Leave or N to Stay')
                    command = get()
                    if command == 'y':
                        still = False
                        self.disappear('&')
                    elif command == 'n':
                        still == True
                if not len(chest.items):
                    self.disappear('&')

    #Similar to inspect, but it's only used when using potions during combat
    def usepotion(self):
        still = True
        apnum = 0
        while still:
            namelist = []
            for item in self.inventory:
                if item.type == "healPotion" or item.type == "magicPotion":
                    if item not in namelist:
                        namelist.append(item)
            limit = len(namelist)
            if apnum >= limit:
                apnum = 0
            elif apnum < 0:
                apnum = limit-1

            if limit == 0:
                print('You have nothing in inventory.')
                still = False
            else:
                clear()
                inv = {}
                for item in self.inventory:
                    if item in inv:
                        inv[item] += 1
                    else:
                        inv[item] = 1
                clear()
                self.status()
                print()
                print("'You can use the following items in your bag:'")
                for item in namelist:
                    if item == namelist[apnum]:
                        print('>   ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                    else:
                        print('    ' + str(inv[item]) + "x " + str(item.name)+ ' s'+str(item.size))
                print()
                print()
                print('    ' + namelist[apnum].desc)
                namelist[apnum].describe()
                print()
                print()
                print('Press U to Use, X to Leave')
                print()
                command = get()
                if command == 'w':
                    apnum -=1
                elif command == 's':
                    apnum +=1
                elif command == 'u':
                    print('Press Y to Use this Item or N to Cancel')
                    command = get()
                    if command == 'y':
                        namelist[apnum].use(self)
                    elif command == 'n':
                        still == True
                elif  command == 'x':
                    print('Press Y to Leave or N to Stay')
                    command = get()
                    if command == 'y':
                        still = False
                    elif command == 'n':
                        still == True

    def additem(self, item):
        if self.invsize + item.size <= self.invmax:
            self.inventory.append(item)
            self.invsize +=item.size
        else:
            print('You don not have enough space in your inventory')

    def drop(self, item):
        self.inventory.remove(item)
        self.invsize -= item.size

    def buy(self, allitem):
        still = True
        apnum = 0
        warn = 0
        while still:
            limit = len(allitem)
            if apnum >= limit:
                apnum = 0
            elif apnum < 0:
                apnum = limit-1
            print('You have: ' + str(self.money))
            print()
            print("You Can Buy: ")
            for item in allitem:
                if len(str(item.price)) == 1:
                    sp = '   '
                else:
                    sp = '  '
                if item == allitem[apnum]:
                    print('>   $' + str(item.price) + sp + str(item.name))
                else:
                    print('    $' + str(item.price) + sp + str(item.name))
            print('    ' + allitem[apnum].desc)
            allitem[apnum].describe()
            print()
            print()
            print('Press B to buy or X to leave')
            if warn == 1:
                print('You Don\'t Have enough Money')
            elif warn ==2:
                print('Your Inventory Doesn\'t have enough Space')
            warn = 0
            command = get()
            if command == 'w':
                apnum -=1
            elif command == 's':
                apnum +=1
            elif command == 'b':
                print('Press Y to Buy this Item or N to Cancel')
                command = get()
                if command == 'y':
                    if self.money >= allitem[apnum].price:
                        if self.invsize + allitem[apnum].size <= self.invmax:
                            self.inventory.append(allitem[apnum])
                            self.invsize += allitem[apnum].size
                            self.money -= allitem[apnum].price
                        else:
                            warn = 2
                    else:
                        warn = 1
                elif command == 'n':
                    still == True
            elif  command == 'x':
                print('Press Y to Leave or N to Stay')
                command = get()
                if command == 'y':
                    still = False
                elif command == 'n':
                    still == True

    def sell(self):
        still = True
        apnum = 0
        while still:
            namelist = []
            for item in self.inventory:
                if item not in namelist:
                    namelist.append(item)
            limit = len(namelist)
            if apnum >= limit:
                apnum = 0
            elif apnum < 0:
                apnum = limit-1

            if limit == 0:
                print('You have nothing to sell.')
                still = False
            else:
                clear()
                inv = {}
                for item in self.inventory:
                    if item in inv:
                        inv[item] += 1
                    else:
                        inv[item] = 1
                clear()
                self.status()
                print()
                print("Now you have: ")
                for item in namelist:
                    if item == namelist[apnum]:
                        print('>   ' + str(inv[item]) + "x " + str(item.name))
                    else:
                        print('    ' + str(inv[item]) + "x " + str(item.name))
                print()
                print('    ' + namelist[apnum].desc)
                namelist[apnum].describe()
                command = get()
                if command == 'w':
                    apnum -=1
                elif command == 's':
                    apnum +=1
                elif command == 't':
                    print('Press y to sell this Item or n to Cancel. Press x to leave')
                    command = get()
                    if command == 'y':
                        self.removeitem(namelist[apnum])
                        self.money += int(namelist[apnum].price * 0.5)
                    elif command == 'n':
                        still == True
                elif  command == 'x':
                    print('Press y to Leave or n to Stay')
                    command = get()
                    if command == 'y':
                        still = False
                    elif command == 'n':
                        still == True

    #according the number of player moving to regenerate health
    def regeneration(self):
        if self.health<self.maxhealth:
                if self.health+1 >= self.maxhealth:
                    self.health = self.maxhealth
                else:
                    self.health += 1

    #It's for the player to craft useful items from materials
    def craft(self, recipe):
        #help to check possible items to make
        def select(inv, recipe):
            m = {}
            for item in inv:
                if item.type == 'material':
                    if item in m:
                        m[item] += 1
                    else:
                        m[item] = 1
            possible = []
            for product in recipe:
                make = True
                for ingredient in recipe[product]:
                    if not ingredient in m:
                        make = False
                    else:
                        if recipe[product][ingredient] > m[ingredient]:
                            make = False
                if make:
                    possible.append(product)
            return possible

        def shift(possible, ls):
            i=0
            while i < len(possible):
                for item in ls:
                    if possible[i] == item.name:
                        possible[i] = item
                i+=1
            return possible

        still = True
        apnum = 0
        while still:
            possible = select(self.inventory,recipe)
            possible = shift(possible, recipe['lists'])
            limit = len(possible)
            if apnum >= limit:
                apnum = 0
            elif apnum < 0:
                apnum = limit - 1
            if limit == 0:
                print('You can make nothing from what you have.')
                time.sleep(2)
                still = False
            else:
                clear()
                print()
                print("You can make: ")
                for item in possible:
                    if item == possible[apnum]:
                        print('>   ' + item.name)

                    else:
                        print('    ' + item.name)
                print()
                print()
                print('    ' + possible[apnum].desc)
                possible[apnum].describe()
                print()
                print()
                print('Press c to craft or x to leave')
                print()
                command = get()
                if command == 'w':
                    apnum -=1
                elif command == 's':
                    apnum +=1
                elif command == 'c':
                    print('Press y to Craft this Item or n to Cancel')
                    command = get()
                    if command == 'y':
                        ingredients = recipe[possible[apnum].name]
                        for item in ingredients:
                            while ingredients[item] > 0:
                                self.drop(item)
                                ingredients[item] -= 1
                        self.additem(possible[apnum])
                        possible.remove(possible[apnum])
                        print("Craft success!")
                        time.sleep(0.8)
                    else:
                        still = True
                elif command == 'x':
                    print('Press y to Leave or n to Stay')
                    command = get()
                    if command == 'y':
                        still = False
                    elif command == 'n':
                        still == True

    #it's a talking system when you are around the merchant
    #it feels like real life
    def talkmerchant(self, allitem):
        boo = ["1. I want to buy something", "2. I want to sell things to get money", "3. Let's talk.", "4. I want to leave."]
        apnum = 0
        still = True

        def talk(self):
            apnum = 0
            still = True
            choices = ["1. Why I keep seeing you here?", "2. How can you move between levels?", "3. Who am I?", "4. Why I don't need to eat here?", "5. I want to leave"]
            while still:
                clear()
                print("'Sure, what do you want to talk about? I'm only a merchant who happens to be here. I like to talk to people.' The merchant smiled at you.")
                print()
                print()
                print("What do you want to say?")
                print("Press c to choose")
                print()
                if apnum == len(choices):
                    apnum = 0
                elif apnum < 0:
                    apnum = 1
                for item in choices:
                    if item == choices[apnum]:
                        print('>   ' + item)
                        x = item
                        number = int(item[0:1])
                    else:
                        print('    ' + item)
                print()
                print()
                command = get()
                if command == 'w':
                    apnum -=1
                elif command == 's':
                    apnum +=1
                elif command == 'c':
                    if number == 1:
                        print("'I'm part of the tower. I exist when the tower exists.'")
                        time.sleep(1.5)
                        choices.remove(x)
                    elif number == 2:
                        print("'The merchant has everything, you know.'")
                        time.sleep(1.5)
                        choices.remove(x)
                    elif number == 3:
                        print("'24601.' The merchant pointed to your striped shirt. Interesting, you never realized this.")
                        time.sleep(1.5)
                        choices.remove(x)
                    elif number == 4:
                        print("'This is simple magic.'")
                        time.sleep(1.5)
                        choices.remove(x)
                    else:
                        print("'Okay, see you around.' The merchant said this to you, waved and returned to the sleep.")
                        print()
                        time.sleep(1)
                        still = False

        while still:
            clear()
            print("The merchant was lying on the ground, yawning. Seeing you approaching, the merchant smiled.")
            print("'Ah it's you again! Still not giving up? I have some items for you. Do you want to BUY any? Or you prefer to SELL some of your things to me?'")
            print()
            print()
            print("What do you want to say?")
            print("Press c to choose")
            print()
            if apnum == len(boo):
                apnum = 0
            elif apnum < 0:
                apnum = 1
            for item in boo:
                if item == boo[apnum]:
                    print('>   ' + item)
                    number = int(item[0:1])
                else:
                    print('    ' + item)
            print()
            print()
            command = get()
            if command == 'w':
                apnum -=1
            elif command == 's':
                apnum +=1
            elif command == 'c':
                if number == 1:
                    clear()
                    self.buy(allitem)
                    still = False
                elif number == 2:
                    clear()
                    self.sell()
                    still = False
                elif number == 3:
                    talk(self)
                    still = False
                else:
                    print("'Okay, see you around.' The merchant said this to you, waved and returned to the sleep.")
                    time.sleep(1)
                    print()
                    still = False


from django.db import models

class Click(models.Model):
    campaign = models.IntegerField(db_index=True)
    timestamp = models.DateTimeField()

    class Meta:
        indexes = [
            models.Index(fields=["campaign", "timestamp"])
        ]

    @classmethod
    def count_campaign_clicks(cls,
                              campaign: int,
                              after_date: str = None,
                              before_date: str = None) -> int:
        """
        SQL:
            SELECT count(*)
            FROM click_logs_click
            WHERE campaign = '4510461'
              AND timestamp BETWEEN '2021-11-07 03:10:00'
              AND '2021-11-07 03:30:00';
        """
        qs = cls.objects.filter(campaign=campaign)
        if after_date:
            qs = qs.filter(timestamp__gt=after_date)
        if before_date:
            qs = qs.filter(timestamp__lt=before_date)
        return qs.count()

    def __repr__(self):
        return f'Click(campaign={self.campaign}, timestamp={self.timestamp})'

    def __str__(self):
        return repr(self)


#你想得到一个由迭代器生成的切片对象，但是标准切片操作并不能做到。
def increase(n):
    i = n
    while True:
        yield i
        i += 1

c = increase(0)
from itertools import islice
for i in islice(c,20,30):
    print(i)
#islice() 会消耗掉传入的迭代器中的数据。 必须考虑到迭代器是不可逆的这个事实。


import numpy as np

def ingresar_matriz():
    print("Ingrese la matriz de transición:")
    n = int(input("Ingrese el número de filas: "))
    m = int(input("Ingrese el número de columnas: "))

    matriz = []
    for i in range(n):
        fila = []
        for j in range(m):
            elemento = float(input(f"Ingrese el elemento [{i}][{j}]: "))
            fila.append(elemento)
        matriz.append(fila)

    return matriz

def multiplicar_matrices(matriz1, matriz2):
    filas1 = len(matriz1)
    columnas1 = len(matriz1[0])
    filas2 = len(matriz2)
    columnas2 = len(matriz2[0])
    resultado = []

    for i in range(filas1):
        fila = []
        for j in range(columnas2):
            suma = 0
            for k in range(columnas1):
                producto = matriz1[i][k] * matriz2[k][j]
                suma += producto
            fila.append(suma)
        resultado.append(fila)

    return resultado

def calcular_distribucion_estacionaria(matriz, estado_inicial, num_iteraciones):
    matriz_np = np.array(matriz)
    n = matriz_np.shape[0]

    distribucion_actual = np.array(estado_inicial, ndmin=2).T

    print("\nCalculando la distribución estacionaria:")

    for i in range(num_iteraciones):
        print(f"\nIteración {i + 1}:")
        print(f"Distribución actual:\n{distribucion_actual}")

        distribucion_nueva = multiplicar_matrices(matriz, distribucion_actual)
        distribucion_actual = distribucion_nueva

    distribucion_estacionaria = distribucion_actual

    return distribucion_estacionaria


import os
import pandas as pd
import pprint

def read_dict(in_path: str(), in_file: str(), in_dict_num: int()):
    # output_dict
    out_dict = dict()
    # input_dict_path
    input = os.path.join(in_path, in_file)
    df = pd.read_csv(input, index_col=0)
    # csv -> dict
    for i in range(0, len(df)):
        key = df.index[i]
        val = df.at[key, df.columns[0]]
        if val < 4:
            continue
        if key not in out_dict.keys():
            out_dict[key] = val
        else:
            continue
    return out_dict

def exist_dict(d1: dict(), d2: dict()):
    # dict1, dict2 are compered -> exist_num of dict is higher than other dict, exist
    # len_value d1 > d2

    # popをするためにリストに変更
    d2_keys = list(d2.keys())
    d2_values = list(d2.values())

    for k, v in d1.items():
        for index, key in enumerate(d2_keys):
            # 更新された同じ長さの単語を見ないようにする．
            if len(key) >= len(k):
                continue
            # 短い単語が長い単語に含まれるとき
            if key in k:
                # 出現頻度が長い単語のほうが多い，同じ
                if v >= d2_values[index]:
                    # print('out: {}, {} -> in: {}, {}'.format(d2_keys[index],d2_values[index], k, v))
                    d2_keys.pop(index)
                    d2_values.pop(index)
                    d2_keys.append(k)
                    d2_values.append(v)

    out_d = dict(zip(d2_keys, d2_values))

    return out_d

def output_csv(character_dict: dict(), publish: str()):
    characters = int()
    for count in character_dict.values():
        characters = characters + count
    # print(characters)

    # 出力ファイルの作成
    df = (pd.io.json.json_normalize(character_dict)).T
    df = df.set_axis(['count('+str(characters)+')'], axis='columns')

    # 出力path
    output_path = os.path.join(os.getcwd(), 'out', 'character_dict', publish)
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    df.to_csv(os.path.join(output_path, 'dictionary_words_' + publish + '.csv'))

def main():
    input_words_len = 2
    folder = 'character_dict'
    publish = '(養生教訓)医者談義 5巻'
    in_path = os.path.join(os.getcwd(), 'out', folder, publish)

    op_dict = dict()
    dm_dict = dict()

    for ndict in range(input_words_len, len(os.listdir(in_path))):
        # input path, file, ndict
        in_file = str(ndict) + 'words_' + publish + '.csv'
        # read
        dm_dict = read_dict(in_path, in_file, ndict)
        # op_dict initialize
        if ndict == input_words_len:
            op_dict = dm_dict
            continue

        # prevent zize0
        if len(dm_dict) == 0:
            continue

        op_dict = exist_dict(dm_dict, op_dict)

    print('output2csv: {}'.format(os.path.join(in_path)))
    output_csv(op_dict, publish)

    return 0

main()
# test()


import numpy as np
import cv2 as cv
from time import time

def blur_face_avg(img):
    """ Return a (average) blurred version of the input image. """
    return cv.blur(img, (91, 91))

def show_face(img, window_name="Face"):
    """ Show the image. """
    if img is not None:
        cv.imshow(window_name, img)

def load_face_classifier():
    """ Load and return the haar cascade face classifier. """
    face_cascade = cv.CascadeClassifier('classifier/haarcascade_frontalface_default.xml')
    return face_cascade

def detect_face(face_cascade, gray):
    """ Find faces in the image. If faces are found, the detectMultiScale function returns the positions of
     detection faces as Rect(x, y, w, h). """
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    return faces

def show_faces(faces, img):
    """ Show the detected faces with a rectangle on the original image. """
    roi, blur = None, None
    for (x, y, w, h) in faces:
        # Calculate center point and radius for circular masks.
        cx = w // 2
        cy = h // 2
        r = h // 2   # int(((w ** 2 + h ** 2) ** 0.5)/2)  # c² = a² + b², for a bigger circle, the roi itself needs to
        # be bigger which results in inconsistencies.

        # Draw a rectangle/circle on the image.
        # img = cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        img = cv.circle(img, (x+cx, y+cy), r, (101, 201, 255), 2)

        # Create a ROI.
        roi = roi_image(x, y, w, h, img)

        # Blur the ROI.
        blur = blur_face_avg(roi)

        # Create masks (normal and inverted).
        mask = np.full((roi.shape[0], roi.shape[1], 1), 0, dtype=np.uint8)  # Init black mask.
        cv.circle(mask, (cx, cy), r, (255, 255, 255), -1)  # Fill in a white circle in the middle to create the mask.
        mask_inverted = 255 - mask

        # Apply the masks.
        fg = cv.bitwise_and(blur, blur, mask=mask)         # Foreground: blurred circle face.
        bg = cv.bitwise_and(roi, roi, mask=mask_inverted)  # Background: part outside the detected face.

        # Add the foreground & background together and insert it into the frame.
        img[y:y+h, x:x+w] = cv.add(fg, bg)

    show_face(img)

def roi_image(x, y, w, h, img):
    """ Return a ROI image based on the input rectangle (x, y, w, h) and the image. """
    return img[y:y+h, x:x+w]

def detect_blur_video():
    # State variable: to blur or not to blur.
    to_blur = True

    # Open a video capture.
    capture = cv.VideoCapture(0, cv.CAP_DSHOW)

    # Load the face classifier.
    face_classifier = load_face_classifier()

    # Get start time.
    time_start = time()

    # Init number of frames variable.
    n_frames = 0
    while True:
        key = 0xFF & cv.waitKey(1)

        # Exit the loop if 'q' is pressed.
        if key == ord('q'):
            break

        # Toggle blurring if 'b' is pressed.
        if key == ord('b'):
            to_blur = not to_blur

        # Get a frame from the video capture.
        ret, frame = capture.read()

        # Continue if can't get a frame from the video capture.
        if not ret:
            continue

        # Convert RGB frame to grayscale.
        gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

        if to_blur:
            # Detect faces.
            face_detections = detect_face(face_classifier, gray)

            # Show detected faces.
            show_faces(face_detections, frame)
        else:
            show_face(frame)

        n_frames += 1

    # Get end time.
    time_end = time()

    # Calculate elapsed time (seconds).
    time_elapsed = time_end - time_start
    # Calculate and print frames per second.
    fps = n_frames / time_elapsed
    print(f"Fps = {fps:.2f}; Time elapsed = {time_elapsed:.2f} seconds.")

    # When everything done, release the capture.
    capture.release()
    cv.destroyAllWindows()


# -*- coding: utf-8 -*-
from gensim import models
def main():
	model = models.Word2Vec.load('word2vec.model')

	try:

		for word in ['月','蟾','嫦娥','兔','中秋','赏月','桂花','花灯','饼','桂花']:
			q_list = word.split()
			if len(q_list) == 1:
				topn = 5
				print("相似詞前 %d 排序"%topn)
				res = model.wv.most_similar(q_list[0],topn = topn)

				for item in res:
					print(word,'-->',item[0]+","+str(item[1]))

	except Exception as e:
		print(repr(e))

if __name__ == "__main__":
	main()


# coding=utf-8

import logging

from abc import ABC, abstractmethod
from app.repositories import redis_client

__author__ = 'LongHB'
_logger = logging.getLogger(__name__)

KEY_SPLITTER = ":"
NONE_VALUE_TO_CACHE = b''

class RedisCacheBase(ABC):
    """
    ABC means this class is abstract class
    """

    def __init__(self, name: str, ttl: int = None):
        """
        :param name: The name of the data is cached
        :param ttl: time-to-live in second of caching data. Default to None -> No expired time
        """

        self.name = name
        self.ttl = ttl
        self.redis_client = redis_client

    def _get_full_key(self, key: str) -> str:
        return self.name + KEY_SPLITTER + key

    def get(self, key: str):
        if key is None:
            return None
        full_key = self._get_full_key(key)
        value = self.redis_client.get(full_key)
        if value == NONE_VALUE_TO_CACHE: return None
        value = value.decode("utf-8") if value else value
        if not value:
            value = self.fetch_data(key)
            redis_client.set(full_key, NONE_VALUE_TO_CACHE, ex=self.ttl) if not value else \
                redis_client.set(full_key, value, ex=self.ttl)
        return value

    @abstractmethod
    def fetch_data(self, key: str):
        raise NotImplemented


# Pytanie 11 - na podstawie listy jezyki stworz listę jezyki_odwrocone
# zawierającą elementy listy jezyki w odwroconej kolejnosci

langs = ['python', 'java', 'c#', 'ruby']
print(langs)

# 1
langs_reversed = langs[::-1]
print(langs_reversed)

# 2
langs_reversed2 = list(reversed(langs))
print(langs_reversed2)

# 3
# langs.reverse()
# print(langs)

# 4
langs_reversed3 = []
for lang in langs:
    langs_reversed3.insert(0, lang)
print(langs_reversed3)


from collections import namedtuple

DSampleProps = namedtuple("DSampleProps",
        ["title", "line", "marker", "fill"])

DLine = namedtuple("DLine", ["color", "style", "width"])
DMarker = namedtuple("DMarker", ["color", "style", "size"])
DFill = namedtuple("DFill", ["color", "style"])

def setline(h, l):
    h.SetLineColor(l.color)
    h.SetLineStyle(l.style)
    h.SetLineWidth(l.width)

    return h

def setmarker(h, m):
    h.SetMarkerColor(m.color)
    h.SetMarkerStyle(m.style)
    h.SetMarkerSize(m.size)

    return h

def setfill(h, f):
    h.SetFillColor(f.color)
    h.SetFillStyle(f.style)

    return h


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

import os
import ctypes
import cv2
from glob import iglob

this_dir = os.path.dirname(__file__)

DLL_PATH = os.path.join(this_dir,'lib', 'libtesseract.so' )
TESSDATA_PREFIX = os.path.join(this_dir,'tessdata' )
english_lang = 'eng'
chinese_lang = 'chi_sim'

tesseract_chinese = ctypes.cdll.LoadLibrary(DLL_PATH)
tesseract_chinese.TessBaseAPICreate.restype = ctypes.c_uint64   #由于系统为64位,这里类型需指明为c_uint64
api_chinese = tesseract_chinese.TessBaseAPICreate()

def tess_init_chinese():
    rc = tesseract_chinese.TessBaseAPIInit3(ctypes.c_uint64(api_chinese), TESSDATA_PREFIX, chinese_lang)
    if rc:
        tesseract_chinese.TessBaseAPIDelete(ctypes.c_uint64(api_chinese))
        print('Could not initialize tesseract.\n')
        exit(3)

def tess_recognition_from_file_chinese(img_name):
    tesseract_chinese.TessBaseAPIProcessPages(
        ctypes.c_uint64(api_chinese), img_name, None, 0, None)
    tesseract_chinese.TessBaseAPIGetUTF8Text.restype = ctypes.c_uint64
    text_out = tesseract_chinese.TessBaseAPIGetUTF8Text(ctypes.c_uint64(api_chinese))
    return ctypes.string_at(text_out)

tesseract_english = ctypes.cdll.LoadLibrary(DLL_PATH)
tesseract_english.TessBaseAPICreate.restype = ctypes.c_uint64   #由于系统为64位,这里类型需指明为c_uint64
api_english = tesseract_chinese.TessBaseAPICreate()

def tess_init_english():
    rc = tesseract_english.TessBaseAPIInit3(ctypes.c_uint64(api_english), TESSDATA_PREFIX, english_lang)
    if rc:
        tesseract_english.TessBaseAPIDelete(ctypes.c_uint64(api_english))
        print('Could not initialize tesseract.\n')
        exit(3)

def tess_recognition_from_file_english(img_name):
    tesseract_english.TessBaseAPIProcessPages(
        ctypes.c_uint64(api_english), img_name, None, 0, None)
    tesseract_english.TessBaseAPIGetUTF8Text.restype = ctypes.c_uint64
    text_out = tesseract_english.TessBaseAPIGetUTF8Text(ctypes.c_uint64(api_english))
    return ctypes.string_at(text_out)

def cleanup(temp_name):
    ''' Tries to remove files by filename wildcard path. '''
    for filename in iglob(temp_name + '*' if temp_name else temp_name):
        try:
            os.remove(filename)
        except OSError:
            pass

#Deprecated
def tessRecognition(img, tmp_dir, english = True):
    img_name = os.path.join(tmp_dir, 'img.png')
    cv2.imwrite(img_name, img)
    if english:
        text_out = tess_recognition_from_file_english(img_name)
    else:
        text_out = tess_recognition_from_file_chinese(img_name)
    cleanup(img_name)
    return text_out

def tessRecognitionByFullpath(imgpath, isenglish):
    if isenglish:
        text_out = tess_recognition_from_file_english(imgpath)
    else:
        text_out = tess_recognition_from_file_chinese(imgpath)
    cleanup(imgpath)
    return text_out

# def from_file(path):
#     tesseract.TessBaseAPIProcessPages(
#         ctypes.c_uint64(api), path, None, 0, None)
#     tesseract.TessBaseAPIGetUTF8Text.restype = ctypes.c_uint64
#     text_out = tesseract.TessBaseAPIGetUTF8Text(ctypes.c_uint64(api))
#     return ctypes.string_at(text_out)

# if __name__ == '__main__':
#     image_file_path = b'/home/cvrsg/JpHu/TestGround/tesseract/build/bin/pics/1.png'
#     # result = from_file(image_file_path)
#     tessInit()
#     result = tessRecognition(image_file_path)
#     print(result)


import os
import re
import math
import sys
from scipy.stats import wasserstein_distance

class CalcJS:
	def __init__(self, fichier_source):
		self.delta = 0.0005
		fp = open (fichier_source, "r")
		self.dict_tokens_source = {}
		cpt = 0
		self.nb_tokens_source = 0
		for ligne in fp:
			if cpt > 2:
				ligne = ligne.rstrip()
				tab_tokens = ligne.split("\t")
				for i in range(len(tab_tokens)):
					if i in self.dict_tokens_source:
						self.dict_tokens_source[i] += int(tab_tokens[i])
					else:
						self.dict_tokens_source[i] = int(tab_tokens[i])
					self.nb_tokens_source += int(tab_tokens[i])
			cpt += 1
		fp.close()

	def calcule_wd(self, indiv):
		dict_tokens_resume = indiv.tokens
		nb_tokens_resume = 0
		for t in dict_tokens_resume:
			nb_tokens_resume += dict_tokens_resume[t]

		taille_vocabulaire_source = len (self.dict_tokens_source)
		taille_vocabulaire_resume = len (dict_tokens_resume)
		#print(dict_tokens_resume)

		#Calcul des probabilites lissees
		self.probas_lissees_resume = self.probas_lissees (dict_tokens_resume, nb_tokens_resume, taille_vocabulaire_resume, self.delta)
		self.probas_lissees_source = self.probas_lissees (self.dict_tokens_source, self.nb_tokens_source, taille_vocabulaire_source, self.delta)
		#print self.probas_lissees_resume
		#print self.probas_lissees_source
		#return self.jensen_shannon (self.probas_lissees_resume, self.probas_lissees_source)
		return wasserstein_distance(list(self.probas_lissees_source.values()),list(self.probas_lissees_resume.values()))
		#return wasserstein_distance(list(self.dict_tokens_source.values()),list(dict_tokens_resume.values()))

	def calcule_js(self, indiv):
		dict_tokens_resume = indiv.tokens
		nb_tokens_resume = 0
		for t in dict_tokens_resume:
			nb_tokens_resume += dict_tokens_resume[t]

		taille_vocabulaire_source = len (self.dict_tokens_source)
		taille_vocabulaire_resume = len (dict_tokens_resume)
		#print(dict_tokens_resume)

		#Calcul des probabilites lissees
		self.probas_lissees_resume = self.probas_lissees (dict_tokens_resume, nb_tokens_resume, taille_vocabulaire_resume, self.delta)
		self.probas_lissees_source = self.probas_lissees (self.dict_tokens_source, self.nb_tokens_source, taille_vocabulaire_source, self.delta)
		#print self.probas_lissees_resume
		#print self.probas_lissees_source
		#self.wd=wasserstein_distance(self.probas_lissees_source.values(),self.probas_lissees_resume.values())
		#return self.jensen_shannon (self.probas_lissees_resume, self.probas_lissees_source)
		return self.jensen_shannon ()

	def calcule_monotonicity(self, indiv):
		dict_tokens_resume = indiv.tokens
		return self.pearson(self.dict_tokens_source,dict_tokens_resume)

	def calcule_monotonicity_w(self, indiv):
		dict_tokens_resume = indiv.tokens
		return self.pearson_weighted(self.dict_tokens_source,dict_tokens_resume)

	def kullback_leibler (self, probas1, probas2):
		kl = 0
		for token in probas1:
			kl += probas1[token] * math.log( probas1[token] / probas2[token] )
		return kl

	def jensen_shannon (self):
		probas_moyenne = {}
		for token in self.probas_lissees_source:
			#print("token"+str(token) + str(self.probas_lissees_source.keys()))
			probas_moyenne[token] = (self.probas_lissees_resume[token] + self.probas_lissees_source[token]) / 2
		#print("------"+str(self.probas_lissees_resume)+"----------")

		return self.kullback_leibler(self.probas_lissees_source, probas_moyenne) + self.kullback_leibler(self.probas_lissees_resume, probas_moyenne ) / 2

	def probas_lissees (self, dict_tokens, nb_tokens, taille_vocabulaire, delta ):
		probas = {}
		somme_probas = 0
		for token in dict_tokens:
			probas[token] = dict_tokens[token] + delta
			probas[token] = probas[token] / (nb_tokens + 1 *   delta * taille_vocabulaire)
			somme_probas += probas[token]
		#print "----------------Somme probas : "+str(somme_probas)+"----------------"
		return probas

	def pearson(self,dic1,dic2):
		ldec1 = sorted(dic1, key=dic1.get, reverse=True)
		#print(ldec1)
		ldec2 = sorted(dic2, key=dic2.get, reverse=True)
		#print(ldec2)
		#remplacer les valeurs de ldec2 par leur index dans ldec1
		indL1=[]
		for l2 in ldec2:
			indL1.append(ldec1.index(l2))
		#print(indL1)
		#compter le nombre d'inversions d'indexes
		count=0
		first = indL1.pop(0)
		index=0
		for l in indL1:
			#print("first:"+str(first)+",l:"+str(l))
			if first > l:
				count=count+1
			first = l
			index = index + 1
		return(count)

	def pearson_weighted(self,dic1,dic2):
		ldec1 = sorted(dic1, key=dic1.get, reverse=True)
		#print(ldec1)
		ldec2 = sorted(dic2, key=dic2.get, reverse=True)
		#print(ldec2)
		#remplacer les valeurs de ldec2 par leur index dans ldec1
		indL1=[]
		for l2 in ldec2:
			indL1.append(ldec1.index(l2))
		#print(indL1)
		#compter le nombre d'inversions d'indexes
		count=0
		first = indL1.pop(0)
		index=1
		for l in indL1:
			#print("first:"+str(first)+",l:"+str(l))
			if first > l:
				return l
				#count=count+index
			first = l
			index = index + 1
		return(count)

#out_ws.txt

#"python ../Genetic/algo_ge.py ../TAC/u08_corr/D0802/D0802-A/concat-D0802.txt out_ws.txt
#	python rouge.py out_ws.txt ../TAC/u08_corr/D0802 >> stat_ws.txt


# imports
import pandas as pd
import labstep

"""
Data Cleaning and Tidying functions
"""

def AuthenticateByUserApiKey(email, apiKey, workspace_id):
    """
    1. logs in user
    2. returns workspace
    """
    # logs in
    user = labstep.authenticate(
        email,
        apiKey
        )
    # gets workspace.
    workspace = user.getWorkspace(
            workspace_id = workspace_id
            )

    return workspace, user

def WrangleLabstepDataFields(lysate_data, lysate_data_categories):

    """
    Iterates over the lysate_data_categories to look up data values in the experiement
    associated with the lysate item.
    Extracts and returns data as a pd Series.
    """
    # initialise dict
    retrieved_data_dict = {}

    # iterate over the categories
    for category in lysate_data_categories.keys():

        # extracts the data
        retrieved_data = lysate_data.get(category)

        # If the data field is empty, i.e is 'NoneType' object, then set as None
        if retrieved_data is None:

            # populates the dictionary with None
            retrieved_data_dict[category] = None
        else:
            # if not then extract the data
            # populates the dictionary
            retrieved_data_dict[category] = retrieved_data.getValue()
    # returns series
    return pd.Series(retrieved_data_dict)

def GetLysateData(lysate_name, lysate_data_categories, workspace):

    # retrieve the lysate resource record and access the item.
    # Note, there should be only one item.

    # as this returns a list and there should only be one lysate with that name,
    # a sanity check is performed.
    lysate = workspace.getResources(
    search_query = lysate_name
    )

    if len(lysate) > 1:
        raise Exception("There appears to be more than one lysate in the inventory with the name: " + lysate_name + ". Please resolve before proceeding.")
    elif len(lysate) == 0:
        raise Exception("There appears no lysates in the inventory with the name: " + lysate_name + ". Please resolve before proceeding.")
    else:
        # if theres only one, extract
        lysate = lysate[0]

    # extract the item for that lysate and repeat sanity check.
    lysate = lysate.getItems()
    if len(lysate) > 1:
        raise Exception("There appears to be more than one item associated with " + lysate_name + ". Please resolve before proceeding.")
    elif len(lysate) == 0:
        raise Exception("There appears no items associated with " + lysate_name + ". Please resolve before proceeding.")
    else:
        # if theres only one, extract
        lysate = lysate[0]

    # extract the data associated with that lysate
    lysate_data = lysate.getData()

    # function defined above
    # returns pd Series
    lysate_data_series = WrangleLabstepDataFields(lysate_data, lysate_data_categories)

    # append lysate name to make matching unambigious
    lysate_data_series["Lysate_Inventory_Record"] = lysate_name

    return lysate_data_series


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

"""
@author: Kai Borowiak
@summary: TestCase for testlink.objects.TestTestCase
"""

# IMPORTS
import random
import string
import unittest
from mock import patch, Mock

from testlink.objects.tl_testcase import TestCase

def randput(length=10): return "".join([random.choice(string.letters) for _ in xrange(random.randint(1, length))])

def randint(length=10): return int("".join([random.choice(string.digits) for _ in xrange(random.randint(1, length))]))

class TestCaseTests(unittest.TestCase):
    """TestCase Object Tests"""

    def __init__(self, *args, **kwargs):
        super(TestCaseTests, self).__init__(*args, **kwargs)
        self._testMethodDoc = "TestCase: " + self._testMethodDoc

    @patch('testlink.objects.TestCase.getTestProject')
    def test__str__(self, _patch):
        """String representation"""
        ext_id = randint()
        name = randput()

        project = Mock()
        project.prefix = randput()
        _patch.return_value = project

        obj = TestCase(name=name, external_id=ext_id)
        _string = str(obj)
        self.assertEqual(_string, "Testcase %s-%s: %s" % (project.prefix, ext_id, name))


import numpy as np
from tqdm import tqdm

import Dynamics.dynamics as dyn
import utils.convert as convert
import utils.quaternion as quat
import utils.horizons as horizons
import Dynamics.body as body

class Simulator():

    def __init__(self, bodies, start, end, dt, horizons_dt):
        self.bodies = bodies
        t_array = np.arange(start, end, convert.convertSecToDays(dt))
        self.t_array = t_array
        self.time_since_j2000 = t_array[0]
        self.last_time = 2*t_array[0] - t_array[1]
        self.spacecrafts = []
        self.prev_true = self.time_since_j2000
        self.horizons_dt = horizons_dt
        for i, body in enumerate(self.bodies):
            if body.name == "Earth":
                self.idx_earth = i

    def init_bodies(bodies, start_time, end_time, dt):
        objs = []
        for obj in bodies:
            states, dt = horizons.get_state_from_horizons(obj, start_time, end_time, dt)
            obj = body.Body(obj, states)
            objs.append(obj)
        return objs, dt

    def add_spacecrafts(self, spacecraft):
        self.spacecrafts = spacecraft

    def check_impact(self, sat, body):
        if np.linalg.norm(sat.state - body.state) < body.radius:
            print(f"{sat.get_name()} impacted {body.name}")
            if body.name == "Earth":
                sat.set_crashed(True, self.time_since_j2000)
            else:
                sat.set_crashed(False)

    def tic(self, dt):
        self.time_since_j2000 += dt
        next_true = self.prev_true + self.horizons_dt
        if self.time_since_j2000 == next_true:
            self.prev_true = self.time_since_j2000

        for sat in self.spacecrafts:
            new_state, new_qEci2Body, ecef_state, ll_state = np.zeros(6), np.zeros(4), np.zeros(6), np.zeros(2)
            if not sat.is_crashed():
                new_state, new_qEci2Body = dyn.orbit_prop(sat, dt, self.bodies)
                bci_state = new_state - self.bodies[0].state
            else:
                if sat.ecef_crash is not None:
                    ecef_state = sat.ecef_crash
                    new_state = self.bodies[self.idx_earth].state + convert.ecef2eci(ecef_state, convert.convertSecToDays(self.time_since_j2000))
                    bci_state = new_state - self.bodies[0].state
                    new_qEci2Body = quat.init_quat(bci_state, "lvlh")
            for body in self.bodies:
                self.check_impact(sat, body)
            if self.bodies[0].name == "Earth":
                ecef_state = convert.eci2ecef(bci_state, convert.convertSecToDays(self.time_since_j2000))
                ll_state = convert.ecef2lla(ecef_state)
            sat.update_state(self.time_since_j2000, new_state, new_qEci2Body, bci_state, ecef_state, ll_state)
        for body in self.bodies:
            body.update_state(self.bodies, dt, self.time_since_j2000 == next_true)

    def run(self):
        for t in tqdm(self.t_array):
            self.tic(convert.convertDaysToSec(t-self.last_time))
            self.last_time = t
        return self.t_array, self.spacecrafts


import sqlite3
import pickle
import time

from .conn import conn, cur

#History table.

def put(list_of_dicts):

    columns = [
        'tank_id', 'created_at', 'recency',
        'battle_life_time', 'capture_points', 'damage_assisted_radio',
        'damage_dealt', 'damage_received', 'direct_hits_received',
        'frags', 'hits', 'losses', 'piercings', 'piercings_received',
        'shots', 'spotted', 'survived_battles', 'wins', 'xp'
    ]

    columns_str = ', '.join(columns)
    question_marks = ', '.join(['?' for i in columns])

    rows = []
    for item in list_of_dicts:
        rows.append([item[col] for col in columns])

    cur.executemany(f'INSERT INTO history ({columns_str}) VALUES ({question_marks})', rows)
    conn.commit()


from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from math import sqrt

from material import Material

materials = {}

def normal(center_point, clockwise, widdershins):
  n = [clockwise[j] - center_point[j] for j in range(3)]
  p = [widdershins[j] - center_point[j] for j in range(3)]
  cross = [p[1] * n[2] - p[2] * n[1], p[2] * n[0] - p[0] * n[2], p[0] * n[1] - p[1] * n[0]]
  length = sqrt(cross[0] ** 2 + cross[1] ** 2 + cross[2] ** 2)
  n = [cross[j] / length for j in range(3)]
  return n

def decodeTuple(chunks):
  return [float(chunk) for chunk in chunks[1:]]

class Model(object):
  def __init__(self, input_name, pre_func=None, post_func=None):
    input_file = open(input_name)
    vertices = [None]
    textures = [None]
    normals = [None]
    lines = []
    points = []
    tris = []
    quads = []
    faces = []
    currentMaterial = None

    for line in input_file.readlines():
      chunks = line.split()
      if len(chunks) > 0:
        if   chunks[0] == 'v':
          vertices.append(decodeTuple(chunks))
        elif chunks[0] == 'vt':
          textures.append(decodeTuple(chunks))
        elif chunks[0] == 'vn':
          normals.append(decodeTuple(chunks))
        elif chunks[0] == 'p':
            points.extend([(vertices[int(chunk)], currentMaterial) for chunk in chunks[1:]])
        elif chunks[0] == 'l':
          line = []
          for chunk in chunks[1:]:
            if chunk.contains('/'):
              v = vertices[int(chunk.split('/')[0])]
              vt = textures[int(chunk.split('/')[1])]
            else:
              v = vertices[int(chunk)]
              vt = None
            line.append((v, vt))
          lines.append((line, currentMaterial))
        elif chunks[0] == 'f':
          face = []
          for chunk in chunks[1:]:
            if len(chunk.split('/')) == 3:
              v = vertices[int(chunk.split('/')[0])]
              vt = textures[int(chunk.split('/')[1])] if chunk.split('/')[1] != '' else None
              vn = normals[int(chunk.split('/')[2])]
            elif len(chunk.split('/')) == 2:
              v = vertices[int(chunk.split('/')[0])]
              vt = textures[int(chunk.split('/')[1])]
              vn = None
            else:
              v = vertices[int(chunk)]
              vt = None
              vn = None
            face.append((v, vt, vn))
          if len(face) == 3:
            tris.append((face, currentMaterial))
          elif len(face) == 4:
            quads.append((face, currentMaterial))
          else:
            faces.append((face, currentMaterial))
        elif chunks[0] == 'mtllib':
          loadMtlLib(input_name.rpartition("/")[0] + "/" + chunks[1])
        elif chunks[0] == 'usemtl':
          currentMaterial = materials[chunks[1]]

    currentMaterial = None
    # generate the glGenLists thingy
    self.listID = glGenLists(1); glNewList(self.listID, GL_COMPILE_AND_EXECUTE)

    if pre_func is not None: pre_func()

    glBegin(GL_POINTS)
    for point in points:
      if point[1] is not None and point[1] != currentMaterial:
        point[1].setMaterial()
        currentMaterial = point[1]
      glVertex(point[0])
    glEnd()

    for line in lines:
      glBegin(GL_LINE)
      if line[1] is not None and line[1] != currentMaterial:
        line[1].setMaterial()
        currentMaterial = line[1]
      for vertex in line[0]:
        glVertex(vertex[0])
      glEnd()

    glBegin(GL_TRIANGLES)
    for tri in tris:
      if tri[1] is not None and tri[1] != currentMaterial:
        tri[1].setMaterial()
        currentMaterial = tri[1]
      for i in range(len(tri[0])):
        if tri[0][i][1] is not None:
          # Texture
          None
        else:
          # No texture
          None
        if tri[0][i][2] is not None:
          glNormal3f(tri[0][i][2][0], tri[0][i][2][1], tri[0][i][2][2])
        else:
          previous = tri[0][i - 1][0]
          next = tri[0][i + 1 - len(tri[0])][0]
          n = normal(tri[0][i][0], previous, next)
          glNormal(n[0], n[1], n[2])
        glVertex(tri[0][i][0])
    glEnd()

    glBegin(GL_QUADS)
    for quad in quads:
      if quad[1] is not None and quad[1] != currentMaterial:
        quad[1].setMaterial()
        currentMaterial = quad[1]
      for i in range(len(quad[0])):
        if quad[0][i][1] is not None:
          # Texture
          None
        else:
          # No texture
          None
        if quad[0][i][2] is not None:
          glNormal3f(quad[0][i][2][0], quad[0][i][2][1], quad[0][i][2][2])
        else:
          previous = quad[0][i - 1][0]
          next = quad[0][i + 1 - len(quad[0])][0]
          n = normal(quad[0][i][0], previous, next)
          glNormal(n[0], n[1], n[2])
        glVertex(quad[0][i][0])
    glEnd()

    for face in faces:
      if len(face[0]) == 4:
        glBegin(GL_QUADS)
      elif len(face[0]) == 3:
        glBegin(GL_TRIANGLES)
      else:
        glBegin(GL_POLYGON)
      if face[1] is not None and face[1] != currentMaterial:
        face[1].setMaterial()
        currentMaterial = face[1]
      for i in range(len(face[0])):
        if face[0][i][1] is not None:
          # Texture
          None
        else:
          # No texture
          None
        if face[0][i][2] is not None:
          glNormal3f(face[0][i][2][0], face[0][i][2][1], face[0][i][2][2])
        else:
          previous = face[0][i - 1][0]
          next = face[0][i + 1 - len(face[0])][0]
          n = normal(face[0][i][0], previous, next)
          glNormal(n[0], n[1], n[2])
        glVertex(face[0][i][0])
      glEnd()

    if post_func is not None: post_func()

    glEndList()

  def display(self):
    glCallList(self.listID)

def loadMtlLib(lib_name):
  mtlLib = open(lib_name)
  mtls = mtlLib.read().split("newmtl ")
  for mtl in mtls[1:]:
    lines = mtl.splitlines()
    materials[lines[0]] = Material(lines[1:])


from setuptools import setup

install_requires = [
    "cmdtree>=0.0.3",
    "RPi.GPIO>=0.6.0",
]

entry_points = {
        'console_scripts': ['pi-fan-tuner=pi_fan_tuner:main'],
    }

setup(
    name='PIFanTuner',
    version='0.0.1',
    py_modules=["pi_fan_tuner", ],
    install_requires=install_requires,
    entry_points=entry_points,
    url='https://github.com/winkidney/PIFanTuner',
    license='MIT',
    author='winkidney',
    author_email='winkidney@gmail.com',
    description='RaspberryPI CPU fan tuner with a s8050 Triode',
)


import argparse
import os
import os.path as osp

import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import network
import loss
import pre_process as prep
import torch.utils.data as util_data
import lr_schedule
import data_list
from data_list import ImageList
from torch.autograd import Variable

optim_dict = {"SGD": optim.SGD}

def train(config):
    ## set pre-process
    prep_dict = {}
    prep_config = config["prep"]
    prep_dict["train_set1"] = prep.image_train( \
                            resize_size=prep_config["resize_size"], \
                            crop_size=prep_config["crop_size"])
    prep_dict["train_set2"] = prep.image_train( \
                            resize_size=prep_config["resize_size"], \
                            crop_size=prep_config["crop_size"])

    ## prepare data
    dsets = {}
    dset_loaders = {}
    data_config = config["data"]
    dsets["train_set1"] = ImageList(open(data_config["train_set1"]["list_path"]).readlines(), \
                                transform=prep_dict["train_set1"])
    dset_loaders["train_set1"] = util_data.DataLoader(dsets["train_set1"], \
            batch_size=data_config["train_set1"]["batch_size"], \
            shuffle=True, num_workers=4)
    dsets["train_set2"] = ImageList(open(data_config["train_set2"]["list_path"]).readlines(), \
                                transform=prep_dict["train_set2"])
    dset_loaders["train_set2"] = util_data.DataLoader(dsets["train_set2"], \
            batch_size=data_config["train_set2"]["batch_size"], \
            shuffle=True, num_workers=4)

    hash_bit = config["hash_bit"]

    ## set base network
    net_config = config["network"]
    base_network = net_config["type"](**net_config["params"])

    use_gpu = torch.cuda.is_available()
    if use_gpu:
        base_network = base_network.cuda()

    ## collect parameters
    parameter_list = [{"params":base_network.feature_layers.parameters(), "lr":1}, \
                      {"params":base_network.hash_layer.parameters(), "lr":10}]

    ## set optimizer
    optimizer_config = config["optimizer"]
    optimizer = optim_dict[optimizer_config["type"]](parameter_list, \
                    **(optimizer_config["optim_params"]))
    param_lr = []
    for param_group in optimizer.param_groups:
        param_lr.append(param_group["lr"])
    schedule_param = optimizer_config["lr_param"]
    lr_scheduler = lr_schedule.schedule_dict[optimizer_config["lr_type"]]

    ## train
    len_train1 = len(dset_loaders["train_set1"]) - 1
    len_train2 = len(dset_loaders["train_set2"]) - 1
    transfer_loss_value = classifier_loss_value = total_loss_value = 0.0
    best_acc = 0.0
    for i in range(config["num_iterations"]):
        if i % config["snapshot_interval"] == 0:
            torch.save(nn.Sequential(base_network), osp.join(config["output_path"], \
                "iter_{:05d}_model.pth.tar".format(i)))

        ## train one iter
        base_network.train(True)
        optimizer = lr_scheduler(param_lr, optimizer, i, **schedule_param)
        optimizer.zero_grad()
        if i % len_train1 == 0:
            iter1 = iter(dset_loaders["train_set1"])
        if i % len_train2 == 0:
            iter2 = iter(dset_loaders["train_set2"])
        inputs1, labels1 = iter1.next()
        inputs2, labels2 = iter2.next()
        if use_gpu:
            inputs1, inputs2, labels1, labels2 = \
                Variable(inputs1).cuda(), Variable(inputs2).cuda(), \
                Variable(labels1).cuda(), Variable(labels2).cuda()
        else:
            inputs1, inputs2, labels1, labels2 = Variable(inputs1), \
                Variable(inputs2), Variable(labels1), Variable(labels2)

        inputs = torch.cat((inputs1, inputs2), dim=0)
        outputs = base_network(inputs)
        similarity_loss = loss.pairwise_loss(outputs.narrow(0,0,inputs1.size(0)), \
                                 outputs.narrow(0,inputs1.size(0),inputs2.size(0)), \
                                 labels1, labels2, \
                                 hashbit = hash_bit,\
                                 gamma=config["loss"]["gamma"], \
                                 normed=config["loss"]["normed"], \
                                 q_lambda=config["loss"]["q_lambda"])
        total_loss_value = total_loss_value + similarity_loss.float().data[0]
        similarity_loss.backward()
        if (i+1) % len_train1 == 0:
            print("Epoch: {:05d}, loss: {:.3f}".format(i//len_train1, total_loss_value))
            total_loss_value = 0.0
        optimizer.step()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='DCH')
    parser.add_argument('--gpu_id', type=str, default='0', help="device id to run")
    parser.add_argument('--dataset', type=str, default='coco', help="dataset name")
    parser.add_argument('--hash_bit', type=int, default=48, help="number of hash code bits")
    parser.add_argument('--net', type=str, default='ResNet50', help="base network type")
    parser.add_argument('--prefix', type=str, help="save path prefix")
    parser.add_argument('--lr', type=float, help="learning rate")
    parser.add_argument('--q_lambda', type=float, help="parameter weight")
    args = parser.parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id

    # train config
    config = {}
    config["num_iterations"] = 10000
    config["snapshot_interval"] = 3000
    config["dataset"] = args.dataset
    config["hash_bit"] = args.hash_bit
    config["output_path"] = "../snapshot/"+config["dataset"]+"_"+ \
                            str(config["hash_bit"])+"bit_"+args.prefix
    if not osp.exists(config["output_path"]):
        os.mkdir(config["output_path"])
    config["out_file"] = open(osp.join(config["output_path"], "log.txt"), "w")

    if not osp.exists(config["output_path"]):
        os.mkdir(config["output_path"])
    config["network"] = {}
    if "ResNet" in args.net:
        config["network"]["type"] = network.ResNetFc
        config["network"]["params"] = {"name":args.net, "hash_bit":config["hash_bit"]}
    elif "VGG" in args.net:
        config["network"]["type"] = network.VGGFc
        config["network"]["params"] = {"name":args.net, "hash_bit":config["hash_bit"]}
    elif "AlexNet" in args.net:
        config["network"]["type"] = network.AlexNetFc
        config["network"]["params"] = {"hash_bit":config["hash_bit"]}
    config["prep"] = {"test_10crop":True, "resize_size":256, "crop_size":224}
    config["optimizer"] = {"type":"SGD", "optim_params":{"lr":1.0, "momentum":0.9, \
                           "weight_decay":0.0005, "nesterov":True}, "lr_type":"step", \
                           "lr_param":{"init_lr":args.lr, "gamma":0.5, "step":3000} }

    config["loss"] = {"gamma":15.0, "normed":True, "q_lambda":args.q_lambda}

    if config["dataset"] == "imagenet":
        config["data"] = {"train_set1":{"list_path":"../data/imagenet/train.txt", "batch_size":36}, \
                          "train_set2":{"list_path":"../data/imagenet/train.txt", "batch_size":36}}
    elif config["dataset"] == "nus_wide":
        config["data"] = {"train_set1":{"list_path":"../data/nus_wide/train.txt", "batch_size":36}, \
                          "train_set2":{"list_path":"../data/nus_wide/train.txt", "batch_size":36}}
    elif config["dataset"] == "coco":
        config["data"] = {"train_set1":{"list_path":"../data/coco/train.txt", "batch_size":36}, \
                          "train_set2":{"list_path":"../data/coco/train.txt", "batch_size":36}}
    print(config["loss"])
    train(config)


'''1(1)(0), 1(2)(0), 2(3)(1), 3(4)(2), 5(5)(3), 8(6)(4), 13(7)(5), 21(8)(6), 34(9)(7), 55(10)(8), ...
Обозначим fib(n)(i), где fib - это число Фибоначчи, n - номер числа (натуральное число), i - кол-во циклов '''

CONST = 1
fib1 = CONST
fib2 = CONST
print('Calculate the element of the Fibonacci series according to the number entered.')
n = int(input('Please enter the Fibonacci number: '))
if n < 1:
    print('Enter a natural number! For example, 1, 2, 3, 4, 5, 6, 7, 8, 9 …')
elif n == 1 or n == 2:
    print ('Согласно определения первые два числа из ряда Фибоначчи равны ', CONST)
else:
    i = 2 # так как первый цикл обращается сразу к 3-му элемену (n = 3)
    while n != i: # как только номер цикла совпадет с номером элемента следует выйти из цикла i = n
        fib_sum = fib2 + fib1
        fib1 = fib2  # второй элемент превратить надо в первый
        fib2 = fib_sum  # сумма превращается во второй. И так по циклу пока счетчик не остановит цикл
        i += 1
    print('It corresponds to the element of the Fibonacci series', fib_sum)

# в блоке else начинал думать так: i = 0, и когда n=i+2 то надо остановить цикл, а потом просто додумался сделать i=2


import itertools
import operator
import sys

def _is_int(string):
    try:
        int(string)
        return True
    except ValueError:
        return False

class SemanticVersion(object):
    """A pure semantic version independent of serialisation.

    See the pbr doc 'semver' for details on the semantics.
    """

    def __init__(
            self, major, minor=0, patch=0, prerelease_type=None,
            prerelease=None, dev_count=None):
        """Create a SemanticVersion.

        :param major: Major component of the version.
        :param minor: Minor component of the version. Defaults to 0.
        :param patch: Patch level component. Defaults to 0.
        :param prerelease_type: What sort of prerelease version this is -
            one of a(alpha), b(beta) or rc(release candidate).
        :param prerelease: For prerelease versions, what number prerelease.
            Defaults to 0.
        :param dev_count: How many commits since the last release.
        """
        self._major = major
        self._minor = minor
        self._patch = patch
        self._prerelease_type = prerelease_type
        self._prerelease = prerelease
        if self._prerelease_type and not self._prerelease:
            self._prerelease = 0
        self._dev_count = dev_count or 0  # Normalise 0 to None.

    def __eq__(self, other):
        if not isinstance(other, SemanticVersion):
            return False
        return self.__dict__ == other.__dict__

    def __hash__(self):
        return sum(map(hash, self.__dict__.values()))

    def _sort_key(self):
        """Return a key for sorting SemanticVersion's on."""
        # key things:
        # - final is after rc's, so we make that a/b/rc/z
        # - dev==None is after all other devs, so we use sys.maxsize there.
        # - unqualified dev releases come before any pre-releases.
        # So we do:
        # (major, minor, patch) - gets the major grouping.
        # (0|1) unqualified dev flag
        # (a/b/rc/z) - release segment grouping
        # pre-release level
        # dev count, maxsize for releases.
        rc_lookup = {'a': 'a', 'b': 'b', 'rc': 'rc', None: 'z'}
        if self._dev_count and not self._prerelease_type:
            uq_dev = 0
        else:
            uq_dev = 1
        return (
            self._major, self._minor, self._patch,
            uq_dev,
            rc_lookup[self._prerelease_type], self._prerelease,
            self._dev_count or sys.maxsize)

    def __lt__(self, other):
        """Compare self and other, another Semantic Version."""
        # NB(lifeless) this could perhaps be rewritten as
        # lt (tuple_of_one, tuple_of_other) with a single check for
        # the typeerror corner cases - that would likely be faster
        # if this ever becomes performance sensitive.
        if not isinstance(other, SemanticVersion):
            raise TypeError("ordering to non-SemanticVersion is undefined")
        return self._sort_key() < other._sort_key()

    def __le__(self, other):
        return self == other or self < other

    def __ge__(self, other):
        return not self < other

    def __gt__(self, other):
        return not self <= other

    def __ne__(self, other):
        return not self == other

    def __repr__(self):
        return "pbr.version.SemanticVersion(%s)" % self.release_string()

    @classmethod
    def from_pip_string(klass, version_string):
        """Create a SemanticVersion from a pip version string.

        This method will parse a version like 1.3.0 into a SemanticVersion.

        This method is responsible for accepting any version string that any
        older version of pbr ever created.

        Therefore: versions like 1.3.0a1 versions are handled, parsed into a
        canonical form and then output - resulting in 1.3.0.0a1.
        Pre pbr-semver dev versions like 0.10.1.3.g83bef74 will be parsed but
        output as 0.10.1.dev3.g83bef74.

        :raises ValueError: Never tagged versions sdisted by old pbr result in
            just the git hash, e.g. '1234567' which poses a substantial problem
            since they collide with the semver versions when all the digits are
            numerals. Such versions will result in a ValueError being thrown if
            any non-numeric digits are present. They are an exception to the
            general case of accepting anything we ever output, since they were
            never intended and would permanently mess up versions on PyPI if
            ever released - we're treating that as a critical bug that we ever
            made them and have stopped doing that.
        """

        try:
            return klass._from_pip_string_unsafe(version_string)
        except IndexError:
            raise ValueError("Invalid version %r" % version_string)

    @classmethod
    def _from_pip_string_unsafe(klass, version_string):
        # Versions need to start numerically, ignore if not
        version_string = version_string.lstrip('vV')
        if not version_string[:1].isdigit():
            raise ValueError("Invalid version %r" % version_string)
        input_components = version_string.split('.')
        # decimals first (keep pre-release and dev/hashes to the right)
        components = [c for c in input_components if c.isdigit()]
        digit_len = len(components)
        if digit_len == 0:
            raise ValueError("Invalid version %r" % version_string)
        elif digit_len < 3:
            if (digit_len < len(input_components) and
                    input_components[digit_len][0].isdigit()):
                # Handle X.YaZ - Y is a digit not a leadin to pre-release.
                mixed_component = input_components[digit_len]
                last_component = ''.join(itertools.takewhile(
                    lambda x: x.isdigit(), mixed_component))
                components.append(last_component)
                input_components[digit_len:digit_len + 1] = [
                    last_component, mixed_component[len(last_component):]]
                digit_len += 1
            components.extend([0] * (3 - digit_len))
        components.extend(input_components[digit_len:])
        major = int(components[0])
        minor = int(components[1])
        dev_count = None
        post_count = None
        prerelease_type = None
        prerelease = None

        def _parse_type(segment):
            # Discard leading digits (the 0 in 0a1)
            isdigit = operator.methodcaller('isdigit')
            segment = ''.join(itertools.dropwhile(isdigit, segment))
            isalpha = operator.methodcaller('isalpha')
            prerelease_type = ''.join(itertools.takewhile(isalpha, segment))
            prerelease = segment[len(prerelease_type)::]
            return prerelease_type, int(prerelease)
        if _is_int(components[2]):
            patch = int(components[2])
        else:
            # legacy version e.g. 1.2.0a1 (canonical is 1.2.0.0a1)
            # or 1.2.dev4.g1234 or 1.2.b4
            patch = 0
            components[2:2] = [0]
        remainder = components[3:]
        remainder_starts_with_int = False
        try:
            if remainder and int(remainder[0]):
                remainder_starts_with_int = True
        except ValueError:
            pass
        if remainder_starts_with_int:
            # old dev format - 0.1.2.3.g1234
            dev_count = int(remainder[0])
        else:
            if remainder and (remainder[0][0] == '0' or
                              remainder[0][0] in ('a', 'b', 'r')):
                # Current RC/beta layout
                prerelease_type, prerelease = _parse_type(remainder[0])
                remainder = remainder[1:]
            while remainder:
                component = remainder[0]
                if component.startswith('dev'):
                    dev_count = int(component[3:])
                elif component.startswith('post'):
                    dev_count = None
                    post_count = int(component[4:])
                else:
                    raise ValueError(
                        'Unknown remainder %r in %r'
                        % (remainder, version_string))
                remainder = remainder[1:]
        result = SemanticVersion(
            major, minor, patch, prerelease_type=prerelease_type,
            prerelease=prerelease, dev_count=dev_count)
        if post_count:
            if dev_count:
                raise ValueError(
                    'Cannot combine postN and devN - no mapping in %r'
                    % (version_string,))
            result = result.increment().to_dev(post_count)
        return result

    def brief_string(self):
        """Return the short version minus any alpha/beta tags."""
        return "%s.%s.%s" % (self._major, self._minor, self._patch)

    def debian_string(self):
        """Return the version number to use when building a debian package.

        This translates the PEP440/semver precedence rules into Debian version
        sorting operators.
        """
        return self._long_version("~")

    def decrement(self):
        """Return a decremented SemanticVersion.

        Decrementing versions doesn't make a lot of sense - this method only
        exists to support rendering of pre-release versions strings into
        serialisations (such as rpm) with no sort-before operator.

        The 9999 magic version component is from the spec on this - pbr-semver.

        :return: A new SemanticVersion object.
        """
        if self._patch:
            new_patch = self._patch - 1
            new_minor = self._minor
            new_major = self._major
        else:
            new_patch = 9999
            if self._minor:
                new_minor = self._minor - 1
                new_major = self._major
            else:
                new_minor = 9999
                if self._major:
                    new_major = self._major - 1
                else:
                    new_major = 0
        return SemanticVersion(
            new_major, new_minor, new_patch)

    def increment(self, minor=False, major=False):
        """Return an incremented SemanticVersion.

        The default behaviour is to perform a patch level increment. When
        incrementing a prerelease version, the patch level is not changed
        - the prerelease serial is changed (e.g. beta 0 -> beta 1).

        Incrementing non-pre-release versions will not introduce pre-release
        versions - except when doing a patch incremental to a pre-release
        version the new version will only consist of major/minor/patch.

        :param minor: Increment the minor version.
        :param major: Increment the major version.
        :return: A new SemanticVersion object.
        """
        if self._prerelease_type:
            new_prerelease_type = self._prerelease_type
            new_prerelease = self._prerelease + 1
            new_patch = self._patch
        else:
            new_prerelease_type = None
            new_prerelease = None
            new_patch = self._patch + 1
        if minor:
            new_minor = self._minor + 1
            new_patch = 0
            new_prerelease_type = None
            new_prerelease = None
        else:
            new_minor = self._minor
        if major:
            new_major = self._major + 1
            new_minor = 0
            new_patch = 0
            new_prerelease_type = None
            new_prerelease = None
        else:
            new_major = self._major
        return SemanticVersion(
            new_major, new_minor, new_patch,
            new_prerelease_type, new_prerelease)

    def _long_version(self, pre_separator, rc_marker=""):
        """Construct a long string version of this semver.

        :param pre_separator: What separator to use between components
            that sort before rather than after. If None, use . and lower the
            version number of the component to preserve sorting. (Used for
            rpm support)
        """
        if ((self._prerelease_type or self._dev_count)
                and pre_separator is None):
            segments = [self.decrement().brief_string()]
            pre_separator = "."
        else:
            segments = [self.brief_string()]
        if self._prerelease_type:
            segments.append(
                "%s%s%s%s" % (pre_separator, rc_marker, self._prerelease_type,
                              self._prerelease))
        if self._dev_count:
            if not self._prerelease_type:
                segments.append(pre_separator)
            else:
                segments.append('.')
            segments.append('dev')
            segments.append(self._dev_count)
        return "".join(str(s) for s in segments)

    def release_string(self):
        """Return the full version of the package.

        This including suffixes indicating VCS status.
        """
        return self._long_version(".", "0")

    def rpm_string(self):
        """Return the version number to use when building an RPM package.

        This translates the PEP440/semver precedence rules into RPM version
        sorting operators. Because RPM has no sort-before operator (such as the
        ~ operator in dpkg),  we show all prerelease versions as being versions
        of the release before.
        """
        return self._long_version(None)

    def to_dev(self, dev_count):
        """Return a development version of this semver.

        :param dev_count: The number of commits since the last release.
        """
        return SemanticVersion(
            self._major, self._minor, self._patch, self._prerelease_type,
            self._prerelease, dev_count=dev_count)

    def version_tuple(self):
        """Present the version as a version_info tuple.

        For documentation on version_info tuples see the Python
        documentation for sys.version_info.

        Since semver and PEP-440 represent overlapping but not subsets of
        versions, we have to have some heuristic / mapping rules, and have
        extended the releaselevel field to have alphadev, betadev and
        candidatedev values. When they are present the dev count is used
        to provide the serial.
        - a/b/rc take precedence.
        - if there is no pre-release version the dev version is used.
        - serial is taken from the dev/a/b/c component.
        - final non-dev versions never get serials.
        """
        segments = [self._major, self._minor, self._patch]
        if self._prerelease_type:
            type_map = {('a', False): 'alpha',
                        ('b', False): 'beta',
                        ('rc', False): 'candidate',
                        ('a', True): 'alphadev',
                        ('b', True): 'betadev',
                        ('rc', True): 'candidatedev',
                        }
            segments.append(
                type_map[(self._prerelease_type, bool(self._dev_count))])
            segments.append(self._dev_count or self._prerelease)
        elif self._dev_count:
            segments.append('dev')
            segments.append(self._dev_count - 1)
        else:
            segments.append('final')
            segments.append(0)
        return tuple(segments)

class VersionInfo(object):

    def __init__(self, package):
        """Object that understands versioning for a package

        :param package: name of the python package, such as glance, or
                        python-glanceclient
        """
        self.package = package
        self.version = None
        self._cached_version = None
        self._semantic = None

    def __str__(self):
        """Make the VersionInfo object behave like a string."""
        return self.version_string()

    def __repr__(self):
        """Include the name."""
        return "pbr.version.VersionInfo(%s:%s)" % (
            self.package, self.version_string())

    def _get_version_from_pkg_resources(self):
        """Obtain a version from pkg_resources or setup-time logic if missing.

        This will try to get the version of the package from the pkg_resources
        record associated with the package, and if there is no such record
        falls back to the logic sdist would use.
        """
        # Lazy import because pkg_resources is costly to import so defer until
        # we absolutely need it.
        import pkg_resources
        try:
            requirement = pkg_resources.Requirement.parse(self.package)
            provider = pkg_resources.get_provider(requirement)
            result_string = provider.version
        except pkg_resources.DistributionNotFound:
            # The most likely cause for this is running tests in a tree
            # produced from a tarball where the package itself has not been
            # installed into anything. Revert to setup-time logic.
            from pbr import packaging
            result_string = packaging.get_version(self.package)
        return SemanticVersion.from_pip_string(result_string)

    def release_string(self):
        """Return the full version of the package.

        This including suffixes indicating VCS status.
        """
        return self.semantic_version().release_string()

    def semantic_version(self):
        """Return the SemanticVersion object for this version."""
        if self._semantic is None:
            self._semantic = self._get_version_from_pkg_resources()
        return self._semantic

    def version_string(self):
        """Return the short version minus any alpha/beta tags."""
        return self.semantic_version().brief_string()

    # Compatibility functions
    canonical_version_string = version_string
    version_string_with_vcs = release_string

    def cached_version_string(self, prefix=""):
        """Return a cached version string.

        This will return a cached version string if one is already cached,
        irrespective of prefix. If none is cached, one will be created with
        prefix and then cached and returned.
        """
        if not self._cached_version:
            self._cached_version = "%s%s" % (prefix,
                                             self.version_string())
        return self._cached_version


from goose3 import Goose
import pandas as pd
import time
from newspaper import Article
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
import random
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from deep_translator import GoogleTranslator
from googletrans import Translator

path_to_csv = r"C:\Users\ujwal\Downloads\PREIPO_Final_Report_2023-07-01.csv"

def get_text(path_to_csv):

    def translatedeep(text):
        translation = GoogleTranslator(source='auto', target='en').translate(text)
        return translation

    def translate(text):
        translator = Translator()
        translation = translator.translate(text, dest='en')
        return translation.text

    df = pd.read_csv(path_to_csv)
    g = Goose({
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0",
                    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                    'sec-fetch-site': 'none',
                    'sec-fetch-mode': 'navigate',
                    'sec-fetch-user': '?1',
                    'sec-fetch-dest': 'document',
                    'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
                })
    errors_404 = []

    options = webdriver.ChromeOptions()
    options.headless = True
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("--window-size=1920,1080")
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    service = ChromeService(executable_path=ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=options)

    def reset_driver():
        url = "https://www.ibm.com/demos/live/natural-language-understanding/self-service/home"
        driver.get(url)
        url_parts = driver.find_elements(By.CLASS_NAME, "story-button")
        for url in url_parts:
            if "URL" in str(url.text):
                url.click()
                break
        time.sleep(round(random.uniform(0.5, 1), 2))

    reset_driver()

    def get_from_goose(link):
        """ Fastest way to extract article """
        return g.extract(url=link).cleaned_text

    def get_from_newspaper(link):
        " Mediocre fast "
        article = Article(link)
        article.download()
        article.parse()
        return article.text

    def get_from_ibm(link):
        """ Very Slow """
        try:
            textarea = driver.find_element(By.ID, "nlu-analyze-textbox-1")
            textarea.clear()
            textarea.send_keys(link)
            time.sleep(round(random.uniform(1, 2), 2))
            button = driver.find_element(By.CLASS_NAME, "bx--btn--tertiary")
            button.click()
            text = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'results-text'))).text
            if not text:
                print(f"\n Error: {link} \n")
            time.sleep(round(random.uniform(1, 2), 2))
            button = driver.find_element(By.CLASS_NAME, "bx--btn--tertiary")
            button.click()
            time.sleep(round(random.uniform(0.6, 1), 2))
            return text
        except Exception as e:
            print(f"\n Exception: {e} \n Error: {link} \n")
            reset_driver()
            return None

    length = len(df)

    for i, row in df.iterrows():
        text=""
        flag=0
        link = row["link"]
        try:
            text = get_from_goose(link)
            if text:
                df.at[i, "text"] = text
            else:
                text = get_from_newspaper(link)
                if text:
                    df.at[i, "text"] = text
                else:
                    flag = 1
                    text = get_from_ibm(link)
                    if text:
                        df.at[i, "text"] = text
        except Exception as error:
            if "404" or "500" in str(error):
                length-=1
                print(length)
                errors_404.append(link)
                continue
            if flag==0:
                try:
                    text = get_from_newspaper(link)
                    if text:
                        df.at[i, "text"] = text
                    else:
                        text = get_from_ibm(link)
                        if text:
                            df.at[i, "text"] = text
                except:
                    text = get_from_ibm(link)
                    if text:
                        df.at[i, "text"] = text
            else:
                text = get_from_ibm(link)
                if text:
                    df.at[i, "text"] = text

        length-=1
        print(length)

    driver.quit()

    df.to_csv(path_to_csv)

if __name__ == "__main__":
    get_text(path_to_csv)


# Enter your code here. Read input from STDIN. Print output to STDOUT

# read input
_ = input()
setA = set(input().split())
_ = input()
setB = set(input().split())

# itersections the two sets
intersectAB = setA.intersection(setB)

# print
print(len(intersectAB))


from os import walk
from os.path import join
import time

class Monitor():
  def __init__(self):
    self.fund_settings = None
    self.funds = []
    self.APIs = []

  def set_fund_settings(self, fund_settings):
    self.fund_settings = fund_settings
    self.parse_fund_settings()

  def parse_fund_settings(self):
    def get_files_in_path(path):
      paths = []
      for root, dirs, files in walk(path):
        for file in files:
          full_path = join(root, file)
          paths.append(full_path)
      return paths

    for fund_name, settings in self.fund_settings.items():
      fund = {
        "name": fund_name,
        "system_id": settings['system_id'],
        'api_key': settings['api_key'],
        "targets": {},
      }
      for target_name, target_settings in settings['targets'].items():
        target_settings['signal_files'] = get_files_in_path(target_settings['path'])
        fund['targets'][target_name] = target_settings
        target_settings['position'] = 0
      self.funds.append(fund)

      print("Init fund: " + fund_name)
      print("\tTrading target: " + target_name)
      print("\tSignal_files:")
      for signal_file in target_settings['signal_files']:
        print("\t\t"+signal_file)

  def append_api(self, api):
    self.APIs.append(api)

  def start(self):
    print("Start tracking...")
    while(1):
      self.track_files()
      time.sleep(0.001)

  def track_files(self):
    for fund in self.funds:
      try:
        total_position = 0
        system_id = fund['system_id']
        api_key = fund['api_key']
        update = False

        for symbol, target in fund['targets'].items():
          for signal_file in target['signal_files']:
            with open(signal_file) as fd:
              # Only one line in the signal output file
              line = fd.readline()
              position = int(line.split(',')[1])
              total_position += position
          if target['position'] != total_position:
            update = True

        if update:
          for api in self.APIs:
            api.reset()
            for symbol, target in fund['targets'].items():
              api.set_positions(symbol, target['symbol_type'], total_position)
            api.commit_positions(system_id, api_key)
          target['position'] = total_position
      except:
        pass

if __name__ == "__main__":
  from config import *
  from api.C2 import C2
  c2 = C2(c2_config)
  monitor = Monitor()
  monitor.append_api(c2)
  monitor.set_fund_settings(fund_settings)
  monitor.start()