from copy import deepcopy as dc
import numpy as np
from collections import namedtuple
import beepy
import beepy.Lfp
import beepy.Unit

class Session:
    def __init__(self, fs, ts, x, y, hd=None, vel=None, lfp=None, units=None):
        self.__fs = fs
        self.__raw = {'ts': dc(ts), 'x': dc(x), 'y': dc(y), 'i': np.cumsum(np.ones_like(x)) - 1}
        if hd is not None:
            self.add_custom(hd, 'hd')
        if vel is not None:
            self.add_custom(vel, 'vel')

        self.__active_lfp = None
        self.__active_unit = None

        self.__lfp = lfp
        self.__units = units

        self.epoch = [-np.inf, np.inf]

        self.metadata = {}

        self.__mode = 'cat'

    @property
    def data(self):
        return dc(self.__data)

    @property
    def mode(self):
        return self.__mode

    @mode.setter
    def mode(self, arg):
        if arg == 'cat':
            self.__mode = 'cat'
        elif arg == 'map':
            self.__mode = 'map'
        else:
            print('Mode must be "cat" or "map"')

    def add_custom(self, var, name):
        assert self.__raw['ts'].__len__() == var.__len__(), \
            "Var must be same length as original time sequence: %d" % (self.__raw['ts'].__len__())
        self.__raw[name] = dc(var)

    def align(self):
        ts_beh = self.__raw['ts']

        for lfp in self.__lfp.values():
            lfp.align(ts_beh)

        for clu in self.__units.values():
            if self.__lfp is not None:
                k = list(self.__lfp.keys())[0]
                clu.align(self.__raw, self.__lfp[k]._Lfp__raw)
            else:
                clu.align(self.__raw)

    @property
    def epoch(self):
        return self.__epoch

    @epoch.setter
    def epoch(self, value):
        self.__spk_aligner(epoch=value)

    @property
    def unit(self):
        if self.__active_unit is None:
            return None
        else:
            return self.__units[self.__active_unit]

    @property
    def lfp(self):
        if self.__lfp is None:
            print("No LFP data loaded")
            return None
        elif self.active_lfp is None:
            print("Must set active_lfp")
            return None
        else:
            return self.__lfp[self.active_lfp]

    @lfp.setter
    def lfp(self, value):
        if type(value) == beepy.Lfp.Lfp:
            self.__lfp = value
            self.__lfp.epoch = self.epoch
            self.__lfp.align(self.__raw['ts'])
        elif type(value) == dict:
            for channel in value.values():
                channel.epoch = self.epoch
                channel.align(self.__raw['ts'])
            self.__lfp = value
        else:
            print("Input must be singleton or dict of beepy.LFP.lfp objects")

    @property
    def active_unit(self):
        return self.__active_unit

    @active_unit.setter
    def active_unit(self, value):
        if value in self.__units.keys():
            self.__spk_aligner(unit=value)
        elif value is None:
            self.__spk_aligner(unit=value)
        else:
            print('Note a valid unit key')

    @property
    def active_lfp(self):
        return self.__active_lfp

    @active_lfp.setter
    def active_lfp(self, value):
        if value in self.__lfp.keys():
            self.__spk_aligner(lfp=value)
        elif value is None:
            self.__spk_aligner(lfp=value)
        else:
            print("Not a valid lfp name")

    def __spk_aligner(self, epoch=None, unit=None, lfp=None):
        if epoch is None:
            epoch = self.epoch
        if unit is None:
            unit = self.active_unit
        if lfp is None:
            lfp = self.active_lfp

        if type(epoch[0]) != list:
            epoch = [epoch]

        self.__data = {}
        for k in self.__raw:
            self.__data[k] = [[]] * epoch.__len__()

        for e in np.arange(epoch.__len__()):
            inds = (self.__raw['ts'] > epoch[e][0]) & (self.__raw['ts'] < epoch[e][1])
            for k in self.__raw:
                self.__data[k][e] = self.__raw[k][inds]

        if lfp is not None:
            self.__lfp[lfp].epoch = epoch

        if unit is not None:
            self.__units[unit].align(self.__raw, self.__lfp[self.active_lfp]._Lfp__raw)
            self.__units[unit].epoch = epoch

        self.__epoch = epoch
        self.__active_lfp = lfp
        self.__active_unit = unit

    def __repr__(self):
        s = "BeEpy.Session with: \n"
        s += "  N raw = %d \n" % (self.__raw['ts'].__len__())
        # s += "\n"
        # s += "  N epoched = %d" % (self.__data['ts'].__len__())
        s += "And fields: \n"
        for k in self.__raw.keys():
            s += k
            s += '; '

        return s


from django.urls import path
from rooms import views

app_name='rooms'

urlpatterns =[
  path('auditorium/',views.AuditoriumView.as_view(),name='auditorium'),
  path('conference/',views.ConferenceView.as_view(),name='conference'),
  path('computerlab/',views.ComputerlabView.as_view(),name='computerlab'),
]


def swap(arr, i, j):
    print("Swapping", arr[i], "and", arr[j])
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp

def isSmaller(arr,i,j):
    print("Comparing",arr[i],"and",arr[j])
    if arr[i] < arr[j]:
        return True
    else:
        return False

def bubbleSort(arr):
  for i in range(1, len(arr)):
      for j in range(0, len(arr) - i):
          if isSmaller(arr, j + 1, j):
              swap(arr, j + 1, j)

def printList(arr):
    for i in range(len(arr)):
        print(arr[i], end="\n")
    print()

if __name__ == '__main__':
    arr = []
    n = int(input())
    for i in range(0, n):
        ele = int(input())
        arr.append(ele)
    bubbleSort(arr)
    printList(arr)

3 Dynamic Programming/2 Time and Space Complexity/01 Bubble Sort.py


import json
import requests
import trafilatura
from nltk.tokenize import word_tokenize
from nltk.probability import FreqDist
import concurrent.futures
import string
from bs4 import BeautifulSoup

from const import API_KEY, CONTENT_TYPE, PATTERNS

def fetch_google_search_results(query: str, gl: str, hl: str, num: int = 10) -> dict:
    url = "https://google.serper.dev/search"
    headers = {'X-API-KEY': API_KEY, 'Content-Type': CONTENT_TYPE}
    payload = json.dumps({"q": query, "gl": gl, "hl": hl, "num": num, "autocorrect": False})
    response = requests.post(url, headers=headers, data=payload)
    response = json.loads(response.text)
    return {
        "organic": [item for item in response.get('organic', [])],
        "paa": [item for item in response.get('peopleAlsoAsk', [])],
        "related": [item for item in response.get('relatedSearches', [])],
    }

def get_content(url: str) -> str:
    try:
        return trafilatura.fetch_url(url)
    except:
        print(f"can't get content from url: {url}")
        return None

def get_url_content(url:str) -> str:
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future = executor.submit(get_content, url)
        try:
            content = future.result()
            if content is not None:
                return {
                    "content": content,
                    "url": url,
                    "text": trafilatura.extract(content, include_images=False),
                    "html": trafilatura.extract(content),
                    "title": extract_title_from_html(content),
                    "descr": extract_descr_from_html(content),
                    "headings": extract_headings_from_html(content),
                }
            else:
                print(f"can't get content from url")
                return ""
        except Exception as e:
            print(e)
            print(f"can't get content from url")
            return ""

def get_content_list(urls: list) -> list:
    contents = []
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future_to_url = {executor.submit(get_content, url): url for url in urls}
        for future in concurrent.futures.as_completed(future_to_url):
            url = future_to_url[future]
            try:
                content = future.result()
                if content is not None:
                    contents.append({
                        "content": content,
                        "url": url,
                        "text": trafilatura.extract(content, include_images=False),
                        "html": trafilatura.extract(content),
                        "title": extract_title_from_html(content),
                        "descr": extract_descr_from_html(content),
                        "headings": extract_headings_from_html(content),
                    })
                else:
                    print(f"can't get content from url: {url}")
            except Exception:
                print(f"can't get content from url: {url}")
    return contents

def filter_backlinks(backlinks: list, patterns: list = PATTERNS, offset: int = 0) -> list:
    return [{"index": index + offset + 1, "url": backlinks[index]} for index in range(0, len(backlinks)) if any(pattern in backlinks[index] for pattern in patterns)]

def preprocess_content(contents: list, stop_words: set) -> FreqDist:
    filtered_tokens = [
        token.lower() for token in word_tokenize(contents)
        if token.lower() not in stop_words and token.isalpha()
    ]

    return FreqDist(filtered_tokens)

def remove_punctuation_and_numerics(word_frequencies: FreqDist) -> str:
    for word in list(word_frequencies):
        if word in string.punctuation or not word.isalpha():
            del word_frequencies[word]
    return word_frequencies

