# -*- coding: utf-8 -*-
"""Module containing the FieldPlot class for scalar fields on the Earth

This class plots scalar variables (e.g. temperature) on the globe. Optionally a
overlayed vector field (e.g. wind) can be plotted as well."""
from itertools import izip
import numpy as np
import netCDF4 as nc
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
import matplotlib as mpl
from .._basemap import Basemap
from copy import deepcopy
from ..formatoptions import FieldFmt, FmtBase, WindFmt, _get_fmtkeys_formatted
from ..warning import warnings, Nc2MapWarning, warn
from .._cmap_ops import get_cmap
from _map_base import MapBase, _props, returnbounds
from _windplot import WindPlot

class FieldPlot(MapBase):
    """Class to visualize two-dimensional scalar fields on a map. Optionally
    you can also combine this with a nc2map.mapos.WindPlot instance (see
    __init__ methods for details."""
    # ------------------ define properties here -----------------------
    var = _props.dim('var', """String. Variable name in the reader""")

    @property
    def meta(self):
        meta = super(FieldPlot, self).meta
        if self.wind is not None:
            for key, val in self.wind.meta.items():
                meta.setdefault(key, val)
        return meta

    def __init__(self, reader, var, u=None, v=None, fmt={}, **kwargs):
        """
        Input:
            - var: string. Name of the variable which shall be plotted and red
                the netCDF file
            - u: string (Default: None). Name of the zonal wind variable if a
                 WindPlot shall be visualized (set u and v to also plot the
                 wind above the data of var)
            - v: string (Default: None). Name of the meridional wind variable
                 if a WindPlot shall be visualized
            - fmt: dictionary containing the formatoption keywords as keys and
                 settings as value.
        Keywords inherited from MapBase initialization are: %(mapbase)s
        Possible formatoption keywords are (see nc2map.show_fmtdocs() for
        details): %(fmt)s
        And the windplot specific options are (see nc2map.show_fmtdocs('wind')
        for details): %(windfmt)s
        """
        # docstring is extended below with the formatoption keywords
        self.name = kwargs.get('name', 'mapo')
        self.set_logger()

        kwargs_keys = ['var', 'u', 'v', 'fmt']
        max_kwarg_len = max(map(len, kwargs_keys)) + 2
        self.logger.debug("Original kwargs:")
        for key in kwargs_keys:
            self.logger.debug('    %s:'.ljust(max_kwarg_len)  + '%s',
                              key, str(locals()[key]))

        super(FieldPlot, self).__init__(reader, fmt=fmt, var=var, **kwargs)
        # define formatoptions
        if isinstance(fmt, dict):
            var = fmt.get('var', var)  # update from variables
            updatewind = u is not None and v is not None
            self.fmt = FieldFmt(**dict(
                item for item in fmt.items() + [('updatewind', updatewind)]
                if item[0] not in self.dims))
            self.logger.debug(
                "Dimensions set up by formatoptions dictionary:")

        elif not fmt:
            self.fmt = FieldFmt()
        else:
            if not isinstance(fmt, FieldFmt):
                warn('Expected FieldFmt instance but found %s for '
                     'formatoptions!' % str(type(fmt)))
            self.fmt = fmt

        # create basemap
        self.logger.debug(
            "Initialize Basemap instance with %s", self.fmt._projops)
        self.mapproj = Basemap(**self.fmt._projops)

        self.data = self.get_data(var)
        self._reinitialized = 1
        if u is not None and v is not None:
            self.wind = WindPlot(
                self.reader, time=self.time, level=self.level, ax=self._ax,
                fmt=self.fmt.windplot, u=u, v=v, mapsin=self._mapsin,
                name=self.name, mapproj=self.mapproj)
            self.wind._draw_mapproj = 0
        else:
            self.wind = None

    def _reinit(self, u=None, v=None, **kwargs):
        """Function to reinitialize the data"""
        # set _reinitialized for unstructured grids in make_plot
        self._reinitialized = 1
        self._reinitialize = 0
        super(FieldPlot, self)._reinit(**kwargs)
        zipped_kwargs = izip(['u', 'v'], [u, v])
        not_none_kwargs = (key for key, val in zipped_kwargs
                           if val is not None)
        self.logger.debug('Reinit %s', ', '.join(not_none_kwargs))
        self.data = self.get_data(self.var)
        if self.wind is not None:
            self.wind.mapproj = self.mapproj
            self.wind._reinit(u=u, v=v)

    def update(self, todefault=False, plot=True, force=False, **kwargs):
        """Update the MapBase instance, formatoptions, variable, time and
        level.
        Possible keywords (kwargs) are
          - all keywords as set by formatoptions (see initialization __init__)
          - time: integer. Sets the time of the MapBase instance and the data
          - level: integer. Sets the level of the MapBase instance and the data
          - var: string. Sets the variable of the MapBase instance and the data
        Additional keys in the windplot dictionary are:
          - u: string. Sets the variable of the WindPlot instance and reloads
              the data
          - v: Sets the variable of the WindPlot instance and reloads the data
          - time, level and var as above
        """
        if not hasattr(self, '_basemapops'):
            self.logger.info("Setting up projection...")
            self._setupproj()
        # set current axis
        plt.sca(self.ax)
        self.logger.debug('Updating...')
        self.logger.debug('Plot after finishing: %s', plot)
        # patterns for warnings that shall be depreciated

        # check the keywords
        dims = self.dims.copy()
        dimsorig = self.dims_orig
        for dim in self.data.dims.dtype.fields.keys():
            dims.setdefault(dim, 0)
            dimsorig.setdefault(dim, 0)
        possible_keys = self.fmt._default.keys() + dims.keys()
        for key in kwargs:
            self.fmt.check_key(key, possible_keys=possible_keys)

        # save current formatoptions
        old_fmt = self.fmt.asdict()

        # delete formatoptions which are already at the wished state
        if not todefault and not force:

            kwargs = {key: value for key, value in kwargs.items()
                      if (not isinstance(value, dict) and
                          np.all(value != old_fmt.get(
                          key, dims.get(key, self.fmt._default.get(key)))))}
        elif not force:
            self.logger.debug('Update to default...')
            oldkwargs = kwargs.copy()
            defaultitems = self.fmt._default.items()
            kwargs = {
                key: kwargs.get(key, value) for key, value in defaultitems
                if key != 'windplot' and (
                    (key not in kwargs and np.all(
                        value != getattr(self.fmt, key)))
                    or (key in kwargs and np.all(
                        kwargs[key] != getattr(self.fmt, key))))}
            if not self.fmt._enablebounds:
                kwargs = {key: value for key, value in kwargs.items()
                          if key not in ['cmap', 'bounds']}
            # update dimensions
            kwargs.update({
                key: oldkwargs.get(key, value)
                for key, value in dimsorig.items()
                if (dims[key] != dimsorig[key]
                    or (key in oldkwargs and oldkwargs[key] != dims[key]))})
            if 'windplot' in oldkwargs:
                kwargs['windplot'] = oldkwargs['windplot']
            else:
                kwargs['windplot'] = {}

        self.logger.debug("Update to ")
        try:
            max_kwarg_len = max(map(len, kwargs.keys())) + 3
        except ValueError:
            max_kwarg_len = 0
        for key, val in kwargs.items():
            self.logger.debug('    %s:'.ljust(max_kwarg_len)  + '%s',
                              key, str(val))

        try:  # False: Remove plot, True: make plot, None: do nothing
            enable = bool(kwargs['enable'])
            self.logger.debug('Updating enable to %s', enable)
            self.fmt.enable = enable
        except KeyError:
            enable = None  # no change

        # update plotting of cbar properties and close cbars
        if 'plotcbar' in kwargs:
            self.logger.debug(
                "Found 'plotcbar' in kwargs --> remove non-used cbars")
            if kwargs['plotcbar'] in [False, None]:
                kwargs['plotcbar'] = ''
            if kwargs['plotcbar'] is True:
                kwargs['plotcbar'] = 'b'
            cbars2close = [cbar for cbar in self.fmt.plotcbar
                          if cbar not in kwargs['plotcbar']]
            self._removecbar(cbars2close)

        # update extend and ticklabels
        if ('extend' in kwargs
                or kwargs.get('ticklabels', False) is None):
            self.logger.debug(
                "Found 'extend' or 'ticklabels' in kwargs --> remove cbars")
            self._removecbar(resize=False)

        # update masking options
        maskprops = {key: value for key, value in kwargs.items()
                     if key in self.fmt._maskprops}
        if maskprops:
            self.logger.debug(
                'Found masking properties: %s. --> reinit',
                ', '.join(maskprops.keys()))
            self.fmt.update(**maskprops)

        # update basemap properties
        bmprops = {key: value for key, value in kwargs.items()
                   if key in self.fmt._bmprops}
        if bmprops:
            self.logger.debug(
                'Found basemap properties: %s. --> reinit',
                ', '.join(bmprops.keys()))
            with warnings.catch_warnings():
                if self.wind is None and not 'windplot' in kwargs:
                    warnings.filterwarnings(
                        "ignore", 'Stereographic', Nc2MapWarning,
                        'nc2map.formatoptions._windfmt', 0)
                self.fmt.update(**bmprops)

        # update mapobject dimensions and reinitialize
        newdims = {key: value for key, value in kwargs.items()
                   if key in dims.keys()}
        if newdims:
            self.logger.debug(
            'Found new dimensions: %s. --> reinit',
            ', '.join(newdims.keys()))

        if newdims or bmprops or maskprops or enable or self._reinitialize:
            if bmprops:
                self.mapproj = Basemap(**self.fmt._projops)
            self._reinit(**newdims)
            self._make_plot = 1
        if ((bmprops or not hasattr(self, '_basemapops'))
                and plot):
            self._setupproj()

        self._make_plot = self._make_plot or set(kwargs) & set(
            ['cmap', 'bounds', 'norm', 'opacity', 'rasterized', 'grid'])

        # color oceans and land
        if any(key in kwargs for key in ['ocean_color', 'land_color']):
            self._draw_lsmask(kwargs.get('ocean_color', self.fmt.ocean_color),
                              kwargs.get('land_color', self.fmt.land_color))

        # handle plot enabling changing
        if enable is False:
            self.logger.debug("Found 'enable' in kwargs --> remove plot")
            self._removeplot()

        if ('grid' in kwargs or self._make_plot) and hasattr(self, 'grid'):
            for line in self.grid:
                line.remove()
            del self.grid

        # update rest
        self.logger.debug('Update fmtBase instance')
        fmt_update_kwargs = {key: value for key, value in kwargs.items()
                             if key not in self.fmt._bmprops + dims.keys()}
        for key in dims.keys():
            try:
                fmt_update_kwargs['windplot'].pop(key)
            except KeyError:
                pass
        self.fmt.update(**fmt_update_kwargs)

        # update ticks
        self._update_ticks(kwargs)

        # update map projection
        self._update_mapproj(kwargs=kwargs, plot=plot)

        # update wind
        if self.wind is not None or 'windplot' in kwargs:
            if 'windplot' in kwargs:
                self.logger.debug("Found 'windplot' in kwargs...")
                windops = kwargs['windplot']
                # if WindPlot is currently not set, enable it now
                if (windops.get('u', None) is not None
                        and windops.get('v', None) is not None
                        and self.fmt.windplot.enable
                        and self.wind is None):
                    self.logger.debug("No windplot set so far --> Set it now")
                    self.wind = WindPlot(
                        reader=self.reader, time=self.time, level=self.level,
                        ax=self._ax, fmt=self.fmt.windplot, u=windops['u'],
                        v=windops['v'], mapsin=self._mapsin, name=self.name,
                        mapproj=self.mapproj)
                    self.wind._draw_mapproj = 0
                for key in ['time', 'level']:
                    if key in kwargs:
                        windops.setdefault(key, kwargs[key])
            else:
                windops = {}
            if self.wind is not None:
                self.logger.debug('Updating wind...')
                windops.update({key: getattr(self.fmt, '_'+key) for key in
                                    self.fmt._bmprops})
                self.wind.update(plot=False, todefault=todefault, **windops)
                self.wind._make_plot = self._make_plot or self.wind._make_plot
        if plot:
            self.make_plot()
        else:
            self._make_plot = 0

        self.logger.debug('Update Done.')

    def _mask_data(self, data):
        data = super(FieldPlot, self)._mask_data(data)
        if self.fmt.maskless is not None:
            self.logger.debug('Masking below %s', self.fmt.maskless)
            data[:] = np.ma.masked_less(data[:], self.fmt.maskless, copy=True)
        if self.fmt.maskleq is not None:
            self.logger.debug('Masking below or equal to %s',
                              self.fmt.maskleq)
            data[:] = np.ma.masked_less_equal(data[:], self.fmt.maskleq,
                                           copy=True)
        if self.fmt.maskgreater is not None:
            self.logger.debug('Masking above %s', self.fmt.maskgreater)
            data[:] = np.ma.masked_greater(data[:], self.fmt.maskgreater,
                                           copy=True)
        if self.fmt.maskgeq is not None:
            self.logger.debug('Masking above or equal to %s',
                              self.fmt.maskgeq)
            data[:] = np.ma.masked_greater_equal(data[:], self.fmt.maskgeq,
                                              copy=True)
        if self.fmt.maskbetween is not None:
            self.logger.debug('Masking between %s', str(self.fmt.maskbetween))
            data[:] = np.ma.masked_inside(data[:], self.fmt.maskbetween[0],
                                       self.fmt.maskbetween[1], copy=True)
        return data

    def _moviedata(self, times, nowind=False, **kwargs):
        """generator to get the data for the movie"""
        try:
            windops = kwargs.pop('windplot')
        except KeyError:
            windops = {}
        kwargs = {key: iter(val) for key, val in kwargs.items()}
        windops = {key: iter(val) for key, val in windops.items()}
        for i, time in enumerate(times):
            # yield time step, data, formatoptions
            fmt = {key: next(value) for key, value in kwargs.items()}
            fmt.update({key: next(value) for key, value in windops.items()})
            yield (time, fmt)

    def _setupproj(self):
        # docstring is set equal to MapBase _setupproj method below
        super(FieldPlot, self)._setupproj()
        if self.wind is not None:
            self.wind.mapproj = self.mapproj

    def _runmovie(self, args):
        """Function to update the movie with args from self._moviedata"""
        if 'windplot' not in args[-1]:
            args[-1]['windplot'] = {}
        self.update(time=args[0], **args[-1])
        return

    def _removeplot(self):
        """Removes the plot from the axes and deletes the plot property from
        the instance"""
        self.logger.debug("    Removing plot...")
        if hasattr(self, 'plot'):
            try:
                self.plot.remove()
            except:
                pass
            try:
                for line in self.grid:
                    line.remove()
                del self.grid
            except AttributeError:
                pass
            del self.plot
        self._removecbar()

    def make_plot(self):
        """Make the plot with the current settings and the WindPlot. Use it
        after reinitialization and _setupproj. Don't use this function
        to update the plot but rather the update function!"""
        self.logger.debug('Making plot...')
        if self.fmt.enable and self._make_plot:
            if not hasattr(self, '_basemapops'):
                self.logger.info("Setting up projection...")
                self._setupproj()
            plt.sca(self.ax)
            if not self.fmt._enablebounds:
                pass
            elif self.fmt.bounds[0] in ['rounded', 'sym', 'minmax',
                                      'roundedsym']:
                self.logger.debug('    Calculate bounds with %s',
                                  str(self.fmt.bounds))
                self._bounds = returnbounds(self.data[:], self.fmt.bounds)
            else:
                self.logger.debug('   Found manually set bounds')
                self._bounds = self.fmt.bounds
            if self.fmt._enablebounds:
                if self.fmt.norm == 'bounds':
                    self._cmap = get_cmap(self.fmt.cmap, len(self._bounds)-1)
                    self._norm = mpl.colors.BoundaryNorm(self._bounds,
                                                         self._cmap.N)
                else:
                    self._cmap = get_cmap(self.fmt.cmap)
                    if self.fmt.norm is None:
                        self._norm = mpl.colors.Normalize()
                        self._norm.vmin = self._bounds.min()
                        self._norm.vmax = self._bounds.max()
                    else:
                        self._norm = self.fmt.norm
                if self.fmt.opacity is not None:
                    self._calculate_opacity()
            if hasattr(self, 'plot'):
                self.logger.debug('    Remove plot')
                self.plot.remove()
                # does not work atm
                # self.plot.set_cmap(self._cmap)
                # self.plot.set_norm(self._norm)
                # self.plot.set_array(self.data[:-1,:-1].ravel())
            # else:
                # make plot
            self.logger.debug('    Draw plot')
            if (self.reader._udim(self.get_var())
                    and self.fmt.plottype == 'quad'):
                self.logger.debug(
                    "Found unstructured variable. I set the 'plottype' "
                    "keyword to 'tri'")
                self.fmt.plottype = 'tri'
            if self.fmt.plottype == 'quad':
                if self.fmt.grid:
                    settings = {'edgecolors': self.fmt.grid}
                else:
                    settings = {'edgecolors': 'face'}
                lat2d, lon2d = self.data.grid
                self.plot = self.mapproj.pcolormesh(
                    lon2d, lat2d, self.data[:], cmap=self._cmap,
                    norm=self._norm, rasterized=self.fmt.rasterized,
                    latlon=self.fmt.latlon, **settings)
            elif self.fmt.plottype == 'tri':
                if self.data.triangles:
                    triangles = self.data.triangles
                    self.plot = self.mapproj.tripcolor(
                        triangles, self.data[:], cmap=self._cmap,
                        norm=self._norm, rasterized=self.fmt.rasterized,
                        alpha=None,
                        latlon=False if not self._reinitialized else \
                            self.fmt.latlon)
                else:
                    triangles = Triangulation(self.data.lon, self.data.lat)
                    self.plot = self.mapproj.tripcolor(
                        triangles, self.data[:], cmap=self._cmap,
                        norm=self._norm, rasterized=self.fmt.rasterized,
                        latlon=self.fmt.latlon)
                if self.fmt.grid:
                    try:
                        self.grid = self.mapproj.triplot(
                            triangles, latlon=False, **self.fmt.grid)
                    except TypeError:
                        self.grid = self.mapproj.triplot(
                            triangles, self.fmt.grid, latlon=False)

            else:
                raise ValueError("Unknown plottype %s" % self.fmt.plottype)

        if self.fmt.enable and not (
                self.fmt.plotcbar == '' or self.fmt.plotcbar == ()
                or self.fmt.plotcbar is None or self.fmt.plotcbar is False):
            self._draw_colorbar()

        if self.wind is not None:
            if self.wind.fmt.enable:
                self.logger.debug('Wind is enabled --> make windplot')
                if not hasattr(self, "_basemapops"):
                    self.wind._setupproj()
                self.wind.make_plot()

        self._configureaxes()
        self._make_plot = 0
        self._reinitialized = 0
        if self.fmt.tight:
            self.logger.debug("'tight' is True --> make tight_layout")
            plt.tight_layout()

    def close(self, num=0):
        """Close the MapBase instance.
        Arguments may be
        - 'data': To delete all data (but not close the netCDF4.MFDataset
        instance)
        - 'figure': To close the figure and the figures of the colorbars.
        Without any arguments, everything will be closed (including the
        netCDF4.MFDataset instance) and deleted.
        """
        self.logger.debug("Closing...")
        super(FieldPlot, self).close(num)
        if not num % 7:
            num /= 7
        if self.wind is not None:
            self.wind.close(num)

    def asdict(self, **kwargs):
        """Returns a dictionary containing the current formatoptions and (if
        the time, level or var changed compared to the original
        initialization) the time, level or var"""
        fmt = super(FieldPlot, self).asdict(**kwargs)
        #if self.var != self.varorig:
            #fmt.update({'var': self.var})
        if self.wind is not None:
            fmt.update({'windplot': self.wind.asdict()})
        return fmt

    def copy(self, *args, **kwargs):
        """Copy method. Docstring is set below to be equal to the MapBase.copy
        method"""
        mapo = super(FieldPlot, self).copy(*args, **kwargs)
        if self.wind is not None:
            mapo.wind = self.wind.copy(*args, **kwargs)
        return mapo

    # ------------------ modify docstrings here --------------------------
    __init__.__doc__ += __init__.__doc__ % {
        'fmt': '\n' + _get_fmtkeys_formatted(),
        'windfmt': '\n' + _get_fmtkeys_formatted('wind', 'windonly'),
        'mapbase': '\n' + MapBase.__init__.__doc__}
    _setupproj.__doc__ = MapBase._setupproj.__doc__
    copy.__doc__ = MapBase.copy.__doc__


