import pandas as pd
from datetime import datetime

def load_data(filename, cancel=True):
    if cancel:
        dates_to_parse = ["booking_datetime", "checkin_date",
                          "checkout_date", "hotel_live_date", "cancellation_datetime"]
    else:
        dates_to_parse = ["booking_datetime", "checkin_date",
                          "checkout_date", "hotel_live_date"]
    df = pd.read_csv(filename, parse_dates=dates_to_parse)
    return df

def calculate_percentage_and_count(df, feature):
    # Group by 'days_cancellation_1' column and apply lambda function
    # to calculate the percentage of 'True' values in 'label' column for each group,
    # as well as count the total number of labels in each group
    result = df.groupby(feature)['label'].agg([('y', lambda x: (x == True).mean()), ('count', 'count')]) \
        .reset_index()

    # Rename the columns
    result.columns = ['x', 'y', 'count']

    return result

def cancellation_cost(cancellation_policy,
                      cancellation_date,
                      checkin_date,
                      order_cost,
                      num_nights):
    cancellation_date = datetime.strptime(cancellation_date, "%Y-%m-%d")
    checkin_date = datetime.strptime(checkin_date, "%Y-%m-%d")
    days_until_checkin = (checkin_date - cancellation_date).days
    cost_per_night = order_cost / num_nights

    policies = cancellation_policy.split("_")
    no_show_policy = None

    to_return = 0

    for policy in policies:
        if "P" not in policy and "N" not in policy:
            raise ValueError("Invalid cancellation policy")
        if "D" not in policy:
            policy_type = policy[-1]  # Get the last character of policy, which is 'N' or 'P'
            charge = int(policy.split(policy_type)[0].split(' ')[-1])  # Get the charge value before 'N' or 'P'
            no_show_policy = (policy_type, charge)
            continue

        days, charge, policy_type = policy.split('D')[0], policy.split('D')[1][:-1], policy.split('D')[1][-1]
        days = int(days)
        charge = int(charge)

        if days_until_checkin <= days:
            if policy_type == 'N':
                to_return = charge * cost_per_night
            else:  # policy_type == 'P'
                to_return = (charge / 100) * order_cost

    if days_until_checkin <= 0 and no_show_policy is not None:
        policy_type, charge = no_show_policy
        if policy_type == 'N':
            return charge * cost_per_night
        else:  # policy_type == 'P'
            return (charge / 100) * order_cost

    return to_return

def parse_policy(policy_code, num_nights):
    policies = policy_code.split("_")
    parsed_policy = [-1, -1, -1, -1, -1]  # Initialize the list with -1

    if policy_code == "UNKNOWN": return parsed_policy

    for i, policy in enumerate(policies):
        if policy == '':
            continue
        if "D" not in policy:
            policy_type = policy[-1]  # Get the last character of policy, which is 'N' or 'P'
            charge = int(policy.split(policy_type)[0].split(' ')[-1])  # Get the charge value before 'N' or 'P'
            if policy_type == 'P':
                parsed_policy[4] = charge
            else:  # policy_type == 'N'
                parsed_policy[4] = (charge / num_nights) * 100
        else:
            days, charge, policy_type = policy.split('D')[0], policy.split('D')[1][:-1], policy.split('D')[1][-1]
            if policy_type == 'P':
                parsed_policy[i * 2] = int(days)
                parsed_policy[i * 2 + 1] = int(charge)
            else:  # policy_type == 'N'
                parsed_policy[i * 2] = int(days)
                parsed_policy[i * 2 + 1] = (int(charge) / num_nights) * 100

    return parsed_policy


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 14 22:45:08 2016
@author: kewenjing

main_function of Question #3
change the moving lid: we have the moving lid both the upper surface and bottom: Ut,Ub
"""
from pylab import *
from project_function import *

# change the boundary condition
def set_BC_Q3(u,Ut,Ub):
    for i in range (N+1):
        u[0,i]=Ub
        u[N,i]=Ut
    return u

Ut=U0
Ub=-U0

u=zeros((N+1,N+1))
v=zeros((N+1,N+1))
P=zeros((N+1,N+1))
T=zeros((N+1,N+1))

T_initial(T)

v=set_BC(v)
u=set_BC(u)
u=set_BC_Q3(u,Ut,Ub)

E=0 #energy

for step in range (300):    # time step
    convection(u,v,1)
    diffusion(u,v,1)
    for i in range (5):     # time step of P, do the iteration to get stable P
        P=divergence_free(P,u,v)
    rescale_P(P)
    update(u,v,P)
    v=set_BC(v)
    set_BC(u)
    set_BC_Q3(u,Ut,Ub)
    tFin=step*dt
    T,ave,var=scalar_eq(T,u,v,1)
    E=energy(E,u,Ut)
    while (var<0.1*315**2):
        break

x=linspace(0,L,N+1)
y=linspace(0,L,N+1)
quiver(x,y,u,v)
#
#imshow(u,origin='lower')
#colorbar()
#show()
#
#imshow(v,origin='lower')
#colorbar()
#show()
#
#imshow(P,origin='lower')
#colorbar()
#show()

imshow(T,origin='lower')
colorbar()
show()

print(E)
print(tFin)
print(var)


import logging
from collections.abc import Iterable
from rich import inspect
from basics_14_code import func
# from basics_14_code_file import func

def replace_handlers(logger, new_handlers):
    if not isinstance(new_handlers, Iterable):
        new_handlers = [new_handlers]
    for h in logger.handlers:
        logger.removeHandler(h)
    for new_h in new_handlers:
        logger.addHandler(new_h)

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

stderr = logging.StreamHandler()
stderr.setLevel(logging.DEBUG)
stderr.setFormatter(
    logging.Formatter("{asctime} {name:20} {levelname:10} {message}", style="{")
)
log.addHandler(stderr)
# inspect(log)

l14 = logging.getLogger("basics_14_code")
replace_handlers(l14, stderr)

func()
log.debug("Сообщение уровня debug")
log.info("Сообщение уровня info")
log.warning("Сообщение уровня warning")


import pandas as pd

def dataprofile(data_all_df,data_cols,database_name,table_name):

    data_df = data_all_df
    columns2Bprofiled = data_df.columns
    database_name = database_name
    table_name = table_name

    dprof_df = pd.DataFrame({'database_name':[database_name] * len(data_df.columns),\
                             'table_name':[table_name] * len(data_df.columns),\
                             'column_names':data_df.columns,\
                             'data_types':[x for x in data_df.dtypes]})
    dprof_df = dprof_df[['database_name','table_name','column_names', 'data_types']]
    dprof_df.set_index('column_names', inplace=True, drop=False)

    # ======== Step 1 ==============
    num_rows = data_df.count()
    dprof_df['num_rows'] = num_rows

    # ======== Step 2 ==============
    # number of rows with nulls and nans
    df_nacounts = data_df.isnull().sum().transpose()
    df_nacounts = df_nacounts.reset_index()
    df_nacounts.columns = ['column_names','num_null']
    dprof_df = pd.merge(dprof_df, df_nacounts, on = ['column_names'], how = 'left')

    # ============ Step 3 ============
    # number of rows with white spaces (one or more space) or blanks
    dprof_df['num_spaces'] = dprof_df['num_null']
    dprof_df['num_blank'] = dprof_df['num_null']

    # ========== Step 4 ===============
    # using the in built describe() function
    desc_df = data_df.describe().transpose()
    desc_df=desc_df[['count', 'mean', 'std', 'min', 'max']]
    desc_df = desc_df.iloc[:, :]
    desc_df = desc_df.reset_index()
    desc_df.columns.values[0] = 'column_names'
    desc_df = desc_df[['column_names','count', 'mean', 'std']]
    dprof_df = pd.merge(dprof_df, desc_df , on = ['column_names'], how = 'left')

    # =============== Step 5 ============================

    allminvalues = data_df[columns2Bprofiled].min(axis=0,skipna=False)
    allmaxvalues = data_df[columns2Bprofiled].max(axis=0, skipna=False)

    x = [(x,data_df.where(data_df[x] == y, axis=0)[x].count()) for x,y in zip(columns2Bprofiled, allminvalues)]
    allmincounts = pd.DataFrame(x, columns=['column_names','counts_min'])

    y = [(x, data_df.where(data_df[x] == y, axis=0)[x].count()) for x, y in zip(columns2Bprofiled, allmaxvalues)]
    allmaxcounts = pd.DataFrame(y, columns=['column_names', 'counts_max'])

    df_counts = dprof_df[['column_names']]
    df_counts.loc[:,'min'] = allminvalues.values
    df_counts.loc[:, 'max'] = allmaxvalues.values

    df_counts = pd.merge(df_counts, allmincounts,on='column_names')
    df_counts = pd.merge(df_counts, allmaxcounts, on='column_names')

    df_counts = df_counts[['column_names','min','counts_min','max','counts_max']]
    dprof_df = pd.merge(dprof_df, df_counts , on = ['column_names'], how = 'left')

    # ============= Step 6 =============================
    # number of distinct values in each column
    dprof_df['num_distinct'] = [len(data_df[x].unique().tolist()) for x in columns2Bprofiled]

    # ================ Step 7 ============================
    # most frequently occuring value in a column and its count
    dprof_df['most_freq_valwcount'] = [data_df[x].value_counts().sort_values(ascending=False).head(1).reset_index().values.tolist()[0] for x in columns2Bprofiled]
    dprof_df['most_freq_value'] = [x[0] for x in dprof_df['most_freq_valwcount']]
    dprof_df['most_freq_value_count'] = [x[1] for x in dprof_df['most_freq_valwcount']]
    dprof_df = dprof_df.drop(['most_freq_valwcount'],axis=1)

    # least frequently occuring value in a column and its count
    dprof_df['least_freq_valwcount'] = [data_df[x].value_counts().sort_values(ascending=True).head(1).reset_index().values.tolist()[0] for x in columns2Bprofiled]
    dprof_df['least_freq_value'] = [x[0] for x in dprof_df['least_freq_valwcount']]
    dprof_df['least_freq_value_count'] = [x[1] for x in dprof_df['least_freq_valwcount']]
    dprof_df = dprof_df.drop(['least_freq_valwcount'],axis=1)

    return dprof_df

# Set the spark session and conf parameters

df = pd.read_csv("dataprofiling_file1.csv", header =0)
cols2profile = df.columns
dprofile = dataprofile(df,cols2profile,'test_db','test_table')
print(dprofile)


import streamlit as st
import pickle
import pandas as pd

def recommend(food):
    item_index = item[item['item_name'] == food].index[0]
    distance = similarity[item_index]
    item_list = sorted(list(enumerate(distance)), reverse=True, key=lambda x: x[1])
    listitem = []
    pictures=[]
    count = 0
    #recommended_items=[]
    for i in item_list:

        item_id = i[0]
        #fetch_image from api

        if item.iloc[i[0]].item_name not in listitem:
            listitem.append(item.iloc[i[0]].item_name)
            pictures.append(img[img['item_name']==item.iloc[i[0]].item_name]['images'].values[0])
            #pictures.append(img[img['name']==item.iloc[i[0]].item_name]['image'].values[0])
            count += 1
            if count == 6:
                break

    #return recommended_items
    return listitem,pictures

item_dict = pickle.load(open('item_dict.pkl','rb'))
item=pd.DataFrame(item_dict)

option=pickle.load(open('dict_item.pkl','rb'))
opt=pd.DataFrame(option)

image=pickle.load(open('image.pkl','rb'))
img=pd.DataFrame(image)

similarity= pickle.load(open('cos_similarity.pkl','rb'))

st.title('Content Based Food Recommender System')

selected_item_name=st.selectbox(
'Which item do you prefer?',
opt['item_name'].values)

if st.button('Recommend'):
    names,pictures = recommend(selected_item_name)
    number=0

    col1, col2, col3, col4, col5 =st.columns(5)
    with col1:
            st.text(names[1])
            st.image(pictures[1])
    with col2:
            st.text(names[2])
            st.image(pictures[2])
    with col3:
            st.text(names[3])
            st.image(pictures[3])

    with col4:
            st.text(names[4])
            st.image(pictures[4])

    with col5:
            st.text(names[5])
            st.image(pictures[5])


from test_base import MainTestCase
import os
from odk_viewer.models import ParsedInstance, DataDictionary
from django.core.urlresolvers import reverse
import odk_viewer

class TestGPS(MainTestCase):

    def test_gps(self):
        self._create_user_and_login()
        self._publish_survey()
        self._make_submissions()
        self._check_has_geopoints()
        self._check_link_to_map_view()

    def _publish_survey(self):
        self.this_directory = os.path.dirname(__file__)
        xls_path = os.path.join(
            self.this_directory, "fixtures", "gps", "gps.xls")
        MainTestCase._publish_xls_file(self, xls_path)

    def _make_submissions(self):
        surveys = ['gps_1980-01-23_20-52-08',
                   'gps_1980-01-23_21-21-33']
        for survey in surveys:
            path = os.path.join(
                self.this_directory,
                'fixtures', 'gps', 'instances', survey + '.xml')
            self._make_submission(path)

    def _check_has_geopoints(self):
        self.assertEqual(DataDictionary.objects.count(), 1)
        dd = DataDictionary.objects.all()[0]
        # should have been saved to dd.surveys_with_geopoints during submission
        self.assertTrue(dd.has_surveys_with_geopoints())

    def _check_link_to_map_view(self):
        response = self.client.get("/%s/" % self.user.username)
        map_url = 'href="/%s/forms/gps/map"' % self.user.username
        self.assertContains(response, map_url)


"""empty message

