import logging

from PySide2 import QtCore, QtWidgets

from hyo2.abc.lib.gdal_aux import GdalAux
from hyo2.soundspeedmanager.dialogs.dialog import AbstractDialog
from hyo2.soundspeedmanager.dialogs.export_profile_metadata_fields_dialog import ExportProfileMetadataFieldsDialog
from hyo2.soundspeed.db.export import ExportDbFields

logger = logging.getLogger(__name__)

class ExportProfileMetadataDialog(AbstractDialog):

    def __init__(self, main_win, lib, parent=None):
        AbstractDialog.__init__(self, main_win=main_win, lib=lib, parent=parent)

        self.fmt_outputs = list()

        self.setWindowTitle("Export metadata profiles")
        self.setMinimumWidth(160)

        # outline ui
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.setLayout(self.mainLayout)

        # label
        hbox = QtWidgets.QHBoxLayout()
        self.mainLayout.addLayout(hbox)
        hbox.addStretch()
        label = QtWidgets.QLabel("Select output formats:")
        hbox.addWidget(label)
        hbox.addStretch()
        # buttons
        hbox = QtWidgets.QHBoxLayout()
        self.mainLayout.addLayout(hbox)
        hbox.addStretch()
        # noinspection PyUnresolvedReferences
        self.buttonBox = QtWidgets.QDialogButtonBox(QtCore.Qt.Vertical)
        hbox.addWidget(self.buttonBox)
        hbox.addStretch()
        # add buttons
        for fmt in GdalAux.ogr_formats.keys():
            btn = QtWidgets.QPushButton("%s" % fmt)
            btn.setCheckable(True)
            self.buttonBox.addButton(btn, QtWidgets.QDialogButtonBox.ActionRole)
            btn.setToolTip("Select %s format [*.%s]" % (fmt, ", *.".join(GdalAux.ogr_exts[fmt])))

        # noinspection PyUnresolvedReferences
        self.buttonBox.clicked.connect(self.on_select_btn)

        # option for opening the output folder
        settings = QtCore.QSettings()
        export_filter_fields = settings.value("export_filter_fields")
        if export_filter_fields is None:
            settings.setValue("export_filter_fields", False)
        hbox = QtWidgets.QHBoxLayout()
        self.mainLayout.addLayout(hbox)
        hbox.addStretch()
        self.filterFields = QtWidgets.QCheckBox('Filter fields', self)
        self.filterFields.setChecked(settings.value("export_filter_fields") == 'true')
        hbox.addWidget(self.filterFields)
        hbox.addStretch()

        self.mainLayout.addSpacing(16)

        # export
        hbox = QtWidgets.QHBoxLayout()
        self.mainLayout.addLayout(hbox)
        hbox.addStretch()
        btn = QtWidgets.QPushButton("Export data")
        btn.setMinimumHeight(36)
        hbox.addWidget(btn)
        # noinspection PyUnresolvedReferences
        btn.clicked.connect(self.on_export_btn)
        hbox.addStretch()

    def on_select_btn(self, btn):
        logger.debug("%s -> %s" % (btn.text(), btn.isChecked()))
        fmt = btn.text()

        if btn.isChecked():
            self.fmt_outputs.append(fmt)
        else:
            if fmt in self.fmt_outputs:
                self.fmt_outputs.remove(fmt)

    def on_export_btn(self):
        logger.debug("export clicked")
        if len(self.fmt_outputs) == 0:
            msg = "Select output formats before metadata export!"
            # noinspection PyCallByClass,PyArgumentList
            QtWidgets.QMessageBox.warning(self, "Export warning", msg, QtWidgets.QMessageBox.Ok)
            return

        filter_fields = None
        if self.filterFields.isChecked():
            logger.debug("user want to filter the fields in output")
            filter_fields = ExportDbFields()
            settings = QtCore.QSettings()

            for ff in filter_fields.fields.keys():
                filter_fields.fields[ff] = settings.value("export_field_%s" % ff, "true") == "true"

            dlg = ExportProfileMetadataFieldsDialog(filter_fields=filter_fields, parent=self)
            ret = dlg.exec_()
            if ret:
                logger.debug("fields: %s" % (dlg.filter_fields.fields, ))

                filter_fields = dlg.filter_fields
                for ff in filter_fields.fields.keys():
                    settings.setValue("export_field_%s" % ff, filter_fields.fields[ff])

        # actually do the export
        self.progress.start()
        success = True

        # try:

        for fmt in self.fmt_outputs:
            ret = self.lib.export_db_profiles_metadata(ogr_format=GdalAux.ogr_formats[fmt],
                                                       filter_fields=filter_fields)
            if not ret:
                success = False
                # noinspection PyCallByClass,PyArgumentList
                QtWidgets.QMessageBox.critical(self, "Database", "Unable to export as %s!" % fmt)

        # except RuntimeError as e:
        #     self.progress.end()
        #     msg = "Issue in exporting the metadata.\nReason: %s" % e
        #     QtWidgets.QMessageBox.critical(self, "Export error", msg, QtWidgets.QMessageBox.Ok)
        #     return

        if success:
            self.lib.open_outputs_folder()

        self.progress.end()
        self.accept()


import random
import numpy as np
import matplotlib.pyplot as plt

def coin_flip():
    return random.randint(0,1)

def monte_carlo(n):
    #n defines the coin flipping times
    Probability = []
    Result = 0
    for i in range(n):
        if coin_flip() == 1:
            Result += 1
        else:
            Result = Result
        Probability.append(Result/(i+1))

    plt.axhline(y = 0.5, color = 'r', linestyle = '-')
    plt.xlabel('Flip')
    plt.ylabel('Probability')
    plt.plot(Probability)
    plt.show(block=False)

    return Probability[-1]

if __name__ == "__main__":
    m = 5
    n = 5000
    Probability_sum = 0
    for i in range(m):
        Probability_sum += monte_carlo(n)
    final_Probability = Probability_sum/m

    print('After {} coin flipping, the probability of head is {}'.format(m*n, final_Probability))


from rdkit.Chem import AllChem
from rdkit import Chem
from rdkit import RDLogger
from pathlib import Path
import glob, sys
import argparse
RDLogger.DisableLog('rdApp.*')
import pickle

import logging

def set_file_logger(filename: str, name: str = 'candle', level: int = logging.DEBUG, format_string = None):
    """Add a stream log handler.

    Args:
        - filename (string): Name of the file to write logs to
        - name (string): Logger name
        - level (logging.LEVEL): Set the logging level.
        - format_string (string): Set the format string

    Returns:
       -  None
    """
    if format_string is None:
        format_string = "%(asctime)s.%(msecs)03d %(name)s:%(lineno)d [%(levelname)s]  %(message)s"

    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    handler = logging.FileHandler(filename)
    handler.setLevel(level)
    formatter = logging.Formatter(format_string, datefmt='%Y-%m-%d %H:%M:%S')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger

def process_files(smile_file, out_file, log_file, index_start, batchsize, pickle_out, debug=False):
    import time
    import shutil
    import os
    start = time.time()
    import logging
    import pickle

    from rdkit.Chem import AllChem

    count = 0
    bad_count = 0
    logger = set_file_logger(log_file, level=logging.DEBUG if debug else logging.INFO)
    logger.info(f"Running fingerprint on {smile_file} from index_start:{index_start}")

    tmp_out_file = '/dev/shm/{}'.format(os.path.basename(out_file))

    logger.info(f"Writing output temporarily to {tmp_out_file}")
    smiles = []
    with open(smile_file) as current:
        current.seek(index_start)
        smiles = [current.readline() for i in range(batchsize)]

    with open(tmp_out_file, 'w') as out_handle:
        if pickle_out:
            outputs = []
        for line in smiles:
            smile, *remainder = line.split()
            try:
                logger.debug(f"Processing smile {smile}")
                fprint = AllChem.GetMorganFingerprintAsBitVect(Chem.MolFromSmiles(smile), 2, nBits=2048).ToBase64()
            except:
                logger.exception("Caught exception")
                fprint = None
                bad_count += 1
            id = remainder[0] if remainder else ''
            if pickle_out:
                outputs += [(smile, id, fprint)]
            else:
                print('{},{},{}'.format(smile, id, fprint), file=out_handle)
            count += 1

    if pickle_out:
        with open(tmp_out_file, 'wb') as f:
            pickle.dump(outputs, f)
    shutil.move(tmp_out_file, out_file)

    logger.info("Bad smile count {}".format(bad_count))
    logger.info("Completed {} smiles from {} in {:8.3f}s".format(count,
                                                                 smile_file,
                                                                 time.time() - start))
    logger.handlers.pop()
    return out_file

def main(argv):
    parser = argparse.ArgumentParser(description='Program to process Moyer maps')
    parser.add_argument('-d', '--debug', help='Debug level', required=False, type=int, choices=[0, 1, 2])
    parser.add_argument('-P', '--profile', help='Profile code', required=False, default=False, action='store_true')
    parser.add_argument('-i', '--input', help='Input file', required=True)
    args = parser.parse_args()

    # Default debug level is 1: set -d 0 for no info at all, -d 2 for verbose info
    if args.debug != None:
        global debug
        debug = args.debug

    if args.profile:
        pr = cProfile.Profile()
        pr.enable()

    if args.input == 'Enamine':
        files = glob.glob('SMILEs/Enamine_Real_SMILEs/2019q3-4_Enamine_REAL_01_canonical_only.smi')
        sep   = '\t'
        process_files(files, sep, None)
    elif args.input == 'pubchem':
        files = ['SMILEs/pubchem_canonical.csv']
        sep   = ','
        process_files(files, sep, 'pubchem')

    if args.profile:
        pr.disable()
        s = io.StringIO()
        sortby = SortKey.CUMULATIVE
        ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
        ps.print_stats()
        print(s.getvalue())

if __name__ == '__main__':
    process_files('/projects/CVD_Research/foster/SMILEs/Enamine_Real_SMILEs/2019q3-4_Enamine_REAL_16_canonical_only.smi',
                  '/projects/candle_aesp/yadu/covid_fingerprinting/test.csv',
                  '/projects/candle_aesp/yadu/covid_fingerprinting/test.log',
                  251,
                  5,
                  debug=True)
    #main(sys.argv[1:])


escape_word =["a", "an", "the", "am" ,"is", "are", "and", "of", "in" , "on", "with", "from", "to"]

def capitalise(phrase):
    word=""
    cap = phrase.split()
    for x in cap:
        if x not in escape_word:
            word += (" " + x.title())
        else:
            word += (" " + x)

    return word

print(capitalise("I am an educator and a researcher"))
print(capitalise("big data is the future of information technology"))
print(capitalise("He wants to have breakfast with her in the hotel"))


import datetime
import json
import os
import discord

from discord.ext import commands
from discord.ext.commands import Context

from essentials.utils_numunit import format_number_with_units

from essentials.reset_system import reset_system

class YesOrNo(discord.ui.View):
    def __init__(self, author):
        super().__init__()
        self.value = None
        self.author = author

    @discord.ui.button(label="네", style=discord.ButtonStyle.blurple)
    async def confirm(
        self, button: discord.ui.Button, interaction: discord.Interaction
    ):
        self.value = True
        self.stop()

    @discord.ui.button(label="아니요", style=discord.ButtonStyle.red)
    async def cancel(self, button: discord.ui.Button, interaction: discord.Interaction):
        self.value = False
        self.stop()

    async def interaction_check(self, interaction: discord.Interaction):
        return interaction.user.id == self.author.id

