import jax
import flax
import jax.numpy as jnp
import numpy as np
from scipy import integrate
from models import utils as mutils

def get_div_fn(fn):
  """Create the divergence function of `fn` using the Hutchinson-Skilling trace estimator."""

  def div_fn(x, t, eps):
    grad_fn = lambda data: jnp.sum(fn(data, t) * eps)
    grad_fn_eps = jax.grad(grad_fn)(x)
    return jnp.sum(grad_fn_eps * eps, axis=tuple(range(1, len(x.shape))))

  return div_fn

def get_likelihood_fn(sde, model, inverse_scaler, hutchinson_type='Rademacher',
                      rtol=1e-5, atol=1e-5, method='RK45', eps=1e-5):
  """Create a function to compute the unbiased log-likelihood estimate of a given data point.

  Args:
    sde: A `sde_lib.SDE` object that represents the forward SDE.
    model: A `flax.linen.Module` object that represents the architecture of the score-based model.
    inverse_scaler: The inverse data normalizer.
    hutchinson_type: "Rademacher" or "Gaussian". The type of noise for Hutchinson-Skilling trace estimator.
    rtol: A `float` number. The relative tolerance level of the black-box ODE solver.
    atol: A `float` number. The absolute tolerance level of the black-box ODE solver.
    method: A `str`. The algorithm for the black-box ODE solver.
      See documentation for `scipy.integrate.solve_ivp`.
    eps: A `float` number. The probability flow ODE is integrated to `eps` for numerical stability.

  Returns:
    A function that takes random states, replicated training states, and a batch of data points
      and returns the log-likelihoods in bits/dim, the latent code, and the number of function
      evaluations cost by computation.
  """

  def drift_fn(state, x, t):
    """The drift function of the reverse-time SDE."""
    score_fn = mutils.get_score_fn(sde, model, state.params_ema, state.model_state, train=False, continuous=True)
    # Probability flow ODE is a special case of Reverse SDE
    rsde = sde.reverse(score_fn, probability_flow=True)
    return rsde.sde(x, t)[0]

  @jax.pmap
  def p_div_fn(state, x, t, eps):
    """Pmapped divergence of the drift function."""
    div_fn = get_div_fn(lambda x, t: drift_fn(state, x, t))
    return div_fn(x, t, eps)

  p_drift_fn = jax.pmap(drift_fn)  # Pmapped drift function of the reverse-time SDE
  p_prior_logp_fn = jax.pmap(sde.prior_logp)  # Pmapped log-PDF of the SDE's prior distribution

  def likelihood_fn(prng, pstate, data):
    """Compute an unbiased estimate to the log-likelihood in bits/dim.

    Args:
      prng: An array of random states. The list dimension equals the number of devices.
      pstate: Replicated training state for running on multiple devices.
      data: A JAX array of shape [#devices, batch size, ...].

    Returns:
      bpd: A JAX array of shape [#devices, batch size]. The log-likelihoods on `data` in bits/dim.
      z: A JAX array of the same shape as `data`. The latent representation of `data` under the
        probability flow ODE.
      nfe: An integer. The number of function evaluations used for running the black-box ODE solver.
    """
    rng, step_rng = jax.random.split(flax.jax_utils.unreplicate(prng))
    shape = data.shape
    if hutchinson_type == 'Gaussian':
      epsilon = jax.random.normal(step_rng, shape)
    elif hutchinson_type == 'Rademacher':
      epsilon = jax.random.randint(step_rng, shape,
                                   minval=0, maxval=2).astype(jnp.float32) * 2 - 1
    else:
      raise NotImplementedError(f"Hutchinson type {hutchinson_type} unknown.")

    def ode_func(t, x):
      sample = mutils.from_flattened_numpy(x[:-shape[0] * shape[1]], shape)
      vec_t = jnp.ones((sample.shape[0], sample.shape[1])) * t
      drift = mutils.to_flattened_numpy(p_drift_fn(pstate, sample, vec_t))
      logp_grad = mutils.to_flattened_numpy(p_div_fn(pstate, sample, vec_t, epsilon))
      return np.concatenate([drift, logp_grad], axis=0)

    init = jnp.concatenate([mutils.to_flattened_numpy(data), np.zeros((shape[0] * shape[1],))], axis=0)
    solution = integrate.solve_ivp(ode_func, (eps, sde.T), init, rtol=rtol, atol=atol, method=method)
    nfe = solution.nfev
    zp = jnp.asarray(solution.y[:, -1])
    z = mutils.from_flattened_numpy(zp[:-shape[0] * shape[1]], shape)
    delta_logp = zp[-shape[0] * shape[1]:].reshape((shape[0], shape[1]))
    prior_logp = p_prior_logp_fn(z)
    bpd = -(prior_logp + delta_logp) / np.log(2)
    N = np.prod(shape[2:])
    bpd = bpd / N
    # A hack to convert log-likelihoods to bits/dim
    # based on the gradient of the inverse data normalizer.
    offset = jnp.log2(jax.grad(inverse_scaler)(0.)) + 8.
    bpd += offset
    return bpd, z, nfe

  return likelihood_fn


"""
Check symbols in watch list to make sure they are tradable, and get Smart Routing information.

Only common stocks in USD currency are selected.

"""

import sqlite3
from pprint import pprint
import threading
import time
from datetime import datetime

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.client import MarketDataTypeEnum
from ibapi.ticktype import TickTypeEnum

import settings

results = list()

class IBapi(EWrapper, EClient):
    """
    Client that communicates with IB Gateway.

    """

    def __init__(self):
        EClient.__init__(self, self)

    def symbolSamples(self, reqId: int,
                      contractDescriptions):
        """
        Handle reponse to matching symbol lookup.

        Args:
            reqId:                      Request ID.
            contractDescriptions:       List of contract descriptions.

        """

        super().symbolSamples(reqId, contractDescriptions)

        candidiates = list()

        # Iterate through each item in contract description list.
        for desc in contractDescriptions:
            deriv_sec_types = ''
            candidiates.append((desc.contract.symbol,
                                desc.contract.secType,
                                desc.contract.primaryExchange,
                                desc.contract.currency))

        # Filter search result to only common stock, name matching query pattern exactly, and in USD currency.
        candidiates = list(filter(lambda desc: desc[0] == symbol and desc[1] == 'STK' and desc[3] == 'USD',
                                  candidiates))

        # Catch no match.
        assert len(candidiates) == 1

        # Store result.
        global results
        c = candidiates[0]
        results.append(c)

def run_loop():
    app.run()

# Create IB client and connect to IB Gateway API.
app = IBapi()
app.connect('127.0.0.1', 4002, 1)

# Launch client thread.
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

# Give client time to establish conneciton.
time.sleep(1)

# Specify day in watchlist to check.
#date = '2020-07-24'
date = datetime.now().strftime('%Y-%m-%d')

db = sqlite3.connect(settings.DATA_DIRECTORY / settings.IB_DATABASE_NAME)

# Get symbols on watchlist to check.
rows = db.execute('SELECT symbol FROM ib_watch WHERE date=?',
                  (date,)).fetchall()
rows = list([date, r[0], '', ''] for r in rows)

for idx in range(len(rows)):

    # Make symbol match request to client.
    symbol = rows[idx][1]
    print(f'Querying {symbol}')
    app.reqMatchingSymbols(idx, symbol)

    # Rate limit is 1 query per second.
    time.sleep(1.1)

# Give client time to finish requests.
time.sleep(10)

results = tuple((r[1], r[2], r[3], r[0]) for r in results)
pprint(results)

# Update 'ib_watch' table with query results.
db.executemany('''
UPDATE ib_watch SET type=?, primary_exchange=?, currency=?
    WHERE symbol==?;''', results)

db.commit()

db.close()
app.disconnect()


from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django.conf import settings

# Create your models here.
class UserProfile(models.Model):
    body_lean = 'Ecto'
    body_healthy = 'Endo'
    body_normal = 'Meso'

    body_type_choices = [
        (body_lean, 'Ectomorph'),
        (body_healthy, 'Endomorph'),
        (body_normal, 'Mesomorph'),
    ]

    height = models.CharField(max_length=255)
    weight = models.IntegerField()
    body_type = models.CharField(max_length=4, choices=body_type_choices, default=body_normal)
    phone = models.CharField(max_length=255, unique=True)
    birth_date = models.DateField(null=True)
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    def __str__(self):
        return f'{self.user.first_name} {self.user.last_name}'

    def first_name(self):
        return self.user.first_name

    def last_name(self):
        return self.user.last_name

    class Meta:
        ordering = ['user__first_name', 'user__last_name']

class Exercise(models.Model):
    body_part = models.CharField(max_length=255)
    muscle_name = models.CharField(max_length=255)
    exercise_name = models.CharField(max_length=255)
    sets = models.CharField(max_length=255)
    reps = models.CharField(max_length=255)

    def __str__(self) -> str:
        return self.exercise_name

    class Meta:
        ordering = ['body_part']

class WorkoutList(models.Model):
    created_at = models.DateField(auto_now_add=True)
    title = models.CharField(max_length=255, default="")

    def __str__(self) -> str:
        return self.title

    class Meta:
        ordering = ['title']

class WorkoutStructure(models.Model):
    workout = models.ForeignKey(WorkoutList, on_delete=models.CASCADE, related_name='workout_lists')
    body_part = models.CharField(max_length=255,default="", blank=True)
    muscle_name = models.CharField(max_length=255,default="", blank=True)
    exercise_name = models.CharField(max_length=255, default="", blank=True)
    sets = models.CharField(max_length=255, default="", blank=True)
    reps = models.CharField(max_length=255, default="", blank=True)

    def __str__(self) -> str:
        return self.exercise_name

    class Meta:
        ordering = ['body_part']

class WorkoutLog(models.Model):
    created_at = models.DateField(auto_now_add=True)
    work_list = models.ForeignKey(WorkoutList, on_delete=models.CASCADE)
    work_structure = models.ForeignKey(WorkoutStructure, on_delete=models.CASCADE)
    set_count = models.IntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(10)]
    )
    rep_count = models.IntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(50)]
    )
    weight = models.IntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(300)]
    )
    note = models.TextField()

    def __str__(self) -> str:
        return self.note

    class Meta:
        ordering = ['created_at']

class ProgressImage(models.Model):
    workout_no = models.ForeignKey(WorkoutList, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True)
    image = models.ImageField(upload_to='base/images', null=True)


#from re import S
from typing import Text
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/", methods=["GET", "POST"])
def index():
    sistema = str(request.form.get("sistema"))
    ip = str(request.form.get("ip"))
    qm = str(request.form.get("qm"))
    cpu = str(request.form.get("CPU"))
    select = []
    if request.method == "GET":
        return render_template("index.html")
    else:
        select.append(f"\n Sistema Operacional: {sistema}")
        select.append(f"\n Endereço IP: {ip}")
        select.append(f"\n Quantidade de memória:{qm}")
        select.append(f"\n Quantidade de CPU: {cpu}")
        info = open("info.txt","w")
        info.writelines(select)
        info.close()
    return Text(select)
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5050, debug=True)


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import os
import requests
import time
import re
from bs4 import BeautifulSoup
import json

