import xml.etree.ElementTree as ET
import hashlib

filetypes = (".lua",".txt")

manifest = ET.parse("manifest.xml")
root = manifest.getroot()

for file in root.iter("File"):
	path = file.get('name')
	if not path.endswith(filetypes):
 		continue
	try:
		old_hash = file.get('sha1')
		hash = hashlib.sha1(open(path, 'rb').read()).hexdigest()
		file.set("sha1", hash)
		if old_hash != hash:
			print("path {} hash changed: now {}".format(path,hash))
	except FileNotFoundError:
		print("file not found, skipping: {}".format(path))

############VERY HACKY#############
#ElementTree re-orders attributes by default sorted() order on Python <=3.7
#This... er... changes that behaviour
import builtins
_sorted = builtins.sorted
builtins.sorted = lambda x,**args:_sorted(x,**args,reverse=True)

manifest.write("manifest.xml")


# uncompyle6 version 3.7.4
# Python bytecode 2.5 (62131)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/nowandnext/applications/schedule_to_rss.py
# Compiled at: 2010-04-06 19:32:03
"""
Generate an RSS feed of the schedule.
"""
import PyRSS2Gen, datetime, sys, logging
from nowandnext.utils.cmdline import cmdline
from nowandnext.utils.detectos import osinfo
from nowandnext.timezones.utc import utc
from nowandnext.calendar import periods
from nowandnext.calendar.scheduleevent import scheduleevent
from nowandnext.calendar.calQuery import CalQuery, NoCalendarEntry
from nowandnext.utils.textparser import textparser
from nowandnext.timezones.uk import uk as timezone_uk
log = logging.getLogger(__name__)

class schedule_to_rss(cmdline):

    def __init__(self, configfilepath):
        self._config = self.getconfiguration(configfilepath)
        self._calargs = (self._config.get('pinger', 'account'),
         self._config.get('pinger', 'password'),
         self._config.get('pinger', 'calendar_name'))
        self.default_website = self._config.get('feed', 'link')

    def calendaritemtorssitem(self, calendaritem):
        scheduleitem = scheduleevent(calendaritem, default_website=self._config.get('feed', 'link'))
        query = textparser.translate(textparser.parsetodict(calendaritem.getEvent().getDescription()))
        description = ('\n').join(query['unmatched'])
        startdate = scheduleitem.getStartTime(tz=timezone_uk)
        startdate.replace(tzinfo=utc)
        item = PyRSS2Gen.RSSItem(title=scheduleitem.getTitle(), link=query.get('web', self.default_website), description=description, guid=scheduleitem.getGuid(), pubDate=startdate)
        return item

    def __call__(self):
        calendaritems = self.getcalendaritems()
        builddate = datetime.datetime.now(tz=timezone_uk).astimezone(utc)
        rss = PyRSS2Gen.RSS2(title=self._config.get('feed', 'title'), link=self._config.get('feed', 'link'), description=self._config.get('feed', 'description'), lastBuildDate=builddate, items=[ self.calendaritemtorssitem(a) for a in calendaritems ])
        rss.write_xml(sys.stdout, encoding='utf-8')

    def getcalendaritems(self):
        now = datetime.datetime.now(timezone_uk)
        sometimeinthefuture = now + periods.onehour * 24
        cal = CalQuery(*self._calargs)
        eventinstances = []
        try:
            eventinstances.append(cal.getCurrentEventInstance(now))
        except NoCalendarEntry:
            log.warn('There is no calendar entry for now.')

        eventinstances.extend([ a for a in cal.getEventInstances(now, sometimeinthefuture) if a not in eventinstances ])
        return eventinstances

def main():
    logging.basicConfig()
    (options, args) = schedule_to_rss.mkParser().parse_args()
    if options.verbose:
        logging.getLogger('').setLevel(logging.INFO)
    else:
        logging.getLogger('').setLevel(logging.WARN)
    os_spesific_handler = osinfo.get_logger('Pinger')
    os_spesific_handler.setLevel(logging.WARN)
    logging.getLogger('').addHandler(os_spesific_handler)
    s2r = schedule_to_rss(options.configfilepath)
    s2r()

if __name__ == '__main__':
    main()


from django.urls import path

from .views.adminsek import PilihWilayahView
from .views.base import (DaftarAduanView, TambahAduanView, DetailAduanView, CetakDetailAduanView, PilihAduanView,
                         DilimpahkanView, DiteruskanView, DipindahkanView)
from .views.korwil import CatatanView, KorwilUploadView, IdleView
from .views.utils import DaftarSpamView, SpamView, get_auditee, unread_message
from .views.verifikator import PesanView, DitolakView, ProsesView

app_name = 'pengaduanApp'

urlpatterns = [
    path('ajax/get_auditee/', get_auditee, name='getAuditeeUrl'),

    path('<int:pk>/aduan/', PilihAduanView.as_view(), name='pilihAduanUrl'),
    path('<int:pk>/catatan/', CatatanView.as_view(), name='catatanUrl'),
    path('<int:pk>/cetak/', CetakDetailAduanView.as_view(), name='cetakDetailUrl'),
    path('<int:pk>/dilimpahkan/', DilimpahkanView.as_view(), name='dilimpahkanUrl'),
    path('<int:pk>/diteruskan/', DiteruskanView.as_view(), name='diteruskanUrl'),
    path('<int:pk>/dipindahkan/', DipindahkanView.as_view(), name='dipindahkanUrl'),
    path('<int:pk>/diproses/', ProsesView.as_view(), name='diprosesUrl'),
    path('<int:pk>/ditolak/', DitolakView.as_view(), name='ditolakUrl'),
    path('<int:pk>/pesan/', PesanView.as_view(), name='pesanUrl'),
    path('<int:pk>/pilih_wilayah/', PilihWilayahView.as_view(), name='pilihWilayahUrl'),
    path('<int:pk>/spam/', SpamView.as_view(), name='spamUrl'),
    path('<int:pk>/idle/', IdleView.as_view(), name='idleUrl'),
    path('<int:pk>/upload/', KorwilUploadView.as_view(), name='korwilUploadUrl'),
    path('<int:pk>/', DetailAduanView.as_view(), name='detailAduanUrl'),
    path('spam/', DaftarSpamView.as_view(), name='daftarSpamUrl'),
    path('tambah/', TambahAduanView.as_view(), name='tambahAduanUrl'),
    path('unread/', unread_message, name='unreadPesanUrl'),
    path('', DaftarAduanView.as_view(), name='daftarAduanUrl'),
]


# MIT License
#
# Copyright (c) 2020 Mehran Maghoumi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# ----------------------------------------------------------------------------------------------------------------------
import torch
import torch.jit as jit
import torch.nn as nn
from torch.nn import Parameter
from typing import List, Tuple
from torch import Tensor
import math

# ----------------------------------------------------------------------------------------------------------------------
class LayerNormGRUCell(jit.ScriptModule):
    def __init__(self, input_size, hidden_size, bias=True):
        super(LayerNormGRUCell, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size

        self.ln_i2h = torch.nn.LayerNorm(2*hidden_size, elementwise_affine=False)
        self.ln_h2h = torch.nn.LayerNorm(2*hidden_size, elementwise_affine=False)
        self.ln_cell_1 = torch.nn.LayerNorm(hidden_size, elementwise_affine=False)
        self.ln_cell_2 = torch.nn.LayerNorm(hidden_size, elementwise_affine=False)

        self.i2h = torch.nn.Linear(input_size, 2 * hidden_size, bias=bias)
        self.h2h = torch.nn.Linear(hidden_size, 2 * hidden_size, bias=bias)
        self.h_hat_W = torch.nn.Linear(input_size, hidden_size, bias=bias)
        self.h_hat_U = torch.nn.Linear(hidden_size, hidden_size, bias=bias)
        self.reset_parameters()

    def reset_parameters(self):
        std = 1.0 / math.sqrt(self.hidden_size)
        for w in self.parameters():
            w.data.uniform_(-std, std)

    @jit.script_method
    def forward(self, x, h):
        # type: (Tensor, Tensor) -> Tensor
        h = h
        h = h.view(h.size(0), -1)
        x = x.view(x.size(0), -1)

        # Linear mappings
        i2h = self.i2h(x)
        h2h = self.h2h(h)

        # Layer norm
        i2h = self.ln_i2h(i2h)
        h2h = self.ln_h2h(h2h)

        preact = i2h + h2h

        # activations
        gates = preact[:, :].sigmoid()
        z_t, r_t = gates.chunk(2, 1)
        # z_t = gates[:, :self.hidden_size]
        # r_t = gates[:, -self.hidden_size:]

        # h_hat
        h_hat_first_half = self.h_hat_W(x)
        h_hat_last_half = self.h_hat_U(h)

        # layer norm
        h_hat_first_half = self.ln_cell_1( h_hat_first_half )
        h_hat_last_half = self.ln_cell_2( h_hat_last_half )

        h_hat = torch.tanh(h_hat_first_half + torch.mul(r_t, h_hat_last_half))

        h_t = torch.mul(1-z_t, h) + torch.mul(z_t, h_hat)

        # Reshape for compatibility

        h_t = h_t.view(h_t.size(0), -1)
        return h_t

# ----------------------------------------------------------------------------------------------------------------------
class JitGRULayer(jit.ScriptModule):
    def __init__(self, cell, *cell_args):
        super(JitGRULayer, self).__init__()
        self.cell = cell(*cell_args)

    @jit.script_method
    def forward(self, x, hidden):
        # type: (Tensor, Tensor) -> Tuple[Tensor, Tensor]
        inputs = x.unbind(0)
        outputs = torch.jit.annotate(List[Tensor], [])

        for i in range(len(inputs)):
            hidden = self.cell(inputs[i], hidden)
            outputs += [hidden]

        return torch.stack(outputs), hidden

# ----------------------------------------------------------------------------------------------------------------------
class JitGRULN(jit.ScriptModule):
    __constants__ = ['hidden_size', 'num_layers', 'batch_first', 'layers']

    def __init__(self, input_size, hidden_size, num_layers, batch_first=False, bias=True):
        super(JitGRULN, self).__init__()
        # The following are not implemented.
        assert bias

        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.batch_first = batch_first

        if num_layers == 1:
            self.layers = nn.ModuleList([JitGRULayer(LayerNormGRUCell, input_size, hidden_size)])
        else:
            self.layers = nn.ModuleList([JitGRULayer(LayerNormGRUCell, input_size, hidden_size)] + [JitGRULayer(LayerNormGRUCell, hidden_size, hidden_size)
                                                                                for _ in range(num_layers - 1)])

    @jit.script_method
    def forward(self, x, h=None):
        # type: (Tensor, Optional[Tensor]) -> Tuple[Tensor, Tensor]
        output_states = jit.annotate(List[Tensor], [])

        # Handle batch_first cases
        if self.batch_first:
            x = x.permute(1, 0, 2)

        if h is None:
            h = torch.zeros(self.num_layers, x.shape[1], self.hidden_size, dtype=x.dtype, device=x.device)

        output = x
        i = 0

        for rnn_layer in self.layers:
            output, hidden = rnn_layer(output, h[i])
            output_states += [hidden]
            i += 1

        # Don't forget to handle batch_first cases for the output too!
        if self.batch_first:
            output = output.permute(1, 0, 2)

        return output, torch.stack(output_states)


import json
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from invoices.mixins import Paginate
from invoices.models import Invoice, Item
from invoices.serializers import InvoiceSerializer, ItemSerializer

class InvoiceAPI(LoginRequiredMixin, ViewSet):
    """ Endpoint that manages invoice detail data
    """
    serializer_class = InvoiceSerializer

    def detail(self, *args, **kwargs):
        serializer = self.serializer_class(get_object_or_404(
          self.serializer_class.Meta.model, id=kwargs.get('inv_id')))

        return Response(serializer.data, status=200)

    def update(self, *args, **kwargs):
        invoice = get_object_or_404(Invoice, id=kwargs.get('inv_id'))
        serializer = self.serializer_class(data=self.request.data, instance=invoice)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=200)

    def delete(self, *args, **kwargs):
        invoice = get_object_or_404(Invoice, id=kwargs.get('inv_id'))
        invoice.delete()

        return Response(status=200)