class ForDev(commands.Cog, name="for_dev"):
    def __init__(self, bot):
        self.bot = bot

    @commands.hybrid_group(
        name="데브",
        description="DEV계열을 위한 명령어들.",
    )
    async def dev(self, context: Context) -> None:
        if context.invoked_subcommand is None:
            embed = discord.Embed(
                description="서브 커맨드를 정확히 확인해주세요.",
                color=self.bot.color_cancel,
            )
            await context.send(embed=embed)

    @dev.command(name="sid_삭제", description="해당 유저의 SID를 삭제합니다.")
    async def dev_sid_delete(self, context: Context, user: discord.User):
        if str(context.author.id) in self.bot.owners:
            member = context.guild.get_member(
                user.id
            ) or await context.guild.fetch_member(user.id)
            embed = discord.Embed(
                title="SID_삭제",
                description=f"{member}의 SID를 삭제합니다.",
                color=self.bot.color_main,
            )
            choice = YesOrNo(context.author)
            message = await context.send(embed=embed, view=choice)
            await choice.wait()
            if choice.value:
                if os.path.isfile(f"{self.bot.abs_path}/database/users/{user.id}.json"):
                    target_del_house = []
                    with open(
                        f"{self.bot.abs_path}/database/users/{context.author.id}.json",
                        "r",
                    ) as f:
                        data = json.load(f)
                        if data["primary_house"]:
                            target_del_house.append(data["primary_house"])
                        if data["owned_house"]:
                            for i in data["owned_house"]:
                                target_del_house.append(i)

                    if target_del_house:
                        for i in target_del_house:
                            with open(
                                f"{self.bot.abs_path}/database/states/{i[0]}.json", "r"
                            ) as f:
                                data2 = json.load(f)
                                if [i[1], i[2]] in data2["residential"]:
                                    data2["residential"].remove([i[1], i[2]])

                            with open(
                                f"{self.bot.abs_path}/database/states/{i[0]}.json", "w"
                            ) as f:
                                json.dump(data2, f)

                    os.remove(f"{self.bot.abs_path}/database/users/{user.id}.json")
                    embed = discord.Embed(
                        title="성공",
                        description="성공적으로 SID를 삭제하였습니다.",
                        color=self.bot.color_success,
                    )
                    await message.edit(embed=embed, view=None, content=None)
                else:
                    embed = discord.Embed(
                        title="오류.",
                        description=f"{member}의 SID는 존재하지 않습니다.",
                        color=self.bot.color_cancel,
                    )
                    await message.edit(embed=embed, view=None, content=None)
            else:
                embed = discord.Embed(
                    title="취소", description="취소하셨습니다.", color=self.bot.color_cancel
                )
                await message.edit(embed=embed, view=None, content=None)
        else:
            embed = discord.Embed(
                title="오류.", description="당신은 DEV계열이 아닙니다.", color=self.bot.color_cancel
            )
            await context.send(embed=embed)

    @dev.command(name="모든_주_초기화", description="모든 주의 변수를 초기 설정 값으로 되돌립니다.")
    async def dev_states_reset(self, context: Context):
        if str(context.author.id) in self.bot.owners:
            embed = discord.Embed(
                title="모든 주 초기화",
                description="모든 주의 변수를 초기 설정 값으로 되돌립니다.",
                color=self.bot.color_main,
            )
            choice = YesOrNo(context.author)
            message = await context.send(embed=embed, view=choice)
            await choice.wait()
            if choice.value:
                for state in self.bot.states:
                    data = {
                        "initial_support_money": eval(
                            f"self.bot.{state}_initial_money"
                        ),
                        "grid_x": 0,
                        "grid_y": 0,
                        "residential": [],
                        "corporate": [],
                        "industrial": [],
                        "natural": [],
                        "traffic": [],
                        "security": [],
                        "hospital": [],
                        "leisure": [],
                    }
                    with open(
                        f"{self.bot.abs_path}/database/states/{state}.json", "w"
                    ) as f:
                        json.dump(data, f)
                    self.bot.logger.info(f"{state} 초기화 완료.")

                for user_file in os.listdir(f"{self.bot.abs_path}/database/users/"):
                    if user_file.endswith(".json"):
                        with open(f"./database/users/{user_file}", "r") as file:
                            data = json.load(file)
                            data["primary_house"] = []
                            data["owned_house"] = []
                        with open(f"./database/users/{user_file}", "w") as file:
                            json.dump(data, file)

                embed = discord.Embed(
                    title="초기화 완료.",
                    description=f"모든 주의 변수가 초기 값으로 돌아갔습니다.",
                    color=self.bot.color_success,
                )
                await message.edit(embed=embed, view=None, content=None)
            else:
                embed = discord.Embed(
                    title="취소", description="취소하셨습니다.", color=self.bot.color_cancel
                )
                await message.edit(embed=embed, view=None, content=None)
        else:
            embed = discord.Embed(
                title="오류.", description="당신은 DEV계열이 아닙니다.", color=self.bot.color_cancel
            )
            await context.send(embed=embed)

    @dev.command(name="natzhashite_상태", description="Natzhashite의 상태를 확인합니다.")
    async def dev_check_natzhashite(self, context: Context):
        if str(context.author.id) in self.bot.owners:
            embed = discord.Embed(
                title="Natzhashite 현 상황", description=None, color=self.bot.color_main
            )

            all_spaces = 0
            all_occupied_spaces = 0
            for state in self.bot.states:
                with open(
                    f"{self.bot.abs_path}/database/states/{state}.json", "r"
                ) as f:
                    data = json.load(f)
                    all_spaces += (data["grid_x"] + 1) * (data["grid_y"] + 1)
                    all_occupied_spaces += (
                        len(data["residential"])
                        + len(data["corporate"])
                        + len(data["industrial"])
                        + len(data["natural"])
                        + len(data["traffic"])
                        + len(data["security"])
                        + len(data["hospital"])
                        + len(data["leisure"])
                    )

            all_user_money = 0
            all_user_num = 0
            for user_file in os.listdir(f"{self.bot.abs_path}/database/users"):
                if user_file.endswith(".json"):
                    with open(
                        f"{self.bot.abs_path}/database/users/{user_file}", "r"
                    ) as file:
                        data = json.load(file)
                        all_user_money += data["money"]
                        all_user_num += 1
            embed.add_field(
                name="Spaces", value=f"{all_occupied_spaces}/{all_spaces}", inline=True
            )

            embed.add_field(name="Users", value=f"{all_user_num}", inline=True)

            embed.add_field(
                name="개인 소유의 전체 재화량",
                value=f"{format_number_with_units(bot=self.bot, number=all_user_money)}",
                inline=True,
            )

            await context.send(embed=embed)
        else:
            embed = discord.Embed(
                title="오류.", description="당신은 DEV계열이 아닙니다.", color=self.bot.color_cancel
            )
            await context.send(embed=embed)

    @dev.command(name="reset_everything", description="Natzhashite 전체를 초기 상황으로 되돌립니다.")
    async def dev_reset_everything(self, context: Context):
        if str(context.author.id) in self.bot.owners:
            log_msg = reset_system(self.bot.abs_path, self.bot.config)
            await context.send(log_msg)
            announce_channel = self.bot.get_channel(self.bot.announce_channel)
            await announce_channel.send(log_msg)
        else:
            embed = discord.Embed(
                title="오류.", description="당신은 DEV계열이 아닙니다.", color=self.bot.color_cancel
            )
            await context.send(embed=embed)

async def setup(bot):
    await bot.add_cog(ForDev(bot))


import torch
import torch.nn as nn

from models.layers import get_activation_fn

class GraphConv(nn.Module):
    def __init__(self, in_dim: int, out_dim: int, identity_proj: bool = False):
        super().__init__()
        if identity_proj:
            assert in_dim == out_dim
        self.linear = nn.Identity() if identity_proj else nn.Linear(in_dim, out_dim)
        self._reset_parameters()

    def _reset_parameters(self):
        if isinstance(self.linear, nn.Linear):
            nn.init.xavier_uniform_(self.linear.weight)
            nn.init.normal_(self.linear.bias)

    def forward(self, edges: torch.Tensor, feat: torch.Tensor) -> torch.Tensor:
        """
        Args:
            edges: shape: [bs, n, n]
            x: shape: [bs, n, dim]
        """
        # [bs, n, n] [bs, n, dim] -> [bs, n, dim]
        adj = edges + edges.transpose(1, 2)
        In = torch.zeros_like(adj)
        In.diagonal(dim1=1, dim2=2).fill_(1)
        feat = torch.bmm(adj / 2 + In, feat)
        return self.linear(feat)

class Layer(nn.Module):
    def __init__(self, emb_dim: int, activation: str, identity_proj: bool = False):
        super().__init__()
        self.g_conv = GraphConv(emb_dim, emb_dim, identity_proj)
        self.norm = nn.LayerNorm(emb_dim)
        self.activation = get_activation_fn(activation)

    def forward(self, edges: torch.Tensor, feat: torch.Tensor, feat_mask: torch.BoolTensor = None):
        feat = self.g_conv(edges, feat)
        if feat_mask is not None:
            feat.masked_fill_(feat_mask[..., None], 0)
        feat = self.activation(self.norm(feat))
        return feat

class GNN(nn.Module):
    def __init__(
        self,
        num_codes: int,
        embed_dim: int,
        num_layers: int,
        identity_proj: bool = False,
        activation: str = "relu"
    ):
        super().__init__()
        self.num_codes = num_codes
        self.embed_dim = embed_dim
        self.num_layers = num_layers

        self.embedding = nn.Embedding(
            num_embeddings=num_codes + 1,
            embedding_dim=embed_dim,
            padding_idx=num_codes
        )
        layers = [Layer(embed_dim, activation, identity_proj) for _ in range(num_layers)]
        self.layers = nn.ModuleList(layers)
        self.fc = nn.Linear(embed_dim, embed_dim)
        self._reset_parameters()

    def _reset_parameters(self):
        nn.init.normal_(self.fc.weight)
        nn.init.zeros_(self.fc.bias)
        nn.init.trunc_normal_(self.embedding.weight[:self.num_codes])

    def forward(
        self,
        nodes: torch.Tensor,
        edges: torch.Tensor,
        ingredients: torch.LongTensor,
        feat_mask: torch.BoolTensor = None
    ):
        """
        Args:
            nodes: [bs, n]
            edges: [bs, n, n]
        """
        # code embedding [bs, n, dim]
        feat: torch.Tensor = self.embedding(ingredients)
        for layer in self.layers:
            feat = layer(edges, feat, feat_mask)
        # weighted average pooling
        feat = feat * nodes[..., None]
        feat = feat.mean(dim=1)
        feat = self.fc(feat)
        return feat


import datetime

today = datetime.date.today()
# print(today)

# print(today.weekday())
# print(today.isoweekday())

# lets calculate a duration of 7 days using timedelta
tdelta = datetime.timedelta(days=7)

# you can add or subtract time delta
# print(today - tdelta)

# diff between two dates is timedelta
bday = datetime.date(2020, 9, 12)
till_bday = bday - today
print(till_bday)

# this was about dates,
# now lets talk about time

t1 = datetime.datetime(2020, 11, 22, 9, 30, 45, 100000)
# print(t1.date())
# print(t1.time())
# print(t1.year)
# print(t1.month)