def density(most_common_keywords: any, content: str) -> dict:
    return {word: float(content.count(word)/len(content.split(' '))) for word, freq in most_common_keywords}

def extract_content_from_html(html: str) -> str:
    soup = BeautifulSoup(html, 'html.parser')
    return soup.get_text()

def extract_title_from_html(html: str) -> str:
    soup = BeautifulSoup(html, 'html.parser')
    return soup.title.string

def extract_descr_from_html(html: str) -> str:
    try:
        soup = BeautifulSoup(html, 'html.parser')
        return soup.find("meta",  property="og:description").get("content", None)
    except Exception as e:
        print(e)
        return "Pas de description"

def extract_headings_from_html(html: str) -> list:
    soup = BeautifulSoup(html, 'html.parser')
    return [{"type": heading.name, "text": heading.text} for heading in soup.find_all(["h1", "h2", "h3", "h4", "h5", "h6"])]


import logging
from os import system
import logging.config
from time import sleep

from tabulate import tabulate

from helper import (
    Table,
    LogMsg,
    OtherMsg,
    LoadingLogo,
)
from control.class_command import command
from .register import func_register
from .login import func_login
"""
The implementation of the program is from this part, which was written by nested loop
"""

logger = logging.getLogger(__name__)

def main():
    logger.info(LogMsg.start)
    LoadingLogo(OtherMsg.multi_games).show()
    loop_condition_main_page = True
    while loop_condition_main_page:
        # -------------------------MAIN PAGE-------------------------
        system("clear")
        logger.info(LogMsg.main_section)
        main_page = [
                    [Table.description_register],
                    [Table.description_login],
                    [Table.description_help],
                    ]
        print(tabulate(main_page, [Table.main_page.center(35)], tablefmt="heavy_grid"))
        input_command = input(OtherMsg.icon_input).casefold()
        try:
            user_command = command(input_command, ("-login", "-register", "-help", "-exit"))
        except ValueError:
            logger.info(LogMsg.command_report_main.format(input_command))
            print(OtherMsg.valid_command, "'-login' '-register' '-help' '-exit'")
            sleep(0.7)
        else:
            if input_command == "-help":
                logger.info(LogMsg.help_main_section)
                user_command.dash_help()
                input("Press 'enter' on the keyboard to exit -help ")
                system("clear")
            elif input_command == "-exit":
                logger.info(LogMsg.exit_main_section)
                user_command.dash_exit()
                loop_condition_main_page = False
            elif input_command == "-register":
                sleep(0.1)
                func_register(logger)
            elif input_command == "-login":
                sleep(0.1)
                func_login(logger)
    logger.info(LogMsg.end)


from pyitab.utils.math import dot_correlation
from pyitab.analysis.decoding.regression import RoiRegression
from pyitab.analysis.base import Analyzer
from pyitab.preprocessing import SampleSlicer
from pyitab.preprocessing.base import Transformer
from pyitab.ext.sklearn.feature_selection import positive_correlated, \
    negative_correlated

from sklearn.pipeline import Pipeline
from sklearn.feature_selection import SelectFpr, f_regression
from sklearn.preprocessing import FunctionTransformer
from sklearn.svm import SVR

from scipy.io import savemat

import numpy as np
import os
import itertools

import logging
logger = logging.getLogger(__name__)

class Identifiability(Analyzer):

    def fit(self, ds, attr='targets'):

        unique = np.unique(ds.sa[attr].value)
        row, col = np.triu_indices(len(unique), k=0)

        identifiability_matrix = np.zeros((len(unique), len(unique)))
        accuracy_matrix = np.zeros((len(unique), len(unique)))

        task_combinations = itertools.combinations_with_replacement(unique, 2)
        correlation_matrix = dict()
        for j, (t1, t2) in enumerate(task_combinations):
            ds1 = SampleSlicer(**{attr: [t1]}).transform(ds)
            ds2 = SampleSlicer(**{attr: [t2]}).transform(ds)

            r = dot_correlation(ds1.samples, ds2.samples)

            i_self = np.mean(np.diag(r))

            id1 = np.triu_indices(r.shape[0], k=1)
            id2 = np.tril_indices(r.shape[0], k=-1)

            i_diff1 = np.mean(r[id1])
            i_diff2 = np.mean(r[id2])

            identifiability_matrix[row[j], col[j]] = i_self - i_diff1
            identifiability_matrix[col[j], row[j]] = i_self - i_diff2

            prediction1 = np.argmax(r, axis=0)
            prediction2 = np.argmax(r, axis=1)

            accuracy1 = np.count_nonzero(prediction1 == np.arange(r.shape[0]))/r.shape[0]
            accuracy2 = np.count_nonzero(prediction2 == np.arange(r.shape[0]))/r.shape[0]

            accuracy_matrix[row[j], col[j]] = accuracy1
            accuracy_matrix[col[j], row[j]] = accuracy2

            correlation_matrix[t1+'+'+t2] = r

        self.scores = dict()
        self.scores['matrix'] = identifiability_matrix
        self.scores['vars'] = unique
        self.scores['accuracy'] = accuracy_matrix
        self.scores['r'] = correlation_matrix

        self._info = self._store_info(ds, attr=attr)

        return

    def save(self, path=None, scores=None, **kwargs):

        self.name = 'identifiability'
        path, prefix = super().save(path, **kwargs)
        kwargs.update({'prefix': prefix})

        filename = self._get_filename(**kwargs)
        logger.info("Saving %s" % (filename))

        if scores is None:
            scores = self.scores

        savemat(os.path.join(path, filename), scores)

class BehaviouralFingerprint(RoiRegression):
    """This analysis is based on the paper  `Shen et al. 2017, Nature Protocol
    <http://dx.doi.org/10.1038/nprot.2016.178>`_

    The pipeline is used to predict individual behaviour from brain connectivity.

    """

    def __init__(self,
                 estimator=None,
                 n_jobs=1,
                 scoring=['r2'],
                 permutation=0,
                 verbose=1,
                 **kwargs
                 ):

        def fx_transformation(X):
            X_ = np.sum(X, axis=1, keepdims=True)
            return X_

        if estimator is None:

            estimator = Pipeline([
                ('fsel', SelectFpr(f_regression)),
                ('trans', FunctionTransformer(fx_transformation)),
                ('clf', SVR())
            ])
        elif not isinstance(estimator, Pipeline):

            estimator = Pipeline([
                ('fsel', SelectFpr(f_regression)),
                ('trans', FunctionTransformer(fx_transformation)),
                ('clf', estimator)
            ])

        return RoiRegression.__init__(self,
                                      estimator=estimator,
                                      n_jobs=n_jobs,
                                      scoring=scoring,
                                      permutation=permutation,
                                      verbose=verbose,
                                      name='fingerprint_shen',
                                      **kwargs
                                      )

    def fit(self, ds,
            cv_attr='chunks',
            roi='all',
            roi_values=None,
            prepro=Transformer(),
            return_predictions=False,
            return_splits=True,
            return_decisions=False,
            separate_posneg=True,
            **kwargs):

        if not separate_posneg:
            return super().fit(ds, cv_attr, roi, roi_values, prepro,
                               return_predictions, return_splits,
                               return_decisions, **kwargs)

        strategies = {
            'positive': positive_correlated,
            'negative': negative_correlated
        }

        self._scores = dict()
        for i, (s, selection) in enumerate(strategies.items()):
            self.estimator.steps[0] = ('fsel', SelectFpr(selection, alpha=1))
            super().fit(ds, cv_attr, roi, roi_values, prepro,
                        return_predictions, return_splits,
                        return_decisions, **kwargs)
            self._scores[s] = self.scores.copy()

    def save(self, path=None, **kwargs):

        for feature, score in self._scores.items():
            self.scores = score
            super().save(path, feature=feature, **kwargs)


import subprocess
from SUMOenv import SumoEnv
from DQN import DQN, ReplayBuffer
import matplotlib.pyplot as plt
import threading

device = "cpu"
sumo_cfg = "/Users/yuyanlin/Desktop/AdaptiveTrafficLight/Simulator/RunSimulator.sumocfg"

all_total_reward = []

