# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from celery import shared_task, task
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

import psutil
import time
import json
import datetime
import netifaces
from django_redis import get_redis_connection
from collections import defaultdict, OrderedDict
from app.utils import network
from django.utils.translation import ugettext_lazy as _

DJANGO_CELERY_TCP_CONNECT_INFO = "dblog-celery-tcp-connect-info-{time}"
DJANGO_CELERY_TCP_CONNECT_INFO_SHOW = "dblog-celery-tcp-connect-info-show-{time}"

DJANGO_CELERY_NET_MONITOR_INFO = "dblog-celery-net-monitor-info-{network}-{time}"
DJANGO_CELERY_NET_MONITOR_INFO_SHOW = "dblog-celery-net-monitor-info-show-{network}-{time}"

KB = 1 << 10
MB = 1 << 20
GB = 1 << 30
TB = 1 << 40
PB = 1 << 50
EXPIRE_TIME = 3600*24*5
labelStep = 30

# ---------------------------
# TCP 连接情况
@shared_task(bind=False, ignore_result=True, store_errors_even_if_ignored=True)
def tcp_connect_info():
    """  统计 TCP连接情况
    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
    TIME_WAIT 8947 等待足够的时间以确保远程TCP接收到连接中断请求的确认
    FIN_WAIT1 15 等待远程TCP连接中断请求，或先前的连接中断请求的确认
    FIN_WAIT2 1 从远程TCP等待连接中断请求
    ESTABLISHED 55 代表一个打开的连接
    SYN_RECV 21 再收到和发送一个连接请求后等待对方对连接请求的确认
    CLOSING 2 没有任何连接状态
    LAST_ACK 4 等待原来的发向远程TCP的连接中断请求的确认

    TCP连接状态详解
    LISTEN： 侦听来自远方的TCP端口的连接请求
    SYN-SENT： 再发送连接请求后等待匹配的连接请求
    SYN-RECEIVED：再收到和发送一个连接请求后等待对方对连接请求的确认
    ESTABLISHED： 代表一个打开的连接
    FIN-WAIT-1： 等待远程TCP连接中断请求，或先前的连接中断请求的确认
    FIN-WAIT-2： 从远程TCP等待连接中断请求
    CLOSE-WAIT： 等待从本地用户发来的连接中断请求
    CLOSING： 等待远程TCP对连接中断的确认
    LAST-ACK： 等待原来的发向远程TCP的连接中断请求的确认
    TIME-WAIT： 等待足够的时间以确保远程TCP接收到连接中断请求的确认
    CLOSED： 没有任何连接状态

    https://blog.csdn.net/qadada/article/details/8423561
    http://www.echojb.com/coldfusion/2016/10/08/228099.html
    http://www.echojb.com/coldfusion/2016/10/08/228099.html

    https://my.oschina.net/u/1169079/blog/397705
    https://gist.github.com/ameizi/cb126be7383fb463eae8
    https://gist.github.com/ameizi/cb126be7383fb463eae8
    :return:
    """
    status_list = ["CLOSED", "CLOSE_WAIT", "CLOSING", "ESTABLISHED", "FIN_WAIT1", "FIN_WAIT2", "LAST_ACK", "LISTEN",
                   "SYN_RECV", "SYN_SENT", "TIME_WAIT"]
    fmt = time.strftime("%Y-%m-%d %H:%M")
    time_fmt1, time_fmt2 = fmt.split(" ")
    key = DJANGO_CELERY_TCP_CONNECT_INFO.format(time=time_fmt1)
    status_temp = []
    net_connections = psutil.net_connections()
    for k in net_connections:
        status_temp.append(k.status)

    status_result = []
    for status in status_list:
        status_result.append((
            status, status_temp.count(status) ))
    redis = get_redis_connection()
    d = json.dumps({ 'time': time_fmt2, "results": status_result })
    res = ( redis.pipeline()
            .lpush(key, d)
            .expire(key, EXPIRE_TIME)
            .lrange(key, 0, -1)
            .execute())

    # 缓存
    stat_cpu = res[-1]
    set_tcp_connect_info(redis, stat_cpu, time_fmt1)

def set_tcp_connect_info(redis, stat_cpu, fmt):
    key = DJANGO_CELERY_TCP_CONNECT_INFO_SHOW.format(time=fmt)
    stat_cpu = reversed(stat_cpu)
    categories = []
    datasets = defaultdict(list)
    for x in stat_cpu:
        x = json.loads(x)
        category = x['time']
        x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11 = x['results']
        categories.append(category)
        datasets['CLOSED'].append(x1[1])
        datasets['CLOSE_WAIT'].append(x2[1])
        datasets['CLOSING'].append(x3[1])
        datasets['ESTABLISHED'].append(x4[1])
        datasets['FIN_WAIT1'].append(x5[1])
        datasets['FIN_WAIT2'].append(x6[1])
        datasets['LAST_ACK'].append(x7[1])
        datasets['LISTEN'].append(x8[1])
        datasets['SYN_RECV'].append(x9[1])
        datasets['SYN_SENT'].append(x10[1])
        datasets['TIME_WAIT'].append(x11[1])
    datasets = dict(datasets)
    datasets_lists = []
    for k, v in datasets.iteritems():
        datasets_lists.extend(v)
    yaxisminValue = min(datasets_lists)
    # yaxisminValue = min([i for i in datasets_lists if i>0])
    yaxismaxValue = max(datasets_lists)
    redis.set(key, json.dumps({
        "datasets": datasets,
        "categories": categories,
        "yaxisminValue": yaxisminValue,
        "yaxismaxValue": yaxismaxValue,
    }))

def get_tcp_connect_info():
    now = datetime.datetime.now()
    before_now = now - datetime.timedelta(days=1)
    now_fmt = now.strftime("%Y-%m-%d")
    before_now_fmt = before_now.strftime("%Y-%m-%d")
    now_key = DJANGO_CELERY_TCP_CONNECT_INFO_SHOW.format(time=now_fmt)
    before_now_key = DJANGO_CELERY_TCP_CONNECT_INFO_SHOW.format(time=before_now_fmt)

    redis = get_redis_connection()
    now_tcp = _get_tcp_connect_info(redis, now_key)
    # before_now_tcp = _get_tcp_connect_info(redis, now_key, bn="bnow")
    before_now_tcp = _get_tcp_connect_info(redis, before_now_key, bn="bnow")
    return now_tcp, before_now_tcp

def _get_tcp_connect_info(redis, key, bn="now"):
    j = redis.get(key)
    if not j: return None
    d = json.loads(j)
    categories = d["categories"]
    # length = len(categories)
    # index = 0
    # while 1:
    #     if index>=length:break
    #     _c = categories[index]
    #     if _c.endswith(":00"):
    #         break
    #     index +=1
    datasets = d["datasets"]
    yaxisminValue = d["yaxisminValue"]
    yaxismaxValue = get_tcp_yaxismaxValue(d["yaxismaxValue"])
    category = "|".join(categories)
    dataset = []
    for status in ("CLOSED", "CLOSE_WAIT", "CLOSING", "ESTABLISHED", "FIN_WAIT1", "FIN_WAIT2", "LAST_ACK", "LISTEN", "SYN_RECV", "SYN_SENT", "TIME_WAIT"):
        data = datasets[status]
        dataset.append({
            "seriesname": status,
            "data": "|".join(map(str, data))
        })
    caption = _(u"今日TCP连接情况") % {}
    if bn=="bnow":
        caption = _(u"昨日TCP连接情况") % {}
    js = {
        'type': 'zoomline',
        'renderAt': 'tcp-info-container-{}'.format(bn),
        'width': '100%',
        'height': '500',
        'dataFormat': 'json',
        'dataSource': {
            "chart": {
                "caption": caption,  # 图表主标题
                "subcaption": "",  # 图表副标题
                "paletteColors": "#9585BF,#a90329,#3e7c8a,#c79121,#D6487E,#629B58,#ba43a9,#060AFE,#705da0,#cca300,#447ebc,#447ebc",
                # "paletteColors": "#7eb26d,#eab839,#3e7c8a,#ef843c,#e24d42,#1f78c1,#ba43a9,#705da0,#508642,#cca300,#447ebc,#447ebc",
                "captionFontSize": "16",
                "subcaptionFontSize": "16",
                "subcaptionFontBold": "0",
                "showBorder": "0",
                "bgColor": "#ffffff", # 图表背景色，6位16进制颜色值
                "canvasBgColor": "#1f1d1d",
                # "canvasBgColor": "#ffffff",  # 画布背景色，6位16进制颜色值
                # "bgColor": "#1f1d1d",
                "baseFont": "Helvetica Neue,Arial", # 图表字体样式
                "baseFontSize": "12", # 图表字体大小
                "showCanvasBorder": "0",
                "showShadow": "0",
                "showAlternateHGridColor": "0",
                "yaxisname": _(u"连接数") % {}, # 纵向坐标轴(y轴)名称
                "xaxisname": _(u"时间") % {}, # 横向坐标轴(x轴)名称
                "yaxisminValue": "{}".format(yaxisminValue),
                "yaxismaxValue": "{}".format(yaxismaxValue),
                "forceAxisLimits" : "1",
                "pixelsPerPoint": "0",

                "pixelsPerLabel": "12",  #间隔，每60分钟一个间隔

                "lineThickness": "1",
                "compactdatamode" : "1",
                "dataseparator" : "|",
                "labelHeight": "30",
                "scrollheight": "10",
                "flatScrollBars": "1",
                "scrollShowButtons": "0",
                "scrollColor": "#cccccc",
                "legendBgAlpha": "0",
                "legendBorderAlpha": "0",
                "legendShadow": "0",
                "legendItemFontSize": "14", # 项目字体大小
                "legendItemFontColor": "#666666",
                "formatNumberScale":"0",
                "showhovercap": True,
                "labelStep": "{}".format(labelStep),#间隔，每60分钟一个间隔
                # "numVDivLines": "12", # 垂直分割线
                # "numberSuffix": " KB/s",# 增加数字后缀
                # "numberPrefix": "KB/S", # 增加数字前缀     % 为 '%25'
                "numDivLines": 5,  # 水平分割线
            },
            "categories": [{
                "category":  "{}".format(category)
            }],
            "dataset": dataset
        }
    }
    return json.dumps(js)

# ---------------------------
# 监控网卡流量
@shared_task(bind=False, ignore_result=True, store_errors_even_if_ignored=True)
def network_monitor_info():
    key_info, net_in, net_out = network.get_nets_io_rate(network.get_nets_io)
    # time_fmt = time.strftime("%Y-%m-%d %H:%M")
    now = datetime.datetime.now()
    bnow_fmt = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    bbnow_fmt = (now - datetime.timedelta(days=2)).strftime("%Y-%m-%d")
    now_fmt_ = now.strftime("%Y-%m-%d %H:%M")
    now_fmt, time_fmt2 = now_fmt_.split(" ")
    redis = get_redis_connection()
    for k in key_info:
        key = DJANGO_CELERY_NET_MONITOR_INFO.format(network=k, time=now_fmt)
        _in = net_in.get(k)
        _out = net_out.get(k)
        d = json.dumps({'time': time_fmt2, "results": [_in, _out]})
        res = (redis.pipeline()
               .lpush(key, d)
               .expire(key, EXPIRE_TIME)
               # .ltrim(key, 0, 12*24*3)
               .lrange(key, 0, -1)
               .execute())
        stat_net = res[-1]
        set_network_monitor_info(redis, reversed(stat_net), k, now_fmt, bnow_fmt, bbnow_fmt)