tdelta = datetime.timedelta(hours=2)
# print(t1 + tdelta)

# constructor with datetime

t11 = datetime.datetime.today()
t12 = datetime.datetime.now()
t13 = datetime.datetime.utcnow()

# print(t11)
# print(t12)
# print(t13)

# using pytz for time zone information
import pytz

# timezone aware UTC

# dt = datetime.datetime(2020, 1, 22, 9, 45, 59, 100000, tzinfo=pytz.UTC)

# print(dt)

dt_now = datetime.datetime.now(tz=pytz.UTC)
print(dt_now)

my_timezone = dt_now.astimezone(pytz.timezone('Asia/Kolkata'))

print(my_timezone)

# for tz in pytz.all_timezones:
# 	print(tz)

dt_kol = datetime.datetime.now(tz = pytz.timezone('Asia/Kolkata'))

# convert the date to string format
print(dt_kol.strftime('%B %d, %Y'))

# convert string to date
str_dt = 'July 26, 2012'

dt = datetime.datetime.strptime(str_dt, '%B %d, %Y')
print(dt)


import pytest
from collections import OrderedDict

from pytestflow.graph import DAG, GraphError

@pytest.mark.parametrize('input_dict,expected', [
    ({'a': [1, 2, 3]}, OrderedDict(**{1: [], 2: [], 3: [], 'a': [1, 2, 3]})),
    ({'a': 'hello'}, OrderedDict(**{'hello': [], 'a': ['hello']})),
    ({'a': (1, 2, 3)}, OrderedDict(**{1: [], 2: [], 3: [], 'a': [1, 2, 3]}))
])
def test_dag_init_from_dict(input_dict, expected):
    graph = DAG(**input_dict)
    assert graph._graph == expected

def test_detect_cycle():
    input_dict = {'a': ['b', 'c'], 'b': ['d', 'a']}

    with pytest.raises(GraphError):
        DAG(**input_dict)

def test_topo_sort():
    input_dict = {'a': ['b', 'c'], 'b': ['d', 'e'], 'c': ['d'], 'd': ['e']}
    graph = DAG(**input_dict)

    assert graph.is_sorted
    assert list(graph.keys()) in (['e', 'd', 'c', 'b', 'a'], ['e', 'd', 'b', 'c', 'a'])

def test_add_edge():
    input_dict = {'a': ['b'], 'b': ['c']}
    graph = DAG(**input_dict)

    assert list(graph.keys()) == ['c', 'b', 'a']

    graph.add_edge('d', 'b')
    assert list(graph.keys()) in (['c', 'b', 'd', 'a'], ['c', 'b', 'a', 'd'])

def test_add_edge_cycle():
    input_dict = {'a': ['b'], 'b': ['c']}
    graph = DAG(**input_dict)

    assert list(graph.keys()) == ['c', 'b', 'a']

    with pytest.raises(GraphError):
        graph.add_edge('b', 'a')

    assert graph.is_sorted

def test_remove_edge():
    input_dict = {'a': ['b'], 'b': ['c'], 'd': ['b']}
    graph = DAG(**input_dict)

    assert list(graph.keys()) in (['c', 'b', 'd', 'a'], ['c', 'b', 'a', 'd'])

    graph.remove_edge('d', 'b')
    print(graph)
    assert list(graph.keys()) == ['c', 'b', 'a']
    assert 'd' not in graph


# 服务器
import socketserver
import socket
class MyServer(socketserver.BaseRequestHandler):
	def handle(self):
		# 自定义的逻辑...
		print("----> 执行这句话")
		# print(self.request)
		# print(self.client_address)
		conn = self.request
		while True:
			# 接收消息
			msg = conn.recv(1024)
			print(msg.decode("utf-8"))
			# pass

			conn.send(b'world')

# 生成一个对象
# ThreadingTCPServer( "ip端口号" ,"自定义的类"  )
server = socketserver.ThreadingTCPServer( ("127.0.0.1",9000) , MyServer )
# 循环调用
server.serve_forever()

'''
try:
    # lst = [1, 2, 3]
    # print(lst[99])
    print(333)
finally:
    print(1)
    print(2)
'''

"""
try .. finally ..
不论try 代码块里面的代码是不是报错,
都会执行finally这个代码块中的内容.
作用:正常情况,如果报错,后面的代码一律不执行,
但是特殊条件下,又必须在报错的行为中,执行某些代码,
那么这样的代码加到finally当中
"""


import math

def is_prime(n):
    if n % 2 == 0 and n > 2:
        return False
    return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))

curr = 2
num = 0
ten_thousand = False
while not ten_thousand:
    if is_prime(curr):
        num = num + 1

    if num == 10001:
        print(curr)
        ten_thousand = True

    curr = curr + 1


"""As a toy program, this simple traffic light procedure demonstrates
assertions that may save you from making simple errors when programming.
To run the program without checking the assertions, use the -O option
when calling python from the terminal.
"""

market_2nd = {'ns': 'green', 'ew': 'red'}
mission_16th = {'ns': 'red', 'ew': 'green'}

def switchLights(stoplight):
    assert 'red' in stoplight.values(), "Neither light is red!"
    for key in stoplight.keys():
        if stoplight[key] == 'green':
            stoplight[key] = 'yellow'
        elif stoplight[key] == 'yellow':
            stoplight[key] = 'red'
        elif stoplight[key] == 'red':
            stoplight[key] = 'green'

print(market_2nd)
switchLights(market_2nd)
print(market_2nd)
switchLights(market_2nd)
print(market_2nd)
switchLights(market_2nd)
print(market_2nd)


l=[{'s':1,'d':2,'a':3},{'s':4,'d':5,'a':6}]
# print(l[1].keys())
for i in range(2):
    for key,value in l[i].items():
        if key =='d':
            print(value)


import plotly.graph_objects as go # Biblioteca usada para criar gráficos

headerColor = 'grey'        # Variável para definir a cor do cabeçalho
rowEvenColor = 'lightgrey'  # Variável para definir a cor da linha intercalada [Even]
rowOddColor = 'white'       # Variável para definir a cor da linha intercalada [Odd]

# Cria a tabela
fig = go.Figure(data=[go.Table(
  # Configuração do cabeçalho
  header=dict(
    values=['EXPENSES','Q1','Q2','Q3','Q4'], #
 utilizado para quebra de linha na mesma célula
    line_color='darkslategray', # Configuração da cor de linha
    fill_color=headerColor, # Configuração da cor de preenchimento = variável declarada
    align=['left','center'], # Posiciona o texto
    font=dict(color='white', size=12) # Cor e tamanho do texto
  ),
  # Configuração das células
  cells=dict(
    # Configuração dos dados nas colunas, separadas por [],[],[],...
    values=[
      ['Salaries', 'Office', 'Merchandise', 'Legal', 'TOTAL'],
      [1200000, 20000, 80000, 2000, 12120000],
      [1300000, 20000, 70000, 2000, 130902000],
      [1300000, 20000, 120000, 2000, 131222000],
      [1400000, 20000, 90000, 2000, 14102000]],
    line_color='darkslategray', # Configuração da cor de linha

    # Lista 2D de cores para linhas alternadas
    fill_color = [[rowOddColor,rowEvenColor,rowOddColor, rowEvenColor,rowOddColor]*5], # 5 = Quantidade de linhas intercaladas.
    align = ['left', 'center'], # Posiciona o texto
    font = dict(color = 'darkslategray', size = 11) # Cor e tamanho do texto
    ))
])

fig.show() # Plota o gráfico

# plotly.express pode criar toda a fígura de uma só vez. Ele usa a graph_objects internamente


# pylint:disable=C0111,C0103

def query_orders(db):
    query ='''
        SELECT *
        FROM  Orders o
        ORDER BY OrderID ASC
    '''
    db.execute(query)
    return db.fetchall()

def get_orders_range(db, date_from, date_to):
    query = '''
        SELECT *
        FROM  Orders o
        WHERE OrderDate > ? AND OrderDate < ?
    '''
    db.execute(query, (date_from, date_to))
    #db.execute(query)
    return db.fetchall()

    # return a list of orders displaying all columns with OrderDate between date_from and date_to

def get_waiting_time(db):

    query = '''
        SELECT *, (julianday(o.ShippedDate) - julianday(o.OrderDate))  AS TimeDelta
        FROM  Orders o
        ORDER BY TimeDelta ASC
    '''
    db.execute(query)
    return db.fetchall()

    # get a list with all the orders displaying each column
    # and calculate an extra TimeDelta column displaying the number of days
    # between OrderDate and ShippedDate, ordered by ascending TimeDelta
    #pass  # YOUR CODE HERE


import pycuda.driver as drv
from pycuda.compiler import SourceModule
import numpy as np
from smii.modeling.propagators.pycuda import PycudaPropagator

