"""
    Copyright 2015 Impera

    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.

    Contact: bart@impera.io

    This module defines the Impera protocol between components. The protocol is marshaling and
    transport independent. Example support can be json over http and amqp
"""

from http import client
import logging
import sched
import socket
import threading
import time
import inspect
import urllib

from collections import defaultdict

import tornado.ioloop
import tornado.web
import tornado.gen
from impera import methods
from impera.config import Config

LOGGER = logging.getLogger(__name__)
_clients = {}
IMPERA_MT_HEADER = "X-Impera-tid"

class Result(object):
    """
        A result of a method call
    """
    def __init__(self, multiple=False, code=0, result=None):
        self._multiple = multiple
        if multiple:
            self._result = []
            if result is not None:
                self._result.append(result)
        else:
            self._result = result
        self.code = code
        self._callback = None

    def add_result(self, result):
        """
        Add a new result to an instance

        :param result: The result to store
        """
        assert(self._multiple)
        self._result.append(result)
        if self._callback:
            self._callback(self)

    def get_result(self):
        """
            Only when the result is marked as available the result can be returned
        """
        if self.available():
            return self._result
        raise Exception("The result is not yet available")

    def set_result(self, value):
        if not self.available():
            assert(not self._multiple)
            self._result = value
            if self._callback:
                self._callback(self)

    def available(self):
        if self._multiple:
            return len(self._result) > 0 is not None or self.code > 0
        else:
            return self._result is not None or self.code > 0

    def wait(self, timeout=60):
        """
            Wait for the result to become available
        """
        count = 0
        while count < timeout:
            time.sleep(0.1)
            count += 0.1

    result = property(get_result, set_result)

    def callback(self, fnc):
        """
            Set a callback function that is to be called when the result is ready. When multiple
            results are expected, the callback is called for each result.
        """
        self._callback = fnc

class Transport(threading.Thread):
    """
        This class implements a transport for the Impera protocol.

        :param end_point_name The name of the endpoint to which this transport belongs. This is used
            for logging and configuration purposes
    """
    __data__ = tuple()
    __transport_name__ = None
    __broadcast__ = False
    __network__ = True

    offline = False

    @classmethod
    def create(cls, TransportClass, endpoint=None):
        """
            Create an instance of the transport class or return None if the transport requires a network but we are operating
            in offline mode
        """
        if cls.offline and TransportClass.__network__:
            return None

        return TransportClass(endpoint)

    def __init__(self, endpoint=None):
        self.__end_point = endpoint
        super().__init__(name=self.id)
        self.daemon = True
        self._connected = False

    endpoint = property(lambda x: x.__end_point)

    def get_id(self):
        """
            Returns a unique id for a transport on an endpoint
        """
        return "%s_%s_transport" % (self.__end_point.name, self.__class__.__transport_name__)

    id = property(get_id)

    def start_endpoint(self):
        """
            Start the transport as endpoint
        """
        self.start()

    def stop_endpoint(self):
        """
            Stop the transport as endpoint
        """
        self.stop()

    def start_client(self):
        """
            Start this transport as client
        """
        self.start()

    def stop_client(self):
        """
            Stop this transport as client
        """
        self.stop()

    def start(self):
        """
            Start the transport as a new thread
        """
        super().start()

    def stop(self):
        """
            Stop the transport
        """
        self._connected = False

    def call(self, method, destination=None, **kwargs):
        """
            Perform a method call
        """
        raise NotImplementedError()

    def _decode(self, body):
        """
            Decode a response body
        """
        if body is not None and len(body) > 0:
            body = tornado.escape.json_decode(body)
        else:
            body = None

        return body

    def set_connected(self):
        """
            Mark this transport as connected
        """
        LOGGER.debug("Transport %s is connected", self.get_id())
        self._connected = True

    def is_connected(self):
        """
            Is this transport connected
        """
        return self._connected

class RESTHandler(tornado.web.RequestHandler):
    """
        A generic class use by the transport
    """
    def initialize(self, transport, config):
        self._transport = transport
        self._config = config

    def _call(self, args, kwargs, operation):
        """
            An rpc like call
        """
        self.set_header("Access-Control-Allow-Origin", "*")
        if operation.upper() not in self._config:
            allowed = ", ".join(self._config.keys())
            self.set_header("Allow", allowed)
            self.set_status(405, "%s is not supported for this url. Supported methods: %s" % (operation, allowed))
            return

        try:
            # create message that contains all arguments (id, query args and body)
            message = self._transport._decode(self.request.body)
            if message is None:
                message = {}

            for key, value in self.request.query_arguments.items():
                if len(value) == 1:
                    message[key] = value[0].decode("latin-1")
                else:
                    message[key] = [v.decode("latin-1") for v in value]

            if "id" in kwargs and (message is None or "id" not in message):
                message["id"] = kwargs["id"]

            # validate message against config
            config = self._config[operation][0]
            if "id" in config and config["id"] and "id" not in message:
                self.set_status(500, "Invalid request. It should contain an id in the url.")
                return

            if "mt" in config and config["mt"]:
                if IMPERA_MT_HEADER not in self.request.headers:
                    self.set_status(500, "This is multi-tenant method, it should contain a tenant id")
                    return

                else:
                    message["tid"] = self.request.headers[IMPERA_MT_HEADER]
                    if message["tid"] == "":
                        self.set_status(500, "%s header set without value." % IMPERA_MT_HEADER)
                        return

                    self.add_header(IMPERA_MT_HEADER, message["tid"])

            # validate message against the arguments
            argspec = inspect.getfullargspec(self._config[operation][2])
            args = argspec.args

            if "self" in args:
                args.remove("self")

            all_fields = set(message.keys())
            if argspec.defaults is not None:
                defaults_start = len(args) - len(argspec.defaults)
            else:
                defaults_start = -1

            for i in range(len(args)):
                arg = args[i]
                if arg not in message:
                    if defaults_start >= 0 and (i - defaults_start) < len(argspec.defaults) and \
                       argspec.defaults[i - defaults_start] is None:
                        # a default value of none is provided
                        message[arg] = None
                    else:
                        self.set_status(500, "Invalid request. Field '%s' is required." % arg)
                        return
                else:
                    all_fields.remove(arg)

            if len(all_fields) > 0 and argspec.varkw is None:
                self.set_status(500, ("Request contains fields %s " % all_fields) +
                                "that are not declared in method and no kwargs argument is provided.")
                return

            LOGGER.debug("Calling method %s on %s" % self._config[operation][1])
            method_call = getattr(self._config[operation][1][0], self._config[operation][1][1])

            result = method_call(**message)
            if result is None:
                raise Exception("Handlers for method calls should at least return a status code.")

            reply = None
            if isinstance(result, tuple):
                if len(result) == 2:
                    code, reply = result
                else:
                    raise Exception("Handlers for method call can only return a status code and a reply")

            else:
                code = result

            if reply is not None:
                if "reply" in self._config[operation][0] and self._config[operation][0]:
                    self.write(tornado.escape.json_encode(reply))
                    self.set_header("Content-Type", "application/json")

                else:
                    LOGGER.warn("Method %s returned a result although it is has not reply!")

            self.set_status(code)

        except Exception:
            LOGGER.exception("An exception occured")
            self.set_status(500)

        self.finish()

    @tornado.gen.coroutine
    def head(self, *args, **kwargs):
        self._call(operation="HEAD", args=args, kwargs=kwargs)

    @tornado.gen.coroutine
    def get(self, *args, **kwargs):
        self._call(operation="GET", args=args, kwargs=kwargs)

    @tornado.gen.coroutine
    def post(self, *args, **kwargs):
        self._call(operation="POST", args=args, kwargs=kwargs)

    @tornado.gen.coroutine
    def delete(self, *args, **kwargs):
        self._call(operation="DELETE", args=args, kwargs=kwargs)

    @tornado.gen.coroutine
    def patch(self, *args, **kwargs):
        self._call(operation="PATCH", args=args, kwargs=kwargs)

    @tornado.gen.coroutine
    def put(self, *args, **kwargs):
        self._call(operation="PUT", args=args, kwargs=kwargs)

    @tornado.gen.coroutine
    def options(self, *args, **kwargs):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Methods", "HEAD, GET, POST, PUT, OPTIONS, DELETE, PATCH")
        self.set_header("Access-Control-Allow-Headers", "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token, " +
                        IMPERA_MT_HEADER)

        self.set_status(200)
        self.finish()

class RESTTransport(Transport):
    """"
        A REST (json body over http) transport. Only methods that operate on resource can use all
        HTTP verbs. For other methods the POST verb is used.
    """
    __data__ = ("message", "blob")
    __transport_name__ = "rest"

    def __init__(self, endpoint):
        super().__init__(endpoint)
        self.set_connected()

    def _create_base_url(self, properties, msg=None):
        """
            Create a url for the given protocol properties
        """
        url = ""
        if "id" in properties and properties["id"]:
            if msg is None:
                url = "/%s/(?P<id>.*)" % properties["method_name"]
            else:
                url = "/%s/%s" % (properties["method_name"], msg["id"])

        elif "index" in properties and properties["index"]:
            url = "/%s" % properties["method_name"]
        else:
            url = "/%s" % properties["method_name"]

        return url

    def start_endpoint(self):
        """
            Start the transport
        """
        url_map = {}
        for method, method_handlers in self.endpoint.__methods__.items():
            properties = method.__protocol_properties__
            call = (self.endpoint, method_handlers[0])

            url = self._create_base_url(properties)

            if url not in url_map:
                url_map[url] = []

            url_map[url].append((properties, call, method.__wrapped__))

            # ##