def main():

    env = SumoEnv(sumo_cfg_file=sumo_cfg,
                  simulation_time=50
                  )
    simulation_time = 50
    input_dim = env.observation_space().shape[0]
    output_dim = env.action_space().n
    agent = DQN(env = env,mode="train",input_dim=input_dim,output_dim=output_dim,gamma=0.95,replay_size=20000,batch_size=32,eps_start=0.95,eps_end=0.05,eps_decay=30000,LR=1e-4)
    replay_buffer = ReplayBuffer(agent.replay_size)

    for episode in range(151):
        total_reward = 0
        initial_state = env.reset()

        env.train_state = initial_state
        done = False
        invalid_action = False

        while not done:
            state = env.computeState()
            action = agent.selectAction(state, agent.learn_step_counter, invalid_action)
            next_state, reward, done, info = env.step(env.tl_id[0],action)
            total_reward += reward
            #total_reward = env.traffic_light.total_reward
            if info['do_action'] is None:
                invalid_action = True
                continue
            invalid_action = False
            replay_buffer.push(state, next_state, reward, info['do_action'])
            agent.memory = replay_buffer
            agent.learn()
            #print("each step reward", reward)
        all_total_reward.append(total_reward)
        if episode % 5 ==0:
            #each_episode_return = []
            #each_episode_return.append(all_total_reward[0])
            #for i in range (1,len(all_total_reward)):
                #each_episode_return.append(all_total_reward[i]-all_total_reward[i-1])
            print(all_total_reward)

        env.close()

        print('i_episode:', episode)
        print('learn_steps:', agent.learn_step_counter)
    plt.plot(all_total_reward)
    plt.title('Returns for Cartpole')
    plt.xlabel('episode')
    plt.ylabel('returns')
    plt.savefig('/Users/yuyanlin/Desktop')

print(main())


"""
The team module provides a class (Team) for coordinating units.
"""
class Team(object):
    """
    A Team allows units to obey designations and make reservations.
    """
    def __init__(self):
        self.designations = []
        self.reservations = {
            'entity': [],
            'location': [],
            'mine': [],
            'designation': []
        }
        self.stockpiles = []

    def _assert_is_legal_kind(self, kind):
        assert kind in self.reservations, \
               'illegal reservation kind: %s' % (kind,)

    def reserve(self, kind, obj):
        """
        Make a reservation of a given kind on an object.

        Arguments:
            kind: the kind of reservation
            obj: the object
        """
        self._assert_is_legal_kind(kind)
        assert not self.is_reserved(kind, obj), \
               'tried to reserve already-reserved %s' % (kind,)
        self.reservations[kind].append(obj)

    def relinquish(self, kind, obj):
        """
        Delete a reservation of a given kind on an object.

        Arguments:
            kind: the kind of reservation
            obj: the object
        """
        self._assert_is_legal_kind(kind)
        assert self.is_reserved(kind, obj), \
               'tried to relinquish already-unreserved %s' % (kind,)
        self.reservations[kind].remove(obj)

    def is_reserved(self, kind, obj):
        """
        Return whether a reservation of the given kind is on an object.

        Arguments:
            kind: the kind of reservation
            obj: the object

        Returns: whether the entity is reserved
        """
        self._assert_is_legal_kind(kind)
        return obj in self.reservations[kind]

    def get_unreserved_designations(self, kind):
        """
        Return a list of all unreserved designations of a certain kind.

        Args:
            kind (string): The kind of designation.

        Returns:
            A list of all unreserved designations of the given kind.
        """
        return list(filter(lambda d:
                             d['kind'] == kind
                             and not self.is_reserved('designation', d),
                           self.designations))


# -*- coding: utf-8 -*-
"""
Created on Tue Mar 13 18:31:23 2018

@author: Cristian
"""
from SpeedTyper import lettersToPractice

# Explamples from topcoder
Examples = {'db':["dcab",[250, 300, 400, 800]],
            'bad':["keyboard",[100,200,300,500,600,800,900,1200]],
            'q':["rewq",[500, 1000, 1500, 4000]],
            '':["abc",[2000, 4000, 6000]],
            }

def tester():
    # recorriendo las soluciones de ejemplo
    for key in Examples.keys():
        if lettersToPractice(Examples[key][0],Examples[key][1])==key:
            print("El programador clickeo las letras",
                  Examples[key][0],
                  "que tenían como tiempos",
                  Examples[key][1],
                  "y el programa retorno exitosamente",
                  key,)
            print()
        else:
            print("El programador clickeo las letras",
                  Examples[key][0],
                  "que tenían como tiempos",
                  Examples[key][1],
                  "y el programa retorno fallidamente",
                  lettersToPractice(Examples[key][0],Examples[key][1]),
                  "cuando debio haber retornado",
                  key,)
            print()

tester()


#!/usr/bin/env python3

import os
import rospy
from duckietown.dtros import DTROS, NodeType
from std_msgs.msg import String
from duckietown_msgs.msg import WheelsCmdStamped
from duckietown_msgs.msg import WheelEncoderStamped
from duckietown_msgs.msg import Pose2DStamped
from duckietown_msgs.srv import ChangePattern
from std_msgs.msg import String
import math
import wheel_int
import time

HOST_NAME = os.environ["VEHICLE_NAME"]

def clip_0_2pi(rad):
    return rad % (2 * math.pi)

def clip_npi_pi(rad):
    return (rad + math.pi) % (2 * math.pi) - math.pi

