# PWNZ Copyright 2021 By Zan4eg#5557
# Импорты библиотек

import discord
import random
from discord.ext import commands
import asyncio
import socket
import smtplib
import datetime
import pyowm
import json
from datetime import timedelta
import os
from Cybernator import Paginator as pag
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import json, socket, threading, time, concurrent.futures
from six.moves import urllib
from random import choice
import string
import requests
import pyshorteners

PREFIX = '.' # Переменная префикса

Bot = commands.Bot( command_prefix = PREFIX ) # Установка префикса бота
@Bot.remove_command('help') #Удаление стандартной комманды help

def get_random_string(length):
	letters = string.ascii_letters + string.digits
	result_str = ''.join(random.choice(letters) for i in range(length))
	return result_str

# При загрузке бота
@Bot.event
async def on_ready():
    activity = discord.Game(name = "PWNZ | .help", url='https://twitch.com/zan4egpayne')
    await Bot.change_presence( status = discord.Status.online, activity = activity )
    print("Logged in as PWNZ!")
    print("PWNZ Copyright 2021 By Zan4eg#5557")
    print("Бот запущен и готов к работе!")
    while True:
        await asyncio.sleep(8)
        await Bot.change_presence( status = discord.Status.online, activity = discord.Game(name = ".help | PWNZ") )
        await asyncio.sleep(8)
        await Bot.change_presence( status = discord.Status.online, activity = discord.Game(name = "Бот создан специально для сервера PWNZ'a") )
        await asyncio.sleep(8)
        await Bot.change_presence( status = discord.Status.online, activity = discord.Game(name = "Бот создан Zan4eg#5557") )

@Bot.event
async def on_command_error(ctx, error):
	if isinstance(error, commands.CommandNotFound):
		await ctx.send(embed = discord.Embed(description = f'{ctx.author.name}, команда не найдена!', colour = discord.Color.red()))

# Идеи
@Bot.command()
async def suggest( ctx, *, suggest = None):
    if suggest is None:
        await ctx.send(":x: | Вы не указали идею!")

    else:
        channel = Bot.get_channel(870959982185680946)
        emb=discord.Embed( title = '', colour= 0x04ff00 )
        emb.set_author(name=ctx.author.name + "#" + ctx.author.discriminator + ", отправил своё улучшение!", icon_url = ctx.author.avatar_url)
        emb.add_field( name = 'Текст идеи:', value = '** ```{}``` **'.format( suggest ) )
        emb.set_footer(text= "© PWNZ Bot 💚 | Идеи")
        emb.timestamp = datetime.datetime.utcnow()
        message = await channel.send(embed=emb)
        await message.add_reaction('✅')
        await message.add_reaction('❌')
        await ctx.message.delete()
        await ctx.send("Вы успешно отправили свою идею!")

# Закрытие канала
@Bot.command( pass_context=True )
async def close(ctx):
    role = discord.utils.get(ctx.guild.roles, id=870959126929358858)
    if role not in ctx.author.roles:
        await ctx.send(":x: | Вы не состоите в **Support Team**!")
    else:
        if ctx.channel.category.id == 870960366388129842 and ctx.channel.name != 'обращение-close':
            member = await Bot.fetch_user(ctx.channel.name)
            await ctx.channel.set_permissions(member, send_messages=False,read_messages=True)
            await ctx.send(f"{member.mention}, **саппорт {ctx.author.mention} установил вашему обращению статус: 'Закрыто'**")
            message = await ctx.send(f"Поставьте пожалуйста свою оценку саппорту!")
            await message.add_reaction('👍')
            await message.add_reaction('👎')
            cursor.execute(f"INSERT INTO reactions VALUES ('{message.channel.id}', '{message.id}', '{ctx.message.author.id}')")
            connection.commit()
            mss = await ctx.send("Идёт закрытие канала! Пожалуйста, подождите")
            await ctx.channel.edit(name="обращение-close")
            await ctx.channel.set_permissions(role, send_messages=False,read_messages=True)
            await ctx.message.delete()
            #await Bot.get_channel(829756582581370890).send(ctx.message.author.mention+" +1 балл (" +ctx.channel.mention+")")
            await mss.delete()
        else:
        	await ctx.send(":x: | Этот канал не является каналом поддержки")

# Поддержка
@Bot.event
async def on_message(message):
    await Bot.process_commands(message)
    channel = message.channel
    support_channel = Bot.get_channel(870960257613066251)
    isBot = message.author.bot
    if(message.author.bot): return
    if(channel != support_channel): return
    await message.delete()
    guild = message.guild
    channel2 = await guild.get_channel(870960366388129842).create_text_channel(message.author.id)
    await channel2.set_permissions(message.author, read_messages=True,send_messages=True)
    emb=discord.Embed( title = '', colour= 0x04ff00 )
    emb.set_author(name="Обращение к команде поддержки")
    emb.set_footer(text=f"{message.author.display_name}", icon_url = message.author.avatar_url)
    emb.add_field( name = 'Суть обращения:', value = '{}'.format(message.content) )
    await channel2.send(message.author.mention+", **`для команды поддержки`** <@&870959126929358858>")
    await channel2.send(embed=emb)
    #message2 = await channel.send(message.author.mention + ", вы успешно оставили своё обращение! Перейдите в канал " + channel2.mention + " для просмотра ответа.")
    await asyncio.sleep(5)
    #await message2.delete()

# Эмбед ТП
@Bot.command( pass_context=True )
async def embed(ctx):
    if ctx.message.author.guild_permissions.administrator:
        emb1 = discord.Embed( title="Тех.Поддержка сервера PWNZ", colour=0xff8c00 ) # Создаем ембед
        emb1.add_field( name='Правила подачи обращения', value='```1) Запрещено отправлять оскорбительные сообщения\n2) Запрещено отправлять сообщения с непристойным материалом\n3) Запрещено создавать обращения без причины```', inline=False)
        emb1.add_field( name='Всего', value='`обработанных обращений:` ?', inline=True)
        emb1.add_field( name='Всего', value='`обращений на рассмотрении:` ?', inline=True)
        emb1.add_field( name='Всего', value='`закрытых обращений:` ?', inline=True)
        emb1.set_footer(text= "© PWNZ Support | Тех.Поддержка")
        emb1.set_image( url='https://images-ext-2.discordapp.net/external/cQxwDjOv26SB4UNSoL58YRtmhJFOediiOfT8tVSqAGw/https/images-ext-2.discordapp.net/external/uxj2OXVnN-UuIlbnrx9bTD7aYuLJoUmSC8uInIL9b9Q/https/images-ext-2.discordapp.net/external/RoNgImbrFiwy16IZVStGaUy4ZZrJPSuVcRN1r7l-SQY/https/imgur.com/LKDbJeM.gif' )
        emb1.timestamp = datetime.datetime.utcnow()
        await ctx.send( embed = emb1)
        await ctx.message.delete()
    else:
        await ctx.send(":x: | У вас нет прав!")

# Информация о пользователе
@Bot.command( pass_context=True )
async def info(ctx, user: discord.Member = None):
    if user is None:
        await ctx.send(f"**{ctx.author}**, укажите участника сервера \n Пример команды: ***.info `@Пользователь`***")
    else:
        emb = discord.Embed( title="Информация об {}".format(user.name), colour=0x04ff00 ) # Создаем ембед
        emb.add_field( name='Имя', value=user.name ) # Получаем имя пользователя
        emb.add_field( name='Присоединился', value=user.joined_at ) # Получаем присоедение пользователя
        emb.add_field( name='Айди', value=user.id ) # Получаем айди пользователя
        emb.set_thumbnail( url=user.avatar_url ) # Получаем аватар пользователя
        emb.set_footer(text= "Упомянули: {}".format(user.name), icon_url= user.avatar_url) # Получаем имя пользователя и его аву (опять же)
        await ctx.send( embed = emb )

# Информация о создателе
@Bot.command( pass_context=True )
async def botcreater(ctx):
    emb = discord.Embed( title="Дискорд **Zan4eg#5557**", colour=0x04ff00 )  # Создаем ембед
    emb.set_footer(text= "Бот создан: 15.05.2021") # Получаем имя пользователя и его аву (опять же)
    await ctx.send( embed = emb )

# Очистка сообщений
@Bot.command( pass_context = True )
@commands.has_permissions( administrator = True ) # Установка нужных прав для комманды
async def clear ( ctx, amount : int = None):  # Создание комманды/функции
    if amount is None:
        await ctx.send(f"**{ctx.author}**, укажите количество сообщений для удаления \n Пример команды: ***.clear `кол-во сообщений`***")
    else:
        await ctx.channel.purge( limit = amount ) # Сама очистка
        emb = discord.Embed( description=f'✅  Очищено {amount} сообщений!', colour=0x04ff00 ) # Создание отчета об очистке
        await ctx.send( embed = emb )

# Информация о сервере
@Bot.command()
async def serverinfo(ctx):
    # Задаем переменные
    channels = len(ctx.guild.channels)
    text_channels = len(ctx.guild.text_channels)
    voice_channels = len(ctx.guild.voice_channels)
    categories = len(ctx.guild.categories)
    members = len(ctx.guild.members)
    embed = discord.Embed(title = 'Информация о сервере:', description = f'Название сервера: `{ctx.guild.name}`\nАйди сервера: `{ctx.guild.id}`\nВсего участников: `{members}`\nВсего каналов и категорий: `{channels}`\nТекстовые каналы: `{text_channels}`\nГолосовые каналы: `{voice_channels}`\nКатегорий: `{categories}`', colour= 0x04ff00)
    await ctx.send( embed=embed )

# Статистика каналов
@Bot.command()
async def stat(ctx, channel: discord.TextChannel = None):
    if not channel: #проверяем ввели ли канал
        channel = ctx.channel
        text = 'в данном канале'
    else:
        text = f'в #{channel.name}'
    await ctx.send(f"{ctx.author.mention}, я начинаю вычисления, подождите немного...") #отправляем сообщение о начале отсчёта
    counter = 0
    yesterday = datetime.datetime.today() - timedelta(days = 1)
    #начинаем считать сообщения
    async for message in channel.history(limit=None, after=yesterday):
        counter += 1
    counter2 = 0
    weekago = datetime.datetime.today() - timedelta(weeks = 1)
    async for message in channel.history(limit=None, after=weekago):
        counter2 += 1
    counter3 = 0
    monthago = datetime.datetime.today() - timedelta(weeks = 4)
    async for message in channel.history(limit=None, after=monthago):
        counter3 += 1
    embed = discord.Embed(title = f'Статиститка сообщений {text}', colour= 0x04ff00) #создаём embed-сообщение о подсчётах
    embed.add_field(name = 'За сегодня', value = f'{counter}', inline = False) #добавляем поле "За сегодня"
    embed.add_field(name = 'За неделю', value = f'{counter2}', inline = False) #добавляем поле "За неделю"
    embed.add_field(name = 'За месяц', value = f'{counter3}', inline = False) #добавляем поле "За месяц"
    await ctx.send( f'{ctx.author.mention}', embed = embed ) #вывод сообщения с информацией о подсчётах

# Генератор паролей
lenght = int( '20' )
chars = '+-/*$#?=@<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
@Bot.command()
async def passgen( ctx ):
    password = ''
    for i in range( lenght ):
        password += random.choice( chars )
    emb = discord.Embed( description=f'✅ Ваш пароль был сгенерирован и отправлен вам в лс!', colour=0x04ff00 )
    em = discord.Embed( description=f'✅ Ваш пароль: {password}\n⚠ Никому не показывайте этот пароль!', colour=0x04ff00 )
    await ctx.send( embed = emb )
    await ctx.author.send( embed = em )