Revision ID: 76d81c37d676
Revises: 21d138d4483a
Create Date: 2021-09-20 19:51:31.102664

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = '76d81c37d676'
down_revision = '21d138d4483a'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('trading', sa.Column('Bid outcome', sa.Float(), nullable=True))
    op.drop_index('Date, time', table_name='trading')
    op.drop_column('trading', 'Bid outcome Volume')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('trading', sa.Column('Bid outcome Volume', mysql.FLOAT(), nullable=True))
    op.create_index('Date, time', 'trading', ['Date, time'], unique=False)
    op.drop_column('trading', 'Bid outcome')
    # ### end Alembic commands ###


import json
import logging
import lzma
import os
from datetime import datetime
from typing import List, Optional, Tuple, Type, Union

from enforce_typing import enforce_types

from ocean_lib.agreements.consumable import AssetNotConsumable, ConsumableCodes
from ocean_lib.agreements.service_types import ServiceTypes
from ocean_lib.aquarius import Aquarius
from ocean_lib.assets.asset_downloader import download_asset_files, is_consumable
from ocean_lib.assets.ddo import DDO
from ocean_lib.data_provider.data_encryptor import DataEncryptor
from ocean_lib.data_provider.data_service_provider import DataServiceProvider
from ocean_lib.exceptions import AquariusError, InsufficientBalance
from ocean_lib.models.compute_input import ComputeInput
from ocean_lib.models.data_nft import DataNFT, DataNFTArguments
from ocean_lib.models.data_nft_factory import DataNFTFactoryContract
from ocean_lib.models.datatoken_base import (
    DatatokenArguments,
    DatatokenBase,
    TokenFeeInfo,
)
from ocean_lib.models.dispenser import DispenserArguments
from ocean_lib.models.fixed_rate_exchange import ExchangeArguments
from ocean_lib.ocean.util import (
    create_checksum,
    get_address_of_type,
    get_args_object,
    get_from_address,
    to_wei,
)
from ocean_lib.services.service import Service
from ocean_lib.structures.algorithm_metadata import AlgorithmMetadata
from ocean_lib.structures.file_objects import (
    ArweaveFile,
    FilesType,
    GraphqlQuery,
    SmartContractCall,
    UrlFile,
)
from ocean_lib.web3_internal.constants import ZERO_ADDRESS

logger = logging.getLogger("ocean")

class AssetArguments:
    def __init__(
        self,
        wait_for_aqua: bool = True,
        dt_template_index: Optional[int] = 1,
        pricing_schema_args: Optional[
            Union[DispenserArguments, ExchangeArguments]
        ] = None,
        metadata: Optional[dict] = None,
        with_compute: Optional[bool] = False,
        compute_values: Optional[dict] = None,
        credentials: Optional[dict] = None,
    ):
        self.wait_for_aqua = wait_for_aqua
        self.dt_template_index = dt_template_index
        self.pricing_schema_args = pricing_schema_args
        self.metadata = metadata
        self.with_compute = with_compute
        self.compute_values = compute_values
        self.credentials = credentials if credentials else {"allow": [], "deny": []}