chromedriver = "/driver/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
actions = ActionChains(driver)

base_url = 'https://www.archdaily.com/'

project_link = base_url + input('Project Code : ')

def project_scraper(driver , project_link):
            response = requests.get(project_link)
            soup = BeautifulSoup(response.content , 'html.parser')
            paragraph = soup.find_all('p')
            list_of_paragraph = []
            for each_paragpraph in paragraph:
                list_of_paragraph.append(each_paragpraph.text.strip())
            driver.get(project_link)

            timeout = 20
            try:
                WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, '//*[contains(concat( " ", @class, " " ), concat( " ", "js-char-list", " " ))]')))
            except TimeoutException:
                print('Timed out waiting for page to load')
                driver.quit()
            try:
                area =  driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-specs__item", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]')[0].text.split(':')[1].strip()
                year =  driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-specs__item", " " )) and (((count(preceding-sibling::*) + 1) = 3) and parent::*)]')[0].text.split(':')[1].strip()
                photographs = driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-specs__item", " " )) and (((count(preceding-sibling::*) + 1) = 4) and parent::*)]')[0].text.split(':')[1].strip()
            except:
                area = ''
                year = ''
                photographs = ''
            project_dict =  {
                    'Project ID': project_link[26:32],
                    'Project Titel' :driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-title-big--bmargin-big", " " ))]')[0].text.split('/')[0].strip(),
                    'Project Type': driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-specs__header-category", " " ))]//a')[0].text.strip(),
                    'City': driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-specs__header-location", " " ))]')[0].text.split(',')[0].strip(),
                    'Country': driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-specs__header-location", " " ))]//a')[0].text.strip(),
                    'Architects': driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "afd-specs__item", " " )) and (((count(preceding-sibling::*) + 1) = 1) and parent::*)]')[0].text.split(':')[1].strip(),
                    'Area': area,
                    'Year': year,
                    'Photographs': photographs,
                    'Project URL': project_link.strip(),
                    'Text Content': list_of_paragraph[7:-2]

            }
            project_js = json.dumps(project_dict, indent=4 , ensure_ascii=False).encode('utf8')
            with open('projct_details.json', 'w' , encoding='utf-8') as file:
                file.write(project_js.decode() + ',')
            print('Project details are ready.')

def project_image_downloader(driver ,project_link):
    response = requests.get(project_link)
    soup = BeautifulSoup(response.content , 'html.parser')
    a_tag = soup.find('a' , attrs={'class':'gallery-link afd-desktop-e'})
    link = base_url + a_tag.get('href')
    driver.get(link)

    timeout = 20
    try:
        WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, '//*[contains(concat( " ", @class, " " ), concat( " ", "js-gal-mob-img-onview", " " ))]')))
    except TimeoutException:
        print('Timed out waiting for page to load')
        driver.quit()

    this_table = driver.find_elements_by_id('gallery-items')
    for this in this_table:
        temp = this.get_attribute('data-images')

    large_urls = []

    urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', temp)
    for url in urls:
        if 'large_jpg' in str(url):
            large_urls.append(url)
    i = 1
    for each in large_urls:
        with open('{}.jpg'.format(i), 'wb') as handle:
                response = requests.get(each, stream=True)
                print('Picture number {} downloaded'.format(i))
                time.sleep(2)

                if not response.ok:
                    print(response)

                for block in response.iter_content(1024):
                    if not block:
                        break

                    handle.write(block)
        i += 1

if __name__ == "__main__":
    project_scraper(driver , project_link)
    project_image_downloader(driver ,project_link)


import threading
from ..lib.drush import DrushAPI
from ..lib.thread_progress import ThreadProgress

import sublime
import sublime_plugin

class DrushUpdateDbCommand (sublime_plugin.WindowCommand):
    """
    A command to invoke update-db.
    """

    def run(self):
        self.drush_api = DrushAPI(self.window.active_view())
        self.drupal_root = self.drush_api.get_drupal_root()
        if self.drupal_root is "drush":
            sublime.status_message('Could not invoke update-db as you are not '
                                   'working in a Drupal directory!')
            return
        thread = DrushUpdateDbThread(self.window, self.drush_api)
        thread.start()
        ThreadProgress(thread,
                       'Invoking update-db for %s' % self.drupal_root,
                       "Update.php was successfully run on '%s'" %
                       self.drupal_root)

class DrushUpdateDbThread(threading.Thread):
    """
    A thread to run update-db.
    """
    def __init__(self, window, drush_api):
        self.window = window
        self.drush_api = drush_api
        threading.Thread.__init__(self)

    def run(self):
        self.drush_api.run_command('updatedb', list(), list())


import json
import base64
import logging
from util.lambda_utils import extract_data_from_log
from model.execution_log import ExecutionLog

logger = logging.getLogger(__name__)

class LambdaFunction:
    """ Class representing AWS Lambda function """

    def __init__(self, arn: str, lambda_client):
        self.arn = arn
        self.client = lambda_client

    def list_aliases(self):
        """ Returns all aliases of Lambda function """
        return self.client.list_aliases(FunctionName=self.arn)['Aliases']

    def delete_all_lambda_aliases(self):
        aliases = self.list_aliases()
        for alias in aliases:
            self.delete_alias(alias=alias['Name'])
            self.delete_version(version=alias['FunctionVersion'])

    def get_alias(self, alias: str):
        """ Returns details about a Lambda alias
        :param alias: Alias of Lambda
        :return dict: details of Lambda alias (AliasArn, Name, FunctionVersion, ...)
        """
        logger.info(f"Checking alias {alias}")
        return self.client.get_alias(FunctionName=self.arn, Name=alias)

    def get_config(self, alias: str = None):
        """ Returns the configuration for the Lambda
         :param alias: Alias of Lambda
         :return configuration of Lambda or Lambda alias
         """
        if alias:
            return self.client.get_function_configuration(FunctionName=self.arn, Qualifier=alias)
        else:
            return self.client.get_function_configuration(FunctionName=self.arn)

    def get_memory_size(self, alias: str = None):
        """ Returns the configured memory size for the Lambda
        :param alias: Alias of Lambda
        :return memory size of Lambda or Lambda alias
        """
        return self.get_config(alias)['MemorySize']

    def get_time_out(self, alias: str = None):
        """ Returns the configured timeout for the Lambda
        :param alias: Alias of Lambda
        :return timeout of Lambda or Lambda alias in seconds
       """
        return self.get_config(alias)['Timeout']

    def verify_alias_exists(self, alias: str) -> bool:
        """ Checks if an alias exists for the Lambda function
        :param alias: Alias to check
        :return True if alias exists, else False
        """
        try:
            self.get_alias(alias)
            return True
        except self.client.exceptions.ResourceNotFoundException:
            return False
        except Exception as err:
            # real exception
            raise err

    def create_memory_config(self, value: int, alias: str):
        """ Creates a new Lambda alias with given memory size
        :param value: memory size for configuration
        :param alias: Alias for the memory config
        """
        try:
            self.set_memory_size(value)
            version = self.publish_version()['Version']
            if self.verify_alias_exists(alias):
                self.update_alias(alias, version)
            else:
                self.create_alias(alias, version)
        except Exception as error:
            if 'Alias already exists' not in str(error):
                raise error

    def set_memory_size(self, value: int):
        """ Set memory size of Lambda to given value
        :param value: memory size to set
        :return details about new Lambda configuration
        """
        if self.get_config()['MemorySize'] != value:
            logger.info(f"Setting memory size to: {value}")
            return self.client.update_function_configuration(FunctionName=self.arn, MemorySize=value)
        else:
            logger.info("Function already has given memory size")

    def publish_version(self):
        """ Create new version from current code and configuration """
        logger.info("Publishing new version")
        return self.client.publish_version(FunctionName=self.arn)

    def create_alias(self, alias: str, version: str):
        """ Creates an alias for a Lambda function version """
        logger.info(f"Creating alias: {alias}")
        return self.client.create_alias(FunctionName=self.arn, FunctionVersion=version, Name=alias)

    def update_alias(self, alias: str, version: str):
        """ Updates the configuration of a Lambda function alias
        :param alias: alias to update
        :param version: version to update
        """
        logger.info(f"Updating alias: {alias}")
        return self.client.update_alias(FunctionName=self.arn, FunctionVersion=version, Name=alias)

    def delete_alias(self, alias: str):
        """ Deletes a Lambda function alias
        :param alias: alias to be deleted
        """
        logger.info(f"Deleting alias: {alias}")
        return self.client.delete_alias(FunctionName=self.arn, Name=alias)

    def delete_version(self, version: str):
        """ Deletes a Lambda function version
        :param version: version to be deleted
        """
        logger.info(f"Deleting version: {version}")
        return self.client.delete_function(FunctionName=self.arn, Qualifier=version)

    def invoke(self, alias: str, payload: dict, log_type: str = 'Tail'):
        """ Invokes Lambda function
        :param alias: alias of Lambda function to be invoked. If not given, the standard Lambda function will be invoked.
        :param payload: input payload for Lambda function
        :param log_type: (optional) log type
        :return `ExecutionLog` containing details about execution
        """
        logger.info(f"Invoking function {self.arn}:{alias if alias else '$LATEST'} with payload {payload}")
        bytes_payload = bytes(json.dumps(payload), "utf-8")
        try:
            if alias:
                res = self.client.invoke(FunctionName=self.arn, Qualifier=alias, Payload=bytes_payload, LogType=log_type)
            else:
                res = self.client.invoke(FunctionName=self.arn, Payload=bytes_payload, LogType=log_type)
            log_result = res['LogResult']
            log_str = base64.b64decode(log_result).decode('utf-8')

            log = extract_data_from_log(log_str)
        except Exception as e:
            logger.error("Function invocation failed: " + str(e))
            memory_size = self.get_memory_size(alias=alias)
            timeout_ms = self.get_time_out(alias=alias) * 1000
            log = ExecutionLog(memory_size=memory_size, init_duration=0, duration=timeout_ms,
                               billed_duration=timeout_ms)
        return log


import pytest

from mnamer.endpoints import tmdb_find, tmdb_movies, tmdb_search_movies
from mnamer.exceptions import MnamerException, MnamerNotFoundException
from mnamer.providers import Tmdb
from tests import JUNK_TEXT, RUSSIAN_LANG

pytestmark = [
    pytest.mark.network,
    pytest.mark.tmdb,
    pytest.mark.flaky(reruns=1),
]

