import os

import stanza

from tuw_nlp import logger
from tuw_nlp.grammar.text_to_4lang import TextTo4lang
from tuw_nlp.graph.lexical import LexGraphs
from tuw_nlp.graph.utils import graph_to_pn
from tuw_nlp.text.pipeline import CachedStanzaPipeline
from tuw_nlp.text.preprocessor import Preprocessor

class Featurizer:
    def __init__(self, cache_dir="cache", lang=None, preprocessor=None):
        self.feat_names = {}
        self.cache_dir = cache_dir
        self.lang = lang
        self.preproc = Preprocessor(preprocessor)

    def gen_events(self, iterable):
        raise NotImplementedError

    def get_feat_name(self, feat):
        return self.feat_names.get(feat, feat)

class TFLFeaturizer(Featurizer):
    def __init__(self, *args, **kwargs):
        super(TFLFeaturizer, self).__init__(*args, **kwargs)

        self.lexgraphs = LexGraphs()
        self.nlp_cache = os.path.join(self.cache_dir, "nlp_cache.json")

    def get_features(self, fl, tfl):
        features = set()
        for sg_tuple, sg in self.lexgraphs.gen_lex_subgraphs(fl, 2):
            features.add(sg_tuple)
            self.feat_names[sg_tuple] = graph_to_pn(sg)
        return list(features)

    def gen_events(self, iterable):
        with TextTo4lang(
            lang=self.lang, nlp_cache=self.nlp_cache, cache_dir=self.cache_dir
        ) as tfl:

            for raw_text, label in iterable:
                text = self.preproc(raw_text)
                features = []
                try:
                    for fl in tfl(text):
                        features += self.get_features(fl, tfl)
                except TypeError:
                    logger.error(f"tfl error on this text: {text}")

                yield features, label

class SimpleFeaturizer(Featurizer):
    def __init__(self, *args, **kwargs):
        super(SimpleFeaturizer, self).__init__(*args, **kwargs)
        self.nlp_cache = os.path.join(self.cache_dir, "nlp_cache.json")
        self.nlp_init = lambda: stanza.Pipeline(lang=self.lang)

    def get_features(self, raw_text, nlp):
        text = self.preproc(raw_text)
        doc = nlp(text)
        feats = set()
        for sen in doc.sentences:
            for tok in sen.words:
                # feats.append(f'{tok.lemma}_{tok.xpos}')
                if tok.upos not in ("NOUN", "PROPN"):
                    feats.add(tok.lemma)
                # feats.add(tok.text)

        return list(feats)

    def gen_events(self, iterable):
        with CachedStanzaPipeline(None, self.nlp_cache, init=self.nlp_init) as nlp:
            for raw_text, label in iterable:
                text = self.preproc(raw_text)
                features = self.get_features(text, nlp)
                yield features, label

def get_featurizer(method, cache_dir="cache", lang=None, preprocessor=None):
    if method == "fl":
        featurizer_cls = TFLFeaturizer
    elif method == "simple":
        featurizer_cls = SimpleFeaturizer

    return featurizer_cls(cache_dir=cache_dir, lang=lang, preprocessor=preprocessor)


bl_info = {
    "name": "T+ SymDim",
    "author": "mkbreuer",
    "version": (1, 1),
    "blender": (2, 7, 8),
    "location": "VIEW3D > Tool Shelf [T] or Property Shelf [N]",
    "description": "Addon for symmetrizing objects",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "ToolPlus"}

# LOAD UI #

from toolplus_symdim.sym_ui_panel       import (VIEW3D_TP_Symmetry_Panel_TOOLS)
from toolplus_symdim.sym_ui_panel       import (VIEW3D_TP_Symmetry_Panel_UI)

# LOAD ICONS #
from . icons.icons                  import load_icons
from . icons.icons                  import clear_icons

# LOAD OPERATORS #
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'toolplus_symdim'))

if "bpy" in locals():
    import imp
    imp.reload(sym_cut)
    imp.reload(sym_dim)
    imp.reload(sym_mods)

else:
    from . import sym_cut
    from . import sym_dim
    from . import sym_mods

# LOAD MODULS #

import bpy
from bpy import*
from bpy.props import*

import bpy.utils.previews
from bpy.types import AddonPreferences, PropertyGroup

# UI REGISTRY #

def update_panel_location(self, context):
    try:
        bpy.utils.unregister_class(VIEW3D_TP_Symmetry_Panel_UI)
        bpy.utils.unregister_class(VIEW3D_TP_Symmetry_Panel_TOOLS)

    except:
        pass

    try:
        bpy.utils.unregister_class(VIEW3D_TP_Symmetry_Panel_UI)
    except:
        pass

    if context.user_preferences.addons[__name__].preferences.tab_location == 'tools':

        VIEW3D_TP_Symmetry_Panel_TOOLS.bl_category = context.user_preferences.addons[__name__].preferences.tools_category

        bpy.utils.register_class(VIEW3D_TP_Symmetry_Panel_TOOLS)

    if context.user_preferences.addons[__name__].preferences.tab_location == 'ui':
        bpy.utils.register_class(VIEW3D_TP_Symmetry_Panel_UI)

    if context.user_preferences.addons[__name__].preferences.tab_location == 'off':
        pass

# ADDON PREFERENCES #
class TP_Panels_Preferences(AddonPreferences):
    bl_idname = __name__

    prefs_tabs = EnumProperty(
        items=(('info',       "Info",       "Info"),
               ('location',   "Location",   "Location"),
               ('url',        "URLs",       "URLs")),
               default='info')

    #Tab Location
    tab_location = EnumProperty(
        name = 'Panel Location',
        description = 'location switch',
        items=(('tools', 'Tool Shelf', 'place panel in the tool shelf [T]'),
               ('ui', 'Property Shelf', 'place panel in the property shelf [N]'),
               ('off', 'Off', 'on or off for panel in the shelfs')),
               default='tools', update = update_panel_location)

    tools_category = StringProperty(name = "TAB Category", description = "add name for a new category tab", default = 'T+', update = update_panel_location)

    # DRAW PREFERENCES #
    def draw(self, context):
        layout = self.layout

        #Info
        row= layout.row(align=True)
        row.prop(self, "prefs_tabs", expand=True)

        if self.prefs_tabs == 'info':

            box = layout.box().column(1)

            row = box.column(1)
            row.label(text="T+ SimDim!")
            row.label(text="Addon for symmetrizing objects (origin=pivot)")
            row.label(text="Functions:")
            row.label(text="1 > cut mesh and delete choosen side")
            row.label(text="2 > cut mesh and add mirror modifier")
            row.label(text="3 > cut mesh, add and apply mirror modifier continuous")
            row.label(text="4 > switch in and stay in editmode")
            row.label(text="5 > switch in and stay in sculptmode")
            row.label(text="6 > copy dimension from axis to axis")
            row.label(text="7 > delete both sides to get a profil loopcut")

            row.separator()

            row.label(text="Happy Blending! :)")

        #Location
        if self.prefs_tabs == 'location':

            box = layout.box().column(1)

            row = box.row(1)
            row.label("Location: Symmetry Panel")

            row = box.row(1)
            row.prop(self, 'tab_location', expand=True)

            box.separator()

            row = box.row(1)
            if self.tab_location == 'tools':

                box.separator()

                row.prop(self, "tools_category")

            box.separator()

            row = layout.row()
            row.label(text="! save user settings for a durably new panel location !", icon ="INFO")

            box.separator()

        #Weblinks
        if self.prefs_tabs == 'url':

            box = layout.box().column(1)

            row = box.column_flow(2)
            row.operator('wm.url_open', text = 'BlenderArtist', icon = 'BLENDER').url = "https://blenderartists.org/forum/showthread.php?427208-Addon-T-SymDim"
            row.operator('wm.url_open', text = 'GitHub', icon = 'SCRIPT').url = "https://github.com/mkbreuer/ToolPlus"

# PROPERTY GROUP #
class Dropdown_TP_Symmetry_Props(bpy.types.PropertyGroup):

    display_dim = bpy.props.BoolProperty(name="Open / Close", description="Open / Close", default=False)

# REGISTRY #

import traceback

def register():

    try: bpy.utils.register_module(__name__)
    except: traceback.print_exc()

    bpy.types.WindowManager.tp_collapse_menu_symmetry = bpy.props.PointerProperty(type = Dropdown_TP_Symmetry_Props)

    update_panel_location(None, bpy.context)

def unregister():

    try: bpy.utils.unregister_module(__name__)
    except: traceback.print_exc()

    del bpy.types.WindowManager.tp_collapse_menu_symmetry

if __name__ == "__main__":
    register()


import string

from absl import app
from iree.tf.support import tf_test_utils
import numpy as np
import tensorflow.compat.v2 as tf

class StringsModule(tf.Module):
  """A Module for converting a set of ids to the concatenated string."""

  def __init__(self):
    wordparts = [str(c) for c in string.printable]
    self.wordparts = tf.constant(wordparts, tf.string)

  @tf.function(input_signature=[
      tf.TensorSpec((None, None), dtype=tf.int32),
  ])
  def print_ids(self, ids):
    string_tensor = tf.strings.as_string(ids)
    tf.print(string_tensor)

  @tf.function(input_signature=[
      tf.TensorSpec((None,), dtype=tf.int32),
      tf.TensorSpec((None,), dtype=tf.int32),
  ])
  def gather(self, string_values, indices):
    tf.print(tf.gather(tf.as_string(string_values), indices))

#  @tf.function(input_signature=[
#      tf.TensorSpec((None, None), dtype=tf.int32),
#  ])
#  def strings_to_ids(self, ids):
#    wps = tf.gather(self.wordparts, ids)
#    return tf.strings.reduce_join(wps, 1)

class StringsTest(tf_test_utils.TracedModuleTestCase):

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self._modules = tf_test_utils.compile_tf_module(StringsModule)

  def test_print_ids(self):

    def print_ids(module):
      input_ids = np.asarray([[1, 2, 3, 4, 5, 6], [10, 11, 12, 14, 15, 16]],
                             dtype=np.int32)
      module.print_ids(input_ids)

    self.compare_backends(print_ids, self._modules)

  def test_gather(self):

    def gather(module):
      string_values = np.asarray([ord(c) for c in string.printable],
                                 dtype=np.int32)
      input_indices = np.asarray([12, 10, 29, 21, 10, 34], dtype=np.int32)
      module.gather(string_values, input_indices)

    self.compare_backends(gather, self._modules)

#  def test_strings_to_ids(self):
#
#    def strings_to_ids(module):
#      input_ids = np.asarray(
#          [[12, 10, 29, 28, 94, 15, 24, 27, 94, 25, 21, 10, 34],
#           [13, 24, 16, 28, 94, 15, 24, 27, 94, 28, 29, 10, 34]])
#      module.strings_to_ids(input_ids)
#
#    self.compare_backends(strings_to_ids, self._modules)

def main(argv):
  del argv  # Unused
  if hasattr(tf, 'enable_v2_behavior'):
    tf.enable_v2_behavior()
  tf.test.main()

if __name__ == '__main__':
  app.run(main)


from utility.menus import Menu
from utility.util import FileSelector, MenuResponses, BetterFont
import utility.constants as constants
import pygame
from SHKFileParser import parse

class SHKScript:
    def __init__(self, pathToScript: str):
        # if this raises an error thats your fault not mine
        with open(pathToScript, "r") as f:
            self.script = f.read()

        self.pyhk = parse(self.script)

    def execute(self):
        exec(self.pyhk)  # this can create UB and has security flaws, but its easiest way to do it.

