#!/usr/bin/python3
import os, sys
import random
import sqlite3
import time
import cherrypy
import re

DB_NAME = 'database.db'

class InjectionDemo(object):
    @cherrypy.expose
    def index(self):
        return open(os.path.join(sys.path[0], 'public', 'html', 'index.html'))

    @cherrypy.expose
    def shutdown(self):
        cherrypy.engine.exit()
        return open(os.path.join(sys.path[0], 'public', 'html', 'shutdown.html'))

@cherrypy.expose
class DatabaseHandler(object):
    @cherrypy.tools.accept(media='text/plain')
    def GET(self, selectTerm):

        # NOTE: Constructing SQL querys with string concatenation allows for injection!
        queryString = "SELECT username FROM users WHERE firstname='{}'".format(selectTerm)

        # Connect to database
        with sqlite3.connect(DB_NAME) as connection:
            # Return string for index.html
            display_string = ""

            # Separate into multiple commands
            for query in queryString.split(';'):
                try:
                    # Attempt to execute sqlite3 query
                    responce = connection.execute(query)

                    # Create label for result table
                    display_string += "{};

".format(query)

                    # Format output into HTML tables
                    for row in responce.fetchall():
                        display_string += "
"
                        for field in row:
                            display_string += "{}	".format(field)
                        display_string += '
'
                    display_string += '

'

                # Handle improper query strings
                except sqlite3.OperationalError as err:
                    print(err)
                    return "{}".format(err)

            # Report empty results
            display_string = re.sub("

","No results found
", display_string)
            return display_string

def setup_database():
    '''
    Create the `users` table in database when server spins up
    '''
    with sqlite3.connect(DB_NAME) as connection:
        connection.execute("CREATE TABLE users (username, firstname, lastname, password)")
        connection.execute("INSERT INTO users VALUES ('admin', 'admin', '', 'Dt,pwN,Z<--%uN$F')")
        connection.execute("INSERT INTO users VALUES ('dama2741', 'david', 'madison', 'monkey123')")
        connection.execute("INSERT INTO users VALUES ('agnwl390', 'allen', 'mackinzie', 'password1234')")
        connection.execute("INSERT INTO users VALUES ('stwe5528', 'steven', 'west', 'password!')")
        connection.execute("INSERT INTO users VALUES ('rabl4950', 'raquel', 'black', 'tellmeyourname')")
        connection.execute("INSERT INTO users VALUES ('juja1425', 'juliette', 'jabroni', 'secret')")
        connection.execute("INSERT INTO users VALUES ('jodo3849', 'john', 'doe', 'joejoemonkeybo')")
        connection.execute("INSERT INTO users VALUES ('besm4241', 'becky', 'smith', 'qwerty1234')")

def cleanup_database():
    '''
    Drop the `users` table from database on server shutdown
    '''
    with sqlite3.connect(DB_NAME) as connection:
        connection.execute("DROP TABLE users")

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.staticdir.root': os.path.abspath(sys.path[0])
        },
        '/database': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type', 'text/plain')],
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './public'
        }
    }

    cherrypy.engine.subscribe('start', setup_database)
    cherrypy.engine.subscribe('stop', cleanup_database)

    webapp = InjectionDemo()
    webapp.database = DatabaseHandler()
    cherrypy.quickstart(webapp, '/', conf)


from multiprocessing import context
from django.shortcuts import redirect, render
from login_app.models import *
from fav_books_app.models import *
from django.contrib import messages

def show_books(request):
    user_id = request.session['userid']
    context = {
        "user": User.objects.get(id = user_id),
        "books": Book.objects.all()
        }
    return render(request, "books.html", context)

# method should redirect to the root page and delete the session
# (so that the user can't access to the wall page anymore, unless he/she login again)
def logout(request):
    request.session.clear()
    return redirect("/")

def add_book(request):
    errors = Book.objects.basic_validator(request.POST)
    if len(errors) > 0:
        for key, value in errors.items():
            messages.error(request, value)
        return redirect('/books')

    user_id = request.session['userid']
    this_user = User.objects.get(id = user_id)
    this_book = Book.objects.create(
                                    title = request.POST['title'],
                                    desc = request.POST['desc'],
                                    uploaded_by = this_user
                                    )
    this_user.liked_books.add(this_book)
    return redirect("/books")

def like(request):
    user_id = request.session['userid']
    book_id = request.POST.get('book_id', False)
    this_user = User.objects.get(id = user_id)
    this_book = Book.objects.get(id = book_id)
    this_user.liked_books.add(this_book)
    return redirect(f"/books/{book_id}")

def show_info(request, book_id):
    user_id = request.session['userid']
    this_user = User.objects.get(id = user_id)
    this_book = Book.objects.get(id = book_id)
    context={
            "user": this_user,
            "book": this_book
            }
    return render(request, "book_info.html", context)

def update(request):
    book_id = request.POST['book_id']
    errors = Book.objects.basic_validator(request.POST)
    if len(errors) > 0:
        for key, value in errors.items():
            messages.error(request, value)
        return redirect(f"/books/{book_id}")

    this_book = Book.objects.get(id = book_id)
    this_book.title = request.POST['title']
    this_book.desc = request.POST['desc']
    this_book.save()
    return redirect(f"/books/{book_id}")

def delete(request):
    book_id = request.POST['book_id']
    this_book = Book.objects.get(id = book_id)
    this_book.delete()
    return redirect ("/books")

def unlike (request):
    book_id = request.POST['book_id']
    user_id = request.session['userid']
    this_user = User.objects.get(id = user_id)
    this_book = Book.objects.get(id = book_id)
    this_user.liked_books.remove(this_book)
    return redirect(f"/books/{book_id}")

def show_fav_books(request):
    user_id = request.session['userid']
    this_user = User.objects.get(id = user_id)
    fav_books = this_user.liked_books.all()
    context={
            "user": this_user,
            "fav_books": fav_books
            }
    return render(request, "fav_books.html", context)


# imports
import cv2
import numpy as np
import math
import time

# variables
result_window = "Display Window"
#clear = lambda: os.system('clear')
start_time = 0

# functions
def drawdotted(img, pt1, pt2, color, thickness=1):

    dist = ((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) ** .5
    pts = []

    for i in np.arange(0, dist, 5):
        r = i / dist
        x = int((pt1[0] * (1 - r) + pt2[0] * r) + .5)
        y = int((pt1[1] * (1 - r) + pt2[1] * r) + .5)
        p = (x, y)
        pts.append(p)

    for p in pts:
        cv2.circle(img, p, thickness, color, -1)

def compute_ptr(bb, img, w, h):

    # handle
    if len(bb) < 2:
        print ("No Markers Detected")
        time.sleep(1)
        return 1

    # preliminary computations
    w = float(w)
    h = float(h)
    height, width, channels = img.shape  # width and height of image
    # centers
    c0 = width / 2, height / 2  # center of image
    c1 = ((bb[0][0][0] + bb[0][1][0]) / 2), ((bb[0][0][1] + bb[0][1][1]) / 2)  # center of one marker
    c2 = ((bb[-1][0][0] + bb[-1][1][0]) / 2), ((bb[-1][0][1] + bb[-1][1][1]) / 2)  # center of other marker
    # midpoint
    m = (c1[0] + c2[0]) / 2, (c1[1] + c2[1]) / 2  # midpoint of line connecting markers
    # radius for circles
    radius1 = int((math.sqrt(w * w + h * h)) / float(2))  # radius for marker circles
    radius2 = int(radius1 / 2)

    # centers
    # for center
    color = (0, 0, 255)
    cv2.circle(img, c0, int(radius2), color, thickness=2, lineType=8, shift=0)
    cv2.line(img, (c0[0], c0[1] - radius1), (c0[0], c0[1] + radius1), color, 2)
    cv2.line(img, (c0[0] - radius1, c0[1]), (c0[0] + radius1, c0[1]), color, 2)
    # for markers midpoint and lines
    color = (255, 0, 0)
    cv2.circle(img, m, radius2 / 2, color, thickness=2, lineType=8, shift=0)
    cv2.circle(img, c1, radius1, color, thickness=2, lineType=8, shift=0)
    cv2.circle(img, c2, radius1, color, thickness=2, lineType=8, shift=0)
    drawdotted(img, c1, c2, color, thickness=1)

    # ptr
    color = (0, 200, 0)
    # pan
    x1 = c0[0]
    x2 = m[0]
    pan = x1 - x2
    ori = ''
    print("")
    if pan >= 0:
        cv2.arrowedLine(img, (x1, c0[1]), (x2, c0[1]), color, thickness=2, tipLength=0.2)
        ori = "towards right."
    elif pan < 0:
        cv2.arrowedLine(img, (x1, c0[1]), (x2, c0[1]), color, thickness=2, tipLength=0.2)
        ori = "towards left."
    print ("Pan: %d pixels %s" % (abs(pan), ori))

    # tilt
    y1 = c0[1]
    y2 = m[1]
    tilt = y1 - y2
    if tilt >= 0:
        cv2.arrowedLine(img, (m[0], y1), (m[0], y2), color, thickness=2, tipLength=0.1)
        ori = "downwards."

    elif tilt < 0:
        cv2.arrowedLine(img, (m[0], y1), (m[0], y2), color, thickness=2, tipLength=0.1)
        ori = "upwards."
    print ("Tilt: %d pixels %s" % (abs(tilt), ori))

    # roll
    if (c2[0] - c1[0]) == 0:
        print("Only one marker detected!")
        time.sleep(1)
        return 1
    slope = float(c2[1] - c1[1]) / (c2[0] - c1[0])
    angle = np.rad2deg(np.arctan(float(slope)))
    if -45 < angle < 45:
        if pan >= 0:
            if angle >= 0:
                ori1 = "horizontal"
                ori2 = "clockwise"
            else:
                ori1 = "horizontal"
                ori2 = "anti-clockwise"
            cv2.line(img, (m[0], m[1]), (m[0] - abs(m[0] - c1[0]), m[1]), (0, 0, 255), 2)
            cv2.ellipse(img, (m[0], m[1]), (abs(m[0] - c2[0]), abs(m[0] - c2[0])), 0, 180, 180 + angle, (0, 200, 0), 2)
        else:
            if angle >= 0:
                ori1 = "horizontal"
                ori2 = "anti-clockwise"
            else:
                ori1 = "horizontal"
                ori2 = "clockwise"
            cv2.line(img, (m[0], m[1]), (m[0] + abs(m[0] - c2[0]), m[1]), (0, 0, 255), 2)
            cv2.ellipse(img, (m[0], m[1]), (abs(m[0] - c2[0]), abs(m[0] - c2[0])), 0, 360 + angle, 360, (0, 200, 0), 2)
        print ("Roll: %d degrees from %s axis in %s direction." % (abs(angle), ori1, ori2))
    else:
        if tilt >= 0:
            if angle >= 0:
                j = (90 - angle)
                ori1 = "vertical"
                ori2 = "anti-clockwise"
            else:
                j = -(90 + angle)
                ori1 = "vertical"
                ori2 = "clockwise"
            cv2.line(img, (m[0], m[1]), (m[0], m[1] - abs(m[1] - c2[1])), (0, 0, 255), 2)
            cv2.ellipse(img, (m[0], m[1]), (abs(m[1] - c2[1]), abs(m[1] - c2[1])), 0, 270, 270 - j, (0, 200, 0), 2)

        else:
            if angle >= 0:
                j = (90 - angle)
                ori1 = "vertical2"
                ori2 = "anti-lockwise"
            else:
                j = -(90 + angle)
                ori1 = "vertical2"
                ori2 = "clockwise"
            cv2.line(img, (m[0], m[1]), (m[0], m[1] + abs(m[1] - c2[1])), (0, 0, 255), 2)
            cv2.ellipse(img, (m[0], m[1]), (abs(m[1] - c2[1]), abs(m[1] - c2[1])), 0, 90 - j, 90, (0, 200, 0), 2)
        print ("Roll: %d degrees from %s axis in %s direction." %((90 - abs(angle)), ori1, ori2))
    print("--- %s seconds ---" % (time.time() - start_time))
    cv2.imshow(result_window, img)
    cv2.waitKey(1)

def read_frames():
    # read from camera
    camera = cv2.VideoCapture(1)
    for i in range(10):
        return_value, image = camera.read()
        cv2.imwrite('opencv'+str(i)+'.png', image)

def template_matching(img_rgbs, str1):

    # template matching
    # read images
    img_rgb = img_rgbs
    templatec = cv2.imread(str1, cv2.IMREAD_COLOR)
    templateg = cv2.imread(str1, 0)

    # create windows
    cv2.namedWindow(result_window, cv2.WINDOW_AUTOSIZE)

    # width and height
    w, h = templateg.shape[::-1]

    res = cv2.matchTemplate(img_rgb, templatec, cv2.TM_CCOEFF_NORMED)
    threshold = 0.6
    loc = []
    bb = []
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (255, 0, 0), 2)
        bb.append((pt, (pt[0] + w, pt[1] + h)))

    compute_ptr(bb, img_rgb, w, h)

