from datetime import datetime, timezone, timedelta
import math
import discord
from typing import Union

def sgn(num):
    if num > 0:
        return 1
    if num == 0:
        return 0

    return -1

class Time:
    @staticmethod
    def get_info(mode: str = 'custom', custom_form: str = '') -> Union[str, int, None]:
        dt1 = datetime.utcnow().replace(tzinfo=timezone.utc)
        dt2 = dt1.astimezone(timezone(timedelta(hours=8)))  # 轉換時區 -> 東八區

        if mode == 'custom':
            return str(dt2.strftime(custom_form))

        if mode == 'whole':
            return str(dt2.strftime("%Y-%m-%d %H:%M:%S"))
        if mode == 'main':
            return str(dt2.strftime("%Y-%m-%d"))
        if mode == 'vice':
            return str(dt2.strftime("%H:%M:%S"))

        if mode == 'hour':
            return int(dt2.strftime("%H"))
        if mode == 'week_day':
            return int(dt2.isoweekday())
        if mode == 'day_of_week':
            return str(dt2.strftime("%A"))

        return None

    @staticmethod
    def get_range(hour) -> str:
        morning = range(6, 12)
        noon = range(12, 13)
        after_noon = range(13, 18)
        evening = range(18, 24)
        night = range(0, 6)

        if hour in morning:
            return 'morning'
        if hour in noon:
            return 'noon'
        if hour in after_noon:
            return 'after_noon'
        if hour in evening:
            return 'evening'
        if hour in night:
            return 'night'

        return 'morning'

class FluctMath:
    @staticmethod
    async def lvl_ind_calc(log, member_week_count, contrib, avr_contrib) -> float:
        theta1 = sgn(contrib - avr_contrib)

        active_days = sum(map(int, log))
        theta2 = sgn(active_days - (1 / 2) * member_week_count)

        return float(
            -sgn(theta1 + theta2) * abs((contrib - avr_contrib) * (theta1 + theta2)) / 2
        )

    @staticmethod
    def score_weight_update(t_score, avr_score, max_score, min_score) -> float:

        if max_score - min_score == 0:
            alpha = (t_score - avr_score)
        else:
            alpha = (t_score - avr_score) / (max_score - min_score)

        pt1 = float(1 / 2)
        pt2 = float(3 / (2 * (1 + pow(math.e, -5 * alpha + math.log(2)))))
        return pt1 + pt2

class DiscordExt:
    @staticmethod
    async def create_embed(title, thumbnail, color, fields_name, values) -> discord.Embed:
        if thumbnail == 'default':
            thumbnail = 'https://i.imgur.com/MbzRNTJ.png'

        embed = discord.Embed(title=title, color=color)
        embed.set_thumbnail(url=thumbnail)
        if len(fields_name) != len(values):
            embed.add_field(name="Error", value='N/A', inline=False)
            return embed

        for (fn, vl) in zip(fields_name, values):
            embed.add_field(name=fn, value=vl, inline=False)

        embed.set_footer(text=Time.get_info('whole'))
        return embed

    @staticmethod
    async def get_member_nick_name(guild, member_id) -> str:
        member_name = guild.get_member(member_id).nick
        if member_name is None:
            member_name = guild.get_member(member_id).name

        return member_name

class ProgressDisplay:
    def __init__(self, channel: discord.TextChannel, content, total):
        self.channel = channel
        self.content = content
        self.total = total
        self.message = None

    async def active(self):
        message = await self.channel.send(f'{self.content} (0/{self.total})')
        self.message = message

    async def display(self, progress):
        if self.message is not None:
            await self.message.edit(content=f'{self.content} ({progress}/{self.total})')


import logging

__author__ = 'Eric_Xie'

class LoggingDemo(object):

    def __init__(self):
        super(LoggingDemo, self).__init__()

    def add_file_handler(self, log_file, level):
        file_format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
        logging.basicConfig(level=level,
                            format=file_format,
                            filename=log_file,
                            filemode='w')


import json
import os
import random

#import GPUtil
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.python.estimator.canned.dnn import DNNClassifier
from tensorflow.python.estimator.run_config import RunConfig
from tensorflow.python.estimator.training import TrainSpec, EvalSpec, train_and_evaluate

#os.environ['CUDA_VISIBLE_DEVICES'] = str(GPUtil.getFirstAvailable()[0])
tf.logging.set_verbosity(tf.logging.INFO)

#print ("module %s loaded" % module_url)

train_fp = ['./questiondata/train_5500-UTF8.label']
eval_fp = ['./questiondata/TREC_10-UTF8.label']

batch_size = 128

# file1 = open('./questiondata/train_1000.label', 'r', encoding = "ISO-8859-1")
# Lines = file1.readlines()
# myset = set()
# for line in Lines:
#   splited = line.split(" ")
#   myset.add(splited[0])
# print(myset)

# hardcoded question-type
# question_classes = ['abb','exp','animal','body','color','cremat','currency',
#                     'dismed','event','food','instru','lang','letter','other',
#                     'plant','product','religion','sport','substance','symbol','techmeth',
#                     'termeq','veh','word','def','manner','reason','gr',
#                     'ind','title','desc','city','country','mount','other','state',
#                     'code','count','date','dist','money','order','other','period','percent',
#                     'speed','temp','size','weight']

question_classes = ['ENTY:other', 'NUM:dist', 'HUM:gr', 'NUM:speed', 'ENTY:instru', 'ENTY:product', 'ENTY:letter',
                  'ENTY:veh', 'ENTY:color', 'LOC:other', 'NUM:period', 'ENTY:event', 'ENTY:sport', 'ENTY:animal', 'LOC:country',
                  'ENTY:symbol', 'NUM:count', 'HUM:ind', 'DESC:reason', 'ENTY:plant', 'HUM:desc', 'DESC:def', 'ENTY:religion',
                  'ABBR:exp', 'ENTY:body', 'ENTY:word', 'ENTY:termeq', 'ENTY:lang', 'NUM:perc', 'ENTY:cremat', 'LOC:state',
                  'ENTY:techmeth', 'ENTY:food', 'NUM:ord', 'NUM:volsize', 'HUM:title', 'NUM:date', 'LOC:city', 'DESC:manner',
                  'ENTY:dismed', 'LOC:mount', 'NUM:money', 'ENTY:substance', 'NUM:other', 'NUM:code', 'ENTY:currency', 'NUM:temp',
                  'ABBR:abb', 'NUM:weight', 'DESC:desc']

def get_encodes(x):

    text = [line.decode("utf-8").split(" ", 1)[1] for line in x]
    #tf.reset_default_graph()
    module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
    embed = hub.load(module_url)
    features = embed(text)
    with tf.Session() as sess:
        sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
        features = sess.run(features)
    labels = [[line.decode("utf-8").split(" ", 1)[0]] for line in x]

    return features, labels

config = tf.ConfigProto()
#config.gpu_options.allow_growth = True
run_config = RunConfig(model_dir='./data/cips/save/question-uce-model',
                       session_config=config,
                       save_checkpoints_steps=200)

estimator = DNNClassifier(
    hidden_units=[512],
    feature_columns=[tf.feature_column.numeric_column('feature', shape=(512,))],
    n_classes=len(question_classes),
    config=run_config,
    label_vocabulary=question_classes,
    dropout=0.1)

input_fn = lambda fp: (tf.data.TextLineDataset(fp)
                       .apply(tf.contrib.data.shuffle_and_repeat(buffer_size=10000))
                       .batch(batch_size)
                       .map(lambda x: tf.py_func(get_encodes, [x], [tf.float32, tf.string], name='use_client'))
                       .map(lambda x, y: ({'feature': x}, y))
                       .prefetch(20))

train_spec = TrainSpec(input_fn=lambda: input_fn(train_fp), max_steps=3000)
eval_spec = EvalSpec(input_fn=lambda: input_fn(eval_fp), throttle_secs=0)

train_and_evaluate(estimator, train_spec, eval_spec)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

iris = pd.read_csv('C:\\0Krzysztof\WD\iris.data', names=["SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", "Species"])

iris_setosa = iris[iris["Species"]=="Iris-setosa"]
iris_versicolor = iris[iris["Species"]=="Iris-versicolor"]
iris_virginica = iris[iris["Species"]=="Iris-virginica"]

x3 = iris_virginica["SepalLengthCm"]
y3 = iris_virginica["PetalLengthCm"]
plt.scatter(x3, y3, color="r", label="Iris virginica")

x = iris_setosa["SepalLengthCm"]
y = iris_setosa["PetalLengthCm"]
plt.scatter(x, y, color="g", label="Iris setosa")

x2 = iris_versicolor["SepalLengthCm"]
y2 = iris_versicolor["PetalLengthCm"]
plt.scatter(x2, y2, color="b", label="Iris versicolor")

plt.xlabel("Sepal length (cm)")
plt.ylabel("Petal length (cm)")
plt.title("Iris dataset: petal length and sepal length")
plt.legend(loc="lower right")
plt.plot()
plt.show()