GOONIES_IMDB_ID = "tt0089218"
GOONIES_TMDB_ID = "9340"
JUNK_IMDB_ID = "tt1234567890"

def test_tmdb_find__imdb_success():
    expected_top_level_keys = {
        "movie_results",
        "person_results",
        "tv_episode_results",
        "tv_results",
        "tv_season_results",
    }
    expected_movie_results_keys = {
        "adult",
        "backdrop_path",
        "genre_ids",
        "id",
        "original_language",
        "original_title",
        "overview",
        "media_type",
        "poster_path",
        "popularity",
        "release_date",
        "title",
        "video",
        "vote_average",
        "vote_count",
    }
    result = tmdb_find(Tmdb.api_key, "imdb_id", GOONIES_IMDB_ID)
    assert isinstance(result, dict)
    assert set(result.keys()).issuperset(expected_top_level_keys)
    assert len(result.get("movie_results", {})) > 0
    assert expected_movie_results_keys.issuperset(
        set(result.get("movie_results", {})[0].keys())
    )

def test_tmdb_find__api_key_fail():
    with pytest.raises(MnamerException):
        tmdb_find(JUNK_TEXT, "imdb_id", GOONIES_IMDB_ID, cache=False)

def test_tmdb_find__invalid_id_imdb():
    with pytest.raises(MnamerException):
        tmdb_find(Tmdb.api_key, "imdb_id", JUNK_TEXT, cache=False)

def test_tmdb_find__not_found():
    with pytest.raises(MnamerNotFoundException):
        tmdb_find(Tmdb.api_key, "imdb_id", JUNK_IMDB_ID)

def test_tmdb_find__language():
    results = tmdb_find(Tmdb.api_key, "imdb_id", GOONIES_IMDB_ID, RUSSIAN_LANG)
    assert any(result["title"] == "Балбесы" for result in results["movie_results"])

def test_tmdb_find__invalid_source():
    with pytest.raises(MnamerException):
        tmdb_find(Tmdb.api_key, "abc123", GOONIES_IMDB_ID, cache=False)

def test_tmdb_movies__success():
    expected_top_level_keys = {
        "adult",
        "backdrop_path",
        "belongs_to_collection",
        "budget",
        "genres",
        "homepage",
        "id",
        "imdb_id",
        "original_language",
        "original_title",
        "overview",
        "popularity",
        "poster_path",
        "production_companies",
        "production_countries",
        "release_date",
        "revenue",
        "runtime",
        "spoken_languages",
        "status",
        "tagline",
        "title",
        "video",
        "vote_average",
        "vote_count",
    }
    result = tmdb_movies(Tmdb.api_key, GOONIES_TMDB_ID)
    assert isinstance(result, dict)
    assert set(result.keys()).issuperset(expected_top_level_keys)
    assert result.get("title") == "The Goonies"

def test_tmdb_movies__api_key_fail():
    with pytest.raises(MnamerException):
        tmdb_movies(JUNK_TEXT, "", cache=False)

def test_tmdb_movies__id_tmdb_fail():
    with pytest.raises(MnamerException):
        tmdb_movies(Tmdb.api_key, JUNK_TEXT, cache=False)

def test_tmdb_movies__not_found():
    with pytest.raises(MnamerNotFoundException):
        tmdb_movies(Tmdb.api_key, "1" * 10)

@pytest.mark.xfail(strict=False)
def test_tmdb_movies__language():
    result = tmdb_movies(Tmdb.api_key, GOONIES_TMDB_ID, RUSSIAN_LANG)
    assert result.get("title") == "Балбесы"

def test_tmdb_search_movies__success():
    expected_top_level_keys = {
        "page",
        "results",
        "total_pages",
        "total_results",
    }
    expected_results_keys = {
        "adult",
        "backdrop_path",
        "genre_ids",
        "id",
        "original_language",
        "original_title",
        "overview",
        "popularity",
        "poster_path",
        "release_date",
        "title",
        "video",
        "vote_average",
        "vote_count",
    }
    result = tmdb_search_movies(Tmdb.api_key, "the goonies", 1985)
    assert isinstance(result, dict)
    assert set(result.keys()).issuperset(expected_top_level_keys)
    assert isinstance(result["results"], list)
    assert expected_results_keys.issuperset(set(result.get("results", [{}])[0].keys()))
    assert result["results"][0]["original_title"] == "The Goonies"
    result = tmdb_search_movies(Tmdb.api_key, "the goonies")
    assert len(result["results"]) > 1

def test_tmdb_search_movies__bad_api_key():
    with pytest.raises(MnamerException):
        tmdb_search_movies(JUNK_TEXT, "the goonies", cache=False)

def test_tmdb_search_movies__bad_title():
    with pytest.raises(MnamerNotFoundException):
        tmdb_search_movies(Tmdb.api_key, JUNK_TEXT, cache=False)

def test_search_movies__language():
    results = tmdb_search_movies(Tmdb.api_key, "the goonies", language=RUSSIAN_LANG)
    assert any(result["title"] == "Балбесы" for result in results["results"])


from django.urls import path
from .views import *

urlpatterns = [
    path('room/add', room_add),
    path('reserve/add', reserve_add),
    path('room/delete/<int:room_id>', room_delete),
    path('reserve/delete/<int:booking_id>', reserve_delete),
    path('rooms', get_all_room),
    path('reserve', get_all_reserve),
]


"""
@Author: 邓润庭
@Date:   2021/3/19
"""

from typing import List

# 自底向上
def min_coin_num(coins: List, amount: int):
    dp = [float("inf")] * (amount + 1)
    dp[0] = 0

    for n in range(amount + 1):
        for coin in coins:
            if coin <= n:
                dp[n] = min(dp[n], dp[n - coin] + 1)

    return dp[amount] if dp[amount] != float("inf") else -1

print(min_coin_num([1, 2, 5], 6))


import spacy
import neuralcoref

nlp = spacy.load('en_core_web_sm')
neuralcoref.add_to_pipe(nlp)

def resolve_corefs(text):
    doc = nlp(text)
    text_revised=text

    for i in range(len(doc._.coref_clusters)):             #Coref clusters is a list of cluster objects
        cluster = doc._.coref_clusters[i]
        words_in_cluster=[]
        for l in cluster.mentions:                         #Each cluster objects contains mentions, a list of lists of tokens e.g. [[Jon,Snow],[he]]
            tokens_combined=""
            for text in l:
                tokens_combined+=str(text) + ' '
            words_in_cluster.append(tokens_combined.strip())
        text_revised = text_revised.replace(words_in_cluster[1],words_in_cluster[0])
    return text_revised

#Some sample strings to try

#Working examples:

#text='My sister has a dog. She loves him.'    #Detects sister=she, dog=him
#text='The President today announced a new government program. It would promote things worth promoting, he said.'
#^ Ties The President to He and 'a new government program' to 'it'.

#Kinda working:

#text='Jim is a rich man. He shaves daily with a razor. It is gold-plated.' #Detects Jim=He, but not Razor=It
#text='Johnny was so adjective, even Sarah had to admit Johhny was more adjective than she was.' #Detects Johnny=he, but says 'even Sarah'=she.


from subprocess import PIPE, run
import time
import zmq

def listen():
    ctx = zmq.Context()
    sock = ctx.socket(zmq.REP)
    sock.bind("tcp://*:8085")
    print("Starting loop for OT2 internal listener...")
    i = 1
    while True:
        msg = sock.recv_string()
        print(msg)
        i += 1
        time.sleep(1)
        if msg != None:
            msg_output, msg_error, msg_returncode = execute_command(msg)
            sock.send_string(msg_output + '@' + msg_error + '@' + str(msg_returncode))

    sock.close()

def execute_command(command):
    print("<---------------- Executing the protocol on the OT2 --------------->")
    result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
    print("Return code: ",  result.returncode)
    print("Error Message:\n" + result.stderr)
    print("Output Message:\n" + result.stdout)
    return result.stdout, result.stderr, result.returncode

def main_null():
    print("This function is not meant to have a main function")

if __name__ == "__main__":
    listen()


import debugpy

from detector import *

debugpy.listen(5678);
print("Waiting for debugger to attach");
debugpy.wait_for_client();

#rules = load_definitions("./detector.json");
det=Detector("./detector.json");
rules=det.get_rules();
print(rules);
fields=rules['fields'];
print(fields);
print(fields[1]);


#!/usr/bin/env python3

import sys
import datetime
import re
import os
import time
import json
import csv
import tempfile
import zipfile
import argparse

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

hmms_pattern = re.compile(
r"""
^(
(?P<L>L)|
(?P<yup>yup|yep)|
(?P<heh>he(h+))|
(?P<huh>hu(h+))|
(?P<why>wh(y+))|
(?P<what>wh(a+)(t*))|
(?P<mhm>(m+)h(m+))|
(?P<hmm>h(m+))|
(?P<lol>l(o+)l)|
(?P<lmao>lm(f?)(a+)(o+))|
(?P<yes>ye(s*))|
(?P<no>n(o+))|
(?P<oh>(o+)(h*))
)$
""", re.IGNORECASE | re.MULTILINE | re.VERBOSE)

