import sys
import os
import copy
import torch
import torch.nn as nn
import gym
import my_gym
import hydra
import numpy as np

from omegaconf import DictConfig
from bbrl.utils.chrono import Chrono

from bbrl import get_arguments, get_class
from bbrl.workspace import Workspace
from bbrl.agents import Agents, TemporalAgent

from bbrl_examples.models.loggers import Logger
from bbrl.utils.replay_buffer import ReplayBuffer

from bbrl_examples.models.stochastic_actors import (
    SquashedGaussianActor,
    TunableVarianceContinuousActor,
    DiscreteActor,
)
from bbrl_examples.models.critics import ContinuousQAgent
from bbrl_examples.models.shared_models import soft_update_params
from bbrl_examples.models.envs import create_env_agents

from bbrl.visu.visu_policies import plot_policy
from bbrl.visu.visu_critics import plot_critic

# HYDRA_FULL_ERROR = 1

import matplotlib

matplotlib.use("TkAgg")

# Create the SAC Agent
def create_sac_agent(cfg, train_env_agent, eval_env_agent):
    obs_size, act_size = train_env_agent.get_obs_and_actions_sizes()
    assert (
        train_env_agent.is_continuous_action()
    ), "SAC code dedicated to continuous actions"
    actor = SquashedGaussianActor(
        obs_size, cfg.algorithm.architecture.actor_hidden_size, act_size
    )
    tr_agent = Agents(train_env_agent, actor)
    ev_agent = Agents(eval_env_agent, actor)
    critic_1 = ContinuousQAgent(
        obs_size, cfg.algorithm.architecture.critic_hidden_size, act_size
    )
    target_critic_1 = copy.deepcopy(critic_1)
    critic_2 = ContinuousQAgent(
        obs_size, cfg.algorithm.architecture.critic_hidden_size, act_size
    )
    target_critic_2 = copy.deepcopy(critic_2)
    train_agent = TemporalAgent(tr_agent)
    eval_agent = TemporalAgent(ev_agent)
    train_agent.seed(cfg.algorithm.seed)
    return (
        train_agent,
        eval_agent,
        actor,
        critic_1,
        target_critic_1,
        critic_2,
        target_critic_2,
    )

def make_gym_env(env_name):
    return gym.make(env_name)

# Configure the optimizer
def setup_optimizers(cfg, actor, critic_1, critic_2):
    actor_optimizer_args = get_arguments(cfg.actor_optimizer)
    parameters = actor.parameters()
    actor_optimizer = get_class(cfg.actor_optimizer)(parameters, **actor_optimizer_args)
    critic_optimizer_args = get_arguments(cfg.critic_optimizer)
    parameters = nn.Sequential(critic_1, critic_2).parameters()
    critic_optimizer = get_class(cfg.critic_optimizer)(
        parameters, **critic_optimizer_args
    )
    return actor_optimizer, critic_optimizer

def setup_entropy_optimizers(cfg):
    if cfg.algorithm.target_entropy == "auto":
        entropy_coef_optimizer_args = get_arguments(cfg.entropy_coef_optimizer)
        # Note: we optimize the log of the entropy coef which is slightly different from the paper
        # as discussed in https://github.com/rail-berkeley/softlearning/issues/37
        # Comment and code taken from the SB3 version of SAC
        log_entropy_coef = torch.log(
            torch.ones(1) * cfg.algorithm.entropy_coef
        ).requires_grad_(True)
        entropy_coef_optimizer = get_class(cfg.entropy_coef_optimizer)(
            [log_entropy_coef], **entropy_coef_optimizer_args
        )
    else:
        log_entropy_coef = 0
        entropy_coef_optimizer = None
    return entropy_coef_optimizer, log_entropy_coef

def prepare_critic_loss_data(
    ag_actor, q_agent_1, q_agent_2, target_q_agent_1, target_q_agent_2, rb_workspace
):
    # Compute q_values from both critics with the actions present in the buffer:
    # at t, we have Q(s,a) from the (s,a) in the RB
    q_agent_1(rb_workspace, t=0, n_steps=1)
    q_values_rb_1 = rb_workspace["q_value"]
    q_agent_2(rb_workspace, t=0, n_steps=1)
    q_values_rb_2 = rb_workspace["q_value"]

    with torch.no_grad():
        # Replay the current actor on the replay buffer to get actions of the current policy
        # change this
        ag_actor(rb_workspace, t=1, n_steps=1, stochastic=True, predict_proba=False)
        action_logprobs_next = rb_workspace["action_logprobs"]
        # Compute target q_values from both target critics: at t+1, we have Q(s+1,a+1) from the (s+1,a+1)
        # where a+1 has been replaced in the RB

        target_q_agent_1(rb_workspace, t=1, n_steps=1)
        post_q_values_1 = rb_workspace["q_value"]
        target_q_agent_2(rb_workspace, t=1, n_steps=1)
        post_q_values_2 = rb_workspace["q_value"]

    post_q_values = torch.min(post_q_values_1, post_q_values_2).squeeze(-1)
    return q_values_rb_1, q_values_rb_2, post_q_values, action_logprobs_next

def compute_critic_loss(
    cfg,
    reward,
    must_bootstrap,
    q_values_1,
    q_values_2,
    q_next,
    action_logprobs,
    ent_coef,
):
    # Compute temporal difference
    v_phi = q_next - ent_coef * action_logprobs
    target = (
        reward[:-1][0] + cfg.algorithm.discount_factor * v_phi * must_bootstrap.int()
    )
    td_1 = target - q_values_1.squeeze(-1)
    td_2 = target - q_values_2.squeeze(-1)
    td_error_1 = td_1**2
    td_error_2 = td_2**2
    critic_loss_1 = td_error_1.mean()
    critic_loss_2 = td_error_2.mean()
    return critic_loss_1, critic_loss_2

def prepare_actor_loss_data(ag_actor, q_agent_1, q_agent_2, rb_workspace):
    """Actor loss computation (done before any update)"""
    # Recompute the q_values from the current policy, not from the actions in the buffer
    ag_actor(rb_workspace, t=0, n_steps=1, stochastic=True, predict_proba=False)
    action_logprobs_new = rb_workspace["action_logprobs"]
    q_agent_1(rb_workspace, t=0, n_steps=1)
    q_values_1 = rb_workspace["q_value"]
    q_agent_2(rb_workspace, t=0, n_steps=1)
    q_values_2 = rb_workspace["q_value"]
    current_q_values = torch.min(q_values_1, q_values_2).squeeze(-1)
    return action_logprobs_new, current_q_values

def compute_actor_loss(ent_coef, action_logp, q_values):
    actor_loss = ent_coef * action_logp - q_values
    return actor_loss.mean()

def run_sac(cfg):
    # 1)  Build the  logger
    logger = Logger(cfg)
    best_reward = -10e9
    ent_coef = cfg.algorithm.entropy_coef

    # 2) Create the environment agent
    train_env_agent, eval_env_agent = create_env_agents(cfg)

    # 3) Create the A2C Agent
    (
        train_agent,
        eval_agent,
        actor,
        critic_1,
        target_critic_1,
        critic_2,
        target_critic_2,
    ) = create_sac_agent(cfg, train_env_agent, eval_env_agent)
    ag_actor = TemporalAgent(actor)
    q_agent_1 = TemporalAgent(critic_1)
    target_q_agent_1 = TemporalAgent(target_critic_1)
    q_agent_2 = TemporalAgent(critic_2)
    target_q_agent_2 = TemporalAgent(target_critic_2)
    train_workspace = Workspace()
    rb = ReplayBuffer(max_size=cfg.algorithm.buffer_size)

    # Configure the optimizer
    actor_optimizer, critic_optimizer = setup_optimizers(cfg, actor, critic_1, critic_2)
    entropy_coef_optimizer, log_entropy_coef = setup_entropy_optimizers(cfg)
    nb_steps = 0
    tmp_steps = 0
    # Initial value of the entropy coef alpha. If target_entropy is not auto, will remain fixed
    if cfg.algorithm.target_entropy == "auto":
        target_entropy = -np.prod(train_env_agent.action_space.shape).astype(np.float32)
    else:
        target_entropy = cfg.algorithm.target_entropy

    # Training loop
    for epoch in range(cfg.algorithm.max_epochs):
        # Execute the agent in the workspace
        if epoch > 0:
            train_workspace.zero_grad()
            train_workspace.copy_n_last_steps(1)
            train_agent(
                train_workspace,
                t=1,
                n_steps=cfg.algorithm.n_steps,
                stochastic=True,
                predict_proba=False,
            )
        else:
            train_agent(
                train_workspace,
                t=0,
                n_steps=cfg.algorithm.n_steps,
                stochastic=True,
                predict_proba=False,
            )

        transition_workspace = train_workspace.get_transitions()
        action = transition_workspace["action"]
        nb_steps += action[0].shape[0]
        rb.put(transition_workspace)
        rb_workspace = rb.get_shuffled(cfg.algorithm.batch_size)

        done, truncated, reward, action_logprobs_rb = rb_workspace[
            "env/done", "env/truncated", "env/reward", "action_logprobs"
        ]

        if nb_steps > cfg.algorithm.learning_starts:
            # Determines whether values of the critic should be propagated
            # True if the episode reached a time limit or if the task was not done
            # See https://colab.research.google.com/drive/1erLbRKvdkdDy0Zn1X_JhC01s1QAt4BBj?usp=sharing
            must_bootstrap = torch.logical_or(~done[1], truncated[1])

            (
                q_values_rb_1,
                q_values_rb_2,
                post_q_values,
                action_logprobs_next,
            ) = prepare_critic_loss_data(
                ag_actor,
                q_agent_1,
                q_agent_2,
                target_q_agent_1,
                target_q_agent_2,
                rb_workspace,
            )

            critic_loss_1, critic_loss_2 = compute_critic_loss(
                cfg,
                reward,
                must_bootstrap,
                q_values_rb_1[0],
                q_values_rb_2[0],
                post_q_values[1],
                action_logprobs_next,
                ent_coef,
            )
            logger.add_log("critic_loss_1", critic_loss_1, nb_steps)
            logger.add_log("critic_loss_2", critic_loss_2, nb_steps)
            critic_loss = critic_loss_1 + critic_loss_2

            action_logprobs_new, current_q_values = prepare_actor_loss_data(
                ag_actor, q_agent_1, q_agent_2, rb_workspace
            )

            actor_loss = compute_actor_loss(
                ent_coef, action_logprobs_new[0], current_q_values[0]
            )
            logger.add_log("actor_loss", actor_loss, nb_steps)

            # Entropy coef update part #####################################################
            if entropy_coef_optimizer is not None:
                # Important: detach the variable from the graph
                # so that we don't change it with other losses
                # see https://github.com/rail-berkeley/softlearning/issues/60
                ent_coef = torch.exp(log_entropy_coef.detach())
                # See Eq. (17) of the SAC and Applications paper
                entropy_coef_loss = -(
                    log_entropy_coef * (action_logprobs_rb + target_entropy)
                ).mean()
                entropy_coef_optimizer.zero_grad()
                entropy_coef_loss.backward(retain_graph=True)
                entropy_coef_optimizer.step()
                logger.add_log("entropy_coef_loss", entropy_coef_loss, nb_steps)

            # Actor update part ###################################################################
            actor_optimizer.zero_grad()
            actor_loss.backward()
            torch.nn.utils.clip_grad_norm_(
                actor.parameters(), cfg.algorithm.max_grad_norm
            )

            actor_optimizer.step()
            # Critic update part ############################################################
            critic_optimizer.zero_grad()
            # We need to retain the graph because we reuse the action_logprobs are used
            # to compute both the actor loss and the critic loss
            critic_loss.backward()
            torch.nn.utils.clip_grad_norm_(
                critic_1.parameters(), cfg.algorithm.max_grad_norm
            )
            torch.nn.utils.clip_grad_norm_(
                critic_2.parameters(), cfg.algorithm.max_grad_norm
            )
            critic_optimizer.step()
            #########################################################################################

            # Soft update of target q function
            tau = cfg.algorithm.tau_target
            soft_update_params(critic_1, target_critic_1, tau)
            soft_update_params(critic_2, target_critic_2, tau)
            # soft_update_params(actor, target_actor, tau)

        if nb_steps - tmp_steps > cfg.algorithm.eval_interval:
            tmp_steps = nb_steps
            eval_workspace = Workspace()  # Used for evaluation
            eval_agent(
                eval_workspace,
                t=0,
                stop_variable="env/done",
                stochastic=False,
                predict_proba=False,
            )
            rewards = eval_workspace["env/cumulated_reward"][-1]
            mean = rewards.mean()
            logger.log_reward_losses(rewards, nb_steps)
            print(f"nb_steps: {nb_steps}, reward: {mean}")
            # print("ent_coef", ent_coef)
            if cfg.save_best and mean > best_reward:
                best_reward = mean
                directory = "./sac_agent/"
                if not os.path.exists(directory):
                    os.makedirs(directory)
                filename = (
                    directory
                    + cfg.gym_env.env_name
                    + "#sac#team#"
                    + str(mean.item())
                    + ".agt"
                )
                eval_agent.save_model(filename)
                if cfg.plot_agents:
                    plot_policy(
                        actor,
                        eval_env_agent,
                        "./sac_plots/",
                        cfg.gym_env.env_name,
                        best_reward,
                        # stochastic=False,
                    )
                    plot_critic(
                        q_agent_1.agent,  # TODO: do we want to plot both critics?
                        eval_env_agent,
                        "./sac_plots/",
                        cfg.gym_env.env_name,
                        best_reward,
                    )