class Scalar2D(PycudaPropagator):
    def __init__(self, model, dx, source_dt, sources, pml_width=None,
                 nvcc_options=None):

        source = """
__constant__ float fd1_d[2];
__constant__ float fd2_d[3];

__global__ void step_d(const float * const wfc,
                float * const wfp,
                const float * const phiz,
                float * const phizp,
                const float * const phix,
                float * const phixp,
                const float * const sigmaz,
                const float * const sigmax,
                const float * const model2_dt2,
                const float dt,
                const int nb,
                const int nz,
                const int nx)
{
        int zblocks_per_shot = gridDim.y / nb;
        int x = blockDim.x * blockIdx.x + threadIdx.x;
        int z = blockDim.y * (blockIdx.y % zblocks_per_shot) + threadIdx.y;
        int b = blockIdx.y / zblocks_per_shot;
        int i = z * nx + x;
        int bi = b * nz * nx + i;
        float lap;
        float wfc_x;
        float wfc_z;
        float phix_x;
        float phiz_z;
        bool in_domain = (x > 1) && (x < nx - 2)
                && (z > 1) && (z < nz - 2);

        if (in_domain)
        {
                lap = (fd2_d[0] * wfc[bi] +
                                fd2_d[1] *
                                (wfc[bi + 1] +
                                 wfc[bi - 1] +
                                 wfc[bi + nx] +
                                 wfc[bi - nx]) +
                                fd2_d[2] *
                                (wfc[bi + 2] +
                                 wfc[bi - 2] +
                                 wfc[bi + 2 * nx] +
                                 wfc[bi - 2 * nx]));

                wfc_x = (fd1_d[0] *
                                (wfc[bi + 1] -
                                 wfc[bi - 1]) +
                                 fd1_d[1] *
                                 (wfc[bi + 2] -
                                 wfc[bi - 2]));

                wfc_z = (fd1_d[0] *
                                (wfc[bi + nx] -
                                 wfc[bi - nx]) +
                                 fd1_d[1] *
                                 (wfc[bi + 2 * nx] -
                                 wfc[bi - 2 * nx]));

                phix_x = (fd1_d[0] *
                                (phix[bi + 1] -
                                 phix[bi - 1]) +
                                 fd1_d[1] *
                                 (phix[bi + 2] -
                                 phix[bi - 2]));

                phiz_z = (fd1_d[0] *
                                (phiz[bi + nx] -
                                 phiz[bi - nx]) +
                                 fd1_d[1] *
                                 (phiz[bi + 2 * nx] -
                                 phiz[bi - 2 * nx]));

                wfp[bi] = 1 / (1 + dt * (sigmaz[z] + sigmax[x]) / 2) *
                        (model2_dt2[i] * (lap + phix_x + phiz_z) +
                        dt * (sigmaz[z] + sigmax[x]) * wfp[bi] / 2 +
                        (2 * wfc[bi] - wfp[bi]) -
                        dt * dt * sigmaz[z] * sigmax[x] * wfc[bi]);

                phizp[bi] = phiz[bi] -
                        dt * (sigmaz[z] * phiz[bi] +
                        (sigmaz[z] - sigmax[x]) * wfc_z);
                phixp[bi] = phix[bi] -
                        dt * (sigmax[x] * phix[bi] +
                        (sigmax[x] - sigmaz[z]) * wfc_x);

        }
}

__global__ void add_sources_d(float * const wfp,
                const float * const model2_dt2,
                const float * const source_amplitude,
                const int * const sources_loc,
                const int step,
                const int nz,
                const int nx,
                const int nt, const int ns)
{
        int s = threadIdx.x;
        int b = blockIdx.x;
        int i = sources_loc[b * ns * 2 + s * 2 + 0] * nx +
            sources_loc[b * ns * 2 + s * 2 + 1];
        int bi = b * nz * nx + i;

        wfp[bi] += source_amplitude[b * ns * nt + s * nt + step] *
            model2_dt2[i];
}
"""
        if nvcc_options is None:
            nvcc_options = ['--restrict', '--use_fast_math', '-O3']

        mod = SourceModule(source, options=nvcc_options)
        jitfunc1 = mod.get_function('step_d')
        jitfunc2 = mod.get_function('add_sources_d')
        fd1_d = mod.get_global('fd1_d')[0]
        fd2_d = mod.get_global('fd2_d')[0]

        pad_width = 2
        super(Scalar2D, self).__init__(jitfunc1, jitfunc2, fd1_d, fd2_d,
                                       model, dx, source_dt, sources,
                                       pad_width, pml_width=pml_width)

    def step(self, num_steps=1):

        for it in range(num_steps):
            for inner_it in range(self.timestep.step_ratio):
                self.jitfunc1(self.wavefield.current_gpu,
                              self.wavefield.previous_gpu,
                              self.pml.phi[0].current_gpu,
                              self.pml.phi[0].previous_gpu,
                              self.pml.phi[1].current_gpu,
                              self.pml.phi[1].previous_gpu,
                              self.pml.sigma_gpu[0],
                              self.pml.sigma_gpu[1],
                              self.model.padded_property_gpu['vp2dt2'],
                              np.float32(self.timestep.dt),
                              np.int32(self.sources.num_shots),
                              np.int32(self.geometry.model_shape_padded[0]),
                              np.int32(self.geometry.model_shape_padded[1]),
                              grid=self.griddim, block=self.blockdim)
                self.jitfunc2(self.wavefield.previous_gpu,
                              self.model.padded_property_gpu['vp2dt2'],
                              self.sources.amplitude_gpu,
                              self.sources.padded_locations_gpu,
                              np.int32(self.timestep.step_idx),
                              np.int32(self.geometry.model_shape_padded[0]),
                              np.int32(self.geometry.model_shape_padded[1]),
                              np.int32(self.timestep.num_steps),
                              np.int32(self.sources.num_sources_per_shot),
                              grid=(self.sources.num_shots, 1),
                              block=(self.sources.num_sources_per_shot, 1, 1))
                self.wavefield.current_gpu, self.wavefield.previous_gpu = \
                    self.wavefield.previous_gpu, self.wavefield.current_gpu
                self.pml.phi[0].current_gpu, self.pml.phi[0].previous_gpu = \
                    self.pml.phi[0].previous_gpu, self.pml.phi[0].current_gpu
                self.pml.phi[1].current_gpu, self.pml.phi[1].previous_gpu = \
                    self.pml.phi[1].previous_gpu, self.pml.phi[1].current_gpu

            self.timestep.step_idx += 1

        drv.memcpy_dtoh(self.wavefield.current, self.wavefield.current_gpu)

        return self.wavefield.inner


import qisys.sh
import qisys.script

from qisys.test.conftest import TestWorkTree

def test_qisrc_remove_exsiting(qisrc_action):
    worktree = qisrc_action.worktree
    worktree.create_project("foo")
    qisrc_action("remove", "foo")
    worktree = TestWorkTree()
    assert not worktree.get_project("foo")
    assert worktree.tmpdir.join("foo").check(dir=True)

def test_qisrc_remove_existing_from_disk(qisrc_action):
    worktree = qisrc_action.worktree
    worktree.create_project("foo")
    qisrc_action("remove", "foo", "--from-disk")
    worktree = TestWorkTree()
    assert not worktree.get_project("foo")
    assert not worktree.tmpdir.join("foo").check(dir=True)

def test_qisrc_fails_when_not_exists(qisrc_action):
    error = qisrc_action("remove", "foo", raises=True)
    assert "No such project: foo" in error


import main
import unittest

class TestMainPage(unittest.TestCase):

    def setUp(self):
        self.app = main.app.test_client()
        self.app.testing = True

    def test_status_code(self):
        response = self.app.get('/')
        self.assertEqual(response.status_code, 200)

    def test_message(self):
        response = self.app.get('/')
        message = main.page("Hello!!! Welcome to the world of NordCloud ;)")
        self.assertEqual(response.data.decode("utf-8"), message)

if __name__ == '__main__':
    unittest.main()


"""This module defines interfaces so that clients can use numpy
without their clients knowning about implementation.

It has high cyclomatic complexity in some methods-- numpy is likely to grab
array structure when you don't want it to-- hence you must HIDE the fact
that your tensors look like arrays. Moreover, operator overloading is not
trivial:

You cannot let numpy run the show!

In spite of the long methods and hiddeous If-Then depth, there are some
winners (of the beauty contest-- it's all congenial):

enlister
dishcharger
matrix_wrap

are (nested) decorators to handle methods that cannot work on arraylike
data and singleton data with the same code (well, now they can).
"""
#pylint: disable=E1101

## \namespace geo.utils.arraylike Array_like interfaces
import abc
import functools
import itertools
import operator

from . import trig
## For debugging the getitem, getattr, getatribute complexities.
VERB = 0

## Got length?
def got_length(arg):
    """arg is a container-- old school check."""
    try:
        len(arg)
    except TypeError:
        return False
    return True

## operator.itemgetter, with Error protection.
def itemgetter(index):
    """itergetter w/ error protection."""

    def func(item):
        """get item or None"""
        try:
            item = item[index]
        except TypeError:
            pass
        return item
    return func

## For method that cannot act "array_like", array_like results are put
# into a list
def enlister(method):
    """Enlisted method is run on items in an iterable (Geometric),
    and the results are returned in a list."""

    method.__doc__ = method.__doc__ + " Or as list thereof."

    @functools.wraps(method)
    def wrapped_method(self, *args, **kwargs):
        """array_like's method call will be mapped to a list"""
        if self._got_length():  # external poly (array/singleton)
            result = [method(item, *args, **kwargs) for item in self]
        else:
            result = method(self, *args, **kwargs)
        return result
    return wrapped_method

## For enlister wrapped method who's results can be caste array_like:
# this decorator does it.
def discharger(method):
    """Method results--If enlisted-- are repackaged into correct
    Geometric Animal."""

    @functools.wraps(method)
    def wrapped_method(self, *args, **kwargs):
        """Result be de-enlisted, If needed."""
        # just call the method
        result = method(self, *args, **kwargs)
        # If it returned a list, then the results must be reconstituted
        if isinstance(result, list):  # external poly
            # chose type for where else? The list.
            if trig.NUMPY:
                from numpy import array
            else:
                array = list
            return type(result[0])(
                *itertools.imap(array,
                                itertools.izip(*[item.iter()
                                                 for item in result]))
            )
        return result
    return wrapped_method

## Decorator to wrap result in a method.
def matrix_wrap(method):
    """Wrap discharged method in a matrix."""

    @functools.wraps(method)
    @discharger
    @enlister
    def wrapped_method(self, *args, **kwargs):
        """discharger(enlister(method(*args, **kwargs)))"""
        return self.frommatrix(method(self)(self.asmatrix(), *args, **kwargs))
    return wrapped_method

