from abc import abstractmethod
from typing import Iterable, Union

import numpy as np
from scipy.linalg import block_diag, eigh

from .cca_base import _CCA_Base
from ..utils.check_values import _process_parameter, check_views

# from hyperopt import fmin, tpe, Trials

class rCCA(_CCA_Base):
    """
    A class used to fit Regularised CCA (canonical ridge) model. Uses PCA to perform the optimization efficiently for high dimensional data.

    Citation
    --------
    Vinod, Hrishikesh D. "Canonical ridge and econometrics of joint production." Journal of econometrics 4.2 (1976): 147-166.

    :Example:

    >>> from cca_zoo.models import rCCA
    >>> X1 = np.random.rand(10,5)
    >>> X2 = np.random.rand(10,5)
    >>> model = rCCA(c=[0.1,0.1])
    >>> model.fit([X1,X2])
    """

    def __init__(
            self,
            latent_dims: int = 1,
            scale: bool = True,
            centre=True,
            copy_data=True,
            random_state=None,
            c: Union[Iterable[float], float] = None,
            eps=1e-3,
            accept_sparse=None,
    ):
        """
        Constructor for rCCA

        :param latent_dims: number of latent dimensions to fit
        :param scale: normalize variance in each column before fitting
        :param centre: demean data by column before fitting (and before transforming out of sample
        :param copy_data: If True, X will be copied; else, it may be overwritten
        :param random_state: Pass for reproducible output across multiple function calls
        :param c: Iterable of regularisation parameters for each view (between 0:CCA and 1:PLS)
        :param eps: epsilon for stability
        :param accept_sparse: which forms are accepted for sparse data
        """
        if accept_sparse is None:
            accept_sparse = ["csc", "csr"]
        super().__init__(
            latent_dims=latent_dims,
            scale=scale,
            centre=centre,
            copy_data=copy_data,
            accept_sparse=accept_sparse,
            random_state=random_state,
        )
        self.c = c
        self.eps = eps

    def _check_params(self):
        self.c = _process_parameter("c", self.c, 0, self.n_views)

    def fit(self, views: Iterable[np.ndarray], y=None, **kwargs):
        """
        Fits a regularised CCA (canonical ridge) model

        :param views: list/tuple of numpy arrays or array likes with the same number of rows (samples)
        """
        views = check_views(
            *views, copy=self.copy_data, accept_sparse=self.accept_sparse
        )
        views = self._centre_scale(views)
        self.n_views = len(views)
        self.n = views[0].shape[0]
        self._check_params()
        views, C, D = self._setup_evp(views, **kwargs)
        self._solve_evp(views, C, D, **kwargs)
        return self

    @abstractmethod
    def _setup_evp(self, views: Iterable[np.ndarray], **kwargs):
        Us, Ss, Vts = _pca_data(*views)
        self.Bs = [
            (1 - self.c[i]) * S * S / self.n + self.c[i] for i, S in enumerate(Ss)
        ]
        if len(views) == 2:
            self._two_view = True
            C, D = self._two_view_evp(Us, Ss)
        else:
            self._two_view = False
            C, D = self._multi_view_evp(Us, Ss)
        return Vts, C, D

    @abstractmethod
    def _solve_evp(self, views: Iterable[np.ndarray], C, D=None, **kwargs):
        p = C.shape[0]
        if self._two_view:
            [eigvals, eigvecs] = eigh(C, subset_by_index=[p - self.latent_dims, p - 1])
            eigvecs = eigvecs
            idx = np.argsort(eigvals, axis=0)[::-1][: self.latent_dims]
            eigvecs = eigvecs[:, idx].real
            w_y = views[1].T @ np.diag(1 / np.sqrt(self.Bs[1])) @ eigvecs
            w_x = (
                    views[0].T
                    @ np.diag(1 / self.Bs[0])
                    @ self.R_12
                    @ np.diag(1 / np.sqrt(self.Bs[1]))
                    @ eigvecs
                    / np.sqrt(eigvals[idx])
            )
            self.weights = [w_x, w_y]
        else:
            [eigvals, eigvecs] = eigh(
                C, D, subset_by_index=[p - self.latent_dims, p - 1]
            )
            idx = np.argsort(eigvals, axis=0)[::-1]
            eigvecs = eigvecs[:, idx].real
            self.weights = [
                Vt.T
                @ np.diag(1 / np.sqrt(B))
                @ eigvecs[split: self.splits[i + 1], : self.latent_dims]
                for i, (split, Vt, B) in enumerate(
                    zip(self.splits[:-1], views, self.Bs)
                )
            ]

    def _two_view_evp(self, Us, Ss):
        Rs = [U @ np.diag(S) for U, S in zip(Us, Ss)]
        self.R_12 = Rs[0].T @ Rs[1]
        M = (
                np.diag(1 / np.sqrt(self.Bs[1]))
                @ self.R_12.T
                @ np.diag(1 / self.Bs[0])
                @ self.R_12
                @ np.diag(1 / np.sqrt(self.Bs[1]))
        )
        return M, None

    def _multi_view_evp(self, Us, Ss):
        D = block_diag(
            *[np.diag((1 - self.c[i]) * S * S + self.c[i]) for i, S in enumerate(Ss)]
        )
        C = np.concatenate([U @ np.diag(S) for U, S in zip(Us, Ss)], axis=1)
        C = C.T @ C
        C -= block_diag(*[np.diag(S ** 2) for U, S in zip(Us, Ss)]) - D
        D_smallest_eig = min(0, np.linalg.eigvalsh(D).min()) - self.eps
        D = D - D_smallest_eig * np.eye(D.shape[0])
        self.splits = np.cumsum([0] + [U.shape[1] for U in Us])
        return C, D

class CCA(rCCA):
    """
    A class used to fit a simple CCA model

    Implements CCA by inheriting regularised CCA with 0 regularisation

    Citation
    --------
    Hotelling, Harold. "Relations between two sets of variates." Breakthroughs in statistics. Springer, New York, NY, 1992. 162-190.

    :Example:

    >>> from cca_zoo.models import CCA
    >>> X1 = np.random.rand(10,5)
    >>> X2 = np.random.rand(10,5)
    >>> model = CCA()
    >>> model.fit(X1,X2)
    """

    def __init__(
            self,
            latent_dims: int = 1,
            scale: bool = True,
            centre=True,
            copy_data=True,
            random_state=None,
    ):
        """
        Constructor for CCA
        :param latent_dims: number of latent dimensions to fit
        :param scale: normalize variance in each column before fitting
        :param centre: demean data by column before fitting (and before transforming out of sample
        :param copy_data: If True, X will be copied; else, it may be overwritten
        :param random_state: Pass for reproducible output across multiple function calls
        """
        super().__init__(
            latent_dims=latent_dims,
            scale=scale,
            centre=centre,
            copy_data=copy_data,
            c=[0.0, 0.0],
            random_state=random_state,
        )

class PLS(rCCA):
    """
    A class used to fit a simple PLS model

    Implements PLS by inheriting regularised CCA with maximal regularisation

    :Example:

    >>> from cca_zoo.models import PLS
    >>> X1 = np.random.rand(10,5)
    >>> X2 = np.random.rand(10,5)
    >>> model = CCA()
    >>> model.fit([X1,X2])
    """

    def __init__(
            self,
            latent_dims: int = 1,
            scale: bool = True,
            centre=True,
            copy_data=True,
            random_state=None,
    ):
        """
        Constructor for CCA
        :param latent_dims: number of latent dimensions to fit
        :param scale: normalize variance in each column before fitting
        :param centre: demean data by column before fitting (and before transforming out of sample
        :param copy_data: If True, X will be copied; else, it may be overwritten
        :param random_state: Pass for reproducible output across multiple function calls
        """
        super().__init__(
            latent_dims=latent_dims,
            scale=scale,
            centre=centre,
            copy_data=copy_data,
            c=[1.0, 1.0],
            random_state=random_state,
        )

def _pca_data(*views: np.ndarray):
    """
    :param views: numpy arrays with the same number of rows (samples) separated by commas
    """
    views_U = []
    views_S = []
    views_Vt = []
    for i, view in enumerate(views):
        U, S, Vt = np.linalg.svd(view, full_matrices=False)
        views_U.append(U)
        views_S.append(S)
        views_Vt.append(Vt)
    return views_U, views_S, views_Vt


from app import models, timeslotHelp, db, login_serializer, login_manager, app
from flask import flash
import numpy
from datetime import date, timedelta
import datetime

# Attempts to get a user based off of the remember token received from the browser
@login_manager.token_loader
def load_token(token):
    max_age = app.config["REMEMBER_COOKIE_DURATION"].total_seconds()

    # Decrypt the Security Token, data = [username, hashpass]
    data = login_serializer.loads(token, max_age=max_age)

    # Find the User
    user = models.User.query.filter_by(id=data[0]).first()

    # Check Password and return user or None
    # if user and user.is_correct_password(data[1]):
    if user and user.password == data[1]:
        return user
    return None

# Returns a set of 7 or less teachers based off of the current number selected
def build_display_teachers(teacher_num):
    teachers = models.Teacher.query.order_by(models.Teacher.order.asc()).all()

    # We only want to display 7 teachers at a time, so divvy up the teachers according to the current teacher set
    # Handle boundaries
    if teacher_num == '-1' or teacher_num == -1:
        teacher_num = 0
    if teacher_num * 7 > len(teachers):
        teacher_num -= 1
    display_teachers = teachers[teacher_num * 7:(teacher_num + 1) * 7]
    return display_teachers

# Builds a 3d array used to display the appointments for a day
# Takes in the date and the teachers that we are displaying
# Also used to find which appointment is selected when trying to edit or delete
def build_display_appointments(today_date, teachers):
    # TODO take a look at this today date
    appointments = list()

    all_appointments = models.Appointment.query.filter_by(is_timeoff=False).filter_by(is_active=True).all()
    all_makeups = [app for app in all_appointments if app.is_makeup == True]
    all_absences = models.Absence.query.all()

    for app in all_appointments:
        if is_appointment_today(today_date, app) and not is_absent(today_date, app, all_absences) and not is_rescheduled(today_date, app, all_makeups):
            appointments.append(app)

    display_appointments = numpy.empty((len(teachers), len(timeslotHelp.get_timeslots()), 5), dtype=object)
    teacher_ids = [teacher.id for teacher in teachers]

    for appointment in appointments:
        # Skip the appointment if it is not from one of the teachers being displayed
        if appointment.teacher_id not in teacher_ids:
            continue
        for slot in timeslotHelp.timeslot_string_to_timeslot_num(appointment.times):
            for count in range(0, 5):
                if display_appointments[teacher_ids.index(appointment.teacher_id)][slot - 1][count] is None:
                    display_appointments[teacher_ids.index(appointment.teacher_id)][slot - 1][count] = appointment
                    break
    return display_appointments