def main():

    print("Gimbal Stabilization System")
    camera = cv2.VideoCapture(0)
    while True:
        #clear()
        k = cv2.waitKey(1) & 0xFF
        # press 'q' to exit
        if k == ord('q'):
            break
        start_time = time.time()
        return_value, image = camera.read()
        template_matching(image, "template_red (2).png")

if __name__ == '__main__':
    main()


import math
import json
import os
import csv

from src.userdata_template import template

def make_spirit_upstone_dict():
    r = csv.reader(open('data/spirit.csv', encoding='utf-8'))
    d = {
        '일반': dict(),
        '고급': dict(),
        '영웅': dict(),
        '전설': dict()
    }
    for l in r:
        level, normal, high, hero, legend = l
        level, normal, high, hero, legend = int(level), int(normal), int(high), int(hero), int(legend)
        d['일반'][level] = normal
        d['고급'][level] = high
        d['영웅'][level] = hero
        d['전설'][level] = legend

    return d

spirit_upstone = make_spirit_upstone_dict()

def calc_phys_atk(userdata, costume_option, pet_option, egg_option,
                  weapon_own, weapon_mount, spirit_own, spirit_mount):
    # calculate atk +
    atk = 17 \
        + userdata['능력치']['공격력'] \
        + userdata['펜던트']['유혹'] \
        + userdata['펜던트']['독수리'] \
        + userdata['스킬-패시브']['플루토의 낫'] * 50

    # calculate atk *
    atk *= (userdata['스킬-패시브']['주피터의 지혜'] / 100 + 1) * \
            (costume_option['공격력'] / 100 + 1) * \
            (pet_option['공격력'] / 100 + 1) * \
            (userdata['제물']['벌집'] * 4 / 100 + 1) * \
            (userdata['컬렉션']['공격력'] / 100 + 1) * \
            (egg_option / 100 + 1) * \
            ((weapon_mount['공격력'] + weapon_own['공격력']) / 100 + 1) * \
            ((spirit_mount['공격력'] + spirit_own['공격력']) / 100 + 1) * \
            (userdata['스킬-패시브']['만해'] / 100 + 1)

    return atk

def calc_magi_atk(userdata, costume_option, pet_option, egg_option,
                  weapon_own, weapon_mount, spirit_own, spirit_mount):
    # calculate atk +
    atk = userdata['능력치']['마력'] \
        + userdata['펜던트']['숲'] \
        + userdata['펜던트']['십자가'] \
        + userdata['스킬-패시브']['플루토의 보주'] * 50

    # calculate atk *
    atk *= (userdata['스킬-패시브']['넵튠의 지혜'] * 2 / 100 + 1) * \
            (costume_option['마력'] / 100 + 1) * \
            (pet_option['마력'] / 100 + 1) * \
            (userdata['제물']['해골'] * 4 / 100 + 1) * \
            (userdata['컬렉션']['마력'] / 100 + 1) * \
            (egg_option / 100 + 1) * \
            ((weapon_mount['마력'] + weapon_own['마력']) / 100 + 1) * \
            ((spirit_mount['마력'] + spirit_own['마력']) / 100 + 1) * \
            (userdata['스킬-패시브']['만해'] / 100 + 1)

    return atk

def calc_hp(userdata, costume_option,
                weapon_own, spirit_own, spirit_mount):
    # calculate HP +
    hp = userdata['능력치']['HP 증가'] + \
            userdata['스킬-패시브']['플루토의 옷'] * 50

    # calculate HP *
    hp *= (userdata['스킬-패시브']['불칸의 지혜'] / 100 + 1) * \
            (costume_option['체력'] / 100 + 1) * \
            (userdata['제물']['식기'] * 10 / 100 + 1) * \
            (weapon_own['HP 증가'] / 100 + 1) * \
            ((spirit_mount['HP 증가'] + spirit_own['HP 증가']) / 100 + 1) * \
            (userdata['스킬-패시브']['만해'] / 100 + 1)

    return hp

def calc_mp(userdata, weapon_own):
    mp = userdata['능력치']['MP 증가'] + \
            userdata['스킬-패시브']['사신의 의무'] * 5 + \
            userdata['제물']['술잔'] * 20 + \
            weapon_own['MP 증가'] + \
            userdata['펜던트']['사신의 눈물']

    return mp

def calc_crit_chance(userdata, offering_option, weapon_mount):
    crit_chance = userdata['능력치']['치명타 확률 증가'] + \
                    userdata['스킬-패시브']['아폴로의 지혜'] * 0.2 + \
                    offering_option['치명타 확률'] + \
                    weapon_mount['치명타 확률']

    return crit_chance

def calc_crit_dmg(userdata, costume_option, offering_option,
                    weapon_own, weapon_mount, spirit_own, spirit_mount):
    crit_dmg = userdata['능력치']['치명타 데미지 증가'] + \
                userdata['펜던트']['천둥'] + \
                userdata['스킬-패시브']['영웅의 기운'] * 2 + \
                costume_option['치명타 데미지'] + \
                offering_option['치명타 데미지'] + \
                weapon_own['치명타 데미지'] + \
                weapon_mount['치명타 데미지'] + \
                spirit_own['치명타 데미지'] + \
                spirit_mount['치명타 데미지'] + \
                100

    crit_dmg *= (spirit_own['치명타 데미지 곱'] / 100 + 1)

    return crit_dmg

def calc_reaper_chance(userdata):
    reapear_chance = userdata['능력치']['사신타 확률 증가'] + \
                        userdata['펜던트']['광기'] + \
                        userdata['스킬-패시브']['타나토스의 손길'] * 0.02 + \
                        userdata['제물']['귀걸이'] * 0.2

    return reapear_chance

def calc_reaper_dmg (userdata):
    reapear_dmg = 150 * \
                    (userdata['능력치']['사신타 데미지 증가'] / 100 + 1) * \
                    (userdata['스킬-패시브']['타나토스의 분노'] * 0.02 / 100 + 1)

    return reapear_dmg

def calc_immune(userdata, costume_option):
    immune = costume_option['상태이상 면역']

    return immune

def calc_hp_regen(userdata, weapon_own):
    hp_regen = userdata['스킬-패시브']['지상의 기도'] * 0.25 + \
                weapon_own['HP 회복']

    return hp_regen

def calc_evade(userdata, costume_option, weapon_own):
    evade = costume_option['회피율'] + \
            userdata['스킬-패시브']['미네르바의 지혜'] * 2 + \
            weapon_own['회피율']

    return evade

def calc_exp(userdata, costume_option, pet_option):
    exp = userdata['스킬-패시브']['전설의 주문'] * 2 + \
            costume_option['경험치'] + \
            pet_option['경험치'] + \
            userdata['제물']['반지'] * 10

    return exp

def calc_gold(userdata, costume_option, pet_option):
    gold = userdata['펜던트']['행운'] + \
            userdata['스킬-패시브']['블레싱'] * 20 + \
            costume_option['골드'] + \
            pet_option['경험치'] + \
            userdata['제물']['촛대'] * 10

    return gold

def calc_weapon_drop(userdata, weapon_own):
    drop = 6 * \
            (weapon_own['드랍률'] / 100 + 1)

    return drop

def calc_spirit_drop(userdata):
    drop = 2

    return drop

def calc_min_max_dmg(userdata):
    min_dmg = 80 + \
                userdata['펜던트']['성검']

    max_dmg = 120

    return min_dmg, max_dmg

def calc_status(userdata, costume_option, pet_option, egg_option, offering_option,
                weapon_own, weapon_mount, spirit_own, spirit_mount):
    phys_atk = calc_phys_atk(userdata, costume_option, pet_option, egg_option,
                                weapon_own, weapon_mount, spirit_own, spirit_mount)
    magi_atk = calc_magi_atk(userdata, costume_option, pet_option, egg_option,
                                weapon_own, weapon_mount, spirit_own, spirit_mount)
    crit_chance = calc_crit_chance(userdata, offering_option, weapon_mount)
    crit_dmg = calc_crit_dmg(userdata, costume_option, offering_option,
                    weapon_own, weapon_mount, spirit_own, spirit_mount)
    reaper_chance = calc_reaper_chance(userdata)
    reaper_dmg = calc_reaper_dmg (userdata)

    phys_power = phys_atk * \
                    ((crit_dmg / 100 - 1) * crit_chance / 100 + 1) * \
                    ((reaper_dmg / 100 - 1) * reaper_chance / 100 + 1)

    magi_power = magi_atk * \
                    ((crit_dmg / 100 - 1) * crit_chance / 100 + 1) * \
                    ((reaper_dmg / 100 - 1) * reaper_chance / 100 + 1)

    whole_power = phys_power + magi_power

    return {
        '총 전투력': whole_power,
        '물공 전투력': phys_power,
        '마공 전투력': magi_power,
        '물리공격력': phys_atk,
        '마력': magi_atk,
        'HP': calc_hp(userdata, costume_option, weapon_own, spirit_own, spirit_mount),
        '치명타 확률(%)': crit_chance,
        '치명타 데미지(%)': crit_dmg,
        '사신타 확률(%)': reaper_chance,
        '사신타 데미지(%)': reaper_dmg,
        'MP': calc_mp(userdata, weapon_own),
        '상태이상 면역(%)': calc_immune(userdata, costume_option),
        'HP회복력(%)': calc_hp_regen(userdata, weapon_own),
        '회피(%)': calc_evade(userdata, costume_option, weapon_own),
        '경험치 추가 획득(%)': calc_exp(userdata, costume_option, pet_option),
        '골드 추가 획득(%)': calc_gold(userdata, costume_option, pet_option),
        '장비 드랍 확률(%)': calc_weapon_drop(userdata, weapon_own),
        '정령 드랍 확률(%)': calc_spirit_drop(userdata),
        '용의 알 적용 / 최대(%)': calc_dragon_egg(userdata),
        '최소/최대 데미지(%)': calc_min_max_dmg(userdata)
    }