class ExecuteMenu(Menu):
    def __init__(self, display: pygame.Surface, fpsClock: pygame.time.Clock, fps, res) -> None:
        super().__init__(display, fpsClock, fps, res)

        # path to the script for execution
        self.scriptPath = ""
        self.executeFileSelector = FileSelector((20, 200), 900, constants.white, constants.white, 125, constants.whiteGray, constants.white, "SHK File: ", ButtonWidth=3, rounding=5)

        # other
        self.FONT_TopText = BetterFont(constants.white, 50, "./assets/CourierPrimeCode-Regular.ttf")

    def run(self) -> MenuResponses:  # noqa:line_sep
        while True:
            self.fpsClock.tick(self.gameFPS)

            events = pygame.event.get()  # so that we can hook into other event handlers if needed.
            # event hooking
            self.executeFileSelector.handle_events(events)
            # event handling
            for event in events:
                if event.type == pygame.QUIT:
                    return MenuResponses.QUIT

            # logic

            # rendering
            self.screen.fill(constants.black)
            self.executeFileSelector.render_to(self.screen)

            self.FONT_TopText.render_to(self.screen, (self.FONT_TopText.get_center(self.screen, "SHK Execution Menu", x=True).x, 40), "SHK Execution Menu2")

            pygame.display.flip()


import pkg_resources
import pandas as pd
from tqdm import tqdm
from symspellpy import SymSpell
from typing import Optional
from sklearn.base import BaseEstimator, TransformerMixin
from .._types import validate_text_input
tqdm.pandas()

class SpellingPreprocessor(BaseEstimator, TransformerMixin):
    """Spelling preprocessor using SymSpell.

    Parameters
    ----------
    speller : str, default='sentence'
        Which algorithms to use for correction. Possible algorithms: ['sentence', 'noisy_sentence'].

    max_edit_distance : int, default=1
        The maximum edit distance between input andsuggested words.

    word_counts_file : Optional[str], default=None
        Load word count file from filesystem. Default uses from Turkish TurPy word counts file.

    ignore_non_words : bool, default=False
        A flag to determine whether numbers and acronyms are left alone during the spell checking process.

    transfer_casing : bool, default=False
        A flag to determine whether the casing --- i.e., uppercase vs lowercase --- should be carried over from `phrase`.

    split_by_space : bool, default=False
        Splits the phrase into words simply based on space.

    ignore_term_with_digits : bool, default=False
        A flag to determine whether any term with digits is left alone during the spell checking process. Only works when ``ignore_non_words` is also ``True``.

    max_segmentation_word_length : Optional[int], default=None
        The maximum word length that should be considered.

    ignore_token : Optional[str], default=None
        A regex pattern describing what words/phrases to ignore and leave unchanged.
    """

    def __init__(self,
                 speller: str = "sentence",
                 max_edit_distance: int = 1,
                 word_counts_file: Optional[str] = None,

                 # Sentence
                 ignore_non_words: bool = False,
                 transfer_casing: bool = False,
                 split_by_space: bool = False,
                 ignore_term_with_digits: bool = False,

                 # Noisy Sentence
                 max_segmentation_word_length: Optional[int] = None,
                 ignore_token: Optional[str] = None,
                 ):

        self.speller = speller
        self.max_edit_distance = max_edit_distance
        self.word_counts_file = word_counts_file

        self.ignore_non_words = ignore_non_words
        self.transfer_casing = transfer_casing
        self.split_by_space = split_by_space
        self.ignore_term_with_digits = ignore_term_with_digits

        self.max_segmentation_word_length = max_segmentation_word_length
        self.ignore_token = ignore_token

        if self.speller not in ["sentence", "noisy_sentence"]:
            raise ValueError("Speller can only be one of the ['sentence', 'noisy_sentence']")

        self.sym_spell = SymSpell()

        if self.word_counts_file:
            self.sym_spell.load_dictionary(self.word_counts_file, 0, 1, encoding="utf-8")
        else:
            path = pkg_resources.resource_filename('turpy', 'resources/word_count.txt')
            self.sym_spell.load_dictionary(path, 0, 1, encoding="utf-8")

    def _correct_spelling(self, text: str):
        """Correct a string"""
        if self.speller == "sentence":
            suggestions = self.sym_spell.lookup_compound(text, max_edit_distance=self.max_edit_distance, ignore_non_words=self.ignore_non_words,
                                                         transfer_casing=self.transfer_casing, split_by_space=self.split_by_space,
                                                         ignore_term_with_digits=self.ignore_term_with_digits)
            corrected = suggestions[0].term

        elif self.speller == "noisy_sentence":
            suggestions = self.sym_spell.word_segmentation(text, max_edit_distance=self.max_edit_distance,
                                                           max_segmentation_word_length=self.max_segmentation_word_length, ignore_token=self.ignore_token)
            corrected = suggestions[0]

        # elif self.speller == "word":
        #    suggestions = self.sym_spell.lookup(text)
        #    corrected = suggestions[0].term

        return corrected

    def fit(self, X: pd.Series, y=None):
        """Does nothing. Exist for compatibility reasons for sklearn pipelines."""
        return self

    def transform(self, X: pd.Series, y=None):
        """Preprocess text from given text series.

        Parameters
        ----------
        X : pd.Series
            Pandas text series containing texts.

        y : Optional[pd.Series]
            Ignored.

        Returns
        -------
        X : pd.Series
            Preprocessed text series.
        """
        validate_text_input(X)
        X = X.progress_apply(self._correct_spelling)

        return X


# -*- coding: utf-8 -*-
__author__ = 'Vit'
from bs4 import BeautifulSoup

from common.util import _iter, quotes
from data_format.url import URL
from interface.view_manager_interface import ViewManagerFromModelInterface
from model.site.picture.base_of_tgp import TgpSite

class BravoeroticaSite(TgpSite):
    @staticmethod
    def can_accept_url(url: URL) -> bool:
        return url.contain('bravoerotica.com/')

    @staticmethod
    def create_start_button(view:ViewManagerFromModelInterface):
        menu_items=dict(Movies=URL('http://www.bravoerotica.com/erotic-tube/latest-updates/'),
                        Pics=URL('http://www.bravoerotica.com/'),
                        # LatestUpdates=URL('http://www.tomorrowporn.com/latest_updates.html'),
                    )

        view.add_start_button(picture_filename='model/site/resource/picture/bravoerotica.png',
                              menu_items=menu_items,
                              url=URL("http://www.bravoerotica.com/"))

    def get_shrink_name(self):
        return 'BE '

    def parse_thumbs_tags(self, soup: BeautifulSoup, url: URL):
        tags_containers = _iter(soup.find_all('li', {'class': 'dropdown'}))
        for tags_container in tags_containers:
            for tag in tags_container.find_all('a', {'href': lambda s: '/st/niches/' in s}):
                # psp(tag.prettify())
                self.add_tag(str(tag.string).strip(), URL(tag.attrs['href'], base_url=url))

    def get_pagination_container(self, soup: BeautifulSoup)->BeautifulSoup:
        return soup.find('div', {'class': ['pages','pg']})

    # def parse_thumb_title(self, soup: BeautifulSoup, url: URL) -> str:
    #     return 'BE '+ url.get().partition('bravoerotica.com/')[2].rpartition('.')[0]

    def parse_video_thumbs(self, soup: BeautifulSoup, url: URL):
        thumbs_containers = _iter(soup.find_all('div', {'class': 'video_list'}))
        for thumbs_container in thumbs_containers:
            for thumbnail in _iter(thumbs_container.find_all('a', {'class': ['item']})):
                href = URL(thumbnail.attrs['href'], base_url=url)
                description = thumbnail.img.attrs['alt']
                thumb_url = URL(thumbnail.img.attrs['src'], base_url=url)

                duration = thumbnail.find('span', {'class': "duration"})
                dur_time = '' if duration is None else str(duration.span.string)

                self.add_thumb(thumb_url=thumb_url, href=href, popup=description,
                                           labels=[{'text': dur_time, 'align': 'top right'},
                                                   {'text': description, 'align': 'bottom center'}])

    def parse_video(self, soup: BeautifulSoup, url: URL):
        content = soup.find('div', {'class': 'player'})
        if content is not None:
            script = content.find('script', text=lambda x: 'flashvars =' in str(x))
            if script is not None:
                data = str(script.string).replace(' ', '')
                file = quotes(data, "video_url:'", "'")
                self.add_video('DEFAULT', URL(file, base_url=url))

    def get_picture_tag_containers(self, soup:BeautifulSoup)->list:
        crumbles=soup.find('div',{'class':'crumbles'})
        return crumbles.find_all('h1') if crumbles else []

if __name__ == "__main__":
    pass


import requests

from csv import reader
from sqlalchemy.exc import OperationalError, IntegrityError

from .config import OUI_FILES
from tools.ntnmodels import OUI_MAL, OUI_MAM, OUI_MAS, OUI_CID, OUI_IAB
from tools.ntndb import db_session, init_db

def clear_table(table_name):

    try:

        Session = db_session()

        if table_name == 'OUI_MAL':

            Session.query(OUI_MAL).delete()

        elif table_name == 'OUI_MAM':

            Session.query(OUI_MAM).delete()

        elif table_name == 'OUI_MAS':

            Session.query(OUI_MAS).delete()

        elif table_name == 'OUI_CID':

            Session.query(OUI_CID).delete()

        elif table_name == 'OUI_IAB':

            Session.query(OUI_IAB).delete()

        Session.commit()

    # Tables do not exist, we initiate them
    except OperationalError:

        init_db()

def update_table(oui_csv, table_name):

    clear_table(table_name)

    Session = db_session()

    next(oui_csv)

    for row in oui_csv:

        if table_name == 'OUI_MAL':

            Session.add(OUI_MAL(row[1], row[2], row[3]))

        elif table_name == 'OUI_MAM':

            Session.add(OUI_MAM(row[1], row[2], row[3]))

        elif table_name == 'OUI_MAS':

            Session.add(OUI_MAS(row[1], row[2], row[3]))

        elif table_name == 'OUI_CID':

            Session.add(OUI_CID(row[1], row[2], row[3]))

        elif table_name == 'OUI_IAB':

            Session.add(OUI_IAB(row[1], row[2], row[3]))

    Session.commit()

def download_oui_lists():

    for oui_file in OUI_FILES:

        response = requests.get(oui_file['url'])

        if response.status_code == 200:

            print(f"Rebuilding Table {oui_file['table_name']}")

            oui_csv = reader(response.text.splitlines())

            update_table(oui_csv, oui_file['table_name'])

if __name__ == "__main__":

    download_oui_lists()


import cv2
src=cv2.imread('./data/img.png',cv2.IMREAD_COLOR);
#이미지 변경 방법1 = 절대 크기로 변경
dst = cv2.resize(src,dsize=(640,480),interpolation=cv2.INTER_LINEAR)
#이미지 변경 방법2 = 비율에 맞게 상대 크기로 변경
dst2 = cv2.resize(src,dsize=(0,0),fx=0.3,fy=0.7,interpolation=cv2.INTER_LINEAR)
#dst2 = cv2.resize(src,절대크기,상대크기(fx),상대크기(y),보간법)

cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.imshow('dst2',dst2)
cv2.waitKey()
cv2.destroyAllWindows()


import discord
from discord.ext import commands
from buttons import Buttonify
from config import token

bot = commands.Bot(command_prefix="?", intents = discord.Intents.all())

@bot.event
async def on_ready():
    print("im ready oky")