#             argspec = inspect.getfullargspec(method_handlers[1])
#             args = argspec.args
#             if "id" in args:
#                 args.remove("id")
#             if "self" in args:
#                 args.remove("self")
#             print(properties["operation"], url, args, method_handlers[1])
            # ##

        handlers = []
        for url, configs in url_map.items():
            handler_config = {}
            for cfg in configs:
                handler_config[cfg[0]["operation"]] = cfg

            handlers.append((url, RESTHandler, {"transport": self, "config": handler_config}))
            LOGGER.debug("Registering handler(s) for url %s and methods %s" % (url, ", ".join(handler_config.keys())))

        port = 8888
        if self.id in Config.get() and "port" in Config.get()[self.id]:
            port = Config.get()[self.id]["port"]

        application = tornado.web.Application(handlers)
        application.listen(port)
        super().start()

    def run(self):
        LOGGER.debug("Starting tornado IOLoop")
        tornado.ioloop.IOLoop.instance().start()

    def stop_endpoint(self):
        super().stop()
        tornado.ioloop.IOLoop.instance().stop()

    def start_client(self):
        pass

    def stop_client(self):
        pass

    def _get_client_config(self):
        """
            Load the configuration for the client
        """
        LOGGER.debug("Getting config in section %s", self.id)
        port = 8888
        if self.id in Config.get() and "port" in Config.get()[self.id]:
            port = Config.get()[self.id]["port"]

        host = "localhost"
        if self.id in Config.get() and "host" in Config.get()[self.id]:
            host = Config.get()[self.id]["host"]

        LOGGER.debug("Using %s:%s", host, port)
        return (host, port)

    def call(self, properties, args, kwargs={}):
        host, port = self._get_client_config()
        conn = client.HTTPConnection(host, port)

        operation = properties["operation"]

        # create the message
        msg = kwargs

        # map the argument in arg to names
        argspec = inspect.getfullargspec(properties["method"])
        for i in range(len(args)):
            msg[argspec.args[i + 1]] = args[i]

        url = self._create_base_url(properties, msg)

        headers = {}
        if properties["mt"]:
            if "tid" not in msg:
                raise Exception("A multi-tenant method call should contain a tid parameter.")

            headers[IMPERA_MT_HEADER] = msg["tid"]
            del msg["tid"]

        if not (operation == "POST" or operation == "PUT" or operation == "PATCH"):
            qs_map = msg.copy()
            if "id" in qs_map:
                del qs_map["id"]

            # encode arguments in url
            if len(qs_map) > 0:
                url += "?" + urllib.parse.urlencode(qs_map)

            body = ""
        else:
            body = tornado.escape.json_encode(msg)

        LOGGER.debug("Calling server with url %s", url)

        try:
            conn.request(operation, url, body, headers)
            res = conn.getresponse()
        except Exception as e:
            return Result(code=500, result=str(e))

        return Result(code=res.status, result=self._decode(res.read()))

class DirectTransport(Transport):
    """
        Communicate directly within the same python process
    """
    __data__ = ("message", "blob")
    __transport_name__ = "direct"
    __network__ = False
    __broadcast__ = True

    _transports = defaultdict(list)
    connections = []

    @classmethod
    def register_transport(cls, transport):
        cls._transports[transport.endpoint.name].append(transport)

    def __init__(self, endpoint=None):
        super().__init__(endpoint)
        self.__class__.register_transport(self)
        self.set_connected()

    def call(self, method, destination=None, **kwargs):
        # select the correct transport
        targets = []
        for conn in self.connections:
            if conn[0] == self.endpoint.name:
                if conn[1] in self._transports:
                    targets.extend(self._transports[conn[1]])

        if targets is None or len(targets) == 0:
            raise Exception("Unable to find a direct transport to use.")

        # ##
        results = []
        for target in targets:
            if hasattr(target.endpoint, "__methods__"):
                method_call = None
                for method_name, method_class in target.endpoint.__methods__.items():
                    if method_class == method:
                        method_call = getattr(target.endpoint, method_name)
                        break

                operation = None
                if "operation" in kwargs:
                    operation = kwargs["operation"]

                if operation == "POST":
                    operation = None

                if method_call is None:
                    LOGGER.warning("Cannot find method call for operation.")
                    continue

                result = method_call(operation=operation, body=kwargs)

                if result is None:
                    LOGGER.error("Handlers for method calls should at least return a status code.")
                    continue

                reply = None
                if isinstance(result, tuple):
                    if len(result) == 2:
                        code, reply = result
                    else:
                        LOGGER.error("Handlers for method call can only return a status code and a reply")
                        continue

                else:
                    code = result

                if method.__reply__:
                    if reply is None:
                        body = {}
                    else:
                        body = reply

                    results.append((code, body))
                elif reply is not None:
                    LOGGER.warn("Method %s returned a result although it is has not reply!" % self._method_type)

        end_result = None
        if len(results) > 1:
            end_result = Result(multiple=True)
            for res in results:
                end_result.add_result(res)

        elif len(results) == 1:
            end_result = Result(code=results[0][0], result=results[0][1])

        return end_result

class handle(object):
    """
        Decorator for subclasses of an endpoint to handle protocol methods

        :param method A subclass of method that defines the method
        :param operation The operation to use (POST, GET, PUT, HEAD, ...)
        :param id Is the special parameter id required
        :param index Does this method handle the index
    """
    def __init__(self, method):
        self.method = method

    def __call__(self, function):
        """
            The wrapping
        """
        class_name = self.method.__qualname__.split('.<locals>', 1)[0].rsplit('.', 1)[0]
        module = inspect.getmodule(self.method)
        method_class = getattr(module, class_name)

        if not hasattr(method_class, "__method_name__"):
            raise Exception("%s should have a __method_name__ variable." % method_class)

        if "method_name" not in self.method.__protocol_properties__:
            self.method.__protocol_properties__["method_name"] = method_class.__method_name__

        function.__protocol_method__ = self.method
        return function

class EndpointMeta(type):
    """
        Meta class to create endpoints
    """
    def __new__(cls, class_name, bases, dct):
        if "__methods__" not in dct:
            dct["__methods__"] = {}

        methods = {}
        for name, attr in dct.items():
            if name[0:2] != "__" and hasattr(attr, "__protocol_method__"):
                if attr.__protocol_method__ in methods:
                    raise Exception("Unable to register multiple handlers for the same method.")

                methods[attr.__protocol_method__] = (name, attr)

        dct["__methods__"] = methods

        return type.__new__(cls, class_name, bases, dct)

class Scheduler(threading.Thread):
    """
        An event scheduler class
    """
    def __init__(self, daemon=True):
        super().__init__(name="Scheduler", daemon=daemon)
        self._sched = sched.scheduler()
        self._scheduled = set()
        self._running = True

    def add_action(self, action, interval, now=False):
        """
            Add a new action

            :param action A function to call periodically
            :param interval The interval between execution of actions
            :param now Execute the first action now and schedule subsequent invocations
        """
        LOGGER.debug("Scheduling action %s every %d seconds", action, interval)

        def action_function():
            LOGGER.info("Calling %s" % action)
            if action in self._scheduled:
                try:
                    action()
                except Exception:
                    LOGGER.exception("Uncaught exception while executing scheduled action")

                finally:
                    self._sched.enter(interval, 1, action_function)

        self._sched.enter(interval, 1, action_function)
        self._scheduled.add(action)

        if now:
            action()

    def remove(self, action):
        """
            Remove a scheduled action
        """
        if action in self._scheduled:
            self._scheduled.remove(action)

    def run(self):
        while self._running:
            self._sched.run(blocking=True)
            time.sleep(0.1)

    def stop(self):
        self._running = False

class Endpoint(object):
    """
        An end-point in the rpc framework
    """
    def __init__(self, name, role):
        self._name = name
        self._role = role
        self._node_name = self.set_node_name()
        self._end_point_names = []

    def get_end_point_names(self):
        return self._end_point_names

    def add_end_point_name(self, name):
        """
            Add an additional name to this endpoint to which it reacts and sends out in heartbeats
        """
        LOGGER.debug("Adding '%s' as endpoint", name)
        self._end_point_names.append(name)

    def clear_end_points(self):
        """
            Clear all endpoints
        """
        self._end_point_names = []

    name = property(lambda self: self._name)
    role = property(lambda self: self._role)
    end_point_names = property(get_end_point_names)

    def set_node_name(self):
        """
            This determines the name of the machine this endpoint is running on
        """
        node_name = Config.get("config", "node-name", None)
        if node_name is None:
            node_name = self._get_hostname()

        return node_name

    def _get_hostname(self):
        """
            Determine the hostname of this machine
        """
        return socket.gethostname()

    def get_node_name(self):
        return self._node_name

    node_name = property(get_node_name)

class ServerEndpoint(Endpoint, metaclass=EndpointMeta):
    """
        A service that receives method calls over one or more transports
    """
    __methods__ = {}

    def __init__(self, name, role, transport=RESTTransport):
        super().__init__(name, role)
        self._transport = transport
        self._transport_instance = None
        self._sched = Scheduler()

        self.running = True

    def schedule(self, call, interval=60):
        self._sched.add_action(call, interval)

    def start(self):
        """
            Start this end-point using the central configuration
        """
        LOGGER.debug("Starting transport for endpoint %s", self.name)
        self._transport_instance = Transport.create(self._transport, self)

        if self._transport_instance is not None:
            self._transport_instance.start_endpoint()

        LOGGER.debug("Starting scheduler")
        self._sched.start()

        while self.running:
            try:
                time.sleep(0.1)
            except KeyboardInterrupt:
                self.stop()

    def stop(self):
        """
            Stop the end-point and all of its transports
        """
        self.running = False
        if self._transport_instance is not None:
            self._transport_instance.stop_endpoint()
            LOGGER.debug("Stopped %s", self._transport_instance)

        self._sched.stop()

