"""Extraction."""
import json
import logging
import time
from pathlib import Path
from typing import Any, List

import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup, SoupStrainer
from requests.exceptions import HTTPError

DATA_DIR = "/opt/airflow/data"
IMDB_TABLES = ["title.basics", "title.ratings"]

def _extract_nyt_reviews(url: str, key: str, left_boundary: str, right_boundary: str) -> bool:
    """Extract NYT movie reviews from movie review API.

    Fetch movie reviews in a time frame starting at left_boundary and ending
    at right_boundary. The server only allows for 10 requests per minute so,
    there will be a timeout of one minute in case a 429 status code is
    encountered. The result is dumped as json to ./data.

    Args:
        url: URL for the NYT movie review API.
        key: Key for the NYT movie review API.
        left_boundary: Start date, format must be %Y-%m-%d.
        right_boundary: End date, format must be %Y-%m-%d.

    Returns:
        Boolean indicating if reviews were dumped.
    """
    movies = []
    has_more = True
    offset = 0

    while has_more:
        try:
            response = requests.get(
                url=url + "/reviews/search.json",
                params={
                    "api-key": key,
                    "opening-date": f"{left_boundary}:{right_boundary}",
                    "offset": str(offset),
                },
            )
            response.raise_for_status()

            response_parsed = response.json()

            # Check if response has more results
            has_more = response_parsed["has_more"]
            offset += 20

            results = response_parsed["results"]
            if results is not None:
                movies += results

        except HTTPError as err:
            # Pause for 1 minute in case request limit is reached
            if err.response.status_code == 429:
                time.sleep(60)
            else:
                logging.error(err)

    file_name = "nyt-review.json"

    if movies:
        logging.info(f"Fetched {len(movies)} movie reviews. Writing to {file_name}.")
        with open(f"{DATA_DIR}/nyt/nyt-review.json", "w") as f:
            json.dump(movies, f, indent=4)
    else:
        logging.info("No reviews available.")

    return True if movies else False

def _get_download_links(url: str) -> List[str]:
    """Get download links from url.

    Parse the site and extract all hrefs that point to zipped files.

    Args:
        url: The URL for the site to parse.

    Returns:
        A list of urls.
    """
    links = []
    response = requests.get(url)

    for link in BeautifulSoup(response.content, parse_only=SoupStrainer("a"), features="lxml"):
        if hasattr(link, "href") and link["href"].endswith("gz"):
            links.append(link["href"])

    return links

def _extract_imdb_datasets(url: str) -> List[str]:
    """Extract datasets from IMDB.

    Fetch the title.basics and title.ratings datasets from IMDB and dump new
    rows as csv.gz to ./data.

    Args:
        url: URL to get download links via _get_download_links.

    Returns:
        List of dumped table names.
    """
    urls = _get_download_links(url)
    urls = [url for url in urls if any(keep_url in url for keep_url in IMDB_TABLES)]
    tbl_urls = {tbl: url for tbl, url in zip(IMDB_TABLES, urls)}

    dumped_tbls: List[str] = []

    for tbl, url in tbl_urls.items():
        df = pd.read_table(url, header=0, compression="gzip")
        ids_file = f"{DATA_DIR}/imdb/ids/ids.{tbl}.csv"

        if Path(ids_file).exists():
            existing_ids = pd.read_csv(ids_file, header=None).squeeze("columns")
            df = df.loc[~df.tconst.isin(existing_ids)]

        # Append new ids
        df.tconst.to_csv(ids_file, header=False, index=False, mode="a")

        # '\\N' encodes missing values
        df = df.where(df != "\\N", other=np.nan)

        n_rows = df.shape[0]

        file_name = f"imdb/tables/{tbl}.csv.gz"

        if n_rows > 0:
            logging.info(f"Fetched {n_rows} new rows for {tbl}. Writing to {file_name}.")

            dumped_tbls.append(tbl)

            df.to_csv(f"{DATA_DIR}/{file_name}", index=False)
        else:
            logging.info(f"No new rows for {tbl}.")

    return dumped_tbls

def _branch_nyt_tests(**context: Any) -> str:
    """Branch for testing.

    Skip the data tests if there are no new reviews available.

    Args:
        context: Airflow context.

    Returns:
        ID of task to run.
    """
    has_results = context["task_instance"].xcom_pull(
        task_ids="extract_nyt_reviews", key="return_value"
    )
    return "run_tests_raw_nyt_reviews" if has_results else "skip_tests_raw_nyt_reviews"

def _branch_nyt_copy(**context: Any) -> str:
    """Branch for copying.

    Skip the copy if there are no new reviews available.

    Args:
        context: Airflow context.

    Returns:
        ID of task to run.
    """
    has_results = context["task_instance"].xcom_pull(
        task_ids="extract_nyt_reviews", key="return_value"
    )
    return "copy_raw_nyt_table" if has_results else "skip_copy_raw_nyt_table"

def _branch_imdb_tests(**context: Any) -> List[str]:
    """Branch for testing IMDB datasets.

    Skip the data tests if there are no new records available.

    Args:
        context: Airflow context.

    Returns:
        IDs of tasks to run.
    """
    dumped_tbls = context["task_instance"].xcom_pull(
        task_ids="extract_imdb_datasets", key="return_value"
    )

    next_tasks = []
    for tbl in IMDB_TABLES:
        tbl_suffix = tbl.replace("title.", "")
        if tbl in dumped_tbls:
            next_tasks.append(f"run_tests_raw_imdb_{tbl_suffix}")
        else:
            next_tasks.append(f"skip_tests_raw_imdb_{tbl_suffix}")

    return next_tasks

def _branch_imdb_copy(**context: Any) -> List[str]:
    """Branch for copying IMDB datasets.

    Skip the copy if there are no new records available.

    Args:
        context: Airflow context.

    Returns:
        IDs of tasks to run.
    """
    dumped_tbls = context["task_instance"].xcom_pull(
        task_ids="extract_imdb_datasets", key="return_value"
    )

    next_tasks = []
    for tbl in IMDB_TABLES:
        tbl_suffix = tbl.replace("title.", "")
        if tbl in dumped_tbls:
            next_tasks.append(f"copy_raw_imdb_{tbl_suffix}_table")
        else:
            next_tasks.append(f"skip_copy_raw_imdb_{tbl_suffix}_table")

    return next_tasks


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
	Use this to execute the differential kinematics
	controller in our kinecontrol paper.
