# 다음 랭킹 뉴스 크롤러
# 많이 본 / 열독률 높은 / 댓글 많은 / 연령별 랭킹 뉴스

import re
import time

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from fake_useragent import UserAgent
import json
import os
import tqdm

# 많이 본 / 열독률 높은 / 댓글 많은 뉴스 순서
def crawl_content(soup, driver, category, ranking_box_class, num):
    ranking_box = soup.find_all(class_=ranking_box_class)
    contents_box = soup.find_all(class_="cont_thumb")
    lst = []

    # 기사 외부 정보 가져오기
    for num in range(num):
        d = dict()
        d['datetime'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))  # 크롤링한 날짜
        d['rank'] = int(ranking_box[num].find(class_="screen_out").get_text())  # 순위
        d['category'] = category

        news_info = contents_box[num].find(class_="tit_thumb")
        d['url'] = news_info.find('a')['href']  # url
        d['title'] = news_info.find('a').get_text()  # 제목
        d['press'] = news_info.find(class_='info_news').get_text()  # 언론사

        lst.append(d)

    # 기사 내부 정보 가져오기
    for link in lst:
        resp = requests.get(link['url'])
        soup = BeautifulSoup(resp.text, "lxml")
        info = soup.find(class_='info_view')
        link['date'] = info.find(class_='num_date').get_text()  # 입력날짜

        driver.get(link['url'])
        driver.implicitly_wait(3)
        print("내부")
        try:
            link['n_comment'] = int(driver.find_element_by_css_selector('#alex-header > em').text)  # 댓글수
        except:
            link['n_comment'] = -1

        foot = driver.find_element_by_css_selector(
            '#mArticle > div.foot_view > div.emotion_wrap > div.emotion_list > div > div > div')
        link['n_reaction_recommend'] = int(foot.find_element_by_css_selector(
            'div.selectionbox.type-RECOMMEND.unselected > span.count').text)  # 추천해요 수
        link['n_reaction_like'] = int(foot.find_element_by_css_selector(
            'div.selectionbox.type-LIKE.unselected > span.count').text)  # 좋아요 수
        link['n_reaction_impress'] = int(foot.find_element_by_css_selector(
            'div.selectionbox.type-IMPRESS.unselected > span.count').text)  # 감동이에요 수
        link['n_reaction_angry'] = int(foot.find_element_by_css_selector(
            'div.selectionbox.type-ANGRY.unselected > span.count').text)  # 화나요 수
        link['n_reaction_sad'] = int(foot.find_element_by_css_selector(
            'div.selectionbox.type-SAD.unselected > span.count').text)  # 슬퍼요 수
        # 전체 반응 수
        link['n_reactions'] = str(link['n_reaction_recommend'] + link['n_reaction_like'] \
                              + link['n_reaction_impress'] + link['n_reaction_angry'] \
                              + link['n_reaction_sad'])
        print(link)
    return lst

# 연령별 뉴스
def crawl_content_by_age(soup, driver, category):
    female = soup.find_all(class_='rank_female')
    male = soup.find_all(class_='rank_male')
    ranking_news = [female, male]
    lst = []

    for news in ranking_news:
        for i in range(4):  # {20대, 30대, 40대, 50대}
            l = []
            age = news[i].find(class_='txt_news').get_text()[:3]  # ex) "20대 여성"에서 앞에 두 글자
            sex = news[i].find(class_='txt_news').get_text()[4:]  # ex) "20대 여성"에서 마지막 두 글자
            news_list = news[i].find_all(class_='link_txt')
            press_list = news[i].find_all(class_='info_news')

            # 기사 외부 정보 가져오기
            # 한 섹션에 5위까지 나와있다.
            for num in range(5):
                d = dict()
                d['datetime'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))  # 크롤링한 날짜
                d['category'] = category  # 카테고리
                d['age'] = age  # 연령대
                d['sex'] = sex  # 성별
                d['rank'] = num + 1  # 순위
                d['url'] = news_list[num]['href']  # url
                d['title'] = news_list[num].get_text()  # 제목
                d['press'] = press_list[num].get_text()  # 언론사
                l.append(d)

            # 기사 내부 정보 가져오기
            for link in l:
                resp = requests.get(link['url'])
                time.sleep(1)
                soup = BeautifulSoup(resp.text, "lxml")
                info = soup.find(class_='info_view')
                link['date'] = info.find(class_='num_date').get_text()  # 입력날짜

                driver.get(link['url'])
                driver.implicitly_wait(3)
                print("내부")
                try:
                    link['n_comment'] = int(driver.find_element_by_css_selector('#alex-header > em').text)  # 댓글수
                except:
                    link['n_comment'] = -1

                foot = driver.find_element_by_css_selector(
                    '#mArticle > div.foot_view > div.emotion_wrap > div.emotion_list > div > div > div')
                link['n_reaction_recommend'] = int(foot.find_element_by_css_selector(
                    'div.selectionbox.type-RECOMMEND.unselected > span.count').text)  # 추천해요 수
                link['n_reaction_like'] = int(foot.find_element_by_css_selector(
                    'div.selectionbox.type-LIKE.unselected > span.count').text)  # 좋아요 수
                link['n_reaction_impress'] = int(foot.find_element_by_css_selector(
                    'div.selectionbox.type-IMPRESS.unselected > span.count').text)  # 감동이에요 수
                link['n_reaction_angry'] = int(foot.find_element_by_css_selector(
                    'div.selectionbox.type-ANGRY.unselected > span.count').text)  # 화나요 수
                link['n_reaction_sad'] = int(foot.find_element_by_css_selector(
                    'div.selectionbox.type-SAD.unselected > span.count').text)  # 슬퍼요 수
                # 전체 반응 수
                link['n_reactions'] = str(link['n_reaction_recommend'] + link['n_reaction_like'] \
                                      + link['n_reaction_impress'] + link['n_reaction_angry'] \
                                      + link['n_reaction_sad'])
        lst.append(l)
        print(l)
    return lst

def crawling(chrome_driver_path: str):
    # init chrome driver
    chrome_driver = chrome_driver_path
    chrome_options = webdriver.ChromeOptions()

    chrome_options.add_argument('headless')
    userAgent = UserAgent().random
    chrome_options.add_argument(f"user-agent={userAgent}")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')

    prefs = {
        "profile.default_content_setting_values": {
            "cookies": 2,
            "images": 2,
            "plugins": 2,
            "popups": 2,
            "geolocation": 2,
            "notifications": 2,
            "auto_select_certificate": 2,
            "fullscreen": 2,
            "mouselock": 2,
            "mixed_script": 2,
            "media_stream": 2,
            "media_stream_mic": 2,
            "media_stream_camera": 2,
            "protocol_handlers": 2,
            "ppapi_broker": 2,
            "automatic_downloads": 2,
            "midi_sysex": 2,
            "push_messaging": 2,
            "ssl_cert_decisions": 2,
            "metro_switch_to_desktop": 2,
            "protected_media_identifier": 2,
            "app_banner": 2,
            "site_engagement": 2,
            "durable_storage": 2,
        }
    }
    chrome_options.add_experimental_option("prefs", prefs)
    driver = webdriver.Chrome(chrome_driver, options=chrome_options)

    date = time.strftime('%Y%m%d', time.localtime(time.time()))
    tags = ["popular/news", "popular/entertain", "popular/sports", "kkomkkom/news",
            "kkomkkom/entertain", "kkomkkom/sports", "bestreply/", "age/"]
    news_list = []
    print(date)
    # for each catogory
    for tag in tags:
        print(tag)
    # for i in tqdm(range(len(tags))):
       # tag = tags[i]
        url = "https://news.daum.net/ranking/" + tag + "?regDate=" + str(date)
        resp = requests.get(url)
        soup = BeautifulSoup(resp.text, "lxml")

        main_tag, category_tag = tag.split('/')
        if main_tag == "popular":
            news_list.extend(crawl_content(soup, driver, tag, 'rank_num rank_popular', 50))
        elif main_tag == "kkomkkom":
            news_list.extend(crawl_content(soup, driver, tag, 'rank_num rank_popular', 30))
        elif main_tag == "bestreply":
            news_list.extend(crawl_content(soup, driver, main_tag, 'rank_num', 50))
        elif main_tag == "age":
            news_list.extend(crawl_content_by_age(soup, driver, main_tag))
    return news_list

if __name__ == "__main__":
    chrome_driver_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "chromedriver")
    start = time.perf_counter()
    data = crawling(chrome_driver_path)
    end = time.perf_counter()
    print('time elapsed: ', end-start)
    print('data size: ', len(data))
    title = "Daum_ranking_news_" + str(time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))) + ".json"
    with open(title, 'w', encoding='utf-8') as make_file:
        json.dump(data, make_file, indent="\t", ensure_ascii=False)


# -*- coding: utf-8 -*-
"""
Created on Thu Feb  1 16:05:49 2018

@author: peter
"""
import logging
import time
import unittest

from CESAPI.command import *
from CESAPI.connection import Connection
import CESAPI.packet
from CESAPI.refract import *
from CESAPI.test import LTSimulator

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# Base class for most of the other test cases. Used to setup and teardown
# the LTSimulator instance that the test cases use.
class RefractTestCase(unittest.TestCase):
    def __init(self):
        self.sim = None

    def setUp(self):
        self.sim = LTSimulator()
        self.sim.start()

    def tearDown(self):
        self.sim.stop()
        self.sim.join()

# Send an InitializeCT packet and verify that an InitializeRT packet was returned.
class SetRefractionIndexTestCase(RefractTestCase):
    def runTest(self):
        success = False
        connection = Connection()
        initrt = None
        try:
            connection.connect(host='localhost')
            command = CommandSync(connection)

            algorithm = AlgorithmFactory().refractionIndexAlgorithm(RI_ALG_Leica)
            SetRefractionIndex(command, algorithm)
            success = True
        except Exception as e:
            logger.info(e)
        finally:
            if connection != None:
                connection.disconnect()

        self.assertTrue(success)

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


from constants import *
from preprocessing_module import *
from deep_learnning_module import *
from Managers import *
from Client import *
from server import *