class ClientMeta(type):
    """
        A meta class that programs all protocol method call into the Client class.
    """
    def __new__(cls, class_name, bases, dct):
        classes = methods.Method.__subclasses__()  # @UndefinedVariable

        for mcls in classes:
            for attr in dir(mcls):
                attr_fn = getattr(mcls, attr)
                if attr[0:2] != "__" and hasattr(attr_fn, "__wrapped__"):
                    dct[attr] = attr_fn

        return type.__new__(cls, class_name, bases, dct)

class Client(Endpoint, metaclass=ClientMeta):
    """
        A client that communicates with end-point based on its configuration
    """
    def __init__(self, name, role, transport=RESTTransport):
        Endpoint.__init__(self, name, role)
        self._transport = transport
        self._transport_instance = None

        LOGGER.debug("Start transports for client %s", self.name)
        tr = Transport.create(self._transport, self)
        self._transport_instance = tr

    def is_transport_connected(self):
        """
            Check if all transports are connected
        """
        if self._transport_instance is None:
            return False

        return self._transport_instance.is_connected()

    def _call(self, args, kwargs, protocol_properties):
        """
            Execute the rpc call
        """
        method = protocol_properties["method"]
        class_name = method.__qualname__.split('.<locals>', 1)[0].rsplit('.', 1)[0]
        module = inspect.getmodule(method)
        method_class = getattr(module, class_name)

        if not hasattr(method_class, "__method_name__"):
            raise Exception("%s should have a __method_name__ variable." % method_class)

        protocol_properties["method_name"] = method_class.__method_name__

        return self._transport_instance.call(protocol_properties, args, kwargs)


import pathlib
import platform
import shutil
import textwrap
import unittest

import onlinejudge_verify.languages.cplusplus_bundle as cplusplus_bundle
import tests.utils
from onlinejudge_verify.languages.cplusplus_bundle import BundleError

@unittest.skipIf(platform.system() == 'Darwin', 'We cannot use the fake g++ of macOS.')
class TestStringMethods(unittest.TestCase):
    def test_no_newline(self) -> None:
        # 末尾に改行がないコードをincludeした時に改行が足されていることの確認

        files = {
            'no_newline.cpp': b'void foo() {}',
            'example.test.cpp': textwrap.dedent("""\
                #include "no_newline.cpp"
                #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_B"
                """).encode(),
        }
        path = pathlib.Path('example.test.cpp')
        with tests.utils.load_files(files) as tempdir:
            with tests.utils.chdir(tempdir):
                bundler = cplusplus_bundle.Bundler(iquotes=[tempdir])
                bundler.update(path)
                self.assertIn(b'void foo() {}\n', bundler.get())

    def test_uncommenting(self) -> None:
        # prepare files
        files = {
            'foo.cpp': textwrap.dedent("""\
                void foo() {}
                """).encode(),
            'bar.cpp': textwrap.dedent("""\
                void bar() {}
                """).encode(),
            'example.test.cpp': textwrap.dedent("""\
                #include "foo.cpp"
                /*
                #include "bar.cpp"
                */
                // #include "bar.cpp"
                """).encode(),
        }
        path = pathlib.Path('example.test.cpp')
        with tests.utils.load_files(files) as tempdir:
            with tests.utils.chdir(tempdir):

                # check the result
                bundler = cplusplus_bundle.Bundler(iquotes=[tempdir])
                bundler.update(path)
                bundled = bundler.get()
                self.assertIn(b'void foo() {}', bundled)
                self.assertNotIn(b'void bar() {}', bundled)

    @unittest.skipIf(shutil.which('clang++') is None, 'clang++ is required for this test')
    def test_reject_clang(self) -> None:
        files = {
            'example.test.cpp': b'',
        }
        path = pathlib.Path('example.test.cpp')
        with tests.utils.load_files(files) as tempdir:
            with tests.utils.chdir(tempdir):
                bundler = cplusplus_bundle.Bundler(iquotes=[tempdir], compiler='clang++')
                self.assertRaises(BundleError, lambda: bundler.update(path))


#!/usr/bin/env python3

import sys
import sqlite3
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error

def ape(y_true, y_pred):
    return np.abs(y_pred - y_true)/y_true

def se(y_true, y_pred):
    return (y_true - y_pred)**2

def ae(y_true, y_pred):
    return np.abs(y_true - y_pred)

if(len(sys.argv) != 2):
    print("Usage:\n\t" + sys.argv[0] + " <samples.db>")
    exit(-1)

print("Reading samples..")
conn = sqlite3.Connection(sys.argv[1])
samples = pd.read_sql_query(
    'select * from samples',
    conn, index_col=['bench','app','dataset','name'])

# Samples can be filtered by index
# This example get all entry in which the index 1 (app) is equal to 'kmeans'
# samples[samples.index.get_level_values(1) == 'kmeans']

X = samples[samples.columns[1:-1]]
y = samples['time']

print("Dividing into train and test set..")
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=17)

# Referr to https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html#sklearn.ensemble.RandomForestRegressor

###### Complete this section:

# regr =

##### End of Section

print("Train model..")
regr.fit(X_train, y_train)
y_predict = regr.predict(X_test)
print("Predicting:")
results = pd.DataFrame()
results['y_true'] = y_test
results['y_predict'] = regr.predict(X_test)
results['mse'] = se(y_test, results['y_predict'])
results['mae'] = ae(y_test, results['y_predict'])
results['mape'] = ape(y_test, results['y_predict'])
print(results)
print('Mean errors:')
print(results.mean())


'''
Created on Jun 27, 2016

@author: ubuntu
'''
import sqlite3 as database

class DBConnection(object):
    '''
    classdocs
    '''

    def __init__(self):
        self.__connection = None
        self.__cursor = None
        self.__database_name = "database.db"

        self.__createTables()

    def __createTables(self):

        query1 = '''CREATE TABLE IF NOT EXISTS devices
             (serial TEXT PRIMARY KEY NOT NULL)'''

        query2 = '''CREATE TABLE IF NOT EXISTS dev_values
             (serial TEXT PRIMARY KEY NOT NULL, dev_value TEXT NOT NULL)'''

        self._commit(query1)
        self._commit(query2)

    def __connect (self):
        self.__connection = database.connect(self.__database_name)
        self.__cursor = self.__connection.cursor()

        return self.__cursor

    def __disconnect(self):
        self.__cursor.close()
        self.__connection.close()

    def _commit(self,query):
        self.__cursor = self.__connect()
        self.__cursor.execute(query)
        self.__connection.commit()
        self.__disconnect()

        return self.__cursor

    def _query(self,query):
        self.__cursor = self.__connect()
        return self.__cursor.execute(query)


#выполнили Скибин и Александрова

import random

class battle:

    count = 0

    def __init__(self,name='player'): #создание персонажа
        self._stats=[100,[1,0],0,10,[0,0,0]] #статы, [здоровье,скорость,броня,патроны,усиления]
        battle.count += 1
        self._name = name
        self._position = [0,0]
        print('Добро пожаловать в битву, {0}!'.format(self._name))

    def get_stats(self): #получение характеристик персонажа
        print('Характеристики игрока {0}:\nЗдоровье - {1}\nСкорость - {2}\nБроня - {3}\nПатроны - {4}\nУсиления - Броня:{5}, Ускорение:{6}, Аптечка:{7}'.format(self._name,self._stats[0],self._stats[1][0],self._stats[2],self._stats[3],self._stats[4][0],self._stats[4][1],self._stats[4][2]))

    def get_position(self): #получение позиции персонажа
        print('Позиция игрока {0} - {1}'.format(self._name,self._position))

    def shot(self,pers2): #выстрел
        r = random.randint(0,1)
        self._stats[2] += -1
        if pers2._stats[2] != 0: #если у атакуемого персонажа есть броня, при попадании снимется она, а не здоровье
            pers2._stats[2] += -1
        else:
            pers2._stats[0] += -5*r
        print('Игрок {0}, выстрелил и{2} попал в игрока {1}'.format(self._name,pers2._name,' не' * (1-r)))

    def search(self): #поиск предметов
        _t = random.randint(0,5)
        _tools = ['броню','ускорение','аптечку','патроны','пустой ящик с патронами','поврежденную броню','пустую консервную банку']
        if _t <= 2:
            self._stats[4][_t] += 1
        elif _t == 3:
            self._stats[3] += random.randint (1,5)
        self._position[0] += 1
        self._position[1] += -1
        print('Игрок {0} нашел - {1}'.format(self._name,_tools[_t]))

    def use_aid(self): #использование аптечки
        self._stats[0] += 3
        self._stats[4][2] += -1
        print('Игрок {0} использовал аптечку'.format(self._name))
        self._position[0] += 1 #анимация*
        self._position[1] += -1 #анимация*

    def use_speed(self): #использование ускорения
        self._stats[1][0] += 1
        self._stats[1][1] += 3
        self._stats[4][1] += -1
        self._position[0] += 1 #анимация*
        self._position[1] += -1 #анимация*
        print('Игрок {0} использовал ускорение'.format(self._name))

    def use_armor(self): #использование брони
        self._stats[2] += 1
        self._stats[4][0] += -1
        self._position[0] += 1 #анимация*
        self._position[1] += -1 #анимация*
        print('Игрок {0} использовал броню'.format(self._name))

    def move(self): #передвижение
        if self._stats[1][0] == 1: #если у персонада нет ускорения, он будет двигаться медленно
            self._position[0] += 5
            self._position[1] += 5
        elif self._stats[1][0] != 1: #если ускорение есть, персонаж будет двигаться быстрее
            self._position[0] += 15
            self._position[1] += 15
            if self._stats[1][1] == 0: #ускорение дается лишь на время, с каждым ходом, действие ускорения заканчивается
                self._stats[1][0] += -1
            else:
                self._stats[1][1] += -1