# Example to use Buttonify Function:
@bot.command()
async def test(ctx):
    # Creating a custom red button with label "idk",  emoji "ðŸš€", and disabled set to True
    red_disabled_button_view = Buttonify(label="idk", emoji="ðŸš€", disabled=True, style="red_button")
    # Creating a custom green button with label "meow", emoji "ðŸš€", and disabled set to False (default)
    green_button_view = Buttonify(label="meow", emoji="ðŸš€", style="green_button")
    # Creating a custom primary (gray) button with label "hello", emoji "ðŸ‘‹", and disabled set to False (default)
    primary_button_view = Buttonify(label="hello", emoji="ðŸ‘‹", style="primary_button")
    # Creating a custom secondary (gray) button with label "bye", emoji "ðŸ‘‹", and disabled set to False (default)
    secondary_button_view = Buttonify(label="bye", emoji="ðŸ‘‹", style="secondary_button")
    await ctx.send("Here are the buttons:", view=red_disabled_button_view)
    await ctx.send("Here's another button:", view=green_button_view)
    await ctx.send("Here's a primary button:", view=primary_button_view)
    await ctx.send("Here's a secondary button:", view=secondary_button_view)
# Example ends!

bot.run(token)


import numpy as np

# try to import cupy
try:
    import cupy as xp

except (ImportError, ModuleNotFoundError) as e:
    import numpy as xp

# Cython imports
from pyinterp_cpu import interpolate_arrays_wrap as interpolate_arrays_wrap_cpu
from pyinterp_cpu import get_waveform_wrap as get_waveform_wrap_cpu

# Python imports
from few.utils.baseclasses import SummationBase, SchwarzschildEccentric
from few.utils.citations import *

# Attempt Cython imports of GPU functions
try:
    from pyinterp import interpolate_arrays_wrap, get_waveform_wrap

except (ImportError, ModuleNotFoundError) as e:
    pass

class CubicSplineInterpolant:
    """GPU-accelerated Multiple Cubic Splines

    This class produces multiple cubic splines on a GPU. It has a CPU option
    as well. The cubic splines are produced with "not-a-knot" boundary
    conditions.

    This class can be run out of Python similar to
    `scipy.interpolate.CubicSpline <https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.CubicSpline.html#scipy-interpolate-cubicspline>`_.
    However, the most efficient way to use this method is in a customized
    cuda kernel. See the
    `source code for the interpolated summation<https://github.com/BlackHolePerturbationToolkit/FastEMRIWaveforms/blob/master/src/interpolate.cu>`_
    in cuda for an example of this.

    This class can be run on GPUs and CPUs.

    args:
        t (1D double xp.ndarray): t values as input for the spline.
        y_all (2D double xp.ndarray): y values for the spline.
            Shape: (ninterps, length).
        use_gpu (bool, optional): If True, prepare arrays for a GPU. Default is
            False.

    """

    def __init__(self, t, y_all, use_gpu=False):

        # set gpu usage and interpolation function based on gpu usage
        if use_gpu:
            self.xp = xp
            self.interpolate_arrays = interpolate_arrays_wrap

        else:
            self.xp = np
            self.interpolate_arrays = interpolate_arrays_wrap_cpu

        # hardcoded,only cubic spline is available
        self.degree = 3

        # get quantities related to how many interpolations
        ninterps, length = y_all.shape

        # store this for reshaping flattened arrays
        self.reshape_shape = (self.degree + 1, ninterps, length)

        # interp array is (y, c1, c2, c3)
        interp_array = self.xp.zeros(self.reshape_shape)

        # fill y
        interp_array[0] = y_all

        interp_array = interp_array.flatten()

        # arrays to store banded matrix and solution
        B = self.xp.zeros((ninterps * length,))
        upper_diag = self.xp.zeros_like(B)
        diag = self.xp.zeros_like(B)
        lower_diag = self.xp.zeros_like(B)

        # perform interpolation
        self.interpolate_arrays(
            t, interp_array, ninterps, length, B, upper_diag, diag, lower_diag
        )

        # set up storage of necessary arrays
        self.t = t
        self.interp_array = self.xp.transpose(
            interp_array.reshape(self.reshape_shape), [0, 2, 1]
        ).flatten()

        # update reshape_shape
        self.reshape_shape = (self.degree + 1, length, ninterps)

    def attributes_CubicSplineInterpolate(self):
        """
        attributes:
            interpolate_arrays (func): CPU or GPU function for mode interpolation.
            interp_array (1D double xp.ndarray): Array containing all spline
                coefficients. It is flattened after fitting from shape
                (4, length, ninterps). The 4 is the 4 spline coefficients.

        """

    @property
    def citation(self):
        """Return the citation for this class"""
        return few_citation + few_software_citation

    @property
    def y(self):
        """y values associated with the spline"""
        return self.interp_array.reshape(self.reshape_shape)[0].T

    @property
    def c1(self):
        """constants for the linear term"""
        return self.interp_array.reshape(self.reshape_shape)[1].T

    @property
    def c2(self):
        """constants for the quadratic term"""
        return self.interp_array.reshape(self.reshape_shape)[2].T

    @property
    def c3(self):
        """constants for the cubic term"""
        return self.interp_array.reshape(self.reshape_shape)[3].T

    def __call__(self, tnew):
        """Evaluation function for the spline

        Put in an array of new t values at which all interpolants will be
        evaluated.

        args:
            tnew (1D double xp.ndarray): Array of new t values. All of these new
                t values must be within the bounds of the input t values,
                including the beginning t and **excluding** the ending t.

        raises:
            ValueError: a new t value is not in the bounds of the input t array.

        """

        # find were in the old t array the new t values split
        inds = self.xp.searchsorted(self.t, tnew, side="right") - 1

        if np.any(inds < 0) or np.any(inds >= len(self.t)):
            raise ValueError(
                "New t array outside bounds of input t array. This is not allowed."
            )

        x = tnew - self.t[inds]
        x2 = x * x
        x3 = x2 * x

        out = (
            self.y[:, inds]
            + self.c1[:, inds] * x
            + self.c2[:, inds] * x2
            + self.c3[:, inds] * x3
        )
        return out

    def d1(self, tnew):
        inds = self.xp.searchsorted(self.t, tnew)

        x = tnew - self.t[inds]
        x2 = x * x

        out = (
            self.c1[:, inds] + 2.0 * self.c2[:, inds] * x + 3.0 * self.c3[:, inds] * x2
        )
        return out

    def d2(self, tnew):
        inds = self.xp.searchsorted(self.t, tnew)

        x = tnew - self.t[inds]

        out = 2.0 * self.c2[:, inds] * x + 6.0 * self.c3[:, inds] * x
        return out

    def d3(self, tnew):
        inds = self.xp.searchsorted(self.t, tnew)
        out = 6.0 * self.c3[:, inds]
        return out

class InterpolatedModeSum(SummationBase, SchwarzschildEccentric):
    """Create waveform by interpolating sparse trajectory.

    It interpolates all of the modes of interest and phases at sparse
    trajectories. Within the summation phase, the values are calculated using
    the interpolants and summed.

    This class can be run on GPUs and CPUs.

    """

    def __init__(self, *args, **kwargs):

        SchwarzschildEccentric.__init__(self, *args, **kwargs)
        SummationBase.__init__(self, *args, **kwargs)

        self.kwargs = kwargs

        if self.use_gpu:
            self.xp = xp
            self.get_waveform = get_waveform_wrap

        else:
            self.xp = np
            self.get_waveform = get_waveform_wrap_cpu

    def attributes_InterpolatedModeSum(self):
        """
        attributes:
            get_waveform (func): CPU or GPU function for waveform creation.

        """

    @property
    def citation(self):
        return few_citation + few_software_citation

    def sum(
        self,
        t,
        teuk_modes,
        ylms,
        Phi_phi,
        Phi_r,
        m_arr,
        n_arr,
        *args,
        dt=10.0,
        **kwargs
    ):
        """Interpolated summation function.

        This function interpolates the amplitude and phase information, and
        creates the final waveform with the combination of ylm values for each
        mode.

        args:
            t (1D double xp.ndarray): Array of t values.
            teuk_modes (2D double xp.array): Array of complex amplitudes.
                Shape: (len(t), num_teuk_modes).
            ylms (1D complex128 xp.ndarray): Array of ylm values for each mode,
                including m<0. Shape is (num of m==0,) + (num of m>0,)
                + (num of m<0). Number of m<0 and m>0 is the same, but they are
                ordered as (m==0 first then) m>0 then m<0.
            Phi_phi (1D double xp.ndarray): Array of azimuthal phase values
                (:math:`\Phi_\phi`).
            Phi_r (1D double xp.ndarray): Array of radial phase values
                 (:math:`\Phi_r`).
            m_arr (1D int xp.ndarray): :math:`m` values associated with each mode.
            n_arr (1D int xp.ndarray): :math:`n` values associated with each mode.
            *args (list, placeholder): Added for future flexibility.
            dt (double, optional): Time spacing between observations (inverse of sampling
                rate). Default is 10.0.
            **kwargs (dict, placeholder): Added for future flexibility.

        """

        init_len = len(t)
        num_teuk_modes = teuk_modes.shape[1]
        num_pts = self.num_pts

        length = init_len
        ninterps = self.ndim + 2 * num_teuk_modes  # 2 for re and im
        y_all = self.xp.zeros((ninterps, length))

        y_all[:num_teuk_modes] = teuk_modes.T.real
        y_all[num_teuk_modes : 2 * num_teuk_modes] = teuk_modes.T.imag

        y_all[-2] = Phi_phi
        y_all[-1] = Phi_r

        spline = CubicSplineInterpolant(t, y_all, use_gpu=self.use_gpu)

        try:
            h_t = t.get()
        except:
            h_t = t

        # the base class function __call__ will return the waveform
        self.get_waveform(
            self.waveform,
            spline.interp_array,
            m_arr,
            n_arr,
            init_len,
            num_pts,
            num_teuk_modes,
            ylms,
            dt,
            h_t,
        )


import numpy as np

class Bingo:
    def __init__(self):
        self.boards = None
        self.marks = None
        self.win_board_idx = None
        self.bingo_loser_array = None
        self.numbers = None

    def load_bingo_file(self,filename):
        with open(filename) as f:
            # Get bingo numbers on first line
            self.numbers = np.array(list(map(int,f.readline().split(','))))
            # Read bingo boards on remaining lines
            board_string = f.read()
        board_list = board_string[1:-1].split('\n\n') # Split each board into string, separated by double \n
        # Convert board_list to 3d int array
        num_boards = len(board_list)
        self.boards = np.zeros((num_boards,5,5))
        for board_idx,board in enumerate(board_list):
            for line_idx,line in enumerate(board.split('\n')):
                line_vals = np.array(list(map(int,line.split())))
                self.boards[board_idx,line_idx,:] = line_vals

    def play(self, win_condition):
        self.marks = np.zeros_like(self.boards,dtype=bool)
        for bingo_number in self.numbers:
            self.update_boards(bingo_number)
            if self.check_bingo_win(win_condition):
               idx = self.win_board_idx
               winning_board_marks = self.marks[idx]
               winning_board = self.boards[idx]
               # Return final score: unmarked numbers * number just called
               return int(np.sum(winning_board[~winning_board_marks])*bingo_number)

    def check_bingo_win(self,win_condition):

        def board_win(board_marks):
             col_vals = np.all(board_marks,axis=0) # column streaks
             row_vals = np.all(board_marks,axis=1) # row streaks
             return (np.any(col_vals) or np.any(row_vals))
        # PART 1 WIN CONDTION
        if win_condition == 'first':
            for board_idx, board_marks in enumerate(self.marks):
                if board_win(board_marks):
                    self.win_board_idx = board_idx
                    return True
        # PART 2 WIN CONDITION
        if win_condition == 'last':
            # Wait for when last remaining player wins
            if np.sum(self.bingo_loser_array) == 1:
                if board_win(self.marks[self.bingo_loser_array,:,:]):
                    # Get number of losing player
                    self.win_board_idx = int(np.argwhere(self.bingo_loser_array))
                    return True
            else:
                # Return True for all the players who havent won
                self.bingo_loser_array = [not(board_win(m)) for m in self.marks]
                return False

    def update_boards(self,number):
        # Add locations that match number to prexisting marks
        self.marks = self.marks | (self.boards == number)
        return  self.marks