@hydra.main(
    config_path="./configs/",
    # config_name="sac_cartpolecontinuous.yaml",
    config_name="sac_pendulum.yaml",
    version_base="1.1",
)
def main(cfg: DictConfig):
    # print(OmegaConf.to_yaml(cfg))
    chrono = Chrono()
    torch.manual_seed(cfg.algorithm.seed)
    run_sac(cfg)
    chrono.stop()

if __name__ == "__main__":
    sys.path.append(os.getcwd())
    main()


import CALIB
import argparse
from helpers import create_logger, create_donemark

uuid = create_logger()

parser = argparse.ArgumentParser(description="Phase calibration.")
parser.add_argument("-m", "--ms", required=True, help="MS to calibrate")
parser.add_argument("-b", "--correct-model-beam", action='store_true', help="Correct model beam (i.e. target obs)")
parser.add_argument("-d", "--donemark", required=True, help="Donemark file")
parser.add_argument("-s", "--skymodel", required=True, help="Path of skymodel to use")
parser.add_argument("-c", "--calsolns", required=True, help="Pathname of calibration solutions")
args = parser.parse_args()

CALIB.ndppp_phasecal(ms=args.ms,
               correctModelBeam=args.correct_model_beam,
               skymodel=args.skymodel,
               keep_parsets=True,
               calsolns=args.calsolns,
               filename_id=uuid)

create_donemark(args.donemark)


import asyncio
import discord
from discord.ext import commands
from config import DISCORD_BOT_TOKEN
import os

intents = discord.Intents.default()
intents.typing = False
intents.presences = True
intents.message_content = True

async def main():
    print(f"Starting Skali Bot...")
    bot = commands.Bot(command_prefix=";", intents=intents)
    async with bot:
        for filename in os.listdir("./cogs"):
            if filename.endswith(".py"):
                await bot.load_extension(f"cogs.{filename[:-3]}")
        await bot.start(DISCORD_BOT_TOKEN)

if __name__ == "__main__":
    asyncio.run(main())


import asyncio

class Manager(object):
    """
    A Manager's job is to manage collections of Measurer's and Senders
    """

    def __init__(self, measurers_and_senders):
        self.measurers_and_senders = measurers_and_senders

    def __init_measurements(self):
        for measurer_group in self.measurers_and_senders:
            measurer_group['measurement'] = None

    def run(self, count=None):
        """
        For every measurer, see if the value has changed, if it has,
        push to senders.
        """
        @asyncio.coroutine
        def check_status(measurer_group):
            new_status = measurer_group['measurer'].measure()

            if new_status != measurer_group['measurement']:
                return new_status
            else:
                return None

        @asyncio.coroutine
        def send_measurement(sender, measurement):
            sender.send(measurement)
            return

        @asyncio.coroutine
        def my_coroutine():
            loop_count = 0
            self.__init_measurements()
            while True:
                for measurer_group in self.measurers_and_senders:  # async for
                    meas_status = yield from check_status(measurer_group)
                    if meas_status is not None:
                        measurer_group['measurement'] = meas_status
                        for sender in measurer_group['senders']:  # async for
                            yield from send_measurement(sender,
                                                   measurer_group['measurement'])

                if count is not None:
                    loop_count += 1
                    if loop_count >= count:
                        break

        loop = asyncio.get_event_loop()
        loop.run_until_complete(my_coroutine())

if __name__ == '__main__':
    Manager('').run()


import json

import pytest
import requests

def pytest_addoption(parser):
    group = parser.getgroup('slack')
    group.addoption(
        '--ssl_verify',
        action='store',
        dest='ssl_verify',
        default=True,
        help='Set the TLS certificate verification'
    )
    group.addoption(
        '--slack_channel',
        action='store',
        dest='slack_channel',
        default=None,
        help='Set the channel name to report'
    )
    group.addoption(
        '--slack_hook',
        action='store',
        dest='slack_hook',
        default=None,
        help='Used for reporting to slack'
    )

    group.addoption(
        '--slack_report_link',
        action='store',
        dest='slack_report_link',
        default=None,
        help='Set the report link'
    )

    group.addoption(
        '--slack_username',
        action='store',
        dest='slack_username',
        default=None,
        help='Set the reporter name'
    )

    group.addoption(
        '--slack_message_prefix',
        action='store',
        dest='slack_message_prefix',
        default=None,
        help='Set a prefix to come before the test result counts.'
    )

    group.addoption(
        '--slack_timeout',
        action='store',
        dest='slack_timeout',
        default=10,
        help='Set the report send timeout'
    )

    group.addoption(
        '--slack_success_emoji',
        action='store',
        dest='slack_success_emoji',
        default=':thumbsup:',
        help='Set emoji for a successful run'
    )

    group.addoption(
        '--slack_failed_emoji',
        action='store',
        dest='slack_failed_emoji',
        default=':thumbsdown:',
        help='Set emoji for a failed run'
    )

    group.addoption(
        '--slack_success_icon',
        action='store',
        dest='slack_success_icon',
        default=None,
        help='Set icon (a url) for a successful run. Overrides slack_success_emoji'
    )

    group.addoption(
        '--slack_failed_icon',
        action='store',
        dest='slack_failed_icon',
        default=None,
        help='Set icon (a url) for a failed run. Overrides slack_failed_icon'
    )

@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(terminalreporter, exitstatus, config):
    yield

    if not config.option.slack_hook:
        return
    # special check for pytest-xdist plugin, cause we do not want to send report for each worker.
    if hasattr(terminalreporter.config, 'workerinput'):
        return
    timeout = config.option.slack_timeout
    failed = len(terminalreporter.stats.get('failed', []))
    passed = len(terminalreporter.stats.get('passed', []))
    skipped = len(terminalreporter.stats.get('skipped', []))
    error = len(terminalreporter.stats.get('error', []))
    xfailed = len(terminalreporter.stats.get("xfailed", []))
    xpassed = len(terminalreporter.stats.get("xpassed", []))

    report_link = config.option.slack_report_link
    slack_hook = config.option.slack_hook
    channel = config.option.slack_channel
    ssl_verify = config.option.ssl_verify
    message_prefix = config.option.slack_message_prefix

    slack_username = config.option.slack_username if config.option.slack_username else 'Regression testing results'

    if int(exitstatus) == 0:
        color = "#56a64f"
        emoji = config.option.slack_success_emoji
        icon = config.option.slack_success_icon
    else:
        color = '#ff0000'
        emoji = config.option.slack_failed_emoji
        icon = config.option.slack_failed_icon

    final_results = 'Passed=%s Failed=%s Skipped=%s Error=%s XFailed=%s XPassed=%s' % (
        passed, failed, skipped, error, xfailed, xpassed)
    if report_link:
        final_results = '<%s|%s>' % (report_link, final_results)
    if message_prefix:
        final_results = '%s: %s' % (message_prefix, final_results)

    results_pattern = {
        "color": color,
        "text": final_results,
        "mrkdwn_in": [
            "text",
            "pretext"
        ]
    }

    payload = {"channel": channel,
               "username": slack_username,
               "attachments": [results_pattern],
               "icon_emoji": emoji if icon is None else None,
               "icon_url": icon}

    requests.post(slack_hook, data=json.dumps(payload), timeout=timeout, verify=ssl_verify)


import json
import fiftyone as fo
from glob import glob
from argparse import ArgumentParser

def main(dataset_dir: str, output_dir: str, dataset_name: str) -> None:

    dataset = fo.Dataset(name=dataset_name, overwrite=True, persistent=True)

    for filepath in glob(f"{dataset_dir}/images/*"):
        sample = fo.Sample(filepath=filepath)
        annotation_path = filepath.replace("images", "annotations").replace(
            "png", "json"
        )
        with open(annotation_path, "r") as f:
            annotation = json.loads(f.read())
            detections = [
                fo.Detection(label=obj["category"], bounding_box=obj["bbox"])
                for obj in annotation["objects"]
            ]
            sample["ground_truth"] = fo.Detections(detections=detections)
        dataset.add_sample(sample)

    dataset.export(
        export_dir=output_dir,
        dataset_type=fo.types.COCODetectionDataset,
        label_field="ground_truth",
    )

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("-d", "--dataset_dir", type=str)
    parser.add_argument("-o", "--output_dir", type=str)
    parser.add_argument("-n", "--dataset_name", type=str)
    args = parser.parse_args()
    main(args.dataset_dir, args.output_dir, args.dataset_name)


import logging
import os
import re
from typing import List

from sqlalchemy import select
from sqlalchemy.orm import Session
from tqdm import tqdm

from ..config import LOG_FMT
from ..paths import PATH_DATA
from ..utils import hashf, make_regex
from .tables import File_Index, Mapping, Param, Regex, Response, Supplier, Unit

logging.basicConfig(level=logging.INFO, format=LOG_FMT, style="{")
logger = logging.getLogger(__name__)

def make_file_index(session: Session):
    stmt = select(Response.hash.distinct())
    to_add = []
    logger.info("Making File Index:")
    for hash1 in tqdm(session.execute(stmt).scalars().all(), desc="[ INFO  ]"):
        path = PATH_DATA / hash1
        if not path.exists():
            continue

        for file in os.listdir(path):
            name, ext = os.path.splitext(file)
            hash2 = hashf(f"{hash1}/{name}.{ext}")
            stmt = select(File_Index.hash2).where(File_Index.hash2 == hash2)

            if session.execute(stmt).first() is not None:
                continue

            to_add.append(File_Index(fname=name, ext=ext, hash=hash1, hash2=hash2))
    session.add_all(to_add)
    session.commit()
    logger.info(f"Added {len(to_add)} files to the index.")

def get_regex(category: str, session: Session) -> List[str]:
    stmt = select(Regex.regex).where(Regex.category == category)
    return session.execute(stmt).scalars().all()

def get_params(session: Session, get_regex=False) -> List[str]:
    stmt = select(Param.param, Param.alias, Param.regex).where(Param.param != None)
    return [
        re.sub("\((?!\?)", "(?:", regex)
        if (regex and get_regex)
        else make_regex(param, alias)
        for param, alias, regex in session.execute(stmt).all()
    ]

def get_units(session: Session) -> Mapping:
    stmt = select(Unit.regex, Unit.unit)
    return {
        re.compile(regex, flags=re.I | re.S): unit
        for regex, unit in session.execute(stmt).all()
    }