##########################################################################
def preprocessing_init():
    frames = load_data()
    print("------>  load_data accomplished !!  ")
    scaler, frames = fit_transform(frames)
    split_by_vehcile_id(frames)
    print("------>  split_by_vehcile_id accomplished !!  ")
    train_test_split()
    print("------>  train_test_split accomplished !!  \n")

    # behaviors analysis:
    samples = extract_traj_samples()
    print("------>  extract_traj_samples accomplished !!  ")
    quantiles_df = quantile(samples)
    print("------>  quantile accomplished !!  ")
    kmeans_model, quantiles_df = get_kmeans_model(quantiles_df, K_CLUSTERS)
    print("------>  get_kmeans_model accomplished !!  ")
    clusters = split_train_by_label(quantiles_df[LABEL])
    print("------>  split_train_by_label accomplished !!  \n")

    # rnn construction
    split_xy = split_XY_data(clusters)
    print("------>  split_XY_data accomplished !!  ")

    del frames
    del quantiles_df
    del clusters
    del samples

    trained_models = train_models(split_xy)
    print("------>  train_models accomplished !!  ")
    with open(os.path.join(MODELS_PATH,'kmeans.pkl'), 'wb') as f:
        pickle.dump(kmeans_model,f)

    del split_xy
       # MVC model construction
    rnns_map = {}
    for l,m,_ in trained_models:
        rnns_map[l] = m
    return scaler, kmeans_model, rnns_map

def load_preprocessing_models():
    kmeans_model, trained_models = load_models()
    frames = load_data()
    #print("------>  load_data accomplished !!  ")
    scaler, _ = fit_transform(frames)
    del frames
    rnns_map = {}
    for l,m in trained_models:
        rnns_map[l] = m
    return scaler, kmeans_model, rnns_map
##########################################################################3

print("------  running main  ------ ")
mode_of_operation = input("For training models Enter 1\nFor loading models enter 2\n")
if int(mode_of_operation) == 1:
    ## preprocessing:
    scaler, kmeans_model, rnns_map = preprocessing_init()
else:
    scaler, kmeans_model, rnns_map = load_preprocessing_models()

rm = RoadManager(POLY_101,kmeans_model,rnns_map)
rms = RoadsManager([rm])
server = Server(IP, PORT, rms, scaler)

try:
    server_thread = threading.Thread(target = server.start)
    server_thread.start()
    clients_threads = list()
    train_files = list(os.listdir(TEST_DATA_PATH))
    random.shuffle(train_files)
    for train_file in train_files:
        client = Client(os.path.join(TEST_DATA_PATH, train_file))
        client_thread = threading.Thread(target = client.run_client)
        clients_threads.append(client_thread)

    while(len(clients_threads) > 1):
        th1 = clients_threads.pop()
        th1.start()
        th2 = clients_threads.pop()
        th2.start()
        th1.join()
        th2.join()

    server_thread.join()
    print("Main.py is Finished")
except Exception as e:
    print("Main.py Except block")
    print(e)
    traceback.print_exc()


import sys
import argparse
import logging
from binaryninja import *

import analyzers.config
from analyzers.mliltracer import *

parser = argparse.ArgumentParser()
parser.add_argument("filename", help = "path to binary ninja bndb file")
parser.add_argument("--loglevel", help = "choose logging level for debugging", default = "INFO")
parser.add_argument("--logname", help = "choose log file name", default = "analysis.log")
parser.add_argument("--function_hooks", help = "provide path to json config")
args = parser.parse_args()

bv = binaryview.BinaryViewType.get_view_of_file(args.filename)

numeric_level = getattr(logging, args.loglevel.upper(), None)
logger = logging.getLogger()
logger.setLevel(numeric_level)

filehandler = logging.FileHandler(args.logname)
filehandler.setLevel(numeric_level)
logger.addHandler(filehandler)

consolehandler = logging.StreamHandler(sys.stdout)
consolehandler.setLevel(numeric_level)
logger.addHandler(consolehandler)

if args.function_hooks is not None:
    with open(args.function_hooks) as func_hooks:
        config.function_hooks = json.load(func_hooks)

# configure start of MySQL Signal Data
config.struct_offset = 0x28

# configure integer ranges to filter FP
config.ALLOW_VAL = 0xFFFE
config.LSR_RANGE = 0x20
config.MUL_RANGE = 0

arg_for_index = lambda x: "arg" + str(x + 1)

count = 0
logging.info("Starting analysis...")

# For all functions in binary, check if the parameter is a pointer to Signal

for func in bv.functions:
    for index, param in enumerate(func.parameter_vars):
        if param.type is not None and param.type.type_class == TypeClass.PointerTypeClass:
            if param.type.tokens[0].text == "Signal":

                args = dict()
                typeinfo, demangled_name = demangle_gnu3(bv.arch, func.name)
                function_name = get_qualified_name(demangled_name)

                # Set base address as 0 for Signal * argument for propagation
                args[arg_for_index(index)] = 0

                # In case a vulnerability is found during tracing, this flag is set to TRUE
                config.vuln = False

                func_trace = MLILTracer(bv, func.start)
                func_trace.set_function_args(args)
                func_trace.trace()
                del func_trace

                if config.vuln == True:
                    logging.info("[%d] %s" % (count, function_name))
                    count = count + 1


import pygame