@Bot.command()
async def randcolor(ctx):
	await ctx.message.delete()
	random_number = random.randint(0,16777215)
	hex_number = str(hex(random_number))
	hex_number ='#'+ hex_number[2:]
	em = discord.Embed(title="Random Color Hex", description = f'Hex color: {hex_number}', color=random_number)
	await ctx.send(embed = em)

@commands.cooldown(1, 5, commands.BucketType.user)
@Bot.command()
async def ping(ctx):
    em = discord.Embed(title="".format(ctx.guild.name), description="", color=0x04ff00)
    em.set_author(name="")
    em.add_field(name="Ping", value='Понг! :ping_pong:', inline=True)
    em.add_field(name="MS", value=f'Пинг бота: **{ctx.bot.latency * 1000:,.2f}ms**', inline=True)
    await ctx.send(embed=em)

@Bot.command()
async def slot(ctx):
    emojis = "🍎🍊🍐🍋🍉🍇🍓🍒"
    a = random.choice(emojis)
    b = random.choice(emojis)
    c = random.choice(emojis)
    slotmachine = f"**[ {a} {b} {c} ]\n{ctx.author.name}**,"
    if (a == b == c):
        await ctx.send(embed=discord.Embed.from_dict({"title":"Казино", "description":f"{slotmachine} Все совпадения, вы выиграли!"}))
    elif (a == b) or (a == c) or (b == c):
        await ctx.send(embed=discord.Embed.from_dict({"title":"Казино", "description":f"{slotmachine} 2 подряд вы выиграли!"}))
    else:
        await ctx.send(embed=discord.Embed.from_dict({"title":"Казино", "description":f"{slotmachine} Нет совпадения, вы проиграли"}))

@Bot.command()
async def userpic(ctx, *, avamember: discord.Member):
    emb = discord.Embed(title = f"Аватар {avamember.name}", colour = 0x04ff00)
    emb.set_image(url = avamember.avatar_url)
    await ctx.send(embed = emb)

@Bot.command()
async def botinfo(ctx):
    guilds = await Bot.fetch_guilds(limit = None).flatten()
    emb = discord.Embed(title = "Статистика", colour = 0x04ff00)
    emb.add_field(name = "Основная:", value = f"Серверов: **{len(guilds)}**\nУчастников: **{len(set(Bot.get_all_members()))}**")    # 1: Количество серверов, 2: количество уникальных участников на всех серверах
    emb.add_field(name = "Бот:", value = f"Задержка: **{int(Bot.latency * 1000)} мс**") # Скорость соединения бота с API дискорда
    await ctx.send(embed = emb)

@Bot.command()
async def tinyurl(ctx, url : str = None):
    if url is None:
        await ctx.send(embed = discord.Embed(
                title = "Укоротитель ссылок",
                description = "Ошибка | Укажите ссылку которую хотите укоротить",
                colour = 0x04ff00
            ))
    else:
        shortener = pyshorteners.Shortener()
        short_url = shortener.tinyurl.short(url)
        await ctx.send("Ваша ссылка готова : " + short_url)

# Навигация по командам
@Bot.command( pass_context = True )
async def help( ctx, amount = 1 ):

    emb1=discord.Embed( title = 'Навигация по командам :pushpin:', colour= 0x04ff00 )
    emb1.set_thumbnail( url='https://cdn.discordapp.com/attachments/724886353065803778/766295278927216670/e52a182a29690cf9.png' )
    emb1.add_field( name = '``{}info``'.format( PREFIX ), value = 'Информация об пользователе.' )
    emb1.add_field( name = '``{}botcreater``'.format( PREFIX ), value = 'Создатель бота.' )
    emb1.add_field( name = '``{}stat``'.format( PREFIX ), value = 'Стистика каналов.' )
    emb1.add_field( name = '``{}serverinfo``'.format( PREFIX ), value = 'Информация о сервере.' )
    emb1.add_field( name = '``{}ping``'.format( PREFIX ), value= 'Узнать задержку бота.' )
    emb1.add_field( name = '``{}userpic``'.format( PREFIX ), value= 'Узнать аватар пользователя.' )
    emb1.add_field( name = '``{}botinfo``'.format( PREFIX ), value= 'Узнать статистику бота.' )
    emb1.add_field( name = '``{}tinyurl``'.format( PREFIX ), value= 'Укоротить ссылку.' )
    emb2=discord.Embed( title = 'Навигация по командам :pushpin:', colour= 0x04ff00 )
    emb2.set_thumbnail( url='https://cdn.discordapp.com/attachments/724886353065803778/766295281406181437/1abb1364301a30c7.png' )
    emb2.add_field( name = '``{}clear``'.format( PREFIX ), value = 'Очистка чата.' )
    emb2.add_field( name = '``{}suggest``'.format( PREFIX ), value = 'Предложить идею серверу.' )
    emb3=discord.Embed( title = 'Навигация по командам :pushpin:', colour= 0x04ff00 )
    emb3.set_thumbnail( url='https://cdn.discordapp.com/attachments/724886353065803778/766295277001768990/e2104f40da530197.png' )
    emb3.add_field( name = '``{}passgen``'.format( PREFIX ), value= 'Сгенерировать сложный пароль.' )
    emb3.add_field( name = '``{}slot``'.format( PREFIX ), value= 'Казино.' )
    emb3.add_field( name = '``{}randcolor``'.format( PREFIX ), value= 'Рандомный цвет.' )

    embeds = [emb1, emb2, emb3]

    message = await ctx.send(embed=emb1)
    page = pag(Bot, message, only=ctx.author, use_more=False, embeds=embeds)
    await page.start()

token = os.environ.get('BOT_TOKEN')
Bot.run( str(token) )


class Pedido (object):
    persona = None
    hora = None
    plato = None
    entrega = None
    fecha = None

    def set (self, p, h, pl, e, f):
        self.persona = p
        self.hora = h
        self.plato = pl
        self.entrega = e
        self.fecha = f


__author__ = 'alfaro'

from django.conf.urls import patterns, url
from .views import tipo_create, tipo_list, atributo_list, atributo_create, tipo_delete, atributo_delete, tipo_update, item_create, item_list

""" fases/urls
Se establecen los urls asociados a fases
"""

urlpatterns = patterns('',
                       url(r'^(?P<pk>\d+)$', tipo_list, name='lista_tipo'),
                       url(r'^nuevo/(?P<pk>\d+)$', tipo_create, name='nuevo_tipo'),
                       url(r'^atributos/(?P<pk>\d+)$', atributo_list, name='lista_atributo'),
                       url('^atributos/nuevo/(?P<pk>\d+)$', atributo_create, name='nuevo_atributo'),
                       url(r'^borrar/(?P<pk>\d+)$', tipo_delete, name='borrar_tipo'),
                       url('^atributos/borrar/(?P<pk>\d+)$', atributo_delete, name='borrar_atributo'),
                       url(r'^editar/(?P<pk>\d+)$', tipo_update, name='editar_tipo'),
                       url(r'^items/(?P<pk>\d+)$', item_list, name='lista_item'),
                       url(r'^items/nuevo/(?P<pk>\d+)$', item_create, name='nuevo_item'),

)


"""Commandline Interface for Testing Purposes
Created on Fall 2019
CS108 Project
@author: Jiho Kim (jk249)
"""
import newton

nt = newton.Newton()

expr = input("Please enter f(x)= ")
init = float(input("Please enter initial guess (x0): "))
num_ = int(input("Please enter number of iterations: "))

lt = nt.newton(expr, init, num_)

print("")
print("Iteration 0 |", init)

for i in range(len(lt)):
    print("Iteration " + str(i + 1) + " |", lt[i])


# -*- coding: utf-8 -*-
"""
Created on Fri Dec 20 11:39:23 2019

@author: jacky
"""

from keras import layers
from keras import models

def down_block(x,filters,kernel_size=(3,3),strides=(1,1)):
    x1 = layers.Conv2D(filters,
               kernel_size=kernel_size,
               strides=strides,
               padding='same', # same or valid
               # activation=None,
               # use_bias=True,
               kernel_initializer='glorot_uniform',
               bias_initializer='zeros',
               kernel_regularizer=None,
               bias_regularizer=None,
               activity_regularizer=None)(x)
    x = layers.BatchNormalization(axis=-1)(x1)
    x3 = layers.Activation('relu')(x)
    return layers.add([x1,x3])

def up_block(x,filters,kernel_size=(3,3),strides=(2,2)):
    x1 = layers.Conv2DTranspose(filters,
                                kernel_size=kernel_size,
                                strides=strides,
                                padding='same',
                                # use_bias=True,
                                kernel_initializer='glorot_uniform',
                                bias_initializer='zeros',
                                kernel_regularizer=None,
                                bias_regularizer=None,
                                activity_regularizer=None)(x)
    x = layers.BatchNormalization(axis=-1)(x1)
    x3 = layers.Activation('relu')(x)
    return layers.add([x1,x3])

def SRRNN():
    x = layers.Input(shape=(256,256,1))
    d1 = down_block(x,16)
    p1 = layers.MaxPooling2D(pool_size=(2, 2))(d1)

    d2 = down_block(p1,32)
    d2 = down_block(d2,32)
    p2 = layers.MaxPooling2D(pool_size=(2, 2))(d2)

    d3 = down_block(p2,48)
    d3 = down_block(d3,48)
    p3 = layers.MaxPooling2D(pool_size=(2, 2))(d3)

    d4 = down_block(p3,64)
    d4 = down_block(d4,64)
    p4 = layers.MaxPooling2D(pool_size=(2, 2))(d4)

    d5 = down_block(p4,96)
    d5 = down_block(d5,96)
    p5 = layers.MaxPooling2D(pool_size=(2, 2))(d5)

    d6 = down_block(p5,128)
    p6 = layers.MaxPooling2D(pool_size=(2, 2))(d6)

    d7 = layers.add([up_block(p6,128),d6])
    d8 = layers.add([up_block(d7,96),d5])
    d9 = layers.add([up_block(d8,64),d4])
    d10 = layers.add([up_block(d9,48),d3])
    d11 = layers.add([up_block(d10,32),d2])
    d12 = layers.add([up_block(d11,16),d1])

    y = layers.Conv2D(1, kernel_size=(3,3), strides=(1,1), padding='same',
                      kernel_initializer='glorot_uniform', bias_initializer='zeros')(d12)

    model = models.Model(inputs=x, outputs=y)
    model.summary()
    return model


import json
import logging
from os import environ

from google.oauth2.service_account import Credentials

logging.basicConfig(
    level=logging.INFO, format="%(name)s - %(asctime)s - %(levelname)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S"
)

def get_crendentials(google_credentials):
    scopes = (
        "https://www.googleapis.com/auth/bigquery",
        "https://www.googleapis.com/auth/cloud-platform",
        "https://www.googleapis.com/auth/drive",
    )

    google_credentials = json.loads(google_credentials)
    credentials = Credentials.from_service_account_info(google_credentials)
    credentials = credentials.with_scopes(scopes)
    return credentials

mode = environ.get("MODE", "staging")
bigquery_project_id = "restricted"
google_credentials = get_crendentials(environ["GOOGLE_CREDENTIALS"])
dataset = "workable"
subdomain = environ.get("SUBDOMAIN")
private_key = environ.get("PRIVATE_KEY")


import timing
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

import re #for data parsing

##########Functions##########

count = 0
def counter(inc, printInc):
	global count
	if printInc == 0:
		count = 0
		#print(count)
	else:
		count += inc
		#if count%(printInc) == 0:
			#print(count)
	return count

