# This script checks that exchanges strategies work

# ##############################################################################################
# Importations
from datetime import datetime

from lib.DefaultNatures.DefaultNatures import *

from src.common.Agent import Agent
from src.common.Aggregator import Aggregator
from src.common.World import World
from src.common.Strategy import *

from src.tools.GraphAndTex import GraphOptions
from src.tools.SubclassesDictionary import get_subclasses

# ##############################################################################################
# Minimum
# the following objects are necessary for the simulation to be performed
# you need exactly one object of each type
# ##############################################################################################

# ##############################################################################################
# Importation of subclasses
# all the subclasses are imported in the following dictionary
subclasses_dictionary = get_subclasses()

# ##############################################################################################
# Creation of the world
# a world contains all the other elements of the model
# a world needs just a name
name_world = "validation"
world = World(name_world)  # creation

# ##############################################################################################
# Definition of the path to the files
world.set_directory("cases/ValidationCases/Results/ExchangeStrategy")  # here, you have to put the path to your results directory

# ##############################################################################################
# Definition of the random seed
# The default seed is the current time (the value returned by datetime.now())
world.set_random_seed("seed")

# ##############################################################################################
# Time parameters
# it needs a start date, the value of an iteration in hours and the total number of iterations
start_date = datetime(year=1, month=1, day=1, hour=0, minute=0, second=0, microsecond=0)  # a start date in the datetime format
world.set_time(start_date,  # time management: start date
               1,  # value of a time step (in hours)
               24)  # number of time steps simulated

# ##############################################################################################
# Model
# ##############################################################################################

# ##############################################################################################
# Creation of nature
# low voltage electricity
LVE = load_low_voltage_electricity()

# ##############################################################################################
# Creation of daemons
price_manager_elec = subclasses_dictionary["Daemon"]["PriceManagerDaemon"]("prices", {"nature": LVE.name, "buying_price": 0, "selling_price": 0})  # sets prices for flat rate

subclasses_dictionary["Daemon"]["LimitPricesDaemon"]({"nature": LVE.name, "limit_buying_price": 1, "limit_selling_price": -1})  # sets prices for the system operator

# ##############################################################################################
# Creation of strategies
# the different distribution strategies
strategy_light_autarky = subclasses_dictionary["Strategy"]["LightAutarkyFullButFew"](get_emergency)
strategy_autarky = subclasses_dictionary["Strategy"]["AutarkyFullButFew"](get_emergency)
strategy_always_satisfied = subclasses_dictionary["Strategy"]["AlwaysSatisfied"]()
strategy_max_exchanges = subclasses_dictionary["Strategy"]["ExchangesFullButFew"](get_emergency)

# strategy grid, which always proposes an infinite quantity to sell and to buy
grid_strategy = subclasses_dictionary["Strategy"]["Grid"]()

# ##############################################################################################
# Manual creation of agents
light_autarky_owner = Agent("light_autarky_owner")

autarky_owner = Agent("autarky_owner")

always_satisfied_owner = Agent("always_satisfied_owner")

exchanges_owner = Agent("exchanges_owner")

aggregators_manager = Agent("aggregators_manager")

# ##############################################################################################
# Manual creation of contracts
BAU_contract = subclasses_dictionary["Contract"]["EgoistContract"]("BAU_contract", LVE, price_manager_elec)

curtailment_contract = subclasses_dictionary["Contract"]["CurtailmentContract"]("curtailment_contract", LVE, price_manager_elec)

cooperative_contract = subclasses_dictionary["Contract"]["CooperativeContract"]("cooperative_contract_elec", LVE, price_manager_elec)

# ##############################################################################################
# Creation of aggregators
aggregator_grid = Aggregator("national_grid", LVE, grid_strategy, aggregators_manager)

aggregator_light_autarky = Aggregator("local_grid_emergency", LVE, strategy_light_autarky, aggregators_manager, aggregator_grid, BAU_contract)
aggregator_autarky = Aggregator("local_grid_price", LVE, strategy_autarky, aggregators_manager, aggregator_grid, BAU_contract)
aggregator_always_satisfied = Aggregator("local_grid_quantity", LVE, strategy_always_satisfied, aggregators_manager, aggregator_grid, BAU_contract)
aggregator_max_exchanges = Aggregator("local_grid_exchanges", LVE, strategy_max_exchanges, aggregators_manager, aggregator_grid, BAU_contract, capacity={"buying": 12, "selling": inf})

# ##############################################################################################
# Manual creation of devices

