import random
import os.path
import numpy as np

from pysc2.agents import base_agent
from pysc2.env import sc2_env, run_loop
from pysc2.lib import actions, features, units
from absl import app

import torch
from torch.utils.tensorboard import SummaryWriter

from s10274.skdrl.pytorch.model.mlp import DuelingQNet
from s10274.skdrl.common.memory.memory import ExperienceReplayMemory
from s10274.skdrl.pytorch.model.dqn import DQN, prepare_training_inputs

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#writer = SummaryWriter()

class TerranAgentWithRawActsAndRawObs(base_agent.BaseAgent):
    actions = ("do_nothing",
               "harvest_minerals",
               "harvest_vespene",
               "build_refinery",
               "build_supply_depot",
               "build_barracks",
               "build_factory",
               "build_techlab",
               "build_command_center",
               "train_scv",
               "train_marine",
               "train_tank",
               "marine_attack",
               "tank_attack")

    def unit_type_is_selected(self, obs, unit_type):
        if (len(obs.observation.single_select) > 0 and
            obs.observation.single_select[0].unit_type == unit_type):
              return True

        if (len(obs.observation.multi_select) > 0 and
            obs.observation.multi_select[0].unit_type == unit_type):
              return True

        return False

    def get_my_units_by_type(self, obs, unit_type):
        return [unit for unit in obs.observation.raw_units
                if unit.unit_type == unit_type
                and unit.alliance == features.PlayerRelative.SELF]

    def get_enemy_units_by_type(self, obs, unit_type):
        return [unit for unit in obs.observation.raw_units
                if unit.unit_type == unit_type
                and unit.alliance == features.PlayerRelative.ENEMY]

    def get_my_completed_units_by_type(self, obs, unit_type):
        return [unit for unit in obs.observation.raw_units
                if unit.unit_type == unit_type
                and unit.build_progress == 100
                and unit.alliance == features.PlayerRelative.SELF]

    def get_enemy_completed_units_by_type(self, obs, unit_type):
        return [unit for unit in obs.observation.raw_units
                if unit.unit_type == unit_type
                and unit.build_progress == 100
                and unit.alliance == features.PlayerRelative.ENEMY]

    def get_distances(self, obs, units, xy):
        units_xy = [(unit.x, unit.y) for unit in units]
        return np.linalg.norm(np.array(units_xy) - np.array(xy), axis=1)

    def step(self, obs):
        super(TerranAgentWithRawActsAndRawObs, self).step(obs)
        if obs.first():
            command_center = self.get_my_units_by_type(
                obs, units.Terran.CommandCenter)[0]
            self.base_top_left = (command_center.x < 32)

    def do_nothing(self, obs):
        return actions.RAW_FUNCTIONS.no_op()

    def harvest_minerals(self, obs):
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        idle_scvs = [scv for scv in scvs if scv.order_length == 0]
        if len(idle_scvs) > 0:
            mineral_patches = [unit for unit in obs.observation.raw_units
                               if unit.unit_type in [
                                   units.Neutral.BattleStationMineralField,
                                   units.Neutral.BattleStationMineralField750,
                                   units.Neutral.LabMineralField,
                                   units.Neutral.LabMineralField750,
                                   units.Neutral.MineralField,
                                   units.Neutral.MineralField750,
                                   units.Neutral.PurifierMineralField,
                                   units.Neutral.PurifierMineralField750,
                                   units.Neutral.PurifierRichMineralField,
                                   units.Neutral.PurifierRichMineralField750,
                                   units.Neutral.RichMineralField,
                                   units.Neutral.RichMineralField750
                               ]]
            scv = random.choice(idle_scvs)
            distances = self.get_distances(obs, mineral_patches, (scv.x, scv.y))
            mineral_patch = mineral_patches[np.argmin(distances)]
            return actions.RAW_FUNCTIONS.Harvest_Gather_unit(
                "now", scv.tag, mineral_patch.tag)
        return actions.RAW_FUNCTIONS.no_op()

    def harvest_vespene(self, obs):
        refineries = self.get_my_units_by_type(obs, units.Terran.Refinery)
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        idle_scvs = [scv for scv in scvs if scv.order_length == 0]
        if len(idle_scvs) > 0 and len(refineries) > 0:
            scv = random.choice(idle_scvs)
            distances = self.get_distances(obs, refineries, (scv.x, scv.y))
            refinery = refineries[np.argmin(distances)]
            return actions.RAW_FUNCTIONS.Harvest_Gather_unit(
                "now", scv.tag, refinery.tag)
        return actions.RAW_FUNCTIONS.no_op()

    # supply depot을 5개 건설
    def build_supply_depot(self, obs):
        supply_depots = self.get_my_units_by_type(obs, units.Terran.SupplyDepot)
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        if (len(supply_depots) < 5 and obs.observation.player.minerals >= 100 and
                len(scvs) > 0):
            supply_depot_coordinates = [(19, 26), (22, 26), (24, 25), (26, 24), (28, 23)] if self.base_top_left \
                else [(29, 42), (32, 42), (35, 42), (37, 42), (39, 42)]
            now_supply_depot_coordinates = [(supply_depot.x, supply_depot.y) for supply_depot in supply_depots]

            remain_supply_depot_coordinates = []
            for c in supply_depot_coordinates:
                if c not in now_supply_depot_coordinates:
                    remain_supply_depot_coordinates.append(c)

            distances = self.get_distances(obs, scvs, remain_supply_depot_coordinates[0])
            scv = scvs[np.argmin(distances)]
            return actions.RAW_FUNCTIONS.Build_SupplyDepot_pt(
                "now", scv.tag, remain_supply_depot_coordinates[0])
        return actions.RAW_FUNCTIONS.no_op()

    def build_barracks(self, obs):
        completed_supply_depots = self.get_my_completed_units_by_type(
            obs, units.Terran.SupplyDepot)
        barrackses = self.get_my_units_by_type(obs, units.Terran.Barracks)
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        if (len(completed_supply_depots) > 0 and len(barrackses) == 0 and
                obs.observation.player.minerals >= 150 and len(scvs) > 0):
            barracks_xy = (22, 21) if self.base_top_left else (35, 45)
            distances = self.get_distances(obs, scvs, barracks_xy)
            scv = scvs[np.argmin(distances)]
            return actions.RAW_FUNCTIONS.Build_Barracks_pt(
                "now", scv.tag, barracks_xy)
        return actions.RAW_FUNCTIONS.no_op()

    def build_refinery(self, obs):
        completed_supply_depots = self.get_my_completed_units_by_type(
            obs, units.Terran.SupplyDepot)
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        if (len(completed_supply_depots) > 0 and obs.observation.player.minerals >= 100 and len(scvs) > 0):
            vespene_patches = [unit for unit in obs.observation.raw_units
                               if unit.unit_type in [
                                   units.Neutral.RichVespeneGeyser,
                                   units.Neutral.VespeneGeyser
                               ]]
            scv = random.choice(scvs)
            distances = self.get_distances(obs, vespene_patches, (scv.x, scv.y))
            vespene_patch = vespene_patches[np.argmin(distances)]
            return actions.RAW_FUNCTIONS.Build_Refinery_pt(
                "now", scv.tag, vespene_patch.tag)
        return actions.RAW_FUNCTIONS.no_op()

    def build_factory(self, obs):
        completed_supply_depots = self.get_my_completed_units_by_type(
            obs, units.Terran.SupplyDepot)
        completed_refineries = self.get_my_completed_units_by_type(
            obs, units.Terran.Refinery)
        factory = self.get_my_units_by_type(obs, units.Terran.Factory)
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        if (len(completed_supply_depots) > 0 and len(completed_refineries) > 0 and len(factory) == 0 and
                obs.observation.player.minerals >= 200 and obs.observation.player.vespene >= 100
                and len(scvs) > 0):
            factory_xy = (25, 21) if self.base_top_left else (30, 45)
            distances = self.get_distances(obs, scvs, factory_xy)
            scv = scvs[np.argmin(distances)]
            return actions.RAW_FUNCTIONS.Build_Factory_pt("now", scv.tag, factory_xy)
        return actions.RAW_FUNCTIONS.no_op()

    # techlab은 factory의 addon 건물이므로 factory가 건설되어 있어야 함
    def build_techlab(self, obs):
        completed_factory = self.get_my_completed_units_by_type(
            obs, units.Terran.Factory)
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        if (len(completed_factory) > 0 and obs.observation.player.minerals >= 50 and obs.observation.player.vespene >= 50
                and len(scvs) > 0):
            return actions.RAW_FUNCTIONS.Build_TechLab_quick("now", completed_factory[0].tag)
        return actions.RAW_FUNCTIONS.no_op()

    # command center 추가 건설
    def build_command_center(self, obs):
        completed_command_center = self.get_my_completed_units_by_type(
            obs, units.Terran.CommandCenter)
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        if (len(completed_command_center) > 0 and obs.observation.player.minerals >= 400
                and len(scvs) > 0):
            cc_xy = (40, 21) if self.base_top_left else (18, 47)
            distances = self.get_distances(obs, scvs, cc_xy)
            scv = scvs[np.argmin(distances)]
            return actions.RAW_FUNCTIONS.Build_CommandCenter_pt(
                "now", scv.tag, cc_xy)
        return actions.RAW_FUNCTIONS.no_op()

    # scv 추가 생성
    def train_scv(self, obs):
        completed_command_centers = self.get_my_completed_units_by_type(
            obs, units.Terran.CommandCenter)
        free_supply = (obs.observation.player.food_cap -
                       obs.observation.player.food_used)
        # food_workers = mineral 일꾼 + vespene 일꾼
        # print(obs.observation.player.food_workers)
        if (len(completed_command_centers) > 1 and obs.observation.player.minerals >= 50 and free_supply > 0 and
                obs.observation.player.food_workers < 20):
            command_centers = self.get_my_units_by_type(obs, units.Terran.CommandCenter)[1]
            if command_centers.order_length < 5:
                return actions.RAW_FUNCTIONS.Train_SCV_quick("now", command_centers.tag)
        return actions.RAW_FUNCTIONS.no_op()

    def train_marine(self, obs):
        completed_barrackses = self.get_my_completed_units_by_type(
            obs, units.Terran.Barracks)
        free_supply = (obs.observation.player.food_cap -
                       obs.observation.player.food_used)
        if (len(completed_barrackses) > 0 and obs.observation.player.minerals >= 100
                and free_supply > 0):
            barracks = self.get_my_units_by_type(obs, units.Terran.Barracks)[0]
            if barracks.order_length < 5:
                return actions.RAW_FUNCTIONS.Train_Marine_quick("now", barracks.tag)
        return actions.RAW_FUNCTIONS.no_op()

    def train_tank(self, obs):
        completed_factorytechlabs = self.get_my_completed_units_by_type(
            obs, units.Terran.FactoryTechLab)
        free_supply = (obs.observation.player.food_cap -
                       obs.observation.player.food_used)
        # techlab이 건설되어야 tank 생성 가능
        if (len(completed_factorytechlabs) > 0 and obs.observation.player.minerals >= 150 and obs.observation.player.vespene >= 100
                and free_supply > 0):
            factory = self.get_my_units_by_type(obs, units.Terran.Factory)[0]
            if factory.order_length < 5:
                return actions.RAW_FUNCTIONS.Train_SiegeTank_quick("now", factory.tag)
        return actions.RAW_FUNCTIONS.no_op()

    def marine_attack(self, obs):
        marines = self.get_my_units_by_type(obs, units.Terran.Marine)
        # idle_marines = [marine for marine in marines if marine.order_length == 0]

        # rally point 설정
        if 0 < len(marines) < 10 and self.unit_type_is_selected(obs, units.Terran.Barracks):
            if self.base_top_left:
                return actions.FUNCTIONS.Rally_Units_minimap("now", [29, 21])
            else:
                return actions.FUNCTIONS.Rally_Units_minimap("now", [29, 46])

        # marine 10대가 모였을 때 attack
        elif len(marines) == 10:
            attack_xy = (38, 44) if self.base_top_left else (19, 23)
            marins_tag = []
            for marine in marines:
                marins_tag.append(marine.tag)
            # marine 사정거리 4
            x_offset = random.randint(-4, 4)
            y_offset = random.randint(-4, 4)
            return actions.RAW_FUNCTIONS.Attack_pt(
                "now", marins_tag, (attack_xy[0] + x_offset, attack_xy[1] + y_offset))

        elif len(marines) > 10:
            attack_xy = (38, 44) if self.base_top_left else (19, 23)
            distances = self.get_distances(obs, marines, attack_xy)
            marine = marines[np.argmax(distances)]
            x_offset = random.randint(-4, 4)
            y_offset = random.randint(-4, 4)
            return actions.RAW_FUNCTIONS.Attack_pt(
                "now", marine.tag, (attack_xy[0] + x_offset, attack_xy[1] + y_offset))

        # # 명령을 받지 않은 marine이 10 이상 모여있으면 다른 방향으로 공격 시도
        # elif len(idle_marines) > 10:
        #     marines_tag = []
        #     for marine in idle_marines:
        #         marines_tag.append(marine.tag)
        #     attack_xy = (18, 47) if self.base_top_left else (40, 21)
        #     # marine 사정거리 4
        #     x_offset = random.randint(-4, 4)
        #     y_offset = random.randint(-4, 4)
        #     return actions.RAW_FUNCTIONS.Attack_pt(
        #         "now", marines_tag, (attack_xy[0] + x_offset, attack_xy[1] + y_offset))

        return actions.RAW_FUNCTIONS.no_op()

    def tank_attack(self, obs):
        tanks = self.get_my_units_by_type(obs, units.Terran.SiegeTank)
        # idle_tanks = [tank for tank in tanks if tank.order_length == 0]

        # rally point 설정
        if 0 < len(tanks) < 3 and self.unit_type_is_selected(obs, units.Terran.Factory):
            if self.base_top_left:
                return actions.FUNCTIONS.Rally_Units_minimap("now", [29, 21])
            else:
                return actions.FUNCTIONS.Rally_Units_minimap("now", [29, 46])

        # tank 3대가 모였을 때 attack
        elif len(tanks) == 3:
            attack_xy = (38, 44) if self.base_top_left else (19, 23)
            tanks_tag = []
            for tank in tanks:
                tanks_tag.append(tank.tag)
            # tank 사정거리 7
            x_offset = random.randint(-7, 7)
            y_offset = random.randint(-7, 7)
            return actions.RAW_FUNCTIONS.Attack_pt(
                "now", tanks_tag, (attack_xy[0] + x_offset, attack_xy[1] + y_offset))

        elif len(tanks) > 3:
            attack_xy = (38, 47) if self.base_top_left else (19, 23)
            distances = self.get_distances(obs, tanks, attack_xy)
            tank = tanks[np.argmax(distances)]
            x_offset = random.randint(-4, 4)
            y_offset = random.randint(-4, 4)
            return actions.RAW_FUNCTIONS.Attack_pt(
                "now", tank.tag, (attack_xy[0] + x_offset, attack_xy[1] + y_offset))

        # # 명령을 받지 않은 tank가 3대 이상 모여있으면 다른 방향으로 공격 시도
        # elif len(idle_tanks) > 3:
        #     tanks_tag = []
        #     for tank in idle_tanks:
        #         tanks_tag.append(tank.tag)
        #     attack_xy = (18, 47) if self.base_top_left else (40, 21)
        #     # tank 사정거리 7
        #     x_offset = random.randint(-7, 7)
        #     y_offset = random.randint(-7, 7)
        #     return actions.RAW_FUNCTIONS.Attack_pt(
        #         "now", tanks_tag, (attack_xy[0] + x_offset, attack_xy[1] + y_offset))

        return actions.RAW_FUNCTIONS.no_op()