# Builds a 3d array used to display the appointments for a day
# Takes in the date and the teachers that we are displaying
def build_display_timeoff(all_timeoff, today_date, teachers):

    # Current, Recurring time off
    teacher_recurring_timeoff = models.Appointment.query.filter_by(is_timeoff=True).filter_by(is_active=True).all()

    today_timeoff = list()

    for timeoff in all_timeoff:
        if is_timeoff_today(today_date, timeoff):
            today_timeoff.append(timeoff)

    display_timeoff = numpy.empty((len(teachers), len(timeslotHelp.get_timeslots()), 5), dtype=object)
    teacher_ids = [teacher.id for teacher in teachers]

    # Handle single time off
    for timeoff in today_timeoff:
        # Skip the appointment if it is not from one of the teachers being displayed
        if timeoff.teacher_id in teacher_ids:
            for slot in today_timeslots_in_timeoff(today_date, timeoff):
                for count in range(0, 5):
                    display_timeoff[teacher_ids.index(timeoff.teacher_id)][slot - 1][count] = timeoff

    # Factor in all recurring time off
    for timeoff in teacher_recurring_timeoff:
        if is_appointment_today(today_date, timeoff) and timeoff.teacher_id in teacher_ids:
            for slot in timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times):
                for count in range(0, 5):
                    display_timeoff[teacher_ids.index(timeoff.teacher_id)][slot - 1][count] = timeoff
    return display_timeoff

# The time off for a teacher in a given week
def build_display_teacher_week_timeoff(all_timeoff, teacher, dates_of_week):

    # Current, Recurring time off
    teacher_recurring_timeoff = models.Appointment.query.filter_by(teacher_id=teacher.id).filter_by(is_timeoff=True).filter_by(is_active=True).all()

    week_timeoff = list()

    for timeoff in all_timeoff:
        if timeoff.teacher_id == teacher.id:
            week_timeoff.append(timeoff)

    display_timeoff = numpy.empty((6, len(timeslotHelp.get_timeslots()), 5), dtype=object)

    # Display all the appointments that are during that week
    for timeoff in week_timeoff:
        for idx, date in enumerate(dates_of_week):
            if is_timeoff_today(date, timeoff):
                for slot in today_timeslots_in_timeoff(date, timeoff):
                    for count in range(0, 5):
                        display_timeoff[idx][slot - 1][count] = timeoff

    # Factor in all recurring time off
    for timeoff in teacher_recurring_timeoff:
        for idx, date in enumerate(dates_of_week):
            if is_appointment_today(date, timeoff):
                for slot in timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times):
                    for count in range(0, 5):
                        display_timeoff[idx][slot - 1][count] = timeoff

    return display_timeoff

# Builds a 3d array used to display the appointments for a week and specific teacher
# Takes in the dates and the teacher that we are displaying
# Also used to find which appointment is selected when trying to edit or delete
def build_display_teacher_appointments(dates_of_week, current_teacher):
    display_appointments = numpy.empty((6, len(timeslotHelp.get_timeslots()), 5), dtype=object)

    teacher_appointments = models.Appointment.query.filter_by(is_timeoff=False).filter_by(teacher_id=current_teacher.id).all()

    # Remove makeup appointments and absences
    teacher_makeups = [app for app in teacher_appointments if app.is_makeup == True]
    all_absences = models.Absence.query.all()

    # Display all the appointments that are during that week
    for app in teacher_appointments:
        for idx, date in enumerate(dates_of_week):
            if is_appointment_today(date, app):
                for slot in timeslotHelp.timeslot_string_to_timeslot_num(app.times):
                    for count in range(0, 5):
                        # Add if not absent or rescheduled for that day
                        if not is_teacher_appointment_absent(date, app, all_absences) and not is_teacher_appointment_rescheduled(date, app, teacher_makeups):
                            if display_appointments[idx][slot - 1][count] is None:
                                display_appointments[idx][slot - 1][count] = app
                                break
    return display_appointments

# Used for making appointments, gets all the appointments at a specific date and time to return them for checking
# validity of making an appointment
# Used to check for appointment validity, so absences and makeups must be excluded
def get_appointments_at_date_time(date, timeslot, db_appointments, all_absences, all_makeups):
    timeslot = str(timeslot)

    # Remove appointments not at the specified timeslot and day
    current_appointments = [app for app in db_appointments if timeslot in app.times.split(' ') and is_appointment_today(timeslotHelp.date_string_to_object(date).date(), app)]

    return_appointments = list()
    for app in current_appointments:
        if not is_absent(timeslotHelp.date_string_to_object(date).date(), app, all_absences) and not is_rescheduled(timeslotHelp.date_string_to_object(date).date(), app, all_makeups):
            return_appointments.append(app)
    return return_appointments

# Used for making appointments, gets all the appointments at a specific date
# and time for a specific teacher to return them for checking
# validity of making an appointment
# Used to check for appointment validity, so absences and makeups must be excluded
def get_teacher_appointments_at_date_time(date, timeslot, teacher, db_appointments, all_absences):
    timeslot = str(timeslot)

    all_makeups = [app for app in db_appointments if app.is_makeup == True]

    # Remove appointments not at the specified timeslot and day
    current_appointments = [app for app in db_appointments if timeslot in app.times.split(' ') and is_appointment_today(timeslotHelp.date_string_to_object(date).date(), app)]

    return_appointments = list()
    for app in current_appointments:
        if not is_absent(timeslotHelp.date_string_to_object(date).date(), app, all_absences) and not is_rescheduled(timeslotHelp.date_string_to_object(date).date(), app, all_makeups):
            return_appointments.append(app)
    return return_appointments

# Function to check each appointment and if it is all old prevent it from being checked for conflicts
# See if they still want it in the database for history or if i can just remove it
def remove_old_appointments():
    appointments = models.Appointment.query.filter_by(is_active=True).all()
    all_absences = models.Absence.query.filter_by(is_active=True).all()
    all_timeoff = models.TimeOff.query.filter_by(is_active=True).all()

    today_date = datetime.datetime.now().date() - timedelta(1)

    for app in appointments:
        # Check one day appointments
        if app.end_date == '' and app.days_of_week == '' and \
           timeslotHelp.date_string_to_object(app.start_date).date() < today_date:
            app.is_active = False
        # Check recurring appointments
        if app.end_date != '' and app.days_of_week != '' and \
           timeslotHelp.date_string_to_object(app.end_date).date() < today_date:
            app.is_active = False

    for absence in all_absences:
        if timeslotHelp.date_string_to_object(absence.date_of_absence).date() < today_date:
            absence.is_active = False

    for timeoff in all_timeoff:
        # Check one day time off
        if timeoff.end_date == '':
            if timeslotHelp.date_string_to_object(timeoff.start_date).date() < today_date:
                timeoff.is_active = False
        # Check multiple day time off
        elif timeslotHelp.date_string_to_object(timeoff.end_date).date() < today_date:
            timeoff.is_active = False
    db.session.commit()

# True if time off is today
def is_timeoff_today(today_date, timeoff):
    # Check timeoff that are one day
    if timeoff.end_date == '':
        if timeslotHelp.date_string_to_object(timeoff.start_date).date() == today_date:
            return True

    else:
        if timeslotHelp.date_string_to_object(timeoff.end_date).date() >= today_date >= \
                timeslotHelp.date_string_to_object(timeoff.start_date).date():
            return True

    return False

# Returns an array of all the timeslots that are a part of the time off on a given day
# assumes that today_date is in timeoff
def today_timeslots_in_timeoff(today_date, timeoff):
    all_slots = timeslotHelp.get_timeslot_indices()

    # If end date is null, then go from start to finish
    if timeoff.end_date == '':
        slots = all_slots[int(timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times)[0]) - 1:]

    # Handle 4 cases. Either first day, middle day, end day, or only one day.
    else:
        if timeoff.end_date == timeoff.start_date:
            slots = timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times)
        elif timeslotHelp.date_object_to_string(today_date) == timeoff.start_date:
            slots = all_slots[int(timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times)[0]) - 1:]
        elif timeslotHelp.date_object_to_string(today_date) == timeoff.end_date:
            slots = all_slots[:int(timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times)[-2])]
        else:
            slots = all_slots

    return slots

# True if appointments occurs on the current day.  Only checks appointments that are active
def is_appointment_today(today_date, app):
    if not app.is_active:
        return False
    # Check appointments that are one day
    if app.end_date == '' and app.days_of_week == '':
        if timeslotHelp.date_string_to_object(app.start_date).date() == today_date:
            return True

    else:
        # Check recurring appointments
        # Get the day of week from the today_date
        if app.end_date != '':
            # Add if on a recurring day of week and not past the end date but today is after the start date
            if str(today_date.weekday() + 1) in timeslotHelp.day_letters_to_day_nums(app.days_of_week):
                if timeslotHelp.date_string_to_object(app.end_date).date() >= today_date >= \
                        timeslotHelp.date_string_to_object(app.start_date).date():
                    return True

        # Check never ending appointments
        else:
            # Add if on the day of week
            if str(today_date.weekday() + 1) in timeslotHelp.day_letters_to_day_nums(app.days_of_week):
                return True

    return False

def is_absent(today_date, app, all_absences):
    if app.appointments is None or app.tutee is None or app.service is None:
        return False
    absent = False

    for absence in all_absences:
        # is absent if there is an absence with that appointment on that day and for the same appointment
        if today_date == timeslotHelp.date_string_to_object(absence.date_of_absence).date() and app.id == absence.appointment_id:
            absent = True
            break

    return absent

def is_rescheduled(today_date, app, all_makeups):
    if app.appointments is None or app.tutee is None or app.service is None:
        return False
    rescheduled = False

    for makeup in all_makeups:
        # is rescheduled if there is an makeup for that student with the old time, date, and teacher
        if app.id != makeup.id and app.tutee == makeup.tutee and today_date == timeslotHelp.date_string_to_object(makeup.old_date).date() and app.appointments.name == makeup.old_teacher and app.times == makeup.old_time:
            rescheduled = True
            break

    return rescheduled

def is_teacher_appointment_absent(date, app, teacher_absences):
    absent = False

    for absence in teacher_absences:
        # is absent if there is an absence with that appointment on that day and for the same appointment
        if date == timeslotHelp.date_string_to_object(absence.date_of_absence).date() and app.id == absence.appointment_id:
            absent = True
            break

    return absent