## This base class controls "__getitem__" behavior and provides a component
#  iterator.
class ABCNumpy(object):
    """This class is for classes that may have singleton on numpy array
    attributes (Vectors, Coordinates, ....)."""

    __metaclass__ = abc.ABCMeta

    ## B/c they need to know
    _got_length = got_length

    ## Object is iterable ONLY If its attributes are-- this method
    #  works of tensors and coordinates, so there is some peg-point
    #  problems--the peg has to be set in an extended method--which
    #  may violate LSP.
    #  \param index a general index
    #  \retval instance of type(self)
    #  If self isn't indexable at run-time.
    def __getitem__(self, index):
        if len(self):  # external poly
            return type(self)(*itertools.imap(itemgetter(index), self.iter()))

    ## reversed(Tensor).
    def __reversed__(self):
        return type(self)(*map(list, map(reversed, self.iter())))

    ## \param index a general index
    #  \param value an instance type(self).
    #  \retval None this is a procedure
    #  \throws exceptions.NonGeometricTypeError on silly value
    #  \throws exceptions.PolymorphismError on going array_like
    #  when self doesn't play that.
    def __setitem__(self, index, value):
        try:
            values = list(value.iter())
            items = list(self.iter())
            if len(items) != len(values):  # guard
                raise TypeError
        except (AttributeError, TypeError):
            from .exceptions import NonGeometricTypeError
            msg = (type(value).__name__ +
                   " does not constitute a " +
                   type(self).__name__)
            raise NonGeometricTypeError(msg)
        else:
            try:
                for item, value in itertools.izip(items, values):
                    operator.setitem(item, index, value)
                    continue
            except TypeError:
                from .exceptions import PolymorphismError
                msg = (
                    type(self).__name__ +
                    " instance isn't built for item assignment"
                    )
                raise PolymorphismError(msg)

    def __delitem__(self, index):
        for item in self.iter():
            operator.delitem(item, index)

    ## len is the len of the components-If they all have the same length
    # \throws ValueError on illdefined length
    def __len__(self):
        lens = []
        for item in self.iter():
            try:
                item_length = len(item)
            except TypeError:
                continue
            else:
                lens.append(item_length)
            continue
        if lens:  # raise
            if lens.count(lens[0]) == len(lens):  # guard
                result = lens[0]
            else:
                # non-unique len--> check it before you wreck it.
                raise ValueError("object length is bad: %s" % str(lens))
            return result
        raise TypeError("{} instance isn't iterable".format(self.__class__))

    ## For container attributes: search, otherwise return NotImplemented so
    #  that the object doesn't look like a container when introspected.
    #  \param other
    #  \retval bool IFF other is in self
    #  \throws exceptions.PolymorphismError IFF self doesn't play arrays
    def __contains__(self, other):
        """any(map(other.__eq__, self))"""
        try:
            return any(itertools.imap(other.__eq__, self))
        except TypeError:
            from .exceptions import PolymorphismError
            msg = (
                type(self).__name__ +
                " instance isn't built like a container"
                )
            raise PolymorphismError(msg)

    ## Container like iter
    #  \retval generator
    #  \throws exceptions.PolymorphismError is iter() was uncalled for.
    def __iter__(self):
        try:
            return itertools.starmap(type(self),
                                     itertools.izip(*self.iter()))
        except TypeError:
            from .exceptions import PolymorphismError
            msg = (
                type(self).__name__ +
                " instance isn't built like a sequence."
                )
            raise PolymorphismError(msg)

    ## Component iter.
    # \yields Geometric Components
    def iter(self):
        """return a generator that generates components"""
        return itertools.imap(self.__getattribute__, self.components)

    ## List of Components
    # \returns list of components
    def tolist(self):
        """return a list of #components"""
        return list(self.iter())

    ## Recast data type
    # \param dtype a numpy dtype
    # \return geometric object, recast
    def asdtype(self, dtype):
        """Cast component arrays astype)"""
        return self.numpy_method('astype', dtype)

    ##  This allows you to broadcast functions (numpy functions) to the
    # attributes and rebuild a class
    # \param func \f$ f \f$
    # \param args arguments to f
    # \param kwargs keywords to f
    # \returns \f$ T'_{ijk} = f(T_{ijk}, *args, **kwargs) \f$
    def broadcast(self, func, *args, **kwargs):
        """vector.broadcast(func, *args, **kwargs) -->
        Vector(*map(func, vector.iter()))

        That is: apply func component wise, and return a new Vector
        """
        f_ak = functools.partial(func, *args, **kwargs)
        return type(self)(*itertools.imap(f_ak, self.iter()))

    ## Mean
    # \returns \f$ T_{i...k} \rightarrow
    # \frac{1}{n}\sum_0^{n-1}{T_{i...k}[n]} \f$
    def mean(self, *args, **kwargs):
        """broadcast np.mean (see broadcast.__doc__)"""
        from numpy import mean
        return self.broadcast(mean, *args, **kwargs)

    ## Sum
    # \returns \f$ T_{i...k} \rightarrow \sum_0^{n-1}{T_{i...k}[n]} \f$
    def sum(self, *args, **kwargs):
        """broadcast np.sum (see broadcast.__doc__)"""
        from numpy import sum as np_sum
        return self.broadcast(np_sum, *args, **kwargs)

    ## Cumulative Sum
    # \returns \f$ T_{i...k}[n] \rightarrow \sum_0^{n-1}{T_{i...k}[n]} \f$
    def cumsum(self, *args, **kwargs):
        """broadcast np.cumsum (see broadcast.__doc__)"""
        from numpy import cumsum
        return self.broadcast(cumsum, *args, **kwargs)

    ## Apply np.ravel to components
    def ravel(self, order=0):
        """broadcast numpy.ravel"""
        from numpy import ravel
        return self.broadcast(ravel, order=order)

    ## Conjugate via components' conjugate method
    # \returns \f$ T_{ij\ldots} \rightarrow T^{*}_{ij\ldots} \f$
    def conjugate(self, method_name='conjugate'):
        """broadcast conjugate methodcaller"""
        return self.numpy_method(method_name)

    ## Call a numpy method by name
    def numpy_method(self, method_name, *args, **kwargs):
        """np.method(method_name, *args, **kwargs)

        broadcast np.ndarray method (see broadcast.__doc__)"""
        return self.broadcast(operator.methodcaller(method_name,
                                                    *args, **kwargs))

    ## For a tensor, chart, or coordinate made of array_like objects:\n
    #  append a like-wise object onto the end
    def append(self, other):
        """For array_like attributes, append and object into the end."""
        #pylint: disable=E0203
        from numpy import append
        result = type(self)(*itertools.starmap(
            append,
            itertools.izip(self.iter(), other.iter())))
        ## TODO: get rid of this attrocity, pegged thingies need to
        # extend this method with the peg adder- otherwise it is burden
        # for all other objects in geo's universe.
        try:
            peg = self.peg
        except AttributeError:
            pass
        else:
            result.peg = peg
        return result

    ## The idea is to check equality for all tensor/coordinate types and for
    #  singleton and numpy arrays, \n so this is pretty concise-- the previous
    #  version was 10 If-then blocks deep.
    # \param other anything
    # \returns bool or ndarray of dtype=bool_.
    def __eq__(self, other):
        """Check tensor equality, for each component.

        Returns BOOL or INT (0,1) for:

        (1) inter-type comparison
        (2) non array-like comparison

        Othersie, returns numpy.ndarray(dtype=bool) for:

        (1) Any broadcastable comparison (intra-type).
        """
        try:
            iter_o = other.iter()
        except AttributeError:
            # or raise an Error?
            pass
        else:
            #pylint: disable=E0602
            for count, (left, right) in enumerate(itertools.izip(self.iter(),
                                                                 iter_o)):

                ## Set the value OR array of values, who knows?
                value = (left == right)
                if not count:  # initializer
                    # on 1st pass: set values
                    result = value
                else:
                    # on 2+ pass, update value
                    result *= value
                    # go to next case
                    continue
                # can't break on False, b/c of array_like behavior
                continue
            # result may be array_like
            return result
        # result is straight False after TypeError in comparison
        return False

    ## not __eq__,  while per preserving array_like behavior- not easy --
    #  note that function/statement calling enforces type checking
    #  (no array allowed),
    #  \n while method calling does not.
    def __ne__(self, other):
        """See __eq__"""
        inv = self.__eq__(other)
        try:
            result = operator.not_(inv)
        except ValueError:
            #pylint: disable=E1103
            result = (1-inv).astype(bool)
        return result

    ## This covers <   <= >  >= and raises a RuntimeError, unless numpy
    #  calls it, and then that issue is addressed (and it's complicated)
    def __cmp__(self, other):
        """This method is called in 2 cases:

        Vector Comparison: If you compare (<, >, <= , >= ) too non-scalar
        tensors, or rotations--that makes no sense and you get a TypeError.

        Left-Multiply with Numpy: this is a little more subtle. If you are
        working with array_like tensors, and say, do

        >>> (v**2).w*v   #instead of:
        >>> (v**2)*v     #(which works), numpy will take over and call
        __cmp__ in order to figure how to do linear algebra on
        the array_like components of your tensor.
        The whole point of the Scalar class is to avoid this
        pitfall.

        Side Note: any vector operation should be manIfestly co variant--
        that is-- you don't need to access the "w"-- so you should not get
        this error.

        But you might.
        """
        raise TypeError(
            """comparison operation not permitted on %s\ncheck for left
            "mul by a numpy array.\n Right operand  is %s""" % (
                type(self).__name__,
                type(other).__name__))

    ## In principle, we always want true division: this is here in case a
    #  client calls it
    def __truediv__(self, other):
        return self.__div__(other)

    ## In principle, we always want true division: this is here in case a
    #  client calls it
    def __rtruediv__(self, other):
        return self.__rdiv__(other)

    ## This is risky and pointless-- who calls float?
#    def __float__(self):
#        return float(abs(self).w)

    ## +T is
    #  T   --- is as in python\n This method is used in coordinate transforms
    #  when an identity transform is required, dummy keyword allows
    #  polymorphism.
    def __pos__(self, peg=None):
        """+obj is obj"""
        #pylint: disable=W0613
        return self

    ## Get some numpy properties from the instance- numpy may request this.
    # \param attrgetter_ ?
    # \returns shapes of something
    # \throws TypeError for numpy.
    def _array_property(self, attrgetter_):
        shapes = []
        for item in self.iter():
            try:
                shapes.append(attrgetter_(item))
            except AttributeError:
                pass
            continue
        if shapes:  # external poly
            if shapes.count(shapes[0]) == len(shapes):  # raise
                return shapes[0]
        raise TypeError

    @property
    def shape(self):
        """Numpy shape of elements."""
        return self._array_property(operator.attrgetter('shape'))

    @property
    def ndim(self):
        """Numpy ndim of elements."""
        return self._array_property(operator.attrgetter('ndim'))

    @property
    def size(self, x=0):
        """Numpy size."""
        for count, item in enumerate(self.iter()):
            x = item.size
            if count:  # initializer
                if x != result[-1]:  # raise
                    #pylint: disable=E0601
                    msg = "Ill formed array structures in {}"
                    raise TypeError(msg.format(self))
                result.append(x)
            else:
                result = [x]
        return x

    ## Broadcast trig.Re
    # \f$ \Re{({\bf\vec{v}})} =
    # \Re{(v_x)}{\bf\hat{e}}_x +
    # \Re{(v_y)}{\bf\hat{e}}_y +\Re{(v_z)}{\bf\hat{e}}_z \f$ \n
    # and likewise for other ranks.
    @property
    def real(self):
        """v.real"""
        return self.broadcast(trig.Re)

    ## Broadcast trig.Im
    # \f$ \Im{{\bf\vec{v}}} = \Im{v_x}{\bf\hat{e}}_x +
    # \Im{v_y}{\bf\hat{e}}_y +\Im{v_z}{\bf\hat{e}}_z \f$ \n
    # and likewise for other ranks.
    @property
    def imag(self):
        """v.imag"""
        return self.broadcast(trig.Im)


# This code is called when instances of this SOP cook.
node = hou.pwd()
geo = node.geometry()

# Add code to modify the contents of geo.

filepath = hou.pwd().parm('file').eval()
textFile = hou.readFile(filepath)

countryList = textFile.split("|")
countryList = list(filter(None, countryList))
countryList = [c.strip() for c in countryList]
countryList = countryList[1:]

# Add 2 attributes lat and lon
geo.addAttrib(hou.attribType.Point, "lat", 0)
geo.addAttrib(hou.attribType.Point, "lon", 0)

for country in countryList:
    countryInfo = country.split("$")
    countryInfo = list(filter(None, countryInfo))

    # Get the country and continent names
    countryName = countryInfo[0]
    countryName = ''.join(e for e in countryName if e.isalnum())  # trim all white spaces, illegal characters
    continentName = countryInfo[1]
    continentName = ''.join(e for e in continentName if e.isalnum())  # trim all white spaces, illegal characters

    # create a point group for every country
    if geo.findPointGroup(countryName) == None:
        countryPtsGroup = geo.createPointGroup(countryName)

    """
    #create a point group for every continent
    if geo.findPointGroup(continentName) == None:
        continentPtsGroup = geo.createPointGroup(continentName)
    """
    regions = countryInfo[2].split("*")
    regions = list(filter(None, regions))

    for region in regions:
        borderCoords = region.split(";")
        borderCoords = list(filter(None, borderCoords))

        poly = geo.createPolygon()

        for borderCoord in borderCoords:
            latlon = borderCoord.split(",")
            lat = float(latlon[0])
            lon = float(latlon[1])

            point = geo.createPoint()
            point.setPosition((lat, lon, 0))
            poly.addVertex(point)

            # set newly added point to country group
            countryPtsGroup = geo.findPointGroup(countryName)
            countryPtsGroup.add(point)
            """
            continentPtsGroup = geo.findPointGroup(continentName)
            continentPtsGroup.add(point)
            """
        # Check if need to set polygon closed to False
        if node.parm('IsClosed').eval() == False:
            poly.setIsClosed(False)


# hash table implementation

# creating a class hashTable