# -*- coding: utf-8 -*-
"""
Django settings for quips project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
from __future__ import absolute_import, unicode_literals

import environ

try:
    from psycopg2cffi import compat

    compat.register()
except ModuleNotFoundError:
    # only use psycopg2cffi if available
    pass

ROOT_DIR = environ.Path(__file__) - 3  # (/a/b/myfile.py - 3 = /)
APPS_DIR = ROOT_DIR.path("quips")

env = environ.Env()

# APP CONFIGURATION
# ------------------------------------------------------------------------------
DJANGO_APPS = (
    # Default Django apps:
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    # Useful template tags:
    # 'django.contrib.humanize',
    # Admin
    "django.contrib.admin",
)
THIRD_PARTY_APPS = (
    "memoize",  # function memoization
    "rest_framework",  # DRF
)

# Apps specific for this project go here.
LOCAL_APPS = (
    "quips.quips",
    "quips.api",
    "quips.slackapp",
    "quips.website",
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

# MIDDLEWARE CONFIGURATION
# ------------------------------------------------------------------------------
MIDDLEWARE = (
    # WhiteNoise really thinks it needs to be first
    # http://whitenoise.evans.io/en/stable/django.html#django-middleware
    "whitenoise.middleware.WhiteNoiseMiddleware",
    # Make sure djangosecure.middleware.SecurityMiddleware is listed first
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
)

# MIGRATIONS CONFIGURATION
# ------------------------------------------------------------------------------
MIGRATION_MODULES = {"sites": "quips.contrib.sites.migrations"}

# DJANGO REST FRAMEWORK
# ------------------------------------------------------------------------------
REST_FRAMEWORK = {
    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
    "PAGE_SIZE": 10,
}

# DEBUG
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = env.bool("DJANGO_DEBUG", False)

# FIXTURE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS
FIXTURE_DIRS = (str(APPS_DIR.path("fixtures")),)

# EMAIL CONFIGURATION
# ------------------------------------------------------------------------------
EMAIL_BACKEND = env(
    "DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend"
)

# MANAGER CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = (("""Brad Smith""", "bradster@infinitewarp.com"),)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS

# DATABASE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    # Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ
    "default": env.db("DATABASE_URL", default="postgres:///quips"),
}
DATABASES["default"]["ATOMIC_REQUESTS"] = True

# GENERAL CONFIGURATION
# ------------------------------------------------------------------------------
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = "UTC"

# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = "en-us"

# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True

# TEMPLATE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#templates
TEMPLATES = [
    {
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
        "DIRS": [
            str(APPS_DIR.path("templates")),
        ],
        "OPTIONS": {
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
            "debug": DEBUG,
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
            # https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
            "loaders": [
                "django.template.loaders.filesystem.Loader",
                "django.template.loaders.app_directories.Loader",
            ],
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.template.context_processors.i18n",
                "django.template.context_processors.media",
                "django.template.context_processors.static",
                "django.template.context_processors.tz",
                "django.contrib.messages.context_processors.messages",
                # Your stuff: custom template context processors go here
            ],
        },
    },
]

# See: http://django-crispy-forms.readthedocs.org/en/latest/install.html#template-packs
CRISPY_TEMPLATE_PACK = "bootstrap3"

# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR("staticfiles"))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/"

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (str(APPS_DIR.path("static")),)

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
)

# MEDIA CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR("media"))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = "/media/"

# URL Configuration
# ------------------------------------------------------------------------------
ROOT_URLCONF = "config.urls"

# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = "config.wsgi.application"

# AUTHENTICATION CONFIGURATION
# ------------------------------------------------------------------------------
AUTHENTICATION_BACKENDS = ("django.contrib.auth.backends.ModelBackend",)

# Custom user app defaults

# SLUGLIFIER
AUTOSLUG_SLUGIFY_FUNCTION = "slugify.slugify"

# Location of root django.contrib.admin URL, use {% url 'admin:index' %}
ADMIN_URL = "admin/"

# Your common stuff: Below this line define 3rd party library settings
DEFAULT_QUIP_UUID = env("DEFAULT_QUIP_UUID", default=None)
RANDOM_QUIP_CACHE_DURATION = env.int(
    "RANDOM_QUIP_CACHE_DURATION", default=60 * 60 * 24
)  # default to one day
GIPHY_HTML_VIDEO = env.bool("GIPHY_HTML_VIDEO", default=True)

# New in Django 3.2:
# 3.1 and older default is 32-bit AutoField. 3.2 now recommends 64-bit BigAutoField.
# See "Customizing type of auto-created primary keys" in the 3.2 release notes:
# https://docs.djangoproject.com/en/3.2/releases/3.2/
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# Class in python

# Create Class
class MyFirstClass:
    name = "Adi Fatkhurozi"

print(MyFirstClass)

# Cretate An Object
class1 = MyFirstClass()

print(class1.name)

# create a class that has a constructor
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p1 = Person("John", 36)

print(p1.name + " " + str(p1.age))

# Create a method in class
class Person1:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def myfunc(self):
        print("Hello my name is " + self.name)

p1 = Person1("John", 36)
p1.myfunc()

# Set the age property from p1
p1.age = 90

# Delete the age property from p1
del p1.age

# Pass Class
class Person2:
    pass


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

"""
import json
import time
from collections import OrderedDict

TEDSLISTFILE = 'tedslist.txt'
SCRIPTLISTFILE = 'scriptlist.txt'
SENSORDATAFILE = 'sensordatafile.txt'
CONTROLLERDATAFILE = 'controllerdatafile.txt'
PLOTLISTFILE = 'plotlistfile.txt'
NEW_TEDS = "new_teds"

HEADERS = {"Content-type": "application/json",
           "Accept": "text/plain",
           "User-Agent": "ToRMConsole"}

CONSOLE_PAGE = "/tormconsole"
CONSOLE_TEDS_PAGE = "/tormconsole/teds"
NEW_TEDS_PAGE = "/new_teds"
REMOVE_TEDS_PAGE = "/tormconsole/remove"
CONSOLE_PLOT_DATA = "/tormconsole/plotdata"
CONSOLE_CONTROLLERS_DATA = "/tormconsole/controllers"

TEDS_FOLDER = "teds"
FILE_FOLDER = "files"
SCRIPT_FOLDER = "scripts"

RX_NAME_ID= 'ID'
## TEDS field related constant strings
# Strings for the TEDS field names
TEDS_NAME_NAME = 'Name'
TEDS_NAME_ID = 'ID'
TEDS_NAME_ACTIVE = 'Active'
TEDS_NAME_ADDRESS = 'SPI Address'
TEDS_NAME_CLKSPD = 'Clock Speed'
TEDS_NAME_SPI_SELECT = 'SPI Select'
TEDS_NAME_CSHIGH = 'Chip Select Idle High'
TEDS_NAME_CLOCKPOLPHASE = 'Clock Polarization/Phase Mode'
TEDS_NAME_SETUP = 'Data for Setup'

TEDS_NAME_REGISTER = 'Register'
TEDS_REGISTER_FRIENDNAME = 'Register Name'
TEDS_NAME_ACTIVEREGISTER = 'Active Register'
TEDS_NAME_ACCESSDATA = 'SPI Access Bytes'
TEDS_NAME_NEXT_INSTRUCTION = 'Next Instruction'
TEDS_NAME_SIUNIT = 'SI Unit'
TEDS_NAME_MINVALUE = 'Minimum Value'
TEDS_NAME_MAXVALUE = 'Maximum Value'
TEDS_NAME_CONVCONST = 'Value Constant Convertion'
TEDS_NAME_FIRSTDATABYTE = 'First Data Byte'
TEDS_NAME_LASTDATABYTE = 'Last Data Byte'
TEDS_NAME_FIRSTDATABIT = 'First Data Bit'
TEDS_NAME_LASTDATABIT = 'Last Data Bit'
TEDS_NAME_ISACTUATOR = 'Is Actuator'
TEDS_NAME_ACTUATORVALUE = 'Actuator Value'
TEDS_NAME_ISSENSOR = 'Is Sensor'
TEDS_NAME_ACTIVATEINTERRUPT = 'Activate Interruption'
TEDS_NAME_WARNINGTHRESHOLD = 'Warning Threshold'
TEDS_NAME_DANGERTHRESHOLD = 'Danger Threshold'

# Strings for the TEDS field tips
TEDS_TIP_NAME = 'Name for user identification.'
TEDS_TIP_ID = 'TEDS unique identification number.'
TEDS_TIP_ACTIVE = 'Set if the transducer will be used in the system.'
TEDS_TIP_ADDRESS = 'SPI physical address connected to the system. \
    It has a maximum value of 1024.'
TEDS_TIP_CLKSPD = 'Transducer SPI clock speed.'
TEDS_TIP_SPI_SELECT = 'Raspberry pin SPI selection GPIO(7/8)'
TEDS_TIP_CSHIGH = 'Set if chip select is idle high and active low.'
TEDS_TIP_CLOCKPOLPHASE = 'Clock polarization/phase mode: [pol|pha] 0b00=0... \
    0b11=3  '
TEDS_TIP_SETUP = 'Setup/Configuration bytes needed for the transducer initialization.'

TEDS_TIP_ACTIVEREGISTER = 'Set if the register will be used.'
TEDS_TIP_ACCESSDATA = 'Bytes used to access the register.'
TEDS_TIP_NEXT_INSTRUCTION = 'Interested Data came on the next instruction'
TEDS_TIP_SPI_SELECT = 'Raspberry pin SPI selection GPIO(7/8)'
TEDS_TIP_SIUNIT = 'The SI physical unit of the register.'
TEDS_TIP_MINVALUE = 'Minimum value it is able to measure.'
TEDS_TIP_MAXVALUE = 'Maximum value it is able to measure.'
TEDS_TIP_CONVCONST = 'Constant used to convert to digital input/output data'
TEDS_TIP_FIRSTDATABYTE = 'Start Byte of the data. Starts with B0 (first \
    sent/received byte), B1...'
TEDS_TIP_LASTDATABYTE = 'Last sent/recieved Data Byte'
TEDS_TIP_FIRSTDATABIT = 'The number of the position of the first bit (Most \
    Significant Bit) of the data in the First Data Byte. \
    Byte = (MSB)|b7|b6|b5|b4|b3|b2|b1|b0|(LSB)'
TEDS_TIP_LASTDATABIT = 'The number of the position of the last bit (Least \
    Significant Bit) of the data in the Last Data Byte. \
    Byte = (MSB)|b7|b6|b5|b4|b3|b2|b1|b0|(LSB)'
TEDS_TIP_ISACTUATOR = 'Checkbox confirming the transducer\'s actuation \
    function.'
TEDS_TIP_ACTUATORVALUE = 'Actuator target value'
TEDS_TIP_ISSENSOR = 'Checkbox confirming the transducer\'s sensing function.'
TEDS_TIP_ACTIVATEINTERRUPT = 'Activate Interruption'
TEDS_TIP_WARNINGTHRESHOLD = 'Warning  Threshold Value'
TEDS_TIP_DANGERTHRESHOLD = 'Danger  Threshold Value'

# TEDS dictionaries
TEDS_BUILD_DICT = [{'name': TEDS_NAME_NAME, 'type': 'str', 'value': '', 'tip': TEDS_TIP_NAME},
        {'name': TEDS_NAME_ID, 'type': 'str', 'value': '', 'tip': TEDS_TIP_ID, 'readonly': True},
        {'name': TEDS_NAME_ACTIVE, 'type': 'bool', 'value': True, 'tip': TEDS_TIP_ACTIVE},
        {'name': TEDS_NAME_ADDRESS, 'type': 'int', 'value': 1, 'limits': (1, 2 ** 10), 'tip': TEDS_TIP_ADDRESS},
        {'name': TEDS_NAME_CLKSPD, 'type': 'int', 'value': 0, 'siPrefix': True, 'suffix': 'kHz', 'tip': TEDS_TIP_CLKSPD},
        {'name': TEDS_NAME_SPI_SELECT, 'type': 'int', 'value': 1, 'tip':TEDS_TIP_SPI_SELECT},
        {'name': TEDS_NAME_CSHIGH, 'type': 'bool', 'value': True, 'tip': TEDS_TIP_CSHIGH},
        {'name': TEDS_NAME_CLOCKPOLPHASE, 'type': 'int', 'value': 0, 'limits': (0, 2 ** 2 - 1), 'tip': TEDS_TIP_CLOCKPOLPHASE},
        {'name': TEDS_NAME_SETUP, 'type': 'str', 'value': '', 'tip': TEDS_TIP_SETUP}
        ]