class PilotNode(DTROS):
    def __init__(self, node_name, wheel_integration: wheel_int.WheelPositionIntegration):
        super(PilotNode, self).__init__(node_name=node_name, node_type=NodeType.GENERIC)
        self.sub_left = rospy.Subscriber(f'{HOST_NAME}/left_wheel_encoder_node/tick', WheelEncoderStamped, self.left_callback)
        self.sub_right = rospy.Subscriber(f'{HOST_NAME}/right_wheel_encoder_node/tick', WheelEncoderStamped, self.right_callback)
        self.pub = rospy.Publisher(f'/{HOST_NAME}/wheels_driver_node/wheels_cmd', WheelsCmdStamped, queue_size=10)
        self.pose_pub = rospy.Publisher(f'/{HOST_NAME}/pose2d', Pose2DStamped, queue_size=10)
        self.rate = rospy.Rate(60)  # in Hz
        self.wheel_integration = wheel_integration
        self.speed = 0.6
        self.count = 0
        self.LEFT_TICKS = None
        self.RIGHT_TICKS = None
        self.initial_left = None
        self.initial_right = None

    def change_pattern(self, patternStr):
        rospy.wait_for_service(f'/{HOST_NAME}/led_emitter_node/set_pattern')
        try:
            changePatternSrv = rospy.ServiceProxy(f'/{HOST_NAME}/led_emitter_node/set_pattern', ChangePattern)
            msg = String()
            msg.data = patternStr
            changePatternSrv(msg)
        except rospy.ServiceException as e:
            print('Service request failed')
            print(e)

    def left_callback(self, msg):
        self.wheel_integration.update_left(msg.data, rospy.get_rostime())
        # if not self.initial_left:
        #     self.initial_left = msg.data
        # self.LEFT_TICKS = msg.data - self.initial_left
    def right_callback(self, msg):
        self.wheel_integration.update_right(msg.data, rospy.get_rostime())
        # if not self.initial_right:
        #     self.initial_right = msg.data
        # self.RIGHT_TICKS = msg.data - self.initial_right

    def run(self):
        DISTANCE = 1.25
        while not rospy.is_shutdown():
            t1 = time.time()
            self.change_pattern('RED')
            self.driveForTime(0, 0, 5)

            self.change_pattern('WHITE')
            self.adjustToTargetRotation(0)
            self.driveToPoint(1.25, 0)
            self.adjustToTargetRotation(math.pi / 2)
            self.driveToPoint(1.25, 1.25)
            self.adjustToTargetRotation(math.pi)
            self.driveToPoint(0, 1.25)

            self.change_pattern('RED')
            self.driveForTime(0, 0, 5)

            # state 3
            self.change_pattern('BLUE')
            self.driveToPoint(0, 0)
            self.adjustToTargetRotation(math.pi / 2)

            self.change_pattern('RED')
            self.driveForTime(0, 0, 5)

            self.change_pattern('GREEN')
            self.driveForTime(0.75, 0.25, 6)

            self.driveForTime(0, 0, 5)

            t2 = time.time()

            # print the vehicle position and angle
            x, y, theta = self.wheel_integration.get_state_meters()
            print(f'final position:\nx{x:.3f} y{y:.3f} theta{theta:.3f}')

            print('execution time: ' + str(t2 - t1))

            break
        rospy.signal_shutdown("finished")

    def stop_momentum(self):
        """
        stay still to reduce the momentum of the car to zero after carrying out some movement
        """
        self.driveForTime(0, 0, 0.8)

    def drive(self, left_speed, right_speed):
        msg = WheelsCmdStamped()
        msg.vel_left = left_speed
        msg.vel_right = right_speed
        self.pub.publish(msg)

        x, y, theta = self.wheel_integration.get_state_meters()
        pose = Pose2DStamped()
        pose.header.t = rospy.get_rostime()
        pose.x = x
        pose.y = y
        pose.theta = clip_npi_pi(theta)
        self.pose_pub.publish(pose)
        # print(f'x{x:.3f} y{y:.3f} theta{theta:.3f}, decision: vleft{left_speed:.3f}, vright{right_speed:.3f}')

    def driveForTime(self, left_speed, right_speed, tsec):
        for i in range(int(math.ceil(tsec * 60))):
            self.drive(left_speed, right_speed)
            self.rate.sleep()

    def driveToPoint(self, x, y):
        print(f'f-drive to point {x:.3f} {y:.3f}')
        while True:
            curx, cury, curtheta = self.wheel_integration.get_state_meters()
            dx, dy = x - curx, y - cury
            target_dist = math.sqrt(dx ** 2 + dy ** 2)
            if target_dist < 0.05:  # if within 50 millimeter then we think we are already on the target
                self.drive(0, 0)
                break

            to_target = math.atan2(dy, dx)
            to_adjust = clip_npi_pi(to_target - curtheta)
            OFF_THRESHOLD = 0.9
            if abs(to_adjust) > OFF_THRESHOLD:
                print('angle is off, adjust rotation')
                self.stop_momentum()
                self.adjustRotation(to_adjust)
                self.stop_momentum()
            else:
                speed = self.speed
                q = to_adjust / OFF_THRESHOLD * 0.5
                self.drive(speed * (1 - q), speed * (1 + q))
                self.rate.sleep()
        print('forward complete')

    def driveBackwardToPoint(self, x, y):
        print(f'b-drive to point {x:.3f} {y:.3f}')
        while True:
            curx, cury, curtheta = self.wheel_integration.get_state_meters()
            dx, dy = x - curx, y - cury
            target_dist = math.sqrt(dx ** 2 + dy ** 2)
            if target_dist < 0.05:  # if within 50 millimeter then we think we are already on the target
                self.drive(0, 0)
                break

            to_target = math.atan2(dy, dx)
            to_adjust = (to_target - curtheta) % (math.pi * 2) - math.pi
            OFF_THRESHOLD = 0.9
            if abs(to_adjust) > OFF_THRESHOLD:
                print('angle is off, adjust rotation')
                self.stop_momentum()
                self.adjustRotation(to_adjust)
                self.stop_momentum()
            else:
                speed = self.speed
                q = to_adjust / OFF_THRESHOLD * 0.5
                self.drive(-speed * (1 + q), -speed * (1 - q))
                self.rate.sleep()
        print('backward complete')

    def adjustRotation(self, to_adjust):
        print(f'adjust rotation {to_adjust}')
        curx, cury, curtheta = self.wheel_integration.get_state_meters()
        target_theta = curtheta + to_adjust
        if to_adjust > 0:
            while curtheta < target_theta - 0.21:
                self.drive(-self.speed, self.speed)
                curx, cury, curtheta = self.wheel_integration.get_state_meters()
                self.rate.sleep()
        else:
            while curtheta > target_theta + 0.21:
                self.drive(self.speed, -self.speed)
                curx, cury, curtheta = self.wheel_integration.get_state_meters()
                self.rate.sleep()
        print('adjust complete')
        self.drive(0, 0)

    def adjustRotationWhileDriving(self, to_adjust):
        curx, cury, curtheta = self.wheel_integration.get_state_meters()
        target_theta = curtheta + to_adjust
        if to_adjust > 0:
            while curtheta < target_theta:
                self.drive(self.speed * 0.5, self.speed * 1.5)
                curx, cury, curtheta = self.wheel_integration.get_state_meters()
                self.rate.sleep()
        else:
            while curtheta > target_theta:
                self.drive(self.speed * 1.5, self.speed * 0.5)
                curx, cury, curtheta = self.wheel_integration.get_state_meters()
                self.rate.sleep()
        self.drive(0, 0)

    def adjustToTargetRotation(self, adjust_target_radian):
        curx, cury, curtheta = self.wheel_integration.get_state_meters()
        to_adjust = ((adjust_target_radian - curtheta) + math.pi) % (math.pi * 2) - math.pi
        if abs(to_adjust) > 0.05:
            self.adjustRotation(to_adjust)

    def driveForDistance(self, distance):
        curx, cury, curtheta = self.wheel_integration.get_state_meters()

        targetx, targety = curx + distance * math.cos(curtheta), cury + distance * math.sin(curtheta)
        self.driveToPoint(targetx, targety)
        self.stop_momentum()
        self.adjustToTargetRotation(curtheta)  # make sure the rotation is the same as initial rotation

    def driveBackwardForDistance(self, distance):
        curx, cury, curtheta = self.wheel_integration.get_state_meters()

        targetx, targety = curx - distance * math.cos(curtheta), cury - distance * math.sin(curtheta)
        self.driveBackwardToPoint(targetx, targety)
        self.stop_momentum()
        self.adjustToTargetRotation(curtheta)  # make sure the rotation is the same as initial rotation

if __name__ == '__main__':
    try:
        print(f'running on robot {HOST_NAME}')
        node = PilotNode('my_pilot_node', wheel_int.WheelPositionIntegration(33, 0, 0, math.pi / 2))
        node.run()
        rospy.spin()
    except rospy.ROSInterruptException:
        pass


from selenium import webdriver
from time import sleep
from PIL import Image
from pytesseract import image_to_string
'''
    If you have a problem with geckodriver
    1.Install geckodriver from this link https://github.com/mozilla/geckodriver/releases
    2.
    For Windows:
    https://stackoverflow.com/questions/42524114/how-to-install-geckodriver-on-a-windows-system
    For Linux:
    https://askubuntu.com/questions/870530/how-to-install-geckodriver-in-ubuntu
    For MacOS:
    https://www.kenst.com/2016/12/installing-marionette-firefoxdriver-on-mac-osx/
'''

class Bot:
    def __init__(self):
        self.driver = webdriver.Firefox(executable_path='C:/geckodriver.exe')
        self.navigate()

    def take_skreenshot(self):
        self.driver.save_screenshot('avito.png')

    def take_recon(self):
        image = Image.open('tel.gif')
        print(image_to_string(image))

    def crop(self, location, size):
        image = Image.open('avito.png')
        x = location['x']
        y = location['y']
        width = size['width']
        height = size['height']

        image.crop((x, y, x+width, y+height)).save('tel.gif')
        self.take_recon()

    def navigate(self):
        self.driver.get('https://www.avito.ru/moskva/kvartiry/2-k._kvartira_396_m_1116_et._1746055343')

        blocks = self.driver.find_element_by_xpath('//a[@class="button item-phone-button js-item-phone-button button-origin contactBar_greenColor button-origin_full-width button-origin_large-extra item-phone-button_hide-phone item-phone-button_card js-item-phone-button_card contactBar_height"]')
        blocks.click()
        sleep(3)
        self.take_skreenshot()
        img = self.driver.find_element_by_xpath('//div[@class="item-phone-big-number js-item-phone-big-number"]//*')
        location = img.location         # dict {'x': 131, 'y': 521}
        size = img.size                 # dict {'width': 14412, 'height': 1421}

        self.crop(location, size)

def main():
    class_bot = Bot()

if __name__ == '__main__':
    main()


## @file
## @brief meta: custom CentOS 7 installer

from metaL import *

p = Project(
    title='''custom CentOS 7 installer''',
    about='''
* massive retail installer

* http://www.smorgasbork.com/2014/07/16/building-a-custom-centos-7-kickstart-disc-part-1/
''')

p.dev \
    // 'build-essential' \
    // 'syslinux isolinux xorriso xorriso-tcltk' \
    // 'virtualbox-6.1'

p.mk.dir // f'{"FW":<7} = $(CWD)/firmware' // f'{"GZ":<7} = $(CWD)/gz'

p.fw = Dir('firmware'); p.d // p.fw; p.fw // (giti() // '*')
p.gz = Dir('gz'); p.d // p.gz; p.gz // (giti() // '*')

p.mk.package \
    // (Sec()
        // f'{"CENTOS_BUILD":<13} = 2009'
        // f'{"CENTOS_VER":<13} = 7.9.$(CENTOS_BUILD)'
        // f'{"CENTOS_MIRROR":<13} = https://mirror.yandex.ru/centos/$(CENTOS_VER)/isos/x86_64'
        // f'{"CENTOS":<13} = CentOS-7-x86_64'
        )
p.mk.package \
    // (Sec(pfx='')
        // f'{"NETINST":<13} = $(CENTOS)-NetInstall-$(CENTOS_BUILD)'
        // f'{"NETINST_ISO":<13} = $(NETINST).iso'
        // f'{"NETINST_TORR":<13} = $(NETINST).torrent'
        // f'{"NETINST_URL":<13} = $(CENTOS_MIRROR)/$(NETINST_TORR)'
        )

