import json
import sys

from colorcet import bmw
import dask
import pandas as pd
import datashader as ds
from datashader import transfer_functions as tf
from datashader.utils import lnglat_to_meters as webm
from datashader_fix.tiles import render_tiles # use version of render tiles with this fix: https://github.com/holoviz/datashader/pull/874

# The threads scheduler is more efficient than the multiprocessor one (which is the default for dask.bag)
# See https://docs.dask.org/en/latest/setup/single-machine.html
dask.config.set(scheduler='threads')

df = pd.read_csv('./data/inaturalist.csv')

def get_extents(df, x, y):
    return df[x].min(), df[y].min(), df[x].max(), df[y].max()

def load_data_func(x_range, y_range):
    return df.loc[df['x'].between(*x_range) & df['y'].between(*y_range)]

def rasterize_func(df, x_range, y_range, height, width):
    cvs = ds.Canvas(x_range=x_range, y_range=y_range,
                    plot_height=height, plot_width=width)
    agg = cvs.points(df, 'x', 'y')
    return agg

def shader_func(agg, span=None):
    img = tf.shade(agg, cmap=bmw, how='log', span=span)
    return img

def post_render_func(img, **kwargs):
    return img

if __name__ == '__main__':
    output_path = 'tiles'
    full_extent_of_data = get_extents(df, 'x', 'y')
    results = render_tiles(full_extent_of_data,
                        range(1, 7),
                        load_data_func=load_data_func,
                        rasterize_func=rasterize_func,
                        shader_func=shader_func,
                        post_render_func=post_render_func,
                        output_path=output_path)


from easygui import *
import os

way = fileopenbox('请查找显示的文本',default='*.txt')
print(way)
with open(way,encoding='utf-8') as file_name:
        txt = file_name.read()
        title = os.path.basename(way)
        msg = ('文件【%s】的内容如下：' % title)
        textbox(msg,title,txt)


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

"""
.. _log

Basic stdout log classes: Error, Verbose and Warning.

**Content**
"""

# *credits*:      `gjacopo <jacopo.grazzini@ec.europa.eu>`_
# *since*:        Fri May  8 15:21:31 2020

#%% Settings

import os, sys, warnings
import inspect
import functools
import six

DEFVERBOSE          = False # True

REDUCE_ANSWER       = False # ! used for testing purpose: do not change !
EXCLUSIVE_ARGUMENTS = False # ! used for settings: do not change !

#%% Core functions/classes

#==============================================================================
# Class Warning
#==============================================================================

class Warnings(Warning):
    """Dummy class for warnings in this package.

        >>> Warnings(warnmsg, expr=None)

    Arguments
    ---------
    warnmsg : str
        warning message to display.

    Keyword arguments
    -----------------
    expr : str
        input expression in which the warning occurs; default: :data:`expr` is
        :data:`None`.

    Example
    -------

        >>> Warnings('This is a very interesting warning');
            Warnings: ! This is a very interesting warning !
    """
    def __init__(self, msg='', **kwargs):
        self.msg = msg
        expr = kwargs.pop('expr',None)
        if expr is not None:    self.expr = expr
        else:                   self.expr = ''
        # warnings.warn(self.msg)
        print(self)
    def __repr__(self):             return self.msg
    def __str__(self):
        #return repr(self.msg)
        return (
                "! %s%s%s !" %
                (self.msg,
                 ' ' if self.msg and self.expr else '',
                 self.expr
                 )
            )

#==============================================================================
# Class Verbose
#==============================================================================

class Verbose(object):
    """Dummy class for verbose printing mode in this package.

        >>> Verbose(msg, verb=True, expr=None)

    Arguments
    ---------
    msg : str
        verbose message to display.

    Keyword arguments
    -----------------
    verb : bool
        flag set to :data:`True` when the string :literal:`[verbose] -` is added
        in front of each verbose message displayed.
    expr : str
        input expression in which the verbose mode is called; default: :data:`expr` is
        :data:`None`.

    Example
    -------

        >>> Verbose('The more we talk, we less we do...', verb=True);
            [verbose] - The more we talk, we less we do...
    """
    def __init__(self, msg='', **kwargs):
        expr = kwargs.pop('expr','')
        verb = kwargs.pop('verb', DEFVERBOSE)
        self.msg = msg
        if verb is True:
            print('\n! [verbose] - %s !' % self.msg)
        if expr is not None:    self.expr = expr
    #def __repr__(self):
    #    return self.msg
    def __str__(self):
        return repr(self.msg)

#==============================================================================
# Class Error
#==============================================================================

class Error(Exception):
    """Dummy class for exception raising in this package.

        >>> raise Error(msg, type=None, code=None, expr='')

    Arguments
    ---------
    errmsg : str
        message -- explanation of the error.

    Keyword arguments
    -----------------
    type : object
        error type; when :data:`errtype` is left to :data:`None`, the system tries
        to retrieve automatically the error type using :data:`sys.exc_info()`.
    code : (float,int)
        error code; default: :data:`errcode` is :data:`None`.
    expr : str
        input expression in which the error occurred; default: :data:`expr` is
        :data:`None`.

    Example
    -------

        >>> try:
                assert False
            except:
                raise Error('It is False')
            Traceback ...
            ...
            Error: !!! AssertionError: It is False !!!
    """

    def __init__(self, msg='',  **kwargs):
        self.msg = msg
        typ = kwargs.pop('type',None)
        code = kwargs.pop('code',None)
        expr = kwargs.pop('expr','')
        if expr is not None:                self.expr = expr
        else:                               self.expr = ''
        if typ is None:
            try:            typ = sys.exc_info()[0]
            except:         pass
        if inspect.isclass(typ):            self.type = typ.__name__
        elif isinstance(typ, (int,float)):  self.type = str(typ)
        else:                               self.type = typ
        if code is not None:                self.code = str(code)
        else:                               self.code = ''
        # super(Error,self).__init__(self, msg)

    def __str__(self):
        # return repr(self.msg)
        str_ = ("%s%s%s%s%s%s%s" %
                (self.type or '',
                 ' ' if self.type and self.code else '',
                 self.code or '',
                 ': ' if (self.type or self.code) and (self.msg or self.expr) else '',
                 self.msg or '',
                 ' ' if self.msg and self.expr else '',
                 self.expr or '' #[' ' + self.expr if self.expr else '']
                 )
                )
        return ( "%s%s%s" %
                ('' if str_.startswith('!!!') else '!!! ',
                 str_,
                 '' if str_.endswith('!!!') else ' !!!'
                 )
                )

#==============================================================================
# Method deprecated
#==============================================================================

def deprecated(reason, run=True):
    """This is a decorator which can be used to mark functions as deprecated.

        >>> new = deprecated(reason)

    Arguments
    ---------
    reason : str
        optional string explaining the deprecation.

    Keywords arguments
    ------------------
    run : bool
        set to run the function/method/... despite being deprecated; default:
        :data:`False` and the decorated method/function/... is not run.

    Examples
    --------
    The deprecated function can be used to decorate different objects:

        >>> @deprecated("use another function")
        ... def old_function(x, y):
        ...     return x + y
        >>> old_function(1, 2)
            __main__:1: DeprecationWarning: Call to deprecated function old_function (use another function).
            3
        >>> class SomeClass(object):
        ... @deprecated("use another method", run=False)
        ... def old_method(self, x, y):
        ...     return x + y
        >>> SomeClass().old_method(1, 2)
            __main__:1: DeprecationWarning: Call to deprecated function old_method (use another method).
        >>> @deprecated("use another class")
        ... class OldClass(object):
        ...     pass
        >>> OldClass()
            __main__:1: DeprecationWarning: Call to deprecated class OldClass (use another class).
            <__main__.OldClass at 0x311e410f0>

    Note
    ----
    It will result in a warning being emitted when the function is used and when
    a :data:`reason` is passed.
    """
    # see https://stackoverflow.com/questions/2536307/decorators-in-the-python-standard-lib-deprecated-specifically
    if isinstance(reason, six.string_types): # happyType.isstring(reason):
        def decorator(func1):
            if inspect.isclass(func1):
                fmt1 = "Call to deprecated class {name} ({reason})."
            else:
                fmt1 = "Call to deprecated function {name} ({reason})."
            @functools.wraps(func1)
            def new_func1(*args, **kwargs):
                warnings.simplefilter('always', DeprecationWarning)
                warnings.warn(
                    fmt1.format(name=func1.__name__, reason=reason),
                    category=DeprecationWarning,
                    stacklevel=2
                )
                warnings.simplefilter('default', DeprecationWarning)
                if run is True:
                    return func1(*args, **kwargs)
            return new_func1
        return decorator
    elif inspect.isclass(reason) or inspect.isfunction(reason):
        func2 = reason
        if inspect.isclass(func2):
            fmt2 = "Call to deprecated class {name}."
        else:
            fmt2 = "Call to deprecated function {name}."
        @functools.wraps(func2)
        def new_func2(*args, **kwargs):
            warnings.simplefilter('always', DeprecationWarning)
            warnings.warn(
                fmt2.format(name=func2.__name__),
                category=DeprecationWarning,
                stacklevel=2
            )
            warnings.simplefilter('default', DeprecationWarning)
            if run is True:
                return func2(*args, **kwargs)
        return new_func2
    else:
        raise Error('wrong type for input reason - %s not supported' % repr(type(reason)))


from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
    os.path.join(basedir, 'app.sqlite')
db = SQLAlchemy(app)
ma = Marshmallow(app)

class Professor(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=False)
    school = db.Column(db.String(144), unique=False)

    def __init__(self, name, school):
        self.name = name
        self.school = school

class ProfessorSchema(ma.Schema):
    class Meta:
        fields = ('name', 'school')

professor_schema = ProfessorSchema()
professors_schema = ProfessorSchema(many=True)

# Endpoint to create a new professor

@app.route('/professor', methods=["POST"])
def add_professor():
    name = request.json['name']
    school = request.json['school']

    new_professor = Professor(name, school)

    db.session.add(new_professor)
    db.session.commit()

    professor = Professor.query.get(new_professor.id)

    return professor_schema.jsonify(professor)

# Endpoint to query all professors
@app.route("/professors", methods=["GET"])
def get_professors():
    all_professors = Professor.query.all()
    result = professors_schema.dump(all_professors)
    return jsonify(result.data)

# Endpoint to query a single professors
@app.route("/professor/<id>", methods=["GET"])
def get_professor(id):
    professor = Professor.query.get(id)
    return professor_schema.jsonify(professor)

if __name__ == '__main__':
    app.run(debug=True)


from fastapi import FastAPI
import random
from typing import Optional, List
from models import User, Gender, Role
from uuid import uuid4

app = FastAPI()

db: List[User] = [
    User(
        id=uuid4(),
        first_name="Daniel",
        last_name="Villery",
        gender=Gender.male,
        roles=[Role.user, Role.admin],
    )
]

@app.get("/")
async def root():
    return {"example": "this is an example", "data": 0}

@app.get("/users")
async def get_users():
    return db

@app.post("/users/new")
async def register_user(user: User):
    db.append(user)
    return {"id": user.id}

@app.get("/random")
def get_random():
    rn: int = random.randint(0, 100)
    return {"number": rn, "limit": 100}

@app.get("/random/{limit}")
async def get_random(limit: int):
    rn: int = random.randint(0, limit)
    return {"number": rn, "limit": limit}

@app.get("/beats")
def get_beats():
    name: str = "name"
    artist: str = "artist"
    url: Optional[str] = None
    return {"name": name, "artist": artist, "url": url}


from string import ascii_lowercase

