import random

print('Welkom bij dit spel!')
print('Regels: \n1. \nJe moet in 10 rondes proberen een getal te raden \ntussen de 1 en 1000')
print('2. \nals je in 10 rondes niet het getal raad dan ben je af en\n stopt het spel\n ')
print('Dus beginnen maar!')

score = 0
ronde = 0
repeat = True
repeat1 = True

while repeat1 == True:
    getal = random.randint(1,1000)
    print(getal)
    x = 0
    ronde = ronde + 1
    repeat = True

    while repeat == True:

        try:
            geraden_getal = int(input("Wat denk je dat het getal is? : "))
            x = x + 1
            geraden_getal
            repeat = False
            roundtwintig = geraden_getal - getal

            if int(geraden_getal) > 1000 :
                repeat = True
                print("Heb je geluisterd? het getal moet binnen de duizend zijn!!!")

            elif geraden_getal > getal:
                print("Lager!")
                repeat = True

            elif geraden_getal < getal:
                print("Hoger!")
                repeat = True

            if abs(int(roundtwintig)) <= 20:
                    print("Je bent heel warm!")
                    repeat = True
            elif abs(int(roundtwintig)) <= 50:
                    print("Je bent warm!")
                    repeat = True

            elif geraden_getal == getal:
                score = score + 1
                print("Dat is juist!\n" +"Jouw score : "+ str(score))
                opnieuw = input("Nog een getal raden? [Ja/Nee]\n").lower()

                if opnieuw == "ja":
                    repeat1 = True
                    if int(ronde) == 5:
                        print("Helaas zijn je ronde's voorbij\nGame Over!\n" + "Jouw score: " + str(score))
                        quit()

                elif opnieuw == "nee":
                    print("Bedankt voor spelen!\nLatersss! ")
                    quit()

            if int(x) == 10:
                repeat = False
                print("Helaas zijn je pogingen voorbij\n" + "Jouw score : " + str(score))

                opnieuw = input("Nog een getal raden? [Ja/Nee]\n").lower()
                if opnieuw == "ja":
                    repeat1 = True
                    if int(ronde) == 5:
                        print("Helaas zijn je ronde's voorbij\nGame Over!\n" + "Jouw score: " + str(score))
                        quit()

                elif opnieuw == "nee":
                    print("Bedankt voor spelen!\nTot latersss!")
                    quit()

        except Exception as error:
            print("Dat is geen getal! ")


import logging

'''
To Use logger in your own file

from logger import logger

newlogger = logger(loglevel = 'debug', cmdline = True).getLogger()
newlogger.debug("Debug Message")
newlogger.info('Info Message')
'''

class logger():
    def __init__(self, loggerName = 'LaserCutter', loglevel = "info", cmdline = False, filename = "lasercutter.log"):
        '''
        Initialize a new logger that logs
        Keyword Arguments:
            loggerName (str): Name of Logger.  Default is 'Lasercutter'
            loglevel (str): Level of Log set to 'debug' for debug mode.  Default is 'info'
            cmdline (bool): Parameter to log to cmdline. Default is False
            filename (str): Name of logging file.  Default is 'lasercutter.log'
        '''
        if loglevel == "debug":
            level = logging.DEBUG
        else:
            level = logging.INFO
        formatter = logging.Formatter('%(name)s: %(levelname)s - %(message)s')
        self.logger = logging.getLogger(loggerName)
        if(cmdline):
            cmd_handler = logging.StreamHandler()
            cmd_handler.setLevel(level)
            cmd_handler.setFormatter(formatter)
            self.logger.addHandler(cmd_handler)
        file_handler = logging.FileHandler(filename = filename, mode = 'a', encoding = None, delay = False)
        file_handler.setFormatter(formatter)
        file_handler.setLevel(level)
        self.logger.addHandler(file_handler)
        self.logger.setLevel(level)

    def getLogger(self):
        '''
        Returns: The logger initialized when creating the class
        '''
        return self.logger


# pylint: disable=invalid-name,redefined-builtin
"""dfman package info"""

import os

modname = distname = 'dfman'
numversion = (0, 0, 2)
version = '.'.join([str(num) for num in numversion])

install_requires = [
]

license = 'MIT'
description = 'Dotfile Manager'
url = 'https://github.com/jniedrauer/dfman'
author = 'Josiah Niedrauer'
author_email = 'jniedrauer@gmail.com'

classifiers = [
    'Development Status :: 2 - Pre-Alpha',
    'Environment :: Console',
    'Intended Audience :: End Users/Desktop',
    'License :: OSI Approved :: MIT License',
    'Operating System :: POSIX',
    'Programming Language :: Python :: 3',
    'Programming Language :: Python :: 3 :: Only',
    'Topic :: Utilities'
]

long_description = """"""

scripts = [os.path.join('bin', distname)]


import numpy as np

v = np.array([1,2,3])
print("Vector")
print(v)    # [1 2 3]

np.shape(v) # (3,)

# Creamos matriz columna
v[:, np.newaxis]
"""
    v valdrá:
    array([[1],
       [2],
       [3]])
"""

v[:,np.newaxis].shape   # (3, 1)

# Matriz fila
v[np.newaxis,:].shape   # (1, 3)


# n번째 물건 부터 배낭을 담을지 결정한다.
w = [0, 5, 4, 6, 3]
v = [0, 10, 40, 30, 50]

N = 4
maxW = 10

memo = [[-1] * (maxW + 1) for _ in range(N + 1)]

# 기저 사례 초기화
for i in range(N + 1): memo[i][0] = 0
for i in range(maxW + 1): memo[0][i] = 0

for i in range(1, N + 1):
    for j in range(1, maxW + 1):
        case1 = case2 = 0
        if j >= w[i]:                   # k번째 물건을 담는 경우
            case1 = memo[i - 1][j - w[i]] + v[i]

        case2 = memo[i - 1][j]          # k번째 물건을 담지 않는 경우

        memo[i][j] = case1 if case1 > case2 else case2

print('최대가치 = %d' % memo[N][maxW])

print('----------------------------------')
for i in range(1, N + 1):
    for j in range(1, maxW + 1):
        print('%2d' % memo[i][j], end=' ')
    print()


#!/usr/bin/env python3

import chess
import chess.pgn
import os, io

openings = {}

for fname in os.listdir("openings"):
    with open("openings/" + fname) as f:
        for line in f:
            line = line.strip()
            if line:
                eco, name, moves = line.split("\t")
                epd = chess.pgn.read_game(io.StringIO(moves)).end().board().epd()
                openings[epd] = f"{eco} {name}: {moves}"

with open("openings.txt") as f:
    with open("openings_new.txt", "w") as of:
        for line in f:
            line = line.strip()
            if line:
                _, moves = line.split("1.")
                epd = chess.pgn.read_game(io.StringIO("1." + moves)).end().board().epd()
                try:
                    line = openings[epd]
                except KeyError:
                    print("Missing:", line, epd)
                of.write(line + "\n")


# Splash / startup screen

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

from octopyclient.common import CommonPanel
from .panels.system import SystemPanel
from octopyclient.igtk import *

class SplashPanel(CommonPanel):
    def __init__(self, ui):
        CommonPanel.__init__(self, ui)
        logo = ImageFromFileScaled("logo-octoprint.png")

        self.label = FmtLabel("...")
        self.label.set_hexpand(True)
        self.label.set_line_wrap_mode(True)
        self.label.set_max_width_chars(30)
        self.label.set_text("Initializing printer...")

        main = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=15)
        main.set_valign(Gtk.Align.END)
        main.set_vexpand(True)
        main.set_hexpand(True)

        main.add(logo)
        main.add(self.label)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
        box.add(main)
        box.add(self.createActionBar())

        self.g.add(box)

    def createActionBar(self):
        bar = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=5)
        bar.set_property("height-request", displayScale(120))
        bar.set_halign(Gtk.Align.END)

        self.RetryButton = ButtonImageStyle("Retry", "refresh.svg", "color2", self.releaseFromHold)
        self.RetryButton.set_property("width-request", displayScale(IMAGE_SIZE_LARGE))
        self.RetryButton.set_property("visible", True)
        bar.add(self.RetryButton)
        ctx = self.RetryButton.get_style_context()
        ctx.add_class("hidden")

        sys = ButtonImageStyle("System", "info.svg", "color3", self.showSystem)
        sys.set_property("width-request", displayScale(IMAGE_SIZE_LARGE))
        bar.add(sys)

        return bar

    def putOnHold(self):
        self.RetryButton.show()
        ctx = self.RetryButton.get_style_context()
        ctx.remove_class("hidden")
        self.label.set_text("Cannot connect to the printer. Tap \"Retry\" to try again.")

    def releaseFromHold(self, source):
        self.RetryButton.hide()
        ctx = self.RetryButton.get_style_context()
        ctx.add_class("hidden")
        self.label.set_text("Startup...")
        self.ui.connectionAttempts = 0

    def showSystem(self, source):
        self.ui.OpenPanel(SystemPanel(self.ui), self)


from .piece import piece
from src.helpers.constants import MOVES

class king(piece):
    def __init__(self, team_name='white'):
        # special handling for 2, 0 as ONLY valid for the first move
        self.valid_moveset = [[MOVES.up], [MOVES.down], [MOVES.left], [MOVES.right],
                              [MOVES.up_right], [MOVES.up_left], [MOVES.down_left], [MOVES.down_right]]
        self.team_name = team_name
        super(king, self).__init__(
            valid_moveset=self.valid_moveset, team_name=team_name)
        self.piece_abbreviation = '♚' if team_name == 'white' else '♔'
        self.capture_direction = []


import uuid
import pathlib
from django.db import models
from api_base.models import TimeStampedModel
from api_post.models import Category, Source
from django.utils.text import slugify
from api_post.constants import PostStatus
from django.utils import timezone
from api_user.models import User

class Posts(TimeStampedModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.TextField(max_length=255, blank=True)
    slug = models.SlugField(unique=True, null=True)
    thumbnail = models.CharField(max_length=255, blank=True, null=True)
    category = models.ForeignKey(
        Category, related_name="posts", null=True, blank=True, on_delete=models.SET_NULL
    )
    source = models.OneToOneField(
        Source, related_name="posts", on_delete=models.SET_NULL, null=True, blank=True
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="posts", blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    likes = models.IntegerField(default=0)
    liked_by = models.ManyToManyField(User, related_name="liked_by", null=True, blank=True)
    publish_date = models.DateTimeField(default=timezone.now)
    author = models.CharField(max_length=255, blank=True)
    summary = models.CharField(max_length=255, blank=True)
    status = models.CharField(choices=PostStatus.choices(), default=PostStatus.DRAFT.value, max_length=50)
    views = models.IntegerField(default=0)

    def __str__(self):
        return self.title

    class Meta:
        db_table = "posts"
        ordering = ["-created_at"]


import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms

# Device configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

# Hyper parameters
num_epochs = 2
num_classes = 10
batch_size = 100
learning_rate = 0.001

# MNIST dataset
train_dataset = torchvision.datasets.MNIST(root='../../data/',
                                           train=True,
                                           transform=transforms.ToTensor(),
                                           download=True)

test_dataset = torchvision.datasets.MNIST(root='../../data/',
                                          train=False,
                                          transform=transforms.ToTensor())

# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False)

class CNN(nn.Module):
    def __init__(self, num_classes=10):
        super(CNN, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)
        )
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)
        )
        self.fc = nn.Linear(7*7*32, num_classes)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1)
        out = self.fc(out)
        return out

model = CNN(num_classes).to(device)

# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# Training
for epoch in range(num_epochs):
    for (images, labels) in train_loader:
        images = images.to(device)
        labels = labels.to(device)

        outputs = model(images)
        loss    = criterion(outputs, labels)

        # optimization
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    print("Epoch: {}, loss = {:.5f}".format(epoch, loss.item()))

# Testing
model.eval()
with torch.no_grad():
    correct = 0
    total   = 0
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)

        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
    print("Accuracy: {:.4f}".format(correct / total))


import os
import pickle

import h5py
from tqdm.auto import tqdm

def write_pickle(
    padded_train_trajectories,
    padded_valid_trajectories,
    words,
    word2idx,
    pickle_filename,
):
    with open(pickle_filename, "wb") as f:
        pickle.dump(
            (padded_train_trajectories, padded_valid_trajectories, words, word2idx),
            f,
            protocol=pickle.HIGHEST_PROTOCOL,
        )

def read_pickle(pickle_filename):
    with open(pickle_filename, "rb") as f:
        return pickle.load(f)

def write_hdf5(
    padded_train_trajectories, padded_valid_trajectories, words, hdf5_filename
):
    with open(hdf5_filename, "wb") as f:
        with h5py.File(f, "w") as h5f:
            train_group = h5f.create_group("train")
            valid_group = h5f.create_group("valid")

            for key, data in tqdm(
                padded_train_trajectories.items(), desc="Writing training data"
            ):
                train_group.create_dataset(key, data=data, compression="gzip")

            for key, data in tqdm(
                padded_valid_trajectories.items(), desc="Writing validation data"
            ):
                valid_group.create_dataset(key, data=data, compression="gzip")

            h5f.attrs["words"] = " ".join(words)

def read_hdf5(hdf5_filename):
    with open(hdf5_filename, "rb") as f:
        with h5py.File(f, "r") as h5f:
            words = h5f.attrs["words"].split()
            word2idx = {w: i for i, w in enumerate(words)}

            # We read the whole thing into memory in its uncompressed
            # form, which is expensive upfront and also in terms of memory
            # usage, but works better for random access
            return (
                {k: v[:] for k, v in h5f["train"].items()},
                {k: v[:] for k, v in h5f["valid"].items()},
                words,
                word2idx,
            )

def read_data(filename):
    _, ext = os.path.splitext(filename)

    if ext in (".pt", ".pb"):
        return read_pickle(filename)

    if ext == ".h5":
        return read_hdf5

    raise RuntimeError(f"Don't know how to read extension {ext}")


def premik(ukaz, x, y, smer):
    smeri = "NESW"
    premiki = [(0, -1), (1, 0), (0, 1), (-1, 0)]
    ismer = smeri.index(smer)
    if ukaz == "R":
        smer = smeri[(ismer + 1) % 4]
    elif ukaz == "L":
        smer = smeri[(ismer - 1) % 4]
    else:
        dx, dy = premiki[ismer]
        x += dx * ukaz
        y += dy * ukaz
    return x, y, smer
#print(premik("R",0,0,"N"))

def izvedi(ime_datoteke):
     ime = open(ime_datoteke)
     rezultat = (0,0,"N")
     seznam_zaporednih_stanj = [rezultat]
     for line in ime:
         #print(line)
         if "DESNO" == line.strip():
            rezultat = premik("R",rezultat[0],rezultat[1],rezultat[2])
         elif "LEVO" == line.strip():
            rezultat = premik("L",rezultat[0],rezultat[1],rezultat[2])
         elif "N" == line[0]:
            line = line.split(" ")
            rezultat = premik(int(line[1]),rezultat[0],rezultat[1],rezultat[2])
         seznam_zaporednih_stanj.append(rezultat)
     return seznam_zaporednih_stanj

#print(izvedi("primer.txt"))

def opisi_stanje(x,y,smer):
    if smer == "N":
        smer = "^"
        return "{:3}:{:<3}{:>2}".format(x,y,smer)
    if smer == "S":
        smer = "v"
        return "{:3}:{:<3}{:>2}".format(x,y,smer)
    if smer == "E":
        smer = ">"
        return "{:3}:{:<3}{:>2}".format(x,y,smer)
    if smer == "W":
        smer = "<"
        return "{:3}:{:<3}{:>2}".format(x,y,smer)

#print(opisi_stanje(0, 12, "N"))

def prevedi(ime_vhoda,ime_izhoda):
    stanja_datoteke = izvedi(ime_vhoda)
    ime = open(ime_izhoda, "w")
    for stanje in stanja_datoteke:
        lepse_stanje = opisi_stanje(stanje[0],stanje[1],stanje[2])
        ime.write(lepse_stanje + "\n")

#print(prevedi("primer.txt", "stanja.txt"))

def opisi_stanje_2(x,y,smer):
    if smer == "N":
        smer = "^"
        return "{:2}{:>4}:{:>2}".format(smer,"(" + str(x),str(y) + ")")
    if smer == "S":
        smer = "v"
        return "{:2}{:>4}:{:>2}".format(smer,"(" + str(x),str(y) + ")")
    if smer == "E":
        smer = ">"
        return "{:2}{:>4}:{:>2}".format(smer,"(" + str(x),str(y) + ")")
    if smer == "W":
        smer = "<"
        return "{:2}{:>4}:{:>2}".format(smer,"(" + str(x),str(y) + ")")

print(opisi_stanje_2(0,12,"N"))


import re

import typing as t

from pyke import tasks
from pyke import errors as e

def task(*args, **options: t.Any) -> t.Callable:
    r"""
    Function decorator to create a Pykefile task with a name
    and certain options.

    :param name: Name of the task to be called from the ``pyke``
        command. If no name is given, the function name is used.
        If the task uses a regex ``pattern`` option, that pattern
        will used and matched against args passed to ``pyke``.
    :type name: `Text`, optional

    :options:
        * *default* (``bool``) --
            Setting to ``True`` will set the task as default. This
            task will run when ``pyke``  doesn't recieve any arguments.
        * *deps* (``Iterable[Text]``) --
            List of task names to run before the current task. Tasks
            that use patterns cannot be used as task dependencies.
        * *pattern* (``Text``, `valid regex`) --
            Regex pattern to match against args. All found matches
            will be passed to the task function as string arguments.

    """
    def decorator(func: t.Callable) -> t.Callable:
        try:
            name = args[0]
        except IndexError:
            if options.get("pattern"):
                name = options["pattern"]
            else:
                name = func.__name__

        tasks[name] = {"f": func, "o": options}

        return func

    return decorator

def default_task() -> t.Union[t.Callable, t.NoReturn]:
    for _, task in tasks.items():
        if task["o"].get("default"):
            return task

    return None

def get_task(target: t.Text) -> t.Mapping:
    for n, task in tasks.items():
        print(re.findall(n, target))
        if re.findall(n, target):
            return task

def exec_task(
    task: t.Union[t.Mapping, t.Text],
    args: t.Optional[t.Iterable] = []
) -> t.Any:
    if isinstance(task, t.Text):
        task: t.Mapping = get_task(task)

    rlist: t.Iterable = []
    opts: t.Mapping = task["o"]

    if opts.get("deps"):
        for i in opts["deps"]:
            try:
                tasks[i]["f"]()
            except KeyError:
                raise e.NoTaskFoundError(i)

    if opts.get("pattern"):
        rlist = re.findall(opts["pattern"], ' '.join(args))

    return task["f"](*rlist)


from tkinter import *
from sys import exit

root = Tk()
root.title("BorkaConvert 0.1")

topframe = Frame(root)
topframe.pack(side=TOP)
middleframe = Frame(root)
middleframe.pack()
lowerframe = Frame(root)
lowerframe.pack(side=BOTTOM)

def stop():
    root.destroy()
    exit()

def retrive():
    inputverdi = inn1.get()
    enhetinput = inn2.get()
    enhetoutput = inn3.get()

    ########## tempratur ##########
    if enhetinput.lower() == "celsius" or enhetinput.lower() == "c" :
        enhetinputtype = 1
        konverteringstype = 1
        fancy = "Celsius °C"

    if enhetinput.lower() == "kelvin" or enhetinput.lower() == "k" :
        enhetinputtype = 2
        konverteringstype = 1
        fancy = "K (Kelvin)"

    if enhetinput.lower() == "fahrenheit" or enhetinput.lower() == "f" :
        enhetinputtype = 3
        konverteringstype = 1
        fancy = "Fahrenheit °F"

    if enhetoutput.lower() == "celsius" or enhetoutput.lower() == "c":
        enhetoutputtype = 1
        konverteringstypeout = 1
        fancyout = "Celsius °C"

    if enhetoutput.lower() == "kelvin" or enhetoutput.lower() == "k":
        enhetoutputtype = 2
        konverteringstypeout = 1
        fancyout = "K (Kelvin)"

    if enhetoutput.lower() == "fahrenheit" or enhetoutput.lower() == "f":
        enhetoutputtype = 3
        konverteringstypeout = 1
        fancyout = "Fahrenheit °F"

    ##konverteringstype mulig eller ikke
    if konverteringstype == konverteringstypeout and str(konverteringstype) == 1 :
        konversjon = "tempratur"

    ##konversjon
    if str(enhetinputtype) == "1":
        if str(enhetoutputtype) == "2":
            outputverdi = float(inputverdi) + 273.15
        if str(enhetoutputtype) == "3":
            outputverdi = (((float(inputverdi))*(9/5))+32)

    if str(enhetinputtype) == "2":
        if str(enhetoutputtype) == "1":
            outputverdi = float(inputverdi) - 273.15
        if str(enhetoutputtype) == "3":
            outputverdi = (((float(inputverdi))*(9/5))-459.67)

    if str(enhetinputtype) == "3":
        if str(enhetoutputtype) == "1":
            outputverdi = (float(inputverdi) - 32)*(5/9)
        if str(enhetoutputtype) == "2":
            outputverdi = ((float(inputverdi)+459.67)*(9/5))

    printfuver = (inputverdi + " " + fancy + " er "  + "%.2f" % float(outputverdi) + " " + fancyout)

    printfu = Label(lowerframe, text=printfuver)
    printfu.pack()

intro = Label(middleframe, text= "Jeg ønsker å konverte:")
intro.pack(side=LEFT)

inn1 = Entry(middleframe)
inn1.pack(side=LEFT)

inn2 = Entry(middleframe)
inn2.pack(side=LEFT)

intro2 = Label(middleframe, text= " til ")
intro2.pack(side=LEFT)

inn3 = Entry(middleframe)
inn3.pack(side=LEFT)

buttonenter = Button(lowerframe, text="kalkuler", fg="red", command=retrive)
buttonenter.pack(side=BOTTOM)

button1 = Button(topframe, text="exit", fg="red", command=stop)
button1.pack(side=LEFT)

navn = Label(topframe, text="Enhets konverterer")
navn.pack(side=LEFT)

root.mainloop()


#!/usr/bin/pypy3
from line import Point
from line import Line

class BestLine:
	def __init__(self, points: list):
		if (len(points) < 2):
			self.numPoints = len(points)
			return
		self.numPoints = 0
		for i in range(0, len(points) - 1):
			for j in range(i + 1, len(points)):
				a = points[i]
				b = points[j]
				l = Line(a, b)
				intersections = 2 + sum(1 for p in points[j+1:] if l.intersects(p))
				if (intersections > self.numPoints):
					self.numPoints = intersections
					self.bestFit = l


import csv
import pandas as pd
import pprint
import json
from SF_API import SalesforceAPIBangBang

pp = pprint.PrettyPrinter(indent=4)

data = pd.read_csv(".\\data\\01 SF Folder Lists.csv")
folderNamesDict = dict(zip(data['FolderName'],data['FolderId']))

print(" • • • Folder Names List • • • ")
pp.pprint(folderNamesDict)

