import collections
import functools
import xml.dom.minidom

import dbus

import qui.models.base
from typing import Any, Callable, Dict, Union

OBJECT_MANAGER_INTERFACE = 'org.freedesktop.DBus.ObjectManager'
PROPERTIES_INTERFACE = 'org.freedesktop.DBus.Properties'
PROPERTIES_CHANGED = 'PropertiesChanged'

_DictKey = Union[str, dbus.String]  # pylint: disable=invalid-name,no-member

class Interface(qui.models.base.Interface):
    ''' Represents a D-Bus Interface '''

    # pylint: disable=too-few-public-methods
    def __init__(self, node: xml.dom.minidom.Element) -> None:
        name = _name(node)
        signals = {
            _attr(signal_node, 'name'): qui.models.base.Signal(signal_node)
            for signal_node in _children(node, 'signal')
        }
        methods = {
            _attr(method_node, 'name'): qui.models.base.Method(method_node)
            for method_node in _children(node, 'method')
        }
        super(Interface, self).__init__(name, methods, signals)

class Model(qui.models.base.Model):
    ''' Wrapper around the `dbus.proxies.ProxyObject`.  '''

    # pylint: disable=too-few-public-methods

    def __init__(self, proxy: dbus.proxies.ProxyObject) -> None:  # pylint: disable=no-member
        self.proxy = proxy
        super(Model, self).__init__(self._init_interfaces())

    def _setup_methods(self):
        for iface_name, iface in self.interfaces.items():
            for method in iface.methods.keys():
                func = self._wrap_dbus_method(iface_name, method)
                partial_function = functools.partial(func, self.proxy)
                setattr(self, method, partial_function)

    def _init_interfaces(self) -> Dict[str, Interface]:
        ''' Initialize the `Interface` objects '''
        xml_str = str(self.proxy.Introspect())
        doc = xml.dom.minidom.parseString(xml_str)

        root = doc.childNodes[1]  # skip doctype
        assert root.nodeName == 'node'

        return {
            _name(iface): Interface(iface)
            for iface in root.getElementsByTagName('interface')
        }

    def _wrap_dbus_method(self, iface_name: str, func_name: str) -> Callable:
        ''' Wrapper around a lambda calling
            `dbus.proxies.ProxyObject.get_dbus_method()` with the right
            interface and executing it.
        '''
        return lambda obj, *args, **kwargs: \
            self.proxy.get_dbus_method(func_name, dbus_interface=iface_name)(*args, **kwargs)

class Method(qui.models.base.Method):
    ''' Represents a D-Bus Method '''

    # pylint: disable=too-few-public-methods
    def __init__(self, method: xml.dom.minidom.Element) -> None:
        args = [
            _attr(arg, 'type') for arg in _children(method, 'arg')
            if _attr(arg, 'direction') == 'in'
        ]
        super(Method, self).__init__(name=_name(method), *args)

class Signal(qui.models.base.Signal):
    ''' Represents a D-Bus Signal '''

    def __init__(self, signal: xml.dom.minidom.Element) -> None:
        kwargs = {
            _name(arg): _attr(arg, 'type')
            for arg in _children(signal, 'arg')
        }
        super(Signal, self).__init__(name=_name(signal), **kwargs)

class Properties(Model, collections.MutableMapping):
    ''' Provides dictionary access to a `org.freedesktop.DBus.Properties` object
    ''' # pylint: disable=too-few-public-methods,too-many-ancestors

    def __init__(self,
                 proxy: dbus.proxies.ProxyObject,  # pylint: disable=no-member
                 data: dbus.Dictionary=None,  # pylint: disable=no-member
                 *args, **kwargs) -> None:
        super(Properties, self).__init__(proxy, *args, **kwargs)
        assert PROPERTIES_INTERFACE in self.interfaces
        if data is None:
            # pylint: disable=no-member
            self._data = self.GetAll('')  # type: ignore
        else:
            self._data = data

        def _update(interface, changed_properties, invalidated):
            ''' Update the internal dictionary when a property changes '''
            # pylint: disable=unused-argument
            for key, value in changed_properties.items():
                self._data[key] = value

        proxy.connect_to_signal(PROPERTIES_CHANGED, _update,
                                dbus_interface=PROPERTIES_INTERFACE)

    def __getitem__(self, key: _DictKey):
        return self._data[key]

    def __setitem__(self, key: _DictKey, value: Any) -> None:
        self.proxy.Set('', key, value)  # type: ignore

    def __delitem__(self, key: _DictKey):
        msg = 'It is not possible to delete D-Bus properties'
        raise NotImplementedError(msg)

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

    def __iter__(self):
        return iter(self._data)

class ObjectManager(Model):
    ''' A Model which has child models. '''

    # pylint: disable=too-few-public-methods
    def __init__(self,
                 proxy: dbus.proxies.ProxyObject,  # pylint: disable=no-member
                 cls:type=Properties) -> None:
        super(ObjectManager, self).__init__(proxy)
        assert OBJECT_MANAGER_INTERFACE in self.interfaces
        child_data = self.GetManagedObjects()
        self.children = {}  # type: Dict[dbus.ObjectPath, Properties]
        bus = dbus.SessionBus()  # pylint: disable=no-member
        for child_path, _kwargs in child_data.items():
            _data = list(_kwargs.values())[0]
            child_proxy = bus.get_object(bus_name=proxy.bus_name,
                                         object_path=child_path,
                                         follow_name_owner_changes=True)
            self.children[child_proxy.object_path] = cls(child_proxy, _data)

    def GetManagedObjects(self):
        ''' Wrapper around
            'org.freedesktop.DBus.ObjectManager.GetManagedObjects'. This wrapper
            is used to keep mypy and pylint happy.
        '''  # pylint: disable=useless-super-delegation
        # type:ignore pylint: disable=no-member,invalid-name
        return super(ObjectManager, self).GetManagedObjects()

def _name(elem: xml.dom.minidom.Element) -> str:
    ''' Returns the name attribute from a `xml.dom.minidom.Element`. '''
    return str(elem.getAttribute('name'))

def _children(elem: xml.dom.minidom.Element,
              name: str) -> xml.dom.minidom.NodeList:
    ''' Returns children with specified name elements of a
        `xml.dom.minidom.Element`.
    '''
    return elem.getElementsByTagName(name)

def _attr(elem: xml.dom.minidom.Element, name: str) -> str:
    ''' Shorthand for `str(elem.getAttribute(name))`. '''
    return str(elem.getAttribute(name))


import sys
input = sys.stdin.readline
from collections import deque

T = int(input())
for _ in range(T):
    p = input().rstrip()
    n = int(input())
    tmp = input().rstrip()[1:-1].split(",")
    q = deque(tmp)
    if not n:
        q = deque()

    cnt = 0
    flag = False
    for i in p:
        if i == "R":
            # q.reverse()
            cnt += 1
        else:
            if q:
                # 현재까지 뒤집는 횟수가 홀수면 뒤집어서 popleft => pop()
                # 짝수면 안 뒤집어도 됨 => popleft()
                if cnt % 2:
                    q.pop()
                else:
                    q.popleft()
            else:
                print("error")
                flag = True
                break
    if flag:
        continue

    # R 횟수가 홀수면 뒤집기
    if cnt % 2:
        q.reverse()

    result = "["
    result += ",".join(list(q))
    result += "]"
    print(result)


# Author : Shivam Chauhan
# Date   : March 6, 2019

# This scripts takes search matter as input and open up the best
# matched links(on the first page usually) in new tabs in the browser
# automatically.

from bs4 import BeautifulSoup
import requests
import webbrowser

content = input("Enter the content to be searched : ") # Get the content
urlAddress = 'http://google.com/search?q=' + content  # Set up Url
try:
	res = requests.get(urlAddress) # Get request object
	res.raise_for_status() # Check the status
	webbrowser.open(urlAddress) # Open browser's windows
	soup = BeautifulSoup(res.text, 'html.parser')
	linkElements = soup.select('.r a')
	numOpen = min(5, len(linkElements))
	for i in range(numOpen): # Opening up of tabs
		webbrowser.open('http://google.com' + linkElements[i].get('href'))
except Exception as err:
	print("Oops!!, you entered a wrong URL " )


#!/usr/bin/env python3
# coding: utf-8

import json
import nrn_validation as V

V.override_defaults_from_args()

# dendrite geometry: 200 µm long, diameter 1 µm to 0.4 µm.
geom = [(0, 1.0), (200, 0.4)]

model = V.VModel()
model.add_soma(12.6157)
model.add_dendrite("taper", geom)
model.add_iclamp(5, 80, 0.3, to="taper")

data = V.run_nrn_sim(100, report_dt=10, model="ball_and_taper")
print(json.dumps(data))
V.nrn_stop()


import json
from django.shortcuts import render
from django.http import JsonResponse
from common.utils import common_response, logger, page_calculate
from common.models import User, Grade
from knowledges.models import Knowledge
from knowledges.serializers.knowledge import knowledge_filter, knowledge_data

# 教程列表页面
def knowledge_list_page(request):
    if request.method == "GET":
        knowledges = [{"id": k.id, "name": k.title} for k in knowledge_filter()]
        datas = {
            "knowledges": knowledges
        }
        return render(request, "knowledge/knowledge_list.html", datas)

# 教程列表
def knowledge_list(request):
    if request.method == "GET":
        user = request.user
        filter_name = request.GET.get('filter_name')
        filter_knowledge = request.GET.get('filter_knowledge')
        query = knowledge_filter()
        if filter_knowledge:
            query = query.filter(id=filter_knowledge)
        if filter_name:
            query = query.filter(title__icontains=filter_name)
        data_dict = {}
        try:
            logger.info(query.count())
            data_dict, datas = page_calculate(request_data=request.GET, data=query)
            result = []
            for data in datas:
                logger.info(data.id)
                res = knowledge_data(knowledge=data)
                result.append(res)
            data_dict['rows'] = result
        except Exception as ex:
            logger.exception(ex)
        return JsonResponse(data_dict)

# 教程删除
def knowledge_delete(request):
    req = json.loads(request.body)
    knowledge_id = req.get('id')
    knowledge_filter(id=knowledge_id).delete()
    return common_response()


from collections.abc import Mapping, Sequence
from pathlib import Path
from typing import Literal

import pytest
from freezegun import freeze_time

import cmk.utils.paths
from cmk.utils.hostaddress import HostName

from cmk.base.plugins.agent_based import logwatch_ec
from cmk.base.plugins.agent_based.agent_based_api.v1 import Metric, Result, Service, State
from cmk.base.plugins.agent_based.agent_based_api.v1.type_defs import (
    CheckResult,
    DiscoveryResult,
    StringTable,
)
from cmk.base.plugins.agent_based.logwatch_section import parse_logwatch
from cmk.base.plugins.agent_based.utils import logwatch as logwatch_

from cmk.ec.export import SyslogMessage

_STRING_TABLE_NO_MESSAGES = [
    ["[[[log1]]]"],
    ["[[[log2]]]"],
    ["[[[log3:missing]]]"],
    ["[[[log4:cannotopen]]]"],
    ["[[[log5]]]"],
    ["[[[log1:missing]]]"],
]