def is_teacher_appointment_rescheduled(date, app, teacher_makeups):
    rescheduled = False

    for makeup in teacher_makeups:
        # is rescheduled if there is an makeup for that student with the old time, date, and teacher
        if app.id != makeup.id and app.tutee == makeup.tutee and date == timeslotHelp.date_string_to_object(makeup.old_date).date() and app.times == makeup.old_time:
            rescheduled = True
            break

    return rescheduled

def teacher_off_on(timeslots, date, teacher_time_off, recurring_time_off):
    for timeoff in teacher_time_off:

        time_off_times = timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times)
        dateTimeOffStart = datetime.datetime.strptime(timeoff.start_date + ' ' + timeslotHelp.timeslot_num_to_time_string(time_off_times[0]), '%m/%d/%Y %I:%M')
        timeoff_end_date = timeoff.start_date if timeoff.end_date == '' else timeoff.end_date
        dateTimeOffEnd = datetime.datetime.strptime(timeoff_end_date + ' ' + timeslotHelp.timeslot_num_to_time_string(time_off_times[-1]), '%m/%d/%Y %I:%M')
        startTimeToCheck = datetime.datetime.strptime(date.strftime("%B %d, %Y") + ' ' + timeslotHelp.timeslot_num_to_time_string(timeslots[0]), '%B %d, %Y %I:%M')
        endTimeToCheck = datetime.datetime.strptime(date.strftime("%B %d, %Y") + ' ' + timeslotHelp.timeslot_num_to_time_string(timeslots[-1] + 1), '%B %d, %Y %I:%M')
        if dateTimeOffStart <= startTimeToCheck <= dateTimeOffEnd or dateTimeOffStart <= endTimeToCheck <= dateTimeOffEnd:
            return True

    # Also handle recurring time off for the teacher
    for timeoff in recurring_time_off:
        if is_appointment_today(date, timeoff):
            timeoff_times_list = timeslotHelp.timeslot_string_to_timeslot_num(timeoff.times)
            for time in timeslots:
                if time in timeoff_times_list:
                    return True

    return False


from django.test import TestCase

from ..settings import ALLOWED_HOSTS
from ..api import *
from ..models import *
from ..link_actions import get_id

class MockRequest:
    method = 'POST'
    scheme = 'https'
    META = {
        'HTTP_HOST': ALLOWED_HOSTS[0]
    }

class ApiTestCase(TestCase):
    request = MockRequest()

    def setUp(self):
        active = ShortUrlActive()
        active.save()

        # pylint: disable=no-member
        urlkey = ShortUrlKey.objects.create(keyhash=make_password('test'))

        # pylint: disable=no-member
        self.url = ShortUrl.objects.create(
            pk=get_id('test'),
            link='https://example.com',
            active=active,
            urlkey=urlkey
        )

class TestErrorSuccess(ApiTestCase):
    def test_success(self):
        self.assertDictEqual(
            success(self.request, self.url),
            {'status': 'success',
                'error_code': 0,
                'name': self.url.name,
                'link': self.url.link,
                'short': format_short(self.request, self.url.name),
                'created_at': self.url.created_at,
                'edited_at': self.url.edited_at,
                'uses': self.url.uses,
                'safe': self.url.is_safe,
                'active': {
                    'is_active': self.url.active.is_active,
                    'reason': self.url.active.reason,
                    'deactivated_since': self.url.active.deactivated_since},
                'key': False})

    def test_error(self):
        self.assertDictEqual(
            error(2, 'test'),
            {'status': 'error',
                'error_code': 2,
                'message': 'url with name \'test\' does not exist'})
        self.assertDictEqual(
            error(7, 'test'),
            {'status': 'error',
                'error_code': 7,
                'message': '\'test\' is not an allowed link'})

class TestRetrieve(ApiTestCase):
    def test_exists(self):
        self.assertDictEqual(
            retrieve(self.request, {'urlname': 'test'}),
            success(self.request, self.url))

class TestCreate(ApiTestCase):
    def test_invalid_name(self):
        self.assertDictEqual(
            create(self.request, {'urlname': '!nval!d',
                                  'link': 'https://example.com'}),
            error(6, '!nval!d'))

    def test_exists(self):
        self.assertDictEqual(
            create(self.request, {'urlname': 'test',
                                  'link': 'https://example.com'}),
            error(3, 'test'))

    def test_success(self):
        resp = create(
            self.request, {'urlname': 'new', 'link': 'https://example.com'})
        self.assertEqual(resp['error_code'], 0)

class TestEdit(ApiTestCase):
    def test_newlink(self):
        self.assertEqual(
            edit(self.request, {'urlname': 'test',
                                'newlink': 'https://example.org',
                                'key': 'test'})['link'],
            'https://example.org')

    def test_newkey(self):
        self.assertEqual(
            edit(self.request, {'urlname': 'test',
                                'newkey': 'testkey',
                                'key': 'test'})['key'],
            'testkey')

    def test_active(self):
        self.assertFalse(
            edit(self.request, {'urlname': 'test',
                                'active': 'false',
                                'key': 'test'})['active']['is_active'])
        self.assertTrue(
            edit(self.request, {'urlname': 'test',
                                'active': 'true',
                                'key': 'test'})['active']['is_active'])

    def test_reason(self):
        self.assertEqual(
            edit(self.request, {'urlname': 'test',
                                'reason': 'test',
                                'active': 'false',
                                'key': 'test'})['active']['reason'],
            'test')

    def test_max_reason(self):
        self.assertDictEqual(
            edit(self.request, {'urlname': 'test',
                                'active': 'false',
                                'reason': '_'*257,
                                'key': 'test'}),
            error(9))

    def test_wrong_key(self):
        self.assertDictEqual(
            edit(self.request, {'urlname': 'test',
                                'newkey': '',
                                'key': 'wrong key'}),
            error(8))

class TestDelete(ApiTestCase):
    def test_delete(self):
        delete(self.request, {'urlname': 'test', 'key': 'test'})
        # pylint: disable=no-member
        self.assertFalse(ShortUrl.objects.filter(pk=get_id('test')).exists())

    def test_not_exists(self):
        self.assertDictEqual(
            delete(self.request, {'urlname': 'not_exists', 'key': 'test'}),
            error(2, 'not_exists'))

    def test_wrong_key(self):
        self.assertDictEqual(
            delete(self.request, {'urlname': 'test', 'key': 'wrong key'}),
            error(8))


import os
from unittest.mock import patch

import pytest

from pytorch_lightning import Trainer
from pytorch_lightning.loggers import CometLogger
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.base import EvalModelTemplate

def test_comet_logger_online():
    """Test comet online with mocks."""
    # Test api_key given
    with patch('pytorch_lightning.loggers.comet.CometExperiment') as comet:
        logger = CometLogger(
            api_key='key',
            workspace='dummy-test',
            project_name='general'
        )

        _ = logger.experiment

        comet.assert_called_once_with(
            api_key='key',
            workspace='dummy-test',
            project_name='general'
        )

    # Test both given
    with patch('pytorch_lightning.loggers.comet.CometExperiment') as comet:
        logger = CometLogger(
            save_dir='test',
            api_key='key',
            workspace='dummy-test',
            project_name='general'
        )

        _ = logger.experiment

        comet.assert_called_once_with(
            api_key='key',
            workspace='dummy-test',
            project_name='general'
        )

    # Test neither given
    with pytest.raises(MisconfigurationException):
        CometLogger(
            workspace='dummy-test',
            project_name='general'
        )

    # Test already exists
    with patch('pytorch_lightning.loggers.comet.CometExistingExperiment') as comet_existing:
        logger = CometLogger(
            experiment_key='test',
            experiment_name='experiment',
            api_key='key',
            workspace='dummy-test',
            project_name='general'
        )

        _ = logger.experiment

        comet_existing.assert_called_once_with(
            api_key='key',
            workspace='dummy-test',
            project_name='general',
            previous_experiment='test'
        )

        comet_existing().set_name.assert_called_once_with('experiment')

    with patch('pytorch_lightning.loggers.comet.API') as api:
        CometLogger(
            api_key='key',
            workspace='dummy-test',
            project_name='general',
            rest_api_key='rest'
        )

        api.assert_called_once_with('rest')

def test_comet_logger_dirs_creation(tmpdir, monkeypatch):
    """ Test that the logger creates the folders and files in the right place. """
    # prevent comet logger from trying to print at exit, since
    # pytest's stdout/stderr redirection breaks it
    import atexit
    monkeypatch.setattr(atexit, 'register', lambda _: None)

    logger = CometLogger(project_name='test', save_dir=tmpdir)
    assert not os.listdir(tmpdir)
    assert logger.mode == 'offline'
    assert logger.save_dir == tmpdir

    _ = logger.experiment
    version = logger.version
    assert set(os.listdir(tmpdir)) == {f'{logger.experiment.id}.zip'}

    model = EvalModelTemplate()
    trainer = Trainer(default_root_dir=tmpdir, logger=logger, max_epochs=1, limit_val_batches=3)
    trainer.fit(model)

    assert trainer.checkpoint_callback.dirpath == (tmpdir / 'test' / version / 'checkpoints')
    assert set(os.listdir(trainer.checkpoint_callback.dirpath)) == {'epoch=0.ckpt'}


import numpy as np

class movement:

	'''def linea(self, vi, vf, dt):
		linea = []
		t = 0
		while(t < 1 + dt):
			xt = (vi[0] + t * (vf[0] - vi[0])
			yt = (vi[1] + t * (vf[1] - vi[1])
			zt = (vi[2] + t * (vf[2] - vi[2])
			t+=dt
			punto = [xt, yt, zt]
			linea.append(punto)
		return linea'''
	def hermite(self, P1, P4, R1, R4, dt):
		linea = []
		t = 0
		MH = np.matrix([[2,-2,1,1],[-3,3,-2,-1],[0,0,1,0],[1,0,0,0]])
		GH = np.matrix([P1,P4,R1,R4])
		while(t < 1 + dt):
			T = np.matrix([[t**3, t**2, t, 1.0]])
			Qt = T * MH * GH
			t+= dt
			linea.append(Qt)
		return linea

	def bezier(self, p1, p2, p3, p4, dt):
		linea = []
		t = 0
		MB = np.matrix([[-1, 3, -3, 1],[3, -6, 3, 0],[-3, 3, 0, 0],[1, 0, 0, 0]])
		GB = np.matrix([p1, p2, p3, p4])
		while(t < 1 + dt):
			T = np.matrix([[t**3, t**2, t, 1.0]])
			Qt = T * MB * GB
			t+= dt
			linea.append(Qt)
		return linea


# Packages to import
import math
import copy