def err(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    sys.exit(1)

def find_hmms(pattern, string, hmms_dict):
    results = pattern.finditer(string)
    for found in results:
        hmm_found = None
        for name, value in found.groupdict().items():
            if value is not None:
                hmm_found = name

        if hmm_found is None:
            continue

        if hmm_found not in hmms_dict:
            hmms_dict[hmm_found] = 0
        hmms_dict[hmm_found] += 1

def clean(string):
    return "".join([i if i.isalnum() else "_" for i in string])

def word_clean(word, prefix):
    return "i_%s_%s" % (clean(prefix.upper()), clean(word))

def compile_words(words, prefix):
    buf = ""
    for i in words:
        print(i)
        buf += "(?P<%s>%s)|" % (word_clean(i, prefix), i)
    buf = buf[:-1]
    print(buf)
    return re.compile(buf, re.IGNORECASE | re.MULTILINE | re.VERBOSE)

def get_dms(path):
    try:
        with open(os.path.join(path, "index.json"), encoding="utf-8") as f:
            names = json.load(f)
    except Exception:
        err("Could not find %s" % os.path.join(path, "index.json"))

    # generate list of dms
    #     channel id : name
    print("Collecting Names")
    dms = {}
    for i in os.listdir(path):
        if os.path.isdir(os.path.join(path, i)):
            with open(os.path.join(path, i, "channel.json"), encoding="utf-8") as file:
                if json.load(file)["type"] == 1:
                    name = names[i[1:]]
                    startstr = "Direct Message with "
                    dms[i] = name[len(startstr):] if name.startswith(startstr) else name
    return dms

def read(path, args):
    path = os.path.join(path, "messages/")
    print("Finding DMs")
    dms = get_dms(path)
    if args.users: # if we are selecting by users find them
        args.users = [i.lower() for i in args.users] # to make it not case sensetive
        selected = dict(filter(lambda u: any_in(args.users, u[1].lower()), dms.items()))
        if selected:
            dms = selected
        else:
            print(f"Can't find a user named {args.users}, showing all", file=sys.stderr)

    # Read contents of DMs
    print("Reading DMs")
    leaders = {}

    for directory, username in dms.items():
        with open(os.path.join(path, directory, "messages.csv"), encoding="utf-8") as f:
            timestamp = []
            hmms = []
            msgs = csv.reader(f)
            next(msgs) # skip the header
            msgs = reversed(list(msgs))
            pattern = hmms_pattern if args.words is None else compile_words(args.words, username.split('#')[0])
            hmms_dict = {}

            for line in msgs:
                timestamp.append(datetime.datetime.fromisoformat(line[1]))
                msg_content = line[2]
                find_hmms(pattern, msg_content, hmms_dict)
                hmms.append(hmms_dict.copy())

            msg_total = [i for i in range(len(timestamp))]
            if len(msg_total) != 0:
                leaders[username] = [timestamp, msg_total, hmms]

    # sort leaders
    leaders = dict(sorted(leaders.items(), key=lambda i: i[1][1]))

    if args.list:
        total_total = 0
        for name, v in leaders.items():
            total_messages = v[1][-1]
            total_total += total_messages
            print(f"{name:<40} {total_messages:>10}")
        name = "Total"
        print(f"{name:<40} {total_total:>10}")
        return

    # use -n and -s args to determine how many users to show assumes the leaders have been sorted
    leaders = {k: leaders[k] for k in list(reversed(list(leaders)))[args.startafter:args.numlines]}

    # find the maximum timestamp and add it to all of the lists to make lines reach the end
    max_timestamp = max([max(v[0]) for _, v in leaders.items() if len(v[0]) != 0])
    for k, v in leaders.items():
        v[0].append(max_timestamp)
        v[1].append(v[1][-1])

    # TODO clean up
    # transpose hmms into lists of counts
    for k, v in leaders.items():
        transposed_hmms = []
        hmms_list_of_dicts = v[2]
        hmm_types = hmms_list_of_dicts[-1].keys()
        for name in hmm_types:
             values = [hmm_dict.get(name, 0) for hmm_dict in hmms_list_of_dicts]
             values.append(values[-1])
             transposed_hmms.append((name, values))

        leaders[k][2] = transposed_hmms

    startafter = args.startafter
    if startafter > len(leaders):
        print(f"Can't start after {startafter} users, you only have {len(leaders)}, starting at 0", file=sys.stderr)
        startafter = 0

    leaders = dict(sorted(leaders.items(), key=lambda i: i[1][1], reverse=True))

    names = [user for user in leaders.keys()]
    print(f"Showing data for user(s): {', '.join(names)}")

    if args.hmms or args.words != None:
        print("AHHHHHHHHH")
        leaders = dict(sorted(leaders.items(), key=lambda i: hack(i), reverse=True))
    for name, data in leaders.items():
        if args.hmms or args.words != None:
#            if len(leaders.items()) > 1:
#                err("Can't show hmms for more than one user, please make your constraints more specific,\nRun with --list to see all users")
            for name, values in sorted(data[2], key=lambda i: i[1][-1], reverse=True):
                plt.plot(data[0], values, "-", label=name)
        else:
            plt.plot(data[0], data[1], "-", label=name)

    # Graph stuff
    print("Plotting")
    plt.legend(loc="upper left")
    #plt.title("Top %d most messaged users over time" % args.numlines)
    plt.ylabel("Messages")
    plt.xlabel("Date")
    plt.xticks(rotation=75)
    plt.tight_layout()
    plt.show()

def hack(i):
    try:
        x = i[1][2][-1][-1][-1]
        return x
    except Exception:
        return 0

def uint(value):
    ivalue = int(value)
    if ivalue <= 0:
        raise argparse.ArgumentTypeError("%s is an invalid, need a positive int value" % value)
    return ivalue

def any_in(lst1, lst2):
    for i in lst1:
        if i in lst2:
            return True
    return False

parser = argparse.ArgumentParser(description="Graph discord messages over time")
# REQUIRED ARGS
parser.add_argument("path", metavar="FILE", type=str, nargs=None, help="The path to your data package in its .zip format")
# SELECTION ARGS
parser.add_argument("-n", dest="numlines", metavar="numlines", type=uint, default=10, help="The top n users to graph")
parser.add_argument("-s", "--skip", dest="startafter", metavar="startafter", type=uint, default=0, help="Skip the first n top users")
parser.add_argument("-u", "--user" , dest="users", metavar="users", type=str, nargs="+", default=None, help="Show only the given users data. Works with --hmms and ---words")
# PROGRAMS
parser.add_argument("-l", "--list" , dest="list", action="store_true", help="List all DMs to stdout and exit")
parser.add_argument("--hmms" , dest="hmms", action="store_true", help="Show statistics for words like hmm, huh, or lol")
parser.add_argument("-w", "--words", nargs="+", dest="words", default=None, help="Show statistics for words of your choice. Supports regex (buggy)")

args = parser.parse_args()
if args.hmms and args.words:
    raise argparse.ArgumentTypeError("-w/--words cannot be used with --hmms")

path = args.path
if not os.path.isfile(path):
    err("No file %s" % path)

zf = zipfile.ZipFile(path, "r")

with tempfile.TemporaryDirectory() as tf:
    print("Extracting")
    zf.extractall(tf)
    # ensure we have ma messages dir
    if "messages" not in os.listdir(tf):
        err("No \"messages\" directory found in file")
    read(tf, args)


import torch

device = 'cuda' if torch.cuda.is_available() else 'cpu'
LR = 1e-2
WORD_PAD = '<PAD>'
WORD_PAD_ID = 0
WORD_UNK = '<UNK>'
WORD_UNK_ID = 1
LABEL_O_ID = 0
LABEL_OTH_ID = 26

VOCAB_SIZE = 3000
VOCAB_PATH = './output/vocab.txt'

DEV_PATH = './input/data/ResumeNER/dev.char.bmes'
TRAIN_PATH = './input/data/ResumeNER/train.char.bmes'
TEST_PATH = './input/data/ResumeNER/test.char.bmes'
LABEL_PATH = './output/label.txt'
# 词向量 100 维
EMBEDDING_DIM = 100
# LSTM 隐藏层输出维度
HIDDEN_SIZE = 128
MODEL_DIR = './output/model/'
MODEL_CNN_DIR = './output/model/cnn_lstm_crf_model/'
MODEL_LSTM_DIR = './output/model/lstm_crf_model/'
MODEL_2CNN_LSTM_DIR = './output/model/cnn2_lstm_crf_model/'


"""
This module contains various utility functions that are exposed to API users
"""
from __future__ import print_function, absolute_import, division
from future.builtins import *
from future import standard_library
standard_library.install_aliases()

# Copyright 2017 Autodesk Inc.
#
# 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 moldesign as mdt
from moldesign import units as u

from . import toplevel, __all__ as _pkgall

from moldesign.interfaces.openbabel import add_hydrogen, guess_bond_orders
from moldesign.interfaces.pdbfixer_interface import mutate_residues, add_water
from moldesign.interfaces.tleap_interface import create_ff_parameters
from moldesign.interfaces.ambertools import calc_am1_bcc_charges, calc_gasteiger_charges

_pkgall.extend(('add_hydrogen guess_bond_orders mutate_residues add_water'
                ' create_ff_parameters calc_am1_bcc_charges calc_gasteiger_charges ').split())

ATNUM_VALENCE_CHARGE = {6: {3: -1, 4: 0},
                        7: {2: -1, 3: 0, 4: 1},
                        8: {1: -1, 2: 0, 3: 1},
                        9: {1: 0, 2: 1}}

ION_CHARGE = {1: 1,  # H
              11: 1,  # Na+
              19: 1,  # K+
              12: 2,  # Mg 2+
              20: 2,  # Ca 2+
              9: -1,  # F-
              17: -1,  # Cl-
              35: -1,  # Br-
              53: -1}  # I-

@toplevel
def assign_formal_charges(mol, ignore_nonzero=True):
    """ Assign formal charges to C,N,O,F atoms in this molecule based on valence

    Args:
        mol (moldesign.Molecule): Molecule to assign formal charges to. The formal charges of its
           atoms and its total charge will be adjusted in place.
        ignore_nonzero (bool): If formal charge is already set to a nonzero value, ignore this atom

    Note:
        This method ONLY applies to C,N, O and F, based on a simple valence model.
        These results should be manually inspected for consistency.

    Raises:
        UnhandledValenceError: for cases not handled by the simple valence model

    References:
        These assignments are illustrated by the formal charge patterns in
            http://www.chem.ucla.edu/~harding/tutorials/formalcharge.pdf
    """
    from moldesign.exceptions import UnhandledValenceError

    # TODO: scrape formal charge data from the PDB chem comp dictionary

    # cache these values in case we fail to assign charges
    totalcharge = mol.charge
    oldcharges = [atom.formal_charge for atom in mol.atoms]

    for atom in mol.atoms:
        if ignore_nonzero and atom.formal_charge != 0:
            continue

        v = atom.valence
        newcharge = None

        if atom.atnum in ATNUM_VALENCE_CHARGE:
            if v in ATNUM_VALENCE_CHARGE[atom.atnum]:
                newcharge = ATNUM_VALENCE_CHARGE[atom.atnum][v]
            else:
                for oldcharge, a in zip(oldcharges, mol.atoms):
                    a.oldcharge = oldcharge
                mol.charge = totalcharge
                raise UnhandledValenceError(atom)
        elif atom in ION_CHARGE and v == 0:
            newcharge = ION_CHARGE[atom.atnum]

        if newcharge is not None:
            mol.charge += newcharge * u.q_e - atom.formal_charge
            atom.formal_charge = newcharge * u.q_e

@toplevel
def set_hybridization_and_saturate(mol):
    """ Assign bond orders, saturate with hydrogens, and assign formal charges

    Specifically, this is a convenience function that runs:
    ``mdt.guess_bond_orders``, ``mdt.add_hydrogen``, and ``mdt.assign_formal_charges``

    Note:
        This does NOT add missing residues to biochemical structures. This functionality will be
        available as :meth:`moldesign.add_missing_residues`

    Args:
        mol (moldesign.Molecule): molecule to clean

    Returns:
        moldesign.Molecule: cleaned version of the molecule
    """
    m1 = mdt.guess_bond_orders(mol)
    m2 = mdt.add_hydrogen(m1)
    assign_formal_charges(m2)
    return m2

@toplevel
def guess_histidine_states(mol):
    """ Attempt to assign protonation states to histidine residues.

    Note:
        This function is highly unlikely to give accurate results! It is intended for convenience
        when histidine states can easily be guessed from already-present hydrogens or when they are
        judged to be relatively unimportant.

    This can be done simply by renaming HIS residues:
      1. If HE2 and HD1 are present, the residue is renamed to HIP
      2. If only HE2 is present, the residue is renamed to HIE
      3. Otherwise, the residue is renamed to HID (the most common form)

    Args:
        mol (moldesign.Molecule): molecule to change (in place)
    """
    for residue in mol.residues:
        if residue.resname == 'HIS':
            oldname = str(residue)
            if 'HE2' in residue and 'HD1' in residue:
                residue.resname = 'HIP'
            elif 'HE2' in residue:
                residue.resname = 'HIE'
            else:
                residue.resname = 'HID'
            print('Renaming %s from HIS to %s' % (oldname, residue.resname))

@toplevel
def split_chains(mol, distance_threshold=1.75*u.angstrom):
    """ Split a molecule's chains into unbroken biopolymers and groups of non-polymers

    This function is non-destructive - the passed molecule will not be modified.

    Specifically, this function will:
       - Split any chain with non-contiguous biopolymeric pieces into single, contiguous polymers
       - Remove any solvent molecules from a chain into their own chain
       - Isolate ligands from each chain into their own chains

    Args:
        mol (mdt.Molecule): Input molecule
        distance_threshold (u.Scalar[length]): if not ``None``, the maximum distance between
           adjacent residues for which we consider them "contiguous". For PDB data, values greater
           than 1.4 Angstrom are eminently reasonable; the default threshold of 1.75 Angstrom is
           purposefully set to be extremely cautious (and still much lower than the distance to
           the *next* nearest neighbor, generally around 2.5 Angstrom)

    Returns:
        mdt.Molecule: molecule with separated chains
    """

    tempmol = mol.copy()

    def bonded(r1, r2):
        if r2 not in r1.bonded_residues:
            return False
        if distance_threshold is not None and r1.distance(r2) > distance_threshold:
            return False
        return True

    def addto(chain, res):
        res.chain = None
        chain.add(res)

    allchains = [mdt.Chain(tempmol.chains[0].name)]
    for chain in tempmol.chains:
        chaintype = chain.residues[0].type
        solventchain = mdt.Chain(None)
        ligandchain = mdt.Chain(None)

        for ires, residue in enumerate(chain.residues):
            if residue.type == 'unknown':
                thischain = ligandchain
            elif residue.type in ('water', 'solvent', 'ion'):
                thischain = solventchain
            else:
                assert residue.type == chaintype
                if ires != 0 and not bonded(residue.prev_residue, residue):
                    allchains.append(mdt.Chain(None))
                thischain = allchains[-1]

            addto(thischain, residue)

        for c in (solventchain, ligandchain):
            if c.num_atoms > 0:
                allchains.append(c)

    return mdt.Molecule(allchains)


import sqlite3

connection = sqlite3.connect('gfg.db')
cursor = connection.cursor()

#SQL command to create a table in the database
command = """
CREATE TABLE emp(
    staff_number INTEGER PRIMARY KEY,
    fname VARCHAR(20),
    lname VARCHAR(30),
    gender CHAR(1),
    joining DATE
);
"""
cursor.execute(command)

command = """
INSERT INTO emp
VALUES (23, "Rishabh", "Bansal", "M", "2014-03-28");
"""
cursor.execute(command)

command = """
INSERT INTO emp
VALUES (1, "Bill", "Gates", "M", "1980-10-28");
"""
cursor.execute(command)

#geting results
cursor.execute("SELECT * FROM emp")
result = cursor.fetchall()
print(result)

connection.commit()
connection.close()


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="chartree",
    version="0.1.0",
    author="bsmrvl",
    author_email="ben.j.somerville@icloud.com",
    description="Grow trees",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/bsmrvl/a-trees-character",
    packages=setuptools.find_packages(),
    install_requires=['numpy','IPython'],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)


