import datetime
from typing import List, Optional

import numpy as np
from ..common import (
    ActionType, Context, DataSource, Processor, ProcessorFactory, TradingFrequency,
    Position, ProcessorAction, Mode, DATETIME_TYPE)
from ..data_loader import get_shortable_symbols
from ..stock_universe import IntradayVolatilityStockUniverse

NUM_UNIVERSE_SYMBOLS = 20

class AbcdProcessor(Processor):

    def __init__(self,
                 lookback_start_date: DATETIME_TYPE,
                 lookback_end_date: DATETIME_TYPE,
                 data_source: DataSource,
                 output_dir: str) -> None:
        super().__init__(output_dir)
        self._positions = dict()
        self._stock_universe = IntradayVolatilityStockUniverse(lookback_start_date,
                                                               lookback_end_date,
                                                               data_source,
                                                               num_stocks=NUM_UNIVERSE_SYMBOLS)
        self._shortable_symbols = set(get_shortable_symbols())

    def get_trading_frequency(self) -> TradingFrequency:
        return TradingFrequency.FIVE_MIN

    def setup(self, hold_positions: List[Position], current_time: Optional[DATETIME_TYPE]) -> None:
        to_remove = [symbol for symbol, position in self._positions.items()
                     if position['status'] != 'active']
        for symbol in to_remove:
            self._positions.pop(symbol)

    def get_stock_universe(self, view_time: DATETIME_TYPE) -> List[str]:
        return list(set(self._stock_universe.get_stock_universe(view_time) +
                        list(self._positions.keys()) + ['TQQQ']))

    def process_data(self, context: Context) -> Optional[ProcessorAction]:
        if self.is_active(context.symbol):
            return self._close_position(context)
        elif context.symbol not in self._positions:
            return self._open_position(context)

    def _open_position(self, context: Context) -> Optional[ProcessorAction]:
        if abs(context.current_price / context.prev_day_close - 1) > 0.5:
            return
        t = context.current_time.time()
        if (datetime.time(10, 30) <= t <= datetime.time(15, 30) and
                context.current_price > context.prev_day_close):
            if context.symbol == 'TQQQ':
                return self._open_long_position(context, 0.7, 0.4)
            else:
                return self._open_long_position(context, 1, 0.6)
        if (datetime.time(12, 0) <= t <= datetime.time(15, 30) and
                context.current_price < context.prev_day_close and
                context.symbol in self._shortable_symbols and
                context.symbol != 'TQQQ'):
            return self._open_short_position(context)

    def _open_long_position(self, context: Context, a: float, b: float) -> Optional[ProcessorAction]:
        market_open_index = context.market_open_index
        if market_open_index is None:
            return
        open_price = context.intraday_lookback['Open'][market_open_index]
        intraday_closes = context.intraday_lookback['Close'][market_open_index:]
        if len(intraday_closes) < 10:
            return
        if intraday_closes[-1] >= intraday_closes[-2]:
            return
        if abs(intraday_closes[-3] - intraday_closes[-2]) < abs(intraday_closes[-2] - intraday_closes[-1]):
            return
        max_i = np.argmax(intraday_closes)
        intraday_high = intraday_closes[max_i]
        if not context.prev_day_close < open_price < context.current_price < intraday_high:
            return
        l2h = context.l2h_avg
        if intraday_high / open_price - 1 < a * l2h:
            return
        if intraday_high / context.current_price - 1 < b * l2h:
            return
        if len(intraday_closes) > max_i + 15 or len(intraday_closes) < max_i + 10:
            return
        self._logger.debug(f'[{context.current_time.strftime("%F %H:%M")}] [{context.symbol}] '
                           f'L2h: {l2h}. Intraday high: {intraday_high}. Current price: {context.current_price}.')
        self._positions[context.symbol] = {'entry_time': context.current_time,
                                           'status': 'pending',
                                           'side': 'long'}
        return ProcessorAction(context.symbol, ActionType.BUY_TO_OPEN, 1)

    def _open_short_position(self, context: Context) -> Optional[ProcessorAction]:
        market_open_index = context.market_open_index
        if market_open_index is None:
            return
        open_price = context.intraday_lookback['Open'][market_open_index]
        intraday_closes = context.intraday_lookback['Close'][market_open_index:]
        if len(intraday_closes) < 10:
            return
        intraday_low = np.min(intraday_closes)
        if not context.prev_day_close > open_price > context.current_price > intraday_low:
            return
        h2l = context.h2l_avg
        if intraday_low / open_price - 1 > 0.7 * h2l:
            return
        if intraday_low / context.current_price - 1 > 0.5 * h2l:
            return
        min_i = np.argmin(intraday_closes)
        if len(intraday_closes) > min_i + 10 or len(intraday_closes) < min_i + 5:
            return
        if context.mode == Mode.TRADE:
            self._logger.debug(f'[{context.current_time.strftime("%F %H:%M")}] [{context.symbol}] '
                               f'Checking bar shape conditions. Current price: {context.current_price}.')
        for i in range(-5, 0):
            if intraday_closes[i] < intraday_closes[i - 1]:
                break
        else:
            return
        if abs(intraday_closes[-3] - intraday_closes[-2]) < abs(intraday_closes[-2] - intraday_closes[-1]):
            return
        if intraday_closes[-1] <= intraday_closes[-2]:
            return
        self._logger.debug(f'[{context.current_time.strftime("%F %H:%M")}] [{context.symbol}] '
                           f'H2l: {h2l}. Intraday low: {intraday_low}. Current price: {context.current_price}.')
        self._positions[context.symbol] = {'entry_time': context.current_time,
                                           'status': 'active',
                                           'side': 'short'}
        return ProcessorAction(context.symbol, ActionType.SELL_TO_OPEN, 1)

    def _close_position(self, context: Context) -> Optional[ProcessorAction]:
        position = self._positions[context.symbol]
        side = position['side']
        intraday_closes = context.intraday_lookback['Close']
        take_profit = (context.current_time == position['entry_time'] + datetime.timedelta(minutes=30)
                       and len(intraday_closes) >= 7)
        if side == 'long':
            take_profit = take_profit and context.current_price > intraday_closes[-7]
        else:
            take_profit = take_profit and context.current_price < intraday_closes[-7]
        is_close = (take_profit or
                    context.current_time >= position['entry_time'] + datetime.timedelta(minutes=40)
                    or context.current_time.time() >= datetime.time(16, 0))
        self._logger.debug(f'[{context.current_time.strftime("%F %H:%M")}] [{context.symbol}] '
                           f'Closing position: {is_close}. Current price {context.current_price}.')
        if is_close:
            position['status'] = 'inactive'
            action_type = ActionType.SELL_TO_CLOSE if side == 'long' else ActionType.BUY_TO_CLOSE
            return ProcessorAction(context.symbol, action_type, 1)

class AbcdProcessorFactory(ProcessorFactory):

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

    def create(self,
               lookback_start_date: DATETIME_TYPE,
               lookback_end_date: DATETIME_TYPE,
               data_source: DataSource,
               output_dir: str,
               *args, **kwargs) -> AbcdProcessor:
        return AbcdProcessor(lookback_start_date, lookback_end_date, data_source, output_dir)


# web2.py
#웹서버에 요청
import urllib.request
#크롤링
from bs4 import BeautifulSoup

#패키지명.모듈명.함수명()
data = urllib.request.urlopen("http://comic.naver.com/webtoon/list.nhn?titleId=20853&weekday=fri")

#전체문서에서 검색
#
#     마음의 소리 50화 <격렬한 나의 하루>
#
soup = BeautifulSoup(data, "html.parser")
#필요한 태그만 검색:  10개를 가져오기
cartoons = soup.find_all("td", class_="title")
#1개만 검색
print("갯수:{0}".format( len(cartoons)) )
#슬라이싱을 해서 첫번째만 가져오기
title = cartoons[0].find("a").text
link = cartoons[0].find("a")["href"]
print(title)
print(link)

#파일로 저장(첨부연산): wt, a+
f = open("c:\\work\\webtoon.txt", "wt", encoding="utf-8")
for item in cartoons:
    title = item.find("a").text.strip()
    print(title)
    f.write(title + "\n")

print("웹툰 크롤링 종료")
f.close()


from typing import Callable, Dict, Any, Awaitable
from aiogram import BaseMiddleware
from aiogram.types import TelegramObject
from database.database_service import RequestDB
import asyncpg

class InitUsersMeddleware(BaseMiddleware):
    def __init__(self, *args: object, connect_db: asyncpg.pool.Pool) -> None:
        super().__init__(*args)
        self.connection = connect_db

    async def __call__(self,
    handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
    event: TelegramObject,
    data: Dict[str, Any]
    ) -> Any:
        async with self.connection.acquire() as connect:
            data['db_connect'] = connect
            result = await handler(event, data)
            return result


# x=3+4.5
# y=16
# print(y%x)
#
#
# print(x%6)
#
#
# type(x)
# print(type(x))
#
#
# print(float(99)+100)
#
# nam =input('Who are you')
# # print('Welcome', nam)
# print('Hey There!')
# name = input ('Enter you name:')
#
# print('Your Name:', name)
# # https://www.coursera.org/learn/python/lecture/bYbGU/2-3-expressions-part-3

# onvert elevator floors
# inp = input('Europe floor')
# usf = int(inp) + 1
# print('USA floor', usf)

#### 2.2 asmnt

# inp = input('name')
# print("Hello", inp)

#### 2.3 asmnt

hrs = input("Enter Hours:")
rate = input("Enter Hourly Rate:")
float_hrs = float(hrs) # this is what you weren't doing before, need to make a new variable to make a float, int, ext
float_rate= float(rate)
Tpay = float_hrs * float_rate
print("Pay:", Tpay)


import tensorflow as tf
import numpy as np

## Data
SIZE = 200000
EPOCHS = 10
x1 = np.random.randint(0, 100, SIZE)
x2 = np.random.randint(0, 100, SIZE)
x = np.dstack((x1, x2))[0]
y1 = 3*(x1**(1/2)) + 2*(x2**2)
y2 = x1 + x2 + 80
y = np.dstack((y1, y2))[0]

## tf.keras.Sequential + fit
model1 = tf.keras.Sequential()
model1.add(tf.keras.layers.Dense(64, input_shape=(2,) , activation='sigmoid'))
model1.add(tf.keras.layers.Dense(128, activation = 'relu'))
model1.add(tf.keras.layers.Dense(2))
model1.compile(optimizer=tf.keras.optimizers.Adam(0.001), loss=tf.keras.losses.MSE)
model1.fit(x=x, y=y, epochs=EPOCHS)

## tf.keras.Model
class Model2(tf.keras.Model):
	def __init__(self):
		super(Model2, self).__init__()
		self.hidden1 = tf.keras.layers.Dense(64, input_shape=(2,) ,activation='sigmoid')
		self.hidden2 = tf.keras.layers.Dense(128, activation='relu')
		self.out = tf.keras.layers.Dense(2)
		self.compile(optimizer=tf.keras.optimizers.Adam(0.001), loss=tf.keras.losses.MSE)

	def call(self, inputs):
		x = self.hidden1(inputs)
		x = self.hidden2(x)
		x = self.out(x)
		return x

model2 = Model2()
model2.fit(x=x, y=y, epochs=EPOCHS)

## tf.keras.Sequential without fit but tf.GradientTape()
model3 = tf.keras.Sequential()
model3.add(tf.keras.layers.Dense(64, input_shape=(2,) , activation='sigmoid'))
model3.add(tf.keras.layers.Dense(128, activation = 'relu'))
model3.add(tf.keras.layers.Dense(2))
# model3.build()
opt =tf.keras.optimizers.Adam(0.001)
mse =tf.keras.losses.MSE
batch = 100
for i in range(10):
	for j in range(0, SIZE, batch):
		with tf.GradientTape() as tape:
			y_pre = model3(x[j:j+batch])
			loss = mse(y[j:j+batch], y_pre)
		grads = tape.gradient(loss, model3.trainable_variables)
		processed_grads = [g for g in grads]
		grads_and_vars = zip(processed_grads, model3.trainable_variables)
		opt.apply_gradients(grads_and_vars)
	print ('Model3: ', i, ' : ', np.mean(loss.numpy()))