def set_network_monitor_info(redis, nstat_net, net, now_fmt, bnow_fmt, bbnow_fmt):
    bkey = DJANGO_CELERY_NET_MONITOR_INFO.format(network=net, time=bnow_fmt)
    bbkey = DJANGO_CELERY_NET_MONITOR_INFO.format(network=net, time=bbnow_fmt)
    bstat_net = reversed(redis.lrange(bkey, 0, -1))
    bbstat_net = reversed(redis.lrange(bbkey, 0, -1))
    skey = DJANGO_CELERY_NET_MONITOR_INFO_SHOW.format(network=net, time=now_fmt)
    bskey = DJANGO_CELERY_NET_MONITOR_INFO_SHOW.format(network=net, time=bnow_fmt)
    bbskey = DJANGO_CELERY_NET_MONITOR_INFO_SHOW.format(network=net, time=bbnow_fmt)
    # stat_net = []
    # stat_net.extend(bbstat_net)
    # stat_net.extend(bstat_net)
    # stat_net.extend(nstat_net)
    for key, stats in [(skey, nstat_net),(bskey, bstat_net),(bbskey, bbstat_net)]:
        if not stats: continue
        categories = []
        datasets = defaultdict(list)
        for x in stats:
            x = json.loads(x)
            category = x['time']
            categories.append(category)
            x1, x2 = x['results']
            datasets['Incoming network traffic on {}'.format(net)].append(x1)
            datasets['Outgoing network traffic on {}'.format(net)].append(x2)
        datasets = dict(datasets)
        if not datasets: continue
        datasets_lists = []
        for k, v in datasets.iteritems():
            datasets_lists.extend(v)
        yaxisminValue = min(datasets_lists)
        # yaxisminValue = min([i for i in datasets_lists if i>0])
        yaxismaxValue = max(datasets_lists)
        ( redis.pipeline()
          .set(key, json.dumps({
            "datasets": datasets,
            "categories": categories,
            "yaxisminValue": yaxisminValue,
            "yaxismaxValue": yaxismaxValue,}))
          .expire(key, EXPIRE_TIME)
          .execute())

def get_network_monitor_info():
    key_info = network.get_nets()  # 获取网卡名称
    j = []
    keys = []
    redis = get_redis_connection()
    routingNicName = str(netifaces.gateways()['default'][netifaces.AF_INET][1])
    key_info.remove(routingNicName)
    key_info.insert(0, routingNicName)
    now = datetime.datetime.now()
    bnow_fmt = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    bbnow_fmt = (now - datetime.timedelta(days=2)).strftime("%Y-%m-%d")
    now_fmt = now.strftime("%Y-%m-%d")
    for k in key_info:
        d = _get_network_monitor_info(redis, k, now_fmt, bnow_fmt, bbnow_fmt)
        if d:
            keys.append(k)
            j.append( (k, d) )
    return keys, j

def _get_network_monitor_info(redis, net, now_fmt, bnow_fmt, bbnow_fmt):
    # key = DJANGO_CELERY_NET_MONITOR_INFO_SHOW.format(network=net, time=now_fmt)
    skey = DJANGO_CELERY_NET_MONITOR_INFO_SHOW.format(network=net, time=now_fmt)
    bskey = DJANGO_CELERY_NET_MONITOR_INFO_SHOW.format(network=net, time=bnow_fmt)
    bbskey = DJANGO_CELERY_NET_MONITOR_INFO_SHOW.format(network=net, time=bbnow_fmt)
    datajs = []
    days = [('today', _(u'今日'), skey, now_fmt), ('yest', _(u'昨日'), bskey, bnow_fmt), ('bday', _(u'前日'), bbskey, bbnow_fmt)]
    for daykey, day, key, day_fmt in days:
        j = redis.get(key)
        if not j:
            datajs.append((daykey, day, None))
            continue
        d = json.loads(j)
        categories = d["categories"]
        # length = len(categories)
        # index = 0
        # while 1:
        #     if index>=length:break
        #     _c = categories[index]
        #     if _c.endswith(":00"):
        #         break
        #     index +=1
        datasets = d["datasets"]
        yaxismaxValue = d["yaxismaxValue"]
        _unit, _unit_str, _unit_round = KB, 'KB/s', 1
        if yaxismaxValue>= MB:
            _unit, _unit_str, _unit_round  = MB, 'MB/s', 2
        yaxisminValue = 0
        yaxismaxValue = network.trans_io(d["yaxismaxValue"], _unit, _unit_round)
        yaxismaxValue = get_net_yaxismaxValue(yaxismaxValue, _unit)
        category = "|".join(categories)
        _in_key = 'Incoming network traffic on {}'.format(net)
        _out_key = 'Outgoing network traffic on {}'.format(net)
        _ins = [network.trans_io(i, _unit, _unit_round) for i in datasets[_in_key]]
        _outs = [network.trans_io(i, _unit, _unit_round) for i in datasets[_out_key]]
        dataset = [{
            "seriesname": _in_key,
            "data": "|".join(map(str, _ins))
        },{
            "seriesname": _out_key,
            "data": "|".join(map(str, _outs))
        }]
        caption = _(u'%(name)s网卡%(day)s流量监控') % {'name': net, "day": day_fmt}
        js = {
            'type': 'zoomline',
            'renderAt': 'net-info-container-{}-{}'.format(net, daykey),
            'width': '100%',
            'height': '500',
            'dataFormat': 'json',
            'dataSource': {
                "chart": {
                    "caption": caption,  # 图表主标题
                    "subcaption": "",  # 图表副标题
                    "paletteColors": "#c79121,#447ebc",
                    "captionFontSize": "16",
                    "subcaptionFontSize": "16",
                    "subcaptionFontBold": "0",
                    "showBorder": "0",
                    "bgColor": "#ffffff", # 图表背景色，6位16进制颜色值
                    "canvasBgColor": "#1f1d1d",
                    # "canvasBgColor": "#ffffff",  # 画布背景色，6位16进制颜色值
                    # "bgColor": "#1f1d1d",
                    "baseFont": "Helvetica Neue,Arial", # 图表字体样式
                    "baseFontSize": "12", # 图表字体大小
                    "showCanvasBorder": "0",
                    "showShadow": "0",
                    "showAlternateHGridColor": "0",
                    "yaxisname": _(u"流量") % {}, # 纵向坐标轴(y轴)名称
                    "xaxisname": _(u"时间") % {}, # 横向坐标轴(x轴)名称
                    "yaxisminValue": "{}".format(yaxisminValue),
                    "yaxismaxValue": "{}".format(yaxismaxValue),
                    "forceAxisLimits" : "1",
                    "pixelsPerPoint": "0",

                    "pixelsPerLabel": "12",  #间隔，每60分钟一个间隔

                    "lineThickness": "1",
                    "compactdatamode" : "1",
                    "dataseparator" : "|",
                    "labelHeight": "30",
                    "scrollheight": "10", #
                    "flatScrollBars": "1",
                    "scrollShowButtons": "0",
                    "scrollColor": "#cccccc",
                    "legendBgAlpha": "0",
                    "legendBorderAlpha": "0",
                    "legendShadow": "0",
                    "legendItemFontSize": "14", # 项目字体大小
                    "legendItemFontColor": "#666666",
                    "formatNumberScale":"0",
                    "showhovercap": True,
                    "labelStep": "{}".format(labelStep),  # 间隔，每60分钟一个间隔
                    # "numVDivLines": "12", # 垂直分割线
                    "numberSuffix": _unit_str,# 增加数字后缀
                    # "numberPrefix": "KB/S", # 增加数字前缀     % 为 '%25'
                    "numDivLines": 5, # 水平分割线
                },
                "categories": [{
                    "category":  "{}".format(category)
                }],
                "dataset": dataset
            }
        }
        datajs.append((daykey, day, json.dumps(js)))
    return datajs

def get_tcp_yaxismaxValue(yaxismaxValue):
    if yaxismaxValue>200:
        d = 150
        while 1:
            if d >= yaxismaxValue:
                return d
            d += 30
    else:
        d = 60
        while 1:
            if d >= yaxismaxValue:
                return d
            d += 30

def get_net_yaxismaxValue(yaxismaxValue, unit):
    if unit == MB:
        d = 1.2
        while 1:
            if d>=yaxismaxValue:
                return d
            d += 0.3
    else:
        if yaxismaxValue>=30:
            d = u = 30
        else:
            d = u = 3
        while 1:
            if d >= yaxismaxValue:
                return d
            d += u


class Student:
    def __init__(self, name, rut, marks):
        self.name = name
        self.rut = rut
        self.marks = marks

    def get_name(self):
        return self.name

    def get_rut(self):
        return self.rut

    def get_marks(self):
        return self.marks

    def get_avg(self):
        marks = self.marks
        marks = [float(x) for x in marks]
        avg = round(marks[0] * .2 + marks[1] * .2 + marks[2] * .05 + marks[3] * .2 + marks[4] * .25 + marks[5] * .1, 1)
        return avg

def parse_data(filename):
    students = []
    file = open(filename+'.txt', 'r')
    data = file.readlines()
    for x in data:
        parsed = x.split(';')
        students.append(Student(
            parsed[0],
            parsed[1],
            [
                parsed[2],
                parsed[3],
                parsed[4],
                parsed[5],
                parsed[6],
                parsed[7]
            ]
        ))
    return students

def generate_lines(data):
    lines = []
    for x in data:
        line = x.get_name()+','+x.get_rut()+','+str(x.get_avg())
        lines.append(line)
    return lines

def save_data(data, filename):
    file = open(filename+'.txt', 'w')
    for x in data:
        file.write(x+'\n')
    return print("Archivo TXT generado correctamente.")

def save_csv(data, filename):
    file = open(filename+'.csv', 'w')
    file.write("RUT,NOMBRE,NP\n")
    for x in data:
        file.write(x+'\n')
    return print("Archivo CSV generado correctamente.")

students = parse_data('estudiantes')
lines = generate_lines(students)
save_data(lines, 'NP')
save_csv(lines, 'NP_csv')


import torch.nn as nn
import torch
import pennylane as qml
from pennylane import numpy as np
# import pennylane_qulacs

from networks.backbones.custom_layers.QuanvLayer import Quanv

class QuanvNet(nn.Module):
    def __init__(self):
        super(QuanvNet, self).__init__()
        # Conv Layers
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=3)
        self.conv3 = nn.Conv2d(20, 5, kernel_size=1)

        self.convALT = nn.Conv2d(5, 4, kernel_size=2) # Acts like the Quanv Layer

        # Quanvolution Layer
        self.Quanv1 = Quanv(kernal_size=3, output_depth=4, circuit_layers=1)
        self.AdaptPool = nn.AdaptiveMaxPool2d(3)

        # Fully Connected Layers
        self.fc1 = nn.Linear(36, 12)
        self.fc2 = nn.Linear(12, 3)

        self.pool = nn.MaxPool2d(2)
        self.relu = nn.ReLU()
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = self.relu(self.conv3(x))
        # x = self.sigmoid(self.convALT(x))
        x = self.sigmoid(self.Quanv1(x))
        x = self.AdaptPool(x)
        x = x.view(-1, 36)
        x = self.relu(self.fc1(x))
        x = self.fc2(x)
        return x


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import sklearn.preprocessing as sklpre
import sklearn.model_selection as sklms

# TO DO: Make method for getting model data that is separate from regression method