def get_supplier_urls(session: Session):
    stmt = select(Supplier.url).where(Supplier.url != None)
    return session.execute(stmt).scalars().all()

def get_keyword_label(session: Session):
    stmt = select(Regex.category.distinct()).where(Regex.id > 0)
    return session.execute(stmt).scalars().all()


import random
import sys
import roll_tables as tables

MAX_FEATURES = 4

class Settlement:
	# Filled constructor
	def __init__(self, name="", size="", features=[], population=0, location="",
		purpose="", resources=[], races=[], gods=[]):
		self.name = name
		self.size = size
		self.features = features
		self.location = location
		self.population = population
		self.purpose = purpose
		self.resources = resources
		self.races = races
		self.gods = gods

	# Roll the dice! Get a random number between start and num_sides (inclusive)
	def roll(self, num_sides, start=1, debug=False):
		return random.randint(start, num_sides)

	def roll_name(self, debug=False):
		first = tables.first_name_element[self.roll(len(tables.first_name_element))]
		second = tables.second_name_element[self.roll(len(tables.second_name_element))]
		name = first + second
		if debug:
			print('Name: ' + name)
		return name

	# handle logic for rolling for X number of features
	def roll_features(self, debug=False):
		num_features = self.roll(MAX_FEATURES)
		features = []
		i = 1
		while i <= num_features:
			k = self.roll(len(tables.features_roll_table))
			if tables.features_roll_table[k] not in features:
				features.append(tables.features_roll_table[k])
				i += 1

		if debug:
			print('Features: ' + str(features))
		return features

	def roll_resources(self, purpose, debug=False):
		num_trade_resources = purpose[1]
		num_metal_resources = purpose[2]
		resource_list = []
		if num_trade_resources > 0:
			i = 1
			while i <= num_trade_resources:
				trade_roll = self.roll(len(tables.trade_resource_roll_table))
				resource = tables.trade_resource_roll_table[trade_roll]
				if resource not in resource_list:
					resource_list.append(resource)
					i += 1
		if num_metal_resources > 0:
			metal_roll = self.roll(len(tables.metal_roll_table))
			resource_list.append(tables.metal_roll_table[metal_roll])
		if debug:
			print('Resources: ' + str(resource_list))
		return resource_list

	def roll_races(self, debug=False):
		race_roll = self.roll(len(tables.race_roll_table))
		main_race = tables.race_roll_table[race_roll]
		if debug:
			print('Main Race: ' + main_race)
		race_list = []
		if main_race == 'Elves':
			race_list.append(tables.elf_roll_table[self.roll(len(tables.elf_roll_table))])
		elif main_race == 'Orcs':
			race_list.append(tables.orc_roll_table[self.roll(len(tables.orc_roll_table))])
		elif "Mix" in main_race:
			i = int(main_race[0])
			roll_range_start = 3
			roll_range_end = len(tables.race_roll_table) - 2
			k = 1
			while k <= i:
				new_race = tables.race_roll_table[self.roll(roll_range_end, roll_range_start)]
				if new_race == 'Elves':
					new_race = tables.elf_roll_table[self.roll(len(tables.elf_roll_table))]
				elif new_race == 'Orcs':
					new_race = tables.orc_roll_table[self.roll(len(tables.orc_roll_table))]

				if new_race not in race_list:
					race_list.append(new_race)
					k += 1
		else:
			race_list.append(main_race)

		if debug:
			print('Races: ' + str(race_list))
		return race_list

	def roll_gods(self, debug=False):
		gods_roll = self.roll(len(tables.gods_roll_table))
		main_god = tables.gods_roll_table[gods_roll]
		if debug:
			print('Main God: ' + main_god)
		god_list = []
		if "Mix" in main_god:
			i = int(main_god[0])
			roll_range_start = 2
			roll_range_end = len(tables.gods_roll_table) - 2
			k = 1
			while k <= i:
				new_god = tables.gods_roll_table[self.roll(roll_range_end, roll_range_start)]
				if new_god not in god_list:
					god_list.append(new_god)
					k += 1
		else:
			god_list.append(main_god)

		if debug:
			print('Gods: ' + str(god_list))
		return god_list

	def printArray(self, arr):
		x = len(arr)
		y = len(arr) - 1
		result = ""
		if x == 1:
			result += arr[0]
		else:
			for i in range(x):
				if i == y:
					result += "and {}".format(arr[i])
				else:
					result += "{}, ".format(arr[i])
		return result

	# convert the object to a nicely formatted string
	def toPrettyString(self):
		sentence = '{} is a {} of about {} people, located in {}. '.format(self.name, self.size, self.population, self.location)
		sentence += 'Its residents are comprised of {}. The main god(s) worshipped here is/are {}. '.format(self.printArray(self.races), self.printArray(self.gods))
		sentence += 'It was built as a {}, with resources such as {}. It has a some notable features, such as {}'.format(self.purpose, self.printArray(self.resources), self.printArray(self.features))
		return sentence

	def toCSVString(self):
		return 'nothing for now'

	# Generate a settlement and fill it in
	def generate_settlement(self, debug=False):
		name = self.roll_name(debug)
		features = self.roll_features(debug)
		races = self.roll_races(debug)
		gods = self.roll_gods(debug)

		size = tables.size_roll_table[self.roll(len(tables.size_roll_table))]
		if debug:
			print('size: ' + size)

		population_tuple = tables.population_roll_table[size]
		population = (self.roll(population_tuple[0]) * population_tuple[1]) + population_tuple[2]
		if debug:
			print('Population: ' + str(population))

		location = tables.location_roll_table[self.roll(len(tables.location_roll_table))]
		if debug:
			print('Location: ' + location)

		purpose_roll = tables.purpose_roll_table[self.roll(len(tables.purpose_roll_table))]
		purpose = purpose_roll[0]
		if debug:
			print('Purpose: ' + purpose)
		resources = self.roll_resources(purpose_roll, debug)

		self.name = name
		self.features = features
		self.races = races
		self.gods = gods
		self.population = population
		self.location = location
		self.purpose = purpose
		self.resources = resources
		self.size = size
		return self
		#return self.__init__(name, size, features, population, location, purpose, resources, races, gods)

def __main__():
	nargs = len(sys.argv)

	if nargs > 2:
		print("Error: There should not be any arguments passed via CLI")
		return 1

	if nargs == 1:
		debug = False
	elif nargs == 2:
		if sys.argv[1] == 'True':
			debug = True
		elif sys.argv[1] == 'False':
			debug = False

	# Create instance of settlement class for generator use
	settlement_instance = Settlement()

	# Ask for input (currently pseudo code)
	print('How many settlements would you like to generate?: ')
	num_settlements = int(raw_input())

	# Create basic gui

	# Output to csv

	# Generator loop

	if num_settlements <= 0:
		print("Error: Please input a non-zero number!")
		return 1
	else:
		# 'range' is not inclusive of the end
		for i in range(0, num_settlements):
			print('--------NEW SETTLEMENT---------')
			s = settlement_instance.generate_settlement(debug)
			print(s.toPrettyString())
	return 0

__main__()


import datetime
from django.db import models

from ckeditor.fields import RichTextField

from base.models import BaseModel
from apps.home.utils import shp_file_directory_path

class SHP(BaseModel):
    name = models.CharField(
        max_length=50
    )
    description = RichTextField(
        max_length=1000,
        blank=True
    )
    file = models.FileField(
        upload_to=shp_file_directory_path
    )
    uploaded_date = models.DateField(
        default=datetime.date.today,
        blank=True
    )

    def __str__(self) -> str:
        return self.name

    class Meta:
        ordering = ('created_at',)
        verbose_name = "SHP"
        verbose_name_plural = "SHP"
        db_table = "shp"


## course scheduler

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        """
        Algorithm DFS:
        1. initialize two boolean list.(fixed size)
        2. iterate prerequistes and built a hash map:
            key: course:
            value: a set of precourse
        3. recursively check if course not in visited, check if it's prerequests in stack or not.

        1. set up a dict
            key: course
            value: a set of precourse
        2. initialize a stack with first course
        3. while stack is not empty:
            put the precourse of curr course into stack,
                if precourse is in stack, return False
                else: continue
        4. return True if stack is empty
        """
        def isCyclic(c, visited, stack):
            visited[c] = True
            stack[c] = True
            # print(c)
            # print(visited)
            # print(stack)
            if c in dic:
                for pre in dic[c]:
                    if visited[pre] == False:
                        if isCyclic(pre, visited, stack) == True:
                            return True
                    elif stack[pre] == True:
                        return True
            stack[c] = False
            return False

        if prerequisites:
            dic = {}
            for pair in prerequisites:
                course = pair[0]
                pre_co = pair[1]
                if course in dic:
                    dic[course].add(pre_co)
                else:
                    dic[course] = {pre_co}
            # print(dic)

            visited = [False] * numCourses
            stack = [False] * numCourses

            for i in range(numCourses):
                if isCyclic(i, visited, stack) == True:
                    return False
        return True


"""
Modify mesh vertex positions.
"""
from vtkplotter import *

t = Text2D(__doc__)

dsc = Disc().lineWidth(0.1)
coords = dsc.points()

for i in range(50):
    coords[:,2] = sin(i/10.*coords[:,0])/5 # move vertices in z
    dsc.points(coords)  # modify mesh
    show(dsc, t, resetcam=not i, interactive=0) # resetcam only for i=0

interactive()


import unittest
from thesis.src.data.datamodule import DataModule
from thesis.src.utils import utils
from pprint import pprint

class TestDataModule(unittest.TestCase):
    def __init__(self, methodName: str = "runTest") -> None:
        super().__init__(methodName)
        self.model_name = "mt5"
        self.task_names = ["paws-x", "nc", "wpr"]

    def test_task_names(self):
        data_module = DataModule(model_name = self.model_name, task_names=self.task_names)
        self.assertEqual(data_module.task_names, self.task_names, 'task names must match')

    def test_load_data(self):
        data_module = DataModule(model_name = self.model_name, task_names=self.task_names)
        data = data_module.load_data(self.task_names)
        self.assertEqual(sorted(data), sorted(self.task_names), 'task names must match')

    def test_load_wpr_guids(self):
        task_names = ["wpr"]
        data_module = DataModule(model_name = self.model_name, task_names=task_names)
        data = data_module.load_data(task_names)
        data = utils.format_columns(data[task_names[0]], task_names[0], to_text=True)
        self.assertEqual(len(data["train"].features), 4, 'guids must be added')

    def test_prepare_data(self):
        # data_module = DataModule(model_name = self.model_name, task_names=self.task_names)
        # data_module.prepare_data()
        # print(data_module.)
        pass

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


# Реализация матриц через ООП