import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
#descargar pip install opencv-python capitulo 12
import cv2
import time

class Cv2(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_screenshot_open_cv(self):
        driver = self.driver
        driver.get("http://www.google.com")
        driver.save_screenshot('img2.png')
        time.sleep(3)

    def test_comparing_images(self):
        img1= cv2.imread('img1.png')
        img2 = cv2.imread('img2.png')

        diferencia = cv2.absdiff(img1,img2)
        image_gris = cv2.cvtColor(diferencia,cv2.COLOR_BGR2GRAY)
        contours,_ = cv2.findContours(image_gris,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

        for c in contours:
            if cv2.contourArea(c) >= 20:
                possicion_x,possicion_y,ancho,alto = cv2.boundingRect(c)
                cv2.rectangle(img1,(possicion_x,possicion_y),(possicion_x+ancho,possicion_y,alto),(0,0,255),2)


from flask import Flask, request, jsonify, render_template
import pickle
import Functions as f
app = Flask(__name__)
model = pickle.load(open('model.pkl','rb'))

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict',methods=['POST'])
def predict():
    inputValue = request.form['SMS']
    inputValue = f.expand_contractions(inputValue)
    inputValue = f.preprocessStepOne(inputValue)
    inputValue = f.remove_stopwords(inputValue)
    inputValue = f.get_stem(inputValue)
    print(inputValue)
    prediction = model.predict_proba([inputValue])

    ham = prediction[0][0]
    spam = prediction[0][1]

    return render_template('index.html', prediction_text=f'spam : {spam} \n , ham : {ham}')

@app.route('/results',methods=['POST'])
def results():

    data = request.get_json(force=True)
    prediction = model.predict_proba(data.values())

    ham = prediction[0][0]
    spam = prediction[0][1]
    return jsonify(ham,spam)

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


from aiogram import Dispatcher, types
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from data_base.sqlite_db import sql_add_command
from keyboards import admin_kb

class FSMAdmin(StatesGroup):
    photo = State()
    name = State()
    description = State()
    price = State()

async def cm_open_keyboard(message: types.Message):
    if (message.from_user.username == "Imitater"):
        await message.answer("Клавиатура админа открыта!",
                             reply_markup=admin_kb.edit_kb)
    else:
        await message.answer("ТЫ НЕ АДМИН!")

# dp.message_handler(commands='Загрузить', state=None)
async def cm_start(message: types.Message):
    await FSMAdmin.photo.set()
    await message.reply('Загрузи фото')

# dp.message_handler(content_types=['photo'], state=FSMAdmin.photo)
async def load_photo(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['photo'] = message.photo[0].file_id
    await FSMAdmin.next()
    await message.reply("Теперь введи название")

# dp.message_handler(state=FSMAdmin.name)
async def load_name(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['name'] = message.text
    await FSMAdmin.next()
    await message.reply("Введи описание")

# dp.message_handler(state=FSMAdmin.description)
async def load_description(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['description'] = message.text
    await FSMAdmin.next()
    await message.reply("Введи цену")

# dp.message_handler(state=FSMAdmin.price)
async def load_price(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['price'] = float(message.text)
    await sql_add_command(state=state)
    # после этой команды полностью очищается словарь с данными, введеными во время состояния
    await state.finish()

async def cansel_handler(message: types.Message, state: FSMContext):
    current_state = await state.get_state()
    if current_state is None:
        return
    await state.finish()
    await message.reply("ОК")

def register_handlers_admin(dp: Dispatcher):
    dp.register_message_handler(
        cm_open_keyboard, Text(equals="Открыть клавиатуру"))
    dp.register_message_handler(cm_start, commands=["Загрузить"], state=None)
    dp.register_message_handler(cansel_handler, state="*", commands='отмена')
    dp.register_message_handler(cansel_handler, Text(
        equals='отмена', ignore_case=True), state="*")
    dp.register_message_handler(load_photo, content_types=[
                                'photo'], state=FSMAdmin.photo)
    dp.register_message_handler(load_name, state=FSMAdmin.name)
    dp.register_message_handler(load_description, state=FSMAdmin.description)
    dp.register_message_handler(load_price, state=FSMAdmin.price)


import discord
from discord.ext import commands
import random
from coinmarketcap import Market
import datetime
from pinance import Pinance
import json
from riotwatcher import RiotWatcher
from requests import HTTPError
import math
import sqlite3
import time
import sys

#Checks to see if master database for crypto betting games exists
#if the .db doesnt exist then the file will be created.
try:
    conn = sqlite3.connect('file:cryptocurrencyusers.db?mode=rw', uri=True)
    c = conn.cursor()
    c.close
except sqlite3.OperationalError:

    conn = sqlite3.connect('cryptocurrencyusers.db')
#if you ever need to remake the users table, you can just delete the .db file and uncomment this section
    c = conn.cursor()
    command = "CREATE TABLE users (userid text, bank float, joined text);"

    c.execute(command)
    conn.commit()
    c.close()
finally:
    conn = sqlite3.connect('cryptocurrencyusers.db')

#This will run on the initial startup and acquire all cryptocurrency values.
#The Total supply for BTC does not appear though for some reason and thus all values are indexing are off by 1.

def updatecryptos():
    t0 = time.time()
    coinmarketcap = Market()
    cryptocoins = coinmarketcap.ticker(start = 0,limit = 0)

    Allcryptos = {}
    cryptobets = {}

    for x in range(0,len(cryptocoins) - 1):
        name = cryptocoins[x]
        Allcryptos[name['symbol']] = cryptocoins[x]
        cryptobets[name['symbol']] = 0
    print('All coins have been updated')
    t1 = time.time()
    total = t1-t0
    print(total)
    return Allcryptos

description = 'Harry'
bot = commands.Bot(command_prefix='.', description=description)

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command()
async def add(left : int, right : int):
    """Adds two numbers together."""
    await bot.say(left + right)

@bot.command()
async def roll(dice : str):
    """Rolls a dice in NdN format."""
    try:
        rolls, limit = map(int, dice.split('d'))
    except Exception:
        await bot.say('Format has to be in NdN!')
        return

    result = ', '.join(str(random.randint(1, limit)) for r in range(rolls))
    await bot.say(result)

@bot.command(description='For when you wanna settle the score some other way')
async def choose(*choices : str):
    """Chooses between multiple choices."""
    await bot.say(random.choice(choices))

@bot.command()
async def repeat(times : int, content='repeating...'):
    """Repeats a message multiple times."""
    for i in range(times):
        await bot.say(content)

@bot.command()
async def joined(member : discord.Member):
    """Says when a member joined."""
    await bot.say('{0.name} joined in {0.joined_at}'.format(member))

@bot.group(pass_context=True)
async def cool(ctx):
    """Says if a user is cool.
    In reality this just checks if a subcommand is being invoked.
    """
    if ctx.invoked_subcommand is None:
        await bot.say('No, {0.subcommand_passed} is not cool'.format(ctx))

@cool.command(name='bot')
async def _bot():
    """Is the bot cool?"""
    await bot.say('Yes, the bot is cool.')

@bot.command()
async def unmutev(member: discord.Member):
    #Unmutes member in voice channels.
    await bot.say('Unmuting '+ str(member) + ' from voice channels in this server.')
    await bot.server_voice_state(member,mute= False)

@bot.command()
async def mutev(member : discord.Member):
    #Mutes member in voice channels.
    await bot.say('Muting '+ str(member) + ' from voice channels in this server.')
    await bot.server_voice_state(member,mute= True)

@bot.command()
async def ban( member : discord.Member):
    #Bans a member from server
    await bot.say('Banning ' + str(member) + ' from the server.')
    await bot.ban(member, 0)

@bot.command(pass_context = True)
async def hello(ctx):
    author = ctx.message.author.name
    print(author)

    embed = discord.Embed(color = discord.Colour.purple())
    embed.set_author(name = 'Hello')
    embed.add_field(name = "User",value = author)
    await bot.say(embed=embed)

#@bot.event
#async def on_command_error(event,ctx):
    #this catches an errors with users that don't exist
    #await bot.send_message(ctx.message.channel,"User doesn't exist.")

@bot.command(pass_context = True)
async def cryptoregister(ctx):
    author = ctx.message.author

    joined = str(int(time.time()))
    c = conn.cursor()
    try:
        command = "INSERT INTO users (userid, bank,joined) VALUES ('" + author.id + "',10000," + joined + "); "
        c.execute(command)
        conn.commit()
        command = "CREATE TABLE '" + author.id +"' (coin text, amount float, worth float);"
        c.execute(command)
        conn.commit()
        string = (author.name + ' has been added to the crypto bet game. You have been awarded $10,000 to start.')
        return True
    except sqlite3.IntegrityError:
        print('Failed to insert values %d, %s')
        string = ('An error has occured.')
        return False
    except sqlite3.OperationalError:
        print('User always is registered.')
        string = ('You are already registered for the crypto betting game.')
    finally:
        c.close()
        embed = discord.Embed(color=discord.Colour.purple())
        embed.set_author(name='Crypto Register Status')
        embed.add_field(name="User", value=author)
        embed.add_field(name = 'status',value = string)
        await bot.say(embed=embed)

@bot.command(pass_context = True)
async def cryptobetstatus(ctx):

    Allcryptos = updatecryptos()
    author = ctx.message.author
    c = conn.cursor()
    userid =(author.id,)

    c.execute('SELECT * FROM users WHERE userid=?',userid)
    userinformation = c.fetchone()
    if userinformation == None:
        string = ('You are currently not registered. To register type the command: cryptoregister')
        embed = discord.Embed(color=discord.Colour.purple())
        embed.set_author(name='Crypto Bet Status')
        embed.add_field(name="User", value=author)
        embed.add_field(name = 'status',value = string)
        await bot.say(embed=embed)
        return
    datejoined = datetime.datetime.fromtimestamp(int(userinformation[2])).strftime('%Y-%m-%d')
    bank = ('$'  + str(math.floor(userinformation[1])))
    joined = ('You joined on ' + datejoined + '.')

    c.execute("SELECT name FROM sqlite_master WHERE type='table';")
    print(c.fetchall())

    #fetches coin investments from userid's table
    c.execute('SELECT * FROM \'' + author.id + "';")
    investments = c.fetchall()
    print(len(investments))
    print(investments)

    totalwealth = math.floor(userinformation[1])

    embed = discord.Embed(color=discord.Colour.purple())
    embed.set_author(name='Current Portfolio')
    embed.add_field(name="User", value=author)
    embed.add_field(name='Status', value='Registered')
    embed.add_field(name = 'Joined',value = joined)
    embed.add_field(name = 'Bank',value = bank)
    embed.add_field(name = 'Coins',value = '(Coins owned, Total Value, Overall Change)',inline = False)

    if len(investments) != 0:
        for x in range(0, len(investments)):
            investment = investments[x]
            coin = investment[0]
            amount = investment[1]
            price = float(Allcryptos[coin]['price_usd'])
            total = price*amount
            totalwealth = totalwealth + total

            if price*amount > investment[2]:
                percentchange = round(((price*amount)/investment[2] - 1)*100,2)
            elif price*amount < investment[2]:
                percentchange = 0 - round((investment[2]/(price*amount) -1)*100,2)
            else:
                percentchange = 0
            cointotal = str(amount)
            pricevalue = ('$' + str(round(total,2)))
            percentvalue = (str(percentchange)+ '%')
            embed.add_field(name = coin, value =(cointotal +'     ' + pricevalue + '     ' + percentvalue),inline= False)

    else:
        string = ('You currently do not own any cryptocoins.')
        clear_fields()
        embed = discord.Embed(color=discord.Colour.purple())
        embed.set_author(name='Current Portfolio')
        embed.add_field(name="User", value=author)
        embed.add_field(name='Status', value='Registered')
        embed.add_field(name = 'Coins',value ='None',inline = False)
        embed.add_field(name='Total Wealth', value=bank,inline = False)

    wealth = ('$' + str(round(totalwealth,2)))
    embed.add_field(name = 'Total Wealth',value =wealth)

    await bot.say(embed=embed)

@bot.command(pass_context = True)
async def cryptobet(ctx, order : str, coin = None, payment =None):

    order = order.upper()
    print(order)
    if order == "HELP":
        await bot.say('Command is written as: ",cryptobet <order> <coin> <amount>". Bot only can take input of cryptocoins symbol such as BTC or ETH. To buy 5 Bitcoins one would type ",cryptobet BUY BTC 5"')
        return
    elif order != "BUY" and order != "SELL":
        await bot.say('Order has to be either "HELP", "BUY" or "SELL".')
        return

    #If help command was not inputted than ensure that a coin was inputted
    try:
        coin = coin.upper()
    except AttributeError:
        await bot.say('No cryptocoin was inputted.')
        return

    Allcryptos = updatecryptos()
    c = conn.cursor()
    author = ctx.message.author
    userid = (author.id,)

    #checks to see if coin exists
    try:
        price = float(Allcryptos[coin]['price_usd'])
    except KeyError:
        await bot.say('No cryptocoin inputted or invalid crypto. Coins are pulled from coinmarketcap.com. Command is written as: ",cryptobet <order> <coin> <amount>"')
        return

    print(order)
    print(coin)
    print(payment)
    print(author.id)

    # If help command was not inputted than ensure that a payment was inputted
    try:
        payment = payment.upper()
    except AttributeError:
        await bot.say('No payment was inputted')
        return

    if payment != "ALL":
        try:
            payment = float(payment)
        except TypeError:
            await bot.say('No payment was inputted.')
            return
        except ValueError:
            await bot.say('Payment was not a valid input.')
            return

    # get total cash available for user
    c.execute('SELECT * FROM users WHERE userid=?', userid)
    userinformation = c.fetchone()
    cashavaiable = userinformation[1]

    if order == "BUY":

        #if there isnt enough cash or value is negative than the functions ends
        if price*payment >= cashavaiable:
            await bot.say('Not enough cash to buy this amount of '+ coin +'.')
            c.close()
            return

        elif payment < 0:
            await bot.say('Payment can not be a negative number.')
            c.close()
            return
        elif payment == 0:
            await bot.say('You can not buy 0 worth of cryptocoins')
            c.close()
            return

        #CALCULATE CASH REMAINING AND UPDATE USERS TABLE
        cashavaiable = cashavaiable - price*payment
        print(cashavaiable)

        c.execute("UPDATE users SET bank = ? WHERE userid = ?",(cashavaiable,author.id))
        conn.commit()

        c.execute("SELECT 1 FROM  \'" + author.id + "' WHERE coin = '" + coin + "'")
        if c.fetchone():
            c.execute("SELECT amount FROM  \'" + author.id + "' WHERE coin = '" + coin + "'")
            currentamount = c.fetchone()
            updatedamount = currentamount[0] + payment

            c.execute("SELECT worth FROM  \'" + author.id + "' WHERE coin = '" + coin + "'")
            coinworth = list(c.fetchone())
            print(coinworth)
            coinworth[0] = coinworth[0] + payment*price
            print(type(updatedamount))
            print(type(coinworth))
            print(coinworth)
            c.execute('UPDATE  \'' + author.id + "' SET amount =?, worth = ?  WHERE coin = ?",(updatedamount,coinworth[0],coin))
            conn.commit()

            await bot.say(author.name + ' you have bought ' + str(payment) + ' ' + coin + ' for a total of $' + str(round(price * payment,3)) + '.')
            await bot.say('You now own ' + str(updatedamount) + ' ' + coin + '.')

        else:
            coinworth = payment*price
            c.execute('INSERT INTO  \'' + author.id + "' (coin,amount,worth) VALUES ('" + coin + "'," + str(payment) +"," +str(coinworth) +");")
            conn.commit()
            updatedamount = payment
            await bot.say(author.name + ' you have bought ' + str(payment) + ' ' + coin + ' for a total of $' + str(round(price*payment,3)) +'.')

    elif order == "SELL":

        c.execute("SELECT 1 FROM  \'" + author.id + "' WHERE coin = '" + coin + "'")
        if c.fetchone():
            c.execute("SELECT amount FROM  \'" + author.id + "' WHERE coin = '" + coin + "'")
            currentamount = c.fetchone()

            if payment == "ALL":
                cashavaiable = cashavaiable + price* currentamount[0]
                c.execute("UPDATE users SET bank = ? WHERE userid = ?", (cashavaiable, author.id))
                conn.commit()
                c.execute('DELETE FROM  \'' + author.id + "' WHERE coin = ?", (coin,))
                conn.commit()
                await bot.say('You have sold all of your investment in ' + coin + ' for a total of $' + str(round(price * currentamount[0], 3)) + '.')

            elif payment <= currentamount[0] and payment > 0:
                updatedamount = currentamount[0] - payment
                print(updatedamount)
                c.execute('UPDATE  \'' + author.id + "' SET amount =? WHERE coin = ?",(updatedamount,coin))
                conn.commit()

                # CALCULATE CASH REMAINING AND UPDATE USERS TABLE
                cashavaiable = cashavaiable + price * payment
                print(cashavaiable)

                c.execute("UPDATE users SET bank = ? WHERE userid = ?", (cashavaiable, author.id))
                conn.commit()

                if updatedamount != 0:

                    await bot.say(author.name + ' you have sold ' + str(payment) + ' ' + coin + ' for a total of $' + str(round(price * payment,3)) + '.')
                    await bot.say('You now own ' + str(updatedamount) + ' ' + coin + '.')
                else:
                    c.execute('DELETE FROM  \'' + author.id + "' WHERE coin = ?", (coin,))
                    conn.commit()

                    await bot.say('You have sold all of your investment in ' + coin + ' for a total of $' + str(round(price * payment,3)) + '.' )

            elif payment < 0:
                await bot.say('You can not sell a negative amount of coins.')
            else:
                await bot.say('You do not own enough of ' + coin + ' to complete this order. You currently posses ' + str(currentamount[0]) + ' ' + coin + '.')

        else:
            await bot.say('You do not own any ' + coin + '.')

    else:
        await bot.say("'" + order + "' is not a valid command. Only 'BUY' and 'SELL' are commands.")

        c.close()

@bot.command()
async def crypto(coin : str):

    Allcryptos = updatecryptos()
    coin = coin.upper()
    if  coin == "HELP":
        await bot.say('Command is written as: ",crypto <currency>". Bot only can take input of cryptocoins symbol such as BTC or ETH. To get data on Ethereum one would type ",crypto ETH".')
    else:
        try:
            coinchosen = Allcryptos[coin]
            name = coinchosen['name']
            rank = coinchosen['rank']
            price = coinchosen['price_usd']
            change1hr = coinchosen['percent_change_1h']
            change24hr = coinchosen['percent_change_24h']
            change7d = coinchosen['percent_change_7d']
            lastupdate = coinchosen['last_updated']
            timeupdated = datetime.datetime.fromtimestamp(int(lastupdate)).strftime('%Y-%m-%d %H:%M:%S')
            await bot.say(name + ' is currently rank ' + rank +' and is priced at $' + price +'. The percentage change for the last hour is ' +change1hr +'% while the percentage change for 24 hours is '+ change24hr +'% and finally the percentage change for the last 7 days is '+ change7d +'%. This was last updated at '+ timeupdated +'.')
        except KeyError:
            await bot.say('Error: This input is not a cryptocurrency symbol.')

@bot.command()
async def cryptomarket():
    coinmarketcapstats = coinmarketcap.stats()
    bitcoin_percentage_of_market_cap = coinmarketcapstats['bitcoin_percentage_of_market_cap']
    total_currencies = coinmarketcapstats['active_currencies']
    total_markets = coinmarketcapstats['active_markets']
    lastupdate = coinmarketcapstats['last_updated']
    timeupdated = datetime.datetime.fromtimestamp(int(lastupdate)).strftime('%Y-%m-%d %H:%M:%S')

    await bot.say("Bitcoin is "+ str(bitcoin_percentage_of_market_cap) + "%" + " of the market. There are currently: " +str(total_currencies) +' cryptocurrencies on the market, spread through: '+ str(total_markets) +' markets on the internet.This was last updated at '+ timeupdated +'.')

@bot.command()
async def stock(symbol: str):
    symbol = symbol.upper()
    if  symbol == "HELP":
        await bot.say('Command is written as: ",stock <Stock>". Bot only can take input of stock symbol such as AAPL or GOOGL. To get data on Apple one would type ",crypto AAPL".')
    else:
        try:

            company = Pinance(symbol)
            company.get_quotes()
            stock = company.quotes_data

            price = stock['regularMarketPrice']
            percentchange = stock['regularMarketChangePercent']
            index = stock['fullExchangeName']
            lastupdate = stock['regularMarketTime']
            timeupdated = datetime.datetime.fromtimestamp(int(lastupdate)).strftime('%Y-%m-%d %H:%M:%S')
            await bot.say(symbol + ' is currently trading at $' + str(price) + ' corresponding to a ' + str(percentchange) +'% change. This was last updated at '+ timeupdated)

        except KeyError:
            await bot.say('Error: This input is not a stock symbol.')

@bot.command()
async def league(summonername : str):
    watcher = RiotWatcher('League Of Legends API required')
    my_region = 'na1'

    try:
        me = watcher.summoner.by_name(my_region, summonername)

        my_ranked_stats = watcher.league.positions_by_summoner(my_region,me['id'])
        print(my_ranked_stats)
        solo_tier = None
        solo_rank = None
        solo_win = None
        solo_lose = None
        flex_tier = None
        flex_rank = None
        flex_win = None
        flex_lose = None

        if len(my_ranked_stats) == 0:
            await bot.say('You currently aren\'t placed in rank.')
        else:
            for x in range(0,len(my_ranked_stats)):
                if my_ranked_stats[x]['queueType'] == 'RANKED_FLEX_SR':
                    flex_tier = my_ranked_stats[x]['tier']
                    flex_rank = my_ranked_stats[x]['rank']
                    flex_win = my_ranked_stats[x]['wins']
                    flex_lose = my_ranked_stats[x]['losses']
                    winpercentflex = round((flex_win/(flex_win+flex_lose))*100,1)
                elif my_ranked_stats[x]['queueType'] == 'RANKED_SOLO_5x5':
                    solo_tier = my_ranked_stats[x]['tier']
                    solo_rank = my_ranked_stats[x]['rank']
                    solo_win = my_ranked_stats[x]['wins']
                    solo_lose = my_ranked_stats[x]['losses']
                    winpercentsolo = round((solo_win/(solo_win+solo_lose))*100,1)

            if flex_rank == None and solo_rank != None:
                await bot.say(
                    'Your Solo rank is: ' + str(solo_tier) + ' ' + str(solo_rank) + ' and your Flex rank is: Unranked.')
                await bot.say('Your Solo winrate is: ' + str(winpercentsolo) + '%.')
            elif flex_rank != None and solo_rank == None:
                await bot.say('Your Solo rank is: Unranked' + ' and your Flex rank is: ' + str(flex_tier) + ' ' + str(
                    flex_rank) + '.')
                await bot.say('Your Flex winrate is: ' + str(winpercentflex) + '%.')
            elif flex_rank != None and solo_rank != None:
                await bot.say(
                    'Your Solo rank is: ' + str(solo_tier) + ' ' + str(solo_rank) + ' and your Flex rank is: ' + str(
                        flex_tier) + ' ' + str(flex_rank) + '.')
                await bot.say('Your Solo winrate is: ' + str(winpercentsolo) + '% and your Flex winrate is: ' + str(
                    winpercentflex) + '%.')

    except HTTPError as err:
        if err.response.status_code == 429:
            await bot.say('We should retry in {} seconds.'.format(e.headers['Retry-After']))
            await bot.say('this retry-after is handled by default by the RiotWatcher library')
            await bot.say('future requests wait until the retry-after time passes')
        elif err.response.status_code == 404:
            await bot.say('Summoner with that ridiculous name not found.')
        else:
                raise
                # print(my_ranked_stats)
 #    if len(my_ranked_stats) > 1:
 #
 #        flex_ranked_stats = my_ranked_stats[0]
 #        solo_ranked_stats = my_ranked_stats[1]
 #        print(flex_ranked_stats)
 #        print(solo_ranked_stats)
 #        flex_rank_tier = flex_ranked_stats['tier']
 #        flex_rank_divsion = flex_ranked_stats['rank']
 #        solo_rank_tier = solo_ranked_stats['tier']
 #        solo_rank_divsion = solo_ranked_stats['rank']
 #        await bot.say('Your Solo rank is: ' + str(solo_rank_tier) +' ' + str(solo_rank_divsion) +' and your Flex rank is: ' + str(flex_rank_tier)+ ' ' + str(flex_rank_divsion) +'.')
 #    else:
 #        await bot.say('You currently aren\'t placed in rank.')

@bot.command()
async def Help():

    embed = discord.Embed(color=discord.Colour.purple())
    embed.set_author(name='Commands')
    for i in range(0,len(functions)):
        embed.add_field(name='Command', value=functions[i])

    await bot.say(embed=embed)

# helps create a list of functions that are in this module
functions = dir()
functions.remove('HTTPError')
functions.remove('Market')
functions.remove('Pinance')
functions.remove('RiotWatcher')
functions.remove('__annotations__')
functions.remove('__builtins__')
functions.remove('__cached__')
functions.remove('__doc__')
functions.remove('__file__')
functions.remove('__loader__')
functions.remove('__name__')
functions.remove('__package__')
functions.remove('__spec__')
functions.remove('c')
functions.remove('conn')
functions.remove('datetime')
functions.remove('discord')
functions.remove('description')
functions.remove('commands')
functions.remove('json')
functions.remove('math')
functions.remove('on_ready')
functions.remove('random')
functions.remove('sqlite3')
functions.remove('sys')
functions.remove('time')

bot.run('DISCORD API REQUIRED')


import os
import re
import sys
import argparse

def parse_args():
    p = argparse.ArgumentParser(
        description='recursively search `pattern` in all files under `path`')
    p.add_argument('pattern')
    p.add_argument('path')
    return p.parse_args()

def search_file(name, pattern):
    with open(name) as file:
        for i, line in enumerate(file.readlines()):
            m = pattern.search(line)
            if m:
                print_matches(name, i+1, line, m)

def colored(msg):
    return ''.join(['\x1b[32m', msg, '\x1b[0m'])

def print_matches(filename, lineno, line, match):
    start, end = match.start(0), match.end(0)
    before, found, after = line[:start], line[start:end], line[end:]
    line = ''.join([before, colored(found), after])
    print('{}:{}:{}'.format(filename, lineno, line), end='')

def is_binary(name):
    CHUNKSIZE = 1024
    with open(name, 'rb') as file:
        while 1:
            chunk = file.read(CHUNKSIZE)
            if b'\0' in chunk:
                return True
            if len(chunk) < CHUNKSIZE:
                break
    return False

def main():
    args = parse_args()

    pattern = re.compile(args.pattern)

    for dirpath, dirnames, filenames in os.walk(args.path):
        for name in filenames:
            name = os.path.join(dirpath, name)
            if not is_binary(name):
                try:
                    search_file(name, pattern)
                except Exception as err:
                    print("Can't read file:{}:{}".format(name, str(err)), file=sys.stderr)

if __name__ == '__main__':
    main()


'''
Created on May 2, 2014

@author: A. Rupam Mahmood

'''

import numpy as np
import pylab as pl
import random
import matplotlib as mpl

class MDP(object):
  '''
  It creates a generic MDP object.
  Requires Pssa, Rssa and other structures to be specified.
  '''
  def __init__(self, params):
    '''
    Create all the necessary structure for the MDP
    '''
    self.ns         = params['ns']
    self.na         = params['na']
    self.ftype      = params['ftype'] # feature type
    if self.ftype=='tabular':   self.nf = self.ns
    elif self.ftype=='binary':  self.nf = int(np.ceil(np.log(self.ns+1)/np.log(2)))
    else: self.nf = params['nf']

    self.Rstd       = params['Rstd'] # std dev. for generating random rewards
    self.initsdist  = params['initsdist'] # initial state distribution
    self.Gamma      = params['Gamma'] # diagonal matrix of state-dependent gammas
    self.mdpseed    = params['mdpseed'] # seed to generate mdps
    self.rdmdp      = np.random.RandomState(self.mdpseed)

    if type(self.initsdist)==str:
      self.initsdistname = self.initsdist
      if self.initsdistname=='statemiddle':
        self.initsdist= np.zeros(self.ns)
        self.initsdist[(self.ns-1)/2] = 1.
    else:
      self.initsdistname = None

    self.Pssa       = self.getPssa()
    self.Rssa       = self.getRssa()
    self.bpol       = self.getBPolicy()
    (self.Pssb, self.exprb) = getPolInducedModel(self.Pssa, self.Rssa, self.bpol)
    if 'offpolicy' in params and params['offpolicy']==True:
      self.offpolicy  = True
      self.tpol       = self.getTPolicy()
      (self.Psst, self.exprt) = getPolInducedModel(self.Pssa, self.Rssa, self.tpol)
    else:
      self.offpolicy  = False
      self.tpol       = self.bpol
      (self.Psst, self.exprt) = (self.Pssb, self.exprb)

    self.Phi                = self.getPhi(self.ftype, self.ns, self.nf, rndobj=self.rdmdp)
    self.dsb                = self.steadystateprob(self.Pssb)
    if self.initsdist=='steadystate': self.initsdist = self.dsb

  def getPssa(self, params):
    raise NotImplementedError

  def getRssa(self, params):
    raise NotImplementedError

  def getBPolicy(self):
    raise NotImplementedError

  def getTPolicy(self):
    raise NotImplementedError

  def initTrajectory(self, runseed):
    self.rdrun    = np.random.RandomState(runseed)
    if self.initsdistname=='statezero':
      self.s  = 0
    elif self.initsdistname=='statemiddle':
      self.s  = (self.ns-1)/2
    else:
      self.s  = sampleFrom(self.initsdist, self.rdrun)

  def step(self):
    a = 0 if self.na==1 else self.getAction(self.bpol, self.s, self.rdrun)
    snext   = self.getNextState(self.Pssa, self.s, a, self.rdrun)
    noise   = self.rdrun.normal(0, self.Rstd) if self.Rstd>0 else 0.
    R       = self.getReward(self.Rssa, self.s, snext, a) + noise
    phi     = self.Phi[self.s]
    phinext = self.Phi[snext]
    g       = self.Gamma[self.s, self.s]
    gnext   = self.Gamma[snext, snext]
    stemp   = self.s
    self.s  = snext
    return {'s':stemp, 'phi':phi, 'act':a, \
            'R':R, 'snext':snext, 'phinext':phinext, \
            'g':g, 'gnext':gnext}

  def getRho(self, s, a):
    return self.tpol[s,a]/self.bpol[s,a]

  def isTerminal(self):
    return self.Gamma[self.s, self.s] == 0.0

  def getPhi(self, ftype, ns, nf=None, rndobj=None):
    Phi = 0.0
    if ftype=='tabular':
      Phi = np.eye(ns)
    elif ftype=='binary':
      nf = int(np.ceil(np.log(ns+1)/np.log(2)))
      Phi = np.zeros((ns, nf))
      for i in range(ns):
        for j in range(nf):
          Phi[i, nf-j-1] = ((i+1)>>j) & 1
        a = sum(Phi[i,]*Phi[i,])
        Phi[i,] = Phi[i,]/np.sqrt(a)
    elif ftype=='normal':
      Phi = np.zeros((ns, nf))
      for i in range(ns):
        for j in range(nf):
          Phi[i, j] = rndobj.normal(0, 1)
        a = sum(Phi[i,]*Phi[i,])
        Phi[i,] = Phi[i,]/np.sqrt(a)
    return Phi

  def getAction(self, pol, s, rndobj):
    rndnum = rndobj.uniform(0, 1)
    return mpl.mlab.find(rndnum<np.cumsum(pol[s,:]))[0]

  def getNextState(self, Pssa, s, a, rndobj):
    rndnum = rndobj.uniform(0, 1)
    return mpl.mlab.find(rndnum<np.cumsum(Pssa[s,:,a]))[0]

  def getReward(self, Rssa, s, snext, a):
    return Rssa[s,snext,a]

  @staticmethod
  def getFixedPoint(Pss, ExpR, Phi, ds, Gamma, Lmbda):
    (ns, ns) = np.shape(Pss)
    D = np.diag(ds)
    ImPGLinv = pl.inv(np.eye(ns)- np.dot(Pss, np.dot(Gamma, Lmbda)))
    PhiTD = np.dot(Phi.T, D)
    ImPG = np.eye(ns) - np.dot(Pss, Gamma)
    A = np.dot(np.dot(PhiTD, ImPGLinv), np.dot(ImPG, Phi))
    b = np.dot(PhiTD, np.dot(ImPGLinv, ExpR))
    thstar = np.dot(pl.inv(A), b)
    return thstar

  def steadystateprob(self, Pss):
    (eigvals, eigvecs) = pl.eig(Pss.T)
    eigi = np.argmax(eigvals)
    diotas = eigvecs[:,eigi]/sum(eigvecs[:,eigi])
    return np.real(diotas)

## general ergodic MDP, general gamma
## Pssa is of s X s' X a form

def getRandomMDP(ns, na, rndobj, rtype='uniform', rparam=1):
  Pssa = np.reshape(rndobj.uniform(0, 1, ns*ns*na) + 10**-10 , \
                                (ns, ns, na))

  rsums = np.sum(Pssa, 1)
  Pssa = Pssa / rsums[:, None]
  if rtype=='uniform':
    Rssa = np.reshape(rndobj.uniform(0, rparam, ns*ns*na),
                                (ns, ns, na))
  if rtype=='normal':
    Rssa = np.reshape(rndobj.normal(0, rparam, ns*ns*na),
                                (ns, ns, na))
  return (Pssa, Rssa)

def getPolInducedModel(Pssa, Rssa, pol):
    Pss = getPss(Pssa, pol)
    Rsa = getRsa(Rssa, Pssa)
    ExpR = getExpR(Rsa, pol)

    return (Pss, ExpR)

def followon(Pss, startstatedist):
  (ns, ns) = np.shape(Pss)
  return np.dot(pl.inv(np.eye(ns) - Pss.T), startstatedist)

def getPss(Pssa, pol):
  (ns, t1, nacts) = np.shape(Pssa)
  Pss = np.zeros((ns, ns))
  for i in range(ns):
    for j in range(ns):
      Pss[i, j] = np.dot(Pssa[i, j, ], pol[i, ])
  return Pss

def getRsa(Rssa, Pssa):
  (ns, t1, nacts) = np.shape(Rssa)
  Rsa = np.zeros((ns, nacts))
  for i in range(ns):
    for j in range(nacts):
        Rsa[i, j] = np.dot(Pssa[i,:,j], Rssa[i,:,j])
  return Rsa

def getExpR(Rsa, pol):
  (ns, t1) = np.shape(Rsa)
  ExpR = np.zeros(ns)
  for i in range(ns): ExpR[i] = np.dot(pol[i,], Rsa[i,])
  return ExpR

def getRandomlySampledPolicy(ns, na, rndobj, coverage=True):
  pol = np.reshape(np.array([rndobj.uniform(0, 1) + coverage*10**-15 for i in range(ns*na)]), (ns, na) )
  pol = np.array([ pol[i]/sum(pol[i]) for i in range(ns) ])
  return pol

def getUniformRandomPolicy(ns, na):
  pol = np.ones((ns, na))/na
  return pol

def getSkewedPolicy1(ns, na, rndobj):
  pol = np.zeros((ns, na))
  for i in range(ns):
    a = rndobj.randint(0, na)
    if na>1: pol[i,:] = 0.01/(na-1)
    pol[i, a] = 0.99
  return pol

def sampleFrom(dist, rndobj):
  rndnum = rndobj.uniform(0, 1)
  return mpl.mlab.find(rndnum<np.cumsum(dist))[0]

def getPhiIndices(Phi):
  (ns, nf) = np.shape(Phi)
  PhiIndices = [[]]*ns
  for s in range(ns):
    PhiIndices[s] = mpl.mlab.find(Phi[s]!=0)
  return PhiIndices

class PerformanceMeasure(object):
  '''
  This class takes the responsibility of measuring performance of an estimate.
  Only for on-policy case at this point.
  '''

  def __init__(self, params, prob):
    self.T            = params['T'] # number of total steps
    self.N            = params['N'] # number of data points to store
    self.Phi          = prob.Phi
    self.prob         = prob
    if 'offpolicy' in params and params['offpolicy']==True:
      self.Pss             = prob.Psst
      self.expr            = prob.exprt
    else:
      self.Pss             = prob.Pssb
      self.expr            = prob.exprb
    self.thstar       = prob.getFixedPoint(self.Pss, self.expr, prob.Phi, prob.dsb, prob.Gamma, 1.)
    self.VTrueProj    = np.dot(prob.Phi, self.thstar)
    self.D            = np.diag(prob.dsb)
    self.normFactor   = np.dot(self.VTrueProj, np.dot(self.D, self.VTrueProj))
    self.MSPVE          = np.zeros(self.N)

  def getThstarMSPBE(self, Lmbda):
    return self.prob.getFixedPoint(self.Pss, self.expr, self.Phi, self.prob.dsb, self.prob.Gamma, Lmbda)

  def calcMSPVE(self, alg, t): # t is the number of the current step
    index               = t*self.N/self.T
    msediff             = np.dot(self.Phi, alg.estimate()) - self.VTrueProj
    self.MSPVE[index]  += np.dot( np.dot(msediff, self.D), msediff)
    if np.mod(t+1, self.T/self.N)==0:
      self.MSPVE[index]  /= self.T/self.N

  def getNormMSPVE(self):
    return self.MSPVE/self.normFactor


"""empty message

Revision ID: 2edc93bcb35f
Revises: 559f3d72eb69
Create Date: 2021-09-06 23:54:24.682098

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2edc93bcb35f'
down_revision = '559f3d72eb69'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('token', sa.String(), nullable=True))
    op.add_column('users', sa.Column('expiration', sa.Integer(), nullable=True))
    op.add_column('users', sa.Column('wordpress_id', sa.Integer(), nullable=True))
    op.create_unique_constraint(None, 'users', ['wordpress_id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'users', type_='unique')
    op.drop_column('users', 'wordpress_id')
    op.drop_column('users', 'expiration')
    op.drop_column('users', 'token')
    # ### end Alembic commands ###


import pandas as pd
import  numpy as np
from PIL import Image
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
df = pd.read_csv('D:/polynomial/covid19/new_version/test_covid19.csv')
#print(df)
df=shuffle(df)
print(df.head())

reshaped_image = df["image_address"].map(lambda x: np.asarray(Image.open(x).resize((224,224), resample=Image.LANCZOS).\
                                                          convert("RGB")))

out_vec = np.stack(reshaped_image, 0)

print(out_vec.shape)

out_vec = out_vec.astype("float32")
print(out_vec.max())
out_vec /= 255

labels = df["dis_cat"].values
print(labels)
'''
X_train, X_val, y_train, y_val = train_test_split(out_vec, labels, test_size=0.10,  stratify=labels)

np.save("new_224_224_val.npy", X_val)
np.save("new_val_labels.npy", y_val)

np.save("new_224_224_train.npy", X_train)
np.save("new_train_labels.npy", y_train)

'''
np.save("D:/polynomial/covid19/new_version/new_covid19_test.npy", out_vec)
np.save("D:/polynomial/covid19/new_version/new_covid19_test_labels.npy", labels)


from model import unet
import cv2
import numpy as np
import os
import torch
from torch.utils.data.dataset import Dataset
import torchvision.transforms as transforms
from tqdm import tqdm
# os.environ['CUDA_VISIBLE_DEVICES'] = '3'

matches = [100, 200, 300, 400, 500, 600, 700, 800]

input_path = './test_A/'
output_path = './results/'

class TestDataset(Dataset):
    def __init__(self, input_path, transform):
        self.filename = os.listdir(input_path)
        self.transform = transform

    def __getitem__(self, index):
        image_path = os.path.join(input_path, self.filename[index])
        img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
        img_index = os.path.splitext(self.filename[index])[0]
        return self.transform(img), img_index  # 返回单张测试图片和其对应的文件名编号， 编号用于保存预测出的标签图

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

def predict(weights_path):
    model = unet(num_classes=len(matches)).cuda()
    model.load_state_dict(torch.load(weights_path))
    model.eval()
    with torch.no_grad():
        for input, img_index in tqdm(test_loader):   # type(img_index): list
            input = input.cuda()
            outputs = model(input)
            outputs = outputs.data.cpu().numpy() # outputs： (batch_size, 8,256,256)

            for idx, output in enumerate(outputs):
                output = output.argmax(axis=0)  # (256,256)
                save_img = np.zeros((256, 256), dtype=np.uint16)
                for i in range(256):
                    for j in range(256):
                        save_img[i][j] = matches[int(output[i][j])]
                cv2.imwrite(os.path.join(output_path, img_index[idx] + '.png'), save_img)

if __name__ == "__main__":

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])

    test_loader = torch.utils.data.DataLoader(
        TestDataset(input_path, transform),
        batch_size=16, shuffle=False, num_workers=8, pin_memory=True
    )

    weights_path = './baseline.pt'
    predict(weights_path=weights_path)


#!/usr/bin/python3

##
## http://quotes.natune.net/quoteservice/rss2/zitat-des-tages.xml
##

import urllib3
import xml.etree.ElementTree as ET
import sys

def onlinezitat():
    http = urllib3.PoolManager()
    r = http.request('GET', 'http://quotes.natune.net/quoteservice/rss2/zitat-des-tages.xml')
    ### status 200 is OK
    #print ("%s" % r.status)
    if(r.status != 200):
        print("[!!] Webseite kann nicht aufgerufen werden: %s" % r.status)
        sys.exit(0)

    ### output in utf8
    #print ("%s" % r.data.decode('utf-8'))
    urltext = r.data.decode('utf-8')

    root =  ET.fromstring(urltext)
    root.findall('.')
    for xline in root.findall('./channel/item'):
        zitat = xline.find('description').text
        author = xline.find('author').text
        print ('\x1b[4;33;40m[*] Online Zitat des Tages:\x1b[0m\n\n\x1b[4;34;40m"%s"\x1b[0m \n\t\t\t\t -- %s' % (zitat, author))

def main():
    onlinezitat()

if __name__ == "__main__":
    main()


import math
import copy
import numpy as np
import matplotlib.pyplot as plt

isdebug = False

# 指定k个高斯分布参数。这里指定k=2。注意2个高斯分布具有同样均方差Sigma，分别为Mu1,Mu2。
def ini_data(Sigma,Mu1,Mu2,k,N):
    global X   # 记录正态分布的点
    global Mu  # 记录整体模型的Mu
    global Expectations  # 记录整体期望值
    X = np.zeros((1,N))  # 创建一个1×N的矩阵, 存储模型点的取值
    Mu = np.random.random(2)  # 随机初始化整体模型的Mu包含两个元素
    Expectations = np.zeros((N,k))  # 初始化期望值
    for i in range(0,N):
        if np.random.random(1) > 0.5:  # 随机生成点, 两个子模型的生成的点数一致
            X[0, i] = np.random.normal()*Sigma + Mu1  # Mu1的正态分布
        else:
            X[0, i] = np.random.normal()*Sigma + Mu2  # Mu2的正态分布
    if isdebug:
        print("***********")
        print(u"初始观测数据X：")
        print(X)

# EM算法：步骤1，计算E[zij]
def e_step(Sigma,k,N):
    global Expectations  # 记录整体期望值
    global Mu  # 记录整体模型的Mu
    global X  # 记录正态分布的点
    for i in range(0, N):  # 遍历这整体的N个点
        Denom = 0  # 记录期望值的分母
        # 计算期望值, 公式：http://img.blog.csdn.net/20140820211401271?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hhc2RtZW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
        # 与书本公式对应只是符号不同
        for j in range(0,k):
            # 计算分母
            Denom += math.exp((-1/(2*(float(Sigma**2))))*(float(X[0,i]-Mu[j]))**2)
        for j in range(0,k):  # 遍历模型
            # 计算分子
            Numer = math.exp((-1/(2*(float(Sigma**2))))*(float(X[0,i]-Mu[j]))**2)
            Expectations[i,j] = Numer / Denom  # 计算每个模型的对于每个点的期望值
    if isdebug:
        print("***********")
        print("隐藏变量E（Z）：")
        print(Expectations)
# EM算法：步骤2，求最大化E[zij]的参数Mu
def m_step(k,N):
    global Expectations  # 记录整体期望值
    global X  # 记录正态分布的点
    # 迭代Mu
    for j in range(0,k): # 遍历模型
        Numer = 0
        Denom = 0
        for i in range(0,N):
            Numer += Expectations[i,j]*X[0,i]  # 母子
            Denom += Expectations[i,j]  # 分母
        Mu[j] = Numer / Denom  # 迭代Mu

# 算法迭代iter_num次。或达到精度Epsilon停止迭代
def run(Sigma,Mu1,Mu2,k,N,iter_num,Epsilon):
    '''

    :param Sigma: 方差
    :param Mu1: 模型一的参数
    :param Mu2: 模型二的参数
    :param k: 混合模型包含k个高斯参数预计
    :param N: 每个模型有N个点
    :param iter_num: 最大迭代次数
    :param Epsilon: 控制退出的精确度阀值
    :return: Nan
    '''
    ini_data(Sigma,Mu1,Mu2,k,N)
    print(u"初始<u1,u2>:", Mu)
    for i in range(iter_num):
        Old_Mu = copy.deepcopy(Mu)  # 记录迭代前的Mu
        e_step(Sigma,k,N)  # E步
        m_step(k,N)  # M步
        print(i,Mu)
        if sum(abs(Mu-Old_Mu)) < Epsilon:  # 迭代变化小于阀值, 退出
            break

if __name__ == '__main__':
   run(6, 40, 20, 2, 1000, 1000, 0.0001)
   plt.hist(X[0,:],50)
   plt.show()

EM算法高斯混合模型2.py


import requests
from bs4 import BeautifulSoup
import schedule
import time
import re

def scrape_website():
    url = 'https://www.topstocktips.com/tipbn'
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')

    # Get the page title
    title = soup.title.text
    print(f"Page title: {title}")

    # Get the compensation message
    compensation_element = soup.find('span', text=re.compile('We have been compensated up to'))
    if compensation_element is not None:
        compensation_text = compensation_element.text
        print(f"Compensation message: {compensation_text}")

schedule.every().minute.do(scrape_website)

while True:
    schedule.run_pending()
    time.sleep(1)


import requests
import pytest

# class Test_search():
# 加推公司
def test_recommendCompany():
    url = "http://192.168.101.102/api/search/app/company/recommendCompany"
    res = requests.get(url)
    print(res.json())
    code = res.json()['code']
    try:
        assert code == 0
        print("成功")
    except:
        print("失败")


####
# python calc_gcc.py genome.fa annotation.gtf <gene|exon>
####
from pyfaidx import Fasta
import sys
from Bio.SeqUtils import GC
genome_file=sys.argv[1]
annotation_file=sys.argv[2]
# genome_file='/mnt/lab_data/montgomery/shared/genomes/hg19/hg19.fa'
# annotation_file='/srv/persistent/bliu2/shared/annotation/gtex/gencode.v19.genes.v6p.hg19.gtf'

genome = Fasta(genome_file)

if sys.argv[3]=='gene':
	with open(annotation_file,'r') as f:
		for line in f:
			if "##" in line: continue
			split_line=line.strip().split('\t')
			if split_line[2]=='gene':
				chrom=split_line[0]
				start=int(split_line[3])-1
				end=int(split_line[4])
				gene_id=split_line[8].split(';')[0].replace('gene_id ','').replace('"','')
				gcc=GC(genome[chrom][start:end].seq)
				sys.stdout.write("%s\t%s\n"%(gene_id,gcc))

elif sys.argv[3]=='exon':
	with open(annotation_file,'r') as f:
		for line in f:
			if "##" in line: continue

			split_line=line.strip().split('\t')
			if split_line[2]=='gene':
				gene_id=split_line[8].split(';')[0].replace('gene_id ','').replace('"','')
				try:
					gcc=GC(seq)
					sys.stdout.write("%s\t%s\n"%(gene_id_bak,gcc))
				except NameError:
					pass

				# initialize:
				seq=""
				gene_id_bak=gene_id

			if split_line[2]=='exon':
				chrom=split_line[0]
				start=int(split_line[3])-1
				end=int(split_line[4])
				seq+=genome[chrom][start:end].seq
		gcc=GC(seq)
		sys.stdout.write("%s\t%s\n"%(gene_id,gcc))

else:
	sys.stderr.write('3rd option must be either gene or exon')


from bisect import bisect
from enum import Enum
from hashlib import md5
from typing import List, Dict, Optional

# Default amount of replicas per node
RING_REPLICAS = 16
NODE_ID = 'node_id'
NODE_WGT = 'weight'
NODE_VN = 'v_nodes'

def _default_hash_fn(key) -> int:
    return int(md5(str(key).encode("utf-8")).hexdigest(), 16)

class RingObjectType(Enum):
    OBJ = 1
    ID = 2

class RingHash:
    """
    The basic idea is that each server is mapped to a point on
    a circle (You can think of the circle as all integers 0 ..2³²-1.)
    with a hash function. To lookup the server for a given key, you hash
    the key and find that point on the circle.
    Then you scan forward until you find the first hash value for any server.
    In practice, each server appears multiple times on the circle. \
    These extra points are called “virtual nodes”, or “vnodes”  or replicas.

    Node look up take O(log(n)) time.
    ring hash should be used if number of nodes quite stable, in other case
    highest random weight (HRW) hashing preferable
    """

    def __init__(self, nodes=None, replicas_cnt=None, hash_fn=None):
        """
        create ring hash structure

        :param nodes: list is nodes ids/ configurations
        :param replicas_cnt: number of replicas/vNodes of each node
        :param hash_fn: a hash function

        Examples:
        >>> from hashing.ring_hash_v2 import RingHash
        >>> ring = RingHash([3, 6, 9], 1)
        >>> node1 = ring.get(3)
        >>> node2 = ring.get(100)
        """
        self._keys = []
        self._nodes = {}
        self._ring = {}

        if hash_fn and not hasattr(hash_fn, "__call__"):
            raise TypeError("hash_fn should be a callable function")
        self._hash_fn = hash_fn or _default_hash_fn
        self._default_replicas = replicas_cnt or RING_REPLICAS

        self.add(nodes)

    def _get_hash(self, node_name: str, w: int) -> int:
        key = f"{node_name}-{w}"
        return self._hash_fn(key)

    def _create_node(self, node) -> Dict[str, any]:
        if isinstance(node, str) or isinstance(node, int):
            node = {NODE_ID: node}
        elif not isinstance(node, dict):
            raise ValueError(
                f"node should be a string or a dict got {type(node)}"
            )
        if NODE_ID not in node:
            raise ValueError(
                f"'{NODE_ID}' property should be present in dict"
            )
        if NODE_WGT not in node:
            node[NODE_WGT] = 1
        elif not isinstance(node[NODE_WGT], int) or node[NODE_WGT] < 1:
            raise ValueError(
                f"node weight '{node[NODE_WGT]}' should be int and be grate than 0"
            )
        if NODE_VN not in node:
            node[NODE_VN] = self._default_replicas
        elif not isinstance(node[NODE_VN], int) or node[NODE_VN] < 1:
            raise ValueError(
                f"node replicas count '{node[NODE_VN]}' should be int and be grate than 0"
            )
        return node

    def _add_nodes(self, nodes: List[Dict[str, any]]):
        """add nodes into the ring"""
        _nodes = []
        for node in nodes:
            _node = self._create_node(node)
            if _node[NODE_ID] in self._nodes:
                raise ValueError(
                    f"node with the name {_node[NODE_ID]} already had been added"
                )
            _nodes.append(_node)

        for node in _nodes:
            node_id = node[NODE_ID]
            self._nodes[node_id] = node
            for w in range(0, node[NODE_VN] * node[NODE_WGT]):
                _hash = self._get_hash(node_id, w)
                self._ring[_hash] = node_id
        self._keys = sorted(self._ring.keys())

    def _get_node_id(self, node_id: str) -> str:
        """return node Id for given key"""
        _hash = self._hash_fn(node_id)
        inx = bisect(self._keys, _hash)
        if inx == len(self._keys):
            inx = 0
        return self._ring[self._keys[inx]]

    def remove(self, node_id: str) -> Dict[str, any]:
        """remove node from the ring"""
        if node_id not in self._nodes:
            raise ValueError(
                f"node with id '{node_id}' not found"
            )
        node: Dict[str, any] = self._nodes.pop(node_id)
        for w in range(0, node[NODE_VN] * node[NODE_WGT]):
            _hash = self._get_hash(node_id, w)
            del self._ring[_hash]
        self._keys = sorted(self._ring.keys())
        return node

    def get(self, key: Optional[str, int], res_type: Optional[RingObjectType] = None):
        """getting node for given key"""
        node_id = self._get_node_id(key)
        if res_type is None:
            res_type = RingObjectType.ID
        if res_type == RingObjectType.ID:
            return node_id
        node = self._nodes[node_id]
        return node

    def add(self, nodes):
        """add node(s) to ring hash"""
        if nodes is None:
            return
        if not isinstance(nodes, list):
            nodes = [nodes]
        self._add_nodes(nodes)

    def __str__(self):
        return f"ring_hash [len={len(self._nodes)}]"

    def __len__(self):
        """size of ring hash"""
        return len(self._nodes)


"""
clustering_utils.py: utilitary functions for the clustering.py module.
"""

import numpy as np
from enum import IntEnum
from .utils import find_in_sequence

class Link(IntEnum):
    """
    Represents state of coreferring links.
    Must be negative integers to not interfere with the clustering process.
    """
    NO_ANTECEDENT   = -1
    PROCESSED       = -2

def cluster_labels_to_entity_clusters(cluster_labels):
    """
    @cluster_labels is the second return from the affinity_matrix computation,
     and cluster_labels[mention_idx] = mention's cluster
    """
    clusters = dict()
    for idx, label in enumerate(cluster_labels):
        if label not in clusters:
            clusters[label] = list()
        clusters[label].append(idx)

    return [clusters[key] for key in clusters.keys()]

def coreference_links_to_entity_clusters(links):
    """
    Transforms the given array of coreference links into a set of entities (mention clusters).
    Each entity/cluster is represented by the mentions' indices.
    """
    clusters = []
    for i in range(len(links) - 1, -1, -1):
        new_cluster = set()
        j = i

        while True:
            antecedent = links[j]
            links[j] = Link.PROCESSED
            new_cluster.add(j)

            # end of coreference link
            if antecedent == Link.NO_ANTECEDENT:
                clusters.append(new_cluster)
                break
            # linking to previously processed cluster
            elif antecedent == Link.PROCESSED:
                previous_cluster_idx = find_in_sequence(lambda s: j in s, clusters)
                clusters[previous_cluster_idx].update(new_cluster)
                break

            j = antecedent

    return clusters

def generate_affinity_matrix(document, mention_pair_predictions):
    """
    Generates an affinity/similarity matrix from the given mention-pair scores.
    @returns affinity_matrix[m1_idx, m2_idx] = affinity_score
    """
    num_mentions = len(document.mentions)
    affinity_matrix = np.ndarray(shape=(num_mentions, num_mentions), dtype=np.float32)
    affinity_matrix.fill(0)

    for idx in range(len(mention_pair_predictions)):
        i1, i2 = document.pairwise_combinations[idx]
        affinity_matrix[i1,i2] = mention_pair_predictions[idx]
        affinity_matrix[i2,i1] = mention_pair_predictions[idx]

    return affinity_matrix


from discgolfspider.items import CreateDiscItem
from discgolfspider.helpers.retailer_id import create_retailer_id

import scrapy

class FrisbeebutikkenSpider(scrapy.Spider):
    name = "frisbeebutikken"
    allowed_domains = ["frisbeebutikken.no"]
    start_urls = ["https://frisbeebutikken.no/categories/golfdisker/sort-by/1/?page=1"]

    def parse(self, response):
        for product in response.css(".product-box"):
            disc = CreateDiscItem()

            try:
                disc["name"] = product.css(".title::text").get()
                disc["image"] = product.css(".image img::attr(src)").get()

                brand = product.css(".manufacturer-box img::attr(alt)").get()
                url = product.css(".product_box_title_row a::attr(href)").get()
                disc["url"] = url
                disc["spider_name"] = self.name
                disc["brand"] = brand
                disc["retailer"] = "frisbeebutikken.no"
                disc["retailer_id"] = create_retailer_id(brand, url)
                disc["in_stock"] = int(product.css(".product::attr(data-quantity)").get()) > 0
                disc["speed"] = disc["glide"] = disc["turn"] = disc["fade"] = None

                price = -9999
                sale_price: str = product.css(".has-special-price > span::text").get()
                regular_price: str = product.css(".price::text").get()

                if sale_price:
                    price = self.price_to_int(sale_price)
                else:
                    price = self.price_to_int(regular_price)

                disc["price"] = price

                yield disc
            except Exception as e:
                self.logger.error(f"Error parsing disc: {disc['name']}({disc['url']})")
                self.logger.error(e)

        next_page = response.css(".paginator_link_next::attr(href)").get()

        if next_page is not None:
            yield response.follow(next_page, callback=self.parse)

    def price_to_int(self, price: str) -> int:
        try:
            price_number = price.strip().split(",")[0]
            return int(price_number)
        except Exception as e:
            raise ValueError(f"Could not parse price: {e}")


"""
### downloads.py

- This library was designed to download sample codes
"""
import cli
import filesystem as fs
import requests
import shutil
import time
from exceptions import exception as ex
from filesystem import wrapper as wr
from pathlib import Path
from zipfile import ZipFile

samples_folder = f'{fs.CURRENT_LOCATION}/Samples'

DICT_SAMPLES = {
    0:["GetInfo", "https://github.com/hbisneto/GetInfo/archive/refs/heads/main.zip"],
    1:["JoKenPo", "https://github.com/hbisneto/JoKenPo/archive/refs/heads/main.zip"],
    2:["MyTimeline", "https://github.com/hbisneto/MyTimeline/archive/refs/heads/main.zip"],
    3:["JupyterBridge", "https://github.com/hbisneto/JupyterBridge/archive/refs/heads/main.zip"]
}

def download_samples():
    cli.make_menu("DOWNLOAD SAMPLE CODE", separator_style=">")
    for i in DICT_SAMPLES:
        print(f'[{i+1}] - {DICT_SAMPLES[i][0]}')
    print('[0] - << Go Back')
    print()

    opt = int(input("[?]: Type the option number: "))
    cli.separator(style="<")

    if opt > len(DICT_SAMPLES):
        ex.throw.invalid_option()
        return

    if opt == 0:
        return

    # Get information to download
    app_name = DICT_SAMPLES[opt-1][0]
    app_url = DICT_SAMPLES[opt-1][1]

    # Verify if Samples folder exists
    wr.create_directory(f'{samples_folder}/{app_name}', create_subdirs=True)
    download_sample_item(app_name, app_url)

def download_sample_item(appname, url):
    cli.make_menu(f"DOWNLOADING: {appname}...", new_line = True)
    print("[Status]: Starting Download...")
    print('[Status]: Verifying repository to download...')

    try:
        FROM = f'{fs.CURRENT_LOCATION}/main.zip'
        TO = f'{samples_folder}/{appname}/{appname}.zip'

        ServerResponse = requests.get(url, stream = True)
        FileName = url.split("/")[-1]
        with open(FileName, 'wb') as f:
            for Chunk in ServerResponse.iter_content(chunk_size = 1024):
                if Chunk:
                    f.write(Chunk)
        print(f'[Status]: "{appname}" download 100% completed!')
        print(f'[Status]: Verifying "{appname}"...')
        print(f'[Done]: "{appname}" download process complete!')
    except:
        print("[ERROR]: Could't connect to the server!")
        print(f'[Process]: Cleaning cache...')
        time.sleep(3)
        shutil.rmtree(f'{samples_folder}/{appname}/')
        shutil.rmtree
        print("[Process]: Cache cleaned!")

    try:
        Path(FROM).rename(TO)
        cli.separator()
    except:
        return

    cli.make_menu(f"EXTRACTING: {appname}...", new_line = True)
    with ZipFile(TO, 'r') as zipObj:
        zipObj.extractall(f'{samples_folder}/{appname}/')
    print(f'[Done]: "{appname}" extraction process complete!')
    cli.separator()


#importing required modules
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense

#reading the dataset
dataset = pd.read_csv('SOCR-HeightWeight.csv')
#setting x and y values
y=dataset['Weight(Pounds)']
x=dataset['Height(Inches)']

#creating model that is an empty brain
model=Sequential()
model.get_config()

#adding layers
model.add(
    Dense(
        units=1,
        activation="linear",
        kernel_initializer="zeros",
        bias_initializer="zeros",
        input_shape=(1,)
    )
)

#getting information about model summary
model.summary()

model.get_config()

#model.get_layer("dense").input
#model.get_layer("dense").output

model.compile(loss="mean_squared_error",optimizer="Adam")

#trianing model
model.fit(x, y,epochs=5)

w=model.predict([68.22])
print("Estimated Weight of that person is = ",w)


#%matplotlib inline
import matplotlib.pylab as plt
import os
import glob
import numpy as np
import math
from data_augmentation_h5py import *
import cnn_utils
import ipt_utils
import natsort
import time
import random
import tensorflow as tf
from keras.callbacks import ModelCheckpoint, EarlyStopping

os.environ["CUDA_VISIBLE_DEVICES"] = "0"
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)

'''

# -----------------------------------------------------
# IF COMBINING HEALTHY AND UNHEALTHY BRAIN
# DATASETS INTO ONE DIRECTORY WITHOUT CREATING
# A VALIDATION DIRECTORY
# -----------------------------------------------------
imgs_list = glob.glob("c:/users/fungs/bmen501/Data/Training_Orig/*.nii.gz")
masks_list = glob.glob("c:/users/fungs/bmen501/Data/Training_STAPLE/*.nii.gz")

# shuffle the images and masks
combined_list = list(zip(imgs_list, masks_list))
random.shuffle(combined_list)
imgs_list_shuf, masks_list_shuf = zip(*combined_list)

# percentage of data used for validation
val_perc = 0.25

# number of brains in validation set
val = math.ceil(val_perc*len(imgs_list))

# train and validation split
img_train = imgs_list_shuf[:-val]
mask_train = masks_list_shuf[:-val]
img_val = imgs_list_shuf[-val:]
mask_val = masks_list_shuf[-val:]

'''

# -----------------------------------------------------
# IF SPLITTING BOTH HEALTHY AND UNHEALTHY BRAINS
# INTO TRAINING AND VALIDATION DIRECTORIES
# -----------------------------------------------------
# loading all the filenames
# h5py images
img_train = glob.glob("c:/users/fungs/bmen501/Data/Train-hdf5/Image/*.h5")
mask_train = glob.glob("c:/users/fungs/bmen501/Data/Train-hdf5/Mask/*.h5")
img_val = glob.glob("c:/users/fungs/bmen501/Data/Val-hdf5/Image/*.h5")
mask_val = glob.glob("c:/users/fungs/bmen501/Data/Val-hdf5/Mask/*.h5")

# nifti images
# img_train = glob.glob("c:/users/fungs/bmen501/Data/Train/Image/*.nii.gz")
# mask_train = glob.glob("c:/users/fungs/bmen501/Data/Train/Mask/*.nii.gz")
# img_val = glob.glob("c:/users/fungs/bmen501/Data/Val/Image/*.nii.gz")
# mask_val = glob.glob("c:/users/fungs/bmen501/Data/Val/Mask/*.nii.gz")

# -----------------------------------------------------

# sort the sets
img_train = natsort.natsorted(img_train)
mask_train = natsort.natsorted(mask_train)
img_val = natsort.natsorted(img_val)
mask_val = natsort.natsorted(mask_val)

print(img_train[268])
print(mask_train[268])

# training set data generator
train_dg = DataGeneratorH5PY2D(img_train, mask_train, (96,96), (30,30), 36,
                 rot = 30, shift_x = 15, shift_y = 10,
                 scale = 0.6, shear = 15, flip_x = True, flip_y = True, shuffle = True)

# validation set data generator
val_dg = DataGeneratorH5PY2D(img_val, mask_val, (96,96), (30,30), 9,
                  rot = 30, shift_x = 15, shift_y = 10,
                  scale = 0.6, shear = 15, flip_x = True, flip_y = True, shuffle = True)

# t  =time.time()
# X,Y = train_dg.__getitem__(10)
# t = time.time() -t
# print(t)

# string used to save models after each epoch
model_name = "./Network/unet_data_best_15pat.hdf5"

# early stopping callback to shut down training after
# 10 epochs with no improvement
earlyStopping = EarlyStopping(monitor='val_dice_coef', patience=15, verbose=0, mode='max')

# checkpoint callback to save model  along the epochs
checkpoint = ModelCheckpoint(model_name, mode = 'max', monitor='val_dice_coef',verbose=0,
                             save_best_only=False, save_weights_only = True)

# create model
model = cnn_utils.get_unet_mod(patch_size = (96,96), nchannels = 1, learning_rate = 1e-4,
                                learning_decay = 5e-9)

hist = model.fit(train_dg, epochs=500, verbose = 1, validation_data = (val_dg),
                    callbacks = [checkpoint, earlyStopping], max_queue_size = 20, workers = 12)

# saving training history
np.save("./network/dice_15pat.npy",np.array(hist.history['dice_coef']))
np.save("./network/val_dice_15pat.npy",np.array(hist.history['val_dice_coef']))

# plotting the dice coefficient
dice = np.array(hist.history['dice_coef'])
val_dice = np.array(hist.history['val_dice_coef'])
loss = np.array(hist.history['loss'])
val_loss = np.array(hist.history['val_loss'])
print (val_loss.argmin())
plt.figure(figsize = (4,4/1.5),facecolor = "w",dpi = 450)
plt.plot(dice,'x-', markersize = 4,label = "Train")
plt.plot(val_dice,'+-',markersize = 4,label = "Validation")
plt.legend()
plt.grid()
plt.xlim(0,500)
plt.ylim(0.90,1.0)
plt.xlabel("Epoch")
plt.ylabel("Dice coefficient")
plt.show()


import sys
from collections import deque

input = sys.stdin.readline

N, M = map(int, input().split())

graph = [[] for _ in range(N + 1)]

for _ in range(M):
    a, b = map(int, input().split())
    graph[b].append(a)

def search(start):
    visited[start] = True

    que = deque()
    que.append(start)

    cnt = 1
    while que:
        v = que.popleft()

        for i in graph[v]:
            if not visited[i]:
                que.append(i)
                cnt += 1
    rel[start] = cnt

rel = [0] * (N + 1)

for i in range(1, N + 1):
    visited = [False] * (N + 1)
    search(i)

max_value = max(rel)

for i in range(len(rel)):
    if max_value == rel[i]:
        print(i, end=' ')

백준-효율적인해킹.py