class HashTable():

    def __init__(self,size):

        self.size = size     # initial size of DS
        self.table = [None]*size # initialization of the table

    def hashfunc(self,key):   # Function to calculate the index of the key in hash table
        return hash(key) % self.size

    def put(self,key,value):

        index = self.hashfunc(key)

        if self.table[index] is None: # check if there're no key-value pair in that index,initialize the list.
            self.table[index] = []
        for pair in self.table[index]: # iterate through the pair of index

            if pair[0] == key:
                pair[1] = value
                return
        self.table[index].append([key,value]) # otherwise append [key,value] pair

    def get(self,key):

        index = self.hashfunc(key)

        if self.table[index] is not None:

            for pair in self.table[index]:

                if pair[0] == key:
                    return pair[1]
        return None

    def remove(self,key):
        index = self.hashfunc(key)

        if self.table[index] is not None:
            for i, pair in enumerate(self.table[index]):

                if pair[0] == key:
                    del self.table[index][i]
                    return

# main part to excute our code

hashmap = HashTable(10) # initialize it with the size equals to ten

hashmap.put('cassava', 4)
hashmap.put('banana', 9)
hashmap.put('apple',3)

hashmap.get('apple')
hashmap.get("banana")
hashmap.get('cassava')

hashmap.remove('banana')

print(hashmap.get('banana'))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import click
from kafka import KafkaConsumer
import decode

@click.group()
def cli():
    pass

@cli.command()
@click.argument('host')
@click.argument('topic')
@click.argument('consumer_group')
@click.option('--output_file', default='events_output.json')
def consume(host, topic, consumer_group, output_file):
    params = {
      'group_id': consumer_group,
      'bootstrap_servers': host,
    }
    consumer = KafkaConsumer(topic, **params)

    for message in consumer:
      print(message.topic, message.offset, message.key)
      print(decode.decode_msg(message.value))
      print('--------'*10)

if __name__ == "__main__":
    cli()


import os
import argparse
import configparser
import logging
import tensorflow as tf
import threading
import multiprocessing
from simulator import Simulator
from model import Model
from trainer import (Counter, Trainer, Player,
                     check_dir, init_dir, init_log)

def parse_args():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='option', help="train or evaluate")
    sp_train = subparsers.add_parser('train', help='train agents')
    sp_train.add_argument('--gui', action='store_true', help="shows SUMO gui")
    sp_train.add_argument('--tfboard', action='store_true', help="enable logging to tensorboard")
    sp_train.add_argument('--port', type=int, required=False, default=0, help="port of sumo")
    sp_play=subparsers.add_parser('play', help="play the game")
    sp_play.add_argument('--port', type=int, required=False, default=0, help="port of sumo")
    args = parser.parse_args()
    if not args.option:
        parser.print_help()
        exit(1)
    return args

def init_simulator(config, port=0):
    env = Simulator(config, port=port)
    return env

def train(args):
    cwd = os.getcwd()
    out_dir = os.path.join(cwd, 'out')
    dirs = init_dir(out_dir)
    init_log(dirs['log'])
    config_ini = os.path.join(cwd, 'config', 'config.ini')
    config = configparser.ConfigParser()
    config.read(config_ini)

    # init step counter
    total_step = int(config.getfloat('TRAIN_CONFIG', 'total_step'))
    log_step = int(config.getfloat('TRAIN_CONFIG', 'log_interval'))
    global_counter = Counter(total_step, log_step)

    # number of worker agents
    no_of_workers = multiprocessing.cpu_count()/2  # num of agents

    # init tf
    seed = config.getint('ENV_CONFIG', 'seed')
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))

    # init simulator
    sim = init_simulator(config['ENV_CONFIG'], port=args.port)
    # init agent
    model = Model(sim.n_s_ls, sim.n_a_ls, sim.n_w_ls, sim.n_f_ls, total_step, sess,
                  config['MODEL_CONFIG'])

    with tf.device("/cpu:0"):
        sim = init_simulator(config['ENV_CONFIG'], port=args.port) # terminate inside
        global_ac = Model(sim.n_s_ls, sim.n_a_ls, sim.n_w_ls, sim.n_f_ls, total_step, sess,
                  config['MODEL_CONFIG'], name='global_ac')

        workers = []
        # loop for each workers
        for i in range(no_of_workers):
            i_name = 'W_%i' % i
            port=args.port + i*2
            workers.append(Worker(sim.n_s_ls, sim.n_a_ls, sim.n_w_ls, sim.n_f_ls, total_step, global_ac, sess,
                  config['MODEL_CONFIG'], name= i_name, port=port))

    coord = tf.train.Coordinator()

    # model.load(out_dir + '/model/model')
    # print(sim.n_s_ls, sim.n_a_ls, sim.n_w_ls, sim.n_f_ls)
    summary_writer = None
    if args.tfboard:
        summary_writer = tf.summary.FileWriter(dirs['log'])

    # train from workers
    trainer = Trainer(sim, model, global_counter, summary_writer, output_path=dirs)
    trainer.run(args.gui)

    # save model
    final_step = global_counter.cur_step
    logging.info('Training: save final model at step %d ...' % final_step)
    model.save(dirs['model'])

def play_fn(agent_dir, seeds, port):
    agent = agent_dir.split('/')[-1]
    if not check_dir(agent_dir):
        logging.error('Evaluation: %s does not exist!' % agent)
        return

    # load config file for env
    cwd = os.getcwd()
    config_ini = os.path.join(cwd, 'config', 'config.ini')
    config = configparser.ConfigParser()
    config.read(config_ini)

    # init simulator
    sim = init_simulator(config['ENV_CONFIG'], port=port)
    sim.episode_length_sec=1e6
    sim.init_test_seeds(seeds)

    # init tf
    tf.reset_default_graph()
    tf.set_random_seed(0)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    model = Model(sim.n_s_ls, sim.n_a_ls, sim.n_w_ls, sim.n_f_ls, 0, sess, config['MODEL_CONFIG'])

    model.load(agent_dir + '/model/model')

    sim.agent = agent
    # play
    player = Player(sim, model)
    player.play()

def play(args):
    cwd = os.getcwd()
    out_dir = os.path.join(cwd, 'out')

    # enforce the same evaluation seeds across agents
    threads = []

    thread = threading.Thread(target=play_fn,
                              args=(out_dir, [0], args.port))
    thread.start()
    threads.append(thread)

    for thread in threads:
        thread.join()

if __name__ == '__main__':
    args = parse_args()
    if args.option == 'train':
        train(args)
    else:
        play(args)


import numpy as np
import statistics
import sys
import matplotlib.pyplot as plt

def Average(lst):
	return sum(lst) / len(lst)

results = {}
found = False
cmd = ''
TYPE = ''
count = 0
with open(sys.argv[1], 'r') as file:
	for line in file:
		if found:
			time = line.split('average = ')[1]
			time = time.split(' seconds')[0]
			results[cmd].append(float(time))
			found = False
		if "platform" in line:
			TYPE = line.split('=')[1]
			print("New Type: " + str(TYPE))
		if "executing: " in line.lower():
			cmd = line.split(': ', 1)[1] + " " + str(TYPE)
			if (cmd not in results):
				results[cmd] = []
			found = True

for command in results:
	print("Command: " + str(command.strip('\n')))
	for val in results[command]:
		print(val)

	print('\n')

def convert_to_throughput(lst, size):
	for item in range(0, len(lst)):
		lst[item] = int(size) / (lst[item] * 1000000000) #GB/sec
	return lst

bare = {}
runc = {}
runsc_ptrace = {}
runsc_kvm = {}
mod_ptrace = {}
mod_kvm = {}
bare_std = {}
runc_std = {}
runsc_ptrace_std = {}
runsc_kvm_std = {}
mod_ptrace_std = {}
mod_kvm_std = {}

plot_results = {}
plot_results["bare"] = {}
plot_results["runc"] = {}
plot_results["runsc_ptrace"] = {}
plot_results["runsc_kvm"] = {}
plot_results["mod_ptrace"] = {}
plot_results["mod_kvm"] = {}

for command in results:
	print("Command: " + str(command.strip('\n')))
	size = int(command.split("100000 ")[1].split(" ./")[0])
	avg = Average(convert_to_throughput(results[command], size))
	std = statistics.stdev(results[command])
	if "runsc" in command:
		if "kvm" in command:
			if "mod" in command:
				mod_kvm[size] = avg
				mod_kvm_std[size] = std
				plot_results["mod_kvm"][size] = results[command]

			else:
				runsc_kvm[size] = avg
				runsc_kvm_std[size] = std
				plot_results["runsc_kvm"][size] = results[command]
		else:
			if "mod" in command:
				mod_ptrace[size] = avg
				mod_ptrace_std[size] = std
				plot_results["mod_ptrace"][size] = results[command]

			else:
				runsc_ptrace[size] = avg
				runsc_ptrace_std[size] = std
				plot_results["runsc_ptrace"][size] = results[command]

	elif "runc" in command:
		runc[size] = avg
		runc_std[size] = std
		plot_results["runc"][size] = results[command]

	else: #bare
		bare[size] = avg
		bare_std[size] = std
		plot_results["bare"][size] = results[command]

for size in bare:
	print("Size: " + str(size))
	print("Bare: " + str(bare[size]))
	print("Runc: " + str(runc[size]))
	print("Runsc-ptrace: " + str(runsc_ptrace[size]))
	print("Runsc-kvm: " + str(runsc_kvm[size]))
	print("Mod-ptrace: " + str(mod_ptrace[size]))
	print("Mod-kvm: " + str(mod_kvm[size]))

def sort_keys(mydict):
	mylist = []

	keylist = mydict.keys()
	keylist.sort()
	for key in keylist:
		mylist.append(mydict[key])
	return mylist

final_bare = sort_keys(bare)
final_runc = sort_keys(runc)
final_runsc_ptrace = sort_keys(runsc_ptrace)
final_runsc_kvm = sort_keys(runsc_kvm)
final_mod_ptrace = sort_keys(mod_ptrace)
final_mod_kvm = sort_keys(mod_kvm)

print("Bare: " + str(bare))
print("Runc: " + str(runc))
print("Runsc-ptrace: " + str(runsc_ptrace))
print("Runsc-kvm: " + str(runsc_kvm))
print("Mod-ptrace: " + str(mod_ptrace))
print("Mod-kvm: " + str(mod_kvm))

bare_std = sort_keys(bare_std)
runc_std = sort_keys(runc_std)
runsc_ptrace_std = sort_keys(runsc_ptrace_std)
runsc_kvm_std = sort_keys(runsc_kvm_std)
mod_ptrace_std = sort_keys(mod_ptrace_std)
mod_kvm_std = sort_keys(mod_kvm_std)

n_groups = 5

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.1
opacity = 0.8
print(bare)

rects1 = plt.bar(index, final_bare, bar_width,
yerr = bare_std,
alpha=opacity,
color='0.3',
label='bare')

rects2 = plt.bar(index + 1*bar_width, final_runc, bar_width,
yerr = runc_std,
hatch='-',
alpha=opacity,
color='0.5',
label='runc')

rects3 = plt.bar(index +  2*bar_width, final_runsc_ptrace, bar_width,
yerr = runsc_ptrace_std,
alpha=opacity,
color='0.7',
label='runsc_ptrace')

rects4 = plt.bar(index + 3*bar_width, final_runsc_kvm, bar_width,
yerr = runsc_kvm_std,
alpha=opacity,
hatch='/',
color='0.9',
label='runsc_kvm')

rects5 = plt.bar(index + 4*bar_width, final_mod_ptrace, bar_width,
yerr = mod_kvm_std,
alpha=opacity,
color='r',
label='mod_ptrace')

rects6 = plt.bar(index + 5*bar_width, final_mod_kvm, bar_width,
yerr = runsc_kvm_std,
alpha=opacity,
color='b',
label='mod_kvm')