#numbers, boards = load_bingo_file('../data/bingo')
bingo = Bingo()
bingo.load_bingo_file('../data/bingo')
# PART 1
print(bingo.play('first'))
# PART 2
print(bingo.play('last'))


#!/usr/bin/env python
"""Puray - a Pure Python Raytracer by Arun Ravindran, 2019"""
import argparse
import importlib
import os
from multiprocessing import cpu_count

from engine import RenderEngine
from scene import Scene

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("scene", help="Path to scene file (without .py extension)")
    parser.add_argument(
        "-p",
        "--processes",
        action="store",
        type=int,
        dest="processes",
        default=0,
        help="Number of processes (0=auto)",
    )
    args = parser.parse_args()
    if args.processes == 0:
        process_count = cpu_count()
    else:
        process_count = args.processes

    mod = importlib.import_module(args.scene)
    scene = Scene(mod.CAMERA, mod.OBJECTS, mod.LIGHTS, mod.WIDTH, mod.HEIGHT)
    engine = RenderEngine()

    os.chdir(os.path.dirname(os.path.abspath(mod.__file__)))
    with open(mod.RENDERED_IMG, "w") as img_fileobj:
        engine.render_multiprocess(scene, process_count, img_fileobj)

if __name__ == "__main__":
    main()


"""
Дан массив размера N. Вставить элемент с нулевым значением перед
минимальным и после максимального элемента массива.
"""

n = int(input("Введите размер N массива: "))
array = [float(input(f"Введите {c} элемент массива: ")) for c, _ in enumerate(range(n), 1)]
array.insert(array.index(min(array)), 0)
array.insert(array.index(max(array)), 0)

print(f"Изменённый массив: {' '.join([str(i) for i in array])}")

Задание 19/3.py


# coding: utf-8
from multiprocessing.managers import BaseManager
from multiprocessing import Process,freeze_support,Queue
import time
from distribute_spider.ControllerNode.URLManager import URLManager
from distribute_spider.ControllerNode.DataOutput import DataOutput

class NodeManager(object):
    def start_manager(self, url_q, result_q):
        self.url_q = url_q
        self.result_q = result_q
        def get_task():
            return self.url_q
        def get_result():
            return self.result_q
        BaseManager.register('get_task_queue', callable=get_task)
        BaseManager.register('get_result_queue', callable=get_result)
        manager = BaseManager(address=('127.0.0.1', 8001), authkey=b'baike')
        return manager

    # 将url通过url_q传递到爬虫节点，将conn_q中的url存储到url管理进程
    def url_manager_proc(self, url_q, conn_q, root_url):
        url_manager = URLManager()
        url_manager.add_new_url(root_url)
        while True:
            while url_manager.has_new_urls():
                # 从url管理器获取新的url
                new_url = url_manager.get_new_url()
                # 将新url发送到工作节点
                url_q.put(new_url)
                print('The total number of used urls is %s' % url_manager.used_url_size())
                # 爬取url数量超过2000关闭进程，保存进度
                if url_manager.used_url_size() > 2000:
                    url_q.put('end')
                    print('控制节点发起结束通知...')
                    # 关闭管理节点，并存储set状态
                    url_manager.save_progress('new_urls.txt', url_manager.new_urls)
                    url_manager.save_progress('used_urls.txt', url_manager.used_urls)
                    return
            # 从conn_q获取url添加到url管理进程
            try:
                if not conn_q.empty():
                    urls = conn_q.get()
                    url_manager.add_new_urls(urls)
            except BaseException:
                time.sleep(0.1)

    # 结果分析进程
    def result_solve_proc(self, result_q, conn_q, store_q):
        while True:
            try:
                if not result_q.empty():
                    content = result_q.get(True)
                    print('获取到返回数据：%s' % content)
                    if content['new_urls'] == 'end':
                        print('结果分析进程结束')
                        store_q.put('end')
                        return
                    # 将结果返回的urls set传递到conn_q，将返回的数据传递到store_q
                    conn_q.put(content['new_urls'])
                    store_q.put(content['data'])
                else:
                    time.sleep(0.1)
            except BaseException:
                time.sleep(0.1)

    def store_proc(self, store_q):
        output = DataOutput()
        while True:
            if not store_q.empty():
                data = store_q.get()
                if data == 'end':
                    print('存储进程结束')
                    output.output_end(output.filepath)
                    return
                output.store_data(data)
            else:
                time.sleep(0.1)

if __name__ == '__main__':
    # 初始化4个消息队列
    url_q = Queue()
    result_q = Queue()
    conn_q = Queue()
    store_q = Queue()
    # 创建分布式管理器
    node = NodeManager()
    freeze_support()
    manager = node.start_manager(url_q, result_q)
    # 创建url管理进程、数据提取进程和数据存储进程
    url_manager_proc = Process(target=node.url_manager_proc, args=(url_q, conn_q, 'https://baike.baidu.com/item/%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB'),)
    result_solve_proc = Process(target=node.result_solve_proc, args=(result_q, conn_q, store_q,))
    store_proc = Process(target=node.store_proc, args=(store_q,))
    # 启动进程
    url_manager_proc.start()
    result_solve_proc.start()
    store_proc.start()
    manager.get_server().serve_forever()


from json import load
from time import sleep
import driver

def get_config():
    """Read configuration file"""

    try:
        with open('default.json', 'r') as file:
            data = load(file)

    except FileNotFoundError:
        with open('config.json', 'r') as file:
            data = load(file)

    return data

def init_tabs(website, tab_amount):
    """Opens tabs according to tab amount set in config.json"""

    for _ in range(tab_amount):
        website.new_tab()

def open_links(website, tab_amount):
    """Open the YouTube link in each tab"""

    for tab in range(tab_amount):
        # Open links
        website.switch_tab(tab)
        website.get_vid()

def play_video(website, tab_amount):
    """Click on the play button"""

    for tab in range(tab_amount):
        # Play the video on each tab
        website.switch_tab(tab)
        website.play_video()

def refresh_all(website, tab_amount):
    """Refresh all tabs"""

    for tab in range(tab_amount):
        # Refresh all tabs
        website.switch_tab(tab)
        website.refresh()

def main():
    """Main Function"""

    print('Initilization')
    config = get_config()

    website = driver.Bot(config['website'], config['browser'])

    print('Opening new tabs')
    init_tabs(website, config['tab_amount'])

    print('Open links')
    open_links(website, config['tab_amount'])

    print('Cycle start')
    print('Playing videos')
    play_video(website, config['tab_amount'])

    for i in range(config['view_cycles']):

        # Cycle the amount of refreshes
        sleep(config['watch_time']) # Watch the video for n amount of times

        print('Refreshing all tabs')
        refresh_all(website, config['tab_amount'])

        print('Clearing cache')
        website.clear_cache() # Clear cache and site cookies
        print(f"Run {i+1}/{config['view_cycles']} complete")

    print('Complete')

if __name__ == '__main__':
    main()


import unittest
import numpy as np
from lsst.sims.catUtils.dust import EBV
import lsst.utils.tests
from lsst.sims.utils.CodeUtilities import sims_clean_up

class TestDustMap(unittest.TestCase):

    @classmethod
    def tearDownClass(cls):
        sims_clean_up()

    def testCreate(self):
        """ Test that we can create the dustmap"""

        # Test that we can load without error
        dustmap = EBV.EBVbase()
        dustmap.load_ebvMapNorth()
        dustmap.load_ebvMapSouth()

        # Test the interpolation
        ra = np.array([0., 0., np.radians(30.)])
        dec = np.array([0., np.radians(30.), np.radians(-30.)])

        ebvMap = dustmap.calculateEbv(equatorialCoordinates=np.array([ra, dec]),
                                      interp=False)
        assert(np.size(ebvMap) == np.size(ra))

class TestMemory(lsst.utils.tests.MemoryTestCase):
    pass

def setup_module(module):
    lsst.utils.tests.init()

if __name__ == "__main__":
    lsst.utils.tests.init()
    unittest.main()


case1 = [[-20, 15], [-14, -5], [-18, -13], [-5, -3]]  # 2

from collections import deque

def solution(routes):
    camera = []
    routes.sort(key=lambda x: (x[1], x[0]))  # 가장 빨리 나가는 순서
    routes = deque(routes)

    route = routes.popleft()
    camera.append(route[1])  # 가장 빨리 나가는 차량의 종착점에 카메라 추가
    while routes:
        # 시작점이 카메라보다 더 뒤에 있다면
        while routes[0][0] <= camera[-1] <= routes[0][1]:
            routes.popleft()
            if not routes: break
        if not routes: break
        # 시작점이 카메라 앞에 있다면
        if routes[0][0] > camera[-1]:
            route = routes.popleft()
            camera.append(route[1])

    return len(camera)

print(solution(case1))

그리디(단속카메라).py


from scapy.all import TCP, IP, ICMP, UDP, ARP
import os
from string import printable as printable_chars
import re

def dbg(*msg):
    if 'DEBUG' in os.environ:
        print(*msg)

# https://gist.github.com/MarkBaggett/d8933453f431c111169158ce7f4e2222
# scapy helper for session analysis
def full_duplex(p):
    sess = "Other"
    if 'Ether' in p:
        if 'IP' in p:
            if 'TCP' in p:
                sess = str(sorted(["TCP", p[IP].src, p[TCP].sport, p[IP].dst, p[TCP].dport],key=str))
            elif 'UDP' in p:
                sess = str(sorted(["UDP", p[IP].src, p[UDP].sport, p[IP].dst, p[UDP].dport] ,key=str))
            elif 'ICMP' in p:
                sess = str(sorted(["ICMP", p[IP].src, p[IP].dst, p[ICMP].code, p[ICMP].type, p[ICMP].id] ,key=str))
            else:
                sess = str(sorted(["IP", p[IP].src, p[IP].dst, p[IP].proto] ,key=str))
        elif 'ARP' in p:
            sess = str(sorted(["ARP", p[ARP].psrc, p[ARP].pdst],key=str))
        else:
            sess = p.sprintf("Ethernet type=%04xr,Ether.type%")
    return sess

def printable(input):
    return ''.join([chr(x) if chr(x) in printable_chars else '.' for x in input])

def is_printable(input):
    return all(chr(x) in printable_chars for x in input)

class ConfigMixin:
    def get_config(self, name, default=None):
        if not hasattr(self, 'config'):
            raise Exception("No config")
        if name in self.config:
            return self.config[name]
        return default

def scrubbed_equals(text, other):
    flag_p = re.compile(b'FLAG{[0-9A-F]+}')
    text = text.strip()
    other = other.strip()
    text = flag_p.sub(b'FLAG{XXX}', text)
    other = flag_p.sub(b'FLAG{XXX}', other)
    return text == other


import sys
import scipy.io as sio
from pprint import pprint
import numpy as np
from sklearn.naive_bayes import GaussianNB

train_mat_fname = "data_train.mat"
train_mat_dict = sio.loadmat(train_mat_fname)
data_train = train_mat_dict['data_train']