def checkio(words: str) -> bool:
    word_split = words.split(' ')
    word_count = 0
    for word in word_split:
        if word[0].lower() in ascii_lowercase:
            word_count += 1
            if word_count == 3:
                return True
        else:
            word_count = 0
    return False

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert checkio("Hello World hello") == True, "Hello"
    assert checkio("He is 123 man") == False, "123 man"
    assert checkio("1 2 3 4") == False, "Digits"
    assert checkio("bla bla bla bla") == True, "Bla Bla"
    assert checkio("Hi") == False, "Hi"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")


from models import Course
from models.tests import BaseTestCase

class CourseTest(BaseTestCase):
    def test_creation(self):
        self.assertIsNotNone(
            Course.create(self.teacher, '', ''),
            'Empty course name'
        )
        self.assertIsNone(
            Course.create(self.teacher, 'pub', ''),
            'Create public course'
        )
        self.assertIsNotNone(
            Course.create(self.teacher, 'Pub', ''),
            'Recreate public course (bad)'
        )
        self.assertIsNone(
            Course.create(self.teacher, 'Pub2', None),
            'Create public course'
        )
        self.assertIsNone(
            Course.create(self.teacher, 'pvt', 'on'),
            'Create private course'
        )
        for name, private in [('Pvt', True), ('Pub', False), ('Pub2', False)]:
            course = Course.all().filter('name =', name).get()
            self.assertIsNotNone(course)
            if private:
                self.assertIsNotNone(course.code)
            else:
                self.assertIsNone(course.code)
            self.assertEqual(course.teacher.email, self.teacher.email)

    def test_joining(self):
        self.assertIsNotNone(
            self.teacher.join_course(self.public.key().id()),
            'Trying to join their own course'
        )

        self.assertEqual(len(self.teacher2.courses()), 0)
        start = len(self.public.students())
        self.assertIsNone(
            self.teacher2.join_course(self.public.key().id()),
            'Other teacher joins course'
        )
        self.assertEqual(len(self.public.students()), start + 1)
        self.assertEqual(len(self.teacher2.courses()), 1)

        self.assertIsNone(
            self.student.join_course(self.public.key().id()),
            'Student joins course'
        )
        self.assertIsNotNone(
            self.student.join_course(self.private.key().id()),
            'Trying to join a private course'
        )
        self.assertIsNone(
            self.student.join_course(self.private.key().id(), self.private.code),
            'Trying to join a private course with the correct code'
        )
        self.assertIsNotNone(
            self.student.join_course(self.private.key().id(), self.private.code),
            'Trying to rejoin a private course with the correct code'
        )

    def test_listing(self):
        self.assertEqual(len(list(self.teacher.courses_taught())), 2)
        self.assertIsNone(self.teacher2.courses_taught().get())
        self.assertIsNone(self.student.courses_taught().get())


import pybg.ql
import pybg.curves as curves

import pybg.instruments.bulletbond as bb
import pybg.instruments.sinkingfundbond as sf

from pybg.enums import (
    DayCounters, Frequencies, BusinessDayConventions, Calendars
    )

from datetime import date

dt0 = date(2008, 9, 15)

print("\nSetting eval date: %s" % dt0)
pybg.ql.set_eval_date(dt0)

govbondcurve = curves.CurveBase(
Calendars.UnitedStates(Calendars.GOVERNMENTBOND),
3,
DayCounters.Actual360(),
Frequencies.Semiannual,
BusinessDayConventions.Unadjusted,
DayCounters.ActualActual(DayCounters.Bond),
DayCounters.ActualActual(DayCounters.ISDA)
)

bcrv = curves.BondCurve(govbondcurve)

dated = [date(2005, 3, 15),
        date(2005, 6, 15),
        date(2006, 6, 30),
        date(2002, 11, 15),
        date(1987, 5, 15)
    ]

maturities = [
        date(2010, 8, 31),
        date(2011, 8, 31),
        date(2013, 8, 31),
        date(2018, 8, 15),
        date(2038, 5, 15)
    ]

couponRates = [
        0.02375,
        0.04625,
        0.03125,
        0.04000,
        0.04500
    ]

marketQuotes = [
        100.390625,
        106.21875,
        100.59375,
        101.6875,
        102.140625
    ]

bond_ids = [
        "B1", "B2", "B3", "B4", "B5"
    ]

depos = {
        "3M": 0.0096,
        "6M": 0.0145,
        "1Y": 0.0194}

q = zip(marketQuotes, maturities, couponRates, dated)
bndcrv = dict(zip(bond_ids, q))

print("build bond curve...")
bcrv.update(bndcrv, depos)

output_line1 = "bond: {}, price/yield: {:7.3f}/{:6.3f}%"
output_line2 = "      check {:7.3f} vs {:7.3f}"
for id, bndrow in bndcrv.items():
    qt, mty, cpn, dtd = bndrow
    b = bb.BulletBond(cpn, mty, dtd, Calendars.UnitedStates(Calendars.GOVERNMENTBOND))
    print(output_line1.format(id, qt, 100.0*b.toYield(qt)))
    b.setEngine(bcrv)
    print(output_line2.format(qt, b.toPrice()))

#bulletbond
print("test bond")
dated = date(2003, 5, 15)
mty = date(2027, 5, 15)

bnd1 = bb.BulletBond(.06, mty, dated, Calendars.UnitedStates(Calendars.GOVERNMENTBOND))

#sinker
print("sinking fund bond")
sfbnd = sf.SinkingFundBond(.06,
                            mty,
                            (40., 40., 40.),
                            Frequencies.Annual,
                            dated)


def read_last(lines, file):
    if lines > 0:
        with open(file) as text:
            file_lines = text.readlines()[-lines:]
        for line in file_lines:
            print(line.strip())
        else:
            print('только положительное число')

read_last(2, 'spisok.txt')
read_last(-1, 'spisok.txt')


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

# tabla-del-n.py
n = input("Tabla del n: ")  # regresa un str
n = int(n)  # str -> int

for i in range(1,11):
    r = n * i
    print("{} x {} = {}".format(n, i, r))


import re
import itertools
import collections

reg = re.compile("(\\w+) would (\\w+) (\\d+) happiness units by sitting next to (\\w+).")
people = collections.defaultdict(dict)
arrangement_happiness = []

def parse_data(line):
	match = re.match(reg, line)
	if match:
		attendee = match.group(1)
		action = match.group(2)
		seated_next = match.group(4)

		happiness = ""
		if action == "gain":
			happiness = int(match.group(3))
		else:
			happiness = -int(match.group(3))

		people[attendee][seated_next] = happiness

def get_arrangements():
	all_arrangements = itertools.permutations(people.keys())

	for arrangement in all_arrangements:
		total_happiness = 0
		for i in range(len(arrangement) - 1):
			total_happiness += people[arrangement[i]][arrangement[i + 1]]
			total_happiness += people[arrangement[i + 1]][arrangement[i]]

		total_happiness += people[arrangement[-1]][arrangement[0]]
		total_happiness += people[arrangement[0]][arrangement[-1]]
		arrangement_happiness.append(total_happiness)

def main():
	with open("input.txt") as f:
		contents = f.readlines()

		for line in contents:
			parse_data(line)

	get_arrangements()
	print("Part one: ", max(arrangement_happiness))

	for person in people:
		people[person]["Me"] = 0

	people["Me"] = {person: 0 for person in people.keys()}
	arrangement_happiness.clear()
	get_arrangements()
	print("Part two: ", max(arrangement_happiness))

if __name__ == "__main__":
	main()


import numpy as np
from allfunctions import myCost, RouletteWheelSelection, Crossover, Mutation, Cumulative
import copy
import time
def GAKH(inputdata):
    tic = time.time()
    MaxIt, MaxDuration, nPop, crossNumber, muteNumber, muteRate, elitismProb, beta, nClusters, nModules, w_ij, d_i, crossRate, Dependencies, CodeList, DependencyMatrix, nDependecies, dInArray, dOutArray, isDirected, outFileName  = inputdata
    objective = 0
    clusters = []

    #%% Initialization
    '''
    Answer representation is here for future reference:
        |3|4|5|10|11 ....|3|
        length: number of modules
        number in between: The cluster that module is asssigned too
    '''

    with open(outFileName + "-iters-GAKH.csv", "a+") as q:
        q.write("Iteration,Iteration_CPU_Time,Total_CPU_Time,Objective\n")
        q.flush()

    population=[]
    for i in range(nPop):
        # Get the solution of DP-RL
        pop = [np.random.randint(0,nClusters) for i in range(nModules)]
        modularity = myCost(pop,inputdata)

        #Update the population
        population.append([pop,modularity])

    # Sort the Population
    sortedPopulation=copy.deepcopy(population)
    sortedPopulation.sort(key=lambda x: x[1], reverse = 1)
    population = sortedPopulation
    #%% Main Loop
    for iteration in range(MaxIt):
        tic_iter = time.time()
        Newpop=[]
        # Selecet Elite Parents and move them to next generation
        nElite=int(nPop*elitismProb)
        nNonElite=nPop-nElite
        for i in range(nElite):
             Newpop.append(sortedPopulation[i])
        # Select Parents to Crossover and Mutation
        P=[]
        WorstCost=sortedPopulation[-1][1]
        for i in range(len(sortedPopulation)):
             temp=np.exp(-beta*sortedPopulation[i][1]/(abs(WorstCost)+0.000000000001))
             P.append(temp)
        temp=[]
        for i in range(len(P)):
             temp.append(P[i]/sum(P))
        P=temp
        # Crossover (Cumulative Motion)
        chbest = population[0]
        chworst = population[-1]

        for k in range(crossNumber):
            chi=population[RouletteWheelSelection(P)]
            chj=population[RouletteWheelSelection(P)]
            chibest = chi
            chjbest = chj
            chiworst = chi
            chjworst =chj
            X,Y,A,B,K,Z = Cumulative(chi,chj,chibest,chiworst,chjbest,chjworst,chbest,chworst, inputdata)

            Newpop.append(X)
            Newpop.append(Y)
            Newpop.append(A)
            Newpop.append(B)
            Newpop.append(K)
            Newpop.append(Z)

        # Mutation (Local Movement)
        for k in range(muteNumber):
            chi=population[RouletteWheelSelection(P)]
            offspring=Mutation(chi,inputdata)
            Newpop.append(offspring)
            offspring=Mutation(offspring,inputdata)
            Newpop.append(offspring)
            offspring=Mutation(chbest,inputdata)
            Newpop.append(offspring)
        # Random Diffusion

        population=copy.deepcopy(Newpop)
        sortedPopulation=copy.deepcopy(population)
        sortedPopulation.sort(key=lambda x: x[1], reverse=1)
        sortedPopulation = sortedPopulation[:nPop]
        population = sortedPopulation
        BestSol=sortedPopulation[0]
        BestCost=BestSol[1]
        # print(BestCost)
        with open(outFileName + "-iters-GAKH.csv", 'a+') as f:
            toc_iter = time.time()
            f.write(str(iteration)+ ',' + str(toc_iter-tic_iter) + "," + str(toc_iter-tic) + ',' + str(BestCost) + '\n')
            f.flush()
        if time.time()-tic > MaxDuration:
            break
    return(BestCost, sortedPopulation[0][0])


import xml.etree.ElementTree as ET
import json

# Parse the KML file
tree = ET.parse('tests/places.kml')

# Get the root element
root = tree.getroot()

# Find all Placemark elements
placemarks = root.findall('.//{http://www.opengis.net/kml/2.2}Placemark')

# Loop through the placemarks and print the name and coordinates
data = []

for placemark in placemarks:
    name = placemark.find(
        './/{http://www.opengis.net/kml/2.2}name').text.strip()
    coordinates = placemark.find(
        './/{http://www.opengis.net/kml/2.2}coordinates').text.strip()

    temp = {"name": name, "coordinates": [float(x) for x in coordinates.split(",")[:2]]}
    data.append(temp)