class OceanAssets:
    """Ocean asset class for V4."""

    @enforce_types
    def __init__(self, config_dict, data_provider: Type[DataServiceProvider]) -> None:
        """Initialises OceanAssets object."""
        self._config_dict = config_dict
        self._chain_id = config_dict["CHAIN_ID"]

        self._metadata_cache_uri = config_dict.get("METADATA_CACHE_URI")
        self._data_provider = data_provider

        downloads_path = os.path.join(os.getcwd(), "downloads")
        self._downloads_path = config_dict.get("DOWNLOADS_PATH", downloads_path)
        self._aquarius = Aquarius.get_instance(self._metadata_cache_uri)

        self.data_nft_factory = DataNFTFactoryContract(
            self._config_dict, get_address_of_type(config_dict, "ERC721Factory")
        )

    @enforce_types
    def validate(self, ddo: DDO) -> Tuple[bool, list]:
        """
        Validate that the ddo is ok to be stored in aquarius.

        :param ddo: DDO.
        :return: (bool, list) list of errors, empty if valid
        """
        # Validation by Aquarius
        validation_result, validation_errors = self._aquarius.validate_ddo(ddo)
        if not validation_result:
            msg = f"DDO has validation errors: {validation_errors}"
            logger.error(msg)
            raise ValueError(msg)

        return validation_result, validation_errors

    @staticmethod
    @enforce_types
    def _encrypt_ddo(
        ddo: DDO,
        provider_uri: str,
        encrypt_flag: Optional[bool] = True,
        compress_flag: Optional[bool] = True,
    ):
        # Process the DDO
        ddo_dict = ddo.as_dictionary()
        ddo_string = json.dumps(ddo_dict, separators=(",", ":"))
        ddo_bytes = ddo_string.encode("utf-8")
        ddo_hash = create_checksum(ddo_string)

        # Plain DDO
        if not encrypt_flag and not compress_flag:
            flags = bytes([0])
            document = ddo_bytes
            return document, flags, ddo_hash

        # Only compression, not encrypted
        if compress_flag and not encrypt_flag:
            flags = bytes([1])
            # Compress DDO
            document = lzma.compress(ddo_bytes)
            return document, flags, ddo_hash

        # Only encryption, not compressed
        if encrypt_flag and not compress_flag:
            flags = bytes([2])
            # Encrypt DDO
            encrypt_response = DataEncryptor.encrypt(
                objects_to_encrypt=ddo_string,
                provider_uri=provider_uri,
                chain_id=ddo.chain_id,
            )
            document = encrypt_response.text
            return document, flags, ddo_hash

        # Encrypted & compressed
        flags = bytes([3])
        # Compress DDO
        compressed_document = lzma.compress(ddo_bytes)

        # Encrypt DDO
        encrypt_response = DataEncryptor.encrypt(
            objects_to_encrypt=compressed_document,
            provider_uri=provider_uri,
            chain_id=ddo.chain_id,
        )

        document = encrypt_response.text

        return document, flags, ddo_hash

    @staticmethod
    @enforce_types
    def _assert_ddo_metadata(metadata: dict):
        assert isinstance(
            metadata, dict
        ), f"Expected metadata of type dict, got {type(metadata)}"

        asset_type = metadata.get("type")

        assert asset_type in (
            "dataset",
            "algorithm",
        ), f"Invalid/unsupported asset type {asset_type}"

        assert "name" in metadata, "Must have name in metadata."

    @enforce_types
    def create_algo_asset(
        self,
        name: str,
        url: str,
        tx_dict: dict,
        image: str = "oceanprotocol/algo_dockers",
        tag: str = "python-branin",
        checksum: str = "sha256:8221d20c1c16491d7d56b9657ea09082c0ee4a8ab1a6621fa720da58b09580e4",
        *args,
        **kwargs,
    ) -> tuple:
        """Create asset of type "algorithm", having UrlFiles, with good defaults"""

        asset_args = get_args_object(args, kwargs, AssetArguments)

        if not asset_args.metadata:
            metadata = OceanAssets.default_metadata(name, tx_dict, "algorithm")

            metadata["algorithm"] = {
                "language": "python",
                "format": "docker-image",
                "version": "0.1",
                "container": {
                    "entrypoint": "python $ALGO",
                    "image": image,
                    "tag": tag,
                    "checksum": checksum,
                },
            }

            asset_args.metadata = metadata

        files = [UrlFile(url)]

        return self.create_bundled(files, tx_dict, asset_args)

    @enforce_types
    def create_url_asset(
        self,
        name: str,
        url: str,
        tx_dict: dict,
        *args,
        **kwargs,
    ) -> tuple:
        """Create asset of type "data", having UrlFiles, with good defaults"""
        asset_args = get_args_object(args, kwargs, AssetArguments)
        if not asset_args.metadata:
            asset_args.metadata = OceanAssets.default_metadata(name, tx_dict)

        files = [UrlFile(url)]

        return self.create_bundled(files, tx_dict, asset_args)

    @enforce_types
    def create_arweave_asset(
        self, name: str, transaction_id: str, tx_dict: dict, *args, **kwargs
    ) -> tuple:
        """Create asset of type "data", having UrlFiles, with good defaults"""
        asset_args = get_args_object(args, kwargs, AssetArguments)
        if not asset_args.metadata:
            asset_args.metadata = OceanAssets.default_metadata(name, tx_dict)

        files = [ArweaveFile(transaction_id)]

        return self.create_bundled(files, tx_dict, asset_args)

    @enforce_types
    def create_graphql_asset(
        self, name: str, url: str, query: str, tx_dict: dict, *args, **kwargs
    ) -> tuple:
        """Create asset of type "data", having GraphqlQuery files, w good defaults"""
        asset_args = get_args_object(args, kwargs, AssetArguments)
        if not asset_args.metadata:
            asset_args.metadata = OceanAssets.default_metadata(name, tx_dict)

        files = [GraphqlQuery(url, query)]

        return self.create_bundled(files, tx_dict, asset_args)

    @enforce_types
    def create_onchain_asset(
        self,
        name: str,
        contract_address: str,
        contract_abi: dict,
        tx_dict: dict,
        wait_for_aqua: bool = True,
        dt_template_index: Optional[int] = 1,
        pricing_schema_args: Optional[
            Union[DispenserArguments, ExchangeArguments]
        ] = None,
        *args,
        **kwargs,
    ) -> tuple:
        """Create asset of type "data", having SmartContractCall files, w defaults"""
        chain_id = self._chain_id
        onchain_data = SmartContractCall(contract_address, chain_id, contract_abi)
        files = [onchain_data]

        asset_args = get_args_object(args, kwargs, AssetArguments)
        if not asset_args.metadata:
            asset_args.metadata = OceanAssets.default_metadata(name, tx_dict)

        return self.create_bundled(files, tx_dict, asset_args)

    @classmethod
    @enforce_types
    def default_metadata(cls, name: str, tx_dict: dict, type="dataset") -> dict:
        address = get_from_address(tx_dict)

        date_created = datetime.now().isoformat()
        metadata = {
            "created": date_created,
            "updated": date_created,
            "description": name,
            "name": name,
            "type": type,
            "author": address[:7],
            "license": "CC0: PublicDomain",
        }
        return metadata

    @enforce_types
    def create_bundled(
        self, files: List[FilesType], tx_dict: dict, asset_args: AssetArguments
    ):
        provider_uri = DataServiceProvider.get_url(self._config_dict)

        self._assert_ddo_metadata(asset_args.metadata)
        name = asset_args.metadata["name"]
        data_nft_args = DataNFTArguments(name, name)

        if asset_args.dt_template_index == 2:
            datatoken_args = DatatokenArguments(
                f"{name}: DT1", files=files, template_index=2, cap=to_wei(100)
            )
        else:
            datatoken_args = DatatokenArguments(f"{name}: DT1", files=files)

        if not asset_args.pricing_schema_args:
            data_nft, datatoken = self.data_nft_factory.create_with_erc20(
                data_nft_args, datatoken_args, tx_dict
            )

        if isinstance(asset_args.pricing_schema_args, DispenserArguments):
            data_nft, datatoken = self.data_nft_factory.create_with_erc20_and_dispenser(
                data_nft_args, datatoken_args, asset_args.pricing_schema_args, tx_dict
            )

        if isinstance(asset_args.pricing_schema_args, ExchangeArguments):
            (
                data_nft,
                datatoken,
                _,
            ) = self.data_nft_factory.create_with_erc20_and_fixed_rate(
                data_nft_args, datatoken_args, asset_args.pricing_schema_args, tx_dict
            )

        ddo = DDO()
        # Generate the did, add it to the ddo.
        ddo.did = data_nft.calculate_did()
        # Check if it's already registered first!
        if self._aquarius.ddo_exists(ddo.did):
            raise AquariusError(
                f"Asset id {ddo.did} is already registered to another asset."
            )

        ddo.chain_id = self._chain_id
        ddo.metadata = asset_args.metadata
        ddo.credentials = asset_args.credentials
        ddo.nft_address = data_nft.address

        access_service = datatoken.build_access_service(
            service_id="0",
            service_endpoint=provider_uri,
            files=files,
        )
        ddo.add_service(access_service)

        if asset_args.with_compute or asset_args.compute_values:
            ddo.create_compute_service(
                "1",
                provider_uri,
                datatoken.address,
                files,
                asset_args.compute_values,
            )

        # Validation by Aquarius
        _, proof = self.validate(ddo)
        proof = (
            proof["publicKey"],
            proof["v"],
            proof["r"][0],
            proof["s"][0],
        )

        document, flags, ddo_hash = self._encrypt_ddo(ddo, provider_uri, True, True)

        wallet_address = get_from_address(tx_dict)

        data_nft.setMetaData(
            0,
            provider_uri,
            wallet_address.encode("utf-8"),
            flags,
            document,
            ddo_hash,
            [proof],
            tx_dict,
        )

        # Fetch the ddo on chain
        if asset_args.wait_for_aqua:
            ddo = self._aquarius.wait_for_ddo(ddo.did)

        return (data_nft, datatoken, ddo)

    # Don't enforce types due to error:
    # TypeError: Subscripted generics cannot be used with class and instance checks
    def create(
        self,
        metadata: dict,
        tx_dict: dict,
        credentials: Optional[dict] = None,
        data_nft_address: Optional[str] = None,
        data_nft_args: Optional[DataNFTArguments] = None,
        deployed_datatokens: Optional[List[DatatokenBase]] = None,
        services: Optional[list] = None,
        datatoken_args: Optional[List["DatatokenArguments"]] = None,
        encrypt_flag: Optional[bool] = True,
        compress_flag: Optional[bool] = True,
        wait_for_aqua: bool = True,
    ) -> Optional[DDO]:
        """Register an asset on-chain. Asset = {data_NFT, >=0 datatokens, DDO}

        Creating/deploying a DataNFT contract and in the Metadata store (Aquarius).

        :param metadata: dict conforming to the Metadata accepted by Ocean Protocol.
        :param publisher_wallet: account of the publisher registering this asset.
        :param credentials: credentials dict necessary for the asset.
        construct the serviceEndpoint for the `access` (download) service
        :param data_nft_address: hex str the address of the data NFT. The new
        asset will be associated with this data NFT address.
        :param data_nft_args: object of DataNFTArguments type if creating a new one
        :param deployed_datatokens: list of datatokens which are already deployed.
        :param encrypt_flag: bool for encryption of the DDO.
        :param compress_flag: bool for compression of the DDO.
        :param wait_for_aqua: wait to ensure ddo's updated in aquarius?
        :return: tuple of (data_nft, datatokens, ddo)
        """
        self._assert_ddo_metadata(metadata)

        provider_uri = DataServiceProvider.get_url(self._config_dict)

        if not data_nft_address:
            data_nft_args = data_nft_args or DataNFTArguments(
                metadata["name"], metadata["name"]
            )
            data_nft = data_nft_args.deploy_contract(self._config_dict, tx_dict)
            # register on-chain
            if not data_nft:
                logger.warning("Creating new NFT failed.")
                return None, None, None
            logger.info(f"Successfully created NFT with address {data_nft.address}.")
        else:
            data_nft = DataNFT(self._config_dict, data_nft_address)

        # Create DDO object
        ddo = DDO()

        # Generate the did, add it to the ddo.
        ddo.did = data_nft.calculate_did()
        # Check if it's already registered first!
        if self._aquarius.ddo_exists(ddo.did):
            raise AquariusError(
                f"Asset id {ddo.did} is already registered to another asset."
            )
        ddo.chain_id = self._chain_id
        ddo.metadata = metadata

        ddo.credentials = credentials if credentials else {"allow": [], "deny": []}

        ddo.nft_address = data_nft.address
        datatokens = []

        if not deployed_datatokens:
            services = []
            for datatoken_arg in datatoken_args:
                new_dt = datatoken_arg.create_datatoken(
                    data_nft, tx_dict, with_services=True
                )
                datatokens.append(new_dt)

                services.extend(datatoken_arg.services)

            for service in services:
                ddo.add_service(service)
        else:
            if not services:
                logger.warning("services required with deployed_datatokens.")
                return None, None, None

            datatokens = deployed_datatokens
            dt_addresses = []
            for datatoken in datatokens:
                if deployed_datatokens[0].address not in data_nft.getTokensList():
                    logger.warning(
                        "some deployed_datatokens don't belong to the given data nft."
                    )
                    return None, None, None

                dt_addresses.append(datatoken.address)

            for service in services:
                if service.datatoken not in dt_addresses:
                    logger.warning("Datatoken services mismatch.")
                    return None, None, None

                ddo.add_service(service)

        # Validation by Aquarius
        _, proof = self.validate(ddo)
        proof = (
            proof["publicKey"],
            proof["v"],
            proof["r"][0],
            proof["s"][0],
        )

        document, flags, ddo_hash = self._encrypt_ddo(
            ddo, provider_uri, encrypt_flag, compress_flag
        )

        wallet_address = get_from_address(tx_dict)

        data_nft.setMetaData(
            0,
            provider_uri,
            wallet_address.encode("utf-8"),
            flags,
            document,
            ddo_hash,
            [proof],
            tx_dict,
        )

        # Fetch the ddo on chain
        if wait_for_aqua:
            ddo = self._aquarius.wait_for_ddo(ddo.did)

        return (data_nft, datatokens, ddo)

    @enforce_types
    def update(
        self,
        ddo: DDO,
        tx_dict: dict,
        provider_uri: Optional[str] = None,
        encrypt_flag: Optional[bool] = True,
        compress_flag: Optional[bool] = True,
    ) -> Optional[DDO]:
        """Update a ddo on-chain.

        :param ddo - DDO to update
        :param publisher_wallet - who published this ddo
        :param provider_uri - URL of service provider. This will be used as base to construct the serviceEndpoint for the `access` (download) service
        :param encrypt_flag - encrypt this DDO?
        :param compress_flag - compress this DDO?
        :return - the updated DDO, or None if updated ddo not found in aquarius
        """
        self._assert_ddo_metadata(ddo.metadata)

        if not provider_uri:
            provider_uri = DataServiceProvider.get_url(self._config_dict)

        assert ddo.nft_address, "need nft address to update a ddo"
        data_nft = DataNFT(self._config_dict, ddo.nft_address)

        assert ddo.chain_id == self._chain_id

        for service in ddo.services:
            service.encrypt_files(ddo.nft_address, ddo.chain_id)

        # Validation by Aquarius
        validation_result, errors_or_proof = self.validate(ddo)
        if not validation_result:
            msg = f"DDO has validation errors: {errors_or_proof}"
            logger.error(msg)
            raise ValueError(msg)

        document, flags, ddo_hash = self._encrypt_ddo(
            ddo, provider_uri, encrypt_flag, compress_flag
        )

        proof = (
            errors_or_proof["publicKey"],
            errors_or_proof["v"],
            errors_or_proof["r"][0],
            errors_or_proof["s"][0],
        )

        wallet_address = get_from_address(tx_dict)

        tx_result = data_nft.setMetaData(
            0,
            provider_uri,
            wallet_address.encode("utf-8"),
            flags,
            document,
            ddo_hash,
            [proof],
            tx_dict,
        )

        ddo = self._aquarius.wait_for_ddo_update(ddo, tx_result.transactionHash.hex())

        return ddo

    @enforce_types
    def resolve(self, did: str) -> "DDO":
        return self._aquarius.get_ddo(did)

    @enforce_types
    def search(self, text: str) -> list:
        """
        Search for DDOs in aquarius that contain the target text string
        :param text - target string
        :return - List of DDOs that match with the query
        """
        logger.info(f"Search for DDOs containing text: {text}")
        text = text.replace(":", "\\:").replace("\\\\:", "\\:")
        return [
            DDO.from_dict(ddo_dict["_source"])
            for ddo_dict in self._aquarius.query_search(
                {"query": {"query_string": {"query": text}}}
            )
            if "_source" in ddo_dict
        ]

    @enforce_types
    def query(self, query: dict) -> list:
        """
        Search for DDOs in aquarius with a search query dict
        :param query - dict with query parameters
          More info at: https://docs.oceanprotocol.com/api-references/aquarius-rest-api
        :return - List of DDOs that match the query.
        """
        logger.info(f"Search for DDOs matching query: {query}")
        return [
            DDO.from_dict(ddo_dict["_source"])
            for ddo_dict in self._aquarius.query_search(query)
            if "_source" in ddo_dict
        ]

    @enforce_types
    def download_asset(
        self,
        ddo: DDO,
        consumer_wallet,
        destination: str,
        order_tx_id: Union[str, bytes],
        service: Optional[Service] = None,
        index: Optional[int] = None,
        userdata: Optional[dict] = None,
    ) -> str:
        service = service or ddo.services[0]  # fill in good default

        if index is not None:
            assert isinstance(index, int), logger.error("index has to be an integer.")
            assert index >= 0, logger.error("index has to be 0 or a positive integer.")

        assert (
            service and service.type == ServiceTypes.ASSET_ACCESS
        ), f"Service with type {ServiceTypes.ASSET_ACCESS} is not found."

        path: str = download_asset_files(
            ddo, service, consumer_wallet, destination, order_tx_id, index, userdata
        )
        return path

    @enforce_types
    def pay_for_access_service(
        self,
        ddo: DDO,
        tx_dict: dict,
        service: Optional[Service] = None,
        consume_market_fees: Optional[TokenFeeInfo] = None,
        consumer_address: Optional[str] = None,
        userdata: Optional[dict] = None,
        consume_market_swap_fee_amount: Optional[int] = 0,
        consume_market_swap_fee_address: Optional[str] = ZERO_ADDRESS,
    ):
        # fill in good defaults as needed
        service = service or ddo.services[0]
        wallet_address = get_from_address(tx_dict)
        consumer_address = consumer_address or wallet_address

        consumable_result = is_consumable(
            ddo,
            service,
            {"type": "address", "value": wallet_address},
            userdata=userdata,
        )

        if consumable_result != ConsumableCodes.OK:
            raise AssetNotConsumable(consumable_result)

        data_provider = DataServiceProvider

        initialize_args = {
            "did": ddo.did,
            "service": service,
            "consumer_address": consumer_address,
        }

        initialize_response = data_provider.initialize(**initialize_args)
        provider_fees = initialize_response.json()["providerFee"]

        params = {
            "consumer": consumer_address,
            "service_index": ddo.get_index_of_service(service),
            "provider_fees": provider_fees,
            "consume_market_fees": consume_market_fees,
            "tx_dict": tx_dict,
        }

        # main work...
        dt = DatatokenBase.get_typed(self._config_dict, service.datatoken)
        balance = dt.balanceOf(wallet_address)

        if balance < to_wei(1):
            try:
                params[
                    "consume_market_swap_fee_amount"
                ] = consume_market_swap_fee_amount
                params[
                    "consume_market_swap_fee_address"
                ] = consume_market_swap_fee_address
                receipt = dt.get_from_pricing_schema_and_order(**params)
            except Exception:
                receipt = None

            if receipt:
                return receipt

            raise InsufficientBalance(
                f"Your token balance {balance} {dt.symbol()} is not sufficient "
                f"to execute the requested service. This service "
                f"requires 1 wei."
            )

        receipt = dt.start_order(**params)

        return receipt.transactionHash

    @enforce_types
    def pay_for_compute_service(
        self,
        datasets: List[ComputeInput],
        algorithm_data: Union[ComputeInput, AlgorithmMetadata],
        compute_environment: str,
        valid_until: int,
        consume_market_order_fee_address: str,
        tx_dict: dict,
        consumer_address: Optional[str] = None,
    ):
        data_provider = DataServiceProvider
        wallet_address = get_from_address(tx_dict)

        if not consumer_address:
            consumer_address = wallet_address

        initialize_response = data_provider.initialize_compute(
            [x.as_dictionary() for x in datasets],
            algorithm_data.as_dictionary(),
            datasets[0].service.service_endpoint,
            consumer_address,
            compute_environment,
            valid_until,
        )

        result = initialize_response.json()
        for i, item in enumerate(result["datasets"]):
            self._start_or_reuse_order_based_on_initialize_response(
                datasets[i],
                item,
                TokenFeeInfo(
                    consume_market_order_fee_address,
                    datasets[i].consume_market_order_fee_token,
                    datasets[i].consume_market_order_fee_amount,
                ),
                tx_dict,
                consumer_address,
            )

        if "algorithm" in result:
            self._start_or_reuse_order_based_on_initialize_response(
                algorithm_data,
                result["algorithm"],
                TokenFeeInfo(
                    address=consume_market_order_fee_address,
                    token=algorithm_data.consume_market_order_fee_token,
                    amount=algorithm_data.consume_market_order_fee_amount,
                ),
                tx_dict,
                consumer_address,
            )

            return datasets, algorithm_data

        return datasets, None

    @enforce_types
    def _start_or_reuse_order_based_on_initialize_response(
        self,
        asset_compute_input: ComputeInput,
        item: dict,
        consume_market_fees: TokenFeeInfo,
        tx_dict: dict,
        consumer_address: Optional[str] = None,
    ):
        provider_fees = item.get("providerFee")
        valid_order = item.get("validOrder")

        if valid_order and not provider_fees:
            asset_compute_input.transfer_tx_id = valid_order
            return

        service = asset_compute_input.service
        dt = DatatokenBase.get_typed(self._config_dict, service.datatoken)

        if valid_order and provider_fees:
            asset_compute_input.transfer_tx_id = dt.reuse_order(
                valid_order, provider_fees=provider_fees, tx_dict=tx_dict
            ).transactionHash.hex()
            return

        asset_compute_input.transfer_tx_id = dt.start_order(
            consumer=consumer_address,
            service_index=asset_compute_input.ddo.get_index_of_service(service),
            provider_fees=provider_fees,
            consume_market_fees=consume_market_fees,
            tx_dict=tx_dict,
        ).transactionHash.hex()