def my_round(i):
    """ This function is for rounding.
    Keyword:
    (float) i : number to be inputted
    Returns:
    (int) f: number rounded up or down based on mathematical rounding rules
    """
    f = math.floor(i)
    return f if i - f < 0.5 else f+1

def count_the_months(another_list):

    """ This function designed to count the number of months.
    Keywords:
    (list) another_list: input list of all the dates (some are repeating)
    Returns: length of set of dates or dictionary if dates per each measure are different
    """

    # Create a dates set and a measure dictionary
    dates_set = set()
    measure_dict = dict()

    for row in another_list:

        # If the date is not in the set, then add it
        if row[1] not in dates_set:
            dates_set.add(row[1])

        # If the measure is in the dictionary, increase the count
        if row[2] in measure_dict:
            measure_dict[row[2]] += 1
        else:
            # Otherwise add it to the dictionary
            measure_dict[row[2]] = 1

    # Check for an empty dictionary first if that's possible
    expected_value = next(iter(measure_dict.values()))
    all_equal = all(value == expected_value for value in measure_dict.values())

    return len(dates_set) if all_equal else measure_dict

def calculate_average_crossing_per_month_and_measure(num_of_months, list_with_agg_values):
    """ Helper function used to calculate the average crossings per month and per measure.
    Keywords:
    num_of_months (dict or list): the number of months based on the frequency of each measure
    list_with_agg_values (list): the list with Border, Date, Measure, and aggregated values
    Returns:
    list_with_avg (list): the list with the average crossing values per month and per measure
    """

    list_with_avg = []

    # Going through the list of aggregated valves backwards
    # the list was sorted with the most recent date up first, so hence we are adding from the
    # the bottom up and not top down direction
    for i in range(len(list_with_agg_values) - 1, 0, -1):
        each_row = list_with_agg_values[i]

        # Now check whether the number of the months per measure is the same or not:
        # If it's not, we going to calculate the average for each measure's frequency
        if isinstance(num_of_months, dict):
            for key, value in num_of_months.items():
                if each_row[2] == key:
                    if i % value == 0:
                        accumulation, counter = 0, 0
                        each_row = each_row + [0]
                    else:
                        # Add up each of the previous months' values
                        each_row_before = list_with_agg_values[i + 1]
                        accumulation += each_row_before[3]

                        # Similarly add for each month to the counter
                        counter += 1

                        # For each row, get the average value of crossing based for each measure and border
                        each_row = each_row + [my_round(accumulation / counter)]

                    # And keep track in the list
                    list_with_avg.append(each_row)
        else:
            # Otherwise, if the frequency is the same for all of the measures
            if i % (num_of_months - 1) == 0:
                accumulation, counter = 0, 0
                each_row = each_row + [0]
            else:
                # Add up each of the previous months' values
                each_row_before = list_with_agg_values[i + 1]
                accumulation += each_row_before[3]

                # Similarly add for each month to the counter
                counter += 1

                # For each row, get the average value of crossing based for each measure and border
                each_row = each_row + [my_round(accumulation / counter)]

            # And keep track in the list
            list_with_avg.append(each_row)

    return list_with_avg


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 29 15:44:13 2018