# Each device is created 4 times
# light autarky strategy
device_BAU_light_autarky = subclasses_dictionary["Device"]["Background"]("device_BAU_light_autarky", BAU_contract, light_autarky_owner, aggregator_light_autarky, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
device_curtailment_light_autarky = subclasses_dictionary["Device"]["Background"]("device_curtailment_light_autarky", curtailment_contract, light_autarky_owner, aggregator_light_autarky, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
production_light_autarky = subclasses_dictionary["Device"]["DummyProducer"]("production_light_autarky", cooperative_contract, light_autarky_owner, aggregator_light_autarky, {"device": "dummy_usage"}, {"max_power": 12}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/DummyProducer.json")

# autarky strategy
device_BAU_autarky = subclasses_dictionary["Device"]["Background"]("device_BAU_autarky", BAU_contract, autarky_owner, aggregator_autarky, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
device_curtailment_autarky = subclasses_dictionary["Device"]["Background"]("device_curtailment_autarky", curtailment_contract, autarky_owner, aggregator_autarky, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
production_autarky = subclasses_dictionary["Device"]["DummyProducer"]("production_autarky", cooperative_contract, autarky_owner, aggregator_autarky, {"device": "dummy_usage"}, {"max_power": 12}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/DummyProducer.json")

# always satisfied strategy
device_BAU_always_satisfied = subclasses_dictionary["Device"]["Background"]("device_BAU_always_satisfied", BAU_contract, always_satisfied_owner, aggregator_always_satisfied, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
device_curtailment_always_satisfied = subclasses_dictionary["Device"]["Background"]("device_curtailment_always_satisfied", curtailment_contract, always_satisfied_owner, aggregator_always_satisfied, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
production_always_satisfied = subclasses_dictionary["Device"]["DummyProducer"]("production_always_satisfied", cooperative_contract, always_satisfied_owner, aggregator_always_satisfied, {"device": "dummy_usage"}, {"max_power": 12}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/DummyProducer.json")

# exchanges strategy
device_BAU_exchanges = subclasses_dictionary["Device"]["Background"]("device_BAU_exchanges", BAU_contract, exchanges_owner, aggregator_max_exchanges, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
device_curtailment_exchanges = subclasses_dictionary["Device"]["Background"]("device_curtailment_exchanges", curtailment_contract, exchanges_owner, aggregator_max_exchanges, {"user": "dummy_user", "device": "dummy_usage"}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/Background.json")
production_exchanges = subclasses_dictionary["Device"]["DummyProducer"]("production_exchanges", cooperative_contract, exchanges_owner, aggregator_max_exchanges, {"device": "dummy_usage"}, {"max_power": 12}, filename="cases/ValidationCases/AdditionalData/DevicesProfiles/DummyProducer.json")

# ##############################################################################################
# Creation of the validation daemon
description = "This script checks that exchange strategies work"

filename = "exchange_strategy_validation"

reference_values = {"light_autarky_owner.LVE.energy_bought": [0, 2, 4, 6, 8, 10, 12, 12, 12, 12, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
                    "light_autarky_owner.LVE.energy_sold": [0, 2, 4, 6, 8, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],

                    "autarky_owner.LVE.energy_bought": [0, 2, 4, 6, 8, 10, 12, 12, 12, 12, 12, 12, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    "autarky_owner.LVE.energy_sold": [0, 2, 4, 6, 8, 10, 12, 12, 12, 12, 12, 12, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

                    "always_satisfied_owner.LVE.energy_bought": [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46],
                    "always_satisfied_owner.LVE.energy_sold": [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],

                    "exchanges_owner.LVE.energy_bought": [0, 2, 4, 6, 8, 10, 12, 12, 12, 12, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
                    "exchanges_owner.LVE.energy_sold": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
                    }

name = "LVE_light_autarky"
export_plot1 = {
    "name": name,
    "filename": "export_"+name,
    "options": GraphOptions(f"{name}_graph_options", ["csv", "LaTeX"], "multiple_series"),
    "X": {"catalog_name_entry": "iteration", "label": r"$t \, [\si{\hour}]$"},
    "Y": {"label": r"$\mathcal{P}_{\textrm{bought}} \, [$\euro{}$]$",
          "graphs": [ {"catalog_name_entry": "light_autarky_owner.LVE.energy_bought_reference", "style": "points", "legend": r"ref."},
                      {"catalog_name_entry": "light_autarky_owner.LVE.energy_bought_simulation", "style": "lines", "legend": r"num."}
                    ]
          },
    "Y2": {"label": r"$\mathcal{P}_{\textrm{sold}} \, [$\euro{}$]$",
          "graphs": [ {"catalog_name_entry": "light_autarky_owner.LVE.energy_sold_reference", "style": "points", "legend": r"ref."},
                      {"catalog_name_entry": "light_autarky_owner.LVE.energy_sold_simulation", "style": "lines", "legend": r"num."}
                    ]
          },
}

name = "LVE_autarky"
export_plot2 = {
    "name": name,
    "filename": "export_"+name,
    "options": GraphOptions(f"{name}_graph_options", ["csv", "LaTeX"], "multiple_series"),
    "X": {"catalog_name_entry": "iteration", "label": r"$t \, [\si{\hour}]$"},
    "Y": {"label": r"$\mathcal{P}_{\textrm{bought}} \, [$\euro{}$]$",
          "graphs": [ {"catalog_name_entry": "autarky_owner.LVE.energy_bought_reference", "style": "points", "legend": r"ref."},
                      {"catalog_name_entry": "autarky_owner.LVE.energy_bought_simulation", "style": "lines", "legend": r"num."}
                    ]
          },
    "Y2": {"label": r"$\mathcal{P}_{\textrm{sold}} \, [$\euro{}$]$",
          "graphs": [ {"catalog_name_entry": "autarky_owner.LVE.energy_sold_reference", "style": "points", "legend": r"ref."},
                      {"catalog_name_entry": "autarky_owner.LVE.energy_sold_simulation", "style": "lines", "legend": r"num."}
                    ]
          },
}

name = "LVE_always_satisfied"
export_plot3 = {
    "name": name,
    "filename": "export_"+name,
    "options": GraphOptions(f"{name}_graph_options", ["csv", "LaTeX"], "multiple_series"),
    "X": {"catalog_name_entry": "iteration", "label": r"$t \, [\si{\hour}]$"},
    "Y": {"label": r"$\mathcal{P}_{\textrm{bought}} \, [$\euro{}$]$",
          "graphs": [ {"catalog_name_entry": "always_satisfied_owner.LVE.energy_bought_reference", "style": "points", "legend": r"ref."},
                      {"catalog_name_entry": "always_satisfied_owner.LVE.energy_bought_simulation", "style": "lines", "legend": r"num."}
                    ]
          },
    "Y2": {"label": r"$\mathcal{P}_{\textrm{sold}} \, [$\euro{}$]$",
          "graphs": [ {"catalog_name_entry": "always_satisfied_owner.LVE.energy_sold_reference", "style": "points", "legend": r"ref."},
                      {"catalog_name_entry": "always_satisfied_owner.LVE.energy_sold_simulation", "style": "lines", "legend": r"num."}
                    ]
          },
}

name = "Bought_Energy_Alltogether"
export_plot4 = {
    "name": name,
    "filename": "export_"+name,
    "options": GraphOptions(f"{name}_graph_options", ["csv", "LaTeX"], "multiple_series"),
    "X": {"catalog_name_entry": "iteration", "label": r"$t \, [\si{\hour}]$"},
    "Y": {"label": r"$\mathcal{P}_{ref.} \, [\si{\watt}]$",
          "graphs": [ {"catalog_name_entry": "light_autarky_owner.LVE.energy_bought_reference", "style": "points", "legend": r"light autarky ref."},
                      {"catalog_name_entry": "autarky_owner.LVE.energy_bought_reference", "style": "points", "legend": r"autarky ref."},
                      {"catalog_name_entry": "always_satisfied_owner.LVE.energy_bought_reference", "style": "points", "legend": r"always satisfied ref."}
                      ]
          },
    "Y2": {"label": r"$\mathcal{P}_{num.} \, [\si{\watt}]$",
          "graphs": [ {"catalog_name_entry": "light_autarky_owner.LVE.energy_bought_simulation", "style": "lines", "legend": r"light autarky num."},
                      {"catalog_name_entry": "autarky_owner.LVE.energy_bought_simulation", "style": "lines", "legend": r"autarky num."},
                      {"catalog_name_entry": "always_satisfied_owner.LVE.energy_bought_simulation", "style": "lines", "legend": r"always satisfied num."}
                      ]
          }
}

name = "Sold_Energy_Alltogether"
export_plot5 = {
    "name": name,
    "filename": "export_"+name,
    "options": GraphOptions(f"{name}_graph_options", ["csv", "LaTeX"], "multiple_series"),
    "X": {"catalog_name_entry": "iteration", "label": r"$t \, [\si{\hour}]$"},
    "Y": {"label": r"$\mathcal{P}_{ref.} \, [\si{\watt}]$",
          "graphs": [ {"catalog_name_entry": "light_autarky_owner.LVE.energy_sold_reference", "style": "points", "legend": r"lgt autark."},
                      {"catalog_name_entry": "autarky_owner.LVE.energy_sold_reference", "style": "points", "legend": r"autark."},
                      {"catalog_name_entry": "always_satisfied_owner.LVE.energy_sold_reference", "style": "points", "legend": r"alws satis."}
                      ]
          },
    "Y2": {"label": r"$\mathcal{P}_{num.} \, [\si{\watt}]$",
          "graphs": [ {"catalog_name_entry": "light_autarky_owner.LVE.energy_sold_simulation", "style": "lines", "legend": r""},
                      {"catalog_name_entry": "autarky_owner.LVE.energy_sold_simulation", "style": "lines", "legend": r""},
                      {"catalog_name_entry": "always_satisfied_owner.LVE.energy_sold_simulation", "style": "lines", "legend": r""}
                      ]
          }
}

parameters = {"description": description, "filename": filename, "reference_values": reference_values, "tolerance": 1E-6, "export_plots": [export_plot1, export_plot2, export_plot3, export_plot4, export_plot5]}

validation_daemon = subclasses_dictionary["Daemon"]["ValidationDaemon"]("exchange_strategy_test", parameters)

# ##############################################################################################
# Simulation start
world.start()


import unittest
import numpy as np
import pandas as pd

from inmoose.pycombat.covariates import make_design_matrix
from inmoose.pycombat.pycombat_norm import check_mean_only, check_NAs
from inmoose.pycombat.pycombat_norm import compute_prior, postmean, postvar, it_sol, int_eprior
from inmoose.pycombat.pycombat_norm import calculate_mean_var, calculate_stand_mean
from inmoose.pycombat.pycombat_norm import standardise_data, fit_model, adjust_data
from inmoose.pycombat import pycombat_norm

class test_pycombat(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.batch = np.asarray([1,1,1,2,2,3,3,3,3])
        matrix = np.transpose([np.random.normal(size=1000,loc=3,scale=1),np.random.normal(size=1000,loc=3,scale=1),np.random.normal(size=1000,loc=3,scale=1),
                              np.random.normal(size=1000,loc=2,scale=0.6),np.random.normal(size=1000,loc=2,scale=0.6),
                              np.random.normal(size=1000,loc=4,scale=1),np.random.normal(size=1000,loc=4,scale=1),np.random.normal(size=1000,loc=4,scale=1),np.random.normal(size=1000,loc=4,scale=1)])

        self.matrix = pd.DataFrame(data=matrix,columns=["sample_"+str(i+1) for i in range(9)],index=["gene_"+str(i+1) for i in range(1000)])
        self.matrix_adjusted = pycombat_norm(self.matrix, self.batch)

        # useful constants for unit testing
        ref_batch = None
        mean_only = False
        par_prior = False
        precision = None
        mod = None
        self.dat = self.matrix.values
        design, self.batchmod, _, self.batches, self.n_batches, self.n_batch, self.n_array, ref = make_design_matrix(self.dat, self.batch, None, mod, True, ref_batch)
        self.design = np.transpose(design)

        NAs = check_NAs(self.dat)
        self.B_hat, self.grand_mean, self.var_pooled = calculate_mean_var(self.design, self.batches, ref, self.dat, NAs, self.n_batches, self.n_batch, self.n_array)
        self.stand_mean = calculate_stand_mean(self.grand_mean, self.n_array, self.design, self.n_batch,self.B_hat)
        self.s_data = standardise_data(self.dat, self.stand_mean, self.var_pooled, self.n_array)
        self.gamma_star, self.delta_star, self.batch_design = fit_model(self.design,self.n_batch, self.s_data, self.batches, mean_only, par_prior, precision, ref, NAs)
        self.bayes_data = adjust_data(self.s_data, self.gamma_star, self.delta_star, self.batch_design, self.n_batches, self.var_pooled, self.stand_mean, self.n_array, ref, self.batches, self.dat)

    def test_compute_prior(self):
        print("aprior",compute_prior("a",self.gamma_star,False))
        self.assertEqual(compute_prior("a",self.gamma_star,True), 1)
        print("bprior",compute_prior("b",self.gamma_star,False))
        self.assertEqual(compute_prior("b",self.gamma_star,True), 1)

    def test_postmean(self):
        self.assertEqual(np.shape(postmean(self.gamma_star,self.delta_star,self.gamma_star,self.delta_star)), np.shape(self.gamma_star))

    def test_postvar(self):
        self.assertEqual(np.sum(postvar([2,4,6],2,1,1) - [2,3,4]), 0)

    #def test_it_sol(self):
    #    ()

    #def test_int_eprior(self):
    #    ()

    @unittest.skip("automate the verification")
    def test_check_mean_only(self):
        check_mean_only(True)
        check_mean_only(False)
        print("Only one line of text should have been printed above.")

    def test_check_NAs(self):
        self.assertFalse(check_NAs([0,1,2]))

    def test_calculate_mean_var(self):
        self.assertEqual(np.shape(self.B_hat)[0], np.shape(self.design)[0])
        self.assertEqual(np.shape(self.grand_mean)[0], np.shape(self.dat)[0])
        self.assertEqual(np.shape(self.var_pooled)[0], np.shape(self.dat)[0])

    def test_calculate_stand_mean(self):
        self.assertEqual(np.shape(self.stand_mean), np.shape(self.dat))

    def test_standardise_data(self):
        self.assertEqual(np.shape(self.s_data), np.shape(self.dat))

    def test_fit_model(self):
        self.assertEqual(np.shape(self.gamma_star)[1], np.shape(self.dat)[0])
        self.assertEqual(np.shape(self.delta_star)[1], np.shape(self.dat)[0])
        self.assertEqual(np.shape(self.batch_design), np.shape(self.design))

    def test_adjust_data(self):
        self.assertEqual(np.shape(self.bayes_data), np.shape(self.dat))

    def test_pycombat(self):
        self.assertEqual(np.shape(self.matrix), np.shape(self.matrix_adjusted))
        self.assertTrue(np.mean(self.matrix.values)-np.mean(self.matrix_adjusted.values) <= np.mean(self.matrix.values)*0.05)
        self.assertTrue(np.var(self.matrix_adjusted.values) <= np.var(self.matrix.values))


from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers

class Encoder(APIView):

    def get(self, request):
        number = self.request.query_params['NumToEncode']
        numberInt = int(number)

        # Add 8192 to the value.
        translated_number = numberInt + 8192

        # Encoding requires to form two bytes from the value.
        # Most significant bit of each byte has to be cleared. In binary: bits 1 to 0
        # Using & operator that compares 1 to 1 return 0
        firstByte = translated_number & 0x007F
        secondByte = translated_number & 0xFF80

        # In binary second_byte bits are shifted to the left by 1 places inserting a 0
        shiftedSecondByte = secondByte << 1
        encodedValue = firstByte + shiftedSecondByte

        # Format the two bytes as a hexadecimal string and return it as key.
        key = hex(encodedValue)[2:]
        print(key)
        return Response({'key': key})

class Decoder(APIView):

    def get(self, request):
        s1 = self.request.query_params['Num1T0Decode']
        s2 = self.request.query_params['Num2ToDecode']

        firstNumberToDecode = int(s1, 16)
        SeconNumberToDecode = int(s2, 16)

        # Return bits shifted to the left by 7 places inserting 0
        # If f_byte is 7f which in binary is 1111111, gives 11111110000000
        # This gives back a 14-bit range number
        ShiftedFirstNumberToDecode = firstNumberToDecode << 7

        decodedValue = (ShiftedFirstNumberToDecode + SeconNumberToDecode) - 8192
        key = decodedValue
        print(key)
        return Response({'key': key})


import simplenlp
from metanl import english
import math, random
from luminoso3.background_space import get_commonsense_assoc
from colorizer.color_data import make_lab_color_data, lab_to_rgb, rgb_to_hsv
from colorizer.colorvote import weighted_elect_samples

ENGLISH = simplenlp.get('en')
ASSOC = get_commonsense_assoc('en', 100)

COLORDATA = {}
origdata = make_lab_color_data()

def importance_factor(colorname):
    imp = 10000 / math.sqrt(english.word_frequency(colorname.split()[0], 1000000))
    return int(imp)

for key, values in origdata.items():
    subset_values = random.sample(values,
      min(len(values), int(math.ceil(importance_factor(key)*math.sqrt(len(values))))))
    COLORDATA[key] = subset_values

def output_colors(labcolors):
    return [lab_to_rgb(c) for c in sorted(labcolors)]

class IncrementalColorizer(object):
    def __init__(self, ncolors):
        self.ncolors = ncolors
        self.colors = [(128,128,128)] * ncolors
        self.active_colors = [(128,128,128)] * (ncolors//2)
        self.colordata = {}
        self._load_color_data()
        self.votes = []

    def _load_color_data(self):
        self.colordata = COLORDATA

    def get_color_votes(self, concept, weight=1):
        votes = []
        if concept in self.colordata:
            morevotes = [(color, weight) for color in self.colordata[concept]]
            votes.extend(morevotes)
        similar = ASSOC.terms_similar_to_vector(
            ASSOC.vector_from_terms([(concept, 1)]))[:3]
        for sim, wsim in similar:
            if sim in self.colordata:
                morevotes = [(color, wsim*weight*0.1) for color in
                             self.colordata[sim]]
                votes.extend(morevotes)
        return votes

    def add_text(self, text, force_stopword=False):
        active_concept = text
        active_concept_norm = ENGLISH.normalize(text).strip()
        if force_stopword or ENGLISH.is_stopword(text):
            return {
                'colors': output_colors(self.colors),
                'active': text,
                'activeColors': [],
                'weight': 0
            }
        active_votes = []
        if active_concept:
            active_votes = self.get_color_votes(active_concept_norm)
            if not active_votes and text.endswith('y'):
                active_concept_norm = ENGLISH.normalize(text[:-1]).strip()
                active_votes = self.get_color_votes(active_concept_norm)
            if not active_votes and text.endswith('ish'):
                active_concept_norm = ENGLISH.normalize(text[:-3]).strip()
                active_votes = self.get_color_votes(active_concept_norm)

            self.votes.sort(key=lambda v: v[1])
            self.votes = [(b, w*0.8) for (b, w) in self.votes[-400:]]
            self.votes.extend(active_votes)
            self.active_colors = weighted_elect_samples(active_votes,
                                                        self.ncolors//2+1)

            self.colors = weighted_elect_samples(self.votes, self.ncolors)
        return {
            'colors': output_colors(self.colors),
            'active': active_concept,
            'activeColors': output_colors(self.active_colors),
            'weight': len(active_votes)
        }


import cv2
import numpy as np
import Fuzzy_means as fz
from tkinter import *
from tkinter.ttk import *
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import font
from PIL import Image, ImageTk
global file_path
file_path = []
def Openfile():
    global file_path
    file_path =  filedialog.askopenfilename(initialdir = "/home/fasahat/pCloudDrive/MyPictures/",title = "Select file to open",filetypes = [("picture","*.jpg .png .tif .bmp .gif"),("all files","*.*")])
    #file_path =  filedialog.askopenfilename(initialdir = "P:\MyPictures",title = "Select file to open",filetypes = [("picture","*.jpg .png .tif .bmp .gif"),("all files","*.*")])
    image = Image.open(file_path)
    canvas_in.image = ImageTk.PhotoImage(image.resize((170, 195), Image.ANTIALIAS))
    canvas_in.create_image(0, 0, image=canvas_in.image, anchor='nw')
    img = cv2.imread(file_path)
    SS=img.shape
    my_text = tk.Text(bottom_right, height=1, width=16)
    my_text.grid(row=0, column=0)
    my_text.insert(tk.END, str(SS))

def Savefile():
    filedialog.asksaveasfilename(initialdir = "/", title = "Save file as",filetypes = [("picture","*.png"),("all files","*.*")])
    #filedialog.asksaveasfilename(initialdir = "P:\", title = "Save file as",filetypes = [("picture","*.png"),("all files","*.*")])
def Dis_input():
    global T_img
    top=Toplevel()
    top.title("Input Image")
    T_img=ImageTk.PhotoImage(Image.open(file_path))
    T_label=tk.Label(top,image=T_img).pack()
    Exit_button2 =tk.Button(top, text="Exit", command=top.destroy).pack()

def Segmentation():
    global tempfile_path
    #tempfile_path="/home/fasahat/pCloudDrive/MyPictures/temp_file.png"
    tempfile_path='P:\MyPictures\temp_file.png'
    file_path
    if len(file_path) > 0:
        img = cv2.imread(file_path)
        if Type == 'Color':
            in_img = img
            if Method == 'KM':
                i_img = img.reshape((-1,3)) # color
                i_img = np.float32(i_img)
                criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, It, EE)
                ret,label,center=cv2.kmeans(i_img,K,None,criteria,It,cv2.KMEANS_RANDOM_CENTERS)
            else:
                ret,label,center = fz.Clustering(K, Type, Method, img, It, EE)
        if Type == 'Grayscale':
            in_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            if Method == 'KM':
                i_img =in_img.reshape((-1,1)) # grayscale
                i_img = np.float32(i_img)
                criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, It, EE)
                ret,label,center=cv2.kmeans(i_img,K,None,criteria,It,cv2.KMEANS_RANDOM_CENTERS)
            else:
                ret,label,center = fz.Clustering(K, Type, Method, img, It, EE)
        center = np.uint8(center)
        res = center[label.flatten()]
        res2 = res.reshape((in_img.shape))
        cv2.imwrite(tempfile_path,res2)
        image_seg = Image.open(tempfile_path)
        canvas_seg.image = ImageTk.PhotoImage(image_seg.resize((170, 195), Image.ANTIALIAS))
        canvas_seg.create_image(0, 0, image=canvas_seg.image, anchor='nw')

def Dis_seg():
    global S_img
    top=Toplevel()
    top.title("Segmented Image")
    S_img=ImageTk.PhotoImage(Image.open(tempfile_path))
    S_label=tk.Label(top,image=S_img).pack()
    Exit_button1 =tk.Button(top, text="Exit", command=top.destroy).pack()

def K_value():
    global K
    K=int(cluster_box.get())

def EE_value():
    global EE
    EE=float(epsilon_box.get())

def It_value():
    global It
    It=int(iteration_box.get())

def method_name():
    global Method
    Method=method_box.get()

def Type_name():
    global Type
    Type=Type_box.get()

def Data_in():
    K_value()
    EE_value()
    It_value()
    method_name()
    Type_name()
    if Method == 'Select method' or Type=='Select data type':
        messagebox.showerror("Error","Please select the method or type correctly")
    if len(file_path) <1:
        messagebox.showerror("Error","Please select input file correctly")
    if Method != 'Select method' and Type!='Select data type' and len(file_path) >1:
        messagebox.showinfo("Selection Value","Intial setting is sucessfully completed")

#GUI code start from here--------------------------------------------------------

root = tk.Tk()
root.minsize(420, 420)
root.title("Clustering- Image Segmentation")

Cap_font = font.Font(name='TkCaptionFont',exists=True,family='Arial Narrow',size=9,slant='roman',weight='normal')
Cap_font.config

menubar = tk.Menu(root)
# file manage bar
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=Openfile)
filemenu.add_command(label="Save", command=Savefile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

# Image Display manage bar
dismenu = tk.Menu(menubar, tearoff=0)
dismenu.add_command(label="Input Image", command=Dis_input)
dismenu.add_command(label="Segmented Image", command=Dis_seg)
menubar.add_cascade(label="View", menu=dismenu)
root.config(menu=menubar)

#root.geometry("400x400")
top_left = tk.LabelFrame(root, text="Input Data", width=200, height=200)
top_left.grid(row=0, column=0, padx=0, pady=0)

canvas_in = Canvas(top_left, bg='gray', height=170, width=195, borderwidth=2, highlightthickness=2)
canvas_in.grid(row=0, column=0, sticky='NESW', padx=0, pady=0)

#----------------------------------------------------------------------------------------
top_right = tk.LabelFrame(root, text="Segmented Data", width=198, height=200)
top_right.grid(row=0, column=1, padx=0, pady=0)

canvas_seg=Canvas(top_right, bg='gray', height= 170, width=195, borderwidth=2, highlightthickness=2)
canvas_seg.grid(row=0,column=0, sticky='NESW' ,padx=0,pady=0)

#-------------------------------------------------------------------------------------
bottom_left = ttk.LabelFrame(root, text="Initilization", width=200, height=200)
bottom_left.grid(row=1, column=0, padx=0, pady=0)

spin_label1 = tk.Label(bottom_left, text="K :")
spin_label1.grid(row=1, column=0, sticky='W')

spin_label2 = tk.Label(bottom_left, text="E :")
spin_label2.grid(row=2, column=0, sticky='W')

spin_label3 = tk.Label(bottom_left, text="I :")
spin_label3.grid(row=3, column=0, sticky='W')

var1 = tk.StringVar(value='5') # default setting
cluster_box = tk.Spinbox(bottom_left, cursor="arrow", from_=2, to=5, width=10,textvariable=var1, justify=tk.RIGHT, command=K_value)
cluster_box.grid(row=1, column=1, sticky='E', pady=3)

var2 = tk.StringVar(value='0.050') # default setting
epsilon_box = tk.Spinbox(bottom_left, increment=0.001, format='%.3f', cursor="arrow", from_=0, to=1, width=10,textvariable=var2, justify=tk.RIGHT, command=EE_value)
epsilon_box.grid(row=2, column=1, sticky='E', pady=3)

var3 = tk.StringVar(value='10') # default setting
iteration_box = tk.Spinbox(bottom_left,cursor="arrow", from_=5, to=30, width=10,textvariable=var3, justify=tk.RIGHT, command=It_value)
iteration_box.grid(row=3, column=1, sticky='E', pady=3)

#Label('Combobox with text entry')
list1 = ('Select method','KM', 'FCM', 'ORFCM')
var4 = tk.StringVar()
var4.set(list1[0])
method_box = ttk.Combobox(bottom_left, width = 14, height=3, textvariable=var4, values=list1)
method_box.grid(row=4, column=1,sticky='E', pady=3)

list2 = ('Select data type','Color', 'Grayscale')
var5 = tk.StringVar()
var5.set(list2[0])
Type_box = ttk.Combobox(bottom_left, width = 14, height=3, textvariable=var5, values=list2)
Type_box.grid(row=5, column=1,sticky='E', pady=3)

#--------------------------------------------------------------------------------
bottom_right = tk.LabelFrame(root, text="Command Window", width=200, height=200)
bottom_right.grid(row=1, column=1, padx=0, pady=0)

my_text = tk.Text(bottom_right, height=1, width=16)
my_text.grid(row=0, column=0)
my_text.insert(tk.END, "(X,Y,Z)Image Size")

Upload_button = tk.Button(bottom_right, text="Upload data", command= Data_in)
Upload_button.grid(row=1, column=0,pady=3)

seg_button1 = tk.Button(bottom_right, text="segmentation", command=Segmentation)
seg_button1.grid(row=2, column=0, pady=3)

Exit_button = tk.Button(bottom_right, text='Program Exit', command=root.quit)
Exit_button.grid(row=3,column=0, pady=3)

root.iconphoto(False, PhotoImage(file ='/home/fasahat/pCloudDrive/Python_codes/Image Segmentation/Eco-Leafs.png'))
#root.iconbitmap("P:\Python_codes\Image Segmentation\Eco-Leafs.ico")
root.mainloop()


from cell import Cell

class Vertex(object):
    def __init__(self, cell, alt):
        self.cell = cell
        self.alt = alt

    def __repr__(self):
        return '<Vertex cell=%r,alt=%r>' % (self.cell, self.alt)

class Edge(object):
    def __init__(self, orig, dest):
        self.orig = orig
        self.dest = dest

    def __repr__(self):
        return '<Edge orig=%r,dest=%r>' % (self.orig, self.dest)

class Graph(object):
    def __init__(self, edges=()):

        self.vertices = {}
        self.edges = {}
        for e in edges:
            if '<Vertex cell=<Cell col=%r,row=%r>,alt=%r>' % (e.dest.cell.col, e.dest.cell.row, e.dest.alt) not in self.vertices:
                dest = Vertex(cell=Cell(col=e.dest.cell.col, row=e.dest.cell.row), alt=e.dest.alt)
            else:
                dest = self.get_vertex(col=e.dest.cell.col, row=e.dest.cell.row, alt=e.dest.alt)

            if '<Vertex cell=<Cell col=%r,row=%r>,alt=%r>' % (e.orig.cell.col, e.orig.cell.row, e.orig.alt) not in self.vertices:
                orig = Vertex(cell=Cell(col=e.orig.cell.col, row=e.orig.cell.row), alt=e.orig.alt)
            else:
                orig = self.get_vertex(col=e.orig.cell.col, row=e.orig.cell.row, alt=e.orig.alt)

            self.add_edge(orig=orig, dest=dest)

    def add_vertex(self, alt, cell=None, col=None, row=None):
        if cell is not None:
            v = Vertex(cell=cell, alt=alt)
        else:
            v = Vertex(cell=Cell(col=col, row=row), alt=alt)

        self.vertices[v.__repr__()] = v

    def add_edge(self, orig, dest):
        e = Edge(orig=orig, dest=dest)
        self.edges[e.__repr__()] = e

    def get_vertex(self, col, row, alt):
        return self.vertices['<Vertex cell=<Cell col=%r,row=%r>,alt=%r>' % (col, row, alt)]

    def get_edge(self, orig, dest):
        return self.vertices['<Vertex orig=%r,dest=%r>' % (orig, dest)]

def movement(row_orig, col_orig, vel, num_cols):
    next_r = row_orig + vel[0]
    next_c = (col_orig + vel[1]) % num_cols
    return Cell(row=next_r, col=next_c)

def construct_graph(config):
    edges = []
    for a in range(config['A']):  # For all high levels

        for r in range(config['R']):  # For all rows

            for c in range(config['C']):  # For all columns

                for possible_level in range(max(a-1, 1), min(a+2, config['A'])):  # For all high levels the balloon can move

                    wind_velocity = config['a_map'][possible_level][r][c]
                    cell_dest = movement(row_orig=r, col_orig=c, vel=wind_velocity, num_cols=config['C'])

                    # Check if destination cell is out of range
                    if 0 <= cell_dest.row < config['R']:
                        cell_orig = Cell(row=r, col=c)
                    else:
                        continue

                    v_orig = Vertex(cell=cell_orig, alt=a)
                    v_dest = Vertex(cell=cell_dest, alt=possible_level)
                    edges.append(Edge(v_orig, v_dest))

    return Graph(edges=edges)


import argparse
import os
import sys

#krun --directory src/stf  -cPGM="`cat test/semantics/basic_routing/basic_routing.p4`" -pPGM="kast --directory src/ -o kore" -cSTF="`cat src/x.stf`" -pSTF="kast -s STFPgm -m STF-TEST-SYNTAX --directory src/stf -o kore"  --save-temps --verbose
def run_cli(args):
    command = """krun                                                 \\
     --directory src/cli                                                \\
     -cPGM="`cat {p4}`"                                                 \\
     -pPGM="kast -s P4Program -m P4-SYNTAX --directory src/cli -o kore" \\
     -cCLI="`cat {cli}`"                                                \\
     -pCLI="kast -s CLIPgm -m CLI-SYNTAX --directory src/cli -o kore"   \\
     {debugger} {args}                                                 \\
      """
    command = command.format(
        p4=args.p4,
        cli=args.cli,
        debugger="--debugger" if args.debugger else "",
        args=args.krun_args
    )
    print(command, file=sys.stderr)
    os.system(command)

def search_cli(args):
    command = """krun                                                 \\
     --directory src/cli                                                \\
     -cPGM="`cat {p4}`"                                                 \\
     -pPGM="kast -s P4Program -m P4-SYNTAX --directory src/cli -o kore" \\
     -cCLI="`cat {cli}`"                                                \\
     -pCLI="kast -s CLIPgm -m CLI-SYMBOLIC-SYNTAX --directory src/cli -o kore"   \\
      {args}                                                 \\
      """
    command = command.format(
        p4=args.p4,
        cli=args.cli,
        args=args.krun_args
    )
    print(command, file=sys.stderr)
    os.system(command)

def p4assert(args):
    command = """krun                                                 \\
     --directory src/p4assert                                                \\
     -cPGM="`cat {p4}`"                                                 \\
     -pPGM="kast -s P4Program -m P4ASSERT-SYNTAX --directory src/p4assert -o kore" \\
     -cCLI="`cat {cli}`"                                                \\
     -pCLI="kast -s CLIPgm -m CLI-SYMBOLIC-SYNTAX --directory src/p4assert -o kore"   \\
      {args}                                                 \\
      """
    command = command.format(
        p4=args.p4,
        cli=args.cli,
        args=args.krun_args
    )
    print(command, file=sys.stderr)
    os.system(command)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(prog='p4k')
    parser.add_argument('--krun-args', type=str, help='Additional arguments to be passed to krun', default="--verbose --save-temps")
    subparsers = parser.add_subparsers(dest='command')

    parser_run_cli = subparsers.add_parser('run-cli', help='Runs P4 code with CLI file')
    parser_run_cli.add_argument('p4', type=str, help='P4 file')
    parser_run_cli.add_argument('cli', type=str, help='CLI file')
    parser_run_cli.add_argument('--debugger', action='store_true', help='Run in debugger mode', default=False)

    parser_search_cli = subparsers.add_parser('search-cli', help='Symbolically run P4 code with CLI file')
    parser_search_cli.add_argument('p4', type=str, help='P4 file')
    parser_search_cli.add_argument('cli', type=str, help='CLI file')

    parser_p4assert = subparsers.add_parser('p4assert', help='P4 assert')
    parser_p4assert.add_argument('p4', type=str, help='P4 file')
    parser_p4assert.add_argument('cli', type=str, help='CLI file')

    args = parser.parse_args()
    if args.command == 'run-cli':
        run_cli(args)
    if args.command == 'search-cli':
        search_cli(args)
    if args.command == 'p4assert':
        p4assert(args)


"""This is the main convolutional neural network that's used by the agent
   both in RLlib and the non-distributed variant."""
import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Flatten, Permute, Input
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.activations import relu, linear
from ray.rllib.models.tf.tf_modelv2 import TFModelV2

def create_dqn_model(num_actions):
    """Just creates the model and returns it, and is used by the agent to
       create the prediction and target networks."""
    return Sequential([
        Permute((2, 3, 1)),
        Conv2D(32, 8, strides=4, activation=relu),
        Conv2D(64, 4, strides=2, activation=relu),
        Conv2D(64, 3, strides=1, activation=relu),

        Flatten(),
        Dense(512, activation=relu),
        Dense(num_actions, activation=linear),
    ])

class DQNRLlibModel(TFModelV2):
    """This is the same model as above for RLlib. It requires the class to implement
       some API which is needed for RLlib to generate the graph."""
    def __init__(self, obs_space, action_space, num_outputs, model_config, name):
        super().__init__(obs_space, action_space, num_outputs, model_config, name)
        inputs = Input(obs_space.shape)
        cnn1 = Conv2D(32, 8, strides=4, activation=relu)(inputs)
        cnn2 = Conv2D(64, 4, strides=2, activation=relu)(cnn1)
        cnn3 = Conv2D(64, 3, strides=1, activation=relu)(cnn2)
        flatten = Flatten()(cnn3)
        fnn1 = Dense(512, activation=relu)(flatten)
        fnn2 = Dense(num_outputs, activation=linear)(fnn1)
        value_out = Dense(1, activation=None)(fnn2)
        self.base_model = Model(inputs, [fnn2, value_out])
        self.register_variables(self.base_model.variables)

    def forward(self, input_dict, state, seq_lens):
        model_out, self._value_out = self.base_model(input_dict['obs'])
        return model_out, state

    def value_function(self):
        return tf.reshape(self._value_out, [-1])

    def import_from_h5(self, h5_file: str) -> None:
        self.base_model.load_weights(h5_file)


temp=input("Input you temperature: ")
temp=temp.lower()
print(temp)  # check if numbers did not lowered
templetter=temp.find("f")
templetter=temp.find("c")

letter=temp[templetter]

if letter == "f":
    temp = temp.replace('f', '')
    temp=int(temp)
    C=((5*temp)-160)/9  #temp in Celsius

    print(C)
else:
    temp = temp.replace('c', '')
    temp = int(temp)
    F = (9*temp+(32*5)) / 5  # temp in Celsius

    print(F)


import json
from pathlib import Path

icons = json.loads(Path('icons.json').read_text())
recipe_data = json.load(open('recipes.json'))

icons_created = []

target_items = ['LOGISTIC_SCIENCE_PACK']

def create_recipe(target_items, recipe_data, nodes, diagram):
    for item in target_items:
        if item not in recipe_data:
            return
        nodes[item] = f"{item}((  ))"
        for ingredient in recipe_data[item]:
            nodes[
                ingredient] = f"{ingredient}((  ))"
            try:
                diagram[item].append(ingredient)
            except KeyError:
                diagram[item] = [
                    ingredient,
                ]
            create_recipe([
                ingredient,
            ], recipe_data, nodes, diagram)

nodes = {}
diagram = {}

target_items = [
    'LOGISTIC_SCIENCE_PACK', 'AUTOMATION_SCIENCE_PACK',
    'MILITARY_SCIENCE_PACK', 'CHEMICAL_SCIENCE_PACK',
    'PRODUCTION_SCIENCE_PACK', 'UTILITY_SCIENCE_PACK', 'ROCKET_SILO'
]
# target_items = [
#     'ROCKET_SILO',
# ]

create_recipe(target_items, recipe_data, nodes, diagram)

with open('factorio.md', 'w') as f:
    f.write('```mermaid\n')
    f.write('graph TD\n\n')

    for node in nodes.values():
        f.write(f"{node}\n")

    f.write('\n\n')

    for product, ingredients in diagram.items():
        for ingredient in set(ingredients):
            f.write(f"{ingredient:50} --> {product}\n")

    f.write('\n\n\n```\n')


from flask import Flask, request, jsonify
app = Flask(__name__)

languages = [{'name' : 'javascript'},{'name' : 'python'}]
@app.route('/test', methods=['GET'])
def returnAll():
  return jsonify({'languages' : languages})

@app.route('/test', methods=['POST'])
def addOne():
  language = {'name' : request.json['name']}
  languages.append(language)
  return jsonify({'languages' : languages})

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8000)


dp = [[[0]*21 for _ in range(21)] for __ in range(21)]

def solve(x, y, z):
    if x <= 0 or y <= 0 or z <= 0:
        return 1
    if x > 20 or y > 20 or z > 20:
        return solve(20, 20, 20)

    if dp[x][y][z]:
        return dp[x][y][z]

    if x < y < z:
        dp[x][y][z] = solve(x, y, z - 1) + solve(x, y - 1, z - 1) - solve(x, y - 1, z)
        return dp[x][y][z]

    dp[x][y][z] = solve(x - 1, y, z) + solve(x - 1, y - 1, z) + solve(x - 1, y, z - 1) - solve(x - 1, y - 1, z - 1)
    return dp[x][y][z]

while True:
    a, b, c = map(int, input().split())

    if a == b == c == -1:
        break

    print("w(%d, %d, %d) = %d" % (a, b, c, solve(a, b, c)))

"""
DP 메모이제이션에 대해서 묻는 문제.
재귀함수의 계산 결과를 DP에 저장해두고
해당 DP를 호출하여 0이 아닐경우 값이 있다는 뜻이므로 계산을 스킵하여
프로그램 속도를 비약적으로 향상시킴
실제로 DP를 사용하지 않고 해당 알고리즘을 풀게 된다면 숫자 15를 넘어가면 연산이 거의 불가할 것이다.
"""


import tensorflow as tf

class Feed_Forward:
    def __init__(self, architecture=None):
        self.neurons = architecture
    def model(self, x):
        # neurons = [784, 100, 400, 100, 10]
        architecture = []
        for i in range(len(self.neurons) - 1):
            layer = {'weights': tf.Variable(tf.random_normal([self.neurons[i], self.neurons[i+1]])),
                    'biases': tf.Variable(tf.random_normal([self.neurons[i+1]]))}

            architecture.append(layer)

        layer_output = x
        for i in range(len(architecture) - 1):
            layer_output = tf.matmul(layer_output, architecture[i]['weights'])
            layer_output = tf.add(layer_output, architecture[i]['biases'])

            layer_output = tf.nn.relu(layer_output)

        layer_output = tf.matmul(layer_output, architecture[-1]['weights'])
        final_output = tf.add(layer_output, architecture[-1]['biases'])

        return final_output


import logging

from LibTqtk.Connectors import PriceFileConverter
import sys
from dateutil import parser as Prs

class Tick(PriceFileConverter.PriceFileConverter):

    def __init__(self, config_folder, config_file, parse):
        super().__init__(config_folder, config_file, parse)

    def parse_file_name(self, file_name):
        """
        Parse a Dukascopy Renko File name into
        internal format

        :return:
        """
        _comps = file_name.replace(".csv","").split("_")
        logging.info(f"Parsed file name elements {_comps}")

        assert _comps[1].lower() == "ticks", "Apparently wrong file type"
        _fn_info = dict()
        _fn_info["represent"] = "TICK"
        _fn_info["instrument"] = _comps[0]
        _fn_info["bar_size"] = "0"
        _fn_info["price_type"] = "BOTH"
        _fn_info["date_from"] = Prs.parse(_comps[2])
        _fn_info["date_to"] = Prs.parse(_comps[3])

        self.file_name_info = _fn_info

        return _fn_info

if __name__ == "__main__":
    try:
        _config_folder = sys.argv[1]  # config folder
        _config_file = sys.argv[2]  # config file name
        try:
            _split =  bool(sys.argv[6])  # 1: split, 0 no split
        except IndexError:
            _split = False

        _chunksize = None
        if _split:
            try:
                _chunksize = int(sys.argv[7])
            except IndexError:
                _chunksize = 100000

    except Exception as e:
        raise Exception(str(e))

    print("Processing for", _config_file)

    _rk = Tick(_config_folder, _config_file,  "")
    _rk.run()


#!/usr/bin/env python
# coding=utf-8
"""
Author: Deean
Date: 2022-02-11 23:49:14
LastEditTime: 2022-02-11 23:55:39
Description:
FilePath: 2001.可互换矩形的组数.py
"""
#
# @lc app=leetcode.cn id=2001 lang=python3
#
# [2001] 可互换矩形的组数
#

# @lc code=start
from typing import List
from math import gcd
from collections import defaultdict

class Solution:
    def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
        counts = defaultdict(int)
        for item in rectangles:
            counts[(item[0]//gcd(*item), item[1]//gcd(*item))] += 1
        count = 0
        for item in filter(lambda el: el > 1, counts.values()):
            count += item * (item - 1)
        return count // 2
# @lc code=end


# coding: utf-8
import pytest

from contextlib import contextmanager

@contextmanager
def assert_raises(exception_class, msg=None):
    """Check that an exception is raised and its message contains `msg`."""
    with pytest.raises(exception_class) as exception:
        yield
    if msg is not None:
        message = "%s" % exception
        assert msg.lower() in message.lower()


import brownie
import pytest
from brownie.test import given, strategy
from conftest import MAX_UINT256

pytestmark = pytest.mark.usefixtures("mint_alice", "approve_alice")

def test_simple_add(alice, swap):
    swap.addLiquidity([1e3, 1e3],1e3 , MAX_UINT256, {"from": alice})

@pytest.mark.parametrize("min_amount", [0, 2 * 10 ** 18])
def test_initial(
    chain, alice, swap, coins, liquidity, min_amount, decimals, n_coins, initial_amounts
):
    amounts = [10 ** i for i in decimals]

    swap.addLiquidity(amounts, min_amount, chain.time() + 60, {"from": alice})

    for coin, amount, initial in zip(coins, amounts, initial_amounts):
        assert coin.balanceOf(alice) == initial - amount
        assert coin.balanceOf(swap) == amount

    assert liquidity.balanceOf(alice) == n_coins * 10 ** 18
    assert liquidity.totalSupply() == n_coins * 10 ** 18

@given(idx=strategy('uint', min_value=0, max_value=1))
def test_initial_liquidity_missing_coin(chain, alice, swap, liquidity, idx, decimals):
    amounts = [10 ** i for i in decimals]
    amounts[idx] = 0

    with brownie.reverts():
        swap.addLiquidity(amounts, 0, chain.time() + 60, {"from": alice})


import flask
import wtforms
from flask.ext import wtf

from main import app
import auth
import config
import model
import util
import task
import views.public
import json
from google.appengine.ext import ndb
from google.appengine.datastore.datastore_query import Cursor
from werkzeug import exceptions, routing

###############################################################################
# Sitemap
###############################################################################

@app.route('/sitemap.xml')
def sitemap():
  resp_model = {}
  resp_model['pages'] = []
  fetch_home(resp_model)
  fetch_stories(resp_model)
  fetch_about(resp_model)
  fetch_contact(resp_model)
  response = flask.make_response(flask.render_template(
      'share/sitemap/sitemap.xml',
      model = resp_model,
      lastmod=config.CURRENT_VERSION_DATE.strftime('%Y-%m-%d'),
  ))
  response.headers['Content-Type'] = 'application/xml'
  return response

def fetch_about(resp_model):
  about = model.ModuleConfig.get_by('module_id', 'about-page')
  sitemap_item = module_to_sitemap_item(about, 'about')
  sitemap_item['priority'] = '0.2'
  resp_model['pages'].append(sitemap_item)

def fetch_contact(resp_model):
  about = model.ModuleConfig.get_by('module_id', 'contact-page')
  sitemap_item = module_to_sitemap_item(about, 'contact')
  sitemap_item['priority'] = '0.2'
  resp_model['pages'].append(sitemap_item)

def fetch_home(resp_model):
  about = model.ModuleConfig.get_by('module_id', 'home-page')
  sitemap_item = module_to_sitemap_item(about, 'home')
  sitemap_item['priority'] = '1'
  resp_model['pages'].append(sitemap_item)

def fetch_stories(resp_model):
  story_dbs = model.Story.query().filter(
      model.Story.story_item_count > 0).fetch()
  for story_db in story_dbs :
    story = story_to_sitemap(story_db)
    resp_model['pages'].append(story)

def story_to_sitemap(story):
  sitemap = {}
  sitemap['url'] = flask.url_for('story', story_key=util.story_key(story), _external=True)
  sitemap['lastmod'] = story.modified.strftime('%Y-%m-%d')
  sitemap['priority'] = '0.7'
  return sitemap

def module_to_sitemap_item(module, route_name):
  sitemap = {}
  sitemap['url'] = flask.url_for(route_name, _external=True)
  sitemap['lastmod'] = module.modified.strftime('%Y-%m-%d')
  sitemap['priority'] = '0.5'
  return sitemap


# Ecrire un programme en Python pour trouver la somme des séries 1! / 1 + 2! / 2 + 3! /
# 3 + 4! / 4 + 5! / 5 en utilisant la fonction.

def facto(n):
    if n == 0 : # ici c'est la condition d'arrete
        return 1
    else:
        return n*facto(n-1) # ici en utilise la recurcivite pour calculer le factoriel

def sum(x):
    n = 0
    for i in range(1,x+1):
        n += facto(i)/i
    return n
x = int(input('entrer le nombre n : '))
print(sum(x))


from StandardWienerProcess import *
import matplotlib.pyplot as plt
from Rating import drift, diffusion, Model
#%%
# Setup for Wiener Process
Tn = 100
N = 10000
nW = 1
Ns = 5

T, W, dW = StandardWienerProcess(Tn,N,nW,Ns,seed=None)

p = [1,0.05]   # Parameter
x0 = [0.1]        # Initial State

#%%
from Rating import drift, diffusion, Model
from DormandPrince54 import Solve

p = [1,0.5,1,0]
x0 = 0.2
h0 = 0.1
abstol = 10**(-6)
reltol = 10**(-6)
tspan = [0, 10]

T,X = Solve(Model, tspan, x0, h0, abstol, reltol, args=(p,))

plt.plot(T,X)
#plt.axhline(0)
#plt.axhline(p)

#%%

# SDE solve using the EulerMaruyama ExplicitExplicit
from EulerMaruyamaExplicitExplicit import Solve
X = Solve(drift,diffusion,T,x0,W,args=(p,))

plt.figure(1)

for i in range(Ns):
# We unpack each state
    plt.plot(T,X[:,0,i])

#%%

x = np.linspace(0,10,1000)

plt.plot(x,diffusion(x, x, p))

#%%


# -*- coding: utf-8 -*-
"""
Created on Tue Aug 21 21:17:35 2018

@author: shurastogi
"""
def divide5byNum(Num):
    try:
        return float(5/Num)
    except ZeroDivisionError as e:
        print("Please provide number greater than 0, Error is ",e)


pos = input()
x = int(ord(pos[0])-ord('a'))+1
y = int(pos[1])

steps = [(-2, -1), (-1, -2), (1,-2), (2, -1), (2,1), (1,2), (-1, 2), (-2, 1)]
count = 0
for step in steps:
    stepX = step[0] + x
    stepY = step[1] + y
    if stepX > 1 and stepY > 1 and stepX <8 and stepY <8:
        count+=1
print(count)

구현/왕실의 나이트.py


import xml.etree.ElementTree as ET
import re

# Indents and newlines the given XML elements and subelements for reading and merging.
def formatXml(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            formatXml(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

# This parses the given xml file with all namespaces stripped out.
#
# This aims to reproduce the ns_clean option of XMLParser in the lxml 3rd party library.
# We don't use lxml because we want to minimize the install burden on users.
#
# Returns the root element.
def parseClean(file):
    root_tag = ''
    namespaces = []
    with open(file, 'r') as fin:
        # Find the root element tag and the namespaces
        for line in fin:
            if not line.startswith('<?xml'):
                fields = line.split(' ')
                root_tag = fields[0][1:]
                for field in fields[1:]:
                    if 'xmlns' in field:
                        attribs = field.split('=')[0].split(':')
                        if len(attribs) == 2:
                            namespaces.append(attribs[1])
                break
    with open(file, 'r') as fin:
        fin_str = fin.read()

    # Replace the root element with just the tag.
    search = re.search(r'<' + root_tag + '.+?>', fin_str)
    if (search):
        sub_out = search.group(0)
        if sub_out.endswith('/>'):
            sub_in = '<' + root_tag + '/>'
        else:
            sub_in = '<' + root_tag + '>'
        fin_str = re.sub(sub_out, sub_in, fin_str)

    for namespace in namespaces:
        # Find all occurrences of the namespace, ns, in <*ns:> without a quote between
        # < and ns, and remove duplicates.
        occurrences = list(set(re.findall(r'<[^"\'>]*' + namespace + ':', fin_str)))
        for occurrence in occurrences:
            # Clean all occurrences of the namespace
            sub_in = occurrence.replace(namespace + ':', '')
            fin_str = re.sub(occurrence, sub_in, fin_str)
    return ET.fromstring(fin_str)


import numpy as np
from buteo.machine_learning.ml_utils import (
    preprocess_optical,
    preprocess_sar,
    preprocess_coh,
)

base_folder = "path/to/patches/"
folder = base_folder + "raw/"
outdir = base_folder
prefix = ""

def preprocess(prefix, low=0, high=1, optical_top=6000):
    b02 = folder + prefix + "B02.npy"
    b03 = folder + prefix + "B03.npy"
    b04 = folder + prefix + "B04.npy"
    b08 = folder + prefix + "B08.npy"

    b05 = folder + prefix + "B05.npy"
    b06 = folder + prefix + "B06.npy"
    b07 = folder + prefix + "B07.npy"
    b11 = folder + prefix + "B11.npy"
    b12 = folder + prefix + "B12.npy"

    coh_asc = folder + prefix + "COH_asc.npy"
    coh_desc = folder + prefix + "COH_desc.npy"

    vv_asc = folder + prefix + "VV_asc.npy"
    vv_desc = folder + prefix + "VV_desc.npy"

    vv_asc_v2 = folder + prefix + "VV_asc_v2.npy"
    vh_asc_v2 = folder + prefix + "VH_asc_v2.npy"

    label_area = folder + prefix + "label_area.npy"
    label_volume = folder + prefix + "label_volume.npy"
    label_people = folder + prefix + "label_people.npy"

    rgb = preprocess_optical(
        np.stack(
            [
                np.load(b02),
                np.load(b03),
                np.load(b04),
            ],
            axis=3,
        )[:, :, :, :, 0],
        target_low=low,
        target_high=high,
        cutoff_high=optical_top,
    )

    shuffle_mask = np.random.permutation(rgb.shape[0])

    np.save(outdir + prefix + f"RGB.npy", rgb[shuffle_mask])

    # Free memory
    rgb = None

    rgbn = preprocess_optical(
        np.stack(
            [
                np.load(b02),
                np.load(b03),
                np.load(b04),
                np.load(b08),
            ],
            axis=3,
        )[:, :, :, :, 0],
        target_low=low,
        target_high=high,
        cutoff_high=optical_top,
    )

    np.save(outdir + prefix + f"RGBN.npy", rgbn[shuffle_mask])

    rgbn = None

    rededge = preprocess_optical(
        np.stack(
            [
                np.load(b05),
                np.load(b06),
                np.load(b07),
            ],
            axis=3,
        )[:, :, :, :, 0],
        target_low=low,
        target_high=high,
        cutoff_high=optical_top,
    )

    np.save(outdir + prefix + f"REDEDGE.npy", rededge[shuffle_mask])

    # Free memory
    rededge = None

    swir = preprocess_optical(
        np.stack(
            [
                np.load(b11),
                np.load(b12),
            ],
            axis=3,
        )[:, :, :, :, 0],
        target_low=low,
        target_high=high,
        cutoff_high=optical_top,
    )

    np.save(outdir + prefix + f"SWIR.npy", swir[shuffle_mask])

    # Free memory
    swir = None

    reswir = preprocess_optical(
        np.stack(
            [
                np.load(b05),
                np.load(b06),
                np.load(b07),
                np.load(b11),
                np.load(b12),
            ],
            axis=3,
        )[:, :, :, :, 0],
        target_low=low,
        target_high=high,
        cutoff_high=optical_top,
    )

    np.save(outdir + prefix + f"RESWIR.npy", reswir[shuffle_mask])

    # Free memory
    reswir = None

    np.save(
        outdir + prefix + "coh_asc",
        preprocess_coh(np.load(coh_asc), target_low=low, target_high=high)[
            shuffle_mask
        ],
    )
    np.save(
        outdir + prefix + "coh_desc",
        preprocess_coh(np.load(coh_desc), target_low=low, target_high=high)[
            shuffle_mask
        ],
    )

    np.save(
        outdir + prefix + "vv_asc",
        preprocess_sar(np.load(vv_asc), target_low=low, target_high=high)[shuffle_mask],
    )
    np.save(
        outdir + prefix + "vv_desc",
        preprocess_sar(np.load(vv_desc), target_low=low, target_high=high)[
            shuffle_mask
        ],
    )

    np.save(
        outdir + prefix + "vv_asc_v2",
        preprocess_sar(np.load(vv_asc_v2), target_low=low, target_high=high)[
            shuffle_mask
        ],
    )
    np.save(
        outdir + prefix + "vh_asc_v2",
        preprocess_sar(np.load(vh_asc_v2), target_low=low, target_high=high)[
            shuffle_mask
        ],
    )

    np.save(outdir + prefix + "label_area", np.load(label_area)[shuffle_mask])
    np.save(outdir + prefix + "label_volume", np.load(label_volume)[shuffle_mask])
    np.save(outdir + prefix + "label_people", np.load(label_people)[shuffle_mask])

preprocess("")
preprocess("aarhus_")
preprocess("holsterbro_")
preprocess("samsoe_")


# first step is to import the libraries
import requests
from bs4 import BeautifulSoup
import csv
from itertools import zip_longest

# second step is to get the url and save it in a variable
result = requests.get(
    "https://wuzzuf.net/search/jobs/?q=frontend&a=hpb")

# third step is to parse the html code and save it in a variable
src = result.content
# print(src)

# fourth step is to create a soup object
soup = BeautifulSoup(src, 'lxml')
# print(soup.prettify())

# fifth step is to get the elements that we want to scrap
# job title , job skills ,company name , job location , job description

# job title
job_titles = soup.find_all('h2', class_='css-m604qf')
# print(job_titles)

# company name
company_names = soup.find_all('a', class_='css-17s97q8')

# job location
job_locations = soup.find_all('span', class_='css-5wys0k')

# job description
# job_descriptions = soup.find_all('div', class_='css-y4udm8')

# job skills
job_skills = soup.find_all('div', class_="css-y4udm8")
# print(job_skills)

# sixth step is to get text from the elements
# job title
job_titles_text = []
for job_title in job_titles:
    job_titles_text.append(job_title.text)
print(job_titles_text)

# company name
company_names_text = []
for company_name in company_names:
    company_names_text.append(company_name.text)
print(company_names_text)

# job location
job_locations_text = []
for job_location in job_locations:
    job_locations_text.append(job_location.text)
print(job_locations_text)

# job description
# job_descriptions_text = []
# for job_description in job_descriptions:
#     job_descriptions_text.append(job_description.text)
# print(job_descriptions_text)

# job skills
job_skills_text = []
for job_skill in job_skills:
    job_skills_text.append(job_skill.text)
print(job_skills_text)

# seventh step is to save the data in a csv file
d = [job_titles_text, company_names_text,
     job_locations_text, job_skills_text]
export_data = zip_longest(*d, fillvalue='')
with open('G:\web scraping/job.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:
    wr = csv.writer(myfile)
    wr.writerow(("job title", "company name",
                 "job location", "job skills"))
    wr.writerows(export_data)
# myfile.close()
# illustarte the above code
# d is array of arrays that contains the data
# export_data is a variable that contains the data in a zip format
# with open is a function that creates a csv file and save the data in it
# w is a parameter that means write
# encoding is a parameter that means the encoding of the file
# newline is a parameter that means the new line
# wr is a variable that contains the csv writer
# wr.writerow is a function that writes the first row in the csv file
# wr.writerows is a function that writes the rest of the rows in the csv file
# myfile.close() is a function that closes the csv file

# eighth step is to save the data in a json file


import cv2
import numpy as np
from PyQt5.QtCore import pyqtSignal, Qt, QThread

class VideoThread(QThread):
  """Provides OpenCV video feed from IP camera based on CameraController object provided"""
  change_pixmap_signal = pyqtSignal(np.ndarray)

  def __init__(self, camera):
    super().__init__()
    self._run_flag = True
    self.camera = camera

  def run(self):
    # capture from web cam
    if not self.camera.isConnected():
      no_cam_img = np.zeros((600,600,3), np.uint8)
      cv2.putText(no_cam_img, 'No Camera Available', (150,300), cv2.FONT_HERSHEY_DUPLEX, 1, (255,255,255))
      self.change_pixmap_signal.emit(no_cam_img)
    else:
      cap = cv2.VideoCapture(self.camera.getVideo())
      while self._run_flag:
        ret, cv_img = cap.read()
        if ret:
          self.change_pixmap_signal.emit(cv_img)
      cap.release()

  def stop(self):
    """Sets run flag to False and waits for thread to finish"""
    self._run_flag = False
    self.wait()


# https://discord.com/developers/docs/interactions/slash-commands#interaction-interactiontype
from gecore.ps_discord_slash.models.flags import DiscordFlags

class InteractionType:
    PING = 1
    APPLICATION_COMMAND = 2

# https://discord.com/developers/docs/interactions/slash-commands#interaction-interactionresponsetype
class InteractionResponseType:
    PONG = 1
    ACKNOWLEDGE = 2
    CHANNEL_MESSAGE = 3
    CHANNEL_MESSAGE_WITH_SOURCE = 4
    ACKNOWLEDGE_WITH_SOURCE = 5

# https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondataoption
class ApplicationCommandInteractionDataOption:
    def __init__(self, name: str, **kwargs):
        self.name = name
        self.value = kwargs.get('value')
        self.options = kwargs.get('options')

# https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondata
class ApplicationCommandInteractionData:
    def __init__(self, a_id, name: str, **kwargs):
        self.id = a_id,
        self.name = name
        self.options = kwargs.get('options', [ApplicationCommandInteractionDataOption])

class Interaction:
    def __init__(self, i_id, i_type: InteractionType, guild_id, channel_id, member, token: str, version: int):
        self.id = i_id
        self.type = i_type
        self.guild_id = guild_id
        self.channel_id = channel_id
        self.member = member
        self.token = token
        self.version = version

# https://discord.com/developers/docs/interactions/slash-commands#interaction-interactionapplicationcommandcallbackdata
class InteractionResponseData:
    def __init__(self, content, embeds, flags: DiscordFlags):
        self.tts = False
        self.content = content
        self.flags = flags
        self.embeds = embeds
        self.allowed_mentions = []

class InteractionResponse:
    def __init__(self, response_type: InteractionResponseType, response_data: InteractionResponseData):
        self.type = response_type
        self.data = response_data

class Embed:
    def __init__(self, title, fields):
        self.title = title
        self.type = 'rich'
        self.fields = fields

class EmbedField:
    def __init__(self, name, value):
        self.name = name
        self.value = value
        self.inline = False


import os
import tarfile
import tempfile
from datetime import datetime

import click
from tabulate import tabulate

from platformio import fs
from platformio.account.client import AccountClient
from platformio.compat import isascii
from platformio.exception import UserSideException
from platformio.package.manifest.parser import ManifestParserFactory
from platformio.package.manifest.schema import ManifestSchema
from platformio.package.meta import PackageType
from platformio.package.pack import PackagePacker
from platformio.package.unpack import FileUnpacker, TARArchiver
from platformio.registry.client import RegistryClient

def validate_datetime(ctx, param, value):  # pylint: disable=unused-argument
    if not value:
        return value
    try:
        datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
    except ValueError as exc:
        raise click.BadParameter(exc)
    return value

@click.command("publish", short_help="Publish a package to the registry")
@click.argument(
    "package",
    default=os.getcwd,
    metavar="<source directory, tar.gz or zip>",
    type=click.Path(exists=True, file_okay=True, dir_okay=True),
)
@click.option(
    "--owner",
    help="PIO Account username (can be organization username). "
    "Default is set to a username of the authorized PIO Account",
)
@click.option(
    "--type",
    "typex",
    type=click.Choice(list(PackageType.items().values())),
    help="Custom package type",
)
@click.option(
    "--released-at",
    callback=validate_datetime,
    help="Custom release date and time in the next format (UTC): 2014-06-13 17:08:52",
)
@click.option("--private", is_flag=True, help="Restricted access (not a public)")
@click.option(
    "--notify/--no-notify",
    default=True,
    help="Notify by email when package is processed",
)
@click.option(
    "--no-interactive",
    is_flag=True,
    help="Do not show interactive prompt",
)
@click.option(
    "--non-interactive",
    is_flag=True,
    help="Do not show interactive prompt",
    hidden=True,
)
def package_publish_cmd(  # pylint: disable=too-many-arguments, too-many-locals
    package, owner, typex, released_at, private, notify, no_interactive, non_interactive
):
    click.secho("Preparing a package...", fg="cyan")
    no_interactive = no_interactive or non_interactive
    owner = owner or AccountClient().get_logged_username()
    do_not_pack = (
        not os.path.isdir(package)
        and isinstance(FileUnpacker.new_archiver(package), TARArchiver)
        and PackageType.from_archive(package)
    )
    archive_path = None
    with tempfile.TemporaryDirectory() as tmp_dir:  # pylint: disable=no-member
        # publish .tar.gz instantly without repacking
        if do_not_pack:
            archive_path = package
        else:
            with fs.cd(tmp_dir):
                p = PackagePacker(package)
                archive_path = p.pack()

        typex = typex or PackageType.from_archive(archive_path)
        manifest = ManifestSchema().load_manifest(
            ManifestParserFactory.new_from_archive(archive_path).as_dict()
        )
        name = manifest.get("name")
        version = manifest.get("version")
        data = [
            ("Type:", typex),
            ("Owner:", owner),
            ("Name:", name),
            ("Version:", version),
            ("Size:", fs.humanize_file_size(os.path.getsize(archive_path))),
        ]
        if manifest.get("system"):
            data.insert(len(data) - 1, ("System:", ", ".join(manifest.get("system"))))
        click.echo(tabulate(data, tablefmt="plain"))

        # check files containing non-ascii chars
        check_archive_file_names(archive_path)

        # look for duplicates
        check_package_duplicates(owner, typex, name, version, manifest.get("system"))

        if not no_interactive:
            click.confirm(
                "Are you sure you want to publish the %s %s to the registry?\n"
                % (
                    typex,
                    click.style(
                        "%s/%s@%s" % (owner, name, version),
                        fg="cyan",
                    ),
                ),
                abort=True,
            )

        click.secho(
            "The package publishing may take some time depending "
            "on your Internet connection and the package size.",
            fg="yellow",
        )
        click.echo("Publishing...")
        response = RegistryClient().publish_package(
            owner, typex, archive_path, released_at, private, notify
        )
        if not do_not_pack:
            os.remove(archive_path)
        click.secho(response.get("message"), fg="green")

def check_archive_file_names(archive_path):
    with tarfile.open(archive_path, mode="r:gz") as tf:
        for name in tf.getnames():
            if not isascii(name) or not name.isprintable():
                click.secho(
                    f"Warning! The `{name}` file contains non-ASCII chars and can "
                    "lead to the unpacking issues on a user machine",
                    fg="yellow",
                )

def check_package_duplicates(
    owner, type, name, version, system
):  # pylint: disable=redefined-builtin
    found = False
    items = (
        RegistryClient()
        .list_packages(qualifiers=dict(types=[type], names=[name]))
        .get("items")
    )
    if not items:
        return True
    # duplicated version by owner / system
    found = False
    for item in items:
        if item["owner"]["username"] != owner or item["version"]["name"] != version:
            continue
        if not system:
            found = True
            break
        published_systems = []
        for f in item["version"]["files"]:
            published_systems.extend(f.get("system", []))
        found = set(system).issubset(set(published_systems))
    if found:
        raise UserSideException(
            "The package `%s/%s@%s` is already published in the registry"
            % (owner, name, version)
        )
    other_owners = [
        item["owner"]["username"]
        for item in items
        if item["owner"]["username"] != owner
    ]
    if other_owners:
        click.secho(
            "\nWarning! A package with the name `%s` is already published by the next "
            "owners: %s\n" % (name, ", ".join(other_owners)),
            fg="yellow",
        )
    return True


from sqlalchemy import false
from app.db.database import SessionLocal
from app.models.schemas.rate_comment import CommentCreate
from app.db.tables import Comments

db = SessionLocal()

def create_comment(comment_in: CommentCreate):
    comment_new = Comments(**comment_in.dict())
    db.add(comment_new)
    try:
        db.flush()
        db.commit()
        return comment_new
    except:
        db.rollback()
        return None


import asyncio
import random
import openai
import json
import aiogram
from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram import Bot, Dispatcher, executor
from aiogram.types import InlineQuery, \
    InputTextMessageContent, InlineQueryResultArticle
from aiogram.dispatcher.filters.builtin import CommandStart
from datetime import datetime
from aiogram.types import CallbackQuery
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InlineQuery, ReplyKeyboardMarkup, KeyboardButton
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.types import InputFile
import g4f

def ChimeraIsDead(message_log: list, model: str):
    while True:
        try:
            # if model == 'gpt-4':
            response = g4f.ChatCompletion.create(
                model='gpt-4',
                messages=message_log,
                provider=g4f.Provider.ChatgptAi
            )
            # else:
            #     response = g4f.ChatCompletion.create(
            #         model='gpt-3.5-turbo',
            #         messages=message_log,
            #         provider=g4f.Provider.Aichat
            #     )
            if len(str(response)) <= 1:
                pass
            else:
                break
        except KeyError:
            pass
        except:
            pass

    return response

# States
class States(StatesGroup):
    change_custom_lore = State()
class AdminStates(StatesGroup):
    send_message = State()

# Reload JSON data
def reload_data():
    global config, lore_dict, model_dict, buttons
    with open('config.json', 'r', encoding='UTF-8') as f:
        config = json.load(f)

    with open('./db/lore-list.json', 'r', encoding='UTF-8') as f:
        lore_dict = json.load(f)

    with open('./db/model-list.json', 'r', encoding='UTF-8') as f:
        model_dict = json.load(f)

    buttons = {"language": [], "clear": [], "lore": [], "model": []}

    with open('./db/locale.json', 'r', encoding='UTF-8') as f:
        locs = json.load(f)
        for key in locs.keys():
            if str(key).startswith('button-language'):
                buttons['language'].append(str(locs[key]))
            elif str(key).startswith('button-clear'):
                buttons['clear'].append(str(locs[key]))
            elif str(key).startswith('button-lore'):
                buttons['lore'].append(str(locs[key]))
            elif str(key).startswith('button-model'):
                buttons['model'].append(str(locs[key]))

reload_data()

# Set menu command
startcom = types.bot_command.BotCommand(command='start', description='Restart bot.')

openai.api_key = config['chimera-api']
openai.api_base = "https://api.shuttle.rip/v1"
character_limit = config['character_limit']

Languages = config['languages']

bot = Bot(config['bot-token'])
dp = Dispatcher(bot, storage=MemoryStorage())

# Localize string
def getlocalized(line: str, lang: str):
    with open('./db/locale.json', 'r', encoding='UTF-8') as f:
        data = json.load(f)
    try: return data[str(line + '-' + lang[0:2].upper())]
    except:
        try: return data[str(line + '-' + 'EN')]
        except: return 'Failed to translate.'

def timestamp():
    current_time = datetime.now()
    return current_time.timestamp()

# Get total & new users this week or today
def getUserStats():
    with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
        data = json.load(f)

    stats = {'ALL': len(data), 'TODAY': 0, "WEEK": 0}

    for user in data:
        try:
            time = data[str(user)]['timestamp']
            now = timestamp()

            if now - 86400 <= time:
                stats['TODAY'] += 1
                if now - 604800 <= time:
                    stats['WEEK'] += 1

        except: pass

    return stats

# Check if user in database & return his settings
def getUser(user_id: str, username = 'None'):
    with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
        data = json.load(f)

        try:
            user = data[str(user_id)]
        except:
            data[str(user_id)] = {}

        if 'lang' not in data[str(user_id)]:
            data[str(user_id)]['lang'] = config['default-language']
            with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
                json.dump(data, f)

        language = data[str(user_id)]['lang']

        if 'lore' not in data[str(user_id)]:
            data[str(user_id)]['lore'] = config['default-lore']
            with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
                json.dump(data, f)

        lore = data[str(user_id)]['lore']

        if 'model' not in data[str(user_id)]:
            data[str(user_id)]['model'] = config['default-model']
            with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
                json.dump(data, f)

        model = data[str(user_id)]['model']

        if 'custom-lore' not in data[str(user_id)]:
            data[str(user_id)]['custom-lore'] = f'My username is @{username}'
            with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
                json.dump(data, f)

        custom_lore = data[str(user_id)]['custom-lore']

        if 'timestamp' not in data[str(user_id)]:
            data[str(user_id)]['timestamp'] = timestamp()
            with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
                json.dump(data, f)

    return (language, lore, model, custom_lore)

@dp.message_handler(commands=['start'])
async def start(message: types.Message, state: FSMContext):
    await bot.set_my_commands(commands=[startcom])
    with open('./db/message-log.json', 'r', encoding='UTF-8') as log:
        data = json.load(log)
        data[str(message.from_user.id)] = []
    with open('./db/message-log.json', 'w', encoding='UTF-8') as log:
        json.dump(data, log)
    sticker = random.choice(config['start-stickers'])

    language, lore, model, custom_lore = getUser(message.from_user.id, message.from_user.username)

    if '?' in lore:
        lore = '📑 ' + lore.split('?')[0] + ' > ' +  lore.split('?')[1]

    menu = ReplyKeyboardMarkup(row_width=3, resize_keyboard=True)
    menu.row(KeyboardButton(text=str(getlocalized('button-clear', language))))
    menu.row(
        KeyboardButton(text=str(getlocalized("button-language", language))),
        KeyboardButton(text=str(getlocalized("button-lore", language))),
        KeyboardButton(text=str(getlocalized("button-model", language)))
    )
    await state.update_data(proccess=False)
    await bot.send_sticker(message.from_user.id, sticker)
    await bot.send_message(message.from_user.id, str(getlocalized('start-menu', language)).format(language=language, model=model, lore=lore), reply_markup=menu)

# Reload JSON dict without restaring bot
@dp.message_handler(commands=['reload'])
async def reloadjson(message: types.Message, state: FSMContext):
    reload_data()
    await message.answer('✅')

# Bot stats
@dp.message_handler(commands=['stats'])
async def botstats(message: types.Message, state: FSMContext):
    stats = getUserStats()
    await message.answer(f'Total users: {stats["ALL"]}\nNew users\n* Week: {stats["WEEK"]}\n* Today: {stats["TODAY"]}')

# Send message to all bot users
@dp.message_handler(commands=['send'])
async def sendcommand(message: types.Message, state: FSMContext):
    if message.from_user.id == config['admin-id']:
        await AdminStates.send_message.set()
        await message.reply('📝 Введите сообщение для отправки.')

@dp.message_handler(state=AdminStates.send_message)
async def send(message: types.Message, state: FSMContext):
    if message.from_user.id == config['admin-id']:
        await state.update_data(send=message.text)
        menu = InlineKeyboardMarkup(row_width=1).add(InlineKeyboardButton(text='✅', callback_data='send'), InlineKeyboardButton(text='⛔', callback_data='cancel'))
        await message.reply(f'Вот так будет выглядеть рассылка, начать отправку?\n\n{message.text}', reply_markup=menu)

@dp.callback_query_handler(state=AdminStates.send_message)
async def callback(call: CallbackQuery, state: FSMContext):
    if call.data == 'send':
        msg = await state.get_data()
        msg = msg['send']
        await state.finish()
        with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)
            index = 0
            stats = await bot.send_message(call.from_user.id, f'0 / {len(data)}')
            for user in data.keys():
                try:
                    await bot.send_message(chat_id=int(user), text=msg)
                    index+=1
                    await bot.edit_message_text(message_id=stats['message_id'], chat_id=call.from_user.id, text=f'{index} / {len(data)}')
                    await asyncio.sleep(0.13)
                except: pass
            await bot.edit_message_text(message_id=stats['message_id'], chat_id=call.from_user.id,
                                        text=f'🏁 {index} / {len(data)}')
    if call.data == 'cancel':
        await state.finish()

# Chat message handler
@dp.message_handler()
async def chat(message: types.Message, state: FSMContext):
    with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
        data = json.load(f)

    language, lore, model, custom_lore = getUser(message.from_user.id, message.from_user.username)

    if message.text in buttons['clear'] and list(message.text).count(' ') <= 2:
        if '?' in lore:
            lore = '📑 ' + lore.split('?')[0] + ' > ' + lore.split('?')[1]
        await message.reply(str(getlocalized('start-menu', language)).format(language=language, model=model, lore=str(data[str(message.from_user.id)]['lore']).replace('?', ' > ')))
        with open('./db/message-log.json', 'r', encoding='UTF-8') as log:
            data = json.load(log)
            data[str(message.from_user.id)] = []
        with open('./db/message-log.json', 'w', encoding='UTF-8') as log:
            json.dump(data, log)
    elif message.text in buttons['language'] and list(message.text).count(' ') <= 2:
        menu = InlineKeyboardMarkup(row_width=1)
        global Languages
        for lang in Languages:
            menu.add(InlineKeyboardButton(text=lang + (' ✅' if language == lang else ''), callback_data=f'lang-to>{lang}'))
        await message.reply(getlocalized('current', language).format(language), reply_markup=menu)
    elif message.text in buttons['lore'] and list(message.text).count(' ') <= 2:
        menu = InlineKeyboardMarkup(row_width=1)
        for lore in lore_dict.keys():
            if type(lore_dict[lore]) is dict:
                menu.add(InlineKeyboardButton(
                    text=lore, callback_data=f'lore-page>{lore}'))
            else:
                menu.add(InlineKeyboardButton(text=lore + (' ✅' if lore == str(data[str(message.from_user.id)]['lore']) else ''), callback_data=f'lore-to>{lore}'))
        menu.add(InlineKeyboardButton(text='Custom' + (' ✅' if 'custom' == str(data[str(message.from_user.id)]['lore']) else ''), callback_data=f'lore-to>custom'))
        with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)
            await message.reply(getlocalized('current', language).format(str(data[str(message.from_user.id)]['lore']).replace('?', ' > ')), reply_markup=menu)
    elif message.text in buttons['model'] and list(message.text).count(' ') <= 2:
        menu = InlineKeyboardMarkup(row_width=2)
        for model in model_dict['text-models']:
            menu.add(InlineKeyboardButton(text=model + (' ✅' if model == str(data[str(message.from_user.id)]['model']) else ''), callback_data=f'model-to>{model}'))
        await message.reply(getlocalized('current', language).format(str(data[str(message.from_user.id)]['model'])), reply_markup=menu)
    else:
        await message.answer_chat_action("typing")
        await state.update_data(proccess=True)
        message_log = [
            {"role": "system", "content": lore.format(language)},
        ]

        with open('./db/message-log.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)
            try:
                messages = data[str(message.from_user.id)]
            except: messages = []

        for msg in messages:
            message_log.append(msg)
        message_log.append({"role": "user", "content": message.text})

        total_characters = sum(len(message['content'])
                               for message in message_log)
        while total_characters > character_limit and len(message_log) > 1:
            print(
                f"total_characters {total_characters} exceed limit of {character_limit}, removing oldest message")
            total_characters -= len(message_log[1]["content"])
            message_log.pop(1)

        response = None
        response = ChimeraIsDead(message_log, model)
            # return await message.answer(getlocalized('try-later', language))

        # for answer in response['choices']:
        message_log.append({"role": "assistant", "content": response})
        with open('./db/message-log.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)
            message_log.pop(0)
            data[str(message.from_user.id)] = message_log
        with open('./db/message-log.json', 'w', encoding='UTF-8') as f:
            json.dump(data, f)
        await message.answer(response)

        await state.update_data(proccess=False)

@dp.callback_query_handler()
async def callback(call: CallbackQuery, state: FSMContext):
    await call.answer('⌛...')
    if call.data.startswith('lang-to'):
        lang = call.data.split('>')[1]
        with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)
        with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
            try:
                data[str(call.from_user.id)]['lang'] = lang
            except:
                data[str(call.from_user.id)] = {}; data[str(call.from_user.id)]['lang'] = lang
            json.dump(data, f)
        sticker = random.choice(config['start-stickers'])

        language, lore, model, custom_lore = getUser(call.from_user.id, call.from_user.username)

        menu = ReplyKeyboardMarkup(row_width=3, resize_keyboard=True)
        menu.row(KeyboardButton(text=str(getlocalized('button-clear', language))))
        menu.row(
            KeyboardButton(text=str(getlocalized("button-language", language))),
            KeyboardButton(text=str(getlocalized("button-lore", language))),
            KeyboardButton(text=str(getlocalized("button-model", language)))
        )
        await bot.send_sticker(call.from_user.id, sticker)
        await bot.send_message(call.from_user.id,
                               str(getlocalized('start-menu', language)).format(language=language, model=model,
                                                                                lore=lore), reply_markup=menu)

    elif call.data.startswith('lore-to'):
        with open('./db/message-log.json', 'r', encoding='UTF-8') as log:
            data = json.load(log)
            data[str(call.from_user.id)] = []
        with open('./db/message-log.json', 'w', encoding='UTF-8') as log:
            json.dump(data, log)

        lore = call.data.split('>')[1]

        with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)
        with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
            try:
                data[str(call.from_user.id)]['lore'] = lore
            except:
                data[str(call.from_user.id)] = {};
                data[str(call.from_user.id)]['lore'] = lore
            json.dump(data, f)

        if '?' in lore:
            menu = InlineKeyboardMarkup(row_width=1).add(
                InlineKeyboardButton(text='ℹ', callback_data=f'about-lore>{lore}'))
            await bot.send_message(call.from_user.id, lore.split('?')[1], reply_markup=menu)
        elif lore != 'custom':
            menu = InlineKeyboardMarkup(row_width=1).add(
                InlineKeyboardButton(text='ℹ', callback_data=f'about-lore>{lore}'))
            await bot.send_message(call.from_user.id, lore, reply_markup=menu)
        else:
            menu = InlineKeyboardMarkup(row_width=1).add(
                InlineKeyboardButton(text='✏', callback_data=f'change-custom-lore'),
                InlineKeyboardButton(text='ℹ', callback_data=f'about-lore>custom'),
            )
            await bot.send_message(call.from_user.id, lore, reply_markup=menu)

    elif call.data.startswith('lore-page>'):
        with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)

        lore = call.data.split('>')[1]

        menu = InlineKeyboardMarkup(row_width=2)
        for lre in lore_dict[lore].keys():
            try:
                menu.add(InlineKeyboardButton(text=lre + (' ✅' if lre == str(data[str(call.from_user.id)]['lore']).split('?')[1] else ''), callback_data=f'lore-to>{lore}?{lre}'))
            except: menu.add(InlineKeyboardButton(text=lre, callback_data=f'lore-to>{lore}?{lre}'))
        await bot.send_message(call.from_user.id, lore, reply_markup=menu)

    elif call.data.startswith('change-custom-lore'):
        await States.change_custom_lore.set()
        await bot.send_message(call.from_user.id, '✍ Write new custom lore:')

    elif call.data.startswith('about-lore'):
        lore = call.data.split('>')[1]
        if lore == 'custom':
            with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
                data = json.load(f)
                loredata = data[str(call.from_user.id)]['custom-lore']
            return await bot.send_message(call.from_user.id, f'ℹ {lore}:\n{loredata}')
        if '?' in lore:
            loredata = lore_dict[f'{lore.split("?")[0]}'][f'{lore.split("?")[1]}']
            return await bot.send_message(call.from_user.id, f'ℹ {lore}:\n{loredata}')
        await bot.send_message(call.from_user.id, f'ℹ {lore}:\n{lore_dict[lore]}')

    elif call.data.startswith('model-to'):
        with open('./db/message-log.json', 'r', encoding='UTF-8') as log:
            data = json.load(log)
            data[str(call.from_user.id)] = []
        with open('./db/message-log.json', 'w', encoding='UTF-8') as log:
            json.dump(data, log)

        model = call.data.split('>')[1]
        with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
            data = json.load(f)
        with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
            try:
                data[str(call.from_user.id)]['model'] = model
            except:
                data[str(call.from_user.id)] = {};
                data[str(call.from_user.id)]['model'] = model
            json.dump(data, f)
        await bot.send_message(call.from_user.id, model)

@dp.message_handler(state=States.change_custom_lore)
async def changecustomlore(message: types.message, state: FSMContext):
    await state.finish()
    with open('./db/user-settings.json', 'r', encoding='UTF-8') as f:
        data = json.load(f)
    with open('./db/user-settings.json', 'w', encoding='UTF-8') as f:
        try:
            data[str(message.from_user.id)]['custom-lore'] = message.text
        except:
            data[str(message.from_user.id)] = {};
            data[str(message.from_user.id)]['custom-lore'] = message.text
        json.dump(data, f)
    await message.reply('✅')

# Start bot
executor.start_polling(dp, skip_updates=True)