class InvoicesAPI(LoginRequiredMixin, Paginate ,ViewSet):
    """ Endpoint that manages invoice data
    """
    serializer_class = InvoiceSerializer

    def list(self, *args, **kwargs):
        invoices = self.serializer_class.Meta.model.objects.all()
        data = self.paginate(invoices)

        return Response(data, status=200)

    def create(self, *args, **kwargs):
        serializer = self.serializer_class(data=self.request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=201)

    def terms(self, *args, **kwargs):
        terms = json.dumps(self.serializer_class.Meta.model.TERMS)

        return Response(json.loads(terms))

    def latest(self, *args, **kwargs):
        serializer = self.serializer_class(
                self.serializer_class.Meta.model.objects.order_by('date_created').last())

        return Response(serializer.data)

class ItemsAPI(LoginRequiredMixin, ViewSet):
    """ Add item in invoice
    """
    serializer_class = ItemSerializer

    def create(self, *args, **kwargs):
        serializer = self.serializer_class(data=self.request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=201)

class ItemAPI(LoginRequiredMixin, ViewSet):
    """ Detail of item
    """
    serializer_class = ItemSerializer

    def update(self, *args, **kwargs):
        item = get_object_or_404(Item, id=kwargs.get('item_id'))
        serializer = self.serializer_class(data=self.request.data, instance=item)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=200)


import json
from datetime import timezone

import responses

from descarteslabs.compute import Job, JobStatus

from .base import BaseTestCase

class TestCreateJob(BaseTestCase):
    @responses.activate
    def test_create(self):
        params = dict(function_id="some-fn", args=[1, 2], kwargs={"key": "blah"})
        self.mock_job_create(params)

        job = Job(**params)
        assert job.state == "new"
        job.save()
        assert job.state == "saved"
        assert job.id
        assert job.creation_date == self.now.replace(tzinfo=timezone.utc)
        self.assertDictContainsSubset(params, job.to_dict())

    @responses.activate
    def test_create_with_tags(self):
        params = dict(
            function_id="some-fn",
            args=[1, 2],
            kwargs={"key": "blah"},
            tags=["tag1", "tag2"],
        )
        self.mock_job_create(params)

        job = Job(**params)
        assert job.state == "new"
        job.save()
        assert job.state == "saved"
        assert job.id
        assert job.creation_date == self.now.replace(tzinfo=timezone.utc)
        self.assertDictContainsSubset(params, job.to_dict())

class TestListJobs(BaseTestCase):
    @responses.activate
    def test_list_jobs(self):
        self.mock_response(
            responses.GET,
            "/jobs",
            json=self.make_page(
                [self.make_job(), self.make_job()],
                page_cursor="page2",
            ),
        )
        self.mock_response(
            responses.GET,
            "/jobs",
            json=self.make_page([self.make_job()]),
        )
        jobs = list(Job.list())

        for job in jobs:
            assert isinstance(job, Job)
            assert job.state == "saved"
            assert job.to_dict()

        assert len(jobs) == 3
        self.assert_url_called("/jobs?page_size=100", 1)
        self.assert_url_called("/jobs?page_cursor=page2", 1)

    @responses.activate
    def test_list_jobs_compatible(self):
        self.mock_response(
            responses.GET,
            "/jobs",
            json=self.make_page([self.make_job()]),
        )
        list(Job.list(status=JobStatus.PENDING, function_id="some-fn"))
        self.assert_url_called(
            "/jobs",
            params={
                "page_size": 100,
                "status": "pending",
                "function_id": "some-fn",
            },
        )

        list(Job.list(status=[JobStatus.PENDING, JobStatus.RUNNING]))
        self.assert_url_called(
            "/jobs",
            params={
                "page_size": 100,
                "status": ["pending", "running"],
            },
        )

class TestJob(BaseTestCase):
    @responses.activate
    def test_get(self):
        self.mock_response(
            responses.GET,
            "/jobs/some-id",
            json=self.make_job(
                id="some-id",
                function_id="function-id",
                args=[1, 2],
                kwargs={"first": "blah", "second": "blah"},
            ),
        )
        job = Job.get("some-id")
        assert job.state == "saved"
        assert job.to_dict() == {
            "args": [1, 2],
            "creation_date": self.now.replace(tzinfo=timezone.utc).isoformat(),
            "error_reason": None,
            "execution_count": None,
            "exit_code": None,
            "function_id": "function-id",
            "id": "some-id",
            "kwargs": {"first": "blah", "second": "blah"},
            "runtime": None,
            "status": JobStatus.PENDING,
            "tags": [],
            "provisioning_time": None,
            "pull_time": None,
        }

    @responses.activate
    def test_delete(self):
        self.mock_response(responses.DELETE, "/jobs/some-id")
        job = Job(id="some-id", function_id="some-fn", saved=True)
        job.delete()
        self.assert_url_called("/jobs/some-id")
        assert job._deleted is True
        assert job.state == "deleted"

    @responses.activate
    def test_delete_new(self):
        job = Job(id="some-id", function_id="some-fn", saved=True)
        job._saved = False

        with self.assertRaises(ValueError) as ctx:
            job.delete()
        assert "has not been saved" in str(ctx.exception)
        assert job._deleted is False
        assert job.state == "new"

    @responses.activate
    def test_delete_failed(self):
        self.mock_response(responses.DELETE, "/jobs/some-id", status=400)
        job = Job(id="some-id", function_id="some-fn", saved=True)

        with self.assertRaises(Exception):
            job.delete()

        self.assert_url_called("/jobs/some-id")
        assert job._deleted is False
        assert job.state == "saved"

    @responses.activate
    def test_result_empty(self):
        self.mock_response(responses.GET, "/jobs/some-id/result", body=None)
        job = Job(id="some-id", function_id="some-fn", saved=True)
        assert job.result() is None

    @responses.activate
    def test_result_json(self):
        body = json.dumps({"test": "blah"}).encode()
        self.mock_response(responses.GET, "/jobs/some-id/result", body=body)
        job = Job(id="some-id", function_id="some-fn", saved=True)
        assert job.result() == {"test": "blah"}

    @responses.activate
    def test_result_float(self):
        body = json.dumps(15.68).encode()
        self.mock_response(responses.GET, "/jobs/some-id/result", body=body)
        job = Job(id="some-id", function_id="some-fn", saved=True)
        assert job.result() == 15.68

    @responses.activate
    def test_result_cast(self):
        class CustomString:
            @classmethod
            def deserialize(cls, data: bytes):
                return "custom"

        self.mock_response(responses.GET, "/jobs/some-id/result", body="blah")
        job = Job(id="some-id", function_id="some-fn", saved=True)
        assert job.result(CustomString) == "custom"

        with self.assertRaises(ValueError) as ctx:
            job.result(bool)
        assert "must implement Serializable" in str(ctx.exception)

    @responses.activate
    def test_log(self):
        log_lines = ["test", "log"]
        log = "\n".join(
            [
                json.dumps({"date": self.now.isoformat() + "Z", "log": log})
                for log in log_lines
            ]
        )
        log_bytes = (log + "\n").encode()
        self.mock_response(responses.GET, "/jobs/some-id/log", body=log_bytes)

        job = Job(id="some-id", function_id="some-fn", saved=True)
        job.log()

    @responses.activate
    def test_wait_for_complete(self):
        self.mock_response(
            responses.GET,
            "/jobs/some-id",
            json=self.make_job(
                id="some-id",
                function_id="function-id",
                args=[1, 2],
                kwargs={},
            ),
        )
        self.mock_response(
            responses.GET,
            "/jobs/some-id",
            json=self.make_job(
                id="some-id",
                function_id="function-id",
                args=[1, 2],
                kwargs={},
                status=JobStatus.SUCCESS,
            ),
        )
        job = Job(id="some-id", function_id="function-id", saved=True)
        job.wait_for_completion(interval=0.1, timeout=5)
        assert job.status == JobStatus.SUCCESS

    @responses.activate
    def test_wait_for_complete_timeout(self):
        self.mock_response(
            responses.GET,
            "/jobs/some-id",
            json=self.make_job(
                id="some-id",
                function_id="function-id",
                args=[1, 2],
                kwargs={},
            ),
        )
        job = Job(id="some-id", function_id="function-id", saved=True)
        with self.assertRaises(TimeoutError):
            job.wait_for_completion(interval=0.1, timeout=5)

    @responses.activate
    def test_modified_patch(self):
        self.mock_response(
            responses.PATCH,
            "/jobs/some-id",
            json=self.make_job(id="some-id", function_id="some-fn", args=[1, 2]),
        )

        job = Job(id="some-id", function_id="some-fn", saved=True)
        job.args = [1, 2]
        job.save()
        assert job.state == "saved"
        self.assert_url_called("/jobs/some-id", json={"args": [1, 2]})

class TestJobNoApi(BaseTestCase):
    @responses.activate
    def test_no_request_when_saved(self):
        job = Job(id="some-id", function_id="some-fn", saved=True)
        job.save()
        assert len(responses.calls) == 0

    @responses.activate
    def test_deleted(self):
        job = Job(id="some-id", function_id="some-fn", saved=True)
        job._deleted = True

        with self.assertRaises(AttributeError) as ctx:
            job.save()
        assert "Job has been deleted" in str(ctx.exception)


import sys

if sys.version_info >= (3, 8):
    from unittest.mock import AsyncMock
else:
    from asynctest.mock import CoroutineMock as AsyncMock

from unittest.mock import Mock, call, sentinel

import pytest
from pytest import raises

from jj.matchers import AnyMatcher, LogicalMatcher, ResolvableMatcher