p.mk.gz.value += ' $(GZ)/$(NETINST_TORR)'
p.mk.gz_ \
    // (Sec(pfx='')
        // (S('$(GZ)/$(NETINST_TORR):') // '$(CURL) $@ $(NETINST_URL)')
        )

Mod().syslinux(p)

p.sync()


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

from MLFeatureSelection import FeatureSelection as FS
from sklearn.metrics import log_loss
import lightgbm as lgbm
import pandas as pd
import numpy as np

def loadCSV(path=''):
    return pd.read_csv(path)

def prepareData():
    """prepare you dataset here"""
    '''
    train = loadCSV('data/cutData/train/train_20180425_merge_sel.csv')
    val = loadCSV('data/cutData/validate/validate_20180425_merge.csv')
    train.drop_duplicates(subset='instance_id')
    train.pop('instance_id')
    val.pop('instance_id')
    train = train.fillna(-1)
    val = val.fillna(-1)
    feature_name = val.columns.values.tolist()
    del_features = ['realtime','day','context_id']
    feat_import = loadCSV('model/dif_on_feature_mean_4_25.csv')
    del_features += list(feat_import['feature'].values[:43])
    del_features = list(set(del_features))

    for c in del_features:
      if c in feature_name:
        del train[c]
        del val[c]

    del train['context_timestamp']

    train['falg'] = 0;
    val['falg'] = 1;
    df = pd.concat([train,val],ignore_index=True)
    print('CSV加载完成')
    return df
    '''
    data = loadCSV('data/cutData/validate/validate_20180425_merge.csv')
    data.drop_duplicates(subset='instance_id')
    data.pop('instance_id')
    data = data.fillna(-1)

    feature_name = data.columns.values.tolist()
    del_features = ['realtime','day','context_id']
    feat_import = loadCSV('model/dif_on_feature_mean_4_25.csv')
    del_features += list(feat_import['feature'].values[:43])
    del_features = list(set(del_features))

    for c in del_features:
      if c in feature_name:
        del data[c]
    print('CSV加载完成')
    return data

def modelscore(y_test, y_pred):
    """set up the evaluation score"""
    return log_loss(y_test, y_pred)

def validation(X,y,features, clf,lossfunction):
    """set up your validation method"""
    totaltest = 0
    T = (X.context_timestamp >'2018-09-06 23:59:59')
    X_train, X_test = X[~T], X[T]
    X_train, X_test = X_train[features], X_test[features]
    y_train, y_test = y[~T], y[T]
    clf.fit(X_train,y_train, eval_set = [(X_train, y_train), (X_test, y_test)], eval_metric='logloss', verbose=False,early_stopping_rounds=50)
    totaltest += lossfunction(y_test, clf.predict_proba(X_test)[:,1])
    totaltest /= 1.0
    return totaltest

def add(x,y):
    return x + y

def substract(x,y):
    return x - y

def times(x,y):
    return x * y

def divide(x,y):
    return (x + 0.001)/(y + 0.001)

CrossMethod = {'+':add,
               '-':substract,
               '*':times,
               '/':divide,}

def main():
    sf = FS.Select(Sequence = False, Random = True, Cross = False) #select the way you want to process searching
    sf.ImportDF(prepareData(),label = 'is_trade')
    sf.ImportLossFunction(modelscore,direction = 'descend')
    sf.ImportCrossMethod(CrossMethod)
    sf.InitialNonTrainableFeatures(['context_timestamp', 'is_trade'])

    features_import = loadCSV('model/dif_on_feature_mean_4_25.csv')
    initial_features = list(features_import['feature'].values)[::-1][:100]
    sf.InitialFeatures(initial_features)
    sf.clf = lgbm.LGBMClassifier(max_depth=7,
                        n_jobs=60,
                        num_leaves=64,
                        seed=1080,
                        learning_rate=0.05,
                        n_estimators=400,
                        colsample_bytree = 0.8,
                        subsample = 0.8)

    sf.SetLogFile('./model/record_100_7_50.log')
    sf.run(validation)

if __name__ == "__main__":
    main()


from torch.utils.data import DataLoader,BatchSampler,RandomSampler
import torch
import numpy as np