_STRING_TABLE_MESSAGES_LOG1 = [
    ["[[[log1]]]"],
    ["BATCH: 1680617834-122172169179246007103019047128114004006211120121"],
    ["C ERROR: issue 1"],
    ["C ERROR: issue 2"],
    ["[[[log2]]]"],
    ["[[[log3:missing]]]"],
    ["[[[log4:cannotopen]]]"],
    ["[[[log5]]]"],
    ["[[[log1:missing]]]"],
]

_STRING_TABLE_MESSAGES_LOG1_2 = [
    ["[[[log1]]]"],
    ["BATCH: 1680617840-135239174175144102013221144181058125008119107236"],
    ["C ERROR: issue 1"],
    ["C ERROR: issue 2"],
    ["C ERROR: issue 3"],
]

_STRING_TABLE_MESSAGES_LOG5 = [
    ["[[[log2]]]"],
    ["[[[log3:missing]]]"],
    ["[[[log4:cannotopen]]]"],
    ["[[[log5]]]"],
    ["BATCH: 1680617711-122172169179246007103019047128114004006211120555"],
    ["C ERROR: issue 1"],
    ["C ERROR: issue 2"],
]

SECTION1 = logwatch_.Section(
    errors=[],
    logfiles={
        "log1": {
            "attr": "ok",
            "lines": {
                "test": [
                    "W This long message should be written to one spool file",
                    "C And this long message should be written to another spool file",
                    "W This last long message should be written to a third spool file",
                ]
            },
        },
    },
)

@pytest.mark.parametrize(
    "info, fwd_rule, expected_result",
    [
        (_STRING_TABLE_NO_MESSAGES, [], []),
        (
            _STRING_TABLE_NO_MESSAGES,
            [{"separate_checks": True}],
            [
                Service(item="log1", parameters={"expected_logfiles": ["log1"]}),
                Service(item="log2", parameters={"expected_logfiles": ["log2"]}),
                Service(item="log4", parameters={"expected_logfiles": ["log4"]}),
                Service(item="log5", parameters={"expected_logfiles": ["log5"]}),
            ],
        ),
        (_STRING_TABLE_NO_MESSAGES, [{"restrict_logfiles": [".*"]}], []),
        (
            _STRING_TABLE_NO_MESSAGES,
            [
                {
                    "restrict_logfiles": [".*"],
                    "separate_checks": True,
                }
            ],
            [
                Service(item="log1", parameters={"expected_logfiles": ["log1"]}),
                Service(item="log2", parameters={"expected_logfiles": ["log2"]}),
                Service(item="log4", parameters={"expected_logfiles": ["log4"]}),
                Service(item="log5", parameters={"expected_logfiles": ["log5"]}),
            ],
        ),
        (
            _STRING_TABLE_NO_MESSAGES,
            [
                {
                    "restrict_logfiles": [".*"],
                    "separate_checks": False,
                }
            ],
            [],
        ),
        (
            _STRING_TABLE_NO_MESSAGES,
            [
                {
                    "restrict_logfiles": [".*"],
                }
            ],
            [],
        ),
        (
            _STRING_TABLE_NO_MESSAGES,
            [
                {
                    "restrict_logfiles": ["log1"],
                    "separate_checks": True,
                    "method": "pass me on!",
                    "facility": "pass me on!",
                    "monitor_logfilelist": "pass me on!",
                    "monitor_logfile_access_state": "pass me on!",
                    "logwatch_reclassify": "pass me on!",
                    "some_other_key": "I should be discarded!",
                }
            ],
            [
                Service(
                    item="log1",
                    parameters={
                        "expected_logfiles": ["log1"],
                        "method": "pass me on!",
                        "facility": "pass me on!",
                        "monitor_logfilelist": "pass me on!",
                        "monitor_logfile_access_state": "pass me on!",
                        "logwatch_reclassify": "pass me on!",
                    },
                ),
            ],
        ),
    ],
)
def test_logwatch_ec_inventory_single(
    monkeypatch: pytest.MonkeyPatch,
    info: StringTable,
    fwd_rule: Mapping[str, object],
    expected_result: DiscoveryResult,
) -> None:
    parsed = parse_logwatch(info)

    monkeypatch.setattr(logwatch_, "get_ec_rule_params", lambda: fwd_rule)
    actual_result = sorted(logwatch_ec.discover_single(parsed), key=lambda s: s.item or "")
    assert actual_result == expected_result

@pytest.mark.parametrize(
    "info, fwd_rule, expected_result",
    [
        (_STRING_TABLE_NO_MESSAGES, [], []),
        (_STRING_TABLE_NO_MESSAGES, [{"separate_checks": True}], []),
        (
            _STRING_TABLE_NO_MESSAGES,
            [{"separate_checks": False}],
            [
                Service(parameters={"expected_logfiles": ["log1", "log2", "log4", "log5"]}),
            ],
        ),
        (
            _STRING_TABLE_NO_MESSAGES,
            [{"restrict_logfiles": [".*[12]"], "separate_checks": False}],
            [
                Service(parameters={"expected_logfiles": ["log1", "log2"]}),
            ],
        ),
    ],
)
def test_logwatch_ec_inventory_groups(
    monkeypatch: pytest.MonkeyPatch,
    info: StringTable,
    fwd_rule: Mapping[str, object],
    expected_result: DiscoveryResult,
) -> None:
    parsed = parse_logwatch(info)

    monkeypatch.setattr(logwatch_, "get_ec_rule_params", lambda: fwd_rule)
    actual_result = list(logwatch_ec.discover_group(parsed))
    assert actual_result == expected_result

class _FakeForwarder:
    def __call__(
        self,
        method: str | tuple,
        messages: Sequence[SyslogMessage],
    ) -> logwatch_ec.LogwatchForwardedResult:
        return logwatch_ec.LogwatchForwardedResult(num_forwarded=len(messages))

@pytest.mark.parametrize(
    "item, params, parsed, expected_result",
    [
        (
            "log1",
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {"node1": parse_logwatch(_STRING_TABLE_NO_MESSAGES)},
            [
                Result(state=State.OK, summary="Forwarded 0 messages"),
                Metric("messages", 0.0),
            ],
        ),
        (
            "log4",
            {
                "facility": 17,  # default to "local1"
                "method": "",  # local site
                "monitor_logfilelist": False,
                "monitor_logfile_access_state": 2,
                "expected_logfiles": ["log4"],
            },
            {"node1": parse_logwatch(_STRING_TABLE_NO_MESSAGES)},
            [
                Result(state=State.CRIT, summary="[node1] Could not read log file 'log4'"),
                Result(state=State.OK, summary="Forwarded 0 messages"),
                Metric("messages", 0.0),
            ],
        ),
    ],
)
def test_check_logwatch_ec_common_single_node(
    item: str | None,
    params: logwatch_.ParameterLogwatchEc,
    parsed: logwatch_ec.ClusterSection,
    expected_result: CheckResult,
) -> None:
    assert (
        list(
            logwatch_ec.check_logwatch_ec_common(
                item,
                params,
                parsed,
                service_level=10,
                value_store={},
                hostname=HostName("test-host"),
                message_forwarder=_FakeForwarder(),
            )
        )
        == expected_result
    )

def test_check_logwatch_ec_common_single_node_item_missing() -> None:
    assert not list(
        logwatch_ec.check_logwatch_ec_common(
            "log1",
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG5),
            },
            service_level=10,
            value_store={},
            hostname=HostName("test-host"),
            message_forwarder=_FakeForwarder(),
        )
    )

def test_check_logwatch_ec_common_single_node_log_missing() -> None:
    actual_result = list(
        logwatch_ec.check_logwatch_ec_common(
            "log3",
            {
                "facility": 17,  # default to "local1"
                "method": "",  # local site
                "monitor_logfilelist": True,
                "monitor_logfile_access_state": 2,
                "expected_logfiles": ["log3"],
            },
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG5),
            },
            service_level=10,
            value_store={},
            hostname=HostName("test-host"),
            message_forwarder=_FakeForwarder(),
        )
    )

    assert actual_result == [
        Result(state=State.WARN, summary="Missing logfiles: log3 (on node1)"),
        Result(state=State.OK, summary="Forwarded 0 messages"),
        Metric("messages", 0.0),
    ]

@pytest.mark.parametrize(
    ["cluster_section", "expected_result"],
    [
        pytest.param(
            {
                "node1": parse_logwatch(_STRING_TABLE_NO_MESSAGES),
                "node2": parse_logwatch(_STRING_TABLE_NO_MESSAGES),
            },
            [
                Result(state=State.OK, summary="Forwarded 0 messages"),
                Metric("messages", 0.0),
            ],
            id="no messages",
        ),
        pytest.param(
            {
                "node1": parse_logwatch(_STRING_TABLE_NO_MESSAGES),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
            },
            [
                Result(state=State.OK, summary="Forwarded 2 messages from log1"),
                Metric("messages", 2.0),
            ],
            id="messages on one node",
        ),
        pytest.param(
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1_2),
            },
            [
                Result(state=State.OK, summary="Forwarded 5 messages from log1"),
                Metric("messages", 5.0),
            ],
            id="messages on both nodes",
        ),
    ],
)
def test_check_logwatch_ec_common_multiple_nodes_grouped(
    cluster_section: logwatch_ec.ClusterSection,
    expected_result: CheckResult,
) -> None:
    assert (
        list(
            logwatch_ec.check_logwatch_ec_common(
                "log1",
                logwatch_ec.CHECK_DEFAULT_PARAMETERS,
                cluster_section,
                service_level=10,
                value_store={},
                hostname=HostName("test-host"),
                message_forwarder=_FakeForwarder(),
            )
        )
        == expected_result
    )