REGISTER_BUILD_DICT = {'name': TEDS_NAME_REGISTER, 'type': 'group', 'renamable': True, 'removable': True, 'children':[
                {'name':TEDS_REGISTER_FRIENDNAME, 'type': 'str', 'value': ''},
                {'name': TEDS_NAME_ACTIVEREGISTER, 'type': 'bool', 'value': False, 'tip': TEDS_TIP_ACTIVEREGISTER},
                {'name': TEDS_NAME_ACCESSDATA, 'type': 'str', 'value': '', 'tip': TEDS_TIP_ACCESSDATA},
                {'name': TEDS_NAME_NEXT_INSTRUCTION, 'type':'bool','value':False, 'tip': TEDS_TIP_NEXT_INSTRUCTION},
                {'name': TEDS_NAME_SIUNIT, 'type': 'str', 'value': '', 'tip': TEDS_TIP_SIUNIT},
                {'name': TEDS_NAME_MINVALUE, 'type': 'float', 'value': 0, 'tip': TEDS_TIP_MINVALUE},
                {'name': TEDS_NAME_MAXVALUE, 'type': 'float', 'value': 0, 'tip': TEDS_TIP_MAXVALUE},
                {'name': TEDS_NAME_CONVCONST, 'type': 'float', 'value': 0, 'tip': TEDS_TIP_CONVCONST},
                {'name': TEDS_NAME_FIRSTDATABYTE, 'type': 'int', 'value': 0, 'tip': TEDS_TIP_FIRSTDATABYTE},
                {'name': TEDS_NAME_LASTDATABYTE, 'type': 'int', 'value': 0, 'tip': TEDS_TIP_LASTDATABYTE},
                {'name': TEDS_NAME_FIRSTDATABIT, 'type': 'int', 'value': 0, 'limits': (0, 2 ** 3 - 1), 'tip': TEDS_TIP_FIRSTDATABIT},
                {'name': TEDS_NAME_LASTDATABIT, 'type': 'int', 'value': 0, 'limits': (0, 2 ** 3 - 1), 'tip': TEDS_TIP_LASTDATABIT},
                {'name': TEDS_NAME_ISACTUATOR, 'type': 'bool', 'value': False, 'tip': TEDS_TIP_ISACTUATOR},
                {'name': TEDS_NAME_ACTUATORVALUE, 'type': 'float', 'value': 0, 'tip': TEDS_TIP_ACTUATORVALUE},
                {'name': TEDS_NAME_ISSENSOR, 'type': 'bool', 'value': False, 'tip': TEDS_TIP_ISSENSOR},
                {'name': TEDS_NAME_ACTIVATEINTERRUPT, 'type': 'bool', 'value': False, 'tip': TEDS_TIP_ACTIVATEINTERRUPT},
                {'name': TEDS_NAME_WARNINGTHRESHOLD, 'type': 'float', 'value': 0, 'tip': TEDS_TIP_WARNINGTHRESHOLD},
                {'name': TEDS_NAME_DANGERTHRESHOLD, 'type': 'float', 'value': 0, 'tip': TEDS_TIP_DANGERTHRESHOLD}
                ]}

RX_SETUP_BUILD_DICT = [{'name': 'Motherboard', 'type': 'float', 'value': 0},
                {'name': 'Slot', 'type': 'float', 'value': 0},
                {'name': RX_NAME_ID, 'type': 'str', 'value': '', 'readonly': True},
                {'name': 'Rx_Gate_Pair', 'type': 'int', 'value': 0 },
                {'name': 'Gain(Low/High)', 'type': 'int'}
                ]

CONTROLLERS_PARAMETERS = (TEDS_NAME_MINVALUE, TEDS_NAME_MAXVALUE,
                          TEDS_NAME_SIUNIT, TEDS_NAME_ACTUATORVALUE)

def get_list_from_text(inputstring):
    """Convert string type to list type at the TEDS field's input"""
    outputlist = []

    auxlist = inputstring.split(';')
    if inputstring != '':

        for item in auxlist:
            auxlist2 = item.split(',')
            intlist = []

            for item in auxlist2:
                intlist.append(int(item))

            outputlist.append(intlist)

    return outputlist

def get_text_from_list(inputlist):
    """Convert list type to string type at the TEDS field's input"""
    outputstring = ''
    auxlist3 = []

    for sublist in inputlist:
        auxlist = []

        for item in sublist:
            auxlist.append(str(item))

        auxlist2 = ','.join(auxlist)
        auxlist3.append(auxlist2)

    outputstring = ';'.join(auxlist3)
    return outputstring

def id_generator():
    """Create an unique identifier"""
    return time.strftime('%Y%b%d%H%M%S',time.gmtime())

def save_teds(datateds, fileteds):
    """Save TEDS in a file .ted from a dictionary"""
    with open(fileteds + '.ted', 'w') as auxfile:
        json.dump(datateds, auxfile, indent=1)

def load_teds(fileteds):
    """Load TEDS from a file .ted to a dictionary"""
    with open(fileteds + '.ted', 'r') as auxfile:
        datateds = json.load(auxfile)
    return datateds

def load_teds_ordereddict(fileteds):
    """Load TEDS from a file .ted to a ordered dictionary"""
    with open(fileteds + '.ted', 'r') as auxfile:
        datateds = json.load(auxfile, object_pairs_hook=OrderedDict)
    return datateds

def build_dict_from_ordereddict(orddict):
    """Create a new dictionary with the meaningful values"""
    newdict = OrderedDict()
    for key in orddict:
        if orddict[key][0] != None:
            newdict[key] = orddict[key][0]
        else:
            newdict[key] = build_dict_from_ordereddict(orddict[key][1])
    return newdict

def search_entry_index(entry, builddict):
    """ """
    if type(builddict) == type([]):
        for item in builddict:
            if item['name'] == entry:
                return builddict.index(item)

    elif type(builddict) == type({}):
        for item in builddict['children']:
            if item['name'] == entry:
                return builddict['children'].index(item)

    else:
        return None

def set_teds_fields_from_dict(inputdict, teds, basetedsdic, baseregisterdict):
    """Uses the dictionary to set the TEDS fields"""
    for key in inputdict:
        if type(inputdict[key]) == type(OrderedDict()):
            teds.addChild(baseregisterdict, autoIncrementName=True)
            teds.child(key).setName(key)

            for subkey in inputdict[key]:
                if ((subkey == TEDS_NAME_ACCESSDATA)):
                    teds.child(key).child(subkey).setValue(
                        get_text_from_list(inputdict[key][subkey]))
                else:
                    teds.child(key).child(subkey).setValue(inputdict[key][subkey])
        else:
            if key == TEDS_NAME_SETUP:
                teds.child(key).setValue(get_text_from_list(inputdict[key]))
            else:
                teds.child(key).setValue(inputdict[key])
                # if teds does not have the inputdict key?

def set_teds_list_from_dict(inputdict, tedslist):
    """ """
    for key in inputdict:
        tedslist.addChild({'name': key, 'type': 'group', 'children': [
            {'name': inputdict[key], 'type': 'bool', 'value': True}
            ]})

def set_teds_fields_from_file(fileteds, teds, basetedsdic, baseregisterdict):
    """Load TEDS from file and set the fields"""
    auxdict = load_teds_ordereddict(fileteds)
    set_teds_fields_from_dict(auxdict, teds, basetedsdic, baseregisterdict)


import requests
import csv
import os
from bs4 import BeautifulSoup
from selenium import webdriver
from get_keyword import getKeywordsForMulti, getKeywords
import time

def getLinks():
    options = webdriver.ChromeOptions()
    options.add_argument('headless')

    driver = webdriver.Chrome(os.path.dirname(os.path.realpath(__file__)) + "/chromedriver", chrome_options=options)
    driver.get("https://medium.com/netflix-techblog")
    driver.implicitly_wait(7)

    SCROLL_PAUSE_TIME = 3

    # Get scroll height
    last_height = driver.execute_script("return document.body.scrollHeight")

    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # Wait to load page
        time.sleep(SCROLL_PAUSE_TIME)

        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height

    link_list = []
    already_list = []

    try:
        f = open(os.path.dirname(os.path.realpath(__file__)) + "/data/netflix.csv", "r", encoding='utf-8')
        rdr = csv.reader(f)
        for line in rdr:
            already_list.append(line[2])
        f.close()
    except FileNotFoundError:
        f = open(os.path.dirname(os.path.realpath(__file__)) + "/data/netflix.csv", "w", encoding='utf-8', newline='')
        writefile = csv.writer(f)
        writefile.writerow(["title", "content", "url", "cnt", "source", "keyword", "image", "createdAt", "priority"])
        f.close()

    source = driver.page_source
    soup = BeautifulSoup(source, "lxml")
    driver.close()

    links_1 = soup.find_all("div", {"class": "col u-xs-marginBottom10 u-paddingLeft9 u-paddingRight12 u-paddingTop0 u-sm-paddingTop20 u-paddingBottom25 u-size4of12 u-xs-size12of12 u-marginBottom30"})
    links_2 = soup.find_all("div", {"class": "u-lineHeightBase postItem"})
    links_3 = soup.find_all("div", {"class": "col u-xs-marginBottom10 u-paddingLeft0 u-paddingRight0 u-marginBottom60"})

    for l in links_1:
        link = l.find("a")
        # print(link['href'])
        if link['href'] not in already_list:
            link_list.append(link['href'])

    for l in links_2:
        link = l.find("a")
        # print(link['href'])
        if link['href'] not in already_list:
            link_list.append(link['href'])

    for l in links_3:
        link = l.find("a")
        # print(link['href'])
        if link['href'] not in already_list:
            link_list.append(link['href'])

    return link_list

def getData(file, link, multi):
    r = requests.get(link)
    content_source = r.text
    content_soup = BeautifulSoup(content_source, "lxml")

    title = content_soup.select_one('div.section-content > div > h1').text.replace(u'\xa0',' ').replace('\t',' ').lstrip().rstrip().replace("\"", "").replace("\'", "")
    created_at = content_soup.find("time")['datetime'].split("T")[0]

    content = ""
    section_content = content_soup.find_all("div", {"class": "section-content"})
    for s in section_content:

        image = ""
        img = s.find("img")
        if img is not None:
            image = img['src']
        print(image)

        contents = s.find_all("p")
        for c in contents:
            content += c.text
    content = content.replace(u'\xa0',' ').replace('\t',' ').replace('<br>', ' ').replace("\n", ' ').lstrip().rstrip().replace("\"", "").replace("\'", "")
    source = "netflix"

    if multi:
        keyword_list = getKeywordsForMulti(title.lower(), content.lower(), source, False)
    else:
        keyword_list = getKeywords(title.lower(), content.lower(), source, False)

    if keyword_list:
        _keyword = ",".join(keyword_list)
    else:
        _keyword = ""

    file.writerow([title, content[:200] + "...", link, 0, source, _keyword, image, created_at, 0])

    updater = open(os.path.dirname(os.path.realpath(__file__)) + "/data/update.csv", "a", encoding='utf-8', newline='')
    update_writer = csv.writer(updater)
    update_writer.writerow([title, content[:200] + "...", link, 0, source, _keyword, image, created_at, 0])
    updater.close()

def main(multi):
    print("netflix")
    start = time.time()

    link_list = getLinks()

    file = open(os.path.dirname(os.path.realpath(__file__)) + "/data/netflix.csv", "a", encoding='utf-8', newline='')
    writefile = csv.writer(file)
    for i in range(len(link_list)):
        getData(writefile, link_list[i], multi)
    file.close()

    print(time.time() - start)

if __name__ == "__main__":
    main()