class RegressionClass:
    def __init__(self, degree=5, stddev=1, step=0.05):
        self.x = np.arange(0, 1, step)
        self.y = np.arange(0, 1, step)
        self.stddev = stddev
        self.n = len(self.x)
        self.degree = degree
        self.modeled = False
        self.z_ = self.noise_function(self.x, self.y)

    def generate_data(self, x, y):
        """
        Creates the Franke function
        """
        term1 = 0.75 * np.exp(-(0.25 * (9 * x - 2) ** 2) - 0.25 * ((9 * y - 2) ** 2))
        term2 = 0.75 * np.exp(-((9 * x + 1) ** 2) / 49.0 - 0.1 * (9 * y + 1))
        term3 = 0.5 * np.exp(-(9 * x - 7) ** 2 / 4.0 - 0.25 * ((9 * y - 3) ** 2))
        term4 = -0.2 * np.exp(-(9 * x - 4) ** 2 - (9 * y - 7) ** 2)
        return term1 + term2 + term3 + term4

    def noise_function(self, x, y):
        """
        Adds Gaussian noise to the function f,  ~ N(0,stddev)
        """
        f = self.generate_data(x, y)
        noise = np.random.normal(0, self.stddev, size=f.shape)
        return f + noise

    def plot_franke(self):
        """
        3D plot of the Franke function
        """
        fig = plt.figure()
        xx, yy = np.meshgrid(self.x, self.y)
        ax = fig.gca(projection="3d")
        z = self.noise_function(xx, yy)
        # Plot the surface.
        surf = ax.plot_surface(
            self.x, self.y, z, cmap=cm.coolwarm, linewidth=0, antialiased=False
        )
        # Customize the z axis.
        ax.set_zlim(-0.10, 1.40)
        ax.zaxis.set_major_locator(LinearLocator(10))
        ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f"))
        # Add a color bar which maps values to colors.
        fig.colorbar(surf, shrink=0.5, aspect=5)
        plt.show()

    def design_matrix(self):
        """
        Creates the design matrix
        """
        X = np.zeros((2, self.n))
        X[0, :] = self.x
        X[1, :] = self.y
        X = X.T
        poly = sklpre.PolynomialFeatures(self.degree)
        return poly.fit_transform(X)

    def ordinary_least_squares(self):
        """
        Calculates ordinary least squares regression and the variance of
        estimated parameters
        """
        X = self.design_matrix()
        XTX = X.T @ X
        XTz = X.T @ self.z_
        beta = np.linalg.solve(XTX, XTz)  # solves XTXbeta = XTz
        beta_variance = self.stddev ** 2 * np.linalg.pinv(XTX)
        self.beta, self.beta_variance_ = beta, np.diag(beta_variance)
        self.modeled = True

    @property
    def eval_model(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")
        return self.design_matrix() @ self.beta

    @property
    def mean_squared_error(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")

        return np.sum((self.z_ - self.eval_model) ** 2) / self.n

    @property
    def r_squared(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")
        z = self.z_
        return 1 - np.sum((z - self.eval_model) ** 2) / np.sum((z - np.mean(z)) ** 2)

    @property
    def beta_variance(self):
        if not self.modeled:
            raise RuntimeError("Run a regression method first!")
        return self.beta_variance_

if __name__ == "__main__":
    np.random.seed(100)
    test = RegressionClass(degree=5, stddev=1, step=0.05)
    test.ordinary_least_squares()
    print(f"MSE {test.mean_squared_error}")
    print(f"R2 score {test.r_squared}")
    print(f"Beta variance {test.beta_variance}")
    plt.plot(test.eval_model, color="blue")
    plt.plot(test.z_, color="red")
    plt.show()


import httplib2
import json

from flask import  current_app, session
from sqlalchemy import alias, and_, or_

from models.base import db_session
from models.auth import Auth
from models.block import Block
from models.follow import Follow
from models.user import User
from models.user_auth import UserAuth

from utils.config_utils import load_config
from utils.get_from import get_from

class UserController:
  #TODO: Make sure that you can't set your username to a number to get another person's account
  def _get_user(self, identifier):
    if identifier.__class__ is int:
      user = User.query.filter(User.user_id==identifier).first()
    else:
      user = User.query.filter(User.username==identifier).first()
    return user

  def _logout(self):
    from app import oauth2
    if 'user' in session:
      del session['user']
    session.modified = True
    oauth2.storage.delete()

  def _request_user_info(self, credentials):
    http = httplib2.Http()
    credentials.authorize(http)
    resp, content = http.request('https://www.googleapis.com/plus/v1/people/me')

    if resp.status != 200:
      current_app.logger.error(
        "Error while obtaining user profile: \n%s: %s",
        resp,
        content
      )
      return None

    profile = json.loads(content.decode('utf-8'))

    google_auth_id = profile['id']
    email = profile['emails'][0]['value']
    user = {
      'username': email.split("@")[0],
      'email': email,
      'display_name': profile['displayName'],
      'first_name': get_from(profile, ['name', 'givenName']),
      'last_name': get_from(profile, ['name', 'familyName']),
      'image_url': get_from(profile, ['image', 'url']),
    }

    row_user_auth = UserAuth.query.filter(
      and_(
        UserAuth.auth_key==Auth.GOOGLE,
        UserAuth.auth_id==google_auth_id
      )
    ).first()
    if not row_user_auth:
      row_user = User(**user)
      db_session.add(row_user)
      db_session.commit()

      row_user_auth = UserAuth(
        user_id=row_user.user_id,
        auth_key=Auth.GOOGLE,
        auth_id=google_auth_id
      )
      db_session.add(row_user_auth)
      db_session.commit()
    else:
      row_user = row_user_auth.user
      for k,v in user.items():
        setattr(row_user, k, v)
      db_session.merge(row_user)
      db_session.commit()

    session['user'] = row_user.to_json()

  def block_user(self, identifier, active):
    current_user = self.current_user
    user = self._get_user(identifier)

    row_block = Block(
      user_id = current_user.user_id,
      block_id = user.user_id,
      active = active
    )
    db_session.merge(row_block)
    db_session.commit()

    return self._get_user(identifier)

  @property
  def current_user_id(self):
    user_id = None
    if 'user' in session:
      user_id = session['user']['user_id']
    return user_id

  @property
  def current_user(self):
    user_id = self.current_user_id
    user = None
    if user_id:
      user = User.query.filter(User.user_id == user_id).first()
    return user

  def follow_user(self, identifier, active):
    current_user = self.current_user
    user = self._get_user(identifier)

    row_follow = Follow(
      user_id = current_user.user_id,
      follow_id = user.user_id,
      active = active
    )
    db_session.merge(row_follow)
    db_session.commit()

    return self._get_user(identifier)

  def get_blocking(self, user=None):
    if user is None: user = self.current_user
    return user.blocked_users.filter(Block.active).all()

  def get_followers(self, user=None):
    if user is None: user = self.current_user
    return user.follower_users.filter(Follow.active).all()

  def get_following(self, user=None):
    if user is None: user = self.current_user
    return user.following_users.filter(Follow.active).all()

  # TODO: Add ranking algorithm
  def get_following_recommended(self, user=None):
    if user is None: user = self.current_user

    already_following_user_ids = alias(
      db_session.query(
        Follow.follow_id
      ).select_from(
        Follow
      ).filter(
        and_(
          Follow.user_id == user.user_id,
          Follow.active
        )
      ),
      'already_following_user_ids'
    )

    blocking_user_ids = alias(
      db_session.query(
        Block.block_id
      ).select_from(
        Block
      ).filter(
        and_(
          Block.user_id == user.user_id,
          Block.active
        )
      ),
      'blocking_user_ids'
    )

    query = db_session.query(
      User
    ).filter(
      and_(
        User.user_id != user.user_id,
        ~User.user_id.in_(already_following_user_ids),
        ~User.user_id.in_(blocking_user_ids)
      )
    ).limit(
      5
    ).all()

    return query

  def get_user(self, identifier):
    return self._get_user(identifier)

  def get_users(self):
    return User.query.all()


import cv2, sys, numpy, os
import sqlite3
def Show_menu():
    print('1)Create Dataset')
    print('2)Train+Recognite+Attendance')
    print('3)Attendance Sheet')
    print('4)Exit')
def Create_Database():
    haar_file = 'haarcascade_frontalface_default.xml'
    datasets = 'datasets'  #All the faces data will be present this folder
    sub_data = input('Enter your ID ')    #This will creater folders in datasets with the face of people, so change it's name everytime for the new person.
    name = input('Enter your name ')
    con = sqlite3.connect('db_class.db')
    cur = con.cursor()
    sql = "INSERT INTO student (ID,Name) VALUES (?,?)"
    value=(sub_data,name)
    cur.execute(sql,value)
    con.commit()
    con.close()

    path = os.path.join(datasets, sub_data)
    if not os.path.isdir(path):
        os.mkdir(path)
    (width, height) = (130, 100)    # defining the size of images

    face_cascade = cv2.CascadeClassifier(haar_file)
    webcam = cv2.VideoCapture(0) #'0' is use for my webcam, if you've any other camera attached use '1' like this

    # The program loops until it has 30 images of the face.
    count = 1
    while count < 1001:
        (_, im) = webcam.read()
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 4)
        for (x,y,w,h) in faces:
            cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)
            face = gray[y:y + h, x:x + w]
            face_resize = cv2.resize(face, (width, height))
            cv2.imwrite('%s/%s.png' % (path,count), face_resize)
        count += 1
        cv2.imshow('OpenCV', im)
        key = cv2.waitKey(150)
        if key == 27:
            break
    print('Finish Process')
    cv2.waitKey(1)
    input('Press Enter to close window and camera.')
    cv2.destroyAllWindows()
    webcam.release()

def Face_rec():

    haar_file = 'haarcascade_frontalface_alt2.xml'#detect object
    datasets = 'datasets'
    week = input('Input week  ')
    con = sqlite3.connect('db_class.db')
    cur = con.cursor()
    print('Training...')
    print('Press Space Bar to close window and camera.')
    # Create a list of images and a list of corresponding names
    (images, labels, names, id) = ([], [], {}, 0)
    for (subdirs, dirs, files) in os.walk(datasets):
        for subdir in dirs:
            names[id] = subdir
            subjectpath = os.path.join(datasets, subdir)
            for filename in os.listdir(subjectpath):
                path = subjectpath + '/' + filename
                label = id
                images.append(cv2.imread(path, 0))
                labels.append(int(label))
            id += 1
    (width, height) = (130, 100)
    # Create a Numpy array from the two lists above
    (images, labels) = [numpy.array(lis) for lis in [images, labels]]
    model =  cv2.face.LBPHFaceRecognizer_create()
    model.train(images, labels)

    #use LBPHFace recognizer on camera frame
    face_cascade = cv2.CascadeClassifier(haar_file)
    webcam = cv2.VideoCapture(0)
    while True:
        (_, im) = webcam.read()
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x,y,w,h) in faces:
            cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)
            face = gray[y:y + h, x:x + w]
            face_resize = cv2.resize(face, (width, height))
            #Try to recognize the face
            prediction = model.predict(face_resize)
            cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)
            if prediction[1]<70:
                cv2.putText(im,'%s - %.0f' % (names[prediction[0]],prediction[1]),(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
                if week=='1':
                    sql = "UPDATE student SET Week1= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='2':
                    sql = "UPDATE student SET Week2= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='3':
                    sql = "UPDATE student SET Week3= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='4':
                    sql = "UPDATE student SET Week4= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='5':
                    sql = "UPDATE student SET Week5= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='6':
                    sql = "UPDATE student SET Week6= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='7':
                    sql = "UPDATE student SET Week7= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='8':
                    sql = "UPDATE student SET Week8= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
            else:
                cv2.putText(im,'Unknown',(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
        cv2.imshow('OpenCV', im)
        key = cv2.waitKey(1)
        if key == 27:
            break
    cv2.destroyAllWindows()
    webcam.release()

def Crete_datasheet():
    con = sqlite3.connect('db_class.db')
    with con:
        cur = con.cursor()
        cur.execute("SELECT * FROM student")
        rows = cur.fetchall()
        for row in rows:
            print(row)
while True:
    Show_menu()
    event=int(input('Enter your choose '))
    if event ==1:
        Create_Database()
    if event ==2:
        Face_rec()
    if event ==3:
        Crete_datasheet()
    if event==4:
        break
    else:
        Show_menu()
        event=int(input('Enter your choose again '))


# CMPT 145: Binary trees

# This file is copyright (c) Michael C Horsch, provided solely for the
# use of CMPT 145 students.  Students are welcome to use this file
# for their own work, and make copies for their own personal use.
# This file should not be shared for any reason without explicit
# consent of the author.

# Defines the kv tree node ADT
#
# A KVTreeNode is a simple container with four pieces of
# information:
#   key:   the key for the node, used to organize the tree
#   value: the contained information
#   left:  a reference to another KVTreeNode, or None
#   right: a reference to another KVTreeNode, or None

# Implementation notes:
#    This is an  ADT, using objects instead of dictionaries
#    Attributes are public for convenience

class KVTreeNode(object):
    def __init__(self, key, value, left=None, right=None):
        """
        Purpose:
            Create a new KVTreeNode for the given data.
        Pre-conditions:
            key:    A key used to identify the node
            value:  Any data value to be stored in the KVTreeNode
            left:   Another KVTreeNode (or None, by default)
            right:  Another KVTreeNode (or None, by default)
        """
        self.value = value
        self.left = left
        self.right = right
        self.key = key


#
# @file plugin.py
#
# Connect Zen Coding to Gedit.
#

import gedit, gobject, string, gtk, re, core

class ZenCodingPlugin(gedit.Plugin):
    """A Gedit plugin to implement Zen Coding's HTML and CSS shorthand expander."""

    def activate(self, window):
        "Install the expansion feature upon activation."

        ui_manager = window.get_ui_manager()
        action_group = gtk.ActionGroup("GeditZenCodingPluginActions")

        # Create the GTK action to be used to connect the key combo
        # to the Zen Coding expansion (i.e., the good stuff).
        complete_action = gtk.Action(name="ZenCodingAction",
                                     label="Expand Zen code...",
                                     tooltip="Expand Zen Code in document to raw HTML/CSS",
                                     stock_id=gtk.STOCK_GO_FORWARD)

        # Connect the newly created action with key combo
        complete_action.connect("activate",
                                lambda a: self.expand_zencode(window))
        action_group.add_action_with_accel(complete_action,
                                           "<Ctrl><Shift>E")

        ui_manager.insert_action_group(action_group, 0)

        # @TODO: Figure out what these lines do
        ui_merge_id = ui_manager.new_merge_id()
        ui_manager.add_ui(ui_merge_id,
                          "/MenuBar/EditMenu/EditOps_5",
                          "ZenCoding",
                          "ZenCodingAction",
                          gtk.UI_MANAGER_MENUITEM, False)
        ui_manager.__ui_data__ = (action_group, ui_merge_id)

    def deactivate(self, window):
        "Get rid of the expansion feature upon deactivation"

        ui_manager = window.get_ui_manager()
        (action_group, ui_merge_id) = ui_manager.__ui_data__

        # Remove the UI data, action group, and UI itself from Gedit
        del ui_manager.__ui_data__
        ui_manager.remove_action_group(action_group)
        ui_manager.remove_ui(ui_merge_id)

    def expand_zencode(self, window):
        "Take the shorthand code, expand it, and stick it back in the document."

        view = window.get_active_view()
        buffer = view.get_buffer()
        cursor_iter = buffer.get_iter_at_mark(buffer.get_insert())
        statusbar = window.get_statusbar()

        # Grab the current line.
        line = self.get_line(buffer, cursor_iter)
        before = self.get_shorthand(line)
        if not before:
            return

        # Generate expanded code from the shorthand code based on the document's language.
        lang = self.get_language(window)
        after = core.expand_abbreviation(before, 'css' if lang == 'CSS' else 'html', 'xhtml')
        if not after:
            return

        # Indent the expanded code according to editor's preferences.
        after = self.indent_code(line, after, window)

        # Replace the shorthand code with the expanded code.
        if self.replace_with_expanded(cursor_iter, buffer, before, after):
            statusbar.push(statusbar.get_context_id('ZenCodingPlugin'), 'Expanded shorthand code into the real stuff.')
        else:
            statusbar.push(statusbar.get_context_id('ZenCodingPlugin'), 'Code couldn\'t expand. Try checking your syntax for mistakes.')

    def get_line(self, buffer, cursor_iter):
        """Get the full line currently being edited."""

        # Grab the first character in the line.
        line_iter = cursor_iter.copy()
        line_iter.set_line_offset(0)

        # Grab the text from the start of the line to the cursor.
        line = buffer.get_text(line_iter, cursor_iter)

        return line

    def indent_code(self, line, code, editor):
        """Indent the code properly according to the editor's preferences."""

        # Automatically indent the string and replace \t (tab) with the
        # correct number of spaces.
        code = core.pad_string(code, re.match(r"\s*", line).group())
        if editor.get_active_view().get_insert_spaces_instead_of_tabs():
            code = code.replace("\t", " " * editor.get_active_view().get_tab_width())

        return code

    def get_language(self, editor):
        """Get the language of the current document."""

        lang = editor.get_active_document().get_language()
        lang = lang and lang.get_name()

        return lang

    def get_shorthand(self, line):
        """Grab the last word typed (i.e., the shorthand code)."""

        # Find the last space in the line and remove it, setting a variable
        # 'before' to the current line.
        words = line.split(" ")
        word = words[-1].lstrip()
        if not word:
            word = None

        return word

    def replace_with_expanded(self, cursor_iter, buffer, before, after):
        """Replace the shorthand cod with the expanded code."""

        # We are currently lame and do not know how to do placeholders.
        # So remove all | characters from after.
        after = after.replace("|", "")

        # Delete the last word in the line (i.e., the 'before' text, aka the
        # Zen un-expanded code), so that we can replace it.
        word_iter = cursor_iter.copy()
        word_iter.set_line_index(cursor_iter.get_line_index() - len(before))
        buffer.delete(word_iter, cursor_iter)

        # Insert the new expanded text.
        buffer.insert_at_cursor(after)

        return True


from collective.transmogrifier import transmogrifier
from ftw.builder import session
from ftw.builder.testing import BUILDER_LAYER
from ftw.builder.testing import set_builder_session_factory
from ftw.testing import ComponentRegistryLayer
from ftw.testing.quickinstaller import snapshots
from opengever.base import model
from opengever.base.model import create_session
from opengever.meeting.interfaces import IMeetingSettings
from opengever.ogds.base.setup import create_sql_tables
from opengever.ogds.models import BASE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PLONE_FIXTURE
from plone.app.testing import PloneSandboxLayer
from plone.app.testing import ploneSite
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.browserlayer.utils import unregister_layer
from plone.dexterity.schema import SCHEMA_CACHE
from plone.registry.interfaces import IRegistry
from plone.testing import Layer
from plone.testing import z2
from Products.CMFCore.utils import getToolByName
from Testing.ZopeTestCase.utils import setupCoreSessions
from z3c.saconfig import EngineFactory
from z3c.saconfig import GloballyScopedSession
from z3c.saconfig.interfaces import IEngineFactory
from z3c.saconfig.interfaces import IScopedSession
from zope.component import getUtility
from zope.component import provideUtility
from zope.configuration import xmlconfig
from zope.sqlalchemy import datamanager
import transaction

snapshots.disable()

def clear_transmogrifier_registry():
    transmogrifier.configuration_registry._config_info = {}
    transmogrifier.configuration_registry._config_ids = []

def setup_sql_tables():
    # Create opengever.ogds.base SQL tables
    create_sql_tables()

    # Create opengever.globalindex SQL tables
    model.Base.metadata.create_all(create_session().bind)

    # Activate savepoint "support" for sqlite
    # We need savepoint support for version retrieval with CMFEditions.
    if 'sqlite' in datamanager.NO_SAVEPOINT_SUPPORT:
        datamanager.NO_SAVEPOINT_SUPPORT.remove('sqlite')

def truncate_sql_tables():
    tables = BASE.metadata.tables.values() + \
        model.Base.metadata.tables.values()

    session = create_session()

    for table in tables:
        session.execute(table.delete())

class AnnotationLayer(ComponentRegistryLayer):
    """Loads ZML of zope.annotation.
    """

    def setUp(self):
        super(AnnotationLayer, self).setUp()
        import zope.annotation
        self.load_zcml_file('configure.zcml', zope.annotation)

ANNOTATION_LAYER = AnnotationLayer()

class OpengeverFixture(PloneSandboxLayer):

    defaultBases = (PLONE_FIXTURE, BUILDER_LAYER)

    def testSetUp(self):
        super(OpengeverFixture, self).testSetUp()
        setup_sql_tables()

    def testTearDown(self):
        truncate_sql_tables()
        from opengever.testing.sql import reset_ogds_sync_stamp
        with ploneSite() as portal:
            reset_ogds_sync_stamp(portal)
        super(OpengeverFixture, self).testTearDown()

    def setUpZope(self, app, configurationContext):
        # do not install pas plugins (doesnt work in tests)
        from opengever.ogds.base import hooks
        hooks._setup_scriptable_plugin = lambda *a, **kw: None

        xmlconfig.string(
            '<configure xmlns="http://namespaces.zope.org/zope">'

            '  <include package="z3c.autoinclude" file="meta.zcml" />'
            '  <includePlugins package="plone" />'
            '  <includePluginsOverrides package="plone" />'

            '  <include package="opengever.ogds.base" file="tests.zcml" />'
            '  <include package="opengever.setup.tests" />'

            '</configure>',
            context=configurationContext)

        z2.installProduct(app, 'plone.app.versioningbehavior')

        setupCoreSessions(app)

        # Set max subobject limit to 0 -> unlimited
        # In tests this is set to 100 by default
        transient_object_container = app.temp_folder.session_data
        transient_object_container.setSubobjectLimit(0)

    def setUpPloneSite(self, portal):
        self.installOpengeverProfiles(portal)
        self.createMemberFolder(portal)

    def tearDown(self):
        super(OpengeverFixture, self).tearDown()
        clear_transmogrifier_registry()

    def installOpengeverProfiles(self, portal):
        # Copied from metadata.zxml of opengever.policy.base:default
        # The aim is to use the opengever.policy.base:default here, but it
        # changes some things such as the language which will result in
        # lots of failing tests.
        applyProfile(portal, 'plone.app.dexterity:default')
        applyProfile(portal, 'plone.app.registry:default')
        applyProfile(portal, 'opengever.globalindex:default')
        applyProfile(portal, 'opengever.ogds.base:default')
        applyProfile(portal, 'opengever.base:default')
        applyProfile(portal, 'opengever.document:default')
        applyProfile(portal, 'opengever.mail:default')
        applyProfile(portal, 'opengever.dossier:default')
        applyProfile(portal, 'opengever.repository:default')
        applyProfile(portal, 'opengever.journal:default')
        applyProfile(portal, 'opengever.task:default')
        applyProfile(portal, 'opengever.tabbedview:default')
        applyProfile(portal, 'opengever.trash:default')
        applyProfile(portal, 'opengever.inbox:default')
        applyProfile(portal, 'opengever.tasktemplates:default')
        applyProfile(portal, 'opengever.portlets.tree:default')
        applyProfile(portal, 'opengever.contact:default')
        applyProfile(portal, 'opengever.advancedsearch:default')
        applyProfile(portal, 'opengever.sharing:default')
        applyProfile(portal, 'opengever.latex:default')
        applyProfile(portal, 'opengever.meeting:default')
        applyProfile(portal, 'ftw.datepicker:default')
        applyProfile(portal, 'plone.formwidget.autocomplete:default')
        applyProfile(portal, 'plone.formwidget.contenttree:default')
        applyProfile(portal, 'ftw.contentmenu:default')

    def createMemberFolder(self, portal):
        # Create a Members folder.
        setRoles(portal, TEST_USER_ID, ['Manager'])
        portal.invokeFactory('Folder', 'Members')
        portal['Members'].invokeFactory('Folder', TEST_USER_ID)
        setRoles(portal, TEST_USER_ID, ['Member'])

class MemoryDBLayer(Layer):
    """A Layer which only set up a test sqlite db in to the memory
    """

    def testSetUp(self):
        super(MemoryDBLayer, self).testSetUp()
        setup_sql_tables()
        self.session = create_session()

    def testTearDown(self):
        truncate_sql_tables()
        transaction.abort()

def integration_session_factory():
    sess = session.BuilderSession()
    sess.session = create_session()
    return sess

def functional_session_factory():
    sess = session.BuilderSession()
    sess.auto_commit = True
    sess.session = create_session()
    return sess

def memory_session_factory():
    engine_factory = EngineFactory('sqlite:///:memory:')
    provideUtility(
        engine_factory, provides=IEngineFactory, name=u'opengever_db')

    scoped_session = GloballyScopedSession(engine=u'opengever_db')
    provideUtility(
        scoped_session, provides=IScopedSession, name=u'opengever')

    return functional_session_factory()

MEMORY_DB_LAYER = MemoryDBLayer(
    bases=(BUILDER_LAYER,
           set_builder_session_factory(memory_session_factory)),
    name='opengever:core:memory_db')

OPENGEVER_FIXTURE = OpengeverFixture()

OPENGEVER_INTEGRATION_TESTING = IntegrationTesting(
    bases=(OPENGEVER_FIXTURE,
           set_builder_session_factory(integration_session_factory)),
    name="opengever.core:integration")

OPENGEVER_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(OPENGEVER_FIXTURE,
           set_builder_session_factory(functional_session_factory)),
    name="opengever.core:functional")

def activate_filing_number(portal):
    applyProfile(portal, 'opengever.dossier:filing')
    transaction.commit()

def inactivate_filing_number(portal):
    unregister_layer('opengever.dossier.filing')

    portal_types = getToolByName(portal, 'portal_types')
    fti = portal_types.get('opengever.dossier.businesscasedossier')
    fti.behaviors = [behavior for behavior in fti.behaviors
                     if not behavior.endswith('IFilingNumber')]

    SCHEMA_CACHE.invalidate('opengever.dossier.businesscasedossier')

class FilingLayer(PloneSandboxLayer):

    defaultBases = (OPENGEVER_FUNCTIONAL_TESTING,)

    def setUpPloneSite(self, portal):
        activate_filing_number(portal)

    def tearDownPloneSite(self, portal):
        inactivate_filing_number(portal)

OPENGEVER_FUNCTIONAL_FILING_LAYER = FilingLayer()

class MeetingLayer(PloneSandboxLayer):

    def setUpPloneSite(self, portal):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMeetingSettings)
        settings.is_feature_enabled = True
        transaction.commit()

    def tearDownPloneSite(self, portal):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMeetingSettings)
        settings.is_feature_enabled = False
        transaction.commit()

    defaultBases = (OPENGEVER_FUNCTIONAL_TESTING,)

OPENGEVER_FUNCTIONAL_MEETING_LAYER = MeetingLayer()


#(五)分离颜色通道&多通道图像混合
import cv2
'''
cv2.split(m, mv):将一个多通道数组分离成几个单通道数组
m：我们需要进行分离的多通道数组
mv：函数的输出数组或者输出的vector容器
'''
import  argparse
import numpy as np

img1 = cv2.imread("2.jpg")
(B,G,R) = cv2.split(img1)
cv2.imshow("Red", R)
cv2.imshow("Green", G)
cv2.imshow("Blue",B)
print("R_shape:",G.shape,"  G_shape:",G.shape,"  B_shape:",B.shape)

#通道扩展
zeros = np.zeros(img1.shape[:2], np.uint8)
img_B = cv2.merge([B,zeros,zeros])
cv2.imshow("B_", img_B)
img_G = cv2.merge([zeros,G,zeros])
cv2.imshow("G_", img_G)
img_R = cv2.merge([zeros,zeros,R])
cv2.imshow("R_", img_R)

cv2.waitKey()
cv2.destroyAllWindows()


'''Extend your program above. Draw five stars, but between each, pick up the pen, move forward by
350 units, turn right by 144, put the pen down, and draw the next star. You’ll get something like this
(note that you will need to move to the left before drawing your first star in order to fit everything
in the window):'''

import turtle

def drawStar(t,sz):
    for i in range (5):
        for i in range(5):
            t.forward(sz)
            t.right(144)
        t.penup()
        t.right(144)
        t.forward(350)
        t.pendown()

wn=turtle.Screen()
cami=turtle.Turtle()

cami.penup()
cami.goto(100,0)
cami.pendown()
drawStar(cami,50)

wn.exitonclick()


import click
from renku_r_tools.packrat import Packrat

@click.group()
def cli():
    pass

@cli.command(help = 'Link packrat libraries to another location.')
@click.option('-p', '--proj_dir',
    help = 'R project main directory path. Use absolute path.',
    required = True)
@click.option('-s', '--source',
    default = '/home/rstudio/packrat',
    help = 'Main directory path of the new packrat libraries source. \
Use absolute path.',
    required = True,
    show_default = True)
@click.option('-f', '--force',
    is_flag = True,
    help = 'Overwrite existing libraries.')
@click.option('-v', '--verbose',
    is_flag = True,
    help = 'Print various messages.')
def ln_packrat_lib(proj_dir, source, force, verbose):
    packrat = Packrat(proj_dir = proj_dir)
    packrat.ln_lib(ln_source = source,
        force = force,
        verbose = verbose)

if __name__ == '__main__':
    cli()


# 首先导入tk
import tkinter as tk

# 定义窗口
window = tk.Tk()

window.title('BIN信息管理系统')

window.geometry('600x400')

# 定义一个输入文本框
# entry = tk.Entry(window, show="*")
# 表示输入的字符以*号的形式出现
entry = tk.Entry(window, show=None)
# 对文本框内容进行打包
entry.pack()

# 定义一个插入在鼠标所在位置的函数
def insert_point():
    var = entry.get()
    t.insert('insert', var)

# 定义按钮
b1 = tk.Button(window, text='插入到指定位置', width=15, height=2, command=insert_point)
# 打包按钮
b1.pack()

window.mainloop()


"""
Room

Rooms are simple containers that has no location of their own.

"""

from evennia import DefaultRoom
from evennia.utils.utils import inherits_from
from typeclasses import exits

class Room(DefaultRoom):
    def log_action(self, message):
        if not self.db.logged:
            self.db.logged = []
        self.db.logged.append(message)
    def at_object_creation(self):
        self.db.image = "http://mud.streetwitch.com/static/website/images/1.jpg"
        self.db.logged = []
    def return_appearance(self, looker):
        if not looker:
            return
        looker.msg("\n")
        looker.msg(image=[self.db.image, ""])
        # get and identify all objects
      #  visible = (con for con in self.contents if con != looker and con.access(looker, "view")
#and (looker.db.alive == con.db.alive or looker.db.sight ==1 or con.destination) and con.db.invis == 0)
        visible = (con for con in self.contents if con != looker and con.access(looker, "view") and ((looker.db.alive == con.db.alive) or (looker.db.sight or con.destination)))

        exits, users, things = [], [], []
        for con in visible:
            key = con.get_display_name(looker)
            if con.destination:
                exits.append("|c|lc%s|lt%s|le" % (con, key))
            elif con.has_account:
                if(con.db.alive == 0):
                    users.append("|c|lclook %s|lt%s|le|n" % (con, key))
                if(con.db.alive == 1):
                    users.append("|c|lclook %s|lt%s|le|n" % (con, key))
            else:
                things.append("|c|lclook %s|lt%s|le" % (con, key))
        # get description, build string
        string = "|c%s|n\n" % self.get_display_name(looker)
        if(looker.db.alive == 1):
            desc = self.db.desc
        if(looker.db.alive == 0):
            desc = self.db.spiritdesc
        if desc:
            string += "%s" % desc
        if exits:
            string += "\n|wExits:|n " + ", ".join(exits)
        if users or things:
            string += "\n|wYou see:|n " + ", ".join(users + things)
        return string


from __future__ import print_function
from espressomd import System, electrostatics, electrostatic_extensions
from espressomd.shapes import Wall
import espressomd
import numpy as np
from threading import Thread
from math import *
from vtf_files import _writevsf, _writevcf, _vtf_pid_map

required_features = ["EXTERNAL_FORCES", "MASS", "ELECTROSTATICS", "LENNARD_JONES"]
espressomd.assert_features(required_features)

def pprint(message):
    print('\n')
    print(str(message).center(80, '.'))

'''-----------------'''
'''GLOBAL PARAMETERS'''
'''-----------------'''

name = "z_density_test"      #z_density, z_density_no_Ez, z_density_long, z_density_long_no_Ez, z_density_short
# System parameters
scale = 5   #scale # no of particles
simulate = True
#simulate = False
num_steps_equilibration = 20  #relax steps
num_configs = 200              # integration steps
integ_steps_per_config = 500   #steps per integration step
Ez = 10     # capacitor on, of
e = 1
e = 1.602e-19

n_part = int(5000)    #Total number of particles, # of Cl + # of Na
n_ionpairs = int(n_part / 2)
density = 1.1138      # Define density, timestep and temperature
time_step = 0.001823
temp = 1198.3
temp = 1
gamma = 1
#l_bjerrum = 0.885^2 * e^2/(4*pi*epsilon_0*k_B*T) #Bjerrum length
l_bjerrum = 130878.0 / temp
wall_margin = 0.5

# Particle parameters
types       = {"Cl":          0, "Na": 1, "Electrode": 2}
numbers     = {"Cl": n_ionpairs, "Na": n_ionpairs}
charges     = {"Cl":       -1.0*e, "Na": 1.0*e}
lj_sigmas   = {"Cl":       3.85, "Na": 2.52,  "Electrode": 3.37}
lj_epsilons = {"Cl":     192.45, "Na": 17.44, "Electrode": 24.72}

WCA_cut = 2.**(1. / 6.)
lj_cuts     = {"Cl":        WCA_cut * lj_sigmas["Cl"],
               "Na":        WCA_cut * lj_sigmas["Na"],
               "Electrode": WCA_cut * lj_sigmas["Electrode"]}

masses      = {"Cl":  35.453, "Na": 22.99, "Electrode": 12.01}

'''-----------------'''
'''  SYSTEM CONFIG  '''
'''-----------------'''
# Setup System
box_l = (n_ionpairs * sum(masses.values()) / density)**(1. / 3.)    # Use density and mass of particles to calculate boxsize
box_z = box_l + 2.0 * (lj_sigmas["Electrode"] + wall_margin)
elc_gap = box_z * 0.15     #Electrostatic Layer Correction, gap=height of the empty region between the system box and the neighboring artificial images
system = System(box_l=[box_l, box_l, box_z + elc_gap])
system.seed = 42
box_volume = np.prod([box_l, box_l, box_z])

system.periodicity = [1, 1, 1]    # p3m only with periodic boundary
system.time_step = time_step
system.cell_system.skin = 0.3
system.thermostat.set_langevin(kT=temp, gamma=gamma)

# Walls
system.constraints.add(shape=Wall(dist=wall_margin, normal=[0, 0, 1]),
                       particle_type=types["Electrode"])
system.constraints.add(shape=Wall(dist=-(box_z - wall_margin), normal=[0, 0, -1]),
                       particle_type=types["Electrode"])

'''-----------------'''
''' PLACE PARTICLES '''
'''-----------------'''

for i in range(int(n_ionpairs)):
    p = np.random.random(3) * box_l
    p[2] += lj_sigmas["Electrode"]
    system.part.add(id=len(system.part), type=types["Cl"],
                    pos=p, q=charges["Cl"], mass=masses["Cl"])
for i in range(int(n_ionpairs)):
    p = np.random.random(3) * box_l
    p[2] += lj_sigmas["Electrode"]
    system.part.add(id=len(system.part), type=types["Na"],
                    pos=p, q=charges["Na"], mass=masses["Na"])

# Lennard-Jones interactions parameters
def combination_rule_epsilon(eps1, eps2):
    '''Lorentz-Berthelot combination rule'''
    return (eps1 * eps2)**0.5

def combination_rule_sigma(sig1, sig2):
    '''Lorentz-Berthelot combination rule'''
    return (sig1 + sig2) * 0.5

'''-----------------'''
'''  Calculate  lj  '''
'''-----------------'''
for s in [["Cl", "Na"], ["Cl", "Cl"], ["Na", "Na"],
          ["Na", "Electrode"], ["Cl", "Electrode"]]:
    lj_sig = combination_rule_sigma(lj_sigmas[s[0]], lj_sigmas[s[1]])
    lj_cut = combination_rule_sigma(lj_cuts[s[0]], lj_cuts[s[1]])
    lj_eps = combination_rule_epsilon(lj_epsilons[s[0]], lj_epsilons[s[1]])

    system.non_bonded_inter[types[s[0]], types[s[1]]].lennard_jones.set_params(
        epsilon=lj_eps, sigma=lj_sig, cutoff=lj_cut, shift="auto")

if simulate == True:
    energy = system.analysis.energy()
    pprint("Before Minimization: E_total={}".format(energy['total']))
    system.minimize_energy.init(f_max=10, gamma=10, max_steps=2000,
                                max_displacement=0.1)
    system.minimize_energy.minimize()
    energy = system.analysis.energy()
    pprint("After Minimization: E_total={}".format(energy['total']))

    pprint("Tuning Electrostatics")
    # Tuning Electrostatics
    p3m = electrostatics.P3M(prefactor=l_bjerrum * temp,
                            accuracy=1e-2)
    system.actors.add(p3m)
    elc = electrostatic_extensions.ELC(gap_size=elc_gap,
                                    maxPWerror=1e-3)
    system.actors.add(elc)
    pprint("Done tuning electrostatics")

    '''---------------------'''
    ''' add capacitor field '''
    '''---------------------'''
    for p in system.part:
        p.ext_force = [0, 0, Ez * p.q]

    '''-----------------'''
    '''  relax system   '''
    '''-----------------'''
    pprint("Relax system")
    # Temperature Equilibration
    system.time = 0.0
    for i in range(int(num_steps_equilibration)):
        energy = system.analysis.energy()
        temp_measured = energy['kinetic'] / ((3.0 / 2.0) * n_part)
        print("progress={:.0f}%,\tt={:.1f}\tE_total={:.2f}\tE_coulomb={:.2f}\tT={:.4f}\r"
            .format(i * 100. / int(num_steps_equilibration - 1), system.time,
                    energy['total'], energy['coulomb'], temp_measured))
        system.integrator.run(integ_steps_per_config)

    pprint("Done relaxing system")

    # Integration
    bins = 100
    z_dens_na = np.zeros(bins)
    z_dens_cl = np.zeros(bins)
    system.time = 0.0
    cnt = 0

    f_vst = open("out_vst.dat", "w")
    f_vcf = open("out_vcf.dat", "w") #only one file

    '''-----------------'''
    '''Integrate System '''
    '''-----------------'''

    pprint("Integrate")
    energy = system.analysis.energy()
    _writevsf(system, f_vst, types='all', radiuses={}) #only once

    for i in range(num_configs):
        print('progress: {:3.0f}%\tEnergy total: {:4.3f}\tnon_bonded Energy: {:4.3}\tCoulomb Energy: {:4.3}'.format(
                i * 100. / num_configs, energy['total'], energy['non_bonded'], energy['coulomb']))
        energy = system.analysis.energy()
        temp_measured = energy['kinetic'] / ((3.0 / 2.0) * n_part)
        system.integrator.run(integ_steps_per_config)

        _writevcf(system, f_vcf, types='all',folded=True)

        for p in system.part:
            bz = int(p.pos[2] / box_z * bins)
            if p.type == types["Na"]:
                z_dens_na[bz] += 1.0
            elif p.type == types["Cl"]:
                z_dens_cl[bz] += 1.0
        cnt += 1

    f_vst.close()
    f_vcf.close()

    print('progress: 100%')

    # Analysis

    #radial distr fct
    rdf_bins = 100
    r_min = 0.0
    r_max = system.box_l[2] / 2.0
    r, rdf = system.analysis.rdf(rdf_type='rdf', type_list_a=[0], type_list_b=[1],
                            r_min=r_min, r_max=r_max, r_bins=rdf_bins)
    r_rdf = np.column_stack((r, rdf))
    np.savetxt(name+"_rdf.data", r_rdf, header="#r rdf")
    # Average / Normalize with Volume
    z_dens_na /= (cnt * box_volume / bins)
    z_dens_cl /= (cnt * box_volume / bins)
    z_values = np.linspace(0, box_l, num=bins)
    res = np.column_stack((z_values, z_dens_na, z_dens_cl))
    np.savetxt(name+".data", res,
                header="#z rho_na(z) rho_cl(z)")
else:
    res = np.loadtxt(name+".data")
    r_rdf = np.loadtxt(name+"_rdf.data")
    r = r_rdf[:, 0]
    rdf = r_rdf[:, 1]
    z_values = res[:, 0]
    z_dens_na = res[:, 1]
    z_dens_cl = res[:, 2]
pprint("Start plotting")
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6), dpi=80)
plt.subplot(211)
plt.plot(z_values, z_dens_na, label='Na')
plt.plot(z_values, z_dens_cl, label='Cl')
plt.plot([wall_margin+lj_sigmas['Electrode']/2, wall_margin+lj_sigmas['Electrode']/2], [0, np.max(z_dens_cl)])
plt.plot([np.max(z_values) - (wall_margin+lj_sigmas['Electrode']/2),
            np.max(z_values) - (wall_margin+lj_sigmas['Electrode']/2)], [0, np.max(z_dens_cl)])