def calc_costume(userdata):
    normal = userdata['코스튬']['일반']
    normal_value = userdata['코스튬']['일반수치']
    high = userdata['코스튬']['고급']
    high_value = userdata['코스튬']['고급수치']

    phys = (normal_value[0] if normal[0] == '공격력' else 0) + \
            (normal_value[1] if normal[1] == '공격력' else 0) + \
            (high_value[0] if high[0] == '공격력' else 0) + \
            (high_value[1] if high[1] == '공격력' else 0)

    magi = (normal_value[0] if normal[0] == '마력' else 0) + \
            (normal_value[4] if normal[4] == '마력' else 0) + \
            (high_value[0] if high[0] == '마력' else 0) + \
            (high_value[1] if high[1] == '마력' else 0)

    hp = (normal_value[1] if normal[1] == '체력' else 0) + \
            (high_value[0] if high[0] == '체력' else 0) + \
            (high_value[1] if high[1] == '체력' else 0)

    immune = normal_value[2] if normal[2] == '상태이상 면역' else 0
    evade = normal_value[2] if normal[2] == '회피율' else 0
    gold = normal_value[3] if normal[3] == '골드' else 0
    exp = normal_value[3] if normal[3] == '경험치' else 0

    crit_dmg = (normal_value[4] if normal[4] == '치명타 데미지' else 0) + \
            (high_value[0] if high[0] == '치명타 데미지' else 0) + \
            (high_value[1] if high[1] == '치명타 데미지' else 0)

    return {
        '공격력': phys,
        '마력': magi,
        '체력': hp,
        '상태이상 면역': immune,
        '회피율': evade,
        '골드': gold,
        '경험치': exp,
        '치명타 데미지': crit_dmg
    }

def calc_pet(userdata):
    pet = {}
    for key in userdata['펫'].keys():
        pet[key] = 0 if key != userdata['펫']['active'] else userdata['펫'][key]

    return pet