class TerranRandomAgent(TerranAgentWithRawActsAndRawObs):
    def step(self, obs):
        super(TerranRandomAgent, self).step(obs)
        action = random.choice(self.actions)
        return getattr(self, action)(obs)

class TerranRLAgentWithRawActsAndRawObs(TerranAgentWithRawActsAndRawObs):
    def __init__(self):
        super(TerranRLAgentWithRawActsAndRawObs, self).__init__()

        self.s_dim = 21 # state의 개수
        self.a_dim = 14 # action의 개수

        self.lr = 1e-4 * 1
        self.batch_size = 32
        self.gamma = 0.99
        self.memory_size = 200000
        self.eps_max = 1.0
        self.eps_min = 0.01
        self.epsilon = 1.0
        self.init_sampling = 4000
        self.target_update_interval = 10

        self.data_file_qnet = 's10274_rlagent_with_dueling_dqn_qnet'
        self.data_file_qnet_target = 's10274_rlagent_with_dueling_dqn_qnet_target'

        self.qnetwork = DuelingQNet(input_dim=self.s_dim,
                           output_dim=self.a_dim).to(device)

        self.qnetwork_target = DuelingQNet(input_dim=self.s_dim,
                           output_dim=self.a_dim).to(device)

        if os.path.isfile(self.data_file_qnet + '.pt'):
            self.qnetwork.load_state_dict(torch.load(self.data_file_qnet + '.pt'))

        if os.path.isfile(self.data_file_qnet_target + '.pt'):
            self.qnetwork_target.load_state_dict(torch.load(self.data_file_qnet_target + '.pt'))

        # initialize target network same as the main network.
        self.qnetwork_target.load_state_dict(self.qnetwork.state_dict())

        self.dqn = DQN(state_dim=self.s_dim,
                             action_dim=self.a_dim,
                             qnet=self.qnetwork,
                             qnet_target=self.qnetwork_target,
                             lr=self.lr,
                             gamma=self.gamma,
                             epsilon=self.epsilon).to(device)

        self.memory = ExperienceReplayMemory(self.memory_size)

        self.print_every = 1
        self.cum_reward = 0
        self.cum_loss = 0
        self.episode_count = 0

        self.new_game()

    def reset(self):
        super(TerranRLAgentWithRawActsAndRawObs, self).reset()
        self.new_game()

    def new_game(self):
        self.base_top_left = None
        self.previous_state = None
        self.previous_action = None
        self.cum_reward = 0
        self.cum_loss = 0

        # epsilon scheduling
        # slowly decaying_epsilon
        self.epsilon = max(self.eps_min, self.eps_max - self.eps_min * (self.episode_count / 50))
        self.dqn.epsilon = torch.tensor(self.epsilon).to(device)

    def get_state(self, obs):
        scvs = self.get_my_units_by_type(obs, units.Terran.SCV)
        idle_scvs = [scv for scv in scvs if scv.order_length == 0]
        command_centers = self.get_my_units_by_type(obs, units.Terran.CommandCenter)
        supply_depots = self.get_my_units_by_type(obs, units.Terran.SupplyDepot)
        completed_supply_depots = self.get_my_completed_units_by_type(
            obs, units.Terran.SupplyDepot)
        barrackses = self.get_my_units_by_type(obs, units.Terran.Barracks)
        completed_barrackses = self.get_my_completed_units_by_type(
            obs, units.Terran.Barracks)
        marines = self.get_my_units_by_type(obs, units.Terran.Marine)

        queued_marines = (completed_barrackses[0].order_length
        if len(completed_barrackses) > 0 else 0)

        free_supply = (obs.observation.player.food_cap -
                       obs.observation.player.food_used)
        can_afford_supply_depot = obs.observation.player.minerals >= 100
        can_afford_barracks = obs.observation.player.minerals >= 150
        can_afford_marine = obs.observation.player.minerals >= 100

        enemy_scvs = self.get_enemy_units_by_type(obs, units.Terran.SCV)
        enemy_idle_scvs = [scv for scv in enemy_scvs if scv.order_length == 0]
        enemy_command_centers = self.get_enemy_units_by_type(
            obs, units.Terran.CommandCenter)
        enemy_supply_depots = self.get_enemy_units_by_type(
            obs, units.Terran.SupplyDepot)
        enemy_completed_supply_depots = self.get_enemy_completed_units_by_type(
            obs, units.Terran.SupplyDepot)
        enemy_barrackses = self.get_enemy_units_by_type(obs, units.Terran.Barracks)
        enemy_completed_barrackses = self.get_enemy_completed_units_by_type(
            obs, units.Terran.Barracks)
        enemy_marines = self.get_enemy_units_by_type(obs, units.Terran.Marine)

        return (len(command_centers),
                len(scvs),
                len(idle_scvs),
                len(supply_depots),
                len(completed_supply_depots),
                len(barrackses),
                len(completed_barrackses),
                len(marines),
                queued_marines,
                free_supply,
                can_afford_supply_depot,
                can_afford_barracks,
                can_afford_marine,
                len(enemy_command_centers),
                len(enemy_scvs),
                len(enemy_idle_scvs),
                len(enemy_supply_depots),
                len(enemy_completed_supply_depots),
                len(enemy_barrackses),
                len(enemy_completed_barrackses),
                len(enemy_marines))

    def step(self, obs):
        super(TerranRLAgentWithRawActsAndRawObs, self).step(obs)

        #time.sleep(0.5)

        state = self.get_state(obs)
        state = torch.tensor(state).float().view(1, self.s_dim).to(device)
        action_idx = self.dqn.choose_action(state)
        action = self.actions[action_idx]
        done = True if obs.last() else False

        if self.previous_action is not None:
            experience = (self.previous_state.to(device),
                          torch.tensor(self.previous_action).view(1, 1).to(device),
                          torch.tensor(obs.reward).view(1, 1).to(device),
                          state.to(device),
                          torch.tensor(done).view(1, 1).to(device))
            self.memory.push(experience)

        self.cum_reward += obs.reward
        self.previous_state = state
        self.previous_action = action_idx

        if obs.last():
            self.episode_count = self.episode_count + 1

            if len(self.memory) >= self.init_sampling:
                # training dqn
                sampled_exps = self.memory.sample(self.batch_size)
                sampled_exps = prepare_training_inputs(sampled_exps, device)
                self.dqn.learn(*sampled_exps)

            if self.episode_count % self.target_update_interval == 0:
                self.dqn.qnet_target.load_state_dict(self.dqn.qnet.state_dict())

            if self.episode_count % self.print_every == 0:
                msg = (self.episode_count, self.cum_reward, self.epsilon)
                print("Episode : {:4.0f} | Cumulative Reward : {:4.0f} | Epsilon : {:.3f}".format(*msg))

            torch.save(self.dqn.qnet.state_dict(), self.data_file_qnet + '.pt')
            torch.save(self.dqn.qnet_target.state_dict(), self.data_file_qnet_target + '.pt')

            #writer.add_scalar("Loss/train", self.cum_loss/obs.observation.game_loop, self.episode_count)
            #writer.add_scalar("Score", self.cum_reward, self.episode_count)

        return getattr(self, action)(obs)