def parse_log_data(raw_log):
	data = raw_log.split("\n")
	#if data[0].split(" ")[0] == "DAILY": #section header
		#date = "*" + data[1]
	if len(data) != 4:
		date, time, desc, loc = "*", "x", "x", "x"
	else:
		date, time = parse_date_time(data[2])
		desc, loc = parse_description(data[0])
	return [date, time, desc, loc]

def parse_date_time(datetime):
	#data = datetime.split(" ")
	data = re.split("[\s]+", datetime)
	#print(data)
	#date, time = "x", "x"
	date = data[2].strip()
	time = data[1].strip()
	return [date, time] #date, time

def parse_description(rawdata):
	data = re.split("[\–\-]", rawdata)
	#data = re.split("[^a-zA-Z0-9'\s]+", rawdata) #alphanumeric
	#print(data)
	if len(data) <= 1: return ["x", "x"]

	desc = data[0].strip()
	loc = find_address(data)
	return [desc, loc] #desc, location

def find_address(datalist):
	locdata = ""
	for string in datalist:
		string = string.strip()
		if re.match(r".*[0-9]$", string) != None:
			locdata = string
			#print(locdata)
			break
	if locdata == "": return "X"

	locdata = re.split("\s{2,}", string)
	loc = locdata[0].strip()
	return loc

##########Code##########

driver = webdriver.Chrome()

month = "February"
year = "2018"

raw_data_filename = month + "-" + year + "-raw.txt"
parsed_data_filename = month + "-" + year + "-parsed.txt"

raw_data_file = open(str(raw_data_filename), "w")
parsed_data_file = open(str(parsed_data_filename), "w")

raw_data_file.write("Crime Log: "  + month + " " + year + "\n")
#parsed_data_file.write("Crime Log: "  + month + " " + year + "\n")
parsed_data_file.write("Date\tTime\tLocation\tDescription\n")

driver.get("http://uvapolice.virginia.edu/crime-log-" + month + "-" + year)

try:
	section = driver.find_element_by_class_name("field-type-text-with-summary")
	scraped_logs = section.find_elements_by_xpath(".//div/div/div/p")

	#Print all logs for month in year
	print("Printing logs for " + month + " " + year)
	counter(0, 0) #Testing
	for log in scraped_logs:
		raw_log = log.text
		raw_data_file.write(raw_log + "\n\n")

		data = parse_log_data(raw_log)
		date, time, desc, loc = data
		if date[0] != "*": #* indicating day header log entry
			parsed_data_file.write(date+"\t" + time+"\t" + loc+"\t" +desc+"\t" + "\n")
			counter(1, 1) #Testing

	print("Total number of logs: " + str(count))

except NoSuchElementException:
	print("No logs found for " + month + " " + year)
	raw_data_file.write("No logs found for " + month + " " + year)

raw_data_file.close()
parsed_data_file.close()

driver.close()


from breadth_first_search import BreadthFirstSearch
from breadth_first_search import myDictionary

class DepthFirstSearch(BreadthFirstSearch):
    def __init__(self,dictionary,limit = False):
        BreadthFirstSearch.__init__(self,dictionary,FIFO = False,limit = limit)

if __name__ == "__main__":
    newSearch = DepthFirstSearch(myDictionary)
    print(newSearch.findRoute("Iasi","Bucharest"))


from django.contrib.auth import logout
from django.shortcuts import render, redirect, get_object_or_404

from Boutique.models import Articulo

# Create your views here.

def index(request):
    return render(request, "Boutique/index.html")

def login(request):
    return render(request, "Boutique/login.html")

def logOut(request):
    logout(request)
    return redirect('/')

def lista_articulos(request, categoria=None):
    if (categoria):
        if (categoria == 'Liquidacion'):
            articulos = Articulo.objects.filter(es_liquidacion=True)
        else:
            articulos = Articulo.objects.filter(segmento__tipo=categoria)
    else:
        articulos = Articulo.objects.all()
        categoria = 'Todos los articulos'
    return render(request, 'Boutique/articulos.html', {'articulos': articulos, 'categoria': categoria})

def detalle_articulo(request, idarticulo):
    articulo = get_object_or_404(Articulo, pk=idarticulo)
    articulo.contador_visitas += 1
    articulo.save()
    recientes = Articulo.objects.order_by('-fecha_agregado')[0:3]
    vistos = Articulo.objects.order_by('-contador_visitas')[0:3]
    return render(request, 'Boutique/detalle_articulo.html',
                  {'articulo': articulo, 'recientes': recientes, 'vistos': vistos})


# -*- coding: utf-8 -*-
from __future__ import print_function, absolute_import, division
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
import os.path

def peak_finder(self, spec, cwt_range=[1, 3], rc_range=[4, 4]):
    """
    Deprecated peak_finder
    """
    self.hipk_freq = spec.freq
    self.hipk_val = spec.val
    self.hipk = peaks.fp(spec.val, self.threshold, cwt_range, rc_range)

def procfiles(files_to_proc, threshold=-15.0, cwt_range=[1, 7], rc_range=[4, 4], f_range=[20.0, 300.0],
              title_prefix='', dir=None, saveit=False, yrange='minmax'):
    fmin = f_range[0]
    fmax = f_range[1]
    if isinstance(files_to_proc, six.string_types):
        files_to_proc = [files_to_proc]
    for fn in files_to_proc:
        fno = fn
        if dir is not None:
            fno = os.path.join(dir, fn)
        a = np.loadtxt(fno)
        xs = a[:, 0]
        ys = a[:, 1]
        xlimg = np.where(xs > fmin)[0]
        xlim = xlimg[np.where(xs[xlimg] < fmax)]
        x = xs[xlim]
        y = ys[xlim]
        hipk = fp(y, threshold, cwt_range, rc_range)

        plt.figure(fn)
        plt.xlabel('MHz')
        plt.ylabel('dBm')
        plot_title = title_prefix + os.path.basename(fn).split('.')[0]
        plt.title(plot_title)
        # plt.plot(xs[pkin], ys[pkin], 'o')
        plt.plot([fmin, fmin], [min(y), max(y)], 'b--')
        plt.plot([fmax, fmax], [min(y), max(y)], 'b--')
        plt.plot([fmin, fmax], [threshold, threshold], 'b:')
        plt.plot(x, y)
        if isinstance(yrange, six.string_types):
            ymin = min(y)
            ymax = max(y)
        else:
            ymin = yrange[0]
            ymax = yrange[1]
        plt.axis([fmin, fmax + 75.0, ymin, ymax + 1.0])
        plt.plot(x[hipk], y[hipk], 'kv')
        print(" MHz      dBm")
        print("------   ------")
        plt.text(fmax, ymax - 0.5, ' MHz      dBm')
        plt.text(fmax, ymax - 0.75, '_______   ______')
        yoff = -2.5
        for xp, yp in zip(x[hipk], y[hipk]):
            s = "{:>6.2f}  {:>6.2f}".format(xp, yp)
            yval = ymax + yoff
            yoff -= 1.5
            plt.text(fmax, yval, s)
            print("{:>6.2f}  {:>6.2f}".format(xp, yp))
        if saveit:
            ofn = fn.split('.')[0] + '.png'
            plt.savefig(ofn, dpi=200)

def fp(y, threshold=-15.0, cwt_range=[1, 7], rc_range=[4, 4]):
    pkin = signal.find_peaks_cwt(y, np.arange(cwt_range[0], cwt_range[1]))
    hipk = recenter_and_threshold(y, pkin, [4, 4], threshold)
    return hipk

def recenter_and_threshold(y, pkin, rng, threshold):
    pkout = []
    dirout = []
    for p in pkin:
        dir = None
        newp = p
        for r in range(1, rng[0] + 1):
            if p - r < 0:
                break
            if y[p - r] > y[newp]:
                newp = p - r
                dir = 'left'
            else:
                break
        if dir is None:
            for r in range(1, rng[0] + 1):
                if p + r > len(y) - 1:
                    break
                if y[p + r] > y[newp]:
                    newp = p + r
                    dir = 'right'
                else:
                    break

        if (y[newp] > threshold) and (newp not in pkout):
            pkout.append(newp)
            dirout.append(dir)
    return pkout

def ps(fns, fmin=20.0, fmax=300.0):
    for fn in fns:
        a = np.loadtxt(fn)
        lbl = fn.split('_')[0] + '-' + fn.split('_')[1]
        xs = a[:, 0]
        ys = a[:, 1]

        xlimx = np.where(xs > fmin)[0]
        xlim = xlimx[np.where(xs[xlimx] < fmax)]
        plt.plot(xs[xlim], ys[xlim], label=lbl)

    plt.legend()


"""
Instituto Tecnologico de Costa Rica.
Escuela de Ingenieria en Computacion.
IC-1801 Taller de Programacion.
Profesor: Mauricio Aviles.
I Proyecto Programado.
Cifrados.

Jose Andres Garcia Saenz.
Carne 201242273.
"""

import pyperclip
try:
    from Tkinter import *
except:
    from tkinter import *

def cesar(texto,desp,cod):
    if desp in Desp:
        desp=int(desp)
        if cod==True:
            x = ""
            for i in texto:
                if i in Crip:
                    x += Descrip[(Crip[i]+desp)%26]
                else:
                    x += i
            return x
        else:
            x = ""
            for i in texto:
                if i in Crip:
                    x += Descrip[(Crip[i]-desp)%26]
                else:
                    x += i
            return x.capitalize()
    else:
        return "Error: Clave invalida. Ingrese solamente numeros enteros entre 0 y 25."

def transposicion(texto):
    x=""
    texto = texto.split()
    for i in texto:
        if i[-1] not in Crip:
            x += i[0:-1][::-1] + i[-1] + " "
        else:
            x += i[::-1] + " "
    return x.strip()

def binario(texto,cod):
    if digitos(texto,cod)==True:
        x = ""
        if cod==True:
            for i in texto:
                if i==" ":
                    x += "* "
                else:
                    x += BinCrip[i] + " "
            return x.strip()

        else:
            texto = texto.split()
            for i in texto:
                if i == "*":
                    x += " "
                else:
                    x += BinDescrip[i]
            return x.strip().capitalize()
    else:
        return "Error: Ingrese solamente digitos validos: A-Z , a-z"

def vigenere(texto,clave,cod):
    if len(clave.split())==1:
        listatexto = texto.split()
        palabra = ""
        frase = ""
        cont = 0
        if cod==True:
            for i in listatexto:
                for x in i:
                    if x not in Crip:
                        palabra += x
                    elif cont<len(clave):
                        palabra += Descrip[(Crip[x]+Crip[clave[cont]])%26]
                        cont += 1
                    else:
                        cont = 0
                        palabra += Descrip[(Crip[x]+Crip[clave[cont]])%26]
                frase += palabra + " "
                palabra = ""
                cont=0
            return frase.strip()
        else:
            for i in listatexto:
                for x in i:
                    if x not in Crip:
                        palabra += x
                    elif cont<len(clave):
                        palabra += Descrip[(Crip[x]-Crip[clave[cont]])%26]
                        cont += 1
                    else:
                        cont = 0
                        palabra += Descrip[(Crip[x]-Crip[clave[cont]])%26]
                frase += palabra + " "
                palabra = ""
                cont=0
            return frase.strip().capitalize()
    else:
        return "Error: Clave invalida. Ingrese una unica palabra."

def digitos(texto,cod):
    if cod==True:
        for i in texto:
            if i not in BinCrip:
                return False
        return True
    else:
        for i in texto:
            if i not in BinDigitos:
                return False
        return True