plt.xlabel('$z$-axis $(\\mathrm{\\AA})$', fontsize=20)
plt.ylabel('Density $(\\mathrm{u\\AA}^{-3})$', fontsize=20)
plt.legend(fontsize=16)

plt.subplot(212)
plt.plot(r, rdf)
plt.xlabel('$r$ ', fontsize=20)
plt.ylabel('radial distr function g(r)', fontsize=20)
plt.show()

pprint('Done Plotting')


# -*- coding: utf-8 -*-
import datetime
import json
import os

import requests
import requests.exceptions
import secretballot
import simplejson.scanner
from concurrency.fields import IntegerVersionField
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.http import QueryDict
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _
from readability.readability import Document, Unparseable
from taggit.managers import TaggableManager
from taggit.models import TagBase, GenericTaggedItemBase
from taggit_autosuggest.managers import TaggableManager

from frontend.models import Tip
# import the logging library
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)
ISSUE_STATUS_CHOICES = (('active', u'Активный'), ('draft', u'Черновик'),)

LIBRARY_SECTIONS = None

def load_library_sections():
    global LIBRARY_SECTIONS
    titles = [
        'Интересные проекты, инструменты, библиотеки',
        'Релизы'
    ]
    try:
        LIBRARY_SECTIONS = [Section.objects.get(title=title) for title in titles]
    except (ObjectDoesNotExist, Section.DoesNotExist):
        LIBRARY_SECTIONS = []