X = data_train[:, [0, 1, 2, 3, 4]]
Y = []
for label in data_train[:, [5]]:
	Y.append(label[0])

clf = GaussianNB()
clf.fit(X, Y)
#Variances
# pprint(clf.sigma_)

test_mat_fname = "data_test.mat"
test_mat_dict = sio.loadmat(test_mat_fname)
data_test = test_mat_dict['data_test']

X_test = data_train[:, [0, 1, 2, 3, 4]]
Y_test_label = []
data_test_length = 0
for label in data_test[:, [5]]:
	Y_test_label.append(label[0])
	data_test_length += 1

Y_pred = clf.predict(X_test)
counter = 0
for i in range(0, data_test_length):
	if Y_pred[i] == Y_test_label[i]:
		counter += 1

print("Accuracy: %", (100 * counter/float(data_test_length)))


# coding=utf8
import logging
import sys

from mercury_base import EventBus, Meter, Meters, SerialDataTransport, TcpDataTransport, check_crc, hex_str
from mercury_proxy.api import Api
from socket import socket
from simple_socket_server import SimpleSocketServer

with_api = False

if __name__ == '__main__':
    meters_events = EventBus()
    meters = Meters(meters_events)

    socket_server = SimpleSocketServer()

    if with_api:
        api = Api(meters)
        logger = api.logger
    else:
        api = None
        logger = logging.getLogger(__name__)

    log_handler = logging.StreamHandler(sys.stderr)
    log_handler.setFormatter(
        logging.Formatter("[%(asctime)s] %(levelname)s\t%(message)s")
    )
    logger.handlers = [log_handler]

    logger.setLevel(logging.DEBUG)
    # logger.setLevel(logging.INFO)

    @meters_events.on('connect')
    def on_connect(meter: Meter):
        logger.info('Meter M%s with serial number %s is connected', meter.model, meter.serial_number)

    @meters_events.on('request')
    def on_request(meter: Meter, package: bytes):
        logger.debug('[proxy] --> [%s]\t%s', meter.serial_number or 'new meter', hex_str(package, ' '))

    @meters_events.on('answer')
    def on_answer(meter: Meter, package: bytes):
        logger.debug('[proxy] <-- [%s]\t%s', meter.serial_number or 'new meter', hex_str(package, ' '))

    @socket_server.on('start')
    def on_start(ip: str, port: int):
        logger.info('Proxy server is listening on %s:%s', ip, port)

    @socket_server.on('connect')
    def on_connect(_sock, peer):
        logger.debug('New connection from %s:%s', *peer)

    @socket_server.on('disconnect')
    def on_disconnect(_sock, peer):
        logger.debug('Connection from %s:%s is closed', *peer)

    @socket_server.on('message')
    def on_message(sock: socket, peer, message: bytes):
        if bytearray(message)[:1].isalpha():
            logger.warning('It looks like the socket request is HTTP')
            raise ConnectionResetError()
        logger.debug('[%s:%s] --> [proxy]\t%s', *peer, hex_str(message, ' '))
        if not check_crc(message):
            logger.warning('Package from %s:%s has wrong checksum', *peer)
            return
        meter = meters.find_by_package(message)
        if meter:
            answer = meter.send_package(message)
            if answer:
                logger.debug('[%s:%s] <-- [proxy]\t%s', *peer, hex_str(answer, ' '))
                socket_server.send(sock, answer)

    meters.connect_meter(40680048, TcpDataTransport('192.168.0.2', 5051))
    # meters.connect_meter(37793503, SerialDataTransport('/dev/ttyACM0'))

    if api:
        api.run(port=5054)
    socket_server.run(port=5053)


#!/usr/bin/env python3

from collections import Counter
import os
from sys import argv

# =========================
# "ARGS"
# =========================
NAME1 = "/media/array/pregnancy/data/HYPTENSPREG.gwas.imputed_v3.female.tsv"
NAME1 = argv[1]
NAME2 = "/media/array/pregnancy/data/finngen_R5_O15_HYPTENSPREG"
NAME2 = argv[2]
# directories for output
OUT_DIR = "/media/array/pregnancy/data"
OUT_DIR = argv[3]

# path to metal
MTAG_PATH = "/home/achangalidi/projects/pregnancy/mtag/mtag.py"

# =======================================================
# ADDITIONAL FILES and SETTINGS
# =======================================================
# Prepared summstats files for metal
FILE1_FOR_META = f'{OUT_DIR}/filtered_{NAME1.split("/")[-1]}_.tsv'
FILE2_FOR_META = f'{OUT_DIR}/filtered_{NAME2.split("/")[-1]}_.tsv'

# =======================================================
# STEP 2: Launching MTAG
# =======================================================

# запускаем метал
os.system(f'{MTAG_PATH} --sumstats  {FILE1_FOR_META},{FILE2_FOR_META} \
            --out {OUT_DIR}/ \
          --snp_name rsid \
          --beta_name beta \
          --se_name se \
          --p_name pval \
          --eaf_name maf \
          --chr_name chr \
          --bpos_name pos \
          --a1_name ref \
          --a2_name alt \
          --n_name n_sample \
          --z_name z_score \
          --force \
          --perfect_gencov \
          --equal_h2 \
          --no_overlap')

print('\nMETAL done!')


from aiogram import types, Dispatcher
from config import bot, group_id
from database.async_database import Database

async def is_admin(user_id: types.Message):
    member = await bot.get_chat_member(chat_id=group_id, user_id=user_id)
    return True if member['status'] in ['creator', 'admin'] else False

async def start_command_mentors(message: types.Message):
    if message.chat.type == types.chat.ChatType.PRIVATE:
        admin = await is_admin(user_id=message.from_user.id)
        if admin:
            mentor_list = await Database().select_all_users()
            month1 = ['Для 1 месяца:']
            month2 = ['Для 2 месяца:']
            month3 = ['Для 3 месяца:']
            month4 = ['Для 4 месяца:']
            month5 = ['Для 5 месяца:']
            data = ['*Список менторов:*']
            for mentor in mentor_list:
                if 6 > mentor.month > 1:
                    if not mentor.username:
                        month1.append(f'[{mentor.first_name}](tg://user?id={mentor.telegram_id})')
                    else:
                        month1.append(f'[{mentor.username}](tg://user?id={mentor.telegram_id})')
                if 6 > mentor.month > 2:
                    if not mentor.username:
                        month2.append(f'[{mentor.first_name}](tg://user?id={mentor.telegram_id})')
                    else:
                        month2.append(f'[{mentor.username}](tg://user?id={mentor.telegram_id})')
                if 6 > mentor.month > 3:
                    if not mentor.username:
                        month3.append(f'[{mentor.first_name}](tg://user?id={mentor.telegram_id})')
                    else:
                        month3.append(f'[{mentor.username}](tg://user?id={mentor.telegram_id})')
                if 6 > mentor.month > 4:
                    if not mentor.username:
                        month4.append(f'[{mentor.first_name}](tg://user?id={mentor.telegram_id})')
                    else:
                        month4.append(f'[{mentor.username}](tg://user?id={mentor.telegram_id})')
                if mentor.month > 5:
                    if not mentor.username:
                        month5.append(f'[{mentor.first_name}](tg://user?id={mentor.telegram_id})')
                    else:
                        month5.append(f'[{mentor.username}](tg://user?id={mentor.telegram_id})')

            for month in (month1, month2, month3, month4, month5):
                if len(month) > 1: data += month
            data = '\n'.join(data)
            await message.reply(
                text=data,
                parse_mode=types.ParseMode.MARKDOWN)

        else:
            await bot.send_message(
                text='Эта команда только для администраторов группы.',
                chat_id=message.chat.id)
    else:
        await message.reply(
            text='Перейдите по ссылке [@GEEKS_BOT](https://t.me/geek_backend_mentor_bot) и используйте команду /mentors',
            parse_mode=types.ParseMode.MARKDOWN, )

def register_mentors_handlers(dp: Dispatcher):
    dp.register_message_handler(start_command_mentors, commands=['mentors'])


import random

min   = int(32000)
max   = int(2147483647)
count = int(64)

array = list()
for x in range(count):
    randval = random.randint(min, max)
    if not randval in array:
        array.append(randval);
        print(f'{randval}, ', end="")


###################
import pygame, sys, os, math
from pygame.locals import *

class Player():
    def __init__(self, player_settings, room_settings, screen, camera_settings):
        #Player images (the 2's are not used because we did not figure out how to animate the player)
        self.down1 = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/player','player_down1.png')), (player_settings.player_width*room_settings.magnification, player_settings.player_width*room_settings.magnification))
        self.down2 = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/player','player_down2.png')), (player_settings.player_width*room_settings.magnification, player_settings.player_width*room_settings.magnification))
        self.up1 = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/player','player_up1.png')), (player_settings.player_width*room_settings.magnification, player_settings.player_width*room_settings.magnification))
        self.up2 = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/player','player_up2.png')), (player_settings.player_width*room_settings.magnification, player_settings.player_width*room_settings.magnification))
        self.left1 = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/player','player_left1.png')), (player_settings.player_width*room_settings.magnification, player_settings.player_width*room_settings.magnification))
        self.left2 = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/player','player_left2.png')), (player_settings.player_width*room_settings.magnification, player_settings.player_width*room_settings.magnification))
        self.right1 = pygame.transform.flip(self.left1, True, False)
        self.right2 = pygame.transform.flip(self.left2, True, False)
        self.image = self.down1
        #Gets rect's
        self.rect =  self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.screen = screen
        self.moving_up = False
        self.moving_down = False
        self.moving_left = False
        self.moving_right = False
        #location and dimentions of player
        self.height = player_settings.player_height*room_settings.magnification
        self.width = player_settings.player_width*room_settings.magnification
        self.x = ((room_settings.screen_width/2 + self.width/2 - camera_settings.camerax)/room_settings.screen_tile)
        self.y = ((room_settings.screen_height/2 + self.height/2 - camera_settings.cameray)/room_settings.screen_tile)
        self.screen_rect.right = (room_settings.screen_width/2 + self.width/2)
        self.screen_rect.bottom = (room_settings.screen_height/2 - self.height/2)
        self.rect.right = self.screen_rect.right
        self.rect.bottom = self.screen_rect.centery

    def update(self, player_settings):
        #sets the image for the direction of the player
        if self.moving_left:
            # self.rect.centerx -= player_settings.p_speed_factor
            self.image = self.left1
        elif self.moving_right:
            # self.rect.centerx += player_settings.p_speed_factor
            self.image = self.right1
        elif self.moving_down:
            # self.rect.bottom += player_settings.p_speed_factor
            self.image = self.down1
        elif self.moving_up:
            # self.rect.bottom -= player_settings.p_speed_factor
            self.image = self.up1
    def blitme(self, room_settings, camera_settings):
        #blits player and updates position
        self.rect.right = (room_settings.screen_width/2 + self.width/2)
        self.rect.bottom = (room_settings.screen_height/2 + self.height/2)
        self.x = ((room_settings.screen_width/2 + self.width/2 - camera_settings.camerax)/room_settings.screen_tile)
        self.y = ((room_settings.screen_height/2 + self.height/2 - camera_settings.cameray)/room_settings.screen_tile)
        self.screen.blit(self.image, self.rect)