import asyncio
from copy import deepcopy
import time
import heapq

async def gather_transaction_batches(queue: dict, expected_batches: int, timeout=5):
    # Wait until the queue is filled before starting timeout
    while len(set(queue.keys())) == 0:
        await asyncio.sleep(0)

    # Now wait until the rest come in or the timeout is triggered
    start = time.time()
    while len(set(queue.keys())) < expected_batches and time.time() - start < timeout:
        await asyncio.sleep(0)

    work = deepcopy(list(queue.values()))
    queue.clear()

    return work

def pad_work(work: list, expected_masters: list):
    for task in work:
        if task['sender'] in expected_masters:
            expected_masters.remove(task['sender'])

    for missing_master in expected_masters:
        shim = {
            'transactions': [],
            'timestamp': int(time.time()),
            'signature': '0' * 128,
            'input_hash': missing_master,
            'sender': missing_master
        }
        work.append(shim)

def filter_work(work):
    actual_work = [w for w in work if w is not None]
    return sorted(actual_work, key=lambda x: x['timestamp'])
    # filtered_work = []
    # print(work)
    # for tx_batch in work:
    #     # Filter out None responses
    #     if tx_batch is None:
    #         continue
    #
    #     # Add the rest to a priority queue based on their timestamp
    #     print(filtered_work)
    #     print(tx_batch)
    #     heapq.heappush(filtered_work, (tx_batch['timestamp'], tx_batch))
    #
    # # Actually sorts the heap. This can be rewritten to use heap push pop.
    # w = []
    # for i in range(len(filtered_work)):
    #     w.append(heapq.heappop(filtered_work))
    #
    # return w


import cv2
import numpy as np
import threading
import time

ROW=1080
COL=1920
hCOL=COL/2

gamma=5.0
w=640
h=512
l=h*gamma

boundary=[[0,w,ROW-h,ROW],[COL-w,COL,ROW-h,ROW]]

R=np.sqrt((l+h)**2+(0.5*w)**2)
Rmin=np.sqrt(l**2+(0.5*w)**2)
dr=(R-Rmin)/float(h)
w_o=int(0.5*w*R/np.sqrt((0.5*w)**2+l**2))
h_o=int(R-l)
amax=np.arctan((0.5*w)/float(l))
da=amax/float(0.5*w)

coeff=[R,Rmin,dr,w_o,h_o,amax,da,gamma]

print("R=",R," Rmin=",Rmin," dr=",dr," amax=",amax," da=",da)
map=np.zeros((h,w,2))
map_x,map_y=np.full((h,w),-1,dtype=np.float32),np.full((h,w),-1,dtype=np.float32)
output = np.zeros((h,w,3), dtype=np.uint8)
dual_out = np.zeros((ROW,COL,3), dtype=np.uint8)
offset=[[300,-80],[-300,-80]]

H0=np.transpose(np.array([[0.6481,0.1404,0.0],[-0.0334,0.6719,0.0],[120.5052,11.6071,1.0000]]))
H1=np.transpose(np.array([[0.9478,0.0738,0.0000],[0.0121,0.8910,0.0],[15.9848,79.9388,1.0000]]))
H2=np.transpose(np.array([[0.6796,-0.0258,0.0000],[0.0137,0.7180,0.0],[135.4070,86.5596,1.0000]]))
I=np.array([[1.0,0.0,0.0],[0.0,1.,0.],[0.,0.,1.]])
def recalc():
    l=h*coeff[7]
    coeff[0]=np.sqrt((l+h)**2+(0.5*w)**2)
    coeff[1]=np.sqrt(l**2+(0.5*w)**2)
    coeff[2]=(coeff[0]-coeff[1])/float(h)
    coeff[3]=int(0.5*w*coeff[0]/np.sqrt((0.5*w)**2+l**2))
    coeff[4]=int(coeff[0]-l)
    coeff[5]=np.arctan((0.5*w)/float(l))
    coeff[6]=coeff[5]/float(0.5*w)

def st_mesh():
    l=h*coeff[7]
    for px in range(w):
        for py in range(h):
            # map_x[py,px]=-1
            # map_y[py,px]=-1
            r=coeff[0]-coeff[2]*py
            theta=np.pi*0.5+coeff[5]-coeff[6]*px
            x=(r*np.cos(theta)+coeff[3])*w/(2.0*coeff[3])
            y=(coeff[4]+l-r*np.sin(theta))*h/coeff[4]
            map_x[int(y),int(x)]=px
            map_y[int(y),int(x)]=py

# def gl_mesh2():
#     dx,dy=1.0/w,1.0/h
#     r_2min=Rmin**2
#     r_2max=R**2
#     theta_min=np.pi/2.0-amax
#     theta_max=np.pi/2.0+amax
#     for i in range(w):
#         for j in range(h):
#             ax,ay=dx*i,dy*j
#             px,py=ax*w_o*2-w_o,h_o+l-ay*h_o
#             r_2=px**2+py**2
#             theta=np.arctan2(py,px+0.00001)
#             if(r_2>r_2min and r_2<r_2max and theta>theta_min and theta<theta_max):
#                 map[j,i,:]=(int((np.pi/2.0+amax-theta)/da),int((R-np.sqrt(r_2))/dr))
#                 # map_x[j,i],map_y[j,i]=(np.pi/2.0+amax-theta)/da,(R-np.sqrt(r_2))/dr
#             else:
#                 map[j,i,:]=(999,999)

# def remap(input,out,map):
#     for i in range(w):
#         for j in range(h):
#             if(map[j,i,0]>w-1 or map[j,i,1]>h-1):
#                 out[j,i,:]=(0,0,0)
#             else:
#                 out[j,i,:]=input[int(map[j,i,1]),int(map[j,i,0]),:]

def concat(inmat,outmat,offset):
    if(offset[0][0]>0 and offset[0][0]<hCOL-w):
        boundary[0][0],boundary[0][1]=offset[0][0],w+offset[0][0]
    if(offset[0][1]<0 and offset[0][1]>h-ROW):
        boundary[0][2],boundary[0][3]=ROW-h+offset[0][1],ROW+offset[0][1]
    if(offset[1][0]<0 and offset[1][0]>w-hCOL):
        boundary[1][0],boundary[1][1]=COL-w+offset[1][0],COL+offset[1][0]
    if(offset[1][1]<0 and offset[1][1]>h-ROW):
        boundary[1][2],boundary[1][3]=ROW-h+offset[1][1],ROW+offset[1][1]

    outmat[boundary[0][2]:boundary[0][3],boundary[0][0]:boundary[0][1],:]=inmat
    outmat[boundary[1][2]:boundary[1][3],boundary[1][0]:boundary[1][1],:]=inmat
    # outmat[567:1079,0:640,:]=inmat

# cam=cv2.VideoCapture(0)
cam=cv2.VideoCapture('C:/Users/autor/Desktop/matlab to opencv_cuda/Project1/Project1/My_Capture.avi')
# gl_mesh2()
st_mesh()

# cv2.namedWindow("overlay", cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty("overlay",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