def get_start_end_of_week(dt):
    start = dt - datetime.timedelta(days=dt.weekday())
    end = start + datetime.timedelta(days=6)
    return start, end

class Keyword(TagBase):
    class Meta:
        verbose_name = _("Keyword")
        verbose_name_plural = _("Keywords")

class KeywordGFK(GenericTaggedItemBase):
    tag = models.ForeignKey(Keyword, related_name="%(app_label)s_%(class)s_items")

class Issue(models.Model):
    """Выпуск дайджеста."""
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )
    announcement = models.TextField(verbose_name=u'Анонс',
                                    null=True,
                                    blank=True, )
    image = models.ImageField(verbose_name=u'Постер',
                              upload_to='issues',
                              null=True,
                              blank=True, )
    date_from = models.DateField(verbose_name=u'Начало освещаемого периода',
                                 null=True,
                                 blank=True, )
    date_to = models.DateField(verbose_name=u'Завершение освещаемого периода',
                               null=True,
                               blank=True, )
    published_at = models.DateField(verbose_name=u'Дата публикации',
                                    null=True,
                                    blank=True, )
    status = models.CharField(verbose_name=u'Статус',
                              max_length=10,
                              choices=ISSUE_STATUS_CHOICES,
                              default='draft', )
    version = IntegerVersionField(verbose_name=u'Версия')

    tip = models.ForeignKey(Tip, null=True, blank=True, verbose_name=u'Совет')

    trend = models.CharField(verbose_name='Тенденция недели',
                             blank=True,
                             null=True,
                             max_length=255, )

    last_item = models.IntegerField(
        verbose_name='Последняя модерированая новость',
        blank=True,
        null=True, )

    def __str__(self):
        return self.title

    @property
    def link(self):
        return reverse('digest:issue_view', kwargs={'pk': self.pk})

    class Meta:
        ordering = ['-pk']
        verbose_name = u'Выпуск дайджеста'
        verbose_name_plural = u'Выпуски дайджеста'