def main(unused_argv):
   agent1 = TerranRLAgentWithRawActsAndRawObs()
   try:
       with sc2_env.SC2Env(
               map_name="Simple64",
               players=[sc2_env.Agent(sc2_env.Race.terran),
                        sc2_env.Bot(sc2_env.Race.terran,
                                    sc2_env.Difficulty.easy)],
               agent_interface_format=features.AgentInterfaceFormat(
                   action_space=actions.ActionSpace.RAW,
                   use_raw_units=True,
                   raw_resolution=64,
               ),
               step_mul=16,
               disable_fog=True,
               visualize=False
       ) as env:
           run_loop.run_loop([agent1], env, max_episodes=1000)
   except KeyboardInterrupt:
       pass

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


# coding:utf-8

import datetime
import pymongo
import queue
import re
import threading
import IPy
import argparse
import requests
from bs4 import BeautifulSoup
from requests.packages.urllib3.exceptions import InsecureRequestWarning

# 屏蔽HTTPS证书错误
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# HTTP Headers
Headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
			AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}

class httpspider:
    def __init__(self, target, threads, timeout):
        self.target = target
        self.threads = threads
        self.timeout = timeout
        self.semaphore = threading.Semaphore(self.threads)  # Thread cound
        self.init_mongodb()  # 连接mongodb
        self.load_ip()  # Load IP in queue

    # Add ip on queue
    def load_ip(self):
        self.queue = queue.Queue()
        ips = IPy.IP(self.target)
        for ip in ips:
            ip = str(ip)
            ip_d = re.match(r'((\d{1,3}\.){3,})(\d{1,3})', ip).group(3)  # Delete a.b.c.0 & a.b.c.255
            if ip_d == '255' or ip_d == '0':
                continue
            self.queue.put(ip)

    def init_mongodb(self):
        client = pymongo.MongoClient("mongodb://127.0.0.1:27017/httpspider?maxPoolSize=200&waitQueueMultiple=200")
        db = client.httpspider
        self.collection = db.info

    def scan(self):
        # 获取queue ip
        ip = self.queue.get()
        url = 'http://' + ip + '/'
        # 创建结果文件
        # fo = open(self.target[:-3] + '.html', 'a+')
        try:
            req = requests.get(url, verify=False, timeout=self.timeout, headers=Headers)
            size = len(req.content)
            code = req.status_code
            soup = BeautifulSoup(req.content.decode('utf-8', 'ignore'), "html.parser")
            headers = req.headers
            if soup.title:
                if soup.title.string:
                    title = soup.title.string.strip('\n').strip('\r')
                else:
                    title = ''
            else:
                title = ''
            if size != 0 and (code == 200 or code == 301):
                # 写入数据库测试
                self.collection.insert({'ip': ip,
                                        'title': title,
                                        'status': code,
                                        'headers': headers,
                                        'date': datetime.datetime.utcnow()})
        except requests.HTTPError as msg:
            print(ip, ' Error.')

        except requests.exceptions.Timeout as msg:
            print(ip, 'timeout')

        except requests.exceptions.ConnectionError as msg:
            print(ip, '远程主机强迫关闭了一个现有的连接')

        # 释放限制线程数的锁
        self.semaphore.release()

    def run(self):
        while not self.queue.empty():
            if self.semaphore.acquire():
                t = threading.Thread(target=self.scan)
                t.start()

if __name__ == '__main__':
    # Argument parser
    parser = argparse.ArgumentParser(description='网段Web主机发现工具：')
    parser.add_argument('target', help='指定ip段   例:127.0.0.0/24')
    parser.add_argument('-t', type=int, default=100, dest='threads', help="线程数  默认:100")
    parser.add_argument('-o', type=int, default=3, dest='timeout', help="超时时间   默认:3")
    args = parser.parse_args()

    target = args.target
    threads = args.threads
    timeout = args.timeout

    httpspider = httpspider(target, threads, timeout)
    # httpspider = httpspider('123.125.114.0/24', 50, 3)
    httpspider.run()


#Roisin Anglim 11-02-18

#Collatz : If even number divide by 2, if uneven multiply by 3 + 1

# Requests user to enter an interger

i = int(input("input integer "))

print(i)

#Do the following when i is not equal to one

while i != 1:

#When i even number

     if i %2 ==0:

         i= i/2

         print(int(i))

#When i uneven number

     elif i %2 ==1:

         i= i*3+1

         print(int(i))


def cont(nome, estado_civil):
    # Precessamento de dados
    if estado_civil == 1:
        nome_conjugue = input('Digite o nome do conjuguê: ')
        return f'A quantidade de caracteres no nome mais o nome do conjuguê {len(nome) + len(nome_conjugue)}'
    else:
        return f'A quantidade de caracteres no nome é {len(nome)}'

def main():
    # Entrada de dados
    nome = input('Digite seu nome: ')
    estado_civil = int(input('Estado Civil [1- Casado/ 2- Solteiro]: '))

    # Saída de dados
    print(cont(nome,estado_civil))

if __name__ == '__main__':
    main()


#! /usr/bin/env python
from peyotl.nexson_syntax import extract_tree, PhyloSchema
from peyotl.test.support import pathmap
from peyotl.utility import get_logger
import unittest
import os
_LOG = get_logger(__name__)

# round trip filename tuples
RT_DIRS = ['otu', '9', ]

def _get_pair(par, f, s):
    bf = os.path.join(par, f)
    hbf = os.path.join(par, s)
    fp, sp = pathmap.nexson_source_path(bf), pathmap.nexson_source_path(hbf)
    if not os.path.exists(fp):
        _LOG.warn('\nTest skipped because {s} does not exist'.format(s=fp))
        return None, None
    if not os.path.exists(sp):
        _LOG.warn('\nTest skipped because {s} does not exist'.format(s=sp))
        return None, None
    return pathmap.nexson_obj(bf), pathmap.nexson_obj(hbf)

class TestExtract(unittest.TestCase):

    def testNewickExport(self):
        n = pathmap.nexson_obj('10/pg_10.json')
        newick = extract_tree(n, 'tree3', PhyloSchema('newick', tip_label='ot:ottTaxonName', bracket_ingroup=True))
        self.assertTrue('[pre-ingroup-marker' in newick)
        self.assertTrue('[post-ingroup-marker' in newick)
        self.assertTrue(newick.startswith('('))
        newick = extract_tree(n, 'tree3', PhyloSchema('newick', tip_label='ot:ottTaxonName'))
        self.assertTrue('[pre-ingroup-marker' not in newick)
        self.assertTrue('[post-ingroup-marker' not in newick)
        self.assertTrue(newick.startswith('('))

    def testTreeExport(self):
        n = pathmap.nexson_obj('10/pg_10.json')
        newick = extract_tree(n, 'tree3', PhyloSchema('nexus', tip_label='ot:ottTaxonName'))
        self.assertTrue(newick.startswith('#'))
if __name__ == "__main__":
    unittest.main()


import argparse
import os.path as osp

import imageio
import tqdm

from ..utils import get_macro_block_size

def retime(in_file, retime, inplace=False):
    stem, ext = osp.splitext(in_file)
    out_file = stem + "_retime{:.3g}".format(retime) + ext

    reader = imageio.get_reader(in_file)
    meta_data = reader.get_meta_data()
    fps = meta_data["fps"]

    macro_block_size = get_macro_block_size(meta_data["size"])

    writer = imageio.get_writer(
        out_file, fps=fps * retime, macro_block_size=macro_block_size
    )
    for data in tqdm.tqdm(reader, total=reader.count_frames()):
        writer.append_data(data)

    reader.close()
    writer.close()

    if inplace:
        raise NotImplementedError

def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument("in_files", nargs="+", help="input files")
    parser.add_argument(
        "--retime", "-x", type=float, default=1, help="retime factor"
    )
    parser.add_argument(
        "--inplace", "-i", action="store_true", help="operate in-place"
    )
    args = parser.parse_args()

    for in_file in args.in_files:
        retime(in_file=in_file, retime=args.retime, inplace=args.inplace)


#!/usr/bin/python3

from math import *
import matplotlib.pyplot as plt
import matplotlib.patches as patches

import numpy as np
from scipy import interpolate
from scipy.integrate import odeint

d2=.6*0.0254 # (m) inner diameter of the outer tubular housing
d1=.4*0.0254 # (m) diameter of the inner cold cylinder before
               # cutting any grooves

fig,ax=plt.subplots()

ax.set_xlim([-d2/2,d2/2])
ax.set_ylim([-d2/2,d2/2])

# outer housing
circle1=plt.Circle((0,0),d2/2,color='r',fill=False)
ax.add_artist(circle1)

groove_depth=0.1*0.0254 # m
groove_width=0.04*0.0254 # m
n=ngrooves=10

# shorter names
r=d1/2
w=groove_width
d=groove_depth

for groove in range(ngrooves):
    theta=360./ngrooves
    center_angle=groove*theta

    alpha=center_angle*pi/180
    x=r*cos(alpha)
    y=r*sin(alpha)

    dalpha=asin(w/2/r)

    xsl=r*cos(alpha+dalpha)
    ysl=r*sin(alpha+dalpha)
    xel=xsl-d*cos(alpha)
    yel=ysl-d*sin(alpha)

    xsr=r*cos(alpha-dalpha)
    ysr=r*sin(alpha-dalpha)
    xer=xsr-d*cos(alpha)
    yer=ysr-d*sin(alpha)

    # draw line at edge of each groove
    line=plt.plot([xsl,xel],[ysl,yel],color='black')
    line=plt.plot([xsr,xer],[ysr,yer],color='black')
    # and bottom of groove
    line=plt.plot([xel,xer],[yel,yer],color='black')

    alpha_deg=alpha*180/pi
    dalpha_deg=dalpha*180/pi
    arc=patches.Arc((0,0),2*r,2*r,0,alpha_deg+dalpha_deg,alpha_deg+theta-dalpha_deg)
    ax.add_patch(arc)

# Calculation of perimeter of all those grooves
pgroove=2*d+w # inner "U" of a groove
parc=(theta-2*dalpha_deg)*pi/180*r # outer arc length between two grooves
perimeter=ngrooves*(pgroove+parc)

print('The length of the inner U of a groove is %f m'%pgroove)
print('The length of an arc between two grooves is %f m'%parc)
print('The length around all those fins and grooves is %f m'%perimeter)

print()