H=I
scale=1.0
while True:
    _,cam_img=cam.read()
    x,y=int(w/scale),int(h/scale)
    lx,ly=int((w-x)/2),int((h-y)/2)
    # print(lx,ly,x,y)
    scaled=cv2.resize(cam_img[ly:ly+y,lx:lx+x,:],(640,512))
    homo=cv2.warpPerspective(scaled,H,(640, 512))
    fliped=cv2.flip(homo,1)
    output=cv2.remap(fliped,map_x,map_y,interpolation=cv2.INTER_LINEAR)
    # remap(cam_img,output,map)
    concat(output,dual_out,offset)

    cv2.imshow('overlay',dual_out)

    keypress=cv2.waitKey(1) & 0xFF

    if(keypress-255!=0):
        if keypress==27:
            break
        elif keypress==ord(' '):
            # cv2.imwrite('capture.png',cam_img[16:496,:,:])
            cv2.imwrite('capture.png',cam_img)
        elif keypress==ord('w'):
            offset[0][1]-=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")
        elif keypress==ord('a'):
            offset[0][0]-=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")
        elif keypress==ord('s'):
            offset[0][1]+=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")
        elif keypress==ord('d'):
            offset[0][0]+=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")

        elif keypress==ord('i'):
            offset[1][1]-=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")
        elif keypress==ord('j'):
            offset[1][0]-=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")
        elif keypress==ord('k'):
            offset[1][1]+=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")
        elif keypress==ord('l'):
            offset[1][0]+=10
            dual_out=np.zeros((ROW,COL,3), dtype=np.uint8)
            print("offset=(", offset[0][0],",", offset[0][1], offset[1][0],",", offset[1][1],")")
        elif keypress==ord('9'):
            coeff[7]+=0.3
            map_x,map_y=np.full((h,w),-1,dtype=np.float32),np.full((h,w),-1,dtype=np.float32)
            recalc()
            st_mesh()
            print("coeff=",coeff[7])
        elif keypress==ord('0'):
            coeff[7]-=0.3
            map_x,map_y=np.full((h,w),-1,dtype=np.float32),np.full((h,w),-1,dtype=np.float32)
            recalc()
            st_mesh()
            print("coeff=",coeff[7])
        elif keypress==ord('1'):
            H=H0
        elif keypress==ord('2'):
            H=H1
        elif keypress==ord('3'):
            H=H2
        elif keypress==ord('4'):
            H=I
        elif keypress==ord('z'):
            scale+=0.02
            print("scale=",scale)
        elif keypress==ord('x'):
            scale-=0.02
            print("scale=",scale)
        else:
            continue


#!/usr/bin/env python
import sys
sys.path.append('Sublist3r')
import argparse, requests
import sublist3r
from colorama import Fore
from art import *

status_accepted = [200, 403, 402, 404, 503, 505]
basic_tests =   [
				".htaccess",
				".htaccess~",
				"htaccess",
				"htaccess~",
				"htaccess.txt",
				"robots.txt",
				"readme.txt",
				"config/install.php",
				"config/readme.txt"
				]

def fucking_false_positive(response):
	if response.status_code == 200:
		if response.content == None:
			return True
		if (b"404" or b"Not found" or b"not found") in response.content:
			return False
	return False

def print_header():
		ascii_header = text2art("LEECHER", "rand")
		print(ascii_header)
		print("\t\t\tAutomated Security Audit\n")

def make_request(domain, redirect):
	try:
		response = requests.get("https://"+domain, allow_redirects=redirect, verify=False, timeout=5)
		if not response:
			response = requests.get("http://"+domain, allow_redirects=redirect, verify=False, timeout=5)
		return response
	except:
		return None

class   Leecher:
	def __init__(self):
		print_header()
		parser = argparse.ArgumentParser()
		parser.add_argument("-t", "--target", help="Set the target")
		parser.add_argument("--full-check", help="Make a recursive check for all subdomains for all CMS", action="store_true")
		parser.add_argument("-s", "--silent", help="Disable modules output", default=False, action="store_true")
		parser.add_argument("-v", "--verbose", help="Verbose mod", default=False, action="store_true")
		self.args = parser.parse_args()
		if not self.args.target:
			parser.error("Target needed")

	def launch_scan(self):
		subdomains = self.sublister_wrapper(self.args.target)
		print("\nLet's h4ck\n")
		self.process_scan(subdomains)

	def sublister_wrapper(self, domain):
		print("Launching Sublist3r...")
		try:
			subdomains = sublist3r.main(
					domain,
					40,
					None,
					ports=None,
					silent=self.args.silent, verbose=self.args.verbose, enable_bruteforce=False, engines=None
					)
			subdomains.insert(0, domain)
			if self.args.verbose:
				print("Subdomains founds :")
				print(subdomains)
			return subdomains
		except:
			sys.exit("Error while processing Sublist3r");

	def process_scan(self, subdomains):
		for domain in subdomains:
			response = make_request(domain, True)
			if response:
				if response.status_code in status_accepted:
					print("[" + Fore.YELLOW + "+" + Fore.RESET + "] [%s] is responding" % domain)
					self.process_basic_tests(domain)
				else:
					if self.args.verbose:
						print("[" + Fore.RED + "-" + Fore.RESET + "] [%s] is not responding" % domain)

	def process_basic_tests(self, domain):
		for test in basic_tests:
			response = make_request(domain+'/'+test, False)
			if response is not None:
				if response.status_code == 200 and not fucking_false_positive(response):
					print("[" + Fore.GREEN + "✓" + Fore.RESET + "]\t[%s/%s] succeed" % (domain, test))
				else:
					if self.args.verbose:
						print("[" + Fore.RED + "X" + Fore.RESET + "]\t[%s/%s] failed" % (domain, test))

	def wpscan_wrapper(self):
		print("WPScan processing...")
		#use wpscan and save result with sublist for the current url
		return

def	leecher_launch():
	instance = Leecher()
	instance.launch_scan()

if __name__ == '__main__':
	leecher_launch()


from quart import Blueprint, jsonify
from typing import TYPE_CHECKING

from litecord.auth import admin_check
from litecord.schemas import validate, USER_UPDATE
from litecord.admin_schemas import USER_CREATE
from litecord.errors import BadRequest, NotFound
from litecord.utils import extract_limit
from litecord.blueprints.users import handle_user_update
from litecord.common.users import (
    create_user,
    delete_user,
    user_disconnect,
)

if TYPE_CHECKING:
    from litecord.typing_hax import app, request
else:
    from quart import current_app as app, request

bp = Blueprint("users_admin", __name__)

@bp.route("", methods=["POST"], strict_slashes=False)
async def _create_user():
    await admin_check()
    j = validate(await request.get_json(), USER_CREATE)
    user_id, _ = await create_user(
        j["username"], j["email"], j["password"], j.get("date_of_birth"), id=j.get("id")
    )
    return jsonify(await app.storage.get_user(user_id, True)), 201

def args_try(args: dict, typ, field: str, default):
    """Try to fetch a value from the request arguments,
    given a type."""
    try:
        return typ(args.get(field, default))
    except (TypeError, ValueError):
        raise BadRequest(message=f"invalid {field} value")

@bp.route("", methods=["GET"], strict_slashes=False)
async def query_users():
    await admin_check()

    limit = extract_limit(request, 1, 25, 100)
    j = validate(
        request.args.to_dict(),
        {
            "q": {"coerce": str, "required": False, "maxlength": 32},
            "offset": {"coerce": int, "default": 0},
        },
    )
    query = j.get("q") or ""
    offset = j["offset"]
    extra = ""
    args = (query, limit, offset)

    discriminator = None
    if "#" in query:
        query, _, discriminator = query.rpartition("#")
        try:
            discriminator = "%04d" % int(discriminator)
        except ValueError:
            discriminator = None

    if discriminator:
        extra = "AND discriminator = $4"
        args = (query, limit, offset, discriminator)

    result = await app.storage.get_users(
        secure=True,
        extra_clause=", COUNT(*) OVER() as total_results",
        where_clause=f"""
        WHERE username ILIKE '%'||$1||'%' {extra}
        ORDER BY username
        LIMIT $2 OFFSET $3
        """,
        args=args,
    )

    total_results = result[0]["total_results"] if result else 0
    for user in result:
        user.pop("total_results")
    return jsonify({"users": result, "total_results": total_results})

@bp.route("/@me", methods=["GET"])
async def get_me():
    user_id = await admin_check()
    return jsonify(await app.storage.get_user(user_id, True))

@bp.route("/<int:target_id>", methods=["GET"])
async def get_other(target_id):
    await admin_check()
    other = await app.storage.get_user(target_id, True)
    if not other:
        raise NotFound(10013)
    return jsonify(other)

@bp.route("/<int:user_id>", methods=["DELETE"])
@bp.route("/<int:user_id>/delete", methods=["POST"])
async def _delete_user(user_id: int):
    await admin_check()

    await delete_user(user_id)
    await user_disconnect(user_id)

    new_user = await app.storage.get_user(user_id, True)
    return jsonify(new_user)

@bp.route("/<int:user_id>", methods=["PATCH"])
async def patch_user(user_id: int):
    await admin_check()

    j = validate(await request.get_json(), USER_UPDATE)

    if "flags" in j and j["flags"] is not None:
        await app.db.execute(
            """
        UPDATE users
        SET flags = $1
        WHERE id = $2
        """,
            j["flags"],
            user_id,
        )
        j.pop("flags")

    private_user = await handle_user_update(user_id, False)
    return jsonify(private_user)

@bp.route("/<int:user_id>/channels", methods=["GET"])
async def user_dms(user_id: int):
    await admin_check()
    return jsonify(await app.user_storage.get_dms(user_id))

@bp.route("/<int:user_id>/relationships", methods=["GET"])
async def user_relationships(user_id: int):
    await admin_check()
    return jsonify(await app.user_storage.get_relationships(user_id))


import logging
import random
from argparse import ArgumentParser
from itertools import chain
from pprint import pformat
import warnings
import tensorflow as tf

import torch
import torch.nn.functional as F

from transformers import OpenAIGPTLMHeadModel, OpenAIGPTTokenizer, GPT2LMHeadModel, GPT2Tokenizer
from train import SPECIAL_TOKENS, build_input_from_segments, add_special_tokens_
from utils import get_dataset, download_pretrained_model