@author: xsxsz
"""

import numpy as np
import tensorflow as tf
init=tf.global_variables_initializer()
x=tf.constant([1,2],dtype=tf.float32)
tf.cast(x,tf.int32)
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(x))


# coding: utf-8

LAYERS = {
    u'Áreas Reservadas': False,
    u'Certificado Mineiro': True,
    u'Áreas de Conservação': False,
    u'Áreas para Concurso': False,
    u'Blocos de Hidrocarbonetos - Em Vigor': True,
    u'Contratos': True,
    u'Concessão Mineira': True,
    u'Áreas Designadas': True,
    u'Licença de Reconhecimento': True,
    u'Licença de Prospecção e Pesquisa': True,
    u'Autorização de Recursos Minerais para Construção': True,
    u'Todas Licenças Extinto': False,
    u'Áreas de Conservação - Buffer': False,
}


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

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = '''
---
module: get_certificate
author: "John Westcott IV (@john-westcott-iv)"
version_added: "2.8"
short_description: Get a certificate from a host:port
description:
    - Makes a secure connection and returns information about the presented certificate
    - The module can use the cryptography Python library, or the pyOpenSSL Python
      library. By default, it tries to detect which one is available. This can be
      overridden with the I(select_crypto_backend) option. Please note that the PyOpenSSL
      backend was deprecated in Ansible 2.9 and will be removed in Ansible 2.13."
options:
    host:
      description:
        - The host to get the cert for (IP is fine)
      type: str
      required: true
    ca_cert:
      description:
        - A PEM file containing one or more root certificates; if present, the cert will be validated against these root certs.
        - Note that this only validates the certificate is signed by the chain; not that the cert is valid for the host presenting it.
      type: path
    port:
      description:
        - The port to connect to
      type: int
      required: true
    proxy_host:
      description:
        - Proxy host used when get a certificate.
      type: str
      version_added: 2.9
    proxy_port:
      description:
        - Proxy port used when get a certificate.
      type: int
      default: 8080
      version_added: 2.9
    timeout:
      description:
        - The timeout in seconds
      type: int
      default: 10
    select_crypto_backend:
      description:
        - Determines which crypto backend to use.
        - The default choice is C(auto), which tries to use C(cryptography) if available, and falls back to C(pyopenssl).
        - If set to C(pyopenssl), will try to use the L(pyOpenSSL,https://pypi.org/project/pyOpenSSL/) library.
        - If set to C(cryptography), will try to use the L(cryptography,https://cryptography.io/) library.
      type: str
      default: auto
      choices: [ auto, cryptography, pyopenssl ]
      version_added: "2.9"

notes:
  - When using ca_cert on OS X it has been reported that in some conditions the validate will always succeed.

requirements:
  - "python >= 2.7 when using C(proxy_host)"
  - "cryptography >= 1.6 or pyOpenSSL >= 0.15"
'''

RETURN = '''
cert:
    description: The certificate retrieved from the port
    returned: success
    type: str
expired:
    description: Boolean indicating if the cert is expired
    returned: success
    type: bool
extensions:
    description: Extensions applied to the cert
    returned: success
    type: list
    elements: dict
    contains:
        critical:
            returned: success
            type: bool
            description: Whether the extension is critical.
        asn1_data:
            returned: success
            type: str
            description: The Base64 encoded ASN.1 content of the extnesion.
        name:
            returned: success
            type: str
            description: The extension's name.
issuer:
    description: Information about the issuer of the cert
    returned: success
    type: dict
not_after:
    description: Expiration date of the cert
    returned: success
    type: str
not_before:
    description: Issue date of the cert
    returned: success
    type: str
serial_number:
    description: The serial number of the cert
    returned: success
    type: str
signature_algorithm:
    description: The algorithm used to sign the cert
    returned: success
    type: str
subject:
    description: Information about the subject of the cert (OU, CN, etc)
    returned: success
    type: dict
version:
    description: The version number of the certificate
    returned: success
    type: str
'''

EXAMPLES = '''
- name: Get the cert from an RDP port
  get_certificate:
    host: "1.2.3.4"
    port: 3389
  delegate_to: localhost
  run_once: true
  register: cert

- name: Get a cert from an https port
  get_certificate:
    host: "www.google.com"
    port: 443
  delegate_to: localhost
  run_once: true
  register: cert

- name: How many days until cert expires
  debug:
    msg: "cert expires in: {{ expire_days }} days."
  vars:
    expire_days: "{{ (( cert.not_after | to_datetime('%Y%m%d%H%M%SZ')) - (ansible_date_time.iso8601 | to_datetime('%Y-%m-%dT%H:%M:%SZ')) ).days }}"
'''

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_bytes
from ansible.module_utils import crypto as crypto_utils

from distutils.version import LooseVersion
from os.path import isfile
from socket import setdefaulttimeout, socket
from ssl import get_server_certificate, DER_cert_to_PEM_cert, CERT_NONE, CERT_OPTIONAL

import atexit
import base64
import datetime
import traceback

MINIMAL_PYOPENSSL_VERSION = '0.15'
MINIMAL_CRYPTOGRAPHY_VERSION = '1.6'

CREATE_DEFAULT_CONTEXT_IMP_ERR = None
try:
    from ssl import create_default_context
except ImportError:
    CREATE_DEFAULT_CONTEXT_IMP_ERR = traceback.format_exc()
    HAS_CREATE_DEFAULT_CONTEXT = False
else:
    HAS_CREATE_DEFAULT_CONTEXT = True

PYOPENSSL_IMP_ERR = None
try:
    import OpenSSL
    from OpenSSL import crypto
    PYOPENSSL_VERSION = LooseVersion(OpenSSL.__version__)
except ImportError:
    PYOPENSSL_IMP_ERR = traceback.format_exc()
    PYOPENSSL_FOUND = False
else:
    PYOPENSSL_FOUND = True

CRYPTOGRAPHY_IMP_ERR = None
try:
    import cryptography
    import cryptography.exceptions
    import cryptography.x509
    from cryptography.hazmat.backends import default_backend as cryptography_backend
    CRYPTOGRAPHY_VERSION = LooseVersion(cryptography.__version__)
except ImportError:
    CRYPTOGRAPHY_IMP_ERR = traceback.format_exc()
    CRYPTOGRAPHY_FOUND = False
else:
    CRYPTOGRAPHY_FOUND = True

def main():
    module = AnsibleModule(
        argument_spec=dict(
            ca_cert=dict(type='path'),
            host=dict(type='str', required=True),
            port=dict(type='int', required=True),
            proxy_host=dict(type='str'),
            proxy_port=dict(type='int', default=8080),
            timeout=dict(type='int', default=10),
            select_crypto_backend=dict(type='str', choices=['auto', 'pyopenssl', 'cryptography'], default='auto'),
        ),
    )

    ca_cert = module.params.get('ca_cert')
    host = module.params.get('host')
    port = module.params.get('port')
    proxy_host = module.params.get('proxy_host')
    proxy_port = module.params.get('proxy_port')
    timeout = module.params.get('timeout')

    backend = module.params.get('select_crypto_backend')
    if backend == 'auto':
        # Detection what is possible
        can_use_cryptography = CRYPTOGRAPHY_FOUND and CRYPTOGRAPHY_VERSION >= LooseVersion(MINIMAL_CRYPTOGRAPHY_VERSION)
        can_use_pyopenssl = PYOPENSSL_FOUND and PYOPENSSL_VERSION >= LooseVersion(MINIMAL_PYOPENSSL_VERSION)

        # First try cryptography, then pyOpenSSL
        if can_use_cryptography:
            backend = 'cryptography'
        elif can_use_pyopenssl:
            backend = 'pyopenssl'

        # Success?
        if backend == 'auto':
            module.fail_json(msg=("Can't detect any of the required Python libraries "
                                  "cryptography (>= {0}) or PyOpenSSL (>= {1})").format(
                                      MINIMAL_CRYPTOGRAPHY_VERSION,
                                      MINIMAL_PYOPENSSL_VERSION))

    if backend == 'pyopenssl':
        if not PYOPENSSL_FOUND:
            module.fail_json(msg=missing_required_lib('pyOpenSSL >= {0}'.format(MINIMAL_PYOPENSSL_VERSION)),
                             exception=PYOPENSSL_IMP_ERR)
        module.deprecate('The module is using the PyOpenSSL backend. This backend has been deprecated', version='2.13')
    elif backend == 'cryptography':
        if not CRYPTOGRAPHY_FOUND:
            module.fail_json(msg=missing_required_lib('cryptography >= {0}'.format(MINIMAL_CRYPTOGRAPHY_VERSION)),
                             exception=CRYPTOGRAPHY_IMP_ERR)

    result = dict(
        changed=False,
    )

    if not PYOPENSSL_FOUND:
        module.fail_json(msg=missing_required_lib('pyOpenSSL >= 0.15'), exception=PYOPENSSL_IMP_ERR)

    if timeout:
        setdefaulttimeout(timeout)

    if ca_cert:
        if not isfile(ca_cert):
            module.fail_json(msg="ca_cert file does not exist")

    if proxy_host:
        if not HAS_CREATE_DEFAULT_CONTEXT:
            module.fail_json(msg='To use proxy_host, you must run the get_certificate module with Python 2.7 or newer.',
                             exception=CREATE_DEFAULT_CONTEXT_IMP_ERR)

        try:
            connect = "CONNECT %s:%s HTTP/1.0\r\n\r\n" % (host, port)
            sock = socket()
            atexit.register(sock.close)
            sock.connect((proxy_host, proxy_port))
            sock.send(connect.encode())
            sock.recv(8192)

            ctx = create_default_context()
            ctx.check_hostname = False
            ctx.verify_mode = CERT_NONE

            if ca_cert:
                ctx.verify_mode = CERT_OPTIONAL
                ctx.load_verify_locations(cafile=ca_cert)

            cert = ctx.wrap_socket(sock, server_hostname=host).getpeercert(True)
            cert = DER_cert_to_PEM_cert(cert)
        except Exception as e:
            module.fail_json(msg="Failed to get cert from port with error: {0}".format(e))

    else:
        try:
            cert = get_server_certificate((host, port), ca_certs=ca_cert)
        except Exception as e:
            module.fail_json(msg="Failed to get cert from port with error: {0}".format(e))

    result['cert'] = cert

    if backend == 'pyopenssl':
        x509 = crypto.load_certificate(crypto.FILETYPE_PEM, cert)
        result['subject'] = {}
        for component in x509.get_subject().get_components():
            result['subject'][component[0]] = component[1]

        result['expired'] = x509.has_expired()

        result['extensions'] = []
        extension_count = x509.get_extension_count()
        for index in range(0, extension_count):
            extension = x509.get_extension(index)
            result['extensions'].append({
                'critical': extension.get_critical(),
                'asn1_data': extension.get_data(),
                'name': extension.get_short_name(),
            })

        result['issuer'] = {}
        for component in x509.get_issuer().get_components():
            result['issuer'][component[0]] = component[1]

        result['not_after'] = x509.get_notAfter()
        result['not_before'] = x509.get_notBefore()

        result['serial_number'] = x509.get_serial_number()
        result['signature_algorithm'] = x509.get_signature_algorithm()

        result['version'] = x509.get_version()

    elif backend == 'cryptography':
        x509 = cryptography.x509.load_pem_x509_certificate(to_bytes(cert), cryptography_backend())
        result['subject'] = {}
        for attribute in x509.subject:
            result['subject'][crypto_utils.cryptography_oid_to_name(attribute.oid, short=True)] = attribute.value

        result['expired'] = x509.not_valid_after < datetime.datetime.utcnow()

        result['extensions'] = []
        for dotted_number, entry in crypto_utils.cryptography_get_extensions_from_cert(x509).items():
            oid = cryptography.x509.oid.ObjectIdentifier(dotted_number)
            result['extensions'].append({
                'critical': entry['critical'],
                'asn1_data': base64.b64decode(entry['value']),
                'name': crypto_utils.cryptography_oid_to_name(oid, short=True),
            })

        result['issuer'] = {}
        for attribute in x509.issuer:
            result['issuer'][crypto_utils.cryptography_oid_to_name(attribute.oid, short=True)] = attribute.value

        result['not_after'] = x509.not_valid_after.strftime('%Y%m%d%H%M%SZ')
        result['not_before'] = x509.not_valid_before.strftime('%Y%m%d%H%M%SZ')

        result['serial_number'] = x509.serial_number
        result['signature_algorithm'] = crypto_utils.cryptography_oid_to_name(x509.signature_algorithm_oid)

        # We need the -1 offset to get the same values as pyOpenSSL
        if x509.version == cryptography.x509.Version.v1:
            result['version'] = 1 - 1
        elif x509.version == cryptography.x509.Version.v3:
            result['version'] = 3 - 1
        else:
            result['version'] = "unknown"

    module.exit_json(**result)

if __name__ == '__main__':
    main()


from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from starlette.responses import RedirectResponse
from ppt_gen import pdf_to_ppt
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import cosine_sim;
import downim;

class Upload(BaseModel):
    filepath: str
    name: str
class Search(BaseModel):
    text: str
    types: str

app = FastAPI()
app.mount("/static", StaticFiles(directory='static'), name='static')
origins = [
    "http://localhost:4200",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get('/')
async def index():
    return "Welcome to SIH"

@app.post('/search')
async def search(search: Search):
    # search.types => answer or visualise
    # text => TExt to get answer to or visualise
    if (search.types == 'answer'):
        text = cosine_sim.find_answer(search.text);
        return {'result':[{'text': text, 'voice': '', 'image':''}]}
    else:
        result = downim.main(search.text);
        print(result);
        return {'result':result}
@app.post('/pdfpptgen')
async def pdfpptgen(upload: Upload):
    directory, name, qa_pair = pdf_to_ppt(upload.filepath, upload.name)
    # response = RedirectResponse(url='/static/'+ name)
    return {"filepath": 'http://localhost:8000/'+name,
            "questions": qa_pair}


from delira import get_backends
import unittest
import numpy as np
from functools import partial

from delira.data_loading import AbstractDataset, BaseDataManager

class DummyDataset(AbstractDataset):
    def __init__(self, length):
        super().__init__(None, None)
        self.length = length

    def __getitem__(self, index):
        return {"data": np.random.rand(32),
                "label": np.random.randint(0, 1, 1)}

    def __len__(self):
        return self.length

    def get_sample_from_index(self, index):
        return self.__getitem__(index)

class TestPredictor(unittest.TestCase):

    def setUp(self):

        self.dset = DummyDataset(20)
        self.dmgr = BaseDataManager(self.dset, 4, 1, transforms=None)

    # @unittest.skip
    @unittest.skipIf("TF" not in get_backends(),
                     reason="No TF Backend installed")
    def test_predictor_tf(self):
        from delira.training import Predictor
        from delira.models.classification import ClassificationNetworkBaseTf
        from delira.models import AbstractTfNetwork
        from delira.training.train_utils import convert_tf_tensor_to_npy,\
            initialize_uninitialized

        import tensorflow as tf

        class DummyNetwork(ClassificationNetworkBaseTf):
            def __init__(self):
                AbstractTfNetwork.__init__(self)
                self.model = self._build_model(1)

                images = tf.placeholder(shape=[None, 32],
                                        dtype=tf.float32)
                labels = tf.placeholder_with_default(
                    tf.zeros(
                        [tf.shape(images)[0], 1]),
                    shape=[None, 1])

                preds_train = self.model(images, training=True)
                preds_eval = self.model(images, training=False)

                self.inputs["images"] = images
                self.inputs["labels"] = labels
                self.outputs_train["pred"] = preds_train
                self.outputs_eval["pred"] = preds_eval

            @staticmethod
            def _build_model(n_outputs):
                return tf.keras.models.Sequential(
                    layers=[
                        tf.keras.layers.Dense(
                            64,
                            input_shape=(
                                32,
                            ),
                            bias_initializer='glorot_uniform'),
                        tf.keras.layers.ReLU(),
                        tf.keras.layers.Dense(
                            n_outputs,
                            bias_initializer='glorot_uniform')])

        net = DummyNetwork()
        initialize_uninitialized(net._sess)

        predictor = Predictor(
            net,
            {"images": "data"},
            # prepare_batch_fn=partial(DummyNetwork.prepare_batch,
            #                                          input_device="cpu",
            #                                          output_device="cpu"),
            convert_batch_to_npy_fn=convert_tf_tensor_to_npy
        )

        # add singleton dimension for test case
        batch = self.dset[0]
        batch['data'] = batch['data'][np.newaxis, :]

        pred = predictor.predict(batch)

        pred_dmgr = predictor.predict_data_mgr(self.dmgr)

    @unittest.skipIf("TORCH" not in get_backends(),
                     reason="No Torch Backend Installed")
    def test_predictor_torch(self):
        from delira.training import Predictor
        from delira.models import ClassificationNetworkBasePyTorch
        from delira.training.train_utils import convert_torch_tensor_to_npy

        import torch

        class DummyNetwork(ClassificationNetworkBasePyTorch):

            def __init__(self):
                super().__init__(32, 1)

            def forward(self, x):
                return {"pred": self.module(x)}

            @staticmethod
            def _build_model(in_channels, n_outputs):
                return torch.nn.Sequential(
                    torch.nn.Linear(in_channels, 64),
                    torch.nn.ReLU(),
                    torch.nn.Linear(64, n_outputs)
                )

            @staticmethod
            def prepare_batch(batch_dict, input_device, output_device):
                return {"data": torch.from_numpy(batch_dict["data"]
                                                 ).to(input_device,
                                                      torch.float),
                        "label": torch.from_numpy(batch_dict["label"]
                                                  ).to(output_device,
                                                       torch.long)}

        predictor = Predictor(
            DummyNetwork(),
            {"x": "data"},
            convert_torch_tensor_to_npy,
            prepare_batch_fn=partial(DummyNetwork.prepare_batch,
                                     input_device="cpu", output_device="cpu")
        )

        pred = predictor.predict(self.dset[0])

        # print(pred)
        pred_dmgr = predictor.predict_data_mgr(self.dmgr)
        # print(pred_dmgr)

if __name__ == '__main__':
    from multiprocessing import freeze_support
    freeze_support()
    unittest.main()


import argparse
import os
import urllib, json
from json_extract import flatten_json
import csv
import pandas
import math

arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("-i", "--input", required=True, help="Input file")
arg_parser.add_argument("-o", "--output", required=True, help="output file location")
args = vars(arg_parser.parse_args())

src = args['input']
output = args['output']

url_header = "http://localhost:4000/v1/autocomplete?text="

counter = 0
total = 0

print("Reading in name columns from the CSV...")
parsed_data = pandas.read_csv(src, names = ['city', 'library', 'address', 'zipcode', 'phone'], sep='\t')
print("Done")
latandlong = pandas.DataFrame(columns=['latitude', 'longitude'])

latitudes = []
longitudes = []
for index, row in parsed_data.iterrows():
    if total % 10000 == 0:
        print(str(total) + " processed")
    if (str(row['zipcode']) == 'nan'):
        full_location_dirty = str(row['address']) + ", " + str(row['city']) + ", FL"
    else:
        full_location_dirty = str(row['address']) + ", " + str(row['city']) + ", FL " + str(int(row['zipcode']))
    full_location = ' '.join(full_location_dirty.split())
    print(full_location)
    response = urllib.urlopen(url_header + full_location)
    json_data = json.loads(response.read())
    coords = flatten_json(json_data)
    try:
        latitudes.append(coords['features_0_geometry_coordinates_1'])
        longitudes.append(coords['features_0_geometry_coordinates_0'])
    except KeyError:
        counter = counter + 1
        latitudes.append("")
        longitudes.append("")
        # print(full_location + " has no available coordinates, maybe its an address on a highway?")
    total = total + 1

parsed_data['latitude'] = pandas.Series(latitudes)
parsed_data['longitude'] = pandas.Series(longitudes)
parsed_data.to_csv(output, mode='w', header=True, sep='\t', index=False)
print(str(counter) + " / " + str(total) + " had missing latitude/longitude")


# -*- coding: utf-8 -*-
from django.contrib import admin

from .models import ArtnUserProfile, WishItem, ArtnUserPoint, AddAndUse, MemberOutInfo
from .forms import ArtnUserProfileAdminForm, WishItemAdminForm, ArtnUserPointAdminForm

@admin.register(ArtnUserProfile)
class ArtnUserProfileAdmin(admin.ModelAdmin):
    form = ArtnUserProfileAdminForm
    list_display = ('user', 'user_phone', 'full_address',)
    list_display_links = ('user',)
    list_per_page = 20
    search_fields = ('user__username', 'shipping_name',)

    fieldsets = (
        ('사용자 개인정보', {'fields': ('user_phone',)}),
        ('최근 배송지 정보', {'fields': ('shipping_name', 'shipping_phone_1', 'shipping_phone_2', 'shipping_zip',
                                  'shipping_address_1', 'shipping_address_2', )}),
    )

@admin.register(WishItem)
class WishItemAdmin(admin.ModelAdmin):
    form = WishItemAdminForm
    list_display = ('a_code', 'a_image', 'user', 'is_active', 'created_at')
    list_display_links = ('a_code', 'user')
    search_fields = ('artwork__title', 'artwork__artwork_code',
                     'user__username', 'user__id')

class AddAndUseInline(admin.TabularInline):
    model = AddAndUse
    extra = 0

@admin.register(ArtnUserPoint)
class ArtnUserPointAdmin(admin.ModelAdmin):
    form = ArtnUserPointAdminForm
    list_display = ('user', 'now_points', 'date_of_registration', 'first_buy_info',
                    'last_buy_info', 'count_buy_info', 'total_amount_for_user', 'recent_login_date')
    list_display_links = ('user',)
    search_fields = ('user__username', 'user__email',)
    list_per_page = 20
    inlines = [AddAndUseInline]

@admin.register(MemberOutInfo)
class MemberOUtInfoAdmin(admin.ModelAdmin):
    list_display = ('email_id', 'reason_1', 'reason_2', 'reason_3', 'reason_4',
                    'reason_5', 'reason_6', 'reason_7', 'reason_8', 'reason_9', 'created_at')
    list_filter = ('reason_1', 'reason_2', 'reason_3', 'reason_4',
                   'reason_5', 'reason_6', 'reason_7', 'reason_8', 'reason_9',)
    search_fields = ('email_id',)


# Run time around 700 seconds for 1,000,000 lines
# (As tested by using time module)

import random

def scramble(in_filename, out_filename):
    '''
    (str(in_filename), str(out_filename)) -> text
    Scramble the lines of 'in_filename' and write them
    into file 'out_filename', with prefix.
    '''
    with open(in_filename, 'r') as f:
        n = 1  # Start numeration at 1
        lines = []  # List of line numbers
        byte = {}  # Take down byte position for start of each line
        line = " "  # Set line premise
        while line != "":
            lines.append(n)
            byte[n] = str(f.tell())
            n += 1
            line = f.readline()
        lines.remove(n-1)  # There is one extra line added
        random.shuffle(lines)  # Shuffle the lines
        with open(out_filename, 'w') as g:
            m = 0
            for i in lines:
                f.seek(int(byte[i]))  # Find line
                line = f.readline()  # Take down the line
                g.write(str(i) + ':' + line)  # Copy line
                if '\n' not in line:
                    g.write('\n')
                        # Make sure go to next line


from tsp_genetic_python import *

class Route:

    def __init__(self):
        # initiates a route attribute equal to a randomly shuffled list_of_cities
        self.route = sorted(city_list, key=lambda *args: random.random())
        ### Calculates its length:
        self.recheck_rt_len()

    def recheck_rt_len(self):

        # Zeroes its length
        self.length = 0.0
        # for every city in its route attribute:
        for city in self.route:
            # set up a next city variable that points to the next city in the list
            # and wraps around at the end:
            next_city = self.route[self.route.index(city)-len(self.route)+1]
            # Uses the first city's distance_to attribute to find the distance to the next city:
            dist_to_next = city.dis_to[next_city.name]
            # adds this length to its length attr.
            self.length += dist_to_next

    def print_cities_route(self, print_route=False):

        cities_str = ''
        for city in self.route:
            cities_str += city.name + ','
        cities_str = cities_str[:-1] # chops off last comma
        if print_route:
            print('    ' + cities_str)

    def print_city_coordinates(self):

        cities_str = '|'
        for city in self.route:
            cities_str += str(city.x) + ',' + str(city.y) + '|'
        print(cities_str)

    def check_validity_rt(self):
        '''
        self --> Bool()

        Returns True if the route contains all cities in list_of_cities ONCE and ONLY ONCE
        i.e. returns False if there are duplicates.

        Use: if there are multiples of the same city in a route,
        it will converge until all the cities are that same city (length = 0)
        '''
        for city in city_list:
            # helper function defined up to
            if self.count_mult(self.route,lambda c: c.name == city.name) > 1:
                return False
        return True

    # Returns the number of pred in sequence (duplicate checking.)
    def count_mult(self, seq, pred):
        return sum(1 for v in seq if pred(v))

# Contains a population of Route() objects
class population_route(object):

    def __init__(self, size, init):
        self.rt_pop = []
        self.size = size
        # If we want to initialise a population.rt_pop:
        if init:
            for x in range(0,size):
                new_rt = Route()
                self.rt_pop.append(new_rt)
            self.set_fit()

    def set_fit(self):
        '''
        self --> Route()

        Returns the two shortest routes in the population, in a list called self.top_two
        '''
        # sorts the list based on the routes' lengths
        sorted_list = sorted(self.rt_pop, key=lambda x: x.length, reverse=False)
        self.fittest = sorted_list[0]
        return self.fittest


"""