# from sklearn.externals import joblib
import joblib
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import LabelEncoder

data = pd.read_csv('book32-listing.csv',encoding = "ISO-8859-1")

columns = ['Id', 'Image', 'Image_link', 'Title', 'Author', 'Class', 'Genre']
data.columns = columns

books = pd.DataFrame(data['Title'])
author = pd.DataFrame(data['Author'])
genre = pd.DataFrame(data['Genre'])

data['Author'] = data['Author'].fillna('No Book')
data['Title'] = data['Title'].fillna('No Book')

feat = ['Genre']
for x in feat:
    le = LabelEncoder()
    le.fit(list(genre[x].values))
    genre[x] = le.transform(list(genre[x]))

data['everything'] = pd.DataFrame(data['Title'] + ' ' + data['Author'])

vectorizer = TfidfVectorizer(min_df=2, max_features=70000, strip_accents='unicode',lowercase =True,
                            analyzer='word', token_pattern=r'\w+', use_idf=True,
                            smooth_idf=True, sublinear_tf=True, stop_words = 'english')
vectors = vectorizer.fit_transform(data['everything'])

def predict(title):
	s = title
	text = []
	text.append(s)
	text[0] = text[0].lower()
	arr = (vectorizer.transform(text))
	clf = joblib.load('best.pkl')
	prediction = (clf.predict(arr))
	prediction = le.inverse_transform(prediction)[0]
	return prediction


import numpy as np
from numpy import *
import csv
import pandas as pd

class ProcessInput:
# ------------------------------------------------------------------------------------------------
    def rescale_data(self, descriptor_matrix):
        # You have already written code for this.
        mean = descriptor_matrix.flatten().mean()
        std_dev = descriptor_matrix.flatten().std()
        #print("Mean: ", mean, ", Std_deviation: ", std_dev)

        return (descriptor_matrix - mean)/ std_dev

# ------------------------------------------------------------------------------------------------
# What do we need to sort the data?

    def sort_descriptor_matrix(self, descriptors, targets):
        # Placing descriptors and targets in ascending order of target (IC50) value.
        alldata = ndarray((descriptors.shape[0], descriptors.shape[1] + 1))
        alldata[:, 0] = targets
        alldata[:, 1:alldata.shape[1]] = descriptors
        alldata = alldata[alldata[:, 0].argsort()]
        descriptors = alldata[:, 1:alldata.shape[1]]
        targets = alldata[:, 0]

        return descriptors, targets

# ------------------------------------------------------------------------------------------------

# Performs a simple split of the data into training, validation, and testing sets.
# So how does it relate to the Data Mining Prediction?

    def simple_split(self, descriptors, targets):

        testX_indices = [i for i in range(descriptors.shape[0]) if i % 4 == 0]
        validX_indices = [i for i in range(descriptors.shape[0]) if i % 4 == 1]
        trainX_indices = [i for i in range(descriptors.shape[0]) if i % 4 >= 2]

        TrainX = descriptors[trainX_indices, :]
        ValidX = descriptors[validX_indices, :]
        TestX = descriptors[testX_indices, :]

        TrainY = targets[trainX_indices]
        ValidY = targets[validX_indices]
        TestY = targets[testX_indices]

        return TrainX, ValidX, TestX, TrainY, ValidY, TestY

# ------------------------------------------------------------------------------------------------

# try to optimize this code if possible
 # OPTIMIZED based on possible forms of text file as csv
    def open_descriptor_matrix (self, fileName):
        with open(fileName, mode='r') as csvfile:
            all_rows = [row.strip().replace(",", " ").replace(";", " ") for row in csvfile if row != '']
            all_entries = list(map(str.split, all_rows))

            dataArray = array(all_entries, order='C')

        if (min(dataArray.shape) == 1):  # flatten arrays of one row or column
            return dataArray.flatten(order='C')
        else:
            return dataArray

    # ************************************************************************************
    # Try to optimize this code if possible

    def open_target_values (self, fileName):
        with open(fileName, mode='r') as csvfile:
            all_rows = [row.strip().replace(",", " ").replace(";", " ") for row in csvfile if row != '']
            datalist = list(map(str.split, all_rows))

        for i in range(len(datalist)):
            try:
                datalist[i] = float(i)
            except:
                datalist[i] = datalist[i]
        return array(datalist)

    def open_file_to_matrix(self, filename):
        if(filename):
            dataArray = np.loadtxt(filename, delimiter=',')
            return dataArray
        else:
            print("Error loading data from file.")
            return np.zeros((2, 1))

#**********************************************************************************************
# Removes constant and near-constant descriptors.
# But I think also does that too for real data.
# So for now take this as it is

    def removeNearConstantColumns(self, data_matrix, num_unique=10):
        total_columns = data_matrix.shape[1]
        useful_descriptors = [col for col in range(data_matrix.shape[1])
                                if len(set(data_matrix[:, col])) > num_unique]
        filtered_matrix = data_matrix[:, useful_descriptors]

        remaining_desc = zeros(data_matrix.shape[1])
        remaining_desc[useful_descriptors] = 1

        deleted_columns = total_columns - filtered_matrix.shape[1]
        print("Near constant columns deleted: ", deleted_columns)

        return filtered_matrix, where(remaining_desc == 1)[0]

# ------------------------------------------------------------------------------------------------
# part 1: Removes all rows with junk (ex: NaN, etc). Note that the corresponding IC50 value should be deleted too
# Part 2: Remove columns with 20 junks or more. Otherwise the junk should be replaced with zero
# Part 3: remove all columns that have zero in every cell

    def removeInvalidData(self, descriptors, targets):
        total_rows = descriptors.shape[0]
        total_columns = descriptors.shape[1]

        # Combine all data into one matrix
        alldata = ndarray((descriptors.shape[0], descriptors.shape[1] + 1))
        alldata[:, 0] = list(targets)
        alldata[:, 1:alldata.shape[1]] = descriptors

        # Remove all rows with more than 20 junks
        alldata = pd.DataFrame(alldata)
        alldata = (alldata.apply(pd.to_numeric, errors='coerce'))  # Change "junk values" into NaN
        # alldata = alldata.loc[alldata.isna().sum() < 20, :]   # Clean rows
        # alldata = alldata.dropna(axis=0, thresh=20) # Drop rows with 20+ nan/junk

        # Separate all data into descriptors and targets
        descriptors = alldata.loc[:, 1:alldata.shape[1]]
        targets = (alldata.loc[:, 0]).to_numpy()

        # Remove columns with 20 junks or more. Replace junk with zero.
        columns_junk_replaced = (descriptors.loc[:, descriptors.isna().sum() < 20]).shape[1]
        print("Total columns with junk replaced by zero: ", columns_junk_replaced)

        descriptors = descriptors.loc[:, descriptors.isna().sum() < 20]
        descriptors = descriptors.fillna(0)     # Change nan to zero
        descriptors = descriptors.to_numpy()

        removed_columns = total_columns - descriptors.shape[1]
        removed_rows = total_rows - descriptors.shape[0]
        print("Invalid data deleted: ", removed_rows, " rows, and ", removed_columns, " columns")

        return descriptors, targets
#------------------------------------------------------------------------------------------------


# if is a conditional statement used to create a code block.
b_k = 15
if b_k > 5:
    print("There are still empty seats to sit.")

print(b_k<5)

# else bir if ifadesi gibi çalışır. if ifadesinin içindeki hiçbir koşul sağlanmazsa, diğer koşulları çalıştırmak için else kullanılır.

kurs = "Cosmios"

if kurs == "Cosmios":
    print("A fantastic training center.")

# else:
#    print("You should think again.")

if kurs != "Cosmios":
    print("You should think again.")

# if is used for the first one,
# elif for the rest (optional) until the end,
# else is used for anything not caught by other conditional statements.

weight = 80

if weight > 100:
    print("That's too heavy! ")

elif weight > 75 :
    print("I can lift that! ")

else:
    print("That's too light! ")

# name = input("Please enter your name : ")
# print("Entered name : ", name)
# print(type(name))

# age = int(input("Please enter your age : "))
# print("Entered age : ", age)
# print(type(age))

n1 = int(input("Please enter number 1: "))
n2 = int(input("Please enter number 2: "))
n3 = int(input("Please enter number 3: "))

if (n1 >= n2 and n1 >= n3):
    print(f"The largest number {n1}")
    print("First block")

elif (n2 >= n1 and n2 >= n3):
    print(f"The largest number {n2}")
    print("Second block")

else:
    print(f"The largest number {n3}")

# age = int(input("Please enter your age : "))

# if age >=0:

#     if age >= 0 and age <= 8:
#         audience = "Baby"

#     elif  age >= 8 and age <= 16:
#         audience = "Kid"

#     elif  age >= 17 and age <= 25:
#         audience = "Young"