def top_filtering(logits, top_k=0.0, top_p=0.9, threshold=-float("Inf"), filter_value=-float("Inf"), beam=True):
    """Filter a distribution of logits using top-k, top-p (nucleus) and/or threshold filtering
    Args:
        logits: logits distribution shape (vocabulary size)
        top_k: <=0: no filtering, >0: keep only top k tokens with highest probability.
        top_p: <=0.0: no filtering, >0.0: keep only a subset S of candidates, where S is the smallest subset
            whose total probability mass is greater than or equal to the threshold top_p.
            In practice, we select the highest probability tokens whose cumulative probability mass exceeds
            the threshold top_p.
        threshold: a minimal threshold to keep logits
    """
    assert logits.dim() == 1

    if beam == True:
        sorted_logits, sorted_indices = torch.sort(logits, descending=True)
        indices_to_remove = sorted_indices[5:]
        logits[indices_to_remove] = filter_value

    else:
        if top_k > 0:
            # Only work for batch size 1 for now - could update but it would obfuscate a bit the code
            top_k = min(top_k, logits.size(-1))
            # Remove all tokens with a probability less than the last token in the top-k tokens
            indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
            logits[indices_to_remove] = filter_value

        if top_p > 0.0:
            # Compute cumulative probabilities of sorted tokens
            sorted_logits, sorted_indices = torch.sort(logits, descending=True)
            cumulative_probabilities = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)

            # Remove tokens with cumulative probability above the threshold
            sorted_indices_to_remove = cumulative_probabilities > top_p
            # Shift the indices to the right to keep also the first token above the threshold
            sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
            sorted_indices_to_remove[..., 0] = 0

            # Back to unsorted indices and set them to -infinity
            indices_to_remove = sorted_indices[sorted_indices_to_remove]
            logits[indices_to_remove] = filter_value

    indices_to_remove = logits < threshold
    logits[indices_to_remove] = filter_value

    return logits

def sample_sequence(personality, history, tokenizer, model, args, current_output=None):
    special_tokens_ids = tokenizer.convert_tokens_to_ids(SPECIAL_TOKENS)
    if current_output is None:
        current_output = []

    for i in range(args.max_length):
        instance = build_input_from_segments(personality, history, current_output, tokenizer, with_eos=False)

        input_ids = torch.tensor(instance["input_ids"], device=args.device).unsqueeze(0)

        outputs = model.generate(
            input_ids,
            max_length=input_ids.shape[-1] + 40,
            num_beams=10,
            num_return_sequences=1,
            no_repeat_ngram_size=1,
            remove_invalid_values=True,
        )
        return outputs[0].tolist()
        # print(tokenizer.decode(outputs[0], skip_special_tokens=True))

        # token_type_ids = torch.tensor(instance["token_type_ids"], device=args.device).unsqueeze(0)

        # logits = model(input_ids, token_type_ids=token_type_ids)

        # beam_output = model.generate(input_ids, max_length=40, num_beams=5)

        # return beam_output[0].tolist()

    #     if isinstance(logits, tuple):  # for gpt2 and maybe others
    #         logits = logits[0]

    #     logits = logits[0, -1, :] / args.temperature

    #     logits = top_filtering(logits, top_k=args.top_k, top_p=args.top_p)

    #     probs = F.softmax(logits, dim=-1)

    #     prev = torch.topk(probs, 1)[1] if args.no_sample else torch.multinomial(probs, 1)
    #     if i < args.min_length and prev.item() in special_tokens_ids:
    #         while prev.item() in special_tokens_ids:
    #             if probs.max().item() == 1:
    #                 warnings.warn("Warning: model generating special token with probability 1.")
    #                 break  # avoid infinitely looping over special token
    #             prev = torch.multinomial(probs, num_samples=1)

    #     if prev.item() in special_tokens_ids:
    #         break
    #     current_output.append(prev.item())

    # return current_output

## ADDED CODE
def get_data():
    datapath = (
        "/raid/cs18resch11003/aditi/transfer-learning-conv-ai/ParlAI/data/ConvAI2/test_self_original_no_cands.txt"
    )
    data = open(datapath, "r")
    lines = data.readlines()

    personality = []
    persona = []
    for idx, line in enumerate(lines):
        if "your persona:" in line:
            line = line.strip()
            line = line.replace("your persona:", "")
            line = line[3:]
            persona.append(line)
        else:
            if len(persona) != 0:
                personality.append(persona)
                persona = []

    conversations = []
    conversation = []
    for idx, line in enumerate(lines):
        if "your persona:" not in line:
            line = line.strip()
            line = line[2:]
            conversation.append(line)
        else:
            if len(conversation) != 0:
                conversations.append(conversation)
                conversation = []
    conversations.append(conversation)

    prompts = []
    replies = []

    for conversation in conversations:
        temp_prompt = []
        temp_replies = []
        for turn in conversation:
            utterance1, utterance2 = turn.split("\t")
            utterance1 = utterance1.strip()
            utterance2 = utterance2.strip()

            temp_prompt.append(utterance1)
            temp_replies.append(utterance2)
        prompts.append(temp_prompt)
        replies.append(temp_replies)

    assert len(personality) == (len(prompts) + len(replies)) / 2

    return personality, prompts, replies

def run():
    parser = ArgumentParser()
    parser.add_argument(
        "--dataset_path", type=str, default="", help="Path or url of the dataset. If empty download from S3."
    )
    parser.add_argument(
        "--dataset_cache", type=str, default="./dataset_cache", help="Path or url of the dataset cache"
    )
    parser.add_argument(
        "--model",
        type=str,
        default="openai-gpt",
        help="Model type (openai-gpt or gpt2)",
        choices=["openai-gpt", "gpt2"],
    )  # anything besides gpt2 will load openai-gpt
    parser.add_argument("--model_checkpoint", type=str, default="", help="Path, url or short name of the model")
    parser.add_argument(
        "--max_history", type=int, default=2, help="Number of previous utterances to keep in history"
    )
    parser.add_argument(
        "--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu", help="Device (cuda or cpu)"
    )

    parser.add_argument("--no_sample", action="store_true", help="Set to use greedy decoding instead of sampling")
    parser.add_argument("--max_length", type=int, default=20, help="Maximum length of the output utterances")
    parser.add_argument("--min_length", type=int, default=1, help="Minimum length of the output utterances")
    parser.add_argument("--seed", type=int, default=0, help="Seed")
    parser.add_argument("--temperature", type=float, default=0.7, help="Sampling softmax temperature")
    parser.add_argument(
        "--top_k", type=int, default=0, help="Filter top-k tokens before sampling (<=0: no filtering)"
    )
    parser.add_argument(
        "--top_p", type=float, default=0.9, help="Nucleus filtering (top-p) before sampling (<=0.0: no filtering)"
    )
    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__file__)
    logger.info(pformat(args))

    if args.model_checkpoint == "":
        if args.model == "gpt2":
            raise ValueError("Interacting with GPT2 requires passing a finetuned model_checkpoint")
        else:
            args.model_checkpoint = download_pretrained_model()

    if args.seed != 0:
        random.seed(args.seed)
        torch.random.manual_seed(args.seed)
        torch.cuda.manual_seed(args.seed)

    logger.info("Get pretrained model and tokenizer")
    tokenizer_class, model_class = (
        (GPT2Tokenizer, GPT2LMHeadModel) if args.model == "gpt2" else (OpenAIGPTTokenizer, OpenAIGPTLMHeadModel)
    )
    tokenizer = tokenizer_class.from_pretrained(args.model_checkpoint)
    model = model_class.from_pretrained(args.model_checkpoint)
    model.to(args.device)
    add_special_tokens_(model, tokenizer)

    ####################

    # logger.info("Sample a personality")

    # dataset = get_dataset(tokenizer, args.dataset_path, args.dataset_cache)

    # personalities = [dialog["personality"] for dataset in dataset.values() for dialog in dataset]

    # personality = random.choice(personalities)
    # logger.info("Selected personality: %s", tokenizer.decode(chain(*personality)))

    ## ADDED CODE

    personality, prompts, replies = get_data()

    tokens_personality = []

    for persona in personality:
        temp_p = []
        for p in persona:
            temp_p.append(tokenizer.convert_tokens_to_ids(tokenizer.tokenize(p)))
        tokens_personality.append(temp_p)

    history = []
    count = 0
    for idx, (tp, p, r) in enumerate(zip(tokens_personality, prompts, replies)):
        print(f"Personality: {personality[idx]}")
        for _p, _r in zip(p, r):
            print(f"Prompt: {_p}")
            print(f"Original: {_r}")

            history.append(tokenizer.encode(_p))
            with torch.no_grad():
                out_ids = sample_sequence(tp, history, tokenizer, model, args)
            history.append(out_ids)

            history = history[-(2 * args.max_history + 1) :]
            out_text = tokenizer.decode(out_ids, skip_special_tokens=True)
            print(f"Generated: {out_text}\n")

            count += 1

            if count == 50:
                exit(0)

    # while True:
    #     raw_text = input(">>> ")
    #     while not raw_text:
    #         print("Prompt should not be empty!")
    #         raw_text = input(">>> ")

    #     history.append(tokenizer.encode(raw_text))

    #     with torch.no_grad():
    #         out_ids = sample_sequence(personality, history, tokenizer, model, args)

    #     history.append(out_ids)

    #     history = history[-(2 * args.max_history + 1) :]
    #     out_text = tokenizer.decode(out_ids, skip_special_tokens=True)
    #     print(out_text)

if __name__ == "__main__":
    run()


from cgi import test
import os
import json

class Data:
    def __init__(self, trainPath, testPath, valPath):
        self.train = self.readFile(trainPath)
        self.val = self.readFile(valPath)
        self.test = self.readFile(testPath)

    def readFile(self, path):
        with open(os.path.join(os.getcwd(), path), 'r') as f:
            return json.loads(f.read())


#!/usr/bin/env python3

import os
import sys
import csv
import time
import json
import pprint
import socket
import argparse

HOSTNAME = "api.vndb.org"
PORT = 19534
BUFFER_SIZE = 2**12

class TCPSession:
    def __init__(self, hostname, port, sock=None):
        if sock is None:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock
        self.sock.connect((hostname, port))

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.sock.shutdown(socket.SHUT_RDWR)
        self.sock.close()

    def send(self, msg):
        totalsent = 0
        while totalsent < len(msg):
            sent = self.sock.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError('socket connection broken')
            totalsent = totalsent + sent

    def recv(self, delim=b'\x04', buf_size=BUFFER_SIZE):
        chunks = [b'']
        bytes_recd = 0
        while not chunks[-1].endswith(delim):
            chunk = self.sock.recv(buf_size)
            if chunk == b'':
                raise RuntimeError('socket connection broken')
            chunks.append(chunk)
        return b''.join(chunks).rstrip(delim)

    def query(self, cmd):
        self.send(cmd.encode())
        res = self.recv().decode()
        return res

    def login(self):
        cfg = {"protocol": 1, "client": "test", "clientver": 0.1}
        cmd = f'login {json.dumps(cfg).replace(" ", "")}\x04'
        return self.query(cmd)

    def dbstats(self):
        cmd = f'dbstats\x04'
        buf = self.query(cmd)
        res = json.loads(' '.join(buf.split(' ')[1:]))
        return res

    def get(self, typ, flags, filters, options):
        cmd = f'get {typ} {flags} {filters} {options}\x04'
        buf = self.query(cmd)
        res = json.loads(' '.join(buf.split(' ')[1:]))
        return res

    def set(self, typ, ids, fields):
        cmd = f'set {typ} {ids} {fields}\x04'
        return self.query(cmd)