Crip={"A":0 , "B":1 , "C":2 , "D":3 , "E":4 , "F":5 , "G":6 , "H":7 , "I":8 , "J":9 , "K":10 , "L":11 , "M":12 ,
      "N":13 , "O":14 , "P":15 , "Q":16 , "R":17 , "S":18 , "T":19 , "U":20 , "V":21 , "W":22 , "X":23 , "Y":24 , "Z":25 ,
      "a":0 , "b":1 , "c":2 , "d":3 , "e":4 , "f":5 , "g":6 , "h":7 , "i":8 , "j":9 , "k":10 , "l":11 , "m":12 ,
      "n":13 , "o":14 , "p":15 , "q":16 , "r":17 , "s":18 , "t":19 , "u":20 , "v":21 , "w":22 , "x":23 , "y":24 , "z":25 }

Descrip={0:"A" , 1:"B" , 2:"C" , 3:"D" , 4:"E" , 5:"F" , 6:"G" , 7:"H" , 8:"I" , 9:"J" , 10:"K" , 11:"L" , 12:"M" ,
         13:"N" , 14:"O" , 15:"P" , 16:"Q" , 17:"R" , 18:"S" , 19:"T" , 20:"U" , 21:"V" , 22:"W" , 23:"X" , 24:"Y" , 25:"Z"}

Desp=("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25")

BinCrip={"A":"00000" , "B":"00001" , "C":"00010" , "D":"00011" , "E":"00100" , "F":"00101" , "G":"00110" , "H":"00111" , "I":"01000" ,
         "J":"01001" , "K":"01010" , "L":"01011" , "M":"01100" , "N":"01101" , "O":"01110" , "P":"01111" , "Q":"10000" , "R":"10001" ,
         "S":"10010" , "T":"10011" , "U":"10100" , "V":"10101" , "W":"10110" , "X":"10111" , "Y":"11000" , "Z":"11001" ,
         "a":"00000" , "b":"00001" , "c":"00010" , "d":"00011" , "e":"00100" , "f":"00101" , "g":"00110" , "h":"00111" , "i":"01000" ,
         "j":"01001" , "k":"01010" , "l":"01011" , "m":"01100" , "n":"01101" , "o":"01110" , "p":"01111" , "q":"10000" , "r":"10001" ,
         "s":"10010" , "t":"10011" , "u":"10100" , "v":"10101" , "w":"10110" , "x":"10111" , "y":"11000" , "z":"11001" , " ":" " }

BinDescrip={"00000":"A" , "00001":"B" , "00010":"C" , "00011":"D" , "00100":"E" , "00101":"F" , "00110":"G" , "00111":"H" , "01000":"I" ,
            "01001":"J" , "01010":"K" , "01011":"L" , "01100":"M" , "01101":"N" , "01110":"O" , "01111":"P" , "10000":"Q" , "10001":"R" ,
            "10010":"S" , "10011":"T" , "10100":"U" , "10101":"V" , "10110":"W" , "10111":"X" , "11000":"Y" , "11001":"Z" , " ":"" }

BinDigitos=("0" , "1" , " " , "*")

# GUI #

app = Tk()
app.title("Encriptador")
app.wm_resizable(False, False)

t = Text(app,height=10)
t.grid(row=6,column=1)

T = Text(app,height=10)
T.grid(row=6,column=2)

a = Entry(app)
a.grid(row=3,column=2)

clave = Label(app,text="Clave")
clave.grid(row=4,column=2)

def crip():
    texto = t.get("1.0","end")
    metodo = v.get()
    cod = z.get()
    clave = a.get()
    dic = {0:True,1:False}
    if metodo == 0:
        if clave == "":
            return "*Ingrese el desplazamiento*"
        else:
            return cesar(texto,clave,dic[cod])
    elif metodo == 1:
        return transposicion(texto)
    elif metodo == 2:
        return str(binario(texto.strip(),dic[cod]))
    else:
        if clave == "":
            return "*Ingrese la clave*"
        else:
            return vigenere(texto,str(clave),dic[cod])

def insert():
    T.delete("1.0","end")
    T.insert("1.0",crip().strip())
    if d.get() == "True":
        x = "log_"+(time.ctime()).replace(":","-")+".txt"
        file = open("log.txt","w")
        file.write((time.ctime()).replace(":","-")+"   "+t.get("1.0","end")+crip())

def copy():
    pyperclip.copy(T.get("1.0","end"))

def paste():
    t.delete("1.0","end")
    t.insert("1.0",pyperclip.paste().strip())

def clean():
    T.delete("1.0","end")
    t.delete("1.0","end")
    a.delete(0,"end")

def info():
    acerca = Tk()
    acerca.title("Acerca de")
    l1 = Label(acerca,text="Jose Andres Garcia")
    l1.grid(row=1,column=1)
    l2 = Label(acerca,text="2012")
    l2.grid(row=2,column=1)
    acerca.mainloop()

def active():
    d.set("True")

d = StringVar()

v = IntVar()
v.set(0)

z = IntVar()
z.set(0)

menus = Menu(app)
archivo = Menu(menus,tearoff=0)
menus.add_cascade(label="Archivo",menu=archivo)
archivo.add_command(label="Exit",command=app.destroy)
editar = Menu(menus,tearoff=0)
menus.add_cascade(label="Editar",menu=editar)
editar.add_command(label="Copiar",command=copy)
editar.add_command(label="Pegar",command=paste)
editar.add_command(label="Limpiar",command=clean)
app.config(menu=menus)

b1 = Radiobutton(app, text="Cesar",variable=v, value=0,width=40)
b1.grid(row=1,column=1)
b2 = Radiobutton(app, text="Transposicion",variable=v, value=1,width=40)
b2.grid(row=2,column=1)
b3 = Radiobutton(app, text="Binario",variable=v, value=2,width=40)
b3.grid(row=3,column=1)
b4 = Radiobutton(app, text="Vigenere",variable=v, value=3,width=40)
b4.grid(row=4,column=1)

c1 = Radiobutton(app, text="Encriptar",variable=z, value=0,width=40)
c1.grid(row=1,column=2)
c2 = Radiobutton(app, text="Desencriptar",variable=z, value=1,width=40)
c2.grid(row=2,column=2)

copy=Button(app,text="Copy",command=copy,width=40,heigh=2)
copy.grid(row=7,column=1)

enter=Button(app,text="Enter",command=insert,width=40,heigh=2)
enter.grid(row=7,column=2)

paste=Button(app,text="Paste",command=paste,width=40,heigh=2)
paste.grid(row=8,column=1)

clean=Button(app,text="Clean",command=clean,width=40,heigh=2)
clean.grid(row=8,column=2)

app.mainloop()


import itertools
import time
from pprint import pprint
from statistics import mean

import requests
from igraph import Graph, plot

from config import a_token

def get_friends(user_id, fields=''):
    """ Returns a list of user IDs or detailed information about a user's friends """
    assert isinstance(user_id, int), "user_id must be positive integer"
    assert isinstance(fields, str), "fields must be string"
    assert user_id > 0, "user_id must be positive integer"

    domain = "https://api.vk.com/method"
    access_token = a_token
    v = '5.103'

    query = f"{domain}/friends.get?access_token={access_token}&user_id={user_id}&fields={fields}&v={v}"
    response = requests.get(query).json().get('response', {}).get('items', False)
    return response

def age_predict(user_id):
    assert isinstance(user_id, int), "user_id must be positive integer"
    assert user_id > 0, "user_id must be positive integer"

    users_info = get_friends(user_id, 'bdate')
    if users_info:
        bdates = [int(u['bdate'].split('.')[-1]) for u in users_info
                  if 'bdate' in u.keys() and int(u.get('bdate').split('.')[-1]) > 12]
        return round(2019 - mean(bdates))

def to_numbers(arr):
    index = 0
    vals = dict()
    for item in itertools.chain.from_iterable(arr):
        if item not in vals.keys():
            vals[item] = index
            index += 1
    return vals

def get_network(users_ids, as_edgelist=True):
    """ Building a friend graph for an arbitrary list of users """
    # Создание вершин и ребер
    edges = []
    for user in users_ids:
        friends = get_friends(user)
        if not friends:
            continue
        for friend in friends:
            edges.append((user, friend))
    nums = to_numbers(edges)
    edges = [(nums[a], nums[b]) for a, b in edges]
    ids = list(nums.keys())
    matrix = [[0 for _ in range(len(ids))] for _ in range(len(ids))]
    for i, j in edges:
        matrix[i][j] = 1
        matrix[j][i] = 1
    g = Graph(vertex_attrs={"label": ids}, edges=edges, directed=False)
    # Задаем стиль отображения графа
    visual_style = {
        "layout": g.layout_fruchterman_reingold()
    }
    g.simplify(multiple=True, loops=True)

    # Отрисовываем граф
    plot(g, **visual_style)

    return edges if as_edgelist else matrix


import file
import pygame
dictionaries = file.File()
sounds = dictionaries.sounds_dictionary
pygame.init()
pygame.mixer.init()
class Music():
    def __init__(self, level):
        if level == "game":
            self._background = sounds.get("music_game")
            self._bad_collision = pygame.mixer.Sound(sounds.get("bad_collision"))
            self._good_collision = pygame.mixer.Sound(sounds.get("good_collision"))
            self._border_collision = pygame.mixer.Sound(sounds.get("border_collision"))
        else:
            self._background = sounds.get("music_title")

    def play_once(self,id):
        if id == "background":
            sound = self._background
        elif id == "bad collision":
            sound = self._bad_collision
        elif id == "good collision":
            sound = self._good_collision
        elif id == "border collision":
            sound = self._border_collision
        sound.play()
    def play_repeat(self):
        if pygame.mixer.music.get_busy():
            pygame.mixer.music.stop()
        pygame.mixer.music.load(self._background)
        pygame.mixer.music.play(-1)
    def stop_repeat(self):
        if pygame.mixer.music.get_busy():
            pygame.mixer.music.stop()


import unittest

import asyncio
import pytest
from queueueue.views import Task

class TestTask(unittest.TestCase):

    def setUp(self):
        import logging
        logging.basicConfig(level=logging.DEBUG)
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

    def tearDown(self):
        pass

    @asyncio.coroutine
    def test_task_update(self):
        t = Task("test_task", [1, 2, 3], "pool", [1], {})

        update_dict = {
            "stdout": 1,
            "stderr": 2,
            "result": 3,
            "traceback": 4,
            "status": 5
        }

        t.complete(**update_dict)
        assert t.completed.is_set()
        yield from t.completed.wait()
        data = t.completed.data
        assert data["result"] == update_dict["result"] and data["status"] == update_dict["status"]

        for key, value in update_dict.items():
            assert getattr(t, key) == value

    def test_task_update_only_required(self):
        t = Task("test_task", [1, 2, 3], "pool", [1], {})

        update_dict = {
            "wrong": 1,
            "error": 2,
        }

        t.complete(**update_dict)

        for key, value in update_dict.items():
            with pytest.raises(AttributeError):
                assert getattr(t, key) == value

    def test_task_worker_info(self):
        t = Task("test_task", [1, 2, 3], "pool", [1], {})

        w_i = t.worker_info

        assert len(w_i.items()) == 4
        assert "id" in w_i
        assert "name" in w_i
        assert "args" in w_i
        assert "kwargs" in w_i

    def test_task_full_info(self):
        t = Task("test_task", [1, 2, 3], "pool", [1], {})

        f_i = t.full_info

        assert len(f_i.items()) == 11
        assert "id" in f_i
        assert "name" in f_i
        assert "args" in f_i
        assert "kwargs" in f_i
        assert "locks" in f_i
        assert "pool" in f_i
        assert "stdout" in f_i
        assert "stderr" in f_i
        assert "result" in f_i
        assert "status" in f_i
        assert "traceback" in f_i

    def test_task_for_json(self):
        t = Task("test_task", [1, 2, 3], "pool", [1], {})

        assert type(t.for_json()) == dict

    def test_task_equality(self):
        t1 = Task("test_task", [1, 2, 3], "pool", [1], {})
        t2 = Task("test_task", [1, 2, 3], "pool", [1], {})
        t3 = Task("test_task", [1, 2, 3, 4], "pool", [1], {})

        assert t1 == t2
        assert t1 != t3


