import numpy as np

class SubScreen():
    def __init__(self, x, y, width, height, curses):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.curses = curses

    def put_char(self, x, y, char=' ', foreground='white', background='transparent'):
        if x < self.width and x >= self.x and y < self.height and y >= self.y:
            self.curses.put_char(self.x + x, self.y + y, char, foreground, background)
        else:
            raise ValueError('Error: Out of SubScreen boundary.')

    def put_message(self, x, y , message, foreground='white', background='transparent', auto=True, align='left'):
        self.curses.put_message(self.x + x, self.y + y , message, foreground, background, auto, align, box_x=self.x, box_y=self.y, box_width=self.width, box_height=self.height)

    def fill_char(self, char=' ', foreground='white', background='transparent'):
        for i in range(self.x, self.x + self.width):
            for j in range(self.y, self.y + self.height):
                self.curses.put_char(i, j, char, foreground, background)

class MessageScreen(SubScreen):
    def __init__(self, x, y, width, height, curses):
        super(MessageScreen, self).__init__(x, y, width, height, curses)
        self.initialization()

    def initialization(self):
        self.message_id = 1
        self.message_size = self.height
        self.message_storage = ['']*self.message_size
        self.color_storage = ['transparent']*self.message_size
        self.idx_storage = ['']*self.message_size

    def add_message(self, message, color='white'):
        idx = '[%d] '%(self.message_id)
        message = message
        self.message_id += 1
        self.message_storage.append(message)
        self.color_storage.append(color)
        self.idx_storage.append(idx)

        self.message_storage.pop(0)
        self.color_storage.pop(0)
        self.idx_storage.pop(0)

    def draw(self):
        self.fill_char()
        for i in range(len(self.message_storage)):
            self.put_message(0, i, self.idx_storage[i], foreground='white', background='transparent', auto=True, align='left')
            self.put_message(len(self.idx_storage[i]), i , self.message_storage[i], foreground=self.color_storage[i], background='transparent', auto=True, align='left')

class PlayerInfoScreen(SubScreen):
    def __init__(self, x, y, width, height, curses, player):
        super(PlayerInfoScreen, self).__init__(x, y, width, height, curses)
        self.player = player
        self.initialization()

    def initialization(self):
        self.full_health_bar_length = 15
        self.draw()

    def draw(self):
        # Draw background
        self.fill_char(char='█', foreground='peru', background='transparent')
        # Draw HP bar
        health = self.player.current_health
        interval = self.player.health / self.full_health_bar_length / 3
        level = int(np.ceil(health / interval))

        health_title = 'HP '

        if level % 3 == 0:
            remainder = ''
        elif level % 3 == 1:
            remainder = '░'
        elif level % 3 == 2:
            remainder = '▒'

        health_message = '█' * int((level - level%3)/3) + remainder
        self.put_message(0, 0, health_title, foreground='red', background='peru', auto=True, align='left')
        self.put_message(len(health_title), 0, ' '*self.full_health_bar_length, foreground='red', background='transparent', auto=True, align='left')
        self.put_message(len(health_title), 0, health_message, foreground='red', background='transparent', auto=True, align='left')


import sys
import RPi.GPIO as GPIO
import time
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.options
import urllib2
from tornado.options import define,options
define("port",321,type=int)
IN1 = 11
IN2 = 12
IN3 = 13
IN4 = 15
def init():
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(IN1,GPIO.OUT)
        GPIO.setup(IN2,GPIO.OUT)
        GPIO.setup(IN3,GPIO.OUT)
        GPIO.setup(IN4,GPIO.OUT)
def forward(tf):
        GPIO.output(IN1,GPIO.HIGH)
        GPIO.output(IN2,GPIO.LOW)
        GPIO.output(IN3,GPIO.HIGH)
        GPIO.output(IN4,GPIO.LOW)
        print("forward")
        time.sleep(tf)
        GPIO.cleanup()
def backOff(tf):
        GPIO.output(IN1,GPIO.LOW)
        GPIO.output(IN2,GPIO.HIGH)
        GPIO.output(IN3,GPIO.LOW)
        GPIO.output(IN4,GPIO.HIGH)
        print("backOff")
        time.sleep(tf)
        GPIO.cleanup()
def left(tf):
        GPIO.output(IN1,GPIO.LOW)
        GPIO.output(IN2,GPIO.LOW)
        GPIO.output(IN3,GPIO.HIGH)
        GPIO.output(IN4,GPIO.LOW)
        print("left")
        time.sleep(tf)
        GPIO.cleanup()
def right(tf):
        GPIO.output(IN1,GPIO.HIGH)
        GPIO.output(IN2,GPIO.LOW)
        GPIO.output(IN3,GPIO.LOW)
        GPIO.output(IN4,GPIO.LOW)
        print("right")
        time.sleep(tf)
        GPIO.cleanup()
class IndexHandler(tornado.web.RequestHandler):
        def get(self):
                self.render("index.html")
        def post(self):
                init()
                sleep_time = 0.1
                arg = self.get_argument('k')
                if(arg=='w'):
                        forward(sleep_time)
                        url = ("http://192.168.0.101:5000/forward")
                        urllib2.urlopen(url)
                elif(arg=='s'):
                        backOff(sleep_time)
                        url = ("http://192.168.0.101:5000/backOff")
                        urllib2.urlopen(url)
                elif(arg=='a'):
                        left(sleep_time)
                        url = ("http://192.168.0.101:5000/left")
                        urllib2.urlopen(url)
                elif(arg=='d'):
                        right(sleep_time)
                        url = ("http://192.168.0.101:5000/right")
                        urllib2.urlopen(url)
                else:
                        return False
                self.write(arg)
if __name__ == '__main__':
        tornado.options.parse_command_line()
        app = tornado.web.Application(handlers=[(r"/",IndexHandler)])
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(options.port)
        tornado.ioloop.IOLoop.instance().start()


# Qingxiang Guo
# {2022/5/6} {15:38}

#分支结构，单分支结构，条件表达式
money=1000 #余额
s=int(input('请输入取款金额'))
#判断余额是否充足
if money>=s:
    money-=s
    print('取款成功，您的余额为，',   money)


from odoo import fields, models, tools, Command, _
from odoo.tools.misc import clean_context

class MailingContactImport(models.TransientModel):
    _name = 'mailing.contact.import'
    _description = 'Mailing Contact Import'

    mailing_list_ids = fields.Many2many('mailing.list', string='Lists')
    contact_list = fields.Text('Contact List', help='Contact list that will be imported, one contact per line')

    def action_import(self):
        """Import each lines of "contact_list" as a new contact."""
        self.ensure_one()
        contacts = tools.email_split_tuples(', '.join((self.contact_list or '').splitlines()))
        if not contacts:
            return {
                'type': 'ir.actions.client',
                'tag': 'display_notification',
                'params': {
                    'message': _('No valid email address found.'),
                    'next': {'type': 'ir.actions.act_window_close'},
                    'sticky': False,
                    'type': 'warning',
                }
            }

        if len(contacts) > 5000:
            return {
                'type': 'ir.actions.client',
                'tag': 'display_notification',
                'params': {
                    'message': _('You have to much emails, please upload a file.'),
                    'type': 'warning',
                    'sticky': False,
                    'next': self.action_open_base_import(),
                }
            }

        all_emails = list({values[1].lower() for values in contacts})

        existing_contacts = self.env['mailing.contact'].search([
            ('email_normalized', 'in', all_emails),
            ('list_ids', 'in', self.mailing_list_ids.ids),
        ])
        existing_contacts = {
            contact.email_normalized: contact
            for contact in existing_contacts
        }

        # Remove duplicated record, keep only the first non-empty name for each email address
        unique_contacts = {}
        for name, email in contacts:
            email = email.lower()
            if unique_contacts.get(email, {}).get('name'):
                continue

            if email in existing_contacts and not self.mailing_list_ids < existing_contacts[email].list_ids:
                existing_contacts[email].list_ids |= self.mailing_list_ids
            if email not in existing_contacts:
                unique_contacts[email] = {
                    'name': name,
                    'subscription_ids': [
                        Command.create({'list_id': mailing_list_id.id})
                        for mailing_list_id in self.mailing_list_ids
                    ],
                }

        if not unique_contacts:
            return {
                'type': 'ir.actions.client',
                'tag': 'display_notification',
                'params': {
                    'message': _('No contacts were imported. All email addresses are already in the mailing list.'),
                    'next': {'type': 'ir.actions.act_window_close'},
                    'sticky': False,
                    'type': 'warning',
                }
            }

        new_contacts = self.env['mailing.contact'].with_context(clean_context(self.env.context)).create([
            {
                'email': email,
                **values,
            }
            for email, values in unique_contacts.items()
        ])

        ignored = len(contacts) - len(unique_contacts)

        return {
            'type': 'ir.actions.client',
            'tag': 'display_notification',
            'params': {
                'message': (
                    _('%i Contacts have been imported.', len(unique_contacts))
                    + (_(' %i duplicates have been ignored.', ignored) if ignored else '')
                ),
                'type': 'success',
                'sticky': False,
                'next': {
                    'context': self.env.context,
                    'domain': [('id', 'in', new_contacts.ids)],
                    'name': _('New contacts imported'),
                    'res_model': 'mailing.contact',
                    'type': 'ir.actions.act_window',
                    'view_mode': 'list',
                    'views': [[False, 'list'], [False, 'form']],
                },
            }
        }

    def action_open_base_import(self):
        """Open the base import wizard to import mailing list contacts with a xlsx file."""
        self.ensure_one()

        return {
            'type': 'ir.actions.client',
            'tag': 'import',
            'name': _('Import Mailing Contacts'),
            'params': {
                'context': self.env.context,
                'model': 'mailing.contact',
            }
        }


# coding=gbk
import requests
import xlrd
import os
import xlwt
from requests.exceptions import RequestException
from bs4 import BeautifulSoup

wbook = xlwt.Workbook(encoding='utf-8',style_compression = 0)
wtable = wbook.add_sheet('sheet1',cell_overwrite_ok = True)

count = 0
count = int(count)
def get_title(url):

    global count
    try:

        # res = requests.get(url)
        proxies = {
            'http': 'http://127.0.0.1:8080'
        }
        headers = {

            'Connection' : 'Keep-Alive',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',

        }
        requests.packages.urllib3.disable_warnings()
        res = requests.get(url, headers=headers, timeout=5, verify=False)
        res.encoding = res.apparent_encoding

        # res.encoding = 'utf-8'  #
        soup = BeautifulSoup(res.text, 'lxml')
        wtable.write(count, 0, url)
        wtable.write(count, 1, res.status_code)
        if soup.title is not None:
            wtable.write(count, 2, soup.title.text)
        count = count + 1
        print(res.status_code)
        if soup.title is not None:
            print(soup.title.text)
    except RequestException:
        wtable.write(count, 0, url)
        wtable.write(count, 1, '访问超时')
        count = count + 1
        print( url+':'+'无法访问')
        return None

# get_title('https://')
def gobackcode(file):
    with open(file, 'r') as f:
        for line in f.readlines():
            line = line.strip('\n')
            get_title(line)

if __name__ == '__main__':
    if os.path.exists('./done.xls'):
        os.remove('./done.xls')
    gobackcode('./url.txt')
    wbook.save(r'./done.xls')


import os
from unittest.mock import patch

from app.twitter_learning_journal.retrievers.twitter.api import get_api, get_auth_handler