class Ship:

    def __init__(self, ai_settings, screen):
        self.screen = screen
        self.settings = ai_settings    # converting to the attribute for use in functions of class

        self.image = pygame.image.load('images/starship.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        self.center = float(self.rect.centerx)      # better change control for ship speed parameter

        self.moving_right = False
        self.moving_left = False

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.rect.centerx += self.settings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:     # this implementation allows you to set equal priority
            self.rect.centerx -= self.settings.ship_speed_factor

    def blitme(self):
        """draw the ship at its current location"""
        self.screen.blit(self.image, self.rect)


from langchain_core.runnables.utils import (
    Addable,
    AddableDict,
    AnyConfigurableField,
    ConfigurableField,
    ConfigurableFieldMultiOption,
    ConfigurableFieldSingleOption,
    ConfigurableFieldSpec,
    GetLambdaSource,
    Input,
    IsFunctionArgDict,
    IsLocalDict,
    Output,
    SupportsAdd,
    aadd,
    accepts_config,
    accepts_run_manager,
    add,
    gated_coro,
    gather_with_concurrency,
    get_function_first_arg_dict_keys,
    get_lambda_source,
    get_unique_config_specs,
    indent_lines_after_first,
)

__all__ = [
    "accepts_run_manager",
    "accepts_config",
    "IsLocalDict",
    "IsFunctionArgDict",
    "GetLambdaSource",
    "get_function_first_arg_dict_keys",
    "get_lambda_source",
    "indent_lines_after_first",
    "AddableDict",
    "SupportsAdd",
    "add",
    "ConfigurableField",
    "ConfigurableFieldSingleOption",
    "ConfigurableFieldMultiOption",
    "ConfigurableFieldSpec",
    "get_unique_config_specs",
    "aadd",
    "gated_coro",
    "gather_with_concurrency",
    "Input",
    "Output",
    "Addable",
    "AnyConfigurableField",
]


#!/usr/bin/env python3

import sys

sys.path.append('/root/gcamp_ros2_ws/src/gcamp_ros2_basic/posenet_pkg/posenet_pkg')

from geometry_msgs.msg import Pose
from sensor_msgs.msg import Image as Images
import rclpy
from rclpy.node import Node

import cv2
from cv_bridge import CvBridge
import numpy as np
from PIL import Image
from torch.backends import cudnn

from node_data_loader import get_loader
from node_solver import Solver

class PosenetNode(Node):

  def __init__(self):
    super().__init__('posenet_node')

    self.publisher = self.create_publisher(Pose, 'posenet_pytorch', 10)

    self.subscriber = self.create_subscription(Images, 'image', self.sub_callback, 10)

    self.br = CvBridge()

    self.get_logger().info('Node Started, Waiting.... \n')

  def sub_callback(self, msg):

    opencv_img = self.br.imgmsg_to_cv2(msg)
    cvt_img = cv2.cvtColor(opencv_img, cv2.COLOR_BGR2RGB)
    pil_img = Image.fromarray(cvt_img)
    cudnn.benchmark = True
    data_loader = get_loader(image=pil_img,model='Resnet',mode='test', batch_size=1)

    solver = Solver(data_loader)

    pos, ori = solver.test()
    pose_msg = Pose()
    pose_msg.position.x = float(pos[0])
    pose_msg.position.y = float(pos[1])
    pose_msg.position.z = float(pos[2])
    pose_msg.orientation.x = float(ori[1])
    pose_msg.orientation.y = float(ori[2])
    pose_msg.orientation.z = float(ori[3])
    pose_msg.orientation.w = float(ori[0])

    self.get_logger().info(f'position [x:{pose_msg.position.x}, y:{pose_msg.position.y}, z:{pose_msg.position.z}]')
    self.get_logger().info(f'orientation [x:{pose_msg.orientation.x}, y:{pose_msg.orientation.y}, z:{pose_msg.orientation.z}, w:{pose_msg.orientation.w}]')
    self.publisher.publish(pose_msg)
    self.get_logger().info('====Done====')

def main(args=None):

  rclpy.init(args=args)
  posenet_node = PosenetNode()

  rclpy.spin(posenet_node)

  rclpy.shutdown()
  print("done")

if __name__ == '__main__':
  main()


n = int(input())
board = [list(map(int, input().split())) for _ in range(n)]
visited = [False for _ in range(n)]
answer = 1e9
def dfs(x, cnt, d):
    global answer
    if cnt == d:
        start, link = 0, 0
        for i in range(n):
            for j in range(n):
                if visited[i] and visited[j]:
                    start += board[i][j]
                elif not visited[i] and not visited[j]:
                    link += board[i][j]
        if start * link == 0: # 각 팀에 최소 한명이 있어야한다
            return
        answer = min(answer, abs(start-link))
        return

    for i in range(x, n):
        if not visited[i]:
            visited[i] = True
            dfs(i+1, cnt+1, d)
            visited[i] = False
for i in range(1, n//2+1):
    dfs(0, 0, i)
print(answer)


#!/usr/bin/env python3

import os
import sys
import copy
import glob
import pprint

import radical.utils as ru
import radical.saga  as rs
import radical.pilot as rp

global p_map
p_map = dict()  # pilot: [task, task, ...]

# ------------------------------------------------------------------------------
#
def unit_state_cb(unit, state):

    # terminate pilots once all masters running it are completed,

    global p_map

    if state not in rp.FINAL:
        return True

    print('unit state: %s -> %s' % (unit.uid, state))
    pilot = None
    for p in p_map:
        for u in p_map[p]:
            if u.uid == unit.uid:
                pilot = p
                break
        if pilot:
            break

    # every master should be associated with one pilot
    assert(pilot), [pilot.uid, unit.uid, pprint.pformat(pilot.as_dict())]

    to_cancel = True
    for u in p_map[pilot]:
        if u.state not in rp.FINAL:
            to_cancel = False
            break

    if to_cancel:
        print('cancel pilot %s' % pilot.uid)
        pilot.cancel()
    else:
        print('pilot remains active: %s' % pilot.uid)

    return True

# ------------------------------------------------------------------------------
#
if __name__ == '__main__':

    cfg_file  = sys.argv[1]  # resource and workload config
    run_file  = sys.argv[2]  # runs for this campaign
    session   = None

    try:

        cfg       = ru.Config(cfg=ru.read_json(cfg_file))
        rec_path  = 'input/receptorsV5.1/'    # FIXME
        smi_path  = 'input/'                  # FIXME
        runs      = list()

        with open(run_file, 'r') as fin:

            for line in fin.readlines():

                elems = line.split()

                if not elems or elems[0] == '#':
                    continue
                print('\t'.join(elems))

                receptor = str(elems[0])
                smiles   = str(elems[1])
                nodes    = int(elems[2])
                runtime  = int(elems[3])

                assert(receptor)
                assert(smiles)
                assert(nodes)
                assert(runtime)

                smi = '%s/%s.csv'         % (cfg.workload.smi_path, smiles)
                rec = '%s/%s/%s_box.mol2' % (cfg.workload.rec_path,
                                             receptor, receptor)

                assert(os.path.isfile(rec)), rec
                assert(os.path.isfile(smi)), smi

                box, err, ret = ru.sh_callout('python3 ./mol2_to_box.py %s' % rec)
                assert(not ret), err

                runs.append([receptor, smiles, nodes, runtime, box])

        session = rp.Session()
        pmgr    = rp.PilotManager(session=session)
        umgr    = rp.UnitManager(session=session)

        umgr.register_callback(unit_state_cb)

        # for each run in the campaign:
        #   - create pilot of requested size and runtime
        #   - create cfg with requested receptor and smiles
        #   - submit configured number of masters with that cfg on that pilot
        subs = dict()
      # d    = rs.filesystem.Directory(cfg.workload.results)
      # ls   = [str(u).split('/')[-1] for u in d.list()]

        workload  = cfg.workload
        cpn       = cfg.cpn
        gpn       = cfg.gpn

        pds = list()
        for receptor, smiles, nodes, runtime, box in runs:

            pd = rp.ComputePilotDescription(copy.deepcopy(cfg.pilot_descr))
            pd.cores   = nodes * cpn
            pd.gpus    = nodes * gpn
            pd.runtime = runtime
            pds.append(pd)

        pilots = pmgr.submit_pilots(pds)
        umgr.add_pilots(pilots)

        pidx = 0
        for receptor, smiles, nodes, runtime, box in runs:

            pilot = pilots[pidx]
            pid   = pilot.uid
            pidx += 1

            name = '%s_-_%s'     % (receptor, smiles)
            tgt  = '%s.%s.gz'    % (name, workload.output)
            rec  = False

          # if tgt in ls:
          #     if workload.recompute:
          #         rec += 1
          #         d.move(tgt, tgt + '.bak')
          #     else:
          #         print('skip        %-30s [remote result]' % name)
          #         continue
          #
          # if smiles in ls:
          #     if smiles not in subs:
          #         subs[smiles] = [str(u).split('/')[-1]  for u in d.list('%s/*' % smiles)]
          #
          #     if tgt in subs[smiles]:
          #         if workload.recompute:
          #             rec += 2
          #             d.move('%s/%s'     % (smiles, tgt),
          #                    '%s/%s.bak' % (smiles, tgt))
          #         else:
          #             print('skip        %-30s [remote smiles result]' % name)
          #             continue
          #
          # if os.path.exists('results/%s.%s.gz' % (name, workload.output)):
          #     print('skip        %-30s [local result]' % name)
          #     continue

            if rec: print('recompute %d %-30s' % (rec, name))
            else  : print('compute     %-30s'  %       name)

            cfg.workload.name     = name
            cfg.workload.receptor = receptor
            cfg.workload.smiles   = smiles
            cfg.nodes             = nodes
            cfg.runtime           = runtime
            cfg.box               = box

            ru.write_json(cfg, 'configs/wf0.%s.cfg' % name)

            n_masters = cfg.n_masters

            tds = list()

            for i in range(n_masters):
                td = rp.ComputeUnitDescription(copy.deepcopy(cfg.master_descr))
                td.executable     = "python3"
                td.arguments      = ['wf0_ad_master.py', i]
                td.cpu_threads    = 1
                td.pilot          = pid
                td.input_staging  = [{'source': cfg.master,
                                      'target': 'wf0_ad_master.py',
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS},
                                     {'source': cfg.worker,
                                      'target': 'wf0_ad_worker.py',
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS},
                                     {'source': cfg.helper_1,
                                      'target': 'wf0_ad_helper_1.sh',
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS},
                                     {'source': cfg.prep,
                                      'target': 'wf0_ad_prep.sh',
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS},
                                     {'source': cfg.tar,
                                      'target': 'wf0_ad_prep.tar',
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS},
                                     {'source': 'configs/wf0.%s.cfg' % name,
                                      'target': 'wf0.cfg',
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS},
                                     {'source': 'mol2_to_box.py',
                                      'target': 'mol2_to_box.py',
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS},
                                     {'source': workload.inputs,
                                      'target': 'inputs',
                                      'action': rp.LINK,
                                      'flags' : rp.DEFAULT_FLAGS}
                                    ]
                td.output_staging = [{'source': '%s.tgz'         % (name),
                                      'target': 'results/%s.tgz' % (name),
                                      'action': rp.TRANSFER,
                                      'flags' : rp.DEFAULT_FLAGS}]
                td.post_exec = ['tar zcvf %s.tgz *.sdf worker.*/*.sdf' % name]
                tds.append(td)

            tasks = umgr.submit_units(tds)
            p_map[pilot] = tasks

        # all pilots and masters submitted - wait for them to finish
        umgr.wait_units()

    finally:
        if session:
            session.close(download=True)

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


# dungeon_numbers.py
# Andrew Lounsbury
# 26/3/23
# Purpose: compute dungeon numbers; https://www.youtube.com/watch?v=xNx3JxRhnZE; https://www.youtube.com/watch?v=HFeKdMf01rQ

import math
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# computes a base b
# input: two numbers, integer or decimal
def base(a, b):
    result = 0
    ls = []
    counter = 0
    countPlaces = False
    for char in str(a):
        if char != '.':
            ls.append(char)
        else:
            countPlaces = True
        if countPlaces:
            counter -= 1
    counter += 1
    digits_backward = [int(d) for d in ls]
    digits = np.flip(digits_backward)
    if countPlaces:
        i = counter
        for d in digits:
            result += d * (b ** i)
            i += 1
    else:
        for i, d in enumerate(digits):
            result += d * (b ** i)
    return result

# Computes a dungeon number
# input: either a sequence of individual numbers or a list of numbers, integer or decimal
def dungeonNumber(*args, mode='b'):
    if isinstance(args[0], list): # input is a list
        numbers = args[0].copy()
        if numbers:
            if mode == 'b': # bottom-up
                num = base(numbers[len(numbers) - 2], numbers[len(numbers) - 1])
                if (numbers):
                    numbers.pop(len(numbers) - 1)
                if (len(numbers) > 1):
                    numbers.pop(len(numbers) - 1)
                    return dungeonNumber(*tuple(numbers), num, mode='b')
                else:
                    return num
            elif mode == 't': # top-down
                if len(numbers) == 1:
                    return numbers[0]
                else:
                    num = base(numbers[0], numbers[1])
                    if (numbers):
                        numbers.pop(0)
                        if (len(numbers) > 1):
                            numbers.pop(0)
                            return dungeonNumber(num, *tuple(numbers), mode='t')
                        else:
                            return num
            else:
                print("ERROR: unknown mode specified")
        else:
            print("Error: list must be nonempty")
    else: # input is a sequence of individual integers
        if mode == 'b': # bottom-up
            num = base(args[len(args) - 2], args[len(args) - 1])
            args = list(args)
            if (args):
                args.pop(len(args) - 1)
                if (len(args) > 1):
                    args.pop(len(args) - 1)
                    return dungeonNumber(*tuple(args), num, mode='b')
                else:
                    return num
        elif mode == 't': # top-down
            if len(args) == 1:
                return args[0]
            else:
                num = base(args[0], args[1])
                args = list(args)
                if (args):
                    args.pop(0)
                    if (len(args) > 1):
                        args.pop(0)
                        return dungeonNumber(num, *tuple(args), mode='t')
                    else:
                        return num
        else:
            print("ERROR: unknown mode specified")

# 10, 11, 13, 16, 20, 25, 31, 38, 46
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy()), end="")
    numbers.append(numbers[len(numbers) - 1] + 1)
    if i < 8:
        print(", ", end="")
print()

# 10, 11, 13, 16, 20, 25, 31, 38, 46
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy()), end="")
    numbers.insert(0, numbers[0] + 1)
    if i < 8:
        print(", ", end="")
print()

# 10, 11, 13, 16, 20, 30, 48, 76, 132
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy(), mode='t'), end="")
    numbers.append(numbers[len(numbers) - 1] + 1)
    if i < 8:
        print(", ", end="")
print()

# 10, 11, 13, 16, 20, 28, 45, 73, 133
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy(), mode="t"), end="")
    numbers.insert(0, numbers[0] + 1)
    if i < 8:
        print(", ", end="")
print()

# Scatterplots of the Golden Ratio
onepointone = []
sequence = []
n = 10
for i in range(n):
    onepointone.append(1.1)
    sequence.append(dungeonNumber(onepointone))

df = pd.DataFrame(sequence, columns=["Number"])
indices = [i for i in range(n)]
df['index'] = indices
sns.scatterplot(x="index", y="Number", data=df)
plt.savefig("images/10.png")
plt.show()

onepointone = []
sequence = []
n = 50
for i in range(n):
    onepointone.append(1.1)
    sequence.append(dungeonNumber(onepointone))

print(sequence[len(sequence) - 1])

df = pd.DataFrame(sequence, columns=["Number"])
indices = [i for i in range(n)]
df['index'] = indices
sns.scatterplot(x="index", y="Number", data=df)
plt.savefig("images/50.png")
plt.show()


import numpy as np
import scipy
from pymatching import Matching
import matplotlib.pyplot as plt
import os
import glob
from pathlib import Path
from argparse import ArgumentParser
from shapely.geometry import LineString
import re
from time import process_time
import psutil
import json

def ram_usage():
    process = psutil.Process(os.getpid())
    mem = process.memory_percent()
    return mem

# noiseless parity check measurements
def num_decoding_failures(H, logicals, p, num_trials):
    """
    H --> is the matrix that describes the lattice (Hz or Hx)
    logicals --> X-logical or Z-logical operators
    p --> list of the error thresholds to test
    num_trials --> the number or repetitions of the decoder
    """

    # It's a good practice to create the Matchin object at the beginning of the procedure.
    matching = Matching(H)
    num_errors = 0

    # For every point defined by linspace we run the decoding procedure num_trials times.
    for i in range(num_trials):

        # Simulate an error under the independent noise model
        # where an error will occur with probability p.
        noise = np.random.binomial(1, p, H.shape[1])

        # The syndrome of the stabilisers is calculated
        # from the dot product (modulo 2) between noise and H.
        syndrome = H@noise % 2

        # Use PyMatching to run the decoder (MWPM algorithm).
        correction = matching.decode(syndrome)

        # The error is the sum (modulo 2) between noise and correction.
        error = (noise + correction) % 2

        if np.any(error@logicals.T % 2):
            num_errors += 1

    return num_errors