"""
Base HyPC format

Description:
    Base HyPC format script used to:
        - Convert ASCII point cloud to HyPC point cloud
    Also contains methods to update existing HyPC point clouds

Required inputs:
    -ASCII based point cloud file

Created by: Christopher Iseli
Last Modified: 20/10/2018 (Christopher Iseli)
"""

import numpy as np
import pandas as pd
import time, math
from scipy.spatial import cKDTree
import pickle

## ===== Settings ====##

inputFname = "pointCloud_sample.txt"
outputFname = "pointCloud_sample1.hypc"

##--------------------##

class hypc:
    def __init__(self, asciiFname, max_segment = 200000):
        start_time = time.clock()
        print("Importing:", inputFname)
        self.df = pd.read_csv(asciiFname, sep=" ", names=column_names)

        print("Point cloud loaded in: ",time.clock()-start_time)
        self.df.loc[:, 'x':'z'] = self.df.loc[:, 'x':'z'].astype(dtype=np.float64)
        self.df.loc[:, '1':'21'] = self.df.loc[:, '1':'21'].astype(dtype=np.uint16)
        self.df['class'] = np.zeros(self.df.shape[0])
        self.df['segment_id'] = np.empty(self.df.shape[0])
        self.df = self.df.sort_values('x')
        self.df = self.df.reset_index(drop=True)

        # self.segment

        print("Creating K-DTree structure")
        self.kdTree = cKDTree(self.df.loc[:, ['x', 'y', 'z']], compact_nodes=True, balanced_tree=False)
        mins,maxes = self.kdTree.mins,self.kdTree.maxes
        self.extents = [[mins[0],maxes[0]],[mins[1],maxes[1]],[mins[2],maxes[2]]]
        self.medians = [np.median(self.kdTree.data[:,0]),np.median(self.kdTree.data[:,1]),np.median(self.kdTree.data[:,2])]
        self.df = self.df.drop(['x','y','z'],axis=1)
        self.segment_indices,self.segment_bounds = segmentPoints(self)
        print("\nNew HyPC point cloud created..", time.clock()-start_time)
        saveHyPC(self,outputFname)
        print("Time for completion: ", time.clock() - start_time)
        # self.segmentPoints()
        self.stats()

    def stats(self):
        shape = self.df.shape
        print("\nImported ASCII file with:")
        print("\tNum. Points: " +str(shape[0]))
        print("\tAttributes: " + str(shape[1]))
        print("\nExtents: ")
        print("\t X: "+str(self.extents[0]))
        print("\t Y: " + str(self.extents[1]))
        print("\t Z: " + str(self.extents[2]))

def updateHyPC(pointCloud,outFname):

    mins, maxes = pointCloud.kdTree.mins, pointCloud.kdTree.maxes
    pointCloud.extents = [[mins[0], maxes[0]], [mins[1], maxes[1]], [mins[2], maxes[2]]]
    pointCloud.medians = [np.median(pointCloud.kdTree.data[:, 0]), np.median(pointCloud.kdTree.data[:, 1]),np.median(pointCloud.kdTree.data[:, 2])]

    pointCloud.segment_indices, pointCloud.segment_bounds = segmentPoints(pointCloud)
    indices = []
    for idxs in pointCloud.segment_indices:
        indices.append(np.max(idxs))

    saveHyPC(pointCloud, outFname)

def segmentPoints(pointCloud,max_points=200000, num_segments = None):
    if pointCloud.df.shape[0]<200000:
        # segment_indices = [pointCloud.df.index.values.astype(np.uint32)]
        # segment_bounds =  [pointCloud.extents[0][0],pointCloud.extents[1][0],pointCloud.extents[0][1],pointCloud.extents[1][1]]
        num_segments=2
    else:
        num_segments = int(math.sqrt(pointCloud.df.shape[0]/max_points))
    start = time.clock()
    min_x,max_x = pointCloud.extents[0]
    min_y,max_y = pointCloud.extents[1]
    # x_size = int(np.floor((max_y - min_y) / num_segments))
    # y_size = int(np.floor((max_x - min_x) / num_segments))
    x_size = (max_x - min_x) / num_segments
    y_size = (max_y - min_y) / num_segments
    x_segment_bounds,xstep = np.linspace(min_x+x_size, max_x, num= num_segments,retstep = True)
    y_segment_bounds,ystep = np.linspace(min_y+y_size, max_y, num=num_segments,retstep=True)

    # Create bins
    bins_x = np.searchsorted(x_segment_bounds, pointCloud.kdTree.data[:, 0])
    bins_y = np.searchsorted(y_segment_bounds, pointCloud.kdTree.data[:, 1])

    x_segment_bounds =np.insert(x_segment_bounds,0,min_x)
    y_segment_bounds = np.insert(y_segment_bounds, 0, min_y)

    pointCloud.df["bins_x"] = bins_x.astype(np.uint16)
    pointCloud.df["bins_y"] = bins_y.astype(np.uint16)

    segments = pointCloud.df.groupby(['bins_x', 'bins_y'])
    # print("Time to create segments: ", time.clock()-start)

    segment_indices = []
    segment_groups = []
    for i,segment in enumerate(segments):
        segment_indices.append(segment[1].index.values.astype(np.uint32))
        segment_groups.append(list(segment[0]))
    segment_indices = np.array(segment_indices)
    segment_bounds = []

    for i, group in enumerate(segment_groups):
        x_start, x_end = x_segment_bounds[group[0]],x_segment_bounds[group[0]]+xstep
        y_start, y_end = y_segment_bounds[group[1]], y_segment_bounds[group[1]]+ystep
        rect = [x_start,y_start,x_end,y_end]
        segment_bounds.append(rect)

    return segment_indices,segment_bounds

def saveHyPC(hypc_obj,fname):
    print("Saving point cloud to :", fname)
    with open(fname, "wb") as f:
        pickle.dump(hypc_obj, f, pickle.HIGHEST_PROTOCOL)

if __name__ == "__main__":
    column_names = ['x','y','z','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21']
    cloud = hypc(inputFname)


import json

def frequent_words_json():

    with open("newsafr.json", encoding="utf-8") as datafile:
        json_data = json.load(datafile)
        news_list = json_data['rss']['channel']['items']

        word_dict = {}

        for news_item in news_list:
            words = news_item["description"].split()
            for word in words:
                if len(word) > 6:
                    if word in word_dict.keys():
                        word_dict[word] += 1
                    else:
                        word_dict[word] = 1

        word_len = []
        for key, value in word_dict.items():
            word_to_add = [value, key]
            word_len.append(word_to_add)

        word_len_sorted = sorted(word_len)

        print('10 наиболее часто встречающихся слов с количеством повторений:')
        for entity in reversed(word_len_sorted[-10::]):
            print('{} - {}'.format(entity[1], entity[0]))

if __name__ == "__main__":
    frequent_words_json()


dwarfNums = []
dwarfSum = 0

for i in range(9):
	dwarfNum = int(input())
	dwarfSum += dwarfNum
	dwarfNums.append(dwarfNum)

dwarfDifference = dwarfSum - 100

for i in range(len(dwarfNums)):
	difference = dwarfDifference - dwarfNums[i]
	if difference in dwarfNums and dwarfNums.index(difference) != i:
		dwarfNums.remove(dwarfNums[i])
		dwarfNums.remove(difference)
		break

for i in dwarfNums:
	print(i)


__author__ = 'Kalyan'

notes = '''
Write your own implementation of converting a number to a given base. It is important to have a good logical
and code understanding of this.

Till now, we were glossing over error checking, for this function do proper error checking and raise exceptions
as appropriate.

Reading material:
    http://courses.cs.vt.edu/~cs1104/number_conversion/convexp.html
'''

def convert(number, base):
    """
    Convert the given number into a string in the given base. valid base is 2 <= base <= 36
    raise exceptions similar to how int("XX", YY) does (play in the console to find what errors it raises).
    Handle negative numbers just like bin and oct do.
    """
    import string
    # Now check the number and base types passed
    if not isinstance(number, int) or not isinstance(base, int):
        raise TypeError
    # Checking base
    if base < 2 or base > 36:
        raise ValueError
    list_numbers = list(range(0, 37))
    alpha_numerals = list(range(0, 10)) + list(string.ascii_uppercase)
    num_dict = dict(zip(list_numbers, alpha_numerals))
    list1 = []
    n = number
    b = base
    if n < 0:  # As negative change to positive initially
        n = n.__abs__()
    while n > 0:
        r = n % b
        list1.append(str(num_dict[r]))
        n = n // b  # To get integer values to access list rather than generating floats
    list1.reverse()
    if number < 0:
        list1.insert(0, '-')  # Add the negative which we negated with abs() before
    return ''.join(list1)

def test_convert():
    assert "100" == convert(4, 2)
    assert "FF" == convert(255, 16)
    assert "377" == convert(255, 8)
    assert "JJ" == convert(399, 20)
    assert "-JJ" == convert(-399, 20)

    try:
        convert(10, 1)
        assert False, "Invalid base <2 did not raise error"
    except ValueError as ve:
        print(ve)

    try:
        convert(10, 40)
        assert False, "Invalid base >36 did not raise error"
    except ValueError as ve:
        print(ve)

    try:
        convert("100", 10)
        assert False, "Invalid number did not raise error"
    except TypeError as te:
        print(te)

    try:
        convert(None, 10)
        assert False, "Invalid number did not raise error"
    except TypeError as te:
        print(te)

    try:
        convert(100, "10")
        assert False, "Invalid base did not raise error"
    except TypeError as te:
        print(te)


  #This is your SRPN file. Make your changes here.

#This is your SRPN file. Make your changes here.
import re

Stack = []
#result = None
calc_count = 0
random = []
count = 0
d = False

def parse(calc):
  check = "(-?[0-9]+|[\+,\-,\*,\/,\%,\^,\=,d,r]+?)(\s#[^#]*#)*"
  calc_list = re.findall(check, calc)
  return calc_list

def calculate(operator,temp_array):

  result = None
  #checks if there are enough values for it to perform a calculation
  if len(temp_array) < 2:
    print('Stack underflow.')
  #takes the last 2 values and uses them for the calculation
  else:
    num1 = temp_array[-1]
    num2 = temp_array[-2]

    #print(num1)
    #print(operator)
    #print(num2)
    #addition
    if operator == "+" :
      result = num1 + num2

    #subtraction
    if operator == "-" :
      result = num1 - num2

    #multiplication
    if operator == "*" :
      result = num1 * num2

    #division
    if operator == "/" :
      if num2 == 0:
        print("Divide by 0.")
        return
      else:
        result = num1 // num2

    #modulus
    if operator == "%" :
      if num2 == 0:
        print("Divide by 0.")
        return
      else:
        result = num1 % num2

    if operator == "^":
      if num2 < 0:
        print("Negative power.")
        return
      else:
        result = num1 ** num2
    #clean up
    temp_array.pop()
    temp_array.pop()

    #saturation
    if result > 2147483647:
      result = 2147483647

    if result < -2147483648:
      result = -2147483648
    #print(result)
    return result