def calc_dragon_egg(userdata):
    egg_limit = (userdata['레벨'] // 3) * 3 * 5 + 6000
    egg = userdata['용알']

    valid_egg = min(egg, egg_limit)

    return math.trunc(valid_egg/15), math.trunc(egg_limit/15)

def calc_all_weapon_own_effect(userdata):
    result = {
        '공격력': 0.0,
        '마력': 0.0,
        'HP 회복': 0.0,
        '드랍률': 0.0,
        'HP 증가': 0.0,
        'MP 증가': 0.0,
        '회피율': 0.0,
        '치명타 데미지': 0.0,
        '공격력 추가': 0.0,
        '마력 추가': 0.0
    }
    for key in ['저급', '일반', '고급', '영웅', '전설', '신', '불멸']:
        for i in range(4):
            own_effect = calc_weapon_own_effect(key, i, userdata['장비'][key][i])
            for effect in result.keys():
                result[effect] += own_effect[effect]

    result['공격력'] += result['공격력 추가']
    result['마력'] += result['마력 추가']

    del result['공격력 추가']
    del result['마력 추가']

    return result

def calc_weapon_own_effect(rank, idx, level):
    base = {
        "저급": [10, 13, 17, 20],
        "일반": [70, 90, 110, 140],
        "고급": [300, 400, 500, 600],
        "영웅": [800, 1000, 1200, 1400],
        "전설": [2000, 2400, 2800, 3200],
        "신": [9000, 12000, 15000, 20000],
        "불멸": [68000, 238000, 807500, 4675000]
    }
    constants = {
        '저급': (0.33, 3),
        '일반': (0.33, 2),
        '고급': (0.33, 0),
        '영웅': (0.33, 0),
        '전설': (0.33, 0),
        '신': (1, 0),
        '불멸': (1, 0),
    }

    phys_n_magi = round(base[rank][idx] * (level ** 0.461) * constants[rank][0], constants[rank][1])

    phys = phys_n_magi
    magi = phys_n_magi
    hp_regen, item_drop, hp, mp, evade, crit_dmg, phys2, magi2 = 0, 0, 0, 0, 0, 0, 0, 0
    if rank == '전설' and idx == 0:
        hp += level * 10
    elif rank == '전설' and idx == 1:
        mp += level * 5
    elif rank == '전설' and idx == 2:
        evade += min(level * 0.1, 10)
    elif rank == '전설' and idx == 3:
        hp_regen = 0.25 * level
    elif rank == '신' and idx == 0:
        evade += min(level * 0.1, 10)
    elif rank == '신' and idx == 1:
        item_drop = min(level, 100)
    elif rank == '신' and idx == 2:
        phys2 += round(base[rank][idx] * (level ** 0.461) * 0.33, 3)
    elif rank == '신' and idx == 3:
        magi2 += round(base[rank][idx] * (level ** 0.461) * 0.33, 3)
    elif rank == '불멸' and idx == 0:
        hp += level * 15
    elif rank == '불멸' and idx == 1:
        crit_dmg = level * 2.45
    elif rank == '불멸' and idx == 2:
        phys2 += round(base[rank][idx] * (level ** 0.461) * 0.388235294117647, 0)
    elif rank == '불멸' and idx == 3:
        magi2 += round(base[rank][idx] * (level ** 0.461) * 0.388235294117647, 0)

    return {
        '공격력': phys,
        '마력': magi,
        'HP 회복': hp_regen,
        '드랍률': item_drop,
        'HP 증가': hp,
        'MP 증가': mp,
        '회피율': evade,
        '치명타 데미지': crit_dmg,
        '공격력 추가': phys2,
        '마력 추가': magi2
    }

def calc_all_spirit_own_effect(userdata):
    result = {
        '공격력': 0.0,
        '마력': 0.0,
        'HP 증가': 0.0,
        '치명타 데미지': 0.0,
        '치명타 데미지 곱': 0.0
    }

    for rank in ['일반', '고급', '영웅', '전설']:
        for i in range(4):
            spirit_effect = calc_spirit_own_effect(rank, i, userdata['정령'][rank][i])
            for key in spirit_effect.keys():
                result[key] += spirit_effect[key]

    return result

def calc_spirit_own_effect(rank, idx, level):
    base = {
        '일반': 2.5,
        '일반_치명': 5,
        '고급': 5.0,
        '고급_치명': 15,
        '영웅': 10,
        '영웅_치명': 30,
        '전설': 30,
        '전설_치명': 90
    }
    base_crit_mult = [(2.5, 0.05), (5, 0.1), (10, 0.2), (20, 0.3)]

    hp, phys, magi, crit_dmg, crit_dmg_mult = 0, 0, 0, 0, 0
    if idx == 0:
        hp += base[rank] + level * base[rank] * 0.01 if level >= 0 else 0
    if idx == 1:
        phys += base[rank] + level * base[rank] * 0.01 if level >= 0 else 0
    if idx == 2:
        magi += base[rank] + level * base[rank] * 0.01 if level >= 0 else 0
    if idx == 3:
        crit_dmg += base[rank + '_치명'] + level * base[rank + '_치명'] * 0.01 if level >= 0 else 0

    if rank == '전설':
        crit_dmg_mult = base_crit_mult[idx][0] + level * base_crit_mult[idx][1] if level >= 0 else 0

    return {
        '공격력': phys,
        '마력': magi,
        'HP 증가': hp,
        '치명타 데미지': crit_dmg,
        '치명타 데미지 곱': crit_dmg_mult
    }

def calc_offering(userdata):
    crit_chance = min(userdata['제물']['말발굽'], 50) * 0.4 + \
                    max(userdata['제물']['말발굽']-50, 0) * 0.2

    crit_dmg = min(userdata['제물']['고글'], 75) * 5 + \
                max(userdata['제물']['고글']-75, 0) * 10

    return {
        '치명타 데미지': crit_dmg,
        '치명타 확률': crit_chance
    }

def calc_needed_upstone(rank, idx, level):
    base = {
        "저급": 1,
        "일반": 11.18035,
        "고급": 31.62279,
        "영웅": 89.44273,
        "전설": 353.55385,
        "신": 1837.1174,
        "불멸": 31622.7779
    }
    immortal_adder = [300000, 5000000, 10000000, 25000000]

    if level == 200:
        return 0
    if level == 0:
        return '-'

    adder = immortal_adder[idx] if rank == '불멸' else 0

    return int(base[rank] * (level ** 1.5)) + adder

def calc_spirit_full_needed_upstone(rank, level):
    if level == -1:
        return '-'

    req_ups = 0
    for i in range(level, 201):
        req_ups += calc_spirit_needed_upstone(rank, i)

    return req_ups

def calc_spirit_needed_upstone(rank, level):
    if level == -1:
        return '-'

    return spirit_upstone[rank][level]

def calc_full_needed_upstone(rank, idx, level):
    if level == 0:
        return '-'

    req_ups = 0
    for i in range(level, 201):
        req_ups += calc_needed_upstone(rank, idx, i)

    return req_ups

def get_weapon_effect(userdata):
    rank, idx = userdata['장착 무기']
    level = userdata['장비'][rank][idx]

    return calc_weapon_effect(rank, idx, level)

def calc_weapon_effect(rank, idx, level):
    base = {
        "저급": [10, 13, 17, 20],
        "일반": [70, 90, 110, 140],
        "고급": [300, 400, 500, 600],
        "영웅": [800, 1000, 1200, 1400],
        "전설": [2000, 2400, 2800, 3200],
        "신": [9000, 12000, 15000, 20000],
        "불멸": [68000, 238000, 807500, 4675000]
    }
    crit_chance_base = {
        '영웅': [0.1, 0.11, 0.12, 0.13],
        '전설': [0.1, 0.12, 0.14, 0.16],
        '신': [0.1, 0.15, 0.2, 0.25],
        '불멸': [25, 25, 25, 25]
    }
    crit_dmg_base = {
        '신': [2, 2.2, 2.4, 2.7],
        '불멸': [3, 3.5, 4, 4.5]
    }

    phys_n_magi, crit_chance, crit_dmg = 0, 0, 0
    if rank == '저급':
        phys_n_magi = round(base[rank][idx] * \
                        (level ** 0.461), 3)
    elif rank == '일반':
        phys_n_magi = round(base[rank][idx] * \
                        (level ** 0.461), 2)
    else:
        phys_n_magi = round(base[rank][idx] * \
                        (level ** 0.461), 0)

    if rank in crit_chance_base.keys():
        if rank != '불멸':
            crit_chance = crit_chance_base[rank][idx] * \
                            min(level, 100)
        else:
            crit_chance = crit_chance_base[rank][idx]

    if rank in crit_dmg_base.keys():
        crit_dmg = crit_dmg_base[rank][idx] * \
                    level

    return {
        '공격력': phys_n_magi,
        '마력': phys_n_magi,
        '치명타 확률': crit_chance,
        '치명타 데미지': crit_dmg
    }

def get_spirit_effect(userdata):
    rank, idx = userdata['장착 정령']
    level = userdata['정령'][rank][idx]

    return calc_spirit_effect(rank, idx, level)

def calc_spirit_effect(rank, idx, level):
    base = {
        '일반': (5, 0.05),
        '일반_치명': (50, 0.5),
        '고급': (10, 0.1),
        '고급_치명': (150, 1.5),
        '영웅': (20, 0.2),
        '영웅_치명': (300, 3),
        '전설': (60, 0.6),
        '전설_치명': (900, 6)
    }

    hp, phys, magi, crit_dmg = 0, 0, 0, 0
    if idx == 0:
        hp = base[rank][0] + \
                level * base[rank][1] if level >= 0 else 0
    elif idx == 1:
        phys = base[rank][0] + \
                level * base[rank][1] if level >= 0 else 0
    elif idx == 2:
        magi = base[rank][0] + \
                level * base[rank][1] if level >= 0 else 0
    elif idx == 3:
        phys = base[rank][0] + \
                level * base[rank][1] if level >= 0 else 0
        magi = base[rank][0] + \
                level * base[rank][1] if level >= 0 else 0
        crit_dmg = base[rank + '_치명'][0] + \
                level * base[rank + '_치명'][1] if level >= 0 else 0

    return {
        '공격력': phys,
        '마력': magi,
        'HP 증가': hp,
        '치명타 데미지': crit_dmg
    }

def load_data(path):
    f = open(path, encoding='utf-16')
    dic = json.load(f)

    return dic

def save_data(path, data):
    fp = open(path, 'w', encoding='utf-16')
    json.dump(data, fp, indent=4)

def initial_load_data():
    if not os.path.isfile('data/userdata.json'):
        userdata = template
    else:
        userdata = load_data('data/userdata.json')

    return userdata

def keep_seven_digits(num):
    if num == 0:
        return num
    power_of_ten = int(math.log10(num)) + 1 - 7
    num = num // (10 ** power_of_ten) * (10 ** power_of_ten)

    return int(num)

def transform_korean_amount_string(num):
    maj_units = ['만 ', '억 ', '조 ', '경 ', '해 ', '자 ', '양 ', '구 ', '간 ', '정 ', '재 ', '극 ']
    units     = ['']
    for mm in maj_units:
        units.extend(['', '', ''])
        units.append(mm)

    list_amount = list(str(num)) # 라운딩한 숫자를 리스트로 바꾼다
    list_amount.reverse() # 일, 십 순서로 읽기 위해 순서를 뒤집는다

    str_result = '' # 결과
    num_len_list_amount = len(list_amount)

    for i in range(num_len_list_amount):
        str_num = list_amount[i]

        if num_len_list_amount >= 9 and i % 4 == 0 and ''.join(list_amount[i:i+4]) == '0000':
            continue

        if str_num == '0':
            if i % 4 == 0:
                str_result = str_num + units[i] + str_result
            else:
                all_zero = True
                for j in range(i, (i // 4) * 4 + 4):
                    if len(list_amount) > (j+1) and list_amount[j] != '0':
                        all_zero = False

                if not all_zero:
                    str_result = str_num + units[i] + str_result

        else:
            str_result = str_num + units[i] + str_result

    str_result = str_result.strip()
    if len(str_result) == 0:
        return None

    return str_result

def transform_english_amount_string(num, r=2):
    if num == '-':
        return '-'
    int_part = int(num)
    float_part = round(num - int_part, r)
    is_float = float_part != 0 and r != 0

    list_num = list(str(int_part))
    list_num += list(str(float_part))[1:] if is_float else []
    list_num.reverse()

    result = ''
    int_start = not is_float
    int_idx = 0
    for i in range(len(list_num)):
        if list_num[i] == '.':
            int_start = True
            result = list_num[i] + result
        elif is_float and not int_start:
            result = list_num[i] + result
        else:
            if int_idx % 3 == 0 and int_idx > 0:
                result = list_num[i] + ',' + result
            else:
                result = list_num[i] + result
            int_idx += 1

    return result

def check_int(num):
    if isinstance(num, str):
        try:
            num = float(num)
        except:
            return False

    if isinstance(num, int) or (isinstance(num, float) and num.is_integer()):
        return True

    return False


from django.contrib.auth.forms import UserChangeForm, UserCreationForm

from users.models import User

class CustomUserCreationForm(UserCreationForm):
    """Custom User creation form."""

    class Meta:
        model = User
        fields = '__all__'

class CustomUserChangeForm(UserChangeForm):
    """Custom User edit form."""

    class Meta:
        model = User
        fields = '__all__'


from __future__ import division

import random
from collections import OrderedDict
from decimal import Decimal
from functools import partial

import numpy as np
import pytz
from pyspark import Row
from pyspark.sql import SparkSession
from pyspark.sql.types import StringType, DecimalType, DoubleType, StructField, \
    IntegerType, StructType, DateType, TimestampType, ShortType, ArrayType

from petastorm.codecs import CompressedImageCodec, NdarrayCodec, \
    ScalarCodec
from petastorm.etl.dataset_metadata import materialize_dataset
from petastorm.etl.rowgroup_indexers import SingleFieldIndexer
from petastorm.etl.rowgroup_indexing import build_rowgroup_index
from petastorm.unischema import Unischema, UnischemaField, dict_to_spark_row

_DEFAULT_IMAGE_SIZE = (32, 16, 3)

TestSchema = Unischema('TestSchema', [
    UnischemaField('partition_key', np.unicode_, ()),
    UnischemaField('id', np.int64, ()),
    UnischemaField('id2', np.int32, (), ScalarCodec(ShortType()), False),  # Explicit scalar codec in some scalar fields
    UnischemaField('id_float', np.float64, ()),
    UnischemaField('id_odd', np.bool_, ()),
    UnischemaField('python_primitive_uint8', np.uint8, ()),
    UnischemaField('image_png', np.uint8, _DEFAULT_IMAGE_SIZE, CompressedImageCodec('png'), False),
    UnischemaField('matrix', np.float32, _DEFAULT_IMAGE_SIZE, NdarrayCodec(), False),
    UnischemaField('decimal', Decimal, (), ScalarCodec(DecimalType(10, 9)), False),
    UnischemaField('matrix_uint16', np.uint16, _DEFAULT_IMAGE_SIZE, NdarrayCodec(), False),
    UnischemaField('matrix_uint32', np.uint32, _DEFAULT_IMAGE_SIZE, NdarrayCodec(), False),
    UnischemaField('matrix_string', np.string_, (None, None,), NdarrayCodec(), False),
    UnischemaField('empty_matrix_string', np.string_, (None,), NdarrayCodec(), False),
    UnischemaField('matrix_nullable', np.uint16, _DEFAULT_IMAGE_SIZE, NdarrayCodec(), True),
    UnischemaField('sensor_name', np.unicode_, (1,), NdarrayCodec(), False),
    UnischemaField('string_array_nullable', np.unicode_, (None,), NdarrayCodec(), True),
    UnischemaField('integer_nullable', np.int32, (), nullable=True),
])

def _random_binary_string_gen(max_length):
    """Returns a single random string up to max_length specified length that may include \x00 character anywhere in the
    string"""
    size = random.randint(0, max_length)
    return ''.join(random.choice(('\x00', 'A', 'B')) for _ in range(size))

def _random_binary_string_matrix(rows, cols, max_length):
    """Returns a list of lists of random strings"""
    return [[_random_binary_string_gen(max_length) for _ in range(cols)] for _ in range(rows)]

def _randomize_row(id_num):
    """Returns a row with random values"""
    row_dict = {
        TestSchema.id.name: np.int64(id_num),
        TestSchema.id2.name: np.int32(id_num % 2),
        TestSchema.id_float.name: np.float64(id_num),
        TestSchema.id_odd.name: np.bool_(id_num % 2),
        TestSchema.partition_key.name: np.unicode_('p_{}'.format(int(id_num / 10))),
        TestSchema.python_primitive_uint8.name: np.random.randint(0, 255, dtype=np.uint8),
        TestSchema.image_png.name: np.random.randint(0, 255, _DEFAULT_IMAGE_SIZE).astype(np.uint8),
        TestSchema.matrix.name: np.random.random(size=_DEFAULT_IMAGE_SIZE).astype(np.float32),
        TestSchema.decimal.name: Decimal(np.random.randint(0, 255) / Decimal(100)),
        TestSchema.matrix_uint16.name: np.random.randint(0, 2 ** 16 - 1, _DEFAULT_IMAGE_SIZE).astype(np.uint16),
        TestSchema.matrix_uint32.name: np.random.randint(0, 2 ** 32 - 1, _DEFAULT_IMAGE_SIZE).astype(np.uint32),
        TestSchema.matrix_string.name: np.asarray(_random_binary_string_matrix(2, 3, 10)).astype(np.bytes_),
        TestSchema.empty_matrix_string.name: np.asarray([], dtype=np.string_),
        TestSchema.matrix_nullable.name: None,
        TestSchema.sensor_name.name: np.asarray(['test_sensor'], dtype=np.unicode_),
        TestSchema.string_array_nullable.name:
            None if id_num % 5 == 0 else np.asarray([], dtype=np.unicode_)
            if id_num % 4 == 0 else np.asarray([str(i + id_num) for i in range(2)], dtype=np.unicode_),
        TestSchema.integer_nullable.name: None if id_num % 2 else np.int32(id_num),
    }
    return row_dict

def create_test_dataset(tmp_url, rows, num_files=2, spark=None, use_summary_metadata=False):
    """
    Creates a test dataset under tmp_dir, with rows and num_files that has TestSchema.
    :param tmp_url: The URL of the temp directory to store the test dataset in.
    :param rows: The number of rows for the dataset.
    :param num_files: The number of files to partition the data between.
    :param spark: An optional spark session to use
    :param use_summary_metadata: If True, _metadata file will be created.
    :return: A list of the dataset dictionary.
    """

    shutdown = False
    if not spark:
        spark_session = SparkSession \
            .builder \
            .appName('petastorm_end_to_end_test') \
            .master('local[*]')

        spark = spark_session.getOrCreate()
        shutdown = True
    spark_context = spark.sparkContext

    with materialize_dataset(spark, tmp_url, TestSchema, use_summary_metadata=use_summary_metadata):
        id_rdd = spark_context.parallelize(rows, numSlices=40)

        # Make up some random data and store it for referencing in the tests
        random_dicts_rdd = id_rdd.map(_randomize_row).cache()
        dataset_dicts = random_dicts_rdd.collect()

        def _partition_key_to_str(row):
            row['partition_key'] = str(row['partition_key'])
            return row

        random_dicts_rdd = random_dicts_rdd.map(_partition_key_to_str)

        random_rows_rdd = random_dicts_rdd.map(partial(dict_to_spark_row, TestSchema))

        # Create a spark dataframe with the random rows
        dataframe = spark. \
            createDataFrame(random_rows_rdd, TestSchema.as_spark_schema()).sort('id')

        # Save a parquet
        dataframe. \
            coalesce(num_files). \
            write.option('compression', 'none'). \
            partitionBy('partition_key'). \
            mode('overwrite'). \
            parquet(tmp_url)

    # Create list of objects to build row group indexes
    indexers = [
        SingleFieldIndexer(TestSchema.id.name, TestSchema.id.name),
        SingleFieldIndexer(TestSchema.sensor_name.name, TestSchema.sensor_name.name),
        SingleFieldIndexer(TestSchema.string_array_nullable.name, TestSchema.string_array_nullable.name),
        SingleFieldIndexer(TestSchema.partition_key.name, TestSchema.partition_key.name),
    ]
    build_rowgroup_index(tmp_url, spark_context, indexers)

    if shutdown:
        spark.stop()

    return dataset_dicts

def create_test_scalar_dataset(output_url, num_rows, num_files=4, spark=None, partition_by=None):
    """Creates a dataset in tmp_url location. The dataset emulates non-petastorm dataset, i.e. contains only native
    parquet types.

    These are the fields with mock data:
      'id', 'int_fixed_size_list', 'datetime', 'timestamp', 'string', 'string2', 'float64'

    :param output_url: Url specifying the location the parquet store is written to
    :param num_rows: Number of rows in the generated dataset
    :param num_files: Number of parquet files that will be written into the parquet store
    :param spark: An instance of spark session object. If `None` (default), a new spark session is created.
    :param partition_by: A list of fields to partition the parquet store by.
    :return: A list of records with a copy of the data written to the dataset.
    """

    partition_by = partition_by or []
    shutdown = False
    if not spark:
        spark_session = SparkSession \
            .builder \
            .appName('petastorm_end_to_end_test') \
            .master('local[*]')

        spark = spark_session.getOrCreate()
        shutdown = True
        hadoop_config = spark.sparkContext._jsc.hadoopConfiguration()
        hadoop_config.setInt('parquet.block.size', 100)

    def expected_row(i):
        result = {
            'id': np.int32(i),
            'id_div_700': np.int32(i // 700),
            'datetime': np.datetime64('2019-01-02'),
            'timestamp': np.datetime64('2005-02-25T03:30'),
            'string': np.unicode_('hello_{}'.format(i)),
            'string2': np.unicode_('world_{}'.format(i)),
            'float64': np.float64(i) * .66,
            'int_fixed_size_list': np.arange(1 + i, 10 + i).astype(np.int32)
        }
        return result

    expected_data = [expected_row(i) for i in range(num_rows)]

    expected_data_as_scalars = [{k: v.item() if isinstance(v, np.generic) else v for k, v in row.items()} for row
                                in expected_data]

    # np.datetime64 is converted to a timezone unaware datetime instances. Working explicitly in UTC so we don't need
    # to think about local timezone in the tests
    for row in expected_data_as_scalars:
        row['timestamp'] = row['timestamp'].replace(tzinfo=pytz.UTC)
        row['int_fixed_size_list'] = row['int_fixed_size_list'].tolist()
        row['nested_struct'] = {'nested_int': 0}

    rows = [Row(**OrderedDict(sorted(row.items(), key=lambda item: item[0]))) for row in expected_data_as_scalars]

    maybe_int_fixed_size_list_field = [StructField('int_fixed_size_list', ArrayType(IntegerType(), False), False)]

    # WARNING: surprisingly, schema fields and row fields are matched only by order and not name.
    # We must maintain alphabetical order of the struct fields for the code to work!!!
    schema = StructType(
        [
            StructField('datetime', DateType(), False),
            StructField('float64', DoubleType(), False),
            StructField('id', IntegerType(), False),
            StructField('id_div_700', IntegerType(), False),
        ] + maybe_int_fixed_size_list_field +
        [
            StructField('nested_struct', StructType([StructField('nested_int', IntegerType(), True)])),
            StructField('string', StringType(), False),
            StructField('string2', StringType(), False),
            StructField('timestamp', TimestampType(), False),
        ])

    dataframe = spark.createDataFrame(rows, schema)
    dataframe. \
        coalesce(num_files). \
        write.option('compression', 'none'). \
        mode('overwrite'). \
        partitionBy(*partition_by). \
        parquet(output_url)

    if shutdown:
        spark.stop()

    return expected_data

def create_many_columns_non_petastorm_dataset(output_url, num_rows, num_columns=1000, num_files=4, spark=None):
    """Creates a dataset with the following properties (used in tests)

    1. Has 1000 columns
    2. Each column is an int32 integer
    3. Parquet store consists of 4 files (controlled by ``num_files`` argument)

    :param output_url: The dataset is written to this url (e.g. ``file:///tmp/some_directory``)
    :param num_rows: Number of rows in the generated dataset
    :param num_columns: Number of columns (1000 is the default)
    :param num_files: Number of parquet files that will be created in the store
    :param spark: An instance of SparkSession object. A new instance will be created if non specified
    :return:
    """
    shutdown = False
    if not spark:
        spark_session = SparkSession \
            .builder \
            .appName('petastorm_end_to_end_test') \
            .master('local[*]')

        spark = spark_session.getOrCreate()
        shutdown = True

    column_names = ['col_{}'.format(col_id) for col_id in range(num_columns)]

    def generate_row(i):
        return {'col_{}'.format(col_id): i * 10000 for col_id, col_name in enumerate(column_names)}

    expected_data = [generate_row(row_number) for row_number in range(num_rows)]

    rows = [Row(**row) for row in expected_data]

    # WARNING: surprisingly, schema fields and row fields are matched only by order and not name.
    schema = StructType([StructField(column_name, IntegerType(), False) for column_name in column_names])

    dataframe = spark.createDataFrame(rows, schema)
    dataframe. \
        coalesce(num_files). \
        write.option('compression', 'none'). \
        mode('overwrite'). \
        parquet(output_url)

    if shutdown:
        spark.stop()

    return expected_data


class Codec:

    def encode(self, longUrl):
        """Encodes a URL to a shortened URL.

        :type longUrl: str
        :rtype: str
        """
        self.baseUrl = "http://tinyurl/"
        import hashlib, random
        self.store = {}
        m = hashlib.sha256(longUrl.encode('utf-8'))
        key = m[:5]
        import collections
        path = collections.defaultdict(0)

        while key in self.store:
            tmp = longUrl + str(random.random())
            m = hashlib.sha256(tmp.encode('utf-8'))
            key = m[:5]

        self.store[key] = longUrl
        return self.baseUrl + key

    def decode(self, shortUrl):
        """Decodes a shortened URL to its original URL.

        :type shortUrl: str
        :rtype: str
        """
        key = shortUrl.replace(self.baseUrl, '')
        return self.store[key]

s = Codec()
s.encode("https://leetcode.com/problems/design-tinyurl")


import numpy as np
import os
import sys
from scipy.misc import imread

def extract_patch(im, center, size):
    pass

def preprocess(im):
    assert im.ndim == 3
    im -= im.mean(axis=(0, 1))
    im /= im.std(axis=(0, 1))
    return im

def postprocess(x):
    pass

class DataHandler(object):
    def __init__(self, args):
        ''' Handles  preprocessed binary data and KITTI dataset

        Args:
            data_root (str): Path to raw png files
            util_root (str): Path to preprocessed binary data files
            num_tr_img (int): # of training images
            num_val_img (int): # of validation images
            num_val_loc (int): # of val patches
            batch_size (int): Batch size for training
            psz (int): Patch size
            p_size (int): patch size
            half_range: diparity range

        Attributes:
            num_channels (int): Number of image channels (greyscale vs. RGB)
            batch_size (int): batch_size
            psz (int): Patch size
            p_size (int): patch size
            half_range (int): disparity range
            tr_loc (str): location of training data
            val_loc (str): location of validation data
        '''
        self.args = args

        # if kwargs.get("num_val_img") == 0:
        #     self.val_loc = self.tr_loc
        # else:
        #     if verbose:
        #         print("Validating on training set..")

        # if verbose:
        #     print("Training location: {0} \n Validation location: {1}".format(
        #         self.tr_loc[0], self.val_loc[0]))

    def _infer_args(self, filename):
        '''Infer the args based on the pregenerated binaries'''
        fn = filename.split("_")
        fn[-1] = fn[-1].split(".")[0]
        data_split, _, psz, half_range = [fn[0]] + \
            [int(x) for x in fn[1:]]

        num_channels = 3 if self.args["data_version"] == "kitti2015" else 1
        image_shape = (375, 1242, ) + (num_channels, )
        l_psz = (psz*2+1, psz*2+1, num_channels)
        r_psz = (psz*2+1, psz*2+1+half_range*2, num_channels)

        d = {"psz": psz,
             "p_size": psz*2+1,
             "data_split": data_split,
             "half_range": half_range,
             "num_channels": num_channels,
             "image_shape": image_shape,
             "l_psz": l_psz,
             "r_psz": r_psz}

        self.args.update(d)

    def load(self, filename=None, random_shuffle=True, bin_filename="myPerm.bin"):
        '''
        The hardcoded binary files produced by the matlab scripts are:
            myPerm.bin: Contains the filename permutations
            tr_160_18_100.bin: Contains all pixel locations for all images
            in the left image and the corresponding ground truth for training
            val_40_18_100.bin: Contains all pixel locations for all images
            in the left image and the corresponding ground truth for validation
        '''
        bin_path = os.path.join(self.args["util_root"], bin_filename)

        # Read binarys
        print("Reading binarys..")
        self.file_ids = np.fromfile(bin_path, '<f4').astype(int)
        if filename:
            data_path = os.path.join(self.args["util_root"], filename)
            self.pixel_loc = np.fromfile(
                data_path, '<f4').reshape(-1, 5).astype(np.int)
        # Correct for index assigment in Matlab
        if filename:
            self._correct_index(self.pixel_loc)
        # Shuffle in place
        if random_shuffle and filename:
            np.random.shuffle(self.pixel_loc)

        # Read images
        self.ldata, self.rdata = {}, {}
        print("Reading images..")
        # TODO : Refactor this to only load val or train images for specific
        # split
        num_images = self.args["num_tr_img"] + self.args["num_val_img"]
        for idx in range(num_images):
            fn = self.file_ids[idx]
            try:
                self.ldata[fn], self.rdata[fn] = self._load_sample(fn)
            except Exception as e:
                print("Loading error: {}".format(fn))
                raise e

        # Initialize params
        if filename:
            self._infer_args(filename)
            self._initialize_batch_arrays()
            if self.args["data_split"] == "val" and filename:
                self.pixel_loc = self.pixel_loc[:self.args["num_val_loc"]]

    @property
    def generator(self):
        self._create_label_arrays()
        while True:
            sample_counter = 0
            sample_max = self.args["batch_size"]
            for i in range(self.pixel_loc.shape[0]):
                sample_specs = tuple(self.pixel_loc[i])
                img_id = sample_specs[0]

                # img_left, img_right
                i_l, i_r = self.ldata[img_id], self.rdata[img_id]

                try:
                    self.batch_left[sample_counter] = self._extract_patch(
                        i_l, sample_specs, 0, "left")
                    self.batch_right[sample_counter] = self._extract_patch(
                        i_r, sample_specs, self.args["half_range"], "right")
                except Exception as e:
                    print("Error in extracting patches..")
                    raise e

                sample_counter += 1

                if sample_counter == sample_max:
                    yield [self.batch_left, self.batch_right], self.batch_labels
                    # Reset
                    sample_counter = 0
                    self._initialize_batch_arrays()

    def _extract_patch(self, x, sample_specs, half_range, side):
        img_id, loc_type, center_x, center_y, right_center_x = sample_specs
        right_center_y = center_y

        if side == "right":
            center_y, center_x = right_center_y, right_center_x

        # Loc type always 1, so use the horizontal
        p = x[center_y-self.args["psz"]:
              center_y+self.args["psz"]+1,
              center_x-self.args["psz"]-half_range:
              center_x+self.args["psz"]+half_range+1]

        return p

    def _load_sample(self, fn):
        l_path = os.path.join(
            self.args["data_root"],
            "image_2",
            "%06d_10.png" % fn)
        r_path = os.path.join(
            self.args["data_root"],
            "image_3",
            "%06d_10.png" % fn)

        l_img, r_img = imread(l_path), imread(r_path)
        l_img = preprocess(l_img.astype(np.float32))
        r_img = preprocess(r_img.astype(np.float32))

        return l_img, r_img

    def _initialize_batch_arrays(self):
        # Base shape
        b_s = (self.args["batch_size"],
               self.args["num_channels"], self.args["p_size"], )

        self.batch_left = np.zeros(
            b_s + (self.args["p_size"],),
            np.float32).transpose(0, 2, 3, 1)

        self.batch_right = np.zeros(
            b_s + (self.args["p_size"]+self.args["half_range"]*2,),
            np.float32).transpose(0, 2, 3, 1)

    def _create_label_arrays(self, lambdas=(0.5, 0.2, 0.05)):
        # We add 1 in order to get a well-defined center
        self.batch_labels = np.zeros(
            (self.args["batch_size"], self.args["half_range"]*2 + 1),
            np.float32)

        idx = (self.args["half_range"] - (len(lambdas)-1),
               self.args["half_range"] + (len(lambdas)))

        # Add the smoothed loss around the ground truth
        self.batch_labels[:, idx[0]:idx[1]] = lambdas[::-1] + lambdas[1:]

        # Test if most likely is in center of array
        np.testing.assert_equal(
            np.where(self.batch_labels == lambdas[0])[1],
            np.array([self.args["half_range"]]*self.args["batch_size"]))

    @staticmethod
    def _correct_index(x):
        assert x.ndim == 2 and x.shape[1] == 5
        x[:, 2:5] = x[:, 2:5] - 1

if __name__ == "__main__":
    sys.path.append("../viz_utils")
    import visualization as viz
    import IPython

    data_lookup = {
        "train": "tr_160_18_100.bin",
        "val": "val_40_18_100.bin"
    }

    args = {
        "batch_size": 8,
        "data_version": "kitti2015",
        "util_root": "/home/marco/repos/EfficientStereoMatching/data/KITTI2015/debug_15/",
        "data_root": "/home/marco/repos/EfficientStereoMatching/data/KITTI2015/data_scene_flow/training",
        "filename": data_lookup["train"]
    }

    dh = DataHandler(args)
    dh.load()
    gen = dh.generator
    IPython.embed()
    x, y, labels = gen.next()
    viz.plot_pairwise_training_batches(x[0:10], y[0:10])
    print(labels[0])
    viz.plt.show()


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

T = 500
dt = 0.1
W0 = 0
np.random.seed(100)

dW = (dt ** 0.5) * np.random.normal(size=int(T / dt))
W = np.insert(np.cumsum(dW), 0, W0)
X = 1 / (1 - W)

hitting_time = np.where(np.abs(W - 1) <= 0.005)[0][0] / int(T / dt) * T
plt.axvline(x=hitting_time, linestyle='--',
            label='hitting time', color='k', linewidth=0.75)
plt.plot(np.linspace(0, T, len(X)), X)
plt.xlabel('t')
plt.legend()

def hitting(maxT):
    size = int(maxT / dt)
    dW = (dt ** 0.5) * np.random.normal(size=size)
    W = np.insert(np.cumsum(dW), 0, W0)
    try:
        time = np.where(np.abs(W - 1) <= 0.005)[0][0] / size * maxT
    except:
        time = maxT
    return time

trials = 1000
for T in [1000, 5000, 10000]:
    series = [hitting(T) for i in range(trials)]
    plt.figure()
    sns.distplot(series)
    est = sum(series) / trials
    print(est)
plt.show()


import sys
sys.stdin = open("test_input.txt")
# from itertools import permutations
# from itertools import combinations
# from itertools import product
# from itertools import combinations_with_replacement

from collections import deque
input = sys.stdin.readline

N, M = map(int, input().split())
info = [list(map(int, input().split())) for _ in range(N)]

di = [0,0,-1,1,1,1,-1,-1]
dj = [-1,1,0,0,-1,1,-1,1]

def bfs(q):
    while q:
        i, j, cnt = q.popleft()
        for _ in range(8):
            ni = di[_] + i
            nj = dj[_] + j

            if 0 <= ni < N and 0 <= nj < M:
                if info[ni][nj] == 0:
                    info[ni][nj] = cnt + 1
                    q.append((ni,nj,cnt+1))

    return cnt

q = deque()
for i in range(N):
    for j in range(M):
        if info[i][j] == 1:
            q.append((i,j,0))

print(bfs(q))

# for i in range(N):
#     for j in range(M):
#         print(info[i][j], end=" ")
#     print()


import torch
import numpy as np

from torch.utils.data import Dataset
from tqdm import tqdm

from dashboard.model import DistilBertForSequenceClassification
from transformers import DistilBertConfig, DistilBertTokenizer
from sklearn.manifold import TSNE

TOKENIZER = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
DEVICE = torch.device("cpu")
MAX_SEQ_LENGTH = 256

class text_dataset(Dataset):
    def __init__(self, x, y=None, transform=None):
        self.x = x
        self.y = y
        self.transform = transform

    def __getitem__(self, index):
        tokenized_comment = TOKENIZER.tokenize(self.x[index])

        if len(tokenized_comment) > MAX_SEQ_LENGTH:
            tokenized_comment = tokenized_comment[:MAX_SEQ_LENGTH]

        ids_review  = TOKENIZER.convert_tokens_to_ids(tokenized_comment)
        padding = [0] * (MAX_SEQ_LENGTH - len(ids_review))
        ids_review += padding

        assert len(ids_review) == MAX_SEQ_LENGTH

        ids_review = torch.tensor(ids_review)
        if self.y is None:
            return ids_review

        hcc = self.y[index] # toxic comment
        list_of_labels = [torch.from_numpy(hcc)]

        return ids_review, list_of_labels[0]

    def __len__(self):
        return len(self.x)

def preds(model, test_loader):
    predictions = []
    for inputs in test_loader:
        inputs = inputs.to(DEVICE)
        with torch.no_grad():
            outputs = model(inputs)
            outputs = torch.sigmoid(outputs)
            if len(outputs) > 1:
                predictions += outputs.cpu().detach().numpy().tolist()
            else:
                predictions.append(outputs.cpu().detach().numpy().tolist())

    return predictions

def text_to_2d_plane(model, test_loader):
    embeds = []
    for inputs in tqdm(test_loader, total=len(test_loader)):
        inputs = inputs.to(DEVICE)
        with torch.no_grad():
            outputs = model.distilbert(inputs)
            if len(outputs) > 1:
                embeds += outputs[0][:, 0].squeeze().tolist()
            else:
                embeds.append(outputs[0][:, 0].squeeze().tolist())
            # embeds.append(outputs[0][:, 0].squeeze().tolist())
    return TSNE().fit_transform(embeds)

def main():
    config = DistilBertConfig(
        vocab_size=32000, hidden_dim=768,
        dropout=0.1, num_labels=6,
        n_layers=12, n_heads=12,
        intermediate_size=3072)
    state_dict = torch.load('models/distilbert_model_weights.pth', map_location=DEVICE)
    model = DistilBertForSequenceClassification(config)
    model.load_state_dict(state_dict=state_dict)
    model.to(DEVICE)
    comments = ['Fuck u', 'I like u', 'I hate u', 'I wish u were dead!',
    'U r a good man', 'I hope that you will die someday', 'Hate must be vanished',
    'Fuck yeah', 'Fucking aweasome', 'The government should have done somethinng more meaningful' ]
    test_dataset = text_dataset(comments)
    prediction_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=2, shuffle=False)
    planes = text_to_2d_plane(model=model, test_loader=prediction_dataloader)
    print(planes)

if __name__ == '__main__':
    main()


#We thank Jere et al (2019). for making their code available : https://arxiv.org/abs/1912.02316

from torch.utils.data import DataLoader
from tqdm import tqdm
import torchvision.utils as vutils
from timm.models import create_model
import torchvision.transforms as transforms
import torch
# from OnePatch import OnePatchAttack
from cPixelAttack import cOnePixel
import matplotlib.pyplot as plt
import  numpy as np
import torchvision.datasets as dataset
import wandb
from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image, preprocess_image
import torch.nn as nn

#uncomment to log with wandb
wandb.init(project= "one_pixel_attack_vit_small_thesis")
artifact = wandb.Artifact("vit_small_2px_samples_" + str(wandb.run.id) , type="prediction")
columns = ["id", "scr_img", "adv_image", "ground_truth", "pred b4 attack", "confidence", "gradcam_scr", "gradcam_adv", "pred after attack", "adv confidence"]
log_table = wandb.Table(columns=columns)

def show_img(im):
    k = im.view(3, 224, 224)
    #k = im.squeeze(0).permute(1, 2, 0).detach().cpu()
    k = k.permute(1, 2, 0).detach().cpu()
    plt.imshow(k)
    plt.show()

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
net = create_model('vit_small_patch16_224', pretrained=True)
#net =  create_model("small_patch16_224_hierarchical", pretrained=True)
net.to(device)
net.eval()

WIDTH = 224
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
normalize = transforms.Normalize( mean=mean, std=std)
data_transform = transforms.Compose([
    transforms.Resize(WIDTH),
    transforms.CenterCrop(WIDTH),
    transforms.ToTensor(),
    #normalize
    ])
data_to_pil = transforms.ToPILImage()

def reshape_transform(tensor, height=14, width=14):
    result = tensor[:, 1 :  , :].reshape(tensor.size(0),
        height, width, tensor.size(2))
    result = result.transpose(2, 3).transpose(1, 2)
    return result

target_layers = [net.blocks[-1].norm1]
cam1 = GradCAM(model=net, target_layers=target_layers, use_cuda=True, reshape_transform=reshape_transform)
#cam2 = GradCAM(model=net, target_layers=target_layers, use_cuda=True, reshape_transform=reshape_transform)

path_to_resources = '/home/abass.abdulsalam/Documents/imgnet/processed_val/'
batch_size = 1
test_dataset = dataset.ImageFolder(root=path_to_resources, transform=data_transform)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,
                             num_workers=2, shuffle=True)

#scr = OnePatchAttack(net)
scr = cOnePixel(net)
with tqdm(test_loader, unit = 'batch', position = 0, leave=True) as p_bar:
    success = 0
    fool_rate = 0
    total = 0

    for batch_idx, (data, labels) in enumerate(p_bar):
        data = data.to(device)
        gt_label = labels.to(device)

        output =  net(data)
        #output = torch.vstack(output)
        maxval, pred_before_attack = output.data.max(1, keepdim=True)
        total += 1

        attacked_img = scr.forward(data, labels)
        adv_output = net(attacked_img)
        # adv_output = torch.vstack(adv_output)
        maxval_adv, pred_after_attack = adv_output.data.max(1, keepdim=True)

        if pred_after_attack != gt_label:
            success += 1

        if pred_after_attack != pred_before_attack:
            fool_rate += 1

        targets_cam = [ClassifierOutputTarget(gt_label.item())]

        grayscale_cam_scr = cam1(input_tensor=data, targets=targets_cam, aug_smooth=True)
        grayscale_cam_scr = grayscale_cam_scr[0, :]
        rgb_img_scr = np.float32(data.squeeze(0).permute(1, 2, 0).cpu())
        gradcam_img_scr = show_cam_on_image(rgb_img_scr, grayscale_cam_scr)

        grayscale_cam_adv = cam1(input_tensor=attacked_img, targets=targets_cam, aug_smooth=True)
        grayscale_cam_adv = grayscale_cam_adv[0, :]
        rgb_img_adv = np.float32(attacked_img.squeeze(0).permute(1, 2, 0).cpu())
        gradcam_img_adv = show_cam_on_image(rgb_img_adv, grayscale_cam_adv)

            # if batch_idx < 20:
            #     vutils.save_image(data.data, "./%s/%d_%d_original.png" % (
            #     "logs/one_patch/three", batch_idx, labels),
            #                         normalize=True)
            #     vutils.save_image(attacked_img.data, "./%s/%d_%d_adversarial_%d.png" % (
            #         "logs/one_patch/three", batch_idx, labels, label_adv),
            #                         normalize=True)

        # p_bar.set_description("Attack Success: {:.2f}, FR:{:.2f}, Queries: {}".format((success * 100 / total), \
        #                         (fool_rate * 100 /total), scr.required_iterations))

        wandb.log({"Attack Success" : (success * 100 / total),
                   "Fool rate" : (fool_rate * 100 /total),
                   #"queries" : scr.required_iterations[0],
                   "number of pixels":2})

        log_table.add_data(batch_idx, wandb.Image(data), wandb.Image(attacked_img), gt_label.item(), pred_before_attack.item(), maxval.item(),\
                           wandb.Image(gradcam_img_scr), wandb.Image(gradcam_img_adv), pred_after_attack.item(), maxval_adv.item())
    artifact.add(log_table, "predictions")
    wandb.run.log_artifact(artifact)
        #wandb.log({"Deit_tiny" : log_table})

#print(scr.required_iterations[-1])


import os
import math
import torch
import torch.nn as nn
import traceback

from .adabound import AdaBound
from .audio import Audio
from .evaluation import validate
from model.model import VoiceFilter
from model.embedder import SpeechEmbedder

def train(args, pt_dir, chkpt_path, trainloader, testloader, writer, logger, hp, hp_str):
    # load embedder
    embedder_pt = torch.load(args.embedder_path)
    embedder = SpeechEmbedder(hp).cuda()
    embedder.load_state_dict(embedder_pt)
    embedder.eval()

    audio = Audio(hp)
    model = VoiceFilter(hp).cuda()
    if hp.train.optimizer == 'adabound':
        optimizer = AdaBound(model.parameters(),
                             lr=hp.train.adabound.initial,
                             final_lr=hp.train.adabound.final)
    elif hp.train.optimizer == 'adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=hp.train.adam)
    else:
        raise Exception("%s optimizer not supported" % hp.train.optimizer)

    step = 0

    if chkpt_path is not None:
        logger.info("Resuming from checkpoint: %s" % chkpt_path)
        checkpoint = torch.load(chkpt_path)
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        step = checkpoint['step']

        # will use new given hparams.
        if hp_str != checkpoint['hp_str']:
            logger.warning("New hparams is different from checkpoint.")
    else:
        logger.info("Starting new training run")

    try:
        criterion = nn.MSELoss()
        while True:
            model.train()
            for dvec_mels, target_mag, mixed_mag in trainloader:
                target_mag = target_mag.cuda()
                mixed_mag = mixed_mag.cuda()

                dvec_list = list()
                for mel in dvec_mels:
                    mel = mel.cuda()
                    dvec = embedder(mel)
                    dvec_list.append(dvec)
                dvec = torch.stack(dvec_list, dim=0)
                dvec = dvec.detach()

                mask = model(mixed_mag, dvec)
                output = mixed_mag * mask

                # output = torch.pow(torch.clamp(output, min=0.0), hp.audio.power)
                # target_mag = torch.pow(torch.clamp(target_mag, min=0.0), hp.audio.power)
                loss = criterion(output, target_mag)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                step += 1

                loss = loss.item()
                if loss > 1e8 or math.isnan(loss):
                    logger.error("Loss exploded to %.02f at step %d!" % (loss, step))
                    raise Exception("Loss exploded")

                # write loss to tensorboard
                if step % hp.train.summary_interval == 0:
                    writer.log_training(loss, step)
                    logger.info("Wrote summary at step %d" % step)

                # 1. save checkpoint file to resume training
                # 2. evaluate and save sample to tensorboard
                if step % hp.train.checkpoint_interval == 0:
                    save_path = os.path.join(pt_dir, 'chkpt_%d.pt' % step)
                    torch.save({
                        'model': model.state_dict(),
                        'optimizer': optimizer.state_dict(),
                        'step': step,
                        'hp_str': hp_str,
                    }, save_path)
                    logger.info("Saved checkpoint to: %s" % save_path)
                    validate(audio, model, embedder, testloader, writer, step)
    except Exception as e:
        logger.info("Exiting due to exception: %s" % e)
        traceback.print_exc()


from django.db import models

# Create your models here.
from client.models import Client
from accountant.models import Accountant
from utils.models import Subscribe
from django.utils import timezone
from utils.models import Packages

class TypeOfActivity(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.name

class TaxationSystem(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, blank=True)

class Company(models.Model):
    id = models.AutoField(primary_key=True)
    client = models.ForeignKey(Client, blank=True, related_name='client_company', on_delete='cascade', default=None)
    logo = models.ImageField(blank=True, null=True)
    subscribe = models.ForeignKey(Subscribe, on_delete=models.CASCADE, blank=True, related_name='subscribe', null=True)
    name = models.CharField(max_length=255, blank=True)
    HVHH = models.CharField(max_length=255, blank=True)
    address = models.CharField(max_length=255, blank=True)
    accountant = models.ManyToManyField(Accountant, related_name="accountant_many_company", null=True, blank=True)
    created_date = models.DateTimeField(default=timezone.now, null=True)
    created_number = models.IntegerField(null=True, blank=True)
    director_full_name = models.TextField(null=True, blank=True)
    category = models.IntegerField(null=True, blank=True)
    count_employees = models.IntegerField(null=True, blank=True)
    is_deleted = models.BooleanField(default=False,blank=True)
    is_deleted_by_manager = models.BooleanField(default=False,blank=True)
    type_of_activity = models.ManyToManyField(TypeOfActivity, null=True, blank=True)
    packages = models.ForeignKey(Packages, null=True, blank=True, on_delete=models.DO_NOTHING,
                                 related_name="packages_company")
    taxation_system = models.ForeignKey(TaxationSystem, null=True, blank=True, on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.name


import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import json
import time
import sys
from config import config

sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=config['SPOTIPY_CLIENT_ID'],
                                                                         client_secret=config['SPOTIPY_CLIENT_SECRET']))
sp.trace = False

if len(sys.argv) > 1:
    artist_name = ' '.join(sys.argv[1:])
else:
    artist_name = 'AIVA'

results = sp.search(q=artist_name, limit=50)
tids = []
for i, t in enumerate(results['tracks']['items']):
    print(' ', i, t['name'])
    tids.append(t['uri'])

features = sp.audio_features(tids)
for feature in features:
    print(json.dumps(feature, indent=4))
    print()
    analysis = sp._get(feature['analysis_url'])
    print(json.dumps(analysis, indent=4))
    print()


from multiprocessing import Process
from pitftgpio import PiTFT_GPIO
from time import sleep

class Button_input():
    def __init__(self, button_map):
        self.button_map = button_map
        self.proc = Process(target=self.grabber_wrapper,
                            args=(button_map, ))
        self.pitft = PiTFT_GPIO()
        self.proc.start()

    def change_screen(self, new_button_map):
        self.proc.kill()
        self.proc = Process(self.grabber_wrapper, args=(new_button_map))
        self.prc.start()

    def grabber_wrapper(self, command_map):
        while True:
            self.button_grabber(command_map, vals=[False, False, False, False])

    def button_grabber(self, command_map, vals=[False, False, False, False]):
            sleep(.01)
            keylist = [
                       self.pitft.Button3,
                       self.pitft.Button1,
                       self.pitft.Button2,
                       self.pitft.Button4
                       ]
            if sum(keylist) == 0:
                command_map[int("".join(
                    map(lambda x: str(int(x)), vals)),2)]()
                return keylist
            else:
                return [x[0] or x[1] for x in zip(keylist,
                                                  self.button_grabber(command_map, vals=keylist))]


from dss_train_sl import TrainClassifier
from dss_train_sl_siamese import TrainClassifier as SiameseClassifier

import os
import argparse
from cords.utils.config_utils import load_config_data
if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--config_file', type=str, default="configs/SL/config_smi_toy_da.py")
    parser.add_argument('--smi_func_type', type=str, default='fl2mi')
    parser.add_argument('--query_size', type=int, default=100)
    parser.add_argument('--fraction', type=float, default=0.1)
    parser.add_argument('--select_every', type=int, default=20)
    parser.add_argument('--print_every', type=int, default=10)
    parser.add_argument('--save_every', type=int, default=5)
    parser.add_argument('--device', type=str, default='cuda')
    parser.add_argument('--num_epochs', type=int, default=300)
    parser.add_argument('--source_domains', type=str, default="real")
    parser.add_argument('--target_domains', type=str, default="clipart")
    parser.add_argument('--similarity_criterion', type=str, default="gradient")
    parser.add_argument('--ckpt_file', type=str, default=None)
    parser.add_argument('--selection_type', type=str, default="Supervised")
    parser.add_argument('--num-runs', type=int, default=1)
    parser.add_argument('--loss', type=str, default=None,
                        help="Loss function: can be CrossEntropyLoss, ccsa, or dsne")
    parser.add_argument('--fine_tune', type=bool, default=False)
    parser.add_argument('--augment_queryset', type=bool, default=False)
    parser.add_argument('--just-plot', default=False, action='store_true')
    args = parser.parse_args()
    config_file = args.config_file
    config_data = load_config_data(args.config_file)
    # classifier = TrainClassifier(config_file)
    config_data.config_file = config_file
    config_data.dss_args.smi_func_type = args.smi_func_type
    config_data.dss_args.query_size = args.query_size
    config_data.dss_args.fraction = args.fraction
    config_data.dss_args.select_every = args.select_every
    config_data.ckpt.save_every = args.save_every
    if args.loss:
        config_data.loss.type = args.loss
    if args.ckpt_file:
        config_data.ckpt.file = args.ckpt_file
        config_data.ckpt.is_load = True
    config_data.train_args.device = args.device
    config_data.train_args.print_every = args.print_every
    config_data.train_args.num_epochs = args.num_epochs
    config_data.dss_args.similarity_criterion = args.similarity_criterion
    config_data.dss_args.selection_type = args.selection_type
    config_data.train_args.just_plot = args.just_plot
    if config_data.dataset.name in ["domainnet", "toy_da", "toy_da2", "office31", "officehome", "toy_da3"]:
        source_domains = args.source_domains.split(",")
        target_domains = args.target_domains.split(",")
        if config_data.dataset.name in ["domainnet", "office31", "officehome"]:
            config_data.dataset.customImageListParams.source_domains = source_domains
            config_data.dataset.customImageListParams.target_domains = target_domains
        else:
            config_data.dataset.daParams.source_domains = source_domains
            config_data.dataset.daParams.target_domains = target_domains
    if config_data.loss.type == 'ccsa':
        config_data.model.architecture = 'SiameseResNet50'
        config_data.train_args.alpha = 0.25
    if config_data.loss.type == 'dsne':
        config_data.model.architecture = 'SiameseResNet50'
        config_data.train_args.alpha = 0.1
    for i in range(args.num_runs):
        print(f"RUN: {i}")
        config = config_data.copy()
        config['run_id'] = i
        if config_data.loss.type in ['ccsa', 'dsne']:
            config.dss_args.augment_queryset = False
            config.train_args.train_type = 'ft' if args.fine_tune else ''
            config.train_args.ft_epochs = 5 if args.fine_tune else 0
            classifier = SiameseClassifier(config)
        else:
            config.dss_args.fine_tune = args.fine_tune
            config.dss_args.augment_queryset = args.augment_queryset
            classifier = TrainClassifier(config)
        classifier.train()


from util import readFile
import plotly.plotly as py
from plotly.graph_objs import *

parameters = dict(
    classify = dict(
        iterations = 500,
        sigmoid = dict(
            alpha = [0.01, 0.05],
            dim = [100, 200, 400]
        ),
        relu = dict(
            alpha = [0.01, 0.05],
            dim = [100, 200, 400]
        )
    ),
    regression = dict(
        iterations = 300,
        sigmoid = dict(
            alpha = [0.01, 0.001],
            dim = [3, 4, 5]
        ),
        relu = dict(
            alpha = [0.001, 0.005],
            dim = [3, 4, 5]
        )
    )
)

def load_data(file_path):
    lines = readFile(file_path).strip().split("\n")
    results = [tuple([float(n) for n in line.strip().split()]) for line in lines]
    results = zip(*results)
    return map(list, results)

def plot_classify(dataset, activation, alpha, lam, layer_dim, iterations):
    file_name = "_".join([dataset,
                          activation,
                          str(alpha),
                          str(lam),
                          str(layer_dim),
                          str(iterations)]) + ".txt"
    file_path = "result/" + file_name
    data = load_data(file_path)
    epochs = data[0]
    loss = data[2]

    trace0 = Scatter(
        x = epochs,
        y = loss,
        line = dict(
            width = 3,
        ),
        mode = 'lines',
        name = " ".join([dataset, activation, str(alpha), str(layer_dim)]),
    )
    data = [trace0]

    # Plot and embed in ipython notebook!
    layout = dict(
        title = "Validation Total Loss vs. Epoch (%s activation)" % activation,
        xaxis = dict(title = "Epoch"),
        yaxis = dict(title = "Validation Total Loss")
    )

    fig = dict(
        data = data,
        layout = layout
    )
    py.plot(fig, filename="model selection")

def plot_classify_model(dataset, activation):
    lam = 0.4
    iterations = parameters[dataset]["iterations"]
    data = []

    epochs = range(1,iterations+1)
    for alpha in parameters[dataset][activation]["alpha"]:
        for layer_dim in parameters[dataset][activation]["dim"]:
            file_name = "_".join([dataset,
                          activation,
                          str(alpha),
                          str(lam),
                          str(layer_dim),
                          str(iterations)]) + ".txt"
            file_path = "result/" + file_name
            d = load_data(file_path)
            val_loss = d[2]
            trace = Scatter(
                x = epochs,
                y = val_loss,
                mode = "lines",
                name = " ".join([dataset, activation, str(alpha), str(layer_dim)]),
                line = dict(
                    width = 2,
                )
            )
            data.append(trace)

    layout = dict(
        title = "Validation Total Loss vs. Epoch (%s activation)" % activation,
        xaxis = dict(title = "Epoch"),
        yaxis = dict(title = "Validation Total Loss")
    )

    fig = dict(
        data = data,
        layout = layout
    )
    py.plot(fig, filename="model selection")

def main():
    #plot_classify("regression", "sigmoid", 0.001, 0.4, 3, 300)
    plot_classify_model("regression", "relu")

if __name__ == '__main__':
    main()


from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('sobre', views.sobre, name='sobre'),
    path('servicos', views.servicos, name='servicos'),
    path('upload', views.upload, name='upload'),
    path('assist', views.assist, name='assist'),
    path('explorar', views.explorar, name='explorar'),
    path('contato', views.contato, name='contato'),
    path('notfound', views.notfound, name='notfound'),
]


from setuptools import setup, find_packages
PACKAGES = find_packages()

NAME = 'Seattle Food Insepctions'
DESCRIPTION = 'Visualization of Seattle food inspection data overlayed with'\
              'local census information.'
URL = 'https://github.com/hyspacex/Seattle-Food-Inspections'
LICENSE = 'MIT'
AUTHOR = 'djclancy, hyspacex, sadrafh, gtalpey'
VERSION = '1.0'
PACKAGE_DATA = {'seattlefoodinspection': ['data/clean_data/*']}
with open('requirements.txt') as f:
    REQUIRES = f.read().splitlines()

opts = dict(name=NAME,
            description=DESCRIPTION,
            url=URL,
            license=LICENSE,
            author=AUTHOR,
            version=VERSION,
            install_requires=REQUIRES
            )

if __name__ == '__main__':
    setup(**opts)


#!/usr/bin/env python3

import matplotlib.pyplot as plt
import math
import numpy as np
import matplotlib.patches as mpatches
import os
import glob
import json

plt.rc('font', size=12)          # controls default text sizes
plt.rc('axes', titlesize=12)     # fontsize of the axes title
plt.rc('axes', labelsize=12)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=12)    # fontsize of the tick labels
plt.rc('ytick', labelsize=12)    # fontsize of the tick labels
plt.rc('legend', fontsize=12)    # legend fontsize