@pytest.mark.parametrize(
    ["params", "cluster_section", "expected_result"],
    [
        pytest.param(
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {
                "node1": parse_logwatch(_STRING_TABLE_NO_MESSAGES),
                "node2": parse_logwatch(_STRING_TABLE_NO_MESSAGES),
            },
            [
                Result(state=State.CRIT, summary="[node1] Could not read log file 'log4'"),
                Result(state=State.CRIT, summary="[node2] Could not read log file 'log4'"),
                Result(state=State.OK, summary="Forwarded 0 messages"),
                Metric("messages", 0.0),
            ],
            id="no messages",
        ),
        pytest.param(
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {
                "node1": parse_logwatch(_STRING_TABLE_NO_MESSAGES),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
            },
            [
                Result(state=State.CRIT, summary="[node1] Could not read log file 'log4'"),
                Result(state=State.CRIT, summary="[node2] Could not read log file 'log4'"),
                Result(state=State.OK, summary="Forwarded 2 messages from log1"),
                Metric("messages", 2.0),
            ],
            id="messages on one node",
        ),
        pytest.param(
            {
                "facility": 17,  # default to "local1"
                "method": "",  # local site
                "monitor_logfilelist": False,
                "monitor_logfile_access_state": 2,
                "expected_logfiles": ["log4"],
            },
            {
                "node1": parse_logwatch(_STRING_TABLE_NO_MESSAGES),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
            },
            [
                Result(state=State.CRIT, summary="[node1] Could not read log file 'log4'"),
                Result(state=State.CRIT, summary="[node2] Could not read log file 'log4'"),
                Result(state=State.OK, summary="Forwarded 2 messages from log1"),
                Metric("messages", 2.0),
            ],
            id="no access to logfile on both nodes",
        ),
        pytest.param(
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1_2),
            },
            [
                Result(state=State.CRIT, summary="[node1] Could not read log file 'log4'"),
                Result(state=State.OK, summary="Forwarded 5 messages from log1"),
                Metric("messages", 5.0),
            ],
            id="messages on both nodes, same logfile",
        ),
        pytest.param(
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG5),
            },
            [
                Result(state=State.CRIT, summary="[node1] Could not read log file 'log4'"),
                Result(state=State.CRIT, summary="[node2] Could not read log file 'log4'"),
                Result(state=State.OK, summary="Forwarded 4 messages from log1, log5"),
                Metric("messages", 4.0),
            ],
            id="messages on both nodes, different logfiles",
        ),
    ],
)
def test_check_logwatch_ec_common_multiple_nodes_ungrouped(
    params: logwatch_.DictLogwatchEc,
    cluster_section: logwatch_ec.ClusterSection,
    expected_result: CheckResult,
) -> None:
    assert (
        list(
            logwatch_ec.check_logwatch_ec_common(
                None,
                params,
                cluster_section,
                service_level=10,
                value_store={},
                hostname=HostName("test-host"),
                message_forwarder=_FakeForwarder(),
            )
        )
        == expected_result
    )

def test_check_logwatch_ec_common_multiple_nodes_item_completely_missing() -> None:
    assert not list(
        logwatch_ec.check_logwatch_ec_common(
            "log1",
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG5),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG5),
            },
            service_level=10,
            value_store={},
            hostname=HostName("test-host"),
            message_forwarder=_FakeForwarder(),
        )
    )

def test_check_logwatch_ec_common_multiple_nodes_item_partially_missing() -> None:
    assert list(
        logwatch_ec.check_logwatch_ec_common(
            "log1",
            logwatch_ec.CHECK_DEFAULT_PARAMETERS,
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG5),
            },
            service_level=10,
            value_store={},
            hostname=HostName("test-host"),
            message_forwarder=_FakeForwarder(),
        )
    ) == [
        Result(state=State.OK, summary="Forwarded 2 messages from log1"),
        Metric("messages", 2.0),
    ]

def test_check_logwatch_ec_common_multiple_nodes_logfile_missing() -> None:
    assert list(
        logwatch_ec.check_logwatch_ec_common(
            "log3",
            {
                "facility": 17,  # default to "local1"
                "method": "",  # local site
                "monitor_logfilelist": True,
                "monitor_logfile_access_state": 2,
                "expected_logfiles": ["log3"],
            },
            {
                "node1": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
                "node2": parse_logwatch(_STRING_TABLE_MESSAGES_LOG1),
            },
            service_level=10,
            value_store={},
            hostname=HostName("test-host"),
            message_forwarder=_FakeForwarder(),
        )
    ) == [
        Result(state=State.WARN, summary="Missing logfiles: log3 (on node1, node2)"),
        Result(state=State.OK, summary="Forwarded 0 messages"),
        Metric("messages", 0.0),
    ]