from ..._test_utils.fixtures import request_, resolver_
from ..._test_utils.steps import given, then, when

__all__ = ("request_", "resolver_",)

@pytest.mark.asyncio
@pytest.mark.parametrize(("ret_val", "res"), [
    (True, True),
    (False, False),
])
async def test_single_submatcher(ret_val, res, *, resolver_, request_):
    with given:
        submatcher_ = Mock(ResolvableMatcher, match=AsyncMock(return_value=ret_val))
        matcher = AnyMatcher([submatcher_], resolver=resolver_)

    with when:
        actual = await matcher.match(request_)

    with then:
        assert actual is res
        assert submatcher_.mock_calls == [call.match(request_)]

@pytest.mark.asyncio
@pytest.mark.parametrize(("ret_val1", "ret_val2", "res"), [
    (True, True, True),
    (True, False, True),
])
async def test_multiple_truthy_submatchers(ret_val1, ret_val2, res, *, resolver_, request_):
    with given:
        submatcher1_ = Mock(ResolvableMatcher, match=AsyncMock(return_value=ret_val1))
        submatcher2_ = Mock(ResolvableMatcher, match=AsyncMock(return_value=ret_val2))
        matcher = AnyMatcher([submatcher1_, submatcher2_], resolver=resolver_)

    with when:
        actual = await matcher.match(request_)

    with then:
        assert actual is res
        assert submatcher1_.mock_calls == [call.match(request_)]
        assert submatcher2_.mock_calls == []

@pytest.mark.asyncio
@pytest.mark.parametrize(("ret_val1", "ret_val2", "res"), [
    (False, True, True),
    (False, False, False),
])
async def test_multiple_false_submatchers(ret_val1, ret_val2, res, *, resolver_, request_):
    with given:
        submatcher1_ = Mock(ResolvableMatcher, match=AsyncMock(return_value=ret_val1))
        submatcher2_ = Mock(ResolvableMatcher, match=AsyncMock(return_value=ret_val2))
        matcher = AnyMatcher([submatcher1_, submatcher2_], resolver=resolver_)

    with when:
        actual = await matcher.match(request_)

    with then:
        assert actual is res
        assert submatcher1_.mock_calls == [call.match(request_)]
        assert submatcher2_.mock_calls == [call.match(request_)]

def test_empty_submatchers_raises_exception(*, resolver_):
    with when, raises(Exception) as exception:
        AnyMatcher([], resolver=resolver_)

    with then:
        assert exception.type is AssertionError

def test_is_instance_of_logical_matcher(*, resolver_):
    with given:
        submatcher_ = Mock(ResolvableMatcher)
        matcher = AnyMatcher([submatcher_], resolver=resolver_)

    with when:
        actual = isinstance(matcher, LogicalMatcher)

    with then:
        assert actual is True

def test_repr(*, resolver_):
    with given:
        resolver_.__repr__ = Mock(return_value="<Resolver>")
        matcher = AnyMatcher(resolver=resolver_, matchers=[
            Mock(ResolvableMatcher, __repr__=Mock(return_value="<SubMatcher1>")),
            Mock(ResolvableMatcher, __repr__=Mock(return_value="<SubMatcher2>")),
        ])

    with when:
        actual = repr(matcher)

    with then:
        assert actual == "AnyMatcher([<SubMatcher1>, <SubMatcher2>], resolver=<Resolver>)"

def test_pack(*, resolver_):
    with given:
        submatchers = [Mock(ResolvableMatcher), Mock(ResolvableMatcher)]
        matcher = AnyMatcher(submatchers, resolver=resolver_)

    with when:
        actual = matcher.__packed__()

    with then:
        assert actual == {"matchers": submatchers}

def test_unpack(*, resolver_):
    with given:
        submatchers = [Mock(ResolvableMatcher), Mock(ResolvableMatcher)]
        kwargs = {
            "matchers": submatchers,
            "resolver": resolver_,
            "future_field": sentinel,
        }

    with when:
        actual = AnyMatcher.__unpacked__(**kwargs)

    with then:
        assert isinstance(actual, AnyMatcher)


"""Original processMiner.py from client
"""
import datetime
import pygraphviz as pgv
import xml.etree.ElementTree as ET

tree = ET.parse('repairExample.xes')
root = tree.getroot()
log = dict()

ns = {'xes': 'http://www.xes-standard.org/'}

for trace in root.findall('xes:trace', ns):
    caseid = ''
    for string in trace.findall('xes:string', ns):
        if string.attrib['key'] == 'concept:name':
            caseid = string.attrib['value']
    for event in trace.findall('xes:event', ns):
        task = ''
        user = ''
        event_type = ''
        for string in event.findall('xes:string', ns):
            if string.attrib['key'] == 'concept:name':
                task = string.attrib['value']
            if string.attrib['key'] == 'org:resource':
                user = string.attrib['value']
            if string.attrib['key'] == 'lifecycle:transition':
                event_type = string.attrib['value']
        timestamp = ''
        for date in event.findall('xes:date', ns):
            if date.attrib['key'] == 'time:timestamp':
                timestamp = date.attrib['value']
                timestamp = datetime.datetime.strptime(
                    timestamp[:-10], '%Y-%m-%dT%H:%M:%S'
                )
        print(caseid, '|', task, '|', user, '|', timestamp)
        if caseid not in log:
            log[caseid] = []
        event = (task, user, timestamp)
        log[caseid].append(event)

F = dict()
for caseid in log:
    for i in range(0, len(log[caseid]) - 1):
        ai = log[caseid][i][0]
        aj = log[caseid][i+1][0]
        if ai not in F:
            F[ai] = dict()
        if aj not in F[ai]:
            F[ai][aj] = 0
        F[ai][aj] += 1

A = dict()
for caseid in log:
    for i in range(0, len(log[caseid])):
        ai = log[caseid][i][0]
        if ai not in A:
            A[ai] = 0
        A[ai] += 1

G = pgv.AGraph(strict=False, directed=True)
G.graph_attr['rankdir'] = 'LR'
G.node_attr['shape'] = 'box'

x_min = min(A.values())
x_max = max(A.values())
for ai in A:
    text = ai + '\n' + str(A[ai]) + ')'
    gray = int(float(x_max - A[ai]) / float(x_max - x_min) * 100.00)
    fill = 'gray' + str(gray)
    font = 'black'
    if gray < 50:
        font = 'white'
    G.add_node(ai, label=text, style='filled', fillcolor=fill, fontcolor=font)

values = [F[ai][aj] for ai in F for aj in F[ai]]
x_min = min(values)
x_max = max(values)
y_min = 1.0
y_max = 5.0

for ai in F:
    for aj in F[ai]:
        x = F[ai][aj]
        y = y_min + (y_max - y_min) * float(x - x_min)/float(x_max - x_min)
        G.add_edge(ai, aj, label=x, penwidth=y)

G.draw('graph.png', prog='dot')
# processMiner.py
# Displaying processMiner.py.


import pickle
import random
import sys
import subprocess
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.config import Config

zadani = pickle.load(open("gramatika/data.dat", "rb"))
print(zadani)

Config.set('graphics', 'width', '1200')
Config.set('graphics', 'height', '800')
Config.set('graphics', 'resizable', False)

class Okno(Widget):
    l0 = ObjectProperty(None)
    l1 = ObjectProperty(None)
    l2 = ObjectProperty(None)
    l3 = ObjectProperty(None)
    l4 = ObjectProperty(None)
    l5 = ObjectProperty(None)
    l6 = ObjectProperty(None)
    l7 = ObjectProperty(None)
    l8 = ObjectProperty(None)
    l9 = ObjectProperty(None)

    t0 = ObjectProperty(None)
    t1 = ObjectProperty(None)
    t2 = ObjectProperty(None)
    t3 = ObjectProperty(None)
    t4 = ObjectProperty(None)
    t5 = ObjectProperty(None)
    t6 = ObjectProperty(None)
    t7 = ObjectProperty(None)
    t8 = ObjectProperty(None)
    t9 = ObjectProperty(None)

    button = ObjectProperty(None)
    pb = 0
    c = ['', '', '', '', '', '', '', '', '', '']

    def btn(self):
        if self.pb == 0:
            self.pb = 1
            self.button.text = "Zkontrolovat"

            s = 0
            for x in range(10):
                s = 0
                while s == 0:
                    self.c[x] = random.randint(0, len(zadani) - 1)
                    s = 1
                    for y in range(len(zadani) - 1):
                        if self.c[x] == self.c[y]:
                            if x != y:
                                s = 0

            self.l0.text = zadani[self.c[0]][0]
            self.l1.text = zadani[self.c[1]][0]
            self.l2.text = zadani[self.c[2]][0]
            self.l3.text = zadani[self.c[3]][0]
            self.l4.text = zadani[self.c[4]][0]
            self.l5.text = zadani[self.c[5]][0]
            self.l6.text = zadani[self.c[6]][0]
            self.l7.text = zadani[self.c[7]][0]
            self.l8.text = zadani[self.c[8]][0]
            self.l9.text = zadani[self.c[9]][0]
        else:
            ch = 0
            if self.t0.text == zadani[self.c[0]][1]:
                self.l0.color = 0, 1, 0, 1
            else:
                self.l0.color = 1, 0, 0, 1
                ch += 1

            if self.t1.text == zadani[self.c[1]][1]:
                self.l1.color = 0, 1, 0, 1
            else:
                self.l1.color = 1, 0, 0, 1
                ch += 1

            if self.t2.text == zadani[self.c[2]][1]:
                self.l2.color = 0, 1, 0, 1
            else:
                self.l2.color = 1, 0, 0, 1
                ch += 1

            if self.t3.text == zadani[self.c[3]][1]:
                self.l3.color = 0, 1, 0, 1
            else:
                self.l3.color = 1, 0, 0, 1
                ch += 1

            if self.t4.text == zadani[self.c[4]][1]:
                self.l4.color = 0, 1, 0, 1
            else:
                self.l4.color = 1, 0, 0, 1
                ch += 1

            if self.t5.text == zadani[self.c[5]][1]:
                self.l5.color = 0, 1, 0, 1
            else:
                self.l5.color = 1, 0, 0, 1
                ch += 1

            if self.t6.text == zadani[self.c[6]][1]:
                self.l6.color = 0, 1, 0, 1
            else:
                self.l6.color = 1, 0, 0, 1
                ch += 1

            if self.t7.text == zadani[self.c[7]][1]:
                self.l7.color = 0, 1, 0, 1
            else:
                self.l7.color = 1, 0, 0, 1
                ch += 1

            if self.t8.text == zadani[self.c[8]][1]:
                self.l8.color = 0, 1, 0, 1
            else:
                self.l8.color = 1, 0, 0, 1
                ch += 1

            if self.t9.text == zadani[self.c[9]][1]:
                self.l9.color = 0, 1, 0, 1
            else:
                self.l9.color = 1, 0, 0, 1
                ch += 1

            if ch > 0:
                self.button.text = "Chyb: " + str(ch) + " | Zkontrolovat"
            else:
                self.button.text = "Vyhjál, pjvní"
                subprocess.Popen(['python', 'gramatika/win.py'])
                App.stop()
                sys.exit(0)