# Testing
x1 = np.array([100, 9, 62, 79, 94, 91, 71, 41])
x2 = np.array([65, 39, 40, 44, 77, 42, 36, 74])
x = np.dstack((x1, x2))[0]
y1 = 3*(x1**(1/2)) + 2*(x2**2)
y2 = x1 + x2 + 80
y = np.dstack((y1, y2))[0]
yM1 = model1.predict(x)
yM2 = model2.predict(x)
yM3 = model3.predict(x)

for i in range(5):
	print ('''Input: ({}, {}), Answer: ({:.0f}, {:.0f}), \
Model1: ({:.0f}, {:.0f}), \
Model2: ({:.0f}, {:.0f}), \
Model3: ({:.0f}, {:.0f})'''.format(x1[i], x2[i], y1[i], y2[i], yM1[i][0], yM1[i][1], yM2[i][0], yM2[i][1], yM3[i][0], yM3[i][1]))

'''
Input: (100, 65), Answer: (8480, 245), Model1: (8473, 243), Model2: (8491, 247), Model3: (8477, 236)
Input: (9, 39), Answer: (3051, 128), Model1: (3057, 126), Model2: (3037, 129), Model3: (3050, 127)
Input: (62, 40), Answer: (3224, 182), Model1: (3229, 182), Model2: (3210, 180), Model3: (3217, 186)
Input: (79, 44), Answer: (3899, 203), Model1: (3907, 208), Model2: (3888, 203), Model3: (3897, 207)
Input: (94, 77), Answer: (11887, 251), Model1: (11848, 253), Model2: (11893, 255), Model3: (11876, 242)
'''


# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))

# -- Project information -----------------------------------------------------

project = 'reproduce'
copyright = 'Nicolas Tessore'
author = 'Nicolas Tessore'

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.intersphinx',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.git*']

# If true, figures, tables and code-blocks are automatically numbered if they
# have a caption. The numref role is enabled. Obeyed so far only by HTML and
# LaTeX builders. Default is False.
numfig = False

# -- Options for HTML output -------------------------------------------------

# The “title” for HTML documentation generated with Sphinx’s own templates. This
# is appended to the <title> tag of individual pages, and used in the navigation
# bar as the “topmost” element. It defaults to '<project> v<revision>
# documentation'.
html_title = 'reproduce'

# The URL which points to the root of the HTML documentation. It is used to
# indicate the location of document like canonical_url.
html_baseurl = 'https://ntessore.github.io/reproduce/'

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'agogo'

# A dictionary of options that influence the look and feel of the selected
# theme. These are theme-specific.
html_theme_options = {
    'bodyfont': '"Times New Roman", Times, serif',
    'headerfont': '"Times New Roman", Times, serif',
    'bgcolor': 'white',
    'headerbg': 'black',
    'footerbg': 'black',
    'linkcolor': 'blue',
    'headercolor1': 'inherit',
    'headercolor2': 'inherit',
}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# A list of JavaScript filename. The entry must be a filename string or a tuple
# containing the filename string and the attributes dictionary. The filename
# must be relative to the html_static_path, or a full URI with scheme like
# http://example.org/script.js. The attributes is used for attributes of
# <script> tag. It defaults to an empty list.
html_js_files = ['links.js']

# If true, the reST sources are included in the HTML build as _sources/name. The
# default is True.
html_copy_source = False

# If true (and html_copy_source is true as well), links to the reST sources will
# be added to the sidebar. The default is True.
html_show_sourcelink = False

# -- Options for LaTeX output ------------------------------------------------

latex_theme = 'howto'

latex_elements = {
    'fncychap': '',
    'figure_align': 'H',
}

# -- Intersphinx mapping -----------------------------------------------------

intersphinx_mapping = {
    'skypy': ('https://skypy.readthedocs.io/en/latest', None),
}

# -- inline extensions -------------------------------------------------------

SOURCE_URI = 'https://github.com/ntessore/reproduce/tree/master/%s'

from docutils import nodes, utils
from sphinx.util.nodes import split_explicit_title

def source_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
    has_t, title, target = split_explicit_title(text)
    title = utils.unescape(title)
    target = utils.unescape(target)
    refnode = nodes.reference(title, title, refuri=SOURCE_URI % target)
    return [refnode], []

def setup(app):
    app.add_role('source', source_role)
    return {'parallel_read_safe': True, 'parallel_write_safe': True}


import networks as nw
import config as config

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

results = pd.read_csv(r'C:\Users\Public\DAEN690\Model_Cellular_Network\SimResults\NetworkRun-breaktowers.csv')
df_summary = results.groupby(["tower_range", "connection_check", "run_type"], as_index=False).count()

# print(df_summary.run_type.unique())
# ['Break Tower: Aquia_Creek Ln' 'Break Tower: Chesapeak River Ln'
#  'Break Tower: Nguyen Engineering' 'Break Tower: Patriot Cir'
#  'Break Tower: Rivanna River Way']

run = 'Break Tower: Patriot Cir'

df_true = df_summary["connection_check"]==True
df_false = df_summary["connection_check"]==False

df_break = df_summary["run_type"] == run

df_Aquia = df_summary["run_type"] == 'Break Tower: Aquia_Creek Ln'
df_Chesapeak = df_summary["run_type"] == 'Break Tower: Chesapeak River Ln'
df_Nguyen = df_summary["run_type"] == 'Break Tower: Nguyen Engineering'
df_Patriot = df_summary["run_type"] == 'Break Tower: Patriot Cir'
df_Rivanna = df_summary["run_type"] == 'Break Tower: Rivanna River Way'

"""
plt.plot(df_summary[df_true][df_break]["tower_range"]
         , df_summary[df_true][df_break]["cell_id"]
         , 'g'
         , label='Can Connect')
plt.plot(df_summary[df_false][df_break]["tower_range"]
         , df_summary[df_false][df_break]["cell_id"]
         , 'r'
         , label='Cannot Connect')
plt.legend()
plt.ylabel('Number of Device Connections')
plt.xlabel('Tower Range')
plt.suptitle('Simulation Results {run}'.format(run=run))
plt.show()
"""

plt.plot(df_summary[df_Aquia][df_true]["tower_range"]
         , df_summary[df_Aquia][df_true]["cell_id"]
         , color='green'
         , label='Aquia Break')
plt.plot(df_summary[df_Chesapeak][df_true]["tower_range"]
         , df_summary[df_Chesapeak][df_true]["cell_id"]
         , color='yellow'
         , label='Chesapeak Break')
plt.plot(df_summary[df_Nguyen][df_true]["tower_range"]
         , df_summary[df_Nguyen][df_true]["cell_id"]
         , color='red'
         , label='Nguyen Break')
plt.plot(df_summary[df_Patriot][df_true]["tower_range"]
         , df_summary[df_Patriot][df_true]["cell_id"]
         , color='purple'
         , label='Patriot Break')
plt.plot(df_summary[df_Rivanna][df_true]["tower_range"]
         , df_summary[df_Rivanna][df_true]["cell_id"]
         , color='orange'
         , label='Rivanna Break')

plt.legend()
plt.ylabel('Number of Device Connections')
plt.xlabel('Tower Range')
plt.suptitle('Comparing Tower Breaks')
plt.show()


#!/usr/bin/python

import argparse
import sys
import mnist_softmax
import mnist_deep

FLAGS = None

def test_mnist_softmax():
  print('####################### test mnist_softmax.py #######################')
  mnist_softmax.FLAGS = FLAGS
  iter = 20
  accu_list = []
  prec_list = []
  for i in range(iter):
    print('################## iter: %d #################' % i)
    accuracy, precision = mnist_softmax.main([sys.argv[0]] + unparsed)
    accu_list.append(accuracy)
    prec_list.append(precision)
    print('accuracy: {}, precision: {}'.format(accuracy, precision))

  print('accu_list:', accu_list)
  print('prec_list:', prec_list)
  print('average accuracy :', sum(accu_list)/len(accu_list))
  print('average precision:', sum(prec_list)/len(prec_list))

def test_mnist_deep():
  print('####################### test mnist_deep.py #######################')
  mnist_deep.FLAGS = FLAGS
  iter = 10
  accu_list = []
  prec_list = []
  for i in range(iter):
    print('################## iter: %d #################' % i)
    accuracy, precision = mnist_deep.main([sys.argv[0]] + unparsed)
    accu_list.append(accuracy)
    prec_list.append(precision)
    print('accuracy: {}, precision: {}'.format(accuracy, precision))

  print('accu_list:', accu_list)
  print('prec_list:', prec_list)
  print('average accuracy :', sum(accu_list)/len(accu_list))
  print('average precision:', sum(prec_list)/len(prec_list))

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--data_dir', type=str, default='./testdata_1/',
                      help='Directory for storing input data')
  parser.add_argument('--type', type=str, default='all',
                      help='test type: softmax, deep, all')
  FLAGS, unparsed = parser.parse_known_args()
  print('FLAGS:', FLAGS, ', unparsed:', unparsed)

  if FLAGS.type == 'softmax' or FLAGS.type == 'all':
    test_mnist_softmax()

  if FLAGS.type == 'deep' or FLAGS.type == 'all':
    test_mnist_deep()

  # tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)


import wx
from ui.filedrop import *

WINDOW_SIZE = 480