# Calculation of area for flow
annulus=pi*(d2**2-d1**2)/4
agroove=d*w # area of one groove # approximately
aeps=((2*dalpha)/(2*pi))*pi*r**2-2*0.5*(w/2)*(r*cos(dalpha)) # area
                                                             # between
                                                             # the arc
                                                             # and the
                                                             # area of
                                                             # the
                                                             # groove
                                                             # on the
                                                             # previous
                                                             # line
agroove_total=agroove+aeps
agrooves=agroove_total*ngrooves
area=annulus+agrooves

print('Area of annular region between cylinders %f m^2'%annulus)
print('Additional area cut out by each groove %f m^2'%(agroove+aeps))
print('Area of all the grooves %f m^2'%agrooves)
print('Total flow area %f m^2'%area)
print()

a=area # (m^2) area for fluid flow
p=perimeter+pi*d2 # (m) perimeter of flow region

dh=4*a/p # (m) hydraulic diameter

print('Flow area %f m^2'%a)
print('Flow perimeter %f m'%p)
print('Hydraulic diameter %f m'%dh)
print()

L=10.*0.0254 # (m) length of tube(s)
Aw=perimeter*L
print('Length of tubes %f m'%L)
print('Area of cold wall %f m^2'%Aw)
print()

mdot=0.004 # (kg/s) mass flow rate
G=mdot/a # (kg/(m^2*s)) mass flow rate per unit area

print('Mass flux (G) is %f kg/(m^2*s)'%G)

# 350 micro-poise at 20.5 K.... this is at saturation, but probably
# close enough to 20 psia.
# https://nvlpubs.nist.gov/nistpubs/Legacy/TN/nbstechnicalnote641.pdf

mu=3.5e-5 # Pa*s

Re=dh*G/mu # should be dimensionless
print('The Reynolds number is %f'%Re)

fRe=24.00*4

#Creating an elif for f based on Barron eq'ns

if Re < 2300 :
    f=fRe/Re
    #f=64/Re #assuming cicular tube
    print('The laminar friction factor is %f.' %f)
elif 3500 > Re > 2300 :
    f=1.2036*Re**(-0.416) #from vijayan
    print('The friction factor is in between laminar and turbulent')
elif Re > 3500 :
    f=0.316*Re**(-0.25)
    print('The turbulent friction factor is %f.' %f)

B1=1.174*((3.7e-5)/(3.68e-5))**(0.14) #viscosity taken from cams sheets
print('This is B1 %f.' %B1)

if Re < 3500 :
    print('It is laminar or in between')
elif Re > 3500 :
    jh=0.023*Re**(-0.2)*B1
    print('The Colburn factor for the turbulent flow is %f.' %jh)

print()

#all from Barron

# thermal conductivity

kt=0.104 # W/(m*K) a check on this number from
         # https://nvlpubs.nist.gov/nistpubs/Legacy/TN/nbstechnicalnote641.pdf
         # gives the value of about 1.05 mW/(cm*K) = 0.105 W/(m*K) at
         # a temperature of about 22 K.  It says in this reference
         # that the data is uncertain at the 25% level, though.
#

# specific heat

import CoolProp.CoolProp as CP
import numpy as np
fluid='Deuterium'

p_psi=20. # PSI
p=p_psi*6894.76 # Pa

Tin=23.4 # (K) inlet temp
Tw=20.7 # (K) temperature of cold wall
#Ts=(Tw+Tin)/2 # (K) temperature of film, with which we will exchange heat.

T=Tin

Cp=CP.PropsSI('C','P',p,'T',T,fluid) # (kg/(m*K)) found from coolprop -
                                    # found via a table

Pr=(mu*Cp)/(kt) # yes still dimensionless
               # because (Pa*s)*(J/(kg*K))/(W/(m*K))
               # =((kg*m/(s^2*m^2))*s)*(W*s/(kg*K))*((m*K)/W) = 1

print('The Prandtl Number is %f.'%Pr)

#If turb

if Re < 3500 :
    print('Nu=4.8608 because the flow is laminar')
elif Re > 3500 :
    Nuturb=jh*Re*Pr**(1./3.)
    print('This is the turbulent Nusselt Number %f.' %Nuturb)

print()

#Nu_3_ii=4.8608 # Table 90 of Shah and London

#Nu=3.657 # circular duct Barron Eq. (6.30)
#Nu=7.541 # Table 6.2 Barron -- parallel plate =~ thin annulus?
#NuT=7.541 # Table 86 of Shah and London, thin annulus
#NuT=7.541 # Table 138 of Shah and London, parallel plate
#NuT=4.861 # Table 138 of Shah and London, parallel plate one side insulated
Nu=4.8608 # Eq. (283) Shah and London, parallel plate one side insulated

#fRe=64.00 # circular duct Barron Eq. (6.27)
#fRe=96.00 # Table 6.2 Barron
fRe=24.00*4 # Table Table 86 of Shah and London, thin annulus

# calculate heat transfer -- inner surface to fluid

if Re < 3500 :
    hc=Nu*kt/dh # Barron eq'n 6.15
    print('The heat transfer coefficient for laminar flow is %f W/(m^2*K)'%hc)
elif Re > 3500 :
    hc=Nuturb*kt/dh # Barron eq'n 6.17 makes it incredibly tiny compared to eq'n 6.15 maybe should be using eq'n 6.40 ??
    print('The heat transfer coefficient for turbulent flow is %f W/(m^2*K)'%hc)

Ntu=hc*Aw/(mdot*Cp)
print('The number of transfer units is %f'%Ntu)
print()

T1=Tin
T2=T1-(T1-Tw)*(1-exp(-Ntu))
#T2=Tw+(T1-Tw)*exp(-Ntu)

Qtotal=mdot*Cp*(T1-T2) # Eq. (6.43) of Barron

print('For inlet temperature %f K and wall temperature %f K'%(T1,Tw))
print('the outlet temperature is %f K'%T2)
print('and the total heat transfer rate is %f W'%Qtotal)
print()

# calculate pressure drop

#f=fRe/Re
#print('The friction factor is %f'%f)

rho=CP.PropsSI('D','P',p,'T',T,fluid) # (kg/m^3)
print('The density is %f kg/m^3'%rho)

dp=(f*L*G**2)/(dh*2*rho) # (Pa) pressure drop
# unit check:
# [L]=m
# [G**2]=kg^2/(s^2*m^4)
# [dh]=m
# [rho]=kg/m^3
# So [p]=(kg^2/(s^2*m^3))/(kg/m^2)=kg/(s^2*m)=(kg*m/s^2)/m^2=[force]/[area]
# =Pa (as expected)

print('The pressure drop is %f Pa'%dp)

plt.title('The Cross Section of the Heat Exchanger.')
plt.show()

#Rectangular Channels in Parallel, Barron 6.3.3, and ex. 6.3

#for one

Af=w*d # (m^2)

Pf=2*w + 2*d # (m)

Dh=4*Af/Pf # (m)

print('The hydraulic diameter for one rectangular channel is %f m.' %Dh)

#all

Afall=d*(n*w) # (m^2)

#Pfall=2*d + 2*n*w # (m)
Pfall=2*n*d + 2*n*w # (m) # Jeff edited

Dhall=4*Afall/Pfall # (m)

print('The flow area for all the rectangular channels is %f m^2.' %Afall)
print()

print('The hydraulic diameter for all the rectangular channels is %f m.' %Dhall)

#Mass flux

Grect=mdot/Afall

Rerect=Dhall*Grect/mu # should be dimensionless

print('The Reynolds number for the rectangles is %f'%Rerect) #still laminar!                                                               #albeit larger than                                                           #the cylinder

#b/a

ba=w/d #dimentionless

print('The b/a is %f.' %ba) # b/c 0.600 I am using C1, C2, from Table 6.2

fRerect=C1=59.94

frect=fRerect/Rerect

print('The friction factor is %f.'%frect)

Nurect=C2=3.205 #Where I used Nu(T) for constant Temperature on a wall
                #(taking the one wall to be the outter constant, the
                #other C2=3.896=Nu(Q) for constant heat flux at all
                #four walls

                # Jeff says:  I don't fully get the comment above.

hcrect=(Nurect*kt)/Dhall # W/(m^2*K)

print('The hc of the rectangles is %f W/(m^2*K)' %hcrect)

#Wetted area of duct

#Awrect = 2*(w + d)*L # (m^2)
Aw_wet=(2*n*w+2*n*d)*L # (m^2) # Jeff edited # entire wetted wall area -- for friction losses
Aw_heat=(n*w+2*n*d)*L # (m^2) # Jeff edited # area for heat transfer

print('Wetted area of duct is %f' %Aw_wet)
print('Area for heat transfer is %f' %Aw_heat)

#transfer units

Nturect=(hcrect*Aw_heat)/(mdot*Cp)

print('The transfer units is %f' %Nturect)

#heat transfer

Qrect=mdot*Cp*(T1 - Tw)*(1 - exp(-Nturect)) #W

print('The heat transfer is %f W' %Qrect)

#pressure drop

dprect=(frect*L*Grect**2)/(Dhall*2*rho) #Pa

print('The pressure drop is %f Pa.' %dprect)

#heat conduction

dx= 2*0.0254

kt300=50.214*100 #W/m⋅K

dT = -(60/(kt300*a))*dx

kt50 = 13.764*100 # W/m⋅K

dT50 = -(60/(kt50*a))*dx

print('The dT for Cu300 is %f K.' %dT)
print('The dT for Cu50 is %f K.' %dT50)

print(a*10000)

def ODE(T,x):

   dTdx = float(-Qtotal/(kt*a) )

   return dTdx

#inital conditions

T0 = 19 # K

x = np.arange(0, 10, 0.0001)
solnT = odeint(ODE, T0, x) #from scipy
z = interpolate.interp1d(x, solnT[:,0])

#making callable function

def T(x):

    T = z(x)

    return T

plt.plot(T(x),x)
plt.title('Temperature as a function of position')
plt.show()


from selenium.webdriver.support.select import Select
from planner.views import ERR_NO_MEAL, ERR_MEAL_NO_TITLE
from .base import FunctionalTest

class ValidationTest(FunctionalTest):

    def test_cannot_enter_empty_meal(self):
        # Mike visits the homepage and tries to submit an empty meal
        self.browser.get(self.live_server_url + '/meals')
        self.enter_recipe('')
        # After refreshing the home page shows an error message
        self.wait_for(lambda: self.assertEqual(
            self.browser.find_element_by_css_selector('.has-error').text,
            ERR_MEAL_NO_TITLE
        ))
        # He enters a name for his meal and finds it works
        self.enter_recipe('Pork and Apple Sauce')
        self.wait_for_row_in_table('id_meal_table', 'Pork and Apple Sauce')

    def test_cannot_submit_meal_day_change_without_meal_selected(self):
        # Mike tries to submit  add meal without selecting a meal first.
        self.browser.get(self.live_server_url)
        # Select(self.browser.find_element_by_id(
        #     'id_day_selector')).select_by_value("2")
        self.browser.find_element_by_id('meal_selector_submit').click()

        # After refreshing the home page shows an error message
        self.wait_for(lambda: self.assertEqual(
            self.browser.find_element_by_css_selector('.has-error').text,
            ERR_NO_MEAL
        ))