SECTION_STATUS_CHOICES = (('pending', u'Ожидает проверки'),
                          ('active', u'Активный'),)

class Section(models.Model):
    """Раздел."""
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    priority = models.PositiveIntegerField(
        verbose_name=u'Приоритет при показе',
        default=0, )
    status = models.CharField(verbose_name=u'Статус',
                              max_length=10,
                              choices=SECTION_STATUS_CHOICES,
                              default='active', )
    version = IntegerVersionField(verbose_name=u'Версия')
    habr_icon = models.CharField(max_length=255,
                                 verbose_name=u'Иконка для хабры',
                                 null=True,
                                 blank=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['-pk']
        verbose_name = u'Раздел'
        verbose_name_plural = u'Разделы'

class Resource(models.Model):
    """Источник получения информации."""
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )
    link = models.URLField(max_length=255, verbose_name=u'Ссылка', )
    version = IntegerVersionField(verbose_name=u'Версия')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = u'Источник'
        verbose_name_plural = u'Источники'

ITEM_STATUS_CHOICES = (
    ('pending', u'На рассмотрении'),
    ('active', u'Активная'),
    ('draft', u'Черновик'),
    ('moderated', u'Рассмотрена'),
    ('autoimport', u'Автоимпорт'),
    ('queue', u'В очереди'),
)

ITEM_LANGUAGE_CHOICES = (('ru', u'Русский'), ('en', u'Английский'),)

def build_url(*args, **kwargs):
    params = kwargs.pop('params', {})
    url = reverse(*args, **kwargs)
    if not params: return url

    qdict = QueryDict('', mutable=True)
    for k, v in params.items():
        if type(v) is list:
            qdict.setlist(k, v)
        else:
            qdict[k] = v

    return url + '?' + qdict.urlencode()