#     else:
#         audience = "Adult"

#     if audience == "Kid":
#         print("It is free to go to cinema! ")

#     elif audience == "Young":
#         print("Discounted price! ")

#     elif audience == "Adult":
#         print("Normal price.")

#     else:
#         print("You are not suitable to go to the cinema.")

# else:
#     print("You entered an invalid age.")

age = int(input("Please enter your age : "))

if age > 0 and age < 100:
    if age >=0 and age <= 7 :
        audience = "Baby"
        if audience == "Baby":
            print("Not an appropriate age to go to the cinema !")

    elif age >=8 and age <=16 :
        audience = "Kid"
        if audience == "Kid":
            print("It is free to go to cinema!")

    elif age >=17 and age <=25 :
        audience = "Young"
        if audience == "Young":
            print("Discounted price! ")
    else:
        audience = "Adult"
        print("Normal price.")
else:
    print("You entered an invalid age.")

score = int (input("Enter your score : "))

if score >= 90:
    if score >=95:
        Score_letter = "A+"
    else:
        Score_letter = "A"
elif score >= 80:
    if score >=85:
        Score_letter = "B+"
    else:
        Score_letter = "B"
else:
    Score_letter = "Below B"

print("Your degree %s" % Score_letter)

# TERNATY

# <value-1> if <condition> else <value-2>

x = 0
y = 19

max_value = x if x>y else y
print(max_value)

a = x if x > y else y

if x > y:
    print(x)

else:
    print(y)

is_even = "Even" if x % 2 == 0 else "Odd"
is_even_2 = "Odd" if y % 2 != 0 else "Even"
print(is_even)
print(is_even_2)

result = "Pozitive" if x > 0 else ("Negative" if x <0 else "None")
print(result)


# -*- coding: UTF-8 -*-
from boto.sdb.db.property import StringProperty, IntegerProperty
from sdb_orm.model import Model
from sdb_orm.property import AutoIDProperty, BlobProperty

# 宣告游戏地图资料模型
class GameMap(Model):
    # 资料表名称
    _db_name = 'game-maps'
    # 预设使用一致性读取，不然有可能会读到不是最新的资料，因为sdb是分散式数据库
    __consistent__ = True

    # 自动流水号，此字段不可设定，只会自动捞资料表中最大值+1
    # bound是流水号可能值的上限，只可以小于它，这边设为10的8次方
    mid = AutoIDProperty(bound=10**8)
    name = StringProperty(required=True) # 地图名称，字串字段，必要值
    width = IntegerProperty(default=None, required=True, min=1) # 宽，整数字段
    height = IntegerProperty(default=None, required=True, min=1)
    # 因sdb有字段值长度不可超过1024的限制，这边把资料存到Amazon s3变成一个档案
    # 然后在sdb的字段里存路径，这个属性经过包装，可以当字串直接读写
    # 传入auto_id作为自动产生档名的依据
    grid = BlobProperty(required=True, auto_id=mid)


from asyncio import Condition, Task, gather
from queue import SimpleQueue
from typing import Any, Literal, Mapping, Optional, Sequence, Tuple, Union, cast
from uuid import uuid4

from pynvim import Nvim
from pynvim.api.nvim import Nvim
from pynvim_pp.lib import async_call, go
from pynvim_pp.logging import log, with_suppress
from std2.asyncio import cancel, run_in_executor
from std2.pickle import DecodeError, new_decoder

from ...registry import atomic, autocmd, rpc
from ...shared.timeit import timeit
from ...shared.types import Context, NvimPos
from ..context import context
from ..edit import edit
from ..nvim.completions import UserData, complete
from ..rt_types import Stack
from ..state import State, state
from ..trans import trans

q: SimpleQueue = SimpleQueue()

@rpc(blocking=True)
def _launch_loop(nvim: Nvim, stack: Stack) -> None:
    task: Optional[Task] = None

    async def cont() -> None:
        cond = Condition()
        ctx: Optional[Context] = None

        async def c0(ctx: Context) -> None:
            _, col = ctx.position
            metrics = await stack.supervisor.collect(ctx)
            s = state()
            if s.change_id == ctx.change_id:
                vim_comps = tuple(trans(stack, context=ctx, metrics=metrics))
                await async_call(nvim, complete, nvim, col=col, comp=vim_comps)

        async def c1() -> None:
            nonlocal ctx
            while True:
                with with_suppress():
                    ctx = await run_in_executor(q.get)
                    async with cond:
                        cond.notify_all()

        async def c2() -> None:
            nonlocal task
            while True:
                with with_suppress():
                    async with cond:
                        await cond.wait()

                    if task:
                        await cancel(task)

                    if ctx:
                        task = cast(Task, go(nvim, aw=c0(ctx)))

        await gather(c1(), c2())

    go(nvim, aw=cont())

atomic.exec_lua(f"{_launch_loop.name}()", ())

def _should_cont(inserted: Optional[NvimPos], prev: Context, cur: Context) -> bool:
    if prev.change_id == cur.change_id:
        return False
    elif cur.position == inserted:
        return False
    else:
        return (cur.words_before or cur.syms_before) != ""

def comp_func(nvim: Nvim, stack: Stack, s: State, manual: bool) -> None:
    with timeit("GEN CTX"):
        ctx = context(
            nvim, db=stack.bdb, options=stack.settings.match, state=s, manual=manual
        )

    should = _should_cont(s.inserted, prev=s.context, cur=ctx) if ctx else False
    _, col = ctx.position
    complete(nvim, col=col - 1, comp=())

    if manual or should:
        state(context=ctx)
        q.put(ctx)
    else:
        state(inserted=(-1, -1))

@rpc(blocking=True)
def omnifunc(
    nvim: Nvim, stack: Stack, args: Tuple[Tuple[Literal[0, 1], str]]
) -> Union[int, Sequence[Mapping[str, Any]]]:
    (op, _), *_ = args

    if op == 1:
        return -1
    else:
        s = state(commit_id=uuid4())
        comp_func(nvim, stack=stack, manual=True, s=s)
        return ()

_DECODER = new_decoder(UserData)

@rpc(blocking=True)
def _comp_done(nvim: Nvim, stack: Stack, event: Mapping[str, Any]) -> None:
    data = event.get("user_data")
    if data:
        try:
            user_data: UserData = _DECODER(data)
        except DecodeError as e:
            log.warn("%s", e)
        else:
            s = state()
            if user_data.change_uid == s.change_id:
                inserted = edit(
                    nvim, stack=stack, state=s, data=user_data, synthetic=False
                )
                state(inserted=inserted, commit_id=uuid4())
            else:
                log.warn("%s", "delayed completion")

autocmd("CompleteDone") << f"lua {_comp_done.name}(vim.v.completed_item)"


l=[3,4,5,6,88,90]
maxn=l[0]
minn=l[0]
for i in l:
    if(i>maxn):
        maxn=i

    if(i<minn):
        minn=i
print("maximum value:"+str(maxn))
print("minimum value:"+str(minn))
print("difference:"+str(maxn-minn))


from pyeventdispatcher.event_dispatcher import (
    dispatch_global_event,
    listen,
    EventDispatcher,
    Event,
    EventDispatcherException,
    EventSubscriber,
    register_global_listener,
    register_event_subscribers,
)

__version__ = "0.2.3a0"

register = register_global_listener
dispatch = dispatch_global_event


# Q1. Write a function to print length of a list

# Ans :

list = [1,"Hello World",True,12.5]
print("The Length of the List is : ",len(list))

# Q2. Write a function to print the reverse of a list, pass the list as a parameter

# Ans :

def reverseList(l):
    print("After Reversing : ",l[::-1])

list1 = [1,2,3,4,5]
print("Before Reversing : ",list1)
reverseList(list1)

# Q3. Write a function that returns(not prints) the data-type of the last element in a list

# Ans :

def returnDataType(list):
    return(type(list2[-1]))

list2 = ["Junaid",False,23.5,10]
returnDataType(list2)
print(returnDataType(list))


import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"

import torch
import pickle
from tqdm import tqdm
from datasets import load_dataset, load_metric
from transformers import BertTokenizer, BertForMaskedLM, DistilBertTokenizerFast, DistilBertForMaskedLM
from transformers import DataCollatorForLanguageModeling, TrainingArguments, Trainer

tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
model = DistilBertForMaskedLM.from_pretrained('distilbert-base-uncased')

dataset = load_dataset("./Datasets/Arith")

def encode_with_truncation(examples):
    ab = [example.split('=')[0] + "= [MASK] [MASK] [MASK]" for example in examples["text"]]
    return tokenizer(ab, truncation=True, padding="max_length", max_length=512, return_special_tokens_mask=True)

train_dataset = dataset['train'].map(encode_with_truncation, batched=True)
train_dataset.set_format(type="torch", columns=['input_ids', 'attention_mask', 'special_tokens_mask'])

label_col = []
for idx, val in tqdm(enumerate(dataset['train'])):
    label_ids = tokenizer.encode(val['text'].split('=')[1][1:])[1:-1]
    mask_idx = (train_dataset[idx]['input_ids'] == 103).nonzero(as_tuple=True)[0]
    labels = [-100] * 512
    for i, v in enumerate(label_ids):
        labels[mask_idx[i]] = v
    label_col.append(labels)

train_dataset = train_dataset.add_column("labels", label_col)

training_args = TrainingArguments(
    output_dir='./Models/Arith1',
    overwrite_output_dir=True,
    num_train_epochs=50,
    per_device_train_batch_size=16,
    logging_steps=500,
    save_steps=1000,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    tokenizer=tokenizer,
)

trainer.train()


#It's a matlab file that needs to be cleaned up
from scipy.io import loadmat
import h5py,math

from collections import Counter,defaultdict
import numpy as np
from .. import ObjectMetrics

#Return back a parsed list of Support frequency connections from the nyu_depth_v2_labeled matlab matrix.
#That matrix was converted from the originial found at https://cs.nyu.edu/~silberman/datasets/
def readMat(matPath): #10 percent in the paper
        f = h5py.File(matPath+"nyu_depth_v2_labeled.mat",'r')
        rooms = f['labels']
        def conv1(x):
                maximum = len(first_change)
                if x[0] > 0 and x[0] < maximum:
                       x[0] = first_change[x[0]]
                if x[1] > 0 and x[1] < maximum:
                       x[1] = first_change[x[1]]
                return x
        def convert(x):
                if x[2] == 1:
                        x[2] = 'Vertical-Support'
                elif x[2] == 2:
                        x[2] = 'Horizontal-Support'
                else:
                        x[2] = 'Support'
                if x[0] in names_to_id:
                        x[0] = names_to_id[x[0]]
                else:
                        x[0] = 'room'
                if x[1] in names_to_id:
                        x[1] = names_to_id[x[1]]
                else:
                        x[1] = 'room'
                return x
        places = open(matPath + 'scenes.dat','r').readlines()
        places = [i.strip().split('$') for i in places]
        ps = {}
        ret_data = defaultdict(list)
        for x in places:
                ps[int(x[0])] = x[1]
        exitnames = open(matPath + 'names.dat','r').readlines()
        exitnames = [i.strip().split('$') for i in exitnames]
        names_to_id = {}
        for i in exitnames:
                names_to_id[int(i[1])] = i[0]
        support = loadmat(matPath + 'support_labels.mat')['supportLabels']

        #This loads the actual support code
        for i in range(len(support)):
                first_change = np.unique(rooms[i,:,:])
                x = support[i][0].tolist()
                place = ps[i]
                x = list(map(lambda j:conv1(j),x)) #First, convert to correct names per scene
                x = list(map(lambda j:convert(j),x))
                #if place == "bedroom":
                #        print (x)
                if place in ret_data:
                        ret_data[place].append(x)
                else:
                        ret_data[place] = [x]
        return ret_data