with open("tests/print.json","w") as f: json.dump(data,f)


__author__ = "Rohan Pandit"

from algo import algorithm, triTueAlgo, withDelay
import numpy as np
from time import time
from random import randint
from flask import Flask, abort, jsonify, request
from flask_cors import CORS

screenSize = 700

app = Flask(__name__)
CORS(app)

@app.route('/optimize_route', methods=['POST'])
def optimize():
	data = request.get_json(force=True)
	cities = data['matrix']
	path, length = triTueAlgo(cities)
	return jsonify(length = length, path = path)

@app.route('/optimize_with_time', methods=['POST'])
def optimize_with_time():
	data = request.get_json(force = True)
	cities = data['matrix']
	maximumTime = data['maximum_time']
	path, length = withDelay(cities, maximumTime)
	return jsonify(found = not (len(path) == 0), length = length, path = path)

app.run(host='0.0.0.0', port = 8888)


""" Python3: Save single page of pdf as a new pdf file """
import PyPDF2
# Initialize input pdf file
in_pdf = open(r'/path/to/input.pdf', 'rb')
pdf_reader = PyPDF2.PdfFileReader(in_pdf)   # Reader element
pdf_writer = PyPDF2.PdfFileWriter()         # Writer element
pdf_writer.addPage(pdf_reader.getPage(n))   # Choose n-th page number
# Initialize output pdf file
out_pdf = open(r'/path/to/output.pdf', 'wb')
pdf_writer.write(out_pdf)
# Close I/O pdf files
out_pdf.close()
in_pdf.close()


from playwright.sync_api import Playwright, sync_playwright
from main.pages.app import App

app = App()

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False, slow_mo=1000)
    context = browser.new_context()
    page = context.new_page()
    app.login_ui(page)
    page.close()
    context.close()
    browser.close()

with sync_playwright() as playwright:
    run(playwright)


from tensorboard.backend.event_processing import event_accumulator
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join
import math
import numpy as np
import sys
import tuneConfigurations
import warnings
from ray.tune import Analysis

plt.rcParams['figure.dpi'] = 200

