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

"""
Juego Ultimate Tic-Tac-Toe, al cual vamos a llamar "MetaGato"

Es una especie de gato de 81 casillas que se juega mucho en el centro de
cómputo de la LCC/UNISON. Básicamente es un gato compuesto de gatos

Las reglas se pueden consultar en
https://en.wikipedia.org/wiki/Ultimate_tic-tac-toe

"""

from busquedas_adversarios import JuegoSumaCeros2T
from busquedas_adversarios import minimax
from busquedas_adversarios import minimax_t
import tkinter as tk

class MetaGato(JuegoSumaCeros2T):
    """
    El juego del Meta gato, en el cual vamos a codificarlo
    como una lista donde cada estado representa un gato diferente.

    La codificación la vamos a realizar como:

    0   1  2     9 10 11    18 19 20
    3   4  5    12 13 14    21 22 23
    6   7  8    15 16 17    24 25 26

    27 28 29    36 37 38    45 46 47
    30 31 32    39 40 41    48 49 50
    33 34 35    42 43 44    51 52 53

    54 55 56    63 64 65    72 73 74
    57 58 59    66 67 68    75 76 77
    60 61 62    69 70 71    78 79 80

    """
    def __init__(self, jugador=1):
        """
        Inicializa el juego del gato

        """
        self.x0 = 81 * [0] + [None]
        self.x = 81 * [0] + [None]
        self.metagato = 9 * [0]
        self.historial = []
        self.jugador = 1

    def jugadas_legales(self):
        gato = self.x[-1]
        if gato is None:
            return range(81)
        if self.metagato[gato] == 0 and 0 in self.x[gato*9: gato*9+9]:
            return [i for i in range(9*gato, 9*gato + 9) if self.x[i] == 0]
        return ([i for i in range(81) if self.x[i] == 0 and
                    self.metagato[i//9] == 0])

    def terminal(self):
        if 0 not in self.x:
            return 0
        gato = self.historial[-1][1]

        x = self.x[9 * gato: 9 * gato + 9]
        if self.metagato[gato] == 0:
            self.metagato[gato] = self.final_gato(x)
        res = self.final_gato(self.metagato)
        return None if res == 0 else res

    # Supongo que final_gato dictamina si hay metagato
    # Si hay empate, regresa 2. Es lo que se conoce como "lo gano el gato"
    @staticmethod
    def final_gato(x):
        if x[4] != 0 and (x[0] == x[4] == x[8] or x[2] == x[4] == x[6]):
            return x[4]
        for i in range(3):
            if x[3 * i] != 0 and x[3 * i] == x[3 * i + 1] == x[3 * i + 2]:
                return x[3 * i]
            if x[i] != 0 and x[i] == x[i + 3] == x[i + 6]:
                return x[i]
        if all(a != 0 for a in x):
            return 2
        return 0

    def hacer_jugada(self, jugada):
        if self.x[-1] is None:
            self.x[-1] = jugada // 9
        self.historial.append((jugada, jugada//9))
        self.x[jugada] = self.jugador
        self.x[-1] = jugada % 9
        self.jugador *= -1

    def deshacer_jugada(self):
        jugada, gato = self.historial.pop()
        self.x[jugada] = 0
        self.x[-1] = gato
        self.jugador *= -1
        self.deshacer_meta()

    def deshacer_meta(self):
        l = []
        for i in range(9):
            l.append(self.final_gato(self.x[i*9:i*9+9]))
            if l[i] != self.metagato[i]:
                self.metagato[i] = l[i]

    def semifinal_gato(self, x, j):
        for i in range(9):
            #manera medio bonita de checar si hay semifinal con la de en medio
            if x[4] == j and x[8 - i] == x[4] and i != 4 and x[i] == 0:
                return x[4]

            # Checa verticalmente
            if x[i] == j and ((x[(i + 3) % 9] == x[i] or x[(i + 6) % 9] == x[i])
                and (x[(i + 3) % 9] == 0 or x[(i + 6) % 9] == 0)):
                return x[i]
        #Checo horizontalmente
        for i in [0,3,6]:
            if x[i] == j and ((x[i + 1] == x[i] or x[i + 2] == x[i])
                and (x[i + 1] == 0 or x[i + 2] == 0)):
                return x[i]
            if x[i + 1] == j and ((x[i + 1] == x[i] or x[i + 1] == x[i + 2])
                and (x[i] == 0 or x[i + 2] == 0)):
                return x[i]
            if x[i + 2] == j and ((x[i + 2] == x[i + 1] or x[i + 2] == x[i])
                and (x[i + 1] == 0 or x[i] == 0)):
                return x[i]
        return 0

def utilidad_uttt(juego):
    """
    Resultados y comentarios generales de esta funcion de utilidad:

    Con una profundidad igual a 4 y dando el primer turno a la IA, no vimos
    que perdiera ni una sola vez. Probe esta madre en el centro de computo
    con alrededor de 10 personas y dos lograron empatarla. En el caso de que
    no empiece ella, yo logre ganarle 1 vez, y las demas veces perdi.

    Es bastante curioso hacer este juego con minimax, ya que los malos movim-
    ientos no afectan bastante al arbol que se hace en minimax.

    Con profundidad igual a 3 tambien nos gana, pero es mucho mas rapida.
    Si se llegara a optimizar aun mas, podriamos dejarla en 4 para que sea el
    nivel mas complicado. Por eso recomiendo dejarla en 3.

    @param juego: objeto juego del gato. Es necesario ya que necesitamos informa
    cion del juego, como el jugador en turno, los metagatos, etc. Si yo
    la optimizara, haria que los semifinal_gato sean atributos del objeto
    juego, para no tener que calcularlos.

    @return: una utilidad entre 0 y 1
    """
    #sf es semi finales
    mal_mov = 0
    if juego.metagato[juego.x[-1]] != 0:
        mal_mov = -1 * juego.jugador

    sf_circ = [0 for _ in range(9)]
    sf_equis = sf_circ[:]
    for i in range(9):
        sf_equis[i] = juego.semifinal_gato(juego.x[9 * i: 9 * i + 9], 1)
        sf_circ[i] = juego.semifinal_gato(juego.x[9 * i: 9 * i + 9], -1)
    st_equis = juego.semifinal_gato(juego.metagato, 1)
    st_circ = juego.semifinal_gato(juego.metagato, -1)

    return ((3 * sum(juego.metagato) + mal_mov + sum(sf_circ) + sum(sf_equis) +
        5 * st_circ + 5 * st_equis + juego.x[40])/100)
class MetaGatoTK:
    def __init__(self, escala=1):

        self.app = app = tk.Tk()
        self.app.title("El juego del meta gato")
        self.L = L = int(escala) * 50

        tmpstr = "Escoge, X siempre empiezan"
        self.anuncio = tk.Message(app, bg='white', borderwidth=1,
                                  justify=tk.CENTER, text=tmpstr,
                                  width=3 * L)
        self.anuncio.pack()

        barra = tk.Frame(app)
        barra.pack()
        botonX = tk.Button(barra,
                           command=lambda x=True: self.jugar(x),
                           text='(re)iniciar con X')
        botonX.grid(column=0, row=0)
        botonO = tk.Button(barra,
                           command=lambda x=False: self.jugar(x),
                           text='(re)iniciar con O')
        botonO.grid(column=1, row=0)

        ctn = tk.Frame(app, bg='black')
        ctn.pack()
        gatos = [None for _ in range(9)]
        self.tablero = [None for _ in range(81)]
        self.textos = [None for _ in range(81)]
        letra = ('Helvetica', -int(0.9 * L), 'bold')

        for meta in range(9):
            gatos[meta] = tk.Frame(ctn, bg='black', borderwidth=3)
            gatos[meta].grid(row=meta // 3, column=meta % 3)

            for i in range(9):
                ind = meta * 9 + i
                self.tablero[ind] = tk.Canvas(
                    gatos[meta], height=L, width=L,
                    bg='light grey', borderwidth=0
                )
                self.textos[ind] = self.tablero[ind].create_text(
                    L // 2, L // 2, font=letra, text=' '
                )
                self.tablero[ind].grid(row=i // 3, column=i % 3)
                self.tablero[ind].val = 0
                self.tablero[ind].pos = ind

    def jugar(self, primero):
        juego = MetaGato()
        self.actualiza_tablero(juego.x)

        if not primero:
            jugada = minimax(juego, dmax=0, utilidad=utilidad_uttt)
            juego.deshacer_meta()
            juego.hacer_jugada(jugada)
            self.actualiza_tablero(juego.x)

        self.anuncio['text'] = "A ver de que cuero salen más correas"
        for _ in range(81):
            jugada = self.escoge_jugada(juego)
            juego.hacer_jugada(jugada)
            self.actualiza_tablero(juego.x)
            ganador = juego.terminal()
            if ganador is not None:
                break
            # Profundidad 3: tiempos muy buenos en todos los escenarios
            # Profundidad 4: tiempos buenos en la mayoria de los escenarios.
            # cuando hay muchos gatos ganados, y muchas jugadas legales
            # puede tardarse hasta 5 seg en una computadora "estandar"
            jugada = minimax(juego, dmax=3, utilidad=utilidad_uttt)
            juego.deshacer_meta()
            juego.hacer_jugada(jugada)
            self.actualiza_tablero(juego.x)
            ganador = juego.terminal()
            if ganador is not None:
                break

        finstr = ("UN ASQUEROSO EMPATE, aggggg" if ganador == 2 else
                  "Ganaste, bye"
                  if (ganador == 1 and primero) or (ganador < 0 and not primero)
                  else "¡Gané¡  Juar, juar, juar.")
        self.anuncio['text'] = finstr
        self.anuncio.update()

    def escoge_jugada(self, juego):
        jugadas_posibles = list(juego.jugadas_legales())
        if len(jugadas_posibles) == 1:
            return jugadas_posibles[0]

        seleccion = tk.IntVar(self.tablero[0].master, -1, 'seleccion')

        def entrada(evento):
            evento.widget.color_original = evento.widget['bg']
            evento.widget['bg'] = 'grey'

        def salida(evento):
            evento.widget['bg'] = evento.widget.color_original

        def presiona_raton(evento):
            evento.widget['bg'] = evento.widget.color_original
            seleccion.set(evento.widget.pos)

        for i in jugadas_posibles:
            self.tablero[i].bind('<Enter>', entrada)
            self.tablero[i].bind('<Leave>', salida)
            self.tablero[i].bind('<Button-1>', presiona_raton)

        self.tablero[0].master.wait_variable('seleccion')

        for i in jugadas_posibles:
            self.tablero[i].unbind('<Enter>')
            self.tablero[i].unbind('<Leave>')
            self.tablero[i].unbind('<Button-1>')
        return seleccion.get()

    def actualiza_tablero(self, x):
        for i in range(81):
            if self.tablero[i].val != x[i]:
                self.tablero[i].itemconfigure(self.textos[i],
                                              text=' xo'[x[i]])
                self.tablero[i].val = x[i]
                self.tablero[i].update()

    def arranca(self):
        self.app.mainloop()

if __name__ == '__main__':
    MetaGatoTK().arranca()


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

from bs4 import BeautifulSoup
import cloudscraper
import pandas as pd
import datetime
import os
import requests
import re

now = datetime.datetime.now()
dir_path = os.path.dirname(os.path.abspath(__file__))
CSVLOC = os.path.join(dir_path, 'csv', 'archive')

# Search for all CSV files to get all dates in a list
dates = []
for item in os.listdir(CSVLOC):
    if os.path.isfile(CSVLOC + '/' + item) and item != 'history.csv':
        dates.append(item.replace('rki_', '').replace('.csv', ''))

# Call to RKI page to get HTML as string back
url = 'https://www.rki.de/DE/Content/InfAZ/N/Neuartiges_Coronavirus/Fallzahlen.html'
scraper = cloudscraper.create_scraper()
req = scraper.get(url).text
soup = BeautifulSoup(req, 'html.parser')

main = soup.find('div', {'id': 'main'})

# Get RKI update time and format it and set it for csv file later
text = main.findAll('p')
rki_update_time = ''
for p in text:
    part = p.get_text()
    try:
        rki_update_time = re.sub(r"Stand:.* (\d+)\.(\d+)\.(\d+).*um\s(\d+):(\d+).*", r"\3-\2-\1 \4:\5", part)
        rki_update_time = pd.to_datetime(rki_update_time)
        rki_update_time = rki_update_time.strftime('%Y-%m-%d_%H_%M')
    except Exception:
        rki_update_time = ''
    if rki_update_time != '':
        break
rki_update_time = pd.to_datetime(rki_update_time)
rki_update_time = rki_update_time.strftime('%Y-%m-%d_%H_%M')
curr_minute = now.strftime("%Y-%m-%d %H:%M")

# Nothing to do, when data already exists
if rki_update_time in dates:
    print('CSV File from Datetime exits')
    exit(1)

# Get cases table from RKI and parse into a Dataframe
table = main.find('table')
columns = ['Bundesland', 'confirmed', 'confirmed_diff', 'extra', 'extra2', 'deaths']
df = pd.read_html(str(table), thousands='.', decimal=',')[0]

# Format DataFrame
df.columns = columns
df['date'] = pd.to_datetime(curr_minute)
df['confirmed'] = df.confirmed.astype(str).str.replace(r'\.', '').astype(int)
df = df[['Bundesland', 'date', 'confirmed', 'deaths']]

# Set csv filename and write DataFrame into it
csv_file = os.path.join(dir_path, 'csv', 'archive', 'rki_' + rki_update_time + '.csv')
df.to_csv(csv_file)
print('Write data to CSV file ' + str(csv_file))


import random

from itertools import repeat

import os

from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, select
from sqlalchemy import func

import concurrent.futures
import multiprocessing

import logging
import sys

'''
This script can be used as template to write in sqlite database using multiprocess
It can be used when data to insert requires reading and heavy computation
Sqlite's WAL allows read while writing
'''

# Logging stuff
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(relativeCreated)d ms - %(processName)s -  %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)

# DB stuff
db_file = "multi_process_wal_test.sqlite"
connection_string = 'sqlite:///' + db_file
metadata = MetaData()
table1 = Table('table1', metadata,
              Column('id', Integer, primary_key=True),
              Column('number', Integer))

def write_something(lock, n=100000):

    engine = create_engine(connection_string, echo=False)
    conn = engine.connect()

    # read
    s = select([table1.c.number])
    r = conn.execute(s).fetchall()

    # Calculation
    sorted(zip(*r))

    # write whatever you have calculated
    logging.info('before lock')
    with lock:
        logging.info('lock acquired')
        conn.execute(table1.insert(), [{'number': random.random()} for _ in range(n)])
    logging.info('lock released')

    # read
    s = select([func.count(table1)])
    r = conn.execute(s).fetchone()
    return r[0]

def main():

    nb_cores = 6
    nb_lines = 300000

    try:
        os.remove(db_file)
        logging.info('file removed')
    except:
        logging.info('file NOT removed')

    # Generate db
    engine = create_engine(connection_string, echo=False)
    conn = engine.connect()
    metadata.create_all(conn)
    conn.execute('PRAGMA journal_mode=WAL;')	# sqlite feature

    # Shared lock between processes
    m = multiprocessing.Manager()
    lock = m.Lock()

    # Write something
    logging.info('simple writing {0} lines'.format(nb_lines))
    write_something(lock,nb_lines)
    logging.info('finish writing')

    # Write same amount of lines using n processors
    logging.info('writing {0} lines with {1} cores'.format(nb_lines, nb_cores))
    with concurrent.futures.ProcessPoolExecutor(max_workers=nb_cores) as executor:
        results = executor.map(write_something, repeat(lock,nb_cores), repeat(int(nb_lines/nb_cores)))
    logging.info('finish writing')
    print(list(results))

if __name__ == "__main__":
    main()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('systemofdingh', '0003_auto_20150424_0704'),
    ]

    operations = [
        migrations.AlterField(
            model_name='orderdetailmodel',
            name='orderID',
            field=models.ForeignKey(to='systemofdingh.orderModel'),
        ),
    ]


import webapp2
from google.appengine.ext import db
import cgi
from jinja2 import Environment, FileSystemLoader
import os
import datamodel

jinja_environment = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__),"templates")))

lat = 0
lon = 0

import math

def calc_distance(destination):
    global lat, lon
    lat1, lon1 = float(lat), float(lon)
    lat2, lon2 = float(destination.location.lat), float(destination.location.lon)
    radius = 6371 # km

    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
        * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    d = radius * c
    return d

class ResultsPage(webapp2.RequestHandler):
    def get(self):
        global lat
        global lon
        location = cgi.escape(self.request.get('location'))
        lat = cgi.escape(self.request.get('location_lat'))
        lon = cgi.escape(self.request.get('location_lng'))

        activities = db.GqlQuery("SELECT * "
                            "FROM Entry WHERE type='Activity'; ").fetch(1000)
        activities.sort(key=calc_distance)
        restaurants = db.GqlQuery("SELECT * "
                            "FROM Entry WHERE type='Restaurant'; ").fetch(1000)
        restaurants.sort(key=calc_distance)
        bars = db.GqlQuery("SELECT * "
                            "FROM Entry WHERE type='Bar'; ").fetch(1000)
        bars.sort(key=calc_distance)
        template = jinja_environment.get_template('results.html')
        self.response.out.write(template.render(
            {'location':location,
             'lat':lat,
             'lon':lon,
             'activities':activities[:10],
             'restaurants':restaurants[:10],
             'bars':bars[:10]}))

app = webapp2.WSGIApplication([('/resultspage.*', ResultsPage)])


"""Renders admin pages for ops and other management tasks.

Currently just /admin/responses, which shows active responses with tasks that
haven't completed yet.
"""

import datetime
import json

import appengine_config
from activitystreams.oauth_dropins.webutil import handlers
import facebook
import googleplus
from models import Response
import instagram
import twitter
import util

from google.appengine.ext import ndb
import webapp2

class ResponsesHandler(handlers.TemplateHandler):
  NUM_RESPONSES = 30

  def template_file(self):
    return 'templates/admin_responses.html'

  def template_vars(self):
    responses = []

    # Find the most recently propagated responses with error URLs
    for r in Response.query().order(-Response.updated):
      if (len(responses) >= self.NUM_RESPONSES or
          r.updated < datetime.datetime.now() - datetime.timedelta(hours=1)):
        break
      elif not r.error or r.status == 'complete':
        continue

      # r.source = r.source.get()
      r.links = [util.pretty_link(u, new_tab=True) for u in r.error + r.failed]
      r.response = json.loads(r.response_json)
      r.activities = [json.loads(a) for a in r.activities_json]

      responses.append(r)

    responses.sort(key=lambda r: (r.source, r.activities, r.response))
    return {'responses': responses}

class MarkCompleteHandler(util.Handler):
  def post(self):
    responses = ndb.get_multi(ndb.Key(urlsafe=u)
                              for u in self.request.params.getall('key'))
    for r in responses:
      r.status = 'complete'
    ndb.put_multi(responses)
    self.redirect('/admin/responses')

application = webapp2.WSGIApplication([
    ('/admin/responses', ResponsesHandler),
    ('/admin/mark_complete', MarkCompleteHandler),
    ], debug=appengine_config.DEBUG)

if __name__ == '__main__':
  main()


class A:
    def __init__(self, name, address):
        self.name = name
        self.address = address

    def display_name(self, user_input: int):
        print("Name: {}, Address : {}, User Input :{}".format(self.name, self.address, user_input))

    def display_name(self, user_input : str):
        print("Name :", user_input)

    @staticmethod
    def static_data(n):
        fact =1
        for i in range(n, 1, -1):
            fact = fact*i
        return fact

    @classmethod
    def class_data(cls, number):
        if number > 1:
            flag = False
            for i in range(2, number):
                if number%i == 0:
                    #print("Its not Prime Number")
                    break
                else:
                    flag = True
                    #print("Its prime number")
        print("Its prime number") if flag is True else print("Its not an prime number")

    def compare(self, other):
        if self.name == other.name:
            return True
        else:
            return False

obj1 = A("Rahul", "Pune")
obj2 = A("Rahul", "Pune")

obj2.name = "Mohit"
if obj1.compare(obj2):
    print("Object are same")
else:
    print("Object are different")

#A.display_name(obj)
obj1.display_name(12345)
#print(obj.static_data(4))

#print(A.static_data(5))

#obj.class_data(11)


import os
import random
import json
import logging
import abc
import re

import arrow
import six
import requests
from time import time

from soundboard.vox import voxify
from soundboard.config import YAMLConfig, state
from soundboard.mixer import SDLMixer
from soundboard.types import sound_state

class SoundFactory:

    def __init__(self, mixer, directory, state=state):
        """:type mixer: SDLMixer"""
        # https://stackoverflow.com/questions/529240/what-happened-to-types-classtype-in-python-3
        self.mixer = mixer() if isinstance(mixer, type) else mixer
        self.state = state
        self.directory = directory

    def by_name(self, name):
        cls = self.state.sounds.by_name(name)
        return cls(mixer=self.mixer, base_dir=self.directory)

    def __getattr__(self, attr):
        def funky_wrapper(data):
            sound = self.by_name(attr)
            sound.setup(data)
            return sound
        return funky_wrapper

@six.add_metaclass(abc.ABCMeta)
class SoundInterface():

    @abc.abstractproperty
    def simple_name(self):
        pass

    @abc.abstractproperty
    def config_sounds_attribute(self):
        pass

    @abc.abstractmethod
    def play(self):
        pass

class Sound(SoundInterface):
    running = False

    def __init__(self, mixer, base_dir, data=None):
        """:type mixer: SDLMixer"""
        self.mixer = mixer
        self.dir = base_dir
        self.name = "I'm so unnammed"
        self.current_chunk = None
        self.duration_scale = 1.0

        if data:
            self.setup(data)

    def setup(self, paths, duration_scale=1.0):
        self.duration_scale = duration_scale
        paths = [os.path.join(self.dir, p) for p in paths]
        self.chunks = [self.mixer.read(p) for p in paths]

    @staticmethod
    def on_next(chunks, state):
        return chunks[(state.position + 1) % len(chunks)]

    @staticmethod
    def on_end(chunks, state):
        return None

    @staticmethod
    def on_start(chunks, state):
        return chunks[state.position if state.position else 0]

    def signal(self, name):
        func = getattr(self, 'on_' + name)
        current_state = self._get_state()
        chunk = func(self.chunks, current_state)
        return chunk

    def _get_state(self):
        chunk = self.current_chunk
        position = self.chunks.index(chunk) if chunk in self.chunks else None
        return sound_state(chunk, position)

    def _obtain_chunk(self):  # sup :3
        signals = ['start', 'next'][1 if self.running else 0:]
        chunk = next(chunk for chunk in map(self.signal, signals) if chunk)
        return chunk

    def play(self):
        self.current_chunk = self._obtain_chunk()
        self.current_chunk.play(self.duration_scale)
        self.running = True

    def play_all(self):
        for chunk in self.chunks:
            chunk.play(self.duration_scale)

    def end(self):
        self.running = False
        chunk = self.signal('end')
        if chunk:
            return chunk.play()  # noqa

@state.sounds.register
class SimpleSound(Sound):
    simple_name = 'simple'
    config_sounds_attribute = 'file'

    def setup(self, path):
        super(SimpleSound, self).setup([path])

@state.sounds.register
class RandomSound(Sound):
    simple_name = 'random'
    config_sounds_attribute = 'files'

    @staticmethod
    def on_next(chunks, state):
        return random.choice(chunks)

@state.sounds.register
class ListSound(Sound):
    simple_name = 'list'
    config_sounds_attribute = 'files'

    @staticmethod
    def on_start(chunks, state):
        return chunks[0]

@state.sounds.register
class WrappedSound(Sound):
    simple_name = 'wrapped'
    config_sounds_attribute = 'files'

    @staticmethod
    def on_next(chunks, state):
        top = len(chunks) - 1
        pos = max(1, ((state.position + 1) % top))
        return chunks[pos]

    @staticmethod
    def on_start(chunks, state):
        return chunks[0]

    @staticmethod
    def on_end(chunks, state):
        return chunks[-1]

@state.sounds.register
class VoxSound(Sound):
    simple_name = 'vox'
    config_sounds_attribute = 'sentence'
    vox_duration_scale = 1.4

    def setup(self, sentence):
        super(VoxSound, self).setup(voxify(sentence),
                                    duration_scale=self.vox_duration_scale)

    def play(self):
        super(VoxSound, self).play_all()

@state.sounds.register
class WeatherSound(Sound):
    temperature = 2137
    simple_name = 'weather'
    location = 'warsaw,pl'
    config_sounds_attribute = 'location'
    base_sentence = 'black mesa topside temperature is %d degrees'
    api_url = None

    def setup(self, location):
        if location.isdigit():
            location = int(location)
        self.location = location

    def play(self):
        sentence = self.base_sentence % self.temperature
        if self.temperature < 0:
            sentence += 'ebin'
        sound = VoxSound(data=sentence, mixer=self.mixer, base_dir=self.dir)
        sound.play()

    def update_temperature(self, temperature):
        logging.info("temperature for %s, %d -> %d",
                     self.location, self.temperature, temperature)
        self.temperature = temperature

    def get_temperature(cls, location, units='metric'):
        params = {'q': location, 'units': 'metric'}
        text = requests.get(cls.api_url, params=params).text
        json_content = json.loads(text)
        temp = json_content.get('main', {}).get('temp', None)
        return temp

@state.sounds.register
class ZTMSound(Sound):
    simple_name = 'ztm'
    config_sounds_attribute = 'line'

    def setup(self, line):
        self.line = line

    @classmethod
    def _line_humanize(self, line):
        if re.match(r'^[0-9]{1,2}$', line):
            # tram
            return 'topside train number {}'.format(line)
        elif re.match(r'^[0-9]{3}$', line):
            # day bus
            return 'day bust number {}'.format(line)
        elif re.match(r'^m[0-9]+$', line.lower()):
            # metro line
            return 'subsurface train'
        else:
            return 'transportation'

    def _get_transport(self):
        # mock
        now = arrow.utcnow()
        return now.replace(minutes=7, seconds=20)

    def _get_prefix(self, next):
        return 'next'

    def play(self):
        next_transport = self._get_transport()
        next_human = next_transport.humanize()
        # fixup arrow wording for Vox
        next_human = re.sub(r'minute[^s]', 'minutes', next_human)
        human = '{} {} {}'.format(self._get_prefix(next_transport),
                                  self._line_humanize(self.line), next_human)
        sound = VoxSound(data=human, mixer=self.mixer, base_dir=self.dir)
        sound.play()

@state.sounds.register
class LastZTMSound(ZTMSound):
    simple_name = 'lastztm'

    def _get_prefix(self, next_transport):
        now = arrow.utcnow()
        seconds = (next_transport - now).seconds
        if seconds <= (15 * 60):
            return 'warning warning last'
        if seconds <= (30 * 60):
            return 'warning last'
        else:
            return 'last'

    def _get_transport(self):
        # mock
        now = arrow.utcnow()
        return now.replace(minutes=15)

class SoundSet(object):

    def __init__(self, config, mixer=SDLMixer):

        self.config = config
        self.sounds_factory = SoundFactory(mixer, config['wav_directory'])

        self.busy_time = time()
        self.sounds = {}
        self._load_sounds()

        logging.info("Creating board %s", self.name)

    def __getattr__(self, attr):
        if attr in ['name', 'keys']:
            return self.config[attr]
        raise AttributeError(attr)

    @classmethod
    def from_yaml(cls, yaml_path, settings, *args, **kwargs):
        config = YAMLConfig(yaml_path, settings=settings)
        return cls(config, *args, **kwargs)

    def _load_sounds(self):
        for soundentry in self.config['sounds']:
            sound = self._create_sound(soundentry)

            keys = soundentry['keys']
            self.sounds[keys] = sound

    def _create_sound(self, config):
        sound = self.sounds_factory.by_name(config['type'])
        input = config['input']
        if 'name' in config:
            sound.name = config['name']
        sound.setup(input)
        return sound

    def play(self, buttons):
        if not buttons:
            return  # noqa

        buttons = frozenset(buttons)
        sound = self.sounds.get(buttons, None)
        if not sound:
            return  # noqa
        sound.play()

    def stop(self, released_buttons):
        for (buttons, sound) in self.sounds.items():
            if released_buttons & buttons and sound.running:
                sound.end()


import unittest
from heapq import heappush, heappop
from .ds import ListNode

class Solution(unittest.TestCase):
    def mergeKLists(self, lists):
        """
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Basic idea: k-way merge, min heap
        """
        if not lists:
            return None
        heap = []
        for i, head in enumerate(lists):
            if head:
                heappush(heap, (head.val, i, head))

        curr = head = ListNode(None)
        while heap:
            v, i, node = heappop(heap)
            curr.next, curr = node, node
            if node.next:
                heappush(heap, (node.next.val, i, node.next))

        return head.next

    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        head = ListNode(0)
        cur = head

        while l1 != None and l2 != None:
            if l1.val < l2.val:
                cur.next = l1
                l1 = l1.next
            else:
                cur.next = l2
                l2 = l2.next
            cur = cur.next

        if l1 == None:
            cur.next = l2
        elif l2 == None:
            cur.next = l1

        return head.next

    def mergeKLists2(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        length = len(lists)
        if length == 0:
            return None

        # divide and conquer, one fast way (logK) to do merge
        # [1, 2, 3, 4, 5] => [(1, 2), _, (3, 4), _, 5] => [(1, 2, 3, 4), _, _, _, 5] => [(1, 2, 3, 4, 5), _, _, _, _]
        interval = 1
        while interval < length:
            for i in range(0, length - interval, interval * 2):
                lists[i] = self.mergeTwoLists(lists[i], lists[i + interval])
            interval *= 2
        return lists[0]

    def test(self):
        l1 = ListNode.fromList([1,3,7])
        l2 = ListNode.fromList([1,4,6])
        l3 = ListNode.fromList([5,8,9])

        self.assertEqual([1, 1, 3, 4, 5, 6, 7, 8, 9], self.mergeKLists([l1,l2,l3]).toList())


# Copyright 2014: Mirantis Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import math

from rally import exceptions

def mean(values):
    """Find the simple average of a list of values.

    :parameter values: non-empty list of numbers

    :returns: float value
    """
    if not values:
        raise exceptions.InvalidArgumentsException(
                                        "the list should be non-empty")
    return math.fsum(values) / len(values)

def percentile(values, percent):
    """Find the percentile of a list of values.

    :parameter values: list of numbers
    :parameter percent: float value from 0.0 to 1.0

    :returns: the percentile of values
    """
    if not values:
        return None
    values.sort()
    k = (len(values) - 1) * percent
    f = math.floor(k)
    c = math.ceil(k)
    if f == c:
        return values[int(k)]
    d0 = values[int(f)] * (c - k)
    d1 = values[int(c)] * (k - f)
    return (d0 + d1)

def get_atomic_actions_data(raw_data):
    """Retrieve detailed (by atomic actions & total runtime) benchmark data.

    :parameter raw_data: list of raw records (scenario runner output)

    :returns: dictionary containing atomic action + total duration lists
              for all atomic action keys
    """
    atomic_actions = []
    for row in raw_data:
        # find first non-error result to get atomic actions names
        if not row["error"] and "atomic_actions" in row:
            atomic_actions = row["atomic_actions"].keys()
    actions_data = {}
    for atomic_action in atomic_actions:
        actions_data[atomic_action] = [
            r["atomic_actions"][atomic_action]
            for r in raw_data
            if r["atomic_actions"].get(atomic_action) is not None]
    actions_data["total"] = [r["duration"] for r in raw_data if not r["error"]]
    return actions_data


import requests
import re
from tkinter import *

class Bowaer:
    def __init__(self, root):
        root.title('英语词典')
        self.entry = Entry(root,width=40)
        self.button = Button(root, text='翻译', command=self.translate)
        self.str = StringVar()
        self.message = Message(root, textvariable=self.str)

        self.entry.pack()
        self.button.pack()

    def translate(self):
        # 百度翻译url
        url = 'http://fanyi.baidu.com/v2transapi'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
            'Referer': 'http://fanyi.baidu.com/?aldtype=16047',
            'Host': 'fanyi.baidu.com'
        }
        word = self.entry.get()
        form = {
            'from': 'en',
            'to': 'zh',
            'query': word
        }
        # 单词意思正则表达式
        pattern_means = "'means':(.*?)}"
        # 单词词性正则表达式
        pattern_part = "'part':(.*?),"
        request = requests.post(url, headers=headers, data=form)
        json = str(request.json())
        means = re.findall(pattern_means, json)
        parts = re.findall(pattern_part, json)
        # 将词性和意思对应并格式化
        data = str()
        for i in range(len(parts)):
            data = data + re.findall("'(.*?)'", parts[i])[0] + '  '
            for mean in re.findall("'(.*?)'", means[i]):
                data = data + mean + ' '
            # 每一个词性换一行
            data += '\n'
        print(data)
        self.str.set(data)
        self.message.pack()

root = Tk()
app = Bowaer(root)
root.mainloop()


import numpy as np
import torch
from torch.utils.data import DataLoader
from torchvision import transforms

import mlp.data_providers as data_providers
from pytorch_mlp_framework.arg_extractor import get_args
from pytorch_mlp_framework.experiment_builder import ExperimentBuilder
from pytorch_mlp_framework.model_architectures import *
from pytorch_mlp_framework.convolutional_blocks import *

args = get_args()  # get arguments from command line
rng = np.random.RandomState(seed=args.seed)  # set the seeds for the experiment
torch.manual_seed(seed=args.seed)  # sets pytorch's seed

# set up data augmentation transforms for training and testing
''' Data augmentation strategies to improve generalisation of the model'''

transform_train = transforms.Compose([
        transforms.RandomCrop(32, padding=4),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
    ])

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
'''End'''

train_data = data_providers.CIFAR100(root='data', set_name='train', shuffle=True,
                 transform=transform_train,
                 download=True)  # initialize our rngs using the argument set seed
val_data = data_providers.CIFAR100(root='data', set_name='val', shuffle=True,
                 transform=transform_test,
                 download=True)  # initialize our rngs using the argument set seed
test_data = data_providers.CIFAR100(root='data', set_name='test', shuffle=True,
                 transform=transform_test,
                 download=True)  # initialize our rngs using the argument set seed

train_data_loader = DataLoader(train_data, batch_size=args.batch_size, shuffle=True, num_workers=4)
val_data_loader = DataLoader(val_data, batch_size=args.batch_size, shuffle=True, num_workers=4)
test_data_loader = DataLoader(test_data, batch_size=args.batch_size, shuffle=True, num_workers=4)

''' reads from command-line to choose which processing and dim reduction
blocks will be used.'''
if args.block_type == 'conv_block':
    processing_block_type = ConvolutionalProcessingBlock
    dim_reduction_block_type = ConvolutionalDimensionalityReductionBlock
elif args.block_type == 'conv_block_s1':
    processing_block_type = ConvProcessingBlockResnet
    dim_reduction_block_type = ConvDimensionalityReductionBlockResnet
elif args.block_type == 'conv_block_s2':
    processing_block_type = ConvProcessingBlockBNNetwork
    dim_reduction_block_type = ConvDimensionalityReductionBlockBNNetwork
elif args.block_type == 'empty_block':
    processing_block_type = EmptyBlock
    dim_reduction_block_type = EmptyBlock
else:
    raise ModuleNotFoundError
'''End'''

custom_conv_net = ConvolutionalNetwork(  # initialize our network object, in this case a ConvNet
    input_shape=(args.batch_size, args.image_num_channels, args.image_height, args.image_width),
    num_output_classes=args.num_classes, num_filters=args.num_filters, use_bias=False,
    num_blocks_per_stage=args.num_blocks_per_stage, num_stages=args.num_stages,
    processing_block_type=processing_block_type,
    dimensionality_reduction_block_type=dim_reduction_block_type)

conv_experiment = ExperimentBuilder(network_model=custom_conv_net,
                                    experiment_name=args.experiment_name,
                                    num_epochs=args.num_epochs,
                                    weight_decay_coefficient=args.weight_decay_coefficient,
                                    use_gpu=args.use_gpu,
                                    continue_from_epoch=args.continue_from_epoch,
                                    train_data=train_data_loader, val_data=val_data_loader,
                                    test_data=test_data_loader)  # build an experiment object
experiment_metrics, test_metrics = conv_experiment.run_experiment()  # run experiment and return experiment metrics


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

import wx

from app.components.component import Component
from app.ui.ui_event import UIEvent

class UIController(Component):

    def __init__(self, *args, **kwargs):
        kwargs['logger_name'] = kwargs.pop('logger_name', __name__)
        super(UIController, self).__init__(*args, **kwargs)
        self.ui = kwargs.pop('ui')

        self.initial_state = 'started'
        self.events = [
            {
                'name': str('quitting'),
                'src': '*',
                'dst': 'quit'
            },
            {
                'name': str('start_running'),
                'src': 'started',
                'dst': 'running'
            },
            {
                'name': str('finish_running'),
                'src': 'running',
                'dst': 'started'
            },
            {
                'name': str('message'),
                'src': '*',
                'dst': 'started'
            }
        ]

        self.additional_callbacks = {
            'onbeforemessage': self.message
        }

    def message(self, transition):
        event = transition.args[0]
        wx_event = UIEvent(event)
        wx.PostEvent(self.ui.main_window, wx_event)
        return False

    def handle_event(self, event):
        self.logger.debug('in state {} : handling event {}'.format(self.fsm.current, event))
        wx_event = UIEvent(event)
        wx.PostEvent(self.ui.main_window, wx_event)
        self.fsm.trigger(event['name'], event)
        self.logger.debug('transitioned to new state: {} and sent event to UI instance'.format(self.fsm.current))


import hexchat

__module_name__ = "Filter"
__module_author__ = "Ivailo Stoianov"
__module_version__ = "1.0"
__module_description__ = "No more shit m8"

bot_tab = "(Shit)"
spam_tab = "(Pleb_shit)"

def get_bot_tab():
	bot_context = hexchat.find_context(channel=bot_tab)
	if bot_context == None:
		hexchat.command("NEWSERVER -noconnect {0}".format(bot_tab))
		return hexchat.find_context(channel=bot_tab)
	else:
		return bot_context

def get_pleb_tab():
        pleb_context = hexchat.find_context(channel=spam_tab)
        if pleb_context == None:
                hexchat.command("NEWSERVER -noconnect {0}".format(spam_tab))
                return hexchat.find_context(channel=spam_tab)
        else:
                return pleb_context

def filter_cb(word, word_eol, event):
	channel = hexchat.get_info("channel")
	announce_context = get_bot_tab()
	find_context = get_pleb_tab()
	if channel == '#horriblesubs' or channel == '#HorribleSubs':
                if len(word) > 2:
                        if  word[2] == '%':
                                announce_context.prnt("{0} \00323{1}\t({3}){2}".format(channel, *word))
                                return hexchat.EAT_ALL
                if '/msg' in word[1] or '/MSG' in word[1]:
                        announce_context.prnt("{0} \00323{1}\t {2}".format(channel, *word))
                        return hexchat.EAT_ALL
                elif word[1][:5] == '!find' or word[1][:5] == '@find':
                        find_context.prnt("{0} \00323{1}\t {2}".format(channel, *word))
                        return hexchat.EAT_ALL
                else:
                        return hexchat.EAT_NONE

hexchat.hook_print('Channel message', filter_cb)

def unload_callback(userdata):
	for chan in hexchat.get_list("channels"):
		if chan.type == 1 and chan.channel == bot_tab:
			context = hexchat.find_context(channel=bot_tab)
			context.command("CLOSE")
		elif chan.type == 1 and chan.channel == spam_tab:
                        context = hexchat.find_context(channel=spam_tab)
                        context.command("CLOSE")
	hexchat.prnt(__module_name__ + " version " + __module_version__ + " unloaded")

hexchat.hook_unload(unload_callback)

hexchat.prnt(__module_name__ + " version " + __module_version__ + " loaded")


#//Return value
import math
e  = math.exp(1.0)
print (e)

#//Define function display area
def  area (radius):
    temp  = math.pi*radius**2
    return temp

#//OR
def area_sort(radius):
    return math.pi*radius**2

#//Exercise 6

def compare_value(x,y):
    if x<y:
        return -1
    if x==y :
        return  0
    if x>y:
        return 1

#//Display value  bewiet  tow vector
def distance (x1,y1,x2,y2):
    return  math.sqrt((x2-x1)**2 +(y2-y1)**2)

#//COMPOSITION
"""
As you should expect by now, you can call one function from within another. This ability is called composition
"""
def circal_area(x1,y1,x2,y2):
     radius  =  distance (x1,y1,x2,y2)
     return area(radius)
 #//BOOlLEAN  FUNCTION
def is_divisible(x, y):
     return x % y ==0

print(is_divisible(4,6))

#//Display factorial
def  factorial(n):
    if n  == 0  :
        return  1
    else:
        return  n*factorial(n-1)

#//Caculer fibonatci
def  fibonacci (n):
    while(n < 1):
        print ("n is  must  > 0 ")
        if n  ==1 :
            return 1
        if n  ==2 :
            return  1
        else:
            return  fibonacci(n-1) +  fibonacci(n-2)

def factorial_pro( n):
    if not isinstance(n, int) :
        print ("Factorial  is  only define for interger")
    elif n<0 :
        print ("Factorial  in not define  for  nagetive integers")
    elif n==0:
        return  1
    else:
        return n *factorial(n-1)

print (factorial_pro("file"))

def factorial_super_pro(n):
    space  =  ' '*(4*n)
    print (space , 'factorial' ,n)
    if n  ==0 :
        print (space , 'returing 1')
        return 1
    else:
        recurse = n*factorial(n-1)
        print(space ,  'returing ', recurse)
        return  recurse

def Ackermann  (m  , n ):
    if  m  ==  0  :
        return n  +1
    elif m > 0 and  n  ==  0  :
        return  Ackermann(m-1 , 1)
    elif m >0 and  n > 0  :
        return Ackermann(m -1  , Ackermann(m  , n -1))

print  (Ackermann(3,4))


"""this main is used to test interactoins and is mainly used with the util functions
"""
import openvr
from openvr.error_code import InitError_Init_PathRegistryNotFound

from utils.triad_openvr import triad_openvr

def discover_devices():
    v = triad_openvr()
    print(v.print_discovered_objects())

def print_controller_pose(config_file=None):
    v = triad_openvr(configfile_path=config_file)
    import time
    while True:
        pose = v.devices["controller_1"].get_pose_quaternion()

        print(pose)
        time.sleep(1)

def print_tracker_pose(config_file=None):
    v = triad_openvr(configfile_path=config_file)
    import time
    while True:
        pose = v.devices["tracker_1"].get_pose_quaternion()

        print(pose)
        time.sleep(1)

def print_controller_button_state(config_file=None):
    v = triad_openvr(config_file)
    import time
    while True:
        buttons = v.devices["controller_1"].get_controller_inputs()

        print(buttons)
        time.sleep(1)

def print_controller_full(config_file=None):
    v = triad_openvr(config_file)
    import time
    while True:
        pose = v.devices["controller_1"].get_pose_quaternion()
        buttons = v.devices["controller_1"].get_controller_inputs()
        print(pose)
        print(buttons)
        time.sleep(1)

def print_device_information():
    v = triad_openvr()
    print(v.print_discovered_objects())

if __name__ == "__main__":
    try:

        discover_devices()
        # print_controller_full()
        print_tracker_pose()
    except InitError_Init_PathRegistryNotFound:
        print("works")


'''
from tkinter import filedialog
from tkinter import *

from tkinter import Tk, Label, Button

import sys
from tkinter import *
from tkinter.filedialog import askopenfilename
import pandas as pd

fname = "unassigned"

class TkBox:
	def __init__(self, root):
		self.openFile()
	def openFile(self):
	    global fname
	    fname = filedialog.askopenfilename(initialdir = "/")
	    root.destroy()

root = Tk()
root.geometry("200x100")
root.title("Browse image")
my_gui = TkBox(root)
root.mainloop()

print('trying to open '+str(fname)+' as excel file...')
try:
	df = pd.read_excel(fname)
	df = pd.read_excel(fname, converters={col: str for col in df.columns})
	nrow = df.shape[0]
	ncol = df.shape[1]
	print('nrow = '+str(nrow)+', ncol = '+str(ncol))
	if nrow < 1 or ncol != 4:
		fname = ()
except:
	fname = ()

print('info_file = '+str(fname))
'''

import subprocess
import fileinput

with open('facematching/settings.py', 'r') as f:
	f = f.readlines()

i = 0
for line in f:
	if 'ALLOWED_HOSTS =' in line:
		break
	i = i + 1
if(i == len(f)):
	print('not found the phrase "ALLOWED_HOSTS ="')
else:
	result = subprocess.run(['hostname', '-I'], stdout=subprocess.PIPE)
	r = result.stdout.decode("utf-8")
	f[i] = 'ALLOWED_HOSTS = ["localhost","'+r[:-2]+'"]\n'

with open('facematching/settings.py', 'w') as file:
    file.writelines( f )


# -*- coding: utf-8 -*-
import sys
import os

live_path = os.path.dirname(os.path.realpath(__file__))
core_path = os.path.split(live_path)[0]
root_path = os.path.split(core_path)[0]
parent_path = os.path.split(root_path)[0]
sys.path.append(parent_path)

import threading
from tkinter import *
import tkinter.font as tkFont
import time
import grequests
import requests
import pandas as pd
import midas.bin.env as env
from midas.core.data.engine import main_session
import midas.core.data.models as models
from decimal import Decimal
import decimal
from bs4 import BeautifulSoup
import re
from datetime import datetime, timedelta

def main():
    # codes = []
    # symbol2code = {}
    # symbol2rank = {}

    data = {}
    df = pd.read_csv('{data_path}/limit_rank_ones.csv'.format(data_path=env.data_path))

    for i in range(len(df)):
        rank = df.loc[i, 'COL_LIMIT_RANK']
        if rank < 2:
            continue

        ts_code = df.loc[i, 'ts_code']
        symbol = ts_code.split('.')[0]
        market = ts_code.split('.')[1].lower()
        code = f'{market}{symbol}'
        data[code] = {}

        data[code]['rank'] = rank

        data[code]['last_close'] = df.loc[i, 'COL_CLOSE']
        data[code]['name'] = df.loc[i, 'name']

    # for code in data.keys():
    #     response = requests.get(f'http://vip.stock.finance.sina.com.cn/quotes_service/view/CN_TransListV2.php?num=5000&symbol={code}')
    #     time.sleep(0.2)
    #     pattern = re.compile(r"Array\(.+\)")
    #     raw = pattern.findall(response.text)
    #     for item in raw[::-1]:
    #         a = item.replace('Array(', '').replace(')', '')
    #         b = a.split(', ')
    #         time_str = b[0].replace('\'', '')
    #         vol = float(b[1].replace('\'', ''))
    #         price = float(b[2].replace('\'', ''))
    #         direction = b[3].replace('\'', '')
    #
    #         if time_str == '09:25:00':
    #             code = response.url.split('=')[-1]
    #             fund = vol * price
    #             last_close = data[code]['last_close']
    #             rate = round((price / last_close - 1) * 100, 2)
    #             data[code]['price'] = price
    #             data[code]['rate'] = rate
    #             data[code]['fund'] = fund
    #             data[code]['direction'] = direction
    #
    #             print(f'{code} pass')
    #             break

    req_list = []
    for code in data.keys():
        req_list.append(grequests.get(f'http://vip.stock.finance.sina.com.cn/quotes_service/view/CN_TransListV2.php?num=5000&symbol={code}'))

    responses = grequests.map(req_list)
    for response in responses:
        pattern = re.compile(r"Array\(.+\)")
        raw = pattern.findall(response.text)
        for item in raw[::-1]:
            a = item.replace('Array(', '').replace(')', '')
            b = a.split(', ')
            time_str = b[0].replace('\'', '')
            vol = float(b[1].replace('\'', ''))
            price = float(b[2].replace('\'', ''))
            direction = b[3].replace('\'', '')

            if time_str == '09:25:00':
                code = response.url.split('=')[-1]
                fund = vol * price
                last_close = data[code]['last_close']
                rate = round((price / last_close - 1) * 100, 2)
                data[code]['price'] = price
                data[code]['rate'] = rate
                data[code]['fund'] = fund
                data[code]['direction'] = direction
                break

    display_data = {}
    for k, v in data.items():
        try:
            rank = v['rank']
            rate = v['rate']
            fund = v['fund']
            fund = f'{int(fund / 10000)}w'
            if rate > 5:
                if rank not in display_data.keys():
                    display_data[rank] = []
                display_data[rank].append({
                    'code': k,
                    'price': v['price'],
                    'rate': v['rate'],
                    'fund': fund,
                    'name': v['name']
                })
        except Exception as e:
            continue

    for k, v in display_data.items():
        v.sort(key=lambda x: x['rate'], reverse=True)

    for k, v in display_data.items():
        print(k)
        for item in v:
            code = item['code']
            price = item['price']
            rate = item['rate']
            fund = item['fund']
            name = item['name']
            print(f'{code}\t{name}\t{rate}%\t{price}\t{fund}')

    pass

if __name__ == '__main__':
    main()


from tr_option.base import KWTR
from copy import deepcopy

# [ opt20009 : 업종현재가일별요청 ]
class Opt20009(KWTR):

    def __init__(self, core):
        super().__init__(core)

        self.rq_name = self.tr_code = 'opt20009'

        self.record_name_single = '업종현재가일별'
        self.header_single = [
            '현재가', '전일대비기호', '전일대비', '등락률', '거래량', '거래대금', '거래형성종목수', '거래형성비율', '시가', '고가', '저가', '상한', '상승', '보합', '하락', '하한', '52주최고가', '52주최고가일', '52주최고가대비율', '52주최저가', '52주최저가일', '52주최저가대비율',
        ]

        self.record_name_multiple = '업종현재가_일별반복'
        self.header_multiple = [
            '시간n', '현재가n', '전일대비기호n', '전일대비n', '등락률n', '누적거래량n',
        ]

    def tr_opt(self, market_type, input1, prev_next, screen_no):
        # 시장구분 = 0:코스피, 1:코스닥, 2:코스피200
        # 업종코드 = 001:종합(KOSPI), 002:대형주, 003:중형주, 004:소형주 101:종합(KOSDAQ), 201:KOSPI200, 302:KOSTAR, 701: KRX100 나머지 ※ 업종코드 참고

        self.core.set_input_value('시장구분', market_type)
        self.core.set_input_value('업종코드', input1)
        self.core.comm_rq_data(self.rq_name, self.tr_code, prev_next, screen_no)

        self.tr_data = deepcopy(self.core.receive_tr_data_handler[self.tr_code][screen_no])

        return self.tr_data


import numpy as np
import heapq as hp
import math as mth
from collections import defaultdict, OrderedDict
from collections import Counter

'''
Some helper sites
https://stackoverflow.com/questions/12711397/python-recursive-function-to-find-the-largest-number-in-the-list
https://stackoverflow.com/questions/11236006/identify-duplicate-values-in-a-list-in-python
https://stackoverflow.com/questions/1540049/replace-values-in-list-using-python
For Drawing State machines : http://madebyevan.com/fsm/
'''

class Solution(object):

    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """

        '''
        # Amazing solution , with multiple assignments which progresses with time
        '''
        choice = input('Enter your choice: 1 or 2 or 3 \n')
        print('choice is ',choice)

        if int(choice) == 1:
            result = self.quickhelper(prices)
            print(result)
        elif int(choice) == 2:
            self.maxmin = {'max':[],'min':[]}
            print("Input array", prices)
            result = self.helper1(prices)
            print(result)
        elif int(choice) == 3:
            result = self.statemachinesolver(prices)
            print(result)

    def helper1(self, listtest):

        value = len(listtest)
        arr = hp.nlargest(value,listtest)
        tempdict = OrderedDict()

        # Need to make sure indices are not getting repeated ,if they are then we would need to get unique ones
        tempdict = self.finddups(listtest)
        print("sorted array and Dup", arr, tempdict)
        try:
            if value%2!=0:
                # Odd Case
                for i in range(0,int(mth.ceil(value/2))):
                    if i!=(value-i-1):
                        self.maxmin['max'].append(listtest.index(arr[i]))
                        self.maxmin['min'].append(listtest.index(arr[value-i-1]))
                        print(listtest[i], listtest[value-i-1])
                # option to choose the middle in max or min
                self.maxmin['max'].append(listtest.index(arr[int(value/2)]))
                print(self.maxmin)
                #Check if all the indices are unique and replace the ones which are not
                repeatmax = [k for k,v in Counter(self.maxmin['max']).items() if v>1]
                repeatmin = [k for k,v in Counter(self.maxmin['min']).items() if v>1]

                '''
                We would need to change this part based on multiple duplicates
                '''
                if len(repeatmax)!=0:
                    counter1 = 0
                    for key, val in tempdict.items():
                        for index , item in enumerate(self.maxmin['max']):
                            if (item == val[0]):
                                if counter1>=1:
                                    self.maxmin['max'][index] = val[counter1]
                                counter1+=1
                if len(repeatmin)!=0:
                    counter2 = 0
                    for key, val in tempdict.items():
                        if key in self.maxmin['min']:
                            for index , item in enumerate(self.maxmin['min']):
                                if (item == val[0]):
                                    if counter2>=1:
                                        self.maxmin['min'][index] = val[counter2]
                                    counter2+=1
                print("final maxmin dict", self.maxmin)
            else:
                # Even case
                pass
        except IndexError as e:
            print(e)

        result = self.buysell(listtest, self.maxmin)
        return(result)

    def buysell(self, listtest3, maxmindict):
        # Calulate the profit
        buyflag = None
        sellflag = None
        profit = 0
        neglectfirstmaxima = None
        neglectfirstminima = None
        cooldown = False
        cmax=0
        cmin=0

        #Get the minimum index if the first minima index not less than first maxima index
        if maxmindict['max'] < maxmindict['min']:
            startbuypoint = np.min(maxmindict['min'])
            neglectfirstmaxima = True
            neglectfirstminima = False
            cmax +=1
        elif maxmindict['max'] > maxmindict['min']:
            #Get the minima and Buy first
            print("chk1")
            startbuypoint = maxmindict['min'][0]
            neglectfirstmaxima = False
            '''
            Neglect the Fist minima, only if the index of first minima is > 0
            '''
            if startbuypoint > 0:
                neglectfirstminima = True
            cmin +=1

        for idx, val in enumerate(listtest3):
            print("chk-p", idx, val, buyflag, sellflag, cooldown, startbuypoint)
            if idx == startbuypoint and neglectfirstmaxima is True:
                buypoint = val
                buyflag = True
                sellflag = False
                cmin+=1

            elif idx == startbuypoint and neglectfirstminima is True:
                print("chk2")
                buypoint = val
                buyflag = True
                sellflag = False
                cmin+=1

            elif idx > startbuypoint and buyflag is True and cmax<=1:
                # You have bought and need to sell at a maxima , Entering here for first sell
                if neglectfirstmaxima is True:
                    if idx == maxmindict['max'][cmax]:
                        profit += val - buypoint
                        buyflag = False
                        sellflag = True
                        cooldown = True
                        cmax += 1
                elif neglectfirstmaxima is False:
                    # This could be the first sell point
                    if idx == np.min(maxmindict['max']):
                        if idx > maxmindict['max'].index(idx) :
                            '''
                            You Sell at this point instead of the the maxima , as it comes first in time, unless this diff is more
                            But we can only give out the difference when we have all of them, that is once we have calculated them all
                            '''
                            profit += val - buypoint
                            buyflag = False
                            sellflag = True
                            cooldown = True
                            cmax += 1

            elif idx > startbuypoint and buyflag is False and cooldown is True:
                # Cooldown
                profit += 0
                cooldown = False
                if idx in maxmindict['max']:
                    cmax+=1
                elif idx in maxmindict['min']:
                    cmin+=1

            elif idx > startbuypoint and buyflag is False and cooldown is False and sellflag is True:
                # Buy [Minima]
                print(maxmindict)
                if idx <= np.max(maxmindict['min']):
                    if idx in maxmindict['min']:
                        if idx == maxmindict['min'][cmin-1]:
                            buypoint = val
                            buyflag = True
                            sellflag = False
                            cmin+=1
                    elif idx not in maxmindict['min']:
                        print("chk4")
                        # if it is present here, then we will consider it as a minima and buy here
                        buypoint = val
                        buyflag = True
                        sellflag = False
                        cmax +=1
                elif idx > np.max(maxmindict['min']):
                    if idx in maxmindict['max']:
                        pass

            elif idx > startbuypoint and buyflag is True and cooldown is False and sellflag is False:
                print("chk5")
                # Sell [Maxima]
                if idx in maxmindict['max'] :
                    profit += val - buypoint
                    buyflag = False
                    sellflag = True
                    cooldown = True
                    cmax += 1

        return(profit)

    def finddups(self,listtest2):
        D = defaultdict(list)

        for i, item in enumerate(listtest2):
            D[item].append(i)

        D = {k:v for k,v in D.items() if len(v)>1}
        return(D)

    def quickhelper(self,listtest):
        free = 0
        have = cool = float('-inf')
        print("Input",listtest)
        for p in listtest:
            #print("before", free, have, cool)
            free, have, cool = max(free, cool), max(have, free - p), have + p
            #print("after", free, have, cool)
        return max(free, cool)

    def statemachinesolver(self,listtest):
        '''
        This uses State machines for solving , This solution is present at : https://discuss.leetcode.com/topic/30421/share-my-thinking-process
        buy[i] = Before i, Maximum profit for a sequence ending with a Buy
        rest[i] = Before i, Maximum profit for a sequence ending with a rest
        sell[i] = Before i, Maximum profit for a sequence ending with a sell

        We want to deduce the transition functions for buy, sell and rest here

        Basically represent the transition functions in terms of the state that can occur before the particular state we want to represent
        '''
        # For i-1 ,we have a B , so i-2 can be a B or R , BB or RB ,
        # but to maximize profit it cannot be BRB -> we will take care of this with rest[i]
        # For rest[i], we can come from any or all 3 states , also buy[i]<=rest[i] -> so this makes sure that BR is  never there
        # Always sell before you buy , so always SRB , SRRB
        # Also rest[i]<=sell[i-1] , a condition as SR , you sell and make a profit
        # After a Sell there will always be rest , and rests are no profit no loss , so rest[i-1] , can be replaces with sell[i-1]
        '''
        buy[i] = max(buy[i-1], rest[i-1]-p) = max(buy[i-1], sell[i-1]-p)
        rest[i] = max(buy[i-1], rest[i-1], sell[i-1]) = max(rest[i-1], sell[i-1]) = sell[i-1]
        sell[i] = max(sell[i-1], buy[i-1]+p) # Replacing sell with rest
        '''

        if len(listtest) < 2:
            return 0
        sell , buy, old_sell, old_buy = 0, -listtest[0], 0, 0
        for price in listtest:
            old_buy = buy
            buy = max(old_buy, old_sell-price)
            old_sell = sell
            sell =  max(old_buy+price, old_sell)
        return sell

obj = Solution()
#obj.maxProfit([1,0,5,3,2,7,1]) -> PASS
#obj.maxProfit([1,2,3,0,2])
obj.maxProfit([2,7,1,0,15,11,1,2,20])


"""
@author: Junjie Jiang
"""

import sklearn
from sklearn.svm import SVC
from sklearn import datasets
import numpy as np
import config
import Utility
from User import User
from twarc import Twarc
import json
import attributesWithoutLIWC
import attributesWithLIWC
import os
import dom_act
import twarcWork
import depressionScoreCalculator
import get_results

def main():
    get_results.csvToResultsV2("Data/newPlatformDataset.csv")
    Utility.checkUserListNameID()
    depressionScoreCalculator.resultinfoToScore()
    #twarcWork.acceptListToTrainingData()
    '''print("Training SVM...")
    clf = trainSVM()
    print("Predicting...")
    prediction = predictUserList(clf)
    print(prediction)'''
    #dataImitation()

def dataImitation():
    depressed = open("Data/Depressed.txt","r")
    undepressed = open("Data/UnDepressed.txt","r")
    dataimitation = open("Data/Imitation/imitation.csv","a")
    dataimitation.write("depressed or not,volume,retweets,reply,questions,links,numFollowers,numFollowees,depression,swear,I,we,they,shehe,positiveemo,negativeemo\n")
    depressedList = depressed.read().split('\n')
    undepressedList = undepressed.read().split('\n')

    if undepressedList[-1] == '':
        undepressedList = undepressedList[:-1]
    if depressedList[-1] == '':
        depressedList = depressedList[:-1]
    print(depressedList,undepressedList)
    vectorList = []
    liwcDict = Utility.deserialize('Data/Imitation/liwcDict.json')
    k = 0
    for user in (depressedList + undepressedList):
        k += 1
        print( str(k) + 'th user being analyzed...')
        if user in depressedList:
            newUser, data = attributesWithoutLIWC.updateImitationEngagements(user,depressed=True)
            vector = [1]
        else:
            newUser, data = attributesWithoutLIWC.updateImitationEngagements(user,depressed=False)
            vector = [0]
        attributesWithoutLIWC.updateUserFollowers(newUser)
        attributesWithoutLIWC.updateUserFriends(newUser)
        vector += [newUser.volume, newUser.retweets, newUser.reply, newUser.questions, newUser.links,
                      newUser.numFollowers, newUser.numFollowees] + liwcDict[str(user)[1:]]
        linestr = ''
        for item in vector:
            linestr += str(item) + ','
        linestr = linestr[:-1] + '\n'
        dataimitation.write(linestr)
    dataimitation.close()
    return

def matchInformation():
    wList = Utility.readCsv("Data/MTurk/worker_info.csv")[:-1]
    rList = Utility.readCsv("Data/MTurk/result_info.csv")[:-1]
    idList = [ x[3] for x in wList ][1:]
    scoreList =  [ y[11] for y in rList ][1:]
    return idList, scoreList

def trainSVM():
    idList, scoreList = matchInformation()
    vectorList = []
    targetList = []
    liwcDict = Utility.deserialize('Data/Training/liwcDict.json')
    if os.path.exists('Data/Training/TrainingUserList.json'):
        userDict = Utility.deserialize('Data/Training/TrainingUserList.json')
    else:
        userDict = {}
    k = 0
    for userid in idList:
        k += 1
        print( str(k) + 'th user being analyzed...')
        if userid not in userDict:
            newUser, data = attributesWithoutLIWC.updateEngagements(userid,train=True)
            attributesWithoutLIWC.updateUserFollowers(newUser)
            attributesWithoutLIWC.updateUserFriends(newUser)
            pa = 0.0
            na = 0.0
            dom = 0.0
            act = 0.0
            for tweet in data:
                compound = attributesWithLIWC.getCompound(tweet)
                dom, act = dom_act.get_act_dom_score(tweet)
                if compound > 0:
                    pa += 1
                if compound < 0:
                    na += 1
            pa /= len(data)
            na /= len(data)
            dom /= len(data)
            act /= len(data)
            vector = [newUser.volume, newUser.retweets, newUser.reply, newUser.questions, newUser.links,
                      newUser.numFollowers, newUser.numFollowees] + [pa,na,dom,act]#liwcDict[str(userid)]
            userDict[userid] = vector
        else:
            vector = userDict[userid]
        vectorList.append(vector)
        targetList.append(scoreList[idList.index(userid)])
        targetList = ['24','12','0','0','16','0']  # Trial
        #targetList = ['1','1','0','0','1','0']  # Trial
    print(vectorList)
    print(targetList)
    Utility.silentRemove('Data/Training/TrainingUserList.json')
    Utility.silentRemove('Data/Training/VectorList.json')
    Utility.silentRemove('Data/Training/targetList.json')
    Utility.serialize(userDict, 'Data/Training/TrainingUserList.json')
    Utility.serialize(vectorList, 'Data/Training/VectorList.json')
    Utility.serialize(targetList, 'Data/Training/targetList.json')
    clf = sklearn.svm.SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
        decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
        max_iter=-1, probability=False, random_state=None, shrinking=True,
        tol=0.001, verbose=False)
    clf.fit(vectorList, targetList)
    return clf

def predictUserList(clf):
    idfile = open('Data/MTurk/rejectList.csv','r')
    userList = idfile.read().split('\n')[:-1]
    userList = ['14835269','898264108914667520','225761199'] # Trial,'532688309'
    liwcDict = Utility.deserialize('Data/Predicting/liwcDict.json')
    if os.path.exists('Data/Predicting/PredictionDict.json'):
        predictionDict = Utility.deserialize('Data/Predicting/PredictionDict.json')
    else:
        predictionDict = {}
    prediction = {}
    k = 0
    for userid in userList:
        k += 1
        print( str(k) + 'th user being analyzed...')
        if userid not in predictionDict:
            user, data = attributesWithoutLIWC.updateEngagements(userid)
            attributesWithoutLIWC.updateUserFollowers(user)
            attributesWithoutLIWC.updateUserFriends(user)
            pa = 0.0
            na = 0.0
            for tweet in data:
                compound = attributesWithLIWC.getCompound(tweet)
                dom, act = dom_act.get_act_dom_score(tweet)
                if compound > 0:
                    pa += 1
                if compound < 0:
                    na += 1
            pa /= len(data)
            na /= len(data)
            dom /= len(data)
            act /= len(data)
            vector = [user.volume, user.retweets, user.reply, user.questions, user.links,
                      user.numFollowers, user.numFollowees] + [pa, na, dom, act] #liwcDict[str(userid)]
            prediction[userid] = int(list(clf.predict([vector]))[0])
            predictionDict[userid] = prediction[userid]
        else:
            prediction[userid] = predictionDict[userid]
    Utility.silentRemove('Data/Predicting/Prediction.json')
    Utility.silentRemove('Data/Predicting/PredictionDict.json')
    Utility.serialize(prediction, 'Data/Predicting/Prediction.json')
    Utility.serialize(predictionDict, 'Data/Predicting/PredictionDict.json')
    return prediction

def getTrainingData():
    t = Twarc(config.consumer_key, config.consumer_secret, config.access_token, config.access_secret)
    file = open("Data/Depressed.txt",'r')
    depressed = file.read().split('\n')
    file.close()
    indexList = []
    for userName in depressed:
        RI = Utility.checkRobot(userName)['scores']['english']
        if RI >= 0.45:
            indexList.append(depressed.index(userName))
        else:
            tmpName = userName
            if userName[0] == '@':
                userName = userName[1:]
            if not os.path.exists('Data/Training/' + userName + '.json'):
                try:
                    fileStr = ''
                    for tweet in t.timeline(screen_name=userName):
                        fileStr += json.dumps(tweet)+"\n"
                    if fileStr == '':
                        Utility.silentRemove("Data/Training/" + userName + ".json")
                        indexList.append(depressed.index(tmpName))
                    else:
                        outfile = open("Data/Training/" + userName + ".json","a")
                        outfile.write(fileStr)
                        outfile.close
                except Exception as e:
                    Utility.silentRemove("Data/Training/" + userName + ".json")
    depressed = [ depressed[i] for i in range(len(depressed)) if i not in indexList ]
    file = open("Data/Depressed.txt",'w')
    for i in depressed:
        if depressed.index(i) != (len(depressed) - 1):
            file.write(i + '\n')
        else:
            file.write(i)
    file.close()
    file = open("Data/UnDepressed.txt",'r')
    undepressed = file.read().split('\n')
    file.close
    indexList = []
    for userName in undepressed:
        RI = Utility.checkRobot(userName)['scores']['english']
        if RI >= 0.45:
            indexList.append(undepressed.index(userName))
        else:
            tmpName = userName
            if userName[0] == '@':
                userName = userName[1:]
            if not os.path.exists('Data/Training/' + userName + '.json'):
                outfile = open("Data/Training/" + userName + ".json","a")
                try:
                    count = 0
                    for tweet in t.timeline(screen_name=userName):
                        outfile.write(json.dumps(tweet)+"\n")
                        count += 1
                    outfile.close
                    if count == 0:
                        Utility.silentRemove("Data/Training/" + userName + ".json")
                        indexList.append(undepressed.index(tmpName))
                except:
                    outfile.close
                    Utility.silentRemove("Data/Training/" + userName + ".json")
    undepressed = [ undepressed[i] for i in range(len(undepressed)) if i not in indexList ]
    file = open("Data/UnDepressed.txt",'w')
    for i in undepressed:
        if undepressed.index(i) != (len(undepressed) - 1):
            file.write(i + '\n')
        else:
            file.write(i)
    file.close

def trainSVM2():
    file = open("Data/Depressed.txt",'r')
    userNameList = file.read().split('\n')
    file.close
    vectorList = []
    targetList = []
    if os.path.exists('Data/TrainingUserList.json'):
        userDict = Utility.deserialize('Data/TrainingUserList.json')
    else:
        userDict = {}
    for userName in userNameList:
        if (userName != None) and (userName != ''):
            if userName[0] == '@':
                userName = userName[1:]
            if userName not in userDict:
                newUser, data = attributesWithoutLIWC.updateEngagements(userName,train=True)
                attributesWithoutLIWC.updateUserFollowers(newUser)
                attributesWithoutLIWC.updateUserFriends(newUser)
                pa = 0.0
                na = 0.0
                dom = 0.0
                act = 0.0
                for tweet in data:
                    compound = attributesWithLIWC.getCompound(tweet)
                    dom, act = dom_act.get_act_dom_score(tweet)
                    if compound > 0:
                        pa += 1
                    if compound < 0:
                        na += 1
                pa /= len(data)
                na /= len(data)
                dom /= len(data)
                act /= len(data)
                vector = [newUser.volume, newUser.retweets, newUser.reply, newUser.questions, newUser.links,
                          newUser.numFollowers, newUser.numFollowees, pa, na, dom, act]
                userDict[userName] = vector
            else:
                vector = userDict[userName]
            print(vector)
            vectorList.append(vector)
            targetList.append(1)
    file = open("Data/UnDepressed.txt",'r')
    userNameList = file.read().split('\n')
    file.close
    for userName in userNameList:
        if userName[0] == '@':
            userName = userName[1:]
        if userName not in userDict:
            newUser, data = attributesWithoutLIWC.updateEngagements(userName,train=True)
            attributesWithoutLIWC.updateUserFollowers(newUser)
            attributesWithoutLIWC.updateUserFriends(newUser)
            pa = 0.0
            na = 0.0
            for tweet in data:
                compound = attributesWithLIWC.getCompound(tweet)
                dom, act = dom_act.get_act_dom_score(tweet)
                if compound > 0:
                    pa += 1
                if compound < 0:
                    na += 1
            pa /= len(data)
            na /= len(data)
            dom /= len(data)
            act /= len(data)
            vector = [newUser.volume, newUser.retweets, newUser.reply, newUser.questions, newUser.links,
                      newUser.numFollowers, newUser.numFollowees, pa, na, dom, act]
            userDict[userName] = vector
        else:
            vector = userDict[userName]
        print(vector)
        vectorList.append(vector)
        targetList.append(0)
    Utility.silentRemove('Data/TrainingUserList.json')
    Utility.silentRemove('Data/VectorList.json')
    Utility.silentRemove('Data/targetList.json')
    Utility.serialize(userDict, 'Data/TrainingUserList.json')
    Utility.serialize(vectorList, 'Data/VectorList.json')
    Utility.serialize(targetList, 'Data/targetList.json')
    clf = sklearn.svm.SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
        decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
        max_iter=-1, probability=False, random_state=None, shrinking=True,
        tol=0.001, verbose=False)
    clf.fit(vectorList, targetList)
    return clf

def predictUserList2(clf, userList, directory, county):
    if os.path.exists('Data/PredictionDict.json'):
        predictionDict = Utility.deserialize('Data/PredictionDict.json')
    else:
        predictionDict = {}
    prediction = {}
    for user in userList:
        if user.screenName not in predictionDict:
            data = attributesWithoutLIWC.updateEngagements(user)
            attributesWithoutLIWC.updateUserFollowers(user)
            attributesWithoutLIWC.updateUserFriends(user)
            pa = 0.0
            na = 0.0
            for tweet in data:
                compound = attributesWithLIWC.getCompound(tweet)
                dom, act = dom_act.get_act_dom_score(tweet)
                if compound > 0:
                    pa += 1
                if compound < 0:
                    na += 1
            pa /= len(data)
            na /= len(data)
            dom /= len(data)
            act /= len(data)
            vector = [user.volume, user.retweets, user.reply, user.questions, user.links,
                      user.numFollowers, user.numFollowees, pa, na, dom, act]
            prediction[user.screenName] = list(clf.predict([vector]))
            predictionDict[user.screenName] = prediction[user.screenName]
        else:
            prediction[user.screenName] = predictionDict[user.screenName]
    Utility.silentRemove('Data/' + directory + '/' + county + '/Prediction.json')
    Utility.silentRemove('Data/PredictionDict.json')
    Utility.serialize(prediction, 'Data/' + directory + '/' + county + '/Prediction.json')
    Utility.serialize(predictionDict, 'Data/PredictionDict.json')
    return prediction

def testSVM():
    filename = "Data/testTrain.csv"
    trainList = Utility.readCsv(filename)[1:]
    vectorList = []
    targetList = []
    zero = trainList[0][-1]
    trainList = trainList[:-1]
    for row in trainList:
        vectorList.append(row[:-1])
        if row[-1] == zero:
            targetList.append(0)
        else:
            targetList.append(1)

    for item in vectorList:
        print(item)
    clf = sklearn.svm.SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
        decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
        max_iter=-1, probability=False, random_state=None, shrinking=True,
        tol=0.001, verbose=False)
    clf.fit(vectorList, targetList)

    filename = "Data/testPredict.csv"
    trainList = Utility.readCsv(filename)[1:]
    resultList = []
    trainList = trainList[:-1]
    for row in trainList:
        vectorList.append(row[:-1])
        resultList.append(clf.predict([row[:-1]]))
    print(resultList)
'''
user1 = User(screenName='lightofsorrow', ID='823323480682680320')
user2 = User(screenName='rachaelxss', ID='939604578')
user3 = User(screenName='tonyheath2011', ID='248816806')
user4 = User(screenName='mommawedel', ID='47293791')
userList = [user1, user2, user3, user4]
main(userList)
'''
main()
#testSVM()


import numpy as np
import torch
import torch.nn as nn
from torchvision.transforms import ToTensor
from random import gauss
from torch.utils import data
from PIL import Image
from skimage.measure import label

np.random.seed(101)

def get_paired_patch(mr, mr_patch_size, mask, mask_patch_size, nPatches) -> 'List':  # MR, patchsize
    """
    :param mr: MR image 3D
    :param inputpatchsize: [32,32,3]
    :param mask: Binary mask 3D
    :param maskpatchsize: [16,16,3]
    :param nPatches: how many patches
    :return: center shapes are e.g.: (3,500) format
    """
    # trainLabelDir = r'C:\StorageDriveAll\Data\Assignment3\TrainImageLabels'
    foreground = np.array(np.where(mask == 1))  #foreground shape-> (3, 11133) shape[1]-> take all points in mask gt 0
    # print(foreground.shape)
    centers = foreground[:, np.random.permutation(foreground.shape[1])[:nPatches]]
    # print(centers[:, -3])
    # mr_patch_size = inputpatchsize  # [32,32,3]
    # mask_patch_size = maskpatchsize  # [16,16,3]
    # print("Centers shape:", centers.shape)
    mr_patch = []
    mask_patch = []
    bottom = 0
    top = 0
    mid = 0
    for i in range(centers.shape[1]): # number of centers
        if centers[0, i] < int(mr_patch_size[0]/2): # 64
            centers[0, i] = int(mr_patch_size[0]/2)
            # top += 1
        elif centers[0, i] > int(mr.shape[0] - mr_patch_size[0]/2):
            centers[0, i] = int(mr.shape[0] - mr_patch_size[0]/2)

        if centers[1, i] < int(mr_patch_size[1] / 2):
            centers[1, i] = int(mr_patch_size[1] / 2)
            # top += 1
        elif centers[1, i] > int(mr.shape[1] - mr_patch_size[1] / 2):
            centers[1, i] = int(mr.shape[1] - mr_patch_size[1] / 2)

        if centers[2, i] < int(mr_patch_size[2] / 2):
            centers[2, i] = int(mr_patch_size[2] / 2)
            # top += 1
        elif centers[2, i] > int(mr.shape[2] - mr_patch_size[2] / 2):
            centers[2, i] = int(mr.shape[2] - mr_patch_size[2] / 2)
            # print("Bottom")
        #     bottom += 1
        # else:
        #     mid += 1
        #     pass #continue #if continue the loop starts from i again
            # print("Passed")
        # print(centers[:, i])
        patch1 = mr[(centers[0, i] - np.int(mr_patch_size[0] / 2)):(centers[0, i] + np.int(mr_patch_size[0] / 2)),
                (centers[1, i] - np.int(mr_patch_size[1] / 2)):(centers[1, i] + np.int(mr_patch_size[1] / 2)),
                (centers[2, i] - np.int(mr_patch_size[2] / 2)):(centers[2, i] + np.int(mr_patch_size[2] / 2))]
        # print(np.shape(patch1))
        mr_patch.append(patch1)

        patch2 = mask[(centers[0, i] - np.int(mask_patch_size[0] / 2)):(centers[0, i] + np.int(mask_patch_size[0] / 2)),
                (centers[1, i] - np.int(mask_patch_size[1] / 2)):(centers[1, i] + np.int(mask_patch_size[1] / 2)),
                (centers[2, i] - np.int(mask_patch_size[2] / 2)):(centers[2, i] + np.int(mask_patch_size[2] / 2))]
        # print(np.array(patch).shape)
        # print(np.shape(patch2))
        mask_patch.append(patch2)

        if (np.shape(patch1) != mr_patch_size) or (np.shape(patch2) != mask_patch_size):
            # print(np.shape(ctPatch))
            # print(fInd)
            print(i)
            msg = 'issues found in center: ', centers[:, i]
            # raise msg
            # break
            print(msg)
    # print("Top", top)
    # print("Bottom:", bottom)
    # print("Mid", mid)
    return mr_patch, mask_patch  # centers

def padzero(vector, pad_width, iaxis, kwargs):
    vector[:pad_width[0]] = 0
    vector[-pad_width[1]:] = 0
    return vector

class SpleenDataset2(data.Dataset):
    def __init__(self, CT, Mask, transform=None):  # todo: how many subMR should be in one input?? shape?
        self.CT = CT
        self.Mask = Mask
        self.transform = transform

    def getSWN(self, img, x, y):  # soft window normalization
        #         x = 10;y=10
        L = gauss(40, x)
        W = gauss(200, y)
        #         print(L, W)
        W = abs(W)
        maxThr = L + W
        minThr = L - W
        img[img > maxThr] = maxThr
        img[img < minThr] = minThr
        img = 255 * ((img - minThr) / (maxThr - minThr))
        return img

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

    def __getitem__(self, item):
        CT = self.getSWN(self.CT[item], 10, 10)
        # CT = np.concatenate((CT[:, :, np.newaxis], CT[:, :, np.newaxis], CT[:, :, np.newaxis]), axis=2)
        CT = Image.fromarray(CT)
        if self.Mask is not None:
            Mask = Image.fromarray(np.uint8(255 * self.Mask[item]))
            if self.transform is not None:  # in test cases
                CT = self.transform(CT)
                CT = CT.unsqueeze(0)  # (64, 64, 8) -> (1, 64, 64, 8) creating channel/filter
                Mask = self.transform(Mask)
                Mask = Mask.unsqueeze(0)
            else: # we have mask but no transformation -> validation
                CT = ToTensor()(CT)
                CT = CT.unsqueeze(0)
                Mask = ToTensor()(Mask)
                Mask = Mask.unsqueeze(0)
            return CT, Mask

        else: # no mask
            CT = ToTensor()(CT)
            CT = CT.unsqueeze(0)
            return CT

class SpleenDataset(data.Dataset):
    def __init__(self, CT, Mask):
        self.CT = CT
        self.Mask = Mask

    def getSWN(self, img, x, y):  # soft window normalization
        #         x = 10;y=10
        L = gauss(40, x)
        W = gauss(200, y)
        #         print(L, W)
        W = abs(W)
        maxThr = L + W
        minThr = L - W
        img[img > maxThr] = maxThr
        img[img < minThr] = minThr
        img = ((img - minThr) / (maxThr - minThr))
        return img

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

    def __getitem__(self, item):
        #         CT = self.CT[item]
        CT = self.getSWN(self.CT[item], 10, 10)
        # CT = torch.tensor(CT)
        CT = torch.from_numpy(CT.astype(np.float32))
        CT = CT.unsqueeze(0)  # (64, 64, 8) -> (1, 64, 64, 8) creating channel/filter
        if self.Mask is not None: # in test cases
            Mask = self.Mask[item]
            # Mask = torch.tensor(Mask)
            Mask = torch.from_numpy(Mask.astype(np.float32))
            Mask = Mask.unsqueeze(0)
            return CT, Mask

        else:
            return CT

def get_paired_patch2D(img, imgSize, msk, mskSize, nPatches):
    """
    :param img:
    :param imgSize:
    :param msk:
    :param mskSize:
    :param nPatches:
    :return:
    """
    foreground = np.array(np.where(msk == 1))
    centers = foreground[:, np.random.permutation(foreground.shape[1])[:nPatches]]
    img_patch = []
    msk_patch = []

    for i in range(centers.shape[1]): # number of centers
        if centers[0, i] < int(imgSize[0]/2): # 64
            centers[0, i] = int(imgSize[0]/2)
            # top += 1
        elif centers[0, i] > int(img.shape[0] - imgSize[0]/2):
            centers[0, i] = int(img.shape[0] - imgSize[0]/2)

        if centers[1, i] < int(imgSize[1] / 2):
            centers[1, i] = int(imgSize[1] / 2)
            # top += 1
        elif centers[1, i] > int(img.shape[1] - imgSize[1] / 2):
            centers[1, i] = int(img.shape[1] - imgSize[1] / 2)

        if centers[2, i] == img.shape[2]:
            centers[2,i] = img.shape[2] - 1
        elif centers[2, i] == 0:
            centers[2, i] = 1

        patch1 = img[(centers[0, i] - np.int(imgSize[0] / 2)):(centers[0, i] + np.int(imgSize[0] / 2)),
                 (centers[1, i] - np.int(imgSize[1] / 2)):(centers[1, i] + np.int(imgSize[1] / 2)),
                 (centers[2, i] - 1):(centers[2, i] + 2)]
        # print(np.shape(patch1))
        img_patch.append(patch1)

        patch2 = msk[(centers[0, i] - np.int(mskSize[0] / 2)):(centers[0, i] + np.int(mskSize[0] / 2)),
                 (centers[1, i] - np.int(mskSize[1] / 2)):(centers[1, i] + np.int(mskSize[1] / 2)),
                 (centers[2, i] - 1):(centers[2, i] + 2)]
        # print(np.array(patch).shape)
        # print(np.shape(patch2))
        msk_patch.append(patch2)

    return img_patch, msk_patch

def dice_loss(input, target):
    """Dice loss.
    :param input: The input (predicted)
    :param target:  The target (ground truth)
    :returns: the Dice score between 0 and 1.
    """
    eps = 0.0001
    iflat = input.view(-1)
    tflat = target.view(-1)
    intersection = (iflat * tflat).sum()
    union = iflat.sum() + tflat.sum()
    dice = (2.0 * intersection + eps) / (union + eps)
    return 1 - dice

class DiceLoss(nn.Module):
    def __init__(self):
        super(DiceLoss, self).__init__()
        self.smooth = 1.0

    def forward(self, y_pred, y_true):
        assert y_pred.size() == y_true.size()
        y_pred = y_pred[:, 0].contiguous().view(-1)
        y_true = y_true[:, 0].contiguous().view(-1)
        intersection = (y_pred * y_true).sum()
        dsc = (2. * intersection + self.smooth) / (
            y_pred.sum() + y_true.sum() + self.smooth)
        return 1. - dsc

def getLargestCC(segmentation):
    labels = label(segmentation)
    assert( labels.max() != 0 ) # assume at least 1 CC
    largestCC = labels == np.argmax(np.bincount(labels.flat)[1:])+1
    return largestCC


# import urllib
# from bs4 import BeautifulSoup
# urlString = 'http://thetvdb.com/?tab=seasonall&id=79429&lid=7'
# html_doc = urllib.request.urlopen(urlString)
# soup = BeautifulSoup(html_doc, 'html.parser')

# text = soup.get_text()

# f = open("myfile.mkv", "w", encoding="utf-8")
# for line in text:
    # f.writelines(line)

# f.close()

import csv
import shutil
import os
import re

#os.chdir('C:\myscripts\py\curiousGeorge\curiousGeorge\data')

#with open('curiousGeorge.csv', 'r') as csvfile:
#    reader = csv.DictReader(csvfile)
#    for row in reader:
#        matchPattern = re.compile("^(.*?)(" +row['Title'] + ")(.*?$)")
#        for matchFiles in os.listdir('Curious George Collection\\'):
#            mo = matchPattern.search(matchFiles)

#            if mo == None:
#                continue
#            else:
#                print(mo)

#with open('curiousGeorge.csv', 'r') as csvfile:
#    reader = csv.DictReader(csvfile)
#    #for row in reader:
##        # print(row['Episode'] + '-' + row['Title'])
##matchPattern = re.compile("^(.*?)(Curious George Flies a Kite)(.*?$)")
#    for matchFiles in os.listdir('Curious George Collection\\'):
#        for row in reader:
#            matchPattern = re.compile("^(.*?)(" +row['Title'] + ")(.*?$)")
#            mo = matchPattern.search(matchFiles)

#            if mo == None:
#                continue
#            else:
#                print(mo)

with open('curiousGeorge.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    mydict = {rows[1]:rows[0] for rows in reader}
    for file in os.listdir('Curious George Collection\\'):
            matchPattern = re.compile("^[0-9]{2}-([^.].*)\.mkv")
            mo = matchPattern.search(file)

            if mo == None:
                print('The following does not appear to be in a valid format: ' + file)
            elif mo.group(1) in mydict:
                newName = (mydict[mo.group(1)] + ' - ' + mo.group(1) + '.mkv')
                dst = os.path.abspath('Curious George Collection\\')
                oldFile = os.path.join(dst,file)
                newFile = os.path.join(dst, newName)
                #print('Renaming "%s" to "%s" ... ' % (oldFile, newFile))
                shutil.move(oldFile, newFile)
            else:
                print('Could not find: ' + mo.group(1) + ' in the dictionary')


# test various functions in the utils function, or elsewhere

from parameterized import parameterized

from pyjac.utils import enum_to_string, listify
from pyjac.loopy_utils.loopy_utils import JacobianType, JacobianFormat
from pyjac.libgen import build_type

@parameterized([(JacobianType.exact, 'exact'),
                (JacobianType.approximate, 'approximate'),
                (JacobianType.finite_difference, 'finite_difference'),
                (JacobianFormat.sparse, 'sparse'),
                (JacobianFormat.full, 'full'),
                (build_type.chem_utils, 'chem_utils'),
                (build_type.species_rates, 'species_rates'),
                (build_type.jacobian, 'jacobian')])
def test_enum_to_string(enum, string):
    assert enum_to_string(enum) == string

@parameterized([('a', ['a']),
                ([1, 2, 3], [1, 2, 3]),
                ((1, 2, 'a'), [1, 2, 'a']),
                (3, [3])])
def test_listify(value, expected):
    assert listify(value) == expected


#kaggle路径如下：https://www.kaggle.com/c/santa-2019-revenge-of-the-accountants/overview/evaluation

import sys
sys.path.append('./')
'''
sys.path.append('../model')
sys.path.append('../util')
sys.path.append('../source')
sys.path.append('../')

'''
from model.my_solver import MySolver
from util.common import get_penalty, SantaTools
from util.configure import *
import numpy as np
import pandas as pd

pcost_mat = np.full(shape=(N_FAMILY, N_DAYS), fill_value= np.inf)
acost_mat = np.zeros((MAX_OCCUPANCY, MAX_OCCUPANCY), dtype='float64')

#数据加载
data = pd.read_csv('E:\\private\\学习\\2020-bi-programing-practice\\随便写写\\intelligen_logistics\\L22\\santa\\family_data.csv',index_col='family_id')
DESIRED = data.iloc[:,:-1].values -1
for i in range(N_FAMILY):
    n_people = data.loc[i, 'n_people']
    pcost_mat[i,:] = get_penalty(n_people, i, DESIRED[i], N_DAYS)
for i in range(acost_mat.shape[0]):
    for j in range(acost_mat.shape[1]):
        acost_mat[i,j] = max(0, ((i+1-125)/400)*((i+1)** (1/2+abs(i-j+1)/50)))
FAMILY_SIZE = data['n_people'].values

if __name__ == '__main__':
    solver = MySolver(N_FAMILY, N_DAYS, MIN_OCCUPANCY, MAX_OCCUPANCY, DESIRED, FAMILY_SIZE)
    prediction = solver.solveSanta(pcost_mat, acost_mat)
    tools = SantaTools(FAMILY_SIZE, DESIRED, pcost_mat, acost_mat)
    pcost_value, occupancy = tools.pcost(prediction, FAMILY_SIZE, pcost_mat)
    acost_value, _ = tools.acost(occupancy, acost_mat)
    print('pcost_value:{}, acost_value:{:.2f}, (occ.min:{}, occ.max:{})'.format(pcost_value, acost_value, occupancy.min(), occupancy.max()))
    score = tools.cost_function(prediction)
    print('score:{}'.format(score))
    new_prediction = prediction.copy()
    tools.find_better_day_for_family(new_prediction)

    final = tools.stochastic_product_search(top_k=2,
        fam_size=8,
        original=new_prediction,
        n_iter=10000,
        verbose=1000,
        verbose2=50000,
        random_state=2019
        )
    final = tools.seed_finding(2019, final)


def bsearch(arr, beg, end, target):
    print("beg, end, target = ", beg, end, target)
    if beg > end:
        return -1
    mid = beg + (end - beg) // 2

    '''
    2 element in arr special case

    '''

    if (mid - 1 < 0 or mid + 1 > len(arr) - 1) and len(arr) != 2:
        return mid

    if mid - 1 < 0 or mid + 1 > len(arr) - 1 and len(arr) == 2:
        if arr[0] == target:
            return 0
        if arr[1] == target:
            return len(arr) -1
        return -1

    if arr[mid - 1] <= target <= arr[mid]:

        return mid

    elif arr[mid] > target:
        return bsearch(arr, beg, mid - 1, target)
    else:
        return bsearch(arr, mid + 1, end, target)

# Test array
arr = [1, 2]
x = 1

# Function call
print(bsearch(arr, 0, len(arr) - 1, x))