def cleanRules(scenes):
    #Returns a cleaner rule-set where we pre-remove some noise
    #This can be expanded into a better human-in-the-loop system
    r = {}
    for place in sorted(scenes.keys()):
        result = []
        for scene in scenes[place]:
            res = []
            for j in scene:
                #print (j)
                if 'None' not in j:
                    if j[2] == 'Vertical-Support' and 'wall' in j and not('floor' in j or 'room' in j):
                        pass
                        #print("did not add",j)
                    else:
                        res.append(j)
                else:
                    print("did not add",j)
            result.append(res)
        r[place] = result
    return r

def getSupportTypes(scenes):
    #Quick helper function to get the types we have for a given scene
    types = []
    for scene in scenes:
        for j in scene:
            if j[2] not in types:
                types.append(j[2])
    return types

def supportFrequency(scenes, threshold = 0.1, debug = False):
    ''' Determines the support factors per scene'''
    counts = {}
    frequency = Counter()
    thresh = len(scenes)*threshold

    for i in scenes:
        freq = Counter()
        for j in i:
            if 'None' not in j:
                freq[j[0]] +=1
                freq[j[1]] +=1
        for f in freq:
            frequency[f] +=1
    if debug:
        for i in scenes:
            #first = i.split("$")[1]
            #second = i.split("$")[2]
            #if frequency[first] > thresh and frequency[second] > thresh:
            for j in i:
                print(j)
        print (frequency)
    #Then, we run through counts getting only the ones that are higher than the threshold
    #set_freq   = set([str(i) for i in frequency if frequency[i] > thresh])
    #ret_counts = [i.split("$")+[str(counts[i])] for i in counts if counts[i] > s_thresh and i.split("$")[1] in set_freq and i.split("$")[2] in set_freq]
    ret_freq   = [["frequency",str(i),str(frequency[i])] for i in frequency if frequency[i] > thresh]
    return ret_freq

def supportMining(scenes,good_objects,threshold = 0.1):
    '''Performs frequent pattern mining on the directed acyclic graphs for support'''
    min_gap = math.ceil(len(scenes) * threshold) #If we dont' have our single object at the minimum threshold, we won't have any larger support
    counts = Counter()
    for scene in scenes:
        for i in [(edge[1],edge[0]) for edge in scene if edge[0] in good_objects and edge[1] in good_objects]:
            counts[i] +=1
    return [(pair[0],pair[1],str(counts[pair])) for pair in counts if counts[pair] >= min_gap]

def getSupportTypes(scenes):
    #Quick helper function to get the types we have for a given scene
    types = []
    for scene in scenes:
        for j in scene:
            if j[2] not in types:
                types.append(j[2])
    return types

def buildSupportValues(scene,support):
    #Returns a key-value dict that only has the support value in question
    result = []
    for i in scene:
        res = []
        for j in i:
            if j[2] == support:
                res.append([j[0],j[1]])
        if len(res) > 0:
           result.append(res)
    return result

def mineNYU2Data(path_to_data,output_path):
    ''' '''
    mat_path = path_to_data+"data/sunrgbd/nyu_2_mat/"
    debug = False
    support = readMat(mat_path)
    support = cleanRules(support)#Cleans up some of our known noisy data that we shouldn't have
    with open(output_path,'w') as fi:
        for place in sorted(support.keys()):
            print (place)
            fi.write(','.join([place,str(len(support[place]))])+'\n')
            support_types = getSupportTypes(support[place])
            #print (support[place])
            rf = supportFrequency(support[place],0.1)
            for i in rf: #This is each item that was high enough support
                fi.write(','.join([ObjectMetrics.cleanup_word(str(j)) for j in i])+'\n')
            #In rc, we split up into our different factors. That will allow us to search each graph independently
            for s in support_types:
                 prox = supportMining(buildSupportValues(support[place],s),[i[1] for i in rf],0.1)
                 for item in prox:
                     fi.write(','.join([ObjectMetrics.cleanup_word(s)]+[ObjectMetrics.cleanup_word(i) for i in item])+'\n')
                 del prox


import os
import sys
import numpy as np
import configparser
import netCDF4 as nC
import matplotlib.pyplot as plt
from main import is_valid_data_file
from tqdm import tqdm

CONFIG_FILENAME = './config.ini'
CONFIG_PATHS = 'MRR_PATHS'
CONFIG_INPUT_PATH = 'INPUT_PATH'
CONFIG_OUTPUT_PATH = 'OUTPUT_PATH'

PLOT_BASE_TITLE = 'MRR-Pro config from netCDF Archive: '

DATA_UNIT_CHECK = 0
DATA_VAR_LIST_CHECK = 1
DATA_DIM_SIZE = 2
DATA_DELTA_VAL = 3

DATA_FORMAT = {
    'Z_UNITS': {'var': 'Z', 'type': DATA_UNIT_CHECK, 'label': 'Z-dBZ', 'row': 0, 'units': 'dBZ'},
    'SPEC_R': {'var': 'spectrum_raw', 'type': DATA_VAR_LIST_CHECK, 'label': 'R-SPEC', 'row': 1, 'units': 'dBZ'},
    'SPEC_Z': {'var': 'spectrum_reflectivity', 'type': DATA_VAR_LIST_CHECK, 'label': 'Z-SPEC', 'row': 2, 'units': 'dBZ'},
    'SPEC_N': {'var': 'spectrum_n_samples', 'type': DATA_DIM_SIZE, 'label': 'N-SPEC', 'row': 3, 'units': 'dBZ'},
    'RANG_N': {'var': 'range', 'type': DATA_DIM_SIZE, 'label': 'N-Rng', 'row': 4, 'units': 'dBZ'},
    'TIME_DT': {'var': 'time', 'type': DATA_DELTA_VAL, 'label': 'dt', 'row': 5, 'units': 'dBZ'},
    'RANG_DR': {'var': 'range', 'type': DATA_DELTA_VAL, 'label': 'dR', 'row': 6, 'units': 'dBZ'}
}

N_DATA_ROWS = len(DATA_FORMAT)

class NcFileProperties:
    def __init__(self, name, nc_id):
        self.file_name = name
        self.time = int(nc_id.variables['time'][0])
        self.var_list = [var for var in nc_id.variables]
        self.data = np.empty(N_DATA_ROWS, dtype=np.int16)

        for item in DATA_FORMAT:
            var = DATA_FORMAT[item]['var']
            units = DATA_FORMAT[item]['units']
            row = DATA_FORMAT[item]['row']
            method = DATA_FORMAT[item]['type']
            self.data[row] = get_nc_diagnostic_vals(nc_id, var, units, self.var_list, method)

def get_nc_diagnostic_vals(file_id, var_name, unit_str, var_list, data_type):
    if data_type == DATA_UNIT_CHECK:
        if file_id.variables[var_name].getncattr('units') == unit_str:
            return 1
        else:
            return 0
    elif data_type == DATA_VAR_LIST_CHECK:
        if var_name in var_list:
            return 1
        else:
            return 0
    elif data_type == DATA_DIM_SIZE:
        return file_id.dimensions[var_name].size
    elif data_type == DATA_DELTA_VAL:
        return round(file_id.variables[var_name][1] - file_id.variables[var_name][0])

def read_nc_diagnostics(path, filename):
    nc_id = nC.Dataset(path+'/'+filename, "r")
    file_info = NcFileProperties(filename, nc_id)
    nc_id.close()
    return file_info

def dir_check(path, folder_name, length):

    if os.path.isdir(path+'/'+folder_name) is False:
        return False
    if len(folder_name) != length:
        return False
    try:
        year_value = int(folder_name[0:4])
        month_value = int(folder_name[4:6])
        if year_value < 2020 or year_value > 2050:
            return False
        if month_value < 1 or month_value > 12:
            return False
        if length == 8:
            day_value = int(folder_name[6:8])
            if day_value < 1 or day_value > 31:
                return False
        return True
    except ValueError:
        return False

def plot_data(time_data, diagnostic_data, figure_path, sub_path):

    plt.ion()
    fig, axs = plt.subplots(N_DATA_ROWS, sharex=True)
    fig.suptitle(PLOT_BASE_TITLE+sub_path)

    for item in DATA_FORMAT:
        row = DATA_FORMAT[item]['row']
        label = DATA_FORMAT[item]['label']
        axs[row].plot(time_data, diagnostic_data[row], 'o')
        axs[row].set_ylabel(label, fontsize=8, labelpad=10)
        if row < (N_DATA_ROWS-1):
            axs[row].tick_params('x', labelbottom=False)
        else:
            axs[row].tick_params('x', labelsize=8)
    plt.show(block=False)
    plt.savefig(figure_path + '/' + sub_path + '.png')
    plt.close()

def daily_data(path, out_path):
    mrr_folders = []
    # data_array = np.empty((N_DATA_VALS, 0), dtype=np.uint16)
    # time_array = np.empty(0, dtype='datetime64[s]')

    folders = sorted([folder for folder in os.listdir(path) if dir_check(path, folder, 6)])
    for folder in folders:
        try:
            os.mkdir(out_path+'/'+folder)
        except FileExistsError:
            pass
        current_path = os.path.join(path, folder)
        sub_folders = [sub_folder for sub_folder in os.listdir(current_path) if dir_check(current_path, sub_folder, 8)]
        sub_folders.sort()
        mrr_folders.extend(['/' + folder + '/' + sub_folder for sub_folder in sub_folders])

    num_folders = len(mrr_folders)
    for j in tqdm(range(num_folders)):
        sub_path = mrr_folders[j]
        current_path = path + sub_path
        file_list = sorted([file for file in os.listdir(current_path) if is_valid_data_file(current_path + '/' + file)])
        data_block = np.empty((N_DATA_ROWS, len(file_list)), dtype=np.uint16)
        time_block = np.empty(len(file_list), dtype='datetime64[s]')
        for i, file in enumerate(file_list):
            file_data = read_nc_diagnostics(current_path, file)
            data_block[:, i] = file_data.data[:]
            time_block[i] = np.datetime64('1970-01-01') + np.timedelta64(file_data.time, 's')
        plot_data(time_block, data_block, out_path, sub_path)
        # data_array = np.concatenate((data_array, data_block), axis=1)
        # time_array = np.concatenate((time_array, time_block), axis=0)
    # return data_array, time_array

def config_okay(config_data):
    try:
        if os.path.isdir(config_data[CONFIG_PATHS][CONFIG_INPUT_PATH]) is False:
            return False
        if os.path.isdir(config_data[CONFIG_PATHS][CONFIG_INPUT_PATH]) is False:
            os.mkdir(config_data[CONFIG_PATHS][CONFIG_OUTPUT_PATH])
    except KeyError:
        return False
    except FileNotFoundError:
        return False
    return True

if __name__ == "__main__":

    config = configparser.ConfigParser()
    config.read(CONFIG_FILENAME)

    if config_okay(config) is False:
        sys.exit()

    data_path = config[CONFIG_PATHS][CONFIG_INPUT_PATH]
    save_path = config[CONFIG_PATHS][CONFIG_OUTPUT_PATH]

    daily_data(data_path, save_path)


#!/usr/bin/env python3
# *-* coding: utf-8 *-*
"""
Basic metadata handling
=======================

"""
import reda

###############################################################################
ert = reda.ERT()

ert.metadata['measurement_device'] = 'IRIS Syscal Pro 48 ch'
ert.metadata['person_responsible'] = 'Maximilian Weigand'
ert.metadata['nr_electrodes'] = 48
ert.metadata['electrode_spacing'] = 1
# lets add a subgroup containing device-specific information
ert.metadata['device_specific'] = {
    'max_current': 2,
    'memory_block': 2567,
}
print(ert.metadata)

# save metadata
ert.save_metadata('metadata.json')

###############################################################################
ert1 = reda.ERT()
ert1.load_metadata('metadata.json')
print(ert1.metadata)


from typing import List, NamedTuple

INF = float("inf")

class Edge(NamedTuple):
    to: int
    w: int

Graph = List[List[Edge]]