import torch
from torch.utils.data import Dataset, DataLoader
import pandas as pd
import numpy as np

# Model/dataloader definitions
class tsPredDataset(Dataset):
    def __init__(self, x_df, y_df, seq_id, x_cols, y_col, ts_steps=None):
        list_of_x = x_df[[seq_id] + x_cols] \
                    .groupby(seq_id) \
                    .apply(pd.DataFrame.to_numpy)
        if ts_steps is None:
            self.x = np.swapaxes(np.stack(list_of_x)[:, :, 1:], 1, 2).astype('float64')
        else:
            list_of_padded_x = []
            for i in list_of_x.keys():
                padded_x = np.zeros((ts_steps, len(x_cols) + 1))
                padded_x[:list_of_x[i].shape[0], :list_of_x[i].shape[1]] = list_of_x[i]
                list_of_padded_x.append(padded_x)
            self.x = np.swapaxes(np.stack(list_of_padded_x)[:, :, 1:], 1, 2).astype('float64')
        self.y = np.expand_dims(y_df[y_col].to_numpy().astype('float64'), axis = 1)

    def __len__(self):
        return self.y.shape[0]

    def __getitem__(self, idx):
        return self.x[idx, :, :], self.y[idx, :]

def load_train_val(x_path, y_path, seq_id, random_seed=None, train_p=0.8):
    x_df = pd.read_csv(x_path)
    y_df = pd.read_csv(y_path)
    all_seq_id = pd.unique(x_df[seq_id])
    if random_seed is not None:
        np.random.seed(random_seed)
    train_msk = np.random.random(all_seq_id.shape)
    train_id = [i for i, msk in zip(all_seq_id, train_msk) if msk <= train_p]
    val_id = [i for i, msk in zip(all_seq_id, train_msk) if msk > train_p]

    return (
        x_df[x_df[seq_id].isin(train_id)].reset_index(drop=True),
        y_df[y_df[seq_id].isin(train_id)].reset_index(drop=True),
        x_df[x_df[seq_id].isin(val_id)].reset_index(drop=True),
        y_df[y_df[seq_id].isin(val_id)].reset_index(drop=True)
    )

def load_test(x_path, y_path):
    x_df = pd.read_csv(x_path)
    y_df = pd.read_csv(y_path)
    return (x_df, y_df)

def create_datasets(train_x_path, train_y_path, test_x_path, test_y_path,
                    seq_id, x_cols, y_col,
                   random_seed=None, train_p=0.8, ts_steps=None):
    train_x, train_y, val_x, val_y = load_train_val(
        train_x_path, train_y_path, seq_id, random_seed, train_p
    )
    test_x, test_y = load_test(test_x_path, test_y_path)
    train_ds = tsPredDataset(train_x, train_y, seq_id, x_cols, y_col, ts_steps=ts_steps)
    val_ds = tsPredDataset(val_x, val_y, seq_id, x_cols, y_col, ts_steps=ts_steps)
    test_ds = tsPredDataset(test_x, test_y, seq_id, x_cols, y_col, ts_steps=ts_steps)
    return train_ds, val_ds, test_ds


"""
Converts Bibles from https://bible4u.net/en/download
"""

from bs4 import BeautifulSoup
import json
import sys

file = sys.argv[1] + '.xml' if len(sys.argv) > 1 else 'AKJV.xml'
soup = BeautifulSoup(open(file, encoding='utf-8').read(), 'lxml')
bible = {
    "meta": {
        "version": "YLT",
        "fullname": "Young's Literal Translation",
        "language": "en",
        "date": 1862,
        "copyright": "Public Domain"
    },
    "bible": {
    }
}

for book in soup.html.body.xmlbible:
    if book.name == 'biblebook':
        # Start New Book
        bible['bible'][book['bname']] = {}
        for chapter in book:
            if chapter.name is not None:
                # Start New Chapter
                bible['bible'][book['bname']][chapter['cnumber']] = {}
                for verse in chapter:
                    if verse.name is not None:
                        bible['bible'][book['bname']][chapter['cnumber']][verse['vnumber']] = verse.string
                        print(book['bname'], chapter['cnumber'], verse['vnumber'])

with open(sys.argv[1] + '.json', 'w') as outfile:
    json.dump(bible, outfile)


import setuptools

with open("requirements.txt") as f:
    requirements = f.read().splitlines()

setuptools.setup(
    name="standard_distributions",
    version="0.0.1",
    description="Probability distributions package",
    url="",
    author="Kelvin Wahome",
    author_email="kevowahome@gmail.com",
    license="MIT",
    packages=setuptools.find_packages(),
    install_requires=requirements,
    zip_safe=False,
)


import os
import wget
import time
import yaml
import glob
import torch
import random
import inspect
import datetime
import argparse
import numpy as np
import pandas as pd
from collections import defaultdict

from torch.utils.data import TensorDataset, random_split
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from transformers import AlbertConfig
from transformers import AlbertTokenizer, AlbertModel, WEIGHTS_NAME, CONFIG_NAME

#########################################
############ Basic functions ############
#########################################

def check_folder(path):
    """Create adequate folders if necessary."""
    try:
        if not os.path.isdir(path):
            check_folder(os.path.dirname(path))
            os.mkdir(path)
    except:
        pass

def read_yaml(yaml_path):
    """Open and read safely a yaml file."""
    with open(yaml_path, 'r') as stream:
        try:
            parameters = yaml.safe_load(stream)
        except :
            print("Couldn't load yaml file: {}.".format(yaml_path))
            quit()
    return parameters

def save_yaml(data, yaml_path):
    """Open and write safely in a yaml file.
    Arguments:
        - data: list/dict/str/int/float
        -yaml_path: str
    """
    with open(yaml_path, 'w') as outfile:
        yaml.dump(data, outfile, default_flow_style=False)

def filter_args(func, d):
    """ Filter dictionary keys to match the function arguments.
    Arguments:
        - func: function
        - d: dict
    Returns:
        - args: dict
    """
    keys = inspect.getfullargspec(func).args
    args = {key: d[key] for key in keys if ((key!='self') and (key in d.keys()))}
    return args

def get_device(device_number=0, local_rank=-1):
    """ Get the device to use for computations.
    """
    if local_rank == -1:
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        n_gpu = torch.cuda.device_count()
        print('There are %d GPU(s) available.' % torch.cuda.device_count())
        if torch.cuda.is_available():
            print('We will use the GPU:', torch.cuda.get_device_name(device_number))
        else:
            print('No GPU available, using the CPU instead.')
    else:
        torch.cuda.set_device(local_rank)
        device = torch.device("cuda", local_rank)
        n_gpu = 1
        # Initializes the distributed backend which will take care of sychronizing nodes/GPUs
        torch.distributed.init_process_group(backend='nccl')
    return device

def format_time(elapsed):
    """
    Takes a time in seconds and returns a string hh:mm:ss
    """
    # Round to the nearest second.
    elapsed_rounded = int(round((elapsed)))
    # Format as hh:mm:ss
    return str(datetime.timedelta(seconds=elapsed_rounded))

def set_seed(value=1111):
    """ Set all seeds to a given value for reproductibility."""
    random.seed(value)
    np.random.seed(value)
    torch.manual_seed(value)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(value)

#########################################
########### Specific functions ##########
#########################################

def save(model, tokenizer, output_dir, index):
    """ Saving best-practices: if you use defaults names for the model,
    you can reload it using from_pretrained().
    """
    output_dir = os.path.join(output_dir, index)
    # If we save using the predefined names, we can load using `from_pretrained`
    output_model_file = os.path.join(output_dir, WEIGHTS_NAME)
    output_config_file = os.path.join(output_dir, CONFIG_NAME)
    # Create output directory if needed
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    print("Saving model to %s" % output_dir)

    # Save a trained model, configuration and tokenizer using `save_pretrained()`.
    # They can then be reloaded using `from_pretrained()`
    model_to_save = model.module if hasattr(model, 'module') else model  # Take care of distributed/parallel training
    torch.save(model_to_save.state_dict(), output_model_file)
    model_to_save.config.to_json_file(output_config_file)
    tokenizer.save_pretrained(output_dir)

def batchify_per_sentence(iterator, tokenizer, number_of_sentence, max_length=512):
    """Batchify iterator sentence, to get batches of specified number of sentences.
    Arguments:
        - iterator: sentence iterator
        - number_of_sentence: int
    Returns:
        - batch: sequence iterator
        - indexes: tuple of int
    """
    iterator = [item.strip() for item in iterator]
    max_length -= 2 # for special tokens

    batch = []
    indexes = []
    sentence_count = 0
    batch_modifications = 0
    n = len(iterator)
    while sentence_count < n:
        stop = min(sentence_count+number_of_sentence, n)
        token_count = len(tokenizer.tokenize(' '.join(iterator[sentence_count:stop])))
        while token_count > max_length:
            print('WARNING: decreasing number of sentence in a batch to fit max length of {}'.format(max_length))
            batch_modifications += 1
            stop -= 1
            token_count = len(tokenizer.tokenize(' '.join(iterator[sentence_count:stop])))
        batch.append(' '.join(iterator[sentence_count:stop]))
        indexes.append((0, len(tokenizer.tokenize(batch[-1]))))
        sentence_count = stop
    if batch_modifications > 0:
        print('WARNING: {} reductions were done when constructing batches... You should reduce the number of sentence to include.'.format(batch_modifications))
    return batch, indexes

def batchify_per_sentence_with_context(iterator, tokenizer, number_of_sentence, number_sentence_before, max_length=512):
    """Batchify iterator sentence, to get batches of specified number of sentences.
    Arguments:
        - iterator: sentence iterator
        - number_of_sentence: int
        - number_sentence_before: int
    Returns:
        - batch: sequence iterator
        - indexes: tuple of int
    """
    iterator = [item.strip() for item in iterator]
    max_length -= 2 # for special tokens
    assert number_of_sentence > 0

    batch = []
    indexes = []
    sentence_count = 0
    batch_modifications = 0
    n = len(iterator)
    if number_sentence_before > 0:
        start = 0
        stop = min(number_sentence_before, n)
        token_count = len(tokenizer.tokenize(iterator[stop]))
        if token_count > max_length:
            raise ValueError('Cannot fit context with additional sentence. You should reduce context length.')
        batch.append(' '.join(iterator[:stop]))
        indexes.append((0, len(tokenizer.tokenize(batch[-1]))))
        sentence_count = stop

    while sentence_count < n:
        start = sentence_count - number_sentence_before
        stop = min(sentence_count + number_of_sentence, n)
        token_count = len(tokenizer.tokenize(' '.join(iterator[start:stop])))
        while token_count > max_length:
            print('WARNING: decreasing number of sentence in a batch to fit max length of {}'.format(max_length))
            batch_modifications += 1
            stop -= 1
            token_count = len(tokenizer.tokenize(' '.join(iterator[sentence_count:stop])))
            if stop==start+number_sentence_before:
                raise ValueError('Too many context sentence. You reach {} tokens only with context.'.format(token_count))
        batch.append(' '.join(iterator[start:stop]))
        indexes.append((len(tokenizer.tokenize(' '.join(iterator[start:start+number_sentence_before]))), len(tokenizer.tokenize(batch[-1]))))
        sentence_count = stop
    if batch_modifications > 0:
        print('WARNING: {} reductions were done when constructing batches... You should reduce the number of sentence to include.'.format(batch_modifications))
    return batch, indexes