class Matrix:
    def __init__(self, matrix: list):
        self.matrix = matrix

        self.__column_start = -1
        self.__row_start = -1
        self.__i = -1
        self.__l = 0

        self._rows = len(matrix)
        self._columns = len(matrix[0])

        self._is_square = True if len(matrix) == len(matrix[0]) else False

        self._is_zero = True
        for row in matrix:
            for i in row:
                if i != 0:
                    self._is_zero = False

        self._is_identity = False
        if self._is_square is True:
            for index, row in enumerate(matrix):
                if row[index] == 1 and row.count(0) == (len(row) - 1):
                    self._is_identity = True
                else:
                    self._is_identity = False

    def info(self):
        print(f'Размер: {self._rows}x{self._columns}', end=". ")
        if self._is_identity is True:
            print('Единичная')
        elif self._is_zero is True:
            print('Нулевая')
        else:
            print('')

    def __str__(self):
        str_matrix = []
        for line in self.matrix:
            str_matrix.append(f'{" ".join(map(str, line))}\n')
        return ''.join(str_matrix)

    def transpose(self):
        result_matrix = []
        for i in range(self._columns):
            line = []
            for j in range(self._rows):
                line.append(self.matrix[j][i])
            result_matrix.append(line)
        return Matrix(result_matrix)

    def __add__(self, other):
        if self._rows != other._rows or self._columns != other._columns:
            print('Невозможно выполнить сложение матриц разной размерности!')
        else:
            result_matrix = []
            for i, line in enumerate(self.matrix):
                result_matrix.append(list(map(lambda x,y: x + y, self.matrix[i], other.matrix[i])))
            return Matrix(result_matrix)

    def __sub__(self, other):
        if self._rows != other._rows or self._columns != other._columns:
            print('Невозможно выполнить вычитание матриц разной размерности!')
        else:
            result_matrix = []
            for i, line in enumerate(self.matrix):
                result_matrix.append(list(map(lambda x,y: x - y, self.matrix[i], other.matrix[i])))
            return Matrix(result_matrix)

    def __mul__(self, other):
        result_matrix = []
        if isinstance(other, Matrix) is True:
            if self._columns != other._rows:
                print('Матрицы несовместимы: число столбцов первой матрицы не равно числу строк второй!')
            else:
                for i in range(self._rows):
                    result_line = []
                    for j in range(other._columns):
                        result_num = []
                        for k in range(self._columns):
                            result_num.append(self.matrix[i][k] * other.matrix[k][j])
                        result_line.append(sum(result_num))
                    result_matrix.append(result_line)
                return Matrix(result_matrix)
        elif isinstance(other, (int, float, complex)):
            for row in self.matrix:
                result_matrix.append(list(map(lambda x: x * (other), row)))
            return Matrix(result_matrix)

    def minor(self, elem:tuple):
        result_matrix = self.matrix
        result_matrix.pop(elem[0])
        for row in result_matrix:
            row.pop(elem[1])
        return Matrix(result_matrix)

    def determinant(self):
        if self._is_square is False:
            print('Невозможно вычислить определитель неквадратной матрицы!')
        else:
            if self._rows == 2:
                det = self.matrix[0][0]*self.matrix[1][1] - self.matrix[0][1]*self.matrix[1][0]
                return det
            else:
                minors = []
                for i in range(self._columns):
                    minors.append(self.minor((0, i)))
                dets = []
                for dett in minors:
                    dets.append(dett.determinant())
                res = []
                for index, i in enumerate(self.matrix[0]):
                    res.append((-1)**index*i)
                det = sum(map(lambda x, y: x*y, res, dets))
                return det

a = Matrix([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
b = Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
g = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
d = Matrix([[1, 0, 0]])
e = Matrix([[0, 0, 0]])
f = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])
c = Matrix([[9, 8, 7,55, 98, 5], [6, 5, 4, 16, 14, 1], [3, 2, 1, 57, 99, 16]])

s = Matrix([[0, 1, 0, 1, 2], [3, 4, 0, 1, 2]])
l = Matrix([[0, 1, 2], [3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]])
x = s*l

print(a.determinant())
l = Matrix([[3, 4], [6, 7]])
print(a.minor((0,2)).matrix[0])

p = Matrix([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])

print(p)
print(p.minor((2,4)))
print(l.determinant())


from django.db import migrations
from tree.fields import PathField
from tree.operations import (
    CreateTreeTrigger, RebuildPaths, DeleteTreeTrigger,
)
from tree.sql.base import ALPHANUM_LEN

class Migration(migrations.Migration):
    dependencies = [
        ('tests', '0002_add_tmp_model'),
    ]

    operations = [
        migrations.AddField('Something', 'path', PathField(order_by=('name',), max_siblings=ALPHANUM_LEN)),
        CreateTreeTrigger('Something'),
        RebuildPaths('Something'),
        migrations.AlterField('Something', 'path', PathField(order_by=('name',), max_siblings=ALPHANUM_LEN*3)),
        DeleteTreeTrigger('Something'),
        migrations.DeleteModel('Something'),
    ]


from imutils.video import VideoStream
import time
import cv2
import argparse
from pathlib import Path