Description: This program takes numeric input and prints the * pattern accordingly.

"""

def main():
    totalNo = int(input('How many line you have to enter:'))

    if totalNo != 0:
        mylist = []
        for i in range(0, totalNo):
            number = int(input('Enter number:'))
            mylist.append(number)

        # print(list)

        for i in range(0, totalNo):
            for j in range(0, mylist[i]):
                print('*', end='')
            print('')

    else:
        print("Invalid Input")

if __name__ == '__main__':
    main()


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

from django.db import models, migrations
import catalog.models

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
                ('name', models.CharField(max_length=30, verbose_name='Nom')),
                ('slug', models.SlugField(verbose_name='Slug', unique=True, max_length=100)),
                ('description', models.TextField(verbose_name='Description')),
                ('order', models.SmallIntegerField(verbose_name='Ordre')),
                ('picture', models.ImageField(upload_to=catalog.models.Category.get_file_path, verbose_name='Image')),
            ],
            options={
                'ordering': ('order', 'name'),
                'verbose_name_plural': 'Gestions des Catégories',
                'verbose_name': 'Gestions des Catégories',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Categoryincategory',
            fields=[
                ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
                ('order', models.SmallIntegerField(verbose_name='Ordre')),
                ('daughter', models.ForeignKey(verbose_name='Catégorie fille', related_name='daughter', to='catalog.Category')),
                ('selfcategory', models.ForeignKey(null=True, verbose_name='Catégorie mére', related_name='selfcategory', to='catalog.Category')),
            ],
            options={
                'ordering': ('order',),
                'verbose_name_plural': 'hiérarchie  des catégories',
                'verbose_name': 'hiérarchie  des catégories',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Product',
            fields=[
                ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
                ('sku', models.CharField(max_length=6, verbose_name='Référence', unique=True)),
                ('name', models.CharField(max_length=30, verbose_name='Nom')),
                ('slug', models.SlugField(verbose_name='Slug', unique=True, max_length=100)),
                ('description', models.TextField(verbose_name='Description')),
                ('package', models.CharField(max_length='15', default='Unité', verbose_name='Conditionement')),
                ('price', models.DecimalField(max_digits=5, decimal_places=2, verbose_name='Prix')),
                ('picture', models.ImageField(upload_to=catalog.models.Product.get_file_path, default='default.png', verbose_name='Images')),
            ],
            options={
                'ordering': ('sku', 'name'),
                'verbose_name_plural': 'Gestions des Produits',
                'verbose_name': 'Gestions des Produits',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Productincategory',
            fields=[
                ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
                ('order', models.SmallIntegerField(verbose_name='Ordre')),
                ('category', models.ForeignKey(verbose_name='Catégorie', to='catalog.Category')),
                ('product', models.ForeignKey(verbose_name='Produit', to='catalog.Product')),
            ],
            options={
                'ordering': ('order',),
                'verbose_name_plural': 'Lien produit categories',
                'verbose_name': 'Lien produit categories',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Taxrate',
            fields=[
                ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
                ('name', models.CharField(max_length='15', verbose_name='Taxe')),
                ('taxvalue', models.DecimalField(max_digits=5, decimal_places=2, verbose_name='Taux')),
            ],
            options={
                'ordering': ('taxvalue', 'name'),
                'verbose_name_plural': 'Taxes',
                'verbose_name': 'Taxes',
            },
            bases=(models.Model,),
        ),
        migrations.AddField(
            model_name='product',
            name='appliedtax',
            field=models.ForeignKey(verbose_name='Taxe', default=1, to='catalog.Taxrate'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='category',
            name='daughters',
            field=models.ManyToManyField(through='catalog.Categoryincategory', null=True, verbose_name='Catégories fille', to='catalog.Category'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='category',
            name='products',
            field=models.ManyToManyField(through='catalog.Productincategory', default='default.png', verbose_name='Produits', to='catalog.Product'),
            preserve_default=True,
        ),
    ]


import logging
from math import ceil
from pony.orm.core import TransactionError

from arrnounced import db, irc, utils
from arrnounced.backend import renotify, get_configured_backends, get_backend
from arrnounced.eventloop_utils import eventloop_util
from arrnounced.announcement import Announcement

logger = logging.getLogger("WEB-HANDLER")

def get_tracker_status():
    return irc.get_connected()

# Request to check this torrent again
def _locked_notify(announcement_id, backend):
    with db.db_session:
        db_announcement = db.get_announcement(announcement_id)

    if db_announcement is None or len(db_announcement.title) == 0:
        logger.warning("Announcement to notify not found in database")
        return False

    logger.debug("Checking announcement again: %s", db_announcement.title)
    announcement = Announcement(
        db_announcement.title,
        db_announcement.torrent,
        indexer=db_announcement.indexer,
        date=db_announcement.date,
    )

    future = eventloop_util.run(renotify(announcement, backend))
    if future.result():
        announcement.snatched()
        logger.debug("%s accepted the torrent this time!", backend.name)
        with db.db_session:
            db_announcement = db.get_announcement(db_announcement.id)
            db.insert_snatched(db_announcement, announcement.snatch_date, backend.name)
        return True

    logger.debug("%s still refused this torrent...", backend.name)
    return False

def notify_backend(announcement_id, backend_name):
    backend = get_backend(backend_name)
    if backend:
        try:
            return _locked_notify(announcement_id, backend)
        except TransactionError:
            logger.exception("Database transaction failed")
    else:
        logger.warning(
            "Could not find the requested backend '%s'",
            backend_name,
        )

    return False

def get_page_counts(page_size):
    try:
        with db.db_session:
            return (
                ceil(db.get_announced_count() / page_size),
                ceil(db.get_snatched_count() / page_size),
            )
    except TransactionError as e:
        logger.error("%s: %s", type(e).__name__, e)

    return 0, 0

def get_announced_page(page_nr, page_size):
    try:
        with db.db_session:
            return (
                [
                    e.serialize(utils.human_datetime)
                    for e in db.get_announced(limit=page_size, page=page_nr)
                ],
                get_configured_backends(),
            )
    except TransactionError as e:
        logger.error("%s: %s", type(e).__name__, e)

    return [], []

def get_snatched_page(page_nr, page_size):
    try:
        with db.db_session:
            return [
                db.snatched_to_dict(e, utils.human_datetime)
                for e in db.get_snatched(limit=page_size, page=page_nr)
            ]
    except TransactionError as e:
        logger.error("%s: %s", type(e).__name__, e)

    return []


import os

from setuptools import setup, find_packages

base_dir = os.path.dirname(__file__)

with open(os.path.join(base_dir, 'sputnik', 'about.py')) as f:
    about = {}
    exec(f.read(), about)

with open(os.path.join(base_dir, 'README.rst')) as f:
    readme = f.read()

setup(
    zip_safe=True,
    name=about['__title__'],
    version=about['__version__'],
    description=about['__summary__'],
    long_description=readme,
    url=about['__uri__'],
    author=about['__author__'],
    author_email=about['__email__'],
    license=about['__license__'],
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'sputnik = sputnik.__main__:main'
        ]
    },
    install_requires=['semver'],
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: MIT License',
        'Operating System :: POSIX :: Linux',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: Microsoft :: Windows',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Topic :: Scientific/Engineering']
)


"""
A very basic multi-service framework.
It only supports IRC as of now, but I plan on supporting more protocols in the future.