print('создаем новых игроков:')
pers1 = battle()
pers2 = battle('воитель')

print('смотрим статистику:')
pers1.get_stats()

print('стреляем в другого игрока:')
pers1.shot(pers2)
print('статистика атакуемого игрока:')
pers2.get_stats()

print('ищем предметы:')
pers1.search()

print('можем применить что-нибудь из имеющихся усилителей, use_aid, use_soeed, use_armor')


import numpy as np
class falling_game(object):
    def __init__(self,size=(8,8),view=None,gravity=1,seed=0):
        if view == None:
            self.view = size
        else:
            self.view = view

        self.size = size
        self.gravity = gravity
        self.action_space = [-1,0,1]
        self.score_zone = (
            (self.size[0]//2)-1,
            (self.size[0]//2)+1)
        self.reward_multi = 10
        self.punish_multi = -10
        self.level_up = 2
        self.reset()
        self.total_move = 0
        self.seed = seed
        self.input_shape = self.game_state().shape
        return None

    def reset(self):
        self.grid = np.zeros(shape=self.size[::-1],dtype=np.int16)
        self.score = 100
        self.last_reward = None
        self.last_action = None
        self.done = False
        self.level = 1
        state = self.game_state()
        return state

    def tstep(self,action=0):
        self.action(action)
        for y in range(self.size[1])[::-1]:
            for x in range(self.size[0]):
                if self.grid[y][x] >= 1:
                    value = self.grid[y][x]
                    self.grid[y][x] = 0
                    if y < self.size[1]-1:
                        self.grid[y+self.gravity][x] = value
                    else:
                        if self.score_zone[0] <= x <= self.score_zone[1]:
                            if value > 0:
                                self.score += value * self.reward_multi
                            elif value < 0:
                                self.score += value * self.punish_multi

        for _ in range(np.random.randint(self.level+1)):
            self.place(value=np.random.randint(self.level*-1,self.level+1))
        if self.score <= 0:
            #self.done=True
            pass
        if self.score-100 > self.level**self.level_up:
            self.level += 1

        return self.game_state(), self.score, self.done

    def place(self,value=1,x=None):
        np.random.seed(self.seed)
        if x == None:
            x = (np.random.randint(0,self.size[0]-1) + self.total_move)%self.size[0]
        elif x > self.size[0]:
            raise IndexError("X value out of range")

        self.grid[0][x] = value

    def print_grid(self):
        for row in self.grid:
            for char in row:
                print(char,end=" ")
            print("\n",end="")
        print("* "*self.size[0])

    def action(self,action):
        action = int(round(action))
        if action > 1:
            action = 1
        elif action < -1:
            action = -1
        self.total_move += action
        if action != 0:
            for y in range(self.size[1])[::-1]:
                line_buffer = np.zeros(shape=self.size[0],dtype=np.int8)
                for x in range(self.size[0]):
                    line_buffer[(x + action)% self.size[0]] = self.grid[y][x]
                self.grid[y] = line_buffer

    def game_state(self):
        state = self.grid
        state = state.flatten()
        return state


#!/usr/bin/python3.4

import sys
from math import *

#########################
##         KEY         ##
#########################

def square_size():
    square = ceil(sqrt(len(sys.argv[2])))
    return square

def key_encrypt(size):
    key = []
    for i in range(len(sys.argv[2])):
        key.append(ord(sys.argv[2][i]))
    while i + 1 < size ** 2:
        key.append(0)
        i += 1
    ## Affichage clé
    k = 0
    for i in range(size):
        for j in range(size - 1):
            print (key[k], end = "\t")
            k += 1
        print(key[k])
        k += 1
    print ("\nEncrypted message :")
    return key

def key_decrypt(size):
    key = []
    for i in range(len(sys.argv[2])):
        key.append(ord(sys.argv[2][i]))
    while i + 1 < size ** 2:
        key.append(0)
        i += 1
    ## Affichage clé
    k = 0
    for i in range(size):
        for j in range(size - 1):
            print (key[k], end = "\t")
            k += 1
        print(key[k])
        k += 1
    print ("\nDecrypted message :")
    return key

#########################
##       ENCRYPT       ##
#########################

def set_message(size):
    message = []
    size_message = len(sys.argv[1]) % size
    length = len(sys.argv[1])
    for i in range(length):
        message.append(ord(sys.argv[1][i]))
    while size_message < size:
        message.append(0)
        size_message += 1
    print (message)
    return message

def mult_matrix(key, size):
    message = set_message(size)
    length = len(sys.argv[1]) + (len(sys.argv[1]) % size)
    i = 0
    j = 0
    k = 0
    print(length)
    for i in range(length):
    	print("", end="")

    # while i < size:
    #     while (j < size):
    #         key = 0
    #         while (k < size):
    #             message2[i][j] = message2[i][j] + message[i][k] * key[k][j]
    #             k = k + 1
    #         j = j + 1
    #     i = i + 1

#########################
##        EXECS        ##
#########################

def encrypt():
    print ("Key matrix :")
    mult_matrix(key_encrypt(square_size()), square_size())

def decrypt():
    print ("Key matrix :")
    mult_matrix(key_decrypt(square_size()), square_size())

#########################
##        MAIN         ##
#########################

if len(sys.argv) != 4:
    print ("Error : incorrect number of arguments")
    exit(84)

def main():
    if sys.argv[3] == '0':
        encrypt()
    elif sys.argv[3] == '1':
        decrypt()
    else:
        print ("Error : wrong flag")
        exit(84)

main()


import pdb

import argparse

try:
    from solverRuns import run_solver, get_finite_witness
    from utils.SimpleTree import Formula
except:
    from encoding.solverRuns import run_solver, get_finite_witness
    from encoding.utils.SimpleTree import Formula

try:
    from utils.Traces import Trace, ExperimentTraces
except:
    from encoding.utils.Traces import Trace, ExperimentTraces

import logging

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--log", dest="loglevel", default="INFO")
    parser.add_argument("--candidate_formula_1", default="F(a)")
    parser.add_argument("--candidate_formula_2", default="F(&(a,b))")
    parser.add_argument("--trace_length", default = 5)

    args, unknown = parser.parse_known_args()

    """
    traces is
     - list of different recorded values (traces)
     - each trace is a list of recordings at time units (time points)
     - each time point is a list of variable values (x1,..., xk)
    """

    numeric_level = args.loglevel.upper()
    logging.basicConfig(level=numeric_level)

    trace_length = int(args.trace_length)

    f_1 = Formula.convertTextToFormula(args.candidate_formula_1)
    f_2 = Formula.convertTextToFormula(args.candidate_formula_2)
    difference_formula = Formula([encodingConstants.LOR,
                                 Formula([encodingConstants.LAND, f_1, Formula([encodingConstants.LNOT, f_2])]),
                                 Formula([encodingConstants.LAND, Formula([encodingConstants.LNOT, f_1]), f_2])
                                  ])

    #difference_formula = Formula(["&", f_1, Formula([encodingConstants.LNOT, f_2])])
    #difference_formula = Formula(["&", f_2, Formula([encodingConstants.LNOT, f_1])])

    # it is weird trace_length = trace_length +1 --> a mistake in the encoding that was reasons about number of states, rather than number of actions
    counterexample_witness = get_finite_witness(f=difference_formula, trace_length=trace_length)

    if counterexample_witness == "unsat":
        logging.error("Formulas {} and {} are equivalent (at least for all traces of length {}".format(f_1, f_2, trace_length))
    else:

        if counterexample_witness.evaluateFormulaOnTrace(difference_formula) == False:
            raise RuntimeError("looking for witness of satisfiability, but got a trace {} that is not a model for {}".format(cex, difference_formula))

        logging.info("the distinguishing trace is {}".format(counterexample_witness))

if __name__ == "__main__":
    main()


"""
This file is created by Jeyvan Viriya (29802245) for Task_3 in FIT 1008 INTERVIEW PRAC 3 and this is just a further improve-
ments from Task_2 only with addition on the statistic function, and the testing section.
"""

import timeit
class HashTable:
    Primes = [3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
              1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, 17519, 21023,
              25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437, 187751, 225307, 270371,
              324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319, 2009191, 2411033,
              2893249, 3471899, 4166287, 4999559, 5999471, 7199369]

    def __init__(self, table_capacity, hash_base):
        self.capacity = table_capacity
        self.hash_base = hash_base
        self.array = [None] * self.capacity
        self.probe_list = []
        self.collision_count = 0
        self.probe_total = 0
        self.probe_max = 0
        self.rehash_count = 0

    def hash(self, key):
        h = 0
        a = self.hash_base
        for i in range(len(key)):
            h = (h * a + ord(key[i])) % self.capacity
        return h

    def __getitem__(self, key):
        index = self.hash(key)

        for _ in range(self.capacity):
            if self.array[index] is None:
                raise KeyError
            elif self.array[index][0] == key:
                return self.array[index][1]
            else:
                index = (index + 1) % self.capacity
        raise Exception("key not found")

    def __setitem__(self, key, data):
        index = self.hash(key)

        for _ in range(self.capacity):
            if self.array[index] is None:
                self.array[index] = (key, data)
                return
            elif self.array[index][0] == key:
                self.array[index] = (key, data)
                return
            else:
                index = (index + 1) % self.capacity
        raise Exception("hash table is full")

    def __contains__(self, item):
        theIndex = self.hash(item)
        for i in range(self.capacity):
            if self.array[theIndex][0] == item:
                return self.array[theIndex][1]
            if self.array[theIndex] is None:
                raise KeyError("The key is invalid")
        raise KeyError("Key do not exist")

    def rehash(self):
        retVal = False
        i = 0
        while not self.Primes[i] < (self.capacity * 2):
            self.capacity = self.Primes[i]
            retVal = True
            i += 1
        if retVal == False:
            raise ValueError

        newArray = [None] * self.capacity

        oldList = self.array
        self.array = newArray

        for pair in oldList:
            self.__setitem__(pair[0], pair[1])

    def statistic(self):
        """
        this function returns a tuple(collisioncount,probetotal, probemax, rehashcount), consisting of:
        (a) the total number of collisions,
        (b) the totallength of the probe chains,
        (c) the length of the longest probe chain, and
        (d) the number of timesrehash has been called.
        :return:the number of collisions, total length of the chains, the longest prob chain
        :pre-condition: collision must have been made
        :post-condition: the  items have been returned
        :best-case time complexity: O(1)
        :worst-case time complexity: O(1)

        """
        for i in self.probe_list:
            if i != 0:
                self.probe_total += i
                self.collision_count += 1

            if i > self.probe_max:
                self.probe_max = i

        return (self.collision_count, self.probe_total, self.probe_max, self.rehash_count)

def load_dictionary(hash_table, filename):
        openFile = open(filename , "r")
        for lines in openFile:
            lines = lines.strip()
            hash_table[lines] = 1
        openFile.close()

if __name__ == "__main__":
    textFilesList = ["english_large.txt","english_small.txt","french.txt"]
    numCom = [(27183,402221),(250726,1000081)]
    for i in range(len(textFilesList)):
        for j in range(len(numCom)):
            starttime = timeit.default_timer()
            load_dictionary(HashTable(numCom[j][1],numCom[j][0]),textFilesList[i])
            finishtime = timeit.default_timer()
            totalTime = finishtime-starttime
            print("The time needed to load "+textFilesList[i]+"in combination "+str(numCom[j])+" is "+str(totalTime))


# -*- coding: utf-8 -*-
###################################################################################
# Added to enable code completion in IDE's.
if 0:
    import gluon
    global auth; auth = gluon.tools.Auth()
    from gluon import T,request,response,URL,H2
    from applications.baadal.models import *  # @UnusedWildImport
###################################################################################
from helper import is_moderator, is_faculty, is_orgadmin

response.title = request.application
response.google_analytics_id = None

response.top_menu = [
    (T('About'), False, URL('default','index')),
    (T('Blog'), False, URL('default','page_under_construction')),
    (T('Photos'), False, URL('default','page_under_construction')),
    (T('Team Baadal'), False, URL('default','page_under_construction')),
    (T('Contact'), False, URL('default','page_under_construction'))
    ]
if auth.is_logged_in():
    response.user_menu = [
        (H2('USER MENU'),False, dict(_href='#', _id='menu_user')),
        (T('Home'), False, URL('default','index')),
        (T('Request VM'), False, URL('user','request_vm')),
        (T('My VMs'), False, URL('user','list_my_vm')),
        (T('My Tasks'), False, URL('user','list_my_task')),
        (T('Mail Admin'), False, URL('default','page_under_construction')),
        (T('Report Bug'), False, URL('default','page_under_construction'))
        ]

    if (is_moderator() | is_faculty()):
        response.faculty_menu = [
            (H2('FACULTY MENU'),False, dict(_href='#', _id='menu_faculty')),
            (T('Pending Requests {'+str(len(get_pending_requests()))+'} '), False, URL('faculty','pending_requests'))
            ]

        if (is_moderator() | is_orgadmin()):
            response.orgadmin_menu = [
                (H2('ORG-ADMIN MENU'),False, dict(_href='#', _id='menu_orgadmin')),
                (T('List All Org-Level VMs'), False, URL('orgadmin','list_all_orglevel_vm')),
                (T('Pending Org-Level VM Approvals {'+str(get_pending_approval_count())+'}'), False, URL('orgadmin','pending_approvals'))
                ]

            if is_moderator():
                response.admin_menu = [
                    (H2('ADMIN MENU'),False, dict(_href='#', _id='menu_admin')),
                    (T('All VM''s'), False, URL('admin','list_all_vm')),
                    (T('Host and VMs'), False, URL('admin','hosts_vms')),
                    (T('Tasks'), False, URL('admin','task_list')),
                    (T('Sanity Check'), False, URL('admin','sanity_check')),
                    (T('Configure System'), False,dict(_href='#', _id='configure'),[
                        (T('Configure Host'), False, URL('admin','host_details')),
                        (T('Configure Template'), False, URL('admin','add_template')),
                        (T('Configure Datastore'), False, URL('admin','add_datastore'))
                        ])
                    ]


class TreeNode(object):
     def __init__(self, x):
         self.val = x
         self.left = None
         self.right = None

def buildTree(inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """
        if inorder is None or postorder is None or len(inorder) != len(postorder):
            return None
        root = TreeNode(postorder[-1])
        n = inorder.index(postorder[-1])
        root.left = buildTree(inorder[:n], postorder[:n])
        root.right = buildTree(inorder[n+1:], postorder[n:-1])
        return root

29.从中序与后序遍历序列构造二叉树.py

29.从中序与后序遍历序列构造二叉树.py


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

def addInitialData(apps, schema_editor):
	AreaNews = apps.get_model('news', 'AreaNews')
	AreaNews.objects.bulk_create([
		AreaNews(
			title='Noticias',
			content= '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacus massa, scelerisque eget imperdiet at,
					accumsan tincidunt metus. Donec interdum sit amet risus vel dignissim. Donec ultrices cursus tortor, quis eleifend
					quam cursus in. Suspendisse dignissim tincidunt dui sed consequat. Aenean ultrices luctus laoreet. Nam egestas at
					leo sit amet malesuada.''',
			),
		])

class Migration(migrations.Migration):

    dependencies = [
        ('news', '0001_initial'),
    ]

    operations = [
    	migrations.RunPython(addInitialData)
    ]


'''Задан список из целых чисел. Определить количество участков списка, на котором
элементы монотонно возрастают (каждое следующее число больше предыдущего).'''
import random
l = [random.randrange(100) for i in range(10)]
amount =0
cont=0
for pos, elem in enumerate(l):
    if pos + 1 ==len(l):
        break
    if elem < l[pos+1]:
        cont +=1
        continue
    if cont > 0:
        cont =0
        amount +=1
if cont > 0:
    amount +=1
print(cont, amount)
print(l)


"""
Display line segments. Can be useful e.g. for visializing vector fields.
"""

import numpy as np

import pygfx as gfx

from PySide6 import QtWidgets
from wgpu.gui.qt import WgpuCanvas

app = QtWidgets.QApplication([])

canvas = WgpuCanvas()
renderer = gfx.WgpuRenderer(canvas)

scene = gfx.Scene()

x = np.linspace(20, 620, 200, dtype=np.float32)
y = np.sin(x / 10) * 100 + 200

positions = np.column_stack([x, y, np.zeros_like(x)])
colors = np.random.uniform(0, 1, (x.size, 4)).astype(np.float32)
geometry = gfx.Geometry(positions=positions, colors=colors)

# Also see LineSegmentMaterial and LineThinSegmentMaterial
material = gfx.LineSegmentMaterial(
    thickness=6.0, color=(0.0, 0.7, 0.3, 0.5), vertex_colors=True
)
line = gfx.Line(geometry, material)
scene.add(line)

camera = gfx.ScreenCoordsCamera()

if __name__ == "__main__":
    canvas.request_draw(lambda: renderer.render(scene, camera))
    app.exec()


from django.shortcuts import render
import os
import re
import csv, io, json
from django.http import JsonResponse
import pandas as df
from datetime import datetime, timedelta
import time

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from threading import Timer, Thread, Event

# Create your views here.

class TransactionList():

    def __init__(self):
        self.transactionsList = []
        self.dFrameTransaction = df.DataFrame(self.transactionsList)

    def initDatabaseForTransaction(self):
        path = "./"

        transactionsList = []

        for fname in os.listdir(path):

            if re.match(r"^Transaction_*", fname):
                with open(os.path.join(path, fname), 'r') as f:
                    csvReader = csv.DictReader(f)
                    for row in csvReader:
                        transactionObj = dict(row)
                        transactionObj = {
                            'productId' : int(transactionObj['productId']),
                            'transactionId': int(transactionObj['transactionId']),
                            'transactionAmount':float(transactionObj['transactionAmount']),
                            'transactionDatetime': df.to_datetime(transactionObj['transactionDatetime'])
                        }
                        transactionsList.append(transactionObj)
                print(fname)

        self.transactionsList = transactionsList

        self.dFrameTransaction = df.DataFrame(self.transactionsList)

        return self.transactionsList, self.dFrameTransaction

    def getTransaction(self, id):
        queryData = self.dFrameTransaction[(self.dFrameTransaction['transactionId'] == id)]
        transactionJson = json.loads(queryData.iloc[0].to_json())

        transactionJson['transactionDatetime'] = df.to_datetime(transactionJson['transactionDatetime'] / 1000.0,
                                                                unit='s') \
            .strftime("%Y-%m-%d %H:%M:%S")

        transactionJson['productName'] = productList.getProductNameById(queryData.iloc[0].productId)

        return transactionJson

    def getSummaryByCity(self, lastndays):
        queryData = self.dFrameTransaction[
            (self.dFrameTransaction['transactionDatetime'] > df.to_datetime(
                datetime.today() - timedelta(days=lastndays))) &
            (self.dFrameTransaction['transactionDatetime'] < df.to_datetime(datetime.now()))].groupby(
            'productId').sum()

        summaryByManufacturingCityList = []

        for index, row in queryData.iterrows():
            summaryByManufacturingCityListObj = {
                'cityName': productList.getProductCityById(index),
                'totalAmount': float(row['transactionAmount'])
            }
            summaryByManufacturingCityList.append(summaryByManufacturingCityListObj)

        return summaryByManufacturingCityList

    def getSummaryByProduct(self, lastndays):
        queryResp = self.dFrameTransaction[
            (self.dFrameTransaction['transactionDatetime'] > df.to_datetime(
                datetime.today() - timedelta(days=lastndays))) &
            (self.dFrameTransaction['transactionDatetime'] < df.to_datetime(datetime.now()))].groupby(
            'productId').sum()

        summaryByProductsList = []

        for index, row in queryResp.iterrows():
            summaryByProductsListObj = {
                'productName': productList.getProductNameById(index),
                'totalAmount': float(row['transactionAmount'])
            }
            summaryByProductsList.append(summaryByProductsListObj)

        return summaryByProductsList

transactionList = TransactionList()
transactionList.initDatabaseForTransaction()

class WatchdogHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print ('modifiedd')
        transactionList.initDatabaseForTransaction()

    def dispatch(self, event):
        print (event)
        transactionList.initDatabaseForTransaction()

class ProductList(object):
    def __init__(self):
        self.productsList = []
        self.dFrameProducts = df.DataFrame(self.productsList)

    def initDatabaseForProduct(self):
        path = "./"

        productList = []

        for fname in os.listdir(path):
            if fname == "ProductReference.csv":
                with open(os.path.join(path, fname), 'r') as f:
                    csvReader = csv.DictReader(f)
                    for row in csvReader:
                        productObj = dict(row)
                        productObj = {
                            'productId': int(productObj['productId']),
                            'productName': productObj['productName'],
                            'productManufacturingCity': productObj['productManufacturingCity']
                        }
                        productList.append(productObj)
                print(fname)

        self.productsList = productList
        self.dFrameProducts = df.DataFrame(self.productsList)

        return self.dFrameProducts

    def getProductNameById(self, productId):
        productDetails = self.dFrameProducts[(self.dFrameProducts['productId'] == productId)]
        productDetails = json.loads(productDetails.iloc[0].to_json())
        return productDetails['productName']

    def getProductCityById(self, productId):
        productDetails = self.dFrameProducts[(self.dFrameProducts['productId'] == productId)]
        productDetails = json.loads(productDetails.iloc[0].to_json())
        return productDetails['productManufacturingCity']

productList = ProductList()
productsDataFrame = productList.initDatabaseForProduct()

class PT(Thread):

    def __init__(self):
        Thread.__init__(self)

    def run(self):
        event_handler = WatchdogHandler()
        observer = Observer()
        observer.schedule(event_handler, path='./', recursive=False)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()

t = PT()
t.start()

def getProductManufacturingCity(productId):
    productDetails = productsDataFrame[(productsDataFrame['productId'] == productId)]
    productDetails = json.loads(productDetails.iloc[0].to_json())
    return productDetails['productManufacturingCity']

def transactionSummaryByManufacturingCity(request, lastndays):
    summaryByManufacturingCityList = transactionList.getSummaryByCity(lastndays)
    return JsonResponse({'summary': summaryByManufacturingCityList})

def transactionSummaryByProducts(request, lastndays):
    summaryByProductsList = transactionList.getSummaryByProduct(lastndays)
    return JsonResponse({'summary': summaryByProductsList})

def transaction(request, transactionId):
    transactionJson = transactionList.getTransaction(transactionId)
    return JsonResponse(transactionJson)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='BlogPost',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('datetime_created', models.DateTimeField(auto_now_add=True, null=True)),
                ('caption', models.CharField(default=b'', max_length=255)),
                ('body', models.TextField(default=b'')),
                ('publisher_id', models.IntegerField(default=0)),
                ('is_deleted', models.BooleanField(default=False)),
                ('views', models.IntegerField(default=0)),
                ('author', models.ForeignKey(related_name='blog_post_publisher', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ('datetime_created',),
            },
        ),
        migrations.CreateModel(
            name='Profile',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('date_joined', models.DateTimeField(auto_now_add=True, null=True)),
                ('hash', models.CharField(default=b'', max_length=128)),
                ('token', models.CharField(default=b'', max_length=128)),
                ('mail_confirmed', models.BooleanField(default=False)),
                ('first_name', models.CharField(default=b'', max_length=100)),
                ('last_name', models.CharField(default=b'', max_length=100)),
                ('is_deleted', models.BooleanField(default=False)),
                ('is_editor', models.BooleanField(default=False)),
                ('is_reporter', models.BooleanField(default=False)),
                ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]


# coding:utf-8

import math

# Определяем функцию, считающую расстояние между двумя точками, заданными координатами
def lonlat_distance(a, b):
    # длина дуги меридиана в 1 градус в метрах
    degree_to_meters_factor = 111 * 1000  # 111 километров в метрах
    a_lon, a_lat = a  # широта и долгота 1-й точки
    b_lon, b_lat = b  # широта и долгота 2-й точки

    # Берем среднюю по широте точку и считаем коэффициент для нее.
    # угол в радианах для средней точки по широте
    radians_lattitude = math.radians((a_lat + b_lat) / 2.)
    # косинус угла для средней точки
    lat_lon_factor = math.cos(radians_lattitude)

    # Вычисляем смещения в метрах по вертикали и горизонтали.
    # чем больше широта, тем меньше метров будет приходиться на 1 метр по долготе
    # разницу в градусах по долготе умножаем на косинус угла
    dx = abs(a_lon - b_lon) * degree_to_meters_factor * lat_lon_factor
    # разницу в градусах по широте умножаем на кол-во метров в 1 градусе
    dy = abs(a_lat - b_lat) * degree_to_meters_factor

    # Вычисляем расстояние между точками.
    distance = math.sqrt(dx * dx + dy * dy)

    return distance


"""
    api_main
"""

import random
import argparse
import time

from utils import Pid, get_logger
from hardware import Dynamo
from network import ClientHandler

random.seed()

def random_range(floor: float, ceil: float, point: int) -> float:
    """
        Create a 'random' value between the given values
        args:
            floor: The minimum value
            ceil: The maximum value
            point: The number of decimals after the floating point
    """
    return round(random.uniform(floor, ceil), point)

def main():
    """
        main
    """
    my_pid = Pid("cf_api")
    assert not my_pid.is_running()
    my_pid.set_pidfile()
    logger = get_logger(__name__)
    parser = argparse.ArgumentParser(description="API communication experiment")
    parser.add_argument('--range', '-r',
                        help="Production range (in Watt)",
                        nargs=2,
                        metavar=('min', 'max'),
                        type=float,
                        action='store',
                        default=(1, 5))
    parser.add_argument('--point', '-p',
                        help="Number of digits after the floating point",
                        type=int,
                        action='store',
                        default=2)
    args = parser.parse_args()
    client = ClientHandler()
    modules = [
        Dynamo(address, uuid) for address, uuid in
        ((0x2, "001:001:001"), (0x3, "001:001:002"))
        ]
    logger.info("Program started")
    #id_dict = client.get_module_id(tuple(dynamo.uuid for dynamo in modules))
    #for dynamo in modules:
    #    if dynamo.uuid in id_dict:
    #        dynamo.session_id = id_dict[dynamo.uuid]
    #        print("uuid: {}, session_id {}".format(dynamo.uuid,
    #                                               dynamo.session_id))
    #    else:
    #        print("missing session_id for module {}".format(dynamo.uuid))
    id_list = client.get_module_id(tuple(dynamo.uuid for dynamo in modules))
    if not id_list:
        logger.critical("Empty module ids. Execution stop.")
        return 1
    for it, dynamo in enumerate(modules):
        dynamo.session_id = id_list[it]
    logger.info("Initialisation done. Send production NOW.")
    execution = True
    prod_d = dict()
    while execution:
        prod_d.clear()
        for dynamo in modules:
            dynamo.add_prod(
                random_range(args.range[0], args.range[1], args.point))
            prod_d[dynamo.uuid] = dynamo.prod_sum()
        logger.debug(prod_d)
        ret = client.module_send_production(prod_d)
        time.sleep(1)
        if ret is None:
            execution = False
        else:
            for command in ret:
                if command["cmd"] == "setModuleId":
                    for param in command["params"]:
                        for dynamo in modules:
                            if dynamo.uuid == param['UUID']:
                                dynamo.session_id = param["moduleID"]

if __name__ == "__main__":
    main()


# http://www.reddit.com/r/dailyprogrammer/comments/2peac9/20141215_challenge_193_easy_a_cube_ball_cylinder/

import math

volume = float(input())
cube_root = math.pow(volume, 1.0/3.0)

cube_w, cube_h, cube_d = cube_root, cube_root, cube_root
sphere_r = math.pow(volume * 0.75 / math.pi, 1.0/3.0)
cylinder_h = cube_root
cylinder_r = math.sqrt((volume/cube_root)/math.pi)
cone_h = cube_root
cone_r = math.sqrt((3 * volume) / (math.pi * cone_h))

print('Cube: %.2fm wide, %.2fm high, %.2fm deep' % (cube_w, cube_h, cube_d))
print('Sphere: %.2fm radius' % sphere_r)
print('Cylinder: %.2fm high, %.2fm radius' % (cylinder_h, cylinder_r))
print('Cone: %.2fm high, %.2fm base radius' % (cone_h, cone_r))


import tkinter as tk

class MainTab(tk.Frame):
    def __init__(self, root, *args, **kwargs):
        tk.Frame.__init__(self, root, *args, **kwargs)
        self.root = root

        # Contents of MainTab below
        # "LED ON" Button
        self.btn_led_on = tk.Button(self, text="Write")
        self.btn_led_on.grid(row=0, column=0, ipadx=10, ipady=10)

        # "LED OFF" Button
        self.btn_led_off = tk.Button(self, text="Read")
        self.btn_led_off.grid(row=0, column=1, ipadx=10, ipady=10)


all_messages = []

def handle_message(message, nickname="user"):
    '''handles message:
    @message - text of recieved message
    @nickname - nickname of sender

    @returns - text of response
    '''
    global all_messages

    message_split = message.split()
    answer = "no command found"
    if message == "/show":
        answer = ""
        for message in all_messages:
            answer += message + '\n'
    else:
        all_messages.append(message)
        return message
    return answer

'''
if __name__ == "__main__":
    # dirty python magic, will talk about on the next lesson
    # just ignore for now

    nick = input("Enter your nickname: ")

    while True:
        msg = input("Your message: ")
        ans = handle_message(msg, nick)

        print(ans)
'''


import serial, sys

# open serial
ser = serial.Serial('/dev/ttyUSB0', 115200)

# get data
data = ser.readline()

# convert to decimal
data = int(data, 16)

# convert celsius to fahrenheit
data = 9.0/5.0 * data + 32

# store
data = str(data)
f = open('temp_data', 'w')
f.write(data)

f.close()
ser.close()


#!/usr/bin/env python3

import argparse
import json
import os
import multiprocessing
import subprocess
import sys

CUR_DIR = os.fspath(os.path.dirname(__file__))
DATA_DIR = f"{CUR_DIR}/../../data/"
INPUT = {
    'user-db': {
        'cpu-load': f"{DATA_DIR}/20200831_user-db_cpu-load_02.json",
        'network-latency': f"{DATA_DIR}/20200901_user-db_network-latency_01.json",
    },
    'shipping': {
        'cpu-load': f"{DATA_DIR}/20200902_shipping_cpu-load_01.json",
        'network-latency': f"{DATA_DIR}/20200902_shipping_network-latency_01.json",
    },
}

def log(msg):
    print(msg, file=sys.stderr)

def run(method, num_cores, output):
    for container, entries in INPUT.items():
        for anomaly, inputfile in entries.items():
            log(f"Running {method} test in case of {container} {anomaly} {inputfile} ...")

            cmdout = subprocess.Popen(f"{CUR_DIR}/../{method}.py --max-workers {num_cores} {inputfile}",
                    shell=True, stdout=subprocess.PIPE)
            jsonS, _ = cmdout.communicate()
            res = json.loads(jsonS)['metrics_dimension']
            before_num_metrics = res['total'][0]
            filtered_num_metrics = res['total'][1]
            last_num_metrics = res['total'][2]

            output[method]['reduction'].setdefault(container, {})
            output[method]['reduction'][container].setdefault(anomaly, {})
            output[method]['reduction'][container][anomaly]['before_num_metrics'] = before_num_metrics
            output[method]['reduction'][container][anomaly]['filtered_num_metrics'] = filtered_num_metrics
            output[method]['reduction'][container][anomaly]['last_num_metrics'] = last_num_metrics

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--method", help="'tsifter' or 'sieve'", default='all')
    parser.add_argument("--num-cores", help="number of CPU cores",
        type=int, default=multiprocessing.cpu_count())
    args = parser.parse_args()

    output = {
        'tsifter': {
            'reduction': {},
        },
        'sieve': {
            'reduction': {},
        }
    }

    if args.method in ['all', 'tsifter']:
        run('tsifter', args.num_cores, output)
    if args.method in ['all', 'sieve']:
        run('sieve', args.num_cores, output)

    json.dump(output, sys.stdout, indent=4)


#yolov4를 tf로 변환한 것을 사용하기 위한 것
import time
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
#GPU 메모리 증가 허용
if len(physical_devices) > 0:
    tf.config.experimental.set_memory_growth(physical_devices[0], True)
from absl import app
import core.utils as utils
from core.yolov4 import filter_boxes
from tensorflow.python.saved_model import tag_constants
from PIL import Image
import cv2
import numpy as np

#flask를 사용하기 위한 것
from flask import Flask, render_template, Response

framework = 'tf' #'tflite' # tf, tflite, trt
weights = './checkpoints/yolov4-416' #변환한 모델이 저장된 경로 적기
size = 416 # resize images to
tiny = False  # yolo-tiny인 경우 True 아니라면 False
model = 'yolov4' # yolov3 or yolov4
iou = 0.45 # iou threshold
score = 0.25 # score threshold (0.25 이상의 확률값이 나온 것만 탐지하기)

input_size = 416
webcam = cv2.VideoCapture(0) #webcam 사용
app = Flask(__name__)

#tf model load
saved_model_loaded = tf.saved_model.load(weights, tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']

def gen_frames():
    frame_id = 0
    while True:
        # 특정 조건을 만족하는 경우 카메라를 중지하고자 하는 경우 (아래 예시는 frame_id가 10인 경우)
        if (frame_id==10):
            webcam.release()
            break

        return_value, frame = webcam.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            if frame_id == webcam.get(cv2.CAP_PROP_FRAME_COUNT):
                print("Video processing complete")
                break
            raise ValueError("No image! Try with another video format")

        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)

        batch_data = tf.constant(image_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

        # 필요한 값들 계산
        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=iou,
            score_threshold=score
        )
        pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
        image = utils.draw_bbox(frame, pred_bbox)
        result = np.asarray(image)

        #각 물체가 몇%의 확률로 해당 물체라고 판별했는지 해당 물체를 판별한 시각을 출력
        object_num = -1
        flag = 0
        for i in scores.numpy()[0]:
            object_num += 1
            now_time = time.strftime('%Y'+'-'+'%m'+'-'+'%d'+'T'+'%H' + '-' + '%M' + '-' + '%S')
            if (i != 0):
                print(object_num, '번째 물체의 확률:', scores.numpy()[0][object_num], '시각:', now_time)
            else:
                if (object_num == 0):
                    flag = 1
                break

        result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        #이미지 저장
        if (flag == 0):
            cv2.imwrite("C:/Users/USER/Desktop/capture/" + now_time + ".png", result)

        frame_id += 1

        #webcam에서 찍고 있는 화면을 web상에서 보여줌.
        ret, buffer = cv2.imencode('.jpg', result)
        frame1 = buffer.tobytes()
        yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + frame1 + b'\r\n')

@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')

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


from soccersimulator import Strategy, SoccerAction, Vector2D, SoccerTeam
from soccersimulator import VolleySimulation, volley_show_simu,settings

from volleyball_question1 import SuperState,EchauffementStrategy
from volleyball_question2 import AttaquantStrategy

class DefenseurStrategy(Strategy):
    def __init__(self):
        Strategy.__init__(self, "defenseur")

    def compute_strategy(self, state, id_team, id_player):
        s= SuperState(state, id_team, id_player)

        action0=SoccerAction(acceleration=s.position_def-s.player)

        action1=SoccerAction(acceleration=s.ball-s.player)
        action3=SoccerAction(shoot=(s.position_oppose_opposant2-s.player).normalize()*0.5)
        action4=SoccerAction(shoot=(s.position_oppose_opposant2-s.player).normalize()*0.2)
        vec2=(s.position_oppose_opposant2-s.player).normalize()*0.5
        vec3=(s.position_oppose_opposant2-s.player).normalize()*0.2

        if((s.id_team==1 and s.ball.x>settings.GAME_WIDTH/2 and  s.ball.x<s.get_limite) or(s.id_team==2 and s.ball.x<settings.GAME_WIDTH/2 and  s.ball.x>s.get_limite) ):
            if(vec2.x<settings.GAME_WIDTH-10 and vec2.y<settings.GAME_HEIGHT-10 and vec2.x>10 and vec2.y>10):
                return action1+action3
            elif(vec3.x<settings.GAME_WIDTH-10 and vec3.y<settings.GAME_HEIGHT-10 and vec3.x>10 and vec3.y>10):
                return action1+action4
        else:
            return action0

    # Create teams
team1 = SoccerTeam(name="Team 1")
team2 = SoccerTeam(name="Team 2")

# Add players
team1.add("Player 1", AttaquantStrategy())
team2.add("Player 2", DefenseurStrategy())
# Create a match
simu = VolleySimulation(team1, team2)

# Simulate and display the match
volley_show_simu(simu)


import datetime
from picamera import PiCamera
from time import sleep
from sense_hat import SenseHat
import json

camera = PiCamera()
sense = SenseHat()
sense.clear()
img_counter = 0
img_name_counter = 1
interval_secs = 10 #camera
max_history = 72 #12 hours
max_dump = 1 #push to feed
data_dump = {}
sleep(2)

while True:
    data = {}
    temp = sense.get_temperature()
    humidity = sense.get_humidity()
    if img_counter == max_history:
        img_counter = 0
        img_name_counter = 1
    if img_counter % max_dump == 0:
        with open("/var/www/html/data/feed.json", "w") as write_file:
            json.dump(data_dump, write_file)
    camera.capture('/var/www/html/images/' + str(img_name_counter) + '.jpg')
    data['temp'] = temp
    data['img'] = '/images/' + str(img_name_counter) + '.jpg'
    data['hum'] = humidity
    data['time'] = str(datetime.datetime.now())
    json_data = json.dumps(data)
    data_dump[img_counter] = json_data
    print(json_data)
    img_counter = img_counter + 1
    img_name_counter = img_name_counter + 1
    sleep(interval_secs)


#!/usr/bin/env python

"""
This script looks into a working directory and
runs/checks/removes any jobs inside that directory.
If a job(jsonlike) is found but no jobdir present, script will submit to queue.
If a job is found and a jobdir is present:
  - if results are found, job was successful.
      - copy and upload log file (redundant but might be useful to have at toplevel)
      - tar, copy and upload results
      - tar and copy results to local results dir
      - remove job/log/results from working directory.
  - if no results found:
    - if job log is "done", job failed.
      - copy and upload log file
      - copy and upload job file
      - tar and copy results directory to local results dir for debug
      - remove job/log/results from working directory
    - if job log is not "done", job is still running.
      - copy and upload current progress (log)
"""
import argparse
import os
import glob
import shutil
import errno
import subprocess
from qtools import Submitter
import tarfile
import boto3
import botocore
import logging
import time
import sys
import re

import download_rawdata as dr

__version__ = '0.0.1'

PROFILE = 'ncrcrg.u19@gmail.com'

def make_tarfile(output_filename, source_dir, logger):
    """
    Generates tar.gz file from source_dir.

    :param output_filename: basestring
        typically ${SRC}.tar.gz
    :param source_dir: basestring
        the directory to be tarballed
    :param logger: logging.Logger()
        Logger object
    :return:
    """
    logger.info("Making tar file (may take awhile..)")
    if not os.path.exists(output_filename):
        logger.info("{} doesn't exist, generating tar file.".format(output_filename))
        with tarfile.open(output_filename, "w:gz") as tar:
            tar.add(source_dir, arcname=os.path.basename(source_dir))
    else:
        logger.info("{} exists, will not overwrite.".format(output_filename))
    logger.info("Done writing tar file ({})".format(output_filename))
    logger.info("Sleeping for 15s.")
    time.sleep(15)

def remove(to_remove, logger):
    """
    Removes a directory or file using shutil or os.

    :param to_remove: basestring
        the directory/file to remove
    :return:
    """
    try:
        logger.info("Removing {}".format(to_remove))
        shutil.rmtree(to_remove)
    except OSError:
        logger.info("Removing {}".format(to_remove))
        os.remove(to_remove)

def remove_file(to_remove, logger):
    """
    Remove a single file using os.remove().

    :param to_remove: basestring
    :param logger:
    :return:
    """

    logger.info("Removing {}".format(to_remove))
    if os.path.exists(to_remove):
        os.remove(to_remove)
    else:
        logger.warning("Warning, {} does not exist (and so will not remove)".format(to_remove))
    time.sleep(1)

def main():
    """
    Main program.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--project_dir",
        required=False,
        type=str,
        default="/projects/ps-yeolab4/NCRCRG"
    )
    parser.add_argument(
        "--user_dir",
        required=False,
        type=str,
        default="home"
    )
    parser.add_argument(
        "--results_dir",
        required=False,
        type=str,
        default="results_dir"
    )
    parser.add_argument(
        "--log_dir",
        required=False,
        type=str,
        default="logs"
    )
    parser.add_argument(
        "--bucket",
        required=False,
        type=str,
        default="u19"
    )

    # Process args
    args = parser.parse_args()
    project_dir = args.project_dir
    bucket = args.bucket
    work_dir = args.work_dir
    results_dir = args.results_dir

    log_dir = os.path.join(project_dir, args.log_dir)
    user_dir = os.path.join(project_dir, args.user_dir)

    logger = logging.getLogger('status')
    logger.setLevel(logging.INFO)

    ih = logging.FileHandler(os.path.join(log_dir, dr.append_date('status.txt')))
    ih.setLevel(logging.INFO)
    logger.addHandler(ih)

    eh = logging.FileHandler(os.path.join(log_dir, dr.append_date('status.err')))
    eh.setLevel(logging.ERROR)
    logger.addHandler(eh)

    wh = logging.FileHandler(os.path.join(log_dir, dr.append_date('status.txt')))
    wh.setLevel(logging.WARNING)
    logger.addHandler(wh)

    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ih.setFormatter(formatter)
    eh.setFormatter(formatter)
    logger.info("starting program")

    users = glob.glob(os.path.join(user_dir, '*'))
    for user in users:
        user_work_dir = os.path.join(user, work_dir)
        user_results_dir = os.path.join(user, results_dir)

if __name__ == "__main__":
    main()


import numpy as np
import matplotlib.pyplot as plt
import cv2
import pywt
from skimage.feature import greycomatrix, greycoprops

oil_locations = [(230, 290), (257, 303), (273, 336), (311, 336)]
ocean_locations = [(344, 234), (397, 118), (43, 305), (427, 302)]

rgb_img = cv2.imread("./image/article.jpg")
gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY)
rgb_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2RGB)

ocean_crops = []
oil_crops = []
rgb_crops = []
crop_offset = 21

for loc in oil_locations:
  crop = gray_img[loc[1]:loc[1] + crop_offset, loc[0]:loc[0] + crop_offset]
  oil_crops.append(crop)
  cv2.rectangle(rgb_img, (loc[0], loc[1]), (loc[0] + crop_offset,loc[1] + crop_offset), color = (0, 0, 255), thickness = 3)

for loc in ocean_locations:
  crop = gray_img[loc[1]:loc[1] + crop_offset, loc[0]:loc[0] + crop_offset]
  ocean_crops.append(crop)
  cv2.rectangle(rgb_img, (loc[0], loc[1]), (loc[0] + crop_offset,loc[1] + crop_offset), color = (255, 0, 0), thickness = 3)

# display original image with locations of patches
# ax = fig.add_subplot(9, 1, 1)
# ax.imshow(rgb_img)
# ax.set_xlabel('Original Image')
# ax.set_xticks([])
# ax.set_yticks([])
# ax.axis('image')

# Wavelet transform of image, and plot approximation and details
titles = ['Approximation', ' Horizontal detail', 'Vertical detail', 'Diagonal detail']

coeffs2 = pywt.dwt2(gray_img, 'bior1.3')
LL, (LH, HL, HH) = coeffs2
fig = plt.figure(figsize=(12, 3))

for i, a in enumerate([LL, LH, HL, HH]):
  ax = fig.add_subplot(1, 4, i + 1)
  ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
  ax.set_title(titles[i], fontsize=10)
  ax.set_xticks([])
  ax.set_yticks([])

fig.tight_layout()
plt.show()


from PyQt4 import QtGui, uic
import signal

from .context import Context

signal.signal(signal.SIGINT, signal.SIG_DFL)

class NewCrateForm(QtGui.QWidget):

    def __init__(self,parent = None):

        super(NewCrateForm, self).__init__(parent)
        uic.loadUi('newCrate.ui', self)

        self.sectorList = Context.getSectorList()
        self.sector.addItems(self.sectorList)
        self.sector.setCurrentIndex(-1)

        self.crateTypeList = Context.getCrateTypeList()
        self.type.addItems(self.crateTypeList)
        self.type.setCurrentIndex(-1)

        self.childsTitle.hide()
        self.childsLine.hide()
        self.slotsLabel.hide()
        self.slots.hide()

    def save(self):

        print((self.name.text()))
        print((self.node.text()))
        print((self.owner.text()))
        print((self.desc.toPlainText()))
        print((self.type.currentText()))
        print((self.manufactor.text()))
        print((self.model.text()))
        print((self.rack.text()))
        print((self.building.text()))
        print((self.elevation.text()))
        print((self.sector.currentText()))

class EditCrateForm(NewCrateForm):

    def __init__(self,crate,parent = None):
        super(EditCrateForm, self).__init__(parent)

        self.crate = crate

        self.vLine.hide()
        self.hLine.hide()

        self.title.setText(crate['name'])

        self.name.setText(crate['name'])
        self.node.setText(crate['link'])
        self.owner.setText(crate['owner'])
        self.desc.setPlainText(crate['description'])
        self.type.setCurrentIndex(self.type.findText(crate['type']))
        self.manufactor.setText(crate['manufactor'])
        self.model.setText(crate['model'])
        self.rack.setText(crate['location']['rack'])
        self.building.setText(crate['location']['building'])
        self.elevation.setText(crate['location']['elevation'])
        self.sector.setCurrentIndex(self.sector.findText(crate['location']['sector']))

    def save(self):

        print((self.name.text()))
        print((self.node.text()))
        print((self.owner.text()))
        print((self.desc.toPlainText()))
        print((self.type.currentText()))
        print((self.manufactor.text()))
        print((self.model.text()))
        print((self.rack.text()))
        print((self.building.text()))
        print((self.elevation.text()))
        print((self.sector.currentText()))

    def delete(self):
        pass

    def cancel(self):
        self = EditCrateForm(self.crate)


from mrjob.job import MRJob
import re
import os

WORD_RE = re.compile(r"[\w']+")

class MRWordLength(MRJob):
    def mapper(self, _, line):
        filename = os.environ['map_input_file'].split("/")[-1]
        for word in WORD_RE.findall(line):
            yield filename, len(word)

    def combiner(self, filename, lengths):
        total = 0
        count = 0
        for word_len in lengths:
            total += word_len
            count += 1
        yield filename, (total, count)

    def reducer(self, filename, vals):
        total_total = 0
        total_count = 0
        for total, count in vals:
            total_total += total
            total_count += count
        yield filename, total_total / float(total_count)

if __name__ == '__main__':
    MRWordLength.run()