def show_frame(frame, count, msg):
    width, height = frame.shape[:2]
    cv2.putText(frame, f"CountDown: {str(count)}: {msg}", (width // 2, height // 2), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    cv2.imshow("PhotoBooth", frame)

    key = cv2.waitKey(3) & 0xFF

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-d", "--dataset", required=True,
                    help="path to root dataset directory. A directory with name parameter will be created")
    ap.add_argument("-n", "--name", required=True, help="Name of person")
    ap.add_argument("-c", "--count", required=True, help="Number of pictures take")
    args = vars(ap.parse_args())

    vs = VideoStream(src=0).start()
    time.sleep(0.2)

    data_path = Path(args['dataset'])
    data_path =  data_path / args['name']
    data_path.mkdir(parents=True, exist_ok=True)

    # loop over frames from the video file stream
    for i in range(0, int(args['count'])):
        count_down = 3
        print(f"Grab image: {i}")
        count_down_time = time.time()
        frame = vs.read()
        show_frame(frame, str(count_down), f"Picture {i+1} of {args['count']}")
        while True:
            # grab the frame from the threaded video stream
            frame = vs.read()
            if time.time() - count_down_time > 1:
                count_down -= 1
                if count_down > 0:
                    count_down_time = time.time()
                    show_frame(frame, count_down, f"Picture {i+1} of {args['count']}")
                else:
                    new_image_file = data_path / f"{args['name']}_{i}.png"
                    cv2.imwrite(str(new_image_file.absolute()), frame)
                    break


#!/usr/bin/python3
"""Unit test of the requirements for the proyect in general"""

import unittest
import os

class Test_Unittests(unittest.TestCase):
    """Class to test the proyect"""

    def test_unittest(self):
        '''Check the unittest files requirements:
        - existence of all files and directories in the correct location
        - all the python scripts are executable
        - all the files end in a new line
        - all the files start with a #!/usr/bin/python
        - all the files are written with the pycodestyle standard
        - there is a readme
        - all the modules are documented
        - all the classes are documented
        - all the functions are documented
        '''

        self.assertTrue(os.path.isfile('./tests/__init__.py'))
        self.assertTrue(os.path.isfile('./tests/test_models/__init__.py'))

        # P EP8
        pep8 = "pep8 --count ./tests"
        self.assertEqual(os.system(pep8), 0)

        test_list = [
                    './tests/test_test.py',
                    './tests/test_project.py',
                    './tests/test_console.py',
                    './tests/test_models/test_base_model.py',
                    './tests/test_models/test_user.py',
                    './tests/test_models/test_city.py',
                    './tests/test_models/test_review.py',
                    './tests/test_models/test_amenity.py',
                    './tests/test_models/test_place.py']

        for fil in test_list:
            # Existence of all files and directories in the correct location
            self.assertTrue(os.path.isfile(fil), fil)

            # Files are executable
            self.assertTrue(os.access(fil, os.X_OK), fil)

            # First and last line
            with open(fil) as f:
                first = f.readline()
                last = f.read()[-47:]
                self.assertTrue(first == '#!/usr/bin/python3\n', first)
                line = "if __name__ == '__main__':\n    unittest.main()\n"
                self.assertEqual(last, line, fil)

            # Documentation of module
            self.assertTrue(len(fil.__doc__) > 5, fil)

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


#!/usr/bin/python
import os
import sys
from collections import defaultdict

def read_expfile(file_file):
  infile = open(file_file, 'r')
  exp_dict = {}
  low_count_pos = []
  for line in infile.readlines():
    if 'pos' in line: continue
    info = line.rstrip().rsplit("\t")
    if len(info) < 4:
      resi, pos = info
      low_count_pos.append(pos)
    else:
      resi, pos, count, exp = info
      if float(count) >= 6:
        exp_dict[pos] = float(exp)
      else:
        low_count_pos.append(pos)
  infile.close()
  print ('Positions with low counts:\n'+'+'.join(low_count_pos))
  return (exp_dict)

def normalizing_exp(exp_dict):
  max_exp  = max(exp_dict.values())
  min_exp  = min(exp_dict.values())
  print ('exp range (pre-normalized): %f to %f' % (min_exp, max_exp))
  for pos in exp_dict.keys():
    exp = exp_dict[pos]
    exp = (exp-min_exp)/(max_exp-min_exp)*100
    exp_dict[pos] = exp
  print ('exp range (post-normalized): %f to %f' % (min(exp_dict.values()), max(exp_dict.values())))
  return (exp_dict)

def add_exp_to_pdb(exp_dict, pdb_file):
  assert('.pdb' in pdb_file)
  new_pdb_file = pdb_file.replace('.pdb', '_exp.pdb')
  print ("writing: %s" % new_pdb_file)
  infile  = open(pdb_file, 'r')
  outfile = open(new_pdb_file,'w')
  for line in infile.readlines():
    if "ATOM" == line[0:4]:
      pos      = int(line[23:26])
      chain    = line[21:23].rstrip()
      b_factor = line[60:66]
      if chain == 'A' and str(pos) in exp_dict.keys():
        exp = str(round(exp_dict[str(pos)],2))
        exp = exp+'0'*(2-len(exp.rsplit('.')[-1]))
        exp = ' '*(6-len(exp))+exp
        new_line = line[0:60]+exp+line[66::]
        outfile.write(new_line)
      else:
        outfile.write(line)
    else:
      outfile.write(line)
  outfile.close()

def main():
  pdb_file = "PDB/7b62.pdb"
  exp_file = 'result/NTD_DMS_scores_by_resi.tsv'
  exp_dict = read_expfile(exp_file)
  exp_dict = normalizing_exp(exp_dict)
  add_exp_to_pdb(exp_dict, pdb_file)

if __name__ == "__main__":
  main()


#!/usr/bin/env python3
import datetime
import sys
from configparser import ConfigParser
from time import sleep

from src.client import Client
import logging
import os

def convert_time_to_seconds(time_str):
    time_str = time_str.strip('"')
    minutes, seconds = time_str.split('m')
    seconds = seconds.rstrip('s')
    return int(minutes) * 60 + int(seconds)

def initialize_config():
    """ Parse env variables or config file to find program config params

    Function that search and parse program configuration parameters in the
    program environment variables first and the in a config file.
    If at least one of the config parameters is not found a KeyError exception
    is thrown. If a parameter could not be parsed, a ValueError is thrown.
    If parsing succeeded, the function returns a ConfigParser object
    with config parameters
    """

    config = ConfigParser(os.environ)
    # If config.ini does not exists original config object is not modified
    config.read("config/config.ini")

    config_params = {}
    try:
        config_params["server_address"] = os.getenv('SERVER_ADDRESS', config["DEFAULT"]["SERVER_ADDRESS"])
        config_params["query_address"] = os.getenv('QUERY_ADDRESS', config["DEFAULT"]["QUERY_ADDRESS"])
        config_params["logging_level"] = os.getenv('LOGGING_LEVEL', config["DEFAULT"]["LOGGING_LEVEL"])
    except KeyError as e:
        raise KeyError("Key was not found. Error: {} .Aborting server".format(e))
    except ValueError as e:
        raise ValueError("Key could not be parsed. Error: {}. Aborting server".format(e))

    return config_params

def main():
    config_params = initialize_config()
    logging_level = config_params["logging_level"]
    server_address = config_params["server_address"]
    query_address = config_params["query_address"]
    initialize_log(logging_level)

    # Log config parameters at the beginning of the program to verify the configuration
    # of the component
    logging.debug(f"action: config | result: success | "
                  f"server_address: {server_address} | log_level: {logging_level}")

    sleep(20)

    # Initialize client
    client = Client(server_address, query_address)
    client.run()

def initialize_log(logging_level):
    """
    Python custom logging initialization

    Current timestamp is added to be able to identify in docker
    compose logs the date when the log has arrived
    """
    logging.basicConfig(
        format='%(asctime)s %(process)d %(levelname)-8s %(message)s',
        level=logging_level,
        datefmt='%Y-%m-%d %H:%M:%S',
    )

if __name__ == "__main__":
    main()


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

x = np.arange(0,30,0.1)
y = np.sin(x)
plt.plot(x,y,label="f(x)=sin(x)")
plt.xlabel("Oś OX")
plt.ylabel("Oś OY")
plt.title("Wykres funkcji sin(x)")
plt.legend()
plt.show()


#============================================================
# Import modules
#============================================================
import re, os, fnmatch, sys, easygui as eg
from numpy import *
from matplotlib import *
from pylab import *
import asciitable, string, datetime, shutil, atpy

# Declare constant values
secPerYear = 3600.0 * 24.0 * 365.0
rJup = 7.1492e9
rSun = 6.955e10
LSun = 3.9e33

# Select a directory/run to analyze
savedirBase = eg.diropenbox(msg='Pick a run directory to examine')

# Load the atmos summary data
atmos = atpy.Table(savedirBase+'/atmos_data.txt',type='ascii')

# Plot the atmos data
plt.figure(1)
plot(atmos.Time/secPerYear,atmos.R/rJup,'o',markersize=10)
plt.xlabel('Time (years)')
plt.ylabel('Radius of tau = 2/3 surface (Rjup)')
show()

plt.figure(2)
plot(atmos.Teff,atmos.L/LSun,'o',markersize=10)
plt.xlabel('Teff (K)')
plt.xlim([1e3,0.0])
plt.ylabel('Luminsity (Lsun)')
plt.yscale('log')
show()


from copy import deepcopy
import numpy as np
from clients.base import BaseClient

class FedProxClient(BaseClient):
    def __init__(self, model, optimizer, criterion, dataloader, scheduler=None):
        super().__init__(model, optimizer, criterion, dataloader, scheduler)

    def local_update(self, epoch):
        global_model = deepcopy(self.model)
        self.model.train()
        for i in range(epoch):
            for inputs, targets in self.dataloader:
                inputs = inputs.to(self.device)
                targets = targets.to(self.device)

                self.optimizer.zero_grad()

                logits = self.model(inputs)
                loss = self.criterion(logits, targets)

                loss.backward()
                self.optimizer.step(global_model.parameters())

    def receive_param(self, state_dict):
        self.model.load_state_dict(state_dict)


import logging

from . import stop_category_calculator, transport_group_retriever, stop_grade_calculator
from ..types import TransportGroups, TransportStopCategories, TransportStopGradings

logger = logging.getLogger(__name__)

def calculate_quality_grades(registry: dict, params: list) -> None:
    cli = registry['ui']
    cli.setup_cli(params, registry)

    stop_grade_calculator.prepare_calculation(registry)
    transport_groups: TransportGroups = transport_group_retriever.get_transport_groups(registry)

    output_writer = registry['output_writer']
    config = registry['config']

    for due_date_config in config['due-dates']:
        logger.info(f"Calculate public transport quality grades with due date "
                    f"{due_date_config['due-date'].strftime('%Y-%m-%d')}, {due_date_config['type-of-interval']}")
        stop_categories: TransportStopCategories = \
            stop_category_calculator.get_stop_categories(registry, due_date_config, transport_groups)

        stop_gradings: TransportStopGradings = stop_grade_calculator.calculate_stop_grades(
            registry, stop_categories)

        output_writer.write_gradings(config['output'], due_date_config, stop_gradings)

        gradings_with_isochrones = [(uic_ref, gradings) for uic_ref, gradings in stop_gradings.items()
                                    if gradings]

        logger.debug(f"Found {len(gradings_with_isochrones)} stops with isochrones")

    metadata_writer = registry['metadata_writer']
    metadata_writer.write_metadata(config['output'], config['due-dates'])

    output_writer.write_transport_stops(config['output'], [*transport_groups])


from django.core.mail import send_mail
from django.conf import settings
import requests

def send_mail_appointment(to, datetime, pediatrician):
    message = 'Remember that you have an appointment with pediatrician {pediatrician} for the {date} at {time}'.format_map({
            'pediatrician': pediatrician,
            'date': datetime,
            'time': datetime })
    return requests.post(
                        "{mailgun_url}/messages".format_map({'mailgun_url': settings.MAILGUN_API_BASE_URL + '/' + settings.MAILGUN_DOMAIN}),
                        auth=("api", settings.MAILGUN_API_KEY),
                        data={
                            "from": "noreply@sandboxc3b6f01b10144878aae6b0830d2cfe4b.mailgun.org",
                            "to": to,
                            "subject": "Appointment with pediatrician",
                            "text": message})


#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
#encoding=utf-8

'''
Created on 2014年6月17日

@author: kerry
'''
from scheudler import robot_protocol
from base.miglog import miglog
from multiprocessing import Process,Pool,Pipe
from scheudler.RobotNetSvc import MIGRobotInitialScheduler
from scheudler.assistant_netservice import MIGAssistantInitialScheduler
from chat.netservice import MIGSchedulerClient as MIGChatInitialScheduler
from pub.Config import SingletonConfig
#from scheudler.chat_netservice import MIGChatInitialScheduler

def RobotChatLogin(data):
    miglog.log().debug(data["platform"])
    miglog.log().debug(data["uid"])
    robot_chat_client = MIGChatInitialScheduler()
    robot_chat_client.set_platform_id(data["platform"])
    robot_chat_client.set_uid(data["robotid"])
    robot_chat_client.set_oppid(data["uid"])
    robot_chat_client.set_token("token")
    robot_chat_client.set_oppo_type(1)
    #robot_chat_client.Connection("42.121.14.108", 17000)
    robot_chat_client.Connection(SingletonConfig.chathost,SingletonConfig.chatport)
    robot_chat_client.start_run()

def RobotLogin(data):
    robot = data["robot"]
    miglog.log().debug(robot.get_latitude())
    miglog.log().debug(robot.get_longitude())
    miglog.log().debug(robot.get_uid())
    robot_client = MIGRobotInitialScheduler()
    robot_client.set_platform_id(data["platform"])
    robot_client.set_robot_id(robot.get_uid())
    robot_client.set_uid(data["uid"])

    host = SingletonConfig().robothost
    port = SingletonConfig().robotport

    robot_client.Connection(SingletonConfig().robothost,SingletonConfig().robotport)
    #robot_client.Connection("112.124.49.59",19008)
    robot_client.start_run()

def AssistantLogin(data):
    miglog.log().debug(data["platform"])
    miglog.log().debug(data["uid"])
    assistant_client = MIGAssistantInitialScheduler()
    assistant_client.set_platform_id(data["platform"])
    assistant_client.set_uid(data["uid"])
    assistant_client.set_nickname(data["nickname"])
    assistant_client.Connection(SingletonConfig.robothost, int(SingletonConfig.robotport))
    assistant_client.start_run()

class RobotUserMgr(object):
    '''
    classdocs
    '''

    def __init__(self,host,port):
        '''
        Constructor
        '''
        self.platform_id = 0
        self.host = host
        self.port = port

    def SchdulerLogin(self,platform_id,machine_id):
        self.platform_id = platform_id
        login = robot_protocol.LoginPacket()
        login.make_head(2000,1, 0, 0)
        login.set_platform_id(platform_id)
        login.set_machine_id(machine_id)
        return login.packstream()

    def NoticeRobotChat(self,data):
        robot_chat_login = robot_protocol.NoticeRobotChatLogin()
        robot_chat_login.unpackstream(data)
        pool = Pool(processes=1)
        element = {}
        element["platform"] = robot_chat_login.get_platform_id()
        element["uid"] = robot_chat_login.get_uid()
        element["robotid"] = robot_chat_login.get_robotid()
        result = pool.apply_async(RobotChatLogin, [element])
        pool.close()

    def NoticeAssistantInfo(self,data):
        assistant_login = robot_protocol.NoticeAssistantLogin()
        assistant_login.unpackstream(data)
        pool = Pool(processes=1)
        element = {}
        element["platform"] = assistant_login.get_platformid()
        element["uid"] = assistant_login.get_assistantid()
        element["nickname"] = assistant_login.get_nickname()
        result = pool.apply_async(AssistantLogin, [element])
        pool.close()
        #pool.join()

    def NoticeRobotInfo(self,data):
        robotlist =[ 0 for i in range(0)]
        robot_login = robot_protocol.NoticeRobotLogin()
        robot_login.unpackstream(data)
        robot_list = robot_login.getrobotlist()
        miglog.log().debug(len(robot_list))
        #独立用户开辟一个进程 用户模拟用户操作  传送机器人信息和用户id
        if(len(robot_list)>0):
            pool = Pool(processes=len(robot_list))
            for robot in robot_list:
                element = {}
                element["platform"] = self.platform_id
                element["host"] = self.host
                element["port"] = self.port
                element["uid"] =  robot_login.getuid()
                element["robot"] = robot
                result = pool.apply_async(RobotLogin, [element])
            pool.close()
            #pool.join() 只用创建无需等待进程完成


import soundcloud
import urllib.request
import json
import requests

clientID = "YOUR_CLIENT_ID_HERE"
client = soundcloud.Client(client_id=clientID)
api_base = "https://api.soundcloud.com"

def get_user_info(username):
    user = client.get('/resolve', url='http://soundcloud.com/{}/'.format(username))
    user_info = urllib.request.urlopen("{}/users/{}.json?consumer_key={}".format(api_base, user.id, clientID)).read()
    user_info_data = json.loads(user_info.decode())
    number_of_likes = user_info_data['public_favorites_count']
    username = user_info_data['username']
    return username, number_of_likes, user

user_information = get_user_info(input("Please enter your username: "))
user_name = user_information[0]
number_of_user_likes = user_information[1]
userID = user_information[2]

csv_file = open("{} like list.csv".format(user_name), "w", encoding='UTF-8')
csv_file.write("Track Title, Track URL\n")  # Writes headers to CSV file

offset_number = 0
while offset_number < number_of_user_likes:
    try:
        track_fetch = urllib.request.urlopen(
            "{}/users/{}/favorites.json?client_id={}&offset={}&limit1".format(api_base, userID.id,
                                                                              clientID, offset_number)).read()
        track_data = json.loads(track_fetch.decode())
        track_title = track_data[0]["title"].replace(",", "")  # Removes commas as causes issues with .csv files
        csv_file.write("{},{}\n".format(track_title, track_data[0]["permalink_url"]))
        offset_number += 1
        print("{} of {} ({}%)".format(offset_number, number_of_user_likes,
                                      round(float(100 / number_of_user_likes * offset_number), 2)))
    except IndexError:
        print("There is an issue with Soundcloud, please try again")
    except requests.HTTPError:
        print("There is an issue with Soundcloud, please try again")
    except requests.ConnectionError:
        print("Check your internet connection")


# def fn1():
#     print('func 1')
#     fn2()
#
# def fn2():
#     print('func 2')
#
# if __name__ == '__main__':
#     fn1()

# from random import randint
#
# def func1(x):
#     return x % 2
#
# if __name__ == '__main__':
#     nums = [randint(1, 100) for i in range(10)]
#     print(nums)
#     result1 = filter(func1, nums)
#     result2 = filter(lambda x: x % 2, nums)
#     print(list(result1))
#     print(list(result2))

from random import randint

def func2(x):
    return x + 100

if __name__ == '__main__':
    nums = [randint(1, 100) for i in range(10)]
    print(nums)
    result1 = map(func2, nums)
    result2 = map(lambda x: x + 100, nums)
    print(list(result1))
    print(list(result2))


from repository.mongo import Model
from repository.mongo import CollectionEnum
from rpgram.boosters import Classe

class ClasseModel(Model):
    _class = property(lambda self: Classe)
    collection = property(lambda self: CollectionEnum.CLASSES.value)
    alternative_id = property(lambda self: 'name')

if __name__ == '__main__':
    classe = Classe(
        name='CLÉRIGO TESTE MODELO',
        description='CLÉRIGO TESTE MODELO',
        _id='ffffffffffffffffffffffff',
        bonus_strength=5,
        bonus_dexterity=5,
        bonus_constitution=8,
        bonus_intelligence=10,
        bonus_wisdom=16,
        bonus_charisma=16,
        multiplier_strength=0.5,
        multiplier_dexterity=0.5,
        multiplier_constitution=1.0,
        multiplier_intelligence=1.0,
        multiplier_wisdom=1.8,
        multiplier_charisma=1.7,
    )
    classe_model = ClasseModel()
    print(f'Collection: {classe_model.collection}')
    result = classe_model.save(classe)
    print(f'Result: {result}')
    classe2 = classe_model.get('CLÉRIGO TESTE MODELO')
    print('Classe2:\n', classe2)


import requests
import datetime
import time
from Tools.Exchange.Crypto.Kraken import UserInterface

CRYPTO_STICKER = "BTC"
BASE_STICKER = "CAD"
JOINED_STICKER = "XXBTZCAD"

START = time.time()
BUY_SELL_ARC = 5.0  # ARC is average rate of change, used in decide() function
UPDATE_HISTORY = 3  # minutes between each historical OHLC update
HISTORY_LENGTH = 0.25  # hours to search into the past

UI = UserInterface.UI(CRYPTO_STICKER, BASE_STICKER, START, HISTORY_LENGTH)

def mean(data):
    if len(data) > 0:
        return sum(data) / len(data)
    raise Exception("Data provided for mean function has a length of 0")

def median(data):
    return data[int(len(data) / 2)]

def avg_rate_of_change(x1, y1, x2, y2):
    return (float(y2) - float(y1)) / (float(x2) - float(x1))

def get_price():
    resp = requests.get('https://api.kraken.com/0/public/Ticker?pair=' + CRYPTO_STICKER + BASE_STICKER)
    json = resp.json()

    if not json["error"]:
        price = json["result"][JOINED_STICKER]["a"][0]  # change this if using different currency
        return float(price)
    else:
        raise Exception(json["error"])

def get_historical_OHLC(interval, since):  # interval in minutes, since in
    url = "https://api.kraken.com/0/public/OHLC?pair=" + CRYPTO_STICKER + BASE_STICKER
    url += "&interval=" + str(interval)
    url += "&since=" + str(since)

    resp = requests.get(url)
    json = resp.json()

    if not json["error"]:
        price = json["result"]
        return price
    else:
        raise Exception(json["error"])

def update_historical(historical):
    global START
    time_since_update = (time.time() - START)

    if time_since_update > 60 * UPDATE_HISTORY:
        START = time.time()
        return get_historical()
    return historical

def get_historical():
    since = datetime.datetime.now() - datetime.timedelta(hours=HISTORY_LENGTH)
    data = get_historical_OHLC(1, since.timestamp())[JOINED_STICKER]

    return data

def decide(price, historical):
    timestamps = [float(pair[0]) for pair in historical]
    openPrices = [float(pair[1]) for pair in historical]
    average_price = mean(openPrices)
    average_time = median(timestamps)

    averageRateOfChange = avg_rate_of_change(average_price, average_time, price, datetime.datetime.now().timestamp()) * -1

    if averageRateOfChange > BUY_SELL_ARC:
        return "BUY", averageRateOfChange
    elif averageRateOfChange < -BUY_SELL_ARC:
        return "SELL", averageRateOfChange
    return "WAIT", averageRateOfChange

def buy(price, amount, money, crypto):
    if money > amount:
        money -= amount
        crypto += amount / price
    else:
        crypto += money / price
        money = 0

    return money, crypto

def sell(price, amount, money, crypto):
    if crypto >= amount:
        crypto -= amount
        money += amount * price
    else:
        money += crypto * price
        crypto = 0

    return money, crypto

def main():
    money = 100
    crypto = 0
    theoretical = 0
    historical = get_historical()

    run = True
    while run:
        # time.sleep(0.5)
        # delay to make sure we don't overload the api

        price = get_price()
        decision, probability = decide(price, update_historical(historical))

        if decision == "BUY":
            money, crypto = buy(price, money, money, crypto)
        if decision == "SELL":
            money, crypto = sell(price, crypto, money, crypto)

        theoretical = money + (crypto * price)

        '''
        print(
            "Money:", str('{:.15f}'.format(money)),
            "Crypto", str('{:.15f}'.format(crypto)),
            "Theoretical", str('{:.15f}'.format(theoretical)),
            "Decision", decision
        )
        '''

        UI.update(money, crypto, theoretical, price, decision, probability)
        print(UI.price_graph.data)

if __name__ == "__main__":
    main()


#1
ism=['Jon','Kerry','Vardy','Loto','Yoga']
#for ismla in ism:
#    print(f'{ismla} kel uyga' )

#2
numb=list(range(10,101))
#for number in numb:
#    print(f'{number**3} bu {number}ning kubi')

#3
kinolar=[]
for kino in range(5):
    kino=input('Sizga yoqqan kino?\n')
    kinolar.append(kino)

#4
for jino in kinolar:
    print(f'menga yoqqan kino bu {kino}')

#5


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from one_dimensional_linear_regression.one_dimensional_linear_regression_solution import r2_calculator

sns.set()

def load_polynomial_csv_data(data_file):
    X = []
    Y = []
    for line in open(data_file):
        x, y = line.split(',')
        x = float(x)
        X.append([1, x, np.power(x,2)])
        Y.append(float(y))

    return np.array(X), np.array(Y)

def polynomial_linear_regression(X, y):

    w = np.linalg.solve(np.dot(X.T, X), np.dot(X.T, y))
    y_hat = np.dot(X, w)

    _ = r2_calculator(y, y_hat)

    return w, y_hat

if __name__ == '__main__':

    data_file = '../large_files/data_poly.csv'
    X, y = load_polynomial_csv_data(data_file)

    fig = plt.figure()
    plt.scatter(X[:,1], y)
    plt.show()

    w, y_hat = polynomial_linear_regression(X,y)

    fig = plt.figure()
    x_line_predicted = np.linspace(X[:,1].min(), X[:,1].max())
    y_line_predicted = w[0] + w[1] * x_line_predicted + w[2] * np.power(x_line_predicted, 2)
    plt.scatter(X[:,1], y)
    plt.plot(x_line_predicted, y_line_predicted, 'r')
    plt.title("Fitted quadratic")
    plt.show()


from smartmonkey.models import CallbackConfiguration
from smartmonkey.models import OptimizeQuery
from smartmonkey.models import ResultQuery
from smartmonkey.models import RewardRegion
from smartmonkey.models import Service
from smartmonkey.models import Vehicle

def optimize(
    client, vehicles, services, reward_region=[],
    synchronous=True, callback=None,
):
    """Launch a Smartmonkey route optimization given a list
    of vehicles and services.

    This example provides two services and one vehicle to
    the optimization::

        vehicles = [
            {
                "id": "Tesla 1",
                "start": {
                    "lat": 4.13,
                    "lng": 20.18,
                },
                "end": {
                    "lat": 4.23,
                    "lng": 19.85,
                },
                "capacity": [20],
                "timewindow": [36000, 120400],
                "provides": ["candy", "marshmallows"]
            }
        ]

        services = [
            {
                "id": "Provide Candy",
                "location": {"lat": 41.45, "lng": 2.14},
                "size": [1],
                "capacity": [10],
                "timewindows": [[36000, 68400]],
                "duration": 3600,
                "reward": 100,
                "optional": False,
                "pickups": {
                    "id": "Pickup candy",
                    "location": {
                        "lat": 41.345,
                        "lng": 2.15623
                    }
                }
            },
            {
                "id": "Provide Marshmallows",
                "location": {"lat": 41.35, "lng": 2.14},
                "size": [1],
                "capacity": [10],
                "timewindows": [[50000, 120400]],
                "duration": 3600,
                "reward": 100,
                "optional": False,
                "requires": ["marsmallows"],
            },
        ]

    **Synchronous response**

    Now just create the client and launch the optimization function.
    By default the result is synchronous, so the function will end as
    soon as the server finish the process::

        from smartmonkey import Client

        api_client = Client(key=MY_API_CLIENT_KEY)
        result = api_client.optimize(vehicles, services)

    **Asynchronous response**

    Sometimes, one might want to get the result asynchronously. In
    that case is possible to set the parameter synchronous to **False**.
    The response of the server will contain a *job_id* parameter with
    which we'll be able to check the results later::

        result = api_client.optimize(
            vehicles,
            services,
            synchronous=False
        )
        print(result['job_id'])

    **Asynchronous response with callback**

    The third way to get the results is specifying a callback url
    so once the server finish the optimization, the result will be
    sent to that url::

        result = api_client.optimize(
            vehicles,
            services,
            synchronous=False
            callback="http://example.com/"
        )

    :param vehicles: A list of dictionaries representing a vehicle.
        Each vehicle must contain Vehicle Model properties.
    :type vehicles: list

    :param services: A list of dictionaries representing a service.
        Each service must contain Service Model properties
    :type services: list

    :param reward_region: A list of dictionaries with all the reward
        regions, must contain 'lat', 'lng', 'radius', and 'reward'.
    :type reward_region: list

    :param synchronous: *(default True)* If synchronous is set to True,
        the request will still alive until the optimization ends. If set
        to false it will return a job id.
    :type synchronous: bool

    :param callback: *(default None)* If synchronous is False is
        possible to set a callback url where the results will be placed
        once the optimization is finished.
    :type callback: string
    """
    if type(vehicles) != list or len(vehicles) < 1:
        raise Exception(
            "`vehicles` should be a list with at least one vehicle.",
        )
    if type(services) != list or len(services) < 1:
        raise Exception(
            "`services` should be a list with at least one service.",
        )
    if type(reward_region) != list:
        raise Exception("`reward_region` should be a list.")

    vehicles_s = [Vehicle(vehicle) for vehicle in vehicles]
    services_s = [Service(service) for service in services]
    reward_region_s = [RewardRegion(rr) for rr in reward_region]
    configuration = CallbackConfiguration({
        "wait": synchronous,
        "callback": callback,
    })

    query = OptimizeQuery(
        vehicles_s, services_s,
        reward_region_s, configuration,
    )
    return client._request(query)

def get_results(client, job_id):
    """Get the result of an optimization given a job id

    :param job_id: The job id of one optimization. This parameter is returned
        when an optimization process start. Is it possible to retrieve past
        optimizations with the id.
    :type job_id: string
    """
    query = ResultQuery({"job_id": job_id})
    return client._request(query)


from typing import cast
import pathlib
import ezdxf
from ezdxf.document import Drawing

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")

# ------------------------------------------------------------------------------
# setup DXF R2000 paperspace layout
#
# For most use cases a paperspace scaling of 1:1 is to prefer and important:
# the paperspace scaling has no influence on the VIEWPORT scaling - this is a
# total different topic, see example "viewports_in_paperspace.py"
#
# docs about layouts: https://ezdxf.mozman.at/docs/layouts/index.html
# ------------------------------------------------------------------------------

FILENAME = "page_setup_R2000.dxf"

def draw_raster(doc: Drawing):
    marker = doc.blocks.new(name="MARKER")
    attribs = {"color": 2}
    marker.add_line((-1, 0), (1, 0), dxfattribs=attribs)
    marker.add_line((0, -1), (0, 1), dxfattribs=attribs)
    marker.add_circle((0, 0), 0.4, dxfattribs=attribs)

    marker.add_attdef(
        "XPOS", (0.5, -1.0), dxfattribs={"height": 0.25, "color": 4}
    )
    marker.add_attdef(
        "YPOS", (0.5, -1.5), dxfattribs={"height": 0.25, "color": 4}
    )
    modelspace = doc.modelspace()
    for x in range(10):
        for y in range(10):
            xcoord = x * 10
            ycoord = y * 10
            values = {
                "XPOS": f"x = {xcoord}",
                "YPOS": f"y = {ycoord}",
            }
            modelspace.add_auto_blockref("MARKER", (xcoord, ycoord), values)

def setup_active_viewport_configuration(doc: Drawing):
    # This creates a multi-window configuration for the modelspace.
    #
    # delete the current '*Active' viewport configuration
    doc.viewports.delete_config("*ACTIVE")
    # the available display area in AutoCAD has the virtual lower-left
    # corner (0, 0) and the virtual upper-right corner (1, 1)

    # first viewport, uses the left half of the screen
    viewport = doc.viewports.new("*ACTIVE")
    viewport.dxf.lower_left = (0, 0)
    viewport.dxf.upper_right = (0.5, 1)

    # target point defines the origin of the DCS, this is the default value
    viewport.dxf.target = (0, 0, 0)

    # move this location (in DCS) to the center of the viewport
    viewport.dxf.center = (40, 30)

    # height of viewport in drawing units, this parameter works
    viewport.dxf.height = 15

    # aspect ratio of viewport (x/y)
    viewport.dxf.aspect_ratio = 1.0

    # second viewport, uses the right half of the screen
    viewport = doc.viewports.new("*ACTIVE")
    viewport.dxf.lower_left = (0.5, 0)
    viewport.dxf.upper_right = (1, 1)

    # target point defines the origin of the DCS
    viewport.dxf.target = (60, 20, 0)

    # move this location (in DCS, model space = 60, 20) to the center of the viewport
    viewport.dxf.center = (0, 0)

    # height of viewport in drawing units, this parameter works
    viewport.dxf.height = 15

    # aspect ratio of viewport (x/y)
    viewport.dxf.aspect_ratio = 2.0

def setup_paperspace_layouts(doc: Drawing):
    setup_layout1(doc)
    setup_layout2(doc)
    setup_layout3(doc)
    setup_layout4(doc)

def setup_layout1(doc: Drawing):
    from ezdxf.layouts import Paperspace

    name = "Layout1"
    if name in doc.layouts:
        layout = cast(Paperspace, doc.layouts.get(name))
    else:
        layout = doc.layouts.new(name)

    layout.page_setup(
        size=(11, 8.5), margins=(0.5, 0.5, 0.5, 0.5), units="inch"
    )
    lower_left, upper_right = layout.get_paper_limits()
    x1, y1 = lower_left
    x2, y2 = upper_right
    center = lower_left.lerp(upper_right)

    # Add DXF entities to the "Layout1" in paperspace coordinates:
    layout.add_line((x1, center.y), (x2, center.y))  # horizontal center line
    layout.add_line((center.x, y1), (center.x, y2))  # vertical center line
    layout.add_circle((0, 0), radius=0.1)  # plot origin

def setup_layout2(doc: Drawing):
    layout2 = doc.layouts.new("scale 1-1")
    # The default paperspace scale is 1:1
    # 1 mm printed is 1 drawing unit in paperspace
    # For most use cases this is the preferred scaling and important fact:
    # the paperspace scaling has no influence on the VIEWPORT scaling - this is
    # a total different topic, see example "viewports_in_paperspace.py"

    layout2.page_setup(size=(297, 210), margins=(10, 10, 10, 10), units="mm")
    layout2.add_viewport(
        # center of viewport in paperspace units
        center=(100, 100),
        # viewport size in paperspace units
        size=(50, 50),
        # modelspace point to show in center of viewport in WCS
        view_center_point=(60, 40),
        # how much modelspace area to show in viewport in drawing units
        view_height=20,
        status=2,
    )
    lower_left, upper_right = layout2.get_paper_limits()
    x1, y1 = lower_left
    x2, y2 = upper_right
    center = lower_left.lerp(upper_right)

    # Add DXF entities to the "Layout1" in paperspace coordinates:
    layout2.add_line((x1, center.y), (x2, center.y))  # horizontal center line
    layout2.add_line((center.x, y1), (center.x, y2))  # vertical center line
    layout2.add_circle((0, 0), radius=5)  # plot origin

def setup_layout3(doc: Drawing):
    layout3 = doc.layouts.new("scale 1-50")
    # The paperspace is scaled 1:50
    # 1 mm printed is 50 drawing unit in paperspace
    layout3.page_setup(
        size=(297, 210), margins=(10, 10, 10, 10), units="mm", scale=(1, 50)
    )
    layout3.add_viewport(
        # center of viewport in paperspace units, scale = 1:50
        center=(5000, 5000),
        # viewport size in paperspace units, scale = 1:50
        size=(5000, 2500),
        # model space point to show in center of viewport in WCS
        view_center_point=(60, 40),
        # how much model space area to show in viewport in drawing units
        view_height=20,
        status=2,
    )
    layout3.add_circle((0, 0), radius=250)  # plot origin

def setup_layout4(doc: Drawing):
    layout4 = doc.layouts.new("scale 1-1 with offset")
    # The paperspace is scaled 1:1 but has a plot offset
    # 1 mm printed is 1 drawing unit in paperspace

    layout4.page_setup(
        size=(297, 210),
        margins=(10, 10, 10, 10),
        units="mm",
        scale=(1, 1),
        offset=(50, 50),
    )
    lower_left, upper_right = layout4.get_paper_limits()
    x1, y1 = lower_left
    x2, y2 = upper_right
    center = lower_left.lerp(upper_right)

    layout4.add_line((x1, center.y), (x2, center.y))  # horizontal center line
    layout4.add_line((center.x, y1), (center.x, y2))  # vertical center line
    layout4.add_circle((0, 0), radius=5)  # plot origin

def main():
    doc = ezdxf.new("R2000")
    draw_raster(doc)
    setup_active_viewport_configuration(doc)
    setup_paperspace_layouts(doc)
    doc.saveas(CWD / FILENAME)
    print(f'DXF file "{FILENAME}" created.')

if __name__ == "__main__":
    main()


#NEW APP
from django.conf.urls import patterns, include, url
import snv.views

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
		url(r'^$', snv.views.home, name='home',),
        url(r'^info/$', snv.views.info, name='info',),
        url(r'^about/$', snv.views.about, name='about',),
        url(r'^getting-started/$', snv.views.gettingstarted, name='getting-started',),
        #url(r'^$', snv.views.home.as_view(), name='home',),
        url(r'^listuniprot$', snv.views.UniprotList.as_view(), name='uniprot-list',),
        url(r'^uniprot/(?P<pk>(\D+|\d+)+)/$', snv.views.UniprotView.as_view(), name='uniprot-view',),
        url(r'^disease/(?P<pk>\d+)/$', snv.views.DiseaseView.as_view(), name='disease-view',),
		url(r'^snv/(?P<pk>(\D+|\d+)+)/$', snv.views.SnvView.as_view(), name='snv-view',),
        url(r'^interaction/(?P<pk>(\D+|\d+)+)/$',snv.views.InteractionView.as_view(), name='interaction-view'),
		url(r'^interface/(?P<pk>(\D+|\d+)+)/$',snv.views.InterfaceView.as_view(), name='interface-view'),
        url(r'^login/$', 'django.contrib.auth.views.login'),
        url(r'^logout/$', 'django.contrib.auth.views.logout'),
        url(r'^search-form/$', snv.views.search_form),
        url(r'^search/$', snv.views.search, name='search-results',),
        url(r'^viewer/(?P<uniprot_acc>(\D+|\d+)+)/$',snv.views.SuperpositionView.as_view(),name='superposition-view'),
        #url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),

    # url(r'^snvproj/', include('snvproj.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),

)


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

