from django.contrib import messages
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import redirect, get_object_or_404
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView, ListView,View,TemplateView
from django_datatables_view.base_datatable_view import BaseDatatableView
from django import forms
from django.forms import Form, CharField
from Ads_Project.functions import LoginRequiredMixin
from system.forms import IncreaseBalanceFrom
from system.models import Order, User, INCREASE_BALANCE_ORDER, History, Tabligh, COUNT_KHARI_HADAGHAL, TanzimatPaye ,PERFECT_USER_ID,PERFECT_TITLE,PERFECT_PASSPHRASE
from system.templatetags.app_filters import date_jalali
import hashlib
from datetime import datetime

def dargah_test_part_1(request):
    a = f"""
    next
    """
    return HttpResponse(a)

def dargah_test_part_2(request):
    pass

def dargah_test_part_2(request):
    pass

class IncreaseBalanceView(LoginRequiredMixin, FormView):
    template_name = 'system/Malli/Increase_Balance.html'
    form_class = IncreaseBalanceFrom
    success_url = reverse_lazy('ConfirmBalanceView')

    def get(self, request, *args, **kwargs):
        if request.GET.get('balance_increased'):
            messages.success(request, _("Your balance increased"))
        return super().get(request, *args, **kwargs)

    def form_valid(self, form):
        dargah_type = form.cleaned_data['dargah_type']
        how_much = form.cleaned_data['how_much']
        self.request.session['dargah_type'] = dargah_type
        self.request.session['how_much'] = how_much
        my_str=str(self.request.user.id)+' '+self.request.user.username+' '+ str(datetime.now())
        payment_id=hashlib.sha1(my_str.encode("UTF-8")).hexdigest()
        self.request.session['payment_id'] = payment_id
        Order.objects.create(type=INCREASE_BALANCE_ORDER,user=self.request.user,mablagh=float(how_much),payment_id=payment_id,status=0)
        '''user_id = self.request.POST.get('user', None)
        if user_id and (isinstance(user_id, int) or (isinstance(user_id, str) and user_id.isdigit())):
            user_id = int(user_id)
            if User.objects.filter(pk=user_id).exists():
                messages.error(self.request, _("User does not exist"))
                return super().form_invalid(form)

        else:
            user_id = self.request.user.id
        user = get_object_or_404(User, pk=int(user_id))
        user.add_to_kif_pool(form.cleaned_data['how_much'])
        History.objects.create(user=user, type='0', meghdar=int(form.cleaned_data['how_much']))'''

        return super(IncreaseBalanceView, self).form_valid(form)

    def form_invalid(self, form):
        return super().form_invalid(form)

class ConfirmBalanceView(LoginRequiredMixin, TemplateView):
    template_name = 'system/Malli/Confirmation_method.html'

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(object_list=object_list, **kwargs)
        if self.request.session.get('dargah_type')=='1':
            form = Form()
            form.fields = {}
            action_url="https://perfectmoney.is/api/step1.asp"
            how_much=self.request.session.get("how_much")
            submit_name="PAYMENT_METHOD"
            dargah_type="Perfect Money"
            PERFECT_USER_ID_VALUE=TanzimatPaye.get_settings(PERFECT_USER_ID,0)
            PUI = forms.CharField(initial=PERFECT_USER_ID_VALUE, widget=forms.HiddenInput())
            form.fields["PAYEE_ACCOUNT"] = PUI
            PAYMENT_ID = forms.CharField(initial=self.request.session.get('payment_id'), widget=forms.HiddenInput())
            form.fields["PAYMENT_ID"] = PAYMENT_ID
            PERFECT_TITLE_VALUE=TanzimatPaye.get_settings(PERFECT_TITLE,0)
            PT = forms.CharField(initial=PERFECT_TITLE_VALUE, widget=forms.HiddenInput())
            form.fields["PAYEE_NAME"] = PT
            PAYMENT_AMOUNT = forms.CharField(initial=self.request.session.get("how_much"), widget=forms.HiddenInput())
            form.fields["PAYMENT_AMOUNT"] = PAYMENT_AMOUNT
            PAYMENT_UNITS = forms.CharField(initial="USD", widget=forms.HiddenInput())
            form.fields["PAYMENT_UNITS"] = PAYMENT_UNITS
            STATUS_URL = forms.CharField(initial="http://9ed79c3b.ngrok.io/system/perfectpaymentstatus", widget=forms.HiddenInput())
            form.fields["STATUS_URL"] = STATUS_URL
            PAYMENT_URL = forms.CharField(initial="http://9ed79c3b.ngrok.io/system/perfectpaymentsuccess", widget=forms.HiddenInput())
            form.fields["PAYMENT_URL"] = PAYMENT_URL
            PAYMENT_URL_METHOD = forms.CharField(initial="GET", widget=forms.HiddenInput())
            form.fields["PAYMENT_URL_METHOD"] = PAYMENT_URL_METHOD
            NOPAYMENT_URL = forms.CharField(initial="http://9ed79c3b.ngrok.io/system/perfect_money_F", widget=forms.HiddenInput())
            form.fields["NOPAYMENT_URL"] = NOPAYMENT_URL
            NOPAYMENT_URL_METHOD = forms.CharField(initial="GET", widget=forms.HiddenInput())
            form.fields["NOPAYMENT_URL_METHOD"] = NOPAYMENT_URL_METHOD
            context["form"]=form
            context["action_url"]=action_url
            context["how_much"]=how_much
            context["submit_name"]=submit_name
            context["dargah_type"]=dargah_type
        return context

    def get(self, request, *args, **kwargs):
        if "payment_id" in request.session:
            return super().get(request, *args, **kwargs)
        else:
            messages.error(request, "فاکتور دریافتی اشتباه است.")
            return redirect(reverse_lazy('increase_balance'))

class PerfectMoneyFailed(View):
    def get(self, request, *args, **kwargs):
        if "payment_id" in request.session:
            print(request.session.get('payment_id'))
            print(request.GET)
            del request.session['payment_id']
            messages.error(request, "پرداخت شما موفقیت آمیز نبود.")
        else:
            messages.error(request, "فاکتور دریافتی اشتباه است.")

        return redirect(reverse_lazy('increase_balance'))

class HistoryMaliListView(LoginRequiredMixin, ListView):
    model = History
    template_name = 'system/Malli/List_malihistory.html'

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(object_list=object_list, **kwargs)
        return context

class HistoryMaliDatatableView(LoginRequiredMixin, BaseDatatableView):
    model = History
    columns = ['id', 'user', 'type', 'meghdar', 'created_at']

    def get_initial_queryset(self):
        user = self.request.user
        qs = super().get_initial_queryset()
        if user.is_superuser:
            return qs
        qs = qs.filter(user=user)
        return qs

    def render_column(self, row, column):
        if column == 'created_at':
            return date_jalali(row.created_at, 3)
        return super().render_column(row, column)

    def filter_queryset(self, qs):
        search = self.request.GET.get('search[value]', None)
        if self.request.user.is_superuser:
            if search:
                if search in _("deposit"):
                    qs = qs.filter(Q(type=0) | Q(meghdar__icontains=search) | Q(user__username__icontains=search))
                elif search in _("withdraw"):
                    qs = qs.filter(Q(type=1) | Q(meghdar__icontains=search | Q(user__username__icontains=search)))
                elif search in _("Transfer from income money to pocket money"):
                    qs = qs.filter(Q(type=2) | Q(meghdar__icontains=search) | Q(user__username__icontains=search))
                else:
                    qs = qs.filter(Q(meghdar__icontains=search) | Q(user__username__icontains=search))
        else:
            if search:
                if search in _("deposit"):
                    qs = qs.filter(Q(type=0) | Q(meghdar__icontains=search))
                elif search in _("withdraw"):
                    qs = qs.filter(Q(type=1) | Q(meghdar__icontains=search))
                elif search in _("Transfer from income money to pocket money"):
                    qs = qs.filter(Q(type=2) | Q(meghdar__icontains=search))
                else:
                    qs = qs.filter(Q(meghdar__icontains=search))
        return qs

class MoveDaramad2KifView(LoginRequiredMixin, FormView):
    template_name = 'system/Malli/move_daramad_kif.html'
    form_class = IncreaseBalanceFrom
    success_url = reverse_lazy('MoveDaramad2Kif')

    def get(self, request, *args, **kwargs):
        if request.GET.get('balance_increased'):
            messages.success(request, _("Your balance increased"))
        return super().get(request, *args, **kwargs)

    def form_valid(self, form):
        user = self.request.user
        get_value = int(form.cleaned_data['how_much'])

        indirect_allowed = user.allow_indirect()

        kif_daramad = user.get_kif_daramad()
        if (kif_daramad.current_recieved_direct + kif_daramad.current_recieved_indirect) >= get_value:
            if kif_daramad.current_recieved_indirect < kif_daramad.current_recieved_direct:
                if get_value >= kif_daramad.current_recieved_indirect:
                    get_value -= kif_daramad.current_recieved_indirect
                    kif_daramad.current_recieved_indirect = 0
                    if get_value > 0:
                        kif_daramad.current_recieved_direct -= get_value
                    kif_daramad.save()
                else:
                    kif_daramad.current_recieved_indirect -= get_value
                    kif_daramad.save()
            elif indirect_allowed:
                if get_value >= kif_daramad.current_recieved_direct:
                    get_value -= kif_daramad.current_recieved_direct
                    kif_daramad.current_recieved_direct = 0
                    if get_value > 0:
                        kif_daramad.current_recieved_indirect -= get_value
                    kif_daramad.save()
                else:
                    kif_daramad.current_recieved_direct -= get_value
                    kif_daramad.save()

            else:
                if get_value > kif_daramad.current_recieved_direct:
                    messages.error(self.request, _("Selected mount exceeds the withdrawable mount"))
                    return super(MoveDaramad2KifView, self).form_invalid(form)
                else:
                    kif_daramad.current_recieved_direct -= get_value
                    kif_daramad.save()
            get_value = int(form.cleaned_data['how_much'])
            kif_pol = user.get_kif_kif_pool()
            kif_pol.current_balance += int(get_value)
            kif_pol.save()
            History.objects.create(user=user, type='2', meghdar=int(form.cleaned_data['how_much']))
            messages.success(self.request, _("Selected mount is moved to your pocket money"))
            return super(MoveDaramad2KifView, self).form_valid(form)
        else:
            messages.error(self.request, _("Selected mount exceeds the withdrawable mount"))
            return super(MoveDaramad2KifView, self).form_invalid(form)

    def form_invalid(self, form):
        return super().form_invalid(form)