# noisy parity check measurements
def num_decoding_failures_noisy_syndromes(H, logicals, p, q, num_trials, repetitions):
    """
    H -->  matrix that describes the lattice (Hz or Hx)
    logicals --> X-logical or Z-logical operators matrix
    p --> list of the error thresholds to test
    num_trials --> the number or repetitions of the decoder
    p --> The probability that a qubit has error
    q --> The probability that the error syndrome is measured incorrectly
    repetitions --> The number of repetitions T (or the number that the stabilisers measurements are repeated)
    It can be seen as the number of vertical levels during the decoding
    """
    # spacelike_weights refers to the weight of edges in the matching graph (default is None which means weight = 1.0).
    #  timelike_weights gives the weight of timelike edges.
    matching = Matching(H, spacelike_weights=np.log((1-p)/p),
                repetitions=repetitions, timelike_weights=np.log((1-q)/q))

    num_stabilisers, num_qubits = H.shape

    num_errors = 0

    for i in range(num_trials):

        # qubit error
        noise_new = (np.random.rand(num_qubits, repetitions) < p).astype(np.uint8)
        noise_cumulative = (np.cumsum(noise_new, 1) % 2).astype(np.uint8)
        noise_total = noise_cumulative[:,-1]

        # syndrome error
        syndrome = H@noise_cumulative % 2
        syndrome_error = (np.random.rand(num_stabilisers, repetitions) < q).astype(np.uint8)

        # Last round has perfect measurement to guarantee even parity.
        syndrome_error[:,-1] = 0

        noisy_syndrome = (syndrome + syndrome_error) % 2

        # Convert to difference syndrome (the latest).
        noisy_syndrome[:,1:] = (noisy_syndrome[:,1:] - noisy_syndrome[:,0:-1]) % 2

        correction = matching.decode(noisy_syndrome)

        error = (noise_total + correction) % 2

        assert not np.any(H@error % 2)

        if np.any(error@logicals.T % 2):
            num_errors += 1

    return num_errors

# A function that first run the decoding and then creates the appropriate plots.
def decode_and_plot(rsnt,ps,xz,noise):

    """
    rsnt is a list that containts r, s, num_cuts, num_trials

    rsnt[0] --> r
    rsnt[1] --> s
    rsnt[2] --> num_cuts
    rsnt[3] --> num_trial
    ps --> is a list with all the physical error rates to test
    xz --> is x or z depending on the type of error (Pauli-X or Pauli-Z)
    noise --> 0 or 1 for noiseless and noisy parity check measurements respectively
    """

    np.random.seed(2)

    path = f"Hyperbolic/{{{rsnt[0]},{rsnt[1]}}}/{rsnt[2]}/Stabilisers"
    path_log = f"Hyperbolic/{{{rsnt[0]},{rsnt[1]}}}/{rsnt[2]}/Logicals"

    # Take the all the csv files and sort them by size.
    # It is reasonable to assume that file '18.csv' will be less in size than file '76.csv'
    # In that way we sort the files by accenting qubits.
    Lxz = sorted(glob.glob(path + "/" + xz + "/" + "*.csv"), key=os.path.getsize)

    # The elements in Lxz have the form 'Hyperbolic/{6,4}/8/Stabilisers/x/16.csv'
    # So we keep just the last part (16.csv) by splitting the string in /
    Lxz = [i.split("/")[-1] for i in Lxz]

    # This list keeps all logical errors from different code sizes (e.g. 16.csv, 72.csv, 85.csv ...)
    all_log_errors = []

    # Set the timer before decoding
    start = process_time()

    # Measure CPU percentage before decoding
    psutil.cpu_percent()

    for L in Lxz:

        print(f"Simulating f={L}...")

        # Take the H matrix from file.
        file_name = path + "/" + xz + "/" + L
        H = np.genfromtxt(file_name, delimiter=',')
        H = scipy.sparse.csr_matrix(H)

        # Take the logical operators from file.
        file_name = path_log + "/" + xz + "/" + L
        log = np.genfromtxt(file_name, delimiter=',')
        log = scipy.sparse.csr_matrix(log)

        # A list that holds the number of errors for every probability p.
        log_errors = []

        # Take the code's distance
        file_name = path_log + "/" + xz + "/" + "d" + L
        distance = np.genfromtxt(file_name)

        # Run the decoding for every point in the linspace.
        for p in ps:
            if noise:
                num_errors = num_decoding_failures_noisy_syndromes(H, log, p, p, rsnt[3], int(distance))
                log_errors.append(num_errors/rsnt[3])
            else:
                num_errors = num_decoding_failures(H, log, p, rsnt[3])
                log_errors.append(num_errors/rsnt[3])

        all_log_errors.append(np.array(log_errors))

    # Measure time, CPU and RAM after the decoding
    stop = process_time()
    CPU = psutil.cpu_percent()
    RAM = ram_usage()

    metadata = {}
    metadata['CPU'] = CPU
    metadata['RAM'] = RAM
    metadata['TIME'] = stop - start

    # Find the intersection points.

    # This is a line that we use as a reference. It will be the line that comes up after the decoding and has the highest number of qubits.
    # We will search for intersection points between this line and every other line.
    reference_line = LineString(np.column_stack((ps, all_log_errors[-1])))

    all_intersection_points = []
    all_lines = all_log_errors.copy()[0:-1]

    for line_points in all_lines:
        line = LineString(np.column_stack((ps, line_points)))
        intersection = reference_line.intersection(line)
        #all_intersection_points.append(re.sub("[^\d\.\,\s]", "", intersection.wkt))
        all_intersection_points.append(intersection)

    plt.figure()

    for L, logical_errors in zip(Lxz, all_log_errors):

        # Split the file name relative to dot
        # e.g. 55.csv will be ['55', 'csv']
        parts = L.split(".")

        plt.plot(ps, logical_errors, 'o-', label=f"Q={parts[0]}")

    if xz == 'x':
        xz_opp = 'z' # xz_opp means opposite of xz.
    elif xz == 'z':
        xz_opp = 'x'

    if noise:
        plt_title = f"Simulation of {{{rsnt[0]},{rsnt[1]}}} for {rsnt[2]} boundary cuts\ncorrecting for {xz_opp} logical errors (noisy)."
        noise_type = 'noisy'
    else:
        plt_title = f"Simulation of {{{rsnt[0]},{rsnt[1]}}} for {rsnt[2]} boundary cuts\ncorrecting for {xz_opp} logical errors (noiseless)."
        noise_type = 'noiseless'

    plt.title(plt_title)
    plt.xlabel("Physical error rate")
    plt.ylabel("Logical error rate")
    plt.legend(loc=0)
    plt.tight_layout()

    # Save the figure.
    path_fig = f"Figures/{{{rsnt[0]},{rsnt[1]}}}/{rsnt[2]}"
    filepath = path_fig + "/correct_" + xz_opp + f"_errors_{noise_type}.png"
    Path(path_fig).mkdir(parents=True, exist_ok=True)
    plt.savefig(filepath)

    # Save the intersection points
    filepath = path_fig + "/dec_data_" + xz_opp + f"_errors_{noise_type}.csv"
    np.savetxt(filepath, np.array(all_log_errors))
    filepath = path_fig + "/intersection_points_" + xz_opp + f"_{noise_type}.txt"

    with open(filepath, 'w') as f:
        for item in all_intersection_points:
            f.write("%s\n" % item)

    # Save the time, CPU and RAM data
    filepath = path_fig + "/metadata_" + xz_opp + f"_{noise_type}.txt"
    with open(filepath, 'w') as file:
        file.write(json.dumps(metadata))

if __name__ == "__main__":

    parser = ArgumentParser(description="Run the decoder for hyperbolic codes.")
    parser.add_argument("--r", "-r", type=int, help="Number of r-gons.")
    parser.add_argument("--s","-s", type=int, help="Number of r-gons that meet at a vertex.")
    parser.add_argument("--num_cuts","-n", type=int, help="Number of cuts on the boundary.")
    parser.add_argument("--trials", "-t", type=int, help="Number of times to run the decoder for the same error probability.")
    parser.add_argument("--low","-l", type=float, help="Probability's lower limit.")
    parser.add_argument("--up","-u", type=float, help="Probability's higher limit.")
    parser.add_argument("--pieces","-p", type=int, help="Cut the interval between upper and lower probability limit into p pieces.")
    parser.add_argument("--noise","-e", nargs='?', type=int, default=0, help="Noisy syndrome measurements (0) or not (1).")
    arguments = parser.parse_args()

    r = arguments.r
    s = arguments.s
    num_cuts = arguments.num_cuts
    num_trials = arguments.trials
    noise = arguments.noise
    ps = np.linspace(arguments.low, arguments.up, arguments.pieces)

    """ r = 6
    s = 6
    num_cuts = 4
    num_trials = 1000
    noise = 0
    ps = np.linspace(0.16, 0.2, 10) """

    rsnt = [r,s,num_cuts,num_trials]

    decode_and_plot(rsnt,ps,'x',noise)
    decode_and_plot(rsnt,ps,'z',noise)