How it works:

Create a new session instance.
A session requires a protocol, which you can import from the utils dir:

from utils.protocol.irc import irc

You can define a session as follows:

new_session = Session(protocol=irc.IRC)

Now, `new_session` is a session instance with IRC protocol support.
We need to set certain IRC attributes before starting the session.

Required:
new_session.nickname = <string>     You can put question marks ("?") in the nickname for a random number.
new_session.server = <string>       Server hostname or IP to connect to.
new_session.port = <int>            Port of the server, must be an integer.

Optional:
new_session.tls = <bool>            Set to a true boolean to use TLS.
new_session.cert = <string>         Path to your *.pem file.
new_session.channel = <string       Channel to join upon connect.
new_session.alt_nick = <string>     Alternative nickname, in case the given nickname is already in use.
                                    If it happens with this option disabled, it will append some random
                                    numbers at the end of your nick.
new_session.logging = <bool>        Enables or disabled logging. True by default.

We have completed our IRC session instance, we can start it now:

new_session.start()

Your new_session object will connect to the server,
and if you specified a channel, it will join it once connected.
An example can be found at the bottom of this file.

In the Session object, you can interact with your session by making it respond to events.
The handle_event() method is where all the events are being processed.
You can use this to write your own methods and modules.
I already wrote a few examples in the Session class below.
However, it is recommended to code all your functionality into modules.

At the time of writing, the IRC protocol only supports the following events:

irc.IRCEvent.ERROR
irc.IRCEvent.PING
irc.IRCEvent.PRIVMSG
irc.IRCEvent.NOTICE
irc.IRCEvent.JOIN
irc.IRCEvent.PART
irc.IRCEvent.KICK
irc.IRCEvent.MODE
irc.IRCEvent.NICK
irc.IRCEvent.QUIT

You can open utils/protocol/irc/irc.py to add new event support in IRC.get_events() method if you wish.

An example IRC module can be found in the utils/protocol/irc/modules directory.
"""

import threading

from utils.classes import AbstractClass
from utils.logger import logging

# Import your protocol.
from utils.protocol.irc import irc

class Session(AbstractClass, threading.Thread):
    def __init__(self, protocol):
        threading.Thread.__init__(self)
        self.active = 0
        self.events = []
        self.logging = 1
        self.protocol = protocol(self)
        logging.debug(f'Protocol for this session set: {self.protocol}')

    def run(self):
        self.protocol.run()

    def handle_event(self, event_queue):
        """
        We can search for predefined event hooks and interact with them based on the received events.
        """
        for got_events in event_queue:
            event, argument = got_events
            if str(self.protocol) == "IRC":
                """
                Methods inherited from the IRC protocol:

                self.protocol.join(channel)             Join a channel.
                self.protocol.part(channel)             Leave a channel.
                self.protocol.nick(newnick)             Change your nickname.

                Methods inherited from AbstractClass:

                self.say(text, target=None)             Delivers a message on IRC. If target is omitted (default),
                                                        current event_target_obj will be used. This is what you
                                                        want in most situations.
                                                        This method is called from the AbstractClass instead of the
                                                        Protocol class, because different protocols use different
                                                        ways to communicate.

                self.quit()                             Disconnects from IRC and closes the session.
                """
                IRCEvent = irc.IRCEvent

                if event == IRCEvent.PRIVMSG:
                    if str(self.event_target_obj) == self.nickname:
                        logging.debug(f'I got a private message from: {self.event_user_obj}')
                        logging.debug(f'All users: {self.users}')
                        logging.debug(f'All channels: {self.channels}')

                    if argument[0] == '!whoareyou':
                        self.say(self.nickname)

                    if argument[0] == '!sessions':
                        self.say(self.sessions)

                    if argument[0] == '!bye':
                        self.quit('Byebye!')

                    if argument[0] == '!logging':
                        if len(argument) > 1 and argument[1].lower() in ['on', 'off']:
                            if argument[1].lower() == 'on':
                                logging.disable(False)
                                self.say('Logging enabled.')
                            else:
                                logging.disable()
                                self.say('Logging disabled.')

                    if argument[0] == '!listusers':
                        for u in self.users:
                            self.say(u)
                        for c in self.channels:
                            self.say(c)
                            for u in c.users:
                                self.say(f"> {u}")

                    if argument[0] == '!reload':
                        self.say('Reloading all modules...')
                        for module in list(self.modules):
                            self.protocol.reload_module(module)
                        self.say('Done!')

                    if argument[0] == '!modules':
                        for m in self.modules:
                            self.say(m)

                    if argument[0] == '!raw':
                        self.sendline(' '.join(argument[1:]))

    def __repr__(self):
        if hasattr(self, 'protocol'):
            return f'<Session "{repr(self.protocol)}">'
        return '<Session>'

server = "irc.provisionweb.org"
port = 6697

new_session = Session(protocol=irc.IRC)

new_session.nickname = "sif-???"
# new_session.alt_nick = "alternative_nickname"
new_session.server = server
new_session.port = port
new_session.tls = 1
# new_session.cert = "/path/to/cert.pem"
new_session.channel = "#bla"

new_session.start()


def count(ls):
    even=0
    odd=0
    for i in ls:
        if i%2==0:
            even+=1
        else:
            odd+=1
    return even,odd

lst=[2,5,7,6,8,9,5,10,14,8]
even,odd=count(lst)
print ('even:',even,'\n''odd: ',odd)
print('Even : {} and Odd : {}'.format(even,odd))


# -*- coding: utf-8 -*-
"""
Created on May 2 2020