# Lambda function to build scheduler as a AWS CloudWatch Event rule
# Trigger: API Gateway POST request with job config

import json

import boto3

cw = boto3.client('events')
s3 = boto3.resource('s3')

BUCKET_CONFIG = 'aws-od-configs'

LAMBDA_ROLE = ''
LAMBDA_TRIGGER = ''

def lambda_handler(event, context):
    try:
        config = json.loads(event['body'])
        job_id = config.get('id')

        s3.Bucket(
            BUCKET_CONFIG
        ).put_object(
            Body=event['body'].encode(),
            Key=f'{job_id}.json'
        )

        rule = cw.put_rule(
            Name=job_id,
            ScheduleExpression=f'cron({config.get("cron")})',
            State='ENABLED',
            RoleArn=LAMBDA_ROLE
        )

        target = cw.put_targets(
            Rule=job_id,
            Targets=[{
                'Arn': LAMBDA_TRIGGER,
                'Id': 'InitPipelineJob', # ID of the target for this specific rule
                'Input': json.dumps({'id': job_id})
            }]
        )

        return build_response(200, f'successfully created scheduler with ARN {rule["RuleArn"]}')
    except Exception as e:
        return build_response(500, e)

def build_response(code, message=''):
    response = {
        'statusCode': code,
        'headers': {
            'Access-Control-Allow-Origin': '*'
        }
    }

    if message:
        response['body'] = message

    return response


from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.flatpages import views

urlpatterns = [
    path('auth/', include('users.urls')),
    path('auth/', include('django.contrib.auth.urls')),
    path('admin/', admin.site.urls),
    path('about/', views.flatpage, {'url': '/about/'}, name='about'),
    path('tech/', views.flatpage, {'url': '/tech/'}, name='tech'),
    path('', include('api.urls')),
    path('', include('recipes.urls')),
]