plt.xlabel('Write Size ')
plt.ylabel('Average Throughput (GB/Sec)')
plt.title('Throughput of Write System Call')
plt.xticks(index + 3*bar_width, ("4KB", "16KB", "64KB", "256KB", "1MB"))

plt.legend(loc = 'upper left')

plt.tight_layout()
plt.show()


import json
import logging
import re

from bs4 import BeautifulSoup
from decimal import Decimal

from storescraper.categories import ALL_IN_ONE, COMPUTER_CASE, \
    EXTERNAL_STORAGE_DRIVE, GAMING_CHAIR, CPU_COOLER, CASE_FAN, HEADPHONES, \
    KEYBOARD, KEYBOARD_MOUSE_COMBO, MEMORY_CARD, MONITOR, MOTHERBOARD, MOUSE, \
    NOTEBOOK, POWER_SUPPLY, PRINTER, PROCESSOR, RAM, SOLID_STATE_DRIVE, \
    STEREO_SYSTEM, STORAGE_DRIVE, TELEVISION, UPS, USB_FLASH_DRIVE, VIDEO_CARD
from storescraper.product import Product
from storescraper.store import Store
from storescraper.utils import html_to_markdown, remove_words, \
    session_with_proxy

class Digiplot(Store):
    @classmethod
    def categories(cls):
        return [
            EXTERNAL_STORAGE_DRIVE,
            STORAGE_DRIVE,
            SOLID_STATE_DRIVE,
            USB_FLASH_DRIVE,
            HEADPHONES,
            STEREO_SYSTEM,
            POWER_SUPPLY,
            COMPUTER_CASE,
            PRINTER,
            RAM,
            MONITOR,
            NOTEBOOK,
            MOTHERBOARD,
            PROCESSOR,
            CPU_COOLER,
            MOUSE,
            KEYBOARD,
            KEYBOARD_MOUSE_COMBO,
            UPS,
            VIDEO_CARD,
            GAMING_CHAIR,
            CASE_FAN,
            MEMORY_CARD,
            ALL_IN_ONE,
            TELEVISION,
        ]

    @classmethod
    def discover_urls_for_category(cls, category, extra_args=None):
        url_extensions = [
            ['almacenamiento/disco-duro-externo-%28sata%29',
             EXTERNAL_STORAGE_DRIVE],
            ['almacenamiento/disco-duro-externo-%28ssd%29',
             EXTERNAL_STORAGE_DRIVE],
            ['almacenamiento/disco-duro-notebook', STORAGE_DRIVE],
            ['almacenamiento/disco-duro-pc', STORAGE_DRIVE],
            ['almacenamiento/disco-ssd-2,5"', SOLID_STATE_DRIVE],
            ['almacenamiento/disco-ssd-m.2', SOLID_STATE_DRIVE],
            ['almacenamiento/memoria-flash-sd-microsd', MEMORY_CARD],
            ['almacenamiento/pendrive', USB_FLASH_DRIVE],
            ['audio/audifono-alambrico', HEADPHONES],
            ['audio/audifono-bluetooth', HEADPHONES],
            ['audio/parlantes-1.1', STEREO_SYSTEM],
            ['audio/parlantes-2.0', STEREO_SYSTEM],
            ['computador-escritorio/all-in-one', ALL_IN_ONE],
            ['gabinetes-y-fuentes/fuente-poder', POWER_SUPPLY],
            ['gabinetes-y-fuentes/fuente-poder-gamer', POWER_SUPPLY],
            ['gabinetes-y-fuentes/gabinete-gamer', COMPUTER_CASE],
            ['gabinetes-y-fuentes/gabinete-slim', COMPUTER_CASE],
            ['gabinetes-y-fuentes/gabinete-vertical', COMPUTER_CASE],
            ['gabinetes-y-fuentes/ventilador-gabinete', CASE_FAN],
            ['impresoras/laser', PRINTER],
            ['impresoras/multifuncion-laser', PRINTER],
            ['impresoras/multifuncion-tinta', PRINTER],
            ['memorias/memoria-gamer-y-grafica', RAM],
            ['memorias/memoria-notebook-%28sodimm%29', RAM],
            ['memorias/memoria-pc-%28udimm%29', RAM],
            ['monitor-y-televisor/monitor-gamer', MONITOR],
            ['monitor-y-televisor/monitor-led', MONITOR],
            ['monitor-y-televisor/monitor-segunda-seleccion', MONITOR],
            ['monitor-y-televisor/televisor-led', TELEVISION],
            ['notebook-y-tablet', NOTEBOOK],
            ['placa-madre', MOTHERBOARD],
            ['procesadores', PROCESSOR],
            ['procesadores/ventilador-cpu', CPU_COOLER],
            ['teclado-y-mouse/mouse-alambrico', MOUSE],
            ['teclado-y-mouse/mouse-bluetooth', MOUSE],
            ['teclado-y-mouse/mouse-gamer', MOUSE],
            ['teclado-y-mouse/mouse-inalambrico', MOUSE],
            ['teclado-y-mouse/teclado', KEYBOARD],
            ['teclado-y-mouse/teclado-y-mouse', KEYBOARD_MOUSE_COMBO],
            ['ups-y-alargador-elec/ups-hasta-900va', UPS],
            ['ups-y-alargador-elec/ups-sobre-1000va', UPS],
            ['video/tarjetas-video', VIDEO_CARD],
            ['sillas/sillas-gamer', GAMING_CHAIR],
        ]

        session = session_with_proxy(extra_args)
        product_urls = []

        for url_extension, local_category in url_extensions:
            if local_category != category:
                continue

            page = 1

            while True:
                if page > 10:
                    raise Exception('Page Overflow')

                if '/' in url_extension:
                    cat = 'categorys'
                else:
                    cat = 'category'

                url = 'https://www.digiplot.cl/product/{}/{}?page={}'\
                    .format(cat, url_extension, page)

                data = session.get(url).text
                soup = BeautifulSoup(data, 'html.parser')
                product_containers = soup.findAll('div', 'single-product')

                if not product_containers:
                    if page == 1:
                        logging.warning('Empty category: {}'.format(
                            url_extension))
                    break

                for container in product_containers:
                    product_url = container.find('a')['href']
                    product_urls.append(product_url)

                page += 1

        return product_urls

    @classmethod
    def products_for_url(cls, url, category=None, extra_args=None):
        print(url)
        session = session_with_proxy(extra_args)
        response = session.get(url, verify=False)

        soup = BeautifulSoup(response.text, 'html.parser')
        data = re.search(r'value_product = ([\s\S]+?)\];',
                         response.text).groups()[0] + ']'
        data = json.loads(data)[0]

        name = data['descripcion'].strip()
        sku = data['idproducto'].strip()

        stock = 0
        stock_containers = soup.findAll(
            'div', 'product-single__availability-item')
        for container in stock_containers:
            stock_text = container.text.split(':')[1].split('unid')[0].strip()
            if 'Mas de' in stock_text:
                stock = -1
                break
            elif stock_text != '':
                stock += int(remove_words(stock_text))

        offer_price = Decimal(data['precioweb1'])
        normal_price = Decimal(data['precioweb2'])

        if offer_price > normal_price:
            return []

        description = None
        if data['long_descrip']:
            description = html_to_markdown(data['long_descrip'])
        picture_urls = [x['href'] for x in soup.findAll('a', 'fancybox')]

        condition_span = soup.find(
            'p', {'id': 'product_condition'}).find('span', 'editable')
        if 'nuevo' in condition_span.text.lower():
            condition = 'https://schema.org/NewCondition'
        else:
            condition = 'https://schema.org/RefurbishedCondition'

        p = Product(
            name,
            cls.__name__,
            category,
            url,
            url,
            sku,
            stock,
            normal_price,
            offer_price,
            'CLP',
            sku=sku,
            description=description,
            condition=condition,
            picture_urls=picture_urls
        )

        return [p]


import logging
from uapc_aiacad.common.abstract_vis import AbstractEvlVis
from uapc_aiacad.visualization.visualization_transformation import *
from uapc_aiacad.transformations.get_most_purchased_and_less_purchased_articles_per_unit_of_measure import *
from uapc_aiacad.common.validation import Validator

logger = logging.getLogger(__name__)

loading_data_format = "delta"

class VisTopAndFlopArtPerUnitEvlJob(AbstractEvlVis):

    def run(self):
        df = self._extract()
        result_dfs = self._visualise(df)
        self._load(result_dfs)

    def _extract(self):
        logger.info("Starting VisCustomersForHours")
        logger.info("input_file_top10_per_unit  = %s", self.config["databricks"]["data"]["output_file_top10_per_unit"])
        logger.info("input_file_less10_per_unit  = %s", self.config["databricks"]["data"]["output_file_less10_per_unit"])
        logger.info("output_file  = %s", self.config["databricks"]["data"]["vis_top_10_per_unit_file"])

        Validator.raise_if_format_type_is_not_valid(loading_data_format)

        try:
            df_top10_per_unit = self.spark.read.format(loading_data_format).load(
                self.config["databricks"]["data"]["output_file_top10_per_unit"])
            df_10_of_the_least_purchased_per_unit = self.spark.read.format(loading_data_format).load(
                self.config["databricks"]["data"]["output_file_less10_per_unit"])
        except ValueError as error:
            logger.error("Invalid path to dataset!")
            raise ValueError("Invalid path to dataset!", str(error))
        # I do not make vis for df with 10 of the least purchased art per unit, because all articles
        # there are with quantity 1.0
        logger.info('Successfully open the input files')
        return df_top10_per_unit, df_10_of_the_least_purchased_per_unit

    def _visualise(self, df):
        df_top10_per_unit, df_10_of_the_least_purchased_per_unit = df

        logger.info("Doing visualisation...")

        logger.info("Visualize df with top 10 articles sold per unit")
        print("----------------------------------------------------------------------------------")
        print("ACTUAL VISUALIZED DF WITH TOP 10 ARTICLES SOLD PER UNIT")
        vis_of_df_top10_per_unit = creating_visualization_from_df(df=df_top10_per_unit,
                                                                  x_axis_col="quantity_per_unit",
                                                                  y_axis_col="names_of_articles_gm",
                                                                  unit_of_measure="unit",
                                                                  top="most")

        logger.info("Displaying without visualize because all 10 articles are with quantity 1.0")

        print("-----------------------------------------------------------------------")
        print("DataFrame with 10 of the least purchased articles per unit.")
        display_df(df_10_of_the_least_purchased_per_unit)
        print("-----------------------------------------------------------------------")

        logger.info('Successfully did the visualization!')

        return vis_of_df_top10_per_unit

    def _load(self, result_df_vis):
        vis_of_df_top10_per_unit = result_df_vis

        logger.info("Writing the vis file to storage...")
        vis_of_df_top10_per_unit.savefig(self.config["databricks"]["data"]["vis_top_10_per_unit_file"])

        logger.info("Saved output successfully.")


import unittest
from selenium import webdriver

class SearchText(unittest.TestCase):
    def setUpClass(self):
        # create a new Firefox session
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        # navigate to the application home page
        self.driver.get("http://www.google.com/")
        # driver.title
    def test_search_by_text(self):
        driver=self.driver()
        # get the search textboxs

        search_field = self.driver.find_element_by_name("q")
        # enter search keyword and submit
        search_field.send_keys("Python class")
        search_field.submit()
        #get the list of elements which are displayed after the search
        #currently on result page using find_elements_by_class_name method
        list_new = self.driver.find_elements_by_class_name("r")
        assertEqual(11, len(list_new))

    def tearDownClass(inst):
        # close the browser window
        inst.driver.quit()