def process_command(command):
  command_list = parse(command)
  for cmd,comment in command_list:
    execute(cmd)
  return

def execute(command):
  global Stack
  global operate_count
  global random

  #checks for operator
  if command == "+" or command == "-" or command == "/" or command == "*" or command == "%" or command == "^":
    result = calculate(command,Stack)
    if result != None:
      Stack.append(result)
      #print(Stack)

  elif command == "d":
    for i in Stack:
      print(i)

  elif command == "r":
    global count
    random = [1804289383, 846930886, 1681692777, 1714636915, 1957747793, 424238335, 719885386, 1649760492, 596516649, 1189641421, 1025202362, 1350490027, 783368690, 1102520059, 2044897763, 1967513926, 1365180540, 1540383426, 304089172, 1303455736, 35005211, 521595368]
    Stack.append(random[0 + count])
    count += 1

  #checks for equal sign
  elif command == "=":
    if len(Stack) == 0:
      print('Stack empty.')
    else:
      print(result)

  #checks if an integer has been inputted
  else:
    # makes sure program doesn't crash if the value isn't an integer
    try:
      val = int(command)
      if val > 2147483647:
        val = 2147483647
      if val < -2147483647:
        val = -2147483648
      #adds integer to array so that it is can be used in calculations
      Stack.append(val)
      if len(Stack) > 23:
        print('Stack overflow.')
        Stack.pop()
    except:
      exit()

#This is the entry point for the program.
#Do not edit the below
if __name__ == "__main__":
    while True:
        try:
            cmd = input()
            pc = process_command(cmd)
            if pc != None:
                print(str(pc))
        except:
            exit()


#!/usr/bin/python

import time
import datetime
from Adafruit_8x8 import EightByEight, ColorEightByEight
from time import sleep

# ===========================================================================
# 8x8 Pixel Example
# ===========================================================================
InvaderBMPup = [
  0B00011000,
  0B00111100,
  0B01111110,
  0B11011011,
  0B00100100,
  0B01011010,
  0B10100101,
  0B00000000
]

InvaderBMPdown = [
  0B00000000,
  0B00011000,
  0B00111100,
  0B01111110,
  0B11011011,
  0B00100100,
  0B01011010,
  0B10100101
]

grid = ColorEightByEight(address=0x70)

# Draw a bit map graphic
def drawBitMap (bMap):
  for y in range(8):
    for x in range(8):
      if (bMap[y] & (2 ** x)):
        grid.setPixel(x, y, 1)

# Draw the jumping invader
print ("The jumping Invader..")
count = 20
pauseTime = 0.15
grid.clear()

for i in range(count):
  drawBitMap(InvaderBMPdown)
  sleep(pauseTime)
  grid.clear()
  drawBitMap(InvaderBMPup)
  sleep(pauseTime)
  grid.clear()

sleep(3)
grid.clear()


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymongo

class ZhihuPipeline(object):
    def process_item(self, item, spider):
        return item

class MongoPipeline(object):
    def __init__(self):
        host = 'localhost'
        port = 27017
        dbname = 'Zhihu'
        sheetname = 'zhihu_user'
        # 创建MONGODB数据库链接
        client = pymongo.MongoClient(host=host, port=port)
        # 指定数据库
        mydb = client[dbname]
        # 存放数据的数据库表名
        self.post = mydb[sheetname]

    def process_item(self, item, spider):
        self.post.update({'url_token': item['url_token']}, {'$set': item}, True)
        print("***************************************")
        return item


from BI.data_warehouse.connector import Snowflake

DB = Snowflake()
DB.set_user('MACK_DAMAVANDI')

def find_badge_id(email):
    try:
        DB.open_connection()

        DB.execute_query('''SELECT BADGE_ID
                                FROM VSLR.HR.T_EMPLOYEE
                                WHERE LOWER(WORK_EMAIL_ADDRESS) = LOWER(%s)
                                AND NOT TERMINATED''',
                         bindvars=[email])
        results = DB.query_results[0][0]
    finally:
        DB.close_connection()

    return results


"""
Based on tutorial
https://medium.com/@ageitgey/machine-learning-is-fun-part-3-deep-learning-and-convolutional-neural-networks-f40359318721#.54v56wei4
Based on the tflearn example located here:
https://github.com/tflearn/tflearn/blob/master/examples/images/convnet_cifar10.py
"""
from __future__ import division, print_function, absolute_import

# Import tflearn and some helpers
import tflearn
from tflearn.data_utils import shuffle
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
import pickle
import numpy as np
import os
from scipy.misc import imread, imresize, imsave
from sklearn.model_selection import train_test_split
import pandas as pd

## FUNCTIONS
# load images
def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        if filename.find('.jpg') != -1:  # if it contains .jpg
            img = imread(os.path.join(folder,filename))
            if img is not None:
                images.append(img)
    return images

## DEFINES
# Small 19 record dataset
#dir_ALB = '/Users/andrew/Documents/kaggle/fish/cnn-model-001/data/ALB_small'
#dir_OTHER = '/Users/andrew/Documents/kaggle/fish/cnn-model-001/data/OTHER_small'

# Large dataset
dir_ALB = '/Users/andrew/Documents/kaggle/fish/cnn-model-001/data/ALB/resized'
dir_OTHER = '/Users/andrew/Documents/kaggle/fish/cnn-model-001/data/OTHER/resized'

## Load the data set (replace this with something that loads our JPEGS)
# Load all the ALBs
ALB_images = load_images_from_folder(dir_ALB)
ALB_labels = np.repeat('ALB', len(ALB_images), axis=0)

# Load all the OTHERs
multiply_samples = 1
OTHER_images = load_images_from_folder(dir_OTHER)
OTHER_labels = np.repeat('OTHER', len(OTHER_images), axis=0)

# The ALB pics are 6 times more than the OTHER pics so duplicate OTHERS 6x
#OTHER_images = np.repeat(OTHER_images, multiply_samples, axis=0)

# Union datasets
df = []
df.extend(ALB_images); df.extend(OTHER_images)
ALB_images = []; OTHER_images = []
labels = []
labels.extend(ALB_labels); labels.extend(OTHER_labels)
ALB_labels = []; OTHER_labels = []

# Change labels into 1s and 0s
for i in range(len(labels)):
      # Converting 'ALB' field to float
      labels[i] = 1.0 if labels[i] == 'ALB' else 0.0

# Cast all image numbers to floats
df = np.asarray(df)
df = df.astype(float)

# Split into train and test
X, X_test, Y, Y_test = train_test_split(df, labels, test_size=0.33, random_state=42)

# Original load data code
# X, Y, X_test, Y_test = pickle.load(open("full_dataset.pkl", "rb"))

# Shuffle the data (shuffles both arrays by the same offsets)
X, Y = shuffle(X, Y)
X_test, Y_test = shuffle(X_test, Y_test)

# Make sure the data is normalized
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Create extra synthetic training data by flipping, rotating and blurring the
# images on our data set.
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)

# Define our network architecture:

# Input is a 32x32 image with 3 color channels (red, green and blue)
network = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

# Step 1: Convolution
network = conv_2d(network, 32, 3, activation='relu')

# Step 2: Max pooling
network = max_pool_2d(network, 2)

# Step 3: Convolution again
network = conv_2d(network, 64, 3, activation='relu')

# Step 4: Convolution yet again
network = conv_2d(network, 64, 3, activation='relu')

# Step 5: Max pooling again
network = max_pool_2d(network, 2)

# Step 6: Fully-connected 512 node neural network
network = fully_connected(network, 512, activation='relu')

# Step 7: Dropout - throw away some data randomly during training to prevent over-fitting
network = dropout(network, 0.5)

# Step 8: Fully-connected neural network with two outputs (0=isn't a bird, 1=is a bird) to make the final prediction
network = fully_connected(network, 2, activation='softmax')

# Tell tflearn how we want to train the network
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Wrap the network in a model object
model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='fish-classifier.tfl.ckpt')

# Train it! We'll do 100 training passes and monitor it as it goes.
model.fit(X, Y, n_epoch=100, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=96,
          snapshot_epoch=True,
          run_id='fish-classifier')

# Save model when training is complete to a file
model.save("fish-classifier.tfl")
print("Network trained and saved as fish-classifier.tfl!")

## Test network performance

# Predict
prediction = model.predict(X_test)

# Output Scores to CSV
pred_df = pd.DataFrame(prediction, columns=["IS ALB","IS OTHER"])
obs_df = pd.DataFrame(Y_test, columns=["OBSERVATIONS"])
results = pd.concat([pred_df, obs_df], axis=1)
results.to_csv('/Users/andrew/Documents/kaggle/fish/cnn-model-001/results.csv', header=True)


def get_rounds():

  error = "Please enter an integer or press <enter>"

  valid = False

  while not valid:
    print()
    response = input("How many rounds? \nPress <enter> for continuous mode: \n")

    if response == "":
      return("continuous")

    try:
      response = int(response)

      if response > 0:
        return response
      else:
        print(error)

    except ValueError:
      print(error)

num_rounds = get_rounds()
print(num_rounds)


# wczytanie potrzebnych bibliotek:
import roboticstoolbox as rtb
import numpy as np
from spatialmath import *
from spatialmath.base import *
from spatialmath.base.symbolic import *
import math
import matplotlib.pyplot as plt
# ...

# definicje funkcji:

def zadanie_3():
    Tps = np.eye(4)
    Tbs = np.eye(4)
    Rps = np.array([[-1, 0, 0],
                    [0, 1, 0],
                    [0, 0, -1]])
    Rbs = np.array([[0, -1, 0],
                    [1, 0, 0],
                    [0, 0, 1]])
    tps = np.array([3, -6, 4])
    tbs = np.array([2, 5, 0])
    Tbs[0:3, 0:3] = Rbs
    Tbs[0:3, 3] = tbs.T
    Tbs = SE3(Tbs)
    Tps[0:3, 0:3] = Rps.T
    Tps[0:3, 3] = -(Rps.T)@tps
    Tps = SE3(Tps)
    dp = np.eye(4)
    db = np.eye(4)
    ds = np.eye(4)
    dp1 = np.array([1, -2, 2])
    dp[0:3, 3] = dp1.T
    dp = SE3(dp)
    ds = Tps@dp
    db = Tbs@ds
    db = SE3(db)
    print("Zadanie 3.1: ", db.t)
    #zadanie 3.2
    trplot(transl(0, 0, 0), frame='B', rviz=True, width=1)
    trplot(Tbs.A, color='red', frame='S')
    p = Tbs@Tps
    p = SE3(p)
    trplot(p.A, color='black', frame='P')
    plt.quiver(0, 0, 0, -2, 7, 2)
    plt.show()

# ...

# wykonywanie wybranej funkcji
if __name__ == '__main__':
    zadanie_3()


from dbt.main import parse_args
from dbt.config.runtime import RuntimeConfig
from dbt.parser.manifest import ManifestLoader
from dbt.adapters.factory import register_adapter
from dbt.tracking import track_invocation_start, track_invocation_end, flush
from contextlib import contextmanager
from dbt.tracking import disable_tracking