def batchify_per_sentence_with_pre_and_post_context(iterator, tokenizer, number_of_sentence, number_sentence_before, number_sentence_after, max_length=512):
    """Batchify iterator sentence, to get batches of specified number of sentences.
    Arguments:
        - iterator: sentence iterator
        - number_of_sentence: int
        - number_sentence_before: int
        - number_sentence_after: int
    Returns:
        - batch: sequence iterator
        - indexes: tuple of int
    """
    iterator = [item.strip() for item in iterator]
    max_length -= 2 # for special tokens
    assert number_of_sentence > 0

    batch = []
    indexes = []
    sentence_count = 0
    batch_modifications = 0
    n = len(iterator)
    if number_sentence_before > 0:
        start = 0
        stop = min(number_sentence_before, n)
        token_count = len(tokenizer.tokenize(iterator[stop]))
        if token_count > max_length:
            raise ValueError('Cannot fit context with additional sentence. You should reduce context length.')
        batch.append(' '.join(iterator[:stop]))
        indexes.append((0, len(tokenizer.tokenize(batch[-1]))))
        sentence_count = stop

    while sentence_count < n:
        start = sentence_count - number_sentence_before
        stop = min(sentence_count + number_of_sentence, n)
        stop_post_context = min(stop + number_sentence_after, n)
        token_count = len(tokenizer.tokenize(' '.join(iterator[start:stop_post_context])))
        while token_count > max_length:
            print('WARNING: decreasing number of sentence in a batch to fit max length of {}'.format(max_length))
            batch_modifications += 1
            stop -= 1
            stop_post_context = min(stop + number_sentence_after, n)
            token_count = len(tokenizer.tokenize(' '.join(iterator[start:stop_post_context])))
            if stop==start+number_sentence_before:
                raise ValueError('Too many context sentence. You reach {} tokens only with context.'.format(token_count))
        batch.append(' '.join(iterator[start:stop_post_context]))
        indexes.append((len(tokenizer.tokenize(' '.join(iterator[start:start+number_sentence_before]))), len(tokenizer.tokenize(' '.join(iterator[start:stop])))))
        sentence_count = stop
    if batch_modifications > 0:
        print('WARNING: {} reductions were done when constructing batches... You should reduce the number of sentence to include.'.format(batch_modifications))
    return batch, indexes

def batchify(iterator, tokenizer, context_length, max_length=512):
    """Batchify iterator sentence, to get minimum context length
    when possible.
    Arguments:
        - iterator: sentence iterator
        - context_length: int
    Returns:
        - batch: sequence iterator
        - indexes: tuple of int
    """
    iterator = [item.strip() for item in iterator]
    max_length -= 2 # for special tokens

    batch = []
    indexes = []
    sentence_count = 0
    n = len(iterator)

    assert context_length < max_length
    token_count = 0
    while sentence_count < n and token_count < max_length:
        token_count += len(tokenizer.tokenize(iterator[sentence_count]))
        if token_count < max_length:
            sentence_count += 1
    batch.append(' '.join(iterator[:sentence_count]))
    indexes.append((0, len(tokenizer.tokenize(batch[-1]))))

    while sentence_count < n:
        token_count = 0
        sentence_index = sentence_count - 1
        tmp = sentence_count
        while token_count < context_length:
            token_count += len(tokenizer.tokenize(iterator[sentence_index]))
            sentence_index -= 1
        while sentence_count < n and token_count < max_length:
            token_count += len(tokenizer.tokenize(iterator[sentence_count]))
            if token_count < max_length:
                sentence_count += 1
        batch.append(' '.join(iterator[sentence_index+1:sentence_count]))
        indexes.append((len(tokenizer.tokenize(' '.join(iterator[sentence_index+1:tmp]))), len(tokenizer.tokenize(batch[-1]))))
    return batch, indexes

#########################################
###### Activations related functions ####
#########################################

def match_tokenized_to_untokenized(tokenized_sent, untokenized_sent, connection_character='▁', eos_token='[SEP]'):
    '''Aligns tokenized and untokenized sentence given non-subwords "Ġ" prefixed
    Assuming that each subword token that does start a new word is prefixed
    by "Ġ", computes an alignment between the un-subword-tokenized
    and subword-tokenized sentences.
    Args:
      tokenized_sent: a list of strings describing a subword-tokenized sentence
      untokenized_sent: a list of strings describing a sentence, no subword tok.
    Returns:
      A dictionary of type {int: list(int)} mapping each untokenized sentence
      index to a list of subword-tokenized sentence indices
    '''
    mapping = defaultdict(list)
    untokenized_sent_index = 0
    tokenized_sent_index = 0
    while (untokenized_sent_index < len(untokenized_sent) and tokenized_sent_index < len(tokenized_sent)):
        while (tokenized_sent_index+1  < len(tokenized_sent) and (not tokenized_sent[tokenized_sent_index+1].startswith(connection_character)) and tokenized_sent[tokenized_sent_index+1]!=eos_token):
            mapping[untokenized_sent_index].append(tokenized_sent_index)
            tokenized_sent_index += 1
        mapping[untokenized_sent_index].append(tokenized_sent_index)
        untokenized_sent_index += 1
        tokenized_sent_index += 1
    return mapping

def extract_activations_from_token_activations(activation, mapping, indexes):
    """Take the average activations of the tokens related to a given word."""
    new_activations = []
    key_start = None
    key_stop = None
    for key_, value in mapping.items():
        if (value[0] - 1) == (indexes[0]): #because we added special token at the beginning
            key_start = key_
    for key_, value in mapping.items():
        if value[-1] == (indexes[1]): #because we added special token at the beginning
            key_stop = key_
    for word_index in range(key_start, key_stop + 1): # len(mapping.keys()) - 1
        word_activation = []
        word_activation.append([activation[:,index, :] for index in mapping[word_index]])
        word_activation = np.vstack(word_activation)
        new_activations.append(np.mean(word_activation, axis=0).reshape(1,-1))
    return new_activations

def extract_activations_from_special_tokens(activation, mapping):
    """Returns the activations of the special tokens."""
    cls_activations = [activation[:,mapping[0], :].reshape(1,-1)]
    sep_activations = [activation[:,mapping[len(mapping) - 1], :].reshape(1,-1)]
    return cls_activations, sep_activations

def extract_heads_activations_from_special_tokens(activation, mapping):
    """Extract heads activations of each layer for special tokens.
    activation.shape: [nb_layers, nb_heads, sequence_length, hidden_size/nb_heads]"""
    cls_activations = [activation[:, :, mapping[0], :].reshape(1,-1)]
    sep_activations = [activation[:, :, mapping[len(mapping) - 1], :].reshape(1,-1)]
    return cls_activations, sep_activations

def extract_heads_activations_from_token_activations(activation, mapping, indexes):
    """Extract heads activations of each layer for each token.
    Take the average activations of the tokens related to a given word.
    activation.shape: [nb_layers, nb_heads, sequence_length, hidden_size/nb_heads]"""
    new_activations = []
    key_start = None
    key_stop = None
    for key_, value in mapping.items():
        if (value[0] - 1)== (indexes[0]): #because we added special token at the beginning
            key_start = key_
    for key_, value in mapping.items():
        if value[-1] == (indexes[1]): #because we added special token at the beginning
            key_stop = key_
    for word_index in range(key_start, key_stop + 1): # len(mapping.keys()) - 1
        word_activation = []
        word_activation.append([activation[:, :, index, :] for index in mapping[word_index]])
        word_activation = np.vstack(word_activation)
        new_activations.append(np.mean(word_activation, axis=0).reshape(1,-1))
    return new_activations


# pylint: disable=missing-docstring, global-statement, invalid-name
#
# Copyright (C) 2017  Jonas Colmsjö, Claes Strannegård
#

# Imports
# ======

import unittest

from agents import Agent
from agents import Thing
from agents import Direction
from agents import XYEnvironment

from myutils import Logging

# Setup logging
# =============

DEBUG_MODE = True
l = Logging('test_agents', DEBUG_MODE)

# Unit tests
# ==========

class TestAgents(unittest.TestCase):

    def setUp(self):
        l.info('Testing agents...')

    def test_move_forward(self):
        l.info('test_move_forward')

        d = Direction("up")
        l1 = d.move_forward((0, 0))
        self.assertTrue(l1 == (0, -1))

        d = Direction(Direction.R)
        l1 = d.move_forward((0, 0))
        self.assertTrue(l1 == (1, 0))

        d = Direction(Direction.D)
        l1 = d.move_forward((0, 0))
        self.assertTrue(l1 == (0, 1))

        d = Direction("left")
        l1 = d.move_forward((0, 0))
        self.assertTrue(l1 == (-1, 0))

        l2 = d.move_forward((1, 0))
        self.assertTrue(l2 == (0, 0))

    def test_add(self):
        l.info('test_add')

        d = Direction(Direction.U)
        l1 = d + "right"
        l2 = d + "left"
        self.assertTrue(l1.direction == Direction.R)
        self.assertTrue(l2.direction == Direction.L)

        d = Direction("right")
        l1 = d.__add__(Direction.L)
        l2 = d.__add__(Direction.R)
        self.assertTrue(l1.direction == "up")
        self.assertTrue(l2.direction == "down")

        d = Direction("down")
        l1 = d.__add__("right")
        l2 = d.__add__("left")
        self.assertTrue(l1.direction == Direction.L)
        self.assertTrue(l2.direction == Direction.R)

        d = Direction(Direction.L)
        l1 = d + Direction.R
        l2 = d + Direction.L
        self.assertTrue(l1.direction == Direction.U)

        d = Direction(Direction.U)
        l1 = d + Direction.R + Direction.R
        self.assertTrue(l1.direction == Direction.D)

    def test_xyenvironment(self):
        l.info('test_xyenvironment')

        e = XYEnvironment()
        a = Agent()
        t = Thing()

        e.add_thing(a, (1, 1))
        e.add_thing(t, (2, 2))

        # Down is the default direction
        e.execute_action(a, 'Forward', 1)
        self.assertTrue(a.location == (2, 1))

        e.execute_action(a, 'TurnLeft', 1)
        e.execute_action(a, 'Forward', 1)
        self.assertTrue(a.location == (2, 0))

        e.execute_action(a, 'TurnLeft', 1)
        self.assertTrue(a.direction.direction == Direction.L)

        e.execute_action(a, 'Forward', 1)
        self.assertTrue(a.location == (1, 0))

    def tearDown(self):
        l.info('...done with test_agents.')

# Main
# ====

if __name__ == '__main__':
    unittest.main()


# Author: Daiwei (David) Lu
# A custom network in VGG style

import torch
from torch import nn

class ConvPool(nn.Module):
    def __init(self, channelin, channelout):
        super(Net, self).__init__()
        self.convpool = nn.Sequential(
            nn.Conv2d(channelin, channelout, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, 2),
        )

    def forward(self, x):
        return self.convpool(x)