import pygame
from settings import *

pygame.init()
pygame.font.init()
font = pygame.font.Font('freesansbold.ttf', 20)
lose = font.render('Игрок 2 ВЫИГРАЛ!', True, WHITE)
win = font.render('Игрок 1 ВЫИГРАЛ!', True, WHITE)
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption("Pong")
pygame.display.set_icon(pygame.image.load('layers/pong.png'))
x = int(farness + paddle_height * 2 + ball_size)
y = int(0)
screen_size_x, screen_size_y = pygame.display.get_window_size()
print(screen_size_y)
paddle_y = int(0)
enemy_paddle_y = int(0)
down = bool(1)
up = bool(0)
left = bool(0)
right = bool(1)
paddle_down = bool(0)
paddle_up = bool(0)
enemy_paddle_down = bool(0)
enemy_paddle_up = bool(0)
win_state = bool(False)
lose_state = bool(False)
score_p1 = 0
score_p2 = 0

def paddle_ai():
    global enemy_paddle_up, enemy_paddle_down, paddle_up, paddle_down
    if x < screen_size_x // 2:
        if y > enemy_paddle_y + paddle_width // 2:
            enemy_paddle_down = 1
            enemy_paddle_up = 0
        elif y < enemy_paddle_y + paddle_width // 2:
            enemy_paddle_down = 0
            enemy_paddle_up = 1
    if x > screen_size_x // 2:
        if y > paddle_y + paddle_width // 2:
            paddle_down = 1
            paddle_up = 0
        elif y < paddle_y + paddle_width // 2:
            paddle_down = 0
            paddle_up = 1

def main():
    global x, y, up, down, left, right, paddle_y, paddle_up, paddle_down, enemy_paddle_y, enemy_paddle_up, enemy_paddle_down, lose_state, win_state, score_p1, score_p2
    score1 = font.render(f'Счет (Игрок 1): {score_p1}', True, WHITE)
    score2 = font.render(f'Счет (Игрок 2): {score_p2}', True, WHITE)
    key = pygame.key.get_pressed()
    paddle_ai()
    '''Paddle collision'''
    if enemy_paddle_y < 0:
        enemy_paddle_y = 0
    if (enemy_paddle_y + paddle_width) > screen_size_y:
        enemy_paddle_y = screen_size_y - paddle_width
    if paddle_y < 0:
        paddle_y = 0
    if (paddle_y + paddle_width) > screen_size_y:
        paddle_y = screen_size_y - paddle_width
    '''Detect collision'''
    if screen_size_x - farness - paddle_height <= x + ball_size <= screen_size_x - farness and paddle_y < y + ball_size // 2 < paddle_y + paddle_width:
        right = 0
        left = 1
        score_p1 += 1
    if farness + paddle_height >= x - ball_size >= farness and enemy_paddle_y < y + ball_size // 2 < enemy_paddle_y + paddle_width:
        right = 1
        left = 0
        score_p2 += 1
    '''Move objects'''
    y += down * speed - up * speed
    x += right * speed - left * speed
    paddle_y += paddle_down * speed - paddle_up * speed
    enemy_paddle_y += enemy_paddle_down * (speed - 0) - enemy_paddle_up * (speed - 0)
    '''Drawing'''
    pygame.draw.circle(screen, WHITE, (x, y), ball_size)
    pygame.draw.rect(screen, WHITE, ((screen_size_x - farness) - paddle_height, paddle_y, paddle_height, paddle_width))
    pygame.draw.rect(screen, WHITE, (farness, enemy_paddle_y, paddle_height, paddle_width))
    '''Stop moving'''
    paddle_up = 0
    paddle_down = 0
    enemy_paddle_up = 0
    enemy_paddle_down = 0
    if y >= screen_size_y - ball_size:
        up = 1
        down = 0
    if y <= 0 + ball_size:
        up = 0
        down = 1
    if x <= -10:
        win_state = True
    if x >= screen_size_x:
        lose_state = True

    if key[pygame.K_w]:
        paddle_up = 1
        paddle_down = 0
    if key[pygame.K_s]:
        paddle_up = 0
        paddle_down = 1

    if key[pygame.K_UP]:
        enemy_paddle_up = 1
        enemy_paddle_down = 0
    if key[pygame.K_DOWN]:
        enemy_paddle_up = 0
        enemy_paddle_down = 1
    pygame.time.delay(0)

    if win_state:
        screen.blit(win, (20, 0))
        pygame.display.update()
        pygame.time.delay(5000)
        exit()
    if lose_state:
        screen.blit(lose, (20, 0))
        pygame.display.update()
        pygame.time.delay(5000)
        exit()
    screen.blit(score1, (20, 0))
    screen.blit(score2, (20, 30))
    pygame.display.update()

if __name__ == "__main__":
    while True:
        for i in pygame.event.get():
            if i.type == pygame.QUIT:
                exit()
        main()
        screen.fill(BLACK)


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 29 13:05:33 2019