"""
Zad 2. Napisz funkcję even_elements zwracającą listę,
która zawiera tylko elementy z list o parzystych indeksach.
"""

#definicja funkcji
def even_elements(lista):
    result=[]
    for i in range(len(lista)):
        if (i % 2 == 0):          # modulo 2 - sprawdzenie parzystosci
            result.append(lista[i])  #dlaczenie do listy
    return result

#input = [1, 2, 3, 4, 5, 6]
#output = [1, 3, 5]

#wywolanie funkcji
input = [1, 2, 3, 4, 5, 6]
output = even_elements(input) #wywolanie funkcji i wynik przypisany do zmiennej
print(output)                 # "wydrul" wyniku na konsoli


import scipy.io as io
from gaussian_process import GP

# data
data = io.loadmat('gp.mat')
x = data['x']
t = data['t']

train_num = 60
x_train = x[:train_num, :]
x_test = x[train_num:, :]
t_train = t[:train_num]
t_test = t[train_num:]

# train and predict results
theta_list = [[0, 0, 0, 1],
              [1, 4, 0, 0],
              [1, 4, 0, 5],
              [1, 32, 5, 5]]

for i, thetas in enumerate(theta_list):
    gp = GP(thetas=thetas)
    gp.train(x_train, t_train)
    gp.plot(x_test, t_test, title=f"theta_{i}")
    train_error, test_error = gp.rms_error()

    print(f'train error: {train_error:>.2f}  test error: {test_error:>.3f}')