class DBT:
    def __init__(self, dbt_args):
        disable_tracking()
        self.parsed = parse_args(args=dbt_args)
        self.config = RuntimeConfig.from_args(self.parsed)
        register_adapter(self.config)

    def get_manifest(self):
        with tracker(config=self.config, args=self.parsed):
            return ManifestLoader.get_full_manifest(config=self.config)

@contextmanager
def tracker(config, args):
    track_invocation_start(config, args)
    try:
        yield
        track_invocation_end(config, args, result_type="ok")
    finally:
        flush()


from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
import numpy as np
from numpy.lib.format import open_memmap
import pickle

# IMPLEMENTS THE STACKING ALGORITHM
# TAKING AS INPUT A LIST OF MODELS TO USE TOGETHER
# THE reuse VARIABLE INDICATES IF THE MODEL HAS TO BE TRAINED
# AGAIN OR IF IT WILL JUST BE LOADED AND USED AS IS
# WARNING : MAKE SURE THAT A SAVED MODEL INSTANCE (.pkl) EXISTS
def f_STACK(X_train, Y_train, X_test, Y_test, models, reuse=False):
    if reuse:
        with open('Algos/STACK.pkl', 'rb') as filehandler:
            classifier = pickle.load(filehandler)
    # THE STACKING ALGORITHM IS A LOGISTIC CLASSIFIER
    else:
        classifier = LogisticRegression(multi_class='multinomial',
                                        solver='saga', verbose=0,
                                        class_weight='balanced', n_jobs=20)
        algorithms_predictions = np.empty(
                                 shape=(X_train.shape[0], len(models))
                                 )

        # LOADS THE MODEL AND SAVES THEIR PREDICTIONS
        for i, algo in enumerate(models):
            if algo.startswith('NN'):
                path = 'Algos/'+algo[:-1]+'_folder/'+algo+'.pkl'
                with open(path, 'rb') as filehandler:
                    model, model_history = pickle.load(filehandler)
                algorithms_predictions[:, i] = np.argmax(
                                               model.predict(X_train),
                                               axis=1)
            else:
                path = 'Algos/'+algo+'_folder/'+algo+'.pkl'
                with open(path, 'rb') as filehandler:
                    model = pickle.load(filehandler)
                algorithms_predictions[:, i] = model.predict(X_train)
        # TRAINS THE LOGISTIC CLASSIFIER TO CHOOSE OUTPUT DEPENDING
        # ON MODELS' DECISION
        classifier.fit(algorithms_predictions, Y_train)

    # NOW APPLIES ITSELF ON THE TEST SET, IT MUST FIRST OBTAIN THE
    # PREDICTIONS OF THE SUBMODELS
    algorithms_tests = np.empty(shape=(X_test.shape[0], len(models)))
    for i, algo in enumerate(models):
        if algo.startswith('NN'):
            path = 'Algos/'+algo[:-1]+'_folder/'+algo+'.pkl'
            with open(path, 'rb') as filehandler:
                model, model_history = pickle.load(filehandler)
            algorithms_tests[:, i] = np.argmax(
                                     model.predict(X_test),
                                     axis=1)
        else:
            path = 'Algos/'+algo+'_folder/'+algo+'.pkl'
            with open(path, 'rb') as filehandler:
                model = pickle.load(filehandler)
            algorithms_tests[:, i] = model.predict(X_test)
    expected = classifier.predict(algorithms_tests)

    # COMPUTES THE CONFUSION MATRIX
    conf_matrix = confusion_matrix(expected, Y_test)
    normalisation = np.sum(conf_matrix, axis=1, keepdims=True)
    conf_matrix = conf_matrix/normalisation
    print(conf_matrix)

    # SAVES THE MODEL
    with open('Algos/STACK.pkl', 'wb') as filehandler:
        pickle.dump(classifier, filehandler)
    print('Model saved')

    return conf_matrix


import tensorflow as tf

print (tf.__version__)

with tf.device("/GPU:0"):
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.log_device_placement = True
    config.allow_soft_placement = True
    # config = tf.ConfigProto(log_device_placement=True, allow_soft_placement=True)
    sess = tf.Session(config=config)

    # matrix1 = tf.constant([[3., 3.]])
    # matrix2 = tf.constant([[2.],[2.]])
    # product = tf.matmul(matrix1, matrix2)
    # result = sess.run(product)

    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)
    result = sess.run(c)
    print (result)


import time
import numpy as np
import random

def quickSort(a, l,r):
    if r-l > 1:
        mid = int((l+r)/2)
        if a[l] > a[mid] :
            a[l], a[mid] = a[mid], a[l]
        if a[mid] > a[r]:
            a[mid], a[r]=a[r], a[mid]
        if a[l] > a[mid]:
            a[l], a[mid] = a[mid], a[l]
        a[mid], a[r-1] = a[r-1], a[mid]

        v,i,j = a[r], l-1, r
        while True:
            i+=1
            while a[i] < v:
                i+=1
            j-=1
            while a[j] >v:
                j-=1
            if i>=j:
                break
            a[i], a[j] = a[j], a[i]
        a[i], a[r] = a[r], a[i]
        quickSort(a, l, i-1)
        quickSort(a, i+1, r)

def checkSort(a, n):
    isSorted = True
    for i in range(1, n):
        if(a[i] > a[i+1]):
            isSorted = False
        if (not isSorted):
            break
        if isSorted:
            print("정렬완료")
        else:
            print("정렬오류")

# 난수 추출하기!!
def makerandintlist(n):
    randlist = []
    for n in range(0, n):
        randlist.append(random.randint(1,100000))
    return randlist

#_________________________________________________________________________#
if __name__ == '__main__':

    M=15

    lists=[]
    # 난수 추출하기
    for i in range(7):
        temp_list = makerandintlist((i+1)*300000)
        print(len(temp_list))
        lists.append(temp_list)
        lists[i].insert(0, -1)

    time_quick = []

    for i in range(len(lists)):
        starttime= time.time()
        quickSort(lists[i], 1, len(lists[i])-1)
        time_quick.append(time.time()-starttime)
        print('iter', i, ' 실행시간: ', time_quick[i])


# search.py
# ---------
# Licensing Information:  You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).

"""
In search.py, you will implement generic search algorithms which are called by
Pacman agents (in searchAgents.py).
"""

import util

class Node:
    def __init__(self, state, parent=None, action=None, path_cost=0):
        """Create a search tree Node, derived from a parent by an action."""
        self.state = state
        self.parent = parent
        self.action = action
        self.path_cost = path_cost
        self.depth = 0
        if parent:
            self.depth = parent.depth + 1

    def __repr__(self):
        return "<Node {}>".format(self.state)

    def __lt__(self, node):
        return self.state < node.state

    def solution(self):
        """Return the sequence of actions to go from the root to this node."""
        return [node.action for node in self.path()[1:]]

    def path(self):
        """Return a list of nodes forming the path from the root to this node."""
        node, path_back = self, []
        while node:
            path_back.append(node)
            node = node.parent
        return list(reversed(path_back))

    # We want for a queue of nodes in breadth_first_graph_search or
    # astar_search to have no duplicated states, so we treat nodes
    # with the same state as equal.
    def __eq__(self, other):
        return isinstance(other, Node) and self.state == other.state

class SearchProblem:
    """
    This class outlines the structure of a search problem, but doesn't implement
    any of the methods (in object-oriented terminology: an abstract class).

    You do not need to change anything in this class, ever.
    """

    def getStartState(self):
        """
        Returns the start state for the search problem.
        """
        util.raiseNotDefined()

    def isGoalState(self, state):
        """
          state: Search state

        Returns True if and only if the state is a valid goal state.
        """
        util.raiseNotDefined()

    def getSuccessors(self, state):
        """
          state: Search state

        For a given state, this should return a list of triples, (successor,
        action, stepCost), where 'successor' is a successor to the current
        state, 'action' is the action required to get there, and 'stepCost' is
        the incremental cost of expanding to that successor.
        """
        util.raiseNotDefined()

    def getCostOfActions(self, actions):
        """
         actions: A list of actions to take

        This method returns the total cost of a particular sequence of actions.
        The sequence must be composed of legal moves.
        """
        util.raiseNotDefined()

def tinyMazeSearch(problem):
    """
    Returns a sequence of moves that solves tinyMaze.  For any other maze, the
    sequence of moves will be incorrect, so only use this for tinyMaze.
    """
    from game import Directions
    s = Directions.SOUTH
    w = Directions.WEST
    return  [s, s, w, s, w, w, s, w]

def depthFirstSearch(problem):
    """
    Search the deepest nodes in the search tree first.

    Your search algorithm needs to return a list of actions that reaches the
    goal. Make sure to implement a graph search algorithm.

    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:

    print "Start:", problem.getStartState()
    print "Is the start a goal?", problem.isGoalState(problem.getStartState())
    print "Start's successors:", problem.getSuccessors(problem.getStartState())
    """
    "*** YOUR CODE HERE ***"

    from util import Stack

    #creates frontier and root node using getStartState
    #lifo queue to keep track of next state(s) to explore
    frontier = Stack()
    frontier.push(problem.getStartState())

    #holds explored states
    explored = set()
    #holds final path to take
    finalPath = []
    #stack to hold next direction to take
    nextDirection = Stack()
    currState = frontier.pop()

    while not problem.isGoalState(currState):

        if currState not in explored:
            explored.add(currState)
            nextState = problem.getSuccessors(currState)

            for child, direction, cost in nextState:
                frontier.push(child)
                currPath = finalPath+[direction]
                nextDirection.push(currPath)

        currState = frontier.pop()
        finalPath = nextDirection.pop()

    return finalPath

    # util.raiseNotDefined()

def breadthFirstSearch(problem):
    """Search the shallowest nodes in the search tree first."""
    "*** YOUR CODE HERE ***"

    from util import Queue

    #creates frontier and root node using getStartState()
    #FIFO queue with node as the only element
    frontier = Queue()
    frontier.push(problem.getStartState())

    #holds explored states
    explored = set()
    #holds final path to take
    finalPath = []
    #queue to hold next direction to take
    nextDirection = Queue()
    currState = frontier.pop()

    while not problem.isGoalState(currState):

        if currState not in explored:
            explored.add(currState)
            nextState = problem.getSuccessors(currState)

            for child, direction, cost in nextState:
                frontier.push(child)
                currPath = finalPath+[direction]
                nextDirection.push(currPath)

        currState = frontier.pop()
        finalPath = nextDirection.pop()

    return finalPath

    # util.raiseNotDefined()

def uniformCostSearch(problem):
    """Search the node of least total cost first."""
    "*** YOUR CODE HERE ***"

    from util import PriorityQueue

    #create a node for the state; assuming root has path cost of 0
    #creates frontier and root node using getStartState()
    #priority queue to help distinguish best next choice
    frontier = PriorityQueue()
    frontier.push(problem.getStartState(), 0)

    #holds explored states
    explored = set()
    #holds final path to take
    finalPath = []
    #queue to hold next direction to take
    nextDirection = PriorityQueue()
    currState = frontier.pop()

    while not problem.isGoalState(currState):
        if currState not in explored:
            explored.add(currState)
            nextState = problem.getSuccessors(currState)

            for child, direction, cost in nextState:
                currPath = finalPath+[direction]
                pathCost = problem.getCostOfActions(currPath)

                if child not in explored:
                    frontier.push(child, pathCost)
                    nextDirection.push(currPath, pathCost)

        currState = frontier.pop()
        finalPath = nextDirection.pop()

    return finalPath

    # util.raiseNotDefined()