def main() -> None:
    n, m, s = map(int, input().split())

    g: Graph = [[] for _ in range(n)]
    for _ in range(m):
        a, b, w = map(int, input().split())
        g[a].append(Edge(to=b, w=w))

    # Bellman-Ford
    # flag of whether hav negative cycle or not
    exist_nagative_cycle = False
    dist = [INF] * n
    dist[s] = 0

    for i in range(n):
        update = False
        for v in range(n):
            if dist[v] == INF:
                continue
            for e in g[v]:
                dist[e.to] = min(dist[e.to], dist[v] + e.w)
                if dist[e.to] == dist[v] + e.w:
                    update = True
        if not update:
            break

        if i == n - 1 and update:
            exist_nagative_cycle = True

    if exist_nagative_cycle:
        print("NEGATIVE_CYCLE")

    else:
        for v in range(n):
            if dist[v] < INF:
                print(dist[v])

            else:
                print("INF")


# def wab():
#     def revers():
#         a="1234abcd"
#         i=-1
#         while i>=-(len(a)):
#             print(a[i],end="")
#             i-=1
#     revers()
# wab()

# output=dcba4321

#
a=[1,2,3,4]
i=-1
while i>=-len(a):
    print(a[i],end=" ")
    i=i-1


from advent import Solver

# https://adventofcode.com/2022/day/3
# the first half of the line was put in a set only for helping with the second part of the puzzle where set.intersection
# was practically the solution to the puzzle.
# The first part was probably fine using "letter in line"; with the size of inputs, lookup in a set vs going through
# a sequence would have no perceivable difference in performance.
#
# score formulas for the letters according to the puzzle.
# uppercase letter: ord(ch) - 'A' + 27
UPPER = 27 - ord('A')
# uppercase letter: ord(ch) - 'a' + 1
LOWER = 1 - ord('a')

class Solution(Solver):
    def __init__(self):
        self.count = 0
        self.count2 = 0
        self.possible_badges = None
        self.row = 0

    def parse(self, line):
        self.row += 1
        # assuming this is even
        mid = int(len(line) / 2)
        first_half_items = set(line[0:mid])
        failed_item = None
        for c in line[mid:]:
            if c in first_half_items:
                failed_item = c
                break
        self.count += ord(failed_item) + (LOWER if failed_item.islower() else UPPER)
        # possible badges in every triplet
        if self.row % 3 == 1:
            self.possible_badges = set(line)
        else:
            self.possible_badges.intersection_update(set(line))
        if self.row % 3 == 0:
            assert len(self.possible_badges) == 1
            badge = self.possible_badges.pop()
            self.count2 += ord(badge) + (LOWER if badge.islower() else UPPER)

    def solve(self):
        print(f"[1] Priority of item in both compartments {self.count}")
        print(f"[2] Overall priority of badges {self.count2}")


""" RAML parsing library """
try:
    from .version import __version__, version_info
except ImportError:
    version_info = (0, 0, 0)
    __version__ = '{}.{}.{}'.format(*version_info)

authors = (
    ('Alexander Vasin', 'hi@alvass.in'),
)

authors_email = ", ".join(
    '{}'.format(email) for _, email in authors
)

__license__ = 'MIT',
__author__ = ", ".join(
    '{} <{}>'.format(name, email) for name, email in authors
)

# It's same persons right now
__maintainer__ = __author__

__all__ = (
    '__author__', '__author__', '__license__',
    '__maintainer__', '__version__',
    'version_info',
)


import json

def format_ret(return_set, as_json=False):
    """ decouple, allow for modifications to return type
        returns a list of ip addresses in object or json form """
    ret_list = list()
    for item in return_set:
        d = {"ip": item}
        ret_list.append(d)

    if as_json:
        return json.dumps(ret_list)

    return ret_list

def unassigned(data, as_json=False):
    """ https://sendgrid.com/docs/API_Reference/api_v3.html#ip-addresses
        The /ips rest endpoint returns information about the IP addresses
        and the usernames assigned to an IP

        unassigned returns a listing of the IP addresses that are allocated
        but have 0 usera assigned

        data (response.body from sg.client.ips.get())
        as_json False -> get list of dicts
                True  -> get json object

        example:
        sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))

        params = {'subuser': 'test_string', 'ip': 'test_string', 'limit': 1, 'exclude_whitelabels': 'true', 'offset': 1}
        response = sg.client.ips.get(query_params=params)
        if response.status_code == 201:
           data = response.body
           unused = unassinged(data) """

    no_subusers = set()

    if not isinstance(data, list):
        return format_ret(no_subusers, as_json=as_json)

    for current in data:
        num_subusers = len(current["subusers"])
        if num_subusers == 0:
            current_ip = current["ip"]
            no_subusers.add(current_ip)

    ret_val = format_ret(no_subusers, as_json=as_json)
    return ret_val


import abc
import os
from pprint import pprint
import hydra
from omegaconf import DictConfig, OmegaConf, open_dict
import pdb

import common.base_agent as base_agent
from env_wrapper.chickai_env_wrapper import ChickAIEnvWrapper
import common.logger as logger

class Experiment(abc.ABC):
    def __init__(self, config):
        self.env_config = config["Environment"]
        agent_config = config["Agent"]
        agent_count = config["agent_count"]

        run_id = config["run_id"]
        self.mode = config["mode"]

        self.reward = agent_config["reward"]
        self.rewarded = True if self.reward.lower() == "supervised" else False

        self.log_path = config["log_path"]
        self.test_eps = config["test_eps"]
        self.train_eps = config["train_eps"]
        self.agents = []

        for i in range(agent_count):
            with open_dict(agent_config):
                agent_config.agent_id = f"{run_id}_Agent_{i}"
                agent_config.env_log_path = self.env_config['log_path']
                agent_config.rec_path = os.path.join(self.env_config["rec_path"], agent_config.agent_id)
                agent_config.recording_frames = self.env_config["recording_frames"]
            self.agents.append(self.new_agent(agent_config))

    def train_agents(self):
        """
        Function to build training configuration,
        generate environments and start training
        """
        for agent in self.agents:
            env_config = self.env_config
            with open_dict(env_config):

                mode = "rest"
                env_config["mode"] = self.generate_mode_parameter(mode,env_config)
                env_config["random_pos"] = True

                if self.rewarded:
                    env_config["rewarded"] = self.rewarded

                env_config["run_id"] = agent.id + "_" + "train"
                env_config["rec_path"] = agent.rec_path + "/"
                env_config["log_title"] = self.generate_log_title(env_config)

            env = self.generate_environment(env_config)
            agent.train(env, self.train_eps)
            agent.save()
            env.close()

    def test_agents(self,mode):
        for agent in self.agents:
            env_config = self.env_config
            with open_dict(env_config):

                env_config["mode"] = self.generate_mode_parameter(mode, env_config)
                env_config["run_id"] = agent.id + "_" + mode

                if self.rewarded:
                    env_config["rewarded"] = self.rewarded
                env_config["reward"] = self.reward

                env_config["log_title"] = self.generate_log_title(env_config)
            env = self.generate_environment(env_config)
            agent.test(env, self.test_eps, mode)
            env.close()

    def run(self):
        if self.mode == "train":
            self.train_agents()
            ## rest inside the test
        elif self.mode == "test":
            self.test_agents("test")
        elif self.mode == "full":
            self.train_agents()
            self.test_agents("test")
        else:
            self.test_agents("test")

    @abc.abstractmethod
    def generate_environment(self, env_config):
        pass

    def new_agent(self, config):
        return base_agent.BaseAgent(**config)

    @abc.abstractmethod
    def generate_mode_parameter(self, mode, env_config):
        pass

    @abc.abstractmethod
    def generate_log_title(self, env_config):
        pass


import pygame

cubeSize = 20
cubeSpeed = 20

class Block:
    def __init__(self, points, color,cubeSize,cubeSpeed):
        self.points = points
        self.color = color
        self.cubeSize = cubeSize
        self.cubeSpeed = cubeSpeed

    @staticmethod
    def create_long_block():
        points = [(0, 0), (0, 1), (0, 2), (0, 3)]
        color = (0, 255, 0)
        return Block(points, color)

    @staticmethod
    def create_square_block():
        points = [(0, 0), (0, 1), (1, 0), (1, 1)]
        color = (0, 0, 255)
        return Block(points, color)

    @staticmethod
    def create_l_block():
        points = [(0, 0), (0, 1), (0, 2), (1, 2)]
        color = (255, 0, 0)
        return Block(points, color)

def draw_block_to_display(x, y, block):
    for point in block.points:
        pygame.draw.rect(screen, block.color, (x + point[0] * 25, y + point[1] * 25, 25, 25))

def draw_grid_to_display(x, y):
    for row in range(19):
        for col in range(19):
            pygame.draw.rect(screen, color, (x + col * 25, y + row * 25, 25, 25))
        y += 25
    x += 25
    pygame.display.update()

def control_block(block):
    input = pygame.key.get_pressed()
    if input[pygame.K_d]:

        block.move_right()
    if input[pygame.K_a]:
        block.move_left()

    return block

pygame.init()

screen = pygame.display.set_mode((500, 500))
color = (255, 255, 255)

block = Block.create_long_block()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameOver = True

            # Check for key presses

    block = control_block(block)
    screen.fill(color)
    draw_block_to_display(25, 25, block)
    pygame.display.update()


from flask import Flask
import logging
import requests

app = Flask(__name__)

@app.route('/', methods=["GET"])

def hello_world():
 prefix_google = """
 <!-- Google tag (gtag.js) -->
<script async
src="https://www.googletagmanager.com/gtag/js?id=UA-250395355-2"></script>
<script>
 window.dataLayer = window.dataLayer || [];
 function gtag(){dataLayer.push(arguments);}
 gtag('js', new Date());
 gtag('config', 'UA-250395355-2');
</script>
 """
 return prefix_google + "Hello World"

@app.route('/logger', methods=["GET"])

def log():
    print('Info level log python.')
    logString = """
<script>
 console.log("Info level log JavaScript.")
</script>
<br>
<input type="text" name="Textbox" value="This is a textbox">
    """
    return "This is the logger page." + logString

@app.route('/cookies', methods=["GET"])

def cookies():
    #req = requests.get("https://www.google.com/")
    #googleCookies = req.cookies.get_dict()

    req = requests.get("https://analytics.google.com/analytics/web/?hl=fr#/report-home/a250395355w344281288p280864294")
    googleCookies = req.text
    return googleCookies

@app.route('/cookies/auth', methods=["GET", "POST"])

def googleAuth():
    #After a lot of research, I could not manage to find a way to do it so this is a fake result below
    # "#Fake it 'til you make it"
    return "Number of visitors fetched from ganalytics: 1"


import matplotlib
matplotlib.use('Agg')
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch import optim
import torch.nn.functional as F
import torch.utils.data as Data
import numpy as np
from skimage import io as skio
from skimage.transform import resize

import os
import matplotlib.pyplot as plt

use_cuda = torch.cuda.is_available()

import re
import time
import random
import sentences_process
from pprint import pprint

EPOCHS = 300
BATCH_SIZE = 256
NOISE_SIZE = 100
LEARNING_RATE = 0.0002
LEARNING_RATE_BETA = (0.5, 0.999)
USE_EXTRA_DATA = True
#USE_ADDITIVE_NOISE = True
wd_hair = sentences_process.Word_dict(one_hot=True)
wd_eyes = sentences_process.Word_dict(one_hot=True)

def save_imgs(generator, _testing_sents, _iter):
    r, c = 5, 5
    noise = Variable(torch.from_numpy(np.random.normal(0, 1, (r * c, NOISE_SIZE)))).float()
    sents = Variable(torch.from_numpy(_testing_sents)).float()
    if use_cuda:
        noise = noise.cuda()
        sents = sents.cuda()
    # gen_imgs should be shape (25, 64, 64, 3)
    gen_imgs = generator(noise, sents).transpose(1, 2).transpose(2, 3)
    fig, axs = plt.subplots(r, c)
    cnt = 0
    for i in range(r):
        for j in range(c):
            axs[i,j].imshow((gen_imgs[cnt].cpu().data.numpy() + 1.0) / 2.0)
            axs[i,j].axis('off')
            cnt += 1
    fig.savefig("output_imgs/output_%d.png" % _iter)
    plt.close()