class RoomSettings():
    def __init__(self):
        #All the dimentions of things on the screen and the screen
        self.room_width = 256
        self.room_height = 176
        self.magnification = 4
        self.screen_width = int(self.room_width * self.magnification)
        self.screen_height = int(self.room_height * self.magnification)
        self.tile_size = 16
        self.room_tile_width = int(self.room_width / self.tile_size)
        self.room_tile_height = int(self.room_height / self.tile_size)
        self.bg_color = (0,0,0)
        self.screen_tile = int(self.tile_size * self.magnification)
        self.tile_per_roomx = 16
        self.tile_per_roomy = 11
        self.mp = "world.map"

class PlayerSettings():
    def __init__(self, room_settings):
        #Player stats
        self.player_width = 16
        self.player_height = 16
        self.p_speed_factor = room_settings.magnification * 75
        self.p_max_health = 100
        self.p_health = self.p_max_health
        self.p_atk = 5

class Tiles():
    def __init__(self, room_settings):
        self.x = ["1", "1b", "1p", "1r", "1y", "2", "2b", "2bl", "2br", "2ibl", "2ibr", "2itl", "2itr", "2l", "2pd", "2pl", "2pr", "2pu", "2r", "2t", "2tl", "2tr", "3", "3b", "3bl", "3br", "3c", "3cb", "3cbl", "3cbr", "3cibl", "3cibr", "3citl", "3citr", "3cl", "3cr", "3ct", "3ctl", "3ctr", "3ibl", "3ibr", "3itl", "3itr", "3l", "3r", "3t", "3tl", "3tr", "4", "4bl", "4br", "4t", "4tl", "4tr", "5", "5bl", "5blr", "5br", "5h", "5tbl", "5tbr", "5tl", "5tlr", "5tr", "5v", "5x", "6d", "6h", "6u", "6v", "7", "7bl", "7br", "7t", "7tl", "7tr", "8", "8d", "8l", "8r", "8t", "8tb", "8tbl", "8tbr", "8tl", "8tr", "8w"]
        self.texture_tags = {}
        self.utexture_tags = {}
        for g in self.x:
            self.texture_tags[g] = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/map',g+'.png')), (room_settings.screen_tile, room_settings.screen_tile))
            self.utexture_tags[g] = pygame.image.load(os.path.join('finalProject/models/map',g+'.png'))

        #Blocked and speed tiles for each map
        self.blockeda = []
        self.blockedb = []
        self.speeda = []
        self.speedb = []

        #Types of tile that are blocked and speed
        self.blocked_types = ["2", "2b", "2bl", "2br", "2l", "2r", "2t", "2tl", "2tr", "3cibl", "3cibr", "3citl", "3citr", "4bl", "4br", "4t", "4tl", "4tr", "4x", "5", "7", "7bl", "7br", "7t", "7tl", "7tr", "8", "8l", "8r"]
        self.speed_types = ["5bl", "5blr", "5br", "5h", "5tbl", "5tbr", "5tl", "5tlr", "5tr", "5v", "5x"]

        #Sky background
        self.sky = pygame.transform.scale(pygame.image.load(os.path.join('finalProject/models/map', 'sky.png')), (room_settings.screen_width, room_settings.screen_height))
        self.Sky = pygame.Surface(self.sky.get_size(), pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.SRCALPHA)
        self.Sky.blit(self.sky, (0,0))
        del self.sky
        self.size = room_settings.screen_tile
    # def loadtexture(self, file, room_settings):
    #     bitmap = file
    #     surface = pygame.Surface((room_settings.screen_tile, room_settings.screen_tile), pygame.HWSURFACE|pygame.SRCALPHA|pygame.DOUBLEBUF)
    #     surface.blit(bitmap, (0,0))
    #     return surface

    #Checks if tile is blocked or speed
    def blocked_at(self, pos, room_settings):
        if room_settings.mp == "world.map":
            if list(pos) in self.blockeda:
                return True
            else:
                return False
        elif room_settings.mp == "cave.map":
            if list(pos) in self.blockedb:
                return True
            else:
                return False
    def speed_at(self, pos, room_settings):
        if room_settings.mp == "world.map":
            if list(pos) in self.speeda:
                return True
            else:
                return False
        elif room_settings.mp == "cave.map":
            if list(pos) in self.speedb:
                return True
            else:
                return False

class CameraSettings():
    def __init__(self):
        #pos and speed vars
        self.spd = 1
        self.camerax = 0
        self.cameray = 0
        self.moving_up = False
        self.moving_down = False
        self.moving_left = False
        self.moving_right = False
    def update(self, deltatime, room_settings, player_settings, tiles, p):
        #Moves the player based on speed, FPS, blocked tiles, and speed tiles
        if tiles.speed_at((math.floor(p.x-.5), math.floor(p.y-.5)), room_settings):
            self.spd = 1.5
        else:
            self.spd = 1
        if self.moving_left:
            if not tiles.blocked_at((math.floor(p.x-1), math.floor(p.y-.5)), room_settings):
                self.camerax += deltatime * player_settings.p_speed_factor * self.spd
        elif self.moving_right:
            if not tiles.blocked_at((math.floor(p.x), math.floor(p.y-.5)), room_settings):
                self.camerax -= deltatime * player_settings.p_speed_factor * self.spd
        elif self.moving_down:
            if not tiles.blocked_at((math.floor(p.x-.5), math.floor(p.y)), room_settings):
                self.cameray -= deltatime * player_settings.p_speed_factor * self.spd
        elif self.moving_up:
            if not tiles.blocked_at((math.floor(p.x-.5), math.floor(p.y-1)), room_settings):
                self.cameray += deltatime * player_settings.p_speed_factor * self.spd


from pkg_resources import resource_filename, resource_string
from functools import partial
from pathlib import Path

import yaml
from jinja2 import Template

VuePath = resource_filename("vue", "")
IndexTemplate = resource_string("vuecli", "index.html")
StaticContents = {
    "/loading.gif": resource_string("vuecli", "loading.gif"),
    "/vuepy.js": b"\n".join(
        [
            resource_string("brython", "data/brython.js"),
            resource_string("brython", "data/brython_stdlib.js"),
        ]
    ),
    "/vue.js": resource_string("vuecli", "js/vue.js"),
    "/vuex.js": resource_string("vuecli", "js/vuex.js"),
    "/vue-router.js": resource_string("vuecli", "js/vue-router.js"),
}

class Provider:
    Arguments = {}

    def __init__(self, path=None):
        self.path = Path(path if path else ".")
        self.config = self.load_config()

    @staticmethod
    def _normalize_config(config):
        default_scripts = {
            "vuepy": "vuepy.js",
            "vue": "vue.js",
            "vuex": "vuex.js",
            "vue-router": "vue-router.js",
        }
        scripts = {"vuepy": True, "vue": True}
        custom_scripts = config.get("scripts", {})
        if isinstance(custom_scripts, list):
            custom_scripts = {k: k for k in custom_scripts}
        scripts.update(custom_scripts)
        config["scripts"] = {
            k: default_scripts[k] if v is True else v for k, v in scripts.items() if v
        }

    def load_config(self):
        config_file = Path(self.path, "vuepy.yml")
        config = {}
        if config_file.exists():
            with open(config_file, "r") as fh:
                config = yaml.safe_load(fh.read()) or config
        self._normalize_config(config)
        return config

    def render_index(self):
        brython_args = self.config.get("brython_args", {})
        if brython_args:
            joined = ", ".join(f"{k}: {v}" for k, v in brython_args.items())
            brython_args = f"{{ {joined} }}"
        else:
            brython_args = ""

        return Template(IndexTemplate.decode("utf-8")).render(
            stylesheets=self.config.get("stylesheets", []),
            scripts=self.config.get("scripts", {}),
            templates={
                id_: Path(self.path, template).read_text("utf-8")
                for id_, template in self.config.get("templates", {}).items()
            },
            brython_args=brython_args,
        )

    def setup(self):
        self.directory("application", "/", Path(self.path), deep=True)
        self.directory("vuepy", "/vue", VuePath, deep=True)

        entry_point = self.config.get("entry_point", "app")
        self.content(
            "entry_point", "/__entry_point__.py", lambda: f"import {entry_point}\n"
        )
        self.content("index", "/", lambda: self.render_index())
        for route in StaticContents:
            self.content(route, route, partial(StaticContents.get, route))

    def content(self, endpoint, route, content):
        raise NotImplementedError()

    def directory(self, endpoint, route, path, deep=False):
        raise NotImplementedError()

    def deploy(self, **kwargs):
        raise NotImplementedError()


import csv
import json
import math
import random
import shutil
import sys
import tempfile
from argparse import ArgumentParser
from pathlib import Path
from subprocess import Popen
from typing import List

import cv2 as cv
import json5
import numpy as np

def parse_args():
    parser = ArgumentParser()
    parser.add_argument("OUTPUT_DIR")
    parser.add_argument("-s", "--piece_image_size", type=int, default=64)
    return parser.parse_args()

def check_piecemaker_output(input_dir: Path, image_size: int) -> bool:
    size_100_dir = input_dir / "size-100"
    pieces_path = size_100_dir / "pieces.json"

    with open(pieces_path, "r") as fp:
        piece_names = list(json.load(fp).keys())

    raster_dir = size_100_dir / "raster"

    for pid in piece_names:
        image_path = raster_dir / "{}.png".format(int(pid))
        image = cv.imread(str(image_path), cv.IMREAD_UNCHANGED)
        h, w, _ = image.shape
        if h >= image_size or w >= image_size:
            return False

    return True

def main():
    args = parse_args()
    image_size = args.piece_image_size
    output_dir = Path(args.OUTPUT_DIR)
    output_dir.mkdir()

    with tempfile.TemporaryDirectory() as input_dir:
        input_dir = Path(input_dir)

        # Generate a puzzle using `piecemaker`
        background_size = image_size * 3
        white_image = np.full([background_size, background_size, 3], 255)
        white_image_path = input_dir / "white.png"

        is_successful = False
        for _ in range(10):
            cv.imwrite(str(white_image_path), white_image)
            proc = Popen(
                [
                    "piecemaker",
                    "--dir",
                    str(input_dir),
                    "-n",
                    "25",
                    str(white_image_path),
                ]
            )
            proc.wait(50)

            if check_piecemaker_output(input_dir, image_size):
                is_successful = True
                break
            else:
                print("Some pieces are too large. Retrying...", file=sys.stderr)
                shutil.rmtree(input_dir)
                input_dir.mkdir()

        if not is_successful:
            print("Unable to run `piecemaker`. Is it installed?", file=sys.stderr)
            return 1

        size_100_dir = input_dir / "size-100"

        # Load data generated from piecemaker
        pieces_path = size_100_dir / "pieces.json"
        with open(pieces_path, "r") as fp:
            pieces = list(json.load(fp).items())
            pieces.sort(key=lambda args: int(args[0]))

        in_adjacent_path = input_dir / "adjacent.json"
        with open(in_adjacent_path, "r") as fp:
            adjacent = json.load(fp)

        # Write label.csv
        def bbox_center(bbox: List[int]):
            x1, y1, x2, y2 = bbox
            w = x2 - x1
            h = y2 - y1
            cx = x1 + w / 2
            cy = y1 + h / 2
            rotation = 0.0
            center = [cx, cy, rotation]
            return center

        centers = (bbox_center(bbox) for _, bbox in pieces)
        label_path = output_dir / "label.csv"
        with open(label_path, "w") as fp:
            writer = csv.writer(fp)

            for center in centers:
                writer.writerow(center)

        # Write adjacent.csv
        out_adjacent_path = output_dir / "adjacent.json5"
        with open(out_adjacent_path, "w") as fp:
            json5.dump(adjacent, fp)

        # Write images
        raster_dir = size_100_dir / "raster"
        for pid, _ in pieces:
            in_image_path = raster_dir / "{}.png".format(int(pid))
            orig_image = cv.imread(str(in_image_path), cv.IMREAD_UNCHANGED)
            new_image = process_piece_image(orig_image, image_size)
            out_image_path = output_dir / "{:02}.png".format(int(pid))
            cv.imwrite(str(out_image_path), new_image)

        # Generate the reassembled puzzle image
        reassembled_image = reassemble_puzzle(pieces, output_dir, image_size * 5)
        reassemble_image_path = output_dir / "reassemble.png"
        cv.imwrite(str(reassemble_image_path), reassembled_image)