def nullHeuristic(state, problem=None):
    """
    A heuristic function estimates the cost from the current state to the nearest
    goal in the provided SearchProblem.  This heuristic is trivial.
    """
    if problem.isGoalState(state):
        return -20
    else:
        return 0

def aStarSearch(problem, heuristic=nullHeuristic):
    """Search the node that has the lowest combined cost and heuristic first."""
    "*** YOUR CODE HERE ***"

    from util import Queue, PriorityQueue

    #creates frontier and root node using getStartState()
    #priority queue to help distinguish best next choice
    frontier = PriorityQueue()
    frontier.push(problem.getStartState(), 0)

    #holds explored states
    explored = set()
    #holds final path to take
    finalPath = []
    #queue to hold next direction to take
    nextDirection = PriorityQueue()
    currState = frontier.pop()

    while not problem.isGoalState(currState):

        if currState not in explored:
            explored.add(currState)
            nextState = problem.getSuccessors(currState)

            for child, direction, cost in nextState:
                currPath = finalPath+[direction]
                pathCost = problem.getCostOfActions(currPath)+heuristic(child, problem)

                if child not in explored:
                    frontier.push(child, pathCost)
                    nextDirection.push(currPath, pathCost)

        currState = frontier.pop()
        finalPath = nextDirection.pop()

    return finalPath

    # util.raiseNotDefined()

# Abbreviations
bfs = breadthFirstSearch
dfs = depthFirstSearch
astar = aStarSearch
ucs = uniformCostSearch


class ValidWordAbbr(object):
    def __init__(self, dictionary):
        """
        initialize your data structure here.
        :type dictionary: List[str]
        """
        self.abdic = collections.defaultdict(list) # cannot be count; o/w count[word]=1 cannot provide info about which word it is corresponding to
        dictionary = set(dictionary)
        for word in dictionary:
            if len(word) > 2:
                key = word[0] + str(len(word)-2) + word[-1]
            else:
                key = word
            self.abdic[key].append(word)

    def isUnique(self, word):
        """
        check if a word is unique.
        :type word: str
        :rtype: bool
        """
        if len(word) > 2:
            key = word[0] + str(len(word)-2) + word[-1]
        else:
            key = word
        return key not in self.abdic or self.abdic[key] == [word]


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

import sys
import os
import numpy as np
import pandas as pd
from enum import Enum
from collections import deque
import config_data as c
# from PyQt5 import QtCore, QtGui, QtWidgets

EventType = Enum('EventType', ('Start', 'Stop', 'TurnRight', 'TurnLeft', 'HardBrake',
                 'HardSwerve', 'LaneChange', 'CarFollowing', 'PhoneEvent', 'FatigueDriving', 'CutIn'))

class StreamQueue(object):
    ''' 在遍历数据检测事件时，往往需要判断事件点前后一段时间的状态，此类是两个定长队列和一个中间值组成的《队列》，
        该队列遵循先进先出原则，数据先填充到nextQueue，满了再到填充到midlle（一个数据），最后填充到preQueue。
    '''
    def __init__(self, maxlen=10):
        self.preQueue = deque(maxlen=maxlen)
        self.middle = None
        self.nextQueue = deque(maxlen=maxlen)

        self.maxlen = maxlen

    def is_full(self):
        return self.middle is not None and len(self.preQueue) == len(self.nextQueue) == self.maxlen

    def append(self, val):
        if len(self.nextQueue) == self.maxlen:
            if self.middle is not None:
                self.preQueue.append(self.middle)
            self.middle = self.nextQueue.popleft()
        self.nextQueue.append(val)

    def clear(self):
        self.preQueue.clear()
        self.nextQueue.clear()
        self.middle = None