class GramatikaApp(App):
    def build(self):
        return Okno()

if __name__ == "__main__":
    GramatikaApp().run()


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

   Copyright 2021 Recurve Analytics, Inc.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

"""
import os
import pandas as pd
from sqlalchemy import create_engine

from .settings import ACC_COMPONENTS_ELECTRICITY, ACC_COMPONENTS_GAS, database_location

__all__ = (
    "get_db_connection",
    "get_deer_load_shape",
    "get_filtered_acc_elec",
    "get_filtered_acc_gas",
)

def get_db_connection(database_year="2020"):
    """Get the db connection for a given year

    Parameters
    ----------
    database_year: str
        The year corresponding to the database that contains the avoided costs data.
        Requires that year's database to have already been downloaded
        using the `flexvalue downloaded-avoided-costs-data-db --year 20XX` command.

    Returns
    -------
    sqlalchemy.engine.Engine
    """
    full_db_path = os.path.join(database_location(), f"{database_year}.db")
    if not os.path.exists(full_db_path):
        raise ValueError(f"Can not find SQLite file at this path: {full_db_path}")
    database_url = f"sqlite:///{full_db_path}"
    return create_engine(database_url)

def execute_query(database_year, query):
    """Execute arbitrary query on the avoided costs db

    Parameters
    ----------
    database_year: str
        The year corresponding to the database that contains the avoided costs data.
        Requires that year's database to have already been downloaded
        using the `flexvalue downloaded-avoided-costs-data-db --year 20XX` command.

    Returns
    -------
    pd.DataFrame
    """
    con = get_db_connection(database_year=database_year)
    return pd.read_sql(query, con=con).drop("local_pkid_", axis=1)

def get_deer_load_shape(database_year):
    """Returns all of the deer load shape 8760 load profiles

    Parameters
    ----------
    database_year: str
        The year corresponding to the database that contains the avoided costs data.
        Requires that year's database to have already been downloaded
        using the `flexvalue downloaded-avoided-costs-data-db --year 20XX` command.

    Returns
    -------
    pd.DataFrame
    """
    con = get_db_connection(database_year=database_year)
    return pd.read_sql_table("deer_load_shapes", con=con).set_index("hour_of_year")

def get_filtered_acc_elec(database_year, utility, climate_zone, start_year, end_year):
    """Returns the electricity avoided costs data

    Parameters
    ----------
    database_year: str
        The year corresponding to the database that contains the avoided costs data.
        Requires that year's database to have already been downloaded
        using the `flexvalue downloaded-avoided-costs-data-db --year 20XX` command.
    utility: str
        Which uility to filter by when loading avoided costs data
    climate_zone: str
        Which climate zone to filter by when loading avoided costs data
    start_year: int
        Which year to start the filter of avoided costs data
    end_year: int
        Which year to end the filter of avoided costs data

    Returns
    -------
    pd.DataFrame
    """
    columns = [
        "year",
        "month",
        "hour_of_day",
        "hour_of_year",
        *ACC_COMPONENTS_ELECTRICITY,
        "marginal_ghg",
    ]
    columns_str = ", ".join(columns)
    climate_zone = (
        climate_zone if climate_zone.startswith("CZ") else f"CZ{climate_zone}"
    )
    sql_str = f"""
        SELECT *
        FROM acc_electricity
        WHERE utility = '{utility}'
        AND climate_zone = '{climate_zone}'
        AND year >= {start_year}
        AND year <= {end_year}
    """
    con = get_db_connection(database_year=database_year)
    df = pd.read_sql(sql_str, con=con)
    if df.empty:
        raise ValueError(
            "Can not find avoided costs for\n:"
            f"utility:{utility}\nclimate_zone:{climate_zone}\nstart_year:{start_year}\n"
            f"end_year:{end_year}"
        )
    return df

def get_filtered_acc_gas(database_year, start_year, end_year):
    """Returns gas avoided costs data

    Parameters
    ----------
    database_year: str
        The year corresponding to the database that contains the avoided costs data.
        Requires that year's database to have already been downloaded
        using the `flexvalue downloaded-avoided-costs-data-db --year 20XX` command.
    start_year: int
        Which year to start the filter of avoided costs data
    end_year: int
        Which year to end the filter of avoided costs data

    Returns
    -------
    pd.DataFrame
    """
    columns = [
        "year",
        "month",
        *ACC_COMPONENTS_GAS,
    ]
    columns_str = ", ".join(columns)
    sql_str = f"""
        SELECT *
        FROM acc_gas
        WHERE year >= {start_year}
        AND year <= {end_year}
    """
    con = get_db_connection(database_year=database_year)
    return pd.read_sql(sql_str, con=con)

def get_all_valid_utility_climate_zone_combinations(database_year, utility=None):
    """Returns all utility-climate zone combinations

    Parameters
    ----------
    database_year: str
        The year corresponding to the database that contains the avoided costs data.
        Requires that year's database to have already been downloaded
        using the `flexvalue downloaded-avoided-costs-data-db --year 20XX` command.
    utility: str
        (optional) Which uility to filter by when loading avoided costs data

    Returns
    -------
    pd.DataFrame
    """
    where_str = f"WHERE utility = '{utility}'" if utility else ""
    query = f"""
    SELECT *
    FROM acc_electricity_utilities_climate_zones
    {where_str}
    """
    return execute_query(database_year, query)

def get_all_valid_deer_load_shapes(database_year):
    """Returns all valid DEER load shapes

    Parameters
    ----------
    database_year: str
        The year corresponding to the database that contains the avoided costs data.
        Requires that year's database to have already been downloaded
        using the `flexvalue downloaded-avoided-costs-data-db --year 20XX` command.

    Returns
    -------
    list
    """
    query = """
        SELECT *
        FROM deer_load_shapes
        limit 1
        """
    valid_deer_load_shapes = execute_query(database_year, query)
    all_columns_w_utilities = list(
        valid_deer_load_shapes.drop("hour_of_year", axis=1).columns
    )
    # TODO (ssuffian): Reshape db so it is a query by utility column
    return list(
        set(
            [
                c.replace("PGE_", "")
                .replace("SDGE_", "")
                .replace("SCG_", "")
                .replace("SCE_", "")
                for c in all_columns_w_utilities
            ]
        )
    )


import tensorflow as tf
import argparse
# import cv2
from tensorflow.examples.tutorials.mnist.input_data import read_data_sets

#tensorboard --logdir logs --port 6789
class Config:
    def __init__(self):
        self.lr = 0.001
        self.filters = 32
        self.batch_size = 64
        self.classes = 10
        self.eps = 1e-8

        self.epoches = 5
        self.name = 'p07'
        self.save_path = './models/{name}/{name}'.format(name = self.name)
        self.sample_path = './data/MNIST_data'
        self.logdir = './logs/{name}'.format(name = self.name)

    def from_cmd_line(self):
        attrs_dict = self._get_attrs()
        parse = argparse.ArgumentParser()
        for key, value in attrs_dict.items():
            parse.add_argument('--'+key, type = type(value), default=value, help = 'set %s' % key)
        a = parse.parse_args()
        for name in attrs_dict:
            setattr(self, name, getattr(a, name))

    def __repr__(self):
        result = '{'
        for key, value in self._get_attrs().items():
            result += '%s=%s,' % (key, value)
        result = result[0:-1]
        result += '}'
        return result

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

class Tensors:
    def __init__(self, config: Config):
        with tf.device('/gpu:0'):
            self.lr = tf.placeholder(tf.float32, [], 'lr')
            self.x = tf.placeholder(tf.float32, [None, 784], 'x')
            x = tf.reshape(self.x, [-1, 28, 28, 1])

            x = tf.layers.conv2d(x, config.filters, 3, 1, 'same', activation = tf.nn.relu, name = 'conv1') #[-1, 28, 28, 32]

            x = tf.layers.conv2d(x, 2*config.filters, 3, 1, 'same', activation = tf.nn.relu, name = 'conv2') #[-1, 28, 28, 64]
            x = tf.layers.max_pooling2d(x, 2, 2) # #[-1, 14, 14, 64]

            x = tf.layers.conv2d(x, 4*config.filters, 3, 1, 'same', activation = tf.nn.relu, name = 'conv3') #[-1, 28, 28, 128]
            x = tf.layers.max_pooling2d(x, 2, 2) # #[-1, 7, 7, 128]

            x = tf.layers.flatten(x)
            x = tf.nn.relu(x)

            x = tf.layers.dense(x, 1000, activation=tf.nn.relu, name = 'dense1')
            y_predict = tf.layers.dense(x, config.classes, name = 'dense2')
            y_predict = tf.nn.softmax(y_predict)
            y_predict = tf.maximum(y_predict, config.eps)

            self.y = tf.placeholder(tf.int32, [None], 'y')
            y = tf.one_hot(self.y, config.classes)

            self.loss = -tf.reduce_mean(y*tf.log(y_predict))

            opt = tf.train.AdamOptimizer(self.lr)
            self.train_op = opt.minimize(self.loss)

            tf.summary.scalar('loss', self.loss)
            self.summary_op = tf.summary.merge_all()

class Samples:
    def __init__(self, config: Config):
        self.ds = read_data_sets(config.sample_path)

    def next_batch(self, batch_size):
        return self.ds.train.next_batch(batch_size)

    @property
    def num(self):
        return self.ds.train.num_examples

class Mnist:
    def __init__(self, config: Config):
        self.config = config
        graph = tf.Graph()
        with graph.as_default():
            self.tensors = Tensors(config)
            conf = tf.ConfigProto()
            conf.allow_soft_placement = True
            self.session = tf.Session(graph=graph, config = conf)
            self.saver = tf.train.Saver()
            self.file_writer = tf.summary.FileWriter(config.logdir, graph = graph)
            try:
                self.saver.restore(self.session, config.save_path)
                print('resotr the model successfully!')
            except:
                print('the mdoel doese not exst, we have to train a new one')
                self.train()

    def train(self):
        self.session.run(tf.global_variables_initializer())
        self.samples = Samples(self.config)
        batches =self.samples.num // self.config.batch_size
        for epoch in range(self.config.epoches):
            for batch in range(batches):
                x, y = self.samples.next_batch(self.config.batch_size)
                feed_dic = {
                    self.tensors.x: x,
                    self.tensors.y: y,
                    self.tensors.lr: self.config.lr
                }
                _, lo, su = self.session.run([self.tensors.train_op, self.tensors.loss, self.tensors.summary_op], feed_dict=feed_dic)
                self.file_writer.add_summary(su, epoch * batches + batch)
            print('epoch=%d, loss=%f' % (epoch, lo))
        self.saver.save(self.session, self.config.save_path)

    def predict(self):
        pass

    def close(self):
        self.session.close()

if __name__ == '__main__':
    config = Config()
    mnist = Mnist(config)
    mnist.close()


import random
import string
from django.db import transaction
from django.shortcuts import render, redirect, get_object_or_404
from haikunator import Haikunator
from .models import Room, Member, MemberAccept
from django.views.decorators.cache import never_cache
from django.shortcuts import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

from django.contrib.auth.models import User
from django.views.generic import TemplateView
from .forms import FriendForm, LabelForm
from .utils import get_room_or_error, catch_client_error
from django.core.urlresolvers import reverse

haikunator = Haikunator()

@never_cache
@login_required
def about(request):
    return render(request, "chat/about.html")
'''
@never_cache
@login_required
def new_room(request, *args,**kwargs):
    """
    Randomly create a new room, and redirect to it.
    """

    new_room = None
    while not new_room:
        with transaction.atomic():
            label = haikunator.haikunate()
            if Room.objects.filter(label=label).exists():
                continue
            new_room = Room.objects.create(label=label, current_user_id=request.user.id, private = False)

    return redirect("chat_index:chat_room", pk = new_room.pk)
    #return HttpResponseRedirect(reverse("chat_index:chat_room", pk = new_room.pk))
'''

from django.core.exceptions import PermissionDenied

@never_cache
@login_required
#@catch_client_error
def chat_room(request, pk, *args,**kwargs):
    """
    Room view - show the room, with latest messages.

    The template for this view has the WebSocket business to send and stream
    messages, so see the template for where the magic happens.
    """

    # If the room with the given label doesn't exist, automatically create it
    # upon first visit (a la etherpad).
    #room = get_object_or_404(Room, pk=pk)
    room = get_object_or_404(Room, pk=pk)
    access = False
    if room.private == True:
        memberaccepter = MemberAccept.objects.filter(acceptroom_id=pk, accepter_id = request.user.id, agree = True)
        if not memberaccepter:
            if request.user.id != room.current_user_id:
                access = False
            else:
                access = True
        else:
            access = True
    elif room.private == False:
        access = True
    else:
        access = False

    if access != True:
        #need to output with error
        raise PermissionDenied
        #return redirect('chat_index:chat_list_rooms')

    else:
        #rooms = get_room_or_error(pk, request.user)
        #room = Room.objects.get(pk=rooms.pk)

        # We want to show the last 50 messages, ordered most-recent-last
        messages = room.messages.order_by('-timestamp')[:10]
        return render(request, "chat/room.html", {
            'room': room,
            'messages': messages,
        })

from django.core.paginator import Paginator, InvalidPage
from el_pagination.decorators import page_template

@never_cache
@login_required
@page_template('chat/list_rooms_page.html')
def chat_list_rooms(request, template = 'chat/list_rooms.html',
    extra_context=None):
    #template = 'chat/list_rooms.html'
    #page_template = 'chat/list_rooms_page.html'

    context = {
        'object_list': Room.objects.all(),

    }
    if extra_context is not None:
        context.update(extra_context)

    return render(
        request, template, context)

'''
@never_cache
@login_required
def priv_room(request):
    users = User.objects.order_by('id')

    return render(request, "chat/priv_room.html",{
        "users": users,
        })
'''

class FriendView(TemplateView):
    template_name = "chat/priv_room.html"

    def get(self, request, pk, *args, **kwargs):

        room = get_object_or_404(Room, pk=pk)
        #room = Room.objects.get(pk=pk)
        if request.user.id == room.current_user_id:
            form = FriendForm()
            users = User.objects.exclude(id=request.user.id)

            query = request.GET.get("q")

            true_members=[]
            memberlist = None

            if query:
                queryset_list = User.objects.filter(username=query)

            else:
                queryset_list = None

            #your followers
            #drugs = room.who_added_user(request.user)

            #member, created = Member.objects.get_or_create(current_user=request.user, pk=pk)
            #members = member.users.all()
            members = MemberAccept.objects.filter(acceptroom_id=pk, agree=False)
            membersinroom = MemberAccept.objects.filter(acceptroom_id=pk, agree=True)

            if queryset_list:
                memberlist = MemberAccept.objects.filter(acceptroom_id=pk, accepter = queryset_list)

            '''
            for member in members:
                memberlist.append(member)

                for drug in drugs:

                    if drug.pk == member.pk:
                        true_members.append(drug)
                        drugs.remove(drug)
                        memberlist.remove(drug)
            '''

            context = {'form': form, 'users': users,
                   'members': members,
                   'membersinroom': membersinroom,
                   #'drugs': drugs,
                   'object_list': queryset_list,
                   #'true_members': true_members,
                   'memberlist': memberlist,
                   'room': room,
                   }
            return render(request,self.template_name, context)
        else:
            return redirect('home')
    def post(self, request, *args, **kwargs):
        return HttpResponseRedirect(reverse('chat_index:select_room'))

@never_cache
@login_required
def change_members(request, pk, operation, pkid):

    member = User.objects.get(pk=pkid)
    label = get_object_or_404(Room, pk=pk)
    if operation == 'add':
        M = MemberAccept.objects.filter(accepter=member, acceptroom=label)
        if M:
            MemberAccept.objects.get(accepter=member,acceptroom=label)
        else:
            MemberAccept.objects.create(accepter=member,acceptroom=label,agree=False)
        return redirect('chat_index:private_room', pk = pk)
    elif operation == 'remove':
        #Member.remove_member(request.user, label, member)
        M = get_object_or_404(MemberAccept, accepter=member,acceptroom=label)
        M.delete()
        return redirect('chat_index:private_room', pk = pk)

    elif operation == 'delete':
        if label.current_user_id == request.user.id:
            M = get_object_or_404(Room, current_user=member,id=pk)
            M.delete()
            return HttpResponseRedirect(reverse('chat_index:select_room'))
        else:
            raise PermissionDenied
    return redirect('chat_index:private_room', pk = pk)

from django.contrib import messages
@never_cache
@login_required
def get_name(request):
    #template_name = "chat/title_room.html"
    new_room = None
    form = LabelForm(request.POST or None)

    if request.method == 'POST':
        '''
        while not new_room:
            query = request.POST.get("label")
            userid = request.user.id
            #checkmember = Member.objects.filter(current_user_id=userid)
            new_room = Member.objects.create(label=query, current_user_id=userid)
        '''

        #form = LabelForm(request.POST)
        if form.is_valid():
            query = request.POST.get("label")

            #check checkbox
            userid = request.user.id
            private = request.POST.get("private_group", False)
            if private:
                new_room = Room.objects.create(label=query, current_user_id=userid, private = True)
                return HttpResponseRedirect('/chat/private/%s/' % new_room.id)
            else:
                new_room = Room.objects.create(label=query, current_user_id=userid, private = False)
                #reverse for redirecting after POST
                return HttpResponseRedirect(reverse('chat_index:select_room'))

        else:
            render(request, 'chat/title_room.html', {'form': LabelForm()})
    #else:
        #form = LabelForm()
    # if a GET (or any other method) we'll create a blank form

    return render(request, 'chat/title_room.html', {'form': form})

@never_cache
@login_required
def select_room(request, *args,**kwargs):

    rooms = Room.objects.all()
    room_members = MemberAccept.objects.filter(accepter=request.user.id, agree=True)

    #userinrooms = Room.objects.filter(users__id = request.user.id)
    room_invites = MemberAccept.objects.filter(accepter=request.user.id, agree=False)

    room_owners = Room.objects.filter(current_user_id = request.user.id)
    general_rooms = Room.objects.filter(private = 0)
    #general_room_owners = general_rooms.filter(current_user_id = request.user.id)

    '''
    room_chats = []
    for room_member in room_members:
        for room_owner in room_owners:
            room_chats.append(room_member.acceptroom)
            if room_owner.label != room_member.acceptroom:
                room_chats.append(room_owner.label)
    '''

    return render(request, "chat/select_room.html", {
        'rooms': rooms,
        'room_members': room_members,
        #'userinrooms': userinrooms,
        'room_invites': room_invites,
        'room_owners': room_owners,
        'general_rooms': general_rooms,
        #'general_room_owners': general_room_owners,

    })

@never_cache
@login_required
def accept_members(request, pk, operation, pkid):

    accepter = User.objects.get(pk=pkid)
    acceptroom = Room.objects.get(pk=pk)
    if operation == 'agree':
        if accepter.id == request.user.id:
            M = get_object_or_404(MemberAccept, accepter=accepter,acceptroom=acceptroom)
            M.agree = True
            M.save()
            return redirect('chat_index:select_room')
        else:
            raise PermissionDenied

    elif operation == 'disagree':
        if accepter.id == request.user.id:
            M = get_object_or_404(MemberAccept, accepter=accepter,acceptroom=acceptroom, agree = False)
            M.delete()
            return redirect('chat_index:select_room')
        else:
            raise PermissionDenied
    elif operation == 'leave':
        if accepter.id == request.user.id:
            M = get_object_or_404(MemberAccept, accepter=accepter,acceptroom=acceptroom, agree = True)
            M.delete()
            return redirect('chat_index:select_room')
        else:
            raise PermissionDenied
    return redirect('chat_index:select_room')


from .Events import *
from .EMS import Event_Management_System, event_creation, Seminar_methods
from .Users import User, Student, Admin, Attendance
from .Session import session_attendees
from flask_login import login_user
from datetime import datetime

def date_converter(date_input):
    date_format = "%Y-%m-%d"
    date = datetime.strptime(date_input, date_format)
    return date

def bootstrap_system():

    system = Event_Management_System()
    b = 0
    with open("user.csv") as fp: #close file is called automatically
        for line in fp:
            if b > 0:
                l = line.split(",")

                if 'trainer' in line:
                    new_user = Admin(l[0], l[1], l[2], l[3], l[4], "secret")
                    system.add_users(new_user)
                else:
                    new_user = Student(l[0], l[1], l[2], l[3], l[4])
                    system.add_users(new_user)
            b += 1
    return system
'''
    user = Admin("Stephanie", "5133975", "z5133975@unsw.net", "pass", "trainer", "secret")
    system.add_users(user)
    Course = event_creation.create_course("course details", date_converter("2018-5-13"), date_converter("2018-5-13"), date_converter("2018-5-12"), 50, date_converter("2018-5-12"), 10, "test input", user, "UNSW", 0, 0)
    event_creation.add_course(system, Course)    # add new course in system
    user.post_event(Course)         # US1: add to admin subclass my event list
    event_creation.add_order(system, Course) # add new event in system
    Attendance.add_attending(user, Course) #add to users own record list
    Course.listAttendee().append(user)

    newSem = Seminar_methods.create_seminar('details', date_converter('2018-01-12'),date_converter('2018-01-15'), date_converter('2018-01-10'), 100, date_converter('2018-01-10'), 2, 'numSess', 2, 1, 1)
    #def create_seminar(details, start_date, end_date, deregistration, fee, early_bird, maxAttendee, SemName, numSess, typeof, eveID):
    user.post_event(newSem)
    Seminar_methods.add_seminar(system, newSem)

    s1 = Session('SessName', user, date_converter('2018-01-03'), 'Venue', 'SessDetails', 1, 0)
    newSem.add_sess(s1)
    Attendance.add_attending(user, newSem) #add to users own record list
    session_attendees.add_attendees(s1, user) #add to the session's list attendees
    newSem.listAttendee().append(user)     #aooe

    s2 = Session('SessName', user, date_converter('2018-01-04'), 'Venue', 'SessDetails', 1, 1)
    newSem.add_sess(s2)
    session_attendees.add_attendees(s2, user) #add to the session's list attendees
    '''


import torch
import regex
import logging
import numpy as np
from math import ceil

from multiprocessing import Pool as ProcessPool
from multiprocessing.util import Finalize

from ..retriever import TfidfDocRanker
from ..retriever import DocDB

from transformers import AutoModelForQuestionAnswering, AutoTokenizer

DEFAULTS = {
    'ranker': TfidfDocRanker,
    'db': DocDB,
    'reader_model': 'distilbert-base-cased-distilled-squad',
}

logger = logging.getLogger(__name__)

# ------------------------------------------------------------------------------
# Multiprocessing functions to fetch text
# ------------------------------------------------------------------------------

PROCESS_DB = None

def init(db_class, db_opts):
    global PROCESS_DB
    PROCESS_DB = db_class(**db_opts)
    Finalize(PROCESS_DB, PROCESS_DB.close, exitpriority=100)

def fetch_text(doc_id):
    global PROCESS_DB
    return PROCESS_DB.get_doc_text(doc_id)

# ------------------------------------------------------------------------------
# Main DrQA pipeline
# ------------------------------------------------------------------------------

class DrQATransformers(object):
    def __init__(
        self,
        reader_model=None,
        use_fast_tokenizer=True,
        group_length=500,
        batch_size=32,
        cuda=True,
        num_workers=None,
        db_config=None,
        ranker_config=None,
    ):
        """Initialize the pipeline.

        Args:
            reader_model: name of the Huggingface transformer QA model.
            use_fast_tokenizer: whether to use fast tokenizer
            batch_size: batch size when processing paragraphs.
            cuda: whether to use gpu.
            num_workers: number of parallel CPU processes to use for retrieving
            db_config: config for doc db.
            ranker_config: config for ranker.
            group_length: target size for squashing short paragraphs together.
                0 = read every paragraph independently
                infty = read all paragraphs together
        """
        assert use_fast_tokenizer == True, 'Current version only support models with fast tokenizer'
        self.batch_size = batch_size
        self.device = 'cuda' if cuda else 'cpu'
        self.num_workers = num_workers
        self.group_length = group_length

        logger.info('Initializing document ranker...')
        ranker_config = ranker_config or {}
        ranker_class = ranker_config.get('class', DEFAULTS['ranker'])
        ranker_opts = ranker_config.get('options', {})
        self.ranker = ranker_class(**ranker_opts)

        logger.info('Initializing document reader & tokenizer...')
        reader_model = reader_model or DEFAULTS['reader_model']
        self.reader = AutoModelForQuestionAnswering \
            .from_pretrained(reader_model) \
            .eval() \
            .to(self.device)
        self.need_token_type = self.reader.base_model_prefix not in {
            "xlm", "roberta", "distilbert", "camembert", "bart", "longformer"
        }
        tokenizer_kwargs = {}
        if self.reader.base_model_prefix in {'mobilebert'}:
            tokenizer_kwargs['model_max_length'] = self.reader.config.max_position_embeddings
        #
        self.tokenizer = AutoTokenizer.from_pretrained(reader_model, use_fast=use_fast_tokenizer, **tokenizer_kwargs)

        logger.info('Initializing document retrievers...')
        db_config = db_config or {}
        db_class = db_config.get('class', DEFAULTS['db'])
        db_opts = db_config.get('options', {})

        self.processes = ProcessPool(
            num_workers,
            initializer=init,
            initargs=(db_class, db_opts)
        )

    def _split_doc(self, doc):
        """Given a doc, split it into chunks (by paragraph)."""
        curr = []
        curr_len = 0
        for split in regex.split(r'\n+', doc):
            split = split.strip()
            if len(split) == 0:
                continue
            curr.append(split)
            curr_len += len(split)
            # Maybe group paragraphs together until we hit a length limit
            if len(curr) > 0 and curr_len > self.group_length:
                yield ' '.join(curr)
                curr = []
                curr_len = 0
        if len(curr) > 0:
            yield ' '.join(curr)

    def process(self, query, top_n=1, n_docs=5, return_context=False):
        """Run a single query."""

        # Rank documents for query.
        ranked = [self.ranker.closest_docs(query, k=n_docs)]
        all_docids, all_doc_scores = zip(*ranked)

        # Flatten document ids and retrieve text from database.
        # We remove duplicates for processing efficiency.
        flat_docids = list({d for docids in all_docids for d in docids})
        # did2didx = {did: didx for didx, did in enumerate(flat_docids)}
        didx2did = {didx: did for didx, did in enumerate(flat_docids)}
        doc_texts = self.processes.map(fetch_text, flat_docids)

        # Split and flatten documents. Maintain a mapping from doc (index in
        # flat list) to split (index in flat list).
        flat_splits = []
        didx2sidx = []
        sidx2didx = []
        for i, text in enumerate(doc_texts):
            splits = self._split_doc(text)
            didx2sidx.append([len(flat_splits), -1])
            for split in splits:
                flat_splits.append(split)
                sidx2didx.append(i)
            didx2sidx[-1][1] = len(flat_splits)

        # Tokenize
        inputs = self.tokenizer(
            [query]*len(flat_splits),
            flat_splits,
            padding=True,
            truncation='only_second',
            stride=96,
            return_overflowing_tokens=True,
            return_attention_mask=True,
            return_token_type_ids=self.need_token_type,
            return_offsets_mapping=True,
            return_tensors='pt',
        ).to(self.device)

        # Split batches
        n_examples = inputs.input_ids.shape[0]
        n_batch = ceil(n_examples / self.batch_size)
        batches = [i*self.batch_size for i in range(n_batch)] #[0,32,64,..]
        batches.append(n_examples)

        # Feed forward batches
        outputs = []
        for i in range(n_batch):
            with torch.no_grad():
                if self.need_token_type:
                    output = self.reader(
                        inputs.input_ids[batches[i]:batches[i+1]],
                        inputs.attention_mask[batches[i]:batches[i+1]],
                        token_type_ids=inputs.token_type_ids[batches[i]:batches[i+1]],
                    )
                else:
                    output = self.reader(
                        inputs.input_ids[batches[i]:batches[i+1]],
                        inputs.attention_mask[batches[i]:batches[i+1]],
                    )
                outputs.append(output)

        # Join batch outputs
        start_logits = torch.cat([o.start_logits for o in outputs], dim=0)
        end_logits = torch.cat([o.end_logits for o in outputs], dim=0)

        # decode start-end logits to span slice & score
        start, end, score, idx_sort = self.decode_logits(start_logits, end_logits, topk=top_n)

        # Produce predictions, take top_n predictions with highest score
        all_predictions = []
        for i in range(top_n):
            split_id = inputs.overflow_to_sample_mapping[idx_sort[i]].item()
            start_char = inputs.offset_mapping[idx_sort[i], start[i], 0].item()
            end_char = inputs.offset_mapping[idx_sort[i], end[i], 1].item()
            prediction = {
                'doc_id': didx2did[sidx2didx[split_id]],
                'span': flat_splits[split_id][start_char:end_char],
                'doc_score': float(all_doc_scores[0][sidx2didx[split_id]]),
                'span_score': float(score[i]),
            }
            if return_context:
                prediction['context'] = {
                    'text': flat_splits[split_id],
                    'start': start_char,
                    'end': end_char,
                }
            all_predictions.append(prediction)

        return all_predictions

    def decode_logits(self, start_logits, end_logits, topk=1, max_answer_len=None):
        """
        Take the output of any :obj:`ModelForQuestionAnswering` and generate score for each span to be the actual answer.

        In addition, it filters out some unwanted/impossible cases like answer len being greater than max_answer_len or
        answer end position being before the starting position. The method supports output the k-best answer through
        the topk argument.

        Args:
            start_logits (:obj:`tensor`): Individual start logits for each token. # shape: batch, len(input_ids[0])
            end_logits (:obj:`tensor`): Individual end logits for each token.
            topk (:obj:`int`): Indicates how many possible answer span(s) to extract from the model output.
            max_answer_len (:obj:`int`): Maximum size of the answer to extract from the model's output.
        Output:
            starts:  top_n predicted start indices
            ends:  top_n predicted end indices
            scores:  top_n prediction scores
            idx_sort:  top_n batch element ids
        """
        start = start_logits.cpu().numpy().clip(min=0.0)
        end = end_logits.cpu().numpy().clip(min=0.0)
        max_answer_len = max_answer_len or start.shape[1]

        # Compute the score of each tuple(start, end) to be the real answer
        outer = np.matmul(np.expand_dims(start, -1), np.expand_dims(end, 1))

        # Remove candidate with end < start and end - start > max_answer_len
        candidates = np.tril(np.triu(outer), max_answer_len - 1)

        #  Inspired by Chen & al. (https://github.com/facebookresearch/DrQA)
        scores_flat = candidates.flatten()
        if topk == 1:
            idx_sort = [np.argmax(scores_flat)]
        elif len(scores_flat) < topk:
            idx_sort = np.argsort(-scores_flat)
        else:
            idx = np.argpartition(-scores_flat, topk)[0:topk]
            idx_sort = idx[np.argsort(-scores_flat[idx])]

        idx_sort, starts, ends = np.unravel_index(idx_sort, candidates.shape)
        scores = candidates[idx_sort, starts, ends]

        return starts, ends, scores, idx_sort


"""
Author: poiroot
"""

import logging
from datetime import datetime, timedelta
from collections import defaultdict
from itertools import combinations

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error

from .metrics import Metric, getModelCoefs
from ..utils.load import load_data, load_model
from xgboost import XGBRegressor
from keras.models import Sequential
from sklearn.externals import joblib

logger = logging.getLogger(__name__)

class FortuneTeller:
    """
    The predict system

    Parameters
    ----------
    X : pandas DataFrame
        The forecast data

    name : string
        The config target name

    length : int
        The row number of history data

    mean : pandas DataFrame, shape [X.shape[1]]
        The mean of history data, i.e. the data seen by models

    std : pandas DataFrame, shape [X.shape[1]]
        The standard deviation of history data

    cov : pandas DataFrame, shape [X.shape[1], X.shape[1]]
        The covariance matrix of hostory data

    models :
        The models created by sklearn

    Attributes
    ----------
    r2 : float
        The maximum R^2 of models

    RI : float
        The confidence of predict value by system

    MD : float
        The mahalannobis distance of X

    DQIX : float
        The data quality of X computed by multivariate normal distribution

    ZScore : pandas DataFrame, shape [X.shape[1]]
        The z-score of X

    predicts : dict, shape [length of models], key : model, value : float
        The predictive values by models

    predict :float
        The predictive value by weighted average of models

    coefs : dict, shape [length of models], key : model, value : float
        The coefficients of each algorithms

    intercept : float
        The intercept of linear regression model

    error : float
        The mean absolute error of the linear regression model

    algo_name : list
        The algorithm name in config "algo_name"

    mae : float
        The mean absolute error of models

    threshold: float, optional, default 0
        Get predict values whose confidence index greater than threshold

    time : datetime object
        The predict time i.e. get feature time
    """

    def __init__(self, name, data_manager, sql):

        self.time = None
        self.X = None
        self.data_manager = data_manager
        self.config = data_manager.model_resource[name]
        self.name = name
        self.sql = sql
        self.length = self.config["train"].data.shape[0]
        self.mean = self.config["mean"]
        self.std = self.config["std"]
        self.cov = self.config["cov"]
        self.models = self.config["models"]
        self.r2 = max(self.config["algo_r2"])
        self.RI = 0
        self.MD = 0
        self.DQIX = 0
        self.ZScore = 0
        self.predicts = dict()
        self.predict = 0
        self.coefs = dict()
        self.intercept = 0
        self.error = 0
        self.algo_name = [name.split(".")[0] for name in self.config['algo_name']]
        for name in self.algo_name:
            self.coefs[name] = 1 / len(self.models)
        self.mae = 0
        self.threshold = self.config["threshold"]
        self.start_time = None
        self.target_data = None
        self.metric = Metric(self.config["train"].data, self.r2, self.config["confidence"])
        self.contribute = list()
        self.error_method = "mse"
        logger.info("Creating as instance of FortuneTeller")

    def tell(self, time):
        """
        The main method of system
        """
        self.start_time = self.start_time - timedelta(hours = 8)
        #為了能夠往回撈到lims的資料，多回推8小時

        X = self.data_manager.get_feature(self.name, time)
        self.time = time
        self.X = X
        self.MD, self.contribute = self.metric.mahalanobis(X=X, mean=self.mean, cov=self.cov, length=self.length)
        if self.config["confidence"] is True:
            self.DQIX = self.metric.quality(X=X, mean=self.mean, cov=self.cov, log=True)
            self.RI = self.metric.ri(forecast_DQIX=self.DQIX)
        logger.info("Compute indicator score")

        for name, model in zip(self.algo_name, self.models):
            if type(model) ==  type(XGBRegressor()):
                #model.get_booster().feature_names = list(X.columns)
                X = X.reindex(columns = model.get_booster().feature_names)
            if type(model) == type(Sequential()):
                X = X.values.reshape(1,4,12)
                print(X)

            predict = model.predict(X)
            print("scaled predict value:", predict)

            if type(model) == type(Sequential()):
                y_scaler = joblib.load("prep/TRL_Y_scaler_0719.sav")
                predict = y_scaler.inverse_transform(predict)

            print("your predict value :",predict)

            while True:
                if len(predict.shape) > 1:
                    predict = predict[0]
                else:
                    break
            self.predicts[name] = float(predict[0])
        logger.info("Predict")

        end_time = time
        logger.debug("target_data time: {0}~{1}".format(self.start_time, end_time))
        self.target_data = self.data_manager.get_target(self.name, self.start_time, end_time, ["SAMPLED_DATE", "RESULT_VALUE"])
        logger.info("Get target data")
        logger.debug("target_data length: {0}".format(self.target_data.shape[0]))

        self.coefs = self._updateModelCoefs(self.coefs)
        if self.config['revise'] is True:
            self._revise()

        if self.target_data.shape[0] >= self.config['revise_sample_times']:
            self.start_time = self.target_data['time'][1]

        self.predict = 0
        for name in self.algo_name:
            self.predict += self.coefs[name] * self.predicts[name]
        self.predict += self.intercept

        self.mae = self._calculateMAE()

    def _updateModelCoefs(self, weights):
        #print("START UPDATE COEF")
        #print("DATA SHAPE:", self.target_data.shape[0])
        if self.target_data.shape[0] < self.config["revise_sample_times"]:
            return weights

        error = dict()
        value = defaultdict(list)
        for name in self.algo_name:
            self.drop_index = list()
            for index in self.target_data.index:
                row = self.target_data.loc[index]
                predict_value = self._getPredictValue(row["time"],
                                                      name,
                                                      self.config["revise_minutes_low"],
                                                      self.config["revise_minutes_high"])
                if np.isinf(predict_value):
                    #print("np.isinf = T :",predict_value)
                    self.drop_index.append(index)
                    continue
                value[name].append(predict_value)
            #print(self.target_data)
            #print("DROP INDEX:", self.drop_index)

            true_value = self.target_data["value"].copy()
            true_value.drop(self.drop_index, inplace=True)
            #print("true_value :",true_value)
            #print("predict value :",value[name])
            error[name] = self._calculateError(true_value,
                                               value[name],
                                               method=self.error_method)
        #print("PREDICT ERROR :",error)

        if len(weights) == 1:
            return weights
        if sum(error.values()) == 0:
            return weights
        new_weights = getModelCoefs(error)
        #print("new_weights : ",new_weights )
        #print("END UPDATE COEF")
        return new_weights

    def _revise(self):
        """
        The method that revise the predict value by simple math

        Should check quality of y ??
        According LIMS time to get predict value then revise
        Because predict value is immediate

        """

        if self.target_data.shape[0] < self.config["revise_sample_times"]:
            return

        value = list()
        for index in self.target_data.index:
            row = self.target_data.loc[index]
            value.append(self._getPredictValue(row["time"],
                                               self.algo_name,
                                               self.config["revise_minutes_low"],
                                               self.config["revise_minutes_high"]))
        predict = list()
        for part in value:
            num = 0
            for i, algo in enumerate(self.algo_name):
                if not np.isinf(part[i]):
                    num += self.coefs[algo] * part[i]
            if num != 0:
                predict.append(num)
        true_value = self.target_data["value"].copy()
        true_value.drop(self.drop_index, inplace=True)
        self.error = self._calculateError(true_value, predict, method="mae")

        try:
            self.intercept = sum(true_value - predict) / len(predict)
        except ZeroDivisionError:
            self.intercept = 0
        logger.info("Revise")

    def _getPredictValue(self, lims_time, name=None, start_time=-30, end_time=0):
        """
        According the LIMS time and the time interval to get the mean of predictive value
        Should delete extreme predictive values??

        Parameters
        ----------
        lims_time : datetime
            The LIMS time

        name : string or list
            The algorithm name

        start_time : int, optional, default -30
            The start time of the time interval according LIMS time
            i.e. start time of the time interval = LIMS time + start_time

        end_time : int, optional, default 0
            The end time of the time interval according LIMS time
            i.e. end time of the time interval = LIMS time + end_time

        Returns
        -------
        value : float or list
            The mean of predictive values in the time interval
        """
        start = lims_time + timedelta(minutes=start_time)
        end = lims_time + timedelta(minutes=end_time)

        # get RTPMS data and mean it
        predict_df = self.sql.get_model_result(self.name, start, end)
        try:
            predict_df = predict_df[predict_df["conf_idx"] > self.threshold]
            if predict_df.shape[0] == 0:
                logger.debug("There is no {0} data's confidence index greater than threshold, Time: {1} ~ {2}".format(name, start, end))
                if type(name) is list:
                    return [np.inf for i in name]
                return np.inf
            if name is None:
                value = np.mean(predict_df["predict"])
            elif type(name) is list:
                value = list()
                for n in name:
                    value.append(np.mean(predict_df["predict_{0}".format(n)]))
            else:
                value = np.mean(predict_df["predict_{0}".format(name)])
        except KeyError:
            # There is no data in the database
            logger.error("No {0} data in database, Time: {1} ~ {2}".format(name, start, end))
            if type(name) is list:
                return [np.inf for i in name]
            return np.inf
        return value

    def _calculateError(self, true, predict, method="mse"):
        """
        Calculate error between true values and predictive values

        Parameters
        ----------
        true : list
            The true values

        predict : list
            The predictive values

        method : string, optional, default "mse"
            The method to calculate error
                "mse" : mean square error
                "mae" : mean absolute error
                "r2" : 1 - (R squared)
        """
        methods = set(["mse", "mae", "r2"])
        if method not in methods:
            raise ValueError("The method is invalid")
        if len(true) == 0 or len(predict) == 0:
            return 0

        if method == "mse":
            error = mean_squared_error(true, predict)
        elif method == "mae":
            error = mean_absolute_error(true, predict)
        elif method == "r2":
            error = 1 - r2_score(true, predict)

        return error

    def setStartTime(self, time):
        self.start_time = time

    def _calculateMAE(self):
        """
        Calculating MAE bases on the linear regression model and model difference
        """
        mae = self.error
        model_diff_mae = list()

        for indices in combinations(range(len(self.algo_name)), 2):
            diff = abs(self.predicts[self.algo_name[indices[0]]] - self.predicts[self.algo_name[indices[1]]])
            coefs = [abs(self.coefs[self.algo_name[indices[i]]]) for i in range(2)]
            ratio = min(coefs) / max(coefs)
            if ratio is np.nan:
                continue
            model_diff_mae.append(ratio * diff)
        try:
            mae += max(model_diff_mae)
        except ValueError:
            pass

        return mae

    def initialModelCoefs(self):
        time = datetime.now()
        logger.info("Intialize model coefs start")
        if self.config["target_source"] == "rtpms":
            self.target_data = self.data_manager.get_target(self.name, time - timedelta(seconds=5*self.config["predict_sleep_seconds"]), time, ["SAMPLED_DATE", "RESULT_VALUE"])
        else:
            self.target_data = self.data_manager.get_target(self.name, time - timedelta(days=1), time, ["SAMPLED_DATE", "RESULT_VALUE"])
        error = dict()
        value = defaultdict(list)
        drop_index = list()
        for index in self.target_data.index:
            row = self.target_data.loc[index]
            base_time = row["time"]
            times = [base_time + timedelta(minutes=i) for i in range(self.config["revise_minutes_low"],
                                                                     self.config["revise_minutes_high"],
                                                                     self.config["predict_sleep_seconds"] // 60)]
            predicts = defaultdict(list)
            for t in times:
                if t > time:
                    break
                feature = self.data_manager.get_feature(self.name, t)
                for name, model in zip(self.algo_name, self.models):
                    predict = model.predict(feature)
                    while True:
                        if len(predict.shape) > 1:
                            predict = predict[0]
                        else:
                            break
                    if self.config["confidence"] is True:
                        self.DQIX = self.metric.quality(X=feature, mean=self.mean, cov=self.cov, log=True)
                        self.RI = self.metric.ri(forecast_DQIX=self.DQIX)
                        if self.RI > self.threshold:
                            predicts[name].append(predict[0])
                    else:
                        predicts[name].append(predict[0])
            if len(predicts.keys()) == 0:
                drop_index.append(index)
            for name in predicts.keys():
                if len(predicts[name]) == 0:
                    drop_index.append(index)
                    break
                value[name].append(np.mean(predicts[name]))
        true_value = self.target_data["value"].copy()
        true_value.drop(drop_index, inplace=True)
        for name in self.algo_name:
            error[name] = self._calculateError(true_value,
                                               value[name],
                                               method=self.error_method)
        weights = dict()
        total_error = sum(error.values())
        if total_error == 0:
            logger.info("Initialize model coefs and intercept done by total error is zero")
            return self.coefs, 0
        if len(self.algo_name) == 1:
            name = self.algo_name[0]
            weights[name] = 1
        else:
            weights = getModelCoefs(error)
        logger.info("Initialize model coefs done")

        if true_value.shape[0] < self.config["revise_sample_times"]:
            return weights, 0

        predict = list()
        for i in range(len(true_value)):
            num = 0
            for key in value:
                num += weights[key] * value[key][i]
            predict.append(num)

        if self.config["revise"]:
            try:
                intercept = sum(true_value - predict) / len(predict)
            except ZeroDivisionError:
                intercept = 0
        else:
            intercept = 0
        logger.info("Initialize intercept done")
        return weights, intercept


from odoo import models, fields, api, exceptions
from datetime import date,datetime
from dateutil.relativedelta import relativedelta
from collections import OrderedDict
import xlsxwriter
from io import StringIO
import base64
import pandas as pd
import numpy as np
import logging

_logger = logging.getLogger(__name__)

class libro_mayor_reportes_chile(models.TransientModel):
    _inherit = 'wizard.reportes.chile'

    @api.multi
    def dic_libro_mayor(self):
        dic = OrderedDict([
                ('Fecha',''),
                ('Comprobante',''),
                ('Rut',''),
                ('Partner',''),
                ('Cuenta',''),
                ('Glosa',''),
                ('Documento',''),
                ('Debe',0.0),
                ('Haber',0.0),
                ('Saldo',0.0)
                ])
        return dic

    @api.multi
    def _libro_mayor_sql(self,wizard=False):
        if wizard:
            wiz = self.search([('id','=',wizard)])
        else:
            wiz = self
        company = wiz.company_id.id
        #if not (wiz.fecha_inicio or wiz.fecha_term):
        #    raise exceptions.Warning('Debe seleccionar al menos un periodo')
        #periodo = wiz.period_ids[0]
        fecha_inicio = wiz.fecha_inicio
        fecha_term = wiz.fecha_term
        #fiscal = periodo.fiscalyear_id.id
        #perids = set(wiz.period_ids.ids)
        #periodos= '('+",".join(map(str,perids))+')'
        cuentas = ''
        if wiz.acount_ids:
            cuentas = 'and aa.id in ('+",".join(map(str,set(wiz.acount_ids.ids)))+')'
        wiz.env.cr.execute("""
            SELECT
            null as Fecha,
            'Saldo Inicial' as comprobante,
            null,
            null,
            concat_ws(' - ', aa.code::text, aa.name::text) as cuenta,
            null,
            null,
            sum(aml.debit),
            sum(aml.credit)

            FROM
            account_move_line aml,
            account_account aa,
            account_move am

            WHERE
            aml.account_id=aa.id and
            aml.move_id=am.id and
            am.state='posted' and
            aml.company_id = %s and
            aml.date <= '%s'
            %s

            GROUP BY
            cuenta

            UNION ALL

            SELECT
            q1.fecha,
            q1.comprobante,
            q2.rut,
            q2.partner,
            q1.cuenta,
            q1.ref,
            q1.nombre,
            q1.debit,
            q1.credit

            FROM
            (
            SELECT

            aml.date as fecha,
            am.name as comprobante,
            concat_ws(' - ', aa.code::text, aa.name::text) as cuenta,
            aml.ref as ref,
            aml.name as nombre,
            aml.debit as debit,
            aml.credit as credit,
            aml.partner_id as partner_id

            FROM
            account_move_line aml,
            account_account aa,
            account_move am

            WHERE
            aml.account_id=aa.id and
            aml.move_id=am.id and
            am.state='posted' and
            aml.company_id = %s and
            aml.date >= '%s' and
            aml.date <= '%s'
            %s
            )q1

            LEFT JOIN

            (
            SELECT

            rp.id id,
            rp.document_number rut,
            rp.name partner

            FROM

            res_partner rp
            )q2

            ON
            q2.id=q1.partner_id

            ORDER BY
            cuenta, fecha NULLS FIRST, comprobante
            """ %(company,fecha_inicio,cuentas,company,fecha_inicio,fecha_term,cuentas))
        dic = wiz.dic_libro_mayor()
        lista = []
        docs = wiz.env.cr.fetchall()
        # cuentas = set([(record[9],record[4]) for record in docs])
        # for record in cuentas:
        #     lista += wiz._libro_mayor_saldo_inicial_sql(record[0],record[1])
        for record in docs:
            dicti = OrderedDict()
            dicti.update(dic)
            dicti['Fecha']=record[0]
            dicti['Comprobante']=record[1]
            dicti['Rut']=record[2]
            dicti['Partner']=record[3]
            dicti['Cuenta']=record[4]
            dicti['Glosa']=record[5]
            dicti['Documento']=record[6]
            dicti['Debe']=float(record[7])
            dicti['Haber']=float(record[8])
            lista.append(dicti)
        tabla = pd.DataFrame(lista)
        if not tabla.empty:
            tabla['Saldo'] = (tabla['Debe']-tabla['Haber'])
            tabla['Saldo'] = tabla.groupby('Cuenta')['Saldo'].transform(pd.Series.cumsum)
        return tabla


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

"""
Data Provider