def plot_throughput():
    x = np.arange(0, 2)

    # Data is in ../data/zbdench
    xfs_iops = [103.926, 56.749]
    f2fs_iops = [98.813, 59.926]
    f2fs_zns_iops = [94.406, 81.127]
    zenfs_iops = [103.913, 92.196]

    fig, ax = plt.subplots()

    # With number avove bar
    # rects1 = ax.bar(x - 0.2, f2fs_iops, width=0.35, capsize=3, label="F2FS")
    # rects2 = ax.bar(x + 0.2, zenfs_iops, width=0.35, capsize=3, label="ZenFS")
    # ax.bar_label(rects1, padding=3, fmt="%.1f")
    # ax.bar_label(rects2, padding=3, fmt="%.1f")

    ax.bar(x - 0.15, xfs_iops, width=0.1, capsize=3, color="#117733", label="xfs")
    ax.bar(x - 0.05, f2fs_iops, width=0.1, capsize=3, color="#88CCEE", label="F2FS")
    ax.bar(x + 0.05, f2fs_zns_iops, width=0.1, capsize=3, color="#999933", label="F2FS (ZNS)")
    ax.bar(x + 0.15, zenfs_iops, width=0.1, capsize=3, color="#CC6677", label="ZenFS")

    fig.tight_layout()
    ax.set_axisbelow(True)
    ax.grid(which='major', linestyle='dashed', linewidth='1')
    ax.legend(loc='upper right', ncol=2)
    ax.xaxis.set_ticks(x)
    ax.xaxis.set_ticklabels(["fillrandom", "overwrite"])
    ax.set_ylim(bottom=0,top=120)
    ax.set_ylabel('KIOPS')
    plt.savefig(f'rocksdb-atc-eval.pdf', bbox_inches='tight')
    plt.clf()