class EventIdentification(object):
    def __init__(self, data=None, parent=None):
        super().__init__()
        self.data = data

    def SetData(self, data):
        self.data = data

    def StartStopEvent(self, pre=5, nxt=5, spd=3):
        ''' 启动事件定义：对于某个时间点x，x的前pre秒速度小于阈值spd，
            且后nxt秒的速度值均大于阈值spd，则视为该时间点x为启动事件点 '''
        # print('StartStopEvent called.')
        if self.data is None:
            return
        timeAndSpeed = self.data[[c.TimeField, c.SpeedField]]
        # 注意：采样间隔为10，因为Trip原始采集设备一秒采集10次
        timeAndSpeed = timeAndSpeed.iloc[::10, :]
        # 将索引按秒进行编号
        timeAndSpeed.index = list(range(len(timeAndSpeed)))
        # pre和nxt参数不能太小，否则没有现实意义
        pre = max(2, pre)
        nxt = max(2, nxt)
        # 若时间太短，则不检测
        if len(timeAndSpeed) < pre + nxt:
            return

        startEventIndex = []
        stopEventIndex = []
        prevPointSpeed = deque(maxlen=pre)
        nextPointSpeed = deque(maxlen=nxt)
        for i in range(nxt):
            nextPointSpeed.append(timeAndSpeed.loc[i][c.SpeedField])
        # print(nextPointSpeed)
        for i in timeAndSpeed.index:
            if i + nxt >= len(timeAndSpeed):
                break
            prevPointSpeed.append(timeAndSpeed.loc[i][c.SpeedField])
            nextPointSpeed.append(timeAndSpeed.loc[i + nxt][c.SpeedField])
            # 检测出事件点后，其后nxt秒都不需要检测了，避免重复
            if not (len(startEventIndex) and i < nxt + max(startEventIndex)):
                # 启动事件判断：前pre秒速度值小于spd，后nxt秒速度大于spd
                if max(prevPointSpeed) < spd and min(nextPointSpeed) >= spd:
                    startEventIndex.append(i)
                    # currTime = int(timeAndSpeed.loc[i][c.TimeField])
                    # currTime = currTime // 10
                    # timeStr = "%.2d:%.2d:%.2d" % (currTime // 3600, currTime % 3600 // 60, currTime % 3600 % 60)
                    # print('found start point: ', timeStr)
            if not (len(stopEventIndex) and i < nxt + max(stopEventIndex)):
                # 停止事件判断：前pre秒速度值大于spd，后nxt秒速度小于spd
                if min(prevPointSpeed) >= spd and max(nextPointSpeed) < spd:
                    stopEventIndex.append(i)
                    # currTime = int(timeAndSpeed.loc[i][c.TimeField])
                    # currTime = currTime // 10
                    # timeStr = "%.2d:%.2d:%.2d" % (currTime // 3600, currTime % 3600 // 60, currTime % 3600 % 60)
                    # print('found stop point: ', timeStr)
        eventList = []
        for i in startEventIndex:
            eventList.append([EventType.Start, int(timeAndSpeed.loc[i][c.TimeField])])
        for i in stopEventIndex:
            eventList.append([EventType.Stop, int(timeAndSpeed.loc[i][c.TimeField])])
        return eventList

    def HardBrake(self, gap=5):
        ''' 急刹车特征：1.加速度小于某个阈值（0.51）'''
        if self.data is None:
            return
        timeAndAccelX = self.data[[c.TimeField, c.AccelXField]]
        # 注意：采样间隔为10，因为Trip原始采集设备一秒采集10次
        timeAndAccelX = timeAndAccelX.iloc[::10, :]
        # 将索引按秒进行编号
        timeAndAccelX.index = list(range(len(timeAndAccelX)))
        # 若时间太短，则不检测
        if len(timeAndAccelX) < gap:
            return
        matchedIndex = []
        # for i in timeAndAccelX.index:
        #     # 检测出事件点后，其后gap秒都不需要检测了，避免重复
        #     if not (len(matchedIndex) and i < gap + max(matchedIndex)):
        #         if timeAndAccelX.loc[i][c.AccelXField] < -0.51:
        #             matchedIndex.append(i)
        for i, row in enumerate(timeAndAccelX.values):
            if not (len(matchedIndex) and i < gap + max(matchedIndex)):
                if row[1] < -0.51:
                    matchedIndex.append(i)
        eventList = []
        for i in matchedIndex:
            eventList.append([EventType.HardBrake, timeAndAccelX.loc[i][c.TimeField]])
        return eventList

    def HardSwerve(self):
        print('HardSwerve called.')
        if self.data is None:
            return

    def LaneChange(self, spd=10, gap=1):

        def isSignSame(dat, currRow):
            for i in range(1, 10):
                if ((dat.loc[currRow - i - 1][c.LaneOffsetField] < 0) != (dat.loc[currRow - i][c.LaneOffsetField] < 0)) or ((dat.loc[currRow + i - 1][c.LaneOffsetField] < 0) != (dat.loc[currRow + i][c.LaneOffsetField] < 0)):
                    return False
            return True

        def isValueContinious(dat, currRow):
            for i in range(1, 10):
                low = abs(dat.iloc[currRow - i - 1][c.LaneOffsetField] - dat.iloc[currRow - i][c.LaneOffsetField])
                high = abs(dat.iloc[currRow + i - 1][c.LaneOffsetField] - dat.iloc[currRow + i][c.LaneOffsetField])
                # if low < 5 or low > 1500 or high < 5 or high > 1500:
                #     return False
                return (0.005 < low < 1.5) and (0.005 < high < 1.5)

        if self.data is None:
            return
        data = self.data[[c.TimeField, c.SpeedField, c.LaneOffsetField]]
        # data = data.iloc[::5, :]
        data.index = list(range(len(data)))
        eventIndex = []
        for i in data.index:
            if i == 0:
                continue
            speed = data.loc[i][c.SpeedField]
            # curr = data.loc[i][c.LaneOffsetField]
            # prev = data.loc[i - 1][c.LaneOffsetField]
            # 检测出事件点后，其后nxt秒都不需要检测了，避免重复
            if speed > spd and i > 10 and not (len(eventIndex) and i < gap * 2 * 10 + max(eventIndex)):
                # if abs(curr - prev) > 2000 and ((curr>0 and prev<0) or (curr<0 and prev>0)):
                curr = data.iloc[i][c.LaneOffsetField]
                prev = data.loc[i - 1][c.LaneOffsetField]
                if ((curr < 0 != prev < 0) and
                        (abs(curr - prev) > 2.500) and
                        (abs(curr - prev) < 5.000) and
                        abs(curr) > 0.500 and
                        abs(curr) < 3.000 and
                        abs(prev) > 0.500 and
                        abs(prev) < 3.000 and
                        isSignSame(data, i) and
                        isValueContinious(data, i)):
                    eventIndex.append(i)
                    currTime = int(data.loc[i][c.TimeField])
                    currTime = currTime // 10
                    timeStr = "%.2d:%.2d:%.2d" % (currTime // 3600, currTime % 3600 // 60, currTime % 3600 % 60)
                    print('found LaneChange point: ', timeStr)

    def CarFollowing(self, x_bias=120.0, y_bias=2.0, x_accel=5.0, lens=15, minSpeed=10):
        ''' x_bias: 最大纵向距离，该项准则旨在去除自由流的交通流状态；
            y_bias: 最大侧向距离绝对值，确保实验车辆与雷达目标处于同一车道；
            x_accel: 最大纵向加速度绝对值，确保前后车处于稳定的跟驰状态；
            lens: 最小跟车片段长度，单位s，确保处于稳定的跟车状态;
            minSpeed: 最小速度，确保汽车处于行驶状态 '''
        if self.data is None:
            return
        samples = self.data[[c.TimeField, c.SpeedField, c.T0_ObjectID, c.AccelXField, c.T0_XRange, c.T0_YRange]]
        (TimeField, SpeedField, T0_ObjectID, AccelXField, T0_XRange, T0_YRange) = range(6)  # 字段列索引匹配
        # 注意：采样间隔为10，因为Trip原始采集设备一秒采集10次
        samples = samples.iloc[::10, :]
        # 将索引按秒重编号
        samples.index = list(range(len(samples)))
        # 若时间太短，则不检测
        if len(samples) < lens:
            return

        eventList = []  # 检测出的事件
        container = []  # 片段流，存储跟车片段从起始到结束的所有样本
        oldObjectId = 0  # 雷达目标编号，无目标时为0，跟车过程中目标编号保持不变

        # 遍历每一个样本
        # for i in samples.index:
        for row in samples.values:
            curr_objectId = int(row[T0_ObjectID])
            curr_speed = row[SpeedField]
            curr_x_bias = abs(row[T0_XRange])
            curr_y_bias = abs(row[T0_YRange])
            curr_x_accel = abs(row[AccelXField])

            if not len(container):
                # 容器为空，若该样本满足条件，则将该样本作为片段起始点
                if curr_objectId > 0 and curr_speed > minSpeed and curr_x_bias < x_bias and \
                   curr_y_bias < y_bias and curr_x_accel < x_accel:
                    container.append(row)
            else:
                # 容器不为空，将满足条件的样本点加入到片段流container中
                if curr_objectId > 0 and curr_speed > minSpeed and curr_objectId == oldObjectId and \
                   curr_x_bias < x_bias and curr_y_bias < y_bias and curr_x_accel < x_accel:
                    container.append(row)
                # 容器不为空，且此样本点不满足约束条件，意味着片段结束，此时判断片段长度，若>lens,则认为是跟车
                else:
                    if len(container) >= lens:
                        eventList.append([EventType.CarFollowing, int(container[0][TimeField])])
                        # currTime = int(container[0][TimeField])
                        # currTime = currTime // 10
                        # timeStr = "%.2d:%.2d:%.2d" % (currTime // 3600, currTime % 3600 // 60, currTime % 3600 % 60)
                        # print('found car following point: ', timeStr)
                    # 清空容器，设置该数据为新哨兵（若满足条件）
                    container.clear()
                    if curr_objectId > 0 and curr_speed > minSpeed and curr_x_bias < x_bias and \
                       curr_y_bias < y_bias and curr_x_accel < x_accel:
                        container.append(row)
            oldObjectId = curr_objectId
        return eventList

    def PhoneEvent(self):
        if self.data is None:
            return

    def FatigueDriving(self):
        if self.data is None:
            return

    def cut_in(self):
        Y_offset = 1.8  # 过滤条件：1）本车保持车道不变，本车与前车的横向偏移范围：[-1.8m, 1.8m]
        Y1_offset = 5.52  # 过滤条件：2）相邻车道有车，本车与旁边车的横向距离范围：[-5.52m, 5.52m]
        offset = 1.5  # 本车在离本车道的中心线距离范围：[-1500mm,1500mm]

        cols1 = ['System.Time_Stamp', 'vtti.timestamp', 'Road Scout.Lane_Offset', 'FOT_Control.Speed',
                 'SMS.Object_ID_T0', 'SMS.X_Velocity_T0', 'SMS.X_Range_T0', 'SMS.Y_Range_T0',
                 'SMS.Object_ID_T1', 'SMS.X_Velocity_T1', 'SMS.X_Range_T1', 'SMS.Y_Range_T1',
                 'SMS.Object_ID_T2', 'SMS.X_Velocity_T2', 'SMS.X_Range_T2', 'SMS.Y_Range_T2']
        (System_Time_Stamp, vtti_timestamp, Road_Scout_Lane_Offset, FOT_Control_Speed,
         SMS_Object_ID_T0, SMS_X_Velocity_T0, SMS_X_Range_T0, SMS_Y_Range_T0,
         SMS_Object_ID_T1, SMS_X_Velocity_T1, SMS_X_Range_T1, SMS_Y_Range_T1,
         SMS_Object_ID_T2, SMS_X_Velocity_T2, SMS_X_Range_T2, SMS_Y_Range_T2) = range(16)

        data = self.data.loc[:, cols1]

        myQueue = StreamQueue()

        data = data.values

        eventList = []  # 检测出的事件

        for row in data:
            myQueue.append(row)  # append
            if not myQueue.is_full():
                continue
            last = myQueue.preQueue[-1]
            new = myQueue.middle
            lastObj_0 = last[SMS_Object_ID_T0]
            lastObj_1 = last[SMS_Object_ID_T1]
            currObj_0 = new[SMS_Object_ID_T0]
            currObj_1 = new[SMS_Object_ID_T1]
            if lastObj_0 is not None and currObj_0 is not None and lastObj_0 != currObj_0 and lastObj_1 == currObj_0:
                Lane_offset = last[Road_Scout_Lane_Offset]  # 本车道偏移线距离
                X_T0 = last[SMS_X_Range_T0]  # 本车与前车的纵向距离
                NewX_T0 = new[SMS_X_Range_T0]  # 插入车与前车的纵向距离
                Y_T0 = last[SMS_Y_Range_T0]  # 本车与前车的横向距离
                Y_T1 = last[SMS_Y_Range_T1]  # 本车与旁边车的横向距离

                if lastObj_0 == '' or Y_T0 == '' or Y_T1 == '' or NewX_T0 == '' or X_T0 == '':  #若变量存在空值，则执行下一行
                    continue

                if abs(float(Y_T0)) < Y_offset and abs(float(Y_T1)) < Y1_offset and (Lane_offset==' ' or abs(float(Lane_offset)) <= offset) and float(X_T0) > float(NewX_T0):
                    # 限制条件：若插入车插入后的1s内，存在插入车与前车的横向偏移大于1.8m,则不是cut_in事件，视为数据采集误差
                    # 判断插入车前1s内ObjID不变，插入车插入后1s内ObjID不变道
                    # if max(myQueue.nextQueue) > Y_offset:
                    #     continue
                    not_cut_in = False
                    for r in myQueue.preQueue:
                        if r[SMS_Object_ID_T1] != lastObj_1:
                            not_cut_in = True
                            break
                    for r in myQueue.nextQueue:
                        if r[SMS_Object_ID_T0] != currObj_0 or float(r[SMS_Y_Range_T0]) > Y_offset:
                            not_cut_in = True
                            break
                    if not_cut_in:
                        continue

                    cutInTime = int(new[System_Time_Stamp])
                    eventList.append([EventType.CutIn, cutInTime])
                    cutInTime = int(cutInTime // 10)
                    # print('*********************************************************')
                    # print('find cut in event: %d:%s' % (cutInTime // 60, cutInTime % 60))
                    # print(myQueue.preQueue)
                    # print(myQueue.middle)
                    # print(myQueue.nextQueue)
                    # print('*********************************************************')
        return eventList

if __name__ == '__main__':
    # tripData = pd.read_csv('D:\\NDS\data\\359932\\File_ID_359932.csv', encoding='utf-8')
    tripData = pd.read_csv(r'D:\workspace\NDS\data\177959\File_ID_177959.csv', encoding='utf-8')

    # data handle : fill and transform

    fieldList = [c.SpeedField, c.AccelXField, c.AccelYField, c.AccelZField,
                 c.LaneWidthField, c.LaneOffsetField, c.LeftLaneField, c.RightLaneField,
                 c.T0_ObjectID, c.T0_XRange, c.T0_YRange,
                 c.T0_ObjectID.replace('0', '1'), c.T0_XRange.replace('0', '1'), c.T0_YRange.replace('0', '1'),
                 c.T0_ObjectID.replace('0', '2'), c.T0_XRange.replace('0', '2'), c.T0_YRange.replace('0', '2'),
                 c.T0_ObjectID.replace('0', '3'), c.T0_XRange.replace('0', '3'), c.T0_YRange.replace('0', '3')]
    tripData[fieldList] = tripData[fieldList].fillna(method='pad')
    tripData[fieldList] = tripData[fieldList].fillna(0)
    tripData[c.SpeedField] = tripData[c.SpeedField] * 3.6
    mm2mList = [c.LaneWidthField, c.LaneOffsetField, c.LeftLaneField, c.RightLaneField]
    tripData[mm2mList] = tripData[mm2mList] / 1000

    import time
    start = time.time()
    ident = EventIdentification(tripData)
    result = ident.StartStopEvent()
    print(result)
    # result = ident.CarFollowing()
    # ident.LaneChange()
    # ident.cut_in()
    # print(ident.HardBrake())
    end = time.time()
    print('time cost: ', end-start)

    # lanechange = ident.LaneChange()


ATTRIBUTES = (
    'body',
    'agility',
    'reaction',
    'strength',
    'willpower',
    'logic',
    'intuition',
    'charisma',
    'edge',
    'essence',
    'magic'
)
#
DAMAGE_TYPES = (
    'P',
    'S'
)
KNOWLEDGES = (
    'ToDo'
)
LANGUAGES = (
    'Sperethiel',
    'Or`zet',
    'English',
    'Japanise',
    'Mandarine',
    'Russian',
    'French',
    'Italian',
    'German',
    'Aztlaner Spanish',
    'Spanish',
    'Lakota',
    'Dakota',
    'Dine'
)
METATYPES = (
    'human',
    'elf',
    'dwarf',
    'ork',
    'troll'
)
OBJECT_TYPES = (
    'objects',
    'knowledge',
    'skills',
    'spells',
    'contacts',
    'weapons',
    'armor',
    'talismans'
)
WEAPON_CLASSES = (
    'Tasers',
    'Hold-Outs',
    'Light Pistols',
    'Heavy Pistols',
    'Machine Pistols',
    'Submachine Guns',
    'Assault Rifles',
    'Sniper Rifles',
    'Shotguns',
    'Special Weapons',
    'Machine Guns',
    'Cannons & Launchers',
    'Grenades',
    'Rockets and Missiles',
    'Blades',
    'Clubs',
    'Other Melee Weapons',
    'Projectile and Throwing Weapons',
    'Explosives'
)
FIRING_MODES = (
    'SS',
    'SA',
    'SB',
    'BF',
    'LB',
    'FA simple',
    'FA complex',
    'Suppressive Fire'
)