def get_train_loader(images_path, tags_path, ex_images_path='', ex_tags_path=''):
    print('Start pack to DataLoader... ', end='')
    pre_tags = [re.split(',|\t', l.replace('\n', ' ')) for l in open(tags_path, 'r').readlines()]
    tags = [[i[0], [s.split(':')[0] for s in i if 'hair' in s and 'long' not in s and 'short' not in s], [s.split(':')[0] for s in i if 'eyes' in s]] for i in pre_tags]
    #tags = [i for i in tags if [] not in i]
    tags = [[i[0], i[1] if i[1] != '' else '{UNK}', i[2] if i[2] != '' else '{UNK}'] for i in tags]
    tags = [[i[0], ' '.join([j.split(' ')[0] for j in i[1]]), ' '.join([j.split(' ')[0] for j in i[2]])] for i in tags]

    hair_sents = [t[1] for t in tags]
    eyes_sents = [t[2] for t in tags]
    wd_hair.add_sentences(hair_sents)
    wd_eyes.add_sentences(eyes_sents)
    tmp_tags = [[t[0], np.concatenate((wd_hair.sentence2onehot(t[1]), wd_eyes.sentence2onehot(t[2])))] for t in tags]
    xs = np.array([resize(skio.imread(images_path + '/' + tag[0] + '.jpg') / 255.0, (64, 64)) for tag in tags], dtype=np.float32)
    ys = np.array([t[1] for t in tmp_tags])
    if USE_EXTRA_DATA:
        pre_tags_ = [re.split(',|\t', l.replace('\n', ' ')) for l in open(ex_tags_path, 'r').readlines()]
        tags_ = [[i[0], [s.split(':')[0] for s in i if 'hair' in s and 'long' not in s and 'short' not in s], [s.split(':')[0] for s in i if 'eyes' in s]] for i in pre_tags_]
        #tags_ = [i for i in tags_ if [] not in i]
        tags_ = [[i[0], i[1] if i[1] != '' else '{UNK}', i[2] if i[2] != '' else '{UNK}'] for i in tags_]
        tags_ = [[i[0], ' '.join([j.split(' ')[0] for j in i[1]]), ' '.join([j.split(' ')[0] for j in i[2]])] for i in tags_]
        hair_sents_ = [t[1] for t in tags_]
        eyes_sents_ = [t[2] for t in tags_]
        wd_hair.add_sentences(hair_sents_)
        wd_eyes.add_sentences(eyes_sents_)
        tags_ = [[t[0], np.concatenate((wd_hair.sentence2onehot(t[1]), wd_eyes.sentence2onehot(t[2])))] for t in tags_]
        xs_ = np.array([resize(skio.imread(ex_images_path + '/' + tag_[0] + '.jpg') / 255.0, (64, 64)) for tag_ in tags_], dtype=np.float32)
        ys_ = np.array([t[1] for t in tags_])
        xs = np.concatenate((xs, xs_), axis=0)
        # Dict updated, reprocess ys
        tmp_tags = [[t[0], np.concatenate((wd_hair.sentence2onehot(t[1]), wd_eyes.sentence2onehot(t[2])))] for t in tags]
        ys = np.array([t[1] for t in tmp_tags])
        ys = np.concatenate((ys, ys_), axis=0)

    xs = torch.from_numpy(xs * 2.0 - 1.0).transpose(2, 3).transpose(1, 2)
    ys = torch.from_numpy(ys)
    wd_hair.save_dict('dict_hair.txt') #len:16
    wd_eyes.save_dict('dict_eyes.txt') #len:16

    torch_dataset = Data.TensorDataset(data_tensor=xs, target_tensor=ys)
    train_loader = Data.DataLoader(
        dataset=torch_dataset,
        batch_size=BATCH_SIZE,
        shuffle=True,
        num_workers=2
    )
    print('Done!')
    onehot_len = len(wd_hair.w2n) + len(wd_eyes.w2n)
    return train_loader, onehot_len

def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)

class Generator(nn.Module):
    def __init__(self, onehot_len):
        super(Generator, self).__init__()
        self.sents_linear = nn.Linear(onehot_len, 32)
        self.sents_selu = nn.SELU(True)

        self.prelinear = nn.Linear(NOISE_SIZE, 8192)
        self.preselu = nn.SELU(True)

        self.conv1 = nn.ConvTranspose2d(512 + 2, 256, 4, 2, 1, bias = False)
        self.batch1 = nn.BatchNorm2d(256)
        self.selu1 = nn.SELU(True)
        self.conv2 = nn.ConvTranspose2d(256, 128, 4, 2, 1, bias = False)
        self.batch2 = nn.BatchNorm2d(128)
        self.selu2 = nn.SELU(True)
        self.conv3 = nn.ConvTranspose2d(128, 64, 4, 2, 1, bias = False)
        self.batch3 = nn.BatchNorm2d(64)
        self.selu3 = nn.SELU(True)
        self.conv4 = nn.ConvTranspose2d(64, 3, 4, 2, 1, bias = False)
        self.out = nn.Tanh()

    def forward(self, input, sents):
        sents_output = self.sents_selu(self.sents_linear(sents))
        sents_output = sents_output.view(input.size(0), 2, 4, 4)

        output = self.preselu(self.prelinear(input))
        output = output.view(input.size(0), 512, 4, 4)

        output = torch.cat((output, sents_output), 1)
        output = self.selu1(self.batch1(self.conv1(output)))
        output = self.selu2(self.batch2(self.conv2(output)))
        output = self.selu3(self.batch3(self.conv3(output)))
        output = self.out(self.conv4(output))
        return output

class Discriminator(nn.Module):
    def __init__(self, onehot_len):
        super(Discriminator, self).__init__()
        self.sents_linear = nn.Linear(onehot_len, 3 * 64 * 64)
        self.sents_selu1 = nn.SELU(True)
        self.sents_conv = nn.Conv2d(3, 1, 4, 2, 1, bias = False)
        self.sents_batch = nn.BatchNorm2d(1)
        self.sents_selu2 = nn.SELU(True)

        self.conv1 = nn.Conv2d(3, 32, 4, 2, 1, bias = False)
        self.batch1 = nn.BatchNorm2d(32)
        self.selu1 = nn.SELU(True)

        self.conv2 = nn.Conv2d(32 + 1, 64, 4, 2, 1, bias = False)
        self.batch2 = nn.BatchNorm2d(64)
        self.selu2 = nn.SELU(True)
        self.conv3 = nn.Conv2d(64, 128, 4, 2, 1, bias = False)
        self.batch3 = nn.BatchNorm2d(128)
        self.selu3 = nn.SELU(True)
        self.conv4 = nn.Conv2d(128, 256, 4, 2, 1, bias = False)
        self.batch4 = nn.BatchNorm2d(256)
        self.selu4 = nn.SELU(True)
        self.linear = nn.Linear(4096, 1)
        self.out = nn.Sigmoid()

    def forward(self, input, sents):
        sents_output = self.sents_selu1(self.sents_linear(sents)).view(input.size()[0], 3, 64, 64)
        sents_output = self.sents_selu2(self.sents_batch(self.sents_conv(sents_output)))

        output = self.selu1(self.batch1(self.conv1(input)))

        output = torch.cat((output, sents_output), 1)
        output = self.selu2(self.batch2(self.conv2(output)))
        output = self.selu3(self.batch3(self.conv3(output)))
        output = self.selu4(self.batch4(self.conv4(output)))
        output = output.view(input.size()[0], -1)
        output = self.linear(output)
        output = self.out(output)
        return output.view(-1)

def train(_iter, _generator, _discriminator, _optimG, _optimD, _criterion, _train_loader, onehot_len):
    errD_all = Variable(torch.zeros(1))
    errG_all = Variable(torch.zeros(1))
    for idx, (_xs, _ys) in enumerate(_train_loader):
        # Train Discriminator
        _discriminator.zero_grad()
        reals = Variable(_xs)
        sents = Variable(_ys).float()
        labels = Variable(torch.ones(_xs.size()[0]))
        #additive_noise = Variable(torch.FloatTensor(_xs.size()[0], 3, 96, 96))

        if use_cuda:
            reals = reals.cuda()
            sents = sents.cuda()
            labels = labels.cuda()
            #additive_noise = additive_noise.cuda()
        #if USE_ADDITIVE_NOISE:
        #    additive_noise.data.resize_(reals.size()).normal_(0, 0.005)
        #    reals.data.add_(additive_noise.data)
        output = _discriminator(reals, sents)
        errD_real = _criterion(output, labels)

        fake_sents = Variable(torch.from_numpy(np.random.randint(2, size=(sents.size()[0] * onehot_len)))).float().view(sents.size()[0], -1)
        wrong_labels = Variable(torch.zeros(_xs.size()[0]))
        if use_cuda:
            fake_sents = fake_sents.cuda()
            wrong_labels = wrong_labels.cuda()
        output = _discriminator(reals, fake_sents)
        errD_wrong = _criterion(output, wrong_labels)

        noise = torch.from_numpy(np.random.normal(0, 1, (_xs.size()[0], NOISE_SIZE)))
        #noise = torch.randn(_xs.size()[0], NOISE_SIZE)
        noise = Variable(noise).float()
        fakes_labels = Variable(torch.zeros(_xs.size()[0]))
        if use_cuda:
            noise = noise.cuda()
            fakes_labels = fakes_labels.cuda()
        fakes = _generator(noise, sents)
        output = _discriminator(fakes.detach(), sents)
        errD_fake = _criterion(output, fakes_labels)

        errD_all = errD_real + errD_fake + errD_wrong
        errD_all.backward()
        _optimD.step()
        #for p in _discriminator.parameters():
        #    p.data.clamp_(-0.05, 0.05)

        # Train Generator
        _generator.zero_grad()
        fakes_labels = Variable(torch.ones(_xs.size()[0]))
        if use_cuda:
            fakes_labels = fakes_labels.cuda()
        fakes = _generator(noise, sents)
        output = _discriminator(fakes, sents)
        errG = _criterion(output, fakes_labels)
        errG_all = errG
        errG_all.backward()
        _optimG.step()

    return errD_all.data[0], errG_all.data[0]

def trainIters(_generator, _discriminator, _optimG, _optimD, _criterion, _train_loader, _testing_sents, onehot_len, print_every=1):
    print('Start training...')
    start = time.time()

    for iter in range(1, EPOCHS + 1):
        lossD, lossG = train(iter, _generator, _discriminator, _optimG, _optimD, _criterion, _train_loader, onehot_len)

        if iter % print_every == 0:
            avg_epoch_time = (time.time() - start) / print_every
            start = time.time()
            print('Epoch: %d, Avg_Epoch_Time: %.4f, LossD: %.4f, LossG: %.4f' % (iter, avg_epoch_time, lossD, lossG))
        if iter % 50 == 0:
            save_imgs(_generator, _testing_sents, iter)
            torch.save(generator, 'generator.pt')
            torch.save(discriminator, 'discriminator.pt')

train_loader, onehot_len = get_train_loader('AnimeDataset/faces/', 'AnimeDataset/tags_clean.csv', 'AnimeDataset/extra_data/images/', 'AnimeDataset/extra_data/tags.csv')
testing_data = [l.split(',')[1].replace('\n', '').split(' ') for l in list(open('AnimeDataset/testing_tags.txt').readlines())]
testing_hair = [i[0] for i in testing_data]
testing_eyes = [i[2] for i in testing_data]
testing_sents = np.array([np.concatenate((wd_hair.sentence2onehot(testing_hair[i]), wd_eyes.sentence2onehot(testing_eyes[i]))) for i in range(len(testing_data))])

generator = Generator(onehot_len)
generator.apply(weights_init)
discriminator = Discriminator(onehot_len)
discriminator.apply(weights_init)

if use_cuda:
    generator = generator.cuda()
    discriminator = discriminator.cuda()

criterion = nn.BCELoss()
optimizerG = optim.Adam(generator.parameters(), lr = LEARNING_RATE, betas = LEARNING_RATE_BETA)
optimizerD = optim.Adam(discriminator.parameters(), lr = LEARNING_RATE, betas = LEARNING_RATE_BETA)

trainIters(generator, discriminator, optimizerG, optimizerD, criterion, train_loader, testing_sents, onehot_len)