# ARD
gp.ard()
gp.plot(x_test, t_test, "ARD")
train_error, test_error = gp.rms_error()
print(f'train error: {train_error:>.2f}  test error: {test_error:>.3f}')


class Skip(object):
    pass

skip = Skip()
supported_complex_types = (dict, list)
supported_primitive_types = (int, str, float, bool, Skip, type(None))

def _unpack_simple(expected, actual):
    if type(expected) not in supported_primitive_types:
        return False
    if type(actual) not in supported_primitive_types:
        return False

    if isinstance(expected, Skip):
        return True

    if type(expected) != type(actual):
        raise AssertionError(
            f"Expected simple type {type(expected)} but was {type(actual)}"
        )
    elif expected != actual:
        raise AssertionError(f"Expected {expected} but was {actual}")

    return True

def _unpack_complex(expected, actual):
    if isinstance(expected, Skip):
        return True

    if type(expected) != type(actual):
        raise AssertionError(
            f"Expected type is {type(expected)} but was {type(actual)}"
        )

    if isinstance(expected, dict):
        expected: dict = expected
        actual: dict = actual

        diff = expected.keys() - actual.keys()
        if len(diff) != 0:
            raise AssertionError(
                f"Expected keys {expected.keys()} but was {actual.keys()}. Missing keys {diff}"
            )

        for key in expected.keys():
            _unpack(expected[key], actual[key])

        return True
    elif isinstance(expected, list):
        if len(expected) != len(actual):
            raise AssertionError(
                f"Expected length is {len(expected)} but was {len(actual)}"
            )

        for e, a in zip(expected, actual):
            _unpack(e, a)

        return True
    else:
        return False

def _unpack(expected, actual):
    if _unpack_simple(expected, actual):
        pass
    elif _unpack_complex(expected, actual):
        pass
    else:
        raise AssertionError(
            f"Unknown type combination expected->{type(expected)} actual->{type(actual)}"
        )

def assert_matches(expected, actual):
    if type(expected) not in supported_complex_types:
        raise AssertionError(
            f"Expected type should be dict, list but was {type(expected)}"
        )
    if type(actual) not in supported_complex_types:
        raise AssertionError(
            f"Actual type should be dict or list but was {type(actual)}"
        )

    _unpack(expected, actual)


from __future__ import absolute_import, division, print_function
import iotbx.phil
from libtbx.test_utils import show_diff, Exception_expected
from libtbx.utils import Sorry
from libtbx import Auto
import os