DPED Dataset see:http://people.ee.ethz.ch/~ihnatova/

"""
import os
import numpy as np
from PIL import Image
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader

to_tensor = transforms.Compose([
    transforms.ToTensor()
])

class DPEDDataset(Dataset):

    def __init__(self, data_dir, phone='iphone'):

        self.phone_dir = data_dir + phone + '/'
        self.camera = data_dir+'canon/'

        self.num_images = len([name for name in os.listdir(self.phone_dir)
                               if os.path.isfile(os.path.join(self.phone_dir, name))])

        # Load Data
        self.input_images = []
        self.target_images = []
        for i in xrange(self.num_images):
            input_ = Image.open(self.phone_dir+'%s.jpg' % i)
            target_ = Image.open(self.camera+'%s.jpg' % i)

            self.input_images.append(to_tensor(input_))
            self.target_images.append(to_tensor(target_))

    def __len__(self):
        return self.num_images

    def __getitem__(self, idx):

        return self.input_images[idx], self.target_images[idx]


import time
from flask import Flask, jsonify
import random
app = Flask(__name__)
list0=['motor0','motor1','motor2','motor3','sensor0','sensor1','sensor2','sensor3']
list1=['motor4','motor5','motor6','sensor4','sensor5','sensor6','sensor7']
def random_s():
    dic = {}
    for i in range(8):
        dic[str(list0[i])] = str(random.randrange(2))
    return dic

def random_n():
    dic = {}
    for i in range(7):
        dic[str(list1[i])] = str(random.randrange(2))
    return dic

@app.route('/s/status/')
def status_s():
    d=random_s()
    return jsonify(d)

@app.route('/n/status/')
def status_n():
    d=random_n()
    return jsonify(d)

if __name__ == '__main__':
    app.run(port=5000)


# *-* encoding: utf-8 *-*
import argparse,json,os,sys,csv,datetime,re

def extractFollowerNetw(filein,fileout):
    counter_sk = 0
    counter_nofr = 0
    counter_nofl = 0
    counter_notw = 0
    counter_nousr = 0
    counter_line = 0
    counter_ok = 0

    writer = csv.writer(fileout,delimiter=',')
    writer.writerow(['ts','uid','friends_count','followers_count'])

    for line in filein.readlines():
        counter_line += 1
        if counter_line % 10000 == 0:
            print(counter_line,'lines treated')
        try:
            tw = json.loads(line)
        except:
            counter_sk += 1
            continue
        if 'twitter' not in tw.keys():
            counter_notw += 1
            continue

        d = [int(i) for i in re.findall(r'[0-9]+',tw['twitter']['created_at'])]
        dt = datetime.datetime(*d[0:6])
        ts = dt.timestamp()
        try:
            uid = int(tw['twitter']['user']['id'])
        except:
            counter_nousr +=1
            continue
        if 'friends_count' not in tw['twitter']['user'].keys():
            nfr = 0
            counter_nofr += 1
        else:
            nfr = int(tw['twitter']['user']['friends_count'])

        if 'followers_count' not in tw['twitter']['user'].keys():
            nfl = 0
        else:
            nfl = tw['twitter']['user']['followers_count']
        counter_ok +=1
        writer.writerow([ts,uid,nfr,nfl])

    print('Stats for file',filein.name)
    print('Total lines:\t\t',counter_line)
    print('Parsed correctly:\t',counter_ok)
    print('No json:\t\t',counter_sk)
    print('No tweet:\t\t',counter_notw)
    print('No users:\t\t',counter_nousr)
    print('No friends:\t\t',counter_nofr)
    print('No followers:\t',counter_nofl)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-i','--input',
            type=argparse.FileType('r'),
            default=sys.stdin,
            help="Input file, tweets, json format")
    parser.add_argument('-o','--output',
            type=argparse.FileType('w'),
            default=sys.stdout,
            help="Output file, csv, <usr,nbfriends,nbfollowers>")

    args = parser.parse_args()

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    extractFollowerNetw(args.input,args.output)

    args.input.close()
    args.output.close()

if __name__ == '__main__':
    main()


# metabolic_robustness/utils/normdist.py

from scipy.stats import truncnorm
from scipy.stats import norm
from math import sqrt

def truncnorm_pdf(x, mean, var, min, max):
    a, b = _truncnorm_params_transform(mean, var, min, max)
    val = truncnorm.pdf(
        x, a, b,
        loc = mean,
        scale = sqrt(var),
        )
    return(val)

def truncnorm_sample(mean, var, min, max):
    a, b = _truncnorm_params_transform(mean, var, min, max)
    val = truncnorm.rvs(
        a, b,
        loc = mean,
        scale = sqrt(var),
        )
    return(val)

def _truncnorm_params_transform(mean, var, min, max):
    a = (min - mean) / sqrt(var)
    b = (max - mean) / sqrt(var)
    return(a, b)

def norm_pdf(x, mean, var):
    val = norm.pdf(x, loc=mean, scale=sqrt(var))
    return(val)

def norm_sample(mean, var):
    val = norm.rvs(
        loc = mean,
        scale = sqrt(var)
        )
    return(val)