from typing import Any, Iterable, Optional
from collections.abc import MutableSequence

from node import Node, DoubleLinkedNode

class LinkedList(MutableSequence):
    CLASS_NODE = Node

    def __init__(self, data: Iterable = None):
        """Конструктор связного списка"""
        self._len = 0
        self._head: Optional[Node] = None
        self._tail = self._head

        if data is not None:
            for value in data:
                self.append(value)

    def insert(self, index: int, value) -> None:
        if not isinstance(index, int):
            raise TypeError()
        insert_node = Node(value)
        if index == 0:
            insert_node.next = self._head
            self._head = insert_node
            self._len += 1

        elif index >= self._len - 1:
            self.append(value)
        else:
            prev_node = self.step_by_step_on_nodes(index - 1)
            next_node = prev_node.next
            self.linked_nodes(prev_node, insert_node)
            self.linked_nodes(insert_node, next_node)
            self._len += 1

    def __delitem__(self, index: int) -> None:

        """Удаление узла по индексу"""
        if not isinstance(index, int):
            raise TypeError()

        if not 0 <= index < self._len:
            raise IndexError()

        if index == 0:
            self._head = self._head.next
        elif index == self._len - 1:
            tail = self.step_by_step_on_nodes(index-1)
            tail.next = None
            self._tail = tail
        else:
            prev_node = self.step_by_step_on_nodes(index-1)
            del_node = prev_node.next
            next_node = del_node.next
            self.linked_nodes(prev_node, next_node)

        self._len -= 1

    def __len__(self) -> int:
        return self._len

    def append(self, value: Any):
        """ Добавление элемента в конец связного списка. """
        append_node = self.CLASS_NODE(value)

        if self._head is None:
            self._head = self._tail = append_node
        else:
            self.linked_nodes(self._tail, append_node)
            self._tail = append_node

        self._len += 1

    def step_by_step_on_nodes(self, index: int) -> Node:
        """ Функция выполняет перемещение по узлам до указанного индекса. И возвращает узел. """
        if not isinstance(index, int):
            raise TypeError()

        if not 0 <= index < self._len:
            raise IndexError()

        current_node = self._head
        for _ in range(index):
            current_node = current_node.next

        return current_node

    @staticmethod
    def linked_nodes(left_node: Node, right_node: Optional[Node] = None) -> None:
        """
        Функция, которая связывает между собой два узла.
        """
        left_node.next = right_node

    def __getitem__(self, index: int) -> Any:
        """ Метод возвращает значение узла по указанному индексу. """
        node = self.step_by_step_on_nodes(index)
        return node.value

    def __setitem__(self, index: int, value: Any) -> None:
        """ Метод устанавливает значение узла по указанному индексу. """
        node = self.step_by_step_on_nodes(index)
        node.value = value

    def to_list(self) -> list:
        return [linked_list_value for linked_list_value in self]

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({self.to_list()})"

    def __str__(self) -> str:
        return f"{self.to_list()}"

class DoubleLinkedList(LinkedList):
    CLASS_NODE = DoubleLinkedNode

    @staticmethod
    def linked_nodes(left_node: DoubleLinkedNode, right_node: Optional[DoubleLinkedNode] = None) -> None:
        left_node.next = right_node
        right_node.prev = left_node

class NewLinkedList(LinkedList):

    def remove_(self, x):
        # s = 0
        # for i in range(self._len):
        #     s += 1
        #     if self[i] == x:
        #         self.__delitem__(i)  # del self[i]
        #         break
        #     if self._len == s:
        #         raise TypeError

        for i, value in enumerate(self):
            if value == x:
                self.__delitem__(i)  # del self[i]
                break

        if i == len(self) - 1:
            raise TypeError

if __name__ == "__main__":
    list_ = [5, 2, 3, 7, 9]
    linked_list = LinkedList(list_)
    print(linked_list)
    print(linked_list.__repr__())

    linked_list_2 = NewLinkedList(list_)
    linked_list_2.remove_(5)
    print(linked_list_2)

Зачетное задание/Зачетное задание/DoubleLinkedList/task.py


import pybullet
import numpy as np
import os

def get_control_input(control_policy, q: np.ndarray, v: np.ndarray):
    nJ = control_policy.tauJ.shape[0]
    qJ = q[-nJ:]
    dqJ = v[-nJ:]
    return control_policy.tauJ - control_policy.Kp @ (control_policy.qJ-qJ) \
                               - control_policy.Kd @ (control_policy.dqJ-dqJ)

class MPCSimulation(object):
    def __init__(self, simulator):
        self.simulator = simulator

    def run(self, mpc, t0: float, q0: np.ndarray, simulation_time: float,
            feedback_policy: bool=False, feedback_delay: bool=False,
            simulation_steps_per_mpc_update: int=1, verbose: bool=False,
            log: bool=False, record: bool=False, name: str='mpc_sim'):
        assert simulation_steps_per_mpc_update >= 1
        self.simulator.init_simulation(t0, q0)
        self.name = name
        if log:
            log_dir = os.path.join(os.getcwd(), name+"_log")
            self.log_dir = log_dir
            os.makedirs(log_dir, exist_ok=True)
            q_log = open(os.path.join(log_dir, "q.log"), mode='w')
            v_log = open(os.path.join(log_dir, "v.log"), mode='w')
            u_log = open(os.path.join(log_dir, "u.log"), mode='w')
            t_log = open(os.path.join(log_dir, "t.log"), mode='w')
            kkt_log = open(os.path.join(log_dir, "kkt.log"), mode='w')
        if record:
            pybullet.startStateLogging(pybullet.STATE_LOGGING_VIDEO_MP4, name+".mp4")

        use_feedback_policy = (simulation_steps_per_mpc_update >= 2) and feedback_policy
        inner_loop_count = simulation_steps_per_mpc_update - 1
        dt = self.simulator.time_step
        while self.simulator.get_time() < t0 + simulation_time:
            t = self.simulator.get_time()
            q, v = self.simulator.get_state()
            if verbose:
                print('t = {:.6g}:'.format(t))
            if feedback_delay:
                if use_feedback_policy:
                    u = get_control_input(mpc.get_control_policy(t), q, v)
                else:
                    u = mpc.get_initial_control_input().copy()
            if inner_loop_count == 0:
                mpc.update_solution(t, simulation_steps_per_mpc_update*dt, q, v)
                inner_loop_count = simulation_steps_per_mpc_update - 1
            else:
                inner_loop_count -= 1
            kkt_error = mpc.KKT_error(t, q, v)
            if verbose:
                print('KKT error = {:.6g}'.format(kkt_error))
                print('')
            if not feedback_delay:
                if use_feedback_policy:
                    u = get_control_input(mpc.get_control_policy(t), q, v)
                else:
                    u = mpc.get_initial_control_input().copy()
            self.simulator.step_simulation(u)
            if log:
                np.savetxt(q_log, [q])
                np.savetxt(v_log, [v])
                np.savetxt(u_log, [u])
                np.savetxt(t_log, np.array([t]))
                np.savetxt(kkt_log, np.array([kkt_error]))

        if log:
            q_log.close()
            v_log.close()
            u_log.close()
            t_log.close()
            kkt_log.close()
            self.q_log = os.path.abspath(os.path.join(log_dir, "q.log"))
            self.v_log = os.path.abspath(os.path.join(log_dir, "v.log"))
            self.u_log = os.path.abspath(os.path.join(log_dir, "u.log"))
            self.t_log = os.path.abspath(os.path.join(log_dir, "t.log"))
            self.kkt_log = os.path.abspath(os.path.join(log_dir, "kkt.log"))
            self.log_dir = os.path.abspath(log_dir)
            print('Logs are saved at ' + self.log_dir)

        if record:
            self.simulator.disconnect()


import pathlib
from typing import List

def locate_files(path: str, file_type: str) -> List[pathlib.Path]:
    top_path: pathlib.Path = pathlib.Path(path)

    if "." not in file_type:
        print("File types must include a period eg, '.md'")

    located_files: List[pathlib.Path] = []

    for found_file_path in top_path.glob(f"**/*{file_type}"):
        if file_type != ".ipynb":
            located_files.append(found_file_path)
        else:
            if "checkpoint" not in found_file_path.name:
                located_files.append(found_file_path)

    return located_files


import shutil
import tempfile

from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.shortcuts import get_object_or_404
from django.test import Client, TestCase, override_settings
from django.urls import reverse

from ..forms import PostForm
from ..models import Comment, Group, Post, User

TEMP_MEDIA_ROOT = tempfile.mkdtemp(dir=settings.BASE_DIR)