class ClassAlignLoader(object):
    def __init__(self, dataset, num_selected_classes, num_workers, collate_fn):
        self.dataset = dataset
        self.num_selected_classes = num_selected_classes
        self.num_workers = num_workers
        self.collate_fn = collate_fn
        batch_sampler = BatchSampler(RandomSampler(self.dataset), self.num_selected_classes, drop_last=False)
        self.dataloader = torch.utils.data.DataLoader(self.dataset, batch_sampler=batch_sampler,
                                                      collate_fn=self.collate_fn, num_workers=self.num_workers)

    def construct(self, dict):
        self.dataset.consturct(dict)
        batch_sampler = BatchSampler(RandomSampler(self.dataset), self.num_selected_classes, drop_last=False)
        self.dataloader = torch.utils.data.DataLoader(self.dataset,
                                                      batch_sampler=batch_sampler,
                                                      collate_fn=self.collate_fn,
                                                      num_workers=self.num_workers)

    def __iter__(self):
        return iter(self.dataloader)

    def __len__(self):
        dataset_len = 0.0
        for c in range(self.dataset.config["class_num"]):
            c_len = max([len(self.dataset.p2ls[i][c])//self.dataset.bts[i][c] for i in range(2)])
            dataset_len += c_len

        dataset_len = np.ceil(1.0*dataset_len/self.num_selected_classes)
        return dataset_len


#!/usr/bin/env python3.3

"""The main socrates script (i.e., the one users should invoke from
the command line).
"""

import os
import sys
import datetime

import cmdline
import util
import criteria
import config
import hooks

# conditions that are deemed "okay" to be returned by a subprocess when
# socrates is run in batch mode
OKAY_CONDITIONS = [util.EXIT_WITH_MISSING, util.ERR_GRADE_FILE_EXISTS,
                   util.EXIT_WITH_DEFER]

CRITERIA_FILE_PATTERN = r'^[a-z]+\d+[a-z]$'

def main(args):
    """The function invoked when socrates starts from the command line."""
    if args.mode == 'config':
        _config()

    if args.mode == 'edit':
        _edit(args)

    if args.mode in ['grade', 'submit', 'batch']:
        try:
            sname, group = _parse_assignment_name(args.assignment_with_group)
        except ValueError as err:
            util.error(str(err))
            util.exit(util.ERR_BAD_ASSIGNMENT, traceback=False)

        criteria_path = _form_criteria_path(sname, group)

        if not os.path.isfile(criteria_path):
            message = "could not find criteria file for {}"
            message = message.format(sname)

            if group:
                message += ", group " + group

            util.error(message)
            util.exit(util.ERR_CRITERIA_MISSING)

        criteria_object = _create_criteria_object(criteria_path)

        assignment_name, group = criteria_object.name, criteria_object.group
        grade_filename = assignment_name
        if group:
            grade_filename += group
        grade_filename += '-grade.txt'

        if args.mode == 'grade':
            _grade(args, criteria_object, grade_filename)

        elif args.mode == 'submit':
            _submit(args, criteria_object, grade_filename)

        elif args.mode == 'batch':
            _batch(args)

def _config():
    """Handles 'config' mode."""
    from inspect import getmembers, ismodule

    for member_name, member_val in getmembers(config):
        if member_name[0] != '_' and not ismodule(member_val):
            util.print("{}: {}".format(member_name, member_val))

def _edit(args):
    """Handles 'edit' mode. This function creates a temporary file and
    tries to use the user's default editor to edit the specified criteria
    file. If the new file has a syntax error or is missing essential
    components, the user is prompted to edit again or stop.
    """
    import tempfile
    import shutil
    import filecmp
    import subprocess

    import criteria
    from prompt import prompt

    isfile = os.path.isfile

    try:
        short_name, group = _parse_assignment_name(args.assignment_with_group)
    except ValueError as err:
        util.error(str(err))
        util.exit(util.ERR_BAD_ASSIGNMENT, traceback=False)

    existing_path = _form_criteria_path(short_name, group)

    if not isfile(existing_path):
        util.warning("the file '{}' does not exist".format(existing_path))

        choices = ["create and edit this criteria file now",
                   "exit without creating anything"]

        selections = prompt(choices, mode='1')
        if 1 in selections:
            util.info("exiting without changing anything")
            return

    temp_file, temp_path = tempfile.mkstemp(suffix='.yml')

    # copy contents of existing file into temp file
    if isfile(existing_path):
        shutil.copyfile(existing_path, temp_path)

    while True:
        # try to open editor with file
        _edit_file(temp_path)

        try:
            _ = criteria.Criteria.from_yaml(temp_path)

        except Exception as e:
            util.error("proposed criteria file produced an error")
            util.print_traceback()

            choices = ["return to the editor to fix the problem",
                       "abort the change (existing file will not change)"]

            selections = prompt(choices, mode='1')

            if 0 in selections:
                continue
            else:
                util.info("exiting without changing anything")
                break

        else:
            # temporary file contains valid, new version of the criteria file;
            # if the temporary file is any different, we will copy it over
            # the old file

            if isfile(existing_path) and \
               filecmp.cmp(temp_path, existing_path, shallow=False):
                # files seem to be the same
                util.info("no changes made; exiting without changing original")
                os.remove(temp_path)

            else:
                # files are different; copy over the old file
                shutil.copyfile(temp_path, existing_path)

                # remove temporary file
                os.remove(temp_path)

                util.info("{} updated".format(existing_path))

            break

def _grade(args, criteria_object, grade_filename):
    """Handles 'grade' mode. Most of the work is handed off
    to the 'grader' module, which runs interactively.
    """
    import grader
    from prompt import prompt

    if not args.overwrite and os.path.isfile(grade_filename):
        util.error("refusing to overwrite existing grade file")
        util.exit(util.ERR_GRADE_FILE_EXISTS, hooks=False)

    util.ui_start()

    if not args.submission_files:
        util.warning("no submission files specified")

    hooks.run_hooks_for('before_file_search')

    any_missing = grader.grade(criteria_object, args.submission_files,
                               grade_filename,
                               assume_missing=args.assume_missing,
                               late_check=False if args.no_late else True)

    if not args.no_edit:
        util.info("please review the following grade file ({}) "
                  "for issues".format(grade_filename))

        with open(grade_filename) as f:
            util.print(f.read())

        choices = ["edit the grade file now", "do not edit the grade file"]
        selections = prompt(choices, mode='1')

        if 0 in selections:
            _edit_file(grade_filename)

    hooks.run_hooks_for('before_exit')

    util.ui_stop()

    util.info("grading completed")

    if any_missing:
        util.exit(util.EXIT_WITH_MISSING)

def _submit(args, criteria_object, grade_filename, umask=0o002):
    """Handles 'submit' mode. Allows a grader to send completed grade files
    to the "dropbox" directory.
    """
    import shutil

    os.umask(umask)

    num_submitted = 0

    # args.submission_dirs should contain a list of directories whose names
    # are student user names
    for username in args.submission_dirs:
        if username[-1] == os.sep:
            username = username[:-1]            # removes ending '/'

        # check if specified file is a directory
        if not os.path.isdir(username):
            util.error("'{}' is not a directory".format(username))
            continue

        grade_file_path = username + os.sep + grade_filename
        if not os.path.isfile(grade_file_path):
            util.error("not submitting '{}': directory has no "
                       "grade file".format(username))
            continue

        # TODO check if directory name is a valid student name

        dest_path = config.dropbox_dir + os.sep + username

        # if this is the first time this user's grades are being submitted,
        # we may need to create the directory in the dropbox
        try:
            os.umask(0o000)
            util.makedirs(dest_path)
        except OSError:
            util.error("error making user directory in dropbox")
            util.exit(util.ERR_DROPBOX_MAKEDIRS)

        dest_path += os.sep + grade_filename
        shutil.copyfile(grade_file_path, dest_path)
        os.chmod(dest_path, 0o666)

        num_submitted += 1

        util.info("wrote '{}' to dropbox".format(dest_path))

    util.info("submitted {} {}".format(num_submitted,
                                       util.plural('grade', num_submitted)))

def _batch(args):
    """Handles 'batch' mode."""
    import inspect
    import subprocess

    # absolute path of the current running Python script
    proc = os.path.abspath(inspect.getfile(inspect.currentframe()))

    if not args.submission_dirs:
        util.warning("no submissions specified")

    for subdir in args.submission_dirs:
        if not os.path.isdir(subdir):
            util.error("invalid submission directory '{}'".format(subdir))
            continue

        os.chdir(subdir)

        # this will simulate a user executing socrates grade * at a shell
        files_here = os.listdir(os.curdir)

        util.info("running socrates in '{}'".format(subdir))

        sub_args = [proc, 'grade']

        if args.no_edit:
            sub_args.append("--no-edit")

        if args.assume_missing:
            sub_args.append("--assume-missing")

        if args.no_late:
            sub_args.append("--no-late")

        sub_args.append(args.assignment_with_group)

        try:
            return_val = subprocess.call(sub_args + files_here)

        except KeyboardInterrupt:
            util.warning("parent stopping (received interrupt)")
            util.warning("stopped while grading '{}'".format(subdir))
            os.chdir(os.pardir)
            util.exit(util.ERR_INTERRUPTED)

        if return_val != 0 and return_val not in OKAY_CONDITIONS:
            util.error("child process encountered an error")
            os.chdir(os.pardir)
            util.exit(return_val)

        util.info("completed subdirectory '{}'".format(subdir))
        os.chdir(os.pardir)

def _parse_assignment_name(short_name_with_group):
    """Given a short assignment name with a group (e.g., "ps4a"),
    return the assignment's short name ("ps4") and the group
    ("a") in a tuple.
    """
    from re import match

    sng = short_name_with_group

    m = match(CRITERIA_FILE_PATTERN, sng)
    if not m:
        raise ValueError("invalid assignment name '{}'".format(sng))

    return short_name_with_group[:-1], short_name_with_group[-1]

def _form_criteria_path(short_name, group):
    """Given an assignment's short name (e.g., "ps4"), and optionally a
    group, use the configuration file data to form a path to the appropriate
    criteria file for the assignment. The path returned by this function
    starts with config.criteria_dir, which is given by the socrates
    configuration file.
    """
    criteria_path = config.criteria_dir + os.sep + short_name
    if group:
        criteria_path += os.sep + short_name + group + '.yml'
    else:
        criteria_path += '.yml'

    return criteria_path

def _create_criteria_object(criteria_path):
    """Given a path to a valid criteria file, return a Criteria object
    using that criteria file as a source. This function relies on the
    'criteria' module and its Criteria class. Calling that class'
    constructor is pretty costly.
    """
    try:
        criteria_object = criteria.Criteria.from_yaml(criteria_path)

    except FileNotFoundError:
        util.error("criteria file does not exist")
        util.exit(util.ERR_CRITERIA_MISSING)

    except IsADirectoryError:
        util.error("specified criteria is a directory")
        util.exit(util.ERR_CRITERIA_IMPORT)

    return criteria_object

def _get_dropbox_path(args):
    """Given valid arguments from a user, use the configuration file data
    and the assignment specified on the command line to form a path to the
    grading "dropbox", where final grade files should be saved. The path
    returned by this function starts with config.dropbox_dir, which is given
    by the socrates configuration file.
    """
    short_name = args.assignment[0]
    dropbox_path = config.dropbox_dir + os.sep + short_name

    if not os.path.isdir(dropbox_path):
        util.error("no dropbox for assignment '{}'".format(short_name))
        util.exit(util.ERR_BAD_DROPBOX)

    return dropbox_path

def _edit_file(path):
    """Attempts to consult the $EDITOR environment variable for the user's
    favorite text editor (otherwise trying to use vi) to edit the file
    specfied by the given path. socrates will wait for the editor to exit
    before continuing.
    """
    import os
    import subprocess
    import shutil

    if 'EDITOR' in os.environ:
        # run $EDITOR with the grade file
        subprocess.call([os.environ['EDITOR'], path])

    elif shutil.which('nano') is not None:
        # try to run nano
        subprocess.call(['nano', path])
        util.info("your $EDITOR environment variable is not set")
        util.info("if you set $EDITOR, socrates will use that editor")

    else:
        util.error("could not open your favorite text editor or nano")
        util.exit(util.ERR_NO_EDITOR)

if __name__ == '__main__':
    main(cmdline.get_args())


from pathlib import Path
import joblib

import ahocorasick

A = ahocorasick.Automaton()

datafile=Path('../data/words_cui.tsv')
with open(datafile,"r") as source:
    for line in source:
        word,cui=line.strip().split('\t')
        A.add_word(word, (cui, word))

A.make_automaton()
print(A.get_stats())

joblib.dump(A,"./automata/automata_syns_py.pkl.bz2")


num = int(input('Digite um numero: '))
tot = 0
for c in range(1, num + 1):
    if num % c == 0:
        print('*', end='')
        tot += 1
    print('{} '.format(c), end='')
print('  São primos um total de {} números!'.format(tot))

# num = int(input('Digite um numero: '))
# tot = 0
# for c in range(1, num + 1):
#       if num % c == 0:
#           print('\033[33m', end='')
#           tot += 1
#       else:
#           print('\033[31m', end='')
#       print('{} '.format(c), end='')
# print('\n\033[m0 numero {} foi divisivel {} vezes'.format(num, tot))
# if tot == 2:
#       print('E por isso ele é primo!')
# else:
#       print('E por isso não é primo!!')


import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.ops

from ..backbones.basic import ConvUnit
from ..backbones.resnet import ResNet50
from ..backbones.mobilenet import MobileNetV3L
from .components.fpn import FeaturePyramidNetwork
from .operations.anchor import get_priors, make_anchors
from .operations.bbox import clamp_to_canvas, convert_to_cwh, decode_boxes, remove_small, scale_boxes
from .operations.loss import calc_losses, get_losses, match_with_targets
from .operations.post import get_lvidx, final_nms
from .operations.prep import preprocess, prep_targets
from .operations.roi import roi_align_multilevel
from ..utils.weights import load_weights

class RegionProposalNetwork(nn.Module):

    def __init__(self, c, num_anchors, conv_depth):
        super().__init__()
        self.conv = nn.Sequential(*[ConvUnit(c, c, 3, 1, 1, 'relu', None) for _ in range(conv_depth)])
        self.log = nn.Conv2d(c, num_anchors, 1, 1)
        self.reg = nn.Conv2d(c, num_anchors * 4, 1, 1)

    def head(self, x):
        n = x.shape[0]
        x = self.conv(x)
        reg = self.reg(x).permute(0, 2, 3, 1).reshape(n, -1, 4)
        log = self.log(x).permute(0, 2, 3, 1).reshape(n, -1, 1)
        return reg, log

    def filt_dec(self, regs, logs, priors, lvtop):
        res = []
        n = regs[0].shape[0]
        for reg, log, p in zip(regs, logs, priors):
            log, top = log.topk(min(lvtop, log.shape[1]), dim=1)
            reg = reg.gather(1, top.expand(-1, -1, 4))
            pri = p.expand(n, -1, -1).gather(1, top.expand(-1, -1, 4))
            boxes = decode_boxes(reg, pri, mults=(1, 1), clamp=True)
            res.append((boxes, log, log.shape[1]))
        return map(list, zip(*res))

    def forward(self, fmaps, priors, imsizes, cfg, gtboxes=None):
        lvtop = cfg['lvtop'] if not self.training else cfg['lvtop_train']
        imtop = cfg['imtop1'] if not self.training else cfg['imtop1_train']

        tuples = [self.head(x) for x in fmaps]
        regs, logs = map(list, zip(*tuples))

        dregs = [x.detach() for x in regs]
        dlogs = [x.detach() for x in logs]
        boxes, logits, lvlen = self.filt_dec(dregs, dlogs, priors, lvtop)
        boxes = torch.cat(boxes, axis=1)
        obj = torch.cat(logits, axis=1).sigmoid()

        n, dim = boxes.shape[:2]
        boxes, obj = boxes.reshape(-1, 4), obj.flatten()
        idx = torch.nonzero(obj >= cfg['score_thr1']).squeeze()
        boxes, obj = boxes[idx], obj[idx]
        imidx = idx.div(dim, rounding_mode='floor')

        boxes = clamp_to_canvas(boxes, imsizes, imidx)
        boxes, obj, idx, imidx = remove_small(boxes, cfg['min_size1'], obj, idx, imidx)
        groups = imidx * 10 + get_lvidx(idx % dim, lvlen)
        keep = torchvision.ops.batched_nms(boxes, obj, groups, cfg['iou_thr1'])
        keep = torch.cat([keep[imidx[keep] == i][:imtop] for i in range(n)])
        boxes, imidx = boxes[keep], imidx[keep]

        if not self.training:
            return boxes, imidx
        else:
            regs = torch.cat(regs, axis=1)
            logs = torch.cat(logs, axis=1)
            priors = torch.cat(priors)
            loss_obj, loss_reg = get_losses(gtboxes, None, priors, regs, logs, matcher=(0.3, 0.7, True),
                                            sampler=(256, 0.5), types=('ce_bin', 'l1_s'), avg_divs='always_all')
            return boxes, imidx, (loss_obj, loss_reg)

class RoIProcessingNetwork(nn.Module):

    def __init__(self, c, roi_map_size, clin, cfg):
        super().__init__()
        self.conv = nn.ModuleList([ConvUnit(c, c, 3, 1, 1, 'relu') for _ in range(cfg['roi_convdepth'])])
        c1 = c * roi_map_size ** 2
        self.fc = nn.ModuleList([nn.Linear(c1 if i == 0 else clin, clin) for i in range(cfg['roi_mlp_depth'])])
        self.cls = nn.Linear(clin, 1 + cfg['num_classes'])
        self.reg = nn.Linear(clin, cfg['num_classes'] * 4)
        self.ralign_set = cfg['roialign_settings']
        self.bckg_first = cfg['bckg_class_first']

    def heads(self, x):
        if self.conv:
            for layer in self.conv:
                x = layer(x)
        x = x.flatten(start_dim=1)
        if self.fc:
            for mlp in self.fc:
                x = F.relu(mlp(x))
        a = self.reg(x)
        b = self.cls(x)
        return a, b

    def forward(self, proposals, imidx, fmaps, fmaps_strides, imsizes, cfg, gtb=None, gtl=None):
        if self.training:
            # to list
            proposals = [proposals[imidx == i] for i in range(len(gtb))]
            proposals = [torch.cat([p, b.to(torch.float32)]) for p, b in zip(proposals, gtb)]
            # main
            targets, labels, sidx = match_with_targets(gtb, gtl, proposals, 0.5, 0.5, False, 512, 0.25)
            proposals = [p[sampled] for p, sampled in zip(proposals, sidx)]
            # back to joined
            imidx = [torch.full([len(p)], i) for i, p in enumerate(proposals)]
            proposals, imidx = [torch.cat(x) for x in [proposals, imidx]]

        roi_maps = roi_align_multilevel(proposals, imidx, fmaps, fmaps_strides, self.ralign_set)
        reg, log = self.heads(roi_maps)
        reg = reg.reshape(reg.shape[0], -1, 4)

        if self.training:
            labels = torch.cat(labels)
            targets = torch.cat(targets)
            loss_cls = F.cross_entropy(log, labels)
            reg = reg[labels > 0, labels[labels > 0] - 1] # minus 1 because we removed the bckg class
            loss_reg = F.smooth_l1_loss(reg, targets, beta=1/9, reduction='sum') / labels.numel()
            return loss_cls, loss_reg

        scr = F.softmax(log, dim=-1)
        cls = torch.arange(log.shape[1], device=log.device).view(1, -1).expand_as(log)
        scr = scr[:, :-1] if not self.bckg_first else scr[:, 1:]
        cls = cls[:, :-1] if not self.bckg_first else cls[:, 1:]

        n = torch.max(imidx).item() + 1
        dim = reg.shape[1]
        reg, scr, cls = reg.reshape(-1, 4), scr.flatten(), cls.flatten()
        fidx = torch.nonzero(scr > cfg['score_thr2']).squeeze()
        reg, scr, cls = reg[fidx], scr[fidx], cls[fidx]
        idx = fidx.div(dim, rounding_mode='floor')
        proposals, imidx = proposals[idx], imidx[idx]

        proposals = convert_to_cwh(proposals, in_place=True)
        boxes = decode_boxes(reg, proposals, mults=(0.1, 0.2), clamp=True)
        boxes = clamp_to_canvas(boxes, imsizes, imidx)
        boxes, scr, cls, imidx = remove_small(boxes, cfg['min_size2'], scr, cls, imidx)
        b, s, c = final_nms(boxes, scr, cls, imidx, n, cfg['iou_thr2'], cfg['imtop2'])
        return b, s, c

class FasterRCNN(nn.Module):

    thub = 'https://download.pytorch.org/models/'
    mmhub = 'https://download.openmmlab.com/mmdetection/v2.0/'
    links = {
        'tv_resnet50_v1': thub + 'fasterrcnn_resnet50_fpn_coco-258fb6c6.pth',
        'tv_resnet50_v2': thub + 'fasterrcnn_resnet50_fpn_v2_coco-dd69338a.pth',
        'tv_mobilenetv3l_hires': thub + 'fasterrcnn_mobilenet_v3_large_fpn-fb6a3cc7.pth',
        'tv_mobilenetv3l_lores': thub + 'fasterrcnn_mobilenet_v3_large_320_fpn-907ea3f9.pth',
        'mm_resnet50': mmhub + 'faster_rcnn/faster_rcnn_r50_fpn_mstrain_3x_coco/'\
                               'faster_rcnn_r50_fpn_mstrain_3x_coco_20210524_110822-e10bd31c.pth',
        'mm_resnet50_animefaces': 'https://github.com/hysts/anime-face-detector/'\
                                  'releases/download/v0.0.1/mmdet_anime-face_faster-rcnn.pth'
    }

    def tv_conversion(self, wd):
        # TorchVision's RoI head predicts boxes for background class too, only to discard
        # them right away, so might as well not calculate it in the first place
        nm = 'roi_heads.box_predictor.bbox_pred.'
        wd[nm + 'weight'] = wd[nm + 'weight'][4:, :] # [364, 1024] -> [360]
        wd[nm + 'bias'] = wd[nm + 'bias'][4:]        # [364] -> [360]
        return wd

    def mm_conversion(self, wd):
        # in MMDet weights for RoI head, representation FC and final reg/log FCs are switched over
        wl = list(wd.items())
        els = [wl.pop(-1) for _ in range(8)][::-1] # last 8 entries
        for el in els[4:] + els[:4]:
            wl.append(el)
        wd = dict(wl)
        return wd

    def config_base(self):
        cfg = {}
        cfg.update(fpn_batchnorm=None, rpn_convdepth=1, roi_convdepth=0, roi_mlp_depth=2)
        cfg.update(resize_min=800, resize_max=1333)
        cfg.update(score_thr1=0.0, iou_thr1=0.7, imtop1=1000, min_size1=0, lvtop=1000)
        cfg.update(score_thr2=0.05, iou_thr2=0.5, imtop2=100, min_size2=0)
        cfg.update(imtop1_train=2000, lvtop_train=2000)
        cfg.update(weights_add_nbatches=False)
        cfg.update(bbone_bn_freeze=False)
        return cfg

    def config_torchvision(self, cfg):
        cfg.update(num_classes=90, bckg_class_first=True, roialign_settings=(2, False))
        cfg.update(weights_sub=None, weights_extra=self.tv_conversion)
        cfg.update(prep_resize='torch', priors_patches='fit', round_anchors=True)
        cfg.update(min_size1=1e-3, min_size2=1e-2)
        return cfg

    def config_mmdet(self, cfg):
        cfg.update(num_classes=80, bckg_class_first=False, roialign_settings=(0, True))
        cfg.update(weights_sub='state_dict', weights_extra=self.mm_conversion)
        cfg.update(prep_resize='cv2', priors_patches='as_is', round_anchors=False)
        return cfg

    def config_resnet(self, cfg):
        cfg.update(bbone='resnet50', resnet_bn_eps=1e-5)
        cfg.update(cins=[256, 512, 1024, 2048], strides=[4, 8, 16, 32, 64])
        cfg.update(anchors=make_anchors([32, 64, 128, 256, 512], [1], [2, 1, 0.5], cfg['round_anchors']))
        return cfg

    def config_mobile(self, cfg):
        cfg.update(bbone='mobilenetv3l')
        cfg.update(cins=[160, 960], strides=[32, 32, 64])
        cfg.update(anchors=make_anchors([32, 32, 32], [1, 2, 4, 8, 16], [2, 1, 0.5], cfg['round_anchors']))
        return cfg

    def get_config(self, src, arch, version=None):
        cfg = self.config_base()
        cfg = self.config_torchvision(cfg) if src == 'tv' else self.config_mmdet(cfg)
        cfg = self.config_resnet(cfg) if arch == 'resnet50' else self.config_mobile(cfg)
        if src == 'tv' and (arch == 'resnet50' and version == 'v1' or arch == 'mobilenetv3l'):
            cfg.update(weights_add_nbatches=True)
            cfg.update(bbone_bn_freeze=True)
        if src == 'tv' and arch == 'resnet50' and version == 'v1':
            cfg.update(resnet_bn_eps=0.0)
        if src == 'tv' and arch == 'resnet50' and version == 'v2':
            cfg.update(fpn_batchnorm=1e-5, rpn_convdepth=2, roi_convdepth=4, roi_mlp_depth=1)
        if src == 'tv' and arch == 'mobilenetv3l' and version == 'lores':
            cfg.update(resize_min=320, resize_max=640, lvtop=150, imtop1=150, score_thr1=0.05)
        if version == 'animefaces':
            cfg.update(num_classes=1)
        return cfg

    def get_backbone(self, cfg):
        if cfg['bbone'] == 'resnet50':
            bn_eps = cfg['resnet_bn_eps']
            bn = bn_eps if not cfg['bbone_bn_freeze'] else (bn_eps, 'frozen')
            return ResNet50(bn=bn, num_freeze=2)
        if cfg['bbone'] == 'mobilenetv3l':
            return MobileNetV3L([13, 16], bn=(1e-05, 'frozen'), num_freeze=7)

    def __init__(self, pretrained='tv_resnet50_v1', device='cpu'):
        super().__init__()
        cfg = self.get_config(*pretrained.split('_'))
        self.cfg = cfg
        self.bases = list(zip(cfg['strides'], cfg['anchors']))
        self.body = self.get_backbone(cfg)
        self.fpn = FeaturePyramidNetwork(cfg['cins'], 256, None, cfg['fpn_batchnorm'], pool=True, smoothP5=True)
        self.rpn = RegionProposalNetwork(256, len(cfg['anchors'][0]), cfg['rpn_convdepth'])
        self.roi = RoIProcessingNetwork(256, 7, 1024, cfg)
        self.to(device)
        load_weights(self, self.links[pretrained], pretrained, device,
                     cfg['weights_extra'], cfg['weights_sub'], cfg['weights_add_nbatches'])

    def forward(self, imgs, targets=None):
        dv = next(self.parameters()).device
        resize = (self.cfg['resize_min'], self.cfg['resize_max'])
        resize_with = self.cfg['prep_resize']
        priors_patches = self.cfg['priors_patches']
        strides = self.cfg['strides']

        x, sz_orig, sz_used = preprocess(imgs, dv, resize, resize_with)
        priors = get_priors(x.shape[2:], self.bases, dv, 'corner', priors_patches, concat=False)
        xs = self.body(x)
        xs = self.fpn(xs)
        if self.training:
            gtb, gtl = prep_targets(targets, sz_used, sz_orig)
            p, imidx, p_losses = self.rpn(xs, priors, sz_used, self.cfg, gtb)
            d_losses = self.roi(p, imidx, xs[:-1], strides[:-1], sz_used, self.cfg, gtb, gtl)
            return (*p_losses, *d_losses)
        else:
            p, imidx = self.rpn(xs, priors, sz_used, self.cfg)
            b, s, c = self.roi(p, imidx, xs[:-1], strides[:-1], sz_used, self.cfg)
            b = scale_boxes(b, sz_orig, sz_used)
            b, s, c = [[t.detach().cpu().numpy() for t in tl] for tl in [b, s, c]]
            return b, s, c


from desafio_backend import (find_sum_indexes,
                             check_bracket_balance,
                             find_max_profit,
                             find_water_volume)

def test_find_sum_indexes():
    array = [2, 1, 5, 7, 15]
    target = 20
    result = find_sum_indexes(array, target)
    assert result == [2, 4]

def test_find_sum_indexes2():
    array = [2, 1]
    target = 3
    result = find_sum_indexes(array, target)
    assert result == [0, 1]

def test_check_bracket_balance():
    test_string = "jrgjoe(eok{124}kmm)"
    result = check_bracket_balance(test_string)
    assert result == "SIM"

    test_string = "{[([])]}"
    result = check_bracket_balance(test_string)
    assert result == "SIM"

    test_string = "{[(])}"
    result = check_bracket_balance(test_string)
    assert result == "NAO"

    test_string = "{{[([()])]}}"
    result = check_bracket_balance(test_string)
    assert result == "SIM"

def test_find_max_profit():
    input_prices = [7, 1, 5, 3, 6, 4]
    result = find_max_profit(input_prices)
    assert result == 5

    input_prices = [7, 6, 4, 3, 1]
    result = find_max_profit(input_prices)
    assert result == 0

    input_prices = [7, 1, 8, 2, 4, 2, 10]
    result = find_max_profit(input_prices)
    assert result == 9

def test_find_water_volume():
    contour = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
    result = find_water_volume(contour)
    assert result == 6

    contour = [1, 0, 5, 3, 4, 0, 3, 0, 2, 1, 2]
    result = find_water_volume(contour)
    assert result == 8

    contour = [0, 6, 4, 3, 2, 5, 2, 7, 4, 3, 2, 3]
    result = find_water_volume(contour)
    assert result == 15