def process_piece_image(orig_image, image_size: int):
    sh, sw, sc = orig_image.shape
    assert sc == 4
    pad_t = (image_size - sh) // 2
    pad_b = image_size - sh - pad_t
    pad_l = (image_size - sw) // 2
    pad_r = image_size - sw - pad_l

    image = cv.copyMakeBorder(
        orig_image,
        pad_t,
        pad_b,
        pad_l,
        pad_r,
        cv.BORDER_CONSTANT,
        None,
        [0, 0, 0, 0],
    )

    image = cv.cvtColor(image, cv.COLOR_BGRA2GRAY)
    return image

def rotate_image(image, angle):
    image_center = tuple(np.array(image.shape[1::-1]) / 2)
    rot_mat = cv.getRotationMatrix2D(image_center, angle, 1.0)
    result = cv.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv.INTER_LINEAR)
    return result

def reassemble_puzzle(pieces, output_dir: Path, output_size: int):
    # Load piece images
    def load_image(pid):
        image_path = output_dir / "{:02}.png".format(int(pid))
        return cv.imread(str(image_path))

    images = list(load_image(pid) for pid, bbox in pieces)

    # Load center coordinates
    label_path = output_dir / "label.csv"
    with open(label_path, "r") as fp:
        centers = list(list(float(val) for val in params) for params in csv.reader(fp))

    canvas = np.full([output_size, output_size, 3], 0, dtype=np.uint8)

    for image, center in zip(images, centers):
        cx, cy, angle = center
        image = rotate_image(image, angle)
        mask = image != 0

        h, w, _ = image.shape

        y1 = int(math.floor(cy - h / 2))
        y2 = y1 + h
        y3 = max(y1, 0)
        y4 = min(y2, output_size)

        x1 = int(math.floor(cx - w / 2))
        x2 = x1 + w
        x3 = max(x1, 0)
        x4 = min(x2, output_size)

        sub_canvas = canvas[y3:y4, x3:x4]
        sub_mask = mask[(y3 - y1) : (y4 - y1), (x3 - x1) : (x4 - x1)]
        sub_canvas[sub_mask] = random.randint(128, 255)

    return canvas


import pandas as pd
import math
import numpy as np

class MDLP:
    """
    This class find the cut points to discretize a numerical feature using the Fayyad and Irani approach (MDL principle).

    In thi class the constructor allows to specify the minimum possible size for a group (min_groupSize).
    This parameter is interpret as an hard constraint: only cut points that produce buckets larger than
    min_groupSize will be returned.
    Another parameter that is possible to set is "force". If this parameter is setted to True the findCutPoints
    method will retur at list one cut point. This unless even a cut point can be found which produces two subgroups
    with a size greater than min_groupSize and with class partition entropy less than the entropy of the entire set.

    """
    def __init__(self, min_groupsize = 1, force=False):
        """
        :param min_groupsize: int
        :param force: boolean
        """
        self.min_groupsize = min_groupsize
        self.force = force

    def findCutPoints(self, x, y):
        """
        :param x: list, numpy array or pandas Series that contains the values of the numeric feature to discretize
        :param y: list, numpy array or pandas Series that contains binary values (0 or 1) representing the class label

        :return: list of (ascending) ordered cut points
            if the function returns the cut points [c1, c2, ..., cn], with c1<c2<...<cn, the feature can be discretized
            by creating n+1 buckets: (-infinite, c1], (c1, c2], ..., (cn-1, cn], (cn, +infinite)
        """
        df = pd.DataFrame({'x':x, 'y':y})
        total_size = df.shape[0]

        df=df.groupby('x')['y'].agg(['sum','count'])
        df.reset_index(inplace=True)
        total_sum=df['sum'].sum()
        df['prop'] = df['sum'] / df['count']

        cut_points = self.find_partitions(df, total_size, total_sum, self.force)
        cut_points.sort()
        return cut_points

    def find_partitions(self, df, total_size, total_sum, force=False):
        """
        This is a private class function. It works in a recursive manner.

        Parameters
        ----------
        df: pandas.DataFrame
            this dataframe contains, for each distinct values of the feature to discretize, the number of positive
            instances (called sum), the total number of instances (count) and the proportion
            positive_instances/total_num_of_instances (prop). See the findCutPoints method for details.
        total_size: int
            represent the total number of instances of the x feature in the current partition.
        total_sum: int
            represent the total number of positive instances of the x feature in the current partition.
        force: boolean
            force the method to return at list one cut point, exept for some exceptional cases described in the class description.

        Returns
        -------
        list of int:
            list of cut points
        """

        sum = 0
        count = 0
        min_cpe = total_size  # Class Partition Entropy (CPE)
        partition_index = 0
        partition_x = 0
        partition_sum = 0
        partition_count = 0

        #find best candidate cut point
        for i in range(0, df.shape[0] - 1):
            loc = df.iloc[i]
            sum += loc['sum']
            count += loc['count']
            if loc['prop'] == df.iloc[i + 1]['prop'] or count < self.min_groupsize or (
                    total_size - count) < self.min_groupsize:
                continue

            # cakculate CPE cut point
            pc1s0 = sum / count  # probability of class 1 in subgroup 0
            pc0s0 = 1 - pc1s0
            pc1s0_ = pc1s0 if pc1s0 != 0 else 1
            pc0s0_ = pc0s0 if pc0s0 != 0 else 1
            entS0 = -(pc1s0 * math.log2(pc1s0_) + pc0s0 * math.log2(pc0s0_))

            pc1s1 = (total_sum - sum) / (total_size - count)
            pc0s1 = 1 - pc1s1
            pc1s1_ = pc1s1 if pc1s1 != 0 else 1
            pc0s1_ = pc0s1 if pc0s1 != 0 else 1
            entS1 = -(pc1s1 * math.log2(pc1s1_) + pc0s1 * math.log2(pc0s1_))

            cpe = (count / total_size) * entS0 + ((total_size - count) / total_size) * entS1

            if cpe < min_cpe:
                min_cpe = cpe
                partition_index = i
                partition_x = loc['x']
                partition_sum = sum
                partition_count = count
                partition_entS0 = entS0
                partition_entS1 = entS1
        if min_cpe == total_size:
            return []

        #test MDLP condition
        pc1 = total_sum / total_size
        pc0 = 1 - pc1
        pc1_ = pc1 if pc1 != 0 else 1
        pc0_ = pc0 if pc0 != 0 else 1
        entS = -(pc1 * math.log2(pc1_) + pc0 * math.log2(pc0_))
        gain = entS - min_cpe

        remained_pos = total_sum - partition_sum
        total_rem = total_size - partition_count
        c0 = 2 if (partition_sum == 0 or partition_sum == partition_count) else 1
        c1 = 2 if (remained_pos == 0 or remained_pos == total_rem) else 1
        delta = math.log2(9) - c0 * partition_entS0 - c1 * partition_entS1

        delta = math.log2(7) -( 2*entS -c0 * partition_entS0 - c1 * partition_entS1)

        if (gain <= ((math.log2(total_size - 1) + delta) / total_size)):
            if force:
                return [partition_x]
            return []

        #recoursive splitting
        left_partitions = self.find_partitions(df.iloc[:(partition_index+1)], partition_count, partition_sum)
        right_partitions= self.find_partitions(df.iloc[(partition_index+1):], (total_size-partition_count), (total_sum-partition_sum))
        a= [partition_x]+ left_partitions + right_partitions
        return a

class EqualFrequency:
    """
    This class find the cut points to discretize a numerical feature using an approximate equal frequency discretization.
    """
    def __init__(self, min_bin_size=1, num_bins=0):
        """
        Parameters
        __________
        num_bins : int
            this number is to interpret as the maximum number of bins that will be generated.
            If this parameter is not specified (0 by deafault), it will be automatically determined
            based on min_bin_size parameter.
        min_bin_size : int
            Represent the minimum size that a bin can have.

        Notes
        -----
        If the number of bins has to be automatically determined, this will be chosen so that each bin has an average
        size of 1.2 * min_bin_size. Let's call this number automatic_nbin
        ( automatic_nbin = int(x.size/(self.min_group_size*1.2)) )
        If instead the bin number is specified in the constructor (num_bins parameter), the number of bins that will
        actually be generated will be, at most, equal to the minimum between num_bins and automatic_nbin. This is
        to ensure that the constraint given by parameter min_bin_size is respected.
        """
        self.min_group_size = min_bin_size
        self.num_bins = num_bins

    def findCutPoints(self, x):
        """
        :param x: numpy array or pandas series
        :return: list of (ascending) ordered cut points
        """
        #determination of the number of bins
        if self.num_bins >1:
            num_bins = min(self.num_bins, int(x.size/(self.min_group_size*1.2)))
        else:
            num_bins = int(x.size/(self.min_group_size*1.2))
        if num_bins < 2:
            return []

        avg_group_size = x.size / num_bins

        if isinstance(x, pd.Series):
            x = x.to_numpy()
        val, counts = np.unique(x, return_counts=True)

        quantiles = [] #actually this array will contains quantiles * x.size
        sum =0
        for c in counts:
            sum = sum+c
            quantiles.append(sum)

        up_index = 0
        low_index = 0
        current_quantile=avg_group_size #again, is quantile * x.size

        cut_indexes = []
        #for each expected quantile, find his approximation
        while current_quantile < (sum-avg_group_size/2): # sum is equal to x.size
            up_index, low_index = self.findApproximationIndexex(up_index, low_index, current_quantile, quantiles)
            '''
            # this commented code use the cut point that best approximates the expected quantile
            num_up = quantiles[up_index] - current_quantile
            num_low = current_quantile - quantiles[low_index]

            if num_up < num_low:
                if up_index not in cut_indexes:
                    cut_indexes.append(up_index)
            else:
                if low_index not in cut_indexes:
                    cut_indexes.append(low_index)
            '''

            ### here we always choose an approximation by excess of the expected quantile.
            # This consistency can help create more similarly sized bins
            if up_index not in cut_indexes:
                cut_indexes.append(up_index)
            current_quantile += avg_group_size
            ###

        cut_points = []
        bins_size = []
        last_quantile = 0
        for i in cut_indexes:
            cut_points.append(val[i])
            bins_size.append(quantiles[i]-last_quantile)
            last_quantile = quantiles[i]
        bins_size.append(sum - last_quantile)

        # The bins with size <= min_group_size will be merged with one of the other adiacent bins
        self.mergeSmallBins(cut_points, bins_size)
        return cut_points

    def findApproximationIndexex(self,up_index, low_index, current_quantile, quantiles):
        """
        :param up_index: int
        :param low_index: int
        :param current_quantile: float
        :param quantiles: list of float

        :return: (int, int)
        """

        new_up_i = up_index
        while new_up_i < len(quantiles):
            if quantiles[new_up_i] < current_quantile:
                new_up_i+=1
            else:
                break
        new_low_i = low_index
        if new_up_i > up_index:
            new_low_i= new_up_i-1
        return new_up_i, new_low_i

    def mergeSmallBins(self, cut_points, bins_size):
        """
        :param cut_points: list
        :param bins_size: list
        :return: void
        """
        # find the smallest bin
        min_smallbin = self.min_group_size
        min_index = 0
        for i in range(len(bins_size)):
            if bins_size[i]<min_smallbin:
                min_smallbin = bins_size[i]
                min_index = i

        # check if the smallest bin finded has size lower than the min_group_size
        if min_smallbin == self.min_group_size:
            return

        previous_size = 0
        next_size = 0
        if min_index > 0:
            previous_size = bins_size[min_index - 1]
        if min_index < (len(bins_size)-1):
            next_size = bins_size[min_index + 1]

        if previous_size == 0 and next_size ==0:
            return

        if (previous_size == 0) or (next_size > 0 and next_size < previous_size):
            bins_size[min_index+1] = bins_size[min_index+1]+bins_size[min_index]
            cut_points.pop(min_index)
            bins_size.pop(min_index)
        else:
            bins_size[min_index - 1] = bins_size[min_index - 1] + bins_size[min_index]
            cut_points.pop(min_index-1)
            bins_size.pop(min_index)

        self.mergeSmallBins(cut_points, bins_size)