def parse_opt():
    parser = argparse.ArgumentParser()

    parser.add_argument('typ',
                        help='the type of data to fetch')
    parser.add_argument('flags', nargs='?', default='basic',
                        help='what part of that data to fetch')
    parser.add_argument('--dir', '-d', default='data',
                        help='the target dir to output')
    parser.add_argument('--csv', '-c', action='store_true',
                        help='output the result as csv')

    return parser.parse_args()

def fetch_vndb(typ, flags):
    attr = {
        "vn": ('id', 'vn'),
        "release": ('id', 'releases'),
        "producer": ('id', 'producers'),
        "character": ('id', 'chars'),
        "staff": ('id', 'staff'),
        "user": ('id', 'users'),
        "votelist": ('vn', 'vn'),
        "vnlist": ('vn', 'vn'),
        "wishlist": ('vn', 'vn'),
    }
    attr_name, attr_src = attr[typ]

    items = list()
    with TCPSession(HOSTNAME, PORT) as s:
        print(s.login(), flush=True, file=sys.stderr)
        stats = s.dbstats()

        count = 0
        index = 1
        page = 1
        results = 25 if "list" not in typ else 100
        while count < stats[attr_src]:
            fltr = f'({attr_name} = {repr(list(range(index, index+results)))})'
            cfg = {"page": page, "results": results, "reverse": False}
            res = s.get(typ, flags, fltr, json.dumps(cfg).replace(" ", ""))

            if 'id' in res:
                print(pprint.pformat(res), flush=True, file=sys.stderr)
                if res['id'] == 'throttled':
                    time.sleep(res['fullwait'])
                    continue
                raise RuntimeError("Invalid query was posted to VNDB")

            print(pprint.pformat(res), flush=True)
            items.extend(res['items'])
            count += res['num']
            if res['more']:
                page += 1
            else:
                index += results
                page = 1

    return items

def main():
    args = parse_opt()
    if not os.path.exists(args.dir):
        os.makedirs(args.dir)

    if args.typ == "dbstats":
        with TCPSession(HOSTNAME, PORT) as s:
            print(s.login(), flush=True, file=sys.stderr)
            stats = s.dbstats()
            print(pprint.pformat(stats), flush=True)
        if args.csv:
            with open(f"{args.dir}/dbstats.csv", "w") as f:
                writer = csv.DictWriter(f, stats.keys())
                writer.writeheader()
                writer.writerow(stats)
        else:
            with open(f"{args.dir}/dbstats.json", "w") as f:
                json.dump(stats, f)
    else:
        items = fetch_vndb(args.typ, args.flags)
        if args.csv:
            with open(f"{args.dir}/{args.typ}_{args.flags}.csv", "w") as f:
                writer = csv.DictWriter(f, items[0].keys())
                writer.writeheader()
                for item in items:
                    writer.writerow(item)
        else:
            with open(f"{args.dir}/{args.typ}_{args.flags}.json", "w") as f:
                json.dump(items, f)

if __name__ == "__main__":
    main()


# -*- coding: UTF-8 -*-
import logging
import requests
import json
import datetime;
from werkzeug.datastructures import Headers

LOG = logging.getLogger(__name__)

def matchmaker_request(url, token, method, content_type=None, accept=None, data=None):
    """Send a request to MatchMaker and return its response

    Args:
        url(str): url to send request to
        token(str): MME server authorization token
        method(str): 'GET', 'POST' or 'DELETE'
        content_type(str): MME request Content-Type
        accept(str): accepted response
        data(dict): eventual data to send in request

    Returns:
        json_response(dict): server response
    """
    headers = Headers()
    headers = { 'X-Auth-Token': token}
    if content_type:
        headers['Content-Type'] = content_type
    if accept:
        headers['Accept'] = accept

    #sending data anyway so response will not be cached
    req_data = data or {'timestamp' : datetime.datetime.now().timestamp()}
    json_response = None
    try:
        LOG.info('Sending {} request to MME url {}. Data sent: {}'.format(
            method, url, req_data))
        resp = requests.request(
            method = method,
            url = url,
            headers = headers,
            data = json.dumps(req_data)
        )
        json_response = resp.json()
        LOG.info('MME server response was:{}'.format(json_response))

        if isinstance(json_response, str):
            json_response = {
                'message' : json_response,
            }
        elif isinstance(json_response, list): #asking for connected nodes
            return json_response
        json_response['status_code'] = resp.status_code
    except Exception as err:
        LOG.info('An error occurred while sending HTTP request to server ({})'.format(err))
        json_response = {
            'message' : str(err)
        }
    return json_response

def mme_nodes(mme_base_url, token):
    """Return the available MatchMaker nodes

    Args:
        mme_base_url(str): base URL of MME service
        token(str): MME server authorization token

    Returns:
        nodes(list): a list of node disctionaries
    """
    nodes = []
    if not mme_base_url or not token:
        return nodes
    url = ''.join([mme_base_url, '/nodes'])
    nodes = matchmaker_request(url=url, token=token, method='GET')
    LOG.info('Matchmaker has the following connected nodes:{}'.format(nodes))
    return nodes


# import datasets library
from datasets import load_dataset

# assign csv file to variable
data_files = "training.1600000.processed.noemoticon.csv"

# return csv file as DatasetDict
twitter_dataset = load_dataset("csv", data_files=data_files)

# remove unwanted columns from dataset
twitter_dataset = twitter_dataset.map(remove_columns=["1467810369", "Mon Apr 06 22:19:45 PDT 2009", "NO_QUERY", "_TheSpecialOne_"])

# rename target column "label"
twitter_dataset = twitter_dataset.rename_column("0", "label")

# rename text column, "Sequence"
twitter_dataset = twitter_dataset.rename_column("@switchfoot http://twitpic.com/2y1zl - Awww, that's a bummer.  You shoulda got David Carr of Third Day to do it. ;D", "Sequence")

# sequences with a positive sentiment have been assigned a value of 4
# this function changes the 4s to 1s.
def change_positive_to_1(example):
    if example["label"] == 4:
        example["label"] = 1
    return example

# remove all sequences that start with the @.
# these sequences start with user handles
# we do not want our model to learn this
twitter_dataset = twitter_dataset.filter(lambda x: x["Sequence"][0] != "@")

# apply the change_positive_to_1 function to all sequences in the dataset
twitter_dataset = twitter_dataset.map(change_positive_to_1)

print(twitter_dataset)

# change the dataset's format to pandas
twitter_dataset.set_format("pandas")

# print the number of sequences in the cleaned dataset
#print(len(twitter_dataset["train"]))

# print the first ten entries
print(twitter_dataset["train"][:10])

# the commented out code below splits the datatset into train and test split
# 20% of the dataset makes up the test split and 80% makes up the train split

#twitter_dataset = twitter_dataset["train"].train_test_split(train_size=0.8, seed=42)

# the commented out code below saves the splits as "twitter-train.csv" and "twitter-test.csv"

#for split, dataset in twitter_dataset.items():
#   dataset.to_csv(f"twitter-{split}.csv", index = False)


import pandas as pd
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt

label_result = pd.read_csv('HW_AI/HW_Final/label.csv')
label_result = label_result.iloc[:,1]
label_result = label_result.to_numpy()
cpu_result = pd.read_csv('HW_AI/HW_Final/cpu.csv')
cpu_result = cpu_result.iloc[:,1]
cpu_result = cpu_result.to_numpy()
cpu_result = cpu_result.astype(np.int32)
gpu_result = pd.read_csv('HW_AI/HW_Final/gpu.csv')
gpu_result = gpu_result['label'].to_numpy()

label_cpu_mutual = metrics.adjusted_mutual_info_score(label_result, cpu_result)
label_gpu_mutual = metrics.adjusted_mutual_info_score(label_result, gpu_result)
cpu_gpu_mutual = metrics.adjusted_mutual_info_score(cpu_result, gpu_result)
print(cpu_result)
print(gpu_result)
print(label_cpu_mutual)
print(label_gpu_mutual)
print(cpu_gpu_mutual)

cpu_time = [1.4,14.5,1170.2]
gpu_time = [0.97, 0.16, 0.32]

x = [1,2]
read = [1.4, 0.97]
build_graph = [15.9, 1.13]
louvain = [1186.1, 1.45]

plt.barh(x, read, label = 'Read_Files')
plt.barh(x, build_graph, label = 'Build Graph')
plt.barh(x, louvain, label = 'Louvain_Modularity')
plt.yticks([1,2],['Cpu', 'Gpu'])
plt.title('Time compare')
plt.xlabel('Time')
plt.ylabel('Device')
plt.legend(loc = 'best')
plt.show()


import requests
from haversine import haversine

from config import api_key, home

LOC_HOME = [float(x) for x in home.split(",")]

def get_loc_weather(loc):
    degree_sign = "\N{DEGREE SIGN}"
    condition_url = f"https://api.aerisapi.com/observations/{loc}?&format=json&filter=allstations&limit=1&{api_key}"
    resp = requests.get(condition_url).json()
    observation = resp["response"]["ob"]
    condition = observation["weatherShort"]
    temp_cel = str(observation["tempC"]) + degree_sign + "C"
    humidity = str(observation["humidity"]) + " %"
    wind_spe = str(observation["windKPH"]) + " kmph"
    wind_dir = str(observation["windDirDEG"]) + degree_sign
    full_location = str(resp["response"]["place"]["name"]).capitalize()
    print(
        f"The current conditions in {full_location} are:",
        f"Temeprature : {temp_cel}",
        f"Condition   : {condition}",
        f"Humidity    : {humidity}",
        f"Wind Speed  : {wind_spe}",
        f"Wind Dir.   : {wind_dir}",
        sep="\n"
    )

def get_weather(loc):
    co_ors = []
    index = 1
    lookup_url = f"https://api.aerisapi.com/places/search?query=name:{loc}&limit=10&{api_key}"
    geolookup = requests.get(lookup_url)
    places = geolookup.json()["response"]
    if not places:
        print("Location lookup failed")
        return
    for place in places:
        coor = [place["loc"]["lat"], place["loc"]["long"]]
        co_ors.append(coor)
        how_far = round(haversine(coor, LOC_HOME), ndigits=2)
        city = place["place"]["name"]
        country = place["place"]["countryFull"]
        print(index, city, "({})".format(country), how_far, "km")
        index += 1
    choice = int(input("Enter location: "))
    get_loc_weather(co_ors[choice - 1])

def conditions():
    selection = input("Enter City Name:")
    if selection.lower() == "h":
        get_loc_weather(home)
    else:
        get_weather(selection)

if __name__ == "__main__":
    conditions()