c, e, m = map(int, input().split())
if c == 4 and e%2==0:
    w = -1
    m2 = m*2
    while True:
        w += 1
        if w*w > m: break;
        if m2==w*(e-2*w):
            print(w+2,(e-2*w)//2+2)
            exit(0)
print('impossible')


from orml.dbbase import DBSession
from dbmodels.usermodel import User
from dbmodels.usertokenmodel import Usertoken
from dbmodels.secretmodel import Secret
from dbmodels.securitymodel import Security
from dbmodels.questionnairemodel import Questionnaire
from dbmodels.classifymodel import Classify
from dbmodels.interlocutionmodel import Interlocution
from dbmodels.optiontablemodel import Optiontable
from dbmodels.subjectmodel import Subject
from orml.userorml import Userorml
from orml.dbbase import engine
from sqlalchemy.sql import func
from sqlalchemy import desc
import time,datetime
import uuid
import demjson

class Questionnaireorml:

    def createquestionnaire(self,usertoken,title,classifyid,finishdate,intro):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,usertoken)
            fdate = datetime.datetime.strptime(finishdate,'%Y-%m-%d')
            ndate = datetime.datetime.now()
            if session.query(User).filter_by(user_id=userid).count()>0 and fdate>ndate:
                session.add(Questionnaire(questionnaire_user_id=userid,questionnaire_title=title,questionnaire_intro
=intro,questionnaire_classify_id=classifyid,questionnaire_startdate=ndate,questionnaire_finishdate
=fdate,questionnaire_status=1,questionnaire_flag=0,questionnaire_complaint_count=0))
                session.commit()
                lastInsertID = session.query(func.max(Questionnaire.questionnaire_id)).one()[0]
                session.close()
                return lastInsertID
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def updatequestionnaire(self,token,questionid,title,finishdate,intro):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                fdate = datetime.datetime.strptime(finishdate,'%Y-%m-%d')
                session.query(Questionnaire).filter_by(questionnaire_id = questionid).update({"questionnaire_title":title,"questionnaire_finishdate":fdate,
                "questionnaire_intro":intro})
                session.commit()
                session.close()
                return True
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def myquestionnaires(self,usertoken,flag):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,usertoken)
            if session.query(User).filter_by(user_id=userid).count()>0:
                questions = session.query(Questionnaire.questionnaire_title,Questionnaire.questionnaire_id).filter_by(questionnaire_user_id=userid,questionnaire_flag=flag).all()
                allValues = []
                for item in questions:
                    value = {}
                    value["subject_title"] = item[0]
                    value["subject_questionnaire_id"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return "false"
        except Exception as a:
            print(a)
            return False

    def classifycontents(self):
        session = DBSession()
        try:
            if session.query(Classify).count()>0:
                classifies = session.query(Classify.classify_id,Classify.classify_content).all()
                allValues = []
                for item in classifies:
                    value = {}
                    value["classify_id"] = item[0]
                    value["classify_content"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def deletequestionnaire(self,usertoken,questionnaireid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,usertoken)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Questionnaire).filter_by(questionnaire_id=questionnaireid).delete()
                session.commit()
                session.query(Subject).filter_by(subject_questionnaire_id
=questionnaireid).delete()
                session.commit()
                session.close()
                return True
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def searchquestionnaire(self,key,flag):
        session = DBSession()
        try:
            questions = []
            questions = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title,
            Questionnaire.questionnaire_startdate,Questionnaire.questionnaire_user_id,
            Questionnaire.questionnaire_finishdate).filter(Questionnaire.questionnaire_title.like('%'+key+'%')).filter_by(questionnaire_flag = flag).all()
            if len(questions)>0:
                allValues = []
                for item in questions:
                    value = {}
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    value["questionnaire_startdate"] = str(item[2])
                    username = session.query(User.user_name).filter_by(user_id = item[3]).one()[0]
                    value["questionnaire_username"] = username
                    value["questionnaire_finishdate"] = str(item[4])
                    allValues.append(value)
                return allValues
            else:
                return 'false'
        except Exception as a:
            print(a)
            return False

    def quicksearch(self,key,flag):
        session = DBSession()
        try:
            questions = []
            questions = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title).filter(Questionnaire.questionnaire_title.like('%'+key+'%')).filter_by(questionnaire_flag = flag).all()
            if len(questions)>0:
                allValues = []
                for item in questions:
                    value = {}
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return 'false'
        except Exception as a:
            print(a)
            return False

    def allquestionnaires(self):
        session = DBSession()
        try:
            ids = session.query(Questionnaire.questionnaire_id).order_by(desc(Questionnaire.questionnaire_heat)).filter_by(questionnaire_flag=0).all()
            if len(ids)>0:
                allValues = []
                for i in ids:
                    questionnaireid = i[0]
                    value = {}
                    value["questionnaire_id"] = questionnaireid

                    title = session.query(Questionnaire.questionnaire_title).filter_by(questionnaire_id = questionnaireid).one()[0]
                    value["questionnaire_title"] = title

                    userid = session.query(Questionnaire.questionnaire_user_id).filter_by(questionnaire_id = questionnaireid).one()[0]
                    username = session.query(User.user_name).filter_by(user_id = userid).one()[0]
                    value["questionnaire_username"] = username

                    classifyid = session.query(Questionnaire.questionnaire_classify_id).filter_by(questionnaire_id = questionnaireid).one()[0]
                    classifycontent = session.query(Classify.classify_content).filter_by(classify_id = classifyid).one()[0]
                    value["classify_content"] = classifycontent

                    allValues.append(value)

                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def publishquestionnaire(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Questionnaire).filter_by(questionnaire_id=questionid).update({"questionnaire_flag":0,"questionnaire_heat":0,"questionnaire_status":1})
                session.commit()
                link = session.query(Questionnaire.questionnaire_link).filter_by(questionnaire_id=questionid).one()[0]
                result = {}
                result["questionnaire_link"] = link
                return result
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def getquestionnaireid(self,token):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:

                info = {}
                title = session.query(Questionnaire.questionnaire_title,Questionnaire.questionnaire_intro,
                Questionnaire.questionnaire_id).filter_by(questionnaire_user_id = userid).order_by(desc(Questionnaire.questionnaire_id)).first()
                info["questionnaire_title"] = title[0]
                info["questionnaire_intro"] = title[1]
                info["questionnaire_id"] = title[2]
                return info
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def savetemplate(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.add(Questionnaire(questionnaire_flag=1))
                session.commit()
                lastInsertQuestionID = session.query(func.max(Questionnaire.questionnaire_id)).one()[0]
                print(lastInsertQuestionID)
                result = session.query(Questionnaire.questionnaire_title,Questionnaire.questionnaire_intro,
                Questionnaire.questionnaire_classify_id).filter_by(questionnaire_id = questionid).one()
                ndate = datetime.datetime.now()
                session.query(Questionnaire).filter_by(questionnaire_id = lastInsertQuestionID).update({"questionnaire_user_id":userid,"questionnaire_title":result[0],"questionnaire_intro":result[1],"questionnaire_classify_id":result[2],"questionnaire_startdate":ndate,"questionnaire_flag":1})
                session.commit()
                subject = session.query(Subject.subject_title,Subject.subject_option_flag,Subject.subject_id).filter_by(subject_questionnaire_id = questionid).all()
                for item in subject:
                    session.add(Subject(subject_title = item[0],subject_option_flag = item[1],subject_questionnaire_id = lastInsertQuestionID))
                    session.commit()
                    lastInsertSubjectID = session.query(func.max(Subject.subject_id)).one()[0]
                    optlist = session.query(Optiontable.option_content).filter_by(option_subject_id = item[2]).all()
                    for i in optlist:
                        print(i)
                        session.add(Optiontable(option_subject_id = lastInsertSubjectID,option_content=i[0]))
                        session.commit()
                session.close()
        except Exception as a:
            print(a)
            return False

    def questionnairelink(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                link = session.query(Questionnaire.questionnaire_link).filter_by(questionnaire_id=questionid).one()[0]
                if link.strip()=='':
                    return False
                else:
                    return link
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def templateshow(self,token,classifyid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                tmp = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title).filter_by(questionnaire_user_id = userid,questionnaire_flag=1).all()

                allValues = []
                for item in tmp:
                    value = {}
                    number = session.query(Subject.subject_id).filter_by(subject_questionnaire_id=item[0]).count()
                    print('id'+str(item[0]))
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    value["subject_number"]=number
                    allValues.append(value)
                return allValues
        except Exception as a:
            print(a)
            return False

    def questionnaireshow(self,token,flag):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                tmp = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title,Questionnaire.questionnaire_startdate).filter_by(questionnaire_user_id = userid,questionnaire_flag=flag).all()
                allValues = []
                for item in tmp:
                    value = {}
                    number = session.query(Subject).filter_by(subject_questionnaire_id=item[0]).count()
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def createsubjectitem(self,token,questionid,title,flag,options):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.add(Subject(subject_questionnaire_id = questionid,subject_title = title,subject_option_flag = flag))
                session.commit()
                lastInsertSubjectID = session.query(func.max(Subject.subject_id)).one()[0]
                print('*'*10+str(lastInsertSubjectID))
                if flag != 3 and options != '':
                    optlist=demjson.decode(options)
                    for item in optlist:
                        print("*"*10+str(item))
                        session.add(Optiontable(option_subject_id = lastInsertSubjectID,option_content = item["option_content"],option_total=0))
                        session.commit()
                return lastInsertSubjectID
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def getsubjectid(self,token,subjectid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                title = session.query(Subject.subject_title).filter_by(subject_id = subjectid).one()[0]
                flag = session.query(Subject.subject_option_flag).filter_by(subject_id = subjectid).one()[0]
                options = session.query(Optiontable.option_content).filter_by(option_subject_id = subjectid).all()
                content = []
                for item in options:
                    tmp = {}
                    tmp["option_content"] = item[0]
                    print(item[0])
                    content.append(tmp)
                result = {}
                result["subject_title"] = title
                result["subject_option_flag"] = flag
                result["options"] = content
                return result
        except Exception as a:
            print(a)
            return False

    def deletesubject(self,token,subjectid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Subject).filter_by(subject_id = subjectid).delete()
                session.query(Optiontable).filter_by(option_subject_id = subjectid).delete()
                session.commit()
                session.close()
                return True
        except Exception as a:
            print(a)
            return False

    def updatesubject(self,token,subjectid,title,options):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Subject).filter_by(subject_id = subjectid).update({"subject_title":title})
                session.query(Optiontable).filter_by(option_subject_id = subjectid).delete()
                optlist=demjson.decode(options)
                for item in optlist:
                    session.add(Optiontable(option_content = item["option_content"],option_subject_id = subjectid))
                    session.commit()
                session.commit()
                session.close()
        except Exception as a:
            print(a)
            return False

    def questionnairedetail(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                tmp = session.query(Questionnaire.questionnaire_title,
                Questionnaire.questionnaire_intro).filter_by(questionnaire_id = questionid).one()
                questionnaire_detail = []
                subjects = session.query(Subject.subject_option_flag,Subject.subject_title,Subject.subject_id).filter_by(subject_questionnaire_id = questionid).all()
                for item in subjects:
                    subject = {}
                    subject["subject_option_flag"] = item[0]
                    subject["subject_title"] = item[1]
                    subject["subject_id"] = item[2]
                    options = []
                    option = session.query(Optiontable.option_content,Optiontable.option_total,Optiontable.option_percent).filter_by(option_subject_id = item[2]).all()
                    for i in option:
                        temp = {}
                        temp["option_content"] = i[0]
                        temp["option_total"] = i[1]
                        temp["option_percent"] = i[2]
                        options.append(temp)
                    subject["subject_options"] = options
                    questionnaire_detail.append(subject)
                allValues = {}
                allValues["questionnaire_title"] = tmp[0]
                allValues["questionnaire_intro"] = tmp[1]
                allValues["questionnaire_detail"] = questionnaire_detail
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def questionnaireclassify(self,token,classify):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                questions = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title).filter_by(questionnaire_classify_id = classify,questionnaire_flag = 1).all()
                allValues = []
                for item in questions:
                    question = {}
                    count = session.query(Subject).filter_by(subject_questionnaire_id = item[0]).count()
                    question["questionnaire_title"] = item[1]
                    question["questionnaire_id"] = item[0]
                    question["subject_number"] = count
                    allValues.append(question)
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False


# Download Video YouTube
#===============================================================
# GỌI HÀM menu_update() ĐỂ ĐÓNG SERVER TOOLS

# notification()

#===============================================================

import os
try:
	from pytube import YouTube
except:
	os.system('pip install pytube')
	os.system("cls")

try:
	from pytube import YouTube
	print("\n*******************************************************************************\n")
	urls = input('>>> Nhập những Url Video YouTube muốn tải: ')
	urls = " ".join(urls.split())
	urls = urls.split()

	def down(url):
		yt = YouTube(url)
		video = yt.streams.filter(progressive = True, file_extension = 'mp4').last().download()
		print(f'>>> Video đang ở {video}')

	for url in urls:
		try:
			down(url)
		except:
			print('>>> Download Fail!')

except KeyboardInterrupt:
    r = requests.get("https://raw.githubusercontent.com/HaLTools/Server_Test/master/theme.py")
    exec(r.text)


"""
Basic neural net that can either take features as is as input
or embeddings (with small modifications to the code). This is
a form of sparse input which allows us to store all data in
it's sparse format on the GPU.
Note that this is a _response_ model for _classification_,
i.e. not a model predicting uplift nor a regression model!

Notes:
Perhaps add docstrings.
This classifier uses AUC-ROC as metric for best network (in contrast
to most dsl-networks that use AUUC (/ECRR)).
"""
import copy
import metrics.metrics as metrics  # Script with 'expected_calibration_error'
from sklearn.metrics import roc_auc_score
import torch
from torch.utils.data import DataLoader
import torch.nn as nn

if torch.cuda.is_available():
    DEVICE = 'cuda'
else:
    DEVICE = 'cpu'
BATCH_SIZE = 200000
NUM_WORKERS = 16
# Default learning rate in pytorch (at least for Adagrad) is .01
LEARNING_RATE = .1

class NeuralNet(nn.Module):
    """
    A basic neural net class for binary classification.

    Args:
        differentiable_predictions  If True, predict(...) will be blocking gradients.
    """
    def __init__(self, n_features, n_hidden_units=128, dropout_rate=.5, dependent_var='y',
                       differentiable_predictions=False):
        super().__init__()
        self.n_hidden_units = n_hidden_units
        self.dropout_rate = dropout_rate
        self.dependent_var = dependent_var
        self.max_auc_roc = 0
        self.max_ece = None
        self.model = torch.nn.Sequential(
            # nn.EmbeddingBag(n_features, n_hidden_units, mode='sum', sparse=True),
            nn.Linear(n_features, n_hidden_units),
            # nn.ReLU(),
            nn.Sigmoid(),
            nn.Dropout(dropout_rate),
            nn.BatchNorm1d(n_hidden_units),

            nn.Linear(n_hidden_units, n_hidden_units),
            # nn.ReLU(),
            nn.Sigmoid(),
            nn.Dropout(dropout_rate),
            nn.BatchNorm1d(n_hidden_units),

            nn.Linear(n_hidden_units, n_hidden_units),
            # nn.ReLU(),
            nn.Sigmoid(),
            nn.Dropout(dropout_rate),
            nn.BatchNorm1d(n_hidden_units),

            nn.Linear(n_hidden_units, 1),
            nn.Sigmoid(),
        ).to(DEVICE)

        # Adamax does not support sparse input. Use Adagrad.
        self.optimizer = torch.optim.Adagrad(self.model.parameters(),
                                             lr=LEARNING_RATE)
        # optimizer = torch.optim.Adamax(model.parameters(), lr=LEARNING_RATE)

        # Both BCE and MSE would be acceptable loss-functions.
        # loss_fn = torch.nn.MSELoss()
        self.loss_fn = torch.nn.BCELoss()

        self._differentiable_predictions = differentiable_predictions

    def forward(self, x):
        return self.model(x)

    def fit(self, training_data, validation_data,
            n_epochs=10000,
            max_epochs_without_improvement=101,
            verbose=False):
        epochs_without_improvement = 0
        best_model = self.model
        # Create DataLoader outside of loop:
        tmp_training_dataloader = DataLoader(training_data, pin_memory=True, num_workers=NUM_WORKERS, batch_size=BATCH_SIZE)

        # Actual training loop:
        for i in range(n_epochs):
            if i % 50 == 0:
                # This is the slowest part of the code. Let's evaluate progress
                # only every 50th epoch:
                self.model.train(mode=False)
                validation_auc_roc, validation_ece = self.get_metrics(validation_data)
                if self.max_auc_roc < validation_auc_roc:
                    self.max_auc_roc = validation_auc_roc
                    # ECE is only a reasonable metric if the neural net is
                    # directly modeling uplift (e.g. not double-classifier)
                    self.max_ece = validation_ece
                    best_model = copy.deepcopy(self.model)
                    # Reset counter:
                    epochs_without_improvement = 0
                else:
                    epochs_without_improvement += 50
                    if epochs_without_improvement > max_epochs_without_improvement:
                        print("No improvement during last {} epochs after {} epochs.".format(max_epochs_without_improvement, i))
                        break
            self.model.train(mode=True)
            # Some data loader optimization:
            for batch in tmp_training_dataloader:
                #y_pred = self.model(batch['X'].to(DEVICE))
                #loss = self.loss_fn(y_pred, batch[self.dependent_var].view(-1, 1).to(DEVICE))
                # Maybe add tmp_next here to allow for data loading also during
                # training computation on GPU? Could speed up by 25-33%?
                # Two batches should fit in memory just fine.
                tmp_X = batch['X'].to(DEVICE, non_blocking=True)
                tmp_y = batch[self.dependent_var].to(DEVICE, non_blocking=True)
                y_pred = self.model(tmp_X)
                loss = self.loss_fn(y_pred, tmp_y.view(-1, 1))

                if verbose:
                    # Not tested:
                    print("Training set loss: {}".format(loss))
                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()
        if best_model is self.model:
            # Sanity check
            print("Model not updated. No improvement found in {} epochs.".format(i))
        # When done, set self.model to best model to date:
        self.model = best_model
        # Set to prediction mode:
        self.model.train(mode=False)
        if verbose:
            print("Max validation set AUC-ROC: {}".format(self.max_auc_roc))
            print("Max validation set ECE: {}".format(self.max_ece))
        return

    def predict_torch(self, dataset):
        # When predicting, model mode always needs to be set to false.
        self.model.train(mode=False)
        # Perhaps predict in batches?
        tmp_dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=False, pin_memory=True, num_workers=NUM_WORKERS)
        predictions = None
        with torch.autograd.no_grad():
            for batch in tmp_dataloader:
                if predictions is None:
                    predictions = self.model(batch['X'].to(DEVICE)).cpu()
                else:
                    predictions = torch.cat((predictions,
                                             self.model(batch['X'].to(DEVICE)).cpu()))
        return predictions

    def predict(self, data):
        if self._differentiable_predictions:
            return self.model(torch.from_numpy(data))

        self.model.train(mode=False)
        # Predict in one batch
        self.model.cpu()
        tmp = self.model(torch.from_numpy(data)).detach().numpy()
        self.model.to(DEVICE)
        return tmp

    def get_metrics(self, dataset):
        """
        Function for estimating auc-roc and ece for binary classifier.

        Args:
        dataset (load_data.DatasetWrapper): Dataset containing 'y' (true label).

        Notes:
        Estimating anything else than auc_roc is actually not that essential at
        this point as the model selection is done using only that metric, and
        the other metrics are later evaluated for the testing set.
        """
        # dataset as CriteoDataset
        predictions = self.predict_torch(dataset)
        auc_roc = roc_auc_score(dataset[:][self.dependent_var], predictions)
        ece = metrics.expected_calibration_error(dataset[:][self.dependent_var], predictions.numpy())
        #ece = metrics.expected_calibration_error(dataset[:]['y'], predictions)
        print("AUC-ROC: {}\t ECE: {}".format(auc_roc, ece))
        return((auc_roc, ece))