'''
from __future__ import print_function
import Sofa
import math
import sys, os
import time
import logging
import datetime
import numpy as np
from utils import *
from config import *

from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec

logger = logging.getLogger(__name__)

# https://www.sofa-framework.org/community/forum/topic/get-the-position-value-from-a-mechanicalobject-point-in-python/
def moveRestPos(rest_pos, pose):
	str_out = ' '
	dx, dy, dz = pose
	for i in range(0,len(rest_pos)) :
		str_out= str_out + ' ' + str(rest_pos[i][0]+dx)
		str_out= str_out + ' ' + str(rest_pos[i][1]+dy)
		str_out= str_out + ' ' + str(rest_pos[i][2]+dz)
	return str_out

def rotateRestPos(rest_pos,rx,centerPosY,centerPosZ):
	str_out = ' '
	for i in xrange(0,len(rest_pos)) :
		newRestPosY = (rest_pos[i][1] - centerPosY)*math.cos(rx) - (rest_pos[i][2] - centerPosZ)*math.sin(rx) +  centerPosY
		newRestPosZ = (rest_pos[i][1] - centerPosY)*math.sin(rx) + (rest_pos[i][2] - centerPosZ)*math.cos(rx) +  centerPosZ
		str_out= str_out + ' ' + str(rest_pos[i][0])
		str_out= str_out + ' ' + str(newRestPosY)
		str_out= str_out + ' ' + str(newRestPosZ)
	return str_out

class controller(Sofa.PythonScriptController):
	'''
		For examples, see:

		+ Keyboard Control:
			- https://github.com/lakehanne/sofa/blob/master/examples/Tutorials/StepByStep/Dentistry_Python/keyboardControl.py
		+ Parallel and SSH Launcher:
			- https://github.com/lakehanne/sofa/blob/master/tools/sofa-launcher/launcher.py
		+ OneParticle:
			- https://github.com/lakehanne/sofa/blob/master/tools/sofa-launcher/example.py
	'''
	def initGraph(self, root):
		self.move_dist = move_dist #(0, .40, 0)
		self.growth_rate = growth_rate #.5  #was .05
		self.max_pressure = max_pressure #100 # was 15

		self.root = root

		dome_all_dofs 		= self.get_dome_dofs(self.root)
		self.dh_dofs		= dome_all_dofs.dh_dofs
		self.cav_dofs		= dome_all_dofs.cav_dofs
		self.cover_dofs		= dome_all_dofs.cover_dofs

	# domes' mechanical states
	def get_dome_dofs(self, node):
		'here node is root'
		domehead = node.getChild('DomeHead')
		dh_dofs = domehead.getObject('dh_dofs')

		cav_node = domehead.getChild('DomeCavity')
		cav_dofs = cav_node.getObject('dome_cav_dofs')

		cover_node = domehead.getChild('DomeCover')
		cover_dofs = cover_node.getObject('dome_cover_dofs')

		cover_collis_node = domehead.getChild('DomeCoverCollis')
		cover_collis_dofs = cover_collis_node.getObject('dome_cover_collis_dofs')

		return Bundle(dict(dh_dofs=dh_dofs,
						   cav_dofs=cav_dofs,
						   cover_dofs=cover_dofs
						))

	def bwdInitGraph(self,node):
		# find the position at the end of the shape (which has the biggest x coordinate)
		dh_dofs = self.get_dome_dofs(self.root).dh_dofs.position
		max_x, max_y, max_z = 0, 0, 0
		max_idx_x, max_idx_y, max_idx_z = 0, 0, 0

		for i in range(len(dh_dofs)):
			if dh_dofs[i][0] > max_x:
				max_idx_x = i
				max_x = dh_dofs[i][0]
			if dh_dofs[i][1] > max_y:
				max_idx_y = i
				max_y = dh_dofs[i][1]
			if dh_dofs[i][2] > max_z:
				max_idx_z = i
				max_z = dh_dofs[i][2]

		self.max_vals = Bundle(dict(max_x=max_x, max_y=max_y, max_z=max_z))
		print('dh trans [x,y,z] {}, {}, {}'.format(max_x, max_y, max_z))
		return 0

	def run_traj_plotter(self):
		if self.is_chart_updated:
			self.traj_plotter.update(self.data)
			# time.sleep(.11)
		self.is_chart_updated = False

		return 0

	def deform_positive(self, dofs):
		print('dome head dofs: ', dofs.position)

	def onBeginAnimationStep(self, deltaTime):
		deltaTime  += deltaTime
		# obtain associated dofs and cavity dofs

		while(deltaTime < 2):
			self.deform_positive(self.dh_dofs)

		return 0;

	def onEndAnimationStep(self, deltaTime):
		sys.stdout.flush()
		#access the 'position' state vector
		self.bwdInitGraph(self.root)
		return 0;

	def onKeyPressed(self, c):
		self.dt = self.root.findData('dt').value
		incr = self.dt*1000.0
		self.dh_dofs = self.get_dome_dofs(self.root).dh_dofs
		# self.dh_dofs		= dome_all_dofs.dh_dofs

		if (ord(c)==19): # UP Key
			print("expanding ...")
			test = moveRestPos(self.dh_dofs.position, (300.0, 300.0, 300.0))
			self.dh_dofs.findData('position').value = test

		if (ord(c)==21): # DOWN Key
			print("retracting ...")
			test = moveRestPos(self.dh_dofs.position, (-300.0, -300.0, -300.0))
			self.dh_dofs.findData('position').value = test

		self.bwdInitGraph(self.root)

	def onLoaded(self, node):
		return 0;

	def reset(self):
		## Please feel free to add an example for a simple usage in /home/lex/catkin_ws/src/superchicko/sofa/python/xml_2_scn.py
		return 0;

	def onMouseButtonMiddle(self, mouseX,mouseY,isPressed):
		# usage e.g.
		if isPressed :
		   print("Control+Middle mouse button pressed at position "+str(mouseX)+", "+str(mouseY))
		return 0;

	def onScriptEvent(self, senderNode, eventName,data):
		## Please feel free to add an example for a simple usage in /home/lex/catkin_ws/src/superchicko/sofa/python/xml_2_scn.py
		return 0;

	def onMouseButtonRight(self, mouseX,mouseY,isPressed):
		## usage e.g.
		if isPressed :
		   print("Control+Right mouse button pressed at position "+str(mouseX)+", "+str(mouseY))
		return 0;

	def onMouseButtonLeft(self, mouseX,mouseY,isPressed):
		## usage e.g.
		if isPressed :
		   print("Control+Left mouse button pressed at position "+str(mouseX)+", "+str(mouseY))
		return 0;


import tensorflow as tf
import numpy as np
import random

from agents.AbstractAgent import AbstractAgent

from minigames.utils import state_of_marine, move_to_position
from utils.select_algorithm import choose_algorithm
from utils.replay_buffer import UniformBuffer

class Agent(AbstractAgent):
    def __init__(self, env, action_dim, screen_size, method, gamma=0.99, epsilon=1.0, lr=1e-4, loss='mse', batch_size=32,
                epsilon_decrease=0.001, epsilon_min=0.05, update_target=2000, num_episodes=5000, max_memory=100000):
        super(Agent, self).__init__(screen_size)

        obs = env.reset()
        screen = np.array(obs.observation['feature_screen'])
        screen = np.reshape(screen, (screen.shape[1], screen.shape[2], screen.shape[0]))
        screen = tf.convert_to_tensor(screen, dtype=tf.float64)
        self.input_dim = screen.shape
        self.action_dim = action_dim

        # Hiperparametros
        self.gamma = gamma
        self.epsilon = epsilon
        self.lr = lr
        self.loss = loss
        self.batch_size = batch_size

        self.epsilon_decrease = epsilon_decrease
        self.epsilon_min = epsilon_min
        self.update_target = update_target

        self.num_episodes = num_episodes
        self.memory_size = max_memory
        self.cur_frame = 0

        # Red principal y target.
        self.main_nn, self.target_nn, \
        self.optimizer, self.loss_fn = choose_algorithm(method, self.input_dim, self.action_dim,
                                                        self.lr, self.loss)

        # Buffer donde se almacenaran las experiencias del agente.

        self.buffer = UniformBuffer(self.memory_size)

    def step(self, state, pos_marine):
        action = self.select_epsilon_greedy_action(state)

        # Dependiendo de la acción se mueve ciertas coordenadas
        destination = move_to_position(action, self.screen_size)

        return action, self._MOVE_SCREEN("now", self._xy_offset(pos_marine, destination[0], destination[1]))

    def state_marine(self, obs):
        # Representación del beacon y marino
        beacon = self.get_beacon(obs)
        marine = self.get_marine(obs)

        dist = np.hypot((beacon.x - marine.x), (beacon.y - marine.y))

        screen = np.array(obs.observation['feature_screen'])
        screen = np.reshape(screen, (screen.shape[1], screen.shape[2], screen.shape[0]))
        state = tf.convert_to_tensor(screen, dtype=tf.float64)
        pos_marine = self.get_unit_pos(marine)

        return state, pos_marine, dist

    def select_army(self, obs):
        # La primera acción selecciona a los army
        if obs.first():
            return self._SELECT_ARMY

    def select_epsilon_greedy_action(self, state, aux_epsilon=1.0):
        """Realiza una acción aleatoria con prob. épsilon; de lo contrario, realiza la mejor acción."""
        result = tf.random.uniform((1,))
        if result < self.epsilon and result < aux_epsilon:
            return random.choice(range(self.action_dim)) #env.action_space.sample() # Acción aleatoria.
        else:
            state = np.reshape(state, (1, tf.shape(state)[0].numpy(), tf.shape(state)[1].numpy(), tf.shape(state)[2].numpy()))
            return tf.argmax(self.main_nn.predict(state)[0]).numpy() # Acción greddy.

    def train_step(self, states, actions, rewards, next_states, dones):
        """Realiza una iteración de entrenamiento en un batch de datos."""

        next_qs = self.target_nn.predict(next_states, batch_size=self.batch_size)
        max_next_qs = tf.reduce_max(next_qs, axis=-1)
        target = rewards + (1. - dones) * self.gamma * max_next_qs
        with tf.GradientTape() as tape:
            qs = self.main_nn(states)
            action_masks = tf.one_hot(actions, self.action_dim)
            masked_qs = tf.reduce_sum(action_masks * qs, axis=-1)
            loss = self.loss_fn(target, masked_qs)
        grads = tape.gradient(loss, self.main_nn.trainable_variables)
        self.optimizer.apply_gradients(zip(grads, self.main_nn.trainable_variables))
        return loss

    def decrease_epsilon(self):
        """Decrecimiento del epsilon."""
        if self.epsilon > self.epsilon_min:
            self.epsilon -= self.epsilon_decrease
        else:
            self.epsilon = self.epsilon_min

    def copy_weights(self, Copy_from, Copy_to):
        """
        Function to copy weights of a model to other
        """
        variables2 = Copy_from.trainable_variables
        variables1 = Copy_to.trainable_variables
        for v1, v2 in zip(variables1, variables2):
            v1.assign(v2.numpy())
    def save_model(self, filename):
        self.learner.save_q_table(filename + '/model.pkl')

    def load_model(self, filename):
        self.learner.load_model(filename + '/model.pkl')


"""
Tests for voting 10/07/2021.
"""
import pytest

from scripts.vote_2021_10_07 import (start_vote)

from utils.config import ldo_token_address, lido_dao_acl_address, lido_dao_token_manager_address

PURCHASE_CONTRACT_PAYOUT_ADDRESS = '0x689E03565e36B034EcCf12d182c3DC38b2Bb7D33'

payout_curve_rewards = {
    'amount': 3_550_000 * (10 ** 18),
    'address': '0x753D5167C31fBEB5b49624314d74A957Eb271709',
}

payout_balancer_rewards = {
    'amount': 300_000 * (10 ** 18),
    'address': '0x1dD909cDdF3dbe61aC08112dC0Fdf2Ab949f79D8',
}

payout_purchase_contract = {
    'amount': '462962962962963400000000',  # 462,962.9629629634 * (10 ** 18)
    'address': PURCHASE_CONTRACT_PAYOUT_ADDRESS,
}

grant_role_purchase_contract = {
    'address': PURCHASE_CONTRACT_PAYOUT_ADDRESS,
    'permission_name': 'ASSIGN_ROLE'
}

payout_finance_multisig = {
    'amount': 28_500 * (10 ** 18),  # TODO: Check current rate on 1inch before run
    'address': '0x48F300bD3C52c7dA6aAbDE4B683dEB27d38B9ABb',
    'reference': 'Finance multisig transfer to pay a bug bounty'
}

def curve_balance(ldo) -> int:
    """Returns LDO balance of Curve rewards distributor"""
    return ldo.balanceOf(payout_curve_rewards['address'])

def balancer_balance(ldo) -> int:
    """Returns LDO balance of Balancer rewards distributor"""
    return ldo.balanceOf(payout_balancer_rewards['address'])

def purchase_contract_balance(ldo) -> int:
    """Returns LDO balance of purchase contract"""
    return ldo.balanceOf(payout_purchase_contract['address'])

def finance_multisig_balance(ldo) -> int:
    """Returns LDO balance of finance multisig contract"""
    return ldo.balanceOf(payout_finance_multisig['address'])

def has_assign_role_permission(acl, token_manager, who) -> int:
    """Returns if address has ASSIGN_ROLE on TokenManager contract"""
    return acl.hasPermission(who, token_manager, token_manager.ASSIGN_ROLE())

@pytest.fixture(scope='module')
def ldo(interface):
    """Returns contract of LDO token."""
    return interface.ERC20(ldo_token_address)

@pytest.fixture(scope='module')
def acl(interface):
    """Returns ACL contract"""
    return interface.ACL(lido_dao_acl_address)

@pytest.fixture(scope='module')
def token_manager(interface):
    """Returns TokenManager contract"""
    return interface.TokenManager(lido_dao_token_manager_address)

def test_common(
        acl, token_manager, ldo_holder,
        helpers, accounts, dao_voting, ldo
):
    """Perform testing for the whole voting."""
    curve_balance_before = curve_balance(ldo)
    balancer_balance_before = balancer_balance(ldo)
    purchase_contract_balance_before = purchase_contract_balance(ldo)
    finance_multisig_balance_before = finance_multisig_balance(ldo)

    assert not has_assign_role_permission(acl, token_manager, grant_role_purchase_contract['address'])

    vote_id, _ = start_vote({
        'from': ldo_holder
    }, silent=True)

    helpers.execute_vote(
        vote_id=vote_id, accounts=accounts, dao_voting=dao_voting
    )

    curve_balance_after = curve_balance(ldo)
    balancer_balance_after = balancer_balance(ldo)
    purchase_contract_balance_after = purchase_contract_balance(ldo)
    finance_multisig_balance_after = finance_multisig_balance(ldo)

    curve_inc = curve_balance_after - curve_balance_before
    balancer_inc = balancer_balance_after - balancer_balance_before
    purchase_contract_balance_inc = purchase_contract_balance_after - purchase_contract_balance_before
    finance_multisig_balance_inc = finance_multisig_balance_after - finance_multisig_balance_before

    assert curve_inc == payout_curve_rewards['amount'], 'Failed on Curve'
    assert balancer_inc == payout_balancer_rewards['amount'], 'Failed on Balancer'
    assert purchase_contract_balance_inc == payout_purchase_contract['amount'], 'Failed on purchase contract'
    assert has_assign_role_permission(acl, token_manager,
                                      grant_role_purchase_contract['address']), 'Failed on grant ASSIGN_ROLE'
    assert finance_multisig_balance_inc == payout_finance_multisig['amount'], 'Failed on purchase contract'


# -*- coding: utf-8 -*-

from __future__ import unicode_literals

try:
    from imp import reload
except ImportError:
    from importlib import reload

from django.core.urlresolvers import resolve, reverse
from django.http import Http404
from django.test import override_settings
from django.utils.translation import override

import parler.appsettings

from ..models import Category
from ..views import FaqByCategoryView, FaqAnswerView

from .test_base import AldrynFaqTest

class TestFaqByCategoryView(AldrynFaqTest):
    reload_parler_appsettings = True

    def test_view_context(self):
        """Tests that the FaqByCategoryView produces the correct context."""
        category_1 = self.reload(self.category1, "en")
        category_1_url = category_1.get_absolute_url()

        question_1 = self.reload(self.question1, "en")

        request = self.get_page_request(
            page=self.page,
            user=self.user,
            path=category_1_url,
        )

        url_kwargs = resolve(category_1_url).kwargs

        try:
            response = FaqByCategoryView.as_view()(request, **url_kwargs)
        except Http404:
            self.fail('Could not find category')

        self.assertEqualItems(
            response.context_data['object_list'],
            [question_1, ],
        )

    def test_view_context_fallback(self):
        """
        Tests that the FaqByCategoryView produces the correct context
        when requesting a category in an untranslated language.
        """
        category_2 = self.reload(self.category2, "en")
        category_2_url = category_2.get_absolute_url()

        question_2 = self.reload(self.question2, "en")

        request = self.get_page_request(
            page=self.page,
            user=self.user,
            path=category_2_url,
        )

        url_kwargs = resolve(category_2_url).kwargs

        with override_settings(**self.enabled_parler_fallback_settings):
            reload(parler.appsettings)

            try:
                response = FaqByCategoryView.as_view()(request, **url_kwargs)
            except Http404:
                self.fail('Could not find category')

            self.assertEqualItems(
                response.context_data['object_list'],
                [question_2, ],
            )

    def test_view_old_format_redirect(self):
        """
        Tests that the FaqByCategoryView redirects user
        when accessed with old category url format
        """
        category_1 = self.reload(self.category1, "en")
        category_1_url_new = category_1.get_absolute_url()

        kwargs = {"category_slug": category_1.slug}

        with override('en'):
            category_1_url_name = '{ns}:faq-category'.format(
                ns=self.app_config.namespace
            )
            category_1_url_old = reverse(category_1_url_name, kwargs=kwargs)

        request = self.get_page_request(
            page=self.page,
            user=self.user,
            path=category_1_url_old,
        )

        response = FaqByCategoryView.as_view()(request, **kwargs)

        self.assertEquals(response.status_code, 301)
        self.assertEquals(response.url, category_1_url_new)

    def test_list_view(self):
        """Test category list view to contain a proper set of categories"""
        def _do_test_list_view(language_code):
            with override(language_code):
                categories = Category.objects.language(
                    language_code).active_translations(language_code).filter(
                        appconfig=self.app_config)
                url = reverse('{ns}:faq-category-list'.format(
                    ns=self.app_config.namespace))
                response = self.client.get(url, follow=True)
                for category in categories:
                    self.assertContains(response, category.name)

        for language_code in ('en', 'de'):
            _do_test_list_view(language_code)

        with override_settings(**self.settings_en):
            reload(parler.appsettings)
            _do_test_list_view('en')

class TestFaqAnswerView(AldrynFaqTest):
    reload_parler_appsettings = True

    def test_view_context(self):
        """Tests that the FaqByCategoryView produces the correct context."""
        question_1 = self.reload(self.question1, "en")
        question_1_url = question_1.get_absolute_url("en")

        url_kwargs = resolve(question_1_url).kwargs

        request = self.get_page_request(
            page=self.page,
            user=self.user,
            path=question_1_url,
        )

        response = FaqAnswerView.as_view()(request, **url_kwargs)

        self.assertEqual(
            response.context_data['object'],
            question_1,
        )

    def test_view_context_fallback(self):
        """
        Tests that the FaqByCategoryView produces the correct context
        when requesting a category in an untranslated language.
        """
        question_2 = self.reload(self.question1, "en")
        question_2_url = question_2.get_absolute_url("en")

        url_kwargs = resolve(question_2_url).kwargs

        request = self.get_page_request(
            page=self.page,
            user=self.user,
            path=question_2_url,
        )

        with override_settings(**self.enabled_parler_fallback_settings):
            reload(parler.appsettings)
            response = FaqAnswerView.as_view()(request, **url_kwargs)

        self.assertEqual(
            response.context_data['object'],
            question_2,
        )

    def test_view_old_format_redirect(self):
        """
        Tests that the TestFaqAnswerView redirects user
        when accessed with old category url format
        """
        category_1 = self.reload(self.category1, "en")
        question_1 = self.reload(self.question1, "en")
        question_1_url_new = question_1.get_absolute_url()

        kwargs = {
            "category_slug": category_1.slug,
            "pk": question_1.pk
        }

        with override('en'):
            url_name = '{ns}:faq-answer'.format(ns=self.app_config.namespace)
            question_1_url_old = reverse(url_name, kwargs=kwargs)

        request = self.get_page_request(
            page=self.page,
            user=self.user,
            path=question_1_url_old,
        )

        response = FaqAnswerView.as_view()(request, **kwargs)

        self.assertEquals(response.status_code, 301)
        self.assertEquals(response.url, question_1_url_new)

    def test_answer_match_category(self):
        """
        Tests that the question id given in url
        belongs to the given category, if not then 404 is raised.
        """
        category_1 = self.reload(self.category1, "de")
        question_2 = self.reload(self.question2, "de")

        kwargs = {
            "category_pk": category_1.pk,
            "category_slug": category_1.slug,
            "pk": question_2.pk
        }

        with override('de'):
            url_name = '{ns}:faq-answer'.format(ns=self.app_config.namespace)
            question_2_invalid_url = reverse(url_name, kwargs=kwargs)

        request = self.get_page_request(
            page=self.page,
            user=self.user,
            path=question_2_invalid_url,
        )

        with self.assertRaises(Http404):
            FaqAnswerView.as_view()(request, **kwargs)


"""estres forms"""

from django import forms

from estres.models import *

class EstresForm(forms.ModelForm):

    class Meta:
        model = EstresModel

        fields = [
                "numero_escenarios",
                "horizonte_riesgo",
                "fechacorte",
                "info_mercado",
                "inc_financiero",
                "inc_biometricos",
                "inc_frecsev",
        ]
        labels = {
                "numero_escenarios":"Número de escenarios (entero de 1000 a 10000)",
                "horizonte_riesgo":"Horizonte de riesgo",
                "fechacorte":"Fecha de corte",
                "info_mercado":"INFO_MERCADO.xlsx",
                "inc_financiero":"INC_FINANCIERO.xlsx",
                "inc_biometricos":"INC_BIOMETRICOS.xlsx",
                "inc_frecsev":"INC_FRECUENCIA_SEVERIDAD.xlsx",
        }
        widgets = {
                "numero_escenarios":forms.TextInput(),
                "horizonte_riesgo":forms.Select(),
                "fechacorte":forms.DateInput(format=('%Y-%m-%d'), attrs={'class':'datepicker'}),
                "info_mercado":forms.FileInput(attrs={"accept":".xlsx"}),
                "inc_financiero":forms.FileInput(attrs={"accept":".xlsx"}),
                "inc_biometricos":forms.FileInput(attrs={"accept":".xlsx"}),
                "inc_frecsev":forms.FileInput(attrs={"accept":".xlsx"}),
        }


import logging
import os
from time import time
from abc import abstractmethod, ABC
from pathlib import Path
from typing import Any, Iterator, Optional, Union, List, Dict, Set

from forte.common.configuration import Config
from forte.common.exception import ProcessExecutionException
from forte.common.resources import Resources
from forte.data.base_pack import PackType
from forte.data.data_pack import DataPack
from forte.data.multi_pack import MultiPack
from forte.data.types import ReplaceOperationsType
from forte.pipeline_component import PipelineComponent
from forte.utils.utils import get_full_module_name

__all__ = [
    "BaseReader",
    "PackReader",
    "MultiPackReader",
]

logger = logging.getLogger(__name__)

class BaseReader(PipelineComponent[PackType], ABC):
    r"""The basic data reader class. To be inherited by all data readers.

    Args:
        from_cache: Decide whether to read from cache
            if cache file exists. By default (``False``), the reader will
            only read from the original file and use the cache file path
            for caching, it will not read from the ``cache_directory``.
            If ``True``, the reader will try to read a datapack from the
            caching file.
        cache_directory: The base directory to place the
            path of the caching files. Each collection is contained in one
            cached file, under this directory. The cached location for each
            collection is computed by
            :meth:`~forte.data.base_reader.BaseReader._cache_key_function`.

            .. note::

                A collection is the data returned by
                :meth:`~forte.data.base_reader.BaseReader._collect`.

        append_to_cache: Decide whether to append write
            if cache file already exists.  By default (``False``), we
            will overwrite the existing caching file. If ``True``, we will
            cache the datapack append to end of the caching file.
    """

    def __init__(
        self,
        from_cache: bool = False,
        cache_directory: Optional[str] = None,
        append_to_cache: bool = False,
        cache_in_memory: bool = False,
    ):
        super().__init__()
        self.from_cache = from_cache
        self._cache_directory = cache_directory
        self.component_name = get_full_module_name(self)
        self.append_to_cache = append_to_cache
        self._cache_in_memory = cache_in_memory
        self._cache_ready: bool = False
        self._data_packs: List[PackType] = []

        # needed for time profiling of reader
        self._enable_profiling: bool = False
        self._start_time: float = 0.0
        self.time_profile: float = 0.0

    def initialize(self, resources: Resources, configs: Config):
        super().initialize(resources, configs)

        # Clear memory cache
        self._cache_ready = False
        del self._data_packs[:]

    @classmethod
    def default_configs(cls):
        r"""Returns a `dict` of configurations of the reader with default
        values. Used to replace the missing values of input `configs`
        during pipeline construction.

        Here:

          - zip_pack (bool): whether to zip the results. The default value is
            False.

          - serialize_method: The method used to serialize the data. Current
            available options are `json`, `jsonpickle` and `pickle`. Default is
            `json`.

        """
        return {"zip_pack": False, "serialize_method": "json"}

    @staticmethod
    def pack_type():
        raise NotImplementedError

    @abstractmethod
    def _collect(self, *args: Any, **kwargs: Any) -> Iterator[Any]:
        r"""Returns an iterator of data objects, and each individual object
        should contain sufficient information needed to construct or locate
        a data pack in cache.

        For example: `data_source` can be a ``kwarg`` which is the path to a
        file that a reader can take to read and parse a file.

        Args:
            args: Specify the data source.
            kwargs: Specify the data source.

        Returns: Iterator of collections that are sufficient to create one pack.
        """
        raise NotImplementedError

    def parse_pack(self, collection: Any) -> Iterator[PackType]:
        r"""Calls :meth:`_parse_pack` to create packs from the collection.
        This internally setup the component meta data. Users should implement
        the :meth:`_parse_pack` method.
        """
        if collection is None:
            raise ProcessExecutionException(
                "Got None collection, cannot parse as data pack."
            )

        for p in self._parse_pack(collection):
            p.add_all_remaining_entries(self.name)
            yield p

    @abstractmethod
    def _parse_pack(self, collection: Any) -> Iterator[PackType]:
        r"""Returns an iterator of Packs parsed from a collection. Readers
        should implement this class to populate the class input.

        Args:
            collection: Object that can be parsed into a Pack.

        Returns: Iterator of Packs.
        """
        raise NotImplementedError

    def _cache_key_function(self, collection: Any) -> Optional[str]:
        # pylint: disable=unused-argument
        r"""Computes the cache key based on the type of data.

        Args:
            collection: Any object that provides information to identify the
                name and location of the cache file
        """
        return None

    def text_replace_operation(self, text: str) -> ReplaceOperationsType:
        # pylint: disable=unused-argument
        r"""Given the possibly noisy text, compute and return the
        replacement operations in the form of a list of (span, str)
        pairs, where the content in the span will be replaced by the
        corresponding str.

        Args:
            text: The original data text to be cleaned.

        Returns (List[Tuple[Tuple[int, int], str]]):
            the replacement operations.
        """
        return []

    def _get_cache_location(self, collection: Any) -> str:
        r"""Gets the path to the cache file for a collection.

        Args:
            collection: information to compute cache key.

        Returns (Path): file path to the cache file for a Pack.
        """
        # pylint: disable=assignment-from-none
        file_path = self._cache_key_function(collection)
        if file_path is None:
            raise ProcessExecutionException(
                "Cache key is None. You probably set `from_cache` to true but "
                "fail to implement the _cache_key_function"
            )

        return os.path.join(str(self._cache_directory), file_path)

    def _lazy_iter(self, *args, **kwargs):
        for collection in self._collect(*args, **kwargs):
            if self.from_cache:
                for pack in self.read_from_cache(
                    self._get_cache_location(collection)
                ):
                    pack.add_all_remaining_entries()
                    yield pack
            else:
                not_first = False
                for pack in self.parse_pack(collection):
                    # write to the cache if _cache_directory specified
                    if self._cache_directory is not None:
                        self.cache_data(collection, pack, not_first)

                    if not isinstance(pack, self.pack_type()):
                        raise ValueError(
                            f"No Pack object read from the given "
                            f"collection {collection}, returned {type(pack)}."
                        )

                    not_first = True
                    pack.add_all_remaining_entries()
                    yield pack

    def set_profiling(self, enable_profiling: bool = True):
        r"""Set profiling option.

        Args:
            enable_profiling: A boolean of whether to enable profiling
                for the reader or not (the default is True).
        """
        self._enable_profiling = enable_profiling

    def timer_yield(self, pack: PackType):
        r"""Wrapper generator for time profiling. Insert timers around
        'yield' to support time profiling for reader.

        Args:
            pack: DataPack passed from self.iter()
        """
        # Aggregate time cost
        if self._enable_profiling:
            self.time_profile += time() - self._start_time

        yield pack

        # Start timer
        if self._enable_profiling:
            self._start_time = time()

    def iter(self, *args, **kwargs) -> Iterator[PackType]:
        # pylint: disable=protected-access
        r"""An iterator over the entire dataset, giving all Packs processed
        as list or Iterator depending on `lazy`, giving all the Packs read
        from the data source(s). If not reading from cache, should call
        ``collect``.

        Args:
            args: One or more input data sources, for example, most
                DataPack readers accept `data_source` as file/folder path.
            kwargs: Iterator of DataPacks.
        """

        # Start timer
        if self._enable_profiling:
            self._start_time = time()

        if self._cache_in_memory and self._cache_ready:
            # Read from memory
            for pack in self._data_packs:
                if self._check_type_consistency:
                    if hasattr(pack._meta, "record"):
                        self.record(pack._meta.record)
                yield from self.timer_yield(pack)
        else:
            # Read via parsing dataset
            for pack in self._lazy_iter(*args, **kwargs):
                if self._check_type_consistency:
                    if hasattr(pack._meta, "record"):
                        self.record(pack._meta.record)
                if self._cache_in_memory:
                    self._data_packs.append(pack)

                yield from self.timer_yield(pack)

        self._cache_ready = True

    def record(self, record_meta: Dict[str, Set[str]]):
        r"""Modify the pack meta record field of the reader's output. The
        key of the record should be the entry type and values should
        be attributes of the entry type. All the information would be used
        for consistency checking purpose if the pipeline is initialized with
        `enforce_consistency=True`.

        Args:
            record_meta: the field in the datapack for type record that need to
                fill in for consistency checking.
        """
        pass

    def cache_data(self, collection: Any, pack: PackType, append: bool):
        r"""Specify the path to the cache directory.

        After you call this method, the dataset reader will use its
        ``cache_directory`` to store a cache of
        :class:`~forte.data.base_pack.BasePack` read
        from every document passed to read, serialized as one
        string-formatted :class:`~forte.data.base_pack.BasePack`. If the cache file for a given
        ``file_path`` exists, we read the :class:`~forte.data.base_pack.BasePack` from the cache.
        If the cache file does not exist, we will `create` it on our first
        pass through the data.

        Args:
            collection: The collection is a piece of data from the
                :meth:`_collect` function, to be read to produce DataPack(s).
                During caching, a cache key is computed based on the data in
                this collection.
            pack: The data pack to be cached.
            append: Whether to allow appending to the cache.
        """
        if not self._cache_directory:
            raise ValueError("Can not cache without a cache_directory!")

        os.makedirs(self._cache_directory, exist_ok=True)

        cache_filename = os.path.join(
            self._cache_directory, self._get_cache_location(collection)
        )

        logger.info("Caching pack to %s", cache_filename)
        if append:
            with open(
                cache_filename,
                "a",
                encoding="utf-8",
            ) as cache:
                cache.write(pack.to_string() + "\n")
        else:
            with open(
                cache_filename,
                "w",
                encoding="utf-8",
            ) as cache:
                cache.write(pack.to_string() + "\n")

    def read_from_cache(
        self, cache_filename: Union[Path, str]
    ) -> Iterator[PackType]:
        r"""Reads one or more Packs from ``cache_filename``, and yields Pack(s)
        from the cache file.

        Args:
            cache_filename: Path to the cache file.

        Returns:
            List of cached data packs.
        """
        logger.info("reading from cache file %s", cache_filename)
        with open(cache_filename, "r", encoding="utf-8") as cache_file:
            for line in cache_file:
                pack = DataPack.from_string(line.strip())
                if not isinstance(pack, self.pack_type()):
                    raise TypeError(
                        f"Pack deserialized from {cache_filename} "
                        f"is {type(pack)}, but expect {self.pack_type()}"
                    )
                yield pack

    def finish(self, resource: Resources):
        pass

    def set_text(self, pack: DataPack, text: str):
        r"""Assign the text value to the
        :class:`~forte.data.data_pack.DataPack`. This function will
        pass the ``text_replace_operation`` to the
        :class:`~forte.data.data_pack.DataPack` to conduct
        the pre-processing step.

        Args:
            pack: The :class:`~forte.data.data_pack.DataPack` to assign value for.
            text: The original text to be recorded in this dataset.
        """
        pack.set_text(text, replace_func=self.text_replace_operation)

class PackReader(BaseReader[DataPack], ABC):
    r"""A Pack Reader reads data into :class:`~forte.data.data_pack.DataPack`."""

    @staticmethod
    def pack_type():
        return DataPack

class MultiPackReader(BaseReader[MultiPack], ABC):
    r"""The basic :class:`~forte.data.multi_pack.MultiPack` data reader class.
    To be inherited by all
    data readers which return :class:`~forte.data.multi_pack.MultiPack`.
    """

    @staticmethod
    def pack_type():
        return MultiPack


from os import access
from tkinter import image_names
import cv2
import dropbox
import time
import random
start_time=time.time()

def take_snapshot():
    number = random.randint(0,100)
    videoCaptureObject=cv2.VideoCapture(0)
    result=True
    while(result):
        ret,frame=videoCaptureObject.read()
        img_name="img"+str(number)+".png"
        #print(ret,frame)
        cv2.imwrite(img_name,frame)
        start_time=time.time()
        result=False
    return img_name

    print("Snapshot taken")
    videoCaptureObject.release()
    cv2.destroyAllWindows()

def upload_files(image_name):
    access_token="sl.BFNXVhLcVxzWNu4AWyBuMBw9q07HWCeS1ifaeAhkH5wibVA8iwoLjtu8wVC-BWZ_dTMYCduOJ1NHUYhq1GMEDxuPeoUiGHtmbkwJuStlCNJnL8wtjFjws_HXPC7eISi4P6PNqoqvzzE"
    file= image_name
    file_from=file
    file_to="/ary/"+(image_name)
    dbx=dropbox.Dropbox(access_token)
    with open(file_from,"rb") as f:
        dbx.file_upload(f.read(),file_to,mode=dropbox.files.WriteMode.overwrite)
        print("file uploaded")

def main():
    while(True):
        if((time.time() - start_time) >= 5):
            name=take_snapshot()
            upload_files(name)

main()


import torch
from torch.utils.data import Dataset
import numpy as np
from pathlib import Path
from synthesizer.utils.text import text_to_sequence

class SynthesizerDataset(Dataset):
    def __init__(self, metadata_fpath: Path, mel_dir: Path, embed_dir: Path, hparams):
        print("Using inputs from:\n\t%s\n\t%s\n\t%s" % (metadata_fpath, mel_dir, embed_dir))

        with metadata_fpath.open("r") as metadata_file:
            metadata = [line.split("|") for line in metadata_file]

        mel_fnames = [x[1] for x in metadata if int(x[4])]
        mel_fpaths = [mel_dir.joinpath(fname) for fname in mel_fnames]
        embed_fnames = [x[2] for x in metadata if int(x[4])]
        embed_fpaths = [embed_dir.joinpath(fname) for fname in embed_fnames]
        self.samples_fpaths = list(zip(mel_fpaths, embed_fpaths))
        self.samples_texts = [x[5].strip() for x in metadata if int(x[4])]
        self.metadata = metadata
        self.hparams = hparams

        print("Found %d samples" % len(self.samples_fpaths))

    def __getitem__(self, index):
        # Sometimes index may be a list of 2 (not sure why this happens)
        # If that is the case, return a single item corresponding to first element in index
        if index is list:
            index = index[0]

        mel_path, embed_path = self.samples_fpaths[index]
        mel = np.load(mel_path).T.astype(np.float32)

        # Load the embed
        embed = np.load(embed_path)

        # Get the text and clean it
        text = text_to_sequence(self.samples_texts[index], self.hparams.tts_cleaner_names)

        # Convert the list returned by text_to_sequence to a numpy array
        text = np.asarray(text).astype(np.int32)

        return text, mel.astype(np.float32), embed.astype(np.float32), index

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

def collate_synthesizer(batch, r, hparams):
    # batch = shape(char_sequence, mel_spec, speaker_embedding, index)

    # get the lengths of every text sequence in the batch
    x_lens = [len(x[0]) for x in batch]
    # get the length of the longest text sequence
    max_x_len = max(x_lens)

    # make of list of all text sequences padded to the length of the longest one
    chars = [pad1d(x[0], max_x_len) for x in batch]
    # turn that list into a stack
    chars = np.stack(chars)

    # get the horizontal (i.e. stepwise) length of every mel-spectrogram
    spec_lens = [x[1].shape[-1] for x in batch]
    # get the length of the longest one
    max_spec_len = max(spec_lens) + 1

    # if the longest one is not divisible by reduction factor, then make it divisible by reduction factor
    if max_spec_len % r != 0:
        max_spec_len += r - (max_spec_len % r)

    # WaveRNN mel spectrograms are normalized to [0, 1] so zero padding adds silence
    # By default, SV2TTS uses symmetric mels, where -1*max_abs_value is silence.
    if hparams.symmetric_mels:
        mel_pad_value = -1 * hparams.max_abs_value
    else:
        mel_pad_value = 0

    # pad mels along step-axis, then turn in numpy stack (ndArray)
    mel = [pad2d(x[1], max_spec_len, pad_value=mel_pad_value) for x in batch]
    mel = np.stack(mel)

    # get all the speaker transfer embeddings for SV2TTS
    embeds = np.array([x[2] for x in batch])

    # get the indices for vocoder preprocessing
    indices = [x[3] for x in batch]

    # convert all of these sequence containers (apart from indices) to PyTorch tensors
    chars = torch.tensor(chars).long()
    mel = torch.tensor(mel)
    embeds = torch.tensor(embeds)

    return chars, mel, embeds, indices

def pad1d(x, max_len, pad_value=0):
    return np.pad(x, (0, max_len - len(x)), mode="constant", constant_values=pad_value)

def pad2d(x, max_len, pad_value=0):
    return np.pad(x, ((0, 0), (0, max_len - x.shape[-1])), mode="constant", constant_values=pad_value)


#!/usr/bin/env python

"""Test `crtm_api` module."""

from crtm_poll import crtm_api

from aiohttp import ClientSession
from aioresponses import aioresponses
import os
import pytest

class TestAPI:
    def test_can_log_fetch(self, tmpdir):
        fetch_path = 'fetch_log'
        file = tmpdir.join(fetch_path)
        crtm_api.stop_times.fetch_log(file, 'a', 'b', 'c')
        assert file.readlines()[1] == 'a,b,c'

    def test_can_not_log_fetch(self, tmpdir):
        fetch_path = 'fetch_log'
        file = tmpdir.join(fetch_path)
        crtm_api.stop_times.fetch_log(None, 'a', 'b', 'c')
        assert not os.path.isfile(file)

    @pytest.mark.asyncio
    async def test_can_fetch_ok_stop(self):
        with aioresponses() as m:
            m.get('https://www.crtm.es/widgets/api/GetStopsTimes.php?'
                  'codStop=8_17491&orderBy=2&stopTimesByIti=3&type=1',
                  status=200, body='test')
            session = ClientSession()
            fetch_conf = {
                    'log': None,
                    'timeout': 10,
                    'max_connections': 1}
            resp_text = await crtm_api.stop_times.fetch('8_17491', session,
                                                        fetch_conf)
            assert 'test' in resp_text
            await session.close()


from datetime import datetime

import pytest
from model_bakery import baker

from mytodo.models import Tarefa
from mytodo.services import tarefa_service

def test_should_get_tarefa_as_pending(db):
    my_tarefa = baker.make(Tarefa, description='Create an ansible deploy script', due_to=datetime.now())

    assert my_tarefa.status == 'pending'

def test_should_get_tarefa_as_done(db):
    my_tarefa = baker.make(Tarefa, description='Create an ansible deploy script', due_to=datetime.now())

    tarefa_updated = tarefa_service.mark_as_done(my_tarefa.id)

    assert tarefa_updated.status == 'done'

def test_should_raise_an_erro_for_invalid_tarefa_id(db):
    invalid_tarefa = 0
    with pytest.raises(RuntimeError) as error:
        tarefa = tarefa_service.mark_as_done(invalid_tarefa)

    assert str(error.value) == f"Tarefa ID: {invalid_tarefa} invalida"

def test_should_mark_as_undone(db):
    my_tarefa = baker.make(
        Tarefa,
        description='Create an ansible deploy script',
        due_to=datetime.now(),
        done=True)

    tarefa_updated = tarefa_service.mark_as_done(my_tarefa.id)

    assert tarefa_updated.status == 'pending'


from PyQt5.QtWidgets import QLabel,QLineEdit,QPushButton,QMessageBox,QWidget,QApplication,QMainWindow,QTextEdit
from PyQt5.QtGui import QFont
import sys,time

getter=str()
getter2=str()
class FirstWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(200, 100, 500, 500)
        self.setWindowTitle("Ð¡hinese telegram")
        self.start()
    def font(self,obj):
        obj.setFont(QFont("Times",30))
    def start(self):
        yozuv1=QLabel("Login:",self)
        yozuv1.setFont(QFont("Times",25))
        yozuv1.move(50,60)
        self.log=QLineEdit(self)
        self.log.setFont(QFont("Times",25))
        self.log.move(150,60)
        self.log.setPlaceholderText("login kiriting....")

        yozuv2=QLabel("Parol:",self)
        yozuv2.setFont(QFont("Times",25))
        yozuv2.move(50,150)
        self.par=QLineEdit(self)
        self.par.setFont(QFont("Times",25))
        self.par.move(150,150)
        self.par.setPlaceholderText("parol kiriting....")

        ok=QPushButton("OK",self)
        ok.setFont(QFont("Times",50))
        ok.move(200,250)
        ok.clicked.connect(self.run)
        btn=QPushButton("exit",self)
        btn.clicked.connect(self.hide)
    def hide(self):
        self.hide()
    def run(self):
        log_par=[['Admin','12345'],['User','54321']]
        if log_par[0][0]==self.log.text() and log_par[0][1]==self.par.text():
            self.Chat=Chat()
            self.Chat.show()
        if log_par[1][0]==self.log.text() and log_par[1][1]==self.par.text():
            self.Chat1=Chat1()
            self.Chat1.show()

class Chat(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.btn=QPushButton("Back",self)
        self.btn.clicked.connect(self.run1)
        self.resize(500, 500)
        self.send()
        self.setWindowTitle("Admin's Chat")
        self.show()

    def run1(self):
        self.hide()

    def send(self):
        self.wn1=QTextEdit(self)
        self.wn1.setGeometry(10,40,480,150)
        self.wn1.setFont(QFont("Times",20))
        self.wn1.setPlaceholderText("Text kiriting...")
        self.wn2=QTextEdit(self)
        self.wn2.setGeometry(10,200,480,150)
        self.wn2.setFont(QFont("Times",20))
        self.wn2.setPlaceholderText("Sizga kelgan xabarlar... \n")
        self.wn2.setText(getter2)
        self.send1=QPushButton("SendðŸ’¬",self)
        self.send1.setFont(QFont("Times",15))
        self.send1.move(200,400)
        self.send1.clicked.connect(self.sender)

    def sender(self):
        global getter
        getter=self.wn1.toPlainText()
        self.send1.hide()

class Chat1(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.btn=QPushButton("Back",self)
        self.btn.clicked.connect(self.run1)
        self.resize(500, 500)
        self.send()
        self.setWindowTitle("User's Chat")
        self.show()

    def run1(self):
        self.hide()

    def send(self):
        self.wn1=QTextEdit(self)
        self.wn1.setGeometry(10,40,480,150)
        self.wn1.setFont(QFont("Times",20))
        self.wn1.setPlaceholderText("Text kiriting...")
        self.wn2=QTextEdit(self)
        self.wn2.setGeometry(10,200,480,150)
        self.wn2.setFont(QFont("Times",20))
        self.wn2.setPlaceholderText("Sizga kelgan xabarlar... \n")
        self.wn2.setText(getter)
        self.send1=QPushButton("SendðŸ’¬",self)
        self.send1.setFont(QFont("Times",15))
        self.send1.move(200,400)
        self.send1.clicked.connect(self.sender)

    def sender(self):
        global getter2
        getter2=self.wn1.toPlainText()
        self.send1.hide()
app=QApplication(sys.argv)
oyna=FirstWindow()
oyna.show()
sys.exit(app.exec_())


import io
import os.path
import pickle
import json
import time
from threading import Thread
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import _G

# Goole Drive File Downloader
class Downloader:
  def __init__(self, id):
    self.id = id
    self.filename   = ""
    self.downloader = None
    self.progress   = 0
    self.thread     = None
    self.flag_complete = False

  # setup before the download
  def setup(self):
    self.header = _G.GDriveService.files().get(fileId=self.id).execute()
    self.request = _G.GDriveService.files().get_media(fileId=self.id)

  # start download
  def start(self):
    self.filename = self.header['name'].replace(_G.ForbiddenFileChar, '')
    stream = io.FileIO(self.filename, 'wb')
    self.downloader = MediaIoBaseDownload(stream, self.request)
    self.downloader._chunksize = _G.DownloaderChunkSize
    self.thread = Thread(target=self.download)
    self.thread.start()

  # async download the file
  def download(self):
    while self.flag_complete is False:
      stat, self.flag_complete = self.downloader.next_chunk()
      self.progress = int(stat.progress() * 100)

  def is_completed(self):
    return self.flag_complete

def get_auth_creds():
  if not os.path.exists('secret/'):
    os.mkdir('secret')
  if os.path.exists(_G.GDriveCredCache):
    with open(_G.GDriveCredCache, 'rb') as token:
      return pickle.load(token)
  return None

def load_creds_json():
  raw = _G.loadfile_with_resuce(_G.GDriveCredFilename, 'r')
  if not raw:
    raw = os.environ['CLIPPER_GDRIVE_CREDS']
  return json.loads(raw)

def start_auth_session(creds):
  if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
      creds.refresh(Request())
    else:
      flow = InstalledAppFlow.from_client_config(load_creds_json(), _G.GDriveScopes)
      creds = flow.run_local_server(port=0)
    with open(_G.GDriveCredCache, 'wb') as file:
      pickle.dump(creds, file)

  _G.GDriveService = build('drive', 'v3', credentials=creds)

def download_data_async(id):
  worker = Downloader(id)
  worker.setup()
  worker.start()
  return worker

def init():
  creds = get_auth_creds()
  start_auth_session(creds)


"""changing viewer layer names

Revision ID: 99ebe4492cee
Revises: b0b51fd07bfa
Create Date: 2023-06-12 15:34:29.609937

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '99ebe4492cee'
down_revision = 'b0b51fd07bfa'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('datastack', sa.Column('viewer_layer_name', sa.String(length=100), nullable=True))
    op.add_column('image_source', sa.Column('viewer_layer_name', sa.String(length=100), nullable=True))
    op.drop_column('image_source', 'ngl_layer_name')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('image_source', sa.Column('ngl_layer_name', sa.VARCHAR(length=100), autoincrement=False, nullable=True))
    op.drop_column('image_source', 'viewer_layer_name')
    op.drop_column('datastack', 'viewer_layer_name')
    # ### end Alembic commands ###


# Python built-in modules and packages
from dataclasses import dataclass
from typing import Dict, List, Union, Tuple

# --- Useful type hints ---
TableName = str
DatafieldName = str
PacketType = str
PacketData = Union[int, str, float]
Packet = Dict[DatafieldName, PacketData]
Message = Dict[str, List[Packet]]
RowDB = List[Tuple[DatafieldName, PacketData]]
DbFormat = Dict[str, List[DatafieldName]]

# Database table formats
dbFormats: DbFormat = {
    "gps": [
        "timestamp",
        "date",
        "hour",
        "tbr_serial_id",
        "SLIM_status",
        "longitude",
        "latitude",
        "pdop",
        "FIX",
        "num_sat_tracked",
        "comment",
    ],
    "tbr": [
        "timestamp",
        "date",
        "hour",
        "tbr_serial_id",
        "temperature",
        "temperature_data_raw",
        "noise_avg",
        "noise_peak",
        "frequency",
        "comment",
    ],
    "tag": [
        "timestamp",
        "date",
        "hour",
        "tbr_serial_id",
        "comm_protocol",
        "frequency",
        "tag_id",
        "tag_data",
        "tag_data_raw",
        "tag_data_2",  # DS256
        "tag_data_raw_2",  # DS256
        "snr",
        "millisecond",
        "comment",
    ],
}

@dataclass
class DatabasePacket:
    table: TableName
    columns: Tuple[str]
    values: Tuple[PacketData]
    numOfValues: int = 0
    sql_columns: str = ""
    sql_values: str = ""

    def __post_init__(self):
        self.numOfValues = len(self.values)
        self._handle_sql_columns()
        self._handle_sql_values()

    def _handle_sql_columns(self):
        """
        Uses columns=('col_name_1', 'col_name_2', ..., 'col_name_n')
        to set self.sql_columns='(col_name_1, col_name_2, ..., col_name_3)'
        """
        iterable = iter(self.columns)
        column_names = f"({next(iterable)}"
        while True:
            try:
                column_names += f", {next(iterable)}"
            except StopIteration:
                column_names += ")"
                break
        self.sql_columns = column_names

    def _handle_sql_values(self):
        """
        Uses numOfValues to set self.sql_values='(?, ?, ..., ?)' for safer sql insertion
        """
        self.sql_values = f"({'?, '*(self.numOfValues - 1)}?)"

def convert_msg_to_database_format(msg: Message, msgID: int) -> List[DatabasePacket]:
    dbmsg: List[DatabasePacket] = []
    for i, packet in enumerate(msg.payload):
        type: TableName = packet["packetType"]
        dbformat: List[DatafieldName] = dbFormats[type]
        columns, values = [], []  # type: List[str], List[PacketData]
        for datafield in dbformat:
            if datafield in packet:
                columns.append(datafield)
                values.append(packet[datafield])
        # add message_id to packet
        columns.insert(0, "message_id")
        values.insert(0, msgID)
        dbPacket = DatabasePacket(type, tuple(columns), tuple(values))
        dbmsg.append(dbPacket)
    return dbmsg


import datetime
import time
from playsound import playsound

# using deffunction set the alarm-time
def set_alarm(alarm_time):
    while True:
        current_time = datetime.datetime.now().strftime("%H:%M:%S")
        if current_time == alarm_time:
            print("It's an alarm time")
            print("Play the alarm")
            playsound(r"C:\Users\madhu\Downloads\Alarm.mp3.mp3")
            break
            time.sleep(1)

time_formats = ["%H:%M:%S", "%H:%M"]
print("Assigned time formats:")
for i, time_format in enumerate(time_formats, start=1):
    print(str(i) + " " + time_format)
choice = int(input("Enter your choice:"))

selected_format = time_formats[choice - 1]
alarm_time = input("Enter the alarm time(" + selected_format + "):")
if selected_format == "%H:%M:%S":
    alarm_time = datetime.datetime.strptime(alarm_time, "%H:%M:%S").strftime("%H:%M:%S")
if selected_format == "%H:%M":
    alarm_time = datetime.datetime.strptime(alarm_time, "%H:%M").strftime("%H:%M:%S")
# function call
set_alarm(alarm_time)


import os

from setuptools import setup, find_packages
from version import get_git_version

def files(folder, exclude=[]):
    found_files = []
    for root, dirs, files in os.walk(folder):
        for f in files:
            if not any(("%s/%s" % (root, f)).startswith(e) for e in exclude):
                found_files.append((root, f))
    return found_files

def flatten_all_files(*dirs, **kwargs):
    exclude = kwargs.get('exclude', [])
    root = kwargs.get('root', '')
    all_files = []
    for d in dirs:
        for f in files(d, exclude):
            froot, fnm = f
            prefix_start = froot.find(root)
            assert prefix_start > -1, 'Impossible base root provided. Found files do not match: %s' % root
            all_files.append(("%s" % (froot[prefix_start + len(root):]), ["%s/%s" % (froot, fnm)]))
    return all_files

#
data_files = flatten_all_files('build/Onc/production/',
                    exclude=('build/Onc/production/lib/ext-4.2', 'build/Onc/production/.sass-cache'),
                    root='build/Onc/production/'
              )
data_files.append(('', ['favicon.png', 'beep.wav', 'portal.html']))

setup(
    name="opennode.oms.onc",
    version=get_git_version(),
    description="""OpenNode Console application""",
    author="OpenNode Developers",
    author_email="info@opennodecloud.com",
    packages=find_packages(),
    data_files=data_files,
    namespace_packages=['opennode'],
    zip_safe=False,  # we need to serve real files
    entry_points={'oms.plugins': ['onc = opennode.onc.main:OncPlugin']},
    install_requires=[
        "setuptools",  # Redundant but removes a warning
        "opennode.oms.core",
        "opennode.oms.knot",
        ],
    license='GPLv2',
)


from statistics import mean

n, x = (map(int, input().split()))
ar = []

for i in range(x):
    ar.append(list(map(float, input().split())))

s = zip(*ar)

for i in s:
    print (mean(i))


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

import time
import random

from twisted.internet import task
from twisted.internet.protocol import DatagramProtocol

from demo.main import reactor
from demo.proto import ProtocolParser

tasks = {}
finished_tasks = {}

class ClientProtocol(DatagramProtocol):
    def __init__(self, config):
        self.config = config

    def callback(self):
        task_id = '%s_%i_%i' % (self.config['name'], int(time.time()), random.randint(10, 1000))
        proto = ProtocolParser(self.config['name'], {'task_client_request': True}, task_id)
        tasks[proto.task_id] = time.time()
        print('Send client request %s' % proto.task_id)
        self.transport.write(proto.serialize())

    def startProtocol(self):
        self.transport.connect(self.config['dispatcher_address'], self.config['dispatcher_port'])

    def stopProtocol(self):
        reactor.listenUDP(0, self)

    def datagramReceived(self, datagram, addr):
        proto = ProtocolParser.deserialize(datagram)
        print('Incoming request from %s %s' % addr)
        if proto.task_id is None or proto.task_client_response is False:
            print('Wrong packet')
            return None

        if tasks.get(proto.task_id):
            finished_tasks[proto.task_id] = time.time() - tasks[proto.task_id]
            tasks[proto.task_id] = True
            print('Incoming response %s. Work time is %.2f sec' % (proto.task_id, finished_tasks[proto.task_id]))
        else:
            print('Wrong task %s' % proto.task_id)

def initial(conf):
    # Prepare config
    config = {
        'name': 'client_%i_%i' % (int(time.time()), random.randint(10, 1000)),
        'dispatcher_address': '127.0.0.1',
        'dispatcher_port': 8000,
        'task_send_timeout': 60
    }
    config.update(conf)

    # Watchdog beat
    watchdog = ClientProtocol(config)
    reactor.listenUDP(0, watchdog)
    loop = task.LoopingCall(watchdog.callback)
    loop.start(config['task_send_timeout'])

def report():
    times = finished_tasks.values()
    average = sum(times) / len(times) if times else 0
    not_answered_tasks = len([i for i in tasks.values() if i is not True])

    print('Sent tasks: %d' % len(tasks))
    print('Finished tasks: %d' % len(finished_tasks))
    print('Not answered tasks: %d' % not_answered_tasks)
    print('Minimal task time: %.2f' % (min(times) if times else 0))
    print('Average task time: %.2f' % average)
    print('Maximal task time: %.2f' % (max(times) if times else 0))


from __future__ import print_function, absolute_import

import sys

import PyQt4.QtGui as QtGui
import PyQt4.QtScript as QtScript
from PyQt4.QtCore import SIGNAL

app = QtGui.QApplication(sys.argv)

from qtreactor import pyqt4reactor

pyqt4reactor.install()

class DoNothing(object):
    def __init__(self):
        self.count = 0
        self.running = False

    def button_click(self):
        if not self.running:
            from twisted.scripts import trial
            trial.run()

def run():

    t = DoNothing()

    engine = QtScript.QScriptEngine()

    button = QtGui.QPushButton()
    button = engine.newQObject(button)
    engine.globalObject().setProperty("button", button)

    app.connect(button, SIGNAL("clicked()"), t.button_click)

    engine.evaluate("button.text = 'Do Twisted Gui Trial'")
    engine.evaluate("button.styleSheet = 'font-style: italic'")
    engine.evaluate("button.show()")

    app.exec_()
    print('fell off the bottom?...')


# coding: utf-8
import argparse
import importlib.util
import inspect
import os
from datetime import datetime

problemas = []

def problema(mensaje, *args):
    problemas.append(mensaje.format(*args))

def validar_tiempo(inicio, fin, tope, mensaje):
    diferencia = (fin - inicio).total_seconds()
    if diferencia > tope:
        problema(mensaje)

def probar_codigo(interactivo=False, saltear_errores=False, resultado_verboso=False, grupo=None):
    # dependencias
    try:
        from simpleai.search.models import SearchNode
    except ImportError:
        problema('No se pudo importar SimpleAI. Se encuentra instalado?')
        return

    # intentar importar la entrega
    print('Importando la entrega...')

    try:
        inicio = datetime.now()
        if grupo:
            spec = importlib.util.spec_from_file_location("{}.entrega1".format(grupo),
                                                          "{}/entrega1.py".format(grupo))
            entrega1 = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(entrega1)
        else:
            import entrega1
        fin = datetime.now()
    except ImportError:
        problema('No se pudo encontrar el código python. Probablemente el nombre del archivo .py '
                 'no es correcto, o no está en la raiz del repositorio.')
        return

    validar_tiempo(inicio, fin, 3,
                   'El import de la entrega demora demasiado tiempo, probablemente están '
                   'haciendo búsqueda en el import. Hagan lo del if __name__ ... que se '
                   'recomienda en la consigna.')

    # intentar extraer y validar la funcion resolver
    print('Extrayendo la función resolver...')

    resolver = getattr(entrega1, 'resolver', None)

    if resolver is None:
        problema('El módulo python no define la función resolver.')
        return

    firma_resolver = inspect.getargspec(resolver)
    args = firma_resolver.args
    defaults = firma_resolver.defaults or []
    if args[:len(args) - len(defaults)] != ['metodo_busqueda', 'franceses', 'piratas']:
        problema('La función resolver no recibe los parámetros definidos en la entrega.')
        return

    # validar el funcionamiento de la funcion resolver y el planteo del problema en general
    print('Probando la resolución de problemas...')

    franceses_consigna = (
        (0, 2),
        (0, 3),
        (1, 2),
        (1, 3),
        (2, 1),
        (2, 2),
        (2, 3),
        (3, 0),
        (3, 1),
        (3, 2),
        (4, 0),
        (4, 1),
        (5, 0),
    )
    piratas_consigna = (
        (4, 4),
        (4, 5),
        (5, 4),
    )

    # metodo_busqueda, franceses, piratas, limite_largo_camino, limite_tiempo
    pruebas = (
        # sin franceses, 1 pirata
        ('breadth_first', [], [(4, 4)], 14, 2),
        ('depth_first', [], [(4, 4)], 100, 10),
        ('greedy', [], [(4, 4)], 25, 2),
        ('astar', [], [(4, 4)], 14, 2),

        # 3 franceses, 2 piratas
        ('breadth_first', [(1, 0), (2, 1), (3, 0)], [(3, 4), (4, 4)], 19, 10),
        ('depth_first', [(1, 0), (2, 1), (3, 0)], [(3, 4), (4, 4)], 200, 30),
        ('greedy', [(1, 0), (2, 1), (3, 0)], [(3, 4), (4, 4)], 50, 10),
        ('astar', [(1, 0), (2, 1), (3, 0)], [(3, 4), (4, 4)], 19, 10),

        # caso de la consigna
        ('breadth_first', franceses_consigna, piratas_consigna, 33, 30),
        ('depth_first', franceses_consigna, piratas_consigna, 500, 60),
        ('greedy', franceses_consigna, piratas_consigna, 60, 30),
        ('astar', franceses_consigna, piratas_consigna, 33, 30),
    )

    for numero_prueba, prueba in enumerate(pruebas):
        metodo_busqueda, franceses, piratas, limite_largo_camino, limite_tiempo = prueba
        print('  Prueba', numero_prueba, ':', metodo_busqueda, 'franceses:', franceses, 'piratas:',
              piratas)

        if not interactivo or input('ejecutar? (Y/n)').strip() in ('y', ''):
            try:
                inicio = datetime.now()
                resultado = resolver(metodo_busqueda=metodo_busqueda,
                                     franceses=franceses,
                                     piratas=piratas)
                fin = datetime.now()

                if isinstance(resultado, SearchNode):
                    print('     largo camino:', len(resultado.path()))
                    print('     estado:', resultado.state)
                    print('     acciones:', [accion for accion, estado in resultado.path()])
                    if resultado_verboso:
                        print('     meta:', repr(resultado.state))
                        print('     camino:', repr(resultado.path()))
                else:
                    print('     resultado:', str(resultado))
                print('    duración:', (fin - inicio).total_seconds())

                if resultado is None:
                    problema('El resultado devuelto por la función resolver en la prueba {} fue '
                             'None, cuando el problema tiene que encontrar solución y se espera '
                             'que retorne el nodo resultante. Puede que la función resolver no '
                             'esté devolviendo el nodo resultante, o que el problema no esté '
                             'encontrando solución como debería.',
                             numero_prueba)
                elif isinstance(resultado, SearchNode):
                    if limite_largo_camino and len(resultado.path()) > limite_largo_camino:
                        problema('El resultado devuelto en la prueba {} excede el largo de camino'
                                 'esperable ({}) para ese problema y método de búsqueda. Es '
                                 'posible que algo no esté bien.',
                                 numero_prueba, limite_largo_camino)
                else:
                    problema('El resultado devuelto por la función resolver en la prueba {} no es '
                             'un nodo de búsqueda.',
                             numero_prueba)

                if limite_tiempo is not None:
                    validar_tiempo(inicio, fin, limite_tiempo,
                                   'La prueba {} demoró demasiado tiempo (más de {} segundos), '
                                   'probablemente algo no está demasiado '
                                   'bien.'.format(numero_prueba, limite_tiempo))
            except Exception as err:
                if saltear_errores:
                    problema('Error al ejecutar prueba {} ({})', numero_prueba, str(err))
                else:
                    raise

def probar_estadisticas(grupo=None):
    # abrir el archivo de estadisticas
    print('Abriendo estadísticas...')

    nombre_archivo = 'entrega1.txt'
    if grupo:
        nombre_archivo = os.path.join(grupo,nombre_archivo)

    if not os.path.exists(nombre_archivo):
        problema('No se pudo encontrar el archivo de estadísticas. Probablemente el nombre del '
                 'archivo no es correcto, o no está en la raiz del repositorio.')
        return

    with open(nombre_archivo) as archivo_stats:
        lineas_stats = archivo_stats.readlines()

    # validar contenidos
    casos = list(range(1, 5))
    casos_pendientes = casos[:]

    for linea in lineas_stats:
        linea = linea.strip()
        if linea:
            try:
                caso, valores = linea.split(':')
                caso = int(caso)
                valores = list(map(int, valores.split(',')))
                if len(valores) != 4:
                    raise ValueError()

                if caso not in casos:
                    problema('Caso desconocido en archivo de estadísticas: {}', caso)
                elif caso not in casos_pendientes:
                    problema('Caso repetido en archivo de estadísticas: {}', caso)
                else:
                    print('   Encontrado caso', caso)
                    print('    Valores:', valores)
                    casos_pendientes.remove(caso)
            except:
                problema('La siguiente linea de estadísticas no respeta el formato definido: {}',
                         linea)

    if casos_pendientes:
        problema('No se incluyeron las estadísticas de los siguientes casos: {}',
                 repr(casos_pendientes))

def imprimir_resultados():
    def listar_cosas(titulo, cosas):
        if cosas:
            print(titulo + ':')
            for cosa in cosas:
                print('*', cosa)

    listar_cosas('Problemas que es necesario corregir', problemas)

def probar(interactivo=False, saltear_errores=False, resultado_verboso=False, grupo=None):
    print('#' * 80)
    if grupo:
        print("Probando grupo", grupo)
    probar_codigo(interactivo, saltear_errores, resultado_verboso, grupo)
    print()
    probar_estadisticas(grupo)
    print()
    print('Pruebas automáticas terminadas!')
    print()
    imprimir_resultados()
    print('#' * 80)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', action='store_true', help='Interactivo')
    parser.add_argument('-s', action='store_true', help='Saltear errores')
    parser.add_argument('-v', action='store_true', help='Resultado verboso')
    parser.add_argument('--path', help='Path a la entrega')
    args = parser.parse_args()

    probar(args.i, args.s, args.v, args.path)


import argparse
import torch
import torch.nn as nn
import numpy as np
import os
import pickle
from data_loader import get_loader
from build_vocab import Vocabulary
from model import EncoderCNN, DecoderRNN
from torch.nn.utils.rnn import pack_padded_sequence
from torchvision import transforms

# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def main():
    # Create model directory
    if not os.path.exists('models/'):
        os.makedirs('models/')

    # Image preprocessing, normalization for the pretrained resnet
    transform = transforms.Compose([
        transforms.RandomCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406),
                             (0.229, 0.224, 0.225))])

    # Load vocabulary wrapper
    with open('data/vocab.pkl', 'rb') as f:
        vocab = pickle.load(f)

    # Build data loader
    data_loader = get_loader('data/resized2014', 'data/annotations/captions_train2014.json', vocab,
                             transform, 128,
                             shuffle=True, num_workers=2)

    # Build the models
    encoder = EncoderCNN(256).to(device)
    decoder = DecoderRNN(256, 512, len(vocab), 1).to(device)

    # Loss and optimizer
    criterion = nn.CrossEntropyLoss()
    params = list(decoder.parameters()) + list(encoder.linear.parameters()) + list(encoder.bn.parameters())
    optimizer = torch.optim.Adam(params, lr=0.001)

    # Train the models
    total_step = len(data_loader)
    for epoch in range(5):
        for i, (images, captions, lengths) in enumerate(data_loader):

            # Set mini-batch dataset
            images = images.to(device)
            captions = captions.to(device)
            targets = pack_padded_sequence(captions, lengths, batch_first=True)[0]

            # Forward, backward and optimize
            features = encoder(images)
            outputs = decoder(features, captions, lengths)
            loss = criterion(outputs, targets)
            decoder.zero_grad()
            encoder.zero_grad()
            loss.backward()
            optimizer.step()

            # Print log info
            if i % 10 == 0:
                print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Perplexity: {:5.4f}'
                      .format(epoch, 5, i, total_step, loss.item(), np.exp(loss.item())))

            # Save the model checkpoints
            if (i+1) % 1000 == 0:
                torch.save(decoder, os.path.join(
                    'models/', 'decoder-{}-{}.pkl'.format(epoch+1, i+1)))
                torch.save(encoder, os.path.join(
                    'models/', 'encoder-{}-{}.pkl'.format(epoch+1, i+1)))

if __name__ == '__main__':

    main()


#! /usr/bin/python2.7
import rospy
import datetime
import geometry_msgs.msg

CMD_VEL_TOPIC = "/cmd_vel"

def main():
    rospy.init_node("drive_forward_node")
    drive_speed = rospy.get_param("~drive_speed")
    drive_time = rospy.get_param("~drive_time")

    twist_publisher = rospy.Publisher(CMD_VEL_TOPIC, geometry_msgs.msg.Twist)

    rospy.loginfo(
        "Initializing drive forward node with velocity {drive_speed} m/s and drive time {drive_time} seconds".format(
            drive_speed=drive_speed,
            drive_time=drive_time
        )
    )

    drive_forward_message = geometry_msgs.msg.Twist()
    drive_forward_message.linear.x = drive_speed

    stop_message = geometry_msgs.msg.Twist()
    stop_message.linear.x = 0

    drive_start_time = datetime.datetime.now()
    publish_rate = rospy.Rate(10)

    while not rospy.is_shutdown():
        current_drive_time_seconds = (datetime.datetime.now() - drive_start_time).total_seconds()

        if current_drive_time_seconds > drive_time:
            twist_publisher.publish(stop_message)
            rospy.loginfo("Finished driving. Stopping.")
            break

        twist_publisher.publish(drive_forward_message)
        publish_rate.sleep()

    if not rospy.is_shutdown():
        rospy.loginfo("Terminating drive forward node")

if __name__ == "__main__":
    main()