data = pd.read_csv(".\\data\\02 SF User Roles.csv")
userRolesDict = dict(zip(data['NAME'], data['ID']))

print("\n\n • • • User Roles List  • • • \n")
pp.pprint(userRolesDict)

salesforceAPI = SalesforceAPIBangBang()

def invokeSFFolderSharesAPI(folderId, shareList):
    #retrieve folder id
    #retreive user role id
    #create a post request
    foldersJSON = salesforceAPI.sf_api_call('/services/data/v46.0/folders/'+folderId+'/shares',
                                     method='post',
                                     data={"shares": shareList})
    pp.pprint(foldersJSON)

inputData = pd.read_csv(".\\data\\Input-FolderName and New Roles.csv")
pp.pprint(inputData)

for key, group_df in inputData.groupby('FolderName'):
    print("the group for folderName '{}' has {} rows".format(key,len(group_df)))
    shareList  = []
    for gRow in group_df.iterrows():

        roleId = userRolesDict[gRow[1]['ShareWith']]
        pp.pprint("• Role ID : " + roleId)

        if roleId is not None:
            shareList.append({"shareWithId" : roleId,
                          "shareType" : gRow[1]['ShareType'],
                          "accessType" : gRow[1]['AccessType']})

    #pp.pprint(shareList)
    pp.pprint(json.dumps(shareList))

    folderId = folderNamesDict[key]
    pp.pprint("Folder ID : " + folderId)

    invokeSFFolderSharesAPI(folderId, shareList)
    #pp.pprint(shareList)

Invoke-REST_FolderAPI-To-Update-Shares/01 Replace Roles in Folders.py