def exercise():
  master = iotbx.phil.parse(input_string="""\
u=10,12 13 80,90 100
  .type=unit_cell
s=19
  .type=space_group
U=None
  .type=unit_cell
S=None
  .type=space_group
ua=Auto
  .type=unit_cell
sa=Auto
  .type=space_group
""")
  assert not show_diff(master.format(master.extract()).as_str(), """\
u = 10 12 13 80 90 100
s = "P 21 21 21"
U = None
S = None
ua = Auto
sa = Auto
""")
  custom = iotbx.phil.parse(input_string="""\
ua = None
s = Auto
U = 1,2,3
u = Auto
S = 33
sa = None
""")
  assert not show_diff(
    master.fetch(source=custom).extract_format().as_str(), """\
u = Auto
s = Auto
U = 1 2 3 90 90 90
S = "P n a 21"
ua = None
sa = None
""")
  # make sure Unicode strings work!
  custom = iotbx.phil.parse(input_string=u"""\
U = 1 2 3 90 105 90
S = P21
""")
  assert not show_diff(
    master.fetch(source=custom).extract_format().as_str(), """\
u = 10 12 13 80 90 100
s = "P 21 21 21"
U = 1 2 3 90 105 90
S = "P 1 21 1"
ua = Auto
sa = Auto
""")
  #
  master = iotbx.phil.parse(input_string="""\
s=None
  .type=space_group
  .multiple=True
""")
  custom = iotbx.phil.parse(input_string="""\
s=p212121
s=19
""")
  assert not show_diff(master.fetch(source=custom).as_str(), """\
s = 19
""")
  custom = iotbx.phil.parse(input_string="""\
s=19
s=p212121
""")
  assert not show_diff(master.fetch(source=custom).as_str(), """\
s = p212121
""")
  custom = iotbx.phil.parse(input_string="""\
s=18
s=p212121
""")
  assert not show_diff(master.fetch(source=custom).as_str(), """\
s = 18
s = p212121
""")
  #
  master = iotbx.phil.parse(input_string="""\
sel = None
  .type = atom_selection
  .multiple = True
""")
  clai = master.command_line_argument_interpreter()
  assert not show_diff(clai.process(
    "sel=altloc  ' ' or  name C\\* ").as_str(), """\
sel = altloc ' ' or name C\\*
""")
  user = iotbx.phil.parse(input_string="""\
sel = "altloc ' ' or name C\\*"
sel = altloc ' ' or name D\\*
""")
  work = master.fetch(user)
  assert not show_diff(work.as_str(), """\
sel = "altloc ' ' or name C\\\\*"
sel = altloc ' ' or name D\\*
""")
  ex = work.extract()
  assert len(ex.sel) == 2
  assert ex.sel[0] == "altloc ' ' or name C\\*"
  assert ex.sel[1] == "altloc ' ' or name D\\*"
  #
  pcl = iotbx.phil.process_command_line(args=["s = 230"], master_string="""\
s=None
  .type=space_group
""")
  assert not show_diff(str(pcl.work.extract().s), "I a -3 d")
  #
  master_phil_str = """
model = None
  .type = path
use_geometry_restraints = False
  .type = bool
"""
  with open("model.pdb", "w") as f:
    f.write("""\
ATOM      1  O   HOH     1      53.448  18.599 -10.134  1.00 20.00
""")
  pcl = iotbx.phil.process_command_line_with_files(
    args=["model.pdb", "--use_geometry_restraints"],
    master_phil_string=master_phil_str,
    pdb_file_def="model")
  assert (pcl.get_file_type_count("pdb") == 1)
  assert (pcl.get_file_type_count("hkl") == 0)
  params = pcl.work.extract()
  pdb_in = pcl.get_cached_file(params.model)
  assert (pdb_in is not None)
  assert (params.model == os.path.join(os.getcwd(), "model.pdb"))
  assert (params.use_geometry_restraints)
  model_in = pcl.get_file(params.model, force_type="pdb")
  master_phil_str = """
data_dir = None
  .type = path
d_min = None
  .type = float
nproc = None
  .type = int
"""
  pcl = iotbx.phil.process_command_line_with_files(
    args=["/", "3.0", "5"],
    master_phil_string=master_phil_str,
    directory_def="data_dir",
    integer_def="nproc",
    float_def="d_min")
  params = pcl.work.extract()
  assert (params.data_dir == "/")
  assert (params.d_min == 3.0)
  assert (params.nproc == 5)
  # shelx file hack
  with open("tst_iotbx_phil.hkl", "w") as f:
    f.write("""\
   1   2  -1  -23.34    4.56   1
   2  -3   9   12.45    6.12   2
99999999999999999.9999999.999999
-999-999-999-9999.99-9999.99-999
   0   0   0    0.00    0.00   0""")
  master_phil_str = """
data = None
  .type = path
space_group = None
  .type = space_group
unit_cell = None
  .type = unit_cell
"""
  pcl = iotbx.phil.process_command_line_with_files(
    args=[
      "tst_iotbx_phil.hkl",
      "P6122",
      "50,50,40,90,90,120",
    ],
    master_phil_string=master_phil_str,
    reflection_file_def="data",
    space_group_def="space_group",
    unit_cell_def="unit_cell")
  params = pcl.work.extract()
  assert (str(params.space_group) == "P 61 2 2")
  assert (str(params.unit_cell) == "(50, 50, 40, 90, 90, 120)")
  from iotbx.file_reader import any_file
  try :
    hkl_in = any_file(params.data, force_type="hkl")
    print(hkl_in.file_server.miller_arrays[0].is_xray_intensity_array())
  except Sorry as s :
    assert ("Unresolved amplitude/intensity ambiguity" in str(s))
  else :
    raise Exception_expected
  pcl = iotbx.phil.process_command_line_with_files(
    args=["tst_iotbx_phil.hkl=hklf3"],
    master_phil_string=master_phil_str,
    reflection_file_def="data")
  params = pcl.work.extract()
  assert (params.data == "tst_iotbx_phil.hkl=hklf3")
  hkl_in = any_file(params.data, force_type="hkl")
  ma = hkl_in.file_server.miller_arrays[0]
  assert ma.is_xray_amplitude_array()
  assert ma.anomalous_flag() is False

  with open("tst_iotbx_phil.ncs", "w") as f:
    f.write("""
REMARK 350   BIOMT1   1  1.000000  0.000000  0.000000        0.00000
REMARK 350   BIOMT2   1  0.000000  1.000000  0.000000        0.00000
REMARK 350   BIOMT3   1  0.000000  0.000000  1.000000        0.00000
REMARK 350   BIOMT1   2  0.623490  0.781831  0.000000        0.00000
REMARK 350   BIOMT2   2 -0.781831  0.623490  0.000000        0.00000
REMARK 350   BIOMT3   2  0.000000  0.000000  1.000000        0.00000

""")
  master_phil_str = """
ncs_file = None
  .type = path
"""
  pcl = iotbx.phil.process_command_line_with_files(
    args=[
      "tst_iotbx_phil.ncs",
    ],
    master_phil_string=master_phil_str,
    ncs_file_def="ncs_file")
  params = pcl.work.extract()
  assert (os.path.split(str(params.ncs_file))[-1] == "tst_iotbx_phil.ncs")

  with open("tst_iotbx_phil.ncs_spec", "w") as f:
    f.write("""
Summary of NCS information
Wed Mar 16 15:02:22 2016
/Users/terwill/Desktop/working/Jan_2015/phenix/cryo-pdb/3j9c/build

source_info ncs_biomtr.ncs

new_ncs_group
new_operator

rota_matrix    1.0000    0.0000    0.0000
rota_matrix    0.0000    1.0000    0.0000
rota_matrix    0.0000    0.0000    1.0000
tran_orth     0.0000    0.0000    0.0000

center_orth    0.0000    0.0000    0.0000
new_operator

rota_matrix    0.6235   -0.7818    0.0000
rota_matrix    0.7818    0.6235   -0.0000
rota_matrix   -0.0000    0.0000    1.0000
tran_orth     0.0000    0.0000    0.0000

""")
  master_phil_str = """
ncs_file = None
  .type = path
"""
  pcl = iotbx.phil.process_command_line_with_files(
    args=[
      "tst_iotbx_phil.ncs_spec",
    ],
    master_phil_string=master_phil_str,
    ncs_file_def="ncs_file")
  params = pcl.work.extract()
  assert (os.path.split(str(params.ncs_file))[-1] == "tst_iotbx_phil.ncs_spec")

  print("OK")

if (__name__ == "__main__"):
  exercise()


def draw(
        self,
        show_tip_labels=True,
        show_node_support=False,
        use_edge_lengths=False,
        orient="right",
        print_args=False,
        *args,
        **kwargs):
        """
        plot the tree using toyplot.graph.

        Parameters:
        -----------
            show_tip_labels: bool
                Show tip names from tree.
            use_edge_lengths: bool
                Use edge lengths from newick tree.
            show_node_support: bool
                Show support values at nodes using a set of default
                options.

            ...
        """
        ## re-decompose tree for new orient and edges args
        self._decompose_tree(orient=orient, use_edge_lengths=use_edge_lengths)

        ## update kwargs with entered args and all other kwargs
        dwargs = {}
        dwargs["show_tip_labels"] = show_tip_labels
        dwargs["show_node_support"] = show_node_support
        dwargs.update(kwargs)

        ## pass to panel plotter
        canvas, axes, panel = tree_panel_plot(self, print_args, **dwargs)
        return canvas, axes, panel


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# 1)
driver = webdriver.Chrome('D:\\chromedriver.exe')
driver.maximize_window()
# 2)
driver.get('http://yandex.ru')
# 3)
market = driver.find_element_by_xpath("//a[@class='home-link home-link_blue_yes home-tabs__link home-tabs__search'][contains(text(),'Маркет')]")
market.click()

# 4)
time.sleep(10)

electronic = driver.find_element_by_class_name("topmenu__link")
electronic.click()
# 5)
time.sleep(10)
mobiles = driver.find_element_by_xpath("//a[@class='_2qvOOvezty _2x2zBaVN-3 _9qbcyI_fyS'][contains(text(),'Мобильные телефоны')]")
mobiles.click()
# 6)
# 7)
min_price = driver.find_element_by_xpath("//input[@id='glpricefrom']")
min_price.send_keys('20000')
# 8)

inpts = driver.find_elements_by_class_name("_3Uz6PcbAtW")
divs = driver.find_elements_by_class_name("LhMupC0dLR")
for i in range(len(inpts)):
	time.sleep(.1)
	if(inpts[i].get_attribute('name') == 'Производитель Apple' or inpts[i].get_attribute('name') == 'Производитель Samsung'):
		divs[i].click()

# 9) теперь кнопки "применить" нет и все применяется автоматически
# 10)

elements = driver.find_elements_by_class_name("n-snippet-cell2")
if(len(elements) == 12):   # у меня изначально по 48 показываются
	pass
#print(num_elements)
# 11)

first_good = elements[0].find_element_by_class_name("n-link_theme_blue").text
print(first_good)

# 12)

driver.find_element_by_xpath("//input[@id='header-search']").send_keys(first_good)
driver.find_element_by_xpath("//span[@class='search2__button']//button[@type='submit']").click()

# 13)

# это если сразу откроется товар
try:
	if( driver.find_element_by_xpath("//h1").text == first_good):
		print(driver.find_element_by_xpath("//h1").text)
		print('ok!')
# но может открыться список доступных модификаций товара, поэтому сравниваю первый товар в списке
except:
	if( driver.find_element_by_class_name('n-snippet-cell2').find_element_by_class_name('n-link_theme_blue').text == first_good):
		print(driver.find_element_by_class_name('n-snippet-cell2').find_element_by_class_name('n-link_theme_blue').text)
		print('ok!')


from PyQt5 import QtWidgets, QtGui

class QMenu(QtWidgets.QMenu):

    # show qaction tooltips by default
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setToolTipsVisible(True)

class QMessageBox(QtWidgets.QMessageBox):

    # adjust size to  given text:
    def setText(self, text):
        QtWidgets.QMessageBox.setText(self, text)
        # get width on longest line:
        width = max(QtGui.QFontMetrics(QtGui.QFont()).boundingRect(line).width() for line in text.split('\n'))
        # enforce that width with a spacer:
        horizontalSpacer = QtWidgets.QSpacerItem(int(width * 1.5), 0, QtWidgets.QSizePolicy.Minimum,
                                             QtWidgets.QSizePolicy.Expanding);
        layout = self.layout()
        layout.addItem(horizontalSpacer, layout.rowCount(),
                       0, 1, layout.columnCount())

    @staticmethod
    def build(parent, buttons=None, title=None, icon=None, text=None):
        box = QMessageBox(parent)
        if buttons is not None:
            box.setStandardButtons(buttons)
        if title is not None:
            box.setWindowTitle(title)
        if text is not None:
            box.setText(text)
        if icon is not None:
            box.setIcon(icon)
        return box

if __name__ == '__main__':

    app = QtWidgets.QApplication([])

    L = QMessageBox()
    L.setText('''Changes:
<p0>            <p1>            <old>           <new>
location        <- removed
                    eloss       <- removed
setup           comments                        False
modules         <- removed''')
    L.exec_()
    app.exec_()