def test_check_logwatch_ec_common_spool(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(logwatch_ec, "_MAX_SPOOL_SIZE", 32)
    assert list(
        logwatch_ec.check_logwatch_ec_common(
            "log1",
            {
                **logwatch_ec.CHECK_DEFAULT_PARAMETERS,
                "method": "spool:",
            },
            {
                "node1": SECTION1,
            },
            service_level=10,
            value_store={},
            hostname=HostName("test-host"),
            message_forwarder=logwatch_ec.MessageForwarder("log1", HostName("test-host")),
        )
    ) == [
        Result(state=State.OK, summary="Forwarded 3 messages from log1"),
        Metric("messages", 3.0),
    ]
    assert len(list(Path(cmk.utils.paths.omd_root, "var/mkeventd/spool").iterdir())) == 3

class FakeTcpError(Exception):
    pass

class FakeTcpErrorRaised(Exception):
    pass

def _forward_message(
    tcp_result: Literal["ok", "raise exception", "set exception"],
    method: tuple[str, dict[str, object]] = ("tcp", {"address": "127.0.0.1", "port": 127001}),
    text: str = "some_text",
    item: str | None = None,
    application: str = "-",
) -> tuple[logwatch_ec.LogwatchForwardedResult, list[tuple[float, int, list[str]]],]:
    messages_forwarded: list[tuple[float, int, list[str]]] = []

    class TestForwardTcpMessageForwarder(logwatch_ec.MessageForwarder):
        @staticmethod
        def _forward_send_tcp(method, message_chunks, result):
            nonlocal messages_forwarded
            if tcp_result == "ok":
                for message in message_chunks:
                    messages_forwarded.append(message)
                    result.num_forwarded += 1
            elif tcp_result == "set exception":
                result.exception = FakeTcpError("could not send messages")
            elif tcp_result == "raise exception":
                raise FakeTcpErrorRaised("rise and shine")
            else:
                raise NotImplementedError()

    result = TestForwardTcpMessageForwarder(item=item, hostname=HostName("some_host_name"))(
        method=method,
        messages=[
            SyslogMessage(facility=1, severity=1, timestamp=0.0, text=text, application=application)
        ],
    )

    return result, messages_forwarded

def test_forward_tcp_message_forwarded_ok() -> None:
    result, messages_forwarded = _forward_message(tcp_result="ok")
    assert result == logwatch_ec.LogwatchForwardedResult(
        num_forwarded=1,
        num_spooled=0,
        num_dropped=0,
        exception=None,
    )

    assert len(messages_forwarded) == 1
    # first element of message is a timestamp!
    assert messages_forwarded[0][1:] == (
        0,
        ["<9>1 1970-01-01T00:00:00+00:00 - - - - [Checkmk@18662] some_text"],
    )

def test_forward_tcp_message_forwarded_nok_1() -> None:
    result, messages_forwarded = _forward_message(tcp_result="set exception")

    assert result.num_forwarded == 0
    assert result.num_spooled == 0
    assert result.num_dropped == 1
    assert isinstance(result.exception, FakeTcpError)

    assert len(messages_forwarded) == 0

def test_forward_tcp_message_forwarded_nok_2() -> None:
    result, messages_forwarded = _forward_message(tcp_result="raise exception")

    assert result.num_forwarded == 0
    assert result.num_spooled == 0
    assert result.num_dropped == 1
    assert isinstance(result.exception, FakeTcpErrorRaised)

    assert len(messages_forwarded) == 0

SPOOL_METHOD = (
    "tcp",
    {
        "address": "127.0.0.1",
        "port": 127001,
        "spool": {"max_age": 60 * 60, "max_size": 1024 * 1024},
    },
)

def test_forward_tcp_message_forwarded_spool() -> None:
    # could not send message, so spool it
    result, messages_forwarded = _forward_message(
        tcp_result="set exception", method=SPOOL_METHOD, text="spooled"
    )
    assert result.num_forwarded == 0
    assert result.num_spooled == 1
    assert result.num_dropped == 0
    assert isinstance(result.exception, FakeTcpError)
    assert len(messages_forwarded) == 0

    # sending works again, so send both of them
    result, messages_forwarded = _forward_message(
        tcp_result="ok", method=SPOOL_METHOD, text="directly_sent_1"
    )
    assert result.num_forwarded == 2
    assert result.num_spooled == 0
    assert result.num_dropped == 0
    assert len(messages_forwarded) == 2

    assert messages_forwarded[0][2][0].rsplit(" ", 1)[-1] == "spooled"
    assert messages_forwarded[1][2][0].rsplit(" ", 1)[-1] == "directly_sent_1"

    # sending is still working, so send only one
    result, messages_forwarded = _forward_message(
        tcp_result="ok", method=SPOOL_METHOD, text="directly_sent_2"
    )
    assert result.num_forwarded == 1
    assert result.num_spooled == 0
    assert result.num_dropped == 0
    assert len(messages_forwarded) == 1

    assert messages_forwarded[0][2][0].rsplit(" ", 1)[-1] == "directly_sent_2"

def test_forward_tcp_message_forwarded_spool_twice() -> None:
    # we delete the original spool file after reading it.
    # here we want to make sure, that the spool file is recreated. otherwise messages from different
    # time would land into the same spool file and may not be correctly cleaned up.
    spool_dir = Path(cmk.utils.paths.var_dir, "logwatch_spool", "some_host_name")

    # create a spooled message:
    with freeze_time("2023-10-31 16:02:00"):
        result, messages_forwarded = _forward_message(
            tcp_result="set exception", method=SPOOL_METHOD
        )
    assert result.num_forwarded == 0
    assert result.num_spooled == 1
    assert result.num_dropped == 0
    assert isinstance(result.exception, FakeTcpError)
    assert len(messages_forwarded) == 0

    # we expect one spool file to be created:
    assert list(f.name for f in spool_dir.iterdir()) == ["spool.1698768120.00"]

    # create another spooled message:
    with freeze_time("2023-10-31 16:03:00"):
        result, messages_forwarded = _forward_message(
            tcp_result="set exception", method=SPOOL_METHOD
        )
    assert result.num_forwarded == 0
    assert result.num_spooled == 2
    assert result.num_dropped == 0
    assert isinstance(result.exception, FakeTcpError)
    assert len(messages_forwarded) == 0

    # now let's see if we have two spool files
    assert set(f.name for f in spool_dir.iterdir()) == {
        "spool.1698768120.00",
        "spool.1698768180.00",
    }

def test_forward_tcp_message_update_old_spoolfiles() -> None:
    # can be removed with checkmk 2.4.0
    spool_dir = Path(cmk.utils.paths.var_dir, "logwatch_spool", "some_host_name")
    # logwatch_ec with separate_checks = True creates one service per syslog application, but they
    # shared one folder for their spool files. this led to problems when writing spool-files
    # (overwriting each other) and reading spool-files (all items read all spool-files).
    # with werk 15307 it was introduced that each service get its own subfolder for spool-files.
    # here we want to check the update process: spool-files from the host-folder should be
    # read and moved to the correct subfolder.

    # first we create a spooled message for a logwatch_ec service with "separate_checks" = False
    # this is the same as the old behaviour, before werk 15397 with "seperate_checks" = True
    with freeze_time("2023-10-31 16:02:00"):
        _result, messages_forwarded = _forward_message(
            tcp_result="set exception",
            method=SPOOL_METHOD,
            application="item_name_1",
        )
    # we expect one spool file to be created:
    assert list(f.name for f in spool_dir.iterdir()) == ["spool.1698768120.00"]

    # now we do the same, but for a different item:
    with freeze_time("2023-10-31 16:03:00"):
        _result, messages_forwarded = _forward_message(
            tcp_result="set exception",
            method=SPOOL_METHOD,
            application="another_item",
        )
    # we expect two spool files in the host folder:
    assert set(f.name for f in spool_dir.iterdir()) == {
        "spool.1698768120.00",
        "spool.1698768180.00",
    }

    # this was the old behaviour. now we image the customer installed the new version of checkmk.
    # their logwatch_ec services had separate_checks = True from the beginning.

    with freeze_time("2023-10-31 16:04:00"):
        _result, messages_forwarded = _forward_message(
            tcp_result="ok",
            method=SPOOL_METHOD,
            item="item_name_1",
            application="item_name_1",
        )

    # we now expect, that the item_name_1 message was found (although in the old directory) and the
    # new message was also sent:
    assert len(messages_forwarded) == 2

    # and we also expect, the spool-file of another_item to be left alone:
    assert list(f.name for f in spool_dir.iterdir() if f.is_file()) == [
        "spool.1698768180.00",
    ]
    # and a folder which held the spooled message for a short time
    assert list(f.name for f in spool_dir.iterdir() if f.is_dir()) == [
        "item_item_name_1",
    ]
    # but is should now be empty as we sent both messages successfully:
    assert not list(f.name for f in (spool_dir / "item_item_name_1").iterdir())

def test_logwatch_spool_path_is_escaped():
    # item may contain slashes or other stuff, we want to make sure
    # that this is transformed to a single folder name:
    get_spool_path = logwatch_ec.MessageForwarder._get_spool_path
    result = get_spool_path(HostName("some_host_name"), "some/log/path")
    assert result.name == "item_some%2Flog%2Fpath"
    assert result.parent.name == "some_host_name"

    assert get_spool_path(HostName("short"), ".").name == "item_."
    assert get_spool_path(HostName("short"), "..").name == "item_.."


#!/usr/bin/env python
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.admin import Admin
from models import (
    Users,
    Format,
    Sameas,
    Torrent,
    Datasets,
    Role,
    Provider,
    License,
    db
)
from flask import session
from xmlrpclib import ServerProxy
from makeTorrent import makeTorrent
from os import path
from requests import get, post

admin = Admin()

class Models(ModelView):

    def is_accessible(self):
        if session.get('username', None):
            if session.get('is_admin', 'False') == 'True':
                return True
            else:
                return False
        else:
            return False

    def index(self):
        return self.render('admindex.html')

def is_allowed(roles):
    print('Allowed: %s got %s' % (roles, session['role']))
    if session['role'] not in roles:
        return False
    if session['role'] in roles:
        return True

class DatasetsView(ModelView):

    def is_accessible(self):
        if session.get('username', None):
            if session.get('is_admin', 'False') == 'True':
                return True
            else:
                return False
        else:
            return False

    inline_model = (Format,)
    column_list = ('type', 'name', 'description', 'license', 'path', 'url')
    column_filter = ('type', 'name', 'description', 'license', 'path', 'url')
    form_columns = (
        'type', 'name',
        'description', 'license',
        'path', 'url', 'provider',
        'formats', 'sameas'
    )

    def __init__(self, session):
        super(DatasetsView, self).__init__(Datasets, db.session)

class UserView(ModelView):

    def is_accessible(self):
        if session.get('username', None):
            if session.get('is_admin', 'False') == 'True':
                return True
            else:
                return False
        else:
            return False

    def __init__(self, session):
        super(UserView, self).__init__(Users, db.session)
    form_columns = ('username', 'role')

class TorrentView(ModelView):

    def is_accessible(self):
        if session.get('username', None):
            if session.get('is_admin', 'False') == 'True':
                return True
            else:
                return False
        else:
            return False

    def __init__(self, session):
        super(TorrentView, self).__init__(Torrent, db.session)
    column_list = ('torrent', 'updated_at')
    form_columns = ('torrent', )

    def on_model_delete(self, model):
        appConf = admin.app.config
        infoHash = model.infoHash
        headers = {
            'Accept': 'application/json',
            'Authorization': 'Bearer %s' % appConf.get('DATASETKEY')
        }
        if appConf.get('SUNET', False):
            try:
                r = get(
                    'https://datasets.sunet.se'
                    '/api/dataset/%s/delete' % infoHash,
                    headers=headers,
                    verify=appConf.get('VERIFY_SSL', True)
                )
                r.raise_for_status()
            except Exception as e:
                print("Could not delete torrent %s" % e)
        if appConf.get('RTORRENT', False):
            try:
                rtorrent = ServerProxy(appConf.get('XMLRPC_URI'))
                rtorrent.d.erase(infoHash)
            except Exception as e:
                print("Could not remove torrent: %s" % e)

    def on_model_change(self, form, model, is_created):
        datasetID = form.data['torrent'].datasetID
        if is_created:
            dataset = Datasets.query.filter(
                Datasets.datasetID == datasetID
            ).first()
            appConf = admin.app.config
            mk = makeTorrent(announce=appConf.get('ANNOUNCE'))
            mk.multi_file(path.join(appConf.get('DATASET_ROOT'), dataset.path))
            torrentData = mk.getBencoded()
            headers = {
                'Accept': 'application/json',
                'Authorization': 'Bearer %s' % appConf.get('DATASETKEY')
            }
            tData = {'torrent': torrentData}
            if appConf.get('SUNET', False):
                try:
                    r = post(
                        'https://datasets.sunet.se/api/dataset',
                        headers=headers,
                        files=tData,
                        verify=appConf.get('VERIFY_SSL', True)
                    )
                    r.raise_for_status()
                except Exception as e:
                    print("Could not upload torrent: %s" % e)
            model.infoHash = mk.info_hash()
            model.torrentData = torrentData
            torrentFile = path.join(
                appConf.get('TORRENT_WATCH_DIR'),
                model.infoHash + '.torrent'
            )
            with open(torrentFile, 'w') as tf:
                tf.write(torrentData)
            if appConf.get('RTORRENT'):
                try:
                    rtorrent = ServerProxy(appConf.get('XMLRPC_URI'))
                    rtorrent.load(torrentFile)
                    downloadPath = path.dirname(
                        path.join(
                            appConf.get('DATASET_ROOT'),
                            dataset.path
                        )
                    )
                    rtorrent.d.set_directory(model.infoHash, downloadPath)
                except Exception as e:
                    print("Could not add torrent to rtorrent: %s" % e)

        return

admin.add_view(UserView(db.session))
admin.add_view(Models(Role, db.session))
admin.add_view(Models(Provider, db.session))
admin.add_view(Models(License, db.session))
admin.add_view(Models(Format, db.session))
admin.add_view(DatasetsView(db.session))
admin.add_view(TorrentView(db.session))
admin.add_view(Models(Sameas, db.session))


from django.shortcuts import redirect, render

# Create your views here.

def add_to_cart(request):
    return redirect('products')
from django.shortcuts import render , redirect
from django.contrib import  messages
from products.models import Product
from orders.models import Order
from orders.models import OrderDetails
from django.utils import timezone

# Create your views here.

def add_to_cart(request):
    if 'pro_id' in request.GET and 'qty' in request.GET and 'price' in request.GET and request.user.is_authenticated and not request.user.is_anonymous:
        pro_id= request.GET['pro_id']
        qty= request.GET['qty']
        order= Order.objects.all().filter(user= request.user, is_finished=False)

        if not Product.objects.all().filter(id=pro_id).exists():
            return redirect('products')
        pro = Product.objects.get(id=pro_id)

        if order :
            # messages.success(request, 'You have order befor old order')
            old_order= Order.objects.get(user= request.user, is_finished=False)
            if OrderDetails.objects.all().filter(order= old_order,product= pro).exists():
                orderdetails=OrderDetails.objects.get(order= old_order,product= pro)
                orderdetails.quantity += int(qty)
                orderdetails.save()
            else:
                if pro.PrdDescountPrice > 0:
                    orderdetails= OrderDetails.objects.create(product= pro, order= old_order, price= pro.PrdDescountPrice, quantity= qty )
                else:
                    orderdetails= OrderDetails.objects.create(product= pro, order= old_order, price= pro.PrdPrice, quantity= qty )
            messages.success(request, 'Yor add another item to cart')

        else:
            # messages.success(request, 'success order New order')
            if pro.PrdDescountPrice > 0:
                new_oreder= Order()
                new_oreder.user = request.user
                new_oreder.order_date = timezone.now()
                new_oreder.is_finished = False
                new_oreder.save()
                orderdetails= OrderDetails.objects.create(product= pro, order= new_oreder, price= pro.PrdDescountPrice, quantity= qty)
            else:
                new_oreder= Order()
                new_oreder.user = request.user
                new_oreder.order_date = timezone.now()
                new_oreder.is_finished = False
                new_oreder.save()
                orderdetails= OrderDetails.objects.create(product= pro, order= new_oreder, price= pro.PrdPrice, quantity= qty)
            messages.success(request, 'Yor add a new item to cart')

        return redirect('/products/'+ request.GET['pro_id'])

    else:
        messages.info(request, ' You must be lognin first')
        return redirect('signin')

def cart(request):
    context = None
    if request.user.is_authenticated and not request.user.is_anonymous:
        if Order.objects.all().filter(user= request.user,is_finished=False):
            order= Order.objects.get(user= request.user,is_finished=False)
            orderdetails= OrderDetails.objects.all().filter(order= order)
            total = 0
            for sub in orderdetails:
                total += sub.price * sub.quantity

            context={
                'order':order,
                'orderdetails':orderdetails,
                'total':total,
            }

    return render(request, 'orders/cart.html', context)

def remove_from_cart(request, orderdetails_id):
    if  request.user.is_authenticated and not request.user.is_anonymous and orderdetails_id:
        orderdetails = OrderDetails.objects.get(id=orderdetails_id)
        if orderdetails.order.user.id==request.user.id:
            orderdetails.delete()
    return redirect('cart')

def add_qty(request, orderdetails_id):
    if  request.user.is_authenticated and not request.user.is_anonymous and orderdetails_id:
        orderdetails = OrderDetails.objects.get(id=orderdetails_id)
        orderdetails.quantity +=1
        orderdetails.save()
    return redirect('cart')

def sub_qty(request, orderdetails_id):
    if  request.user.is_authenticated and not request.user.is_anonymous and orderdetails_id:
        orderdetails = OrderDetails.objects.get(id=orderdetails_id)
        if orderdetails.quantity > 1:
            orderdetails.quantity -=1
            orderdetails.save()
    return redirect('cart')

def payment(request, ):
    return render(request, 'orders/payment.html')


from django.shortcuts import render, reverse
from django.http import HttpResponse, HttpResponseRedirect
from .models import MyModel

def myview(request):
    myinstances = MyModel.objects.all()
    context = {
        'myinstances': myinstances
    }
    return render(request, 'myapp/mytemplate.html', context)

def mycreate(request):
    post_title = request.POST['post_title']
    post_body = request.POST['post_body']
    mymodel = MyModel(post_title=post_title, post_body=post_body)
    mymodel.save()
    return HttpResponseRedirect(reverse('myapp:myview'))

# def index(request):
#     return HttpResponse('you are at the index')

def about(request):
    return HttpResponse('you are at the About page')


"""Training reserves serializers."""

# Django
from django.core.validators import RegexValidator
from django.utils import timezone

# Django REST Framework
from rest_framework import serializers

# Models
from users.models import User
from bookings.models import TrainingReserve

# Serializers
from users.serializers import UserModelSerializer

class TrainingReserveModelSerializer(serializers.ModelSerializer):
    """Appointment model serializer."""

    user = UserModelSerializer(read_only=True)

    class Meta:
        """Meta class."""
        model = TrainingReserve
        fields = ('__all__')
        read_only_fields = ['user']

class CreateTrainingReserveSerializer(serializers.Serializer):
    """Create a training reserve."""

    identification_regex = RegexValidator(
        regex=r"\d{6,10}$",
        message='Identification number must be entered in the format: 199999999. Up to 11 digits allowed'
    )
    identification_number = serializers.CharField(validators=[identification_regex])
    date = serializers.DateTimeField()

    def validate(self, data):
        """Verify that the user has an active membership,
        and the reserve. """
        user = User.objects.get(
            identification_number=data['identification_number'],
            is_verified=True
        )
        profile = user.profile
        reserves = TrainingReserve.objects.filter(date=data['date']).count()

        try:
            user_reserve = TrainingReserve.objects.get(user=user)
            if user_reserve:
                raise serializers.ValidationError('You already have a reserve.')
        except TrainingReserve.DoesNotExist:
            if profile.is_active == False:
                raise serializers.ValidationError('The user does not have an active membership.')
            if data['date'] <= timezone.now():
                raise serializers.ValidationError('Time not available')
            # Aforo max 26 por hora debido a la pandemia
            if reserves == 26:
                raise serializers.ValidationError('The capacity at this time is full.')
            else:
                return data

    def create(self, validated_data):
        """Create a training reserve."""
        user = User.objects.get(
            identification_number=validated_data.pop('identification_number')
        )
        reserve = TrainingReserve.objects.create(
            user=user,
            date=validated_data['date']
        )
        return reserve


from datetime import datetime, timedelta
from Modules.FlightConstants import FlightConstants

class FlightFinder():
    # AIRLINE: Get airline(s) to fly from
    def get_airline_codes(airlines, airline_choices):
        if airlines: return airlines
        elif airline_choices: return FlightConstants.airlines[airline_choices]
        return FlightConstants.airlines[FlightConstants.ALL_AIRLINES]

    # AIRPORTS: Get all airports that match the first given parameter
    def get_airports(airports, coordinates, address, region, distance, default):
        if airports:
            desired_airports = [FlightConstants.get_airport_from_code(airport) for airport in airports]
        elif coordinates:
            latitude, longitude = coordinates
            desired_airports = FlightFinder.get_all_airports_within_circle(latitude, longitude, distance)
        elif address:
            from geopy.geocoders import Nominatim
            geo = Nominatim(user_agent="my_request").geocode(address)
            latitude, longitude = geo.latitude, geo.longitude

            desired_airports = FlightFinder.get_all_airports_within_circle(latitude, longitude, distance)
        elif region:
            desired_airports = FlightConstants.regions[region]
        else:
            desired_airports = default

        return desired_airports

    def get_airports_nearme(distance, debug=False):
        import geocoder
        latlng = geocoder.ip('me').latlng
        if latlng == None:
            if debug:
                print("You are not connected to the internet; defaulting the the coordinates for PHX airport")
                latlng = (33.433333, -112.033333)
            else:
                exit("You are not connected to the internet; please reconnect and try again")
        latitude, longitude = latlng

        return FlightFinder.get_all_airports_within_circle(latitude, longitude, distance)

    def get_airports_anywhere(distance):
        return FlightFinder.get_all_airports_within_circle(0, 0, float('inf'))

    # DATES: Get start and end date for desired flights
    def get_start_end_date(startdate, length):
        try:
            start = datetime.strptime(startdate, '%m-%d-%Y')
        except:
            start = datetime.strptime(startdate, '%m/%d/%Y')
        return start, start + timedelta(days=length)

    def get_all_airports_within_circle(latitude, longitude, radius):
        if radius == float('inf'): return FlightConstants.get_all_airports()

        origin = (latitude, longitude)
        from geopy.distance import geodesic

        dist_airports = [(geodesic(origin, a.coordinates).miles, a) for a in FlightConstants.get_all_airports()]
        airports_in_range = filter(lambda a: a[0] <= radius, dist_airports)
        airports_in_range_sorted = sorted(airports_in_range, key=lambda a: a[0])
        return [a for dist, a in airports_in_range_sorted]

    def get_all_airports_with_airlines(airlines):
        all_airports = FlightConstants.get_all_airports()
        airline_airports = set()
        for airline in airlines:
            for a1, a2 in FlightConstants.city_to_city_by_airline[airline]:
                airline_airports.add(a1)
                airline_airports.add(a2)

        return [airport for airport in all_airports if airport.airport_code in airline_airports]

    def flight_exists(airline, origin, destination):
        origin_to_destination = FlightConstants.city_to_city_by_airline[airline]
        for o1, d1 in origin_to_destination:
            if o1 == origin and d1 == destination: return True
        return False

{'Nominatim': 'geopy.geocoders.Nominatim', 'geocoder': 'geocoder'}.get_all_airports_within_circle


#!/usr/bin/env python

from nornir import InitNornir
from nornir.core.filter import F

def main():
    nr = InitNornir()
    nr = nr.filter(F(groups__contains="sea") | F(groups__contains="sfo"))
    print(nr.inventory.hosts)

if __name__ == "__main__":
    main()


import asyncio
import html
import json
import os
import re
from time import perf_counter

import aiohttp
import requests
import sqlalchemy as db
from dotenv import load_dotenv

load_dotenv()

print("RUNING")

url: str = os.getenv('DATABASE_URL')

engine = db.create_engine(url)
meta_data = db.MetaData(engine)
db.MetaData.reflect(meta_data)
vapo = meta_data.tables['Vapo']
tag_plus = meta_data.tables['TagPlus']

get_creds = (
    db.select(tag_plus).where(tag_plus.c.id == 1)
)

access_creds = engine.execute(get_creds).fetchone()

access_token = access_creds[2]
refresh_token = access_creds[1]

def check_tokens():
    global access_token
    global refresh_token

    if requests.get(f"https://api.tagplus.com.br/produtos?access_token={access_token}").status_code in range(400, 499):
        res = requests.post('https://api.tagplus.com.br/oauth2/token', data={
            'grant_type': 'refresh_token',
            'refresh_token': refresh_token,
            'client_secret': os.getenv('CLIENT_SECRET'),
            'client_id': os.getenv('CLIENT_ID')
        })
        if res.status_code in range(200, 299):
            res = res.json()
            refresh_token = res['refresh_token']
            access_token = res['access_token']

            update_tokens = (
                db.update(tag_plus).where(tag_plus.c.id == 1).values(accessToken=access_token,
                                                                     refreshToken=refresh_token)
            )
            engine.execute(update_tokens)
        else:
            print('COULD NOT GET ACCESS TOKEN')
            exit()

check_tokens()

urlc = f'https://api.tagplus.com.br/categorias?access_token={access_token}'
urlp = f'https://api.tagplus.com.br/produtos?access_token={access_token}&categoria='

session = requests.session()
data = session.get(urlc).json()
parsedData = []

for item in data:
    if not item["categoria_mae"]:
        if item['id'] == 68:
            continue
        parsedData.append(
            {
                'id': item['id'],
                'name': item['descricao'],
                'slug': item['descricao'],
                'productCount': len(session.get(f'{urlp}{item["id"]}').json()),
                'children': [],
                "icon": '',
                'image': {
                    'thumbnail': '/assets/images/categories/' + item['descricao'].lower() + '.jpg',
                    'original': '/assets/images/categories/' + item['descricao'].lower() + '.jpg',
                }
            }
        )
        print(f"CATEGORY DONE - {item['id']}")

for item in data:
    if item['categoria_mae']:
        for cate in parsedData:
            if item['categoria_mae']['id'] == 68:
                continue
            if cate['id'] == item['categoria_mae']['id']:
                cate['children'].append(
                    {
                        "id": item['id'],
                        "name": item['descricao'],
                        "slug": item['descricao']
                    }
                )
                print(f"SUBCATEGORY DONE - {item['categoria_mae']['id']}")
                break

updateCategories = (
    db.update(vapo).where(vapo.c.id == 1).values(categories=json.dumps(parsedData))
)
engine.execute(updateCategories)

def format_description(text):
    text = html.unescape(text).replace(u'\xa0', u' ').replace('"', '').replace("N'", '').replace('\n', '').replace('\t',
                                                                                '')
    text = text.split(" ")
    text = ' '.join(text)
    return text

data = []
length = 2000

def check_category(cid):
    for c_item in parsedData:
        if len(c_item['children']) >= 1:
            for child in c_item['children']:
                if child['id'] == cid:
                    return {
                        "id": c_item['id'],
                        "name": c_item['name'],
                        "sub_category_id": child['id'],
                        "sub_category_name": child['name']
                    }
    for c_item in parsedData:
        if c_item['id'] == cid:
            return {
                "id": c_item['id'],
                "name": c_item['name'],
                "sub_category_id": None,
                "sub_category_name": None
            }

def adjust_price(price, sale):
    if sale == 0:
        return price
    return sale

async def fetch(s, url):
    async with s.get(f"https://api.tagplus.com.br/produtos/{url}?access_token={access_token}&fields="
                     f"nome,descricao,descricao_longa,codigo,estoque,categoria,valor_venda_varejo,valor_oferta,"
                     f"data_criacao,imagens,imagem_principal") as res:
        if res.status != 200:
            return
        parsed = await res.json()
        if not parsed:
            return
        if not parsed['estoque']['qtd_revenda'] or parsed['estoque']['qtd_revenda'] <= 0:
            return
        img = ''
        if 'imagem_principal' in parsed and parsed['imagem_principal'] is not None and 'url' in parsed[
            'imagem_principal']:
            img = parsed['imagem_principal']['url']
        gallery = parsed['imagens']
        if img in gallery:
            gallery.remove(img)
        product = {
            "id": parsed['id'],
            "code": parsed['codigo'],
            "name": parsed['descricao'],
            "description": format_description(re.sub("\(.*?\)|\<.*?\>", "", parsed['descricao_longa'])),
            "slug": parsed['descricao'],
            "quantity": parsed['estoque']['qtd_revenda'],
            "category": check_category(parsed['categoria']['id']),
            "price": parsed['valor_venda_varejo'],
            "sale_price": adjust_price(parsed['valor_venda_varejo'], parsed['valor_oferta']),
            "image": img,
            "gallery": gallery,
            "created_on": parsed['data_criacao']
        }
        print(f"PRODUCT DONE - {product['id']}")
        data.append(product)

async def fetch_all(s, urls):
    tasks = []
    for url in urls:
        task = asyncio.create_task(fetch(s, url))
        tasks.append(task)
    res = await asyncio.gather(*tasks)
    return res

async def main():
    urls = range(1, length)
    async with aiohttp.ClientSession() as session:
        await fetch_all(session, urls)
    update_products = (
        db.update(vapo).where(vapo.c.id == 1).values(products=json.dumps(data))
    )
    engine.execute(update_products)

if __name__ == '__main__':
    start = perf_counter()
    asyncio.run(main())
    stop = perf_counter()
    print("time taken:", stop - start)

print('ENDING')


from audioop import bias
from enum import auto
import sys
sys.path.append(".")
from agent.DeepScalper.model import Net
import numpy as np
import torch
from torch import nn
model_a = torch.load('/mnt/d/TradeMaster-1/result/AT/trained_model/best_model/best_model.pth').cpu()
model_a.eval()

torch.set_printoptions(threshold=np.inf)
# Display all model layer weights
# txt=""
# for name, para in model_a.named_parameters():
#     print('{}: {}'.format(name, para)+"\n")
#     print('{}: {}'.format(name, para.shape)+"\n")
#     txt+='{}: {}'.format(name, para)+"\n"
# f = open("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/weights.txt",'a')
# f.write(txt)
# f.close()
paras=[]
names=[]
for name, para in model_a.named_parameters():
    para=para.reshape(-1)
    paras.append(para.detach().numpy().tolist())
    names.append(name)

# paras=torch.cat(paras,dim=1).reshape(-1)
# bias weights 放到一起 weights为偶数 bias为奇数

print(names)
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
# colors = [plt.cm.Spectral(i/float(len(paras)-1)) for i in range(len(paras))]
# n, bins, patches = plt.hist(paras, 10, stacked=True, density=False, color=colors[:len(paras)])
# plt.legend({group:col for group, col in zip(names, colors[:len(paras)])},loc='lower center',ncol=int(len(paras)/2),fontsize=8,bbox_to_anchor=(0.25, 1., 0.5, 0.5))
# plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/single_layer_distribution.pdf")
from matplotlib.ticker import FuncFormatter

linear_weights=paras[0:len(paras):2]
linear_name=names[0:len(paras):2]
bias_weights=paras[1:len(paras):2]
bias_name=names[1:len(paras):2]
colors = [plt.cm.Spectral(i/float(len(linear_weights)-1)) for i in range(len(linear_weights))]
colors=["pink",plt.cm.Spectral(1/float(len(linear_weights)-1)),"#c7fdb5","#cea2fd"]
# n, bins, patches=plt.hist(linear_weights,10, stacked=True, density=False, color=colors[:len(linear_weights)],edgecolor='black',cumulative=False)

n, bins, patches=plt.hist(bias_weights,10, stacked=True, density=True, color=colors[:len(bias_weights)],edgecolor='black',cumulative=False)
print(bins[-1]-bins[0])
def pencetail(weights,bins):
    weights_sum=0
    for single_layer_weights in weights:
        for weight in single_layer_weights:
            weights_sum=weights_sum+1
    percentages=[]
    for i in range(len(bins)-1):
        percentage=0
        for single_layer_weights in weights:
            for weight in single_layer_weights:
                if (weight>=bins[i] and weight <bins[i+1]):
                    percentage=percentage+1
        percentage=percentage/weights_sum
        percentages.append(percentage)
    return percentages

percentages=pencetail(bias_weights,bins)
for i in range(10):
    plt.text(bins[i]+(bins[1]-bins[0])/2,percentages[i]*38+0.01, str(round(percentages[i]*100,2)), ha='center', va= 'bottom')
def to_percent(y,position):
    return str(round(y*(bins[-1]-bins[0])*10,2))+"%"#这里可以用round（）函数设置取几位小数
fomatter=FuncFormatter(to_percent)
plt.gca().yaxis.set_major_formatter(fomatter)
plt.legend({group:col for group, col in zip(bias_name, colors[:len(bias_name)])},loc='lower center',
ncol=int(len(paras)/2),fontsize=8,bbox_to_anchor=(0.25, 1., 0.5, 0.5))
plt.ylabel("Percentage")
plt.xlabel("Value of bias")
plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/single_layer_bias_distribution.pdf")

# weights 图像
# n, bins, patches=plt.hist(linear_weights,10, stacked=True, density=True, color=colors[:len(linear_weights)],edgecolor='black',cumulative=False)
# def pencetail(weights,bins):
#     weights_sum=0
#     for single_layer_weights in weights:
#         for weight in single_layer_weights:
#             weights_sum=weights_sum+1
#     percentages=[]
#     for i in range(len(bins)-1):
#         percentage=0
#         for single_layer_weights in weights:
#             for weight in single_layer_weights:
#                 if (weight>=bins[i] and weight <bins[i+1]):
#                     percentage=percentage+1
#         percentage=percentage/weights_sum
#         percentages.append(percentage)
#     return percentages
# percentages=pencetail(linear_weights,bins)
# for i in range(10):
#     plt.text(bins[i]+(bins[1]-bins[0])/2,percentages[i]*10+0.1, round(percentages[i]*100,2), ha='center', va= 'bottom')
#     # plt.text(bins[i]+(bins[1]-bins[0])/2, n[i,:].sum()*1.01, int(n[i,:].sum()), ha='center', va= 'bottom')
# def to_percent(y,position):
#     return str(10*y)+"%"#这里可以用round（）函数设置取几位小数
# fomatter=FuncFormatter(to_percent)
# plt.gca().yaxis.set_major_formatter(fomatter)
# plt.legend({group:col for group, col in zip(linear_name, colors[:len(linear_name)])},loc='lower center',
# ncol=int(len(paras)/2),fontsize=8,bbox_to_anchor=(0.25, 1., 0.5, 0.5))
# plt.ylabel("Percentage")
# plt.xlabel("Value of weights")

# plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/single_layer_weights_distribution.pdf")

# kde graph
# plt.figure(figsize=(16,10))
# sns.kdeplot(paras, shade=True, color="g", label="parameters", alpha=.7)
# plt.title('Density Plot of parameters in deepscalper', fontsize=22)
# plt.legend()
# plt.show()
# plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/all_layer_distribution.pdf")


from typing import List

class Solution:
    def contain_most_water(self, arr: List[int]):
        max_area = 0
        curr_area = 0
        i = 0
        j = len(arr) - 1
        while i < j:
            curr_area = min(arr[i], arr[j]) * (j - i)
            if curr_area > max_area:
                max_area = curr_area
            if arr[i] < arr[j]:
                i = i + 1
            else:
                j = j - 1
        print(max_area)

s = Solution()
s.contain_most_water([1,8,6,2,5,4,8,3,7])

# https://leetcode.com/problems/container-with-most-water/

Python Leetcode Algorithms/1. Array and String/5. contains_most_water.py


'''fonty.lib.telemetry'''
import sys
import json
import platform
import threading
from enum import Enum
from datetime import datetime
from typing import Tuple

import requests
from fonty.version import __version__
from fonty.lib.json_encoder import FontyJSONEncoder
from fonty.lib.constants import TELEMETRY_ENDPOINT, JSON_DUMP_OPTS
from fonty.lib.config import CommonConfiguration

class TelemetryEventTypes(Enum):
    '''An enum of possible telemetry event types.'''
    FONTY_SETUP = 'FONTY_SETUP'
    FONT_INSTALL = 'FONT_INSTALL'
    FONT_UNINSTALL = 'FONT_UNINSTALL'
    FONT_LIST = 'FONT_LIST'
    FONT_LIST_REBUILD = 'FONT_LIST_REBUILD'
    FONT_CONVERT = 'FONT_CONVERT'
    SOURCE_LIST = 'SOURCE_LIST'
    SOURCE_ADD = 'SOURCE_ADD'
    SOURCE_REMOVE = 'SOURCE_REMOVE'
    SOURCE_UPDATE = 'SOURCE_UPDATE'

class TelemetryEvent:
    '''The TelemetryEvent class describes a fonty telemetry event.

    By default, fonty sends some light usage data to better understand (1) how
    users use fonty and (2) also to identify interesting font usage statistics.
    It can however be disabled by turning off the `telemetry` setting in the
    fonty configuration file.
    '''

    #: The event type of this telemetry event.
    event_type: TelemetryEventTypes

    #: The timestamp of this telemetry event.
    timestamp: datetime

    #: The fonty version currently being used.
    fonty_version: str

    #: The operating system family of the current environment.
    os_family: str

    #: The operating system version of the current environment.
    os_version: str

    #: The current Python version.
    python_version: str

    #: The time it took (in seconds) to execute this event.
    execution_time: float

    #: The status code of the current command. 0 means success, >1 means error.
    status_code: int

    #: The additional data that is relevant to this telemetry event.
    data: dict

    def __init__(
        self,
        status_code: int,
        event_type: TelemetryEventTypes,
        execution_time: float = None,
        data: dict = None
    ) -> None:
        self.event_type = event_type
        self.timestamp = datetime.now()
        self.fonty_version = __version__
        self.python_version = '{major}.{minor}.{micro}'.format(
            major=sys.version_info.major,
            minor=sys.version_info.minor,
            micro=sys.version_info.micro
        )
        self.os_family, self.os_version = TelemetryEvent._get_os_info()
        self.execution_time = execution_time
        self.status_code = status_code
        self.data = data

    def send(self, force=False, asynchronous=True) -> None:
        '''Sends the telemetry data to the central logging server.'''
        if not CommonConfiguration.telemetry and not force:
            return

        # Create payload
        d = {
            'timestamp': self.timestamp,
            'status_code': self.status_code,
            'event_type': self.event_type.value,
            'execution_time': self.execution_time,
            'fonty_version': self.fonty_version,
            'os_family': self.os_family,
            'os_version': self.os_version,
            'python_version': self.python_version,
            'data': self.data
        }

        # Send request
        if asynchronous:
            threading.Thread(target=self._send_request, args=(d,)).start()
        else:
            self._send_request(d)

    def _send_request(self, d):
        try:
            requests.post(
                url=TELEMETRY_ENDPOINT,
                data=json.dumps(d, cls=FontyJSONEncoder, **JSON_DUMP_OPTS),
                headers={'Content-Type': 'application/json'}
            )
        except: # pylint: disable=W0702
            pass

    @staticmethod
    def _get_os_info() -> Tuple[str, str]:
        '''Gets the current operating system information and returns it as a
           tuple of (family, version).
        '''
        family = platform.system()
        version = ''

        if family.lower() == 'darwin':
            version = platform.mac_ver()[0]
        elif family.lower() == 'windows':
            win = sys.getwindowsversion() # pylint: disable=E1101
            version = '{}.{}-{}-{}'.format(win.major, win.minor, win.build, win.platform)
        elif family.lower() == 'linux':
            import distro
            dist = distro.linux_distribution(full_distribution_name=False)
            version = '{}-{}'.format(dist[0], dist[1])
        else:
            version = platform.platform()

        return family, version


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

# In[1]:

import pandas as pd
import numpy as np
import librosa
from pathlib import Path
import soundfile as sf
from tqdm import tqdm
from pqdm.processes import pqdm

train = pd.read_csv('../input/train_metadata.csv')
train

def ogg2np(ebird, filename):
    input_path = Path('../input/')
    output_path = Path('../data/')
    input_path = input_path / 'train_short_audio' / ebird / filename
    filename = '.'.join(filename.split('.')[:-1])
    output_path = output_path
    record, sr = sf.read(input_path)
    record = librosa.to_mono(record)
    record = record.astype('float32')
    length = record.shape[0]
    period = int(np.ceil(5 * sr))
    if period == 0:
        filename = '%s_%d.npy' % (filename, 0)
        np.save(output_path / filename, record)
    else:
        for i in range(int(np.ceil(length/period))):
            filename_i = '%s_%d.npy' % (filename, i)
            record_i = record[i*period : (i+3)*period]
            np.save(output_path / filename_i, record_i)
    return length, sr

res = pqdm(zip(train.primary_label, train.filename), ogg2np, n_jobs=8, argument_type='args')

train['sr'] = [r[1] for r in res]
train['length'] = [r[0] for r in res]
train['duration'] = train['length'] / train['sr']

train['filename'] = [fname[:-4]+'.npy' for fname in train.filename]

train.to_csv('../input/train_001.csv', index=False)

train_ff1010 = pd.read_csv('../input/freefield1010/ff1010bird_metadata.csv')
train_ff1010

train_ff1010 = train_ff1010.sort_values(by='itemid').reset_index(drop=True)
train_ff1010

def get_clip(itemid):
    data_path = Path('../input/freefield1010/wav')
    path = data_path / ('%d.wav' % itemid)
    clip, sr_native = librosa.load(path, sr=None, mono=True, dtype=np.float32)
    sr = 32000
    if sr_native != 0:
        clip = librosa.resample(clip, sr_native, sr, res_type='kaiser_best')
    else:
        print('null sr_native')
    return clip, sr, sr_native

train_ff1010 = train_ff1010[train_ff1010.hasbird == 0].reset_index(drop=True)
train_ff1010

def work_sub(itemid):
    output_path = Path('../data/')
    clip, sr, sr_native = get_clip(itemid)
    clip = clip.astype('float32')
    length = clip.shape[0]
    filename = 'ff1010_%d_0.npy' % (itemid)
    np.save(output_path / filename, clip)

    return sr, sr_native, length

res = pqdm(train_ff1010.itemid, work_sub, n_jobs=8)

train_ff1010['primary_label'] = ''
train_ff1010['secondary_labels'] = None
train_ff1010['sr'] = [r[0] for r in res]
train_ff1010['sr_native'] = [r[1] for r in res]
train_ff1010['length'] = [r[2] for r in res]
train_ff1010['duration'] = train_ff1010['length'] / 32000
train_ff1010['filename'] = ['ff1010_%d_0.npy' % (itemid) for itemid in train_ff1010['itemid']]
train_ff1010

train_ff1010['secondary_labels'] = [[]] * len(train_ff1010)

columns = ['duration', 'length', 'primary_label', 'secondary_labels', 'filename']
train_ff1010[columns].to_csv('../input/train_ff1010.csv', index=False)


import os

import numpy as np
import pandas as pd

print(os.getcwd())
bc_data = pd.read_csv('bc_data.csv', header=0)  # 数据集中没有列名信息，所以header设置为0
bc_data.head()
print(bc_data.shape)  # 查看形状
print(bc_data.columns)  # 查看列名
print(bc_data.describe())  # 查看描述性统计信息
data = bc_data.drop(['id'], axis=1)
print(data.head())
x_data = data.drop(['diagnosis'], axis=1)
x_data.head()
y_data = np.ravel(data[['diagnosis']])  # np.ravel()
y_data[0:6]
from sklearn.model_selection import train_test_split

x_trainingSet, x_testSet = train_test_split(x_data)
y_trainingSet, y_testSet = train_test_split(y_data)
print(x_trainingSet.shape)
print(x_testSet.shape)
from sklearn.neighbors import KNeighborsClassifier

myModel = KNeighborsClassifier(algorithm='kd_tree')
myModel.fit(x_trainingSet, y_trainingSet)
y_predictSet = myModel.predict(x_testSet)
print(y_predictSet)
print(y_testSet)
from sklearn.metrics import accuracy_score

print(accuracy_score(y_testSet, y_predictSet))


from cdpcli.compat import json
import dateutil.parser

class ResponseParserFactory(object):

    def create_parser(self):
        return ResponseParser()

class ResponseParser(object):
    DEFAULT_ENCODING = 'utf-8'

    def parse(self, response, shape):
        if response['status_code'] >= 301:
            return self._parse_error(response)
        elif 'Content-Type' in response['headers'] and \
             not self._is_json_response(response['headers']['Content-Type']):
            return response['body']
        elif shape is None:
            return {}
        else:
            return self._parse_shape(shape, self._decode_body(response))

    def _is_json_response(self, content_type):
        if not content_type:
            return False
        if content_type == 'application/json':
            return True
        if content_type.startswith('application/json;'):
            return True
        return False

    def _decode_body(self, response):
        body = response['body']
        if not body:
            return {}
        return json.loads(body.decode(self.DEFAULT_ENCODING))

    def _parse_error(self, response):
        try:
            body = self._decode_body(response)
        except Exception:
            # In the case that we hit a completely dead endpoint, etc, the
            # response might not be valid JSON, but we still want to return
            # a structured error to the caller.
            message = response['body'].decode(self.DEFAULT_ENCODING).strip()
            body = dict(code='UNKNOWN_ERROR', message=message)
        error = {}
        error['code'] = body.get('code', '')
        if 'message' in body:
            error['message'] = body.get('message', '')
        elif 'error' in body:
            error['message'] = body.get('error', '')
        else:
            error['message'] = ' '.join(body.get('errorMessages', []))
        return {'error': error}

    def _parse_shape(self, shape, value):
        handle_method_name = '_handle_%s' % shape.type_name
        method = getattr(self, handle_method_name, self._handle_default)
        return method(shape, value)

    def _handle_array(self, shape, value):
        parsed = []
        for item in value:
            parsed.append(self._parse_shape(shape.member, item))
        return parsed

    def _handle_object(self, shape, value):
        parsed = {}
        for member_name in shape.members:
            member_shape = shape.members[member_name]
            member_value = value.get(member_shape.name)
            if member_value is not None:
                parsed[member_name] = self._parse_shape(
                    shape.members[member_name], member_value)
        return parsed

    def _handle_map(self, shape, values):
        parsed = {}
        for key, value in values.items():
            actual_key = self._parse_shape(shape.key, key)
            actual_value = self._parse_shape(shape.value, value)
            parsed[actual_key] = actual_value
        return parsed

    # Handles both ISO8601 format and RFC822 format.
    # However, server can only parse ISO8601 format as of now.
    def _handle_datetime(self, shape, value):
        try:
            return dateutil.parser.parse(value)
        except (TypeError, ValueError) as e:
            raise ValueError('Invalid timestamp "%s": %s' % (value, e))

    def _handle_default(self, shape, value):
        return value


import gym
import sys
import torch
import numpy as np
from PIL import Image

class DeepMindControl:

    def __init__(self, name, seed, size=(64, 64), camera=None):

        domain, task = name.split('-', 1)
        if domain == 'cup':  # Only domain with multiple words.
          domain = 'ball_in_cup'
        if isinstance(domain, str):
          from dm_control import suite
          self._env = suite.load(domain, task, task_kwargs={'random':seed})
        else:
          assert task is None
          self._env = domain()
        self._size = size
        if camera is None:
          camera = dict(quadruped=2).get(domain, 0)
        self._camera = camera

    @property
    def observation_space(self):
        spaces = {}
        for key, value in self._env.observation_spec().items():
          spaces[key] = gym.spaces.Box(
              -np.inf, np.inf, value.shape, dtype=np.float32)
        spaces['image'] = gym.spaces.Box(
            0, 255, (3,) + self._size , dtype=np.uint8)
        return gym.spaces.Dict(spaces)

    @property
    def action_space(self):
        spec = self._env.action_spec()
        return gym.spaces.Box(spec.minimum, spec.maximum, dtype=np.float32)

    def step(self, action):
        time_step = self._env.step(action)
        obs = dict(time_step.observation)
        obs['image'] = self.render().transpose(2, 0, 1).copy()
        reward = time_step.reward or 0
        done = time_step.last()
        info = {'discount': np.array(time_step.discount, np.float32)}
        return obs, reward, done, info

    def reset(self):
        time_step = self._env.reset()
        obs = dict(time_step.observation)
        obs['image'] = self.render().transpose(2, 0, 1).copy()
        return obs

    def render(self, *args, **kwargs):
        if kwargs.get('mode', 'rgb_array') != 'rgb_array':
          raise ValueError("Only render mode 'rgb_array' is supported.")
        return self._env.physics.render(*self._size, camera_id=self._camera)

class TimeLimit:

    def __init__(self, env, duration):
        self._env = env
        self._duration = duration
        self._step = None

    def __getattr__(self, name):
        return getattr(self._env, name)

    def step(self, action):
        assert self._step is not None, 'Must reset environment.'
        obs, reward, done, info = self._env.step(action)
        self._step += 1
        if self._step >= self._duration:
          done = True
          if 'discount' not in info:
            info['discount'] = np.array(1.0).astype(np.float32)
          self._step = None
        return obs, reward, done, info

    def reset(self):
        self._step = 0
        return self._env.reset()

class ActionRepeat:

    def __init__(self, env, amount):
        self._env = env
        self._amount = amount

    def __getattr__(self, name):
        return getattr(self._env, name)

    def step(self, action):
        done = False
        total_reward = 0
        current_step = 0
        while current_step < self._amount and not done:
          obs, reward, done, info = self._env.step(action)
          total_reward += reward
          current_step += 1
        return obs, total_reward, done, info

class NormalizeActions:

    def __init__(self, env):
        self._env = env
        self._mask = np.logical_and(
            np.isfinite(env.action_space.low),
            np.isfinite(env.action_space.high))
        self._low = np.where(self._mask, env.action_space.low, -1)
        self._high = np.where(self._mask, env.action_space.high, 1)

    def __getattr__(self, name):
        return getattr(self._env, name)

    @property
    def action_space(self):
        low = np.where(self._mask, -np.ones_like(self._low), self._low)
        high = np.where(self._mask, np.ones_like(self._low), self._high)
        return gym.spaces.Box(low, high, dtype=np.float32)

    def step(self, action):
        original = (action + 1) / 2 * (self._high - self._low) + self._low
        original = np.where(self._mask, original, action)
        return self._env.step(original)

class ObsDict:

    def __init__(self, env, key='obs'):
        self._env = env
        self._key = key

    def __getattr__(self, name):
        return getattr(self._env, name)

    @property
    def observation_space(self):
        spaces = {self._key: self._env.observation_space}
        return gym.spaces.Dict(spaces)

    @property
    def action_space(self):
        return self._env.action_space

    def step(self, action):
        obs, reward, done, info = self._env.step(action)
        obs = {self._key: np.array(obs)}
        return obs, reward, done, info

    def reset(self):
        obs = self._env.reset()
        obs = {self._key: np.array(obs)}
        return obs

class OneHotAction:

    def __init__(self, env):
        assert isinstance(env.action_space, gym.spaces.Discrete)
        self._env = env

    def __getattr__(self, name):
        return getattr(self._env, name)

    @property
    def action_space(self):
        shape = (self._env.action_space.n,)
        space = gym.spaces.Box(low=0, high=1, shape=shape, dtype=np.float32)
        space.sample = self._sample_action
        return space

    def step(self, action):
        index = np.argmax(action).astype(int)
        reference = np.zeros_like(action)
        reference[index] = 1
        if not np.allclose(reference, action):
          raise ValueError(f'Invalid one-hot action:\n{action}')
        return self._env.step(index)

    def reset(self):
        return self._env.reset()

    def _sample_action(self):
        actions = self._env.action_space.n
        index = self._random.randint(0, actions)
        reference = np.zeros(actions, dtype=np.float32)
        reference[index] = 1.0
        return reference

class RewardObs:

    def __init__(self, env):
        self._env = env

    def __getattr__(self, name):
        return getattr(self._env, name)

    @property
    def observation_space(self):
        spaces = self._env.observation_space.spaces
        assert 'reward' not in spaces
        spaces['reward'] = gym.spaces.Box(-np.inf, np.inf, dtype=np.float32)
        return gym.spaces.Dict(spaces)

    def step(self, action):
        obs, reward, done, info = self._env.step(action)
        obs['reward'] = reward
        return obs, reward, done, info

    def reset(self):
        obs = self._env.reset()
        obs['reward'] = 0.0
        return obs

class ResizeImage:

    def __init__(self, env, size=(64, 64)):
        self._env = env
        self._size = size
        self._keys = [
            k for k, v in env.obs_space.items()
            if len(v.shape) > 1 and v.shape[:2] != size]
        print(f'Resizing keys {",".join(self._keys)} to {self._size}.')
        if self._keys:
          from PIL import Image
          self._Image = Image

    def __getattr__(self, name):
        if name.startswith('__'):
          raise AttributeError(name)
        try:
          return getattr(self._env, name)
        except AttributeError:
          raise ValueError(name)

    @property
    def obs_space(self):
        spaces = self._env.obs_space
        for key in self._keys:
          shape = self._size + spaces[key].shape[2:]
          spaces[key] = gym.spaces.Box(0, 255, shape, np.uint8)
        return spaces

    def step(self, action):
        obs = self._env.step(action)
        for key in self._keys:
          obs[key] = self._resize(obs[key])
        return obs

    def reset(self):
        obs = self._env.reset()
        for key in self._keys:
          obs[key] = self._resize(obs[key])
        return obs

    def _resize(self, image):
        image = self._Image.fromarray(image)
        image = image.resize(self._size, self._Image.NEAREST)
        image = np.array(image)
        return image

class RenderImage:

    def __init__(self, env, key='image'):
        self._env = env
        self._key = key
        self._shape = self._env.render().shape

    def __getattr__(self, name):
        if name.startswith('__'):
          raise AttributeError(name)
        try:
          return getattr(self._env, name)
        except AttributeError:
          raise ValueError(name)

    @property
    def obs_space(self):
        spaces = self._env.obs_space
        spaces[self._key] = gym.spaces.Box(0, 255, self._shape, np.uint8)
        return spaces

    def step(self, action):
        obs = self._env.step(action)
        obs[self._key] = self._env.render('rgb_array')
        return obs

    def reset(self):
        obs = self._env.reset()
        obs[self._key] = self._env.render('rgb_array')
        return obs


# Load packages
import numpy as np
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
from sklearn.ensemble import IsolationForest
from sklearn.model_selection import train_test_split

# Load data from the csv file
df = pd.read_csv("housing.csv", index_col=False)
df.head()

CONTAMINATION=.2    # The expected outliers to real data ratio
BOOTSTRAP = False   # True if you want to use the bootstrap method;

# Exctract the data without column names from the dataframe
data = df.values

# We will start by splitting the data into X/y train/test data
X, y = data[:, :-1], data[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

# Next, we will set up the Isolation Forest classifier that will detect the outliers
i_forest = IsolationForest(contamination=CONTAMINATION, bootstrap=BOOTSTRAP)
is_inlier = i_forest.fit_predict(X_train)    # +1 if inlier, -1 if outlier

# Finally, we will select the rows without outliers
mask = is_inlier != -1
# and remove these from the train data
X_train, y_train = X_train[mask, :], y_train[mask]

df_wo_outliers=pd.DataFrame(X_train, columns=df.columns[:-1])
df_boxplot = pd.DataFrame(data={'With outliers':df['RM'], 'Without outliers':df_wo_outliers['RM']})
fig = px.box(df_boxplot)
fig.show()


import math
import re
from functools import reduce
from operator import mul

import utils

def parse_img(img_part: str):
    lines = img_part.splitlines()
    return (utils.ints(lines[0])[0], lines[1:])

all_pics = utils.get_list_data_from_file('day20.txt', parser=parse_img, sep='\n\n')

MONSTER= [
    '                  # ',
    '#    ##    ##    ###',
    ' #  #  #  #  #  #   '
]

monster_pattern1 = r'(?=(#....##....##....###))'
monster_pattern2 = r'(?=(.#..#..#..#..#..#))'

class Tile:
    def __init__(self, id, img, neighbors) -> None:
        self.id = id
        self.img = img
        self.neighbors = neighbors
    @property
    def left_border(self):
        return ''.join(item[0] for item in self.img)
    @property
    def right_border(self):
        return ''.join(item[-1] for item in self.img)
    @property
    def up_border(self):
        return self.img[0]
    @property
    def down_border(self):
        return self.img[-1]

    @property
    def borders(self):
        return [self.up_border, self.down_border, self.left_border, self.right_border]

    def is_neighbor(self, tile) -> bool:
        if tile.id == self.id:
            return False
        for border in self.borders:
            for border2 in tile.borders:
                if border == border2 or border == border2[::-1]:
                    return True
        return False

    def is_right_neighbor(self, tile) -> bool:
        if tile.id == self.id:
            return False
        if self.right_border == tile.left_border:
            return True

    def update_neighbors(self, all_pics):
        for pic in all_pics:
            tile = Tile(pic[0], pic[1], [])
            if self.is_neighbor(tile):
                self.neighbors.append(tile.id)
    @property
    def neighbors_count(self):
        return len(self.neighbors)

    def _rotate(self):
        tmp_list = []
        row_cnt = len(self.img)
        col_cnt = len(self.img[0])
        for i in range(col_cnt):
            temp_col = []
            for j in range(row_cnt):
                temp_col.append(self.img[j][i])
            tmp_list.append(''.join(temp_col[::-1]))
        return Tile(self.id, tmp_list, self.neighbors)

    def _flip(self):
        return Tile(self.id, self.img[::-1], self.neighbors)

    def all_form(self):
        tiles = []
        tile = self
        for i in range(4):
            tiles.append(tile)
            tile = tile._rotate()
        tile = self._flip()
        for i in range(4):
            tiles.append(tile)
            tile = tile._rotate()
        return tiles

    def show(self):
        print(self.id)
        for row in self.img:
            print(row)
        print(f'neighbors id: {self.neighbors}')

tiles_dict = {}
for pic in all_pics:
    tile = Tile(pic[0], pic[1], [])
    tile.update_neighbors(all_pics)
    tiles_dict[tile.id] = tile

def part1(all_tiles):
    angles_ids = [ tile.id for tile in all_tiles if tile.neighbors_count == 2]
    return reduce(mul, angles_ids, 1)

def get_first_row(tile, tiles_dict, n):
    row = [tile]
    for i in range(n):
        find = False
        tile = row[-1]
        for neighbor_id in tile.neighbors:
            n_tile = tiles_dict[neighbor_id]
            for ntf in n_tile.all_form():
                if tile.right_border == ntf.left_border:
                    row.append(ntf)
                    find = True
                    break
            if find:
                break
    return row

def get_next_row(row, tiles_dict):
    next_row = []
    for tile in row:
        find = False
        for neighbor_id in tile.neighbors:
            n_tile = tiles_dict[neighbor_id]
            for ntf in n_tile.all_form():
                if tile.down_border == ntf.up_border:
                    next_row.append(ntf)
                    find = True
                    break
            if find:
                break
    return next_row

def build_final_piles(first_pile, tiles_dict, n):
    for tile in first_pile.all_form():
        final_piles = []
        row = get_first_row(tile, tiles_dict, n-1)
        if len(row) < n:
            continue
        final_piles.append(row)
        for i in range(n-1):
            next_row = get_next_row(row, tiles_dict)
            if len(next_row) < n:
                break
            final_piles.append(next_row)
            row = next_row
        if len(final_piles) == n:
            return final_piles

def remove_borders(piles):
    whole_pic = []
    for row in piles:
        new_row = []
        for pile in row:
            pile_img = []
            for sub_img in pile.img[1:-1]:
                pile_img.append(sub_img[1:-1])
            new_row.append(pile_img)
        for item in zip(*new_row):
            whole_pic.append(''.join(item))
    return whole_pic

def get_whole_pic(tiles_dict, n):
    angles = [tile for tile  in tiles_dict.values() if tile.neighbors_count == 2]
    first_pile = angles[0]
    final_piles = build_final_piles(first_pile, tiles_dict, n)
    return remove_borders(final_piles)

def get_monster_cnt(image):
    cnt = 0
    for i in range(1, len(image)-1):
        second_finds = set(m.start(0) for m in re.finditer(monster_pattern1, image[i]))
        if len(second_finds) > 0:
            third_finds = set(m.start(0) for m in re.finditer(monster_pattern2, image[i+1]))
            both_math_set = second_finds & third_finds
            if len(both_math_set) > 0:
                for j in both_math_set:
                    if image[i-1][j+18] == '#':
                        cnt += 1
    return cnt

def part2(tiles_dict):
    n = int(math.sqrt(len(tiles_dict)))
    whole_pic = get_whole_pic(tiles_dict, n)
    whole_pic_tile = Tile(0, whole_pic, [])
    all_images = [tile.img for tile in whole_pic_tile.all_form()]
    sea_monsters_img = None
    sea_monsters_cnt = 0
    for image in all_images:
        sea_monsters_cnt = get_monster_cnt(image)
        if sea_monsters_cnt > 0:
            sea_monsters_img = image
            break
    monster_hash_cnt = sum(row.count('#') for row in MONSTER)
    image_hash_cnt = sum(row.count('#') for row in sea_monsters_img)
    return image_hash_cnt - monster_hash_cnt*sea_monsters_cnt

print(part1(tiles_dict.values()))
print(part2(tiles_dict))