class Item(models.Model):
    """Новость."""
    section = models.ForeignKey(Section,
                                verbose_name=u'Раздел',
                                null=True,
                                blank=True, )
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    is_editors_choice = models.BooleanField(verbose_name=u'Выбор редакции',
                                            default=False, )
    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )
    issue = models.ForeignKey(Issue,
                              verbose_name=u'Выпуск дайджеста',
                              null=True,
                              blank=True, )
    resource = models.ForeignKey(Resource,
                                 verbose_name=u'Источник',
                                 null=True,
                                 blank=True, )
    link = models.URLField(max_length=255, verbose_name=u'Ссылка', )
    additionally = models.CharField(max_length=255,
                                    verbose_name=u'Дополнительно',
                                    null=True,
                                    blank=True)
    related_to_date = models.DateField(
        verbose_name=u'Дата',
        help_text=u'Например, дата публикации новости на источнике',
        default=datetime.datetime.today, )
    status = models.CharField(verbose_name=u'Статус',
                              max_length=10,
                              choices=ITEM_STATUS_CHOICES,
                              default='pending', )
    language = models.CharField(verbose_name=u'Язык новости',
                                max_length=2,
                                choices=ITEM_LANGUAGE_CHOICES,
                                default='en', )
    created_at = models.DateField(verbose_name=u'Дата публикации',
                                  auto_now_add=True, )
    modified_at = models.DateTimeField(verbose_name=u'Дата изменения',
                                       null=True,
                                       blank=True, )

    activated_at = models.DateTimeField(
        verbose_name=u'Дата активации',
        default=datetime.datetime.now,
    )
    priority = models.PositiveIntegerField(
        verbose_name=u'Приоритет при показе',
        default=0, )
    user = models.ForeignKey(User,
                             verbose_name=u'Кто добавил новость',
                             editable=False,
                             null=True,
                             blank=True, )
    version = IntegerVersionField(verbose_name=u'Версия')
    tags = TaggableManager(blank=True)
    keywords = TaggableManager(through=KeywordGFK, blank=True, verbose_name=_("Keywords"))
    to_update = models.BooleanField(verbose_name=u'Обновить новость', default=False, )
    article_path = models.FilePathField(verbose_name='Путь до статьи', blank=True, null=True)

    def save(self, *args, **kwargs):
        try:
            if self.issue is None and self.created_at is not None:
                date_from, date_to = get_start_end_of_week(self.created_at)
                issue = Issue.objects.filter(date_from=date_from,
                                             date_to=date_to)
                if issue.count() == 0:
                    # если нет выпуска, то создадим
                    old_issue = Issue.objects.latest('date_to')
                    cnt_issue = int(old_issue.title.replace('Выпуск ', '')) + 1
                    new_issue = Issue(title='Выпуск %s' % cnt_issue,
                                      date_from=date_from,
                                      date_to=date_to, )
                    new_issue.save()
                    self.issue = new_issue
                elif issue.count() == 1:
                    self.issue = issue[0]
                else:
                    raise Exception('Несколько выпусков на неделе')

        except Exception as e:
            logger.error("Many issues are on one week: {0}".format(e))
        super(Item, self).save(*args, **kwargs)

    @property
    def cls_check(self):
        try:
            item = ItemClsCheck.objects.get(item=self)
            item.check_cls()
        except (ObjectDoesNotExist, ItemClsCheck.DoesNotExist):
            item = ItemClsCheck(item=self)
            item.save()
            item.check_cls(force=True)
        return item.status

    @property
    def link_type(self):
        global LIBRARY_SECTIONS
        if LIBRARY_SECTIONS is None:
            load_library_sections()
        if any([x == self.section_id for x in LIBRARY_SECTIONS]):
            return 'library'
        else:
            return 'article'

    @property
    def text(self):
        if self.article_path is not None and self.article_path and os.path.exists(self.article_path):
            with open(self.article_path, 'r') as fio:
                result = fio.read()
        else:
            try:
                resp = requests.get(self.link)
                text = resp.text
                try:
                    result = Document(text,
                                      min_text_length=50,
                                      positive_keywords=','.join(settings.DATASET_POSITIVE_KEYWORDS),
                                      negative_keywords=','.join(settings.DATASET_NEGATIVE_KEYWORDS)
                                      ).summary()
                except Unparseable:
                    result = text
            except (KeyError,
                    requests.exceptions.RequestException,
                    requests.exceptions.Timeout,
                    requests.exceptions.TooManyRedirects) as e:
                result = ''
            self.article_path = os.path.join(settings.DATASET_ROOT, '{0}.html'.format(self.id))
            with open(self.article_path, 'w') as fio:
                fio.write(result)
            self.save()
        return result

    def get_data4cls(self, status=False):
        result = {
            'link': self.link,
            'data': {
                'language': self.language,
                'title': self.title,
                'description': self.description,
                'article': self.text,
                'type': self.link_type,
            }
        }
        if status:
            result['data']['label'] = self.status == 'active'
        return result

    data4cls = property(get_data4cls)

    @property
    def internal_link(self):
        return reverse('digest:item', kwargs={'pk': self.pk})

    @property
    def tags_as_links(self):
        return [(x.name, build_url('digest:feed', params={'tag': x.name})) for x in self.tags.all()]

    @property
    def tags_as_str(self):
        if self.tags and self.tags.all():
            result = ','.join([x.name for x in self.tags.all()])
        else:
            result = 'Without tag'
        return result

    @property
    def keywords_as_str(self):
        return ', '.join(list(set([x.name for x in self.keywords.all()]))[:13])

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Новость'
        verbose_name_plural = 'Новости'

class ItemClsCheck(models.Model):
    item = models.OneToOneField(Item, verbose_name='Новость')
    last_check = models.DateTimeField(verbose_name='Время последней проверки', auto_now=True)
    status = models.BooleanField(default=False, verbose_name='Оценка')

    def check_cls(self, force=False):
        # print("Run check: {}".format(self.pk))
        if force or self.last_check <= datetime.datetime.now() - datetime.timedelta(days=10):

            try:
                url = "{0}/{1}".format(settings.CLS_URL_BASE, 'api/v1.0/classify/')
                resp = requests.post(url,
                                     data=json.dumps({'links': [
                                         self.item.data4cls
                                     ]}))
                self.status = resp.json()['links'][0].get(self.item.link, False)
            except (requests.exceptions.RequestException,
                    requests.exceptions.Timeout,
                    requests.exceptions.TooManyRedirects,
                    simplejson.scanner.JSONDecodeError) as e:
                self.status = False
            # print("Real run check: {}".format(self.pk))
            self.save()

    def __str__(self):
        return "{0} - {1} ({2})".format(str(self.item), self.status, self.last_check)

    class Meta:
        verbose_name = 'Проверка классификатором'
        verbose_name_plural = 'Проверка классификатором'

class AutoImportResource(models.Model):
    """Источники импорта новостей."""
    TYPE_RESOURCE = (('twitter', u'Сообщения аккаунтов в твиттере'),
                     ('rss', u'RSS фид'),)

    name = models.CharField(max_length=255, unique=True,
                            verbose_name=u'Название источника', )
    link = models.URLField(max_length=255, unique=True, verbose_name=u'Ссылка', )
    type_res = models.CharField(max_length=255,
                                verbose_name=u'Тип источника',
                                choices=TYPE_RESOURCE,
                                default='twitter', )
    resource = models.ForeignKey(Resource,
                                 verbose_name=u'Источник',
                                 null=True,
                                 blank=True, )
    incl = models.CharField(max_length=255,
                            verbose_name=u'Обязательное содержание',
                            help_text='Условие отбора новостей
 \
                   Включение вида [text]
 \
                   Включение при выводе будет удалено',
                            null=True,
                            blank=True, )
    excl = models.TextField(
        verbose_name=u'Список исключений',
        help_text='Список источников подлежащих исключению через ", "',
        null=True,
        blank=True, )
    in_edit = models.BooleanField(verbose_name=u'На тестировании',
                                  default=False, )

    language = models.CharField(verbose_name=u'Язык источника',
                                max_length=2,
                                choices=ITEM_LANGUAGE_CHOICES,
                                default='en', )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'Источник импорта новостей'
        verbose_name_plural = u'Источники импорта новостей'

class Package(models.Model):
    name = models.CharField(max_length=255, verbose_name=u'Название', )

    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )

    url = models.CharField(max_length=255, verbose_name=u'Ссылка', )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'Библиотека'
        verbose_name_plural = u'Библиотеки'

class ParsingRules(models.Model):
    IF_ELEMENTS = (('title', u'Заголовок новости'), ('link', u'Url новости'),
                   ('content', u'Текст новости'),
                   ('description', u'Описание новости'),
                   ('http_code', u'HTTP Code'),)

    IF_ACTIONS = (('equal', u'Равен'), ('contains', u'Содержит'),
                  ('not_equal', u'Не равен'), ('regex', u'Regex match'),)

    THEN_ELEMENT = (('title', u'Заголовок новости'),
                    ('description', u'Описание новости'),
                    ('section', u'Раздел'), ('status', u'Статус'),
                    ('tags', u'Тэг новости'),)

    THEN_ACTION = (('set', u'Установить'), ('add', u'Добавить'),
                   ('remove', u'Удалить часть строки'),)

    name = models.CharField(max_length=255, verbose_name=u'Название правила', )

    is_activated = models.BooleanField(verbose_name=u'Включено',
                                       default=True, )

    if_element = models.CharField(max_length=255,
                                  verbose_name=u'Элемент условия',
                                  choices=IF_ELEMENTS,
                                  default='item_title', )

    if_action = models.CharField(max_length=255,
                                 verbose_name=u'Условие',
                                 choices=IF_ACTIONS,
                                 default='consist', )

    if_value = models.CharField(max_length=255, verbose_name=u'Значение', )

    then_element = models.CharField(max_length=255,
                                    verbose_name=u'Элемент действия',
                                    choices=THEN_ELEMENT,
                                    default='item_title', )

    then_action = models.CharField(max_length=255,
                                   verbose_name=u'Действие',
                                   choices=THEN_ACTION,
                                   default='item_title', )

    then_value = models.CharField(max_length=255, verbose_name=u'Значение', )

    weight = models.PositiveIntegerField(default=100, verbose_name=_('Weight'))

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'Правило обработки'
        verbose_name_plural = u'Правила обработки'
        ordering = ['-weight']

@receiver(post_save, sender=Item)
def update_cls_score(instance, **kwargs):
    try:
        item = ItemClsCheck.objects.get(item=instance)
        item.check_cls()
    except (ObjectDoesNotExist, ItemClsCheck.DoesNotExist):
        item = ItemClsCheck(item=instance)
        item.save()
        item.check_cls(force=True)

secretballot.enable_voting_on(Item)


__author__ = 'keigo'

import sys
import csv

def load_recipe_data(csv_name):

    recipe_list = []
    reader = csv.reader(open(csv_name, mode='r', encoding='utf-8'))
    header = next(reader)

    for row in reader:
        recipe = {
            'recipe_id': row[0],
            'recipe_name': row[1],
        }
        recipe_list.append(recipe)

    return recipe_list

def print_recipe_all(recipe_list):
    for recipe in recipe_list:
        print(recipe['recipe_id'] + ': ' + recipe['recipe_name'])

def print_recipe_by_id(recipe_list, wanted_id):
    for recipe in recipe_list:
        if wanted_id == recipe['recipe_id']:
            print(recipe['recipe_id'] + ': ' + recipe['recipe_name'])

if __name__ == '__main__':
    recipe_csv = sys.argv[1]
    if len(sys.argv) == 3:
        wanted_recipe_id = sys.argv[2]
    else:
        wanted_recipe_id = None

    recipe_list = load_recipe_data(recipe_csv)

    if wanted_recipe_id:
        print_recipe_by_id(recipe_list, wanted_recipe_id)
    else:
        print_recipe_all(recipe_list)


# -*- coding: utf-8 -*-
"""
Sejong_Corpus Converter
Jaemin Cho/j-min

- This file works well with dsindex's SyntaxNet fork! (https://github.com/dsindex/syntaxnet)
- Remove every line but raw sentences and parsed sentences
- Remove sentences with errors
  ex)  [Q="12]"
        (Q [/SS + Q="12/Q" + ]/SS)
- Inputs: Raw Sejong Corpora files ex) BGHO0411.txt (UTF-16)

<!DOCTYPE tei.2 SYSTEM "c:\sgml\dtd\tei2.dtd" [
	<!ENTITY % TEI.corpus "INCLUDE">
	<!ENTITY % TEI.extensions.ent SYSTEM "sejong1.ent">
	<!ENTITY % TEI.extensions.dtd SYSTEM "sejong1.dtd">
]>

<tei.2>
<teiHeader>
	<fileDesc>
		<titleStmt>
			<title>하늘에 뜬 돌도끼, 형태소 분석 전자파일</title>
			<author>손동인</author>
			<sponsor>대한민국 문화관광부</sponsor>
			<respStmt>
				<resp>문헌입력, 표준화, 형태소 정보 부착</resp>
				<name>고려대학교 민족문화연구원</name>
			</respStmt>
		</titleStmt>
		<extent>22,790어절, 3,346문장</extent>
		<publicationStmt>
			<distributor>국립국어연구원</distributor>
			<idno>BGGO0098.txt, 형태분석: BTGO0098.txt, 원본:BRGO0098.txt</idno>
			<availability>배포 불가</availability>
		</publicationStmt>
		<notesStmt>
			<note>균형말뭉치에서 선정</note>
		</notesStmt>
		<sourceDesc>
			<bibl>
....

- Output: Unified Text file without irrelevant tags (UTF-8)
; 새 생명.
(NP 	(DP 새/MM)
	(NP 생명/NNG + ./SF))

; 나는 돈이다.
(S 	(NP_SBJ 나/NP + 는/JX)
	(VNP 돈/NNG + 이/VCP + 다/EF + ./SF))

; 만 원이라는 이름을 붙인 채, 이제 막 태어났다.
(VP 	(NP_AJT 	(VP_MOD 	(NP_OBJ 	(VNP_MOD 	(NP 만/NR)
					(VNP_MOD 원/NNB + 이/VCP + 라는/ETM))
				(NP_OBJ 이름/NNG + 을/JKO))
			(VP_MOD 붙이/VV + ᆫ/ETM))
		(NP_AJT 채/NNB + ,/SP))
	(VP 	(AP 이제/MAG)
		(VP 	(AP 막/MAG)
			(VP 태어나/VV + 았/EP + 다/EF + ./SF))))

; 스슥, 슥-. 이게 마지막 과정이었다.
(S 	(AP 	(AP 	(AP 	(AP 스슥/MAG + ,/SP)
				(AP 슥/MAG))
			(X -/SS))
		(X ./SF))
	(S 	(NP_SBJ 이것/NP + 이/JKS)
		(VNP 	(NP 마지막/NNG)
			(VNP 과정/NNG + 이/VCP + 었/EP + 다/EF + ./SF))))
....
"""