class EqualWidth:
    """
        This class find the cut points to discretize a numerical feature using the equal width discretization.
        """
    def __init__(self, min_bins_size=1, num_bins=0):
        """
        Parameters
        __________
        num_bins : int
            this number is to interpret as the maximum number of bins that will be generated.
            If this parameter is not specified (0 by deafault), it will be automatically determined.
        min_bins_size : int

        Notes
        -----
        min_bins_size and num_bins parameters are to be interpreted as for the EqualFreq class
        """
        self.min_group_size = min_bins_size
        self.num_bins = num_bins

    def findCutPoints(self, x):
        """

        :param x: numpy array or pandas series
        :return: list of (ascending) ordered cut points
        """
        if self.num_bins > 1:
            num_bins = min(self.num_bins, int(x.size / (self.min_group_size * 1.2)))
        else:
            num_bins = int(x.size / (self.min_group_size * 1.2))
        if num_bins < 2:
            return []

        if isinstance(x, pd.Series):
            x = x.to_numpy()
        minim = x.min()
        bin_width = (x.max() - minim)/num_bins
        cut_points = []
        current_cut = minim + bin_width
        for i in range(1, num_bins):
            cut_points.append(current_cut)
            current_cut = current_cut + bin_width
        return cut_points


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

# A data structure is a systematic way of organizing and accessing data,
# An algorithm is a step-by-step procedure for performaing some task in
# a finite amount of time. These concepts are central to computing, but
# to be able to classify some data structures and algorithms as "good",
# we must have precise ways of nanlyzing them.

# The primary analysis tool we will use in this book involes characterizing
# the running times of algorithms and data structure operations, with space
# usage also being of interest.

# Experimental Studies

# If an algorithm has been implemented, we study its running time by
# executing it on various test inputs and recording the time spent
# during each execution.

# Because many processes share use of a computer's central processing unit
# (or CPU), the elasped time will depend on what other processes are
# running on the computer when the test is performed.

# Python includes a more advanced module, name timeit, to help
# automate such evaluations with repetition to account for such
# variance among trials.

from time import time
from timeit import timeit

def algorithm():
    return sum({k for k in range(100)})

def analysis_algorithm1(n):
    start_time = time()      # record the starting time

    for _ in range(n):
        algorithm()

    end_time = time()        # record the ending time
    elapsed = end_time - start_time
    print(elapsed)

def analysis_algorithm2(n):
    elapsed = timeit(
        stmt='algorithm()',
        setup='from __main__ import algorithm',
        number=n
    )
    print(elapsed)

# Challenges of Experimential Analysis

# While experimental studies of running times are valuable, especially when
# finetuning production-quality mode, there are three major limitations to
# their use for algorithm analysis:

    # Experimental running times of two algorithms are difficult to directly
    # compare unless the experiments are performed in the same hardware and
    # software environemnts.

    # Experiments can be done only on a limited set of test inputs; hence,
    # they leave out the running times of inputs not included in the experiment
    # (and these inputs may be important).

    # An algorithm must be fully implemented in order to execute it to study
    # its running time experimentally.

if __name__ == '__main__':
    analysis_algorithm1(100000)
    analysis_algorithm2(100000)

"""
$ py3 algorithm analysis.py
0.5058159828186035
0.5283690920041408
"""


from controller.observer import Observer
from model.model import Model
from ui.debug_ui import DebugGUI
from model.ai import Ai

class Controller:

    def __init__(self, model: Model, ai: Ai, debug_ui: DebugGUI):
        """
                Initial method setting up controller observers and references to other classes.

                Parameters
                ----------
                model : Model
                    Reference to programs model class.
                ai : Ai
                    Reference to programs AI class.
                debug_ui : DebugGUI
                    Reference to programs debug UI class.
        """
        self.model = model
        self.debug_ui = debug_ui
        self.ai = ai

        self.tool_observer = ToolObserver(self.debug_ui)
        self.frame_observer_debug = FrameObserver(debug_ui.frame, debug_ui)

        self.classifications_observer_debug = ClassificationsObserver(debug_ui)
        self.classifications_observer_model = ClassificationsObserver(model)

        self.model.add_frame_observer(self.frame_observer_debug)
        self.model.add_tool_observer(self.tool_observer)

        self.ai.add_classifications_observer(self.classifications_observer_debug)
        self.ai.add_classifications_observer(self.classifications_observer_model)

class FrameObserver(Observer):

    def __init__(self, frame, update_target):
        self.frame = frame
        self.update_target = update_target

    def update(self, frame):
        """
            Updates the current frame for all classes which use the frame.

            Parameters
            -----------
            frame :  Cam
                the current frame of the camera being used

        """
        if frame is None:
            return
        self.frame = frame
        self.update_target.update_frame(frame)

class ClassificationsObserver(Observer):

    def __init__(self, update_target):
        self.update_target = update_target
        self.classifications = None

    def update(self, classifications):
        """
            Updates the current frame for all classes which use the frame.

            Parameters
            ----------
            classifications : []
                array of classified objects within the frame
        """
        if classifications is None:
            return
        self.classifications = classifications
        self.update_target.update_classifications(classifications)

class ToolObserver(Observer):
    def __init__(self, update_target):
        self.update_target = update_target
        self.line = None
        self.traffic_rect = None
        self.traffic_light_red = False

    def update(self):
        """
            Updates the current frame for all classes which use the frame.
        """
        if self.update_target.update_line() is not None:
            self.line = self.update_target.update_line()
        if self.update_target.update_traffic_rect() is not None:
            self.traffic_rect = self.update_target.update_traffic_rect()

    def get_traffic_rectangle(self):
        """
        Returns the rectangle object representing the traffic light object.

        Returns
        -------
        rect:
            Rectangle traffic light object.
        """
        if self.traffic_rect is None or len(self.traffic_rect) < 2:
            return -1
        return self.traffic_rect

    def get_line(self):
        """
        Returns the line object representing the intersection line.

        Returns
        -------
        line:
            intersection line object.
        """
        if self.line is None or len(self.line) < 2:
            return -1
        return self.line

    def set_traffic_light_red(self, traffic_light_red):
        """
        Sets whether the traffic light has been detected red or not,
        and passes that information into the debug_ui so the visual
        display of the traffic light detection can be shown.
        """
        self.traffic_light_red = traffic_light_red
        self.update_target.set_traffic_light_red(self.traffic_light_red)


from ..Model.GraphModel import GraphModel
from math import *
class GraphPresenter:
    """Класс презентер, занимается принятием сигнала от View и передает данные внутр модели
    при условии запуска расчета"""

    def __init__(self, ui):
        self.ui = ui
        self.ui.label.setWordWrap(True)
        self.add_functions()

    def add_functions(self):
        """Метод отлова клавиш из вью"""
        self.ui.graph_button.clicked.connect(lambda: self.calculate_graph())
        self.ui.cleargraph.clicked.connect(lambda: self.ui.graph.clear())
        self.ui.copy.clicked.connect(lambda: self.ui.functionlab.setText(self.ui.label.text()))

    def calculate_graph(self):
        y = []
        x = []
        res_value = 0.
        try:
            xmin = float(self.ui.xminval.toPlainText())
            xmax = float(self.ui.xmaxval.toPlainText())
            accur = float(self.ui.accur.toPlainText())
        except ValueError:
            self.ui.Error.setText('Error: The incorrect values!!!')
            return None
        iter = xmin
        while iter < 100000 and iter <= xmax and iter > -100000 and iter >= xmin:
            Model = GraphModel(self.ui.functionlab.text(), str(iter))
            Model.lexer()
            Model.priority()
            Model.shuntin_yard()
            try:
                res_value = float(Model.calc_polish())
            except ValueError:
                iter += accur
                continue
            except Exception:
                self.ui.Error.setText('Error: The incorrect formula')
                return None
            y.append(res_value)
            x.append(iter)
            iter += accur
        print(x, y)
        self.ui.graph.plot(x, y)
        self.ui.Error.setText('')


import os
import requests
import sys
import json
from bs4 import BeautifulSoup

BASE_DIR = os.path.dirname('__File__')
print(BASE_DIR)

print("Start Crawling")
req = requests.get('https://music.naver.com/listen/top100.nhn?domain=TOTAL')
req.encoding = None
html = req.content #html요소 전부 가져오기
soup = BeautifulSoup(html, 'html.parser') #html 파싱
datas = soup.select(
    '#content > div.NE\=a\:t1a > div._tracklist_mytrack.tracklist_table.tracklist_type1 > table > tbody > tr > td.name > a._title > span'
    )
data = {}

for i in range(len(datas)):
    rank = i+1
    name = datas[i].get_text()
    print(str(rank) + ". " + name)
    data[rank] = name

with open(os.path.join(BASE_DIR, 'musicRank.json'), 'w+',encoding='utf-8') as json_file:
    json.dump(data, json_file, ensure_ascii = False, indent='\t')

print("Crawling Complete")


import os
import numpy as np
import cv2

mainFolder = 'Images'
myFolders = os.listdir(mainFolder)
print(myFolders)

finished=0
wrong=0

for folder in myFolders:
    path = mainFolder +'/'+folder
    images =[]
    myList = os.listdir(path)
    print(f'Total no of images detected {len(myList)}')
    for imgN in myList:
        curImg = cv2.imread(f'{path}/{imgN}')
        curImg = cv2.resize(curImg, (0,0),None,0.4,0.4,None)
        images.append(curImg)

    stitcher = cv2.Stitcher.create()
    (status,result)= stitcher.stitch(images)
    if status==cv2.Stitcher_OK:
        print('Done')
        finished=finished+1
        cv2.imshow(folder,result)
        cv2.waitKey()
        cv2.destroyAllWindows()
    else:
        print('Something went wrong')
        wrong=wrong+1

print('Successful stitches: ',finished, 'Failed stitches: ',wrong)


# r=range(5)
# print(r)
# print(list(r))
#PROGRAMA PARA CALCULAR LA TABLA DEL 5

# tabla=5
# for i in range(1,11):
#     print(f'{tabla}*{i} = {tabla*i}')

#truco _ con esto ignoramos el elemento de la secuencia, se lo salta
# for _ in range(3):
#     n=int(input('introduce un numero: > '))
#     print(f'{n}*3={n*3}')

#cuando no utilizar el tipo range
numeros=[1,3,-5,8,0]
for i in range(len(numeros)):
    print(numeros[i])
#esta forma no es pythonica, simplemente se puede hacer lo mimo de esta
#manera
for i in numeros:
    print(i, end=' ')