def plot(configs, sets='test', save=False, colorsFirst=False, title="", limits=None):
    keyLoss = 'loss'

    lineStyles = ['solid', 'dashed', 'dotted', 'dashdot']
    lineColors = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9']

    if not type(configs) is list:
        configs = [configs]

    if not type(sets) is list:
        sets = [sets]

    if len(configs)*len(sets) > len(lineStyles)*len(lineColors):
        raise ValueError("Too many curves to plot, {} of max {}.".format(len(configs)*len(sets), len(lineStyles)*len(lineColors)))

    fig = plt.figure(figsize=(8,9))
    #gs = fig.add_gridspec(2,1)
    #axLoss = fig.add_subplot(gs[0, 0])
    #axAcc = fig.add_subplot(gs[1, 0])
    axLoss = fig.add_axes([0.07, 0.53, 0.6, 0.42])
    axAcc = fig.add_axes([0.07, 0.05, 0.6, 0.42])
    #fig, (axLoss, axAcc) = plt.subplots(2)
    metrics = []
    i = 0
    for set in sets:
        for config in configs:
            myConfig = getattr(sys.modules['tuneConfigurations'], config)
            metrics.append(myConfig.trackMetric)

            #use tune to pick best in run
            analysis = Analysis(join("tuneOutput", myConfig.path))
            mode = ("max" if myConfig.bestSign == '>' else "min")
            #print("best hyperparameters for {}: {}".format(config, analysis.get_best_config(metric=myConfig.bestKey, mode=mode)))
            tunePath = analysis.get_best_logdir(metric=myConfig.bestKey, mode=mode)
            expPath = join(tunePath,'files', 'tensorBoard')
            keyAcc = "{}_{}".format(metrics[-1], metrics[-1])

            # metrics.append(getattr(sys.modules['configurations'], config).trackMetric)
            # keyAcc = "{}_{}".format(metrics[-1], metrics[-1])
            # expPath = join('files', getattr(sys.modules['configurations'], config).path, 'tensorBoard')
            try:
                keys = [f for f in listdir(expPath) if not isfile(join(expPath, f))]
            except FileNotFoundError:
                warnings.warn("Configuration {} not present. Skipping.".format(config))
                continue

            points = {}
            for k in keys:
                eventPathPart = join(expPath, k, set)
                for runPath in sorted([f for f in listdir(eventPathPart) if isfile(join(eventPathPart, f))]):
                    eventPath = join(eventPathPart, runPath)
                    ea = event_accumulator.EventAccumulator(eventPath)
                    ea.Reload()
                    if not k in points:
                        points[k] = [[v.step for v in ea.Scalars(k)], [v.value for v in ea.Scalars(k)]]
                    else:
                        points[k][0].extend([v.step for v in ea.Scalars(k)])
                        points[k][1].extend([v.value for v in ea.Scalars(k)])

            if limits is None:
                valuesLoss = points[keyLoss]
                valuesAcc = points[keyAcc]
            else:
                valuesLoss = [points[keyLoss][i][limits[0]:limits[1]] for i in [0,1]]
                valuesAcc = [points[keyAcc][i][limits[0]:limits[1]] for i in [0,1]]

            linesLoss = axLoss.plot(valuesLoss[0], valuesLoss[1], label="{} {}".format(config, set))
            linesAcc = axAcc.plot(valuesAcc[0], valuesAcc[1], label="{} {}".format(config, set))
            if colorsFirst:
                linesLoss[0].set_color(lineColors[i%len(lineColors)])
                linesLoss[0].set_linestyle(lineStyles[(i//len(lineColors))%len(lineStyles)])
                linesAcc[0].set_color(lineColors[i%len(lineColors)])
                linesAcc[0].set_linestyle(lineStyles[(i//len(lineColors))%len(lineStyles)])
            else:
                linesLoss[0].set_linestyle(lineStyles[i%len(lineStyles)])
                linesLoss[0].set_color(lineColors[(i//len(lineStyles))%len(lineColors)])
                linesAcc[0].set_linestyle(lineStyles[i%len(lineStyles)])
                linesAcc[0].set_color(lineColors[(i//len(lineStyles))%len(lineColors)])

            i += 1

    # axLoss.legend(loc='upper left', bbox_to_anchor=(1, 1),
    #          ncol=math.ceil(len(configs)/20), fancybox=True, shadow=True)
    #axLoss.set_title(title)
    axLoss.set_xlabel("Epoch")
    axLoss.set_ylabel("Loss")

    # axLoss.set_xticks(np.arange(0, round(axLoss.get_xlim()[1])+10, 10))
    # axLoss.set_xticks(np.arange(round(axLoss.get_xlim()[0]), round(axLoss.get_xlim()[1])+1, 1), minor=True)
    # axLoss.set_yticks(np.arange(0, round(axLoss.get_ylim()[1])+0.05, 0.05))
    # axLoss.set_yticks(np.arange(0, round(axLoss.get_ylim()[1])+0.01, 0.01), minor=True)
    axLoss.grid(which='both')
    axLoss.grid(which='minor', alpha=0.2)
    axLoss.grid(which='major', alpha=0.5)

    # axAcc.legend(loc='upper left', bbox_to_anchor=(1, 1),
    #           ncol=math.ceil(len(configs)/20), fancybox=True, shadow=True)
    #axAcc.set_title(title)
    axAcc.set_xlabel("Epoch")
    axAcc.set_ylabel("Metric ({})".format(list(dict.fromkeys(metrics))))
    #axAcc.set_ylim(0.49,1.)

    #axAcc.set_xticks(np.arange(0, round(axAcc.get_xlim()[1])+10, 10))
    #axAcc.set_xticks(np.arange(round(axAcc.get_xlim()[0]), round(axAcc.get_xlim()[1])+1, 1), minor=True)
    axAcc.set_yticks(np.arange(0.5, 1.01, 0.1))
    axAcc.set_yticks(np.arange(0.49, 1.01, 0.01), minor=True)
    axAcc.grid(which='both')
    axAcc.grid(which='minor', alpha=0.2)
    axAcc.grid(which='major', alpha=0.5)

    fig.suptitle(title)

    handles, labels = axAcc.get_legend_handles_labels()
    fig.legend(handles, labels, bbox_to_anchor=(0.68, 0.95), loc=2, borderaxespad=0.)
    # fig.legend(handles, labels, loc='upper left', bbox_to_anchor=(1, 1),
    #              bbox_transform = plt.gcf().transFigure,
    #              ncol=math.ceil(len(configs)/20), fancybox=True, shadow=True)

    # plt.legend( handles, labels, loc = 'upper left', bbox_to_anchor = (0.9,-0.1,2,2),
    #         bbox_transform = plt.gcf().transFigure )

    # plt.figlegend(handles, labels, loc='upper center', bbox_to_anchor=(0.5, 0), bbox_transform=plt.gcf().transFigure)

    # fig.subplots_adjust(wspace=2, hspace=2,left=0,top=2,right=2,bottom=0)

    #fig.tight_layout()

    #fig.subplots_adjust(right=2)

    fig.show()

    if save:
        fig.savefig('img/plot.eps')#, bbox_inches = 'tight')#, pad_inches = 0)

    plt.close()

def printMetrics(configs, printAllConfigs=False):
    sets = ['train', 'valid', 'test']

    if not type(configs) is list:
        configs = [configs]

    bestConfig = None
    points = {}
    for config in configs:
        myConfig = getattr(sys.modules['tuneConfigurations'], config)
        metric = myConfig.trackMetric

        try:
            #use tune to pick best in run
            analysis = Analysis(join("tuneOutput", myConfig.path))
        except ValueError:
            warnings.warn("Configuration {} not present. Skipping.".format(config))
            continue
        mode = ("max" if myConfig.bestSign == '>' else "min")
        print("best hyperparameters for {}: {}".format(config, analysis.get_best_config(metric=myConfig.bestKey, mode=mode)))
        tunePath = analysis.get_best_logdir(metric=myConfig.bestKey, mode=mode)
        expPath = join(tunePath,'files', 'tensorBoard')
        keyAcc = "{}_{}".format(metric, metric)
        try:
            keys = [f for f in listdir(expPath) if not isfile(join(expPath, f))]
        except FileNotFoundError:
            warnings.warn("Configuration {} not present. Skipping.".format(config))
            continue

        points[config] = {}
        for set in sets:
            points[config][set] = {}
            for k in keys:
                eventPathPart = join(expPath, k, set)
                for runPath in sorted([f for f in listdir(eventPathPart) if isfile(join(eventPathPart, f))]):
                    eventPath = join(eventPathPart, runPath)
                    ea = event_accumulator.EventAccumulator(eventPath)
                    ea.Reload()
                    if not k in points[config][set]:
                        points[config][set][k] = [[v.step for v in ea.Scalars(k)], [v.value for v in ea.Scalars(k)]]
                    else:
                        points[config][set][k][0].extend([v.step for v in ea.Scalars(k)])
                        points[config][set][k][1].extend([v.value for v in ea.Scalars(k)])

        bestSign = myConfig.bestSign
        if bestSign == '>':
            bestI = np.argmax(points[config]['valid'][keyAcc][1]) #point where better metric
        else:
            bestI = np.argmin(points[config]['valid'][keyAcc][1]) #point where better metric

        thisConfig = {
            'name': config,
            'epoch': bestI+1,
            'train': points[config]['train'][keyAcc][1][bestI],
            'valid': points[config]['valid'][keyAcc][1][bestI],
            'test': points[config]['test'][keyAcc][1][bestI],
        }
        if printAllConfigs:
            print("{} (epoch {}):\ttrain {:.3};\tvalid {:.3};\ttest {:.3}".format(thisConfig['name'], thisConfig['epoch'], thisConfig['train'], thisConfig['valid'], thisConfig['test']))

        if bestConfig is None or (bestSign == '>' and thisConfig['valid']>bestConfig['valid']) or (bestSign == '<' and thisConfig['valid']<bestConfig['valid']):
            bestConfig = thisConfig

    if not bestConfig is None:
        print("BEST ==== {} (epoch {}):\ttrain {:.3};\tvalid {:.3};\ttest {:.3}".format(bestConfig['name'], bestConfig['epoch'], bestConfig['train'], bestConfig['valid'], bestConfig['test']))


from http import HTTPStatus
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException

from core.constants.exception_details import GENRE_NOT_FOUND
from core.utils import verify_auth_tokens
from models.genre import Genre
from services.genre import GenreService, get_genre_service

router = APIRouter()

@router.get('/',
            response_model=list[Genre | None],
            description='Список всех жанров',
            summary='Endpoint позволяет получить список жанров',
            response_description='Лист объектов Genre',
            tags=['Доступ ко всем элементам'],
            dependencies=[Depends(verify_auth_tokens)])
async def all_genres(genre_service: GenreService = Depends(get_genre_service)) -> list[Genre]:
    genres = await genre_service.get_list()
    if not genres:
        return []

    return genres

@router.get('/{genre_id}',
            response_model=Genre,
            description='Детальная информация по жанру',
            summary='Endpoint позволяет получить детальную информацию по жанру',
            response_description='Объект Genre',
            tags=['Доступ ко всем элементам'],
            dependencies=[Depends(verify_auth_tokens)])
async def genre_details(genre_id: UUID, genre_service: GenreService = Depends(get_genre_service)) -> Genre:
    genre = await genre_service.get_by_id(genre_id)
    if not genre:
        raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=GENRE_NOT_FOUND)

    return genre


#!/usr/bin/env python3
from socket import socket
from venomsrc.malware.malwarebase import MalwareGenerator
from venomsrc.endpoints.raw_server.polls.basepoll import Poll
from venomsrc.colored import Colors

class ModulePoll(Poll):

    def __init__(self, writer: object, reader: object, sess_key: str, malware_generator: MalwareGenerator):
        super().__init__(writer, reader, sess_key)
        self.listeners = malware_generator.listener
        self.info = malware_generator.info
        self.platforms = malware_generator.platforms
        self.requirements = malware_generator.requirements
        self.generate = malware_generator.generate
        self.requirements_dict = dict()
        for i in self.requirements:
            self.requirements_dict[i] = None

        self.colors = Colors()

    def help_cmd(self):
        return """ayuda"""

    async def start(self):
        while 1:
            command, argv, argc, cmd = await self.poll_cmdreceive()
            # argv  command splited
            # argc = len(argv)  total len
            # cmd = argv[0]

            if not command:
                break

            # check for one-word commands
            if command == "help":
                await self.utils.ssend(self.writer, self.help_cmd(), self.sess_key)
                continue

            if command == "back":
                await self.utils.ssend(self.writer, "OK", self.sess_key)
                return

            if command == "info":
                message = "\t" + self.info
                message += "\n\tPlatforms: " + ",".join(self.platforms)
                message += "\n\tListeners: " + ",".join(self.listeners)

                await self.utils.ssend(self.writer, message, self.sess_key)
                continue

            if command == "show options":
                message = "Malware options:\n\n"
                message += "\t" + self.colors.UNDERLINE + "Name\t" + "Value\n\n" + self.colors.ENDC
                for i in self.requirements_dict:
                    message += "\t" + i + "\t" + str(self.requirements_dict[i]) + "\n"

                await self.utils.ssend(self.writer, message, self.sess_key)
                continue

            if command == "generate":
                await self.utils.ssend(self.writer, self.generate(self.requirements_dict), self.sess_key)
                continue

            if cmd == "set":
                if argc != 3:
                    await self.utils.ssend(self.writer, "Use: set <OPTION> <VALUE>", self.sess_key)
                    continue
                if argv[1] in self.requirements_dict:
                    self.requirements_dict[argv[1]] = argv[2]
                    await self.utils.ssend(self.writer, argv[1] + " => " + argv[2], self.sess_key)
                    continue
                else:
                    await self.utils.ssend(self.writer, "Unkown value!", self.sess_key)
                    continue
            else:
                await self.utils.ssend(self.writer, "Command not found. Type 'help'", self.sess_key)
                continue


from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import numpy as np
from Orange.data import ContinuousVariable, Domain, Table, Variable
from Orange.misc.utils.embedder_utils import EmbedderCache
from Orange.util import dummy_callback

from orangecontrib.imageanalytics.local_embedder import LocalEmbedder
from orangecontrib.imageanalytics.server_embedder import ServerEmbedder
from orangecontrib.imageanalytics.squeezenet_model import SqueezenetModel
from orangecontrib.imageanalytics.utils.image_utils import extract_paths

MODELS = {
    "inception-v3": {
        "name": "Inception v3",
        "description": "Google's Inception v3 model trained on ImageNet.",
        "target_image_size": (299, 299),
        "layers": ["penultimate"],
        "order": 0,
        # batch size tell how many images we send in parallel, this number is
        # high for inception since it has many workers, but other embedders
        # send less images since bottleneck are workers, this way we avoid
        # ReadTimeout because of images waiting in a queue at the server
        "batch_size": 500,
    },
    "painters": {
        "name": "Painters",
        "description": "A model trained to predict painters from artwork\nimages.",
        "target_image_size": (256, 256),
        "layers": ["penultimate"],
        "order": 4,
        "batch_size": 500,
    },
    "deeploc": {
        "name": "DeepLoc",
        "description": "A model trained to analyze yeast cell images.",
        "target_image_size": (64, 64),
        "layers": ["penultimate"],
        "order": 5,
        "batch_size": 500,
    },
    "vgg16": {
        "name": "VGG-16",
        "description": "16-layer image recognition model trained on\nImageNet.",
        "target_image_size": (224, 224),
        "layers": ["penultimate"],
        "order": 2,
        "batch_size": 500,
    },
    "vgg19": {
        "name": "VGG-19",
        "description": "19-layer image recognition model trained on\nImageNet.",
        "target_image_size": (224, 224),
        "layers": ["penultimate"],
        "order": 3,
        "batch_size": 500,
    },
    "openface": {
        "name": "openface",
        "description": "Face recognition model trained on FaceScrub and\n"
        "CASIA-WebFace datasets.",
        "target_image_size": (256, 256),
        "layers": ["penultimate"],
        "order": 6,
        "batch_size": 500,
    },
    "squeezenet": {
        "name": "SqueezeNet",
        "description": "Deep model for image recognition that achieves \n"
        "AlexNet-level accuracy on ImageNet with \n"
        "50x fewer parameters.",
        "target_image_size": (227, 227),
        "layers": ["penultimate"],
        "order": 1,
        "is_local": True,
        "batch_size": 16,
        "model": SqueezenetModel,
    },
}

class ImageEmbedder:
    """
    Client side functionality for accessing a remote image embedding backend.

    Attributes
    ----------
    model
        Name of the model, must be one from MODELS dictionary
    server_url
        The url of the server with embedding backend.

    Examples
    --------
    >>> import Orange
    >>> from orangecontrib.imageanalytics.image_embedder import ImageEmbedder

    >>> # embedding from list of paths
    >>> image_file_paths = ['image001.jpg', 'image001.jpg']
    >>> with ImageEmbedder(model='model_name') as emb:
    ...    embeddings = emb(image_file_paths)

    >>> # embedding from orange tabl
    >>> table = Orange.data.Table('Table_with_image_path.csv')
    >>> with ImageEmbedder(model='model_name') as emb:
    ...    embeddings = emb(table, col="image_path_column")
    """

    _embedder = None

    def __init__(
        self,
        model: str = "inception-v3",
        server_url: str = "https://api.garaza.io/",
    ):
        self.server_url = server_url
        self.model = model
        self._model_settings = self._get_model_settings_confidently()

    def is_local_embedder(self) -> bool:
        """
        Tells whether selected embedder is local or not.
        """
        return self._model_settings.get("is_local", False)

    def _get_model_settings_confidently(self) -> Dict[str, Any]:
        """
        Returns the dictionary with model settings

        Returns
        -------
        The dictionary with model settings
        """
        if self.model not in MODELS.keys():
            model_error = "'{:s}' is not a valid model, should be one of: {:s}"
            available_models = ", ".join(MODELS.keys())
            raise ValueError(model_error.format(self.model, available_models))
        return MODELS[self.model]

    def _init_embedder(self) -> None:
        """
        Init local or server embedder.
        """
        if self.is_local_embedder():
            self._embedder = LocalEmbedder(self.model, self._model_settings)
        else:
            self._embedder = ServerEmbedder(
                self.model,
                self._model_settings["batch_size"],
                self.server_url,
                "image",
                self._model_settings["target_image_size"]
            )

    def __call__(
        self,
        data: Union[Table, List[str], np.array],
        col: Optional[Union[str, Variable]] = None,
        callback: Optional[Callable] = dummy_callback,
    ) -> Union[Tuple[Table, Table, int], List[List[float]]]:
        """
        Embedd images.

        Parameters
        ----------
        data
            Data contains the path to images (locally or online). It can be
            Orange data table or list/array. When data table on input col
            parameter must define which column in the table contains images.
        col
            The column with images in Orange data table. It is not required
            when data are list or array.
        callback
            Optional callback - function that is called for every embedded
            image and is used to report the progress.

        Returns
        -------
        Embedded images. When data is Table it returns tuple with two tables:
        1) original table with embedded images appended to it, 2) table with
        skipped images, 3) number of skipped images.
        When data is array/list it returns the list of list with embeddings,
        each image is represented with vector of numbers.
        """
        assert data is not None
        assert isinstance(data, (np.ndarray, list, Table))
        self._init_embedder()
        if isinstance(data, Table):
            assert col is not None, "Please provide a column for image path"
            # if table on input tables on output
            return self.from_table(data, col=col, callback=callback)
        elif isinstance(data, (np.ndarray, list)):
            # if array-like on input array-like on output
            return self._embedder.embedd_data(data, callback=callback)

    def from_table(
        self,
        data: Table,
        col: Union[str, Variable] = "image",
        callback: Callable = None,
    ) -> Tuple[Table, Table, int]:
        """
        Calls embedding when data are provided as a Orange Table.

        Parameters
        ----------
        data
            Data table with image paths
        col
            The column with image paths
        callback
            Optional callback - function that is called for every embedded
            image and is used to report the progress.
        """
        file_paths = extract_paths(data, data.domain[col])
        embeddings_ = self._embedder.embedd_data(file_paths, callback=callback)
        return ImageEmbedder.prepare_output_data(data, embeddings_)

    def __enter__(self) -> "ImageEmbedder":
        return self

    def __exit__(self, _, __, ___) -> None:
        pass

    def __del__(self) -> None:
        self.__exit__(None, None, None)

    @staticmethod
    def construct_output_data_table(
            embedded_images: Table, embeddings_: np.ndarray
    ) -> Table:
        """
        Join the orange table with embeddings.

        Parameters
        ----------
        embedded_images
            Table with images that were successfully embedded
        embeddings_
            Embeddings for images from table

        Returns
        -------
        Table with added embeddings to data.
        """
        new_attributes = [
            ContinuousVariable("n{:d}".format(d)) for d in range(embeddings_.shape[1])
        ]
        # prevent embeddings to be shown in long drop-downs in e.g. scatterplot
        for a in new_attributes:
            a.attributes["hidden"] = True

        domain_new = Domain(
            list(embedded_images.domain.attributes) + new_attributes,
            embedded_images.domain.class_vars,
            embedded_images.domain.metas,
        )
        table = embedded_images.transform(domain_new)
        with table.unlocked(table.X):  # writing to fresh part, can be unlocked
            table[:, new_attributes] = embeddings_

        return table

    @staticmethod
    def prepare_output_data(
        input_data: Table, embeddings_: List[List[float]]
    ) -> Tuple[Table, Table, int]:
        """
        Prepare output data when data table on input.

        Parameters
        ----------
        input_data
            The table with original data that are joined with embeddings
        embeddings_
            List with embeddings

        Returns
        -------
        Tuple where first parameter is table with embedded images, the second
        table with skipped images and third the number of skipped images.
        """
        skipped_images_bool = [x is None or len(x) == 0 for x in embeddings_]

        if np.any(skipped_images_bool):
            skipped_images = input_data[skipped_images_bool].copy()
            skipped_images.name = "Skipped images"
            num_skipped = len(skipped_images)
        else:
            num_skipped = 0
            skipped_images = None

        embedded_images_bool = np.logical_not(skipped_images_bool)

        if np.any(embedded_images_bool):
            embedded_images = input_data[embedded_images_bool]

            embeddings_ = [
                e for e, b in zip(embeddings_, embedded_images_bool) if b
            ]
            embeddings_ = np.vstack(embeddings_)

            embedded_images = ImageEmbedder.construct_output_data_table(
                embedded_images, embeddings_
            )
            embedded_images.ids = input_data.ids[embedded_images_bool]
            embedded_images.name = "Embedded images"
        else:
            embedded_images = None

        return embedded_images, skipped_images, num_skipped

    def clear_cache(self) -> None:
        """
        Function clear cache for the selected embedder. If embedder is loaded
        cache is cleaned from its dict otherwise we load cache and clean it
        from file.
        """
        if self._embedder:
            # embedder is loaded so we clean its cache
            self._embedder.clear_cache()
        else:
            # embedder is not initialized yet - clear it cache from file
            cache = EmbedderCache(self.model)
            cache.clear_cache()

if __name__ == "__main__":
    image_file_paths = ["tests/test_images/example_image_0.jpg"]
    # with ImageEmbedder(model='inception-v3') as embedder:
    with ImageEmbedder(model="squeezenet") as embedder:
        embedder.clear_cache()
        print(embedder(image_file_paths))


import os
import pandas as pd
import numpy as np
from sklearn.impute import KNNImputer
from sklearn.preprocessing import OrdinalEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from imblearn.over_sampling import RandomOverSampler
import warnings

warnings.filterwarnings("ignore")

import logging
os.makedirs("Application_Logs", exist_ok=True)

logging.basicConfig(
    filename=os.path.join("Application_Logs", 'running_logs.log'),
    level=logging.INFO,
    format="[%(asctime)s: %(levelname)s: %(module)s]: %(message)s",
    filemode="a"
)

class Preprocessor:

    def __init__(self):
        """
        Initializing the log file object
        """
        pass

    def check_null_values(self, data):

        """
        Checking the null values in the Training Data and storing
        the features in the DataWithMissingValues.CSV
        """

        feature_with_null = [feature for feature in data.columns if data[feature].isnull().sum() > 0]
        logging.info("Start of Train Data Preprocessing. checking the null values of the training data")

        if len(feature_with_null) > 0:

            dataframe_with_null = data[feature_with_null].isnull().sum().to_frame().reset_index()
            dataframe_with_null.columns = ["Feature Name", "Number of Missing Values"]

            Missing_Values = "MissingValues"
            os.makedirs(Missing_Values, exist_ok=True)
            dataframe_with_null.to_csv("MissingValues/DataWithMissingValues.CSV", index=False)
            logging.info("Feature Have Some Missing Values.Check Missing Values folder for features having missing values.Exiting the function")

        else:
            logging.info("No Missing Values in any feature.Exiting the function")

    def encode_and_impute_data(self, data):

        """
        Encoding some of the features of the training data and
        dividing the features into numerical and categorical
        :return: training data
        :rtype: DataFrame
        """

        logging.info("Entered the Encode and Impute method function of training")
        data["sex"] = np.where(data["sex"] == "F", 0, 1)
        data["referral_source"] = data["referral_source"].map({"other": 0, "SVI": 1, "SVHC": 2, "STMW": 4, "SVHD": 5})
        data["Class"] = data["Class"].map({"negative": 0
                                              , "compensated_hypothyroid": 1
                                              , "primary_hypothyroid": 2
                                              , "secondary_hypothyroid": 3})
        self.categorical_features = [feature for feature in data.columns if len(data[feature].unique()) < 10
                                     and feature not in ["Class", "sex", "referral_source"]]

        self.numerical_features = [feature for feature in data.columns if feature not in self.categorical_features
                                   and feature not in ["Class"]]

        logging.info("Exited the Encode and Impute method of training")

        return data

    def separate_label_feature(self, data, label_name):

        """
        Separating features into dependent and independent features
        :return: X,y
        :rtype: DataFrame, Series
        """
        logging.info("Entered the separate_label_feature function of training")
        self.X = data.drop(label_name, axis=1)
        self.y = data[label_name]
        logging.info("Labels are separated into dependent and independent.Exiting the function of training")
        return self.X, self.y

    def handle_imbalance_data(self, x_train, y_train, x_valid, y_valid):

        """
        Handling the imbalanceness of the training data
        :return: x_train_sampled, y_train_sampled, x_valid_sampled, y_valid_sampled
        :rtype: DataFrame and Series
        """
        logging.info("Entered the handle_imbalance_data function of training")
        rdsample = RandomOverSampler()

        x_train_sampled, y_train_sampled = rdsample.fit_resample(x_train, y_train)
        x_valid_sampled, y_valid_sampled = rdsample.fit_resample(x_valid, y_valid)

        logging.info("Balancing of data is done.Exiting the function of training")

        return x_train_sampled, y_train_sampled, x_valid_sampled, y_valid_sampled

    def preprocessor_pipeline(self, x_train_sampled, x_valid_sampled):

        """
        Creating a pipeline to encode and impute the features of the training data
        :return: x_train_processed, x_valid_processed
        :rtype: Numpy Array
        """
        logging.info("Entered the preprocessor_pipeline function of training ")
        numerical_transformer = KNNImputer(n_neighbors=2, weights='uniform', missing_values=np.nan)
        categorical_transformer = Pipeline(steps=[
            ('encoder', OrdinalEncoder()),
            ('imputer', KNNImputer(n_neighbors=2, weights='uniform', missing_values=np.nan))
        ])

        preprocessor = ColumnTransformer(
            transformers=[
                ('num', numerical_transformer, self.numerical_features),
                ('cat', categorical_transformer, self.categorical_features)
            ])

        x_train_processed = preprocessor.fit_transform(x_train_sampled)
        x_valid_processed = preprocessor.transform(x_valid_sampled)

        x_train_processed = pd.DataFrame(x_train_processed, columns=x_train_sampled.columns)
        x_valid_processed = pd.DataFrame(x_valid_processed, columns=x_valid_sampled.columns)

        logging.info("All the features are encoded and imputed of training data. Exiting the module")

        return x_train_processed, x_valid_processed


from jax_sandbox.common.dataset import TransitionBatch
import jax
import jax.numpy as jnp

@jax.jit
def returns_to_go(batch: TransitionBatch, gamma: float = 1.0) -> jnp.ndarray:
    '''
    Computes returns to go, optionally discounted by gamma.

    Rewards are of shape (B,).
    '''
    rewards = batch.rewards
    B = rewards.shape[0]
    if gamma < 1.0:
        discounts = jnp.geomspace(1.0, gamma ** (B - 1), num=B)
        rewards *= discounts

    return jnp.cumsum(rewards)


from details.algprog.calc_lis import calc

# картонная крышка
def lid_cb(width, length, lid_hight, thickness_cb):
    indent = 5
    width = (lid_hight*2)+width+(thickness_cb*2)+2+indent
    length = (lid_hight*2)+length+(thickness_cb*2)+2+indent
    return [width, length]

# расход материала
def expence_pap(width, length, hight, thickness_cb, lis_siz):
    lid = lid_cb(width, length, hight, thickness_cb)
    lid_ras = calc([lid], lis_siz)
    return {'Расход': float(lid_ras[0]),
            'Информация': f'{lid[0]}x{lid[1]}мм.'}


# exercício 16: Faça um programa que leia um ângulo qualquer e mostre na tela o valor do seno, cosseno e tangente desse ângulo

import math
# from math import radians, sin, cos, tan

a = int(input('Ângulo: '))
x = math.radians(a)
s = math.sin(x)
c = math.cos(x)
t = math.tan(x)
print('Seno: {:.2f}'. format(s))
print('Cosseno: {:.2f}'. format(c))
print('Tangente: {:.2f}'. format(t))


import gettext
import os

def get_(language):
    """
    Returns the function for localizing text for the given language, which
    is normally assigned to the function name ``_``.  Typical usage::

        _ = get_('en')
        print _("localizable string")

    :param language:
    :return:
    """
    locale_dir = os.path.realpath(os.path.join(
        os.path.dirname(__file__), '..', 'locale'))
    translator = gettext.translation(domain='messages',
                                     localedir=locale_dir,
                                     fallback=True,
                                     languages=[language])
    return translator.ugettext


import FeatureProject
from sklearn.linear_model import LogisticRegression
import os
import ROCX
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report, confusion_matrix
import pandas as pd
from sklearn.decomposition import PCA
import time
import numpy as np
'''进行模型的保存和加载'''
import joblib
import csv

def n_components_analysis(n, x_train, y_train, x_val, y_val,result_file,resultPath,classifier,splitFile):  #

    start = time.time()
    pca = PCA(n_components=n)
    result_file.write("特征降维，传递参数为{}\n".format(n))
    # print("特征降维，传递参数为{}".format(n))
    pca.fit(x_train)

    x_train_pca = pca.transform(x_train)
    x_val_pca = pca.transform(x_val)

    result_file.write("开始进行SVM训练")
    # print("开始进行SVM训练")
    ss = LogisticRegression(class_weight='balanced', penalty='l1', solver='liblinear')
    ss.fit(x_train_pca, y_train)
    ## 使用测试集进行预测概率
    y_pred_prob = ss.fit(x_train_pca, y_train).predict_proba(x_val_pca)
    y_pred = ss.fit(x_train_pca, y_train).predict(x_val_pca)
    # str1 = 'classification_report:\n', classification_report(y_val, y_pred,digits=3)
    # result_file.writelines(str1)
    # str1 = 'confusion_matrix:\n',confusion_matrix(y_val, y_pred)
    # result_file.writelines(str1)
    """
    主成分分析的模型结果也需要进行存储
    """
    pd.DataFrame(y_pred_prob).to_csv(resultPath + '/pred_proba.csv')
    pd.DataFrame(y_pred).to_csv(resultPath + '/pred.csv')
    df1 = pd.read_csv(splitFile + '/y_test.csv')
    df2 = pd.read_csv(resultPath + '/pred.csv', index_col='Unnamed: 0')
    df3 = pd.read_csv(resultPath + '/pred_proba.csv', index_col='Unnamed: 0')
    df4 = pd.concat(objs=[df1, df2, df3], axis=1)
    if classifier == 2:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2']
    if classifier == 3:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2', 'pred_prob3']
    df4.to_csv(resultPath + '/' + str(n) + 'result_compare.csv')

    '''将交叉验证结果写入文件'''
    scores = cross_val_score(ss, x_train_pca, y_train, cv=5, scoring='accuracy')
    result_file.write(str('\n训练集交叉验证结果：\n'))
    result_file.writelines(str(scores))
    '''将模型测试评分结果写入文件'''
    score = ss.score(x_val_pca, y_val)  # 多分类单看一个score不恰当，应该看单独的
    result_file.write(str('\n模型测试评分结果：'))
    result_file.writelines(str(score))

    '''将分类报告写入文件'''
    clf_rep = classification_report(y_val, y_pred, digits=3)
    result_file.write(str('\n分类报告：\n'))
    result_file.write(clf_rep)
    '''将混淆矩阵写入文件'''
    cfu_mx = confusion_matrix(y_val, y_pred)
    result_file.write(str('混淆矩阵：\n'))
    result_file.writelines(str(cfu_mx))
    result_file.write('\n-----------------------------------------------------------------------------\n')

    #     ## 绘制roc曲线
    picName = '选择' + str(n) + '的主成分ClassifierAuc.png'
    if classifier == 3:
        ROCX.three_auc_report(n, classifier,y_val,y_pred_prob,resultPath+'/ROC', picName=picName)
    else:
        ROCX.auc_report(n, y_val, y_pred_prob[:, 1], resultPath+'/ROC', picName=picName)

'''
函数参数说明：
classifer：用于说明是进行二分类还是进行三分类
splitFile：存放测试集、训练集的文件路径
resultPath：用于保存结果的文件路径,比如SVM就写到 ...... /result/SVM
'''

def logistic_reg(classifer,splitFile,resultPath, tp):

    if os.path.exists(resultPath) == False:  # 如果存放结果的目标路径不存在，则进行创建
        os.makedirs(resultPath)

    X_train = pd.read_csv(splitFile + '/X_train.csv').iloc[:, 2:].values
    X_test = pd.read_csv(splitFile + '/X_test.csv').iloc[:, 2:].values
    y_train = pd.read_csv(splitFile + '/y_train.csv').iloc[:, 1].values
    y_test = pd.read_csv(splitFile + '/y_test.csv').iloc[:, 1].values

    estimator = LogisticRegression(class_weight='balanced', penalty='l1', solver='liblinear')
    estimator.fit(X_train, y_train)

    y_pred_prob = estimator.predict_proba(X_test)
    y_pred = estimator.predict(X_test)

    '''训练集同样当成测试集输入并且进行测试'''
    train_pred_prob = estimator.predict_proba(X_train)
    train_pred = estimator.predict(X_train)

    pd.DataFrame(train_pred_prob).to_csv(resultPath + '/train_pred_proba.csv')
    pd.DataFrame(train_pred).to_csv(resultPath + '/train_pred.csv')
    df1 = pd.read_csv(splitFile + '/y_train.csv')
    df2 = pd.read_csv(resultPath + '/train_pred.csv', index_col='Unnamed: 0')
    df3 = pd.read_csv(resultPath + '/train_pred_proba.csv', index_col='Unnamed: 0')
    df4 = pd.concat(objs=[df1, df2, df3], axis=1)
    if classifer == 2:
        df4.columns = ['SubjID', 'Group', 'pred', 'train_pred_prob1', 'train_pred_prob2']
    if classifer == 3:
        df4.columns = ['SubjID', 'Group', 'pred', 'train_pred_prob1', 'train_pred_prob2', 'train_pred_prob3']
    df4.to_csv(resultPath + '/train_result_compare.csv')

    """
            将预测结果、预测结果概率存入/result/logisticRegression/result_compare.csv文件
    """
    pd.DataFrame(y_pred_prob).to_csv(resultPath + '/pred_proba.csv')
    pd.DataFrame(y_pred).to_csv(resultPath + '/pred.csv')
    df1 = pd.read_csv(splitFile + '/y_test.csv')
    df2 = pd.read_csv(resultPath + '/pred.csv', index_col='Unnamed: 0')
    df3 = pd.read_csv(resultPath + '/pred_proba.csv', index_col='Unnamed: 0')
    df4 = pd.concat(objs=[df1, df2, df3], axis=1)
    if classifer == 2:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2']
    if classifer == 3:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2', 'pred_prob3']
    df4.to_csv(resultPath + '/result_compare.csv')

    """
            权重保存
    """
    weights = estimator.coef_.tolist()[0]
    with open(splitFile + '/X_train.csv', 'r') as f:
        reader = csv.reader(f)
        feature = next(reader)
        feature = feature[2:]
    # print('feature:', feature)
    # print('weights:', weights)
    dataframe = pd.DataFrame({'feature': feature, 'weight': weights})
    # print('path:', resultPath + '/Logitic_weights.csv')
    dataframe.to_csv(resultPath + '/Logitic_weights.csv', index=False, sep=',')

    """
            模型得分
    """
    '''打开文件'''
    proba_result = open(resultPath + '/model_score.txt', mode='w')

    '''将模型得出的权重系数写入到文件中'''
    # proba_result.write('逻辑回归得到的权重系数：' + estimator.)
    '''将交叉验证结果写入文件'''
    scores = cross_val_score(estimator, X_train, y_train, cv=5, scoring='accuracy')
    proba_result.write(str('训练集交叉验证结果：\n'))
    proba_result.writelines(str(scores))
    '''将模型测试评分结果写入文件'''
    score = estimator.score(X_test, y_test)  # 多分类单看一个score不恰当，应该看单独的
    proba_result.write('测试集：\n')
    proba_result.write(str('\n模型测试评分结果：'))
    proba_result.writelines(str(score))

    y_pred = estimator.predict(X_test)
    '''将分类报告写入文件'''
    clf_rep = classification_report(y_test, y_pred, digits=6)
    proba_result.write(str('\n分类报告：\n'))
    proba_result.write(clf_rep)
    '''将混淆矩阵写入文件'''
    cfu_mx = confusion_matrix(y_test, y_pred)
    proba_result.write(str('混淆矩阵：\n'))
    proba_result.writelines(str(cfu_mx))
    '''关闭文件'''

    '''训练集评分保存至文件'''
    score = estimator.score(X_train, y_train)  # 多分类单看一个score不恰当，应该看单独的
    proba_result.write(str('\n测试集模型测试评分结果：'))
    proba_result.writelines(str(score))

    y_pred = estimator.predict(X_train)
    '''将分类报告写入文件'''
    clf_rep = classification_report(y_train, y_pred, digits=6)
    proba_result.write(str('\n分类报告：\n'))
    proba_result.write(clf_rep)
    '''将混淆矩阵写入文件'''
    cfu_mx = confusion_matrix(y_train, y_pred)
    proba_result.write(str('混淆矩阵：\n'))
    proba_result.writelines(str(cfu_mx))
    '''关闭文件'''
    proba_result.close()

    if classifer == 3:
        # ROCX.three_auc_report(1, classifer, y_test, y_pred_prob, resultPath+'/ROC', picName='logisticRegression.png', tp=tp)
        joblib.dump(estimator, resultPath + '/model.pkl')
    # else:
    #     ROCX.auc_report(1, y_test, y_pred_prob[:,1], resultPath+'/ROC', picName='logisticRegression.png')
    #     joblib.dump(estimator, resultPath + '/model.pkl')
    #

    """
    PCA
    """
    # if os.path.exists(resultPath+'/PCA') == False:
    #     os.makedirs(resultPath+'/PCA')
    #
    # result_file = open(resultPath + '/PCA' + '/result.txt', mode='w')
    # n_s = np.linspace(0.6, 0.8, num=5)
    #
    # accuracy = []
    # for n in n_s:
    #     tmp = n_components_analysis(n, X_train, y_train, X_test,
    #                                 y_test, result_file, resultPath + '/PCA',
    #                                 classifier=classifer,splitFile=splitFile)  # 使用原始数据直接进行主成分分析，8-2分
    #
    #     accuracy.append(tmp)
    # # acc = 'total accurcy:\n', accuracy
    # # result_file.writelines(acc)
    # result_file.close()
    return


import sublime
import sublime_plugin

import re

autocompletes = {
	"Alert": [
		["Prompt", "Prompt(${1:title}, ${2:message})"],
		["Alert", "Alert(${1:title}, ${2:message})"],
		["Confirm", "Confirm(${1:title}, ${2:message})"],
	],
	"SMAlert": [
		["Prompt", "Prompt(${1:title}, ${2:message})"],
		["Alert", "Alert(${1:title}, ${2:message})"],
		["Confirm", "Confirm(${1:title}, ${2:message})"],
	],
}

class ReactNativeAutocomplete(sublime_plugin.EventListener):
	def on_query_completions(self, view, prefix, locations):
		sn = view.scope_name(locations[0])

		is_ok = False
		# if "meta.block.tsx" in sn or "meta.block.jsx" in sn:
		if "source.tsx" in sn or "source.jsx" in sn:
			is_ok = True
		if not is_ok:
			return None

		curr_pos = view.sel()[0].begin()
		current_obj_region = view.word(curr_pos - 1)
		current_obj = view.substr(current_obj_region)

		return autocompletes.get(current_obj)


import os

import pygame

from battle2.model import Direction
from battle2.model import State
import battle2.eventmanager as evm

SCREENWIDTH = 1600
SCREENHEIGHT = 800  # 1600, 800  # 1920, 1080
WINDOWWIDTH = 800
WINDOWHEIGHT = 600
WINDOWPOS = 100, 100

TILESIZE = 32
PLAYERLAYER = 2
GRIDLAYER = 4

FPS = 60

BLACK = pygame.Color("black")
WHITE = pygame.Color("white")
GREEN = pygame.Color("green")
GRAY = pygame.Color("gray38")
DARKGRAY = pygame.Color("gray12")
BLUE = pygame.Color("blue")
PURPLE = pygame.Color("purple")

class GraphicalView(object):
    """
    Draws the model state onto the screen.
    """
    def __init__(self, ev_manager, model):
        """
        ev_manager (EventManager): Allows posting messages to the event queue.
        model (GameEngine): a strong reference to the game Model.

        Attributes:
        isinitialized (bool): pygame is ready to draw.
        screen (pygame.Surface): the screen surface.
        clock (pygame.time.Clock): keeps the fps constant.
        fonts (pygame.Font): a small font.
        """
        self.ev_manager = ev_manager
        ev_manager.register_listener(self)
        self.model = model
        self.isinitialized = False

    def notify(self, event):
        """
        Receive events posted to the message queue.
        """
        if isinstance(event, evm.InitializeEvent):
            self._initialize()

        elif isinstance(event, evm.DrawMapEvent):
            self._draw_map()

        elif isinstance(event, evm.QuitEvent):
            self.isinitialized = False
            pygame.quit()

        elif isinstance(event, evm.CharUpdateEvent):

            self.current_player.rect.topleft = self.model.current_character.new_position
            self.detect_collision()
            self.current_player.updatespeed = self.model.current_character.movespeed
            self.current_player.update(event)

        elif isinstance(event, evm.InputEvent):
            if event.key == pygame.K_c:
                self.model.current_character.align_to_grid()
                # voeg de rect van de huidige speler toe aan de lists en verwijder de rect van start_pos
                self.model.map.add_rect_to_list(self.current_player.rect, self.model.map.heroes)
                self.model.map.add_rect_to_list(self.current_player.rect, self.model.map.obstacles)
                self.model.map.start_pos = None

                self.model.current_character = self.model.characters[1]
                self.current_player = self.players[1]

                # de rect van de player die aan de beurt is weer verwijderen en ken de start_pos toe
                self.model.map.del_rect_from_list(self.current_player.rect, self.model.map.heroes)
                self.model.map.del_rect_from_list(self.current_player.rect, self.model.map.obstacles)
                self.model.map.start_pos = self.current_player.rect.copy()

                # voeg de info sprites toe aan de mapview vanuit de mapdata
                self.map1.info.append(InfoSprite(self.model.map.start_pos, 'start', GRIDLAYER))
                for rect in self.model.map.trees:
                    self.map1.info.append(InfoSprite(pygame.Rect(rect), 'tree', GRIDLAYER))
                for rect in self.model.map.waters:
                    self.map1.info.append(InfoSprite(pygame.Rect(rect), 'water', GRIDLAYER))
                for rect in self.model.map.heroes:
                    self.map1.info.append(InfoSprite(pygame.Rect(rect), 'hero', GRIDLAYER))

                # voeg de obstacle waarden toe aan de mapview vanuit de mapdata
                for rect in self.model.map.obstacles:
                    self.map1.obstacles.append(pygame.Rect(rect))
                for rect in self.model.map.low_obst:
                    self.map1.low_obst.append(pygame.Rect(rect))

            if event.key == pygame.K_F10:
                self.map1.grid.show ^= True
            if event.key == pygame.K_F11:
                self.info ^= True
            if event.key == pygame.K_F12:
                self.debug ^= True          # simple boolean swith

        elif isinstance(event, evm.TickEvent):
            if not self.isinitialized:
                return
            currentstate = self.model.state.peek()
            if currentstate == State.Intro:
                self.render_intro()
            if currentstate == State.Menu:
                self.render_menu()
            if currentstate == State.Play:
                self.render_play()
            if currentstate == State.Help:
                self.render_help()
            self.clock.tick(FPS)            # limit the redraw speed to 60 frames per second

    def _initialize(self):
        """
        Set up the pygame graphical display and loads graphical resources.
        """
        os.environ['SDL_VIDEO_CENTERED'] = '1'
        pygame.init()
        self.screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT), pygame.DOUBLEBUF)  # | pygame.FULLSCREEN)
        self.background = pygame.Surface(self.screen.get_size())
        self.background.fill(BLACK)
        self.background = self.background.convert()
        self.window = pygame.Surface((WINDOWWIDTH, WINDOWHEIGHT))
        self.window.fill(DARKGRAY)
        self.window = self.window.convert()

        self.clock = pygame.time.Clock()
        self.debugfont = pygame.font.SysFont('courier', 11)
        self.titlefont = pygame.font.SysFont('sans', 25, True)
        self._init_buttons()

        self.players = []
        for character in self.model.characters:
            self.players.append(CharSprite(character.bmp))
        self.current_player = None

        self.info = False
        self.debug = False
        self.isinitialized = True

    def _draw_map(self):
        import pyscroll
        map_layer = pyscroll.BufferedRenderer(self.model.map.map_data, (WINDOWWIDTH, WINDOWHEIGHT), clamp_camera=True)
        self.group = pyscroll.PyscrollGroup(map_layer=map_layer, default_layer=PLAYERLAYER)

        # zet alle players sprites op de juiste posities
        for count, player in enumerate(self.players):
            player.rect.topleft = self.model.characters[count].new_position

        self.current_player = self.players[0]

        self.group.add(self.players)

        # voeg de posities van alles heroes aan de mapdata toe en ook als obstacle
        for char in self.model.characters:
            rect = pygame.Rect(char.new_position[0], char.new_position[1], char.width, char.height)
            self.model.map.add_rect_to_list(rect, self.model.map.heroes)
            self.model.map.add_rect_to_list(rect, self.model.map.obstacles)

        # de rect van de player die aan de beurt is weer verwijderen en ken de start_pos toe
        self.model.map.del_rect_from_list(self.current_player.rect, self.model.map.heroes)
        self.model.map.del_rect_from_list(self.current_player.rect, self.model.map.obstacles)
        self.model.map.start_pos = self.current_player.rect.copy()

        # creeer een mapview
        self.map1 = MapView(self.model.map.width, self.model.map.height)

        # maak van het current hokje een hero-info sprite
        self.map1.current = InfoSprite(self.current_player.rect, 'hero', GRIDLAYER)

        # voeg de info sprites toe aan de mapview vanuit de mapdata
        self.map1.info.append(InfoSprite(self.model.map.start_pos, 'start', GRIDLAYER))
        for rect in self.model.map.trees:
            self.map1.info.append(InfoSprite(pygame.Rect(rect), 'tree', GRIDLAYER))
        for rect in self.model.map.waters:
            self.map1.info.append(InfoSprite(pygame.Rect(rect), 'water', GRIDLAYER))
        for rect in self.model.map.heroes:
            self.map1.info.append(InfoSprite(pygame.Rect(rect), 'hero', GRIDLAYER))

        # voeg de obstacle waarden toe aan de mapview vanuit de mapdata
        for rect in self.model.map.obstacles:
            self.map1.obstacles.append(pygame.Rect(rect))
        for rect in self.model.map.low_obst:
            self.map1.low_obst.append(pygame.Rect(rect))

    def render_intro(self):
        """
        Render the game intro.
        """
        bg_rect = self.background.get_rect()

        somewords = self.titlefont.render('Battle...!', True, GREEN)
        text_rect = somewords.get_rect()
        text_rect.center = bg_rect.width/2, bg_rect.height/2

        self.screen.blit(somewords, text_rect.topleft)
        pygame.display.flip()
        self.screen.blit(self.background, (0, 0))

    def render_play(self):
        """
        Render the game play.
        """
        self.draw_grid()
        self.draw_info()
        self.show_window()
        self.show_debug()
        self.show_buttons()
        pygame.display.flip()
        self.screen.blit(self.background, (0, 0))

    def draw_grid(self):
        if self.map1.grid.show:
            self.group.add(self.map1.grid)
        else:
            self.group.remove(self.map1.grid)

    def draw_info(self):
        if self.info:
            self.map1.current.rect.topleft = self.model.current_character.new_position
            self.group.add(self.map1.current)
            self.group.add(self.map1.info)
        else:
            self.group.remove(self.map1.current)
            self.group.remove(self.map1.info)

    def show_window(self):
        self.group.center(self.current_player.rect.center)
        self.group.draw(self.window)
        self.screen.blit(self.window, WINDOWPOS)

    def show_debug(self):
        if self.debug:
            text = ("FPS:            {}".format(int(self.clock.get_fps())),
                    "step_north:     {}".format(self.model.current_character.step_north),
                    "step_south:     {}".format(self.model.current_character.step_south),
                    "step_west:      {}".format(self.model.current_character.step_west),
                    "step_east:      {}".format(self.model.current_character.step_east),
                    "step_delay:     {}".format(self.model.current_character.step_delay),
                    "last_direction: {}".format(self.model.current_character.last_direction),
                    "move_direction: {}".format(self.model.current_character.move_direction),
                    "movespeed:      {}".format(self.model.current_character.movespeed),
                    "start_pos.x:    {}".format(self.model.map.start_pos[0]
                                                if self.model.map.start_pos is not None else "None"),
                    "old_position.x: {}".format(self.model.current_character.old_position[0]),
                    "new_position.x: {}".format(self.model.current_character.new_position[0]),
                    "start_pos.y     {}".format(self.model.map.start_pos[1]
                                                if self.model.map.start_pos is not None else "None"),
                    "old_position.y  {}".format(self.model.current_character.old_position[1]),
                    "new_position.y  {}".format(self.model.current_character.new_position[1]),
                    "step_count:     {}".format(self.current_player.step_count),
                    "step_animation: {}".format(self.current_player.step_animation),
                    )
            for count, line in enumerate(text):
                self.screen.blit(self.debugfont.render(line, True, WHITE), (0, count * 10))

    def show_buttons(self):
        for button in self.buttons:
            button.draw(self.screen)

    def render_menu(self):
        """
        Render the game menu.
        """
        somewords = self.titlefont.render('You are in the Menu. Space to play. Esc exits.', True, GREEN)
        self.screen.blit(somewords, (0, 0))
        pygame.display.flip()
        self.screen.blit(self.background, (0, 0))

    def render_help(self):
        """
        Render the help screen.
        """
        somewords = self.titlefont.render('Help is here. space, escape or return.', True, GREEN)
        self.screen.blit(somewords, (0, 0))
        pygame.display.flip()
        self.screen.blit(self.background, (0, 0))

    def _init_buttons(self):
        self.button_view = ButtonSprite((SCREENWIDTH-200,   SCREENHEIGHT-300), "V")
        self.button_up = ButtonSprite((SCREENWIDTH-150,     SCREENHEIGHT-300), "Up")
        self.button_down = ButtonSprite((SCREENWIDTH-150,   SCREENHEIGHT-250), "Down")
        self.button_left = ButtonSprite((SCREENWIDTH-200,   SCREENHEIGHT-250), "Left")
        self.button_right = ButtonSprite((SCREENWIDTH-100,  SCREENHEIGHT-250), "Right")
        self.button_cancel = ButtonSprite((SCREENWIDTH-100, SCREENHEIGHT-200), "C")

        self.buttons = [self.button_view, self.button_up, self.button_down, self.button_left, self.button_right,
                        self.button_cancel]

    def detect_collision(self):
        # loop tegen de rand van een obstacle aan
        # er mag maar 1 obstacle in deze lijst zijn
        if len(self.current_player.rect.collidelistall(self.map1.obstacles)) == 1:
            # obj_nr is het nummer van de betreffende obstacle
            obj_nr = self.current_player.rect.collidelist(self.map1.obstacles)
            self.model.current_character.move_side(self.map1.obstacles[obj_nr])
            self.current_player.rect.topleft = self.model.current_character.new_position

        # loop tegen de rand van een low obstacle aan, bijv water
        if len(self.current_player.rect.collidelistall(self.map1.low_obst)) == 1:
            obj_nr = self.current_player.rect.collidelist(self.map1.low_obst)
            self.model.current_character.move_side(self.map1.low_obst[obj_nr])
            self.current_player.rect.topleft = self.model.current_character.new_position

        # loop tegen een obstacle of low_obst aan
        while self.current_player.rect.collidelist(self.map1.obstacles) > -1 or \
                self.current_player.rect.collidelist(self.map1.low_obst) > -1:
            self.model.current_character.move_back()
            self.current_player.rect.topleft = self.model.current_character.new_position

class MapView(object):
    def __init__(self, map_width, map_height):
        self.grid = Grid(map_width, map_height, TILESIZE, GRIDLAYER)
        self.info = []
        self.current = None
        self.obstacles = []
        self.low_obst = []

class CharSprite(pygame.sprite.Sprite):
    # CharSprite extends the pygame.sprite.Sprite class
    def __init__(self, spritesheet):
        pygame.sprite.Sprite.__init__(self)

        self.west_states = {0:  (32, 32, 32, 32), 1: (0, 32, 32, 32), 2: (32, 32, 32, 32), 3: (64, 32, 32, 32)}
        self.east_states = {0:  (32, 64, 32, 32), 1: (0, 64, 32, 32), 2: (32, 64, 32, 32), 3: (64, 64, 32, 32)}
        self.north_states = {0: (32, 96, 32, 32), 1: (0, 96, 32, 32), 2: (32, 96, 32, 32), 3: (64, 96, 32, 32)}
        self.south_states = {0: (32,  0, 32, 32), 1: (0,  0, 32, 32), 2: (32,  0, 32, 32), 3: (64,  0, 32, 32)}

        # Assign the spritesheet to self.full_sprite
        self.full_sprite = pygame.image.load(spritesheet)
        # 'Clip' the sheet so that only one frame is displayed (the first frame of _south_states)
        self.full_sprite.set_clip(pygame.Rect(self.north_states[0]))

        # Create a rect to animate around the screen
        self.image = self.full_sprite.subsurface(self.full_sprite.get_clip())
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)

        self.updatespeed = 0
        self.step_count = 0
        self.step_animation = 0

    def update(self, event):

        if event.movespeed is None:
            if event.last_dir == Direction.North:
                self._clip(self.north_states[0])
            if event.last_dir == Direction.South:
                self._clip(self.south_states[0])
            if event.last_dir == Direction.West:
                self._clip(self.west_states[0])
            if event.last_dir == Direction.East:
                self._clip(self.east_states[0])
        else:
            if event.move_dir == Direction.North:
                self._clip(self.north_states)
            if event.move_dir == Direction.South:
                self._clip(self.south_states)
            if event.move_dir == Direction.West:
                self._clip(self.west_states)
            if event.move_dir == Direction.East:
                self._clip(self.east_states)

        # Update the image for each pass
        self.image = self.full_sprite.subsurface(self.full_sprite.get_clip())

    def _clip(self, clipped_rect):
        if type(clipped_rect) is dict:
            self.full_sprite.set_clip(pygame.Rect(self._get_frame(clipped_rect)))
        else:
            self.step_count = 0
            self.step_animation = 0
            self.full_sprite.set_clip(pygame.Rect(clipped_rect))
        return clipped_rect

    def _get_frame(self, frame_set):
        self.step_count += 1
        if self.step_count % (24 / self.updatespeed) == 1:  # 24 is deelbaar door alle movespeeds
            self.step_animation += 1
            if self.step_animation > 3:
                self.step_animation = 0
        return frame_set[self.step_animation]

class ButtonSprite(pygame.sprite.Sprite):
    def __init__(self, position, caption):
        pygame.sprite.Sprite.__init__(self)

        self.width = 40
        self.height = 40
        self._bgcolor = BLACK
        self._visible = True

        self.image = pygame.Surface((self.width, self.height))
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        self.rect.topleft = position

        self.font = pygame.font.SysFont('sans', 14)
        self.caption = self.font.render(caption, True, WHITE)
        self.caprect = self.caption.get_rect()
        self.caprect.center = self.rect.width / 2, self.rect.height / 2

        self._update()

    def draw(self, surface):
        if self._visible:
            surface.blit(self.image, self.rect.topleft)

    def _update(self):
        self.image.fill(self.bgcolor)
        pygame.draw.rect(self.image, WHITE, (0, 0, self.width, self.height), 1)
        self.image.blit(self.caption, self.caprect)

    @property
    def bgcolor(self):
        return self._bgcolor

    @bgcolor.setter
    def bgcolor(self, value):
        self._bgcolor = value
        self._update()

    @property
    def visible(self):
        return self._visible

    @visible.setter
    def visible(self, value):
        self._visible = value
        self._update()

class InfoSprite(pygame.sprite.Sprite):
    def __init__(self, rect, rect_type, layer):
        pygame.sprite.Sprite.__init__(self)

        self._layer = layer
        self.image = pygame.Surface((rect.width, rect.height))
        self.image.fill(BLACK)
        self.image.set_colorkey(BLACK)
        self.rect_type = rect_type
        pygame.draw.rect(self.image, self._color, (0, 0, rect.width, rect.height), 1)
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        self.rect.topleft = rect.topleft

    @property
    def _color(self):
        if self.rect_type == 'start':
            return GREEN
        if self.rect_type == 'hero':
            return BLUE
        if self.rect_type == 'tree':
            return PURPLE
        if self.rect_type == 'water':
            return BLUE

class Grid(pygame.sprite.Sprite):
    def __init__(self, map_width, map_height, tile_size, layer):
        pygame.sprite.Sprite.__init__(self)

        self._layer = layer
        self.image = pygame.Surface((map_width, map_height))
        self.image.fill(BLACK)
        self.image.set_colorkey(BLACK)
        for i in range(0, map_width, tile_size):
            pygame.draw.line(self.image, GRAY, (0, i), (map_width, i))
        for i in range(0, map_height, tile_size):
            pygame.draw.line(self.image, GRAY, (i, 0), (i, map_height))
        self.image = self.image.convert()
        self.rect = self.image.get_rect()

        self.show = False


import discord
import logging
from dotenv import load_dotenv
import os

load_dotenv()

KEY = os.getenv('DISCORD_KEY')

#logging set up
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    async def on_message(self, message):
        print('Message from {0.author}: {0.content}'.format(message))

client = MyClient()
client.run(f'{KEY}')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import numpy as np
import traceback

def get_spa_labels(path=""):
    """ 返回特征值和数字化的标签 0 dislike 1, little, 2 ok"""
    if not path:
        path = "data/kNndata1.txt"
    with open(path) as f:
        try:
            each_line = f.readlines()
            list_data_list = [x.split() for x in each_line]
            spa_data = np.array(list(map(lambda x: [float(y) for y in x[:3]], list_data_list)))
            labels = [x[-1] for x in list_data_list]
            # np_labels = np.array(labels)
            # num_labels = np.where(np_labels == 'didntLike', 0, np.where(np_labels == 'smallDoses', 1, 2))
            # 在只有1000行的情况下, 下述方法较快, 或许当数据量增大的时候会是第一个快些
            num_labels = np.array([1 if x == 'didntLike' else 2 if x == 'smallDoses' else 3 for x in labels])
            code, spa, labels = 0, spa_data, num_labels
        except:
            error = traceback.format_exc()
            code, spa, labels = 9, error, ""
    return code, spa, labels

def auto_norm(data_set):
    """将数据归一化"""
    """目前的数据格式为[[1,2,3], [2, 3,4]]"""
    assert isinstance(data_set, np.ndarray), "数据类型必须为numpy.ndarray"
    min = data_set.T.min(1)
    max = data_set.T.max(1)
    new_col = (data_set - min)/(max-min)
    return new_col, min, (max-min)

if __name__ == '__main__':
    tic = time.time()
    code, spa, labels = get_spa_labels()
    # test = np.array([[1., 2., 3.], [3., 4., 2.], [2., 3., 1.], [4., 3., 2.]])
    print(auto_norm(spa))
    toc = time.time()
    print(toc-tic)


import decimal
import re

import lxml.html

class Base(object):
    fetched = False

    def __init__(self, mal_id, mal):
        self.mal_id = mal_id
        self.mal = mal

    def _get_url(self):
        return self.base_url % self.mal_id

    def fetch(self):
        if not self.fetched:
            return self.mal._fetch(self)

    def parse(self, html):
        # Ignoring errors here because MAL allows users to use their own encodings
        # Without testing, probably allows the users to store pictures from their latest vacation as a review
        # Anyways, anything we need is (hopefully) in utf-8
        tree = lxml.html.fromstring(html.decode('utf-8', errors='ignore'))

        schema = tree.xpath('//div[@id="contentWrapper"]')[0]

        self.title = schema.xpath('.//span[@itemprop="name"]/text()')[0].strip()
        synopsis = schema.xpath('.//span[@itemprop="description"]/text()')
        if synopsis:
            self.synopsis = synopsis[0].strip()
        else:
            self.synopsis = ''
        self.cover = schema.xpath('.//img[@itemprop="image"]')[0]
        if 'data-src' in self.cover.attrib:
            self.cover = self.cover.attrib['data-src']
        else:
            self.cover = self.cover.attrib['src']

        self.info = info = {}
        self.alternative_titles = alternative_titles = {}
        self.statistics = statistics = {}
        self.related = related = {}
        self.reviews = []

        def duration2int(x):
            runtime = 0
            hours = re.findall(r'(\d+) hr', x)
            minutes = re.findall(r'(\d+) min', x)
            if hours:
                try:
                    runtime += int(hours[0])*60
                except ValueError:
                    pass

            if minutes:
                try:
                    runtime += int(minutes[0])
                except ValueError:
                    pass

            return runtime

        def num2int(x):
            try:
                return int(x.replace(',', ''))
            except ValueError:
                return None

        def num2dec(x):
            try:
                return decimal.Decimal(x)
            except decimal.InvalidOperation:
                return None

        strip2int = lambda x: x != 'N/A' and int(x.strip('#')) or None

        loop_elements = [
            ('Alternative Titles', True, [], alternative_titles, {}),
            ('Information', False, ['Producers', 'Genres', 'Authors', 'Serialization', 'Licensors', 'Studios'], info, {'Episodes': num2int, 'Duration': duration2int, 'Volumes': num2int, 'Chapters': num2int}),
            ('Statistics', False, [], statistics, {'Favorites': num2int, 'Members': num2int, 'Popularity': strip2int, 'Ranked': strip2int}),
        ]

        for block, splitlist, linklist, save_target, postprocess in loop_elements:
            for el in tree.xpath('//h2[text()="%s"]/following-sibling::*' % block):
                if el.tag != 'div' or not el.xpath('span') or ':' not in el.xpath('span/text()')[0]:
                    break

                text = ''.join(el.xpath('text()')).strip()
                info_type = el.xpath('span/text()')[0].strip(':')
                if info_type in linklist:
                    save_target[info_type] = []

                    if 'None found' not in text:
                        for a in el.xpath('a'):
                            save_target[info_type].append({
                                'id': int(re.findall('\d+', a.attrib['href'])[-1]),
                                'name': a.text
                            })
                elif info_type == 'Type':
                    save_target[info_type] = str(sorted(el.xpath('.//text()'), key=lambda x:len(x))[-1])
                elif info_type == 'Premiered':
                    premiered = el.xpath('./a/text()')[0].split(' ')
                    if premiered:
                        year = premiered[1]
                        try:
                            year = int(premiered[1])
                        except ValueError:
                            pass

                        save_target[info_type] = {
                            'season': premiered[0],
                            'year': year,
                        }
                else:
                    save_target[info_type] = text.strip()
                    if splitlist:
                        save_target[info_type] = map(lambda x:x.strip(), save_target[info_type].split(','))
                    elif info_type in postprocess:
                        save_target[info_type] = postprocess[info_type](save_target[info_type])

        score_box = tree.xpath('//div[./span[text()="Score:"]]/span')

        votes = tree.xpath('//span[@itemprop="ratingCount"]/text()')
        if votes:
            statistics['Votes'] = votes[0]
        else:
            statistics['Votes'] = score_box[2].xpath('./text()')[0]

        if 'Votes' in statistics:
            statistics['Votes'] = int(statistics['Votes'].replace(',', ''))

        score = tree.xpath('//span[@itemprop="ratingValue"]/text()')
        if score:
            statistics['Score'] = score[0]
        else:
            statistics['Score'] = score_box[1].xpath('./text()')[0]

        if 'Score' in statistics:
            statistics['Score'] = num2dec(statistics['Score'])

        found_h2 = False
        tags = iter(filter(lambda x:x, map(lambda x:x.strip(': ,'), tree.xpath('//h2[starts-with(text(), "Related ")]/../text()'))))
        current_tag = None

        for el in tree.xpath('//table[@class="anime_detail_related_anime"]/tr'):
            name, relationships = el.xpath('./td')
            name = name.text.strip(':')
            related[name] = []
            for r in relationships.xpath('./a'):
                url = r.attrib['href'].split('/')
                tag_type = url[1]
                tag_id = url[2]
                related[name].append({'type': tag_type, 'id': int(tag_id)})

        self.mal._handle_related(self)

        for review in tree.xpath('//h2[contains(text(), "Reviews")]/following-sibling::*//div[contains(@class, "borderLight")]'):
            rating = int(review.xpath('.//a[text()="Overall Rating"]/../text()')[0].strip(': '))
            review = ''.join(review.xpath('following-sibling::div/text()')).strip() + '\n'.join(review.xpath('following-sibling::div/span/text()')).strip()
            review = review.replace('\n\n', '\n')

            self.reviews.append({
                'rating': rating,
                'review': review
            })

        self.fetched = True