if __name__ == '__main__':
    unittest.main()


import urllib
import time
import shutil
import os
import re
import requests
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium import webdriver

# you need to scroll through the site page manually to load the content
# the number of headers 3 is specified explicitly
# the number of images under each heading can be arbitrary, this is handled by the try catch construct
# the time before downloading is explicitly specified - 3 seconds.

base_path = './icons'

def clear_dir(folder):
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            print('Failed to delete %s. Reason: %s' % (file_path, e))

if not os.path.exists(base_path):
    os.mkdir(base_path)
clear_dir(base_path)

service = Service('./chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://dota2.fandom.com/wiki/Items')
time.sleep(3)
# title k-th                    /html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/h3[k]
# image i-th under title k-th   /html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/div[k+3]/div[i]/a[1]/img
# object with image             /html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/div[k+3]/div[i]
# name of item                  /html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/div[k+3]/div[i]/a[2]

for index_of_title in range(1, 28):
    title_obj = driver.find_element(by=By.XPATH,
                                    value="/html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/h3[{}]".format(index_of_title))
    child_of_title = driver.find_element(by=By.XPATH,
                                         value="/html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/h3[{}]/span".format(index_of_title))
    id = child_of_title.get_attribute('id')
    title_name = re.sub('\s+', '_', id)
    title_name = re.sub('\'', '', title_name)
    title_name = re.sub('-', '_', title_name)
    title_name = title_name.lower()
    folder_name = base_path +'/'+title_name
    if os.path.exists(folder_name):
        clear_dir(folder_name)
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
    index_of_image =  1
    while True:
        try:

            print("index of title {}  index of image {}".format(index_of_title, index_of_image))
            img_element = driver.find_element(by=By.XPATH,
                                              value="/html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/div[{}]/div[{}]/a[1]/img".format(index_of_title+3, index_of_image))
            item_name_object = driver.find_element(by=By.XPATH,
                                                 value="/html/body/div[4]/div[3]/div[3]/main/div[3]/div[1]/div/div[{}]/div[{}]/a[2]".format(index_of_title+3, index_of_image))
            item_name = item_name_object.get_attribute('title')
            item_name = re.sub('\s+','_',item_name)
            item_name = re.sub('\'','',item_name)
            item_name = item_name.lower()
            img_url = img_element.get_attribute('src')
            img_request = requests.get(img_url, stream=True)
            outfile = folder_name + '/' + item_name + '.png'
            with open(outfile, 'wb') as ofstream:
                shutil.copyfileobj(img_request.raw, ofstream)
            index_of_image+=1

        except:
            break

driver.close()


#!/usr/bin/env python
__author__ = 'malonge'

complements = {
    'A': 'T',
    'T': 'A',
    'C': 'G',
    'G': 'C',
    'N': 'N',
    'U': 'A',
    'R': 'Y',
    'Y': 'R',
    'S': 'S',
    'W': 'W',
    'K': 'M',
    'M': 'K',
    'B': 'V',
    'V': 'B',
    'D': 'H',
    'H': 'D'
}

def reverse_complement(seq):
    """
    Reverse complement a nucleotide sequence.
    :param seq: Sequence to be reverse complemented
    :return: A reverse complemented sequence
    """
    rc_seq = ''
    for nuc in seq[::-1]:
        rc_seq += complements[nuc]
    return rc_seq

if __name__ == "__main__":
    import argparse
    from hic_orientation.SeqReader import SeqReader

    parser = argparse.ArgumentParser(description='Orient scaffolds fasta file according to HiCOrient output.')
    parser.add_argument('scaffolds', metavar="<scaffolds.fasta>", type=str, help='Fasta file containing sequences to be oriented.')
    parser.add_argument('orientations', metavar="<orientations.txt>", type=str, help='HiCOrient results tsv file.')
    parser.add_argument('--prefix', type=str, help='Prefix to use for output files.')

    args = parser.parse_args()
    fasta_file = args.scaffolds
    orientations_file = args.orientations
    prefix = args.prefix

    # Make a dictionary of scaffolds and their orientation
    # Need to go in order and make sure that the order of scaffolds that
    # appear in the fasta are consistent with the order of scaffolds in the
    # results file.
    orientation_dict = {}
    orientations = []
    with open(orientations_file, 'r') as f:
        # Discard header
        f.readline()
        for line in f:
            header, orientation, in_block = line.split('\t')
            orientation_dict[header] = orientation
            orientations.append(header)

    # Open the output fasta file. For each input sequence, reverse complement if needed, then write.
    with open(prefix + '.fasta', 'w') as out_file:
        x = SeqReader(fasta_file)
        for (header, sequence), scaffold in zip(x.parse_fasta(), orientations):
            header = header.replace('>', '')
            sequence = sequence.upper()
            # Make sure that the order is consistent across both files.
            if header != scaffold:
                raise ValueError('Fasta and results file are out of order: %s and %s.' % (header, scaffold))
            if header == scaffold:
                if orientation_dict[scaffold] == '+':
                    out_file.write('>' + header + '\n')
                    out_file.write(sequence + '\n')
                else:
                    out_file.write('>' + header + ' - rc\n')
                    out_file.write(reverse_complement(sequence) + '\n')


class Solution:
    def destCity(self, paths: List[List[str]]) -> str:
        """
        ans_dict = {a : b for a,b in paths}
        city_set = {b for a,b in paths}
        for city in city_set:
            if city not in ans_dict:
                return city
        """
        ans_dict = {a : b for a,b in paths}
        for A, B in ans_dict.items():
            if B not in ans_dict:
                return B


import unittest

import plotly.graph_objects as go

from src.priority_queue.operations.time import get_queue_push_time

class TestQueuePerformance(unittest.TestCase):

    def test_performance_vs_queue(self):
        sample: int = 1000
        data = [get_queue_push_time(10, i) for i in range(sample)]

        queue_times = [i[0].values[0] for i in data]
        heap_times = [i[1].values[0] for i in data]

        times = [i for i in range(sample)]

        fig = go.Figure(layout=go.Layout(
            title="Queue performance",
            updatemenus=[dict(type="buttons", direction="right", x=0.9, y=1.16), ],
        ))

        init = 1

        fig.add_trace(
            go.Scatter(x=times[:init],
                       y=queue_times[:init],
                       name="Queue",
                       visible=True,
                       line=dict(color="#33CFA5", dash="dash")))

        fig.add_trace(
            go.Scatter(x=times[:init],
                       y=heap_times[:init],
                       name="Custom Heap",
                       visible=True,
                       line=dict(color="#000000", dash="dash")))

        fig.update(frames=[
            go.Frame(
                data=[
                    go.Scatter(x=times[:k], y=queue_times[:k]),
                    go.Scatter(x=times[:k], y=heap_times[:k])]
            )
            for k in range(0, sample)])

        # Buttons
        fig.update_layout(
            updatemenus=[
                dict(
                    buttons=list([
                        dict(label="Play",
                             method="animate",
                             args=[None, {"frame": {"duration": 100}}])
                    ]))],
            autosize=True
        )

        fig.show()


import numpy as np
import os
import matplotlib.pyplot as plt
import seaborn as sns
from numpy.random import seed
seed(1337)
import tensorflow
tensorflow.compat.v1.disable_eager_execution()
tensorflow.random.set_seed(42)
from tensorflow.python.keras.applications import vgg16
from tensorflow.python.keras.applications.vgg16 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.python.keras.callbacks import ModelCheckpoint
from tensorflow.python.keras import layers, models, Model, optimizers
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

train_data_dir = "dataset/train"
val_data_dir = "dataset/val"
test_data_dir = "dataset/test"
category_names = sorted(os.listdir('dataset/train'))
nb_categories = len(category_names)
img_pr_cat = []
for category in category_names:
    folder = 'dataset/train' + '/' + category
    img_pr_cat.append(len(os.listdir(folder)))
sns.barplot(y=category_names, x=img_pr_cat).set_title("Number of training images per category:")

for subdir, dirs, files in os.walk('data/train'):
    for file in files:
        img_file = subdir + '/' + file
        image = load_img(img_file)
        plt.figure()
        plt.title(subdir)
        plt.imshow(image)
        break

img_height, img_width = 224,224
conv_base = vgg16.VGG16(weights='imagenet', include_top=False, pooling='max', input_shape = (img_width, img_height, 3))

for layer in conv_base.layers:
    print(layer, layer.trainable)

model = models.Sequential()
model.add(conv_base)
model.add(layers.Dense(nb_categories, activation='softmax'))
model.summary()

batch_size = 32

train_datagen =  ImageDataGenerator(
    rescale=1./255
)
test_datagen =  ImageDataGenerator(
    rescale=1./255
)

print('Total number of images for "training":')
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = "categorical")
print('Total number of images for "validation":')
val_generator = test_datagen.flow_from_directory(
val_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = "categorical",
shuffle=False)
print('Total number of images for "testing":')
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = "categorical",
shuffle=False)

learning_rate = 5e-5
epochs = 10
checkpoint = ModelCheckpoint("dance_classifier.h5", monitor = 'val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', save_freq=1)
model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=learning_rate, clipnorm = 1.), metrics = ['acc'])

history = model.fit(train_generator,
                              epochs=epochs,
                              shuffle=True,
                              validation_data=val_generator,
                              callbacks=[checkpoint]
                              )

model.save("dance_classifier.h5")


import unittest

from lib.data.student import Student

class TestStudent(unittest.TestCase):
    def test_first(self):
        last_name = "Da Silva Goncalves Joey"
        first_name = "Joey"
        birth_day = 3
        birth_month = "May"
        birth_year = 1997
        student_id = "0181039342"
        seat = 1

        student = Student(last_name, first_name, birth_day, birth_month, birth_year, student_id, seat)

        self.assertEqual(student.get_month_number(), 5)

    def test_second(self):
        last_name = "Chiller"
        first_name = "Kevin"
        birth_day = 9
        birth_month = "February"
        birth_year = 1991
        student_id = "0161032355"
        seat = 1

        student = Student(last_name, first_name, birth_day, birth_month, birth_year, student_id, seat)

        self.assertEqual(student.get_month_number(), 2)


class Solution:
    def shortestAlternatingPaths(self, n: int, redEdges: List[List[int]], blueEdges: List[List[int]]) -> List[int]:
        def make_adj_list(edge_list):
            res = defaultdict(list)
            for start, end in edge_list:
                res[start].append(end)
            return res
        answer = [-1 for _ in range(n)]
        red_adj = make_adj_list(redEdges)
        blue_adj = make_adj_list(blueEdges)

        visited = set([(0,None)])
        queue = deque([(0,0,None)]) #(node,length,prev_color)
        while queue:
            node,length,color = queue.popleft()

            if answer[node]==-1:
                answer[node]=length

            if color != 'RED':
                for next_node in red_adj[node]:
                    if (next_node,"RED") not in visited:
                        visited.add((next_node,"RED"))
                        queue.append((next_node,length+1,'RED'))
            if color != 'BLUE':
                for next_node in blue_adj[node]:
                    if (next_node,"BLUE") not in visited:
                        visited.add((next_node,"BLUE"))
                        queue.append((next_node,length+1,'BLUE'))

        return answer