import codecs
import os
import re
import shutil

try:
    os.remove("sejong_treebank.txt.v1")
except:
    print ('No such file to remove!')
    pass

sentence_counter = 0

dirname = "stripped"
if not os.path.isdir("./raw_corpus/" + dirname + "/"):
    os.mkdir("./raw_corpus/" + dirname + "/")

dirname = "converted"
if not os.path.isdir("./raw_corpus/" + dirname + "/"):
    os.mkdir("./raw_corpus/" + dirname + "/")

def allfiles(path):
    res = []

    for root, dirs, files in os.walk(path):
        rootpath = os.path.join(os.path.abspath(path), root)

        for file in files:
            filepath = os.path.join(rootpath, file)
            res.append(filepath)

    return res

allfilelist = allfiles(os.getcwd() + '/raw_corpus')
sejonglist = [file for file in allfilelist if "BG" in file]
# print(sejonglist)

for file_counter, rawfile in enumerate(sejonglist):

    with codecs.open(rawfile, "r", encoding = "utf-16-le") as readfile:
        OUT_FILENAME = os.getcwd()+"/raw_corpus/stripped/"+rawfile[-12:-4]+"_stripped.txt"
        first_line = True
        whitespace_chars = ['(', ')', '<', '>', '{', '}', '[', ']', '-', '─', '·', '/', '~']

        with codecs.open(OUT_FILENAME, "w", "utf-8") as writefile:
            for line in readfile:
                if "; " == line[0:2]:
                    if first_line == False:
                        writefile.write("\n")

                    # place whitespaces before and after 'whitespace_chars'
                    temp = line.replace('─', '-')
                    temp = line.replace('―', '-')
                    temp = line.replace("`", "'")
                    for x in whitespace_chars:
                        temp = temp.replace(x, ' '+x+' ')
                    # Remove multiple whitespaces
                    temp = ' '.join(temp.split())
                    # temp = temp.replace('는지', '는 지')
                    temp = temp.replace('하는데', '하는 데')
                    temp = temp.replace('한데', '한 데')
                    temp = temp.replace('온후', '온 후')

                    if '앵, 앵, 앵' in line:
                        # exception
                        writefile.write('; 앵, 앵, 앵 - . 애-앵-. 밖에서 요란한 소리가 울렸다. ')
                    else:
                        writefile.write(temp)
                    writefile.write('\n')
                    sentence_counter += 1

                elif ("(" in line) and (")" in line[-3:]) and ("/" in line):
                    try:
                        temp = re.match('(.*)(\([A-Z_]+ *\t*)+(.+[A-Z]+)([\)]+)', line).groups(3)
                        del(temp)

                        #temp = temp.replace('NP + 데/NN', 'NP 데/NN')
                        #temp = temp.replace('NP_AJT + 데/NN', 'NP_AJT 데/NN')
                        #temp = temp.replace('NP_AJT + 대로/NN', 'NP_AJT 대로/NN')
                        #temp = temp.replace('(X + ∼/SO', '(X ∼/SO')
                        #temp = temp.replace('(X + -/SS', '(X -/SS')
                        temp = line

                    except:
                        #print(rawfile[-12:-4])
                        #print(line)
                        line_groups = re.match('(.*)(\([A-Z_]+ *\t*)+(.+[A-Z]+)[ \+]([\)]+)', line).groups()
                        temp = line_groups[2].rstrip()

                        # (VP + 내 / VV + 아 / EC)) 같이 처음에 + 나오는  오류 제거
                        if '+ ' in line_groups[2][:3]:
                            temp = temp[2:]
                        temp = line_groups[0]+line_groups[1]+temp+line_groups[3]
                        #print(temp)

                    temp = temp.replace('─', '-')
                    temp = temp.replace('―', '-')
                    temp = temp.replace("`", "'")
                    writefile.write(temp)
                    first_line = False
                elif ("<" in line) and not('(' in line) and not (';' in line):
                    # 맨 위에 있는 태그들
                    pass

        print(file_counter+1, OUT_FILENAME, "from", rawfile)

stripped_sejonglist = allfiles(os.getcwd()+"/raw_corpus/stripped")
# stripped_sejonglist = [file for file in strippedfilelist if "stripped" in file]
print(stripped_sejonglist)

errors = 0
error_string = ["Q1", "Q2", "Q3", 'Q4', 'Q5', 'Q6', 'Q=', '(Q', '; Q', '/Q', '/U', '/W', '/Y', '/Z', '(L', '(R']

for file_counter, strippedfile in enumerate(stripped_sejonglist):
    with codecs.open(strippedfile, 'r') as readfile:
        OUT_FILENAME = os.getcwd()+"/raw_corpus/converted/"+strippedfile[-21:-13]+"_converted.txt"
        error_sentence = False
        buffer = ""

        with codecs.open(OUT_FILENAME, "w") as writefile:
            error_counter = 0
            line_counter = 1

            for line in readfile:
                for x in error_string:
                    if x in line:
                        error_sentence = True
                        buffer = ""
                        # print(line_counter, line)

                if "\n" == line:
                    if error_sentence == False:
                        writefile.write(buffer)
                    else:
                        error_counter +=1
                    buffer = ""
                    error_sentence = False

                if error_sentence == False:
                    buffer += line

                line_counter += 1

        print(file_counter+1, OUT_FILENAME, "from", strippedfile, "| error counts: ", error_counter)
    errors += error_counter

converted_sejonglist = allfiles(os.getcwd()+"/raw_corpus/converted")
print(converted_sejonglist)

for file_counter, convertedfile in enumerate(converted_sejonglist):
    print(file_counter+1, convertedfile)

    with codecs.open(convertedfile, 'r', encoding = 'utf-8') as readfile:
        OUT_FILENAME = "sejong_treebank.txt.v1"
        with codecs.open(OUT_FILENAME, "a", "utf-8") as writefile:
            data = readfile.read()
            writefile.write(data)

for temp_directory in ["stripped","converted"]:
    try:
        shutil.rmtree(os.getcwd()+'/raw_corpus/'+temp_directory)
    except OSError as e:
        if e.errno == 2:
            # 파일이나 디렉토리가 없음!
            print ('No such file or directory to remove')
            pass
try:
    shutil.rmtree(os.getcwd()+'/wdir')
except OSError as e:
    if e.errno == 2:
        # 파일이나 디렉토리가 없음!
        print ('No such file or directory to remove')
        pass

print("Converting Sejong Corpus Complete!")
print("Raw corpus 총 문장: ", sentence_counter)
print("# of error sentences: ", errors)
print("남은 총 문장: ", sentence_counter - errors)


import sys
lines=sys.stdin.readlines()

endings=["ch","sh","x","s","z"]
vowels=["a","e","i","o","u"]
p=""

for word in lines:
	word=word.strip()

	if word.endswith("y")==True and word[-2] not in vowels :
		p=word[:-1]+"ies"
	elif word.endswith("f")==True:
		p=word[:-1]+"ves"
	elif word.endswith("fe")==True:
		p=word[:-2]+"ves"
	elif word.endswith("o")==True:
		p=word+"es"
	else:
		p=word+"s"

	for w in endings:
		if word.endswith(w)== True:
			p=word+"es"
	print(p)


#!/usr/bin/env python3
import re
import argparse

class HostNameCleaner:
    def __init__(self, orig_file):
        self.raw_file = orig_file
        self.hostnames = []
        self.clean_hostnames()

    def clean_hostnames(self):
        """
            Passed an Opened file
            Check if each line is a valid Hostname
            if line fails, return don't return that line
        """
        file_by_lines = self.raw_file.read().split("\n")
        for line in file_by_lines:
            if len(line) > 0 and line[0] != '#':
                host_sanitize_l1 = HostNameCleaner.remove_wildcard(line)
                host_sanitize_l2 = HostNameCleaner.is_valid_hostname(host_sanitize_l1)
                if host_sanitize_l2 is not None:
                    self.hostnames.append(host_sanitize_l2)

    @staticmethod
    def remove_wildcard(hostname):
        if hostname[0:2] == "*.":
            hostname = hostname[2:]
            hostname = HostNameCleaner.remove_wildcard(hostname)
        return hostname

    @staticmethod
    def is_valid_hostname(hostname):
        hostname_regex = re.compile("(?!-)[A-Z\d-]{5,63}(?<!-)$", re.IGNORECASE)
        num_periods = re.findall(r'\.', hostname)
        num_non_alpha = re.findall(r'\W', hostname)
        if len(hostname) > 255:
            return None
        if len(num_periods) == 0 or len(num_periods) > 4 or len(num_non_alpha) > len(num_periods):
            return None
        if len(re.findall(r'\s+', hostname)) > 0:    # remove hostnames with whitespace ( spaces / tabs )
            return None
        if all(hostname_regex.match(x) for x in hostname.split(".")) is not True:
            return hostname
        return None

if __name__ == '__main__':
    RE = re.compile(' +')
    parser = argparse.ArgumentParser(description="PyOpenSSL")
    parser.add_argument('--infile', type=argparse.FileType('r', encoding='UTF-8'), required=True)
    args = parser.parse_args()

    with args.infile as file:
        cleaner = HostNameCleaner(file)
        print(cleaner.hostnames)

########### input ###########
# no.commas,please
# stackoverflow.com
# httpbin.org
# weirddtstst
# GGorge. loves apples.
# *.google.com
# tab should  be  removed
# ....%%%%%....@@£££

########### Result ###########
# stackoverflow.com
# httpbin.org
# google.com


#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: Luo-Songtao
# Email: ryomawithlst@gmail/outlook.com
import math

from hash_table import BaseHashTable, HashTableKeyError

class HashTableOnLinkedList(BaseHashTable):
    """基于双向链表的hash表

    使用额外的双向链表存储hash表的元素

    """

    def _insert(self, key, value):
        hash_value = self.hash(key)
        linked_list = DoublyLinkedList() if self._slots[hash_value] is None else self._slots[hash_value]
        self._slots[hash_value] = linked_list
        linked_list.insert(DoublyLinkedListLink(key, value))

    def _delete(self, key):
        result = self._search(key)
        hash_value = self.hash(key)
        self._slots[hash_value].delete(result)
        if self._slots[hash_value].head == None:
            self._slots[hash_value] = None

    def _search(self, key):
        hash_value = self.hash(key)
        result = self._slots[hash_value] if self._slots[hash_value] is None \
                else self._slots[hash_value].search(key)
        if result is None:
            raise HashTableKeyError
        return result

    def __iter__(self):
        for i in self._slots:
            if i is not None:
                for item in i.traversal():
                    yield (item.key, item.value)

if __name__ == "__main__":
    import os
    import sys
    sys.path.insert(0, os.path.abspath('../linked_list/'))
    sys.path.insert(0, os.path.abspath('../../hashes/'))
    from doubly_linked_list import DoublyLinkedList, DoublyLinkedListLink
    from numberic_hashes.multiplicative_hash import multiplicative_hash
    from string_hashes.elf import elf_hash

    table_size = 2**16
    hash_table = HashTableOnLinkedList(table_size, multiplicative_hash, elf_hash)
    data_size = 2**13
    data = [("k%d"%i, "v%d"%i) for i in range(data_size)]

    hash_table.bulk_insert(data)
    print("Load Factor(expect={}): ".format(round(data_size/table_size, 2)), hash_table.load_factor)
    key = "k1234"
    print("Search data(key='{}'): ".format(key), hash_table.search(key))

    hash_table.delete(key)
    print("Deleted data(key='{}'): ".format(key))

    try:
        print("Search data(key='{}'): ".format(key), hash_table.search(key))
    except HashTableKeyError:
        print("The data(key='{}') not in the hash table".format(key))
    rehash_factor = 2
    hash_table.rehash(2)
    print("Rehash by factor={}".format(rehash_factor))
    print("Load Factor(expect={}): ".format(round(data_size/(rehash_factor*table_size), 2)), hash_table.load_factor)