@author: David
"""

import sys, os
path_tools = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
sys.path.insert(1, path_tools)
from tools import *

def preprocess_wm_files_alone(wm_fmri_paths, masks, wm_beh_paths, condition,
    distance='mix', sys_use='unix', nscans_wm=16, TR=2.335):
    #
    ### Mask and process the fmri data
    p=os.getcwd()
    start_process_wm = time.time()
    numcores = multiprocessing.cpu_count()
    wm_masked= Parallel(n_jobs = numcores)(delayed(mask_fmri_process)(fmri_path, masks, sys_use='unix')  for fmri_path in wm_fmri_paths)    ####
    scans_wm_runs = [len(wm_masked[r]) for r in range(len(wm_masked)) ]

    ### TRs of interest
    activity_beh  = Parallel(n_jobs = numcores)(delayed(wm_condition2)(masked_data, beh_path, n_scans, condition, distance=distance, sys_use='unix', TR=TR, nscans_wm=nscans_wm) for masked_data, beh_path, n_scans in zip( wm_masked, wm_beh_paths, scans_wm_runs))    ####
    runs_signal = [activity_beh[i][0] for i in range(len(activity_beh))]
    runs_beh = [activity_beh[i][1] for i in range(len(activity_beh))]

    ## concatenate the runs
    testing_activity = np.vstack(runs_signal)
    testing_behaviour = pd.concat(runs_beh)
    ##
    end_process_wm = time.time()
    process_wm = end_process_wm - start_process_wm
    print( 'Time process wm: ' +str(process_wm))
    return testing_activity, testing_behaviour


from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import os

page = input("긁어올 페이지를 입력해주세요 : ")
driver = webdriver.Chrome('/mnt/c/repository/dev/osscamp/chromedriver.exe')
driver.implicitly_wait(3)
chk = True

try: os.mkdir("photo")
except FileExistsError: pass
try: os.mkdir("preview")
except FileExistsError: pass

driver.get('http://gall.dcinside.com/board/lists/?id=seulgi&page='+ page +'&exception_mode=recommend')
r = driver.page_source
root = BeautifulSoup(r, "html.parser")
textlinks = root.find_all('a', class_ = 'icon_pic_b') #개념글 링크

for a in textlinks:
    path = "photo"
    chk = True
    title = a.get_text()
    title_div =  title.split()
    for word in title_div:
        if(word == '움짤'):
            chk = False
        elif(word == '프리뷰'):
            path = "preview"
        elif(word == "by"):
            if(chk == False): continue
            textlink = 'http://gall.dcinside.com/' + a['href']
            driver.get(textlink)
            page_source = driver.page_source
            root_page_source = BeautifulSoup(page_source, "html.parser")
            imglinks = root_page_source.find_all('li', class_ = 'icon_pic')
            os.mkdir(path+"/" + title)
            for base_link in imglinks:
                imglink = base_link.a.get('href')
                rp_from = "http://image.dcinside.com/download.php"
                rp_to = "http://image.dcinside.com/viewimage.php"
                imglink = imglink.replace(rp_from, rp_to)

                f = open("./"+ path + "/" + title +"/" + base_link.a.contents[0], "wb")
                imgr = requests.get(imglink)
                f.write(imgr.content)
                f.close()


import os
import sys

from fabric.api import (
    run,
    sudo,
    cd,
    hosts,
    settings as fabric_settings
)
from fabric.operations import get, prompt, local
from fabric.contrib.project import rsync_project

WEB_USER = "www-data"

@hosts('pytexas.org')
def deploy():
    with cd('/var/www/PyTexasWeb/'):
        sudo('git pull', user=WEB_USER)
        sudo('git submodule update', user=WEB_USER)
        sudo('su -c "pip install -r requirements.txt --user" {}'.format(WEB_USER))
        sudo('su -c "./manage.py migrate" {}'.format(WEB_USER))
        sudo('su -c "./manage.py collectstatic --noinput" {}'.format(WEB_USER))

        sudo('sudo supervisorctl restart pytx')

@hosts('pytexas.org')
def sync(postgres_role=None, use_text_dump=False):
    dump_file = '/tmp/pytexasweb_dump.sql'
    dump_flags = '-Fc'

    # in some cases, if the postgres role doesn't exist
    # on the machine (ex. installing from homebrew on OSX)
    # wencan choose to use a text dump only so user defaults
    # to database owner on restoration
    if use_text_dump:
        dump_flags = ''

    while 1:
        ans = prompt("Create a new db snapshot [y/n]?")
        if ans.lower() in ['y', 'n']:
            break

    if ans.lower() == 'y':
        sudo("pg_dump {} -h db.internal -U postgres pytexasweb > {}".format(dump_flags, dump_file), user=WEB_USER)

    get(remote_path=dump_file, local_path=dump_file)

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pytx.settings")
    mypath = os.path.dirname(__file__)
    sys.path.append(mypath)

    from django.conf import settings
    db = settings.DATABASES['default']

    # if a different role is passed then use this role instead
    if postgres_role:
        with fabric_settings(warn_only=True):
            local('createuser --superuser {}'.format(postgres_role))
        # replace postgres from the dumpfile with user
        local("perl -pi -e 's/postgres/{}/g' {}".format(postgres_role, dump_file))
        db['USER'] = postgres_role

    with fabric_settings(warn_only=True):
        local("dropdb -h {HOST} -U {USER} -p {PORT} {NAME}".format(**db))
        local("createdb -h {HOST} -U {USER} -p {PORT} {NAME}".format(**db))

    if use_text_dump:
        local("psql {} < {}".format(db['NAME'], dump_file))
    else:
        local("pg_restore -h {HOST} -U {USER} -d {NAME} -p {PORT} {}".format(dump_file, **db))

    rsync_project(
        remote_dir='/var/www/static/uploads/',
        local_dir=settings.MEDIA_ROOT,
        upload=False
    )

@hosts('pytexas.org')
def upgrade_system():
    sudo('apt-get update')
    sudo('apt-get upgrade -y')
    sudo('reboot')


class Solution(object):
    def myPow(self, base, exponent):
        """
        :type base: float
        :type exponent: int
        :rtype: float
        """
        if exponent == 0:
            return 1
        elif exponent == 1:
            return base
        elif exponent == -1:
            return 1.0 / base
        elif exponent % 2 == 0:
            next_pow = self.myPow(base, exponent // 2)
            return next_pow * next_pow
        else:
            next_pow = self.myPow(base, exponent // 2)
            return base * next_pow * next_pow


import pygame
import os
from Socket import Socket
from load_image import load_image

class bulb(pygame.sprite.Sprite):
    """
        Class for the lightbulb. Again, much of this looks just like in the gate class.
    """
    # This code should be pretty self-explanatory.

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.type = None
        self.image = None
        self.state = False
        self.inSockets = []

    def create(self, gateType, mouseCords, screen):
        self.screen = screen
        self.type = gateType
        self.image, self.rect = load_image(os.getcwd()+"/bulb0.png", -1)
        print(self.type, " created, cords: ", self.rect.topleft)
        self.setCords(mouseCords[0], mouseCords[1])
        socket = Socket()
        socket.setType("in")
        socket.setGate(self)
        socket.setIndex(0)
        socket.setScreen(self.screen)
        socket.update()
        self.inSockets.append(socket)

    def SHUTDOWNEVERYTHING(self):
        self.image, self.rect = None, None
        for cab in self.inSockets[0].attachedCables:
            cab.startSocket.attachedCables.remove(cab)
            cab.setStartSocket(None)
            cab.setEndSocket(None)
        self.inSockets[0].attachedCables = []

    def setCords(self, x, y):
        self.rect.topleft = x, y
        self.screen.blit(self.image, (self.rect.topleft))
        self.cords = (x, y)

    def pressed(self, mouse):
        if mouse[0] > self.rect.topleft[0]:
            if mouse[1] > self.rect.topleft[1]:
                if mouse[0] < self.rect.bottomright[0]:
                    if mouse[1] < self.rect.bottomright[1]:
                        for socket in self.inSockets:
                            if socket.pressed(mouse):
                                return "in"+str(socket.index)
                        return "main"
                    else:
                        return False
                else:
                    return False
            else:
                return False
        else:
            return False

    def updateState(self):
        if self.inSockets[0].state:
            self.image, self.rect = load_image(os.getcwd()+"/bulb1.png", -1)
            self.state = True
        else:
            self.image, self.rect = load_image(os.getcwd()+"/bulb0.png", -1)
            self.state = False
        self.rect.topleft = self.cords

    def update(self):
        # There's only one, I jsut added this so the program wouldn't crash when there  isn't one for some reason (could also have an if len(self.inSockets), or try(...) )
        for sock in self.inSockets:
            sock.update()
        self.updateState()
        self.screen.blit(self.image, (self.rect.topleft))


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

import numpy
import os
import gc
import torch
from keras import backend as K
from keras.backend.tensorflow_backend import set_session
from keras.backend.tensorflow_backend import clear_session
from keras.backend.tensorflow_backend import get_session
import tensorflow as tf
from tensorflow.python.client import device_lib

## Cleanning previous session
def clearSession(model=None):
  print("Free memory in GPU ...")
  try:
    del model # this is from global space - change this as you need
  except:
    pass
  gc.collect()
  torch.cuda.empty_cache()
  K.clear_session()
  print("Done")

def clearModel(model=None):
  print("Free memory in GPU ...")
  try:
    del model # this is from global space - change this as you need
  except:
    pass
  print("Done")

def setDeviceConfig(num_CPU, num_GPU):
  num_cores = int(os.cpu_count()/4)
  config = tf.ConfigProto(
              log_device_placement=False,
              intra_op_parallelism_threads=num_cores,
              inter_op_parallelism_threads=num_cores,
              allow_soft_placement=True,
              device_count = {'CPU' : num_CPU,
                              'GPU' : num_GPU}
              )
  config.gpu_options.allow_growth=True
  session = tf.Session(config=config)
  K.set_session(session)

def verifyDevices(device="gpu"):
  clearSession()
  if device == "gpu":
    num_GPU = 1
    num_CPU = 1
    print("Verifiying GPU device ...")
    devices = device_lib.list_local_devices()
    #print(devices)
    if not 'GPU' in str(devices) or not tf.test.is_gpu_available():
      print("Dont have GPU device or you cant use your GPU for missing libs")
      print("USING CPU")
      num_CPU = 1
      num_GPU = 0
    else:
      print("USING GPU")
  else:
    num_CPU = 1
    num_GPU = 0
    print("USING CPU")

  setDeviceConfig(num_CPU, num_GPU)

  print("Done")

def getInputShape(img_dims=[224, 224]):
  img_width, img_height = img_dims[0], img_dims[1]
  if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
  else:
    input_shape = (img_width, img_height, 3)
  return input_shape

def deprocess_image(x):
  """Same normalization as in:
  https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py
  """
  x = x.copy()
  if numpy.ndim(x) > 3:
    x = numpy.squeeze(x)
  # normalize tensor: center on 0., ensure std is 0.1
  x -= x.mean()
  x /= (x.std() + 1e-5)
  x *= 0.1

  # clip to [0, 1]
  x += 0.5
  x = numpy.clip(x, 0, 1)

  # convert to RGB array
  x *= 255
  #if K.image_dim_ordering() == 'th':
  #  x = x.transpose((1, 2, 0))

  if K.image_data_format() == 'channels_first':
    x = x.transpose((1, 2, 0))
  x = numpy.clip(x, 0, 255).astype('uint8')
  return x


import os
import subprocess

os.chdir('../data/css10')

with open('zhtranscript.txt', 'w', encoding='utf-8') as f:
    os.chdir('chinese/data_thchs30/data')
    files = str(subprocess.check_output(['ls']))
    files = files[2:-3].split('\\n')
    for file in files:
        if file.split('.')[-1] == 'trn':
            with open(file, 'r', encoding='utf-8') as rf:
                for line in rf:
                    print((file.split('.')[0] + '|' + line).rstrip(), file=f)
                    break
            subprocess.call(['rm', file])


class Frenet:

    def __init__(self):
        self.t = []
        self.d = []
        self.dot_d = []
        self.ddot_d = []
        self.dddot_d = []
        self.s = []
        self.dot_s =[]
        self.ddot_s = []
        self.dddot_s = []
        self.cd = 0.0
        self.cv = 0.0
        self.ctot = 0.0
        self.ct = 0.0
        self.x = []
        self.y = []


import threading
import time

# python에는 GIL이 있기 때문에
# multithreading을 해도
# 시간상의 성능 향상은 기대하기 어렵다

# 공유 자원
# 전역변수는 대표적인 공유 자원(shared resource)
# 데이터 영역이나 힙 영역에 있다면
# 모든 스레드에서 접근 및 수정이 가능
li = [i+1 for i in range(100000000)]
n = 100000000
offset = n // 4 # 250

# start = time.time()
# for i in range(1000):
#     li[i] *= 2

# elapsed = time.time()-start

# print("elapsed : {}".format(elapsed))

def thread_main(li, n):
    for idx in range(offset * n, offset*(n+1)):
        li[idx]*=2

# 스레드를 담아둘 리스트
threads=[]

for i in range(4):
    th = threading.Thread(target = thread_main, args=(li, i))
    threads.append(th)

# 스레드가 1개 --> 5개 (main 포함)
start = time.time()
for th in threads:
    th.start()

# 모든 스레드의 실행이 끝날 때까지 기다림
for th in threads:
    th.join()

elapsed = time.time()-start
print("elapsed : {}".format(elapsed))

#print(li)


# Author: Arthur Jinyue Guo (jg5505)
import argparse
import builtins
import os
import random
import shutil
import time
import warnings

import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.models as models
import torch.nn.functional as F

# from model.resnet50 import Resnet50
from dataloader import CustomDataset
from model.subbatchnorm import SubBatchNorm2d

parser = argparse.ArgumentParser(description='DL09 MoCo Training')
parser.add_argument('data', metavar='DIR',
                    help='path to dataset')
parser.add_argument('-a', '--arch', metavar='ARCH', default='resnet50')
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
                    help='number of data loading workers (default: 4)')
parser.add_argument('--epochs', default=100, type=int, metavar='N',
                    help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
                    help='manual epoch number (useful on restarts)')
parser.add_argument('-b', '--batch-size', default=128, type=int,
                    metavar='N',
                    help='mini-batch size (default: 128), this is the total '
                         'batch size of all GPUs on the current node when '
                         'using Data Parallel or Distributed Data Parallel')
parser.add_argument('--lr', '--learning-rate', default=30., type=float,
                    metavar='LR', help='initial learning rate', dest='lr')
parser.add_argument('--schedule', default=[60, 80, 120, 160], nargs='*', type=int,
                    help='learning rate schedule (when to drop lr by a ratio)')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
                    help='momentum')
parser.add_argument('--wd', '--weight-decay', default=0., type=float,
                    metavar='W', help='weight decay (default: 0.)',
                    dest='weight_decay')
parser.add_argument('-p', '--print-freq', default=10, type=int,
                    metavar='N', help='print frequency (default: 10)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
                    help='path to latest checkpoint (default: none)')
parser.add_argument('--seed', default=None, type=int,
                    help='seed for initializing training. ')
parser.add_argument('--gpu', default=0, type=int,
                    help='GPU id to use.')
parser.add_argument('--pretrained', default='', type=str,
                    help='path to moco pretrained checkpoint')
parser.add_argument('--eval-per-n-epoch', default=5, type=int,
                    help='evaluate the training result per n epoch (default: 5)')
parser.add_argument('--checkpoint-dir', default='../checkpoints', type=str, metavar='DIR',
                    help='dir to save checkpoints')

best_acc1 = 0

def main():
    args = parser.parse_args()

    if args.seed is not None:
        random.seed(args.seed)
        torch.manual_seed(args.seed)
        cudnn.deterministic = True
        warnings.warn('You have chosen to seed training. '
                      'This will turn on the CUDNN deterministic setting, '
                      'which can slow down your training considerably! '
                      'You may see unexpected behavior when restarting '
                      'from checkpoints.')

    if args.gpu is not None:
        warnings.warn('You have chosen a specific GPU. This will completely '
                      'disable data parallelism.')

    main_worker(args.gpu, args)

def main_worker(gpu, args):
    global best_acc1
    args.gpu = gpu

    if args.gpu is not None:
        print("Use GPU: {} for training".format(args.gpu))

    # create model
    print("=> creating model '{}'".format(args.arch))
    model = models.__dict__[args.arch](num_classes=800, norm_layer=SubBatchNorm2d)
    # model = models.__dict__[args.arch](num_classes=800)

    # # freeze all layers but the last fc
    for name, param in model.named_parameters():
        if name not in ['fc.weight', 'fc.bias']:
            param.requires_grad = False
    # init the fc layer
    model.fc.weight.data.normal_(mean=0.0, std=0.1)
    model.fc.bias.data.zero_()

    # load from pre-trained, before DistributedDataParallel constructor
    if args.pretrained:
        if os.path.isfile(args.pretrained):
            print("=> loading checkpoint '{}'".format(args.pretrained))
            checkpoint = torch.load(args.pretrained, map_location="cpu")

            # rename moco pre-trained keys
            state_dict = checkpoint['state_dict']
            for k in list(state_dict.keys()):
                # retain only encoder_q up to before the embedding layer
                if k.startswith('encoder_q') and not k.startswith('encoder_q.fc'):
                    # remove prefix
                    state_dict[k[len("encoder_q."):]] = state_dict[k]
                # delete renamed or unused k
                del state_dict[k]

            args.start_epoch = 0
            msg = model.load_state_dict(state_dict, strict=False)
            # print(f"=> loading state_dict: \n{list(state_dict.keys())}")
            # print(f"=> missing state keys: \n{msg.missing_keys}")
            assert set(msg.missing_keys) == {"fc.weight", "fc.bias"}

            print("=> loaded pre-trained model '{}'".format(args.pretrained))
        else:
            raise ValueError("=> no pre-trained model found at '{}'".format(args.pretrained))

    if args.gpu is not None:
        torch.cuda.set_device(args.gpu)
        model = model.cuda(args.gpu)
    else:
        print("=> ERROR: gpu must be assigned")

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda(args.gpu)

    # optimize only the linear classifier
    parameters = list(filter(lambda p: p.requires_grad, model.parameters()))
    assert len(parameters) == 2  # fc.weight, fc.bias
    optimizer = torch.optim.SGD(parameters, args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)
    # optimizer = torch.optim.SGD(model.parameters(), args.lr,
    #                             momentum=args.momentum,
    #                             weight_decay=args.weight_decay)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            if args.gpu is None:
                checkpoint = torch.load(args.resume)
            else:
                # Map model to be loaded to specified single gpu.
                loc = 'cuda:{}'.format(args.gpu)
                checkpoint = torch.load(args.resume, map_location=loc)
            args.start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            if args.gpu is not None:
                # best_acc1 may be from a checkpoint from a different GPU
                best_acc1 = best_acc1.to(args.gpu)
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
            del checkpoint
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    # Data loading code
    traindir = args.data
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    transform_train = transforms.Compose([
            transforms.RandomResizedCrop(96),  # add crop resize
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize])
    transform_eval = transforms.Compose([
            transforms.Resize(128),  # add resize
            transforms.CenterCrop(96),  # add crop
            transforms.ToTensor(),
            normalize
            ])

    train_dataset = CustomDataset(traindir, 'train', transform_train)
    eval_dataset = CustomDataset(traindir, 'val', transform_eval)

    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True)
    eval_loader = torch.utils.data.DataLoader(
        eval_dataset, batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True)

    # training code
    for epoch in range(args.start_epoch, args.epochs):

        adjust_learning_rate(optimizer, epoch, args)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch, args)

        if epoch == 0 or (epoch + 1) % args.eval_per_n_epoch == 0:
            accuracy = evaluate(eval_loader, model, args)
            print(f"=> Epoch: {epoch+1}, accuracy: {accuracy:.4f}")
            # remember best acc and save checkpoint
            is_best = accuracy > best_acc1
            best_acc1 = max(accuracy, best_acc1)
            print(f"=> Epoch: {epoch+1}, isBest? : {is_best}")
            save_checkpoint({
                'epoch': epoch + 1,
                'arch': args.arch,
                'accuracy': accuracy,
                'state_dict': model.state_dict(),
                'optimizer': optimizer.state_dict(),
            }, is_best, save_dir=args.checkpoint_dir, epoch=(epoch+1), filename=os.path.join(args.checkpoint_dir, 'checkpoint_{:03d}.pth.tar'.format(epoch+1)))

        # if epoch == args.start_epoch:
        #     sanity_check(model.state_dict(), args.pretrained)

def train(train_loader, model, criterion, optimizer, epoch, args):
    batch_time = AverageMeter('Time', ':6.3f')
    data_time = AverageMeter('Data', ':6.3f')
    losses = AverageMeter('Loss', ':.4e')
    progress = ProgressMeter(
        len(train_loader),
        [batch_time, data_time, losses],
        prefix="Epoch: [{}]".format(epoch+1))

    """
    Switch to eval mode:
    Under the protocol of linear classification on frozen features/models,
    it is not legitimate to change any part of the pre-trained model.
    BatchNorm in train mode may revise running mean/std (even if it receives
    no gradient), which are part of the model parameters too.
    """
    model.eval()

    end = time.time()
    for i, (images, target) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        images = images.cuda(args.gpu, non_blocking=True)
        target = target.cuda(args.gpu, non_blocking=True)

        # compute output
        output = model(images)
        loss = criterion(output, target)

        # update loss record
        losses.update(loss.item(), images.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            progress.display(i)

def adjust_learning_rate(optimizer, epoch, args):
    """Decay the learning rate based on schedule"""
    lr = args.lr
    for milestone in args.schedule:
        lr *= 0.1 if epoch >= milestone else 1.
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

def evaluate(eval_loader, model, args):
    model.eval()

    correct = 0
    total = 0
    with torch.no_grad():
        for data in eval_loader:
            images, labels = data
            images = images.cuda(args.gpu, non_blocking=True)
            labels = labels.cuda(args.gpu, non_blocking=True)

            output = model(images)
            _, predicted = torch.max(output.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

    return (100 * correct  / total)

def save_checkpoint(state, is_best, save_dir, epoch, filename='checkpoint.pth.tar'):
    torch.save(state, filename)
    best_checkpoint = os.path.join(
        save_dir, 'model_best_{:03d}_{:.2f}.pth'.format(epoch, best_acc1))
    if is_best:
        sub_state = state['state_dict']
        torch.save(sub_state, os.path.join(save_dir, 'model_sub.pth'))
        shutil.copyfile(filename, best_checkpoint)

def sanity_check(state_dict, pretrained_weights):
    """
    Linear classifier should not change any weights other than the linear layer.
    This sanity check asserts nothing wrong happens (e.g., BN stats updated).
    """
    print("=> loading '{}' for sanity check".format(pretrained_weights))
    checkpoint = torch.load(pretrained_weights, map_location="cpu")
    state_dict_pre = checkpoint['state_dict']

    for k in list(state_dict.keys()):
        # only ignore fc layer
        if 'fc.weight' in k or 'fc.bias' in k:
            continue

        # name in pretrained model
        k_pre = 'encoder_q.' + k

        assert ((state_dict[k].cpu() == state_dict_pre[k_pre]).all()), \
            '{} is changed in linear classifier training.'.format(k)

    print("=> sanity check passed.")

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self, name, fmt=':f'):
        self.name = name
        self.fmt = fmt
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

    def __str__(self):
        fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
        return fmtstr.format(**self.__dict__)

class ProgressMeter(object):
    def __init__(self, num_batches, meters, prefix=""):
        self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
        self.meters = meters
        self.prefix = prefix

    def display(self, batch):
        entries = [self.prefix + self.batch_fmtstr.format(batch)]
        entries += [str(meter) for meter in self.meters]
        print('\t'.join(entries))

    def _get_batch_fmtstr(self, num_batches):
        num_digits = len(str(num_batches // 1))
        fmt = '{:' + str(num_digits) + 'd}'
        return '[' + fmt + '/' + fmt.format(num_batches) + ']'

if __name__ == '__main__':
    main()


def calc(equation):
    x = 0
    y = ''
    if equation[0].isdigit():
        equation = '+' + equation
    for num in equation[::-1]:
        if num == '+':
            x += int(y)
            y = ''
        elif num == '-':
            x -= int(y)
            y = ''
        else:
            y = num + y
    return x

print(calc('123+2-124'))
print(calc('-12+12-7979+9191'))
print(calc('+1-1+1-1+1-1'))


from datetime import datetime
from config import *

class Criterion(object):
    def __init__(self, item_type):
        pass

    def test(self, item):
        raise NotImplementedError

class Staleness(Criterion):
    def __init__(self, item_type):
        self._now = datetime.now()

        if item_type == "doc":
            self._threshold = DOC_STALENESS_THRESHOLD
        elif item_type == "pr":
            self._threshold = PR_STALENESS_THRESHOLD

    def test(self, item):
        # comment_times = [c.time for c in item.comments]
        # comment_times.sort(reverse=True)

        # if not comment_times:
        #     comment_delta = (self._now - item.last_mentioned)
        #     commented = False
        # else:
        #     comment_delta = (comment_times[0] - item.last_mentioned)
        #     commented = True

        # if comment_delta > self._threshold:

        if (not item.comments) and (self._now - item.last_mentioned > self._threshold):
            item.tests[STALENESS_CRITERION] = {
                "type": STALENESS_CRITERION,
                # "delta": comment_delta,
                # "commented": commented
            }

        return

class Hype(Criterion):
    def test(self, item):
        unique_commenters = set([c.owner for c in item.comments])

        if len(unique_commenters) >= HYPE_THRESHOLD:
            item.tests[HYPE_CRITERION] = {
                "type": HYPE_CRITERION,
                "unique_commenters": unique_commenters
            }

class BotMention(Criterion):
    def test(self, item):
        if item.last_mentioned_bot:
            item.tests[BOT_MENTION_CRITERION] = {"type": BOT_MENTION_CRITERION,
                                                 "last_mentioned_bot": item.last_mentioned_bot}

class HotComment(Criterion):
    def test(self, item):

        if item.comments:
            sorted_comments = sorted(item.comments, key=lambda i: i.replies_count, reverse=True)

            if sorted_comments[0].replies_count >= HOT_COMMENT_THRESHOLD:
                item.tests[HOT_COMMENT_CRITERION] = {"type": HOT_COMMENT_CRITERION, "comment": sorted_comments[0]}


Import('env')
Import('parallel_run')
Import('serial_run')
Import('ip_charm')
Import('bin_path')
Import('test_path')
import os
#----------------------------------------------------------------------
# defines
#----------------------------------------------------------------------

env['CPIN'] = 'touch parameters.out; mv parameters.out ${TARGET}.in'
env['RMIN'] = 'rm -f parameters.out'
env['clocal_cmd'] = '++local'

date_cmd = 'echo $TARGET > test/STATUS; echo "-------------------"; date +"%Y-%m-%d %H:%M:%S";'

run_balance_none = Builder(action = "$RMIN; " + date_cmd + parallel_run + "  $SOURCE  $clocal_cmd $ARGS  > $TARGET 2>&1; $CPIN; $COPY")
env.Append(BUILDERS = { 'RunBalanceNone' : run_balance_none } )
env_mv_none  = env.Clone(COPY = 'mkdir -p ' + test_path + '/Balance/None; mv `ls *.png *.h5` ' + test_path + '/Balance/None')

run_balance_greedy = Builder(action = "$RMIN; " + date_cmd + parallel_run + " $SOURCE  $clocal_cmd $ARGS +balancer GreedyLB > $TARGET 2>&1; $CPIN; $COPY")
env.Append(BUILDERS = { 'RunBalanceGreedy' : run_balance_greedy } )
env_mv_greedy  = env.Clone(COPY = 'mkdir -p ' + test_path + '/Balance/Greedy; mv `ls *.png *.h5` ' + test_path + '/Balance/Greedy')

#run_balance_hybrid = Builder(action = "$RMIN; " + date_cmd + parallel_run + " $SOURCE  $clocal_cmd $ARGS +balancer HybridLB > $TARGET 2>&1; $CPIN; $COPY")
#env.Append(BUILDERS = { 'RunBalanceHybrid' : run_balance_hybrid } )
#env_mv_hybrid  = env.Clone(COPY = 'mkdir -p ' + test_path + '/Balance/Hybrid; mv `ls *.png *.h5` ' + test_path + '/Balance/Hybrid')

run_balance_rand_cent = Builder(action = "$RMIN; " + date_cmd + parallel_run + " $SOURCE  $clocal_cmd $ARGS +balancer RandCentLB > $TARGET 2>&1; $CPIN; $COPY")
env.Append(BUILDERS = { 'RunBalanceRandCent' : run_balance_rand_cent } )
env_mv_rand_cent  = env.Clone(COPY = 'mkdir -p ' + test_path + '/Balance/RandCent; mv `ls *.png *.h5` ' + test_path + '/Balance/RandCent')

run_balance_refine = Builder(action = "$RMIN; " + date_cmd + parallel_run + " $SOURCE  $clocal_cmd $ARGS +balancer RefineLB > $TARGET 2>&1; $CPIN; $COPY")
env.Append(BUILDERS = { 'RunBalanceRefine' : run_balance_refine } )
env_mv_refine  = env.Clone(COPY = 'mkdir -p ' + test_path + '/Balance/Refine; mv `ls *.png *.h5` ' + test_path + '/Balance/Refine')

run_balance_rotate = Builder(action = "$RMIN; " + date_cmd + parallel_run + " $SOURCE  $clocal_cmd $ARGS +balancer RotateLB > $TARGET 2>&1; $CPIN; $COPY")
env.Append(BUILDERS = { 'RunBalanceRotate' : run_balance_rotate } )
env_mv_rotate  = env.Clone(COPY = 'mkdir -p ' + test_path + '/Balance/Rotate; mv `ls *.png *.h5` ' + test_path + '/Balance/Rotate')

#----------------------------------------------------------------------
# Load Balancing
#----------------------------------------------------------------------

# NoLB

balance_none = env_mv_none.RunBalanceNone (
   'test_balance_none.unit',
   bin_path + '/enzo-e',
   ARGS='input/Balance/load-balance-4.in')

Clean(balance_none,
      [Glob('#/' + test_path + '/Balance/balance*png'),
       '#/input/parameters.out',])

# GreedyLB

balance_greedy = env_mv_greedy.RunBalanceGreedy (
   'test_balance_greedy.unit',
   bin_path + '/enzo-e',
   ARGS='input/Balance/load-balance-4.in')

Clean(balance_greedy,
      [Glob('#/' + test_path + '/Balance/balance*png'),
       '#/input/parameters.out',])

# HybridLB

# balance_hybrid = env_mv_hybrid.RunBalanceHybrid (
#    'test_balance_hybrid.unit',
#    bin_path + '/enzo-e',
#    ARGS='input/Balance/load-balance-4.in')

# Clean(balance_hybrid,
#       [Glob('#/' + test_path + '/Balance/balance*png'),
#        '#/input/parameters.out',])

# NeighborLB

# DOES NOTHING?
# balance_neighbor = env_mv_neighbor.RunBalanceNeighbor (
#    'test_balance_neighbor.unit',
#    bin_path + '/enzo-e',
#    ARGS='input/Balance/load-balance-4.in')
#
# Clean(balance_neighbor,
#       [Glob('#/' + test_path + '/Balance/balance*png'),
#        '#/input/parameters.out',])

# RandCentLB

balance_rand_cent = env_mv_rand_cent.RunBalanceRandCent (
   'test_balance_rand_cent.unit',
   bin_path + '/enzo-e',
   ARGS='input/Balance/load-balance-4.in')

Clean(balance_rand_cent,
      [Glob('#/' + test_path + '/Balance/balance*png'),
       '#/input/parameters.out',])

# RefineCommLB

# balance_refine_comm = env_mv_refine_comm.RunBalanceRefineComm (
#    'test_balance_refine_comm.unit',
#    bin_path + '/enzo-e',
#    ARGS='input/Balance/load-balance-4.in')

# Clean(balance_refine_comm,
#       [Glob('#/' + test_path + '/Balance/balance*png'),
#        '#/input/parameters.out',])

# RefineLB

balance_refine = env_mv_refine.RunBalanceRefine (
   'test_balance_refine.unit',
   bin_path + '/enzo-e',
   ARGS='input/Balance/load-balance-4.in')

Clean(balance_refine,
      [Glob('#/' + test_path + '/Balance/balance*png'),
       '#/input/parameters.out',])

# RotateLB

balance_rotate = env_mv_rotate.RunBalanceRotate (
   'test_balance_rotate.unit',
   bin_path + '/enzo-e',
   ARGS='input/Balance/load-balance-4.in')

Clean(balance_rotate,
      [Glob('#/' + test_path + '/Balance/balance*png'),
       '#/input/parameters.out',])


import QuadraticEquation

abc = QuadraticEquation.czytajWspolczynniki()
if abc[0] > 0:
    delta = QuadraticEquation.obliczDelte(abc)
    print("Delta = {}".format(int(delta)))
    if delta < 0:
        print("Delta ujemna, brak pierwiastków.")
    else:
        wynik = QuadraticEquation.obliczPierwiastki(abc,delta)

        abc = [round(x) for x in abc]
        QuadraticEquation.wyswietlPierwiastki(abc,wynik)

        if len(wynik) == 1:
            print(f"Pierwiastki równania: x1 = {wynik[0]}")
        else:
            print(f"Pierwiastki równania: x1 = {wynik[0]}, x2 = {wynik[1]}")


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

# subscribe
# 카메라 링크의 위치 (camera optical frame)
# 3d boundig box의 정보

# publish
# end point 가 이동할 object의 위치 좌표

# parameter
# 파라미터로 몇층인지 받아오기

# 일단 1층을 타겟으로 설정
import math
from math import sin, cos, pi

import rospy
import tf
from std_msgs.msg import Header
from geometry_msgs.msg import Point, Pose, Quaternion, Twist, Vector3
# from gb_detection_3d_msgs.msg import BoundingBoxes3d
# from gb_detection_3d_msgs.msg import BoundingBox3d
from gb_visual_detection_3d_msgs.msg import BoundingBoxes3d
from gb_visual_detection_3d_msgs.msg import BoundingBox3d

target_point = Vector3()
target_point.x = 0.0
target_point.y = 0.0
target_point.z = 0.0
target_button = -1

def init_node():
    rospy.init_node('button_point_publisher')
    bounding_boxes_3d_sub = rospy.Subscriber('/darknet_ros_3d/bounding_boxes', BoundingBoxes3d, cb_bounding_boxes_3d)

    rospy.set_param('button',-1) # 아무것도 계산하지 않음
    # rospy.set_param('button',12) # 테스트
    rate = rospy.Rate(1.0)

    while not rospy.is_shutdown():
        rate.sleep

def cb_bounding_boxes_3d(data):
    global target_button
    global target_point

    button_point_pub = rospy.Publisher("/button_target_point", Vector3, queue_size=20)
    box_data = data
    # print (bounding_boxes_3d)
    boxes_count = len(box_data.bounding_boxes)
    # print (boxes_count)

    for count in range (boxes_count):
        temp_class = box_data.bounding_boxes[count].Class
        # print(temp_class)
        selected_button = rospy.get_param('button')
        if (selected_button == "open" and temp_class == "open"):
            target_button = count
            break
        elif (selected_button == "close" and temp_class == "close"):
            target_button = count
            break
        elif (selected_button == "b1F" and temp_class == "b1"):
            target_button = count
            break
        elif (selected_button == "1f" and temp_class == "1f"):
            target_button = count
            break
        elif (selected_button == "2f" and temp_class == "2f"):
            target_button = count
            break
        elif (selected_button ==  "3f" and temp_class == "3f"):
            target_button = count
            break
        elif (selected_button ==  "4f" and temp_class == "4f"):
            target_button = count
            break
        elif (selected_button ==  "5f" and temp_class == "5f"):
            target_button = count
            break
        elif (selected_button ==  "6f" and temp_class == "6f"):
            target_button = count
            break
        elif (selected_button ==  "up" and temp_class == "up"):
            target_button = count
            break
        elif (selected_button ==  "down" and temp_class == "down"):
            target_button = count
            break
        else:
            target_button = -1

    if((target_button != -1) and (len(box_data.bounding_boxes) >= target_button)):

        target_x_min = box_data.bounding_boxes[target_button].xmin
        target_x_max = box_data.bounding_boxes[target_button].xmax
        target_y_min = box_data.bounding_boxes[target_button].ymin
        target_y_max = box_data.bounding_boxes[target_button].ymax
        target_z_min = box_data.bounding_boxes[target_button].zmin
        target_z_max = box_data.bounding_boxes[target_button].zmax

        float(target_x_min)
        float(target_x_max)
        float(target_y_min)
        float(target_y_max)
        float(target_z_min)
        float(target_z_max)
        target_center_x = (target_x_min+target_x_max)/2
        target_center_y = (target_y_min+target_y_max)/2
        target_center_z = (target_z_min+target_z_max)/2

        target_point.x = target_center_x
        target_point.y = target_center_y
        target_point.z = target_center_z
        # print(target_point.x,target_point.y,target_point.z)
        button_point_pub.publish(target_point)

    else:
        # can't see target button
        target_point.x = 0
        target_point.y = 0
        target_point.z = 0
        button_point_pub.publish(target_point)
        pass

if __name__=="__main__":
    try:
        # print("point init")

        target_button = -1
        init_node()

    except rospy.ROSInterruptException:
        pass

# 0 OPEN
# 1 OPEN_On
# 2 CLOSE
# 3 CLOSE_On
# 4 B1F
# 5 B1F_On
# 6 1F
# 7 1F_On
# 8 2F
# 9 2F_On
# 10 3F
# 11 3F_On
# 12 4F
# 13 4F_On
# 14 5F
# 15 5F_On
# 16 6F
# 17 6F_On
# 18 UP
# 19 UP_On
# 20 DOWN
# 21 DOWN_On


#lists

# name = 'string'
# level = 5
# awesomeness = 9.5
# is_raining = True

# backpack = []

# backpack = [
#     'drink bottle',
#     'yogo',
#     'laptop',
#     'headphones',]

# print (backpack)

# backpack.append('apple')

# print (backpack)

# print (backpack[1])
# (backpack[0]) = 'lunchbox'
# print (backpack)

# name = 'hayley'
# print (name[0])

# for index, item in enumerate (backpack):
#     print(index, item)

# counter = 0
# for item in backpack:
#     print(counter,item)
#     counter = counter + 1

# counter = 0
# while counter < len(backpack):
#     print(counter, backpack[counter])
#     counter = counter + 1

# for 1-10 and while 1-10 range?

num = [1,2,3,4,5,6,7,8,9,10]

counter = 0
for count in num:
    print (num)
    counter = counter + 1

print ('for loop')
for num in range (1,11):
    print (num)

num = 1
while num <= 10:
    print(num)
    num += 1

#Tuples

person = ('hayley','awesome')
print (person)
person[0] = 'lj'

def cats ():
    return ('boris','larry', name)

cat1, cat2, name = cats()