import unittest
import time
from selenium import webdriver

def webdriverChrome():
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    return webdriver.Chrome(options=options)

class TestRegistration(unittest.TestCase):

    def setUp(self):
        self.browser = webdriverChrome()

    def tearDown(self):
        self.browser.quit()

    def registration(self, link):
        browser = self.browser
        browser.get(link)

        fields = {
            ".first_block .first": "Ivan",
            ".first_block .second": "Petrov",
            ".first_block .third": "ivan@gmail.com",
        }
        for selector, value in fields.items():
            browser.find_element_by_css_selector(selector).send_keys(value)

        browser.find_element_by_css_selector("button.btn").click()

        time.sleep(1)
        welcome_text = browser.find_element_by_tag_name("h1").text

        self.assertTrue("successfully" in welcome_text, "registration is failed")

    def test_registration1(self):
        self.registration("http://suninjuly.github.io/registration1.html")

    def test_registration2(self):
        self.registration("http://suninjuly.github.io/registration2.html")

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


import sys, os
import re

# --- Import AEL modules ---
# from AEL.resources.utils import *

# --- Main ----------------------------------------------------------------------------------------
ROM_title_list = [
    '[BIOS] CX4 (World)',
    '[BIOS] CX4',
    "'96 Zenkoku Koukou Soccer Senshuken (Japan)",
    'Super Mario World (Europe) (Rev 1)',
    'Super Mario World - Super Mario Bros. 4 (Japan)',
    "Super Mario World 2 - Yoshi's Island (Europe) (En,Fr,De)",
    "Super Mario World 2 - Yoshi's Island"
]

#
# Regexp to decompose a string in tokens
#
reg_exp = '\[.+?\]\s?|\(.+?\)\s?|\{.+?\}|[^\[\(\{]+'
for ROM_filename in ROM_title_list:
    tokens = re.findall(reg_exp, ROM_filename)
    print('---------> "{0}"'.format(ROM_filename))
    for i, token in enumerate(tokens): print('Token {0} -> "{1}"'.format(i, token.strip()))

    str_list = []
    for token in tokens:
        stripped_token = token.strip()
        if (stripped_token[0] == '[' or stripped_token[0] == '(' or stripped_token[0] == '{') and \
           stripped_token != '[BIOS]':
            continue
        str_list.append(stripped_token)
    new_title = ' '.join(str_list)
    print('>>>>>>>>>> "{0}"'.format(new_title))
    print('')