@override_settings(MEDIA_ROOT=TEMP_MEDIA_ROOT)
class CreateFormTests(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user = User.objects.create_user(username='Leo_test')
        cls.group = Group.objects.create(
            title='test_title',
            slug='test_group',
            description='test_description',
        )
        cls.post = Post.objects.create(
            author=cls.user,
            group=CreateFormTests.group,
            text="Test_text",
        )
        cls.form = PostForm()

    def setUp(self):
        self.authorized_client = Client()
        self.authorized_client.force_login(CreateFormTests.user)
        self.new_text = 'Тест титульника'
        self.update_text = 'Обновленная'
        self.new_comment = 'Невозможно, но факт!'

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()
        shutil.rmtree(TEMP_MEDIA_ROOT, ignore_errors=True)

    def test_create_post(self):
        """Тестируем создание поста через форму"""
        post_count = Post.objects.count()
        small_gif = (
            b'\x47\x49\x46\x38\x39\x61\x02\x00'
            b'\x01\x00\x80\x00\x00\x00\x00\x00'
            b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00'
            b'\x00\x00\x00\x2C\x00\x00\x00\x00'
            b'\x02\x00\x01\x00\x00\x02\x02\x0C'
            b'\x0A\x00\x3B'
        )
        uploaded = SimpleUploadedFile(
            name='small.gif',
            content=small_gif,
            content_type='image/gif'
        )
        form_data = {
            'group': CreateFormTests.group.id,
            'text': self.new_text,
            'image': uploaded,
        }
        response = self.authorized_client.post(
            reverse('posts:post_create'),
            data=form_data,
            follow=True
        )
        self.assertRedirects(response, reverse(
            'posts:profile',
            kwargs={'username': CreateFormTests.post.author})
        )
        self.assertEqual(Post.objects.count(), post_count + 1)
        new_post = Post.objects.latest('pub_date')
        self.assertEqual(new_post.text, self.new_text)
        self.assertEqual(new_post.group, CreateFormTests.group)
        self.assertEqual(new_post.author, CreateFormTests.user)
        self.assertTrue(new_post.image, uploaded)

    def test_edit_post(self):
        """Тестируем корректировку поста через форму."""
        form_data = {
            'group': CreateFormTests.group.id,
            'text': self.update_text,
        }
        response = self.authorized_client.post(reverse(
            'posts:post_edit',
            kwargs={'post_id': self.post.id}),
            data=form_data, follow=True
        )
        self.assertRedirects(response, reverse(
            'posts:post_detail',
            kwargs={'post_id': self.post.id})
        )
        upd_post = get_object_or_404(Post, id=self.post.id)
        self.assertEqual(upd_post.text, self.update_text)
        self.assertEqual(upd_post.group, CreateFormTests.group)
        self.assertEqual(upd_post.author, CreateFormTests.user)

    def test_create_post_non_auth(self):
        """Тестируем создание поста анонимом."""
        post_count = Post.objects.count()
        form_data = {
            'group': CreateFormTests.group.id,
            'text': self.new_text,
        }
        response = self.client.post(
            reverse('posts:post_create'),
            data=form_data,
            follow=True
        )
        self.assertRedirects(response, (
            reverse('users:login') + '?next=' + reverse(
                'posts:post_create'
            )))
        self.assertEqual(Post.objects.count(), post_count)

    def test_edit_post_non_auth(self):
        """Тестируем корректировку поста анонимом."""
        small_gif = (
            b'\x47\x49\x46\x38\x39\x61\x02\x00'
            b'\x01\x00\x80\x00\x00\x00\x00\x00'
            b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00'
            b'\x00\x00\x00\x2C\x00\x00\x00\x00'
            b'\x02\x00\x01\x00\x00\x02\x02\x0C'
            b'\x0A\x00\x3B'
        )
        uploaded = SimpleUploadedFile(
            name='small.gif',
            content=small_gif,
            content_type='image/gif'
        )
        form_data = {
            'image': uploaded,
            'text': self.update_text,
        }
        response = self.client.post(reverse(
            'posts:post_edit',
            kwargs={'post_id': CreateFormTests.post.id}),
            data=form_data, follow=True
        )
        upd_post = get_object_or_404(Post, id=self.post.id)
        self.assertNotEqual(upd_post.text, self.update_text)
        self.assertNotEqual(upd_post.image, uploaded)

        self.assertRedirects(response, (
            reverse(
                'users:login') + '?next=' + reverse(
                    'posts:post_edit', kwargs={'post_id': self.post.id}
            )))

    def test_comment_create_auth_user(self):
        """
        Проверяем что комментарий возможно создать
        авторизованному пользователю.
        """
        count_posts_comments = Comment.objects.count()
        form_data = {
            'text': self.new_comment,
        }
        response = self.authorized_client.post(
            reverse('posts:add_comment', args=[self.post.id]),
            data=form_data,
            follow=True,
        )
        count_comments_after_request = Comment.objects.count()
        create_comment = Comment.objects.first()
        self.assertEqual(
            count_comments_after_request,
            count_posts_comments + 1
        )
        self.assertEqual(create_comment.text, self.new_comment)
        self.assertEqual(create_comment.author, self.user)
        self.assertRedirects(
            response,
            reverse('posts:post_detail', kwargs={'post_id': self.post.id})
        )

    def test_comment_create_non_auth(self):
        """
        Проверяем что аноним не может комментрировать посты.
        """
        count_posts_comments = Comment.objects.count()
        form_data = {
            'text': self.new_comment,
        }
        self.client.post(
            reverse('posts:add_comment', args=[self.post.id]),
            data=form_data,
            follow=True,
        )
        count_comments_after_request = Comment.objects.count()
        self.assertNotEqual(
            count_comments_after_request,
            count_posts_comments + 1
        )


version_info = (0, 1, 0)
__version__ = '.'.join(map(str, version_info[:3])) + ''.join(version_info[3:])

import os
import neovim
import logging
import argparse, io
import traceback
import shlex
from jupyter_nvim.nvimapp import (
    JupyterNvimBufferApp,
    JupyterNvimApp,
)

logger = logging.getLogger(__name__)
# if 'JUPYTER_NVIM_LOGFILE' in os.environ:
#     logfile = os.environ['NVIM_IPY_DEBUG_FILE'].strip()
#     logger.addHandler(logging.FileHandler(logfile, 'w'))
#     logger.level = logging.DEBUG

class HelpActionHandled(Exception):
    pass

class PrintHelpAction(argparse._HelpAction):
    def __call__(self, parser, namespace, values, option_string=None):
        file = io.StringIO()
        parser.print_help(file=file)
        raise HelpActionHandled(file.getvalue())

@neovim.plugin
# @neovim.encoding(True)
class TestPlugin(object):
    def __init__(self, nvim):
        self.nvim = nvim
        self.app = None
        self.log = logger
        self.log.info('Plugin initialized')
        self._init_arg_parser()

    def _init_arg_parser(self):
        def add_help(parser):
            parser.add_argument('-h', '--help', action=PrintHelpAction, default=argparse.SUPPRESS,
                                help='show this help message and exit')

        self.parser = argparse.ArgumentParser(add_help=False)
        add_help(self.parser)
        subparsers = self.parser.add_subparsers()

        def add_parser(name, **kwargs):
            require_childid = kwargs.pop('require_childid', False)
            allow_unknown = kwargs.pop('allow_unknown', False)

            subparser = subparsers.add_parser(name, add_help=False, **kwargs)
            add_help(subparser)
            subparser.set_defaults(subcommand=name)

            def check_unknown(unknowns):
                if not allow_unknown and unknowns:
                    raise ValueError('Unknown options {}'.format(unknowns))
            subparser.set_defaults(check_unknown=check_unknown)

            if require_childid is not None:
                subparser.add_argument('-n', '--name', required=False, help='childid, child app identity', dest='childid')
            return subparser

        init = add_parser('init', help='initialize main app', require_childid=None)

        quit = add_parser('quit', help='quit main app', require_childid=None)
        quit.add_argument('childid', nargs='*', help='if has positional args, quit those child apps. Otherwise, quit all child apps')

        start = add_parser('start', help='start subapp', require_childid=True)
        start.add_argument('app_argv', nargs='*', help='argv for subapp')
        start.add_argument('--out', '-o', action='append', help='out buffer')
        start.add_argument('--inout', '--io', '-i', action='append', help='inout buffer')

        execute = add_parser('execute', help='execute code')
        execute.add_argument('code', nargs='+', help='code')

        run = add_parser('run', help='run code, specified by range')

    @neovim.command('JupyterTestCommand', nargs="*", range=True)
    def testcommand(self, args, range):
        message = 'Command with args: {}, range: {}'.format(args, range)
        self.nvim.current.line = message
        logger.info(message)
        # self.zerorpc.print(message)

    @neovim.function('JupyterTestFunction', sync=True)
    def testsyncfunction(self, args):
        logger.info('test sync function: {}'.format(args))
        return args

    # allow_nested allows async functions to run when another sync function is being called
    # async function cannot return value
    @neovim.function('JupyterTestAsyncNestedFunction', sync=False)
    def testfunction(self, args):
        logger.info('test async nested function {}'.format(args))

    def initialize(self):
        if self.app is None:
            self.app = JupyterNvimApp()
            self.app.initialize(self.nvim, [])
            self.log = self.app.log
            self.log.info('App started')

    @neovim.function('Jupyter', sync=False)
    def main(self, raw_args):
        self.initialize()
        if isinstance(raw_args[0], dict):
            options = raw_args.pop(0)
        else:
            options = {}
        cbuf = self.nvim.current.buffer
        from_commandline = options.get('cmd', False)
        if from_commandline:
            cooked_args = []
            for arg in raw_args:
                cooked_args.extend(shlex.split(arg))
        else:
            cooked_args = raw_args

        self.log.info('%s: %s', options, cooked_args)

        try:
            args, unknown = self.parser.parse_known_args(cooked_args)
            args.check_unknown(unknown)
            if args.subcommand == 'init':
                pass
            elif args.subcommand == 'quit':
                if args.childid:
                    for childid in args.childid:
                        if childid in self.app:
                            self.app.quit_app(childid)
                            self.log.info('quit child app %s', childid)
                        else:
                            self.log.error('childid %s does not exist', childid)
                else:
                    self.quit()
                    self.log.info('quit all child apps and reset main app')
            elif args.subcommand == 'start':
                bufapp = self.app.start_child_app(args.childid, args.app_argv)
                has_buffer = False
                if args.out:
                    has_buffer = bufapp.register_out_buffer(*args.out) or has_buffer
                if args.inout:
                    has_buffer = bufapp.register_inout_buffer(*args.inout) or has_buffer
                if not has_buffer:
                    bufapp.register_out_buffer(cbuf.number)
                self.log.info('subapp started')
            elif args.subcommand == 'execute':
                code = '\n'.join(args.code)
                self.app('execute', code, args.childid)
                self.log.info('execute %s', code)
            elif args.subcommand == 'run':
                line1 = options.get('line1')
                line2 = options.get('line2')
                self.app('execute', '\n'.join(cbuf[line1-1:line2]))

        except HelpActionHandled as ex:
            self.echo(str(ex))
        except Exception as ex:
            buf = io.StringIO()
            traceback.print_exc(limit=None, file=buf)
            self.log.error('parse args error %s:\n%s', cooked_args, buf.getvalue())

    def echo(self, msg):
        self.nvim.vars['jupyter_nvim_msg'] = msg
        self.nvim.command('echo jupyter_nvim_msg')

    @neovim.shutdown_hook
    def shutdown_hook(self):
        self.quit()

    def quit(self):
        if self.app:
            self.app.quit()
            self.app = None
        self.log.info('quit app ...')


"""Added entry in KebechetGithubInstallation table for environment name.

Revision ID: 5c1e7425ce60
Revises: 65ae36e5c38a
Create Date: 2021-08-10 15:38:46.589634+00:00

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "5c1e7425ce60"
down_revision = "65ae36e5c38a"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("kebechet_github_installations", sa.Column("runtime_environment_name", sa.Text(), nullable=True))
    op.create_unique_constraint(None, "kebechet_github_installations", ["slug", "runtime_environment_name"])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "kebechet_github_installations", type_="unique")
    op.drop_column("kebechet_github_installations", "runtime_environment_name")
    # ### end Alembic commands ###


import pexpect
import sys
import os
import subprocess

### ADD NEW TESTS HERE ###
TESTCMDS = [
    ["/bin/ls"],
    [""],
    ["/bin/uname"],
    ["/bin/uname -a"],
    ["     "],
    ["abc"],
    ["echo hello world    how are you??"],
    [" echo -n hello"],
    ["echo \"cat lol.c | cat > lol.c\""],
    ["ls \"\""],
    ["echo $"],
    ["$"],
    ["echo $US"],
    ["echo $USER"],
    ["echo $USER:$USER"],
    ["echo \"$USER:$USER\""]
    ]

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

TESTLOGPATH = os.path.expandvars("$HOME") + "/minishell_tester/testlogs/"
MINISHELLPATH = "./minishell"
ARGC = len(sys.argv)
for arg in sys.argv[1:]:
    if '$' in arg:
        os.environ['PROMPT'] = os.path.expandvars(arg)
        PROMPT = os.path.expandvars(arg)
        break

def referenceresult(minishell, bash_result):
    try:
        minishell.expect_exact(PROMPT, timeout=1)
        if (minishell.before.decode() == bash_result):
            return bcolors.OKGREEN + "OK" + bcolors.ENDC
        return bcolors.FAIL + "KO" + bcolors.ENDC
    except:
        return bcolors.FAIL + "KO" + bcolors.ENDC

def get_bash_result(bash, cmd):
    bash.sendline("export PS1='" + PROMPT + "'")
    bash.expect_exact("\r\n")
    bash.expect_exact(PROMPT)
    bash.sendline(cmd)
    bash.expect_exact(cmd + "\r\n")
    bash.expect_exact(PROMPT)
    return bash.before.decode()

def test(cmdlist, testnum):
    try:
        minishell = pexpect.spawn(MINISHELLPATH)
    except:
        print("Couldn't open minishell. Make sure to execute the tester in the folder where your Makefile and minishell binary are located.")
        exit(1)
    minishell_logfile = TESTLOGPATH + str(testnum).zfill(3) + "_testoutput_minishell.log"
    minishell.logfile_read = open(minishell_logfile, "wb")

    bash = pexpect.spawn("bash")
    bash_logfile = TESTLOGPATH + str(testnum).zfill(3) + "_testoutput_bash.log"
    bash.logfile_read = open(bash_logfile, "wb")

    print(bcolors.HEADER + bcolors.BOLD + f"\nTest {testnum:03d}" + bcolors.ENDC)
    for i, cmd in enumerate(cmdlist):
        bash_result = get_bash_result(bash, cmd)
        print(f"{'Command:':10}{cmd}")
        print(f"{'Expected:':10}{bash_result}")
        minishell.sendline(cmd)
        minishell.expect_exact(cmd + "\r\n")
        print(referenceresult(minishell, bash_result))
    minishell.logfile_read.close()
    bash.logfile_read.close()
    minishell.sendline("exit")
    bash.sendline("exit")

def print_welcome():
    print(bcolors.UNDERLINE + bcolors.BOLD + bcolors.OKBLUE + "\nminishell Tester\n" + bcolors.ENDC)
    print("All results will be compared to your machine's bash")

def print_usage():
    print("Usage: minishell_tester '<your_minishell_prompt_in_single_quotes>' [testnumber]")
    print("(Execute in the root directory of your minishell repo)\n")
    print("Example: Execute All Tests")
    print("minishell_tester 'minishell$ '\n")
    print("Example: Execute Only Test No. 5")
    print("minishell_tester 'minishell$ ' 5\n")
    print("Options:")
    print("  -h, --help\tprint this help essage")
    print("  -u, --update\tupdate minishell_tester")

def build_minishell():
    print("")
    print(bcolors.HEADER + "Executing your Makefile..." + bcolors.ENDC)
    try:
        subprocess.run(["make"])
        print("")
    except:
        print("Couldn't execute Makefile. Make sure to execute the tester in the folder where your Makefile and minishell binary are located.")
        exit(1)

def print_logfile_info():
    print(bcolors.WARNING + "Test logs can be found in $HOME/minishell_tester/testlogs" + bcolors.ENDC)

def execute_tests():
    os.makedirs(TESTLOGPATH, exist_ok=True)
    if ARGC > 2:
        print(bcolors.HEADER + "Executing Test..." + bcolors.ENDC)
        test(TESTCMDS[int(sys.argv[2])], int(sys.argv[2]))
    else:
        print(bcolors.HEADER + "Executing Tests..." + bcolors.ENDC)
        for testnum, cmdlist in enumerate(TESTCMDS):
            test(cmdlist, testnum)

def main():
    for arg in sys.argv[1:]:
        if '$' in arg:
            os.environ['PROMPT'] = os.path.expandvars(arg)
            PROMPT = os.path.expandvars(arg)
            break
    if "-addtest" in sys.argv or "--addtest" in sys.argv:
    # Use subprocess to run the update script
        subprocess.run(["python3", os.path.expandvars("$HOME") + "/minishell_tester/interactive.py", os.environ['PROMPT']])
        exit(0)
    if "-u" in sys.argv or "--update" in sys.argv:
    # Use subprocess to run the update script
        subprocess.run(["python3", os.path.expandvars("$HOME") + "/minishell_tester/check_for_update.py"])
        exit(0)
    if "-h" in sys.argv or "--help" in sys.argv:
        print_usage()
        exit(0)
    if ARGC == 1 or ARGC > 3:
        print(bcolors.WARNING + "Error: Wrong number of arguments\n" + bcolors.ENDC)
        print_usage()
        exit(1)
    print_welcome()
    build_minishell()
    #output = subprocess.check_output(["./minishell"])
    #print(output.decode())
    execute_tests()
    print_logfile_info()

if __name__ == "__main__":
    main()


import tensorflow as tf
import numpy as np
import pandas as pd
from tensorflow.keras.datasets import mnist
from keras.layers import Dense, Flatten, MaxPooling2D, Dropout, Conv2D, Dropout
from keras.models import Sequential, load_model
from keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.applications.resnet50 import ResNet50
from sklearn.model_selection import RandomizedSearchCV
from keras.callbacks import ModelCheckpoint, EarlyStopping
import matplotlib.pyplot as plt
from PIL import Image
import os

earlystop = EarlyStopping(monitor = "val_accuracy", patience = 3)
model_save = ModelCheckpoint('BloodCells.hdf5',
                            save_best_only=True)

classes = ["Eosinophil", "Lymphocite", "Monocyte", "Neutrophil"]
labels = [0 for i in range(2497)] + [1 for i in range(2483)] + [2 for i in range(2478)] + [3 for i in range(2499)]
labelsY = [0 for i in range(623)] + [1 for i in range(620)] + [2 for i in range(620)] + [3 for i in range(624)]
labels = np.array(labels)
labelsY = np.array(labelsY)
trainX = []
testX = []
for files in os.listdir("TRAIN"):
    for image in os.listdir(f"TRAIN/{files}"):
        images = Image.open(f"TRAIN/{files}/{image}")
        images = images.resize((160, 120))
        images = np.array(images)
        trainX.append(images)
trainX = np.array(trainX)
trainX = trainX / 255.0
print(trainX.shape)

for files in os.listdir("TEST"):
    for image in os.listdir(f"TEST/{files}"):
        images = Image.open(f"TEST/{files}/{image}")
        images = images.resize((160, 120))
        images = np.array(images)
        testX.append(images)
testX = np.array(testX)
testX = testX / 255.0
print(testX.shape)
model = Sequential()
model.add(Conv2D(32, kernel_size = (3, 3), activation = 'relu', input_shape = (120, 160, 3)))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(64, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(128, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(256, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(4, activation = 'softmax'))
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()
history = model.fit(trainX, labels, epochs=1000, validation_data = (testX, labelsY), callbacks = [earlystop, model_save], batch_size = 125)
score = model.evaluate(testX, labelsY, verbose = 0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])

def check_acc(model, X_test, y_test):
    incorrect = 0
    correct = 0
    for i in range(len(X_test)):
        prediction = model.predict(X_test[i].reshape(1, 120, 160, 1)).argmax()
        actual = y_test[i]
        if int(prediction) != int(actual):
            incorrect += 1
        else:
            correct += 1

    fig = plt.figure()
    plt.ylabel("Amount")
    plt.xlabel(f"Accuracy: {(correct/(correct+incorrect))*100}%")
    plt.bar(["correct", "incorrect"], [correct, incorrect])
    plt.show()
    print(f"Accuracy: {(correct/(correct+incorrect))*100}%")

#check_acc(model, testX, labelsY)

def plothist(history, metric):
    plt.figure()
    if metric == "accuracy":
        plt.plot(history.history['accuracy'])
        plt.plot(history.history['val_accuracy'])
        plt.title('Model Accuracy')
        plt.ylabel("Accuracy")
    elif metric == "loss":
        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title("Model Loss")
        plt.ylabel("Loss")
    plt.xlabel("Epoch")
    plt.legend(["Train", "Test"])
    plt.show()

plothist(history, "accuracy")
plothist(history, "loss")


from keras.models import load_model
import cv2
import numpy as np

CAT = 0
DOG = 1

IMAGE_WIDTH=128
IMAGE_HEIGHT=128
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)
IMAGE_CHANNELS=3
model = load_model('model.h5')

def predict(bytes_image):
    buf = np.frombuffer(bytes_image, dtype=np.uint8)
    img = cv2.imdecode(buf, flags=cv2.IMREAD_COLOR)
    img = cv2.resize(img, (128, 128))
    img = img[:128, :128]
    img = img / 255
    img = np.expand_dims(img, axis=0)
    p = model.predict(img)
    p = p.argmax(axis=1)
    return p


from django.urls import path, include

from .views import login_page, logout_page,register

app_name = 'user'

urlpatterns = [
    path('login/', login_page, name='login'),
    path('logout/', logout_page, name='logout'),
    path('register/', register, name='register'),
]


import os
import time
import rtmidi
from fractal_noise import FractalNoiseGenerator
from midi_coms import MidiOutWrapper
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--list', '-l', action='store_true', help='List available midi devices and exit')
parser.add_argument('--midi_device', '-m', help='Overwrite default midi device, '\
                                                'a substring match will do (run with -l to show available devices)')
parser.add_argument('--hide_graph', '-g', action='store_true', help='Hide debug window')
parser.add_argument('--print', '-p', action='store_true', help='Print value sent to midi device')

args = parser.parse_args()
list_midi_devices_only = args.list
if 'posix' in os.name:
    midi_port_search_term = 'Claret'
else:
    midi_port_search_term = 'Focusrite'
if args.midi_device:
    midi_port_search_term = args.midi_device
print_value = args.print

# minilogue_xd midi settings
MINILOGUE_MIDI_VCF_CUTOFF_CC = 43
MINILOGUE_MIDI_VCF_MIN = 44
MINILOGUE_MIDI_VCF_MAX = 127
MINILOGUE_MIDI_CHANNEL = 2

# fractal noise parameters
OCTAVES = 5
DT = 1.0/50.0
SHOW_GRAPH = not args.hide_graph
POINTS = 256
SPAN = 1.0
SPEED = 0.20

try:
    midiout = rtmidi.MidiOut()
except rtmidi.SystemError as e:
    pass
midiout = rtmidi.MidiOut()
# Swap for a string that partially matches your midi device
available_ports = midiout.get_ports()
if list_midi_devices_only:
    print("Available midi devices:")
    for idx, port in enumerate(available_ports):
        print(f"  {idx}: {port}")
    print("\nExiting...\n")
    exit(0)

midi_port_number = None
try:
    midi_port_number = [idx for idx, x in enumerate(available_ports) if midi_port_search_term in x][0]
except IndexError as e:
    print(e)
    print(f"Can't find '{midi_port_search_term}' as a substring for in any of the detected midi ports: {available_ports}")
    exit(0)

f = FractalNoiseGenerator(midi_out_min=MINILOGUE_MIDI_VCF_MIN,
                          midi_out_max=MINILOGUE_MIDI_VCF_MAX,
                          points=POINTS,
                          span=SPAN,
                          speed=SPEED,
                          octaves=OCTAVES,
                          dt=DT,
                          debug_window=SHOW_GRAPH)

midiout.open_port(midi_port_number)

midi_wrapper = MidiOutWrapper(midiout, ch=1)

with midiout:
    while True:
        next_value = f.return_next_point()
        f.my_step()
        midi_wrapper.send_control_change(cc=MINILOGUE_MIDI_VCF_CUTOFF_CC, value=next_value, ch=MINILOGUE_MIDI_CHANNEL)
        if not print_value:
            print(f"sent {next_value}")
        time.sleep(DT)

del midiout


import board
import config
import objects
import os
import sys
import random
import time

dir_path = "./levels/"

specialbrick = []
brick = []
enemy = []
spring = []
coin = []
cloud = []
goomba = []
spikey = []
stalker = []
boss = []
castle = []

lists = {
    "specialbrick": specialbrick,
    "brick": brick,
    "enemy": enemy,
    "spring": spring,
    "coin": coin,
    "cloud": cloud,
    "goomba": enemy,
    "spikey": enemy,
    "stalker": enemy,
    "boss": boss,
    "castle": castle
}

def addobjects(bd, *args):
    for arg in args:
        for ob in arg:
            if ob.x > bd.at and ob.x < bd.at + config.show_width:
                bd.add(ob, ob.x, ob.y)

def removeobjects(bd, *args):
    for arg in args:
        for ob in arg:
            if ob.x > bd.at and ob.x < bd.at + config.show_width:
                bd.remove(ob, ob.x, ob.y)

def addCloud(lst, bd, level_name):

    completeName = os.path.join(dir_path + level_name, "cloud.txt")
    file = open(completeName, "w")
    choice = input('Do you want the scene to be cloudy?(y/n)	')
    if choice == 'y':
        for row in range(1, 4):
            for col in range(bd.width):
                if random.randint(0, 70) == 5:
                    temp = objects.Cloud(14, 4, col, row)
                    file.write(str(temp.x) + ',' + str(temp.y) + '\n')
                    lst.append(temp)

def addObjectList(bd, level_name, *names):

    for name in names:

        bd.at = 0

        completeName = os.path.join(dir_path + level_name, name + ".txt")
        file = open(completeName, "w")

        ob = makeObject(name)
        mk = objects.Marker(ob, ob.width, ob.height, 60, 30)

        bd.add(mk, mk.x, mk.y)
        while True:
            bd.clear(config.show_width)

            addobjects(bd, brick, enemy, goomba, spikey, stalker,
                       spring, coin, cloud, specialbrick, boss, castle)

            bd.add(mk, mk.x, mk.y)

            os.system('clear')
            bd.show(config.show_width, config.show_height)
            event = config.get_key(config.get_input())
            if event == config.QUIT:
                break
            elif event == config.SHOOT:
                bd.add(mk, mk.x, mk.y)
                file.write(str(mk.x) + ',' + str(mk.y) + '\n')
                lists[name].append(makeObject(name, mk.x, mk.y))
            else:
                mk.move(event)
                bd.move(event, mk.width)
                bd.add(mk, mk.x, mk.y)
        bd.remove(mk, mk.x, mk.y)
        bd.at = 0

def makeDirectory(name):
    if not os.path.exists(dir_path + name):
        os.makedirs(dir_path + name)

def makeLevel(name):
    if not os.path.exists(dir_path + name):
        os.makedirs(dir_path + name)

    bd = board.Board(config.board_width, config.show_height)
    addCloud(cloud, bd, name)
    addObjectList(bd, name, "brick", "goomba", "spikey", "stalker",
                  "spring", "coin", "specialbrick", "castle", "boss")

def giveList(filename):
    f = open(filename)
    lines = f.readlines()
    lines = [item.rstrip("\n") for item in lines]
    newList = list()
    for item in lines:
        item = item.split(",")
        item = tuple(int(items) for items in item)
        newList.append(item)
    f.close()
    return newList

def loadList(lt, level_name, name):
    l = giveList(os.path.join(dir_path + level_name, name + ".txt"))
    for tup in l:
        lt.append(makeObject(name, tup[0], tup[1]))

def makeObject(obj, x=config.show_width / 2, y=config.show_height / 2):
    if obj == "brick":
        return objects.Brick(7, 3, x, y)
    if obj == "coin":
        return objects.Coin(7, 3, x, y)
    if obj == "spring":
        return objects.Spring(7, 3, x, y)
    if obj == "specialbrick":
        return objects.SpecialBrick(14, 3, x, y)
    if obj == "goomba":
        return objects.Goomba(3, 2, x, y)
    if obj == "spikey":
        return objects.Spikey(4, 2, x, y)
    if obj == "stalker":
        return objects.Stalker(4, 2, x, y)
    if obj == "cloud":
        return objects.Cloud(14, 4, x, y)
    if obj == "castle":
        return objects.Castle(41, 15, x, y)
    if obj == "bullet":
        return objects.Bullet(4, 1, x, y)
    if obj == "boss":
        return objects.Boss(8, 4, x, y)


import os.path
import requests
import re
import textwrap
import sys
if sys.version_info[0] < 3:
    print('You need to run this with Python 3')
    sys.exit(1)

indent = ' ' * 4

def fill(text):
    tmp = textwrap.fill(text)
    return textwrap.indent(tmp, indent)

def filltab(text):
    tmp = textwrap.fill(text, width=120)
    return textwrap.indent(tmp, '\t')

def cpp_array_initializer(arr):
    return '{%s}' % (', '.join(map(str, arr)))

def multiline_cpp_array_initializer(arr):
    alllines = '{\n'
    current_line = '\t'
    for i in arr:
        if(len(current_line) > 80):
            if(current_line[-1] == ' '):
                current_line = current_line[:-1]
            alllines += current_line + '\n'
            current_line = '\t'
        current_line += str(i)
        if(i != len(arr)-1): current_line += ', '
    if(len(current_line) > 0):
        if(current_line[-1] == ' '):
          current_line = current_line[:-1]
        alllines += current_line + '\n'
    alllines += '\n}'
    return alllines

def compose2(f, g):
    return lambda x: f(g(x))

def cpp_arrayarray_initializer(arr):
    alllines = '{'
    for i in range(0,len(arr), 4):
        alllines += '\n\t' + ', '.join(map(cpp_array_initializer,arr[i:i+4]))
        if(i + 4 < len(arr)): alllines += ','
    alllines += '\n}'
    return alllines

url = "https://www.unicode.org/Public/idna/15.0.0/IdnaMappingTable.txt"
filename = "IdnaMappingTable.txt"
def get_table():
    if(not os.path.exists(filename)):
        tablefile = requests.get(url)
        with open(filename,  'wb') as file:
            file.write(tablefile.content)
    with open(filename, 'r') as file:
       return file.read()

def get_version(table_data):
    return re.search("# Version: (.*)", table_data).group(1)

def find_slice(seq, subseq):
    n = len(seq)
    m = len(subseq)
    for i in range(n - m + 1):
        if seq[i:i + m] == subseq:
            return i
    return -1

def values(words):
    answer = []
    for w in words:
        w2 = w[1]
        if(w[1] == 3):
            w2 += (w[2][0] << 8) + (w[2][1] << 24)
        answer.append([w[0][0], w2])
    return answer

UseSTD3ASCIIRules=False
if __name__ == "__main__":
    table_data = get_table()
    print("// IDNA ", get_version(get_table()))
    words = []
    long_mapped = []
    previous_code = -1
    mapping = {"ignored":0,"valid":1,"disallowed":2,"mapped":3}
    for line in table_data.split("\n"):
        if(line.startswith("#") or ";" not in line):
            continue
        line = line[0:line.index("#")]
        line = line.split(";")
        line = [x.strip() for x in line if x.strip() != ""]
        if(not UseSTD3ASCIIRules and line[1].startswith("disallowed_STD3_")): line[1] = line[1][16:]
        if(line[1] == "deviation"): line[1] = "valid" # IDNA2008, see https://www.unicode.org/faq/idn.html
        codepoints = [int(x,16) for x in line[0].split("..")]
        code = mapping[line[1]]
        mapped = []
        if((code != 3) and (code == previous_code)):
            continue # nothing to do
        previous_code = code
        if(code == 3):
            mapped = [int(x,16) for x in line[2].split(" ")]
            already = find_slice(long_mapped, mapped)
            if already != -1:
                long_index = already
            else:
                long_index = len(long_mapped)
                long_mapped += mapped
            long_length = len(mapped)
            words.append([codepoints, code, [long_index, long_length]])
        else:
            words.append([codepoints, code])
    print("""
// clang-format off
#ifndef ADA_IDNA_TABLES_H
#define ADA_IDNA_TABLES_H
#include <cstdint>

namespace ada::idna {
    """)
    print("const uint32_t mappings["+str(len(long_mapped))+"] = ")
    print(multiline_cpp_array_initializer(long_mapped), end=";\n")
    print("const uint32_t table["+str(len(words))+"][2] = ")
    print(cpp_arrayarray_initializer(values(words)), end=";\n")
    print("""

} // namespace ada::idna
#endif // ADA_IDNA_TABLES_H
    """)


from skmultiflow.evaluation import EvaluatePrequential
from skmultiflow.data import UnsupervisedStream

from models.ae import AE
from models.model import Model
from monitoring.monitoring import BaseMonitor
from data.dataset import Dataset
from config import Configuration

if __name__ == '__main__':

    config = Configuration()
    config.parse_commandline()
    config.new_experiment()

    X = Dataset()[config.CHANNEL]  # already segmented and overlapped!
    stream = UnsupervisedStream(X)
    stream.prepare_for_use()

    # ae = self.clf  (issue #5: Make these tests as classes)
    ae = Model(
        AE(
            # architecture hyperparameters
            n_input=1,
            n_hidden=config.n_hidden,
            n_output=1,
            n_layers=config.n_layers,
        )
    )

    monitor = BaseMonitor(
        ae,
        # learning hyperparameters
        # temperature,
        # learning_rate,
        # weight_decay
    )

    evaluator = EvaluatePrequential(
        n_wait=50,
        pretrain_size=2000,
        batch_size=1,
        metrics=[
            'true_vs_predicted',
            'mean_square_error',
            'mean_absolute_error'
        ],
        output_file=config.EXPERIMENT_PERSISTENCE + '/prequential.npy',
        show_plot=True
    )

    evaluator.evaluate(stream=stream, model=monitor)


import matplotlib.pyplot as plt
import numpy as np

def linked_circles(radii,speeds,phases,time=1,n=1001,draw=True):

    t_vals = np.linspace(0,time*2*np.pi,n)

    X,Y = [],[]

    for t in t_vals:
        x = 0
        y = 0
        for R,S,P in zip(radii,speeds,phases):
            x += R*np.sin(t*S+(P*2*np.pi))
            y += R*np.cos(t*S+(P*2*np.pi))
        X.append(x)
        Y.append(y)

    if draw == True:

        fig = plt.figure()
        fig.set_size_inches(12,12)
        ax = plt.axes()
        ax.set_aspect("equal","datalim")
        ax.axis('off')
        ax.set_xticks([])
        ax.set_yticks([])

        plt.plot(X,Y)

    return X,Y

# Doesn't work at all really
def fourier_approxiation(C,num_circles,time=1,draw=True):

    t_vals = np.linspace(0,2*np.pi,len(C))

    def calc_coef(n,C):
        cn = C*np.exp(-1j*2*n*np.pi*t_vals)
        return sum(cn)/len(cn)

    coefs = [calc_coef(N,C) for N in range(1,num_circles+1)]

    R = np.absolute(coefs)
    S = np.array(range(1,num_circles+1))
    P = np.angle(coefs)

    print(R)
    print(S)
    print(P)

    linked_circles(R,S,P,time,len(C),draw=draw)

if __name__ == '__main__':

#    from Conversions import xy_to_complex
#    from ParametricPolygon import regular_polygon

    num_cos = 5
    R = [n*2-1 for n in range(num_cos,0,-1)]
    S = [(n+1)*2-1 for n in range(0,num_cos,1)]
    P = [0]*num_cos
    x,y = linked_circles(R,S,P,1,n=2001)

    num_cos = 4
    R = [(n*2)-1 for n in range(num_cos,0,-1)]
    S = [(n*2)-1 for n in range(0,num_cos,1)]
    P = [0]*num_cos
    x,y = linked_circles(R,S,P,1,n=2001)

    num_cos = 6
    R = [5**n for n in range(0,num_cos,1)]
    S = [6**n for n in range(num_cos,0,-1)]
    P = [0]*num_cos
    x,y = linked_circles(R,S,P,.5,n=4001)


from datetime import datetime
import json
import pandas as pd
import sys

OUT_DIR = "metadata/"

def datify(u):
    return datetime.fromtimestamp(u).strftime('%Y-%m-%d') if u else "Indefinite"

def mapper(data):

    round_mapping = {
        "0x1b165fe4da6bc58ab8370ddc763d367d29f50ef0": {
            "name": "Climate Solutions",
            "color": "0AAD72"
        },
        "0xd95a1969c41112cee9a2c931e849bcef36a16f4c": {
            "name" : "Open Source Software",
            "color": "00A9B7"
        },
        "0xe575282b376e3c9886779a841a2510f1dd8c2ce4": {
            "name": "Ethereum Infrastructure",
            "color": "FCB53B"
        }
    }

    work_start_date  = 1663819200
    impact_end_date  = 0
    collection       = "Gitcoin Alpha Round"
    allowlist        = "ipfs://bafkreiaxdog4clqiitnarc4rrzpgdlcjsg6k2nr2n2t4thwklccza34ubi"
    default_image    = "ipfs://bafkreicchjbpbb2hfcg5mtmlz3zktf2wt5dnux2rzx33ta7b6bhrozlbgi"

    app_data         = data['application']
    project_data     = app_data['project']
    answer_data      = app_data['answers']

    project_address  = app_data['recipient']
    project_name     = project_data['title']
    project_descr    = project_data['description']
    project_url      = project_data['website']

    project_date     = int(str(project_data.get('createdAt', '1673829248'))[:10])
    project_icon     = "ipfs://" + project_data.get('logoImg', 'bafkreiejljnf6xf6kwcvh3wjef5xa3n7gscdumrmurmt4otkozbx5524r4')
    project_banner   = "ipfs://" + project_data.get('bannerImg', 'bafkreigkmcufguhakp4nbucca6d2rt7nw7ourdnkqfbs2gvsue4j4ohsly')
    project_github   = project_data.get('projectGithub')
    project_twitter  = project_data.get('projectTwitter')

    project_funding  = answer_data[1].get('answer')
    project_teamsize = answer_data[2].get('answer')

    round_contract   = app_data['round']
    round_name       = round_mapping[round_contract]['name']
    #bg_color         = round_mapping[round_contract]['color']

    return {
        "name": project_name,
        "description": project_descr,
        "external_url": project_url,
        "image": default_image,
        #"background_color": bg_color,
        "properties": {
            "impact_scope": {
                "name": "Impact Scope",
                "value": [round_name],
                "display_value": round_name
            },
            "work_scope": {
                "name": "Work Scope",
                "value": [project_name],
                "display_value": project_name
            },
            "work_timeframe": {
                "name": "Work Timeframe",
                "value": [work_start_date, project_date],
                "display_value": f"{datify(work_start_date)} → {datify(project_date)}"
            },
            "impact_timeframe": {
                "name": "Impact Timeframe",
                "value": [project_date, impact_end_date],
                "display_value": f"{datify(project_date)} → {datify(impact_end_date)}"
            },
            "contributors": {
                "name": "Contributors",
                "value": project_address,
                "display_value": project_address
            },
            "rights": {
                "name": "Rights",
                "value": ["public-display", "-transfers"],
                "display_value": "Public display"
            },
        },
        "hidden_properties": {
            "version": "1.0.0",
            "collection": collection,
            "icon": project_icon,
            "banner": project_banner,
            "allowlist": allowlist
        }
    }

def get_metadata(data):
    try:
        return mapper(data)
    except Exception as e:
        print(data)
        print(e)
        return None

def parse_csv(csv_path, out_dir):
    df = pd.read_csv(csv_path)

    for _, row in df.iterrows():
        project_name = row['title']
        metadata = get_metadata(eval(row['ipfs_data']))

        filename = project_name.replace("/","-")
        filename = filename if filename[0] != '.' else filename[1:]
        out_path = f"{out_dir}/{filename}.json"
        out_file = open(out_path, "w")
        json.dump(metadata, out_file, indent=4)

        out_file.close()

if __name__ == "__main__":

    csv_path = sys.argv[1]
    out_dir = sys.argv[2] if len(sys.argv) == 3 else OUT_DIR
    parse_csv(csv_path, out_dir)


# import pandas as pd
import os
import cv2
import numpy as np
from tqdm import tqdm
from threading import Thread

# import blobfile as bf
import random

#car_ = pd.read_csv('Image_table.csv')
def sort_contours(cnts, method="left-to-right"):
	# initialize the reverse flag and sort index
	reverse = False
	i = 0
	# handle if we need to sort in reverse
	if method == "right-to-left" or method == "bottom-to-top":
		reverse = True
	# handle if we are sorting against the y-coordinate rather than
	# the x-coordinate of the bounding box
	if method == "top-to-bottom" or method == "bottom-to-top":
		i = 1
	# construct the list of bounding boxes and sort them from top to
	# bottom
	boundingBoxes = [cv2.boundingRect(c) for c in cnts]
	(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
		key=lambda b:b[1][i], reverse=reverse))
	# return the list of sorted contours and bounding boxes
	return (cnts, boundingBoxes)

def get_ret_image(img_le):
    rect_le = (10, 10, img_le.shape[1], img_le.shape[0])
    mask_le = np.zeros(img_le.shape[:2], np.uint8)
    bgd_Model = np.zeros((1, 65), np.float64)
    fgd_Model = np.zeros((1, 65), np.float64)
    cv2.grabCut(img_le, mask_le, rect_le, bgd_Model, fgd_Model, 5, cv2.GC_INIT_WITH_RECT)

    #mask_le2 = get_mask_threshold(img_le)
    mask_le2 = np.where((mask_le == 0) | (mask_le == 2), 0, 1).astype('uint8')  # 做一個只有0 & 1新的mask

    #print(np.unique(mask_le2))
    img_copy_le = img_le.copy()
    img_grabcut_le = img_copy_le * mask_le2[:, :, np.newaxis]
    # cv2.imshow(f"Image", img_grabcut_le)
    # cv2.imshow(f"Mask", mask_le2 * 255)

    contours, hierarchy = cv2.findContours(mask_le2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    if len(contours) > 0:
        contours, _ = sort_contours(contours)
        #print(contours)
        # color = ['red', 'green']
        # for i in range(0, len(contours)):
        # cv2.boundingRect: 透過輪廓找到外接矩形
        # 輸出：(x, y)矩形左上角座標、w 矩形寬(x軸方向)、h 矩形高(y軸方向)
        x, y, w, h = cv2.boundingRect(contours[0])
        # for c in contours:
        #     x, y, w, h = cv2.boundingRect(c)
        #     if w > 20 or h > 20:
        #         break
        # cv2.imshow(f"Cut", img_grabcut_le[y:y + h, x:x + w])
        #
        # cv2.imshow("Original", img_le)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()

        return img_grabcut_le, img_grabcut_le[y:y + h, x:x + w], mask_le2, mask_le2[y:y + h, x:x + w]
    else:
        return None, None, None, None

# path_list = []
# file = open("filter_file_name.txt", "w")
# for im in car_.iloc:
#     if im[3] == 90 or im[3] == 270:
#         p = [t for t in im[2].split("$$")][:-3]
#         p.append(im[2])
#         p = os.path.join(*p)
#         path_list.append(p)
#         file.write(p+"\n")
#         p = os.path.join("resized_DVM", p)
#         print(p)
# file.close()

# get file name
result_path = "/data/harry/Data_generation/diffusers-main/car/preproc_car"
file = open("filter_file_name.txt", "r")
path_list = file.readlines()
file.close()
path_list = random.sample(path_list, 120016)

def job(paths):
    for p in tqdm(paths):
        p = os.path.join(r"resized_DVM", p[:-1])
        image = cv2.imread(p)
        im1, im2, msk1, msk2 = get_ret_image(image)
        if im1 is not None:
            if im2.shape[0] > 20 or im2.shape[1] > 20:
                cv2.imwrite(os.path.join(result_path, "image", p.split("/")[-1][:-3] + "png"), im2)
                cv2.imwrite(os.path.join(result_path, "mask", p.split("/")[-1][:-3] + "png"), msk2)

num_thread = 16
thread_list = []
job_iter = int(len(path_list)/num_thread)
for i in range(num_thread-1):
    t = Thread(target=job,args=([path_list[job_iter*i:job_iter*i+job_iter]]))
    thread_list.append(t)
thread_list.append(Thread(target=job,args=([path_list[job_iter*(num_thread-1):]])))

for j in thread_list:
    j.start()

for j in thread_list:
    j.join()

print("Done")

# image = cv2.imread(r"C:\Users\ASUS\Desktop\Abarth$$595C$$2016$$White$$2_5$$13$$image_3.jpg")
# im1, im2, msk1, msk2 = get_ret_image(image)
# cv2.imshow("im2",im2)
# cv2.imshow("msk2",msk1*255)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


from __future__ import absolute_import

import os

from nose.tools import eq_

from aws.s3 import s3file
from aws.s3.s3test_utils import S3TestBase

QUOTE_EN = 'a journey of a thousand miles begins with a single step'
QUOTE_CH = u'千里之行，始於足下'

class S3FileTest(S3TestBase):
  def test_basic_read(self):
    path = self.get_test_path('test_basic_read.txt')
    key = self.get_key(path)
    with self.cleaning(path):
      key.set_contents_from_string(QUOTE_EN)
      eq_(QUOTE_EN, s3file.open(key, 'r').read())
      eq_(QUOTE_EN[:4], s3file.open(key, 'r').read(length=4))

  def test_unicode_read(self):
    path = self.get_test_path('test_unicode_read.txt')
    key = self.get_key(path)
    with self.cleaning(path):
      key.set_contents_from_string(QUOTE_CH)
      eq_(QUOTE_CH.encode('utf-8'), s3file.open(key, 'r').read())
      eq_(QUOTE_CH.encode('utf-8')[:4], s3file.open(key, 'r').read(length=4))

  def test_seek(self):
    path = self.get_test_path('test_seek.txt')
    key = self.get_key(path)
    with self.cleaning(path):
      key.set_contents_from_string(QUOTE_EN)
      f = s3file.open(key, 'r')
      f.seek(0, os.SEEK_SET)
      eq_(QUOTE_EN[:2], f.read(2))
      f.seek(1, os.SEEK_SET)
      eq_(QUOTE_EN[1:][:2], f.read(2))
      f.seek(-1, os.SEEK_END)
      eq_(QUOTE_EN[-1:], f.read())
      f.seek(0, os.SEEK_SET)
      f.seek(2, os.SEEK_CUR)
      eq_(QUOTE_EN[2:][:2], f.read(2))


import scrapy
from datetime import datetime
from scrapy.exceptions import CloseSpider
from scrapy.loader import ItemLoader

from tudo_celular.items.smartphone import SmartphoneItem

class FichasTecnicasSpider(scrapy.Spider):
    name = "fichas_tecnicas"
    allowed_domains = ["www.tudocelular.com"]
    url = "https://www.tudocelular.com"
    page = 1

    def start_requests(self):
        yield scrapy.Request(url=f"{self.url}/celulares/fichas-tecnicas.html", callback=self.parse)

    def parse(self, response):
        if response.status == 302:
            raise CloseSpider("No more pages scrapy will be closed...")

        for phone in response.xpath('//article[@class="phonelist_item"]'):
            item = ItemLoader(item=SmartphoneItem())
            item.add_value('Brand', phone.xpath('div[2]/a/h4/strong/text()').get())
            item.add_value('Model', phone.xpath('div[2]/a/h4/text()').get())
            item.add_value('Image', phone.css('img').attrib['style'])
            item.add_value('Score', phone.xpath('div[1]/div/strong/text()').get())
            item.add_value('Price', phone.xpath('div[2]/div/a[1]/span[2]/text()').get())
            item.add_value('Url', self.url + phone.xpath('a[@class="pic"]').attrib['href'])
            item.add_value('Extracted_at', datetime.now().isoformat())

            # Phone details
            details_url = self.url + phone.xpath('a[@class="pic"]').attrib['href']
            yield scrapy.Request(details_url, callback=self.parse_details, meta={'item': item})

        # Paginate
        self.page += 1
        next_page = f"{self.url}/celulares/fichas-tecnicas_{self.page}.html"
        yield response.follow(url=next_page, callback=self.parse)

    def parse_details(self, response):
        item = response.meta['item']
        details = {}
        sections = response.xpath('//div[@class="row_titles"]/h3/strong/text()').getall()
        sections.insert(0, "BÁSICO")
        titles = response.xpath('//div[@class="row_titles"]/ul[@class="phone_column_features"]')
        values = response.xpath('//div[@class="phone_column"]/ul[@class="phone_column_features"]')

        # Block to handle values
        for i in range(len(sections)):
            details[sections[i]] = {}
            # Block to handle values that have <i> and <b> tags
            for title, value in zip(titles[i].xpath('li'), values[i].xpath('li')):
                v = ''
                # Handle icon
                if value.xpath('i[@class]'):
                    v = value.xpath('i[@class]').attrib['class'] + ' '
                # Handle bold
                elif value.xpath('b/text()'):
                    v += value.xpath('b/text()').get() + ' '
                # Handle price
                elif value.xpath('a/b/text()'):
                    v += value.xpath('a/b/text()').get() + ' '

                # Handle text
                if value.xpath('text()'):
                    v += value.xpath('text()').get()

                details[sections[i]].update({
                    title.xpath('text()').get(): v
                })

        item.add_value('Details', details)
        yield item.load_item()


from django.shortcuts import render ,redirect,HttpResponse,HttpResponseRedirect
# from django.views.generic import ListView,DetailView

from myapps.models import  Bloods_Group,Contact_us
from django.views.generic import ListView,DetailView, TemplateView
from django.core.mail import send_mail,BadHeaderError
from django.contrib import messages
from myapps.models import Donors_in_Site
from django.core.files.storage import FileSystemStorage
from depend.models import State,Country,District
# Create your views here.

def home(request):
   return render(request,'index.html')

def about_us(request):
   return render(request,'about_us.html')

# def Donor_Registration(request):

#     countryid = request.GET.get('country')

#     stateid = request.GET.get('state')
#     state = None
#     district = None
#     if countryid :
#         getcountry = Country.objects.get(name=countryid)

#         state = State.objects.filter(country=getcountry)
#     if stateid:
#         getstate = State.objects.get(name=stateid)
#         district = District.objects.filter(state=getstate)

#     country = Country.objects.all()
#     bloods= Bloods_Group.objects.all()

#     return render(request, 'Donor_Registration1.html',locals())

def contact_us(request):

    if request.method=='POST':
        name= request.POST.get('name')
        email= request.POST.get('email')
        subject = request.POST.get('subject', )
        message= request.POST.get('message')
        admin='khandokarsamad4@gmail.com'

        if subject and message and email:
           contact=Contact_us(sender_name=name,from_email=email,subject=subject,message=message)
           contact.save()
           send_mail(subject,message,email, [admin,email,name])
           messages.success(request,'Succesfully Send Message!')

        else:
           messages.error(request,'Make sure all fields are entered and valid!')

    return render(request, 'contact.html')

def Donor_Reg_Site(request):
  print("okk")

  if request.method=='POST' :
    country = request.POST.get('country')
    state = request.POST.get('state')
    district = request.POST.get('district')

    if not Country.objects.filter(name=country.title()).exists():
                c=''.join(country)
                c = Country(name=c.title())
                c.save()
                # c=c.capitalize()
                # print(1111111111111111111111)
                # c=Country.objects.create(name=c)

                # c.save()

    if not State.objects.filter(name=state.title()).exists():
                c = Country.objects.get(name=country.title())
                s = State(name=state.title(), country=c)

                s.save()
            #    s=s.capitalize()
            #    c=c.capitalize()
            #    print(22222222222222222)
            #    c = Country.objects.get(name=c)
            #    obj = State(country=c,name=s)

            #    obj.save()
    if not District.objects.filter(name=district.title()).exists():
                c = State.objects.get(name=state.title())
                s = District(name=district.title(), state=c )

                s.save()
    #            d=''.join(district)
    #            s=''.join(state)
    #            d=d.capitalize()
    #            s=s.capitalize()
    #            obj=District.objects.create(state=s,name=d)
    #            obj=obj.capitalize()
    #            obj.save()
    try:
        instance=Donors_in_Site.objects.get(user=request.user)

        if instance:
            instance.user=request.user
            phone = request.POST['phone']
            phone_str = ''.join(phone)
            instance.phone = phone_str

            blood = request.POST.get('blood'),
            blood_str = ''.join(blood)
            instance.blood=blood_str

            dob=request.POST['dob'],
            dob_str=''.join(dob)
            instance.Last_Donate = dob_str

            country = request.POST.get('country'),
            country_str=''.join(country)
            country_str=country_str.title()
            instance.country = country_str

            state = request.POST.get('state'),
            state_str=''.join(state)
            state_str=state_str.title()
            instance.state = state_str

            district = request.POST.get('district'),
            district_str=''.join(district)
            district_str=district_str.title()
            instance.district = district_str

            if len(request.FILES) == 0:
                print('no files')
            else:
                _, file = request.FILES.popitem()
                file = file[0]
                instance.image = file
                instance.save()

            instance.save()

            messages.success(request,'Data has been updated!')
            return render(request, 'index.html')
    except:
        en=Donors_in_Site.objects.create(
        user=request.user,
        phone = request.POST['phone'],
        blood = request.POST.get('blood' ),
        Last_Donate = request.POST['dob'],

        country = request.POST.get('country').title(),

        state = request.POST.get('state').title(),
        district = request.POST.get('district').title()
          )
        if len(request.FILES) == 0:
            print('no files')
        else:
            _, file = request.FILES.popitem()
            file = file[0]
            en.image = file
            en.save()

        print("okk")
        messages.success(request,'Data has been submitted!')
        return render(request, 'index.html')

  else:
        messages.error(request,'something mistake.please Reloade and try again!!')
        return render(request, 'dependantfield.html')

# Create your views here.

class donor(DetailView):
      model=Donors_in_Site
      template_name='loginapp/profile2.html'
      context_object_name='user'


import re
from collections import defaultdict

data = [i.strip() for i in open("inputs/7.txt").readlines()]

def parse(data):
  bags = defaultdict(dict)
  for line in data:
    bag = re.match(r'(.*) bags contain', line).groups()[0]
    for c, b in re.findall(r'(\d+) (\w+ \w+) bag', line):
      bags[bag][b] = int(c)
  return bags

def solve_p1(bags):
  ret = set()
  def search(colour):
    for i in bags:
      if colour in bags[i]:
        ret.add(i)
        search(i)
  search('shiny gold')
  return len(ret)

def solve_p2(bags):
  def search(bag):
    count = 1
    for x in bags[bag]:
      m = bags[bag][x]
      count += m * search(x)
    return count
  return search('shiny gold') - 1

print(solve_p1(parse(data)))
print(solve_p2(parse(data)))


from django import forms
from django.contrib.auth.models import User
from .models import Reservation
from django.forms import TextInput

class ReservationForm(forms.ModelForm):
    class Meta:
        model = Reservation
        fields = [
            'start_date',
            'end_date',
            'name',
        ]
        widgets = {
            'start_date': TextInput(attrs={'id': 'datepicker'}),
            'end_date': TextInput(attrs={'id': 'datepicker2'}),
        }


# -*-coding:utf-8-*-
# @auth ivan
# @time 2018-01-16 14:29
# @goal config
from logging import NOTSET

__all__ = [
    'main_path_data',
    'main_path_out',
    'main_path_test',
    'data_Name',
    'data_Tame',
    'data_type',
    'out_Name',
    'out_type',
    'size',
    'times',
    'deletions',
    'data',
    'data_T',
    'pro_gra_dot',
    'min_cpu',
    'log_mode',
    'log_level',
    'log_format',
    'log_encode'
]

main_path_data = u'G:\\OUT\\07.AI\\MLTools\\data\\'

main_path_out = u'G:\\OUT\\07.AI\\MLTools\\out\\'

main_path_test = u'G:\\OUT\\07.AI\\MLTools\\out\\test\\'

data_Name = u'm1m2_sample'

data_Tame = u'm1m2_sample_T200(MISS)'

data_type = u'.csv'

out_Name = u'fields'

out_type = u'.log'

size = 0.05

times = 5

deletions = {
    'R_POS_CNT_16_Pct_Avg_POS_CNT_1N': [-99000792.0, 85],
    'R_Con_Incs_in_INC_Pay_P_BAL': []
}

special = [-99000792, -99000784, -99000776]

data = main_path_data + data_Name + data_type

data_T = main_path_data + data_Tame + data_type

pro_gra_dot = 'C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe'

min_cpu = 2

log_mode = 'w'

log_level = NOTSET

log_format = '%(levelname)s| %(message)s'

log_encode = 'utf-8'


from __future__ import absolute_import
from __future__ import division

from copy import copy as native_copy
from numbers import Number
import types

import autograd
import numpy
import six
from tangent import annotations as anno
from tangent import non_differentiable
from tangent import quoting

INIT_GRAD = quoting.quote('tangent.init_grad')
ADD_GRAD = quoting.quote('tangent.add_grad')
anno.setanno(INIT_GRAD, 'init_grad', True)
anno.setanno(ADD_GRAD, 'add_grad', True)

def array_size(x, axis):
  """Calculate the size of `x` along `axis` dimensions only."""
  axis_shape = x.shape if axis is None else tuple(x.shape[a] for a in axis)
  return max(numpy.prod(axis_shape), 1)

class Stack(object):
  """A stack type that proxies list's `append` and `pop` methods.

  We don't use list directly so that we can test its type for the multiple-
  dispatch that occurs in `add_grad` and `init_grad`.
  """

  def __init__(self, vals=()):
    self._stack = list(vals)

  def append(self, x):
    self._stack.append(x)

  def pop(self):
    return self._stack.pop()

  def __len__(self):
    return len(self._stack)

  def __str__(self):
    return str(self._stack)

  def __repr__(self):
    return self._stack.__repr__()

# The values are binary functions with signature fn(array, like)
unbroadcasters = {
    numpy.ndarray:
        lambda array, like: unbroadcast_numpy_to(array, numpy.shape(like)),
    numpy.float32:
        lambda array, like: array,
    numpy.float64:
        lambda array, like: array,
    float:
        lambda array, like: array,
    int:
        lambda array, like: array,
    bool:
        lambda array, like: array,
}

def register_unbroadcast(t, unbroadcaster_function):
  """Register a new unbroadcaster.

  Unbroadcasters are used to undo broadcasting, e.g. np.eye(3) + 3
  will broadcast 3 to np.shape(np.eye(3)). In the backward pass, we have to
  undo this.

  Args:
    t: A Python type object. The data type supported by the
      unbroadcaster.
    unbroadcaster_function: A binary function that takes a first argument of
      type t, and a second argument that t needs to be unbroadcast to.
  """
  assert t not in unbroadcasters
  unbroadcasters[t] = unbroadcaster_function

def unbroadcast(array, like):
  """Reverse the broadcasting operation.

  Args:
    array: An array.
    like: An array that could have been broadcasted to the shape of array.

  Returns:
    Tensor with certain dimensions summed to match the shape of `like`.
  """
  unbroadcaster = unbroadcasters[type(array)]
  return unbroadcaster(array, like)

def create_unbroadcast_axis(shape, broadcast_shape):
  """Creates the reduction axis for unbroadcasting.

  Args:
    shape: A list. The shape after the broadcast operation.
    broadcast_shape: A list. The original shape the array being unbroadcast
      had.
  Returns:
    A list. The axes along which the array needs to be reduced. These axes will
    be distributed evenly into the original shape.
  """
  return tuple(
      -(1 + i)
      for i in range(len(broadcast_shape))
      if i >= len(shape) or broadcast_shape[-(1 + i)] > shape[-(1 + i)])

def unbroadcast_numpy_to(array, shape):
  """Reverse the broadcasting operation.

  Args:
    array: An array.
    shape: A shape that could have been broadcasted to the shape of array.

  Returns:
    Array with dimensions summed to match `shape`.
  """
  axis = create_unbroadcast_axis(shape, numpy.shape(array))
  return numpy.reshape(numpy.sum(array, axis=axis), shape)

def unreduce(array, shape, axis, keepdims):
  """Reverse summing over a dimension.

  Args:
    array: The array that was reduced.
    shape: The original shape of the array before reduction.
    axis: The axis or axes that were summed.
    keepdims: Whether these axes were kept as singleton axes.

  Returns:
    An array with axes broadcast to match the shape of the original array.
  """
  unreducer = unreducers[type(array)]
  return unreducer(array, shape, axis, keepdims)

def unreduce_like(array, original_array, axis, keepdims):
  """Reverse summing over a dimension.

  Args:
    array: The array that was reduced.
    original_array: An array whose shape to unreduce to.
    axis: The axis or axes that were summed.
    keepdims: Whether these axes were kept as singleton axes.

  Returns:
    An array with axes broadcast to match the shape of the original array.
  """
  atype = type(array)
  unreducer = unreducers[atype]
  shape = shape_functions[atype]
  return unreducer(array, shape(original_array), axis, keepdims)

def unreduce_array(array, shape, axis, keepdims):
  """Reverse summing over a dimension, NumPy implementation.

  Args:
    array: The array that was reduced.
    shape: The original shape of the array before reduction.
    axis: The axis or axes that were summed.
    keepdims: Whether these axes were kept as singleton axes.

  Returns:
    An array with axes broadcast to match the shape of the original array.
  """
  # NumPy uses a special default value for keepdims, which is equivalent to
  # False.
  if axis is not None and (not keepdims or keepdims is numpy._NoValue):  # pylint: disable=protected-access
    if isinstance(axis, int):
      axis = axis,
    for ax in sorted(axis):
      array = numpy.expand_dims(array, ax)
  return numpy.broadcast_to(array, shape)

# The values are unary functions.
shape_functions = {
    numpy.ndarray: numpy.shape,
    numpy.float32: numpy.shape,
    numpy.float64: numpy.shape,
    float: lambda _: (),
    int: lambda _: (),
    bool: lambda _: (),
}

def register_shape_function(t, shape_function):
  """Register a new shape function.

  Shape functions extract the shape of an array-like object.

  Args:
    t: A Python type object. The data type supported by the
      unreducer.
    shape_function: A unary function that returns a list or tuple with zero
      or more integers representing the dimensions of `t`.
  """
  assert t not in shape_functions
  shape_functions[t] = shape_function

# The values are functions with signature like `unreduce_array`
unreducers = {
    numpy.ndarray: unreduce_array,
    numpy.float32: unreduce_array,
    numpy.float64: unreduce_array,
    float: unreduce_array,
    int: unreduce_array,
    bool: unreduce_array,
}

def register_unreduce(t, unreducer_function):
  """Register a new unreducer.

  Unreducers are used to undo reduction, e.g. np.sum(np.eye(3))
  will reduce a (3,3) array to a scalar. In the backward pass, we have to
  undo this.

  Args:
    t: A Python type object. The data type supported by the
      unreducer.
    unreducer_function: A function with the same signature
      as e.g. `unreduce_array`
  """
  assert t not in unreducers
  unreducers[t] = unreducer_function

def astype(array, y):
  """A functional form of the `astype` method.

  Args:
    array: The array or number to cast.
    y: An array or number, as the input, whose type should be that of array.

  Returns:
    An array or number with the same dtype as `y`.
  """
  if isinstance(y, autograd.core.Node):
    return array.astype(numpy.array(y.value).dtype)
  return array.astype(numpy.array(y).dtype)

def balanced_eq(x, z, y):
  """Gradient of the max operator with tie breaking.

  Args:
    x: The left value
    z: The maximum of x and y
    y: The right value

  Returns:
    The gradient of the left value i.e. 1 if it is the maximum, 0.5 if they are
    equal and 0 if it was not the maximum.
  """
  return (x == z) / (1.0 + (x == y))

def init_common_object(obj):
  """Initialize gradients for the types of common objects we support."""
  if obj is numpy._globals._NoValue:  # pylint: disable=protected-access
    return obj
  raise ValueError('Unknown value to initialize: "%s"' % obj)

init_zero_int_warnings_left = 3
init_zero_bool_warnings_left = 3

def init_zero_int(_):
  """Initialize gradient for an integral type. This prints a warning."""
  global init_zero_int_warnings_left
  if init_zero_int_warnings_left:
    print(
        'WARNING: Creating intermediate variable of an integer type. This may '
        'lead to unexpected results. If unsure, cast arguments to floating '
        'point.')
    init_zero_int_warnings_left -= 1
  return 0

def init_zero_bool(_):
  """Initialize gradient for an bool type. This prints a warning."""
  global init_zero_bool_warnings_left
  if init_zero_bool_warnings_left:
    print(
        'WARNING: Creating intermediate variable of a boolean type. This may '
        'indicate a bug.')
    init_zero_bool_warnings_left -= 1
  return False

# The values are tuples (initializer, allow_lazy_initializer). If
# supports_lazy_initializer is true, Tangent may substitude actual instances
# of the object for the ZeroGradient wrapper, which is a lazy creator.
grad_initializers = {
    # TODO: We may be able to use ZeroGradient for ndarrays, too.
    numpy.ndarray: (numpy.zeros_like, False),
    numpy.float32: (lambda obj: 0.0, False),
    numpy.float64: (lambda obj: 0.0, False),
    numpy.int32: (init_zero_int, False),
    numpy.int64: (init_zero_int, False),
    list: (lambda obj: [init_grad(el) for el in obj], False),
    tuple: (lambda obj: [init_grad(el) for el in obj], False),
    dict: (lambda obj: {k: init_grad(v) for k, v in six.iteritems(obj)}, False),
    Stack: (lambda obj: Stack(), False),
    float: (lambda obj: 0.0, False),
    int: (init_zero_int, False),
    bool: (init_zero_bool, False),
}

if hasattr(types, 'ClassType'):
  grad_initializers[types.ClassType] = (init_common_object, False)
else:
  grad_initializers[type] = (init_common_object, False)

class ZeroGradient(object):
  """Lightweight substitute for zero gradients.

  This object may be used instead of an actual type when manipulating
  objects of the respective type is expensive.
  """

  def __init__(self, like):
    self._like = like

  def like(self):
    return self._like

  def instantiate(self):
    return grad_initializers[type(self._like)](self._like)

def register_init_grad(t, init_grad_function):
  """Register a new gradient initializer.

  Gradient initializers are used to initialize new adjoint and tangent
  variables.
  TODO: Link to the document explaining the overall terminology and mechanics.

  Args:
    t: A Python type object. The data type supported by the initializer.
    init_grad_function: A unary function that takes an argument of type t
      and returns a zero object of the same size as the argument. For example,
      the gradient initializer for Numpy objects is zeros_like.
  """
  assert t not in grad_initializers
  grad_initializers[t] = (init_grad_function, True)

def init_grad(obj, allow_lazy_initializer=False):
  """Initialize the gradient for an object.

  Args:
    obj: The object to initialize the gradient for, can be either a number,
      array, tuple, list, or dictionary.
    allow_lazy_initializer: Whether to allow using the ZeroGradient wrapper,
      for efficiency.

  Returns:
    An object of the same type, shape, etc. but with all numeric values set to
    zero. If the type is unknown, a zero is returned.
  """
  if obj is None:
    # TODO: fixes.py appears to pass None value and expect 0.0 back. Bug?
    return 0.0

  initializer, supports_lazy_initializer = grad_initializers[type(obj)]
  if supports_lazy_initializer:
    if isinstance(obj, ZeroGradient):
      if allow_lazy_initializer:
        return ZeroGradient(obj.like)
      else:
        # TODO: Not sure this should normally be hit. In forward-over-reverse?
        return obj.instantiate()
    else:
      if allow_lazy_initializer:
        return ZeroGradient(obj)
  else:
    assert not isinstance(obj, ZeroGradient)
  return initializer(obj)

upcasting_int_warnings_left = 3

def add_grad_numpy_int_argument(left, right):
  global upcasting_int_warnings_left
  if upcasting_int_warnings_left:
    print(
        'WARNING: Automatically upcasting a temporary integer variable to '
        'float. This may happen if you differentiate with respect to an '
        'integer argument and may lead to unexpected results.')
    upcasting_int_warnings_left -= 1
  right = unbroadcast(numpy.array(right), left)
  return left + right

def add_grad_numpy(left, right):
  right = unbroadcast(numpy.array(right), left)
  return left + right

def add_grad_list(left, right):
  return [add_grad(l, r) for l, r in zip(left, right)]

def add_grad_dict(left, right):
  assert all(k in left for k in right)
  return {k: add_grad(left[k], right[k]) for k in left}

grad_adders = {
    (list, list): add_grad_list,
    (dict, dict): add_grad_dict,
    (numpy.ndarray, numpy.ndarray): add_grad_numpy,
    (numpy.ndarray, list): add_grad_numpy,
    (list, numpy.ndarray): add_grad_numpy,
    (bool, bool): lambda left, right: left or right,
}

def register_add_grad(left_type, right_type, add_grad_function):
  """Register a new gradient adder supporting the given types.

  Gradient adders are used to add (in the sense of arithmetic addition)
  intermediate adjoint and tangent variables.
  TODO: Link to the document explaining the overall terminology and mechanics.

  Args:
    left_type: A Python type object. The data type of the left operand
      supported by the adder.
    right_type: A Python type object. The data type of the right operand
      supported by the adder.
    add_grad_function: A binary function that takes two arguments, left and
      right, of the types left_type and right_type respectively, and returns
      their sum. For example, the gradient adder for Numpy objects is np.add.

  Raises:
    ValueError: If the given type pair was already registered.
  """
  key = (left_type, right_type)
  if key in grad_adders:
    raise ValueError('Types %s already mapped to %s' % (key, grad_adders[key]))
  grad_adders[key] = add_grad_function

def register_all_add_grad(
    add_grad_function, arg_types, exclude=(), ignore_existing=False):
  """Register a gradient adder for all combinations of given types.

  This is a convenience shorthand for calling register_add_grad when registering
  gradient adders for multiple types that can be interchanged for the purpose
  of addition.

  Args:
    add_grad_function: A gradient adder, see register_add_grad.
    arg_types: List of Python type objects. The gradient adder will be
      registered for all pairs of these types.
    exclude: Optional list of type tuples to exclude.
    ignore_existing: Boolean. Whether to silently skip argument pairs that were
      already registered.
  """
  for t1 in arg_types:
    for t2 in arg_types:
      if (t1, t2) in exclude:
        continue
      if ignore_existing and (t1, t2) in grad_adders:
        continue
      register_add_grad(t1, t2, add_grad_function)

register_all_add_grad(
    lambda left, right: left + right,
    (float, numpy.float32, numpy.float64, numpy.ndarray),
    exclude=((numpy.ndarray, numpy.ndarray),))

register_all_add_grad(
    add_grad_numpy_int_argument,
    (float, int,
     numpy.int32, numpy.int64, numpy.float32, numpy.float64,
     numpy.ndarray),
    exclude=((numpy.ndarray, numpy.ndarray),),
    ignore_existing=True)

def add_grad(left, right):
  """Recursively add the gradient of two objects.

  Args:
    left: The left value to add. Can be either an array, a number, list or
        dictionary.
    right: The right value. Must be of the same type (recursively) as the left.

  Returns:
    The sum of the two gradients, which will of the same type.
  """
  # We assume that initial gradients are always identity WRT add_grad.
  # We also assume that only init_grad could have created None values.
  assert left is not None and right is not None
  left_type = type(left)
  right_type = type(right)
  if left_type is ZeroGradient:
    return right
  if right_type is ZeroGradient:
    return left
  return grad_adders[(left_type, right_type)](left, right)

def array_shapes_match(a, b):
  return numpy.shape(a) == numpy.shape(b)

shape_checkers = {}

def register_shape_checker(left_type, right_type, shape_checker_function):
  """Register a new shape checking function supporting given types.

  Shape checkers are primarily used to make sure that the seed derivatives
  passed into generated autodiff functions match their corresponding
  primal values.

  Args:
    left_type: A Python type object. The data type of the left operand
      supported by the adder.
    right_type: A Python type object. The data type of the right operand
      supported by the adder.
    shape_checker_function: A binary function that takes two arguments, left and
      right, of the types left_type and right_type respectively, and returns
      a boolean indicating whether or not they match.

  Raises:
    ValueError: If the given type pair was already registered.
  """
  key = (left_type, right_type)
  if key in shape_checkers:
    raise ValueError('Types %s already mapped to %s' % (key,
                                                        shape_checkers[key]))
  shape_checkers[key] = shape_checker_function

def register_all_shape_checker(shape_checker_function,
                               arg_types,
                               exclude=(),
                               ignore_existing=False):
  """Register a gradient adder for all combinations of given types.

  This is a convenience shorthand for calling register_add_grad when registering
  gradient adders for multiple types that can be interchanged for the purpose
  of addition.

  Args:
    shape_checker_function: A shape checker, see register_shape_checker.
    arg_types: List of Python type objects. The shape checker will be
      registered for all pairs of these types.
    exclude: Optional list of type tuples to exclude.
    ignore_existing: Boolean. Whether to silently skip argument pairs that were
      already registered.
  """
  for t1 in arg_types:
    for t2 in arg_types:
      if (t1, t2) in exclude:
        continue
      if ignore_existing and (t1, t2) in shape_checkers:
        continue
      register_shape_checker(t1, t2, shape_checker_function)

def shapes_match(a, b):
  """Recursively check if shapes of object `a` and `b` match.

  Will walk lists, tuples and dicts.

  Args:
    a: object of type (numpy.ndarray,tf.Tensor,list,tuple,dict)
        to check for matching shapes against `b`.
    b: object to check for matching shape against `a`.

  Returns:
    A boolean indicating whether the shapes of `a` and `b` match.
  """
  if isinstance(a, (tuple, list)) and isinstance(b, (tuple, list)):
    if len(a) != len(b):
      return False
    return all([shapes_match(ia, ib) for ia, ib in zip(a, b)])
  elif isinstance(a, dict) and isinstance(b, dict):
    if len(a) != len(b):
      return False
    match = True
    for (ak, av), (bk, bv) in zip(a.items(), b.items()):
      match = match and all([ak == bk and shapes_match(av, bv)])
    return match
  else:
    shape_checker = shape_checkers[(type(a), type(b))]
    return shape_checker(a, b)

register_all_shape_checker(
    array_shapes_match, (numpy.ndarray, Number, float, int, numpy.float32,
                         numpy.float64, numpy.int32, numpy.int64),
    ignore_existing=True)

def push(stack, x, op_id):
  """Push a value onto the stack (i.e. record it on the tape).

  Args:
    stack: The stack object, which must support appending values.
    x: The value to append. If it is a mutable object like an array or list, it
        will be copied before being added onto the stack.
    op_id: A unique variable that is also passed into the corresponding pop.
        Allows optimization passes to track pairs of pushes and pops.
  """
  if isinstance(x, numpy.ndarray):
    x = x.copy()
  elif isinstance(x, list):
    x = x[:]
  if __debug__:
    stack.append((x, op_id))
  else:
    stack.append(x)

def pop(stack, op_id):
  """Pop a value from the stack (i.e. read it from the tape).

  Args:
    stack: The stack to pop from.
    op_id: A unique variable that is also passed into the matching push.
        Allows optimization passes to track pairs of pushes and pops.

  Returns:
    The last value.
  """
  if __debug__:
    pushed_value, pushed_op_id = stack.pop()
    assert pushed_op_id == op_id, 'Wanted %s, got %s' % (op_id, pushed_op_id)
  else:
    pushed_value = stack.pop()
  return pushed_value

def pop_stack(stack, op_id):
  """Proxy of pop, where we know we're popping a stack off of a stack.

  We know that we don't need to differentiate through this.
  See pop() for more.

  Args:
    stack: The stack to pop from.
    op_id: A unique variable that is also passed into the matching push.
        Allows optimization passes to track pairs of pushes and pops.

  Returns:
    The last value.
  """
  if __debug__:
    pushed_stack, pushed_op_id = stack.pop()
    assert pushed_op_id == op_id, 'Wanted %s, got %s' % (op_id, pushed_op_id)
  else:
    pushed_stack = stack.pop()
  return pushed_stack

def push_stack(stack, substack, op_id):
  """Proxy of push, where we know we're pushing a stack onto a stack.

  Used when differentiating call trees,where sub-functions get their own stack.
  See push() for more.

  Args:
    stack: The stack object, which must support appending values.
    substack: The stack to append.
    op_id: A unique variable that is also passed into the corresponding pop.
        Allows optimization passes to track pairs of pushes and pops.

  Raises:
    ValueError: If a non-stack value for `substack` is passed.
  """
  if substack is not None and not isinstance(substack, Stack):
    raise ValueError(
        'Substack should be type tangent.Stack or None, instead found %s' %
        type(substack))
  if __debug__:
    stack.append((substack, op_id))
  else:
    stack.append(substack)

non_differentiable.register_non_differentiable_functions(
    init_grad, array_size, Stack)

def insert_grad_of(var):
  """The context manager that allows insertion of arbitrary adjoint code.

  This function can be used as a context manager e.g. `with insert_grad_of(x) as dx`
  to write code that will be inserted in the adjoint while having access to the
  gradients of certain variables.

  This function is handled by reverse mode automatic differentiation, and
  shouldn't actually ever be called. If the user wants to use a function
  containing this context manager without taking the derivative, the `tangent`
  decorator should be used to remove it from the code.

  Args:
    var: The variable of which we want the gradient.

  Returns:
    The gradient of this value.

  Raises:
    ValueError: If this context manager isn't removed using the `tangent`
        decorator and the code is actually run.
  """
  raise ValueError('use the tangent decorator for functions containing '
                   'the `with insert_grad_of` statement')

def grad_dot(dy, x1, x2):
  """Gradient of NumPy dot product w.r.t. to the left hand side.

  Args:
    dy: The gradient with respect to the output.
    x1: The left hand side of the `numpy.dot` function.
    x2: The right hand side

  Returns:
    The gradient with respect to `x1` i.e. `x2.dot(dy.T)` with all the
    broadcasting involved.
  """
  if len(numpy.shape(x1)) == 1:
    dy = numpy.atleast_2d(dy)
  elif len(numpy.shape(x2)) == 1:
    dy = numpy.transpose(numpy.atleast_2d(dy))
    x2 = numpy.transpose(numpy.atleast_2d(x2))
  x2_t = numpy.transpose(numpy.atleast_2d(
      numpy.sum(x2, axis=tuple(numpy.arange(numpy.ndim(x2) - 2)))))
  dy_x2 = numpy.sum(dy, axis=tuple(-numpy.arange(numpy.ndim(x2) - 2) - 2))
  return numpy.reshape(numpy.dot(dy_x2, x2_t), numpy.shape(x1))

def copy(source):
  source_type = type(source)
  # If gradient initializers are allowed, then the object is immutable and we
  # don't need to deep copy it.
  if source_type in grad_initializers:
    _, allow_lazy_init_grad = grad_initializers[source_type]
    if allow_lazy_init_grad:
      return source
  return native_copy(source)


# @author - Vikesh Khanna
# This file takes as input the data from FootballData.co.uk in csv format and extracts the following different csv files-
# teams - csv file with each team name. The row number of the team is important as it will be used as pointer in other matrices
# train - The training matrix csv. There are 2*(n+1) columns. Each n+1 columns is teamID and the n features we are capturing. (see legend)

import csv
import sys
import numpy as np
import scipy.io as io
from utils import *

def main(args):
	if len(args)<1:
		print("Usage: python parse.py <csv_file>")
		sys.exit(1)

	csvfile = args[0]

	f = open(csvfile)
	reader = csv.reader(f)

	# Should match the Feature class elements
	header = reader.next()

	# team list
	teams = []

	# train matrix - 2*(n+1) rows. Each row is a match. First row is the team ID, n+1 features of home team, (1, n+1) features of the away team
	train = []

	for row in reader:
		features = {Constants.HOME : [], Constants.AWAY: []}
		outcome = None

		for i in range(len(row)):
			feature = row[i]
			field = header[i]

			if field == Features.HomeTeam or field == Features.AwayTeam:
				if feature not in teams:
					teams.append(feature)

			# Header contains the feature - Useful feature. Must.Take
			if field in Features.FMAP:
				# Find home or away
				location = Features.FMAP[field]

				if field==Features.HomeTeam or field==Features.AwayTeam:
					feature = teams.index(feature) + 1
				else:
					feature = float(feature)

				# Add feature to the appropriate list
				features[location].append(feature)

			elif field==Features.FTR:
				outcome = Features.FTR_MAP[feature]

		# Horizontally join the two lists
		train.append(features[Constants.HOME] + features[Constants.AWAY] + [outcome])

	OUTFILE = "%s.mat"%csvfile

	#Write to mat file
	np_teams = np.array(teams)
	np_train = np.array(train)
	np_dict = {'teams':np_teams, 'train':np_train}

	io.savemat(OUTFILE, np_dict)

if __name__=="__main__":
	main(sys.argv[1:])


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

import requests
from lxml import etree

CN_NUM = {u'〇': 0,
          u'一': 1,
          u'二': 2,
          u'三': 3,
          u'四': 4,
          u'五': 5,
          u'六': 6,
          u'七': 7,
          u'八': 8,
          u'九': 9,
          u'零': 0,
          u'壹': 1,
          u'贰': 2,
          u'叁': 3,
          u'肆': 4,
          u'伍': 5,
          u'陆': 6,
          u'柒': 7,
          u'捌': 8,
          u'玖': 9,
          u'貮': 2,
          u'两': 2,
}

CN_UNIT = {
    u'十': 10,
    u'拾': 10,
    u'百': 100,
    u'佰': 100,
    u'千': 1000,
    u'仟': 1000,
    u'万': 10000,
    u'萬': 10000,
    u'亿': 100000000,
    u'億': 100000000,
    u'兆': 1000000000000,
}

def cn2digTransformer(cn):
    lcn = list(cn)
    unit = 0  # 当前的单位
    ldig = []  # 临时数组

    ret = 0
    tmp = 0

    if lcn[-2:] == ['零', '十']:    # “一千零十”
        ret = 10
        lcn = lcn[:-2]

    while lcn:
        cndig = lcn.pop()
        if cndig in CN_UNIT:          # python2: CN_UNIT.has_key(cndig)
            unit = CN_UNIT.get(cndig)
            if unit == 10000:
                ldig.append('w')  # 标示万位
                unit = 1
            elif unit == 100000000:
                ldig.append('y')  # 标示亿位
                unit = 1
            elif unit == 1000000000000:  # 标示兆位
                ldig.append('z')
                unit = 1
            continue
        else:
            dig = CN_NUM.get(cndig)
            if dig==None:
                return False
            else:
                if unit:
                    dig *= unit
                    if len(ldig) == 1 and isinstance(ldig[0], int) and ldig[0] < 10:   # “七百五”“七千五”的“五”不是个位
                        ldig[0] = ldig[0]*unit//10
                    if len(ldig) == 2 and isinstance(ldig[0], int) and ldig[0] < 10:   # “七亿五”“七万五”的“五”不是个位
                        if ldig[-1] == 'w':
                            ldig[0] *= 1000
                        elif ldig[-1] == 'y':
                            ldig[0] *= 10000000
                        elif ldig[-1] == 'z':
                            ldig[0] *= 100000000000
                    unit = 0

                ldig.append(dig)

    if unit == 10:  # 处理10-19的数字
        ldig.append(10)

    while ldig:
        x = ldig.pop()
        if x == 'w':
            tmp *= 10000
            ret += tmp
            tmp = 0
        elif x == 'y':
            tmp *= 100000000
            ret += tmp
            tmp = 0
        elif x == 'z':
            tmp *= 1000000000000
            ret += tmp
            tmp = 0
        else:
            tmp += x
    ret += tmp
    return str(ret)

class Book(object):

    def start(self, name):
        """根据关键词获取搜索页"""
        url = "http://www.xbiquge.la/modules/article/waps.php"
        response = requests.post(url, data={"searchkey": name})
        if response.status_code == 200:
            content = response.content
            html = etree.HTML(content)
            book_list = html.xpath('//table[@class="grid"]/tr')
            book_list2 = []
            print("搜索结果如下")
            for book in book_list:
                b_name = book.xpath('./td[@class="even"]/a/text()')
                if b_name:
                    b_name = b_name[0]
                    b_id = book.xpath('./td[@class="even"]/a/@href')[0]
                    b_author = book .xpath('./td[@class="even"]/text()')[0]
                    book_list2.append((b_name, b_id))
                    print(b_name, b_id, b_author)
                    print("")
            name = input("请从输入以上列表中的书籍名或输入NO退出:")
            if name == "NO" or name == "No" or name == "no" or name == "nO":
                return
            else:
                for b in book_list2:
                    if name == b[0]:
                        b_url = b[1]
                        self.get_page(b_url)

    def get_page(self, link):
        """定位需要开始的章节"""
        s_list = self.get_section(link)

        start_s = int(input("开始章节:"))
        now_s = 1
        for s in s_list:
            t, u = s
            if int(now_s) >= int(start_s):
                self.get_info(t, u, now_s)
                input("回车继续")
            now_s += 1

    def get_section(self, link):
        """获取章节列表"""
        response = requests.get(link)
        if response.status_code == 200:
            content = response.content
            html = etree.HTML(content)
            section_list = html.xpath('//div[@id ="list"]/dl/dd/a/@href')
            title_list = html.xpath('//div[@id="list"]/dl/dd/a/text()')
            a = 1
            for t in title_list:
                print(a, t)
                a += 1
            return zip(title_list, section_list)
        else:
            return []

    def get_info(self, title, link, now_s):
        """获取并输出章节内容"""
        link = "http://www.xbiquge.la" + link
        print(link)
        print(now_s, title)
        response = requests.get(link)
        if response. status_code == 200:
            content = response.content
            html = etree.HTML(content)
            body = html.xpath('//div[@id="content"]/text()')
            for b in body:
                 print(b)

if __name__ == '__main__':
    # book_name = input("书籍名:")
    book_name = "圣光"
    book = Book()
    book.start(book_name)