class Conv(nn.Module):
    def __init(self, channelin, channelout):
        super(Net, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(channelin, channelout, 3, padding=1),
            nn.ReLU(inplace=True),
        )

    def forward(self, x):
        return self.conv(x)

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(64, 128, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(128, 256, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(256, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, 2))
        self.avgpool = nn.AdaptiveAvgPool2d(output_size=(7, 7))
        self.classifier = nn.Sequential(nn.Linear(512 * 7 * 7, 64),
                                        nn.Sigmoid(),
                                        nn.Dropout(0.25),
                                        nn.Linear(64, 2))

    def forward(self, x):
        # ModuleList can act as an iterable, or be indexed using ints
        x1 = self.features(x)
        x2 = self.avgpool(x1)
        x3 = torch.flatten(x2, 1)
        x4 = self.classifier(x3)
        return x4


#!/usr/bin/env python

# actually we should implement ICMP protocol, but here we just implement UDP pinger for simplicity
import time
import random
from socket import *

# Pinger info
pingCount = 10
waitTime = 1
message = 'data'

# Server info
serverIP = 'localhost'
serverPort = 9601
maxLength = 1024
serverAddress = (serverIP, serverPort)

clientSocket = socket(family=AF_INET, type=SOCK_DGRAM)
clientSocket.settimeout(waitTime)

count = 0
while(count<pingCount):
    clientSocket.sendto(message, serverAddress)
    try:
        start_time = time.clock()
        recvMessage, _ = clientSocket.recvfrom(maxLength)
        RTT = time.clock()-start_time
        print("RTT of Ping {:d}: {:f}".format(count,RTT))
    except:
        print("Loss of Ping {:d}".format(count))
    finally:
        count = count + 1
clientSocket.close()


#
# netlab create command
#
# Creates virtualization provider configuration and automation inventory from
# the specified topology
#
import argparse
import typing
import textwrap
from box import Box

from . import common_parse_args, topology_parse_args
from .. import read_topology,augment,common
from ..outputs import _TopologyOutput

#
# CLI parser for create-topology script
#
def create_topology_parse(args: typing.List[str], cmd: str) -> argparse.Namespace:
  if cmd != 'create':
    epilog = ""
  else:
    epilog = textwrap.dedent('''
      output files created when no output is specified:

        * Virtualization provider file with provider-specific filename
          (Vagrantfile or clab.yml)
        * Ansible inventory file (hosts.yml) and configuration (ansible.cfg)

      For a complete list of output formats please consult the documentation
    ''')
  parser = argparse.ArgumentParser(
    parents=[ common_parse_args(), topology_parse_args() ],
    formatter_class=argparse.RawDescriptionHelpFormatter,
    prog="netlab %s" % cmd,
    description='Create provider- and automation configuration files',
    epilog=epilog)

  parser.add_argument(
    dest='topology', action='store', nargs='?',
    type=argparse.FileType('r'),
    default='topology.yml',
    help='Topology file (default: topology.yml)')

  if cmd == 'create':
    parser.add_argument('-o','--output',dest='output', action='append',help='Output format(s): format:option=filename')
    parser.add_argument('--devices',dest='devices', action='store_true',help='Create provider configuration file and netsim-devices.yml')

  return parser.parse_args(args)

def run(cli_args: typing.List[str], cli_command: str = 'create') -> Box:
  args = create_topology_parse(cli_args, cli_command)
  if not 'output' in args:
    args.output = None
  if not 'devices' in args:
    args.devices = None

  if not args.output:
    args.output = ['provider','devices'] if args.devices else ['provider','ansible:dirs']
  elif args.devices:
    common.error('--output and --devices flags are mutually exclusive',common.IncorrectValue,'create')

  common.set_logging_flags(args)
  topology = read_topology.load(args.topology.name,args.defaults,"package:topology-defaults.yml")
  read_topology.add_cli_args(topology,args)
  common.exit_on_error()

  augment.main.transform(topology)
  common.exit_on_error()

  for output_format in args.output:
    output_module = _TopologyOutput.load(output_format,topology.defaults.outputs[output_format])
    if output_module:
      output_module.write(topology)
    else:
      common.error('Unknown output format %s' % output_format,common.IncorrectValue,'create')

  return topology


import numpy as np
import cv2
import torch
import torchvision
from ml.models.unet import ResNetUNet
import fastai.vision as faiv
from point_from_vector_field import point_from_vector_field
from dataset import PegInHoleDataset
import matplotlib.pyplot as plt
from scipy.ndimage import filters

cap = cv2.VideoCapture("real.mp4")
model = ResNetUNet(4)
model.load_state_dict(torch.load("models/model.pth"))
model.cuda(1)
model.eval()
torch.no_grad()

hole_thresh = 2

while True:
    ret, frame = cap.read()
    if not ret:
        cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
        continue
    h, w = frame.shape[:2]
    h_start = (h - w) // 2
    frame = frame[h_start:h_start + w]
    frame = cv2.resize(frame, (256, 256), interpolation=cv2.INTER_AREA)

    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    _img = torchvision.transforms.ToTensor()(rgb)
    _img = torchvision.transforms.Normalize(*faiv.imagenet_stats)(_img)
    result = model(_img.view(1, *_img.shape).cuda(1))
    result = result.view(result.shape[1:]).detach().cpu().numpy()
    result = result.reshape((2, 2, *result.shape[1:]))
    result = result.transpose((0, 2, 3, 1))

    hole, hole_sobel_xy = point_from_vector_field(result[0])
    peg, peg_sobel_xy = point_from_vector_field(result[1])
    angle_img_hole = PegInHoleDataset.get_angle_image(result[0])
    angle_img_peg = PegInHoleDataset.get_angle_image(result[0])
    hole_max_area = filters.maximum_filter(hole_sobel_xy, size=5)
    mask = np.logical_and(hole_max_area == hole_sobel_xy, hole_sobel_xy > 2)
    holes = np.argwhere(mask)
    for hole in holes:
        cv2.drawMarker(frame, (hole[1], hole[0]), (255, 255, 255), cv2.MARKER_CROSS, markerSize=10, thickness=1)
    cv2.drawMarker(frame, (peg[1], peg[0]), (255, 0, 0), cv2.MARKER_CROSS, markerSize=10, thickness=1)
    cv2.imshow('frame', frame)
    cv2.imshow('hole sobel', hole_sobel_xy / 4)
    cv2.imshow('peg sobel', peg_sobel_xy / 4)
    key = cv2.waitKey(1)
    if key & 0xFF == ord('q'):
        break
    if key & 0xFF == ord('p'):
        cv2.waitKey()
    if key & 0xFF == ord('c'):
        plt.imshow(angle_img_hole, cmap='hsv')
        plt.show()

cap.release()
cv2.destroyAllWindows()


#!/usr/bin/python

"""
Original Code:
  Summary: XMODEM protocol implementation.
  Home-page: https://github.com/tehmaze/xmodem
  Author: Wijnand Modderman, Jeff Quast
  License: MIT

Stripped down and modified for this bootloader by Joe George 2015-2016
"""

from calcCRC import calc_crc

# Protocol bytes
SOH = chr(0x01)

packet_size   = 128
pad           = '\x1a'

def asmPkt(data, sequence):

    data = data.ljust(packet_size, pad)
    crc = calc_crc(data)

    return SOH + (chr(sequence)) + (chr(0xff - sequence)) + data + (chr(crc >> 8)) + (chr(crc & 0xff))

if __name__ == "__main__":
    import sys, binhex
    out = asmPkt('abcdefghijklmnopqrstuvwxyz0123456789', 1)
    for ch in out:
        sys.stdout.write(hex(ord(ch))+' ')
    print
    print

def verify(data, fullcrc):

    data = data.ljust(packet_size, pad)
    crc = calc_crc(data, fullcrc)

    return crc


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.13-x86_64/egg/sr/conftest.py
# Compiled at: 2019-03-23 05:47:02
"""
@Author  : xinxi
@Time    : 2018/12/5 18:34
@describe: 创建driver
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os, sys, subprocess, pytest, time, allure, base64
from allure.constants import AttachmentType
from config import screen_folder
from wdriver import WDriver
sys.path.append('..')
from logger import init_logger
logger = init_logger()

@pytest.fixture()
def driver_setup(request):
    logger.info('自动化测试开始!')
    request.instance.driver = WDriver().init_driver()
    logger.info('driver初始化')

    def driver_teardown():
        logger.info('自动化测试结束!')
        request.instance.driver.quit()

    request.addfinalizer(driver_teardown)

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    u"""
    hook pytest失败
    :param item:
    :param call:
    :return:
    """
    outcome = yield
    rep = outcome.get_result()
    if rep.when == 'call' and rep.failed:
        mode = 'a' if os.path.exists('failures') else 'w'
        with open('failures', mode) as (f):
            if 'tmpdir' in item.fixturenames:
                extra = ' (%s)' % item.funcargs['tmpdir']
            else:
                extra = ''
            f.write(rep.nodeid + extra + '\n')
        logger.info('测试失败了')
        with allure.step('添加失败截图...'):
            allure.attach(rep.nodeid, WDriver().get_driver().get_screenshot_as_png(), type=AttachmentType.PNG)


from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    # path('', admin.site.urls),
    # path('',include('regLogin.urls')),
    # path('',include('pollapp.urls')),
    path('poll-list/',views.PollList,name = "PollList"),
    path('delete/<int:poll_id>/',views.DeletePoll,name = "DeletePoll"),
    path('<int:poll_id>/',views.VotePoll,name = "VotePoll"),
    path('view/<int:question_id>/',views.viewVote,name = "ViewVotes"),
    path('newpoll/',views.Newpoll,name = 'newpoll')

]


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

# In[ ]:

# I gave the function a more descriptive name
# I gave variables a more descriptive name
# I added spaces for consistency
# I put new variables on top
# I deleted unnecessary code
# if (x * x == y * y + z * z):
# solutions.append([x, y, z])

# In[27]:

"""
You are presented with an integer number larger than 5. Your goal is to identify the longest side
possible in a right triangle whose sides are not longer than the number you are given.

For example, if you are given the number 15, there are 3 possibilities to compose right triangles:

1. [3, 4, 5]
2. [6, 8, 10]
3. [5, 12, 13]

The following function shows one way to solve the problem but the code is not ideal or efficient.
Refactor the code based on what you have learned about code simplicity and efficiency.
"""

number = input("What is the maximal length of the triangle side? Enter a number: ")

def longestSide(number):

    solutions = []
    longest_side = 0

    solutions = [[x, y, z] for z in range(3, number) for y in range(4, number) for x in range(5, number)]

    for solution in solutions:
        if longest_side < max(solution):
            longest_side = max(solution)

    return longest_side

print("The longest side possible is " + str(longestSide(int(number))))


"""
Taken and adapted from free version of SL course on Udacity
"""

from numpy import *
import pylab as pl
from sklearn.utils import shuffle
from sklearn.metrics import mean_squared_error
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score
from collections import defaultdict
from sklearn.neural_network import MLPClassifier

d = defaultdict(LabelEncoder)
dataframe = pd.read_csv(filepath_or_buffer="~/Documents/repos/supervised_learning/student/student-por.csv", sep=';')
dataframe1 = pd.read_csv(filepath_or_buffer="~/Documents/repos/supervised_learning/student/student-mat.csv", sep=';')
dfs = [dataframe, dataframe1]
df = pd.concat(dfs)
df = df.drop_duplicates()

df = df.apply(lambda x: d[x.name].fit_transform(x))

cols_to_transform = ['sex', 'school', 'address', 'famsize', 'Pstatus', 'Mjob', 'Fjob', 'reason', 'guardian', 'schoolsup',
                     'famsup', 'paid', 'activities', 'nursery', 'higher', 'internet', 'romantic']

columns = df.columns.tolist()
columns = [c for c in columns if c not in ["Walc",'Dalc', 'G1', 'G2', 'G3', 'Fedu', 'Medu', 'studytime', 'famrel','schoolsup',
                     'famsup', 'paid', 'activities', 'nursery', 'higher', 'internet', 'romantic']]
# Load the boston dataset and seperate it into training and testing set
iris = datasets.load_iris()
#X, y = df[columns], df['Walc']
X, y = iris.data, iris.target
offset = int(0.8 * len(X))
X_train, y_train = X[:offset], y[:offset]
X_test, y_test = X[offset:], y[offset:]

# We will vary the depth of decision trees from 2 to 25
hidden_layers = arange(10, 20)
train_score = zeros(len(hidden_layers))
test_score = zeros(len(hidden_layers))

for i, d in enumerate(hidden_layers):
    estimator = MLPClassifier(learning_rate='constant', activation='tanh', hidden_layer_sizes=(d, d-4, d-8),
                              learning_rate_init=0.001, max_iter=50, random_state=1,
                              solver='lbfgs', tol=0.00001, verbose=True,
                              warm_start=False)
    #estimator = DecisionTreeClassifier(max_depth=d)
    # Setup a Decision Tree Regressor so that it learns a tree with depth d
    # Fit the learner to the training data
    estimator.fit(X_train, y_train)

    # Find the score on the training set
    train_score[i] = accuracy_score(y_train, estimator.predict(X_train))
    # Find the score on the testing set
    test_score[i] = accuracy_score(y_test, estimator.predict(X_test))

pl.figure()
pl.ylim(ymax=2)
pl.title('Neural Net: Performance vs Hidden Layers')
pl.plot(hidden_layers, test_score, label='Iris test score')
pl.plot(hidden_layers, train_score,label='Iris training score')
filtered = ['goout', 'sex', 'age', 'Fjob']
X, y = df[filtered], df['Walc']
offset = int(0.9 * len(X))
X_train, y_train = X[:offset], y[:offset]
X_test, y_test = X[offset:], y[offset:]
max_layers = arange(2,20)
alc_train_score = zeros(len(max_layers))
alc_test_score = zeros(len(max_layers))

for i, d in enumerate(max_layers):
    estimator = MLPClassifier(learning_rate='constant', activation='logistic',hidden_layer_sizes=(d,),
                          learning_rate_init=0.001, alpha=0.001, max_iter=200, random_state=1,
       solver='lbfgs', tol=0.0001, verbose=True,
       warm_start=False)
    estimator.fit(X_train, y_train)

    # Find the score on the training set
    alc_train_score[i] = accuracy_score(y_train, estimator.predict(X_train))
    # Find the score on the testing set
    alc_test_score[i] = accuracy_score(y_test, estimator.predict(X_test))

# Plot training and test error as a function of the depth of the decision tree learnt
#lw=2

pl.plot(max_layers, alc_test_score, label='Alcohol test score')
pl.plot(max_layers, alc_train_score,label='Alcohol training score')

pl.legend()
pl.xlabel('Hidden layer size')
pl.ylabel('Score')
pl.show()


import lru
import numpy as np
import os
import random

from textattack.shared import utils
from textattack.constraints import Constraint
from textattack.constraints.pre_transformation import PreTransformationConstraint
from textattack.shared import TokenizedText
from textattack.attack_results import SkippedAttackResult, SuccessfulAttackResult, FailedAttackResult

class Attack:
    """
    An attack generates adversarial examples on text.

    This is an abstract class that contains main helper functionality for
    attacks. An attack is comprised of a search method, goal function,
    a transformation, and a set of one or more linguistic constraints that
    successful examples must meet.

    Args:
        goal_function: A function for determining how well a perturbation is doing at achieving the attack's goal.
        constraints: A list of constraints to add to the attack, defining which perturbations are valid.
        transformation: The transformation applied at each step of the attack.
        search_method: A strategy for exploring the search space of possible perturbations
    """

    def __init__(self, goal_function=None, constraints=[], transformation=None, search_method=None):
        """ Initialize an attack object. Attacks can be run multiple times. """
        self.search_method = search_method
        self.goal_function = goal_function
        if not self.goal_function:
            raise NameError('Cannot instantiate attack without self.goal_function for predictions')
        if not hasattr(self, 'tokenizer'):
            if hasattr(self.goal_function.model, 'tokenizer'):
                self.tokenizer = self.goal_function.model.tokenizer
            else:
                raise NameError('Cannot instantiate attack without tokenizer')
        self.transformation = transformation
        self.is_black_box = getattr(transformation, 'is_black_box', True)

        if not self.search_method.check_transformation_compatibility(self.transformation):
            raise ValueError('SearchMethod {self.search_method} incompatible with transformation {self.transformation}')

        self.constraints = []
        self.pre_transformation_constraints = []
        for constraint in constraints:
            if isinstance(constraint, PreTransformationConstraint):
                self.pre_transformation_constraints.append(constraint)
            else:
                self.constraints.append(constraint)

        self.constraints_cache = lru.LRU(utils.config('CONSTRAINT_CACHE_SIZE'))

        # Give search method access to functions for getting transformations and evaluating them
        self.search_method.get_transformations = self.get_transformations
        self.search_method.get_goal_results = self.goal_function.get_results

    def get_transformations(self, current_text, original_text=None, **kwargs):
        """
        Applies ``self.transformation`` to ``text``, then filters the list of possible transformations
        through the applicable constraints.

        Args:
            current_text: The current ``TokenizedText`` on which to perform the transformations.
            original_text: The original ``TokenizedText`` from which the attack started.
            apply_constraints: Whether or not to apply post-transformation constraints.

        Returns:
            A filtered list of transformations where each transformation matches the constraints

        """
        if not self.transformation:
            raise RuntimeError('Cannot call `get_transformations` without a transformation.')

        transformed_texts = np.array(self.transformation(current_text,
                            pre_transformation_constraints=self.pre_transformation_constraints,
                            **kwargs))
        return self._filter_transformations(transformed_texts, current_text, original_text)

    def _filter_transformations_uncached(self, transformed_texts, current_text, original_text=None):
        """
        Filters a list of potential transformaed texts based on ``self.constraints``\.

        Args:
            transformed_texts: A list of candidate transformed ``TokenizedText``\s to filter.
            current_text: The current ``TokenizedText`` on which the transformation was applied.
            original_text: The original ``TokenizedText`` from which the attack started.
        """
        filtered_texts = transformed_texts[:]
        for C in self.constraints:
            if len(filtered_texts) == 0: break
            filtered_texts = C.call_many(filtered_texts, current_text,
                                            original_text=original_text)
        # Default to false for all original transformations.
        for original_transformed_text in transformed_texts:
            self.constraints_cache[original_transformed_text] = False
        # Set unfiltered transformations to True in the cache.
        for filtered_text in filtered_texts:
            self.constraints_cache[filtered_text] = True
        return filtered_texts

    def _filter_transformations(self, transformed_texts, current_text, original_text=None):
        """
        Filters a list of potential transformed texts based on ``self.constraints``\.
        Checks cache first.

        Args:
            transformed_texts: A list of candidate transformed ``TokenizedText``\s to filter.
            current_text: The current ``TokenizedText`` on which the transformation was applied.
            original_text: The original ``TokenizedText`` from which the attack started.
        """
        # Populate cache with transformed_texts
        uncached_texts = []
        for transformed_text in transformed_texts:
            if transformed_text not in self.constraints_cache:
                uncached_texts.append(transformed_text)
            else:
                # promote transformed_text to the top of the LRU cache
                self.constraints_cache[transformed_text] = self.constraints_cache[transformed_text]
        self._filter_transformations_uncached(uncached_texts, current_text,
                                                original_text=original_text)
        # Return transformed_texts from cache
        filtered_texts = [t for t in transformed_texts if self.constraints_cache[t]]
        # Sort transformations to ensure order is preserved between runs
        filtered_texts.sort(key=lambda t: t.text)
        return filtered_texts

    def attack_one(self, initial_result):
        """
        Calls the ``SearchMethod`` to perturb the ``TokenizedText`` stored in
        ``initial_result``.

        Args:
            initial_result: The initial ``GoalFunctionResult`` from which to perturb.

        Returns:
            Either a ``SuccessfulAttackResult`` or ``FailedAttackResult``.
        """
        final_result = self.search_method(initial_result)
        if final_result.succeeded:
            return SuccessfulAttackResult(initial_result, final_result)
        else:
            return FailedAttackResult(initial_result, final_result)

    def _get_examples_from_dataset(self, dataset, num_examples=None, shuffle=False,
            attack_n=False, attack_skippable_examples=False):
        """
        Gets examples from a dataset and tokenizes them.

        Args:
            dataset: An iterable of (text, ground_truth_output) pairs
            num_examples (int): the number of examples to return
            shuffle (:obj:`bool`, optional): Whether to shuffle the data
            attack_n (bool): If `True`, returns `num_examples` non-skipped
                examples. If `False`, returns `num_examples` total examples.

        Returns:
            results (Iterable[Tuple[GoalFunctionResult, Boolean]]): a list of
                objects containing (text, ground_truth_output, was_skipped)
        """
        examples = []
        n = 0

        if shuffle:
            random.shuffle(dataset.examples)

        num_examples = num_examples or len(dataset)

        if num_examples <= 0:
            return
            yield

        for text, ground_truth_output in dataset:
            tokenized_text = TokenizedText(text, self.tokenizer)
            goal_function_result = self.goal_function.get_result(tokenized_text, ground_truth_output)
            # We can skip examples for which the goal is already succeeded,
            # unless `attack_skippable_examples` is True.
            if (not attack_skippable_examples) and (goal_function_result.succeeded):
                if not attack_n:
                    n += 1
                # Store the true output on the goal function so that the
                # SkippedAttackResult has the correct output, not the incorrect.
                goal_function_result.output = ground_truth_output
                yield (goal_function_result, True)
            else:
                n += 1
                yield (goal_function_result, False)
            if num_examples is not None and (n >= num_examples):
                break

    def attack_dataset(self, dataset, num_examples=None, shuffle=False, attack_n=False):
        """
        Runs an attack on the given dataset and outputs the results to the
        console and the output file.

        Args:
            dataset: An iterable of (text, ground_truth_output) pairs.
            num_examples: The number of samples to attack.
            shuffle (:obj:`bool`, optional): Whether to shuffle the data. Defaults to False.
            attack_n: Whether or not to attack ``num_examples`` examples. If false, will process
                ``num_examples`` examples including ones which are skipped due to the model
                mispredicting the original sample.
        """

        examples = self._get_examples_from_dataset(dataset,
            num_examples=num_examples, shuffle=shuffle, attack_n=attack_n)

        for goal_function_result, was_skipped in examples:
            if was_skipped:
                yield SkippedAttackResult(goal_function_result)
                continue
            # Start query count at 1 since we made a single query to determine
            # that the prediction was correct.
            self.goal_function.num_queries = 1
            result = self.attack_one(goal_function_result)
            result.num_queries = self.goal_function.num_queries
            yield result

    def __repr__(self):
        """
        Prints attack parameters in a human-readable string.

        Inspired by the readability of printing PyTorch nn.Modules:
        https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/module.py
        """
        main_str = 'Attack' + '('
        lines = []

        lines.append(
            utils.add_indent(f'(search_method): {self.search_method}', 2)
        )
        # self.goal_function
        lines.append(
            utils.add_indent(f'(goal_function):  {self.goal_function}', 2)
        )
        # self.transformation
        lines.append(
            utils.add_indent(f'(transformation):  {self.transformation}', 2)
        )
        # self.constraints
        constraints_lines = []
        constraints = self.constraints + self.pre_transformation_constraints
        if len(constraints):
            for i, constraint in enumerate(constraints):
                constraints_lines.append(utils.add_indent(f'({i}): {constraint}', 2))
            constraints_str = utils.add_indent('\n' + '\n'.join(constraints_lines), 2)
        else:
            constraints_str = 'None'
        lines.append(utils.add_indent(f'(constraints): {constraints_str}', 2))
        # self.is_black_box
        lines.append(utils.add_indent(f'(is_black_box):  {self.is_black_box}', 2))
        main_str += '\n  ' + '\n  '.join(lines) + '\n'
        main_str += ')'
        return main_str

    __str__ = __repr__