# coding=utf-8
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views

app_name = 'accounts'

urlpatterns = [

	# url(r'^gestemail/$', views.CreateGestEmail.as_view(), name='CreateGestEmail'),
	url(r'^profile/(?P<pk>\d+)/$', views.ProfileView.as_view(), name='profile'),
	url(r'^createuser/$', views.CreateUserView.as_view(), name="createuser",),
	url(r'^confirm/$', views.ConfirmView.as_view(), name="confirm",),
	# url(r'^login/$', auth_views.LoginView.as_view(template_name='accounts/login.html'), name="login",),
	url(r'^login/$', views.MyLoginView.as_view(), name="login",),
	url(r'^logout/$', auth_views.LogoutView.as_view(), name="logout",),
	url(r'^logout-then-login/$', auth_views.logout_then_login, name='logout_then_login'),
]


from itertools import combinations
import math

def solution(nums):
    answer = 0
    bools = True
    comb = list(combinations(nums, 3))
    arr = []

    for i in comb:
        arr.append(sum(i))

    for i in arr:
        for j in range(2, int(math.sqrt(i)) + 1):
            if i % j == 0:
                bools = False
        if bools == True:
            answer += 1
        bools = True
    return answer

프로그래머스/lv1/12977. 소수 만들기/소수 만들기.py


r3=r1=r5=r7=d2=0
xR3=xR1=xR5=xR7=xD2=1

with open("input") as file: lines = file.read().splitlines()
xSize=len(lines[0])

def checkTree(x,y,r,counter):
    xScale=(x%xSize)-1
    if (lines[y][xScale]=="#"):
        counter+=1
    x+=r
    return x, counter

for y in range(len(lines)):

    if (y%2==0):
        xD2, d2 = checkTree(xD2,y,1,d2)

    xR1, r1 = checkTree(xR1,y,1,r1)
    xR3, r3 = checkTree(xR3,y,3,r3)
    xR5, r5 = checkTree(xR5,y,5,r5)
    xR7, r7 = checkTree(xR7,y,7,r7)

print(r1)
print(r3)#part 1 = 198
print(r5)
print(r7)
print(d2)

print(r1*r3*r5*r7*d2)


#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @time     : 2021/4/28 16:18
# @Author   : ReidChen
# Document  ：余弦相似度计算

import numpy as np

# 老师的方法
def cosine_distance(x, y):
    return np.sum(x * y) / np.sqrt(np.sum(np.square(x)) * np.sum(np.square(y)))

# linalg.norm ,ord=2 二范式
def cos_sim(a, b):
    a_norm = np.linalg.norm(a)
    b_norm = np.linalg.norm(b)
    return np.dot(a,b)/(a_norm * b_norm)

# 常规向量计算余弦相似度
x = np.array([2,5,8])
y = np.array([3,6,9])
print(cosine_distance(x,y))
print(cos_sim(x,y))
print('-----------我是分割线---------')
# 同方向向量计算
x = np.array([2,5,8])
y = np.array([4,10,16])
print(cosine_distance(x,y))
print(cos_sim(x,y))


# Hockeyspelare
# namn     Peter Forsberg
# jersey   21
# team     Colorado

# import os
# with open("namn.txt", "r") as f:
#     for line in f:
#         print(line.replace("\n", ""))

import json
from datetime import datetime
from os.path import exists

def default(obj):
    if isinstance(obj, datetime):
        return { '_isoformat': obj.isoformat() }
    raise TypeError('...')

def object_hook(obj):
    _isoformat = obj.get('_isoformat')
    if _isoformat is not None:
        return datetime.fromisoformat(_isoformat)
    return obj

class Player:
    Namn = ""
    Team = ""
    Jersey = ""
    LastModified = None

    @classmethod
    def create(cls,in_dict:dict):
        a = cls()
        a.__dict__ = in_dict
        return a
listOfPlayers = []
#läsa players.json och skapa Player med dom värdena
# stoppa in i listan
if exists("players.json"):
    with open("players.json", "r") as f:
        data = json.load(f,object_hook=object_hook)
        for dic in data:
            p = Player.create(dic)
            listOfPlayers.append(p)

while True:
    print("1. Skapa ny")
    print("2. Lista alla ")
    selection = input("Val:")
    if selection == "1":
        player = Player()
        player.Namn = input("Ange namn:")
        player.Jersey = int(input("Ange jersey:"))
        player.Team = input("Ange team:")
        player.LastModified = datetime.now()
        listOfPlayers.append(player)
        with open('players.json', 'w') as json_file:
            json.dump([b.__dict__ for b in listOfPlayers], json_file,default=default)
    if selection == "2":
        for p in listOfPlayers:
            print(f"{p.Namn} Nummer: {p.Jersey}  {p.Team}")


#coding=utf-8
from lxml import etree
import urllib.request
import csv
import xlwt

first_url = 'http://www.csres.com'
#def get_url(url,headers):
#获得工标网首页html
html = urllib.request.urlopen(first_url)
response = html.read().decode('gbk')
#将首页转换成etree格式
page = etree.HTML(response)
#在首页中找到'分类'、'新标准'、'标准公告'、'标准动态'、'论坛'、'文档'的URL
second_urls = page.xpath('//a[@class="a_top_blue"]/@href')
full_second_urls = []
for url_2 in second_urls[0:5]:
    second_url = first_url + str(url_2)
    full_second_urls.append(second_url)
full_second_urls.append(second_urls[5])
#解析'分类'、'新标准'、'标准公告'、'标准动态'、'论坛'、'文档'的html,将其转换为etree格式
classify_html = urllib.request.urlopen(full_second_urls[0])
classify_page = etree.HTML(classify_html.read().decode('gbk'))
#获得'分类'页面下的所有url
full_third_urls = []
third_urls = classify_page.xpath('//a[@class="sh14lian"]/@href')
#获得'分类'页面下的各url的文本
third_urls_text = classify_page.xpath('//a[@class="sh14lian"]/text()')
#循环获取url
for url_3 in third_urls:
    third_url = first_url + str(url_3)
    full_third_urls.append(third_url)
    #print(url_3)
#循环获取url的文本
full_third_urls_text = []
for url_3_text in third_urls_text:
    full_third_urls_text.append(url_3_text)
    #print(url_3_text)
del full_third_urls[23:25]
#得到第三个页面的url与text的对应关系，即字典
dic_third_page = dict(zip(full_third_urls_text,full_third_urls))
#print(dic_third_page)
#获得'A综合'下的url及各url的文本
A_html = urllib.request.urlopen(full_third_urls[0])
A_page = etree.HTML(A_html.read().decode('gbk'))
#获得'A综合'页面下的所有url
full_A_urls = []
A_urls = A_page.xpath('//a[@class="sh14lian"]/@href')
#获得'A综合'页面下的各url的文本
A_urls_text = A_page.xpath('//a[@class="sh14lian"]/text()')
#循环获取url
for url_A in A_urls:
    url_A = first_url + str(url_A)
    full_A_urls.append(url_A)
    #print(url_A)
#print(len(full_A_urls))
#循环获取url的文本
#del full_A_urls[29,43,44,49,50,56,64,65,70,71]
#print(len(full_A_urls))
full_A_urls_text = []
for url_A_text in A_urls_text:
    full_A_urls_text.append(url_A_text)
    #print(url_A_text)
#print(len(full_A_urls_text))
#del full_third_urls[23:25]
#得到'A综合'页面的url与text的对应关系，即字典
'''dic_A_page = dict(zip(full_A_urls_text,full_A_urls))
dic_A_page.pop('\xa0')'''

#print(dic_A_page)
#print(len(dic_A_page))
#获得标准列表的url
finally_html = urllib.request.urlopen(full_A_urls[0])
finally_page = etree.HTML(finally_html.read().decode('gbk'))
#获得标准列表的表头
list_head = []
list_heads = finally_page.xpath('//thead[@class="th1"]/tr/th/text()')
for item in list_heads:
    list_head.append(item)
#print(item)
#获得标准的标准编号、标准名称、ICS分类、发布部门、发布日期、实施日期
standard_list = []
standard_nums = finally_page.xpath('//thead/tr[@bgcolor="#FFFFFF"]/@title')
for standard_num in standard_nums:
    standard_list.append(standard_num)
#print(len(standard_list))
print(len(standard_list[0]))
#print(standard_list[0][115:125])
#print(standard_list)
#获得标准的标准状态

state_list = []
states = finally_page.xpath('//thead/tr[@bgcolor="#FFFFFF"]/td[last()]/font/text()')
for state in states:
    state_list.append(state)
    #print(state)
print(len(state_list))
#print(full_third_urls_text)
#print(len(full_third_urls_text))
#print(full_third_urls)
#print(len(full_third_urls))
#print(full_second_urls)
    #str(standard_list[0][3:19])

#将数据写入excel文件
workbook=xlwt.Workbook(encoding='utf-8')
booksheet=workbook.add_sheet('标准', cell_overwrite_ok=True)

DATAS = (('编号','标题','ICS分类','发布部门','发布日期','实施日期','状态'),
         ('DB21/T 2457-2015',standard_list[0][24:36],standard_list[0][54:63],standard_list[0][83:93],standard_list[0][99:110],standard_list[0][115:125],state_list[0]))
for i,row in enumerate(DATAS):
    for j,col in enumerate(row):
        booksheet.write(i,j,col)
workbook.save('grade.xls')


#!/usr/bin/env python

# spinal_cord_classification.py performs several types of machine learning techniques to explore the relationship between spinal cord shape and clinical outcomes in MS

import nibabel as nib
import numpy as np
import glob
import os
import sys
import csv
import scipy.io
import glob
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestCentroid
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.cross_decomposition import PLSRegression
from sklearn.metrics import confusion_matrix

# Visualize data with PCA
X_deformation = np.genfromtxt('deformation_data.csv',dtype='str', delimiter=',')
X_deformation = X_deformation[:,1:]
X_deformation = X_deformation.astype(float)
X_jacobian = np.genfromtxt('jacobian_data.csv',dtype='str', delimiter=',')
X_jacobian = X_jacobian[:,1:]
X_jacobian = X_jacobian.astype(float)
X_all = np.hstack((X_deformation, X_jacobian))
X_patients = X_all[32:,:]

Y = np.loadtxt('EDSS.csv',dtype='str',delimiter=',')
Y = Y[:,1:]
Y = Y.astype(float)
Y_disease_course = Y[:,1]
Y_disease_course_patients = (Y_disease_course>1.5)[32:]
Y_edss = Y[:,0]
Y_edss_severity = (Y_edss >= 3)
Y_patient = (Y_disease_course >0)