@author: adioj
"""

#import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#Network from scratch

#Input layer
inp = keras.layers.Input(shape = (128,128,3))

#Block #1
b1_c1 = keras.layers.Conv2D(32,kernel_size = (3, 3), activation = 'relu', padding = 'same')(inp)
b1_c2 =keras.layers.Conv2D(32,kernel_size = (3, 3), activation = 'relu', padding = 'same')(b1_c1)
b1_pool = keras.layers.MaxPool2D(pool_size = (2,2))(b1_c2)

#Block #2
b2_c1 = keras.layers.Conv2D(64,kernel_size = (3, 3), activation = 'relu', padding = 'same')(b1_pool)
b2_c2 = keras.layers.Conv2D(64,kernel_size = (3, 3), activation = 'relu', padding = 'same')(b2_c1)
b2_pool = keras.layers.MaxPool2D(pool_size = (2,2))(b2_c2)

#Block #3
b3_c1 = keras.layers.Conv2D(128,kernel_size = (3, 3), activation = 'relu', padding = 'same')(b2_pool)
b3_c2 = keras.layers.Conv2D(128,kernel_size = (3, 3), activation = 'relu', padding = 'same')(b3_c1)
b3_c3 = keras.layers.Conv2D(128,kernel_size = (3, 3), activation = 'relu', padding = 'same')(b3_c2)

flat = keras.layers.Flatten()(b3_c3)

hidden1 = keras.layers.Dense(256, activation = 'relu')(flat)

dropout1 = keras.layers.Dropout(rate = 0.3)(hidden1)

hidden2 = keras.layers.Dense(256, activation = 'relu')(dropout1)

dropout2 = keras.layers.Dropout(rate = 0.3)(hidden2)

out = keras.layers.Dense(2, activation = 'softmax')(dropout2)

model = keras.Model(inputs = inp, outputs = out)

sgd = keras.optimizers.SGD(lr= .0001)
adam = keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(optimizer=adam,
                loss='binary_crossentropy',
                metrics=['accuracy'])

# Summarize convolutional neural network architecture
model.summary()

""" Generate images"""

train_datagen = ImageDataGenerator(
        rescale = 1.0 / 255, # rescale the RGB values down to prevent overfitting
        rotation_range = 20, #Rotate some images to reduce overfitting
        width_shift_range = 0.1, #translate the image randomly by +/- 0.1 of the width of image
        height_shift_range = 0.1,
        shear_range = 0.2, #randomly distort the image by 0.2 (shear is a mathematical function)
        zoom_range = 0.2, #randomly zoom in on the image
        horizontal_flip = True,
        vertical_flip = True)
val_datagen = ImageDataGenerator (
        rescale = 1.0 / 255)

train_data_dir = "C:/Users/Default/Desktop/ML_data_sheets/model_testing"
img_height = 128
img_width = 128
val_data_dir = "C:/Users/Default/Desktop/ML_data_sheets/model_validation"

samples = 2000
epochs = 20
batch_size = 4

"""Generating training and validation images"""

train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size = (img_height, img_width),
        batch_size = batch_size,
        class_mode = 'categorical'
        )
val_generator = val_datagen.flow_from_directory(
        val_data_dir,
        target_size = (img_height, img_width),
        batch_size = batch_size,
        class_mode = 'categorical')

"""Fit the imges to the training set"""

history = model.fit_generator(
        train_generator,
        steps_per_epoch = samples,
        epochs = epochs,
        validation_data = val_generator,
        validation_steps = samples
        )


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

# *********************************************************************************** #
# Name: HICP | main.py                                                                #
# Author: WYCE | github.com/wycedev                                                   #
# Version: 2.0.1 | stable                                                             #
# Date: 2017-02-15 | 2017-03-06                                                       #
# Feel free to use and modify this program for your own purposes.                     #
# Please mention me and my GitHub page in your modification if you want publish it.   #
# *********************************************************************************** #

import os
import sys
import addition

from atexit import register, unregister
from subprocess import Popen, CREATE_NEW_CONSOLE

from ui import UI
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QInputDialog, QFileDialog

welcome = '''=========================================
= Welcome to HICP shell!                =
= Author: WYCE | github.com/wycedev     =
= Version: 2.0.1 | stable               =
= Type "!help" for a list of commands.  =
=========================================
'''

syntax = '''
===============================================
= This command takes the following arguments: =
= The needed arguments are written in <>.     =
= The optional arguments are written in [].   =
===============================================

ARGUMENT\tTYPE'''

class Shell:

    def __init__(self):
        self.logpath = addition.RUN.logpath
        print(welcome)

    @staticmethod
    def _syntax(needed={}, opt={}):
        if needed == {} and opt == {}:
            print("This command does not take any arguments.")
        else:
            print(syntax)
            for argument, _type in needed.items():
                tms = 1
                if len(argument) + 2 < 8:
                    tms += 1
                print('<' + argument + '>' + tms * '\t' + _type)
            for argument, _type in opt.items():
                tms = 1
                if len(argument) + 2 < 8:
                    tms += 1
                print('[' + argument + ']' + tms * '\t' + _type)
            print()

    def log(self, thing):
        if addition.RUN.log:
            with open(self.logpath + addition.RUN.date() + '_HICP.log', 'a+') as logfile:
                print('[' + addition.RUN.time() + '] ' + thing, file=logfile)

    def run(self, command, h=False, arglist=[]):
        if not h:
            return addition.run(command, arglist)
        else:
            self._syntax(addition.getdict(command, 'needed'), addition.getdict(command, 'opt'))

class App(QMainWindow):

    def __init__(self):
        if len(sys.argv) > 1:
            self.sysarg()
        super(App, self).__init__()
        self.ui = UI()
        self.ui.setupUi(self)
        self.init_ui()

    def get_cmd(self, cmd):
        HICP.log('in\t|  ' + cmd)
        cmd = cmd.split()
        if not cmd:
            pass
        else:
            if cmd[0][0] == '!':
                ret = HICP.run(cmd[0][1:], False, cmd[1:])
                HICP.log('out\t|  ' + str(ret).replace('\n', ' '))
                print(ret)
                if 'EXIT...' in str(ret):
                    HICP.log('- SESSION END -\n')
                    unregister(print)
                    sys.exit(0)
                elif 'RESTARTING...' in str(ret):
                    HICP.log('- SESSION END -\n')
                    unregister(print)
                    Popen(sys.argv[0], creationflags=CREATE_NEW_CONSOLE)
                    sys.exit(0)
            elif cmd[0][0] == '?':
                HICP.log('out\t|  printing syntax help...')
                HICP.run(cmd[0][1:], True)
            else:
                HICP.log('out\t|  ' + 'invalid command')
                print("error: invalid command")

    def get_cmd_wargs(self, cmd):
        t, ok = QInputDialog.getText(self, 'User input required', 'Please enter the required argument/s:')
        if ok:
            self.get_cmd(cmd + ' ' + t)
        else:
            HICP.log('out\t|  ' + 'user abortion')
            print('error: user abortion')

    def get_cmd_rec(self):
        f, dummy = QFileDialog.getSaveFileName(self, 'User input required', '',
                                               'HICP Files (*.hicp);;All Files (*.*)', options=QFileDialog.Options())
        self.get_cmd('!rec ' + f)

    def get_cmd_run(self):
        f, dummy = QFileDialog.getOpenFileName(self, 'User input required', '',
                                               'HICP Files (*.hicp);;All Files (*.*)', options=QFileDialog.Options())
        self.get_cmd('!run ' + f)

    def get_syntax(self):
        t, ok = QInputDialog.getText(self, 'User input required', 'Please enter the command you need syntax help for:')
        if ok:
            if t[0] == '!':
                t = t.strip('!')
            self.get_cmd('?' + t)
        else:
            HICP.log('out\t|  ' + 'user abortion')
            print('error: user abortion')

    def manual_input(self, cmd):
        self.ui.lineEdit.setText('')
        self.get_cmd(cmd)

    def init_ui(self):
        self.setWindowIcon(QIcon('hicp2.ico'))
        self.ui.help.clicked.connect(lambda: self.get_cmd('!help'))
        self.ui.math.clicked.connect(lambda: self.get_cmd_wargs('!math'))
        self.ui.os.clicked.connect(lambda: self.get_cmd_wargs('!os'))
        self.ui.os_dtch.clicked.connect(lambda: self.get_cmd_wargs('!os_dtch'))
        self.ui.date.clicked.connect(lambda: self.get_cmd('!date'))
        self.ui.time.clicked.connect(lambda: self.get_cmd('!time'))
        self.ui.rec.clicked.connect(lambda: self.get_cmd_rec())
        self.ui.stoprec.clicked.connect(lambda: self.get_cmd('!stoprec'))
        self.ui.run.clicked.connect(lambda: self.get_cmd_run())
        self.ui.github.clicked.connect(lambda: self.get_cmd('!github'))
        self.ui.chknet.clicked.connect(lambda: self.get_cmd('!chknet'))
        self.ui.info.clicked.connect(lambda: self.get_cmd('!info'))
        self.ui.togglelog.clicked.connect(lambda: self.get_cmd('!togglelog'))
        self.ui.clearlog.clicked.connect(lambda: self.get_cmd('!clearlog'))
        self.ui.logdir.clicked.connect(lambda: self.get_cmd('!logdir'))
        self.ui.copy2win.clicked.connect(lambda: self.get_cmd('!copy2win'))
        self.ui.restart.clicked.connect(lambda: self.get_cmd('!restart'))
        self.ui.exit.clicked.connect(lambda: self.get_cmd('!exit'))
        self.ui.exec.clicked.connect(lambda: self.manual_input(self.ui.lineEdit.text()))
        self.ui.syntax.clicked.connect(lambda: self.get_syntax())
        self.show()

    def sysarg(self):
        msg = 'args given, please wait...'
        print(msg)
        HICP.log('args\t|  ' + msg)
        if len(sys.argv) > 2:
            msg = "error: too many args given\nRestarting into prompt..."
            print(msg)
            HICP.log('args\t|  ' + msg)
        else:
            if os.path.exists(sys.argv[1]):
                self.get_cmd('!run ' + sys.argv[1])
            else:
                HICP.log('invalid args, restarting...')
                msg = "error: invalid arguments\nRestarting into prompt..."
                print(msg)
                HICP.log('args\t|  ' + msg)

HICP = Shell()
HICP.log('- SESSION START -')

_sysarg = False
if None:
    _sysarg = True

register(print, 'EXIT...')
app = QApplication(sys.argv)
w = App()
sys.exit(app.exec_())


import logging

from lib import Tools
from lib.processors.base.processor import Processor

class NumberProcessor(Processor):
    # Allowed: ,.-+e
    unwanted_chars = "a-df-zA-DF-Z;:_·<>\\/'\\|#@()\"\t\n\r!%&=\\\\?¡¿"

    def __init__(self):
        pass

    def process(self, input_string):

        # Trim spaces and remove unwanted chars:
        final_str = Tools.clean_string(input_string, NumberProcessor.unwanted_chars)
        final_str = Tools.clean_spaces(final_str)

        try:
            # Parse the number to the most general one (float)
            number = float(final_str)

            # Turn the number to a string again (to homogenise representation)
            final_str = str(number)
        except ValueError:
            logging.warning("Number string could not be parsed: "
                            + input_string + " (original)"
                            + final_str + " (after cleaning)")
            # If number cannot be parsed, return original string
            final_str = input_string

        return final_str

    def best(self, item_a, item_b):
        if item_a >= item_b:
            return item_a
        return item_b