@patch('app.twitter_learning_journal.retrievers.twitter.api.get_auth_handler')
@patch('app.twitter_learning_journal.retrievers.twitter.api.tweepy')
def test_get_api(mock_tweepy, mock_get_auth_handler):
    api = get_api()

    mock_tweepy.API.assert_called_with(mock_get_auth_handler.return_value,
                                       wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

    assert mock_tweepy.API.return_value == api

@patch('app.twitter_learning_journal.retrievers.twitter.api.tweepy')
def test_get_auth_handler(mock_tweepy):
    os.environ['TWITTER_CONSUMER_KEY'] = 'twitter_consumer_key'
    os.environ['TWITTER_CONSUMER_SECRET'] = 'twitter_consumer_secret'
    os.environ['TWITTER_ACCESS_TOKEN'] = 'twitter_access_token'
    os.environ['TWITTER_TOKEN_SECRET'] = 'twitter_token_secret'

    auth_handler = get_auth_handler()

    mock_tweepy.OAuthHandler.assert_called_with('twitter_consumer_key', 'twitter_consumer_secret')
    mock_tweepy.OAuthHandler.return_value.set_access_token.assert_called_with('twitter_access_token',
                                                                              'twitter_token_secret')

    assert mock_tweepy.OAuthHandler.return_value == auth_handler


import time
import datetime
import socket
import pkg_resources
import OpenSSL
from OpenSSL.SSL import WantReadError
import apnsclient
from rockpack.mainsite import app

def _refresh(self):
    """ Ensure socket is still alive. Reopen if needed. """
    if self._socket is None:
        try:
            self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.configure_socket()

            self._connection = OpenSSL.SSL.Connection(self._certificate.get_context(), self._socket)
            self.configure_connection()
            self._connection.connect(self._address)
            tries = 0
            while True:
                try:
                    self._connection.do_handshake()
                    break
                except WantReadError:
                    tries += 1
                    if tries >= 10:
                        raise
                    time.sleep(0.1)
        except Exception:
            self.close()
            raise

    self._readbuf = ""
    self._feedbackbuf = ""
    self._last_refresh = datetime.datetime.now()

apnsclient.Connection.refresh = _refresh

push_client = apnsclient

session = push_client.Session()

_con = session.get_connection(
    app.config['APNS_PUSH_TYPE'],
    cert_file=pkg_resources.resource_filename(__name__, app.config['APNS_CERT_NAME']),
    passphrase=app.config['APNS_PASSPHRASE']
)

def _feedback_con():
    return session.new_connection(
        app.config['APNS_FEEDBACK_TYPE'],
        cert_file=pkg_resources.resource_filename(__name__, app.config['APNS_CERT_NAME']),
        passphrase=app.config['APNS_PASSPHRASE']
    )

push_client.connection = _con
push_client.get_feedback_connection = _feedback_con


import asyncio
import json
import os
import shutil
import stat
import sys
import tempfile
from argparse import ArgumentParser, Namespace
from contextlib import contextmanager
from datetime import datetime
from pathlib import Path
from typing import Generator, Iterable, List, NoReturn, Optional, Union
from zipfile import ZipFile

import httpx
from pontos.github.actions.core import ActionIO, Console
from pontos.github.actions.env import GitHubEnvironment
from pontos.github.api import GitHubAsyncRESTApi
from pontos.github.models import Artifact, WorkflowRun, WorkflowRunStatus

def is_event(run: WorkflowRun, events: Iterable[str]) -> bool:
    """
    Return True if workflow run event is one of the events
    """
    return run.event.value in events

def created_at(run: WorkflowRun) -> datetime:
    return run.created_at

def parse_list(value: str) -> List[str]:
    """
    Parse a csv line into a list of strings.

    Spaces are stripped and removed.
    """
    values = value.split(",")
    values = (value.strip() for value in values)
    return [value for value in values if value]

def parse_int(value: str) -> Optional[int]:
    try:
        return int(value)
    except (ValueError, TypeError):
        return None

@contextmanager
def temp_directory() -> Generator[Path, None, None]:
    """
    Context Manager to create a temporary directory
    """
    temp_dir = tempfile.TemporaryDirectory()
    dir_path = Path(temp_dir.name)
    try:
        yield dir_path
    finally:
        temp_dir.cleanup()

def parse_arguments() -> Namespace:
    parser = ArgumentParser()
    parser.add_argument("--token", required=True)
    parser.add_argument("--repository", nargs="?")
    parser.add_argument("--workflow", required=True)
    parser.add_argument("--workflow-events", nargs="?")
    parser.add_argument("--branch", required=True)
    parser.add_argument("--name", nargs="?")
    parser.add_argument("--allow-not-found")
    parser.add_argument("--path", required=True)
    parser.add_argument("--user", nargs="?")
    parser.add_argument("--group", nargs="?")
    return parser.parse_args()

class DownloadArtifactsError(Exception):
    pass

class DownloadArtifacts:
    def __init__(
        self,
        *,
        token: Optional[str] = None,
        workflow: Optional[str] = None,
        workflow_events: Optional[str] = None,
        repository: Optional[str] = None,
        branch: Optional[str] = None,
        name: Optional[str] = None,
        path: Optional[str] = None,
        allow_not_found: Optional[str] = None,
        user: Union[str, int] = None,
        group: Union[str, int] = None,
    ) -> None:
        env = GitHubEnvironment()

        token = token or ActionIO.input("token")
        if not token:
            raise DownloadArtifactsError("Missing token.")

        self.workflow = workflow or ActionIO.input("workflow")
        if not self.workflow:
            raise DownloadArtifactsError("Missing workflow.")

        workflow_events = workflow_events or ActionIO.input("workflow-events")
        if not workflow_events:
            self.workflow_events = ["schedule", "workflow_dispatch"]
        else:
            self.workflow_events = parse_list(workflow_events)

        self.branch = branch or ActionIO.input("branch")
        if not self.branch:
            raise DownloadArtifactsError("Missing branch.")

        self.repository = (
            repository or ActionIO.input("repository") or env.repository
        )
        if not self.repository:
            raise DownloadArtifactsError("Missing repository.")

        self.name = name or ActionIO.input("name")

        download_path = path or ActionIO.input("path")
        if not download_path:
            raise DownloadArtifactsError("Missing path.")

        self.user = parse_int(user or ActionIO.input("user"))
        self.group = parse_int(group or ActionIO.input("group"))

        self.download_path = Path(download_path).absolute()

        allow_not_found = allow_not_found or ActionIO.input("allow-not-found")
        self.allow_not_found = allow_not_found == "true"

        self.is_debug = env.is_debug

        self.api = GitHubAsyncRESTApi(token)

        with Console.group("Settings"):
            Console.log(f"repository: {self.repository}")
            Console.log(f"workflow: {self.workflow}")
            Console.log(f"workflow-events: {self.workflow_events}")
            Console.log(f"branch: {self.branch}")
            Console.log(f"name: {self.name}")
            Console.log(f"path: {self.download_path}")
            Console.log(f"allow-not-found: {self.allow_not_found}")
            Console.log(f"user: {self.user}")
            Console.log(f"group: {self.group}")

    async def get_newest_workflow_run(self) -> Optional[WorkflowRun]:
        try:
            runs = [
                run
                async for run in self.api.workflows.get_workflow_runs(
                    self.repository,
                    self.workflow,
                    branch=self.branch,
                    status=WorkflowRunStatus.SUCCESS,
                    exclude_pull_requests=True,
                )
                if not self.workflow_events
                or is_event(run, self.workflow_events)
            ]
        except httpx.HTTPStatusError as e:
            if self.allow_not_found and e.response.status_code == 404:
                return None

            raise DownloadArtifactsError(f"Could not find workflow. {e}") from e

        if self.is_debug:
            urls = "\n".join([run.html_url for run in runs])
            Console.debug(
                f"Workflow runs for events {self.workflow_events}:\n{urls}"
            )

        if not runs:
            return None

        # ensure that newest run is run[0]
        runs = sorted(runs, key=created_at, reverse=True)

        return runs[0]

    def adjust_permissions(self, file_path: Path) -> None:
        try:
            # should be made configurable via a input variable
            stat_result = os.stat(file_path)
            os.chmod(
                file_path,
                stat_result.st_mode
                | stat.S_IWUSR
                | stat.S_IRUSR
                | stat.S_IWGRP
                | stat.S_IRGRP
                | stat.S_IWOTH
                | stat.S_IROTH,
            )
        except OSError as e:
            Console.warning(
                f"Could not change permissions of '{file_path}'. Error was {e}."
            )

        if self.user:
            try:
                shutil.chown(file_path, self.user)
            except (OSError, LookupError) as e:
                Console.warning(
                    f"Could not change owner of '{file_path}' to user "
                    f"'{self.user}'. Error was {e}."
                )

        if self.group:
            try:
                shutil.chown(file_path, None, self.group)
            except (OSError, LookupError) as e:
                Console.warning(
                    f"Could not change owner of '{file_path}' to group "
                    f"'{self.group}'. Error was {e}."
                )

    async def download_artifact(self, artifact: Artifact) -> Optional[Artifact]:
        if self.name and self.name != artifact.name:
            Console.log(
                f"Skipping artifact '{artifact.name} with ID {artifact.id}' "
                f"because it does not match {self.name}."
            )
            return None

        with temp_directory() as temp_dir:
            temp_file = temp_dir / f"{artifact.name}.zip"

            if self.name:
                destination_dir = self.download_path
            else:
                destination_dir = self.download_path / artifact.name

            destination_dir.mkdir(parents=True, exist_ok=True)
            self.adjust_permissions(destination_dir)

            print(
                f"Downloading artifact '{artifact.name}' with ID {artifact.id}",
                end=" ",
            )

            with temp_file.open("wb") as f:
                try:
                    async with self.api.artifacts.download(
                        self.repository, artifact.id
                    ) as download:
                        async for content, _ in download:
                            f.write(content)
                            print(".", end="")
                except httpx.HTTPStatusError as e:
                    raise DownloadArtifactsError(
                        f"HTTP Error {e}: Failed to download '{artifact.name}' with ID "
                        f"{artifact.id}"
                    ) from e
                finally:
                    # add a newline to the print output
                    print()

                Console.log(
                    f"Extracting artifact '{artifact.name}' to "
                    f"'{destination_dir.absolute()}'."
                )

                f.flush()

                zipfile = ZipFile(temp_file)
                for zipinfo in zipfile.infolist():
                    file_path = destination_dir / zipinfo.filename

                    if self.is_debug:
                        Console.debug(f"Extracting '{file_path.absolute()}'")

                    zipfile.extract(zipinfo, destination_dir)
                    self.adjust_permissions(file_path)

            return artifact

    async def run(self) -> None:
        if self.name:
            Console.log(
                f"Download '{self.name}' artifact of workflow '{self.workflow}'"
                f" in repo '{self.repository}' using branch '{self.branch}' to "
                f"'{self.download_path}' 🚀."
            )
        else:
            Console.log(
                f"Download artifacts of workflow '{self.workflow}' in repo "
                f"'{self.repository}' using branch '{self.branch}' to "
                f"'{self.download_path}' 🚀."
            )

        async with self.api:
            run = await self.get_newest_workflow_run()

            if not run:
                if self.allow_not_found:
                    Console.log("No workflow run found.")
                    return
                else:
                    raise DownloadArtifactsError("No workflow run found.")

            Console.log(f"Using workflow run with ID {run.id} {run.html_url}")

            try:
                tasks = [
                    asyncio.create_task(self.download_artifact(artifact))
                    async for artifact in self.api.artifacts.get_workflow_run_artifacts(
                        self.repository, run.id
                    )
                ]
            except httpx.HTTPStatusError as e:
                raise DownloadArtifactsError(
                    f"Could not find workflow run artifacts. {e}"
                ) from e

            artifacts = []
            for task in asyncio.as_completed(tasks):
                artifact = await task
                if artifact:
                    artifacts.append(artifact.name)

            if not artifacts:
                if self.allow_not_found:
                    Console.log("No artifact found.")
                    return
                else:
                    raise DownloadArtifactsError(
                        f"No artifact found for workflow run '{run.id}' in "
                        f"repo '{self.repository}' for workflow "
                        f"'{self.workflow}' and branch '{self.branch}'."
                    )

        ActionIO.output("downloaded-artifacts", json.dumps(artifacts))
        ActionIO.output("total-downloaded-artifacts", len(artifacts))

        Console.log("Downloading artifacts completed successfully ✅.")

def main() -> NoReturn:
    args = parse_arguments()
    try:
        asyncio.run(
            DownloadArtifacts(
                token=args.token,
                repository=args.repository,
                workflow=args.workflow,
                workflow_events=args.workflow_events,
                branch=args.branch,
                name=args.name,
                allow_not_found=args.allow_not_found,
                path=args.path,
                user=args.user,
                group=args.group,
            ).run()
        )
        sys.exit(0)
    except DownloadArtifactsError as e:
        Console.error(f"{e} ❌.")
        sys.exit(1)

if __name__ == "__main__":
    main()


import click
import logging
import lmctl.cli.ctlmgmt as ctlmgmt
from lmctl.cli.safety_net import lm_driver_safety_net
from lmctl.cli.format import determine_format_class, TableFormat
from lmctl.utils.certificates import read_certificate_file

logger = logging.getLogger(__name__)

@click.group(hidden=True, short_help='Use "lmctl create/get/delete resourcedriver"', help='deprecated in v3.0: Commands for managing VIM drivers (CP4NA orchestration 2.1 only)')
def vimdriver():
    click.echo('WARNING: vimdriver command support should only be used with 2.1 versions of CP4NA orchestration')

def get_vim_driver_mgmt_driver(environment_name, config_path, pwd):
    lm_session = ctlmgmt.create_lm_session(environment_name, pwd, config_path)
    return lm_session.vim_driver_mgmt_driver

def format_vim_driver(output_format, vim_driver):
    formatter_class = determine_format_class(output_format)
    if formatter_class is TableFormat:
        formatter = formatter_class(vim_driver_headers, vim_driver_row_processor)
    else:
        formatter = formatter_class()
    result = formatter.convert_element(vim_driver)
    return result

@vimdriver.command(help='Add a VIM driver to CP4NA orchestration')
@click.argument('environment')
@click.option('--config', default=None, help='configuration file')
@click.option('--pwd', '--api-key', default=None, help='password/api_key used for authenticating with CP4NA orchestration. Only required if the environment is secure and a username has been included in your configuration file with no password (api_key when using auth_mode=zen)')
@click.option('--type', 'inf_type', default='Openstack', help='Infrastructure type of the VIM driver to add')
@click.option('--url', help='url of VIM driver to add')
@click.option('--certificate', help='filename of a file containing the public certificate of the VIM driver')
@click.option('-f', '--format', 'output_format', default='table', help='format of output [table, yaml, json]')
def add(environment, config, pwd, inf_type, url, certificate, output_format):
    """Add a VIM driver"""
    vim_mgmt_driver = get_vim_driver_mgmt_driver(environment, config, pwd)
    new_vim_driver = {
        'infrastructureType': inf_type,
        'baseUri': url
    }

    if certificate is not None:
        try:
            new_vim_driver['certificate'] = read_certificate_file(certificate)
        except IOError as e:
            click.echo('Error: reading certificate: {0}'.format(str(e)), err=True)
            exit(1)

    with lm_driver_safety_net():
        vim_driver = vim_mgmt_driver.add_vim_driver(new_vim_driver)
    click.echo(format_vim_driver(output_format, vim_driver))

@vimdriver.command(help='Remove a VIM driver from CP4NA orchestration by ID (or by infrastructure type)')
@click.argument('environment')
@click.argument('driver-id', required=False)
@click.option('--config', default=None, help='configuration file')
@click.option('--pwd', '--api-key', default=None, help='password/api_key used for authenticating with CP4NA orchestration. Only required if the environment is secure and a username has been included in your configuration file with no password (api_key when using auth_mode=zen)')
@click.option('--type', 'inf_type', help='Infrastructure type used to identify the VIM driver to remove. Use this instead of the driver-id argument')
def delete(environment, driver_id, config, pwd, inf_type):
    """Remove a VIM driver by ID or type"""
    vim_mgmt_driver = get_vim_driver_mgmt_driver(environment, config, pwd)
    if driver_id is None:
        if inf_type is None:
            click.echo('Error: Must specify driver-id argument or type option')
            exit(1)
        with lm_driver_safety_net():
            vim_driver = vim_mgmt_driver.get_vim_driver_by_type(inf_type)
        driver_id = vim_driver['id']
        click.echo('Found VIM driver matching type \'{0}\'. Id: {1}'.format(inf_type, driver_id))
    click.echo('Deleting VIM driver: {0}...'.format(driver_id))
    with lm_driver_safety_net():
        vim_mgmt_driver.delete_vim_driver(driver_id)
    click.echo('Deleted VIM driver: {0}'.format(driver_id))

@vimdriver.command(help='Get VIM driver by ID (or by infrastructure type)')
@click.argument('environment')
@click.argument('driver-id', required=False)
@click.option('--config', default=None, help='configuration file')
@click.option('--pwd', '--api-key', default=None, help='password/api_key used for authenticating with CP4NA orchestration. Only required if the environment is secure and a username has been included in your configuration file with no password (api_key when using auth_mode=zen)')
@click.option('--type', 'inf_type', help='Infrastructure type used to identify the VIM driver to get. Use this instead of the driver-id argument')
@click.option('-f', '--format', 'output_format', default='table', help='format of output [table, yaml, json]')
def get(environment, driver_id, config, pwd, inf_type, output_format):
    """Get VIM driver"""
    vim_mgmt_driver = get_vim_driver_mgmt_driver(environment, config, pwd)
    if driver_id is None:
        if inf_type is None:
            click.echo('Error: Must specify driver-id argument or type option')
            exit(1)
        with lm_driver_safety_net():
            vim_driver = vim_mgmt_driver.get_vim_driver_by_type(inf_type)
    else:
        with lm_driver_safety_net():
            vim_driver = vim_mgmt_driver.get_vim_driver(driver_id)
    click.echo(format_vim_driver(output_format, vim_driver))

vim_driver_headers = ['id', 'infrastructureType', 'baseUri']
def vim_driver_row_processor(vim_driver):
    table_row = []
    table_row.append(vim_driver.get('id', ''))
    table_row.append(vim_driver.get('infrastructureType', ''))
    table_row.append(vim_driver.get('baseUri', ''))
    return table_row


# -*- coding: utf-8 -*-

from collections import OrderedDict
from itertools import islice
import logging
import inspect
import os
import sys
import json
import pickle

import lasagne
import numpy as np
import theano
import theano.tensor as TT

from hdf import hdf

def logging_config(name=None, file_level=logging.DEBUG, console_level=logging.DEBUG):
    if name is None:
        name = inspect.stack()[1][1].split('.')[0]
    folder = 'log-{0}'.format(os.path.join(os.getcwd(), name))
    if not os.path.exists(folder):
        os.makedirs(folder)
    logpath = os.path.join(folder, '{0}.log'.format(name))
    print('All Logs will be saved to {0}'.format(logpath))
    logging.root.setLevel(file_level)
    file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    console_formatter = logging.Formatter('%(message)s')
    logfile = logging.FileHandler(logpath)
    logfile.setLevel(file_level)
    logfile.setFormatter(file_formatter)
    logging.root.addHandler(logfile)
    logconsole = logging.StreamHandler()
    logconsole.setLevel(console_level)
    logconsole.setFormatter(console_formatter)
    logging.root.addHandler(logconsole)

def rmsprop(loss_or_grads, params, learning_rate, rho, epsilon):
    grads = lasagne.updates.get_or_compute_grads(loss_or_grads, params)
    updates = OrderedDict()
    for param, grad in zip(params, grads):
        value = param.get_value(borrow=True)
        acc_grad = theano.shared(np.zeros(value.shape, dtype=value.dtype), broadcastable=param.broadcastable)
        acc_grad_new = rho * acc_grad + (1 - rho) * grad
        acc_rms = theano.shared(np.zeros(value.shape, dtype=value.dtype), broadcastable=param.broadcastable)
        acc_rms_new = rho * acc_rms + (1 - rho) * grad ** 2
        updates[acc_grad] = acc_grad_new
        updates[acc_rms] = acc_rms_new
        updates[param] = (param - learning_rate * (grad / TT.sqrt(acc_rms_new - acc_grad_new **2 + epsilon)))
    return updates, grads

def major(actions, skip_frames, batch_size):
    z = []
    for i in range(batch_size):
        s = {}
        for j in range(skip_frames):
            a = actions[i, j]
            if a in s:
                s[a] += 1
            else:
                s[a] = 1
        m = max(list(s.values()))
        if m > 1:
            for j in range(skip_frames):
                a = actions[i, -j-1]
                if s[a] == m:
                    z.append(a)
                    s = {}
                    break
        else:
            z.append(actions[i, -1])
            s = {}
    return np.array(z, dtype=np.uint32)

class T_T(lasagne.layers.Layer):
    '''
    卧槽我就乘个矩阵还要自己写 这TM是在逗我 (｡ŏ﹏ŏ)
    '''
    def __init__(self, incoming, num_units, W, b, nonlinearity, **kwargs):
        super(T_T, self).__init__(incoming, **kwargs)
        self.nonlinearity = (lasagne.nonlinearities.identity if nonlinearity is None
                             else nonlinearity)

        self.num_units = num_units

        num_inputs = int(np.prod(self.input_shape[2:]))

        self.W = self.add_param(W, (num_inputs, num_units, 1), name="WT_T")
        if b is None:
            self.b = None
        else:
            self.b = self.add_param(b, (num_units,), name="b",
                                    regularizable=False)

    def get_output_shape_for(self, input_shape):
        return (input_shape[0], input_shape[1], self.num_units, 1)

    def get_output_for(self, input_tensor, **kwargs):
        if input_tensor.ndim > 3:
            input_tensor = input_tensor.flatten(3)

        activation = TT.tensordot(input_tensor, self.W, axes=(2, 0))
        if self.b is not None:
            activation = activation + self.b.dimshuffle('x', 'x', 0, 'x')
        return self.nonlinearity(activation)

class QwQ(lasagne.layers.Layer):
    '''
    改个type也不行.. Excuse me???? (｡ŏ﹏ŏ)
    '''
    def __init__(self, incoming, dtype, **kwargs):
        super(QwQ, self).__init__(incoming, **kwargs)
        self.dtype = dtype

    def get_output_shape_for(self, input_shape):
        return input_shape

    def get_output_for(self, input_tensor, **kwargs):
        return input_tensor.astype(self.dtype)

def build_wowah_network(num_frames=10):
    '''
    lasagne真心是个坑T_T
    '''
    from constant import Max_guild
    from constant import Races
    from constant import Categories
    from constant_zone import Zonetypes
    total_zonetypes = len(Zonetypes) + 1
    with open('data/zonesjson') as fp:
        zones = json.loads(fp.readline())
    total_zones = max([int(x) for x in zones.keys()]) + 1

    input_width=8
    cat_length=5
    input_height = 1
    cat_size = [Max_guild, len(Races), len(Categories), total_zones, total_zonetypes]

    context = TT.tensor4(name='input')
    l_in = lasagne.layers.InputLayer(
        shape=(None, num_frames, input_width, input_height),
        input_var=context,
    )

    cat_h = []
    for idx in range(cat_length):
        cat_slice = lasagne.layers.SliceLayer(
            l_in,
            indices=idx,
            axis=2,
        )

        cat_int = QwQ(
            cat_slice,
            dtype='int16',
        )

        cat_embed = lasagne.layers.EmbeddingLayer(
            cat_int,
            input_size=cat_size[idx],
            output_size=10,
            W=lasagne.init.Normal(.01),
        )

        cat_reshape = lasagne.layers.DimshuffleLayer(
            cat_embed,
            # pattern=(0, 1, 3, 'x'),
            # we omit the trailing 'x', only for wowah dataset s.t. height=1
            pattern=(0, 1, 3, 2)
        )

        cat_h.append(cat_reshape)

    ord_slice = lasagne.layers.SliceLayer(
            l_in,
            indices=slice(cat_length, None),
            axis=2,
        )

    ord_h = T_T(
        ord_slice,
        num_units=30,
        nonlinearity=lasagne.nonlinearities.rectify,
        W=lasagne.init.Normal(.01),
        b=lasagne.init.Constant(.1)
    )

    l_hidden1 = lasagne.layers.ConcatLayer(
        cat_h + [ord_h, ],
        axis=2,
    )

    l_hidden2 = lasagne.layers.DenseLayer(
        l_hidden1,
        num_units=120,
        nonlinearity=lasagne.nonlinearities.rectify,
        W=lasagne.init.Normal(.01),
        b=lasagne.init.Constant(.1)
    )

    l_out = lasagne.layers.DenseLayer(
        l_hidden2,
        num_units=total_zones,
        nonlinearity=lasagne.nonlinearities.rectify,
        W=lasagne.init.Normal(.01),
        b=lasagne.init.Constant(.1)
    )

    sample = np.zeros((32, num_frames, input_width,input_height), dtype=np.float32)
    sample[:,:,:cat_length,:] = sample[:,:,:cat_length,:].astype(np.uint32)

    context.tag.test_value = sample

    return l_out#, locals()

if __name__ == '__main__':
    '''
    example of execution:
    python architecture.py 0 30000 350 0.0025 advancing
    '''
    if not os.path.exists('data/networks'):
        os.mkdir('data/networks')

    satisfaction_idx = int(sys.argv[1])
    num_batch = int(sys.argv[2])
    n_epoch = int(sys.argv[3])
    lr = float(sys.argv[4]) # 0.00045
    name = sys.argv[5]

    logging_config()

    theano.config.optimizer = 'fast_run' # 'fast_compile'
    theano.config.exception_verbosity = 'low' # 'high'
    theano.config.compute_test_value = 'off' # 'on'

    list_data = [path for path in os.listdir('data') if path.startswith('episodes_{0}'.format(name))]
    hdf_size = max(int(path.split('.')[0].split('-')[1]) for path in list_data)
    data = 'data/episodes_{0}-{1}.hdf'.format(name, hdf_size)

    if satisfaction_idx == 5:
        satisfaction_idxes = range(5)
    else:
        satisfaction_idxes = [satisfaction_idx]
    for satisfaction_idx in satisfaction_idxes:

        l_out = build_wowah_network()

        batch_size = 32
        test_batch = 1000
        num_frames = 10
        skip_frames = 4
        input_width = 8
        input_height = 1
        discount = 0.99
        clip_delta = 1.0
        #lr = 0.00005
        rho = 0.95
        rms_epsilon = 0.01
        with open('data/zonesjson') as fp:
            zones = json.loads(fp.readline())
        zones = {int(x):zones[x] for x in zones}
        total_zones = max([int(x) for x in zones.keys()]) + 1
        num_actions = total_zones

        states = TT.tensor4('states')
        next_states = TT.tensor4('next_states')
        rewards = TT.col('rewards')
        actions = TT.col('actions', dtype='uint32')

        context_shared = theano.shared(
            np.zeros((batch_size, num_frames + skip_frames, input_width, input_height),
                     dtype=theano.config.floatX))
        rewards_shared = theano.shared(
            np.zeros((batch_size, 1), dtype=theano.config.floatX),
            broadcastable=(False, True))
        actions_shared = theano.shared(
            np.zeros((batch_size, 1), dtype='uint32'),
            broadcastable=(False, True))
        state_shared = theano.shared(
            np.zeros((num_frames, input_height, input_width),
                     dtype=theano.config.floatX))

        q_vals = lasagne.layers.get_output(l_out, states)
        next_q_vals = lasagne.layers.get_output(l_out, next_states)
        #next_q_vals = theano.gradient.disconnected_grad(next_q_vals)

        actionmask = TT.eq(TT.arange(num_actions).reshape((1, -1)), actions.reshape((-1, 1))).astype(theano.config.floatX)
        target = (rewards + discount * TT.max(next_q_vals, axis=1, keepdims=True))
        output = (q_vals * actionmask).sum(axis=1).reshape((-1, 1))
        diff = target - output
        quadratic_part = TT.minimum(abs(diff), clip_delta)
        linear_part = abs(diff) - quadratic_part
        loss_batch = 0.5 * quadratic_part ** 2 + clip_delta * linear_part
        loss = TT.sum(loss_batch)

        train_subs = {
            states: context_shared[:, :-skip_frames],
            next_states: context_shared[:, skip_frames:],
            rewards: rewards_shared,
            actions: actions_shared,
        }
        params = lasagne.layers.helper.get_all_params(l_out)
        updates, grads = rmsprop(loss, params, lr, rho, rms_epsilon)
        speed = sum([grad.norm(L=2) for grad in grads]) / sum([grad.shape.prod() for grad in grads])

        train = theano.function(inputs=[], outputs=[loss, speed], updates=updates, givens=train_subs)
        # we evaluate it using value programming
        # actions_hat = TT.argmax(q_vals, axis=1)
        q_func = theano.function(inputs=[states], outputs=q_vals)

        predict_history = np.zeros((test_batch, batch_size))

        for epoch in range(n_epoch):
            total_loss = 0
            total_speed = 0
            hits = 0
            stays = 0
            predict_stays = 0
            keeps = 0
            predict_histogram = [0] * 165
            for idx, (context, reward, action, candidates) in enumerate(hdf(path=data, batch_size=batch_size, num_batch=num_batch)):
                action_star = major(action, skip_frames, batch_size)

                if not idx >= num_batch - test_batch:
                    context_shared.set_value(context)
                    rewards_shared.set_value(reward[:,satisfaction_idx].reshape(batch_size, 1))
                    actions_shared.set_value(action_star.reshape(batch_size, 1))

                    train_results = train()
                    loss, speed = train_results
                    #speed = train_results[1:]
                    #speed = [float(x) for x in speed]
                    total_loss += loss
                    total_speed += speed

                if idx >= num_batch - test_batch:
                    q_hat = q_func(context[:, :-skip_frames])
                    last = context[:,-(skip_frames+1),3,:].flatten()
                    actions_hat = np.argmax(q_hat * candidates, axis=1)
                    keeps += (actions_hat == predict_history[idx-(num_batch-test_batch)]).sum()
                    predict_history[idx-(num_batch-test_batch)] = actions_hat
                    hits += (actions_hat == action_star).sum()
                    stays += (last == action_star).sum()
                    predict_stays += (last == actions_hat).sum()
                    for action in actions_hat:
                        predict_histogram[action] += 1
                    #print(actions_hat, action_star)
                    #print(hits)

            accuracy = hits / (batch_size * test_batch)
            stick = stays / (batch_size * test_batch)
            predict_stick = predict_stays / (batch_size * test_batch)
            dominate_rate = max(predict_histogram) / (batch_size * test_batch)
            keep_rate = keeps / (batch_size * test_batch)
            loss = total_loss / (batch_size * (num_batch - test_batch))
            logging.info('{8} #{3}/{7}: loss={0}, spd={1}, acc={2}, unary={4}/~60%, dmt={5}, keep={6}'.format(loss, total_speed, accuracy, epoch+1, predict_stick, dominate_rate, keep_rate, satisfaction_idx, name))
            #print('stay rate = {0}, unary rate = {1}'.format(stick, predict_stick))
            #logging.info(str(np.argmax(q_hat, axis=1)))
            network = lasagne.layers.get_all_param_values(l_out)
            netfile = open('data/networks/Q-{2}-{0}-{1}.pkl'.format(satisfaction_idx, epoch+1, name), 'wb')
            pickle.dump(network, netfile)


'''
Reid Harry
1/17/2021

In python, when passing an object to a function, all objects that are not primitive
types are passed by reference. Understanding this is incredibly important to understanding
the content of CSE 331. This can be seen in many contexts - e.g. In the snippet below,
`a` and `b` reference the same list. Modifying one modifies the other:

a = [1, 2, 3]

b = a

b.append(4)

print(a)

>> [1, 2, 3, 4]

Exercise 5: Write a function that takes as input a 2-dimensional list, and produces a
deep copy of that list.

The final result and the input should be referentially independent - you should be able
to modify one, at any level, without modifying the other. This should also be achieved
without using any import statements.
'''

from typing import List, Any

def exercise5(data: List[List[Any]]) -> List[List[Any]]:
    """
    param data: a two dimensional list
    return: a referentially independent copy of that list
    """

    newData = []

    # Copies each element from each sublist into new sublists. Those are then copied into newData
    for subList in data:
        newSubList = []
        for item in subList:
            newSubList.append(item)
        newData.append(newSubList)

    return newData


import ssl, os, smtplib
from email.mime.text import MIMEText
from src.utils.transfer import Transfer
from email.mime.multipart import MIMEMultipart

class EmailService:
    @staticmethod
    def send_mail(email: str, text: str) -> Transfer:
        res = Transfer()
        try:
            context = ssl.create_default_context()
            port = os.getenv("EMAIL_PORT")

            body = MIMEMultipart("alternative")
            body["Subject"] = "Recuperação de senha Lucy"
            body["From"] = os.getenv("EMAIL_FROM")
            body["To"] = email
            body.attach(MIMEText(text, "plain"))

            with smtplib.SMTP("smtp-mail.outlook.com", port) as server:
                server.starttls(context=context)
                server.login(os.getenv("EMAIL_FROM"), os.getenv("EMAIL_PASS"))
                server.sendmail(os.getenv("EMAIL_FROM"), email, body.as_string())

        except BaseException as e:
            res.set_status_code(500)
            res.set_message(str(e))
        finally:
            return res


import sys
S = list(sys.stdin.readline().strip())
N, zero, one = len(S), S.count('0')//2, S.count('1')//2
for i in range(N-1,-1,-1):
    if zero and S[i] == '0':
        S[i] = 'x'
        zero -= 1
for i in range(N):
    if one and S[i] == '1':
        S[i] = 'x'
        one -= 1

result = ''
for i in S:
    if i != 'x':
        result += i
print(result)


import os
import datetime
import random
import pickle
from progressbar import ProgressBar
from joblib import Parallel, delayed
import multiprocessing
from tqdm import tqdm
pbar = ProgressBar()

def metapath_gen(case, act_case_dict, act_sec_dict, outpath, numwalks, walklength, case_act_dict):
	#Generating meta-paths for metapath2vec
	outfile = []
	case0 = case
	for j in range(numwalks):
		outline = case0
		for i in range(walklength):
			acts = case_act_dict[case]
			numa = len(acts)
			actid = random.randrange(numa)
			act = acts[actid]
			secs = act_sec_dict[act]
			nums = len(secs)
			sec_id = random.randrange(nums)
			sec = secs[sec_id]
			outline += " " + str(sec)
			cases = act_case_dict[act]
			numc = len(cases)
			caseid = random.randrange(numc)
			case = cases[caseid]
			outline += " " + str(case)
		outfile.append(outline + "\n")
	return outfile

def metapath2vec(code_dir, outpath, embout):
	print("Running Metapath2Vec")
	os.chdir(code_dir)
	pp = 1
	size = 128
	window = 7
	negative = 5
	outpath = "../"+outpath
	embout = "../"+embout
	cmd = "./metapath2vec -train "+outpath+" -output "+embout+" -pp "+str(pp)+" -size "+str(size)+" -window "+str(window)+" -negative "+str(negative)+" -threads 32"
	os.system(cmd)
	print("Embddings saved at "+embout)

def distance(code_dir, embout):
	os.chdir(code_dir)
	cmd = "./distance ../"+embout
	os.system(cmd)

def reverse_dict(D):
	rD = {}
	for i in tqdm(D):
		for j in D[i]:
			try:
				rD[j].append(i)
			except KeyError:
				rD[j] = []
				rD[j].append(i)
	return rD

def make_acts_to_case(act_case_dict):
	act_case = {}
	act_sec = {}
	print('Making act-->case dict')
	for sec in tqdm(act_case_dict):
		cases = act_case_dict[sec]
		act = sec.split('_')[0]
		try:
			act_case[act].extend(cases)
		except:
			act_case[act] = cases
		try:
			act_sec[act].append(sec)
		except:
			act_sec[act] = [sec]
	act_sec = sec_codes(act_sec)
	print('Making case-->act dictionay')
	case_act_dict = reverse_dict(act_case)
	return case_act_dict, act_case, act_sec

def sec_codes(act_sec_dict):
	print('Coding Acts')
	sec_act_dict = reverse_dict(act_sec_dict)
	j = 0
	codes = {}
	for i in sec_act_dict:
		codes[j] = sec_act_dict[i]
		j += 1
	codes = reverse_dict(codes)
	return codes

def main():
	outpath = "saved/metapaths.txt"
	embout = "saved/metapath2vec_embeddings"
	metapath2vec_dir = "code_metapath2vec"

	print("Please specify all the parameters and paths in the script itself")
	print("Enter 0 to run on small network only")
	print("Enter 1 to generate metapaths")
	print("Enter 2 to run metapath2vec on generated metapaths")
	print("Enter 3 to run distance on generated embeddings")
	print("Enter 4 to 1 and 2")
	task = int(input("Enter : "))
	if task == 1 or task == 4 or task ==0:
		numwalks = 100
		walklength = 30
		if task == 0:
			with open('saved/smallact_case_dict.pickle', 'rb') as file:
				act_case_sec_dict = pickle.load(file)
			outpath = "saved/small_metapaths.txt"
			embout = "saved/small_metapath2vec_embeddings"
		else:
			with open('saved/sec_case_dict.pickle', 'rb') as file:
				act_case_sec_dict = pickle.load(file)
		case_act_dict, act_case_dict, act_sec_dict = make_acts_to_case(act_case_sec_dict)
		num_cores = multiprocessing.cpu_count()
		results = []
		for i in tqdm(case_act_dict):
			results.append(metapath_gen(case = i, act_case_dict = act_case_dict, act_sec_dict = act_sec_dict, outpath = outpath, numwalks = numwalks, walklength =  walklength, case_act_dict = case_act_dict))
		print('Saving metapaths as txt')
		with open(outpath, 'w') as file:
			for i in tqdm(results):
				for j in i:
					file.write(j)
	if task == 2 or task == 4 or task == 0:
		metapath2vec(code_dir = metapath2vec_dir, outpath = outpath, embout = embout)
	if task == 3:
		distance(code_dir = metapath2vec_dir, embout = embout)
	else:
		print("Invalid Choice")

if __name__ == '__main__':
	main()


# -*- coding: utf-8 -*-
"""
Created on Sun Jan  3 20:42:04 2021

@author: Henry
"""

import json

if __name__ == "__main__":
    with open('json/champion.json', encoding="utf8") as f:
        data = json.load(f)
    champions = dict()
    for champ in data['data']:
        champions[data['data'][champ]['key']] = champ
    with open('json/id_to_champion.json', 'w') as f:
        json.dump(champions, f)

    with open('json/summoner.json', encoding="utf8") as f:
        data = json.load(f)
    summoners = dict()
    for summoner in data['data']:
        summoners[data['data'][summoner]['key']] = data['data'][summoner]['name']
    summoners['0'] = 'None'
    with open('json/id_to_summoner.json', 'w') as f:
        json.dump(summoners, f)


import pandas as pd
import numpy as np
import os.path as osp

PROJECT_PATH = '/data/pzhutovsky/fMRI_data/PTSD_veterans'
ORIG_CLIN_FILE = osp.join(PROJECT_PATH, 'sourcedata', 'BETER_Geuze_AMS_ML_20170118.csv')
RESPONSE_NONRESPONSE_FILE = osp.join(PROJECT_PATH, 'analysis', 'ptsd_dual_regression', 'labels.csv')
CONTROLS_USED = osp.join(PROJECT_PATH, 'analysis', 'controls_ICA_aroma.gica',
                         'controls_preprocessed_aroma_final_without21_rejected_Sattleworth.txt')
PTSD_USED = osp.join(PROJECT_PATH, 'analysis', 'ptsd_dual_regression', 'dual_regression_ptsd_filtered.txt')
CONTROLS_FD = osp.join(PROJECT_PATH, 'analysis', 'controls_ICA_aroma.gica', 'motion_metrics.csv')
PTSD_FD = osp.join(PROJECT_PATH, 'analysis', 'ptsd_dual_regression', 'motion_metrics.csv')
PTSD_TIV = osp.join(PROJECT_PATH, 'analysis', 'ptsd_vbm', 'ptsd_TIV.csv')

def extract_subjid_from_path(array_path_files):
    """
    1. Partition on 'sub-' (the next word will be ptsd or control);
    2. Partition on '_' (the part before is ptsd/control + number, part after is subject id)
    3. Partition on '/' (the part before is subject id)
    :param array_path_files:
    :return:
    """
    return np.char.partition(np.char.partition(np.char.partition(array_path_files, 'sub-')[:, -1], '_')[:, -1],
                             '/')[:, 0]

def load_TIV():
    return pd.read_csv(PTSD_TIV)

def load_fd():
    df_control = pd.read_csv(CONTROLS_FD)
    df_control['subj_id'] = extract_subjid_from_path(df_control.subj_id.values.astype(str))

    df_ptsd = pd.read_csv(PTSD_FD)
    df_ptsd['subj_id'] = extract_subjid_from_path(df_ptsd.subj_id.values.astype(str))
    return pd.concat((df_control[['subj_id', 'FD_mean']], df_ptsd[['subj_id', 'FD_mean']]), axis=0, ignore_index=True)

def load_labels_file():
    return pd.read_csv(RESPONSE_NONRESPONSE_FILE)[['orig_id', 'reduction_30']]

def load_subj_used():
    return np.loadtxt(CONTROLS_USED, dtype=str), np.loadtxt(PTSD_USED, dtype=str)

def load_clinical_file():
    return pd.read_csv(ORIG_CLIN_FILE)

def add_response_status(df_data, df_response):
    ids_data = df_data.Name.values.astype(str)
    ids_response = df_response.orig_id.values.astype(str)
    df_data['response'] = np.nan
    n_controls = (df_data.Group == "Control").sum()

    for i, id_response in enumerate(ids_response):
        id_subj = ids_data == id_response
        assert id_subj.sum() == 1, 'More than one subject with id {}'.format(ids_response[id_response])
        assert (id_subj.argmax() - n_controls) == i, 'Data not ordered'
        df_data.loc[id_subj, 'response'] = df_response.reduction_30[df_response.orig_id == id_response].values

    assert np.all((df_data.Group == 'Control') == (df_data.response.isnull())), 'Something failed at adding response'
    df_data.response = df_data.response.replace({0: 'Non-Responder', 1: 'Responder'})
    df_data.loc[df_data.response == 'Responder', 'Group'] = 'PTSD-Responder'
    df_data.loc[df_data.response == 'Non-Responder', 'Group'] = 'PTSD-Non-Responder'
    return df_data

def add_FD(df_data, df_fd):
    df_data['FD'] = np.nan
    ids_data = df_data.Name.values.astype(str)
    ids_fd = df_fd.subj_id.values.astype(str)

    for id_data in ids_data:
        id_subj = id_data == ids_fd
        assert id_subj.sum() == 1, 'More than one subject with id {}'.format(ids_data[id_data])
        df_data.loc[ids_data == id_data, 'FD'] = df_fd.FD_mean.loc[id_subj].values

    assert np.all(df_data.FD.notnull()), 'Missing FD value'
    return df_data

def add_TIV(df_data, df_TIV):
    df_data['TIV'] = np.nan
    ids_data = df_data.Name.values.astype(str)
    ids_tiv = df_TIV.subj_id.values.astype(str)

    for id_tiv in ids_tiv:
        id_subj = ids_data == id_tiv
        assert id_subj.sum() == 1, 'More than one subject with id {}'.format(ids_tiv[id_tiv])
        df_data.loc[id_subj, 'TIV'] = df_TIV.loc[ids_tiv == id_tiv, 'TIV'].values

    return df_data

def extract_used_subj(df_all, ids_to_extract):
    ids_all = df_all.Name.values.astype(str)
    ids_subj = []
    for id_to_extract in ids_to_extract:
        id_subj = ids_all == id_to_extract

        assert id_subj.sum() == 1, 'More than one subject with id {}'.format(id_to_extract)
        # quick way to extract the id of a boolean array
        ids_subj.append(np.argmax(id_subj))

    return df_all.iloc[ids_subj].reset_index(drop=True)

def calculate_age(df):
    return (pd.to_datetime(df.T0_KA_datum) - pd.to_datetime(df.Geboorte_datum)).astype('timedelta64[Y]')

def clean_data(df_clin_used):
    df_clin_used['age_at_T0'] = calculate_age(df_clin_used)
    df_clin_used['perc_decr'] = np.nan
    id_ptsd = df_clin_used.Group != 'Control'
    df_clin_used['perc_decr'] = ((df_clin_used.loc[id_ptsd, 'T0_CAPS_TOTAAL'].astype(float) -
                                  df_clin_used.loc[id_ptsd, 'T6_CAPS_TOTAAL'].astype(float))
                                 / df_clin_used.loc[id_ptsd, 'T0_CAPS_TOTAAL'].astype(float)) * 100

    df_clin_used['years_since_last_depl'] = (pd.to_datetime(df_clin_used.T0_KA_datum) -
                                             pd.to_datetime(df_clin_used.Datum_laatste_uitzending,
                                                            errors='coerce')).astype('timedelta64[Y]')
    df_clin_used.rename(columns={'Rechts_Links': 'Handedness',
                                 'Opleiding_corrected': 'Education_self',
                                 'Opleiding_moeder': 'Education_mother',
                                 'Opleiding_vader': 'Education_father',
                                 'Aant_uitzendingen': 'Number_of_times_deployed',
                                 'Depressie_huidig': 'Depression_T0',
                                 'Angststoornis_huidig': 'Anxiety_T0',
                                 'Somatoform_huidig': 'Somatoform_T0',
                                 'Depressie_huidig_T6': 'Depression_T6',
                                 'Angststoornis_huidig_T6': 'Anxiety_T6',
                                 'Somatoform_huidig_T6': 'Somatoform_T6',
                                 'Alcoholafhankelijkheid': 'Alcoholism'}, inplace=True)
    education_renaming = {'Geen': 0,
                          'Lager onderwijs, basisschool': 1,
                          'Lager beroepsonderwijs': 2,
                          '(M)ULO, mavo': 3,
                          'HAVO': 4,
                          'VWO': 5,
                          'MBO': 6,
                          'HBO': 7,
                          'Universiteit': 8,
                          'Anders': 9,
                          '9999': np.nan}
    df_clin_used.Education_mother = df_clin_used.Education_mother.map(education_renaming)
    df_clin_used.Education_father = df_clin_used.Education_father.map(education_renaming)
    df_clin_used.Depression_T0 = df_clin_used.Depression_T0.map({'geen huidige depressie': 'No',
                                                                 'huidige depressie': 'Yes'})
    df_clin_used.Anxiety_T0 = df_clin_used.Anxiety_T0.map({'geen comorbide angstoornis': 'No',
                                                           'comorbide angststoornis': 'Yes'})
    df_clin_used.Somatoform_T0 = df_clin_used.Somatoform_T0.map({'geen huidige somatoforme stoornis': 'No',
                                                                 'huidige somatoforme stoornis': 'Yes'})
    df_clin_used = df_clin_used.replace(['9999', 9999, ' '], [np.nan, np.nan, np.nan])

    ssri_T0 = df_clin_used.SSRI_T0.values.astype(np.float)
    sari_T0 = df_clin_used.SARI_T0.values.astype(np.float)
    ssri_nan_T0 = np.isnan(ssri_T0)
    sari_nan_T0 = np.isnan(sari_T0)
    sri_T0 = ((ssri_T0 == 1) | (sari_T0 == 1)).astype(np.float)
    sri_T0[ssri_nan_T0 | sari_nan_T0] = np.nan

    ssri_T6 = df_clin_used.SSRI_T6.values.astype(np.float)
    sari_T6 = df_clin_used.SARI_T6.values.astype(np.float)
    ssri_nan_T6 = np.isnan(ssri_T6)
    sari_nan_T6 = np.isnan(sari_T6)
    sri_T6 = ((ssri_T6 == 1) | (sari_T6 == 1)).astype(np.float)
    sri_T6[ssri_nan_T6 | sari_nan_T6] = np.nan

    df_clin_used['SRI_T0'] = sri_T0
    df_clin_used['SRI_T6'] = sri_T6

    # Confirmed from Elbert that be3i (BET083) received 8-10 EMDR sessions (will set it to 9);
    # -be5h (BET075) received EMDR
    # -be6h (BET076) received CBT + EMDR
    # -be8h (BET078) received CBT + EMDR
    # -be3i (BET081) received CBT + structured treatment sessions
    # but we don't know how _many_ sessions they received... so I set it to NaN
    df_clin_used.loc[df_clin_used.PPNnr == 'BET083', 'EMDR_between_T0_T6'] = 9
    df_clin_used.loc[df_clin_used.PPNnr == 'BET083', 'CBT_between_T0_T6'] = 0
    df_clin_used.loc[df_clin_used.PPNnr == 'BET083', 'ECL_between_T0_T6'] = 0
    df_clin_used.loc[df_clin_used.PPNnr == 'BET083', 'Structs_between_T0_T6'] = 0

    id_bet075 =  df_clin_used.PPNnr == 'BET075'
    id_bet076 = df_clin_used.PPNnr == 'BET076'
    id_bet078 = df_clin_used.PPNnr == 'BET078'
    id_bet081 = df_clin_used.PPNnr == 'BET081'
    id_missing = id_bet075 | id_bet076 | id_bet078 | id_bet081
    all_therapies = ['EMDR_between_T0_T6', 'CBT_between_T0_T6', 'ECL_between_T0_T6', 'Structs_between_T0_T6']
    df_clin_used.loc[id_missing, all_therapies] = np.nan

    # some controls (which DID NOT receive therapy) have NaN values somehow. I will manually replace those with 0
    # just for consistency
    df_clin_used.loc[df_clin_used.Group == 'Control', all_therapies] = 0.

    all_sessions = df_clin_used.loc[:, all_therapies].astype(np.float)
    df_clin_used['total_number_sessions'] = all_sessions.sum(axis=1, skipna=False)

    df_clin_used[all_therapies] = df_clin_used[all_therapies].astype(np.float)

    # we also want to just know about therapy types (independent of how many sessions)
    # for the 4 missing subjects (where we don't know how many sessions they had), we know WHICH therapies they had
    therapies_used = np.array(['EMDR_used', 'CBT_used', 'ECL_used', 'Structs_used'])
    df_clin_used[therapies_used] = (df_clin_used[all_therapies] > 0).astype(np.int)
    df_clin_used.loc[id_bet075, therapies_used] = [1, 0, 0, 0]
    df_clin_used.loc[id_bet076, therapies_used] = [1, 1, 0, 0]
    df_clin_used.loc[id_bet078, therapies_used] = [1, 1, 0, 0]
    df_clin_used.loc[id_bet081, therapies_used] = [0, 1, 0, 1]

    df_clin_used['therapies_used_categorical'] = np.nan
    used_therapies_ohot = df_clin_used[therapies_used].values

    for i in xrange(df_clin_used.shape[0]):
        subj_therapies = used_therapies_ohot[i].astype(bool)

        if subj_therapies.any():
            therapies_names = therapies_used[subj_therapies]
            if therapies_names.size > 1:
                df_clin_used.loc[i, 'therapies_used_categorical'] = ' + '.join(therapies_names)
            else:
                df_clin_used.loc[i, 'therapies_used_categorical'] = therapies_names[0]
    return df_clin_used

def run():
    df_clin_file = load_clinical_file()
    df_fd = load_fd()
    df_tiv = load_TIV()
    df_labels_file = load_labels_file()
    controls_file, ptsd_file = load_subj_used()
    controls_subjid = extract_subjid_from_path(controls_file)
    ptsd_subjid = extract_subjid_from_path(ptsd_file)

    df_clin_used = extract_used_subj(df_clin_file, np.concatenate((controls_subjid, ptsd_subjid)))
    df_clin_used = add_response_status(df_clin_used, df_labels_file)
    df_clin_used = add_FD(df_clin_used, df_fd)
    df_clin_used = add_TIV(df_clin_used, df_tiv)
    df_clin_used_clean = clean_data(df_clin_used)
    df_clin_used_clean.to_csv(osp.join(PROJECT_PATH, 'analysis', 'clean_clinical_data.csv'), index=False,
                              na_rep='NA')

if __name__ == '__main__':
    run()


import os
from flask import Flask, request, jsonify
from lib import AgentConflict, Agent, Point, SystemFault
from store import AgentStore
from typing import Any
from consensus import calculateConsensus

ENV_NAME = os.getenv("ALG_ENV", "dev")
ALG_SERVER_W_X = os.getenv("ALG_SERVER_W_X")
ALG_SERVER_W_Y = os.getenv("ALG_SERVER_W_Y")
ALG_STEP_SIZE = float(os.getenv("ALG_STEP_SIZE", "0.05"))
ALG_LIMIT = float(os.getenv("ALG_LIMIT", "0.05"))

server = Flask(__name__)
if ENV_NAME != "prod":
    print("Running in DEV mode!")
    server.config["DEBUG"] = True

W: Point = Point(float(ALG_SERVER_W_X), float(ALG_SERVER_W_Y))
AGENT_STORE = AgentStore()

@server.route("/agents", methods=["GET"])
def getRegisteredAgents():
    return jsonify(list(map(lambda a: a.toDict(), AGENT_STORE.getAgents())))

@server.route("/register", methods=["POST"])
def registerAgent():
    payload: Any = request.json
    agentId = str(payload["id"])
    agentUrl = str(payload["url"])
    agent = Agent(agentId, agentUrl)
    registered = AGENT_STORE.addAgent(agent)
    if registered:
        message = "Agent {} registered at {}!".format(agent.agentId, agent.address)
        return jsonify({"status": 200, "message": message})
    else:
        raise AgentConflict("Agent with given id already registered!")

@server.route("/consensus", methods=["POST"])
def getAgentCost():
    meetingPoint, steps = calculateConsensus(AGENT_STORE, W.copy(), step=ALG_STEP_SIZE, limit=ALG_LIMIT)
    return jsonify({"meetingPoint": {"x": meetingPoint.x, "y": meetingPoint.y}, "steps": steps})

@server.errorhandler(AgentConflict)
def agentConflictHandler(e: AgentConflict):
    response = jsonify(e.toDict())
    response.status_code = 409
    return response

@server.errorhandler(SystemFault)
def agentConflictHandler(e: SystemFault):
    response = jsonify(e.toDict())
    response.status_code = 400
    return response

@server.errorhandler(404)
def notFoundErrorHandler(e):
    print(e)
    return jsonify({"status": 404, "error": "Requested resource not found!"})

if __name__ == "__main__":
    PORT = os.getenv("ALG_SERVER_PORT", "5000")
    server.run(host="0.0.0.0", port=int(PORT), use_reloader=False)


from tailseeker.tabledefs import taginfo, refined_taginfo
from io import BytesIO
import subprocess as sp
import pandas as pd
import numpy as np

def get_polya_length_hist(pacallsfile, badflagmask, maxpalen, columnno):
    freqoutput = sp.check_output(
        'zcat {input_file} | perl -e \'while (<>) {{ \
            chomp; my @f=split; if ((@f[2] & {flagmask}) == 0) {{ \
                print "$f[{polya_column}]\\n"; }} }}\' | \
            sort -n | uniq -c'.format(
            input_file=pacallsfile, polya_column=columnno,
            flagmask=badflagmask), shell=True)

    pacounts = pd.read_table(BytesIO(freqoutput), skipinitialspace=True, sep=' ',
                             names=['count', 'polya'], dtype=np.int64).set_index('polya')

    pacounts = pacounts.reindex(list(range(maxpalen + 1)), fill_value=0)

    return pacounts['count']

# ===

inputfiles = list(zip(snakemake.params.samplenames, snakemake.input))

tableschema = refined_taginfo if snakemake.params.refined else taginfo
columnname = 'unaligned_polyA' if snakemake.params.refined else 'polyA'
columnno = tableschema['names'].index(columnname)

pacounts = pd.DataFrame.from_dict(dict(
    [(samplename,
      get_polya_length_hist(lencallfile, snakemake.params.badflagmask,
                            snakemake.params.maxpalength, columnno))
     for samplename, lencallfile in inputfiles]))

# trim all-zero rows at the end of the list
effective_maximum_pa_len = np.where(pacounts.sum(axis=1) > 0)[0].max()
pacounts = pacounts[pacounts.index <= effective_maximum_pa_len]

# write the output in csv format
pacounts.to_csv(snakemake.output[0])


from location import Location

class ChangeWall:
    """

    :param reader:
    :return:
    """

    def __init__(self, reader):
        """

        :param reader:
        """

        self.type = None
        self.location = None
        self.to = None
        self.side = None
        self.model = None

        self.read(reader)

    def read(self, reader):
        """

        :param reader:
        :return:
        """
        if not reader:
            return

        self.type = reader.read_byte()
        self.location = Location(reader)

        if self.type == -9:   # All side
            self.to = reader.read_ubyte()
            self.model = reader.read_ubyte()

        elif self.type == -23:     # One side
            self.side = reader.read_ubyte()
            self.to = reader.read_ubyte()
            self.model = reader.read_ubyte()

        elif self.type == -22:     # Door
            pass

    def decode(self, maze, assets):

        if self.type == -9:
            return "Change wall at {location} all sides from: {model} to: {to}".format(
                location=self.location, model=self.model, to=self.to)

        elif self.type == -23:
            return "Change wall at {location} side: {side} from: {model} to: {to}".format(
                location=self.location, side=self.side, model=self.model, to=self.to)

        elif self.type == -22:
            return "Process door switch at {location}".format(
                location=self.location)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Project ：LeetCodePthonVersion
@File ：54. 螺旋矩阵.py
@Author ：HuntingGame
@Date ：2023-02-03 9:58
C'est la vie!!! enjoy ur day :D
'''
from typing import List

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        """
        还是整个框去把框里的打印对
        :param matrix:
        :return:
        """
        if len(matrix) == 0 or len(matrix[0]) == 0:
            return []
        a = 0
        b = 0
        c = len(matrix) - 1
        d = len(matrix[0]) - 1
        ans = []
        while a <=c and b <=d:
            self.addEdge(matrix,a,b,c,d,ans)
            a+=1
            b+=1
            c-=1
            d-=1

        return ans

    def addEdge(self,m,a,b,c,d,ans:List):

        if a == c: #单横线
            for i in range(b,d+1):
                ans.append(m[a][i])
        elif b == d:#单竖线
            for i in range(a,c+1):
                ans.append(m[i][b])
        else:
            #下面是四个框。上面俩都是最后的情况了。
            curC = b
            curR = a
            while curC !=d:
                ans.append(m[a][curC])
                curC +=1
            while curR !=c:
                ans.append(m[curR][d])
                curR +=1
            while curC !=b:
                ans.append(m[c][curC])
                curC -=1
            while curR !=a:
                ans.append(m[curR][b])
                curR -=1


import numpy as np
import pandas as pd
from mpi4py import MPI
import random
from scipy.stats import cauchy, norm
from tqdm import tqdm
import pydevd_pycharm
port_mapping = [56843, 56842]

class D_CCPSO2_Master(object):
    def __init__(self, i_comm, fun, num_subswarm, dimension_size, x_lower, x_upper, example_num_max=4):

        # mpi通信子
        self.comm = i_comm

        # 优化的问题
        self.fun = fun

        # 停止的标记
        self.stop_counter = 0

        # 子种群的个数(N)
        self.num_subswarm = num_subswarm

        # 优化问题维度
        self.dimension_size = dimension_size

        # 上下界
        self.x_lower = x_lower
        self.x_upper = x_upper

        # example的最大数量
        self.example_num_max = example_num_max

        # # 全局example
        # self.global_example = np.empty([self.example_num_max, self.dimension_size + 1], dtype=np.float64)
        # 全局example
        self.global_example = (self.x_lower + np.random.rand(self.example_num_max, self.dimension_size + 1) * (
                self.x_upper - self.x_lower)).astype(np.float64)
        # 初始化全局example的适应值
        for i in range(self.example_num_max):
            self.global_example[i, -1] = self.fun(self.global_example[i, :-1])

        # # 当前全局example数量
        # self.has_example = 0

    def evolve(self):
        while 1:
            if self.stop_counter == self.num_subswarm:
                break

            # 接收数据的暂存区buf
            data = np.empty(self.dimension_size + 1, dtype=np.float64)

            status = MPI.Status()
            req = self.comm.Irecv(buf=data)
            req.wait(status=status)

            if status.Get_tag() == 1:
                self.stop_counter += 1

            elif status.Get_tag() == 2:
                # if self.has_example < self.example_num_max:
                #     self.global_example[self.has_example] = data.copy()
                #     self.has_example += 1
                # pydevd_pycharm.settrace('localhost', port=port_mapping[0], stdoutToServer=True,
                #                         stderrToServer=True)
                if data[-1] not in self.global_example[:, -1]:
                    example_temp = np.concatenate([self.global_example, [data]])
                    ind = np.argsort(example_temp[:, -1])
                    example_temp = example_temp[ind]
                    self.global_example = example_temp[:-1, :]

            elif status.Get_tag() == 3:
                req2 = self.comm.Isend(self.global_example, dest=status.Get_source())
                req2.wait()

        pd.DataFrame(data=self.global_example[0]).to_csv('results/D_CCPSO2_gbest_example.csv')
        return self.global_example[0]

class D_CCPSO2_Slave(object):
    def __init__(self, i_comm, i_rank, fun, max_number_of_fitness_evaluations, population_size, dimension_size, x_lower,
                 x_upper, example_num_max=4, max_stuck=2):

        # mpi通信子
        self.comm = i_comm

        # Slave代号
        self.rank = i_rank

        # 优化的问题
        self.fun = fun

        # 进行适应度评估的次数
        self.number_of_fitness_evaluations = 0
        self.max_number_of_fitness_evaluations = max_number_of_fitness_evaluations

        # 每一个子种群的粒子数量
        self.population_size = population_size

        # 优化问题维度
        self.dimension_size = dimension_size

        # 子种群大小可选集
        # self.group_size_set = [2, 5, 50, 100, 200]
        # self.group_size_set = [2, 5, 10]
        self.group_size_set = [2, 5, 10, 50, 100, 250]

        # 子种群维度size
        self.s = random.choice(self.group_size_set)
        # 子种群数量
        self.k = int(self.dimension_size / self.s)

        # 维度索引
        self.dimension_indices = list(range(self.dimension_size))

        # 粒子更新方式的选择
        self.p = 0.5

        # 上下界
        self.x_lower = x_lower
        self.x_upper = x_upper

        # 粒子位置
        self.X = (self.x_lower + np.random.rand(self.population_size, self.dimension_size) * (
                self.x_upper - self.x_lower)).astype(np.float64)

        # 粒子个人最好位置
        self.Y = self.X.copy()
        # 粒子相邻最好位置
        self.Y_local = self.X.copy()

        # example的数量
        self.example_num_max = example_num_max
        # Slave获得的榜样vector
        # self.Y_example = self.X[:self.example_num_max, :].copy()
        self.Y_example = np.empty([self.example_num_max, self.dimension_size + 1], dtype=np.float64)

        # 适应值存储
        self.fX = np.ones([self.population_size, self.k]) * np.inf
        self.fY = self.fX.copy()
        # Slave收到Master传来的目前全局最优值
        # self.fY_example = np.inf
        # self.fY_global = np.inf

        # 第一个子节点的进度条
        if self.rank == 1:
            self.pbar = tqdm(range(self.max_number_of_fitness_evaluations), desc="slave rank" + str(self.rank),
                             position=0)

        # 适应度初始化
        for i in range(self.population_size):
            f_this_line = self.fun(self.X[i])
            for j in range(self.k):
                self.fX[i, j] = f_this_line
                self.fY[i, j] = f_this_line

            self.number_of_fitness_evaluations += 1
            if self.rank == 1:
                self.pbar.update(n=1)

        # Y_local初始化
        for j in range(self.k):
            for i in range(self.population_size):
                local_i = self.local_best(j, i)
                for d in range(j * self.s, (j + 1) * self.s):
                    self.Y_local[i, self.dimension_indices[d]] = self.Y[local_i, self.dimension_indices[d]]

        # 全局最优位置未获得提升的轮数
        self.global_dont_change_i = 0
        # 未变更s的轮数
        self.s_dont_change_i = 0
        # xx轮未有提升，将触发：1、维度组合重组，2、子种群维度size重置，xx轮s未变更也将触发重置
        self.max_stuck = max_stuck

        # 历史全局最优适应值
        self.slave_gbest_history = []

    def b_x(self, j, i, example_i):
        particle_vector = self.Y_example[example_i].copy()
        for d in range(j * self.s, (j + 1) * self.s):
            particle_vector[self.dimension_indices[d]] = self.X[i, self.dimension_indices[d]]
        return particle_vector

    def b_y(self, j, i, example_i):
        particle_vector = self.Y_example[example_i].copy()
        for d in range(j * self.s, (j + 1) * self.s):
            particle_vector[self.dimension_indices[d]] = self.Y[i, self.dimension_indices[d]]
        return particle_vector

    def local_best(self, j, i):
        # v_i = self.fun(self.b(j, i, 'y'))
        v_i = self.fY[i, j]
        v_im1 = float('inf')
        v_ip1 = float('inf')
        if i != 0:
            # v_im1 = self.fun(self.b(j, i - 1, 'y'))
            v_im1 = self.fY[i - 1, j]
        if i == 0:
            v_im1 = self.fY[self.population_size - 1, j]
        if i != self.population_size - 1:
            # v_ip1 = self.fun(self.b(j, i + 1, 'y'))
            v_ip1 = self.fY[i + 1, j]
        if i == self.population_size - 1:
            v_ip1 = self.fY[0, j]

        if v_i < v_ip1 and v_i < v_im1:
            return i
        elif v_ip1 < v_im1:
            if i + 1 >= self.population_size:
                return 0
            return i + 1
        else:
            return i - 1

    def evolve(self):
        while 1:

            for j in range(self.k):
                for i in range(self.population_size):
                    for d in range(j * self.s, (j + 1) * self.s):
                        d_update = self.dimension_indices[d]
                        if random.random() <= self.p:
                            self.X[i, d_update] = self.Y[i, d_update] + cauchy.rvs(loc=0, scale=1, size=1) \
                                                  * abs(self.Y[i, d_update] - self.Y_local[i, d_update])
                        else:
                            self.X[i, d_update] = self.Y_local[i, d_update] + norm.rvs(loc=0, scale=1, size=1) \
                                                  * abs(self.Y[i, d_update] - self.Y_local[i, d_update])
                        self.X[i, d_update] = np.clip(self.X[i, d_update], self.x_lower, self.x_upper)

            # 给Master发一个tag=3的request
            req = self.comm.Isend(np.array([], dtype=np.float64), dest=0, tag=3)
            req.wait()
            # 请求最新的example vector
            req = self.comm.Irecv(buf=self.Y_example, source=0)
            req.wait()

            not_improve = True
            example_i = -1
            # example_i = self.example_num_max
            while not_improve:
                example_i += 1
                # example_i -= 1
                # if example_i >= self.example_num_max:
                #     break
                # if example_i < 0:
                #     break
                if example_i >= 1:
                    break
                for j in range(self.k):
                    for i in range(self.population_size):
                        # 适应度计算 (每个子种群的每个粒子)
                        self.fX[i, j] = self.fun(self.b_x(j, i, example_i=example_i)[:-1])
                        self.fY[i, j] = self.fun(self.b_y(j, i, example_i=example_i)[:-1])
                        self.number_of_fitness_evaluations += 2
                        if self.rank == 1:
                            self.pbar.update(n=2)
                        if self.fX[i, j] < self.fY[i, j]:
                            for d in range(j * self.s, (j + 1) * self.s):
                                self.Y[i, self.dimension_indices[d]] = self.X[i, self.dimension_indices[d]]
                            self.fY[i, j] = self.fX[i, j].copy()
                        # if self.fY[i, j] < self.Y_example[example_i, -1]:
                        if self.fY[i, j] < self.Y_example[0, -1]:
                            for d in range(j * self.s, (j + 1) * self.s):
                                self.Y_example[example_i, self.dimension_indices[d]] = self.Y[
                                    i, self.dimension_indices[d]]
                            self.Y_example[example_i, -1] = self.fY[i, j].copy()
                            not_improve = False

            if not_improve:
                self.global_dont_change_i += 1
            else:
                self.global_dont_change_i = 0

            if self.global_dont_change_i >= self.max_stuck:
                random.shuffle(self.dimension_indices)
                self.fX = np.ones([self.population_size, self.k]) * np.inf
                self.fY = self.fX.copy()

            if (self.global_dont_change_i >= self.max_stuck * 2) or (self.s_dont_change_i >= self.max_stuck * 5):
                self.s = random.choice(self.group_size_set)
                self.k = int(self.dimension_size / self.s)
                self.global_dont_change_i = 0
                self.s_dont_change_i = -1
                self.fX = np.ones([self.population_size, self.k]) * np.inf
                self.fY = self.fX.copy()

            self.s_dont_change_i += 1

            for j in range(self.k):
                for i in range(self.population_size):
                    local_i = self.local_best(j, i)
                    for d in range(j * self.s, (j + 1) * self.s):
                        self.Y_local[i, self.dimension_indices[d]] = self.Y[local_i, self.dimension_indices[d]]

            if not not_improve:
                # 把新gbest发给Master(with tag 2)
                req = self.comm.Isend(self.Y_example[example_i], dest=0, tag=2)
                req.wait()
                self.slave_gbest_history.append([self.number_of_fitness_evaluations, self.Y_example[example_i, -1]])

            if self.number_of_fitness_evaluations + (
                    self.k * self.population_size * self.example_num_max * 2) >= self.max_number_of_fitness_evaluations:
                # 给Master发一个tag=1的演化结束消息
                req = self.comm.Isend(np.array([], dtype=np.float64), dest=0, tag=1)
                req.wait()
                break

        if self.rank == 1:
            self.pbar.close()
            tqdm.write("Slave_1's self.number_of_fitness_evaluations = " + str(self.number_of_fitness_evaluations))
            tqdm.write("Slave rank" + str(self.rank) + " complete.\nWaiting for others...")

        pd.DataFrame(data=self.slave_gbest_history).to_csv('results/D_CCPSOw_Slave' + str(self.rank) + '_gbest_history.csv')


from collections import defaultdict

from twisted.application.service import IService, Service
from twisted.logger import Logger

from zope.interface import implementer

log = Logger()

@implementer(IService)
class HookableService(Service):

    """
    This service allows for other services in a Twisted Service tree to be
    notified whenever a certain kind of hook is triggered.

    During the service composition, one is expected to register
    a hook name with the name of the service that wants to react to the
    triggering of the hook. All the services, both hooked and listeners, should
    be registered against the same parent service.

    Upon the hook being triggered, the method "hook_<name>" will be called with
    the passed data in the listener service.
    """

    def register_hook(self, name, listener):
        if not hasattr(self, 'event_listeners'):
            self.event_listeners = defaultdict(list)
        log.debug('registering hook %s->%s' % (name, listener))
        self.event_listeners[name].append(listener)

    def trigger_hook(self, name, **data):

        def react_to_hook(listener, name, **kw):
            try:
                getattr(listener, 'hook_' + name)(**kw)
            except AttributeError:
                log.failure('Error while triggering hook')
                raise RuntimeError(
                    "Tried to notify a hook, but the listener "
                    "service class %s does not seem to have "
                    "defined the proper method: %s"
                    % (listener.__class__, 'hook_' + name))

        if not hasattr(self, 'event_listeners'):
            self.event_listeners = defaultdict(list)
        listeners = self._get_listener_services(name)
        if listeners:
            for listener in listeners:
                react_to_hook(listener, name, **data)

    def _get_sibling_service(self, name):
        if self.parent:
            return self.parent.getServiceNamed(name)

    def _get_listener_services(self, hook):
        if hook in self.event_listeners:
            service_names = self.event_listeners[hook]
            services = [
                self._get_sibling_service(name) for name in service_names]
            return services


#!/usr/bin/env python3

from PIL import Image
import os, json

FILE = "items"
PNG = f"{FILE}.png"
JSON = f"{FILE}.json"
TARGET_DIR = "icons"

os.makedirs(TARGET_DIR, exist_ok=True)

with open(JSON) as f:
    data = json.load(f)

with Image.open(PNG) as img:
    frames = data["textures"][0]["frames"]

    for frame in frames:
        fname = frame["filename"]
        pos = frame["frame"]
        x, y, w, h = (pos[c] for c in ("x", "y", "w","h"))
        part = img.crop((x, y, x+w, y+h))
        part.save(f"{TARGET_DIR}/{fname}")


#!/usr/bin/env python

"""
    model.py
"""

import torch
from torch import nn
from torch.nn import functional as F

class Block(nn.Sequential):
    def __init__(self, in_channels, out_channels):
        super().__init__(
            nn.Conv2d(in_channels, out_channels, 3, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(out_channels),
            nn.MaxPool2d(2)
        )

class SimpleEncoder(nn.Module):
    def __init__(self, in_channels=1, hidden_dims=[64, 64, 64, 64]):
        super().__init__()

        self.layers = nn.Sequential(
            Block(in_channels, hidden_dims[0]),
            Block(hidden_dims[0], hidden_dims[1]),
            Block(hidden_dims[1], hidden_dims[2]),
            Block(hidden_dims[2], hidden_dims[3]),
        )

        self.out_channels = hidden_dims[-1]

    def forward(self, x):
        batch_of_tasks = len(x.shape) == 5
        if batch_of_tasks:
            b, s, c, w, h = x.shape
            x = x.view(b * s, c, w, h)

        x = self.layers(x)
        x = x.mean(dim=(2, 3), keepdim=True).squeeze(-1).squeeze(-1)

        if batch_of_tasks:
            x = x.view(b, s, -1)

        return x

class FastHead(nn.Module):
    def __init__(self, in_channels, out_channels, n_steps, lr=0.1):
        super().__init__()

        self.in_channels  = in_channels
        self.out_channels = out_channels
        self.n_steps      = n_steps

        self.lr      = nn.Parameter(torch.FloatTensor([lr]))
        self.scale   = nn.Parameter(torch.FloatTensor([1]))
        self.weights = nn.Parameter(torch.ones(in_channels, 1))

    def forward(self, x, y):
        batch_of_tasks = len(x.shape) == 3
        if batch_of_tasks:
            b, s, c = x.shape

        w = (self.scale * self.weights)

        if batch_of_tasks:
            w = w.unsqueeze(0).repeat((b, 1, self.out_channels))
        else:
            w = w.repeat((1, self.out_channels))

        for _ in range(self.n_steps):
            out  = x @ w
            loss = F.cross_entropy(out.view(-1, self.out_channels), y.view(-1), reduction='sum')
            grad = torch.autograd.grad(loss, [w], create_graph=True)[0]
            w    = w - self.lr * grad

        return w

class EZML(nn.Module):
    def __init__(self, encoder, n_classes, inner_steps=1):
        super().__init__()

        self.encoder    = encoder
        self.model_head = FastHead(encoder.out_channels, n_classes, n_steps=inner_steps)

    def forward(self, x_sup, y_sup, x_tar):
        enc_sup = self.encoder(x_sup)
        fast_w  = self.model_head(enc_sup, y_sup)
        return self.encoder(x_tar) @ fast_w


import json
from rest_framework import status
from rest_framework.test import APITestCase
from core.tests.base import BaseTestCase
from django.contrib.auth.models import Group, User, Permission
from core.management.commands.users_and_groups import DEFAULT_DEV_ENV_PASS

class GroupsTests(BaseTestCase):

    def test_get_groups_list(self):
        """
        Ensure we can create a new get all groups as admin
        """
        headers = self.auth_headers_for_user("admin")
        response = self.client.get("/api/groups/", follow=True, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(Group.objects.count(), 3)

    def test_groups_api_when_unauthenticated(self):
        response = self.client.get("/api/groups", follow=True)
        self.assertEqual(401, response.status_code)

class GroupPermissionsTests(BaseTestCase):

    def create_new_user_and_group(self):
        """
        helper method of reusable group creation logic
        """

        external_provider = "pharmacist"

        email = "{}@{}.com".format(external_provider, external_provider)
        u = User.objects.create_user(username=external_provider, email=email)
        u.set_password(DEFAULT_DEV_ENV_PASS)
        u.save()

        group_object = Group.objects.create(name=external_provider)
        u.groups.add(group_object)

        return group_object

    def test_new_external_provider_creation(self):
        """
        ensure new group and user are created
        """

        old_group_count = Group.objects.count()
        old_user_count = User.objects.count()

        self.create_new_user_and_group()

        new_group_count = Group.objects.count()
        new_user_count = User.objects.count()

        self.assertEqual((old_group_count + 1), new_group_count)
        self.assertEqual((old_user_count + 1), new_user_count)

    def test_new_external_provider_in_correct_group(self):
        """
        ensure new user in new group
        """

        self.create_new_user_and_group()

        group = Group.objects.get(name="pharmacist")
        user = User.objects.get(username="pharmacist")

        self.assertTrue(group.pk in [group.pk for user in user.groups.all()])

    def test_un_permissioned_group_access_denied(self):
        """
        ensure new un-permissioned users/groups denied
        """

        self.create_new_user_and_group()

        headers = self.auth_headers_for_user('pharmacist')
        response_1 = self.client.get("/api/medications/", follow=True, **headers)
        self.assertEqual(response_1.status_code, status.HTTP_403_FORBIDDEN)

        response_2 = self.client.get("/api/uds/", follow=True, **headers)
        self.assertEqual(response_2.status_code, status.HTTP_403_FORBIDDEN)

    def test_new_external_provider_access(self):
        """
        ensure new users/groups have the assigned permissions
        """

        med_permission_id = Permission.objects.get(codename="view_medication").pk

        group_object = self.create_new_user_and_group()
        group_object.permissions.add(med_permission_id)

        headers = self.auth_headers_for_user("pharmacist")
        response_1 = self.client.get("/api/medications/", follow=True, **headers)
        self.assertEqual(response_1.status_code, status.HTTP_200_OK)

        response_2 = self.client.get("/api/uds/", follow=True, **headers)
        self.assertEqual(response_2.status_code, status.HTTP_403_FORBIDDEN)