class SimpleGuiPanel(wx.Panel):
    def __init__(self, *args, **kw):
        super(SimpleGuiPanel, self).__init__(*args, **kw)
        filedrop = FileDrop(self)
        self.SetDropTarget(filedrop)
        self._initUi()
        self.SetAutoLayout(True)

    def _initUi(self):
        sizer = wx.BoxSizer(wx.VERTICAL)

        self.text = wx.TextCtrl(self, style = wx.TE_PROCESS_ENTER|wx.TE_MULTILINE, size=(WINDOW_SIZE,WINDOW_SIZE))
        self.text.SetValue("")
        sizer.Add(self.text, 1, wx.EXPAND)

        btnBox = wx.BoxSizer(wx.HORIZONTAL)

        clearBtnId = wx.NewId()
        clearBtn = wx.Button(self, clearBtnId, "Clear", size=(50,30))
        clearBtn.Bind(wx.EVT_BUTTON, self.OnClearBtn)
        btnBox.Add(clearBtn, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

        copyBtnId = wx.NewId()
        copyBtn = wx.Button(self, copyBtnId, "Copy", size=(50,30))
        copyBtn.Bind(wx.EVT_BUTTON, self.OnCopyBtn)
        btnBox.Add(copyBtn, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

        runBtnId = wx.NewId()
        runBtn = wx.Button(self, runBtnId, "Run", size=(50,30))
        runBtn.Bind(wx.EVT_BUTTON, self.OnRunBtn)
        btnBox.Add(runBtn, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

        sizer.Add(btnBox, 0, wx.ALIGN_CENTRE, 5)
        self.SetSizer(sizer)

    def OnCallback(self, filelist):
        self._printFileList(filelist)

    def _printFileList(self, files):
        fileList = ""
        for file in files:
            fileList += file + "\n"
        self.text.SetValue(fileList)

    def OnClearBtn(self, event):
        self.text.SetValue("")

    def OnCopyBtn(self, event):
        toCopyData = self.text.GetValue()

        if len(toCopyData) == 0:
            return

        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(wx.TextDataObject(toCopyData))
            wx.TheClipboard.Close()

    def OnRunBtn(self, event):
        pass


from scipy.spatial import distance as dist
from imutils import face_utils
import numpy as np
import imutils
import dlib
import cv2
import math
import sys

class Helper:

	def eyeRatio(self, eye):
		# Possibly replace euclidean() with manual calculation

		# Calculate average height of eye
		h1 = dist.euclidean(eye[1], eye[5])
		h2 = dist.euclidean(eye[2], eye[4])
		heightAverage = (h1 + h2) / 2.0

		# Calculate width of eye
		width = dist.euclidean(eye[0], eye[3])

		# Calculate the eye aspect ratio
		ratio = heightAverage / width

		return ratio

	def eyeRegion(self, eye, frame):
		xmax, ymax = eye[0]
		xmin, ymin = eye[0]
		for (x, y) in eye:
			if x > xmax:
				xmax = x
			if x < xmin:
				xmin = x
			if y > ymax:
				ymax = y
			if y < ymin:
				ymin = y

		return frame[ymin:ymax, xmin:xmax]

	def gammaCorrection(self, image, gamma):
		# Lookup table to map pixel values to new intensity range
		lookupTable = np.array([((i / 255.0) ** gamma) * 255
		                        for i in np.arange(0, 256)]).astype("uint8")

		gammaCorrected = cv2.LUT(image, lookupTable)

		return gammaCorrected

	def get_iris_center(self, image, eye, gamma=1.0):
		# Increased size to width of 100 for better results
		image = imutils.resize(image, width=100)
		# Convert to grayscale
		img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

		# Gamma correction
		img = self.gammaCorrection(img, gamma)

		# Return to caller to adjust gamma in future call
		meanAfterGamma = cv2.mean(img)[0]

		img = cv2.GaussianBlur(img, (5, 5), 0)

		# Normalise image after gamma correction
		cv2.normalize(img, img, 0, 255, cv2.NORM_MINMAX)

		cv2.imshow("{} normalised".format(eye), img)

		# Binarise image
		_, thresh = cv2.threshold(img, 20, 255, cv2.THRESH_BINARY)

		# Closing
		# kernel = np.ones((3,3),np.uint8)
		# thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

		# Opening
		# kernel = np.ones((5,5),np.uint8)
		# thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

		# Invert binarisation
		thresh[thresh == 0] = 127
		thresh[thresh == 255] = 0
		thresh[thresh == 127] = 255
		# X and Y coordinates of centre of iris
		cX = cY = 0

		cv2.imshow("{} Eye threshold".format(eye), thresh)

		cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
		cnts = cnts[0] if imutils.is_cv2() else cnts[1]

		# Check if contour is found
		if len(cnts) == 0:
			return meanAfterGamma, (0, 0)
		c = max(cnts, key=cv2.contourArea)
		if cv2.contourArea(c) > 0:
			# Get center of the contour
			M = cv2.moments(c)
			cX = int(M["m10"] / M["m00"])
			cY = int(M["m01"] / M["m00"])

			arcLength = cv2.arcLength(c, True)
			contourArea = cv2.contourArea(c)
			Pi = 3.14159
			# Calculate approximate radius based on arc length of contour
			# L = 2  * Pi * r
			radiusL = arcLength / (2 * Pi)
			radiusLSquared = radiusL * radiusL
			# Calculate approximate radius based on area of contour
			# A = Pi * r^2
			radiusASquared = contourArea / Pi
			# Use the ratio of radii from arc length and area to determine circularity of contour
			circularity = radiusLSquared / radiusASquared

			# print("radius ratio = {}. x = {}, y = {}. Area = {}, Length = {}".format(radiusRatio, cX, cY, contourArea, arcLength))

			# reject contour if circularity is out of acceptable range
			if circularity > 1.5 or circularity < 0.70:
				return meanAfterGamma, (0, 0)

			cv2.circle(image, (cX, cY), 5, (255, 255, 255), -1)

			cv2.drawContours(image, [c], 0, (0, 255, 0), 1)

		cv2.imshow("{} Eye".format(eye), image)

		cv2.waitKey(1)

		# Return mean intensity after gammma correction,
		# relative x, y coordinates of the centre of the iris
		return meanAfterGamma, (cX / 100, cY / len(image[:]))


## Lab 3
## Hand in at the end of Lab
## Nick Egan, Tues Lab

def main():
	'''
	Main method.  This is where the bulk of the work gets done.
	(You can denote block comments as lines between triple quotes)
	'''

	## 1. Consider the following DNA sequence.  Create two variables that contain
	## the complement of the DNA sequence and the reverse complement of the DNA
	## sequence.  Do this by hand.
	dna = 'CCCATGGTGGGGGGGGGGCAGTCCATAACCC'
	#dna = 'ATAGCATTGC'
	compdna = 'TATCGTAACG'
	revcompdna = 'GCAATGCTAT'
	print('DNA:',dna)
	print('Comp. DNA:',compdna)
	print('Rev. Comp. DNA:',revcompdna)

	## print the output of the two intermediate functions.
	print()
	print('Intermediate Functions:')
	print('complement():',complement(dna))
	print('reverse():',reverse(dna))

	## Compute the reverse complement
	print()
	computedSequence = reverseComplement(dna)

	print('reverseComplement():',computedSequence)

	## 5. Use an IF statement to print 'Correct Answer!' if the computed
	## sequence and the hand-written sequences match and 'Incorrect Answer'
	## otherwise.
	if computedSequence == revcompdna:
		print('Correct Answer!')
	else:
		print('Incorrect Answer...')

	return

## 2. Write a function to return the complement of a string.
def complement(sequence):
	comp = ''
	for i in range(len(sequence)+1):
		letter = sequence[i-1:i]
		if letter == 'A':
			comp = comp + 'T'
		elif letter == 'T':
			comp = comp + 'A'
		elif letter == 'C':
			comp = comp + 'G'
		elif letter == 'G':
			comp = comp + 'C'
	print('<#2 FILL IN HERE>')
	print('comp = ' + comp)
	return comp

## 3. Write a function to return the reverse of a string.
def reverse(sequence):
	rev = ''
	for i in range(len(sequence)):
		letter = sequence[len(sequence) - i-1:len(sequence) - i]
		rev = rev + letter
	print('<#3 FILL IN HERE>')
	print('rev = ' + rev)
	return rev

## 4. Write a function to compute the reverse complement
## of a string.  Return this string.
def reverseComplement(sequence):
	revcomp = ''
	comp = ''
	for i in range(len(sequence)+1):
		letter = sequence[i-1:i]
		if letter == 'A':
			comp = comp + 'T'
		elif letter == 'T':
			comp = comp + 'A'
		elif letter == 'C':
			comp = comp + 'G'
		elif letter == 'G':
			comp = comp + 'C'

	for i in range(len(sequence)):
		letter = comp[len(sequence) - i-1:len(sequence) - i]
		revcomp = revcomp + letter
	print('<#4 FILL IN HERE>')
	print('revcomp = ' + revcomp)
	return revcomp

## The lines below will call the main() function
if __name__ == '__main__':
	main()


#! /usr/bin/python3.6

import os
import sys

import numpy as np

# from PIL import Image
from PIL import Image, ImageDraw, ImageFont

PATH_ROOT_DIR = os.path.abspath(os.path.dirname(sys.argv[0]))+"/"

if __name__ == "__main__":
    print("Hello World!")

    pix = np.zeros((100, 400, 3), dtype=np.uint8)
    img = Image.fromarray(pix)
    draw = ImageDraw.Draw(img)

    # font_name = "joystix monospace.ttf"
    font_name = "kongtext.ttf"
    alphabet_small = "abcdefghijklmnopqrstuvwxytz"
    alphabet_big = alphabet_small.upper()
    col_white = (255, 255, 255)
    # for row in range(0, 6):
    #     y = 10+35*row
    #     x = 20
    #     font_size = 8+row*8
    #     font = ImageFont.truetype(font_name, font_size)
    #     draw.text((x, y), "x: {}, y: {}, font_size: {}, TEst Hello Text!".format(x, y, font_size), col_white, font=font)
    #     draw.text((x, y), "x: {}, y: {}, font_size: {}, TEst Hello Text!".format(x, y, font_size), col_white, font=font)
    def draw_small(x, y):
        draw.text((x, y), "x: {}, y: {}, ".format(x, y)+alphabet_small, col_white, font=font)
    def draw_big(x, y):
        draw.text((x, y), "x: {}, y: {}, ".format(x, y)+alphabet_big, (255, 255, 0), font=font)

    font_size = 8
    font = ImageFont.truetype(font_name, font_size)

    draw_small(16, 33)
    draw_big(24, 8)
    # draw_small(20, 50)

    # draw_small(0, 0)
    # draw_big(0, 8)
    # draw_small(8, 16)
    # draw_big(16, 24)

    # img.show()
    if not os.path.exists("images"):
        os.makedirs("images")
    img.save("images/test_font_sizes.png")


# Two array of bits

a = "1010110111001101101000"
b = "1000011011000000111100110"

n = len(a)
m = len(b)
diff = abs(n - m)

if n > m:
    b.zfill(n)
else:
    a.zfill(m)

ao = [int(i) for i in a]
bo = [int(i) for i in b]
# Initialization

carry = 0
c = []
i = 0

# reverse array for addition
a = ao[::-1]
b = bo[::-1]

# loop invariant
while i < len(a):
    if (a[i] == 1 and b[i] == 0) or (a[i] == 0 and b[i] == 1):
        if carry == 0:
            c.append(1)
        else:
            c.append(0)
            carry = 1
    elif a[i] == 1 and b[i] == 1:
        if carry == 0:
            c.append(0)
            carry = 1
        else:
            c.append(1)
            carry = 1
    else:
        c.append(carry)

    i += 1

# Put carry into the array as MSB
c.append(carry)

# Print the output
print(''.join(str(i) for i in c[::-1]))
#print(''.join(str(i) for i in a).zfill(len(b)))
#print(''.join(str(i) for i in b).zfill(len(a)))


# -*- coding: utf-8 -*-
'''
Задание 7.3a

Сделать копию скрипта задания 7.3.

Дополнить скрипт:
- Отсортировать вывод по номеру VLAN

Ограничение: Все задания надо выполнять используя только пройденные темы.

'''
mac_template = '''{}      {}          {}'''

f = open('CAM_table.txt', 'r')
vlan_raw = []

for lines in f:
    lines = lines.split()
    if 'DYNAMIC' not in lines:
        continue
    else:
        vlan_raw.append(lines)
        #vlan, mac, learn, ports = lines
        #print(mac_template.format(vlan, mac, ports))

result = sorted(vlan_raw, key=lambda x: x[0])

for zaloopa in result:
    a,b,c,d = zaloopa
    print(mac_template.format(a, b, c, d))

f.close()


import scrapy

from scrapy.utils.response import get_base_url

from shiny.util.elements import clean_text_elements

CLASSIFICATION_PERSON = "Person"

class ShinySpider(scrapy.Spider):
    name = "shiny"
    base_url = "http://firefly.wikia.com"
    start_urls = [
        "http://firefly.wikia.com/wiki/Malcolm_Reynolds",
    ]

    def _get_classification(self, parsed_elements):
        """Determine if this page should be parsed or skipped.

        Based on the infobox DOM element.

        """
        for key in parsed_elements.keys():
            if key == "Gender":
                return CLASSIFICATION_PERSON

        return None

    def parse(self, response):
        attributes = {}

        title_element = response.css(".header-title h1::text").extract()

        if len(title_element) == 0:
            return

        attributes["title"] = title_element[0]

        infobox = response.css("table.infoboxtable")
        if len(infobox) == 0:
            self.logger.error("no infobox present for page")
            return

        for row in infobox.css("tr"):
            data_nodes = row.css("td")

            if len(data_nodes) < 2:
                continue

            key_selector = data_nodes[0].css("::text").extract()
            value_selector = data_nodes[1].css("::text").extract()

            key = clean_text_elements(key_selector)
            if key is None:
                self.logger.error("unable to clean key " + key_selector)
                continue

            value = clean_text_elements(value_selector)
            if value is None:
                self.logger.error("unable to clean value " + value_selector)
                continue

            attributes[key] = value

        for href in response.css("#WikiaArticle a::attr(href)"):
            extr = href.extract()
            if not extr.startswith("/"):
                self.logger.warn("not following url: " + extr)
                continue
            if "?" in extr:
                spl = extr.split("?")
                extr = spl[0]
            yield scrapy.Request(
                self.base_url + extr, callback=self.parse)

        clfc = self._get_classification(attributes)
        if clfc is None:
            self.logger.error("unable to determine classification page: {0}"
                              .format(attributes))
            return

        url = get_base_url(response)
        attributes["WIKI_URL"] = url

        yield attributes


from app import app, db
from app.models import Posts
from datetime import datetime
import json
with open('wechat_data.json', 'r') as f:
    wechat_data = json.load(f)

for item in wechat_data:
    post = Posts()
    post.title = item["title"]
    post.abstract = item["digest"]
    post.link = item["link"]
    post.create_time = datetime.fromtimestamp(item["create_time"])
    db.session.add(post)
    db.session.commit()


#python3 内置函数demo

#sorted() 函数对所有可迭代的对象进行排序操作。
#sort 与 sorted 区别：
#sort 是应用在 list 上的方法，sorted 可以对所有可迭代的对象进行排序操作。
#list 的 sort 方法返回的是对已经存在的列表进行操作，而内建函数 sorted 方法返回的是一个新的 list，而不是在原来的基础上进行的操作。

def sortedDemo(listA):
    listB = sorted(listA)
    return listB

if __name__ == "__main__":
    a = [5,4,9,2,1,6,7]
    print(sortedDemo(a))


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

import numpy as np
from sklearn.neighbors.classification import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import SGDClassifier
from sklearn.svm import SVC
from sklearn.model_selection import KFold
from sklearn.preprocessing import scale

D = np.loadtxt("Banco de Dados - Infarto - Dentro-da-Amostra.txt")

X = D[:,0:3]
Y = D[:,3:4].reshape(D.shape[0],).astype(int)

# fix folds for every model, for a fair selection
kf = KFold(n_splits=10, shuffle=True, random_state=77)

models = []

for k in range(1,11):
    m = KNeighborsClassifier(n_neighbors=k)
    models.append( (m, "knn_%d" % k) )

clf = GaussianNB()
models.append( (clf, "naive_bayes") )

sgd = SGDClassifier(loss="perceptron", eta0=1, learning_rate="constant", penalty=None)
models.append( (sgd, "perceptron") )

sgd = SGDClassifier(loss="perceptron", eta0=1, learning_rate="constant", penalty="l1")
models.append( (sgd, "perceptron+l1") )

sgd = SGDClassifier(loss="perceptron", eta0=1, learning_rate="constant", penalty="l2")
models.append( (sgd, "perceptron+l2") )

sgd = SGDClassifier(loss="hinge", penalty=None)
models.append( (sgd, "linear_svm") )

sgd = SGDClassifier(loss="hinge", penalty="l1")
models.append( (sgd, "linear_svm+l1") )

sgd = SGDClassifier(loss="hinge", penalty="l2")
models.append( (sgd, "linear_svm+l2") )

sgd = SGDClassifier(loss="log", penalty=None)
models.append( (sgd, "log_regression") )

sgd = SGDClassifier(loss="log", penalty="l1")
models.append( (sgd, "log_regression+l1") )

sgd = SGDClassifier(loss="log", penalty="l2")
models.append( (sgd, "log_regression+l2") )

for c in np.arange(0.5, 2, 0.1):

    clf = SVC(C=c, kernel="rbf")
    models.append( (clf, "rbf_svm_C%.1f" % c) )

for (model, name) in models:
    scores = []
    for train_index, test_index in kf.split(X):
        model.fit(scale(X[train_index]), Y[train_index])
        score = model.score(scale(X[test_index]), Y[test_index])
        scores.append(score)

    #print("--------", name, "--------")
    #print("precision\t\t", np.mean(scores))
    #print("standard deviation\t", np.std(scores))
    #print("\n")
    print(name, "& $", "%.4f" % np.mean(scores), "$ & $", "%.4f" % np.std(scores), "$\\\\")


my_bag = 'shiny gold'

def main():
    bags = [my_bag]
    with open('input', 'r') as file:
        lines = file.read().splitlines()
        idx = 0
        while idx < len(bags):
            bag = bags[idx]
            for line in lines:
                if bag in line:
                    nextBag = ' '.join(line.split(' ')[:2])
                    if nextBag not in bags:
                        bags.append(nextBag)
            idx += 1
    return len(bags) - 1

if __name__ == "__main__":
    print(main())


import time
import random

def print_msg(msg):
    print(msg)
    time.sleep(2)

def start(item, option):
    print_msg("You find yourself standing in an open field, filled "
                "with grass and yellow wildflowers.\n")
    print_msg("Rumor has it that a "+str(option)+" is somewhere around "
                "here, and has been terrifying the nearby village.\n")
    print_msg("In front of you is a house.\n")
    print_msg("To your right is a dark Cave.\n")
    print_msg("In your hand you hold your trusty (but not very "
                "effective) dagger.\n")

def game_cave(item, option):
    if "sword" in item:
        print_msg("\nYou peer cautiously into the game_cave.")
        print_msg("\nYou've been here before, and gotten all"
                    " the good stuff. It's just an empty Cave"
                    " now.")
        print_msg("\nYou walk back to the field.\n")
    else:
        print_msg("\nYou peer cautiously into the Cave.")
        print_msg("\nIt turns out to be only a very small Cave.")
        print_msg("\nYour eye catches a glint of metal behind a "
                    "rock.")
        print_msg("\nYou have found the magical Sword of Ogoroth!")
        print_msg("\nYou discard your silly old dagger and take "
                    "the sword with you.")
        print_msg("\nYou walk back out to the field.\n")
        item.append("sword")
    field(item, option)

def game_door(item, option):
    print_msg("\nYou approach the game_door of the house.")
    print_msg("\nYou are about to knock when the game_door "
                f"opens and out steps a {option}.")
    print_msg(f"\nEep! This is the {option}'s house!")
    print_msg(f"\nThe {option} attacks you!\n")
    if "sword" not in item:
        print_msg("You feel a bit under-prepared for this, "
                    "what with only having a tiny dagger.\n")
    while True:
        response = input("Would you like to (1) fight or (2) run away?")
        if response == "1":
            if "sword" in item:
                print_msg(f"\nAs the {option} moves to attack, "
                            "you unsheath your new sword.")
                print_msg("\nThe Sword of Ogoroth shines brightly in "
                            "your hand as you brace yourself for the "
                            "attack.")
                print_msg(f"\nBut the {option} takes one look at "
                            "your shiny new toy and runs away!")
                print_msg(f"\nYou have rid the town of the {option}"
                            ". You are victorious!\n")
            else:
                print_msg("\nYou do your best...")
                print_msg(f"but your dagger is no match for the {option}.")
                print_msg("\nYou have been defeated!\n")
            play_again()
            break
        if response == "2":
            print_msg("\nYou run back into the field. "
                        "\nLuckily, you don't seem to have been "
                        "followed.\n")
            field(item, option)
            break

def field(item, option):
    print_msg("Enter 1 to knock on the Door of the house.")
    print_msg("Enter 2 to peer into the Cave.")
    print_msg("What would you like to do?")
    while True:
        response = input("(Please enter 1 or 2.)\n")
        if response == "1":
            game_door(item, option)
            break
        elif response == "2":
            game_cave(item, option)
            break

def play_again():
    again = input("Would you like to play again? (y/n)").lower()
    if again == "y":
        print_msg("\n\n\nExcellent! Restarting the game ...\n\n\n")
        play()
    elif again == "n":
        print_msg("\n\n\nThanks for playing! See you next time.\n\n\n")
    else:
        play_again()

def play():
    item = []
    option = random.choice(["dracula", "pirate", "dragon", "troll",
                            "thief"])
    start(item, option)
    field(item, option)

play()


# coding utf-8

import logging
import sys
from MainSpider import FunPaySpider
from ModelDB import init_tables, Parsings
from datetime import datetime, timedelta
from time import sleep

def GetScanSchedule():
    interval_scan = timedelta(hours=2)
    lastParseTime = Parsings.select().order_by(Parsings.id.desc()).get().time
    timeStart = lastParseTime + interval_scan
    nextTimeParse = timeStart - datetime.now()
    return (timeStart, nextTimeParse)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(message)s',
                        datefmt='%H:%M:%S')
    init_tables()
    if len(sys.argv) > 1 and sys.argv[1] == 'forced':
        logging.warning('spider run once and forced')
        bot = FunPaySpider()  # thread_number=2
        bot.run()
        sys.exit(0)

    failedScan = False
    timeStart, nextTimeParse = GetScanSchedule()
    logging.info('scan was started through ' + str(round(nextTimeParse.seconds / 60)) + ' minutes in ' +
                 timeStart.strftime('%H:%M:%S'))
    while True:
        timeStart, nextTimeParse = GetScanSchedule()
        if datetime.now() > timeStart or failedScan:
            failedScan = False
            bot = FunPaySpider()  # thread_number=2
            bot.run()
            timeStart, nextTimeParse = GetScanSchedule()
            if bot.gameCount == 0:
                logging.info('scan failed repeat after 2 min')
                sleep(120)
                failedScan = True
            else:
                logging.info(
                    'Parse ' + str(bot.gameCount) + ' games finish,\nadded ' + str(bot.dataCount) + ' data rows\n' +
                    'New users ' + str(bot.userCount) + ' was added\n' +
                    'Next scan through ' + str(round(nextTimeParse.seconds / 60)) + ' minutes in ' +
                    timeStart.strftime('%H:%M:%S'))
        else:
            sleep(0.5)


"""
idea: heapq to get Kth element without the help of sorted operation(O(NlogN))
- python: heapq function, heapify a list to heap inplace without return
- stream means that we need a function of add to operate an array due to the dynamic change
- we need to maintain a heap with k elements
"""

import heapq

class KthLargest:
    def __init__(self, k: int, nums: List[int]):
        self.k = k
        self.que = nums
        heapq.heapify(self.que)

    def add(self, val):
        """
        :type val: int
        :rtype: int
        """
        heapq.heappush(self.que, val)
        while len(self.que) > self.k:
            heapq.heappop(self.que)
        return self.que[0]


import sys
import os
import re
from typing import Tuple, List, Any, Generator

class BaseSolution(object):
    __parameters: List[str]
    __base: str
    report_time: callable

    def __init__(self, _base: str, report_time: callable, _parameters: List[str]):
        self.__base = _base
        self.report_time = report_time
        self.__parameters = _parameters

    def setup(self):
        pass

    def part1(self):
        raise NotImplementedError('Solution is not implemented yet!')

    def part2(self):
        raise NotImplementedError('Solution is not implemented yet!')

    def parameters(self, parameter_types: Tuple = (str,),
                   names: Tuple = ("Input File", ),
                   default: Tuple = ('input', )) -> List[Any]:
        try:
            if len(default) != len(parameter_types) or len(names) != len(parameter_types):
                print("Wrong function call: ({}, {}, {})".format(parameter_types, names, default))
                sys.exit(-1)

            number_of_nones = default.count(None)
            if len(self.__parameters) < number_of_nones:
                raise Exception("Wrong number of arguments!")

            for i in range(len(self.__parameters), len(parameter_types)):
                self.__parameters.append(default[i])

            return list([t(v) if (not hasattr(v, '__iter__') or t == str) else t(*v)
                         for t, v in zip(parameter_types, self.__parameters[0:len(parameter_types)])])

        except Exception as exp:
            print(f" !!! {exp}")
            max_name_length = max([len(v) for v in names]) + 3
            template = f'%{max_name_length}s %s = %s'
            pairs = zip(names, parameter_types, [v or "(required)" for v in default])
            ktv = [template % (k, t, v) for k, t, v in pairs]
            print("Required parameters:\n\t{}".format('\n\t'.join(ktv)))
            sys.exit(-1)

    def read_input(self, path: str,
                   separator: str = '\n',
                   ignore: List = None) -> Generator[str, None, None]:

        if ignore is None:
            ignore = ['']

        path = os.path.join(self.__base, path)

        with open(path) as f:
            content: List[str] = f.read().split(separator)
            for section in content:
                if section not in ignore:
                    yield section

    @staticmethod
    def parse(content: str,
              search_pattern: str,
              types: Tuple) -> List[Any]:
        try:
            search_result = re.search(re.compile(search_pattern), content)
            if search_result is None:
                raise Exception('No match!')
            return list([t(v) for t, v in zip(types, search_result.groups())])
        except Exception as exp:
            print(f'!!! Parse error {exp}')
            print(f'Content: {content}')
            print(f'Pattern: {search_pattern}')
            sys.exit(-1)


from django import template
import requests
from bs4 import BeautifulSoup

register = template.Library()

BLOG_URL = 'http://feeds.feedburner.com/agroguia'
def blog_post_brief(index):
    r = requests.get(BLOG_URL)
    rss = BeautifulSoup(r.content, "xml")
    item = rss.find_all('item')[index]
    return {
        'title': item.find('title').get_text(),
        'description': item.find('description').get_text() ,
        'link': item.find('link').get_text() ,
        'creator': item.find('creator').get_text()
    }

register.inclusion_tag('_blog_post.html')(blog_post_brief)


from ast import arg
from pyexpat import model
from MODEL import (
    CNN1d,
    MLP,
    LARS,
    SVR,
    KNN,
    RF,
    AdaBoost,
    XGBoost,
    Bayes
)
import matplotlib.pyplot as plt
import os
import numpy as np
from DATALOADER.read_data import (
    v_names,
    v_outp,
    v_inp,
    doubles,
    singles,
    barr,
    double_mut,
    single_mut,
    todo,
    siteall
)

# integrate all models needed
class Trainer:
    def __init__(self, args, x_train, y_train, x_test, y_test):
        self.args = args
        self.x_train, self.y_train = np.expand_dims(x_train, axis=-1), y_train
        self.x_test, self.y_test = np.expand_dims(x_test, axis=-1), y_test
        if args.model == 'cnn':
            self.model = CNN1d(args=args)
            self.model.build()
        elif args.model == 'mlp':
            self.model = MLP(args=args)
            self.model.build()
        elif args.model == 'lars':
            self.model = LARS(args=args)
        elif args.model == 'svr':
            self.model = SVR(args=args)
        elif args.model == 'knn':
            self.model = KNN(args=args)
        elif args.model == 'rf':
            self.model = RF(args=args)
        elif args.model == 'adaboost':
            self.model = AdaBoost(args=args)
        elif args.model == 'xgboost':
            self.model = XGBoost(args=args)
        elif args.model == 'bayes':
            self.model = Bayes(args=args)
        else:
            ValueError(f'No implemented model {args.model}')
        self.history = self.results = None

    def train(self):
        print(type(self.y_train), self.y_train.dtype)
        # return a dict
        self.history = self.model.train(
            x_train=self.x_train,
            y_train=self.y_train
        )

    def test(self):
        self.results = self.model.test(x_test=self.x_test, y_test=self.y_test)
        print("test loss, test mse:", self.results)

    def vis(self):
        if not os.path.exists(self.args.out_dir):
            os.makedirs(self.args.out_dir)
        # plot loss curve
        plt.plot(self.history['loss'], label='Train', c='r')
        plt.plot(self.history['val_loss'], label='Val', c='b')
        plt.title(f'{self.args.model} loss')
        plt.ylabel('Loss')
        plt.xlabel('Epoch')
        plt.legend(loc='upper left')
        plt.savefig(f'{self.args.out_dir}/{str(self.args.model)}_Loss.jpg')

        # plot mse curve
        plt.cla()
        plt.plot(self.history['mse'], label='Train', c='r')
        plt.plot(self.history['val_mse'], label='Val', c='b')
        plt.title(f'{self.args.model} MSE')
        plt.ylabel('Mse')
        plt.xlabel('Epoch')
        plt.legend(loc='upper left')
        plt.savefig(f'{self.args.out_dir}/{str(self.args.model)}_MSE.jpg')

        if self.args.model == 'xgboost':
            self.model.vis()

    # training and redict the output top10 and bottom10
    def metrics(self):
        names = np.empty((0, 2), dtype=str)
        value = np.empty((0, 18, 1), dtype=float)

        if doubles:
            for n in range(len(barr[0])-1):
                for m in range(len(barr)-1):
                    if n == m :
                        continue
                    for n2 in range(len(barr[0])-1):
                        if n <= n2 :
                            continue
                        for m2 in range(len(barr)-1):
                            if n2 == m2 :
                                continue
                            value = np.append(value,double_mut(barr[0][n+1] + barr[m+1][0],barr[0][n2+1] + barr[m2+1][0]),axis=0)
                            names = np.append(names,[[barr[0][n+1] + barr[m+1][0], barr[0][n2+1] + barr[m2+1][0]]],axis=0)

        if singles:
            for i in todo:
                for s in siteall(i):
                    names = np.append(names,[[s,'']],axis=0)
                    value = np.append(value,single_mut(s),axis=0)

        tmp = self.model.predict(value.reshape(-1, 18)).reshape(-1, 1)
        value = np.empty((len(names), 2))
        for n in range(len(names)):
            value[n][0] = tmp[2*n]
            value[n][1] = tmp[2*n+1]

        f = open("prediction_nativ.dat", "w")
        for n in range(len(names)):
            f.write("{0:10s} {1:10s} {2:6.2f} {3:6.2f}\n".format(names[n][0],names[n][1],value[n][0],value[n][1]))
        f.close()

        means = np.mean(value, axis=1)
        names = names[np.argsort(means)]
        value = value[np.argsort(means)]

        # print top 10 and bottom 10 for validation
        print("Top 10:")
        for n in range(10):
            print("{0:10s} {1:10s} {2:6.2f} {3:6.2f}".format(names[n][0],names[n][1],value[n][0],value[n][1]))

        print("Bottom 10:")
        for n in range(10):
            print("{0:10s} {1:10s} {2:6.2f} {3:6.2f}".format(names[-n-1][0],names[-n-1][1],value[-n-1][0],value[-n-1][1]))

        f = open("prediction.dat", "w")
        for n in range(len(names)):
            f.write("{0:10s} {1:10s} {2:6.2f} {3:6.2f}\n".format(names[n][0],names[n][1],value[n][0],value[n][1]))
        f.close()

    # validating the predict value and real value
    def evaluate(self):
        x = self.x_test
        if self.args.model != 'cnn':
            x = np.squeeze(self.x_test)
        pred = self.model.predict(x)
        length = len(pred)
        pred = pred.reshape(length, 1)
        print(type(pred), pred.shape)
        mse = 0
        a1 = 0
        b1 = 0
        b2 = 0
        for i in range(int(len(v_outp) / 2)):
            print(
                "{0:10s} {1:10s} {2:6.2f} {3:6.2f} {4:6.2f}"
                    .format(v_names[2 * i], v_names[2 * i + 1],
                            pred[2 * i][0], pred[2 * i + 1][0],
                            v_outp[2 * i]))
            mse += ((pred[2 * i][0] + pred[2 * i + 1][0]) / 2 - v_outp[2 * i]) ** 2
            a1 += (pred[2 * i][0] - np.mean(pred)) * (v_outp[2 * i] - np.mean(v_outp)) + (
                    pred[2 * i + 1][0] - np.mean(pred)) * (v_outp[2 * i] - np.mean(v_outp))
            b1 += (pred[2 * i][0] - np.mean(pred)) ** 2 + (pred[2 * i + 1][0] - np.mean(pred)) ** 2
            b2 += (v_outp[2 * i] - np.mean(v_outp)) ** 2 * 2
        print("RMSD:    {0:6.2f}".format(np.sqrt(mse / len(v_outp))))
        print("Pearson: {0:6.2f}".format(a1 / (np.sqrt(b1) * np.sqrt(b2))))
        plt.cla()
        plt.scatter(v_outp, pred, c='r')
        plt.plot((min(v_outp), max(v_outp)), (min(v_outp), max(v_outp)), label='refer', c='b')
        plt.legend('upper left')
        plt.xlabel("calc.")
        plt.ylabel("pred.")
        plt.title("Scatter plot of True vs Predictions({})".format(
            {str(self.args.model)}))
        plt.savefig(
            f'{self.args.out_dir}/{str(self.args.model)}_image.jpg')
        with open("validation.dat", "w") as f:
            for i in range(int(len(v_outp) / 2)):
                f.write(
                    "{0:10s} {1:10s} {2:6.2f} {3:6.2f}\n"
                        .format(v_names[2 * i], v_names[2 * i + 1],
                                (pred[2 * i][0] + pred[2 * i + 1][0]) / 2,
                                v_outp[2 * i])
                )


import os
import xml.dom.minidom

import wx
import wx.xrc

from medipy.base import find_resource
from medipy.gui.control import Int

class CineDialog(wx.Dialog):
    """ Display the dialog to control the cine mode of images
    """

    def __init__(self, parent, wx_id=wx.ID_ANY,
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.DEFAULT_DIALOG_STYLE, name="cine_dialog"):

        self._timer = wx.Timer()
        self._delay = None
        self._image = None
        self._current_layer = 0
        self._status = "stop"

        super(CineDialog, self).__init__(parent, wx_id, "", pos, size, style, name)

        # Resources
        self._play_bitmap = wx.Bitmap(find_resource(
            os.path.join("resources", "gui", "media-playback-start.png")))
        self._pause_bitmap = wx.Bitmap(find_resource(
            os.path.join("resources", "gui", "media-playback-pause.png")))
        stop_bitmap = wx.Bitmap(find_resource(
            os.path.join("resources", "gui", "media-playback-stop.png")))

        # Widgets
        self._layer = Int(self, 1, (1,1))
        self._speed = Int(self, 1, (1, 20))

        self._play_pause = wx.BitmapButton(self, wx.ID_ANY, self._play_bitmap)
        self._stop = wx.BitmapButton(self, wx.ID_ANY, stop_bitmap)
#        self.close = wx.Button(self, wx.ID_ANY, "Close")

        # Layout
        layer_speed_sizer = wx.FlexGridSizer(2, 2)
        layer_speed_sizer.AddGrowableCol(1)

        layer_speed_sizer.Add(wx.StaticText(self, wx.ID_ANY, "Layer : "), flag=wx.ALIGN_CENTER)
        layer_speed_sizer.Add(self._layer, 1)

        layer_speed_sizer.Add(wx.StaticText(self, wx.ID_ANY, "Speed : "), flag=wx.ALIGN_CENTER)
        layer_speed_sizer.Add(self._speed, 1)

        buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
        buttons_sizer.Add(self._play_pause)
        buttons_sizer.Add(self._stop)
#        buttons_sizer.AddStretchSpacer(1)
#        buttons_sizer.Add(self.close, flag=wx.ALIGN_CENTER)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(layer_speed_sizer, 0, wx.EXPAND)
        main_sizer.Add(buttons_sizer, 0, wx.EXPAND)

        self.SetSizer(main_sizer)
        main_sizer.SetSizeHints(self)

        # Events
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self._timer.Bind(wx.EVT_TIMER, self.OnTimer)

        self._layer.add_observer("value", self.on_layer)
        self._speed.add_observer("value", self.on_speed)
        self._play_pause.Bind(wx.EVT_BUTTON, self.OnPlayPauseClicked)
        self._stop.Bind(wx.EVT_BUTTON, self.OnStopClicked)

        self._set_fps(self._speed.value)

    def play(self):
        layer = (self._current_layer+1)%len(self._image.layers)
        self.select_layer(layer)

        self._set_fps(self._speed.value)
        self._timer.Start(self._delay)
        self._status = "play"
        self._play_pause.SetBitmapLabel(self._pause_bitmap)

    def pause(self):
        self._timer.Stop()
        self._status = "pause"
        self._play_pause.SetBitmapLabel(self._play_bitmap)

    def stop(self):
        self._timer.Stop()
        if self._image is not None :
            self.select_layer(0)
            for i in range(self._image.number_of_layers) :
                self._image.set_layer_visibility(i, True)
            self._image.render()
        self._status = "stop"
        self._play_pause.SetBitmapLabel(self._play_bitmap)

    def select_layer(self, layer):
        self._current_layer = layer
        if self._image is not None :
            for i in range(len(self._image.layers)) :
                self._image.set_layer_visibility(i, False)
            self._image.set_layer_visibility(self._current_layer, True)
            self._image.render()
        self._layer.value = self._current_layer+1

    ##############
    # Properties #
    ##############

    def _get_image(self):
        return self._image

    def _set_image(self, image):
        # Restore previous image
        if self._image is not None :
            self.stop()

        self._image = image
        self._layer.range = (1, self._image.number_of_layers)
        self.select_layer(0)

    image = property(lambda x:x._image, _set_image)

    ##################
    # Event handlers #
    ##################

    def OnClose(self, event):
        if self._image is not None :
            self.stop()
        event.Skip()

    def OnTimer(self, event):
        layer = (self._current_layer+1)%len(self._image.layers)
        self.select_layer(layer)

    def OnPlayPauseClicked(self, event):
        if self._status in ["pause", "stop"] :
            self.play()
        else :
            self.pause()

    def OnStopClicked(self, event):
        self.stop()

    def on_layer(self, event):
        self.select_layer(self._layer.value-1)

    def on_speed(self, event):
        self._set_fps(self._speed.value)

    #####################
    # Private interface #
    #####################

    def _set_fps(self, fps):
        self._delay = 1000. / float(fps)
        if self._timer.IsRunning() :
            self._timer.Start(self._delay)


import time

import cv2
import tqdm
from utils import Dataloader_2,Dataloader_1
import yaml
import numpy as np
import os

def getpathlist(path):
    # 找比较好的数据集图像
    plist = os.listdir(path)
    l = []
    for i,paths in tqdm.tqdm(enumerate(plist)):
        p = '/'.join([path,paths])
        img = cv2.imread(p)
        # print(np.sum(img[:,:,1]))
        if np.sum(img[:,:,1]) > 50000 * 255 or np.sum(img[:,:,2]) > 50000 * 255:
            l.append(paths)
    return l

def readboxfile(path):
    with open(path,'r') as f:
        file = f.read()
    boxes = file.split('\n')[:-1]
    anchor = {}
    for box in boxes:
        x = box.split(' ')[0]
        p = box.split(' ')[1:-1]
        y = []
        for v in p:
            y.append(int(v))
        y = np.array(y)
        y = np.reshape(y,[-1,2])
        try:
            anchor[int(x)].append(y)
        except:
            anchor[int(x)] = [y]
    return anchor

if __name__ == '__main__':
    # d = Dataloader_2(r'F:\PycharmProject\sweetdetect\config\config.yaml')
    # with open('config/path.txt','r') as f:
    #     i = 0
    #     path = []
    #     p = f.read().split('\n')[:-1]
    # d.setfilelist(p)
    # d.savedataset(r'F:\dataset\image_cut', 0)

    # listp = getpathlist(r'F:\dataset\labels')
    # with open('config/path.txt','w') as f:
    #     for path in listp:
    #         f.write(path)
    #         f.write('\n')
    # print(len(listp))

    # anchor = readboxfile(r'F:\dataset\image_cut\bonirob_2016-05-17-11-42-26_14_frame120.txt')
    # label = cv2.imread(r'F:\dataset\labels\bonirob_2016-05-17-11-42-26_14_frame120.png')
    # label = cv2.resize(label,[480,320])
    # c = [[0,255,255],[255,255,0]]
    # for k,boxes in anchor.items():
    #     for box in boxes:
    #         cv2.rectangle(label,box[0],box[1],c[k],3)
    # cv2.imshow('label',label)
    # cv2.waitKey(0)

    # d = Dataloader_2(r'F:\PycharmProject\sweetdetect\config\config.yaml')
    # with open('config/path.txt','r') as f:
    #     i = 0
    #     path = []
    #     p = f.read().split('\n')[:-1]
    # d.setfilelist(p)
    # for i in tqdm.tqdm(range(len(p)//4)):
    #     d.savedataset(r'F:\dataset\image_cut', i)

    d = Dataloader_1(r'F:\PycharmProject\sweetdetect\config\config.yaml')
    with open('config/path.txt', 'r') as f:
        i = 0
        path = []
        p = f.read().split('\n')[:-1]
    d.setfilelist(p)
    image,mask,label = next(d)
    img = mask[1].squeeze().numpy() * 255
    img = np.uint8(img)
    # img = img.transpose([1,2,0])
    cv2.imshow('2',img)
    cv2.waitKey(0)


'''
input start time and case name
draw one plot and then make animation
use ls filename to do cycle

20210925
'''
import xarray as xr
import numpy as np
import subprocess
import os
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from matplotlib import colors
import cartopy.crs as ccrs
import cartopy.feature as cfeat
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER
from cartopy.io.shapereader import Reader
import cmaps
from PIL import Image, ImageDraw, ImageSequence
from wrf import (to_np, getvar, smooth2d, get_cartopy, cartopy_xlim,
                         cartopy_ylim, latlon_coords)
from copy import copy
import shapely.geometry as sgeom

def find_side(ls, side):
    """
    Given a shapely LineString which is assumed to be rectangular, return the
    line corresponding to a given side of the rectangle.
    """
    minx, miny, maxx, maxy = ls.bounds
    points = {'left': [(minx, miny), (minx, maxy)],
            'right': [(maxx, miny), (maxx, maxy)],
            'bottom': [(minx, miny), (maxx, miny)],
            'top': [(minx, maxy), (maxx, maxy)],}
    return sgeom.LineString(points[side])

def lambert_xticks(ax, ticks):
    """Draw ticks on the bottom x-axis of a Lambert Conformal projection."""
    te = lambda xy: xy[0]
    lc = lambda t, n, b: np.vstack((np.zeros(n) + t, np.linspace(b[2], b[3], n))).T
    xticks, xticklabels = _lambert_ticks(ax, ticks, 'bottom', lc, te)
    ax.xaxis.tick_bottom()
    ax.set_xticks(xticks)
    ax.set_xticklabels([ax.xaxis.get_major_formatter()(xtick) for xtick in xticklabels])

def lambert_yticks(ax, ticks):
    """Draw ticks on the left y-axis of a Lamber Conformal projection."""
    te = lambda xy: xy[1]
    lc = lambda t, n, b: np.vstack((np.linspace(b[0], b[1], n), np.zeros(n) + t)).T
    yticks, yticklabels = _lambert_ticks(ax, ticks, 'left', lc, te)
    ax.yaxis.tick_left()
    ax.set_yticks(yticks)
    ax.set_yticklabels([ax.yaxis.get_major_formatter()(ytick) for ytick in yticklabels])

def _lambert_ticks(ax, ticks, tick_location, line_constructor, tick_extractor):
    """Get the tick locations and labels for an axis of a Lambert Conformal projection."""
    outline_patch = sgeom.LineString(ax.outline_patch.get_path().vertices.tolist())
    axis = find_side(outline_patch, tick_location)
    n_steps = 30
    extent = ax.get_extent(ccrs.PlateCarree())
    _ticks = []
    for t in ticks:
        xy = line_constructor(t, n_steps, extent)
        proj_xyz = ax.projection.transform_points(ccrs.Geodetic(), xy[:, 0], xy[:, 1])
        xyt = proj_xyz[..., :2]
        ls = sgeom.LineString(xyt.tolist())
        locs = axis.intersection(ls)
        if not locs:
            tick = [None]
        else:
            tick = tick_extractor(locs.xy)
        _ticks.append(tick[0])
    # Remove ticks that aren't visible:
    ticklabels = copy(ticks)
    while True:
        try:
            index = _ticks.index(None)
        except ValueError:
            break
        _ticks.pop(index)
        ticklabels.pop(index)
    return _ticks, ticklabels

# constants
BIGFONT=22
MIDFONT=18
SMFONT=14

# define date of start time and forcast time
stime  = ['2021','06','22'] # year, month, date, hour
ftime1 = ['2021','06','23','08']
ftime2 = ['2021','06','24','08']

figtle = "Precipitation (mm/hour)"
case = ['Njord','PATH']
domin = ['03','04']
nc = 0

path = '/home/lzhenn/cooperate/data'
filedir= [path+'/Njord/'+''.join(stime)+'/',\
          path+'/PATH/'+''.join(stime[0:2])+'/'+''.join(stime)+'12/']
coast_shp = Reader(os.getenv('SHP_LIB')+'/china_coast/china_coastline.dbf').geometries()

# contour level for precip
cnlevels = [0.1, 0.5, 1, 2, 3, 5, 8, 12, 16, 20, 25, 30, 40, 50, 70, 100, 150]
norm = colors.BoundaryNorm(boundaries=cnlevels, ncolors=cmaps.precip2_17lev.N,extend='both')

fn_stream = subprocess.check_output('ls '+filedir[nc]+'wrfout_d'+domin[nc]+'_*', shell=True).decode('utf-8')
fn_list   = fn_stream.split()
print(fn_list[0])
print('filenumber : '+str(len(fn_list)))

ncfile = Dataset(fn_list[0])
var    = getvar(ncfile, "RAINC")
varnp1 = to_np(getvar(ncfile,"RAINC")) + to_np(getvar(ncfile,"RAINNC"))
nproj  = str(var.attrs['projection']).split('(')
print(nproj)

# Get the latitude and longitude points
lats, lons = latlon_coords(var)

# Get the cartopy mapping object
cart_proj = get_cartopy(var)

for itm in fn_list[1:]:
    fn_splt = itm.split('_')
    print(fn_splt) #['wrfout', 'd03', '2021-06-22', '11:00:00']
    figtle = case[nc]+' '+fn_splt[2]+' '+fn_splt[3]+' precip(mm/h)'
    figdir = '/home/lzhenn/cooperate/fig/'+case[nc]+'.S'+''.join(stime)+ \
             '.F'+''.join(fn_splt[2].split('-'))+'-'+fn_splt[3]+'.pr.png'

    # Open the NetCDF file
    ncfile = Dataset(itm)
    varnp  = to_np(getvar(ncfile,"RAINC")) + to_np(getvar(ncfile,"RAINNC"))-varnp1
    varnp1 = to_np(getvar(ncfile,"RAINC")) + to_np(getvar(ncfile,"RAINNC"))

    fig = plt.figure(figsize=(9,9))

    # Set the GeoAxes to the projection used by WRF
    axe = fig.add_axes([0.08, 0.05, 0.8, 0.94], projection=cart_proj)
    #axe = plt.axes(projection=cart_proj)
    #axe.add_geometries(coast_shp, ccrs.PlateCarree(),facecolor='none', edgecolor='grey',linewidth=.5, zorder = 1)
    #axe.add_feature(cfeat.COASTLINE.with_scale('10m'), linewidth=1,color='k')
    coast_shp = Reader(os.getenv('SHP_LIB')+'/china_coast/china_coastline.dbf').geometries()
    coastline = cfeat.ShapelyFeature(coast_shp, ccrs.PlateCarree(), edgecolor='grey', facecolor='none')
    axe.add_feature(coastline, linewidth=0.5)

    plt.contourf(to_np(lons), to_np(lats), varnp, cnlevels,
                 transform=ccrs.PlateCarree(),cmap=cmaps.precip2_17lev,norm=norm,extend='both')

    plt.colorbar(ax=axe, shrink=.58)

    # Set the map bounds
    if nproj[0] == 'LambertConformal':
        fig.canvas.draw()
        # Define gridline locations and draw the lines using cartopy's built-in gridliner:
        xticks = list(np.arange(np.ceil(lons[0,0]+4.0),np.floor(lons[0,-1]+4.0),0.5))
        yticks = list(np.arange(np.ceil(lats[0,0]+4.0),np.floor(lats[-1,0]+4.0),0.5))
        axe.gridlines(xlocs=xticks, ylocs=yticks)
        # Label the end-points of the gridlines using the custom tick makers:
        axe.xaxis.set_major_formatter(LONGITUDE_FORMATTER)
        axe.yaxis.set_major_formatter(LATITUDE_FORMATTER)
        # xticks and yticks only is list
        lambert_xticks(axe, xticks)
        lambert_yticks(axe, yticks)
    else:
        axe.set_xlim(cartopy_xlim(var))
        axe.set_ylim(cartopy_ylim(var))
        axe.set_xticks(np.arange(np.ceil(lons[0,0]),np.floor(lons[0,-1]),1.0), crs=ccrs.PlateCarree())
        axe.set_yticks(np.arange(np.ceil(lats[0,0]),np.floor(lats[-1,0]),1.0), crs=ccrs.PlateCarree())
        axe.xaxis.set_major_formatter(LONGITUDE_FORMATTER)
        axe.yaxis.set_major_formatter(LATITUDE_FORMATTER)
        axe.xaxis.set_major_formatter(LongitudeFormatter())
        axe.yaxis.set_major_formatter(LatitudeFormatter())

    plt.title(figtle,fontsize=SMFONT)
    plt.savefig(figdir)
    plt.show()
    del fig, axe

fn_stream = subprocess.check_output('ls /home/lzhenn/cooperate/fig/'+case[nc]+'.S'+''.join(stime)+'*.pr.png', shell=True).decode('utf-8')
fn_list   = fn_stream.split()
print(fn_list[0])
print('filenumber : '+str(len(fn_list)))
gif_name = './2.gif'

frames = []
for itm in fn_list:
    frame = Image.open(itm)
    frames.append(frame)

frames[0].save(gif_name, save_all=True, append_images=frames[1:],\
            duration = 1000, loop=0, disposal=1)
subprocess.run('rm -f /home/lzhenn/cooperate/fig/'+case[nc]+'.S'+''.join(stime)+'*.pr.png', shell=True)


import logging
import ssl
import httplib

from .http_try import HttpTryProxy
from .http_try import try_receive_response_body
from .http_try import try_receive_response_header
from .. import networking

LOGGER = logging.getLogger(__name__)

class HttpsEnforcer(HttpTryProxy):

    bad_domains = []

    def get_or_create_upstream_sock(self, client):
        LOGGER.info('[%s] force https: %s' % (repr(client), client.url))
        upstream_sock = client.create_tcp_socket(client.dst_ip, 443, 3)
        old_counter = upstream_sock.counter
        upstream_sock = ssl.wrap_socket(upstream_sock)
        upstream_sock.counter = old_counter
        return upstream_sock

    def process_response(self, client, upstream_sock, response, http_response):
        if http_response:
            if httplib.FORBIDDEN == http_response.status:
                client.fall_back(reason='403 forbidden')
            if httplib.NOT_FOUND == http_response.status:
                client.fall_back(reason='404 not found')
        return super(HttpsEnforcer, self).process_response(client, upstream_sock, response, http_response)

    def forward_upstream_sock(self, client, http_response, upstream_sock):
        client.forward(upstream_sock)

    def is_protocol_supported(self, protocol, client=None):
        if not super(HttpsEnforcer, self).is_protocol_supported(protocol, client):
            return False
        for bad_domain in self.bad_domains:
            if client.host.endswith(bad_domain):
                return True
        return False

    @classmethod
    def refresh(cls, proxies):
        cls.bad_domains = list(cls.resolve_blacklist('https-enforcer.dyn.fqrouter.com'))
        LOGGER.error('resolved https enforcer domains: %s' % cls.bad_domains)
        return True

    @classmethod
    def resolve_blacklist(cls, blacklist):
        try:
            bad_domains = set()
            for record in networking.resolve_txt(blacklist):
                record_type, record_content = record.text[0].split('=')
                if 'd' == record_type:
                    bad_domains.add(record_content)
                elif 'r' == record_type:
                    bad_domains |= cls.resolve_blacklist(record_content)
                else:
                    pass # ignore
            return bad_domains
        except:
            LOGGER.exception('failed to resolve blacklist: %s' % blacklist)
            return set()

    def __repr__(self):
        return 'HttpsEnforcer'

class GoogleScrambler(HttpTryProxy):

    def before_send_request(self, client, upstream_sock, is_payload_complete):
        client.google_scrambler_hacked = is_payload_complete
        if client.google_scrambler_hacked:
            if 'Referer' in client.headers:
                del client.headers['Referer']
            LOGGER.info('[%s] scramble google traffic' % repr(client))
            return 'GET http://www.google.com/ncr HTTP/1.1\r\n\r\n\r\n'
        return ''

    def forward_upstream_sock(self, client, http_response, upstream_sock):
        if client.google_scrambler_hacked:
            client.forward(upstream_sock) # google will 400 error if keep-alive and scrambling
        else:
            super(GoogleScrambler, self).forward_upstream_sock(client, http_response, upstream_sock)

    def after_send_request(self, client, upstream_sock):
        google_scrambler_hacked = getattr(client, 'google_scrambler_hacked', False)
        if google_scrambler_hacked:
            try_receive_response_body(try_receive_response_header(client, upstream_sock), reads_all=True)

    def process_response(self, client, upstream_sock, response, http_response):
        google_scrambler_hacked = getattr(client, 'google_scrambler_hacked', False)
        if not google_scrambler_hacked:
            return response
        if len(response) < 10:
            client.fall_back('response is too small: %s' % response)
        if http_response:
            if httplib.FORBIDDEN == http_response.status:
                client.fall_back(reason='403 forbidden')
            if httplib.NOT_FOUND == http_response.status:
                client.fall_back(reason='404 not found')
            if http_response.content_length \
                and httplib.PARTIAL_CONTENT != http_response.status \
                and 0 < http_response.content_length < 10:
                client.fall_back('content length is too small: %s' % http_response.msg.dict)
        return response

    def is_protocol_supported(self, protocol, client=None):
        return False # disable it

    def __repr__(self):
        return 'GoogleScrambler'

GOOGLE_SCRAMBLER = GoogleScrambler()
HTTPS_ENFORCER = HttpsEnforcer()


import mechatronics as mech
import time

#hook up led to designated pin
LED_FAR = 10
LED_MID = 9
LED_NEAR = 11

#initialize led
mech.initialize_led(LED_FAR)
mech.initialize_led(LED_MID)
mech.initialize_led(LED_NEAR)

ON = 1
OFF = 0

#initialize speaker
SPKR = 17
mech.initialize_speaker(SPKR)

LOW_FREQ = 300

mech.set_led(LED_NEAR, ON)
time.sleep(1.5)

TRIG = 22
ECHO = 27
#pins for sonic sensor
TRIG2 = 14
ECHO2 = 15

#initialize sensors
mech.initialize_ranger(TRIG, ECHO)
mech.initialize_ranger(TRIG2, ECHO2)

#function to turn all lights on
def turn_all_led_on():
    mech.set_led(LED_NEAR, ON)
    mech.set_led(LED_MID, ON)
    mech.set_led(LED_FAR, ON)
turn_all_led_on()

##Function to get distance away from sensor and play sound/signal lights
def print_distance_then_wait():
    distance = mech.get_distance(TRIG, ECHO)
    while True:
        print ("The distance is", distance)
        if distance != None:
            if distance > 10:
                time.sleep(2)
                mech.play_speaker(SPKR, OFF, 0)
            elif (distance < 10) and (distance >= 5):
                time.sleep(0.1)
                mech.play_speaker(SPKR, ON, 200)
                mech.set_led(LED_NEAR, ON)
            elif (distance < 5):
                turn_all_led_on()
                time.sleep(1)

#function for second sensor
def sensor2():
    while True:
        distance2 = mech.get_distance(TRIG2, ECHO2)
        print ("This distance is", distance2)
        if distance2 != None:
            if distance2 > 10:
                time.sleep(2)
                mech.play_speaker(SPKR, OFF, 0)
                mech.set_led(LED_FAR, ON)
                mech.set_led(LED_MID, ON)
            elif (distance2 < 10) and (distance2 >= 5):
                time.sleep(0.1)
                mech.play_speaker(SPKR, ON, 200)
                mech.set_led(LED_NEAR, ON)
            elif (distance2 < 5):
                mech.play_speaker(SPKR, ON, 100)
                m(LED_MID, ON)
                time.sleep(1.5)

try:
    print_distance_then_wait()
except KeyboardInterrupt:
    mech.set_led(LED_FAR, OFF)
    mech.set_led(LED_MID, OFF)
    mech.set_led(LED_NEAR, OFF)
    mech.play_speaker(SPKR, OFF, 0)
    mech.cleanup()


#!/usr/bin/python

# This script implements an attack on RSA described by Brumley at all
# Note: this does not actually work for now... This provies it's ineffective
# against FME, however.
# key value - 0b1010101010101010

import os

def calc_time(guess):
	return int(os.popen("python ../timing_models/encrypt_fme.py 0 {} 0".format(guess)).read().strip())

# Read in and parse data
f = open("idealized_data.csv")
data = f.read()
f.close()
data = [ x.split(",") for x in data.split("\n") ][1:-1]

# Calculating the high and low values
low = [ int(x[2]) for x in data if x[1] == "0" ]
high = [ int(x[2]) for x in data if x[1] == "1" ]
low = sum(low)/len(low)
high = sum(high)/len(high)
print("Low value: {}\nHigh value: {}\n".format(low, high))

# Start the attack
key = "1010101010101010"
total_time = calc_time(int(key, 2))
print("Actual Time: {}\n".format(total_time))

guess = "101" # 3 bits for free
for x in range(16-3):
	g_low = guess + "0" + "0"*(16-len(guess)-1)
	g_high = guess + "1" + "0"*(16-len(guess)-1)

	g_low_time = calc_time(int(g_low, 2))
	g_high_time = calc_time(int(g_high, 2))

	g_low_diff = abs(total_time - g_low_time)
	g_high_diff = abs(total_time - g_high_time)

	print("Low: {} {} {}".format(g_low, g_low_time, g_low_diff))
	print("High: {} {} {}".format(g_high, g_high_time, g_high_diff))
	print

	if g_low_diff < g_high_diff: guess += "0"
	else: guess += "1"

print(guess)


class UserManager(object):

    def __init__(self, db, User):
        self.db = db
        self.User = User

    def add(self, **kwargs):
        username = kwargs['username'][0]
        public_key = kwargs['public_key'][0]
        ip = None
        if 'ip' in kwargs:
            ip = kwargs['ip'][0]
        user = self.User(username=username,
                         public_key=public_key)
        user.ip = ip
        self.db.session.add(user)
        self.db.session.commit()

    def get(self, **kwargs):
        username = kwargs['username']
        return self.db.session.query(self.User).filter_by(username=username).first()


from django.shortcuts import render, redirect
from .models import Booking, Menu
from .forms import BookingForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required

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

@login_required
def restaurant(request):
    all_booking = Booking.objects.order_by('date').all
    return render(request, 'restaurant.html', {'all': all_booking})

def booking(request):
    if request.method == 'POST':
        form = BookingForm(request.POST or None)
        if form.is_valid():
            form.save()

        else:
            fname = request.POST['fname']
            lname = request.POST['lname']
            email = request.POST['email']
            guests = request.POST['guests']
            date = request.POST['date']
            info = request.POST['info']
            messages.success(request, (
                'You need set name and time for booking to complete.'))
            return render(request, 'booking.html', {
                'fname': fname,
                'lname': lname,
                'email': email,
                'guests': guests,
                'date': date,
                'info': info,
            })
        messages.success(request, (
            'Booking a table was successfully submitted.'))
        return redirect('booking')
    else:
        return render(request, 'booking.html', {})

def menu(request):
    all_menu = Menu.objects.order_by('price').all
    return render(request, 'menu.html', {'menu': all_menu})


#
# @lc app=leetcode id=200 lang=python
#
# [200] Number of Islands
#

# @lc code=start
class Solution(object):
    def dfs(self, grid, x, y):
        if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == '0':
            return 0
        #直接标记为0防止重复访问，但是会改变原有的值
        grid[x][y] = '0'
        self.dfs(grid, x+1, y)
        self.dfs(grid, x, y+1)
        self.dfs(grid, x-1, y)
        self.dfs(grid, x, y-1)
        return 1

    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        #DFS，回溯，对于一个'1'的点DFS四个方向的值，如果是'0'就标记为island,
        #需要标记是否访问过？
        '''
        if not grid:
            return 0
        cnt = 0
        row, col = len(grid), len(grid[0])
        for i in range(row):
            for j in range(col):
                if grid[i][j] == '1':
                    #DFS其四个方向
                    cnt += self.dfs(grid, i, j)
        return cnt
        '''
        #BFS, use queue,但是会修改原有矩阵
        if not grid:
            return 0
        cnt = 0
        q = collections.deque([])
        row, col = len(grid), len(grid[0])
        for i in range(row):
            for j in range(col):
                if grid[i][j] == '1':
                    cnt +=1
                    grid[i][j] = '0'
                    q.append([i, j])
                    while q:
                        x, y = q.popleft()
                        directions = [[0, 1], [1, 0], [-1, 0], [0, -1]]
                        for d in directions:
                            new_x, new_y = x + d[0], y + d[1]
                            if 0 <= new_x < row and 0 <= new_y < col and grid[new_x][new_y] == '1':
                                grid[new_x][new_y] = '0'
                                q.append([new_x, new_y])
        return cnt

# @lc code=end


class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs) == 0: return ""
        if len(strs) == 1: return strs[0]
        s0 = strs[0]
        diff = 0

        for i in range(0, len(s0)):
            for s in strs[1:]:
                if len(s) <= i or s[i] != s0[i]:
                    diff = 1
                    break
                """print "s0[{0}]:{1} s[{0}]:{2}".format(i, s0[i], s[i])"""
            if diff == 1:
                break
        """print "i:", i"""
        return s0[:i] if diff else s0


from __future__ import annotations  # needed for type annotations in > python 3.7

import logging
from typing import Dict, List, Set, Union

log = logging.getLogger(__name__)

class Quantity:
    def __init__(self, name: str):
        self.name = name
        self.shifts: Dict[str, Set[str]] = {}
        self.ignored_shifts: Dict[str, Set[str]] = {}
        self.children: Dict[str, List[Quantity]] = {}
        self.defined_for_scopes: List[str] = []
        log.debug("Setting up new Quantity {}".format(self.name))

    def __str__(self) -> str:
        return self.name

    def __repr__(self) -> str:
        return self.name

    def __lt__(self, other: Quantity) -> bool:
        return self.name < other.name

    def add(self, name: str) -> None:
        """
        Function is not used for a base quantity
        """
        pass

    def reserve_scope(self, scope: str) -> None:
        """
        Function to reserve a scope for a given quantity. The scopes, in which a quantity is used
        as an output are tracked in the output_scopes list.
        If a quantity is already used within a given scope as output, this will result in an exception.
        This check is triggered for every Producer.
        """
        log.debug("Checking {} / scope {}".format(self.name, scope))
        if (scope == "global" and self.defined_for_scopes == []) or (
            scope != "global" and scope not in self.defined_for_scopes
        ):
            self.defined_for_scopes.append(scope)
        else:
            log.error(
                "Quantity {} is already defined in {} scope !".format(self.name, scope)
            )
            raise Exception

    def get_leaf(self, shift: str, scope: str) -> str:
        """
        Function to get the leaf of a given shift within a given scope.
        A leaf is the name of the quantity used for that scope/shift combination.
        If no shift is defined, the name of the quantity is returned.

        Args:
            shift (str): Name of the shift for which the leaf should be returned
            scope (str): Scope for which the leaf should be returned
        Returns:
            str. Name of the leaf
        """
        if shift in self.get_shifts(scope):
            return self.name + shift
        return self.name

    def get_leaves_of_scope(self, scope: str) -> List[str]:
        """
        Function returns a list of all leaves, which are defined for a given scope.

        Args:
            scope (str): Scope for which the leaves should be returned
        Returns:
            list. List of leaves
        """
        result = [self.name] + [
            self.get_leaf(shift, scope) for shift in self.get_shifts(scope)
        ]
        return result

    def shift(self, name: str, scope: str) -> None:
        """
        Function to define a shift for a given scope. If the shift is marked as ignored, nothing will be added.
        When a new shift is defined, all child quantities of a given quantity will be shifted as well.

        Args:
            name (str): Name of the shift
            scope (str): Scope for which the shift should be defined
        Returns:
            None
        """
        if scope in self.ignored_shifts.keys():
            if name in self.ignored_shifts[scope]:
                log.debug("Ignoring shift {} for quantity {}".format(name, self.name))
                return
        log.debug("Adding shift {} to quantity {}".format(name, self.name))
        if scope not in self.shifts.keys():
            self.shifts[scope] = set()
        if name not in self.shifts[scope]:
            self.shifts[scope].add(name)
            if scope == "global":  # shift children in all scopes if scope is global
                for any_scope in self.children:
                    for c in self.children[any_scope]:
                        c.shift(name, any_scope)
            else:
                if scope in self.children.keys():
                    for c in self.children[scope]:
                        c.shift(name, scope)

    def ignore_shift(self, name: str, scope: str) -> None:
        """
        Function to ignore a shift for a given scope.

        Args:
            name (str): Name of the shift to be ignored
            scope (str): Scope for which the shift should be ignored
        Returns:
            None
        """
        log.debug("Make quantity {} ignore shift {}".format(self.name, name))
        if scope not in self.ignored_shifts.keys():
            self.ignored_shifts[scope] = set()
        self.ignored_shifts[scope].add(name)

    def copy(self, name: str) -> Quantity:
        """
        Generate a copy of the current quantity with a new name.

        Args:
            name (str): Name of the new quantity
        Returns:
            Quantity. a new Quantity object.
        """
        copy = Quantity(name)
        copy.shifts = self.shifts
        copy.children = self.children
        copy.ignored_shifts = self.ignored_shifts
        copy.defined_for_scopes = self.defined_for_scopes
        return copy

    def adopt(self, child: Quantity, scope: str) -> None:
        """
        Adopt a child quantity to the current quantity.
        An adopted quantity will inherit all shifts of the partent quantity.

        Args:
            child (Quantity): The child quantity
            scope (str): Scope for which the child should be adopted
        Returns:
            None
        """
        log.debug(
            "Adopting child quantity {} to quantity {}".format(child.name, self.name)
        )
        if scope not in self.children.keys():
            self.children[scope] = []
        self.children[scope].append(child)

    def get_shifts(self, scope: str) -> List[str]:
        """
        Function returns a list of all shifts, which are defined for a given scope.

        Args:
            scope (str): Scope for which shifts should be returned
        Returns:
            list: List of all shifts, which are defined for a given scope.
        """
        if "global" in self.shifts.keys():
            if scope != "global" and scope in self.shifts.keys():
                log.error(
                    "Quantity {} has shifts in global and {}. Something must be broken!".format(
                        self.name, scope
                    )
                )
                raise Exception
            return list(self.shifts["global"])
        elif scope in self.shifts.keys():
            return list(self.shifts[scope])
        else:
            return []

class QuantityGroup(Quantity):
    """
    A Quantity Group is a group of quantities, that all have the same settings, but different names.
    """

    def __init__(self, name: str):
        super().__init__(name)
        self.quantities: List[Quantity] = []
        self.vec_config: str = ""

    def set_vec_config(self, vec_config: str) -> None:
        """
        Function to set the vec config key

        Args:
            vec_config (str): Name of the vec config key
        Returns:
            None
        """
        self.vec_config = vec_config

    def copy(self, name: str) -> Quantity:
        """
        Copy is not allowed for Quantity Groups.
        """
        log.error("Copy is not allowed for a Quantity Group !")
        raise Exception

    def add(self, name: str) -> None:
        """
        Function to add a new Quantity to the group. This quantity contains the identical shifts as the group itself

        Args:
            name (str): Name of the new Quantity
        Returns:
            None
        """
        if name not in [q.name for q in self.quantities]:
            quantity = Quantity(name)
            quantity.shifts = self.shifts
            quantity.children = self.children
            quantity.ignored_shifts = self.ignored_shifts
            quantity.defined_for_scopes = self.defined_for_scopes
            self.quantities.append(quantity)

    def get_leaves_of_scope(self, scope: str) -> List[str]:
        """
        Function returns a list of all leaves, which are defined for a given scope.
        This is an overload of the function used for the quantity class.

        For the writeout,  loop over all quantities in the group and
        return them all (plus their shifts) in a list.

        Args:
            scope (str): Scope for which the leaves should be returned
        Returns:
            list. List of leaves
        """
        output: List[str] = []
        for quantity in self.quantities:
            output.extend(quantity.get_leaves_of_scope(scope))
        return output

class NanoAODQuantity(Quantity):
    """
    A NanoAODQuantity is a quantity that comes directly from the NanoAOD file.
    Normally, these quantities are not suited to be used in the output ntuple,
    are therefore shielded from using them directly as a output.
    """

    def __init__(self, name: str):
        super().__init__(name)
        self.shifted_naming: Dict[str, str] = {}

    def reserve_scope(self, scope: str) -> None:
        """
        Function used to ensure, that NanoAOD quantities are not used as output
        in a producer. If this is attempted, an Exception will be raised.

        Args:
            scope (str): Scope for which the quantity should be reserved (not used)
        Returns:
            None
        """
        log.error(
            "Quantity {} is a NanoAOD quantity and cant be used as output !".format(
                self.name
            )
        )
        raise Exception

    def register_external_shift(
        self, shift_name: str, external_name: Union[str, NanoAODQuantity]
    ) -> None:
        """
        Function used to register a NanoAOD quantity as a shift of another quantity.
        Iif the shifted version of a quantity already exists in the input,
        this function can be used to register an
        branch from the input as a shifted version of a quantity

        Args:
            shift_name (str): Name of the shift
            external_name (str): Name of the shifted quantity in the NanoAOD
        Returns:
            None
        """
        if shift_name not in self.shifted_naming.keys():
            if isinstance(external_name, NanoAODQuantity):
                self.shifted_naming[shift_name] = str(external_name)
            else:
                self.shifted_naming[shift_name] = external_name
        for any_scope in self.children:
            for c in self.children[any_scope]:
                c.shift(shift_name, any_scope)

    def get_leaf(self, shift: str, scope: str) -> str:
        """
        Overloaded version for the `get_leaf` function to return the
        shifted versions of the quantity if needed.

        Args:
            shift (str): Name of the shift for which the leaf should be returned
            scope (str): Scope for which the leaf should be returned (not used)
        Returns:
            str. Name of the leaf
        """
        if shift in self.shifted_naming.keys():
            return self.shifted_naming[shift]
        return self.name

    def get_shifts(self, scope: str) -> List[str]:
        """
        Overloaded version of the `get_shifts` function

        Args:
            scope (str): Scope for which shifts should be returned (not used)
        Returns:
            list: List of all shifts
        """
        return list(self.shifted_naming.keys())

# Definitions for type annotations
QuantitiesInput = Union[
    Quantity, NanoAODQuantity, List[Union[Quantity, NanoAODQuantity]]
]
QuantitiesStore = Dict[str, Set[Quantity]]