"""scm URL Configuration

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

schema_view = get_schema_view(
    openapi.Info(
        title="Panasonic API (PP+RP)",
        default_version='v1',
        description="Documentation for all PP and RP API",
        terms_of_service="https://www.3scsolution.com",
        contact=openapi.Contact(email="mohit.kumar@3scsolution.com"),
        license=openapi.License(name="Awesome IP"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
    url(r'^doc(?P<format>\.json|\.yaml)$',
            schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('scm/docs/', schema_view.with_ui('swagger', cache_timeout=0),
         name='schema-swagger-ui'),
    path('scm/redoc/', schema_view.with_ui('redoc', cache_timeout=0),
         name='schema-redoc'),
    path('admin/', admin.site.urls),
    path('scm/rp/v1/', include('scm_app.urls')),
    path('user/login/', UserLoginView.as_view() , name='token_obtain_pair'),
]


from django.db import models
from rest_framework import serializers
from .models import PolyLineRoad, Positions, PolyLine,PolyLineTypes,Relevant,PositionGroup

from . import serilizers

class PositionsSerilizer(serializers.ModelSerializer):
    id = serializers.IntegerField(required=False)
    index = serializers.IntegerField(required=False)
    class Meta:
        model = Positions
        fields = ('id', 'lat', 'lng',"index")

class PositionGroupSerializer(serializers.ModelSerializer):

    positions = PositionsSerilizer(many=True)

    class Meta:

        model = PositionGroup
        fields = ('__all__')

class PolyLineASerializer(serializers.ModelSerializer):
    class Meta:
        model = PolyLine
        fields = ('id',)

class PolyLineRoadSerializer(serializers.ModelSerializer):

    polyline = PolyLineASerializer(required=False)

    class Meta:
        model = PolyLineRoad
        fields = ('__all__')

class PolyLineSerializer(serializers.ModelSerializer):

    positionGroup = PositionGroupSerializer(many=True)
    road = PolyLineRoadSerializer(required=False)

    class Meta:
        model = PolyLine
        fields = ('__all__')

    def create(self, validated_data):
        positionGroup = validated_data.pop('positionGroup')
        road = validated_data.pop('road')
        polyline = PolyLine.objects.create(**validated_data)

        print(road)
        typeMarker = validated_data.get('typeMarker')
        print(typeMarker)
        if typeMarker.id == 1:
            polylineRoad = PolyLineRoad.objects.create(polyline=polyline,
            width=road['width'],
            hectar=road['hectar'],
            beton=road['beton'],
            goodSituation=road['goodSituation'],
            badSituation=road['badSituation'],
            yearConstruction=road['yearConstruction']
            )
        for positions in positionGroup:
            posGroup = PositionGroup.objects.create(polyline=polyline)
            print(positions['positions'])
            for path in positions['positions']:
                Positions.objects.create(posGroup=posGroup, **path)
        return polyline

    def update(self, instance, validated_data,partial=True):

        positions_data = validated_data.pop('positions')
        instance.name = validated_data.get('name', instance.name)
        instance.km = validated_data.get('km', instance.km)
        instance.typeMarker = validated_data.get('typeMarker', instance.typeMarker)
        instance.description = validated_data.get('description', instance.description)
        instance.save()

        for position_data in positions_data:
            item_id = position_data.get('id', None)
            if item_id:
                inv_item = Positions.objects.get(id=item_id, polyline=instance)
                inv_item.index = position_data.get('index', inv_item.index)
                inv_item.lat = position_data.get('lat', inv_item.lat)
                inv_item.lng = position_data.get('lng', inv_item.lng)
                inv_item.save()
            else:
                try:
                    index = position_data.get('index', None)
                    inv_item = Positions.objects.get(index=index, polyline=instance)
                    inv_item.index = position_data.get('index', inv_item.index)
                    inv_item.lat = position_data.get('lat', inv_item.lat)
                    inv_item.lng = position_data.get('lng', inv_item.lng)
                    inv_item.save()
                except:
                    Positions.objects.create(polyline=instance, **position_data)

        return instance

class PolylinesTypesSerilizer(serializers.ModelSerializer):

    polyline = PolyLineSerializer(partial=True,many=True)
    class Meta:
        model = PolyLineTypes
        fields = ('__all__')

class RelevantSerializer(serializers.ModelSerializer):

    class Meta:
        model = Relevant
        fields = ('__all__')


import os
import random
import numpy as np
import pandas as pd
from nltk import tokenize

def read_file(f_name: str, dir_path: str):
    """Simple file reader to extract offsets.
    @param f_name is the file name.
    @param dir_path is the path to the `f_name`.

    Returns:
        text: The text of the file, sentences are separated by newlines.
        remapper: A dict mapping the offsets to the new offsets.
    """
    with open(os.path.join(dir_path, f_name), encoding="utf-8-sig") as tmp:
        info = {"title": f_name, "context": tmp.read()}

    title = info["title"]
    text = tokenize.sent_tokenize(info["context"])

    # O.G. offsets do not have spaces in between sentences -> fix this
    text_seen, spaces_added = 0, 0
    remapper = {}
    for idx, sent in enumerate(text):
        for i in range(text_seen, text_seen + len(sent)):
            remapper[i] = i + spaces_added

        spaces_added += 1
        text_seen += len(sent)

    return (title + " ".join(text)).replace("\n", " "), remapper

def split_ids(ids: list, seed: int = 0, val_split: float = 0.10, test_split: float = 0.20):
    """Splits the ids based on the seed and split percents.
    @param ids is a list of document ids (str).
    @param seed is the random seed to use.
    @param val_split is the percent of data to use for validation.
    @param test_split is the percent of data to use for the test set.
    @return train_ids, val_ids, test_ids."""
    random.seed(seed)
    random.shuffle(ids)

    # Train, Val, Test IDs
    train_ids = ids[: int(len(ids) - len(ids) * (test_split + val_split))]
    train_split = int(len(ids) - len(ids) * (test_split + val_split))
    val_ids = ids[train_split : int(train_split + len(ids) * val_split)]
    test_ids = ids[int(train_split + len(ids) * val_split) :]
    return train_ids, val_ids, test_ids

def convert_preds_to_triggers(tokenizer, ids: list, input_ids: list, preds: list):
    """Convert token level predictions to continuous strings.
    @param tokenizer is a HF tokenizer.
    @param ids is a list of strings.
    @param input_ids is a list of tensors containing indicies that each corresponds
    to a discharge summary.
    @param preds is a list of tensors containing binary predictions in which a
    prediction of 1 indicates that the model has predicted that that word is a
    trigger.
    @return a DF of ids, triggers, start/end offsets, and the text."""
    all_ids, all_text, start_indices, end_indices, all_trigger_groups = [], [], [], [], []
    predicted_triggers = np.asarray(input_ids) * np.asarray(preds)

    # For each document, find where there are triggers, and convert to text
    for idx, pt in enumerate(predicted_triggers):
        start_index = [i + 1 for i, x in enumerate(pt) if (i + 1 != len(pt) and x == 0 and pt[i + 1] != 0)]
        end_index = [i + 1 for i, x in enumerate(pt) if (i + 1 != len(pt) and x != 0 and pt[i + 1] == 0)][1:]

        # Use the start + end indexes to get the triggers
        for start, end in zip(start_index, end_index):
            text = tokenizer.convert_tokens_to_string(tokenizer.batch_decode(pt[start:end]))
            before_text = tokenizer.decode(input_ids[idx][:start], skip_special_tokens=True)

            all_trigger_groups.append(text)
            start_indices.append(len(before_text))
            end_indices.append(len(before_text) + len(text))

            all_ids.append(ids[idx])
            all_text.append(tokenizer.decode(input_ids[idx], skip_special_tokens=True))

    return pd.DataFrame(
        {"id": all_ids, "trigger": all_trigger_groups, "st_ch": start_indices, "end_ch": end_indices, "text": all_text}
    )


# Learning Rate Scheduling
# Power Scheduling
from tensorflow import keras
import tensorflow as tf
import numpy as np
import os
import matplotlib as mpl
import matplotlib.pyplot as plt

(X_train_full, y_train_full), (X_test, y_test) = keras.datasets.fashion_mnist.load_data()
X_train_full = X_train_full / 255.0
X_test = X_test / 255.0
X_valid, X_train = X_train_full[:5000], X_train_full[5000:]
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]

optimizer = keras.optimizers.SGD(learning_rate=0.01, decay=1e-4)
model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]),
    keras.layers.Dense(300, activation="selu", kernel_initializer="lecun_normal"),
    keras.layers.Dense(100, activation="selu", kernel_initializer="lecun_normal"),
    keras.layers.Dense(10, activation="softmax")
])
model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"])
n_epochs = 25
history = model.fit(X_train, y_train, epochs=n_epochs,
                    validation_data=(X_valid, y_valid))

import math
learning_rate = 0.01
decay = 1e-4
batch_size = 32
n_steps_per_epoch = math.ceil(len(X_train) / batch_size)
epochs = np.arange(n_epochs)
lrs = learning_rate / (1 + decay * epochs * n_steps_per_epoch)

plt.plot(epochs, lrs, "o-")
plt.axis([0, n_epochs - 1, 0, 0.01])
plt.xlabel("Epoch")
plt.ylabel("Learning Rate")
plt.title("Power Scheduling", fontsize=14)
plt.grid(True)
plt.show()


class Solution:
   def firstBadVersion(self, n: int) -> int:
        checkAfter=False
        checkBefore= True
        start=0
        end=n
        while start < end:
            center= int((start+end)/2)
            check= isBadVersion(center)
            if center<=end-1:
                checkAfter= isBadVersion(center+1)
            if center>0:
                checkBefore= isBadVersion(center-1)

            print(check)
            if check == False and checkAfter == True :
                return center+1
            elif check == True and checkBefore == False:
                return center
            elif check == False:
                start=center
            else:
                end=center
        return -1


from django import http
from django.contrib.auth.decorators import login_required
from django.db.models import Case, Count, Value, When
from django.shortcuts import render

from annotation import services
from annotation.models import Annotation, Dataset, DatasetRow
from sirene.models import CodeNAF
from sirene.services import get_naf_data_by_code

@login_required()
def index(request: http.HttpRequest):
    unsafe_dataset_slug_str = request.GET.get("dataset", None)

    try:
        dataset_instance = Dataset.objects.get(slug=unsafe_dataset_slug_str)
    except Dataset.DoesNotExist:
        return http.HttpResponseNotFound()

    if not request.user.groups.filter(id=dataset_instance.organization.id).exists():
        return http.HttpResponseForbidden()

    context = {
        "dataset_instance": dataset_instance,
        "annotation_queryset": Annotation.objects.filter(created_by=request.user),
    }

    return render(request, "annotation/index.html", context)

@login_required()
def partial_task(request: http.HttpRequest):
    if not request.htmx:
        return http.HttpResponseNotFound()

    unsafe_dataset_instance_id = request.GET.get("dataset_instance_id", None)

    try:
        dataset_instance = Dataset.objects.get(id=unsafe_dataset_instance_id)
    except Dataset.DoesNotExist:
        return http.HttpResponseNotFound()

    # select next row instance:
    # exclude annotated rows
    qs = (
        DatasetRow.objects.annotate(Count("annotations"))
        .filter(dataset_id=unsafe_dataset_instance_id)
        .exclude(annotations__count__gt=0)
    )

    # optionally prioritize rows based on their code_postal prefix
    high_priority_postcode_prefix = dataset_instance.priority_settings.get("postcode_prefix", "not_a_prefix")

    qs = qs.annotate(
        has_high_priority=Case(
            When(data__code_postal__startswith=high_priority_postcode_prefix, then=Value(True)),
            default=Value(False),
        )
    )

    row_instance = qs.order_by("-has_high_priority", "?").first()

    progress_current = (
        DatasetRow.objects.annotate(Count("annotations"))
        .filter(dataset_id=unsafe_dataset_instance_id)
        .filter(annotations__count__gt=0)
        .count()
    )
    progress_total = DatasetRow.objects.filter(dataset_id=unsafe_dataset_instance_id).count()

    if row_instance is None:
        return http.HttpResponse("Vous avez terminé ! :)")

    context = {
        "dataset_instance": dataset_instance,
        "progress_str": f"{progress_current} / {progress_total}",
        "row_instance": row_instance,
        "establishment_queryset": services.search_sirene(
            adresse=row_instance.task_data["similar_address"] or row_instance.task_data["adresse"],
            name=row_instance.task_data["nom"],
            postal_code=row_instance.task_data["code_postal"],
            siret=row_instance.task_data["siret"],
        ),
        "activity_list": [
            get_naf_data_by_code(level, code)
            for level, code in [
                (CodeNAF.Level.SECTION, "O"),
                (CodeNAF.Level.SECTION, "Q"),
                (CodeNAF.Level.DIVISION, "91"),
            ]
        ],
    }

    if dataset_instance.show_nearby_cnfs_permanences:
        context["permanence_queryset"] = services.search_cnfs(
            siret=row_instance.task_data["siret"],
            longitude=row_instance.data["longitude"],
            latitude=row_instance.data["latitude"],
        )

    return render(request, "annotation/task.html", context)

@login_required()
def partial_search(request: http.HttpRequest):
    if not request.htmx:
        return http.HttpResponseNotFound()

    unsafe_address = request.POST.get("adresse", None)
    unsafe_name = request.POST.get("nom", None)
    unsafe_postal_code = request.POST.get("code_postal", None)
    unsafe_siret = request.POST.get("siret", None)
    unsafe_naf_activities = [value.split(",") for value in request.POST.getlist("naf_activities", []) if value != ""]

    context = {
        "establishment_queryset": services.search_sirene(
            adresse=unsafe_address,
            name=unsafe_name,
            postal_code=unsafe_postal_code,
            siret=unsafe_siret,
            naf_activities=unsafe_naf_activities,
        )
    }

    return render(request, "annotation/search.html", context)

@login_required()
def partial_submit(request: http.HttpRequest):
    if not request.htmx:
        return http.HttpResponseNotFound()

    unsafe_dataset_instance_id = request.POST.get("dataset_instance_id", None)
    unsafe_row_instance_id = request.POST.get("row_instance_id", None)
    unsafe_skipped = request.POST.get("skipped", None)
    unsafe_closed = request.POST.get("closed", None)
    unsafe_irrelevant = request.POST.get("irrelevant", None)
    unsafe_is_parent = request.POST.get("is_parent", None)
    unsafe_siret = request.POST.get("siret", None)

    if not DatasetRow.objects.filter(dataset_id=unsafe_dataset_instance_id).filter(id=unsafe_row_instance_id).exists():
        return http.HttpResponseBadRequest()

    Annotation.objects.create(
        row_id=unsafe_row_instance_id,
        skipped=bool(unsafe_skipped),
        closed=bool(unsafe_closed),
        irrelevant=bool(unsafe_irrelevant),
        is_parent=bool(unsafe_is_parent),
        siret=unsafe_siret if unsafe_siret is not None else "",
        created_by=request.user,
    )

    context = {
        "skipped": unsafe_skipped,
        "irrelevant": unsafe_irrelevant,
        "is_parent": unsafe_is_parent,
        "closed": unsafe_closed,
    }

    return render(request, "annotation/submit.html", context)


import pygame
import pygame_menu
import sys
from settings import windowX,windowY, DEBUG, window_caption,logo,gamefont, FPS
from bullets import Bullet
from beings import *
from surfaces import SlowTimeSurf, BuffRandomSurf
from particles import *

pygame.init()
pygame.display.set_caption(window_caption)
window = pygame.display.set_mode((windowX,windowY))
pygame.display.set_icon(logo)
font = pygame.font.Font(gamefont, 46)

################################################################
def play():
    if DEBUG == True:
        from bullets import player_bullets_on_screen,enemy_bul_on_screen
    clock = pygame.time.Clock()
    pygame.mouse.set_visible(False)
    slowsurf = SlowTimeSurf(384,64)
    randsurf = BuffRandomSurf(540,64)
    enemy = Enemy(100,100)
    enemy2 = Enemy(windowX,200)
    enemies_on_screen.append(enemy)
    enemies_on_screen.append(enemy2)
    print(enemies_on_screen)
    player = Player()

    death_timer = 1.5 #in seconds
    death_timer_calculation = (death_timer/FPS)/death_timer
    SCORE = 0
########################## LAYER 0
    while True:
        for event in pygame.event.get():
        	if event.type == pygame.QUIT:
        		pygame.quit(),sys.exit()
        		break

        window.fill((41, 64, 59))
        pygame.draw.rect(window,(0,0,0),(0,720,windowX,5))

        if DEBUG == True:
            for row in range(0,windowX,32):
                pygame.draw.rect(window,(184, 227, 179, 1),(row,0,2,windowY))
            for column in range(0,windowY,64):
                pygame.draw.rect(window,(184, 227, 179),(0,column,windowX,2))
            #print('power_of_random: ',player.power_of_random)
            print('bullets_on_screen: ', str(len(player_bullets_on_screen + enemy_bul_on_screen)))

########################## LAYER 0 END

########################## LAYER 1
        if slowsurf.switch == True:
            slowsurf.place(window)
            if slowsurf.collide(player.player_rect):
                print("<Sakuya slows the flow of time !>")
                slowsurf.switch = False
        else:
            #for enemy in enemies_on_screen: #Doesn`t work and I don`t know why
            slowsurf.activate(enemy)
            slowsurf.activate(enemy2)

        if randsurf.switch == True:
            randsurf.place(window)
            if randsurf.collide(player.player_rect):
                print('<Randoumu no pawa~ !>')
                randsurf.switch = False
        else:
            randsurf.activate(window,player)

        player.teleportation()
        player.shoot(window)
        player.draw(window)
        player.update()
        player.collision(window)

        if player.out_of_area():
            death_timer -= death_timer_calculation
            timertext_color = (255-death_timer*150,170*death_timer,10)
            text = font.render(str(round(death_timer,2)), True, timertext_color)
            window.blit(text,(windowX//2,windowY//2 - 100))
            if death_timer < 0.1:
                print('DED')
                if DEBUG == True:
                    print('TOTAL {}'.format(time.time()-start))
                break
        else:
            if DEBUG == True:
                start = time.time()
            death_timer = 1.5

########################## LAYER 1 END

########################## LAYER 2
        if enemy.alive == True:
            enemy.draw(window)
            enemy.move('right')
            enemy.enemy_shoot(window)
            enemy.collision(window)
            if enemy.enemy_x >= windowX-10: #That returning thingy
                enemy.enemy_x = 5

        if enemy2.alive == True:
            enemy2.draw(window)
            enemy2.enemy_shoot(window)
            enemy2.move('left')
            enemy2.collision(window)
            if enemy2.enemy_x <= 5: #That returning thingy
                enemy2.enemy_x = windowX

        if enemy.out_of_area() and enemy.alive == True or enemy.enemy_y + enemy.en_height >= 720 and enemy.alive == True:
            enemy.alive = False
            SCORE += 1
        if enemy2.out_of_area() and enemy2.alive == True or enemy2.enemy_y + enemy2.en_height >= 720 and enemy2.alive == True:
            enemy2.alive = False
            SCORE += 1

        if enemy.alive == False and enemy2.alive == False:
            print('Win-win')
            break

########################## LAYER 2 END

########################## LAYER 3
        score_font = font.render(str(SCORE), True, (114, 150, 47))
        window.blit(score_font,(100,100))
########################## LAYER 3 END
        clock.tick(FPS)
        pygame.display.update()
        pass

##################################################################
def quit():
    pygame_menu.events.EXIT
    sys.exit()

while True:
    for event in pygame.event.get():
    	if event.type == pygame.QUIT:
    		pygame.quit(),sys.exit()
    		break

    menu = pygame_menu.Menu(windowX,windowY,'Healthless',theme = pygame_menu.themes.THEME_BLUE)
    menu.add_button('Play', play)
    menu.add_button('Quit', quit)
    menu.mainloop(window)


import json
from datetime import datetime

from flask import Blueprint, jsonify, session, request
from server.models import *
from flask_login import current_user, login_required

from server.utils.cipher_suite import *

invite_routes = Blueprint('invite', __name__)

@invite_routes.route('/<IID>', methods=["GET"])
def check_invite(IID):
    bad_invite = {"errors": ["The supplied invite is invalid"]}
    try:
        invite_id = decodeInviteId(IID)

        invite = Show_Partner_Invite.query.get(invite_id)

        SID = request.args.get("SID")
        BID = request.args.get("BID")

        good_invite = invite.to_dict()

        if invite:
            if not invite.is_open:
                raise Exception("Invite has already been accepted")
            if invite.show.SID == SID:
                if BID or invite.booth:
                    if BID == invite.booth.BID:
                        return good_invite, 200
                else:
                    return good_invite, 200
        raise Exception("Invite does not exist")
    except:
        return bad_invite, 401

@invite_routes.route('/<IID>', methods=["POST"])
def accept_invite(IID):
    invite_id = decodeInviteId(IID)

    check = check_invite(IID)

    if check[1] != 200:
        return check[0]

    invite = Show_Partner_Invite.query.get(invite_id)

    good_invite = invite.to_dict()

    if invite:
        invite.acceptee = current_user
        db.session.commit()
        return {'success': 'invite accepted'}
    return {"errors": ["The supplied invite is invalid"]}, 400

@invite_routes.route('/<IID>', methods=["DELETE"])
@login_required
def delete_show_invite(IID):
    invite_id = decodeInviteId(IID)

    invite = Show_Partner_Invite.query.get(invite_id)

    if invite and invite.creator == current_user:
        db.session.delete(invite)
        db.session.commit()
        return {'success': 'Invite has been deleted'}
    else:
        return {'errors': 'Unauthorized'}


'''
Implementation of a Hebbian learning layer to use as a computational module in building deep neural networks.
'''

__author__ = 'Dan Saunders'

from keras import backend as K
from keras.engine.topology import Layer

import numpy as np
import tensorflow as tf

np.set_printoptions(threshold=np.nan)

sess = tf.Session()

class Hebbian(Layer):

	def __init__(self, output_dim, lmbda=1.0, eta=0.0005, connectivity='random', connectivity_prob=0.25, **kwargs):
		'''
		Constructor for the Hebbian learning layer.

		args:
			output_dim - The shape of the output / activations computed by the layer.
			lambda - A floating-point valued parameter governing the strength of the Hebbian learning activation.
			eta - A floating-point valued parameter governing the Hebbian learning rate.
			connectivity - A string which determines the way in which the neurons in this layer are connected to
				the neurons in the previous layer.
		'''
		self.output_dim = output_dim
		self.lmbda = lmbda
		self.eta = eta
		self.connectivity = connectivity
		self.connectivity_prob = connectivity_prob

		if self.connectivity == 'random':
			self.B = np.random.random(self.output_dim) < self.connectivity_prob
		elif self.connectivity == 'zero':
			self.B = np.zeros(self.output_dim)

		super(Hebbian, self).__init__(**kwargs)

	def random_conn_init(self, shape, dtype=None):
		A = np.random.normal(0, 1, shape)
		A[self.B] = 0
		return tf.constant(A, dtype=tf.float32)

	def zero_init(self, shape, dtype=None):
		return np.zeros(shape)

	def build(self, input_shape):
		# create weight variable for this layer according to user-specified initialization
		if self.connectivity == 'all':
			self.kernel = self.add_weight(name='kernel', shape=(np.prod(input_shape[1:]), \
								np.prod(self.output_dim)), initializer='uniform', trainable=False)
		elif self.connectivity == 'random':
			self.kernel = self.add_weight(name='kernel', shape=(np.prod(input_shape[1:]), \
								np.prod(self.output_dim)), initializer=self.random_conn_init, trainable=False)
		elif self.connectivity == 'zero':
			self.kernel = self.add_weight(name='kernel', shape=(np.prod(input_shape[1:]), \
								np.prod(self.output_dim)), initializer=self.zero_init, trainable=False)
		else:
			raise NotImplementedError

		# ensure that no neuron is laterally connected to itself
		self.kernel = self.kernel * tf.diag(tf.zeros(self.output_dim))

		# call superclass "build" function
		super(Hebbian, self).build(input_shape)

	def call(self, x):
		x_shape = tf.shape(x)
		batch_size = tf.shape(x)[0]

		# reshape to (batch_size, product of other dimensions) shape
		x = tf.reshape(x, (tf.reduce_prod(x_shape[1:]), batch_size))

		# compute activations using Hebbian-like update rule
		activations = x + self.lmbda * tf.matmul(self.kernel, x)

		# compute outer product of activations matrix with itself
		outer_product = tf.matmul(tf.expand_dims(x, 1), tf.expand_dims(x, 0))

		# update the weight matrix of this layer
		self.kernel = self.kernel + tf.multiply(self.eta, tf.reduce_mean(outer_product, axis=2))
		self.kernel = tf.multiply(self.kernel, self.B)
		self.kernel = self.kernel * tf.diag(tf.zeros(self.output_dim))

		return K.reshape(activations, x_shape)


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

from PyQt5 import QtWidgets, QtCore, QtGui
import os
import io
import aqt
import sys
from . import preferences
from . import reader_util
from . import updates
from . import about
from . import constants
from .gen import reader_ui
from . import profiles
from .file_state import FileState
from .constants import extensions

class Container(object):
        pass

class MyKeyFilter(QtCore.QObject):
    obj = None

    def eventFilter(self, unused, event):
        obj = self.obj
        if event.type() != QtCore.QEvent.KeyPress:
            return False
        if QtCore.Qt.Key_F1 == event.key() and event.modifiers() & QtCore.Qt.ControlModifier:
            obj.executeDefCommand('sentence_add',0)
        elif event.key() == preferences.lookupKeys[obj.preferences['lookupKey']][1]:
            obj.updateSampleFromPosition()
        elif (event.key() == QtCore.Qt.Key_W and event.modifiers() & QtCore.Qt.AltModifier):
            obj.createAlias()
        elif (event.key() == QtCore.Qt.Key_S and event.modifiers() & QtCore.Qt.AltModifier):
            obj.createSubscription()
        elif (event.key() == QtCore.Qt.Key_R and event.modifiers() & QtCore.Qt.AltModifier):
            obj.restoreRecentIncorrect()
        elif (event.key() == QtCore.Qt.Key_N and event.modifiers() & QtCore.Qt.AltModifier):
            obj.showNotFoundWords()
        elif ord('0') <= event.key() <= ord('9') and obj.anki is not None:
            index = (event.key() - ord('0') - 1) % 10
            if event.modifiers() & QtCore.Qt.ShiftModifier:
                if event.modifiers() & QtCore.Qt.ControlModifier:
                    obj.executeDefCommand('kanji_add', index)
                else:
                    return False
            else:
                if event.modifiers() & QtCore.Qt.ControlModifier:
                    obj.executeDefCommand('vocabulary_add', index)
                elif event.modifiers() & QtCore.Qt.AltModifier:
                    obj.executeDefCommand('vocabulary_add_reading', index)
                else:
                    return False
        elif event.key() == ord('[') and obj.state.scanPosition > 0:
            obj.state.scanPosition -= 1
            obj.updateSampleFromPosition()
        elif event.key() == ord(']') and obj.state.scanPosition < len(obj.textContent.toPlainText()) - 1:
            obj.state.scanPosition += 1
            obj.updateSampleFromPosition()
        else:
            return False
        return True

class MainWindowReader(QtWidgets.QMainWindow, reader_ui.Ui_MainWindowReader):

    class State:
        def __init__(self):
            self.filename = ""
            self.scanPosition = 0
            self.searchPosition = 0
            self.searchText = ""

    def __init__(self, plugin, parent, preferences, languages, anki=None, closed=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.debug = []
        self.setupUi(self)
        self.parent = parent
        self.textContent.mouseMoveEvent = self.onContentMouseMove
        self.textContent.mousePressEvent = self.onContentMousePress
        self.dockAnki.setEnabled(anki is not None)
        self.currentFile = None
        self.plugin = plugin
        self.preferences = preferences
        self.anki = anki
        self.facts = list()
        self.recentIncorrect = None
        self.listDefinitions.clear()
        self.closed = closed
        self.languages = languages
        self.state = self.State()
        self.updates = updates.UpdateFinder()
        self.zoom = 0
        self.updateRecentFiles()

        self.actionAbout.triggered.connect(self.onActionAbout)
        self.actionFeedback.triggered.connect(self.onActionFeedback)
        self.actionFind.triggered.connect(self.onActionFind)
        self.actionFindNext.triggered.connect(self.onActionFindNext)
        self.actionHomepage.triggered.connect(self.onActionHomepage)
        self.actionKindleDeck.triggered.connect(self.onActionKindleDeck)
        self.actionWordList.triggered.connect(self.onActionWordList)
        self.actionOpen.triggered.connect(self.onActionOpen)
        self.actionSave.triggered.connect(self.onActionSave)
        self.actionPreferences.triggered.connect(self.onActionPreferences)
        self.actionToggleWrap.toggled.connect(self.onActionToggleWrap)
        self.actionToggleJapanese.toggled.connect(self.onActionToggleLanguage("japanese"))
        self.actionToggleKorean.toggled.connect(self.onActionToggleLanguage("korean"))
        self.actionToggleChinese.toggled.connect(self.onActionToggleLanguage("chinese"))
        self.actionToggleGerman.toggled.connect(self.onActionToggleLanguage("german"))
        self.actionToggleSpanish.toggled.connect(self.onActionToggleLanguage("spanish"))
        self.actionToggleFrench.toggled.connect(self.onActionToggleLanguage("french"))
        self.actionZoomIn.triggered.connect(self.onActionZoomIn)
        self.actionZoomOut.triggered.connect(self.onActionZoomOut)
        self.actionZoomReset.triggered.connect(self.onActionZoomReset)
        self.dockAnki.visibilityChanged.connect(self.onVisibilityChanged)
        if self.anki is not None:
            self.learnVocabulary.clicked.connect(self.onLearnVocabularyList)
            self.removeVocabulary.clicked.connect(self.onRemoveVocabulary)
        self.listDefinitions.itemDoubleClicked.connect(self.onDefinitionDoubleClicked)
        self.updates.updateResult.connect(self.onUpdaterSearchResult)

        self.keyFilter = MyKeyFilter()
        self.keyFilter.obj = self
        self.installEventFilter(self.keyFilter)
        self.textContent.installEventFilter(self.keyFilter)

        if self.preferences['checkForUpdates']:
            self.updates.start()
        self.profiles = profiles.getAllProfiles(self)
        for profile in self.profiles.values():
            profile.updateDefinitions()
        if self.preferences['rememberTextContent']:
            self.textContent.setPlainText(self.preferences['textContent'])
        elif self.anki is not None:
            self.currentFile = FileState(None, self.preferences['stripReadings'],self.languages,self.profiles)

        self.applyPreferences()

    def getSortedProfiles(self):
        def sortFunction(profile):
            return profile.sortIndex
        return sorted(self.profiles.values(),key=sortFunction)

    def onRemoveVocabulary(self):
        row = self.listDefinitions.currentRow()
        word = self.facts[row]['word']
        profile = self.facts[row]['profile']
        self.listDefinitions.takeItem(row)
        if word in self.currentFile.profiles[profile]['wordsAll']:
            del self.currentFile.profiles[profile]['wordsAll'][word]
        if word in self.currentFile.profiles[profile]['wordsBad']:
            del self.currentFile.profiles[profile]['wordsBad'][word]
        self.setStatus(u'Removed {0} from vocabulary list'.format(word))

    def onLearnVocabularyList(self):
        if self.anki is None:
            return
        self.currentFile.onLearnVocabularyList(self.anki.collection().sched)
        totalSeconds = int(self.currentFile.timeTotal) %  60
        totalMinutes = int(self.currentFile.timeTotal) // 60
        perCardSeconds = int(self.currentFile.timePerWord) %  60
        perCardMinutes = int(self.currentFile.timePerWord) // 60
        QtWidgets.QMessageBox.information(
            self,
            'Yomichan', '{0} correct and {1} wrong\n{2} minutes {3} seconds for all\n{4} minutes {5} seconds per card'
            .format(self.currentFile.correct,self.currentFile.wrong,
                totalMinutes,totalSeconds,
                perCardMinutes,perCardSeconds)
        )

    def applyPreferences(self):

        self.comboTags.addItems(self.preferences['tags'])
        self.applyPreferencesContent()

        if self.preferences['firstRun']:
            QtWidgets.QMessageBox.information(
                self,
                'Yomisama',
                'This may be the first time you are running Yomisama.\nPlease take some time to configure this extension.'
            )

            self.onActionPreferences()

        if self.preferences['maximized'] == 'yes':
            self.showMaximized()
        if self.preferences['windowState'] is not None:
            self.restoreState(QtCore.QByteArray.fromBase64(self.preferences['windowState'].encode("utf-8")))
        if self.preferences['windowGeometry'] is not None:
            self.restoreGeometry(QtCore.QByteArray.fromBase64(self.preferences['windowGeometry'].encode("utf-8")))

    def applyPreferencesContent(self):
        palette = self.textContent.palette()
        palette.setColor(QtGui.QPalette.Base, QtGui.QColor(self.preferences['bgColor']))
        palette.setColor(QtGui.QPalette.Text, QtGui.QColor(self.preferences['fgColor']))
        self.textContent.setPalette(palette)

        self.textContent.setReadOnly(not self.preferences['allowEditing'])
        self.textContent.setAttribute(QtCore.Qt.WA_InputMethodEnabled)

        font = self.textContent.font()
        font.setFamily(self.preferences['fontFamily'])
        font.setPointSize(self.preferences['fontSize'] + self.zoom)
        self.textContent.setLineWrapMode(QtWidgets.QPlainTextEdit.WidgetWidth if self.preferences['wordWrap'] else QtWidgets.QPlainTextEdit.NoWrap)
        self.textContent.setFont(font)

        self.actionToggleWrap.setChecked(self.preferences['wordWrap'])
        self.actionToggleJapanese.setChecked(self.preferences['japanese'])
        self.actionToggleKorean.setChecked(self.preferences['korean'])
        self.actionToggleChinese.setChecked(self.preferences['chinese'])
        self.actionToggleGerman.setChecked(self.preferences['german'])
        self.actionToggleSpanish.setChecked(self.preferences['spanish'])
        self.actionToggleFrench.setChecked(self.preferences['french'])

    def closeEvent(self, event):
        self.preferences['windowState'] = str(self.saveState().toBase64())
        self.preferences['windowGeometry'] = str(self.saveGeometry().toBase64())
        self.preferences['maximized'] = "yes" if self.isMaximized() else "no"

        self.closeFile()
        self.preferences.save()

        if self.anki is not None:
            self.anki.stopEditing()

        if self.closed is not None:
            self.closed()

        for key,profile in self.profiles.items():
            profile.close()

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()

    def dropEvent(self, event):
        url = event.mimeData().urls()[0]
        self.openFile(url.toLocalFile())

    def moveEvent(self, event):
        if hasattr(self, 'windowPosition'):
            self.preferences['windowPosition'] = event.pos().x(), event.pos().y()

    def resizeEvent(self, event):
        if hasattr(self, 'windowSize'):
            self.preferences['windowSize'] = event.size().width(), event.size().height()

    def getFileFilter(self):
        return ';;'.join(
            ['Text files (' +
             ' '.join(map(lambda x:'*'+x,extensions['text'])) +')',
             'All files (*.*)'])

    def onActionOpen(self):
        filename, filt = QtWidgets.QFileDialog.getOpenFileName(
            parent=self,
            caption='Select a file to open',
            directory=self.state.filename,
                filter=self.getFileFilter()
        )
        if filename:
            self.openFile(filename)

    def onActionSave(self):
        filename, filter_str = QtWidgets.QFileDialog.getSaveFileName(
            parent=self,
            caption='Select a file to save',
            directory=self.state.filename,
            filter=self.getFileFilter()
        )
        if filename:
            self.saveFile(filename)

    def onActionKindleDeck(self):
        filename = QtWidgets.QFileDialog.getOpenFileName(
            parent=self,
            caption='Select a Kindle deck to import',
            filter='Deck files (*.db)'
        )
        if filename:
            words = reader_util.extractKindleDeck(filename)
            self.importWordList(words)

    def onActionWordList(self):
        filename = QtWidgets.QFileDialog.getOpenFileName(
            parent=self,
            caption='Select a word list file to import',
            filter=self.getFileFilter()
        )
        if filename:
            words = reader_util.extractWordList(filename)
            self.importWordList(words)

    def onActionPreferences(self):
        dialog = preferences.DialogPreferences(self, self.preferences, self.anki, self.profiles)
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            self.applyPreferencesContent()

    def onActionAbout(self):
        dialog = about.DialogAbout(self)
        dialog.exec_()

    def onActionZoomIn(self):
        font = self.textContent.font()
        if font.pointSize() < 72:
            font.setPointSize(font.pointSize() + 1)
            self.textContent.setFont(font)
            self.zoom += 1

    def onActionZoomOut(self):
        font = self.textContent.font()
        if font.pointSize() > 1:
            font.setPointSize(font.pointSize() - 1)
            self.textContent.setFont(font)
            self.zoom -= 1

    def onActionZoomReset(self):
        if self.zoom:
            font = self.textContent.font()
            font.setPointSize(font.pointSize() - self.zoom)
            self.textContent.setFont(font)
            self.zoom = 0

    def onActionFind(self):
        searchText = self.state.searchText

        cursor = self.textContent.textCursor()
        if cursor.hasSelection():
            searchText = cursor.selectedText()

        searchText, ok = QtWidgets.QInputDialog.getText(self, 'Find', 'Search text:', text=searchText)
        if searchText and ok:
            self.findText(searchText)

    def onActionFindNext(self):
        if self.state.searchText:
            self.findText(self.state.searchText)

    def onActionToggleWrap(self, wrap):
        self.preferences['wordWrap'] = wrap
        self.textContent.setLineWrapMode(QtWidgets.QPlainTextEdit.WidgetWidth if self.preferences['wordWrap'] else QtWidgets.QPlainTextEdit.NoWrap)

    def onActionToggleLanguage(self, language):
        def inner(enable):
            self.preferences[language] = enable
            if language not in self.languages and enable:
                self.plugin.loadLanguage(language,callback=None)
            elif language in self.languages and not enable:
                del self.languages[language]
        return inner

    def onActionHomepage(self):
        url = QtCore.QUrl('http://dayjaby.wordpress.com')
        QtWidgets.QDesktopServices().openUrl(url)

    def onActionFeedback(self):
        url = QtCore.QUrl('http://dayjaby.wordpress.com')
        QtWidgets.QDesktopServices().openUrl(url)

    def onDefinitionDoubleClicked(self, item):
        row = self.listDefinitions.row(item)
        profile = self.preferences['profiles'].get(self.facts[row]['profile'])
        if profile is not None and self.anki is not None:
            key = self.anki.getModelKey(profile['model'])
            self.anki.browse({key:u'"'+self.facts[row]['word']+u'"',u'note':profile['model']})

    def onVisibilityChanged(self, visible):
        self.actionToggleAnki.setChecked(self.dockAnki.isVisible())

    def onUpdaterSearchResult(self, versions):
        if versions['latest'] > constants.c['appVersion']:
            dialog = updates.DialogUpdates(self, versions)
            dialog.exec_()

    def onContentMouseMove(self, event):
        QtWidgets.QPlainTextEdit.mouseMoveEvent(self.textContent, event)
        self.updateSampleMouseEvent(event)

    def onContentMousePress(self, event):
        QtWidgets.QPlainTextEdit.mousePressEvent(self.textContent, event)
        self.updateSampleMouseEvent(event)

    def findTerm(self, text, wildcards=False):
        maxLength = 0
        self.profiles["vocabulary"].definitions = []
        for key, language in self.languages.items():
            if self.preferences[key]:
                vocabDefs, length = language.findTerm(text, wildcards)
                self.profiles["vocabulary"].definitions += vocabDefs
                if length is not None and length > maxLength:
                    maxLength = length
        return maxLength

    def openFile(self, filename):
        self.closeFile()
        try:
            self.currentFile = FileState(filename, self.preferences['stripReadings'],self.languages,self.profiles)
        except IOError:
            self.setStatus(u'Failed to load file {0}'.format(filename))
            QtWidgets.QMessageBox.critical(self, 'Yomichan', 'Cannot open file for read')
            return
        self.listDefinitions.clear()
        self.facts = []

        self.updateRecentFile()
        self.updateRecentFiles()

        allCards = self.plugin.fetchAllCards()
        if allCards is not None:
            self.currentFile.findVocabulary(self.anki.collection().sched,allCards)
            # if file contains exported vocabs, create the cards
            for k,profile in self.currentFile.profiles.items():
                if self.currentFile.exportedVocab:
                    for notFound in profile['wordsNotFound']:
                        self.ankiAddFact(k,profile['wordsMarkup'][notFound],addToList=False)
                for word,card in profile['wordsAll'].items():
                    self.facts.append({'word':word,'profile':k})
                    self.listDefinitions.addItem(word)
            self.listDefinitions.setCurrentRow(self.listDefinitions.count() - 1)

        content = self.currentFile.content
        self.state.filename = filename
        self.state.scanPosition = self.preferences.filePosition(filename)
        if self.state.scanPosition > len(content):
            self.state.scanPosition = 0
        self.textContent.setPlainText(content)
        if self.state.scanPosition > 0:
            cursor = self.textContent.textCursor()
            cursor.setPosition(self.state.scanPosition)
            self.textContent.setTextCursor(cursor)
            self.textContent.centerCursor()

        self.setWindowTitle(u'Yomisama - {0} ({1})'.format(os.path.basename(filename), self.currentFile.encoding))
        self.setStatus(u'Loaded file {0}'.format(filename))

    def saveFile(self, filename):
        try:
            with io.open(filename,'w',encoding='utf-8') as fp:
                if filename.endswith('.json'):
                    content = self.currentFile.getExportJSON(self.textContent.toPlainText())
                else:
                    content = self.textContent.toPlainText()
                    if len(content)>0 and content[-1] != u'\n':
                        content += u'\n'
                    content+= self.currentFile.getAliasList()
                    content+= self.currentFile.getExportVocabularyList('vocabulary') + u'\n'
                    content+= self.currentFile.getExportVocabularyList('sentence') + u'\n'
                    # content+= self.currentFile.getExportVocabularyList('movie') + u'\n'
                    content+= self.currentFile.getExportVocabularyList('kanji')
                fp.write(content)
                fp.close()
        except IOError:
            self.setStatus(u'Failed to save file {0}'.format(filename))
            QtWidgets.QMessageBox.critical(self, 'Yomichan', 'Cannot open file for write')
            return
        self.state.filename = filename
        self.currentFile.filename = filename
        self.updateRecentFile()
        self.updateRecentFiles()
        self.setWindowTitle(u'Yomisama - {0} ({1})'.format(os.path.basename(filename), 'utf-8'))

    def closeFile(self):
        if self.preferences['rememberTextContent']:
            self.preferences['textContent'] = self.textContent.toPlainText()

        self.setWindowTitle('Yomisama')
        self.textContent.setPlainText("")
        self.updateRecentFile(False)
        self.state = self.State()

    def findText(self, text):
        content = self.textContent.toPlainText()
        index = content.find(text, self.state.searchPosition)

        if index == -1:
            wrap = self.state.searchPosition != 0
            self.state.searchPosition = 0
            if wrap:
                self.findText(text)
            else:
                QtWidgets.QMessageBox.information(self, 'Yomisama', 'Search text not found')
        else:
            self.state.searchPosition = index + len(text)
            cursor = self.textContent.textCursor()
            cursor.setPosition(index, QtGui.QTextCursor.MoveAnchor)
            cursor.setPosition(self.state.searchPosition, QtGui.QTextCursor.KeepAnchor)
            self.textContent.setTextCursor(cursor)

        self.state.searchText = text

    def ankiOverwriteFact(self, completeProfile, markup):
        if markup is None:
            self.overwrite = "markup is None"
            return False
        if self.anki is None:
            return False

        profile = self.preferences['profiles'].get(completeProfile)
        if profile is None:
            self.overwrite = "profile is None"
            return False
        fields = reader_util.formatFields(profile['fields'], markup)
        tagsSplit = reader_util.splitTags(self.comboTags.currentText())
        tagsJoined = ' '.join(tagsSplit)
        tagIndex = self.comboTags.findText(tagsJoined)
        if tagIndex > 0:
            self.comboTags.removeItem(tagIndex)
        if tagIndex != 0:
            self.comboTags.insertItem(0, tagsJoined)
        self.preferences.updateFactTags(tagsJoined)
        key = self.anki.getModelKey(profile['model'])
        value = fields[key]
        ids = self.anki.getNotes(profile['model'],key,value)
        if len(ids) == 0:
            self.overwrite = "len(ids)==0"
            return False

        # Overwrite the fields in the note
        # or add a line, if a + is at the end of field name
        note = self.anki.collection().getNote(ids[0])
        self.overwrite = {
            "note": note,
            "fields": fields
        }
        for name, v in fields.items():
            if name in note:
                if name[-1] == '+':
                    if not v in note[name].split(u'<br>'):
                        if len(note[name])>0:
                            note[name]+= u'<br>' + v
                        else:
                            note[name] = v
                else:
                    note[name] = v
        note.flush()
        self.currentFile.profiles[completeProfile]['freshlyAdded'].append(value)
        cids = self.anki.getCardsByNote(profile['model'],key,value)
        if len(cids) == 0:
            return False

        did = self.anki.collection().decks.id(profile['deck'])
        self.anki.updateCards(cids,did)
        card = self.anki.collection().getCard(cids[0])
        self.currentFile.overwriteVocabulary(completeProfile,value,card)
        self.currentFile.addMarkup(completeProfile,value,markup)
        self.facts.append({'word':value,'profile':completeProfile})
        self.listDefinitions.addItem(value)
        self.listDefinitions.setCurrentRow(self.listDefinitions.count() - 1)
        for profile in self.profiles.values():
            profile.updateDefinitions(scroll=True)
        return True

    def ankiAddFact(self, completeProfile, markup, addToList = True):
        if markup is None:
            self.add = "markup is None"
            return False
        if self.anki is None:
            self.add = "anki is None"
            return False
        profile = self.preferences['profiles'].get(completeProfile)
        if profile is None:
            self.add = "profile is None"
            return False
        fields = reader_util.formatFields(profile['fields'], markup)
        if not self.anki.canAddNote(profile['deck'], profile['model'], fields):
            return self.ankiOverwriteFact(completeProfile, markup)
        self.fields = (fields,profile['fields'],markup)
        tagsSplit = reader_util.splitTags(self.comboTags.currentText())
        tagsJoined = ' '.join(tagsSplit)

        tagIndex = self.comboTags.findText(tagsJoined)
        if tagIndex > 0:
            self.comboTags.removeItem(tagIndex)
        if tagIndex != 0:
            self.comboTags.insertItem(0, tagsJoined)
        self.preferences.updateFactTags(tagsJoined)
        self.addNote = [profile['deck'],profile['model'],fields,tagsSplit]
        self.factId = self.anki.addNote(profile['deck'], profile['model'], fields, tagsSplit)
        if self.factId is None:
            self.add = "factId is None"
            return False

        key = self.anki.getModelKey(profile['model'])
        value = fields[key]
        # Put the vocabulary out of 'new' state and add it to the vocabulary list
        self.cardByNote = (profile['model'],key,value)
        ids = self.anki.getCardsByNote(profile['model'],key,value)
        if len(ids) == 0:
            self.add = "len(ids == 0)"
            return False
        card = self.anki.collection().getCard(ids[0])
        self.currentFile.profiles[completeProfile]['freshlyAdded'].append(value)
        self.currentFile.addVocabulary(completeProfile,value,card,addToBadListToo = False)
        self.currentFile.addMarkup(completeProfile,value,markup)
        if value in self.currentFile.profiles[completeProfile]['wordsNotFound']:
            self.currentFile.profiles[completeProfile]['wordsNotFound'].remove(value)
        if self.preferences['unlockVocab']:
            self.anki.collection().sched.earlyAnswerCard(card,2)
        if addToList:
            self.facts.append({'word':value,'profile':completeProfile})
            self.listDefinitions.addItem(value)
            self.listDefinitions.setCurrentRow(self.listDefinitions.count() - 1)
        self.setStatus(u'Added fact {0}; {1} new fact(s) total'.format(value, len(self.facts)))

        for profile in self.profiles.values():
            profile.updateDefinitions(scroll=True)
        return True

    def executeDefCommand(self, command, index):
        commands = command.split("_")
        profile = commands.pop(0)
        self.profiles[profile].runCommand(commands,index)

    def updateSampleMouseEvent(self, event):
        cursor = self.textContent.cursorForPosition(event.pos())
        self.state.scanPosition = cursor.position()
        if event.buttons() & QtCore.Qt.MidButton or event.modifiers() &\
        QtCore.Qt.ShiftModifier or event.buttons() & QtCore.Qt.XButton1:
            self.updateSampleFromPosition()

    def createAlias(self):
        self.updateSampleFromPosition()
        text, ok = QtWidgets.QInputDialog.getText(self, 'Create Alias', 'Replace with:')
        if ok:
            content = self.textContent.toPlainText()
            self.currentFile.alias[text] = content[self.samplePosStart:self.samplePosEnd]
            content = content[:self.samplePosStart] + text + content[self.samplePosEnd:]
            self.samplePosEnd = self.samplePosStart + len(text)
            self.textContent.setPlainText(content)
            cursor = self.textContent.textCursor()
            cursor.setPosition(self.samplePosStart, QtGui.QTextCursor.MoveAnchor)
            cursor.setPosition(self.samplePosEnd, QtGui.QTextCursor.KeepAnchor)
            self.textContent.setTextCursor(cursor)

    def createSubscription(self):
        source, ok = QtWidgets.QInputDialog.getText(self, 'Create Subscription', 'Source: ')
        if ok:
            target, ok = QtWidgets.QInputDialog.getText(self, 'Create Subscription', 'Target: ')
        if ok:
            target = os.path.join(os.path.join(aqt.mw.col.media.dir(),'Yomichan'),target)
            if not os.path.exists(os.path.dirname(target)):
                os.makedirs(os.path.dirname(target))
            self.preferences['subscriptions'].append({'source':source,'target':target})
            self.plugin.loadSubscriptions()

    def getSample(self):
        return self.textContent.toPlainText()[self.samplePosStart:self.samplePosEnd]

    def updateSampleFromPosition(self):
        self.samplePosEnd = self.state.scanPosition + self.preferences['scanLength']
        d = dict()
        cursor = self.textContent.textCursor()
        d['content'] = self.textContent.toPlainText()
        d['samplePosStart'] = self.state.scanPosition
        if d['samplePosStart'] >= len(d['content']):
            return
        if self.samplePosEnd > len(d['content']):
            self.samplePosEnd = len(d['content'])
        d['contentSample'] = d['content'][d['samplePosStart']:self.samplePosEnd]
        alias = None
        if self.currentFile is not None:
            for eng,jpn in self.currentFile.alias.items():
                if d['content'][d['samplePosStart']:].startswith(eng):
                    d['contentSample'] = jpn
                    d['contentSampleFlat'] = jpn
                    alias = eng
                    break
        if alias is None:
            d['contentSample'] = d['content'][d['samplePosStart']:self.samplePosEnd]
            d['contentSampleFlat'] = d['contentSample'].replace('\n', '')

        if len(d['content']) == 0:
            return

        lengthMatched = 0

        for profile in self.getSortedProfiles():
            lengthMatched = profile.onLookup(d,lengthMatched)

        lengthSelect = 0
        for c in d['contentSample'] or lengthSelect > lengthMatched:
            if lengthMatched <= 0:
                break
            lengthSelect += 1
            if c != u'\n':
                lengthMatched -= 1
        if alias is not None:
            lengthSelect = len(d['contentSample'])
        self.samplePosStart = d['samplePosStart']
        self.samplePosEnd = self.samplePosStart + lengthSelect
        cursor.setPosition(self.samplePosStart, QtGui.QTextCursor.MoveAnchor)
        cursor.setPosition(self.samplePosStart + lengthSelect, QtGui.QTextCursor.KeepAnchor)
        self.textContent.setTextCursor(cursor)

    def clearRecentFiles(self):
        self.preferences.clearRecentFiles()
        self.updateRecentFiles()

    def updateRecentFiles(self):
        self.menuOpenRecent.clear()

        filenames = self.preferences.recentFiles()
        if len(filenames) == 0:
            return

        for filename in filenames:
            menu_open_action = self.menuOpenRecent.addAction(filename) # , lambda f=filename: self.openFile(f))
            menu_open_action.triggered.connect(lambda _: self.openFile(filename))

        self.menuOpenRecent.addSeparator()
        self.menuOpenRecent.addAction('Clear file history', self.clearRecentFiles)

    def updateRecentFile(self, addIfNeeded=True):
        if self.state.filename:
            if addIfNeeded or self.state.filename in self.preferences.recentFiles():
                self.preferences.updateRecentFile(self.state.filename, self.state.scanPosition)

    def updateVocabDefs(self, prfl, **options):
        self.currentFile.profiles[prfl]['longestMatch'] = None
        self.currentFile.profiles[prfl]['longestMatchKey'] = u''
        self.profiles[prfl].updateDefinitions()
        longestMatch = self.currentFile.profiles[prfl]['longestMatch']
        longestMatchKey = self.currentFile.profiles[prfl]['longestMatchKey']
        if self.currentFile is not None:
            # User had to look up the word, put it into the wrong list
            if longestMatchKey in self.currentFile.profiles[prfl]['wordsAll']:
                if longestMatchKey in self.currentFile.profiles[prfl]['freshlyAdded']:
                    self.currentFile.profiles[prfl]['freshlyAdded'].remove(longestMatchKey)
                else:
                    self.recentIncorrect = (prfl,longestMatchKey)
                    self.currentFile.profiles[prfl]['wordsBad'][longestMatchKey] = self.currentFile.profiles[prfl]['wordsAll'][longestMatchKey]
                    self.setStatus(u'{0} has been put into the incorrectly answered set'.format(longestMatchKey))

    def restoreRecentIncorrect(self):
        if self.recentIncorrect:
            prfl, word = self.recentIncorrect
            if word in self.currentFile.profiles[prfl]['wordsBad']:
                del self.currentFile.profiles[prfl]['wordsBad'][word]
                self.setStatus(u'{0} was taken out of the wrongly answered vocabulary list.'.format(word))
                self.recentIncorrect = None

    def showNotFoundWords(self):
        QtWidgets.QMessageBox.critical(self, 'Yomichan', u'\n'.join(self.currentFile.profiles['vocabulary']['wordsNotFound']))
        if len(self.currentFile.profiles['vocabulary']['wordsNotFound']) > 0:
            txt = self.currentFile.profiles['vocabulary']['wordsNotFound'][0]
            lbracket = txt.find(u'[')
            if lbracket > 0:
                txt = txt[:lbracket]
            self.findText(txt)

    def importWordList(self, words):
        for profile in self.profiles.values():
            profile.definitions = []

        for word in words:
            for key, language in self.languages.items():
                if self.preferences[key]:
                    self.profiles["vocabulary"].definitions += language.dictionary.findTerm(word)

            self.profiles["kanji"].definitions += self.languages['japanese'].findCharacters(word)

        for profile in self.profiles.values():
            profile.updateDefinitions()

    def setStatus(self, status):
        self.statusBar.showMessage(status)


from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
import numpy as np

class CustomLogisticRegression:

    def __init__(self, fit_intercept=True, l_rate=0.01, n_epoch=100):
        self.fit_intercept = fit_intercept
        self.l_rate = l_rate
        self.n_epoch = n_epoch
        bc_dataset = load_breast_cancer(as_frame=True)["frame"]
        X = bc_dataset.loc[:, ['worst concave points', 'worst perimeter', 'worst radius']]
        scaler = StandardScaler()
        X = scaler.fit_transform(X)
        y = bc_dataset["target"]
        self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, train_size=0.8, random_state=43)
        self.coef_ = None
        self.mse_error = None
        self.log_error = None

    def sigmoid(self, t):
        return 1 / (1 + np.e ** (-t))

    def fit_mse(self, X_train, y_train):
        if self.fit_intercept:
            self.coef_ = np.zeros(X_train.shape[1] + 1)
        else:
            self.coef_ = np.zeros(X_train.shape[1])

        self.mse_error = []
        for _ in range(self.n_epoch):
            mse_error_step = []
            for i, row in enumerate(X_train):
                y_hat = self.predict_proba(row, self.coef_)
                mse_error_iter = ((y_hat - y_train.iloc[i]) ** 2) / len(y_train.values)
                mse_error_step.append(mse_error_iter)
                if self.fit_intercept:
                    self.coef_[0] -= self.l_rate * (y_hat - y_train.iloc[i]) * y_hat * (1 - y_hat)
                    for b in range(1, len(self.coef_)):
                        self.coef_[b] -= self.l_rate * (y_hat - y_train.iloc[i]) * y_hat * (1 - y_hat) * row[b - 1]
                else:
                    for b in range(len(self.coef_)):
                        self.coef_[b] -= self.l_rate * (y_hat - y_train.iloc[i]) * y_hat * (1 - y_hat) * row[b]
            self.mse_error.append(mse_error_step)

    def fit_log_loss(self, X_train, y_train):
        if self.fit_intercept:
            self.coef_ = np.zeros(X_train.shape[1] + 1)
        else:
            self.coef_ = np.zeros(X_train.shape[1])

        self.log_error = []
        for _ in range(self.n_epoch):
            log_error_step = []
            for i, row in enumerate(X_train):
                y_hat = self.predict_proba(row, self.coef_)
                log_error_iter = -(y_train.iloc[i] * np.log(y_hat)
                                   + (1 - y_train.iloc[i]) * np.log(1 - y_hat)) / len(y_train.values)
                log_error_step.append(log_error_iter)
                if self.fit_intercept:
                    self.coef_[0] -= (self.l_rate * (y_hat - y_train.iloc[i])) / len(X_train)
                    for b in range(1, len(self.coef_)):
                        self.coef_[b] -= (self.l_rate * (y_hat - y_train.iloc[i]) * row[b - 1]) / len(X_train)
                else:
                    for b in range(len(self.coef_)):
                        self.coef_[b] -= (self.l_rate * (y_hat - y_train.iloc[i]) * row[b]) / len(X_train)
            self.log_error.append(log_error_step)

    def predict_proba(self, row, coef_):
        if self.fit_intercept:
            t = np.dot(row, coef_[1:]) + coef_[0]
        else:
            t = np.dot(row, coef_)
        return self.sigmoid(t)

    def predict(self, X_test, cut_off=0.5):
        y_pred = np.ones(len(X_test))
        for i, row in enumerate(X_test):
            y_hat = self.predict_proba(row, self.coef_)
            if y_hat < cut_off:
                y_pred[i] = 0.0
        return y_pred

lr = CustomLogisticRegression(fit_intercept=True, l_rate=0.01, n_epoch=1000)

lr.fit_mse(lr.X_train, lr.y_train)
y_predicted_mse = lr.predict(lr.X_test, cut_off=0.5)
acs_mse = accuracy_score(lr.y_test, y_predicted_mse)

lr.fit_log_loss(lr.X_train, lr.y_train)
y_predicted_log = lr.predict(lr.X_test, cut_off=0.5)
acs_log = accuracy_score(lr.y_test, y_predicted_log)

model = LogisticRegression()
model.fit(lr.X_train, lr.y_train)
y_predicted = model.predict(lr.X_test)
acs = accuracy_score(lr.y_test, y_predicted)

dictionary = dict()
dictionary['mse_accuracy'] = acs_mse
dictionary['logloss_accuracy'] = acs_log
dictionary['sklearn_accuracy'] = acs
dictionary['mse_error_first'] = lr.mse_error[0]
dictionary['mse_error_last'] = lr.mse_error[-1]
dictionary['logloss_error_first'] = lr.log_error[0]
dictionary['logloss_error_last'] = lr.log_error[-1]
print(dictionary)

print(f'{format(min(lr.mse_error[0]), ".5f")}')
print(f'{format(min(lr.mse_error[-1]), ".5f")}')
print(f'{max(lr.log_error[0]).round(5)}')
print(f'{max(lr.log_error[-1]).round(5)}')

Logistic Regression from Scratch/logistic.py


import torch
from torch.autograd import Variable
from torch import nn

import mnist_utils

import cifar_data_utils

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# code adapted from https://github.com/kuc2477/pytorch-vae/blob/master/model.py

class CIFARConditionalVAE(nn.Module):
    def __init__(self, slen, channel_num, kernel_num, z_size, n_classes, use_cifar100 = True):
        # configurations
        super().__init__()
        self.slen = slen # side length of image
        self.channel_num = channel_num # number of channels in an image
        self.kernel_num = kernel_num # "channel number" in the encoded image
        self.z_size = z_size # size of latent dimension

        self.n_classes = n_classes

        self.use_cifar100 = use_cifar100

        # encoder
        self.encoder = nn.Sequential(
            self._conv(channel_num, kernel_num // 4),
            self._conv(kernel_num // 4, kernel_num // 2),
            self._conv(kernel_num // 2, kernel_num),
        )

        # encoded feature's size and volume
        self.feature_size = slen // 8
        self.feature_volume = kernel_num * (self.feature_size ** 2)

        # encoder then encorporates the class label
        self.cond_encoder = nn.Sequential(
            self._linear(self.feature_volume + self.n_classes, 128),
            self._linear(128, 128),
            self._linear(128, z_size * 2, relu = False),
        )

        # decodr that encorporates class label
        self.cond_decoder = nn.Sequential(
            self._linear(z_size + self.n_classes, 128),
            self._linear(128, 128),
            self._linear(128, self.feature_volume),
        )

        # self.q_mean = self._linear(self.feature_volume, z_size, relu=False)
        # self.q_logvar = self._linear(self.feature_volume, z_size, relu=False)

        # projection
        # self.project = self._linear(z_size, self.feature_volume, relu=False)

        # decoder
        self.decoder = nn.Sequential(
            self._deconv(kernel_num, kernel_num // 2),
            self._deconv(kernel_num // 2, kernel_num // 4),
            self._deconv(kernel_num // 4, channel_num, relu = True),
            nn.Sigmoid()
        )

    def cond_encoder_forward(self, encoded, label):
        # this is my added function, where we take the convolved image and
        # incorporate the label.

        assert encoded.shape[0] == len(label)

        # append one hot encoding of labels
        one_hot_labels = mnist_utils.get_one_hot_encoding_from_int(label, self.n_classes)
        encoded = encoded.view(-1, self.feature_volume)

        h = torch.cat((encoded, one_hot_labels), dim = 1)

        h = self.cond_encoder(h)

        indx1 = self.z_size
        indx2 = 2 * self.z_size

        latent_means = h[:, 0:indx1]
        latent_log_var = h[:, indx1:indx2]

        return latent_means, latent_log_var, one_hot_labels

    def cond_decoder_forward(self, latent_samples, one_hot_labels):
        assert latent_samples.shape[0] == one_hot_labels.shape[0]
        assert latent_samples.shape[1] == self.z_size
        assert one_hot_labels.shape[1] == self.n_classes

        h = torch.cat((latent_samples, one_hot_labels), dim = 1)

        return self.cond_decoder(h).view(-1, self.kernel_num,
                                                self.feature_size,
                                                self.feature_size)

    def forward(self, x, label):

        n_obs = x.shape[0]

        # encode x
        # this is (N x kernel_num x (slen / 8) x (slen / 8))
        if self.use_cifar100:
            x = x * cifar_data_utils.CIFAR100_STD_TENSOR.to(device) + \
                        cifar_data_utils.CIFAR100_MEAN_TENSOR.to(device)

        else:
            x = x * cifar_data_utils.CIFAR10_STD_TENSOR.to(device) + \
                        cifar_data_utils.CIFAR10_MEAN_TENSOR.to(device)

        encoded = self.encoder(x)

        assert encoded.shape[0] == n_obs

        # combine encoded with the label,
        # and pass through some fully connected layers
        # get latent means and variances
        # means and variances are of shape (N x z_size)
        latent_means, latent_log_var, one_hot_labels = \
            self.cond_encoder_forward(encoded, label)
        latent_std = torch.exp(0.5 * latent_log_var)

        # sample
        latent_samples = self.z(latent_means, latent_log_var)

        assert latent_means.shape[0] == n_obs
        assert latent_std.shape[0] == n_obs
        assert latent_samples.shape[0] == n_obs

        # combine latent sample with labels
        # pass through a few fully connected layers
        latent_samples_label = \
            self.cond_decoder_forward(latent_samples, one_hot_labels)

        assert latent_samples_label.shape[0] == n_obs

        # deconvolve and reconstruct x
        image_mean = self.decoder(latent_samples_label)
        image_var = None

        # return the parameters of distribution of q given x and the
        # reconstructed image.
        return latent_means, latent_std, latent_samples, image_mean, image_var

    # ==============
    # VAE components
    # ==============

    # def q(self, encoded):
    #     unrolled = encoded.view(-1, self.feature_volume)
    #     return self.q_mean(unrolled), self.q_logvar(unrolled)

    def z(self, mean, logvar):
        std = logvar.mul(0.5).exp_()
        eps = (
            Variable(torch.randn(std.size())).to(device)
        )
        return eps.mul(std).add_(mean)

    # def reconstruction_loss(self, x_reconstructed, x):
    #     return nn.BCELoss(size_average=False)(x_reconstructed, x) / x.size(0)
    #
    # def kl_divergence_loss(self, mean, logvar):
    #     return ((mean**2 + logvar.exp() - 1 - logvar) / 2).mean()

    # =====
    # Utils
    # =====

    # @property
    # def name(self):
    #     return (
    #         'VAE'
    #         '-{kernel_num}k'
    #         '-{label}'
    #         '-{channel_num}x{slen}x{slen}'
    #     ).format(
    #         label=self.label,
    #         kernel_num=self.kernel_num,
    #         slen=self.slen,
    #         channel_num=self.channel_num,
    #     )

    # def sample(self, size):
    #     z = Variable(
    #         torch.randn(size, self.z_size).to(device)
    #     )
    #     z_projected = self.project(z).view(
    #         -1, self.kernel_num,
    #         self.feature_size,
    #         self.feature_size,
    #     )
    #     return self.decoder(z_projected).data

    # def _is_on_cuda(self):
    #     return next(self.parameters()).is_cuda

    # ======
    # Layers
    # ======

    def _conv(self, channel_size, kernel_num):
        return nn.Sequential(
            nn.Conv2d(
                channel_size, kernel_num,
                kernel_size=4, stride=2, padding=1,
            ),
            nn.BatchNorm2d(kernel_num),
            nn.ReLU(),
        )

    def _deconv(self, channel_num, kernel_num, relu = True):

        if relu:
            return nn.Sequential(
                nn.ConvTranspose2d(
                    channel_num, kernel_num,
                    kernel_size=4, stride=2, padding=1,
                ),
                nn.BatchNorm2d(kernel_num),
                nn.ReLU(),
            )
        else:
            return nn.Sequential(
                nn.ConvTranspose2d(
                    channel_num, kernel_num,
                    kernel_size=4, stride=2, padding=1,
                ),
                nn.BatchNorm2d(kernel_num),
            )

    def _linear(self, in_size, out_size, relu=True):
        return nn.Sequential(
            nn.Linear(in_size, out_size),
            nn.ReLU(),
        ) if relu else nn.Linear(in_size, out_size)