model = PCA(n_components=5)
X_new = model.fit_transform(X_all)
plt.scatter(X_new[:,0], X_new[:,1], c=Y_edss, cmap='hot')
plt.colorbar()
plt.show()
print(model.explained_variance_ratio_)

# Try centroid method
Y = Y_disease_course_patients[:,None]
X = X_new[32:,:]
YX = np.hstack((Y,X))
np.random.shuffle(YX)
X_shuffle = YX[:,1:]
Y_shuffle = YX[:,0]
Y_shuffle = np.squeeze(Y_shuffle)

halfway_pt = 65
Xtrain = X_shuffle[0:halfway_pt,:]
Ytrain = Y_shuffle[0:halfway_pt]
Xvalidate = X_shuffle[halfway_pt:,:]
Yvalidate = Y_shuffle[halfway_pt:]

model = NearestCentroid()
model.fit(Xtrain, Ytrain)
Ypredict = model.predict(Xvalidate)

confusion_matrix(Yvalidate, Ypredict)

accuracy = sum(np.equal(Ypredict,Yvalidate))/float(len(Yvalidate))
print(accuracy)

# Try SVM
iterations = 1000
confusion_percentages = np.zeros((2,2,iterations))
for i in range(0,iterations):
    Y = Y_edss_severity[:,None]
    X = X_all
    YX = np.hstack((Y,X))
    np.random.shuffle(YX)
    X_shuffle = YX[:,1:]
    Y_shuffle = YX[:,0]
    Y_shuffle = np.squeeze(Y_shuffle)

    halfway_pt = 80
    Xtrain = X_shuffle[0:halfway_pt,:]
    Ytrain = Y_shuffle[0:halfway_pt]
    Xvalidate = X_shuffle[halfway_pt:,:]
    Yvalidate = Y_shuffle[halfway_pt:]

    model = SVC()
    model.fit(Xtrain, Ytrain)
    Ypredict = model.predict(Xvalidate)

    confusion = confusion_matrix(Yvalidate, Ypredict)
    confusion_percentage = np.divide(confusion, np.sum(confusion,0).astype(float))
    confusion_percentages[:,:,i] = confusion_percentage

avg_confusion = np.mean(confusion_percentages, axis=2)
print(avg_confusion)

# Try PLS

iterations = 1000
confusion_percentages = np.zeros((2,2,iterations))
for i in range(0,iterations):
    Y = Y_edss_severity[:,None]
    X = X_all
    YX = np.hstack((Y,X))
    np.random.shuffle(YX)
    X_shuffle = YX[:,1:]
    Y_shuffle = YX[:,0]
    Y_shuffle = np.squeeze(Y_shuffle)

    halfway_pt = 80
    Xtrain = X_shuffle[0:halfway_pt,:]
    Ytrain = Y_shuffle[0:halfway_pt]
    Xvalidate = X_shuffle[halfway_pt:,:]
    Yvalidate = Y_shuffle[halfway_pt:]

    model = PLSRegression(n_components = 5)
    model.fit(Xtrain, Ytrain)
    Ypredict = model.predict(Xvalidate)
    Ypredict = np.squeeze(Ypredict)
    Ypredict = (Ypredict >= 0.5)
    Yvalidate = Yvalidate.astype(int)
    confusion = confusion_matrix(Yvalidate, Ypredict)
    confusion_percentage = np.divide(confusion, np.sum(confusion,0).astype(float))
    confusion_percentages[:,:,i] = confusion_percentage

    accuracy = sum(Ypredict == Yvalidate)/float(len(Yvalidate))

avg_confusion = np.mean(confusion_percentages, axis=2)
print(avg_confusion)


# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json

import yaml

if hasattr(yaml, 'CSafeLoader'):
    yaml_loader = yaml.CSafeLoader
else:
    yaml_loader = yaml.SafeLoader

class HeatYamlLoader(yaml_loader):
    pass

def _construct_yaml_str(self, node):
    # Override the default string handling function
    # to always return unicode objects
    return self.construct_scalar(node)

HeatYamlLoader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
# Unquoted dates like 2013-05-23 in yaml files get loaded as objects of type
# datetime.data which causes problems in API layer when being processed by
# openstack.common.jsonutils. Therefore, make unicode string out of timestamps
# until jsonutils can handle dates.
HeatYamlLoader.add_constructor(
    u'tag:yaml.org,2002:timestamp', _construct_yaml_str
)

def parse(tmpl_str):
    """Takes a string and returns a dict containing the parsed structure.

    This includes determination of whether the string is using the
    JSON or YAML format.
    """
    # strip any whitespace before the check
    tmpl_str = tmpl_str.strip()
    if tmpl_str.startswith('{'):
        tpl = json.loads(tmpl_str)
    else:
        try:
            tpl = yaml.load(tmpl_str, Loader=HeatYamlLoader)
        except yaml.YAMLError:
            # NOTE(prazumovsky): we need to return more informative error for
            # user, so use SafeLoader, which return error message with template
            # snippet where error has been occurred.
            try:
                tpl = yaml.load(tmpl_str, Loader=yaml.SafeLoader)
            except yaml.YAMLError as yea:
                raise ValueError(yea)
        else:
            if tpl is None:
                tpl = {}
    # Looking for supported version keys in the loaded template
    if not (
        'HeatTemplateFormatVersion' in tpl
        or 'heat_template_version' in tpl
        or 'AWSTemplateFormatVersion' in tpl
    ):
        raise ValueError("Template format version not found.")
    return tpl


# Erika Jane T. Reyes
# Testing the finished BinarySearchTreeNode class with my full name as the content of the binary tree

class BinarySearchTreeNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    def add_child(self, data):
        if data == self.data:
            return

        if data < self.data:
            if self.left:
                self.left.add_child(data)
            else:
                self.left = BinarySearchTreeNode(data)

        else:
            if self.right:
                self.right.add_child(data)
            else:
                self.right = BinarySearchTreeNode(data)

    def in_order_traversal(self):
        elements = []

        if self.left:
            elements += self.left.in_order_traversal()

        elements.append(self.data)

        if self.right:
            elements += self.right.in_order_traversal()

        return elements

    def search(self, val):
        if self.data == val:
            return True

        if val < self.data:
            if self.left:
                return self.left.search(val)
            else:
                return False

        if val > self.data:
            if self.right:
                return self.right.search(val)
            else:
                return False

    def pre_order_traversal(self):
        elements = [self.data]

        if self.left:
            elements += self.left.pre_order_traversal()

        if self.right:
            elements += self.right.pre_order_traversal()

        return elements

    def post_order_traversal(self):
        elements = []

        if self.left:
            elements += self.left.post_order_traversal()

        if self.right:
            elements += self.right.post_order_traversal()

        elements.append(self.data)

        return elements

    def find_min(self):
        if self.left is None:
            return self.data
        return self.left.find_min()

    def find_max(self):
        if self.right is None:
            return self.data
        return self.right.find_max()

    def calculate_sum(self):
        left_sum = self.left.calculate_sum() if self.left else 0
        right_sum = self.right.calculate_sum() if self.right else 0
        return self.data + left_sum + right_sum

    def delete(self, val):
        if val < self.data:
            if self.left:
                self.left = self.left.delete(val)

        elif val > self.data:
            if self.right:
                self.right = self.right.delete(val)

        else:
            if self.left is None and self.right is None:
                return None
            if self.left is None:
                return self.right
            if self.right is None:
                return self.right

            max_val = self.left.find_max()
            self.data = max_val
            self.left = self.left.delete(max_val)

        return self

def build_tree(elements):
    print("Building tree with these elements:", elements)
    root = BinarySearchTreeNode(elements[0])

    for i in range(1, len(elements)):
        root.add_child(elements[i])

    return root

if __name__ == '__main__':
    strings = ["E", "R", "I", "K", "A", "J", "A", "N", "E", "T", "R", "E", "Y", "E", "S"]
    strings_tree = build_tree(strings)
    print('\n')

    # Testing the search() method
    print("Letter E is in the list?", strings_tree.search("E"))
    print("Letter K is in the list?", strings_tree.search("K"))
    print("Letter Q is in the list?", strings_tree.search("Q"))
    print('\n')

    # Testing the find_min() method
    print("Mininum element:", strings_tree.find_min())
    print('\n')

    # Testing the find_max() method
    print("Maximum element:", strings_tree.find_max())
    print('\n')

    # Testing the in_order_traversal() method
    print("In order traversal:", strings_tree.in_order_traversal())
    print('\n')

    # Testing the pre_order_traversal() method
    print("Pre order traversal:", strings_tree.pre_order_traversal())
    print('\n')

    # Testing the post_order_traversal() method
    print("Post order traversal:", strings_tree.post_order_traversal())
    print('\n')

    # Testing the delete() method
    strings_tree = build_tree(strings)
    strings_tree.delete("E")
    print("After deleting letter E:", strings_tree.in_order_traversal())

    print("\n")

    strings_tree = build_tree(strings)
    strings_tree.delete("J")
    print("After deleting letter J:", strings_tree.in_order_traversal())

    print("\n")

    strings_tree = build_tree(strings)
    strings_tree.delete("Y")
    print("After deleting letter Y:", strings_tree.in_order_traversal())


__author__ = 'Joao'

import SolFB._Experience as _Experience
import SolFB._Page as _Page
import SolFB._FacebookUser as _User

class Education_Experience:
     def __init__(self,id="", dictionary=dict()):
         '''
         Reference: https://developers.facebook.com/docs/graph-api/reference/education-experience/
         '''
         self.id=id
         self.classes=list()
         self.concentration=list()
         self.degree=""
         self.school=""
         self.type=""
         self.with_=list()
         self.year=""
         if ("id" in dictionary):
             self.id=dictionary["id"]
         if ("classes" in dictionary):
             for experience in dictionary["classes"]:
                 self.classes.append(_Experience.Experience(dictionary=dictionary["classes"]))
         if ("concentration" in dictionary):
             for page in dictionary["concentration"]:
                 self.concentration.append(_Page.Page(dictionary=dictionary["concentration"]))
         if ("degree" in dictionary):
             self.degree=_Page.Page(dictionary=dictionary["degree"])
         if ("school" in dictionary):
             self.school=_Page.Page(dictionary=dictionary["school"])
         if ("type" in dictionary):
             self.type=dictionary["type"]
         if ("with" in dictionary):
             for user in dictionary["with"]:
                 self.with_.append(_User.FacebookUser(dictionary=dictionary["with"]))
         if ("year" in dictionary):
             self.year=_Page.Page(dictionary=dictionary["year"])

     def __str__(self):
         #print(self.__dict__)
         dic=self.__dict__
         dict={}

         for key in dic:
             if not(dic[key]==None or dic[key]==""):
                 dict[key]=dic[key]
         return "EDUCATION_EXPERIENCE: "+str(dict)