if settings.DEBUG:

    import debug_toolbar
    urlpatterns += (path('__debug__/', include(debug_toolbar.urls)),)
    urlpatterns += static(
        settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(
        settings.STATIC_URL, document_root=settings.STATIC_ROOT)


### Wzięte z PL WM -> propzycja rozdzielenia tego - PK

import numpy as np
import pandas as pd

from calc.base_calc import Calc
#from database.db import engine as engine_daneb # zmieniono z oryginalem
from database.db import DbEngine
engine_daneb = DbEngine.create_engine('pytar')

# TODO trzeba dopytać Eli/Uli czy metoda, która wylica tar_sr_il, bedzie jeszcze potrzeban. Trzeba potem podjąc decyzję co dalej z rozdziałem tego

class PlWmPrCalc(Calc):
    def __init__(self, *args, **kwargs):
        super(PlWmPrCalc, self).__init__(*args, **kwargs)
        if self.table_type.lower() == 'pl':
            self.file_prices = self.kwargs['pl_prices']
            self.file_quantity = self.kwargs['pl_quantity']
        elif self.table_type.lower() == 'wm':
            self.file_prices = self.kwargs['wm_prices']
            self.file_quantity = self.kwargs['wm_quantity']
        if self.table_type.lower() in ['pl', 'wm']:
            self.pricelist_table = f'{self.kwargs["schema"]}.om'
            self.pricelist_col_names = self.kwargs['om_prices']

    def create_pricelist(self):
        self.pricelist_join = self.create_join(self.file_prices, self.pricelist_col_names)
        self.cols = ','.join(self.file_prices)
        self.table_name_pricelist = 'tmp.tar_pricelist_%s_%s' % (self.table_type, self.uid)

        self.create_index(engine_daneb, self.pricelist_table, self.pricelist_col_names)
        self.create_index(engine_daneb, self.table_name, self.file_prices)

        # --===!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!===--
        """ ABC -> zmaina w przyszlości na DB - PK """
        query_sql = """
                create table {table_name_pricelist} as
                select t2.*
                from (select distinct {cols} from {table_name}) t1 inner join {pricelist_table} t2 on {pricelist_join}
        """.format(**self.__dict__)

        self.execute_query(engine_daneb, query_sql)

        self.raw_pricelist = pd.read_sql('select * from %s' % self.table_name_pricelist, engine_daneb)
        self.raw_pricelist = self.ensure_dtypes(self.raw_pricelist)
        self.raw_pricelist.columns = self.raw_pricelist.columns.str.lower()

    def calc_from_pricelist(self):
        """Calculates prices from pricelist (OM)."""
        self.create_pricelist()
        b0 = self.raw_pricelist[['wartosc', 'liczba_zakup_opak', 'liczba_jedn_miary'] + self.pricelist_col_names]
        b0 = b0.assign(opakowania=(
                    b0.loc[:, 'liczba_zakup_opak'].astype(np.float32) * b0.loc[:, 'liczba_jedn_miary'].astype(
                np.float32))) # Przypisuje kolumnę i liczy cenę -> zastępuje stare wartości - PK
        b0 = b0.groupby(self.kwargs['om_prices']).sum()
        b = b0.assign(cena_om=b0.loc[:, 'wartosc'] / b0.loc[:, 'opakowania'])
        b = b.reset_index()
        self.pricelist_prices = b[self.pricelist_col_names + ['cena_om']]

    def add_pricelist(self):
        """Adds prices from pricelist OM to tar_sr_il."""
        self.tar_sr_il = self.tar_sr_il.merge(self.pricelist_prices, how='left', left_on=self.file_prices,
                                              right_on=self.pricelist_col_names)
        if (self.tar_sr_il['cena_om'].isnull()).any():
            self.messages['warnings'].append(f'Do pliku {self.table_type} dodają się puste ceny z OM.')
        self.tar_sr_il = self.tar_sr_il.drop(self.kwargs['om_prices'], axis=1)

    def calc_from_unit_price(self):
        """Calculates prices from unit price (prices from files PL, WM)"""
        self.create_tar()
        b = self.tar[['koszt_jednostki', ] + self.file_prices + self.kwargs['case_group'] + [self.quantity_col]]
        b_a = b[b[self.quantity_col] != 0]
        bavg = b_a.groupby(self.kwargs['case_group'] + self.file_prices).apply(
            lambda x: np.average(x['koszt_jednostki'], weights=x[self.file_quantity]))
        b1 = bavg
        b2 = b1.reset_index().rename(columns={0: 'cena_z_pliku'})
        self.unit_prices = b2

    def add_unit_price(self):
        """Adds unit price to PL/WM table."""
        self.tar_sr_il = self.tar_sr_il.merge(self.unit_prices, how='left',
                                              on=self.file_prices + self.kwargs['case_group'])

    def calc_tariff(self):
        self.tariff = self.tar_sr_il.groupby(self.kwargs['case_group'])['koszt_zdarzenia'].sum().reset_index(
            name="suma_%s" % self.table_type)

    # Liczy srednią wystąpień w przypadku (zajmuje się też not nullami, mask zastepuje je) - PK
    def generate_event_cost(self):
        tar_sr_il = self.tar_sr_il
        tar_sr_il['sr_wyst_w_przyp'] = tar_sr_il['sr_wyst_w_przyp'].mask(tar_sr_il['wsp'].notnull(), tar_sr_il['wsp'])
        tar_sr_il['sr_wyst_w_przyp'] = tar_sr_il['sr_wyst_w_przyp'].mask(tar_sr_il['wsp_dni'].notnull(),
                                                                         tar_sr_il['wsp_dni'] * tar_sr_il['czas_hosp'])

        def scale_groups(tar_sr_il, col):
            """Scale groups rates to sr_wyst_w_przyp. If one specifies grp wsp, method is used to expand groups to
            regular mean of occurences in case.
            """
            # grp code
            sum_past_values = tar_sr_il.groupby('grupa')['sr_wyst_w_przyp'].sum().reset_index()
            tar_sr_il_grp = tar_sr_il.reset_index().merge(sum_past_values, on='grupa',
                                                          suffixes=('', '_groups')).set_index('index')
            # calculate rates to scale sr_wyst_w_przyp
            tar_sr_il_grp['rate_to_scale_sr_wyst_groups'] = (
                        tar_sr_il_grp[col] / tar_sr_il_grp['sr_wyst_w_przyp_groups'])
            # create new sr_wyst_w_przyp for grouped rows
            tar_sr_il_grp['sr_wyst_w_przyp_groups'] = tar_sr_il_grp['rate_to_scale_sr_wyst_groups'] * tar_sr_il_grp[
                'sr_wyst_w_przyp']
            # assign new values to old variable name
            tar_sr_il_grp['sr_wyst_w_przyp'] = tar_sr_il_grp['sr_wyst_w_przyp_groups']
            # select only important cols
            tar_sr_il_grp = tar_sr_il_grp[[i for i in tar_sr_il_grp.columns if not i.endswith('_groups')]]
            # add grouped values to original df
            if col == 'gr_wsp_dni':
                tar_sr_il_grp['sr_wyst_w_przyp'] = tar_sr_il_grp['sr_wyst_w_przyp'] * tar_sr_il_grp['czas_hosp']
            tar_sr_il_join = tar_sr_il.join(tar_sr_il_grp, rsuffix='_groups', how='left')
            # assign new values to tar_sr_il
            tar_sr_il_join['sr_wyst_w_przyp'] = tar_sr_il_join['sr_wyst_w_przyp'].mask(
                tar_sr_il_join['sr_wyst_w_przyp_groups'].notnull(), tar_sr_il_join['sr_wyst_w_przyp_groups'])
            # select relevant cols
            tar_sr_il = tar_sr_il_join[[i for i in tar_sr_il_join.columns if not i.endswith('_groups')]]

            return tar_sr_il

        tar_sr_il = scale_groups(tar_sr_il, 'gr_wsp')
        tar_sr_il = scale_groups(tar_sr_il, 'gr_wsp_dni')
        self.tar_sr_il = tar_sr_il

class PlWmCalc(PlWmPrCalc):
    def create_tar_sr_il(self):
        super(PlWmCalc, self).create_tar_sr_il()
        self.tar_sr_il['pl_wm'] = np.NaN # uzupełnia DF ciągiem logicznym potrzebnym do indexowania pl_wm - PK

    def generate_event_cost(self):
        """Adds event costs."""
        super(PlWmCalc, self).generate_event_cost()
        price_col = self.kwargs['price_priority']
        secondary_col = 'cena_om' if price_col != 'cena_om' else 'cena_z_pliku'

        self.tar_sr_il['cena_do_analizy'] = np.where(self.tar_sr_il[price_col].isnull(), self.tar_sr_il[secondary_col],
                                                     self.tar_sr_il[price_col]) # np.where zwraca alement po spełnieniu warunków PK
        self.tar_sr_il['koszt_zdarzenia'] = self.tar_sr_il['cena_do_analizy'] * self.tar_sr_il['sr_wyst_w_przyp']
        self.summarize_event_costs()

class PlCalc(PlWmCalc):
    pass

class WmCalc(PlWmCalc):
    def __init__(self, *args, **kwargs):
        super(WmCalc, self).__init__(*args, **kwargs)
        self.wm_unit_price = self.kwargs['wm_unit_price']


#!/usr/bin/env python
import sys, re, operator, string, inspect

#
# Decorator for enforcing types of arguments in method calls
#
class AcceptTypes():
    def __init__(self, *args):
        self._args = args

    def __call__(self, f):
        def wrapped_f(*args):
            for i in range(len(self._args)):
                if type(args[i]) != self._args[i]:
                    raise TypeError("Expecting %s got %s" % (str(self._args[i]), str(type(args[i]))))
            return f(*args)
        return wrapped_f
#
# The functions
#
@AcceptTypes(str)
def extract_words(path_to_file):
    with open(path_to_file) as f:
        str_data = f.read()
    pattern = re.compile('[\W_]+')
    word_list = pattern.sub(' ', str_data).lower().split()
    with open('../stop_words.txt') as f:
        stop_words = f.read().split(',')
    stop_words.extend(list(string.ascii_lowercase))
    return [w for w in word_list if not w in stop_words]

@AcceptTypes(list)
def frequencies(word_list):
    word_freqs = {}
    for w in word_list:
        if w in word_freqs:
            word_freqs[w] += 1
        else:
            word_freqs[w] = 1
    return word_freqs

@AcceptTypes(dict)
def sort(word_freq):
    return sorted(word_freq.items(), key=operator.itemgetter(1), reverse=True)

word_freqs = sort(frequencies(extract_words(sys.argv[1])))
for (w, c) in word_freqs[0:25]:
    print(w, '-', c)


#!/usr/bin/env python3

# supposed examples to call:
# python3 ./scripts/build_test_macos_travisci.py -g Xcode -source_dir $TRAVIS_BUILD_DIR -build_dir $TRAVIS_BUILD_DIR/build/Xcode --build_type Debug
# python3 ./scripts/build_test_macos_travisci.py -g Xcode -source_dir $TRAVIS_BUILD_DIR -build_dir $TRAVIS_BUILD_DIR/build/Xcode --build_type Release -notclear
# python3 ./scripts/build_test_macos_travisci.py -g Ninja -source_dir $TRAVIS_BUILD_DIR -build_dir $TRAVIS_BUILD_DIR/build/Ninja/Debug --build_type Debug
# python3 ./scripts/build_test_macos_travisci.py -g Ninja -source_dir $TRAVIS_BUILD_DIR -build_dir $TRAVIS_BUILD_DIR/build/Ninja/Release --build_type Release

import os
import subprocess
import argparse
import shutil
import sys

def remove_dir(path):
  command = ['rm'] + ["-rf", path]
  try:
    subprocess.run(command, check = True)
  except subprocess.CalledProcessError as e:
    s = " "
    print(f"Command [{s.join(command)}] exit with code {e.returncode}")
    sys.exit(e.returncode)

def run_cmake(*args):
  command = ['cmake'] + list(args)
  try:
    subprocess.run(command, check=True)
  except subprocess.CalledProcessError as e:
    s = " "
    print(f"Command [{s.join(command)}] exit with code {e.returncode}")
    sys.exit(e.returncode)

def run_ctest(*args):
  command = ['ctest'] + list(args)
  try:
    subprocess.run(command, check=True)
  except subprocess.CalledProcessError as e:
    s = " "
    print(f"Command [{s.join(command)}] exit with code {e.returncode}")
    sys.exit(e.returncode)

def cmake_configure_build_test(IS_SHARED_LIBS, NOT_CLEAR_BUILD_DIR, BUILD_TYPE, CMAKE_GENERATOR, BUILD_DIR, SOURCE_DIR):
  print(f"IS_SHARED_LIBS:[{IS_SHARED_LIBS}]")
  print(f"NOT_CLEAR_BUILD_DIR:[{NOT_CLEAR_BUILD_DIR}]")
  print(f"BUILD_TYPE:[{BUILD_TYPE}]")
  print(f"CMAKE_GENERATOR:[{CMAKE_GENERATOR}]")
  print(f"BUILD_DIR:[{BUILD_DIR}]")
  print(f"SOURCE_DIR:[{SOURCE_DIR}]")

  if os.path.exists(BUILD_DIR):
    if not NOT_CLEAR_BUILD_DIR:
      remove_dir(BUILD_DIR)
      os.makedirs(BUILD_DIR, exist_ok=True)
  else:
    os.makedirs(BUILD_DIR, exist_ok=True)

  print(f"\nCurrent working directory1:[{os.getcwd()}]\n")
  os.chdir(BUILD_DIR)
  print(f"\nCurrent working directory2:[{os.getcwd()}]\n")

  IS_SHARED_LIBS_STR = str(IS_SHARED_LIBS)

  print(f"CMake configure:[cmake -DBUILD_SHARED_LIBS={IS_SHARED_LIBS_STR} -DCMAKE_BUILD_TYPE={BUILD_TYPE} -G {CMAKE_GENERATOR} {SOURCE_DIR}]\n")
  run_cmake(f"-DBUILD_SHARED_LIBS={IS_SHARED_LIBS_STR}", f"-DCMAKE_BUILD_TYPE={BUILD_TYPE}", "-G", CMAKE_GENERATOR, SOURCE_DIR)

  print(f"\nCMake build:[cmake --build . --config {BUILD_TYPE}]\n")
  run_cmake('--build', '.', '--config', BUILD_TYPE)

  print(f"\nCTest run unit tests:[ctest -VV --output-on-failure -C {BUILD_TYPE}]")
  run_ctest('-VV', '--output-on-failure', '-C', BUILD_TYPE)

def main():
  parser = argparse.ArgumentParser(description="""
  This script does:
    1. Create build directory and cd to it.
    2. Run CMake with specified generator type to generate project.
    3. Run CMake to build project.
    4. Run CTest to execute unit tests.
  """)
  parser.add_argument('--is_shared_libs', '-shared', action='store_true', help='If specified - build shared library and static otherwise.')
  parser.add_argument('--not_clear_build_dir', '-notclear', action='store_true', help='If specified - BUILD_DIR will not be cleared before cmake call')
  parser.add_argument('--build_type', default='Debug', choices=['Debug', 'Release'], help='Build type.')
  parser.add_argument('-g', default='Ninja', choices=['Xcode', 'Ninja'], help='CMake generator type.')
  parser.add_argument('-build_dir', default='./build', help='Relative or full path to build directory.')
  parser.add_argument('-source_dir', default='./', help='Relative or full path to directory with root CMakeLists.txt file.')

  args = parser.parse_args()

  IS_SHARED_LIBS = args.is_shared_libs
  NOT_CLEAR_BUILD_DIR = args.not_clear_build_dir
  BUILD_TYPE = args.build_type
  CMAKE_GENERATOR = args.g
  BUILD_DIR = os.path.abspath(args.build_dir)
  SOURCE_DIR = os.path.abspath(args.source_dir)

  cmake_configure_build_test(IS_SHARED_LIBS, NOT_CLEAR_BUILD_DIR, BUILD_TYPE, CMAKE_GENERATOR, BUILD_DIR, SOURCE_DIR)

if __name__ == "__main__":
  main()


# 27. 移除元素
# 给你一个数组 nums 和一个值 val，你需要 原地 移除所有数值等于 val 的元素，并返回移除后数组的新长度。

# 不要使用额外的数组空间，你必须仅使用 O(1) 额外空间并 原地 修改输入数组。

# 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

# 来源：力扣（LeetCode）
# 链接：https://leetcode-cn.com/problems/remove-element
# 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

from typing import List

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        fast = slow = 0
        while fast < len(nums):
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow += 1
            fast += 1
        return slow

class Solution2:
    def removeElement(self, nums: List[int], val: int) -> int:
        left = 0
        right = len(nums)-1
        while left <= right:
            if nums[left] == val:
                nums[left] = nums[right]
                right -= 1
            else:
                left += 1
        return left


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 27 11:19:14 2017

@author: tgadfort
"""

import sys
if '/Users/tgadfort/Python' not in sys.path:
    sys.path.insert(0, '/Users/tgadfort/Python')

from pandas import read_csv, DataFrame, Series, to_numeric
from numpy import repeat, append
from sklearn import datasets

from logger import info
from mvapath import getDataDir
from fileio import getJoblib,saveJoblib
from fsio import setFile, isFile
from colInfo import getDim
from analyzeColumnData import writeDropList

def readCSV(filename):
    info("Reading data ["+filename+"]")
    pddata = read_csv(filename, low_memory=False)

    info("Read data with size "+getDim(pddata))
    return pddata

def readKeyValue(filename):
    info("Reading dict file ["+filename+"] into data frame", ind=6)
    pddata = DataFrame(filename)
    return pddata

def joinTrainTest(trainData, testData):
    info("Joining train and test data", ind=6)
    trainVal  = Series(repeat(1, trainData.shape[0]))
    trainData = trainData.assign(isTrain=trainVal.values)
    info("Train data has size "+getDim(trainData), ind=6)
    testVal   = Series(repeat(0, testData.shape[0]))
    testData  = testData.assign(isTrain=testVal.values)
    info("Test data has size "+getDim(testData), ind=6)
    pddf = trainData.append(testData)
    info("Combined data has size "+getDim(pddf), ind=6)
    return pddf

def joinTrainValid(trainData, validData):
    info("Joining train and validation data", ind=6)
    validVal  = Series(repeat(0, trainData.shape[0]))
    trainData = trainData.assign(isValid=validVal.values)
    info("Train data has size "+getDim(trainData), ind=6)
    validVal   = Series(repeat(1, validData.shape[0]))
    validData  = validData.assign(isValid=validVal.values)
    info("Validation data has size "+getDim(validData), ind=6)
    pddf = trainData.append(validData)
    info("Combined data has size "+getDim(pddf), ind=6)
    return pddf

def readDataset(config, name):
    info("Loading {0} dataset".format(name), ind=0)
    if name == "boston":
        ddata = datasets.load_boston()
    elif name == "diabetes":
        ddata = datasets.load_diabetes()
    elif name == "digits":
        ddata = datasets.load_diabetes()
    elif name == "cancer":
        ddata = datasets.load_breast_cancer()
    elif name == "wine":
        ddata = datasets.load_wine()
    else:
        raise ValueError("Name {0} is not recognized in readDatasets".format(name))

    X = ddata.data
    y = ddata.target
    y = y.reshape((y.shape[0],1))
    df = append(arr=X, values=y, axis=1)
    pddf = DataFrame(df)
    columns = list(ddata.feature_names)
    columns.append("TARGET")
    pddf.columns = columns
    return pddf

def makeDataset(config, name):
    info("Making {0} dataset".format(name))
    if name == "regression":
        X, y = datasets.make_regression(50000, 10, noise=10)
    elif name == "classification":
        X, y = datasets.make_classification(50000, 10)
    else:
        raise ValueError("Name {0} is not recognized in makeDataset".format(name))

    y = y.reshape((y.shape[0],1))
    df = append(arr=X, values=y, axis=1)
    pddf = DataFrame(df)
    columns = ["Var"+str(x) for x in range(X.shape[1])]
    columns.append("TARGET")
    pddf.columns = columns

    return pddf

def makeRegression(config):
    return makeDataset(config, "regression")

def makeClassification(config):
    return makeDataset(config, "classification")

def readKDD99(config):
    info("Getting KDD '99 data.")

    datadir      = getDataDir(config)
    outputConfig = config['output']
    compress     = outputConfig['compress']
    dataName     = setFile(datadir, outputConfig['name'])

    featureConfig = config['feature']
    dlFile        = setFile(datadir, featureConfig['dropList'])

    if isFile(dataName) and isFile(dlFile):
        info("Loading previously create data frames")
        pddf = getJoblib(dataName)
    else:
        info("Downloading KDD '99 data",ind=2)
        tmp = datasets.fetch_kddcup99()
        X = tmp['data']
        y = tmp['target']
        y = y.reshape((y.shape[0],1))
        pddf = DataFrame(append(arr=X, values=y, axis=1))

        tmp = pddf.head(n=1000)
        for column in tmp.columns:
            try:
                tmp[column].mean()
                pddf[column] = to_numeric(pddf[column], errors="coerce")
            except:
                continue

        colFile  = setFile(datadir, "names.dat")
        colnames = open(colFile).readlines()
        targets  = colnames[0].split(",")
        columns  = [x.split(":")[0] for x in colnames[1:]]
        columns.append("TARGET")
        pddf.columns = columns

        info("Saving data to {0}".format(dataName))
        saveJoblib(jlfile=dataName, jldata=pddf, compress=compress)

        info("Saving feature data to {0}".format(dlFile))
        writeDropList(dlFile, pddf, dlData = None)

    return pddf

def readUptake(config):
    info("Getting uptake data.")

    datadir      = getDataDir(config)
    outputConfig = config['output']
    compress     = outputConfig['compress']
    dataName     = setFile(datadir, outputConfig['name'])

    inputConfig  = config['input']
    trainName    = setFile(datadir, inputConfig['train'])
    validName    = setFile(datadir, inputConfig['valid'])

    featureConfig = config['feature']
    dlFile        = setFile(datadir, featureConfig['dropList'])

    if isFile(dataName) and isFile(dlFile):
        info("Loading previously create data frames")
        pddf = getJoblib(dataName)
    else:
        trainData = readCSV(trainName)
        validData = readCSV(validName)

        ## Remove 'market' from validation data
        validData.drop(labels='market', axis=1, inplace=True)

        pddf = joinTrainValid(trainData, validData)
        info("Saving training and validation data")
        saveJoblib(dataName, pddf, compress)
        info("Wrote training and validation data to "+dataName)

        info("Saving feature data")
        writeDropList(dlFile, pddf, dlData = None)

    return pddf

def readData(config):
    info("Getting data for analysis")

    ## Get name
    name     = config['name']

    ## Load the data we need
    if name == "uptake":
        pddf = readUptake(config)
    elif name == "kdd99":
        pddf = readKDD99(config)
    elif name in ["boston", "diabetes", "wine", "digits", "cancer"]:
        pddf = readDataset(config, name)
    elif name == "regression":
        pddf = makeRegression(config)
    elif name == "classification":
        pddf = makeClassification(config)
    else:
        raise ValueError("Name",name,"not recognized in readData()")

    info("Using data that is "+getDim(pddf), ind=0)
    return pddf


from setuptools import setup, find_packages
from glob import glob

def find_scripts(pkgs):
    ret = []
    for pkgname in pkgs:
        ret.extend(glob(pkgname + '/scripts/*.py'))
    return ret

pkgs = find_packages()
scripts = find_scripts(pkgs)

setup(
    name='a107',
    packages=find_packages(),
    include_package_data=True,
    version='21.07.23.0',
    license='GNU GPLv3',
    platforms='any',
    description='A multi-purpose API in Python',
    author='Julio Trevisan',
    author_email='juliotrevisan@gmail.com',
    url='https://github.com/trevisanj/a107',
    keywords= ['debugging', 'introspection', 'file', 'search',
               'conversion', 'datetime', 'config', 'text', 'cryptography'
               ],
    install_requires=["colored", "numpy", "tabulate", "configobj", "python-dateutil"],
    python_requires = '>=3',
    scripts=scripts
)


"""Gaussian descriptor class."""
import os

import ase
import ase.io
try:
    from lammps import lammps
    # For version compatibility; older lammps versions (the serial version
    # we still use on some machines) do not have these constants.
    try:
        from lammps import constants as lammps_constants
    except ImportError:
        pass
except ModuleNotFoundError:
    pass
import numpy as np

from mala.descriptors.lammps_utils import set_cmdlinevars, extract_compute_np
from mala.descriptors.descriptor import Descriptor
from mala.descriptors.atomic_density import AtomicDensity

class MinterpyDescriptors(Descriptor):
    """Class for calculation and parsing of Gaussian descriptors.

    Parameters
    ----------
    parameters : mala.common.parameters.Parameters
        Parameters object used to create this object.
    """

    def __init__(self, parameters):
        super(MinterpyDescriptors, self).__init__(parameters)
        self.verbosity = parameters.verbosity

    @property
    def data_name(self):
        """Get a string that describes the target (for e.g. metadata)."""
        return "Minterpy"

    @property
    def feature_size(self):
        """Get the feature dimension of this data."""
        return self.fingerprint_length

    @staticmethod
    def convert_units(array, in_units="None"):
        """
        Convert the units of a Gaussian descriptor.

        Since these do not really have units this function does nothing yet.

        Parameters
        ----------
        array : numpy.array
            Data for which the units should be converted.

        in_units : string
            Units of array.

        Returns
        -------
        converted_array : numpy.array
            Data in MALA units.
        """
        if in_units == "None" or in_units is None:
            return array
        else:
            raise Exception("Unsupported unit for Minterpy descriptors.")

    @staticmethod
    def backconvert_units(array, out_units):
        """
        Convert the units of a Gaussian descriptor.

        Since these do not really have units this function does nothing yet.

        Parameters
        ----------
        array : numpy.array
            Data in MALA units.

        out_units : string
            Desired units of output array.

        Returns
        -------
        converted_array : numpy.array
            Data in out_units.
        """
        if out_units == "None":
            return array
        else:
            raise Exception("Unsupported unit for Minterpy descriptors.")

    def _calculate(self, atoms, outdir, grid_dimensions, **kwargs):
        from lammps import lammps

        nx = grid_dimensions[0]
        ny = grid_dimensions[1]
        nz = grid_dimensions[2]

        # Check if we have to determine the optimal sigma value.
        if self.parameters.atomic_density_sigma is None:
            self.grid_dimensions = [nx, ny, nz]
            voxel = atoms.cell.copy()
            voxel[0] = voxel[0] / (self.grid_dimensions[0])
            voxel[1] = voxel[1] / (self.grid_dimensions[1])
            voxel[2] = voxel[2] / (self.grid_dimensions[2])
            self.parameters.atomic_density_sigma = AtomicDensity.\
                get_optimal_sigma(voxel)

        # Size of the local cube
        # self.parameters.minterpy_cutoff_cube_size

        # first grid point - for now just assume 0.0, 0.0, 0.0.
        # That is correct in 99% of the cases I am aware of, and once
        # we have a working implementation we can test and refine this.
        # first_grid_point = [0.0, 0.0, 0.0]

        # Get the unisolvent nodes.
        unisolvent_nodes = self._build_unisolvent_nodes()

        # Transform the unisolvent nodes into cartesian coordinates by assuming
        # small cubes around the grid points. Currently only works for cubic
        # cells.
        self.parameters.minterpy_point_list = []
        local_cube = atoms.cell.copy()
        local_cube[0] = local_cube[0] * (self.parameters.
                                         minterpy_cutoff_cube_size /
                                         local_cube[0][0])
        local_cube[1] = local_cube[1] * (self.parameters.
                                         minterpy_cutoff_cube_size /
                                         local_cube[0][0])
        local_cube[2] = local_cube[2] * (self.parameters.
                                         minterpy_cutoff_cube_size /
                                         local_cube[0][0])
        for i in range(np.shape(unisolvent_nodes)[0]):
            self.parameters.\
                minterpy_point_list.\
                append(np.matmul(local_cube, unisolvent_nodes[i]))

        # Array to hold descriptors.
        coord_length = 3 if self.parameters.descriptors_contain_xyz else 0
        minterpy_descriptors_np = \
            np.zeros([nx, ny, nz,
                      len(self.parameters.minterpy_point_list)+coord_length],
                     dtype=np.float64)
        self.fingerprint_length = \
            len(self.parameters.minterpy_point_list)+coord_length

        self.fingerprint_length = len(self.parameters.minterpy_point_list)
        # Perform one LAMMPS call for each point in the Minterpy point list.
        for idx, point in enumerate(self.parameters.minterpy_point_list):
            # Shift the atoms in negative direction of the point(s) we actually
            # want.
            atoms_copied = atoms.copy()
            atoms_copied.set_positions(atoms.get_positions()-np.array(point))

            # The rest is the stanfard LAMMPS atomic density stuff.
            lammps_format = "lammps-data"
            ase_out_path = os.path.join(outdir, "lammps_input.tmp")
            ase.io.write(ase_out_path, atoms_copied, format=lammps_format)

            # Create LAMMPS instance.
            lammps_dict = {}
            lammps_dict["sigma"] = self.parameters.atomic_density_sigma
            lammps_dict["rcutfac"] = self.parameters.atomic_density_cutoff
            lammps_dict["atom_config_fname"] = ase_out_path
            lmp = self._setup_lammps(nx, ny, nz, outdir, lammps_dict,
                                     log_file_name="lammps_mgrid_log.tmp")

            # For now the file is chosen automatically, because this is used
            # mostly under the hood anyway.
            filepath = __file__.split("minterpy")[0]
            if self.parameters._configuration["mpi"]:
                raise Exception("Minterpy descriptors cannot be calculated "
                                "in parallel yet.")
                # if self.parameters.use_z_splitting:
                #     runfile = os.path.join(filepath, "in.ggrid.python")
                # else:
                #     runfile = os.path.join(filepath, "in.ggrid_defaultproc.python")
            else:
                runfile = os.path.join(filepath, "in.ggrid_defaultproc.python")
            lmp.file(runfile)

            # Extract the data.
            nrows_ggrid = extract_compute_np(lmp, "ggrid",
                                             lammps_constants.LMP_STYLE_LOCAL,
                                             lammps_constants.LMP_SIZE_ROWS)
            ncols_ggrid = extract_compute_np(lmp, "ggrid",
                                             lammps_constants.LMP_STYLE_LOCAL,
                                             lammps_constants.LMP_SIZE_COLS)

            gaussian_descriptors_np = \
                extract_compute_np(lmp, "ggrid",
                                   lammps_constants.LMP_STYLE_LOCAL, 2,
                                   array_shape=(nrows_ggrid, ncols_ggrid))

            lmp.close()

            gaussian_descriptors_np = \
                gaussian_descriptors_np.reshape((grid_dimensions[2],
                                                 grid_dimensions[1],
                                                 grid_dimensions[0],
                                                 7))
            gaussian_descriptors_np = \
                gaussian_descriptors_np.transpose([2, 1, 0, 3])
            if self.parameters.descriptors_contain_xyz and idx == 0:
                minterpy_descriptors_np[:, :, :, 0:3] = \
                    gaussian_descriptors_np[:, :, :, 3:6].copy()

            minterpy_descriptors_np[:, :, :, coord_length+idx:coord_length+idx+1] = \
                gaussian_descriptors_np[:, :, :, 6:]

        return minterpy_descriptors_np, nx * ny * nz

    def _build_unisolvent_nodes(self, dimension=3):
        """
        Build the unisolvent nodes.

        Parameters
        ----------
        dimension : int
            Dimension of data (should always be 3 for 3D in our case).

        Returns
        -------
        nodes : numpy.array
            Two-dimensional array containing the nodes.
        """
        import minterpy as mp

        # Calculate the unisolvent nodes.
        mi = mp.MultiIndexSet.from_degree(spatial_dimension=dimension,
                                          poly_degree=self.parameters.minterpy_polynomial_degree,
                                          lp_degree=self.parameters.minterpy_lp_norm)
        unisolvent_nodes = mp.Grid(mi).unisolvent_nodes
        return unisolvent_nodes


import scrapy

class OnlankaSpider(scrapy.Spider):
    name = "onlanka"

    start_urls = [
        'https://www.onlanka.com/'
    ]

    def parse(self, response):
        for news_item in response.css('div.post.type-post'):
            yield {
                'title': news_item.css('a::attr(title)').extract_first(),
                'content': news_item.css('div.post-content p::text').extract(),
                'link': news_item.css('div.read-more a::attr(href)').extract_first(),
                'date_published': news_item.css('span.published::text').extract_first(),
                'category': news_item.css('div.post-category a::text').extract_first()
            }

        next_page = response.css('div.previous a::attr(href)').extract_first()
        if next_page is not None:
            yield response.follow(next_page, callback=self.parse)


import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://localhost:8089/Home/user/login.html')

def find_ele(lacator):

     return WebDriverWait(driver,10,1).until(lambda x:x.find_element(*lacator))

find_ele((By.XPATH,'//*[@id="username"]')).send_keys("13488888888")
find_ele((By.XPATH,'//*[@id="password"]')).send_keys('123456')
find_ele((By.XPATH,'//*[@id="verify_code"]')).send_keys('8888')
find_ele((By.XPATH,'//*[@id="loginform"]/div/div[6]/a')).click()
time.sleep(2)
driver.get('http://localhost:8089/Home/Goods/goodsInfo/id/65.html')

ele =find_ele((By.XPATH,'//*[@id="number"]'))
js = "arguments[0].value=''"
driver.execute_script(js,ele)

# find_ele((By.XPATH,'//*[@id="join_cart"]')).click()
time.sleep(2)
# driver.switch_to.frame(0)
# print(find_ele((By.XPATH, '//*[@id="addCartBox"]/div[1]/div/span')).text)
# find_ele((By.XPATH,'//*[@id="addCartBox"]/div[1]/div/div/a[1]')).click()
# time.sleep(2)
# driver.switch_to.default_content()
#
# time.sleep(2)
find_ele((By.XPATH,'//*[@id="number"]')).send_keys('2')
# driver.quit()


from src.pre_built.sorting import sort_by

def test_sort_by_criteria():
    criteria = "min_salary"
    jobs = [
        {"min_salary": "1000", "max_salary": "5000"},
        {"min_salary": "2000", "max_salary": "3000"},
        ]
    sorted = sort_by(jobs, criteria)
    assert sorted[0] == {"min_salary": "1000", "max_salary": "5000"}

    criteria = "max_salary"
    assert sorted[0] == {"min_salary": "2000", "max_salary": "3000"}


#!/usr/bin/python3
"""list 10 commits (from the most recent to oldest)
of the repository “rails” by the user “rails”"""

import requests
from sys import argv

if __name__ == "__main__":
    a = "https://api.github.com/repos"
    URL = "{}/{}/{}/commits".format(a, argv[2], argv[1])
    resp = requests.get(URL)
    commits = resp.json()
    for c in commits[:10]:
        print(c.get("sha"), end=": ")
        print(c.get("commit").get("author").get("name"))


from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QToolTip, QMessageBox, QMainWindow, QGridLayout, QLabel, QLineEdit, QComboBox, QTextEdit
import socket
import setuptools
import psutil
import netaddr
import netifaces
import threading
import os
import platform
import sys
import shutil
from PyQt5.QtCore import *

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Le serveur de chat")
        self.resize(300, 400)
        self.setup_ui()

    def setup_ui(self):
        self.label = QLabel("Serveur")
        self.line_edit = QLineEdit("127.0.0.1")
        self.label2 = QLabel("Port")
        self.line_edit2 = QLineEdit("10000")
        self.button = QPushButton("Démarrer le serveur")
        self.label3 = QLabel("Nombre maximum de clients")
        self.line_edit3 = QLineEdit("5")
        self.text_edit = QTextEdit()
        self.text_edit.setReadOnly(True)

        self.button.clicked.connect(self.demarrer_serveur)

        layout = QGridLayout()
        layout.addWidget(self.label, 0, 0)
        layout.addWidget(self.line_edit, 0, 1)
        layout.addWidget(self.label2, 1, 0)
        layout.addWidget(self.line_edit2, 1, 1)
        layout.addWidget(self.label3, 2, 0)
        layout.addWidget(self.line_edit3, 2, 1)
        layout.addWidget(self.button, 3, 0, 1, 3)
        layout.addWidget(self.text_edit, 4, 0, 1, 3)
        self.setLayout(layout)

    def demarrer_serveur(self):
        self.serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.serveur.bind((self.line_edit.text(), int(self.line_edit2.text())))
        self.serveur.listen(int(self.line_edit3.text()))
        # self.button.setEnabled(True)
        self.button.setText("Arrêter le serveur")
        self.label.setText("Serveur démarré")
        self.button.clicked.connect(self.arreter_serveur)
        self.label2.setText("Attente de connexion")
        self.label3.setText("Nombre de clients connectés : 0")
        self.thread_ecoute = threading.Thread(target=self.ecoute)
        self.thread_ecoute.start()

    def arreter_serveur(self):
        self.serveur.close()
        self.label.setText("Serveur")
        self.label2.setText("Port")
        self.label3.setText("Nombre maximum de clients")
        self.button.clicked.connect(self.demarrer_serveur)
        self.button.setEnabled(True)
        self.text_edit.clear()

    def ecoute(self):
        while True:
            self.connexion, self.adresse = self.serveur.accept()
            self.label2.setText("Connexion établie avec {}".format(self.adresse))
            self.thread_reception = threading.Thread(target=self.reception)
            self.thread_reception.start()

    def reception(self):
        while True:
            self.message_recu = self.connexion.recv(1024).decode()
            self.text_edit.append(self.message_recu)
            self.label3.setText("Nombre de clients connectés : {}".format(len(threading.enumerate())-2))

if __name__ == "__main__":
    app = QApplication([])
    window = Window()
    window.show()
    app.exec_()


from flask import Flask, jsonify, request
from flask_restful import Resource
from Helpers.DatabaseHelper import DatabaseHelper
import csv
import time
from datetime import datetime
import traceback as tb
from Constants.Status import *
import json
from Helpers.utils import autheticateTenant
from Helpers.authenticate import *
from Controllers.DefChangeController import DefChangeController

class MaintainBusinessRulesController(Resource):
	def __init__(self):
		self.domain_info = autheticateTenant()
		if self.domain_info:
			tenant_info = json.loads(self.domain_info)
			self.tenant_info = json.loads(tenant_info['tenant_conn_details'])
			self.dcc=DefChangeController()
			self.db=DatabaseHelper(self.tenant_info)

	@authenticate
	def get(self, id=None, source_id=None, page=0, col_name=None,business_rule=None):
		print(request.endpoint)
		if request.endpoint == "business_rule_export_to_csv_ep":
			source = request.args.get('source_id')
			return self.export_to_csv(source_id=source)
		if request.endpoint == "business_rule_linkage_ep":
			return self.list_reports_for_rule(business_rule=business_rule)
		if request.endpoint == "business_rule_linkage_multiple_ep":
			source = request.args.get('source_id')
			rules = request.args.get('rules')
			#print(source,rules)
			return self.list_reports_for_rule_list(source_id=source,business_rule_list=rules)
		if request.endpoint == "business_rule_drill_down_rules_ep":
			print('Inside rules drilldown ep')
			source = request.args.get('source_id')
			rules = request.args.get('rules')
			page = request.args.get('page')
			business_date = request.args.get('business_date')
			qualified_data_version = request.args.get('qualified_data_version')
			return self.get_business_rules_list_by_source(rules=rules,source=source,page=page
														,business_date=business_date
														,qualified_data_version=qualified_data_version)
		if request.endpoint == 'get_br_source_suggestion_list_ep':
			source_id = request.args.get('source_id')
			return self.get_source_suggestion_list(source_id=source_id)
		if request.endpoint == 'get_br_source_column_suggestion_list_ep':
			table_name = request.args.get('table_name')
			return self.get_source_column_suggestion_list(table_name=table_name)
		if id:
			return self.render_business_rule_json(id)
		elif page and source_id==None and col_name == None:
			return self.render_business_rules_json(page)
		elif source_id and page and col_name == None:
			return self.render_business_rules_json(page,source_id)
		elif col_name:
			direction = request.args.get('direction')
			return self.render_business_rules_json(page, "source_id",(col_name,direction))
	def post(self,page=None):

		if request.endpoint == "business_rules_ep_filtered":
			filter_conditions = request.get_json(force=True)
			return self.get_business_rules_filtered(filter_conditions)

		if request.endpoint == "validate_python_expr_ep":
			expr_obj=request.get_json(force=True)
			return self.validate_python_expression(expr_obj)

		br = request.get_json(force=True)
		return self.dcc.insert_data(br)

	def put(self, id=None):
		if id == None:
			return BUSINESS_RULE_EMPTY
		data = request.get_json(force=True)
		return self.dcc.update_or_delete_data(data, id)

	# def delete(self, id=None):
	# 	if id == None:
	# 		return BUSINESS_RULE_EMPTY
	# 	res = self.delete_business_rules(id)
	# 	return res
	def render_business_rules_json(self, page=0, source_id="source_id", order=None):
		startPage =  int(page)*100
		business_rules_dict = {}
		business_rules_list = []
		if order:
			cur = self.db.query('select * from business_rules where source_id=' + str(source_id) + ' order by ' + order[0] + ' ' + order[1] + ' limit ' + str(startPage) + ', 100')
		else:
			# cur = self.db.query('select * from business_rules where source_id=' + str(source_id) + ' limit ' + str(startPage) + ', 100')
			cur = self.db.query('select * from business_rules where source_id=' + str(source_id))
		business_rules = cur.fetchall()
		cols = [i[0] for i in cur.description]
		business_rules_dict['cols'] = cols

		for br in business_rules:
			business_rules_list.append(br)
		business_rules_dict['rows'] = business_rules_list
		count = self.db.query('select count(*) as count from business_rules where source_id=' + str(source_id)).fetchone()
		business_rules_dict['count'] = count['count']
		json_dump = business_rules_dict
		return json_dump
	def get_business_rules_filtered(self,filter_conditions):
		filter_string = ""
		for filter_condition in filter_conditions:
			filter_string = filter_string + " AND " + filter_condition['field_name'] + "='" + filter_condition['value'] + "'"
		query = 'select * from business_rules where 1' + filter_string
		cur = self.db.query(query)
		data = cur.fetchall()
		if data:
			return data
		return NO_BUSINESS_RULE_FOUND
	def get_business_rules_list_by_source(self,rules,source,page,business_date,qualified_data_version):

		if page is None:
			page = 0
		startPage = int(page) * 100
		data_dict = {}
		where_clause = ''
		sql = 'select br.* from business_rules br {} where 1 '
		if source is not None:
			where_clause += ' and br.source_id ={}'.format(source,)
		if rules is not None:
			where_clause += ' and br.business_rule in (\''+rules.replace(',','\',\'')+'\')'
		if business_date and business_date !='null' and business_date !='undefined':
			brvsql = "select br_version from qualified_data_vers " + \
			      " where business_date={0} and source_id={1} and version={2}".format(business_date,source,qualified_data_version)
			brv = self.db.query(brvsql).fetchone()

			sql = sql.format(", business_rules_vers v")
			where_clause += " and v.source_id=br.source_id and v.version={}".format(brv['br_version'],) + \
							" and instr(concat(',',v.id_list,','),concat(',',br.id,','))"
		else:
			sql = sql.format("")

		cur = self.db.query(sql + where_clause + " limit " + str(startPage) + ", 100")
		data = cur.fetchall()
		cols = [i[0] for i in cur.description]
		count = self.db.query(sql.replace('br.*','count(*) as count ') + where_clause).fetchone()
		data_dict['cols'] = cols
		data_dict['rows'] = data
		data_dict['count'] = count['count']
		data_dict['table_name'] = 'business_rules'
		data_dict['sql'] = sql

		return data_dict

	def render_business_rule_json(self, id):
		query = 'select * from business_rules where id = %s'
		cur = self.db.query(query, (id, ))
		data = cur.fetchone()
		if data:
			return data
		return NO_BUSINESS_RULE_FOUND
	def ret_source_data_by_id(self, table_name,id):
	    query = 'select * from ' + table_name + ' where id = %s'
	    cur = self.db.query(query, (id, ))
	    data = cur.fetchone()
	    for k,v in data.items():
	    	if isinstance(v,datetime):
	    		data[k] = data[k].isoformat()
	    		print(data[k], type(data[k]))
	    if data:
	        return data
	    return NO_BUSINESS_RULE_FOUND

	def insert_business_rules(self, data):

	    table_name = data['table_name']
	    update_info = data['update_info']
	    update_info_cols = update_info.keys()

	    sql="insert into "+table_name + "("
	    placeholders="("
	    params=[]

	    for col in update_info_cols:
	        sql+=col+","
	        placeholders+="%s,"
	        if col=='id':
	        	params.append(None)
	        else:
	        	params.append(update_info[col])

	    placeholders=placeholders[:len(placeholders)-1]
	    placeholders+=")"
	    sql=sql[:len(sql)-1]
	    sql+=") values "+ placeholders

	    params_tuple=tuple(params)
	    print(sql)
	    print(params_tuple)
	    res=self.db.transact(sql,params_tuple)
	    self.db.commit()

	    return self.ret_source_data_by_id(table_name,res)

	def update_business_rules(self, data, id):

	    table_name=data['table_name']
	    update_info=data['update_info']
	    update_info_cols=update_info.keys()

	    sql= 'update '+table_name+ ' set '
	    params=[]
	    for col in update_info_cols:
	        sql+=col +'=%s,'
	        params.append(update_info[col])

	    sql=sql[:len(sql)-1]
	    sql+=" where id=%s"
	    params.append(id)
	    params_tuple=tuple(params)

	    print(sql)
	    print(params_tuple)

	    res=self.db.transact(sql,params_tuple)

	    if res==0:
	        self.db.commit()
	        return self.ret_source_data_by_id(table_name,id)

	    self.db.rollback()
	    return UPDATE_ERROR

	def delete_business_rules(self, id):
		sql = 'delete from business_rules where id=%s'
		params = (id, )
		res = self.db.transact(sql, params)
		if res == 0:
			self.db.commit()
			return res

		self.db.rollback()
		return DATABASE_ERROR

	def export_to_csv(self, source_id='ALL'):

		sql = 'select * from business_rules where 1=1'

		if source_id != 'ALL':
			sql += " and source_id='" + str(source_id) + "'"

		cur = self.db.query(sql)

		business_rules = cur.fetchall()

		 # keys=business_rules[0].keys()

		keys = [i[0] for i in cur.description]
		filename="business_rules_source_" + str(source_id) + "_" + str(time.time())+ ".csv"

		with open('./static/'+filename, 'wt') as output_file:
			dict_writer = csv.DictWriter(output_file, keys)
			dict_writer.writeheader()
			dict_writer.writerows(business_rules)
		return { "file_name": filename }

	def list_reports_for_rule(self,**kwargs):

		parameter_list = ['business_rule']

		if set(parameter_list).issubset(set(kwargs.keys())):
			business_rule = kwargs['business_rule']

		else:
			return BUSINESS_RULE_EMPTY

		sql = "select distinct report_id,sheet_id,cell_id from report_calc_def, in_use \
		where cell_business_rules like '%," + business_rule + ",%'"
		cur=self.db.query(sql)
		report_list = cur.fetchall()

		return [(rpt['report_id'], rpt['sheet_id'], rpt['cell_id']) for rpt in report_list]

	def list_reports_for_rule_list(self,**kwargs):

		parameter_list = ['source_id', 'business_rule_list']

		if set(parameter_list).issubset(set(kwargs.keys())):
			source_id = kwargs['source_id']
			business_rule_list = kwargs['business_rule_list'].split(',')

		else:
			return BUSINESS_RULE_EMPTY

		sql = "select report_id,sheet_id,cell_id,cell_business_rules,in_use from report_calc_def where source_id=" + str(
			source_id)
		cur=self.db.query(sql)
		data_list = cur.fetchall()

		result_set = []
		if len(business_rule_list)==1 and business_rule_list[0]=='undefined':
			result_set = data_list
		else:
			for data in data_list:
				# print(data['cell_business_rules'])
				cell_rule_list = data['cell_business_rules'].split(',')
				# print(type(cell_rule_list))
				if set(business_rule_list).issubset(set(cell_rule_list)):
					# print(data)
					result_set.append(data)

		return result_set

	def get_source_suggestion_list(self,source_id='ALL'):

		data_dict={}
		where_clause = ''

		sql = "select source_id, source_table_name " + \
		        " from data_source_information " + \
				" where 1 "
		country_suggestion = self.db.query(sql).fetchall()
		if source_id is not None and source_id !='ALL':
		     where_clause =  " and source_id = " + source_id

		source = self.db.query(sql + where_clause).fetchall()

		data_dict['source_suggestion'] = source

		if not data_dict:
		    return {"msg":"No report matched found"},404
		else:
		    return data_dict

	def get_source_column_suggestion_list(self,table_name):
		data_dict = self.db.query("describe " + table_name).fetchall()

		#Now build the agg column list
		return data_dict

	def validate_python_expression(self,expr_obj):
		py_expr_val=expr_obj['expr']
		py_attr=expr_obj['attr']
		py_sample=expr_obj['sample']
		print(py_attr)

		if not py_attr and not py_sample:
			status='INVALID'
			msg="Expression can't be validated"
		else:
			# Sampling option can also be extended for testing
			#input would be a business_date and no of records for Sampling
			#We need to loop through the set and publish the result array for
			#the sampling set e.g. [{attr:{<id:,business_date:,..>},msg:,status:}]
			#on error no need to continue with the sample, just break and display result
			py_items=[]
			if py_attr:
				py_items.append({"attr":py_attr,"msg":"","status":""})
			if py_sample:
				table_name = py_sample['table_name']
				business_date = py_sample['business_date']
				columns = py_sample['columns']
				sample_size = py_sample['sample_size']
				sample_data = self.db.query("select " + columns + " from " + table_name + " where business_date=" + str(business_date) + " and " + columns.replace(","," is not null and ")+ " is not null LIMIT 0," + str(sample_size)).fetchall()
				for data in sample_data:
					py_items.append({"attr":data,"msg":"","status":""})

			for py_item in py_items:
				print(py_item)
				py_expr = py_expr_val
				for attr in py_item['attr'].keys():
					py_expr=py_expr.replace("["+attr+"]","'" + str(py_item['attr'][attr]) + "'")
				try:
					msg=[]
					status='VALID'
					print(py_expr)
					val=eval(py_expr.replace("DERIVED","'DERIVED'"))
					msg.append('Executed expression gives a value of '+str(val))
				except:
					status='INVALID'
					msg=tb.format_exc().splitlines()[-3:]

				py_item["msg"] = msg
				py_item["status"] = status

		#print(msg[-3:])

		return py_items


# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

N = int(input())
a = [int(i) for i in input().split()]

# part
left = a[:N]

left.sort()

for i in range(N,2*N+1):
    if left[0] < a[i]:
        left[0] = a[i]
        left.sort()

right = a[2*N:]
right.sort()
for i in range(N,2*N):
    print(right)
    if right[-1] > a[i]:
        right[-1] = a[i]
        right.sort()

print(left,right)
ans = sum(left)-sum(right)

print(ans)


from tkinter import *

root = Tk()

root.title("Bem Vindo Ao GeekForGeeks! ")
root.geometry('350x200')

menu = Menu(root)
item = Menu(menu)
item.add_command(label='Novo')
menu.add_cascade(label='Arquivo', menu=item)
root.config(menu=menu)

lbl = Label(root, text="Você é  Geek ? ")
lbl.grid()

txt = Entry(root, width=10)
txt.grid(column=1, row=0)

def clicked():
    res = "Sua Resposta foi: "
    txt.get()
    lbl.configure(text=res)

btn = Button(root, text="Clique Aqui", fg="green", command=clicked)
btn.grid(column=2, row=0)

root.mainloop()


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

import time
import cupy as cp
import cudf
import util

def merge_test(data_size, ncols, number_of_tables):
    data_start = 0
    data_step = 30

    total_rows = util.get_rows(data_size, ncols)
    rows_per_table = total_rows / number_of_tables

    tables = []
    for i in range(number_of_tables):
        tbl = util.create_sorted_cudf_df(ncols, rows_per_table, start=data_start, step=data_step)
        data_start += ncols * rows_per_table * (data_step / 3)
        tables.append(tbl)
#        print(" \ntable: " + str(i) + "\n")
#        print(tbl)

    t0 = time.time()
    merged = cudf.merge_sorted(tables)
    t1 = time.time()

#    print("\nmerged table: \n")
#    print(merged)

    delay = (t1 - t0) * 1000
#    print("merge delay: " + str(delay))
    return delay

def sort_or_quantile_test(ncols, data_size, dtype="int64", task_type="sort"):
    if task_type != "sort" and task_type != "quantile":
        raise ValueError("task_type can be either 'sort' or 'quantile'")

    total_rows = util.get_rows(data_size, ncols)
    if dtype == "int64":
        df = util.create_random_data_df(ncols, total_rows)
    elif dtype == "str":
        df = util.create_random_str_df(ncols, total_rows);

    print("memory usage: ", df.memory_usage())

    if task_type == "quantile":
        quantile_points = cp.linspace(0, 1, 100)

    t0 = time.time()
    if task_type == "sort":
        result_df = df.sort_values(by=df.columns)
    elif task_type == "quantile":
        result_df = df._quantiles(quantile_points, "NEAREST")
    t1 = time.time()

#    print("\nresult table: \n")
#    print(result_df)

    delay = (t1 - t0) * 1000
#    print("sort delay: " + str(delay))
    del df, result_df
    return delay

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

data_size = "100MB"
ncols = 1
number_of_tables = 20

tasks = ["sort", "quantile"]
types = ["int64", "str"]

for ttype in tasks:
    for dtype in types:
        print(f"testing {ttype} and {dtype}")
        delay = sort_or_quantile_test(ncols, data_size, dtype, ttype)
        print(f"{ttype} {dtype} delay: ", delay)

# for number_of_tables in range(5, 30, 5):
#     duration = merge_test(data_size, ncols, number_of_tables)
#     print(number_of_tables, " merge duration: ", duration)


from games.balance_game import BalanceGame
from lib.utilities import load_obj
from lib.activator_funcs import sigmoid
from lib.fixed_top_nn import FixedTopologyNeuralNetwork
import sys

sampling_counts = 1
if len(sys.argv) > 1:
    sampling_counts = int(sys.argv[1])

bestIndiv = None
bestIndivScore = None
model = load_obj(800, 'models/cartpole/cmaes_cartpole')
game = BalanceGame()
topology = [(4, sigmoid), (1, sigmoid)]

for i in range(sampling_counts):
    indiv = model.sample()[0]
    score = game.play(FixedTopologyNeuralNetwork(4,topology, indiv))
    if not bestIndivScore or bestIndivScore < score:
        bestIndivScore = score
        bestIndiv = indiv

print(game.play(FixedTopologyNeuralNetwork(4,topology, bestIndiv), render=True, games_amount=3))


from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
import numpy as np
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
from metafeatures import get_metafeatures_dataset, get_histogram
from tensorflow.keras.models import load_model
from unary_transformations import Transformations as Unary_transformations
from binary_transformations import Transformations as Binary_transformations
from sklearn.preprocessing import LabelEncoder
from sklearn.neighbors import NearestNeighbors
import pandas as pd

trans_symbols = ['log', 'sqrt', 'sqr', 'round', 'freq', 'tanh', 'sig', 'z', 'reci', 'minmax', 'std_scaler']

operations = ['+','-','*','/','%']

def preprocess_dataset(df):
    # Preprocess data
    df = df.replace('?', np.nan)

    # df = df.fillna(df.mean())

    X_raw = df.iloc[:,0:-1]
    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
    X_raw = X_raw.select_dtypes(include=numerics)
    X_raw = X_raw.fillna(X_raw.mean())
    X_raw = X_raw.astype('float32')

    le = LabelEncoder()
    y_raw = df.iloc[:,-1].values
    y_raw = le.fit_transform(y_raw)

    return X_raw, y_raw

def get_dataset_unary_encoding(X, y):

    ds = {
        'X': X,
        'y': y,
        'dataset_name': 'test'
    }

    mf, hist = get_metafeatures_dataset(ds), get_histogram(ds)

    ds_encodings = []

    for f_i in range(len(ds['X'].T)):
        encoding = np.concatenate( (mf, hist[f_i]),axis = 0)
        ds_encodings.append(encoding)

    ds_encodings = np.array(ds_encodings)
    ds_encodings = StandardScaler().fit_transform(ds_encodings)
    return ds_encodings

def transform_unary(X, y, column_names, TRM_dataset):

    new_column_names = []
    new_features = []

    # Get the encodings of all the features in dataset
    ds_encodings = get_dataset_unary_encoding(X, y)

    # check for NaN values
    if ( np.isnan(ds_encodings).any() or np.isinf(ds_encodings).any()):
        ds_encodings = np.nan_to_num(ds_encodings)

    # Get 1 neighbor for each feature
    nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree',metric='euclidean', n_jobs = -1).fit(TRM_dataset[:, :-1])
    indices = nbrs.kneighbors(ds_encodings, return_distance = False)

    # Iterate in t indices
    for neigh_index, f_index in zip(indices, range(len(X.T))):

        t_index = int(TRM_dataset[neigh_index,-1])

        if t_index == len(Unary_transformations):
            # No suitable transformation found
            continue

        # The feature is already there
        new_column_name = f'{trans_symbols[t_index]}({column_names[f_index]})'
        if (new_column_name in new_column_names or new_column_name in column_names):
            continue

        success, t_values = Unary_transformations[t_index](X[:,f_index])

        if success and not np.isnan(t_values).any():
            new_features.append(t_values)
            new_column_names.append(new_column_name)

    if len(new_features) > 0:
        new_features = np.array(new_features)
        return new_features.T, new_column_names
    else:
        return None, None

def transform_binary(X, y, column_names, TRM_binary_dataset):
    ds = {
        'X': X,
        'y': y,
        'dataset_name': 'test'
    }

    new_column_names = []
    new_features = []

    nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree',metric='euclidean', n_jobs = -1).fit(TRM_binary_dataset[:, :-1])

    # Get the encodings of all the features in dataset
    mf, hist = get_metafeatures_dataset(ds), get_histogram(ds)

    for f_i in range(len(X.T)):
        for f_j in range(f_i + 1, len(X.T)):

            encoding = np.concatenate( (mf, hist[f_i], hist[f_j] ), axis = 0)

            # Get 1 neighbor for each feature
            neigh_index = nbrs.kneighbors([encoding], return_distance = False)

            t_index = int(TRM_binary_dataset[neigh_index, -1])

            if t_index == len(Binary_transformations):
                # No suitable transformation found
                continue

            # Test if the candidate feature is already there
            new_col_name = f"{operations[t_index]}({column_names[f_i]},{column_names[f_j]})"
            if (new_col_name in new_column_names or new_col_name in column_names):
                continue

            t_f = Binary_transformations[t_index](X[:,f_i], X[:,f_j])

            # if we made the transformation, then we check if there is any NaN or Inf value
            if ( np.isnan(t_f).any() or np.isinf(t_f).any()):
                continue

            new_features.append(t_f)
            new_column_names.append(new_col_name)
            break

    if len(new_features) > 0:
        new_features = np.array(new_features)
        return new_features.T, new_column_names
    else:
        return None, None

def transform_scaler(X, y, column_names, TRM_scaler):

    ds = {
        'X': X,
        'y': y,
        'dataset_name': 'test'
    }

    scalers = [RobustScaler, StandardScaler, MinMaxScaler]

    # Get the encoding of current dataset
    ds_encoding = get_metafeatures_dataset(ds)

    # check for NaN values
    if ( np.isnan(ds_encoding).any() or np.isinf(ds_encoding).any()):
        ds_encoding = np.nan_to_num(ds_encoding)

    # get similar dataset
    nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree',metric='euclidean', n_jobs = -1).fit(TRM_scaler[:, :-1])
    neigh_index = nbrs.kneighbors([ds_encoding], return_distance = False)[0]

    # get scaler index
    t_index = int(TRM_scaler[neigh_index,-1])

    # apply scaling
    scaler = scalers[t_index]()
    X_scaled = scaler.fit_transform(X)

    # rename columns
    scaler_prefix = ['robust_s', 'std_s', 'minmax_s']
    scaled_column_names = []
    for col_name in column_names:
        scaled_column_names.append(f"{scaler_prefix[t_index]}({col_name})")

    # create new scaled DF
    df_scaled = pd.DataFrame(X_scaled, columns = scaled_column_names)
    df_scaled['class'] = y

    return df_scaled


#!/usr/bin/python3

from bc16 import bc16_env
from sys import argv

env = bc16_env.Environment()
if len(argv) < 2:
    env.log('Need two args fin.btap and fout.bin')
    exit(1)

fin = argv[1] + '.btap'
fout = argv[2] + '.bin'

env.log('Converting {0} to {1}'.format(fin, fout))

finHandle = env.open_file_to_read(fin)
foutHandle = env.open_file_to_write(fout)

i = 0
byte = 0
while True:
    b = env.read_byte(finHandle)
    #env.log("read '{0}'".format(b))

    if not b:
        break

    if ord(b) > 0:
        byte += 2**i
    i += 1
    if i > 7:
        env.write_byte(foutHandle, byte)
        #env.log("write '{0}'".format(byte))
        i = 0
        byte = 0

env.close_file(finHandle)
env.close_file(foutHandle)

env.log('Done')


##============================ ea_config_ex_3.py ================================

# Some of the input parameters and options in order to select the settings of the
# evolutionary algorithm are given here for the minimization of f1(x) and f2(x)
# of the ZDT 1 using NSGA-II (Genetic Algorithms).

EA_type = 'GA'
pop_size = 80
n_gen = 300
mut_rate = -1
n_gen_var = 25
cross_rate = 0.9
tour_sel_param = 0.8
perc_rank1 = 1.0
perc_nf = 0


from __future__ import annotations
from datetime import datetime
from functools import total_ordering
from qtpy.QtCore import QObject, Qt
from qtpy.QtGui import QBrush
from typing import Dict, List, Optional, Union
import enum
import logging

logger = logging.getLogger(__name__)

@total_ordering  # Fills in other ordering methods for us
class AlarmSeverity(enum.Enum):
    """
    An enum for the values that an alarm severity can take on. Not inheriting from str so we can do alarm logic
    based comparisons rather than string ones.
    """

    OK = "OK"
    MINOR_ACK = "MINOR_ACK"
    MAJOR_ACK = "MAJOR_ACK"
    INVALID_ACK = "INVALID_ACK"
    UNDEFINED_ACK = "UNDEFINED_ACK"
    MINOR = "MINOR"
    MAJOR = "MAJOR"
    INVALID = "INVALID"
    UNDEFINED = "UNDEFINED"

    def __lt__(self, other):
        """The order in which they are defined is in order of increasing severity for display"""
        if self.__class__ is other.__class__:
            values = [e for e in AlarmSeverity]
            return values.index(self) < values.index(other)
        return NotImplemented

class AlarmItem(QObject):
    """
    An AlarmItem represents the alarm status of a PV, along with all associated data.

    Parameters
    ----------
    name : str
        The name of the PV this alarm is associated with.
    path : str, optional
        The path to the alarm within its configured hierarchy. For example: Accelerator/Line 1/Voltage 2
    alarm_severity : AlarmSeverity, optional
        The current severity level of the alarm itself. May be higher than the PV if latching is enabled.
    alarm_status : str, optional
        The current status of the alarm itself.
    alarm_time : datetime, optional
        The date and time this alarm was raised.
    alarm_value : str, optional
        The current value of this alarm
    pv_severity : AlarmSeverity, optional
        The current severity level of the PV this alarm is associated with.
    pv_status : str, optional
        The current status of the PV this alarm is associated with.
    description : str, optional
        Additional description of this alarm specified by an end-user in a configuration file
    guidance : list, optional
        Any guidance on how to handle this alarm that would be helpful to a user viewing this alarm.
    displays : list, optional
        Any displays this alarm is associated with
    commands : list, optional
        Any commands that should be run in response to this alarm
    enabled : Union[bool, str], optional
        Whether or not this alarm is enabled. A bool in the simple case, a str here will also mean
        disabled, with the str being the date-time it should automatically re-enable.
    filtered: bool
        If true, this alarm is disabled via a filter rule, false otherwise.
    latching : bool, optional
        If set to true, this alarm will remain at the highest severity level it attains even if the PV it is monitoring
        returns to a lower severity level. If false, it will stay in sync with its associated PV.
    delay : int, optional
        The amount of time in seconds a PV must be in an alarm state before raising the alarm
        If not set, alarm is raised immediately.
    alarm_filter : str, optional
        An expression that allows an alarm to enable based on a different PV
    """

    def __init__(
        self,
        name: str,
        path: Optional[str] = None,
        alarm_severity: Optional[AlarmSeverity] = None,
        alarm_status: Optional[str] = None,
        alarm_time: Optional[datetime] = None,
        alarm_value: Optional[str] = None,
        pv_severity: Optional[AlarmSeverity] = None,
        pv_status: Optional[str] = None,
        description: Optional[str] = None,
        guidance: Optional[List[Dict]] = None,
        displays: Optional[List[Dict]] = None,
        commands: Optional[List[Dict]] = None,
        enabled: Optional[Union[bool, str]] = True,
        filtered: Optional[bool] = False,
        latching: Optional[bool] = False,
        annunciating: Optional[bool] = False,
        delay: Optional[int] = None,
        alarm_filter: Optional[str] = None,
    ):
        super().__init__()
        self.name = name
        self.path = path
        self.child_items = []
        self.parent_item = None
        self.alarm_severity = alarm_severity
        self.alarm_status = alarm_status
        self.alarm_time = alarm_time
        self.alarm_value = alarm_value
        self.pv_severity = pv_severity
        self.pv_status = pv_status
        self.description = description
        self.guidance = guidance
        self.displays = displays
        self.commands = commands
        if enabled is None:  # Protect against setting it explicitly to None
            enabled = True
        if filtered is None:
            filtered = False
        if latching is None:
            latching = False
        if annunciating is None:
            annunciating = False
        self.enabled = enabled
        self.filtered = filtered
        self.latching = latching
        self.annunciating = annunciating
        self.delay = delay
        self.alarm_filter = alarm_filter

    def is_leaf(self) -> bool:
        """Return whether or not this alarm is associated with a leaf node in its configured hierarchy"""
        return len(self.child_items) == 0

    def is_enabled(self) -> bool:
        """A convenience method for checking the enabled state of the alarm"""
        if self.filtered:
            return False
        if isinstance(self.enabled, bool):
            return self.enabled
        elif isinstance(self.enabled, str):
            if self.enabled:
                return False  # A non-empty string means a bypass until date has been set, so it is disabled
            else:
                return True
        else:
            logger.error(f"Enabled status for alarm: {self.path} is set to a bad value: {self.enabled}")

    def is_acknowledged(self) -> bool:
        """A convenience method for returning whether or not this item has been acknolwedged"""
        return self.alarm_severity in (
            AlarmSeverity.MINOR_ACK,
            AlarmSeverity.MAJOR_ACK,
            AlarmSeverity.INVALID_ACK,
            AlarmSeverity.UNDEFINED_ACK,
        )

    def is_in_active_alarm_state(self) -> bool:
        """A convenience method for returning whether or not this item is actively in an alarm state"""
        return self.alarm_severity in (
            AlarmSeverity.MINOR,
            AlarmSeverity.MAJOR,
            AlarmSeverity.INVALID,
            AlarmSeverity.UNDEFINED,
        )

    def display_color(self, severity) -> QBrush:
        """
        Return a QBrush with the appropriate color for drawing this alarm based on severity

        Parameters
        ----------
        severity : AlarmSeverity
            The severity to base the display color on
        """
        if not self.is_enabled() and self.is_leaf():
            return QBrush(Qt.gray)
        elif severity == AlarmSeverity.OK:
            return QBrush(Qt.darkGreen)
        elif severity == AlarmSeverity.UNDEFINED:
            return QBrush(Qt.magenta)
        elif severity == AlarmSeverity.MAJOR:
            return QBrush(Qt.red)
        elif severity == AlarmSeverity.MINOR:
            return QBrush(Qt.darkYellow)
        elif severity == AlarmSeverity.MAJOR_ACK:
            return QBrush(Qt.darkRed)
        elif severity == AlarmSeverity.MINOR_ACK:
            return QBrush(Qt.darkGray)
        elif severity == AlarmSeverity.UNDEFINED_ACK:
            return QBrush(Qt.darkMagenta)
        elif severity == AlarmSeverity.INVALID:
            return QBrush(Qt.magenta)
        elif severity == AlarmSeverity.INVALID_ACK:
            return QBrush(Qt.darkMagenta)

    def append_child(self, item: AlarmItem) -> None:
        """Add the input item as a child node for this alarm item"""
        self.child_items.append(item)

    def assign_parent(self, parent: AlarmItem) -> None:
        """Sets the input item as the parent of this alarm item"""
        self.parent_item = parent

    def child(self, row: int) -> AlarmItem:
        """Returns the child item at the input row, or None if nothing exists there"""
        if row < 0 or row >= len(self.child_items):
            return None
        return self.child_items[row]

    def child_count(self) -> int:
        """Return the number of children this item has"""
        return len(self.child_items)

    def row(self) -> int:
        """Return the row of this item relative to its parent, or zero if it has no parent"""
        if self.parent_item:
            return self.parent_item.child_items.index(self)
        return 0

    def column_count(self) -> int:
        """Return the column count of this item"""
        return 1

    def to_config_dict(self) -> Dict[str, any]:
        """Dump the current values of the alarm item into a dict that can be sent as a kafka alarm config message"""
        return {
            "description": self.description,
            "guidance": self.guidance,
            "displays": self.displays,
            "commands": self.commands,
            "enabled": self.enabled,
            "latching": self.latching,
            "annunciating": self.annunciating,
            "delta": self.delay,
            "filter": self.alarm_filter,
        }

    def __repr__(self) -> str:
        return (
            f'AlarmItem("{self.name}", {repr(self.path)}, {str(self.alarm_severity)}, {repr(self.alarm_status)}, '
            f"{repr(self.alarm_time)}, {repr(self.alarm_value)}, {str(self.pv_severity)}, {repr(self.pv_status)}, "
            f"{repr(self.description)}, {repr(self.guidance)}, {repr(self.displays)}, {repr(self.commands)}, "
            f"{self.enabled}, {self.filtered}, {self.latching}, {self.annunciating}, {self.delay}, {repr(self.alarm_filter)})"  # noqa 501
        )