if __name__ == '__main__':
    file_path = '/'.join(os.path.abspath(__file__).split('/')[:-1])

    plot_throughput()


import re
import string
import unicodedata

from nltk.stem.porter import PorterStemmer

stemmer = PorterStemmer()

def normalize_answer(s):
    def remove_articles(text):
        return re.sub(r"\b(a|an|the)\b", " ", text)

    def white_space_fix(text):
        return " ".join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return "".join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return " ".join([lower(x) for x in s]).rstrip()

def remove_empty(a_list):
    new_list = []
    for i in a_list:
        if len(i) > 0:
            if len(i[0]) > 0:
                new_list.append(normalize_answer(i))
    return new_list

def remove_empty_phase(phrases_scores):
    phrase_list = []
    score_list = []
    for (
        phrase,
        score,
    ) in phrases_scores:
        if len(phrase) > 0:
            if len(phrase[0]) > 0:
                phrase_list.append(normalize_answer(phrase))
                score_list.append(score)
    return phrase_list, score_list

def stem_norm_phrase(phrase):
    norm_chars = unicodedata.normalize("NFD", phrase)
    stem_chars = " ".join([stemmer.stem(w) for w in norm_chars.split(" ")])
    return norm_chars, stem_chars

def del_stemming_duplicate_phrase(phrase_list, score_list, return_num):
    tot_phrases_set = set()
    return_phrases, return_scores = [], []

    for phrase, score in zip(phrase_list, score_list):
        norm_phrase, stem_phrase = stem_norm_phrase(phrase)

        if (norm_phrase not in tot_phrases_set) and (
            stem_phrase not in tot_phrases_set
        ):
            return_phrases.append(phrase)
            return_scores.append(score)

        tot_phrases_set.add(norm_phrase)
        tot_phrases_set.add(stem_phrase)

        if len(return_phrases) >= return_num:
            break
    return return_phrases, return_scores


from gcsa.calendar import CalendarListEntry, Calendar
from tests.google_calendar_tests.test_case_with_mocked_service import TestCaseWithMockedService

class TestCalendarListService(TestCaseWithMockedService):
    def test_get_calendar_list(self):
        calendars = list(self.gc.get_calendar_list())
        self.assertEqual(len(calendars), 8)
        self.assertTrue(any(c.id == 'primary' for c in calendars))

    def test_get_calendar_list_entry(self):
        calendar = self.gc.get_calendar_list_entry()
        self.assertEqual(calendar.id, 'primary')
        self.assertIsInstance(calendar, CalendarListEntry)

        calendar = self.gc.get_calendar('1')
        self.assertEqual(calendar.id, '1')

    def test_add_calendar_list_entry(self):
        calendar = CalendarListEntry(
            calendar_id='test_calendar_list_entry',
            _summary='Summary',
            summary_override='Summary override'
        )
        new_calendar = self.gc.add_calendar_list_entry(calendar)

        self.assertIsNotNone(new_calendar.id)
        self.assertEqual(calendar.summary_override, new_calendar.summary_override)

    def test_update_calendar_list_entry(self):
        calendar = CalendarListEntry(
            calendar_id='test_calendar_list_entry',
            _summary='Summary',
            summary_override='Summary override'
        )
        new_calendar = self.gc.add_calendar_list_entry(calendar)

        self.assertEqual(calendar.summary_override, new_calendar.summary_override)

        new_calendar.summary_override = 'Updated summary override'
        updated_calendar = self.gc.update_calendar_list_entry(new_calendar)

        self.assertEqual(new_calendar.summary_override, updated_calendar.summary_override)

        retrieved_updated_calendar = self.gc.get_calendar_list_entry(new_calendar.id)
        self.assertEqual(retrieved_updated_calendar.summary_override, updated_calendar.summary_override)

    def test_delete_calendar_list_entry(self):
        calendar = Calendar(
            summary='Summary'
        )
        with self.assertRaises(ValueError):
            # no calendar_id
            self.gc.delete_calendar_list_entry(calendar)

        calendar = CalendarListEntry(
            calendar_id='test_calendar_list_entry',
            _summary='Summary',
            summary_override='Summary override'
        )

        self.gc.delete_calendar_list_entry(calendar)
        self.gc.delete_calendar_list_entry('2')

        with self.assertRaises(TypeError):
            # should be a Calendar, CalendarListEntry or calendar id as a string
            self.gc.delete_calendar_list_entry(calendar=None)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
from django.conf import settings

class Migration(migrations.Migration):
    dependencies = [
        ('questions', '0002_auto_20151118_2341'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('tags_likes', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='responselike',
            name='user',
            field=models.ForeignKey(related_name='r_likes', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='questionlike',
            name='question',
            field=models.ForeignKey(related_name='q_likes', blank=True, to='questions.Question', null=True),
        ),
        migrations.AddField(
            model_name='questionlike',
            name='user',
            field=models.ForeignKey(related_name='q_likes', to=settings.AUTH_USER_MODEL),
        ),
    ]