# projection_helpers.py

import os
import sys
import h5py
import numba
import numpy as np
from sklearn.utils.extmath import randomized_svd

def offdiag_penalizer(reg, r, m):
    """Construct a list of regularization matrices that penalize the
    off-diagonal elements of A and all elements of the remaining operators.
    """
    regI = _np.sqrt(reg) * _np.eye(r + r**2 + m + 1)
    Gs = [regI] * r
    for i in range(r):
        Gs[r][r,r] = 0
    return Gs

def compute_randomized_svd(data, savepath, n_components=500):
    """Compute and save randomized SVD following
    https://scikit-learn.org/stable/modules/generated/sklearn.utils.extmath.randomized_svd.html

    Parameters
    ----------
    data : (n_variables, n_samples) ndarray
        Snapshot matrix for which the SVD will be computed.

    savepath : string
        Name of file to save the SVD to.

    n_components : float
        Number of singular values and vectors to compute and save.
    """

    Vr,Sigma,_ = randomized_svd(data, n_components, n_iter=15, random_state=42)

    svd_h5file = h5py.File(savepath,'w')
    svd_h5file.create_dataset("U", data=Vr)
    svd_h5file.create_dataset("S", data=Sigma)
    svd_h5file.close()

    return Vr,Sigma
    print(f"SVD computed and saved in {savepath}")

@numba.jit(nopython=True)
def compute_xdot(data, dt):
    """Compute xdot for a chunk of snapshots.

    Parameters
    ----------
    data : (Nfull, n_samples) ndarray
        The dataset. Here Nfull = numvariable*38523.
    dt : float
        The timestep size

    Returns
    -------
    xdot : (Nfull, n_samples-4) ndarray
        Time derivative of data using 4th order 5 point stecil approximation.
        See https://en.wikipedia.org/wiki/Five-point_stencil. No derivative is
        computed for first 2 and last 2 timesteps due to the stencil.
    """

    xdot = (data[:,0:-4] - 8*data[:,1:-3] + 8*data[:,3:-1] - data[:,4:])/(12*dt)
    return xdot

def project_data(data,
                 projected_data_folder,
                 projected_xdot_folder,
                 Vr,
                 dt,
                 r_vals=-1):
    """
    Compute xdot and project both data and xdot onto the POD basis.

    Projected data is saved as "<projected_data_folder>/data_reduced_%d.h5"
    Projected xdot data is saved as "<projected_xdot_folder>/xdot_reduced_%d.h5"

    Parameters
    ----------
    data : (n_variables, n_samples) ndarray
        Data to be projected and used to compute, project xdot.

    projected_data_folder : string
        Folder to save projected data to.

    projected_xdot_folder : string
        Folder to save projected xdot data to.

    Vr: (n_variables, r) ndarray
        The POD basis.

    dt: float
        Timestep size between data (for computing xdot)

    r_vals: list of ints <= Vr.shape[1] (the number of columns in Vr).
        Basis sizes to compute projection for.
    """

    if data.shape[1] < 2:
        raise ValueError("At least two snapshots required for x' computation")

    os.makedirs(os.path.dirname(projected_data_folder), exist_ok=True)
    os.makedirs(os.path.dirname(projected_xdot_folder), exist_ok=True)

    if r_vals == -1:
        r_vals = [Vr.shape[1]]

    # full dimension
    N = data.shape[0]

    # number of timesteps
    numtimesteps = data.shape[1]

    remainder = numtimesteps%1000

    chunck = numtimesteps // 1000 # for efficient memory chunking

    # if more than 1000 snapshots are passed, the projection is done 1000 snapshots at a time.
    # the remainder tells me if it

    for r in r_vals:
        print(" r = ", r)
        VrT = Vr[:,:r].T

        data_reduced = np.zeros((r,numtimesteps))
        xdot_reduced = np.zeros((r,numtimesteps))

        # forward difference for first 2 timesteps and project
        xdot_reduced[:,0] = VrT @ ((data[:,1] - data[:,0])/dt)
        xdot_reduced[:,1] = VrT @ ((data[:,2] - data[:,1])/dt)

        # project first two snapshots
        data_reduced[:,:2] = VrT @ data[:,:2]

        if (numtimesteps > 2):
            if numtimesteps == 3:
                # low order derivative
                xdot_reduced[:,2] = VrT @ ((data[:,2] - data[:,1])/dt)
                data_reduced[:,2] = VrT @ data[:,2]

            elif numtimesteps == 4:
                # low order derivate for last two
                xdot_reduced[:,2] = VrT @ ((data[:,2] - data[:,1])/dt)
                data_reduced[:,2] = VrT @ data[:,2]

                xdot_reduced[:,3] = VrT @ ((data[:,3] - data[:,2])/dt)
                data_reduced[:,3] = VrT @ data[:,3]

            else:
                if chunck == 0:
                    # less than 1000
                    print("Less than 1000")
                    xdot_reduced[:,2:-2] = VrT @ compute_xdot(data,dt)
                    data_reduced[:,:] = VrT @ data

                    # backward difference for last two
                    xdot_reduced[:,-2] = VrT @ ((data[:,-2] - data[:,-3])/dt)
                    xdot_reduced[:,-1] = VrT @ ((data[:,-1] - data[:,-2])/dt)

                else:
                    # more than 1000
                    if remainder != 0:
                        print("Not a multiple of 1000")
                        # not a multiple of 1000

                        # first 1000 - excluding the first 2
                        current_snap = data[:,:1002]
                        data_reduced[:,2:1000] = VrT @ current_snap[:,2:-2]
                        xdot_reduced[:,2:1000] = VrT @ compute_xdot(current_snap,dt)

                        for ii in range(1,chunck):
                            current_snap = data[:,(ii*1000)-2 : (ii+1)*1000 + 2] # grab 1004 snapshots because xdot chops off first 2 and last 2
                            data_reduced[:,ii*1000:(ii+1)*1000] = VrT @ current_snap[:,2:-2]
                            xdot_reduced[:,ii*1000:(ii+1)*1000] = VrT @ compute_xdot(current_snap,dt)

                        if remainder == 1:
                            # just one off (1001,2001,...)
                            xdot_reduced[:,-1] = VrT @ ((data[:,-1] - data[:,-2])/dt)
                            data_reduced[:,-1] = VrT @ data[:,-1]

                        elif remainder == 2:
                            # two off (1002,2002,...)
                            xdot_reduced[:,-2] = VrT @ ((data[:,-2] - data[:,-3])/dt)
                            xdot_reduced[:,-1] = VrT @ ((data[:,-1] - data[:,-2])/dt)
                            data_reduced[:,-2] = VrT @ data[:,-2]
                            data_reduced[:,-1] = VrT @ data[:,-1]

                        else:
                            # more than 2 we can use higher order xdot
                            xdot_reduced[:,chunck*1000:-2] = VrT @ compute_xdot(data[:,1000*chunck-2:],dt)
                            # and project data
                            data_reduced[:,chunck*1000:] = VrT @ data[:,1000*chunck:]

                            # low order for last two
                            xdot_reduced[:,-2] = VrT @ ((data[:,-2] - data[:,-3])/dt)
                            xdot_reduced[:,-1] = VrT @ ((data[:,-1] - data[:,-2])/dt)

                    else:
                        print(" A multiple of 1000")
                        # a multiple of 1000 snapshots

                        # first 1000 - excluding the first 2
                        current_snap = data[:,:1002]
                        data_reduced[:,2:1000] = VrT @ current_snap[:,2:-2]
                        xdot_reduced[:,2:1000] = VrT @ compute_xdot(current_snap,dt)

                        # compute high order derivatives 1000 at a time
                        for ii in range(1,chunck-1):
                            current_snap = data[:,(ii*1000)-2 : (ii+1)*1000 + 2] # grab 1004 snapshots because xdot chops off first 2 and last 2
                            data_reduced[:,ii*1000:(ii+1)*1000] = VrT @ current_snap[:,2:-2]
                            xdot_reduced[:,ii*1000:(ii+1)*1000] = VrT @ compute_xdot(current_snap,dt)

                        # deal with last 1000
                        current_snap = data[:,numtimesteps-1000-2:numtimesteps]
                        data_reduced[:,-1000:numtimesteps] = VrT @ current_snap[:,-1000:]

                        # high order xdot for last 998
                        xdot_reduced[:,numtimesteps-1000:-2] = VrT @ compute_xdot(current_snap,dt)

                        # first order xdot for last two
                        xdot_reduced[:,-2] = VrT @ ((data[:,-2] - data[:,-3])/dt)
                        xdot_reduced[:,-1] = VrT @ ((data[:,-1] - data[:,-2])/dt)

        print("Saving..")
        fname1 = "%sdata_reduced_%d.h5" %(projected_data_folder,r)
        savexf = h5py.File(fname1,'w')
        savexf.create_dataset('data',data = data_reduced)
        savexf.close()

        fname2 = "%sxdot_reduced_%d.h5" %(projected_xdot_folder,r)
        savexdotf = h5py.File(fname2,'w')
        savexdotf.create_dataset('xdot',data = xdot_reduced)
        savexdotf.close()

# Some error evaluators -------------------------------------------------------
def mape(truth, pred):
    """Mean absolute prediction error."""
    mask = truth > 1e-12
    return (((np.abs(truth-pred)/truth)[mask]).sum()+pred[~mask].sum())/len(truth)

def rpe(truth,pred):
    """Relative prediction error."""
    mask = abs(truth) > 1e-10
    rpe_mat = np.zeros(mask.shape)
    rpe_mat[mask] = (np.abs(truth-pred)/abs(truth))[mask]
    rpe_mat[~mask] = abs(pred[~mask])
    return rpe_mat

if __name__ == '__main__':

    # make command line arguments for trainsize and whether to compute svd
    trainsize = input("How many snapshots?")
    trainsize = int(trainsize)
    comp_svd = input("Compute SVD? (True or False -- Type False if it exists in a file 'data/svd_nt%d')")
    # trainsize = 10000

    fulldatapath = "data/data_minmax.h5"
    hf = h5py.File(fulldatapath,'r')
    data = hf['data'][:,:]
    hf.close()
    if comp_svd:
        print("Computing svd...")
        V,_ = compute_randomized_svd(data[:,:trainsize],"data/svd_nt%d.h5" %trainsize)

    else:
        print("loading svd...")
        hfs = h5py.File("data/svd_nt%d.h5" %trainsize, 'r')
        V = hfs['Und'][:,:]

    print("Computing xdot and reducing data...")
    project_data(data[:,:trainsize],
                 f"data/data_reduced_minmax_nt{trainsize}/",
                 f"data/data_reduced_minmax_nt{trainsize}/",
                 V,
                 1e-7,
                 [5,10,15])


from utils.gpu_utils import config_gpu

import tensorflow as tf

from pgn.batcher import batcher
from pgn.pgn_model import PGN
from pgn.train_helper import train_model
from utils.params_utils import get_params
from data_process.wv_loader import Vocab

def train(params):
    # GPU资源配置
    config_gpu(use_cpu=False, gpu_memory=params['gpu_memory'])
    # 读取vocab训练
    print("Building the model ...")
    vocab = Vocab(params["vocab_path"], params["max_vocab_size"])
    params['vocab_size'] = vocab.count

    # 构建模型
    print("Building the model ...")
    # model = Seq2Seq(params)
    model = PGN(params)

    print("Creating the batcher ...")
    dataset = batcher(vocab, params)
    # print('dataset is ', dataset)

    # 获取保存管理者
    print("Creating the checkpoint manager")
    checkpoint = tf.train.Checkpoint(PGN=model)
    checkpoint_manager = tf.train.CheckpointManager(checkpoint, params['checkpoint_dir'], max_to_keep=5)
    checkpoint.restore(checkpoint_manager.latest_checkpoint)
    if checkpoint_manager.latest_checkpoint:
        print("Restored from {}".format(checkpoint_manager.latest_checkpoint))
    else:
        print("Initializing from scratch.")
    # 训练模型
    print("Starting the training ...")
    train_model(model, dataset, params, checkpoint_manager)

if __name__ == '__main__':
    # 获得参数m
    params = get_params()
    # 训练模型
    train(params)


#http://symfoware.blog68.fc2.com/blog-entry-885.html
#https://docs.python.jp/3/library/http.server.html
import http.server
#from BaseHTTPServer import BaseHTTPRequestHandler
import cgi
class PostHandler(http.server.BaseHTTPRequestHandler):

    def do_POST(self):
        # POST されたフォームデータを解析する
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST',
                     'CONTENT_TYPE':self.headers['Content-Type'],
                     })
        # レスポンス開始
        self.send_response(200)
        self.end_headers()
        # フォームに POST されたデータを表示する
        for field in form.keys():
            field_item = form[field]
            print('%s=%s' % (field, form[field].value))

if __name__ == '__main__':
#    from BaseHTTPServer import HTTPServer
    server = http.server.HTTPServer(('localhost', 8080), PostHandler)
    server.serve_forever()


from django.shortcuts import render, get_object_or_404
from .models import Post
from .forms import CommentForm

def read_post_view(request, post_id):
    post = get_object_or_404(Post, id=post_id)

    form = CommentForm(request.POST or None)
    if request.method == 'POST' and form.is_valid():
        comment = form.save(commit=False)
        comment.post = post
        comment.author = request.user if request.user.is_authenticated() else None
        comment.save()

    return render(request, 'posts/read.html', {'post': post, 'form': form})


from rest_framework import viewsets
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.response import Response

from app.models import Deck, User, Card, Game, CardValue, Rule, CardEffect
from app.serializers import DeckSerializer, UserSerializer, CardSerializer, \
    GameSerializer, CardValueSerializer, RuleSerializer, CardEffectSerializer

class CustomObtainAuthToken(ObtainAuthToken):
    def post(self, request, *args, **kwargs):
        response = super(CustomObtainAuthToken, self) \
            .post(request, *args, **kwargs)

        token = Token.objects.get(key=response.data['token'])

        return Response({
            'token': token.key,
            'user': UserSerializer(token.user).data
        })

class DeckViewSet(viewsets.ModelViewSet):
    queryset = Deck.objects.all()
    serializer_class = DeckSerializer
    filter_fields = [
        'id',
        'name',
        'cards',
        'user',
    ]

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_fields = [
        'id',
        'username',
    ]

class CardViewSet(viewsets.ModelViewSet):
    queryset = Card.objects.all()
    serializer_class = CardSerializer
    filter_fields = [
        'id',
        'name',
        'cost',
        'type',
        'health',
        'strength',
        'effect',
    ]

class GameViewSet(viewsets.ModelViewSet):
    queryset = Game.objects.all()
    serializer_class = GameSerializer
    filter_fields = [
        'id',
        'owner',
        'owner_deck',
        'opponent',
        'opponent_deck'
    ]

class CardEffectViewSet(viewsets.ModelViewSet):
    queryset = CardEffect.objects.all()
    serializer_class = CardEffectSerializer
    filter_fields = [
        'id',
        'name',
        'description',
        'player_affected',
        'type',
        'quantity',
    ]

class CardValueViewSet(viewsets.ModelViewSet):
    queryset = CardValue.objects.all()
    serializer_class = CardValueSerializer
    filter_fields = [
        'id',
        'user',
        'card',
        'health',
        'strength',
    ]

class RuleViewSet(viewsets.ModelViewSet):
    queryset = Rule.objects.all()
    serializer_class = RuleSerializer
    filter_fields = [
        'id',
        'name',
        'players_health',
        'cards_in_deck',
        'cards_to_begin',
        'cards_to_draw',
    ]


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('mess', '0003_auto_20160121_1748'),
    ]

    operations = [
        migrations.CreateModel(
            name='MessDate',
            fields=[
                ('id', models.AutoField(primary_key=True, serialize=False, auto_created=True, verbose_name='ID')),
                ('mess_start', models.DateField()),
                ('mess_end', models.DateField()),
                ('display_option', models.IntegerField(choices=[(1, 'Yes'), (0, 'No')])),
                ('deadline', models.DateField()),
            ],
        ),
    ]


import numpy as np
import cv2
from skimage import exposure
from sklearn.utils import shuffle as shuffle_data

class BatchIterator(object):
    def __init__(self, batch_size, shuffle):
        self.batch_size = batch_size
        self.shuffle = shuffle

    def __call__(self, X, y=None):
        n_obs = X.shape[0]

        if y is not None:
            assert n_obs == y.shape[0], 'Different number of labels and images.'
            if self.shuffle: X, y = shuffle_data(X, y)

            for batch in range(0, n_obs, self.batch_size):
                batch_x = X[batch:min(batch + self.batch_size, n_obs), ...]
                batch_y = y[batch:min(batch + self.batch_size, n_obs), ...]
                yield batch_x, batch_y
        else:
            for batch in range(0, n_obs, self.batch_size):
                yield X[batch:min(batch + self.batch_size, n_obs), ...]

def preprocess_data(X, y=None):
    # Convert to grayscale, e.g. single Y channel
    X = 0.299 * X[:, :, :, 0] + 0.587 * X[:, :, :, 1] + 0.114 * X[:, :, :, 2]
    # Scale features to be in [0, 1]
    X = (X / 255.)

    # Apply localized histogram localization
    for i in range(X.shape[0]):
        X[i] = exposure.equalize_adapthist(X[i])

    if y is not None:
        # Convert to one-hot encoding. Convert back with `y = y.nonzero()[1]`
        y = np.eye(43)[y]
        # Shuffle the data
        X, y = shuffle_data(X, y)

    # Add a single grayscale channel
    X = X.reshape(X.shape + (1,))
    return X, y

def _transform_image(image, angle, shear, translation):
    rows, cols, color_channels = image.shape
    rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)

    trans_x = translation * np.random.uniform() - translation / 2
    trans_y = translation * np.random.uniform() - translation / 2
    translation_matrix = np.array([[1, 0, trans_x], [0, 1, trans_y]]).astype('float32')

    pts1 = np.array([[5, 5], [20, 5], [5, 20]]).astype('float32')

    pt1 = 5 + shear * np.random.uniform() - shear / 2
    pt2 = 20 + shear * np.random.uniform() - shear / 2

    pts2 = np.float32([[pt1, 5], [pt2, pt1], [5, pt2]])

    shear_matrix = cv2.getAffineTransform(pts1, pts2)

    image = cv2.warpAffine(image, rotation_matrix, (cols, rows))
    image = cv2.warpAffine(image, translation_matrix, (cols, rows))
    image = cv2.warpAffine(image, shear_matrix, (cols, rows))

    return image

def _expand_class(images, n_obs):
    images_to_create = n_obs - images.shape[0]
    transformed_images = [_transform_image(images[i, ...], 20, 10, 5)
                          for i in np.random.randint(low=0, high=images.shape[0], size=images_to_create).tolist()]

    transformed_images = np.array(transformed_images)
    transformed_images = transformed_images[..., np.newaxis]
    expanded_class = np.concatenate((images, transformed_images), axis=0)
    return expanded_class, expanded_class.shape[0]

def dense_labels(one_hot):
    return one_hot.nonzero()[1]

def split_and_expand(X, y, class_size=(20000, 5000)):
    segregated_data = []

    for class_ in range(43):
        segregated_class = X[dense_labels(y) == class_, ...]
        segregated_data.append(segregated_class)

    X_train, X_val = None, None
    y_train, y_val = None, None

    for class_, images in enumerate(segregated_data):
        if X_train is None:
            X_train, obs = _expand_class(images, class_size[0])
            y_train = np.full(obs, class_, dtype='int64')
            X_val, obs = _expand_class(images, class_size[1])
            y_val = np.full(obs, class_, dtype='int64')
        else:
            tmp, obs = _expand_class(images, class_size[0])
            X_train = np.concatenate((X_train, tmp), axis=0)
            y_train = np.concatenate((y_train, np.full(obs, class_, dtype='int64')), axis=0)

            tmp, obs = _expand_class(images, class_size[1])
            X_train = np.concatenate((X_train, tmp), axis=0)
            y_train = np.concatenate((y_train, np.full(obs, class_, dtype='int64')), axis=0)
    return X_train, y_train, X_val, y_val


# -*- coding: utf-8 -*-
import scrapy
import json
from cp_plan.items import CpPlanItem, Wait_Item
from scrapy.http import Request
import time

class CpPlansSpider(scrapy.Spider):
    name = 'ffc_all'
    allowed_domains = ['http://56070.la/']
    start_urls = ['http://56070.la/json/ffc.json', 'http://56070.la/json/ffc_h2zx.json',
                  'http://56070.la/json/ffc_h3zx.json', 'http://56070.la/json/ffc_h3z6.json']
    sign_num = 4418

    def parse(self, response):
        html = json.loads(response.body)
        # 最新一条等开计划信息
        endlist = html.get('EndList')
        item = CpPlanItem()
        item['title'] = html.get('NewGame')['WaitGame']
        if response.url == 'http://56070.la/json/ffc.json':
            item['type'] = 51
        elif response.url == 'http://56070.la/json/ffc_h2zx.json':
            item['type'] = 52
        elif response.url == 'http://56070.la/json/ffc_h3zx.json':
            item['type'] = 53
        else:
            item['type'] = 54
        item['gameId'] = 5
        yield item
        # 其余的计划信息
        for each in endlist:
            item = CpPlanItem()
            # print(each)
            item['title'] = each['Ruestl']
            if response.url == 'http://56070.la/json/ffc.json':
                item['type'] = 51
            elif response.url == 'http://56070.la/json/ffc_h2zx.json':
                item['type'] = 52
            elif response.url == 'http://56070.la/json/ffc_h3zx.json':
                item['type'] = 53
            else:
                item['type'] = 54
            item['gameId'] = 5

            yield item
            # 爬取 即刻开奖信息
        item = Wait_Item()
        N1 = html.get('TopGame')['R1']
        N2 = html.get('TopGame')['R2']
        N3 = html.get('TopGame')['R3']
        N4 = html.get('TopGame')['R4']
        N5 = html.get('TopGame')['R5']
        item['num'] = '{0},{1},{2},{3},{4}'.format(N1, N2, N3, N4, N5)
        item['gamedate'] = html.get('TopGame')['gameid']
        item['gameId'] = 5

        yield item


import uuid

from django.db import models

class Location(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)

    organisation = models.ForeignKey('aliss.Organisation', related_name='locations',on_delete=models.CASCADE)

    name = models.CharField(max_length=100, blank=True)
    description = models.TextField(blank=True)

    street_address = models.CharField(max_length=100)
    locality = models.CharField(max_length=30)
    region = models.CharField(max_length=30, blank=True)
    state = models.CharField(max_length=30, blank=True)
    postal_code = models.CharField(max_length=10)
    country = models.CharField(max_length=2, default="GB")

    latitude = models.FloatField()
    longitude = models.FloatField()

    created_on = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(
        'aliss.ALISSUser',
        related_name='created_locations',
        null=True,
        on_delete=models.SET_NULL
    )
    updated_on = models.DateTimeField(auto_now=True)
    updated_by = models.ForeignKey(
        'aliss.ALISSUser',
        related_name='updated_locations',
        null=True,
        on_delete=models.SET_NULL
    )

    def is_edited_by(self, user):
        if user == None or user.pk == None:
            return False
        elif user.is_staff:
            return True
        elif self.organisation.claimed_by == None:
            return (user.is_editor or (self.organisation.created_by == user))
        else:
            return self.organisation.claimed_by == user

    def __str__(self):
        return self.formatted_address

    @property
    def formatted_address(self):
        return ", ".join(filter(None, [self.name, self.street_address, self.locality, self.postal_code, self.state]))


from bs4 import BeautifulSoup
import requests
import bs4
import re

ulist = []

#检查url地址
def check_link(url):
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = 'GBK'
        return r.text

    except:
        print('无法链接服务器！！！')

#爬取资源
def get_urls(rurl):
    global ulist
    soup = BeautifulSoup(rurl,'lxml')
    trs = soup.find_all('tbody')
    ulist = pattern.findall(str(trs))

def get_save_contents(content, rurl):
    soup = BeautifulSoup(rurl,'lxml')
    trs = soup.find_all('p')
    value0 = str(trs).replace('[
', '')
    value1 = value0.replace('
', '')
    value2 = value1.replace(' ', '')
    value = value2.replace('
]', '')
    content += value
    try:
        with open('十三步.txt','a') as f:
            f.write(content)
    except:
        pass

def main():
    content = ''
    rs = check_link(url)
    get_save_contents(content, rs)

url0 = 'http://www.kanunu8.com/book2/10731/'
url = url0 + 'index.html'
pattern = re.compile('(?<=href=").+?\.html')
rs = check_link(url)
get_urls(rs)

for chapterurl in ulist:
    url = url0 + chapterurl
main()


#!/usr/bin/env python

# -*- encoding: utf-8 -*-

'''
@Author  :   {lif54334}

@Software:   PyCharm

@File    :   patent.py

@Time    :   2019/3/18 15:36

@Desc    :

'''
import re

import pymysql

def read(path):
    with open(path,'r',encoding='utf8')as f:
        lines=f.readlines()
        nums=len(lines)
        print(nums)
        # print(re.search(r'(\d+)、(.*)', line))
        contents=list()
        for i in range(0,nums,4):
            content = []
            title=lines[i].strip('\n').split('、')[1]
            abs=lines[i+1].strip('\n').split('：')[1]
            time=lines[i+2].strip('\n').split('：')[1]
            person=lines[i+3].strip('\n').split('：')[1]
            content.append(title)
            content.append(abs[0:14000])
            content.append(time[0:4])
            content.append(person[0:100])
            contents.append(content)
    # print(contents)
    return (contents)

def creat(name):
    db = pymysql.connect("localhost", "root", "1234", "xian")
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    table_name = name
    sql = "show tables;"
    cursor.execute(sql)
    tables = [cursor.fetchall()]
    table_list = re.findall('(\'.*?\')',str(tables))
    table_list = [re.sub("'",'',each) for each in table_list]
    if table_name in table_list:
        print("已存在")        #存在返回1
    else:
        print("未存在")
        cursor.execute("CREATE TABLE %s(title varchar(2000),abs varchar (14000),time int(10),person varchar (100))" % table_name)

def insert(data,name):
    table_name=name
    values=[]
    for items in data:
        values.append((items[0],items[1],items[2],items[3]))
        # print(values)
    # print(values)
    print(len(values))
    db = pymysql.connect("localhost", "root", "1234", "xian")
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    cursor.executemany("INSERT INTO %s values(%%s,%%s,%%s,%%s)"%(table_name),values)
    db.commit()
    cursor.close()

if __name__ == '__main__':
    name='zl_light'
    path="light2.txt"
    list=read(path)
    creat(name)
    insert(list,name)


# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# import logging
#
# #日志输入到文件，此定义会把level定义的和高于warn级别的日志写入文件
# logging.basicConfig(filename='logtest.log',level=logging.INFO)
#
# ##只有高于等于warn基本的日志才打印到stdout上
# logging.debug("debug message")
# logging.info("info message")
# logging.warn("warn message")
# logging.error('error message')
# logging.critical('critical message')

import logging
import logging.handlers

LOG_FILE = 'tst.log'

handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'

formatter = logging.Formatter(fmt)   # 实例化formatter
handler.setFormatter(formatter)      # 为handler添加formatter

logger = logging.getLogger('tst')    # 获取名为tst的logger
logger.addHandler(handler)           # 为logger添加handler
logger.setLevel(logging.DEBUG)

logger.info('first info message')
logger.debug('first debug message')


import numpy as np
import torch as to

import pyrado
from pyrado.utils.normalizing import normalize

class UnitCubeProjector:
    """ Project to a unit qube $[0, 1]^d$ and back using explicit bounds """

    def __init__(self, bound_lo: [np.ndarray, to.Tensor], bound_up: [np.ndarray, to.Tensor]):
        """
        Constructor

        :param bound_lo: lower bound
        :param bound_up: upper bound
        """
        self.bound_lo = bound_lo
        self.bound_up = bound_up

    def project_to(self, x: [np.ndarray, to.Tensor]) -> [np.ndarray, to.Tensor]:
        """
        Normalize every dimension individually using the stored explicit bounds and the L_1 norm.

        :param x: input ndarray or Tensor
        :return: element of the unit cube
        """
        span = self.bound_up - self.bound_lo
        span[span == 0] = 1.  # avoid division by 0
        if isinstance(x, to.Tensor):
            return (x - self.bound_lo)/span
        elif isinstance(x, np.ndarray):
            return (x - self.bound_lo.numpy())/span.numpy()
        else:
            raise pyrado.TypeErr(given=x, expected_type=[np.ndarray, to.Tensor])

    def project_back(self, x: [np.ndarray, to.Tensor]) -> [np.ndarray, to.Tensor]:
        """
        Revert the previous normalization using the stored explicit bounds

        :param x: input ndarray or Tensor
        """
        span = self.bound_up - self.bound_lo
        if isinstance(x, to.Tensor):
            return span*x + self.bound_lo
        elif isinstance(x, np.ndarray):
            return span.numpy()*x + self.bound_lo.numpy()
        else:
            raise pyrado.TypeErr(given=x, expected_type=[np.ndarray, to.Tensor])

def cov(x: to.Tensor, data_along_rows: bool = False):
    """
    Compute the covariance matrix given data.

    .. note::
        Only real valued matrices are supported

    :param x: matrix containing multiple observations of multiple variables
    :param data_along_rows: if `True` the variables are stacked along the columns, else they are along the rows
    :return: covariance matrix given the data
    """
    if x.dim() > 2:
        raise ValueError('m has more than 2 dimensions')
    if x.dim() < 2:
        x = x.view(1, -1)
    if data_along_rows and x.size(0) != 1:
        # Transpose if necessary
        x = x.t()

    num_samples = x.size(1)
    if num_samples < 2:
        raise pyrado.ShapeErr(msg='Need at least 2 samples to compute the covariance!')

    x -= to.mean(x, dim=1, keepdim=True)
    return x.matmul(x.t()).squeeze()/(num_samples - 1)

def explained_var(y_mdl: [np.ndarray, to.Tensor], y_obs: [np.ndarray, to.Tensor]) -> (np.ndarray, to.Tensor):
    """
    Calculate proportion of the variance "explained" by the model (see coefficient of determination R^2)
    .. note:: R^2 = 0.49 implies that 49% of the variability of the dependent variable has been accounted for.

    :param y_mdl: predictions from the model
    :param y_obs: observed data (ground truth)
    :return: proportion of the explained variance (i.e. R^2 value)
    """
    if isinstance(y_mdl, np.ndarray) and isinstance(y_obs, np.ndarray):
        ss_total = np.sum(np.power(y_obs - np.mean(y_obs), 2))  # proportional to the variance of the data
        ss_res = np.sum(np.power(y_obs - y_mdl, 2))  # proportional to the residual variance
        return 1. - ss_res/ss_total  # R^2

    elif isinstance(y_mdl, to.Tensor) and isinstance(y_obs, to.Tensor):
        ss_total = to.sum(to.pow(y_obs - to.mean(y_obs), 2))
        ss_res = to.sum(to.pow(y_obs - y_mdl, 2))
        return to.tensor(1.) - ss_res/ss_total

    else:
        raise pyrado.TypeErr(given=y_mdl, expected_type=[np.ndarray, to.Tensor])

def logmeanexp(x: to.Tensor, dim: int = 0) -> to.Tensor:
    """
    Numerically stable way to compute $\log( 1/N \sum_{i=1}^N \exp(x) )$

    :param x: input tensor
    :param dim: dimension to torch.logsumexp along
    :return: $\log( 1/N \sum_{i=1}^N \exp(x) )$
    """
    return to.logsumexp(x, dim=dim) - to.log(to.tensor(x.shape[dim], dtype=to.get_default_dtype()))

def cosine_similarity(x: to.Tensor, y: to.Tensor) -> to.Tensor:
    r"""
    Compute the cosine similarity between two tensors $D_cos(x,y) = \frac{x^T y}{|x| \cdot |y|}$.

    :param x: input tensor
    :param y: input tensor
    :return: cosine similarity value
    """
    if not isinstance(x, to.Tensor):
        raise pyrado.TypeErr(given=x, expected_type=to.Tensor)
    if not isinstance(y, to.Tensor):
        raise pyrado.TypeErr(given=y, expected_type=to.Tensor)

    x_normed = normalize(x, order=2)
    y_normed = x_normed if y is x else normalize(y, order=2)
    return x_normed.dot(y_normed)

def clamp(inp: to.Tensor, lo: to.Tensor, up: to.Tensor) -> to.Tensor:
    """
    Clip the entries of a tensor by the entries of a lower bound and an upper bound tensor.

    :param inp: input tensor
    :param lo: lower bound for each entry
    :param lo: upper bound for each entry
    :return: clipped tensor
    """
    if not (lo < up).all():
        raise pyrado.ValueErr(msg='The lower bounds needs to be element-wise smaller than the upper bound.')
    return to.max(to.min(inp, up), lo)

def clamp_symm(inp: to.Tensor, up_lo: to.Tensor) -> to.Tensor:
    """
    Symmetrically clip the entries of a tensor by the entries of a tensor with only positive entries.
    One use case of this function is if you want to clip the parameter update by a ratio of the old parameters.

    :param inp: input tensor
    :param up_lo: upper and (negative of the) lower bound for each entry
    :return: clipped tensor
    """
    if not (up_lo > 0).all():
        raise pyrado.ValueErr(given=up_lo, g_constraint='0')
    return to.max(to.min(inp.clone(), up_lo), -up_lo)


'''
684: Redundant Connection
'''
from collections import defaultdict
# Graph question , detect a cycle in the graph first
class Solution(object):
    """docstring for Solution."""
    def __init__(self):
        super(Solution, self).__init__()

    def findRedundantConnection(self,edges):
        #(u,v) , u<v
        self.indices={}
        self.g = defaultdict(set)
        for i,x in enumerate(edges):
            self.g[x[0]].add(x[1])
            self.g[x[1]].add(x[0])
            self.indices[(x[0],x[1])] = self.indices[(x[1],x[0])] = i # where the edges occur in edges
        self.path = {x:0 for x in self.g}
        return self.dfs(list(self.g.keys())[0])

        # for x in edges[::-1]:
        #     if x in l2:
        #         return x

    def dfs(self, node):
        for w in self.g[node]:
            if self.path[node]==w:
                continue
            if self.path[w]!=0: #cycle is found
                self.path[w]=node #complete the cycle
                return self.result(w)

            self.path[w]=node
            temp=self.dfs(w)
            if temp:
                return temp
            self.path[w]=0

    def result(self, w):
        prev, curr = w, self.path[w]
        M = self.indices[(curr, prev)] # Find all the cycle edges and get max M
        while curr!=w:
            prev, curr = curr, self.path[curr]
            M=max(M,self.indices[(curr, prev)])
        return edges[M]

obj = Solution()
edges = [[1,2],[1,3],[2,3]]
print(obj.findRedundantConnection(edges))


import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import rc
rc('text', usetex=True)
import pickle

label_map = {"naive" : "naive", \
			"glasso" : "glasso", \
			"scio" : "scio", \
			"clime" : "clime", \
			"OR" : "FMPL", \
			"random": "random"}

color_map = {"naive" : "b", \
			"glasso" : "r", \
			"scio" : "m", \
			"clime" : "k", \
			"OR" : "c", \
			"random" : "g"}

def get_data(method, dir_name):
	f = open(dir_name + '/' + method, 'r')
	data = pickle.load(f)
	f.close()
	return data

def get_data_param(method, dir_name):
	return get_data(method, dir_name)[1]

def save_data(data, method, dir_name):
	f = open(dir_name + '/' + method, 'w')
	pickle.dump(data, f)
	f.close()

def save_figure(fig, name):
	figure_name = "../figures/" + name + ".pdf"
	pd = PdfPages(figure_name)
	pd.savefig(fig)
	pd.close()

def make_figuree(data_set, xs, ys, x_variable_name = None, y_variable_name = None, figure_title = "test_figure", dim = 144):
	if (x_variable_name == None):
		x_variable_name = xs
	if (y_variable_name == None):
		y_variable_name = ys
	fig = plt.figure()
	plt.title(figure_title)
	fontsize = 20
	plt.xlabel(x_variable_name, fontsize = fontsize)
	plt.ylabel(y_variable_name, fontsize = fontsize)
	outdims = []
	indims = []
	if (y_variable_name == "hamming distance"):
		ax = plt.subplot(111)
		if (xs == "outdim"):
			plt.ylabel("hamming distance $/ d_2$")
			for method in data_set:
				outdims = data_set[method][xs]
		if (xs == "indim"):
			plt.ylabel("hamming distance")
			for method in data_set:
				indims = data_set[method][xs]
	for method in data_set:
		yy = data_set[method][ys]
		if (xs == "outdim"):
			yy = [1.0*yyy/dim for yyy, dim in zip(yy, outdims)]
		m = len(data_set[method][xs])
		x = range(m)
		curve_label = label_map[method]
		curve_color = color_map[method]
		plt.plot(x, yy, label = curve_label, color=curve_color)
		plt.xticks(x, data_set[method][xs])
	plt.legend()
	if ((y_variable_name == "hamming distance") and (xs == "noise_stds")):
		plt.gca().set_ylim([0, 35])
	return fig


from flask import render_template, redirect, url_for, flash, Response
from app import app, db
import forms
from models import DATABASE_URL
# from datetime import datetime

import io
import os
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import seaborn as sns
sns.set()
@app.route('/')

@app.route("/Good_Standing")
def Good_Standing():
    return render_template("Good_Standing.html")

@app.route("/about_us")
def about_us():
    return render_template("about_us.html")

@app.route("/hours")
def hours():
    return render_template("hours.html")

@app.route("/View_Hours")
def View_hours():
    return render_template("View_Hours.html")

@app.route("/User_View")
def User_View():
    return render_template("User_View.html")

@app.route('/sign_up', methods=['GET','POST'])
def index():
    form = forms.VolunteerForm()

    if form.validate_on_submit():
        # update session object
        print('Validated')
        student_name = form.student_name.data
        student_ID = form.student_ID.data
        hours = form.hours.data
        event = form.event.data
        Date = form.Date.data

        v = DATABASE_URL(name=student_name, stud_ID=student_ID, hours=hours, event=event, Date=Date)

        db.session.add(v)
        db.session.commit()
        flash('Data Submitted')
        form = forms.VolunteerForm()
        database_url = DATABASE_URL.query.filter_by(stud_ID=form.student_ID.data)
        return render_template('volunteer_found.html', DATABASE_URL=database_url)

    return render_template('sign_up.html', form=form)

@app.route('/specific_volunteers', methods=['GET', 'POST'])
def indx():
    form = forms.SpecificUser()

    if form.validate_on_submit():
        # update session object
        print('Validated')

        return redirect(url_for('volunteer_found'))

    return render_template('specific_volunteers.html', forms=form)

@app.route('/volunteer_found', methods=['GET', 'POST'])
def volunteer_found():
        form = forms.SpecificUser()
        if (form.student_ID.data=='84362345'):
            stud_eventregist = DATABASE_URL.query.all()
        else :
            stud_eventregist = DATABASE_URL.query.filter_by(stud_ID=form.student_ID.data)

        total_hours = 0
        event, hours = [], []

        for data in stud_eventregist:
            # add up all hours volunteered
            total_hours += data.hours

            # save to list elements
            event.append(data.event)
            hours.append(data.hours)

        # convert to pandas dataframe
        df = pd.DataFrame({"Event": event, "Hours": hours})
        print(df)
        #
        gb = df.groupby('Event').sum().reset_index()
        print(gb)

        # remove image if it exits
        if os.path.exists("plot.png"):
            os.remove("plot.png")

        fig = plt.figure()
        axis = fig.add_subplot(1,1,1)

        # documentation
        # https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.bar.html
        axis.bar(gb.Event, gb.Hours, color='blue')
        axis.set_title("Total Hours Volunteered", fontsize=16)
        axis.set_ylabel("Hours", fontsize=14)
        axis.set_xlabel("Event", fontsize=14)

        plt.savefig('plot.png')

        return render_template('volunteer_found.html', studregist=stud_eventregist, url='plot.png', hours=total_hours)

# @app.route('/volunteer_found', methods=['GET', 'POST'])
# def volunteer_fnd():
@app.route('/plot.png')
def plot_png():
    # grab data
    form = forms.SpecificUser()
    eventregist = DATABASE_URL.query.filter_by(stud_ID=form.student_ID.data)

    total_hours = 0
    event, hours = [], []

    for data in eventregist:
        # add up all hours volunteered
        total_hours += data.hours

        # save to list elements
        event.append(data.event)
        hours.append(data.hours)

    # convert to pandas dataframe
    df = pd.DataFrame({"Event": event, "Hours": hours})

    #
    gb = df.groupby('Event').sum().reset_index()
    print(gb)

    # remove image if it exits
    if os.path.exists("plot.png"):
        os.remove("plot.png")

    fig = plt.figure()
    axis = fig.add_subplot(1,1,1)

    # documentation
    # https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.bar.html
    axis.bar(gb.Event, gb.Hours, color='blue')
    axis.set_title("Total Hours Volunteered", fontsize=16)
    axis.set_ylabel("Hours", fontsize=14)
    axis.set_xlabel("Event", fontsize=14)

    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)

    return Response(output.getvalue(), mimetype='image/png')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from catalog_database_setup import Base, Category, Item, User
from random import randint

engine = create_engine('sqlite:///catalog.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()

user = User(email = "chris22931@gmail.com")
session.add(user)
category = Category(name="Apps & Games")
session.add(category)
item = Item(
    name="Tetris",
    description="Description of Tetris",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Minecraft",
    description="Description of Minecraft",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Hungry Shark Evolution",
    description="Description of Hungry Shark Evolution",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Portal",
    description="Description of Portal",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Dota 2",
    description="Description of Dota 2",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="BioShock",
    description="Description of BioShock",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Starcraft",
    description="Description of Starcraft",
    category=category,
    owner=user)
session.add(item)

category = Category(name="Beauty")
session.add(category)
item = Item(
    name="Peel Gel",
    description="Description of Peel Gel",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Sunscreen",
    description="Description of Sunscreen",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Lip Stick",
    description="Description of Lip stick",
    category=category,
    owner=user)
session.add(item)

category = Category(name="Books")
session.add(category)
item = Item(
    name="Chains of Command",
    description="The assault on Earth was thwarted by the destruction of the "
    "aliens seed ship, but with Mars still under Lanky control, survivors work "
    "frantically to rebuild fighting capacity and shore up planetary defenses. "
    "Platoon sergeant Andrew Grayson must crash-course train new volunteers "
    "all while dulling his searing memories of battle with alcohol and meds.",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Wayward",
    description="Welcome to Wayward Pines, population 461. Nestled amid "
    "picture-perfect mountains, the idyllic town is a modern-day Eden... "
    "except for the electrified fence and razor wire, snipers scoping "
    "everything, and the relentless surveillance tracking each word "
    "and gesture.",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="The Hidden Relic",
    description="With the fate of their homeland still in jeopardy, siblings "
    "Ella and Miro must face the Primates evil as he discovers a new "
    "technique: a method to extract essence from human blood.",
    category=category,
    owner=user)
session.add(item)

category = Category(name="Sports & Outdoors")
session.add(category)
item = Item(
    name="Ball",
    description="Not just a normal ball!",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Snowboard",
    description="Really cool Snowboard.",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Skateboard",
    description="Skateboard from the Tony Hawk Edition.",
    category=category,
    owner=user)
session.add(item)

category = Category(name="DVD & Blu-ray")
session.add(category)
item = Item(
    name="The Shawshank Redemption",
    description="Two imprisoned men bond over a number of years, "
    "finding solace and eventual redemption through acts of common decency.",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="The Godfather",
    description="The aging patriarch of an organized crime dynasty transfers "
    "control of his clandestine empire to his reluctant son.",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="The Dark Knight",
    description="When the menace known as the Joker emerges from his "
    "mysterious past, he wreaks havoc and chaos on the people of Gotham, "
    "the Dark Knight must accept one of the greatest psychological "
    "and physical tests of his ability to fight injustice..",
    category=category,
    owner=user)
session.add(item)

category = Category(name="Watches")
session.add(category)
item = Item(
    name="Ice Watch",
    description="Description of Ice Watch",
    category=category,
    owner=user)
session.add(item)
item = Item(
    name="Casio Digital Watch",
    description="Description of Casio Digital Watch",
    category=category,
    owner=user)
session.add(item)

session.commit()


#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

import os
import random
from collections import namedtuple
from typing import Tuple, List, Dict

import numpy as np
from spacy.gold import GoldParse
from spacy.lang.fr import French
from spacy.scorer import Scorer
from spacy.util import minibatch, compounding
from tqdm import tqdm

from ner.model_factory import get_empty_model, entity_types
from resources.config_provider import get_config_default

Offset = namedtuple('Offset', ['start', 'end', 'type'])

def parse_offsets(text: str) -> Offset:
    """
    Convert to the right offset format
    :param text: original line
    :return: a tuple containing the offset
    """
    item = text.split(' ')
    return Offset(int(item[0]), int(item[1]), item[2])

def extract_offsets(text: str, offsets: List[Offset]) -> List[str]:
    """
    Extract a span from a line according to provided offsets
    :param text: original text
    :param offsets: list of tuples containing the positional information and a class of entity
    :return: a list of tokens
    """
    return [text[offset.start:offset.end] for offset in offsets]

def convert_offset_to_words(line: str, offsets: List[Offset], selected_type: str) -> List[str]:
    """
    Convert offsets to word tokens
    :param line: original string
    :param offsets: list of offsets
    :param selected_type: the offset type to keep for this extraction
    :return: a list of words
    """
    offset = [item for item in offsets if item.type == selected_type]
    tokens = extract_offsets(text=line, offsets=offset)
    words = [token.split(" ") for token in tokens]
    words = [word for sublist in words for word in sublist]
    return words

def spacy_evaluate(model, samples: List[Tuple[str, List[Offset]]]) -> Dict[str, float]:
    """
    Compute entity global scores according to Spacy
    :param model: Spacy NER model
    :param samples: list of tuples containgin the filename, the text, and the offsets
    :return: a dict of scores
    """
    scorer = Scorer()
    for input_, annot in samples:
        doc_gold_text = model.make_doc(input_)
        gold = GoldParse(doc_gold_text, entities=annot)
        pred_value = model(input_)
        scorer.score(pred_value, gold)
    return scorer.scores

def eval(model: French, classes: List[str], content: List[Tuple[str, List[Offset]]]):
    scores = {}
    with tqdm(total=len(content), unit=" samples", desc="compute score") as progress_bar:
        for current_line, gold_offsets in content:
            spacy_offsets: List[Offset] = [Offset(ent.start_char, ent.end_char, ent.label_) for ent in
                                           model(current_line).ents]
            for entity_type in classes:
                # predictions from Spacy
                pred_words = convert_offset_to_words(line=current_line,
                                                     offsets=spacy_offsets,
                                                     selected_type=entity_type)
                # manual annotations
                gold_words = convert_offset_to_words(line=current_line,
                                                     offsets=gold_offsets,
                                                     selected_type=entity_type)
                if len(gold_words) > 0:
                    score = len(set(gold_words).intersection(set(pred_words))) / len(set(gold_words))
                    current_scores: List[float] = scores.get(entity_type, [])
                    current_scores.append(score)
                    scores[entity_type] = current_scores
                    # if score != 1:
                    #     print(token_type, ":", pred_tokens, "VS", gold_tokens)
            progress_bar.update()

    for entity_type in classes:
        if (entity_type in scores) and (len(scores[entity_type]) > 0):
            print(entity_type, np.round(100 * np.mean(scores[entity_type]), 2), "%\tnb samples",
                  len(scores[entity_type]))

def load_content(paths) -> List[Tuple[str, List[Offset]]]:
    results: List[Tuple[str, List[Offset]]] = []
    for file_path in paths:
        file_path = os.path.join(eval_dataset_path, file_path)
        if file_path.endswith('.txt'):
            with open(file_path, 'r') as f:
                content_case = [item.strip() for item in f.readlines()]
            path_annotations = file_path.replace('.txt', '.ent')
            with open(path_annotations, 'r') as f:
                annotations = [item.strip() for item in f.readlines()]

            assert len(content_case) > 0
            assert len(content_case) == len(annotations)

            for line_case, line_annotations in zip(content_case, annotations):
                gold_offsets = [parse_offsets(item) for item in line_annotations.split(',') if item != ""]
                if len(gold_offsets) > 0:
                    results.append((line_case, gold_offsets))
    return results

config_training = get_config_default()
eval_dataset_path = config_training["eval_path"]
ner_model = get_empty_model(load_labels_for_training=False)
ner_model = ner_model.from_disk(config_training["model_dir_path"])

file_paths = os.listdir(eval_dataset_path)
content_to_rate: List[Tuple[str, List[Offset]]] = load_content(file_paths[:-20])
content_to_rate_test = load_content(file_paths[-20:])

eval(model=ner_model, classes=entity_types, content=content_to_rate_test)

# results = spacy_evaluate(ner_model, content_to_rate)
# print(results)

print("nb entities", sum([len(item) for item in content_to_rate]))

train_data = [(current_line, GoldParse(ner_model.make_doc(current_line), entities=gold_offsets)) for current_line, gold_offsets in content_to_rate]

with tqdm(total=30, unit=" epoch", desc="update model") as progress_bar:
    for itn in range(30):
        random.shuffle(train_data)
        losses = dict()
        batches = minibatch(train_data, size=compounding(4.0, 32.0, 1.001))
        for batch in batches:
            texts, annotations = zip(*batch)
            ner_model.update(
                texts,
                annotations,
                drop=0.5,
                losses=losses
            )
        progress_bar.update()

eval(model=ner_model, classes=entity_types, content=content_to_rate_test)


from json import dumps, loads

class Product:
	def __init__(self, name, price):
		self.name = name
		self.price = price
	def to_dict(self):
		return {"name": self.name, "price": self.price}

def add_Product(name, price):
	new_product = Product(name, price)
	products.append(new_product)

def save_products(products):
	product_save_list = []
	for product in products:
		product_save_list.append(product.to_dict())
	products_file = open("products.json", "w+")
	products_file.write(dumps(product_save_list))
	products_file.close()

def load_products():
	try :
		products_file = open("products.json", "r")
	except IOError:
		return []
	products_json = products_file.read()
	product_data = loads(products_json)
	products_file.close()

	products = []
	for product in product_data:
		products.append(Product(product['name'], product['price']))
	return products

def list_products(products):
	for product in products:
		print(product.name, " Rs.", product.price)

def total_products(products):
	total = 0
	for product in products:
		total += product.price
	print("Your total cost of all the products is: "+str(total))

products = load_products()

while True:
	print("Type add to add a product.")
	print("Type quit to quit the program.")
	print("Type list to see list of all Products")
	print("Type total to see total cost of all the Products")
	command = input("Type a command: ")

	if command == 'quit':
		save_products(products)
		break

	if command == 'add':
		product_name = input("Enter a name for your Product: ")
		try :
			product_price = float(input("Enter a price for your new Product in Rs: "))
		except ValueError:
			print("Enter a valid price")
			continue
		add_Product(product_name, product_price)

	if command == 'list':
		list_products(products)

	if command == 'total':
		total_products(products)


# import glob
import os
import re
import rarfile
import shutil
import glob
from javcore import common
from javcore import data
from javcore import db
from javcore import site
from send2trash import send2trash

class ImportRegisterBj:

    def __init__(self):

        # rarfile.DEFAULT_CHARSET = "windows-1252"
        # rarfile.UNRAR_TOOL = 'unrar'
        rarfile.UNRAR_TOOL = r'C:\\Program Files (x86)\\UnrarDLL\\Examples\\MASM\\unrar'
        # rarfile.UNRAR_TOOL = r'C:\\SHARE\\unrar.exe'
        self.movie_extension = '.*\.avi$|.*\.wmv$|.*\.mpg$|.*ts$|.*divx$|.*mp4$' \
                               '|.*asf$|.*mkv$|.*rm$|.*rmvb$|.*m4v$|.*3gp$|.*flv'

        self.register_path = "D:\DATA\Downloads\TEMP"
        if not os.path.exists(self.register_path):
            print('not exist path store_path [' + self.register_path + ']')
            exit(-1)

        self.store_path = "C:\mydata\\bj-jpeg"
        if not os.path.exists(self.store_path):
            print('not exist path store_path [' + self.store_path + ']')
            exit(-1)

        self.import_dao = db.import_dao.ImportDao()
        self.bj_dao = db.bj.BjDao()
        self.wiki = site.wiki.SougouWiki()

        self.is_check = True
        # self.is_check = False
        # self.target_max = 200
        # self.__set_files()

    def arrange_execute(self):

        bjs = self.bj_dao.get_all()

        # print('count bjs [{}]'.format(len(bjs)))
        err_list = []
        target_idx = 1
        bj_parser = common.BjParser()
        idx2 = 0
        for idx, bj in enumerate(bjs):

            is_err = False
            if not bj.isSelection == 1:
                continue

            bj.parse(bj_parser)
            bj.print()

            for thumbnail_info in bj.thumbnailInfoList:
                pathname = os.path.join(self.store_path, thumbnail_info.filename)
                if not os.path.isfile(pathname):
                    err_list.append('[' + str(bj.id) + '] JPEGが存在しない [' + pathname + ']')

            rar_pathname = os.path.join(self.register_path, bj.rar_filename)
            size = 0
            if not os.path.isfile(rar_pathname):
                err_list.append('[' + str(bj.id) + '] RARが存在しない [' + rar_pathname + ']')
                is_err = True
            else:
                size = os.path.getsize(rar_pathname)
                print('  OK ' + rar_pathname)

            base_pathname = os.path.join(self.register_path, bj.basename)

            if not os.path.exists(base_pathname):
                file_list = glob.glob(os.path.join(self.register_path, bj.basename + '*'))
                is_exist = False
                for file in file_list:
                    if re.search(self.movie_extension, file, re.IGNORECASE):
                        # err_list.append('[{}] DIRなしで動画が存在する [{}] {}'.format(bj.id, base_pathname, file_list))
                        is_exist = True
                        break

                if is_exist is False:
                    err_list.append('[' + str(bj.id) + '] DIRが存在しない [' + base_pathname + ']')
                    is_err = True
            else:
                print('  OK ' + base_pathname)

            if is_err:
                continue

            for thumbnail_info in bj.thumbnailInfoList:
                src_pathname = os.path.join(self.store_path, thumbnail_info.filename)
                dest_pathname = os.path.join(self.register_path, thumbnail_info.dest_filename)
                if not self.is_check:
                    shutil.copy2(src_pathname, dest_pathname)
                else:
                    print('  {} -> {}'.format(src_pathname, dest_pathname))

            import_data = data.ImportData()
            import_data.title = bj.title
            import_data.postDate = bj.postDate
            import_data.copy_text = bj.base_filename
            import_data.productNumber = bj.basename
            import_data.kind = 5
            import_data.actress = bj.actress
            import_data.isNameOnly = True
            import_data.url = bj.url
            import_data.rating = bj.rating
            import_data.tag = bj.tag
            import_data.maker = bj.postedIn
            import_data.size = size

            if not self.is_check:
                self.import_dao.export_import(import_data)
                self.bj_dao.update_is_selection(bj.id, 9)

            if target_idx > self.target_max:
                break

            target_idx = target_idx + 1

        for err in err_list:
            print(err)

    def __get_actress(self, title, posted_in):
        actress = ''
        if '201' in title and "KOREAN BJ" in title:
            match = re.search('[A-Z ]* 201', title)
            if match:
                replace_str = match.group().replace(' 201', '')
                actress = posted_in.replace(replace_str, '').strip()
                print('    ' + replace_str + '  ' + actress)
            else:
                print('    not match')

        return actress

if __name__ == '__main__':
    importRegister = ImportRegisterBj()
    importRegister.arrange_execute()


import pymongo
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans

'''
This program takes a while to run on larger datasets
But it's worth it!
'''

def getTweetText(tweet_collection):

    #initialising empty lists to later store information.
    user_names = []
    hashtags = []
    text = []
    alldata = tweet_collection.find()

    # extracting the username, hashtags and text from the data and adding them to the lists
    # defined above.
    for tweet in alldata:
        username_tweets = str(tweet['username']).lower()
        hashtags_tweets = str(tweet['hashtags']).lower()
        tweet_text = str(tweet['text']).lower()
        user_names.append(username_tweets)
        hashtags.append(hashtags_tweets)
        text.append(tweet_text)

    return user_names, hashtags, text

def top_results(vectorizer, group):
    k = 6
    centroids = group.cluster_centers_.argsort()[:, ::-1]
    text = vectorizer.get_feature_names()
    for c in range(k):
        print("CLUSTER %d:" % c)
        for i in centroids[c, :5]:
            print(' %s' % text[i])

def clustering(user_names, hashtags, texts):
    vectorizer = TfidfVectorizer(stop_words='english')
    k = 6

    vector_user_names = vectorizer.fit_transform(user_names)
    vector_hashtags = vectorizer.fit_transform(hashtags)
    vector_texts = vectorizer.fit_transform(texts)
    user_namesK = KMeans(n_clusters=k, init='k-means++', max_iter=100, n_init=1)
    user_namesK.fit(vector_user_names)
    hashtagsK = KMeans(n_clusters=k, init='k-means++', max_iter=100, n_init=1)
    hashtagsK.fit(vector_hashtags)
    textK = KMeans(n_clusters=k, init='k-means++', max_iter=100, n_init=1)
    textK.fit(vector_texts)

    return vectorizer, user_namesK, hashtagsK, textK

if __name__ == "__main__":

    # Setting up connection
    client = pymongo.MongoClient('localhost', 27017)
    db = client.WebScience
    tweet_collection = db.tweet_collection

    userNames, hashTags, Texts = getTweetText(tweet_collection)
    vector, user_names_K, hashtags_K, texts_K = clustering(userNames, hashTags, Texts)

    #Getting top results of usernames
    print("USERNAME CLUSTERS:")
    top_results(vector, user_names_K)

    #Getting top results of hashtags
    print("HASHTAG CLUSTERS:")
    top_results(vector, hashtags_K)

    #Getting top results of texts
    print("TEXT CLUSTERS:")
    top_results(vector, texts_K)


from baseentity import BaseEntity

from svrConfig import DB_G,RedisPool_G
from datetime import datetime,timedelta

import redis,json

class HourReport(BaseEntity):
    def __init__(self,hour):
        self.hour=hour
        BaseEntity.expire_time=3600*48
        BaseEntity.__init__(self,"daily_report:%s" % hour)

    def read_from_db(self):

        if datetime.today().hour>=self.hour:
            curtime=datetime.today().replace(hour=self.hour)
        else:
            timespan=timedelta(days=1)
            curtime=(datetime.today()-timespan).replace(hour=self.hour)

        today=curtime.strftime("%Y-%m-%d %H:00:00")

        timespan=timedelta(hours=1)
        today_pre=(curtime-timespan).strftime("%Y-%m-%d %H:00:00")

        #print "Today:%s,Today_END:%s" % (today,today_pre)

        hour_report={}
        hour_report["new_user"]=0
        hour_report["active_user"]=0
        hour_report["total_impression"]=0
        hour_report["total_impression_unique"]=0
        hour_report["total_click"]=0
        hour_report["total_click_unique"]=0

        sqlres=DB_G.query("select status,count(client_id) as num,count(distinct(client_id)) as unum from ad_action_log force index(ix_AL_CAS) where create_time>='%s' and create_time<'%s' group by status" %  (today_pre,today))

        for qres in sqlres:
            if qres['status']=="19":
                hour_report["total_impression"]=qres['num']
                hour_report["total_impression_unique"]=qres['unum']
            elif qres['status']=="20":
                hour_report["total_click"]=qres['num']
                hour_report["total_click_unique"]=qres['unum']

        sqlres=DB_G.query("select count(client_id) as num from sdk2.sdk_clients where create_time>='%s' and create_time<'%s'" %  (today_pre,today))
        qres=sqlres[0]
        hour_report["new_user"]=qres['num']

        sqlres=DB_G.query("select count(distinct(client_id)) as num from ad_pull_log where cur_day='%s'" % today_pre)
        qres=sqlres[0]
        hour_report["active_user"]=qres['num']

        return hour_report

class DailyReport:
    def get(self):
        r=redis.Redis(connection_pool=RedisPool_G)
        daily_report={}
        daily_report["new_user"]=0
        daily_report["active_user"]=0
        daily_report["total_impression"]=0
        daily_report["total_impression_unique"]=0
        daily_report["total_click"]=0
        daily_report["total_click_unique"]=0

        for hour_index in xrange(datetime.today().hour):
            hour_report=HourReport(hour_index)
            daily_report["new_user"]+=hour_report.get()["new_user"]
            daily_report["active_user"]+=hour_report.get()["active_user"]
            daily_report["total_impression"]+=hour_report.get()["total_impression"]
            daily_report["total_impression_unique"]+=hour_report.get()["total_impression_unique"]
            daily_report["total_click"]+=hour_report.get()["total_click"]
            daily_report["total_click_unique"]+=hour_report.get()["total_click_unique"]

        return daily_report

class DailyReportHist:
    def get(self):
        daily_report_hist_list=[]
        r=redis.Redis(connection_pool=RedisPool_G)
        hour=datetime.today().hour

        for hour_index in reversed(xrange(12)):
            if hour-hour_index>=0:
                xhour=hour-hour_index
            else:
                xhour=hour-hour_index+24

            hour_report=HourReport(xhour).get()
            if hour_report is not None:
                daily_report_hist_list.append(hour_report)
            else:
                break

        daily_report_hist={}

        new_user_list=[]
        active_user_list=[]
        impressions_list=[]
        clicks_list=[]
        for report in daily_report_hist_list:
            new_user_list.append(report["new_user"])
            active_user_list.append(report["active_user"])
            impressions_list.append(report["total_impression"])
            clicks_list.append(report["total_click"])

        daily_report_hist["new_user"]=self.calc_hist_string(new_user_list)
        daily_report_hist["active_user"]=self.calc_hist_string(active_user_list)
        daily_report_hist["total_impression"]=self.calc_hist_string(impressions_list)
        daily_report_hist["total_click"]=self.calc_hist_string(clicks_list)

        return daily_report_hist

    def calc_hist_string(self,hist_list):
        max_val=max(hist_list)

        result_str=""

        for val in hist_list:
            if max_val==0:
                result_str="0,"
            else:
                result_str+=str(int(round(val*10/max_val)))+","

        return result_str[:-1]


import os
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

blk = mpl.patches.Rectangle((-70,-120), 140, 80, color='0.5')
blk.set_lw(0)

x = np.r_[-300:300:20]
y = np.zeros_like(x)+30.

fig = plt.figure(figsize=(6,4))
ax = fig.add_axes([0.14, 0.14, 0.8, 0.8])
ax.plot([-400, 400], [0, 0], color='k', ls='-')

ax.add_patch(blk)

ax.set_xlabel("Easting (m)")
ax.set_ylabel("Depth (m)")
ax.plot(x, y, marker='.', color='black', markersize=2, linestyle='None')
plt.xlim(-300, 300)
plt.ylim(-300, 100)
ax.set_aspect('equal')

ytick = [-300, -200, -100, 0, 100]
ax.set_yticks(ytick)

fig.canvas.draw()
fname = '../figs/ATEMlayout.eps'
plt.savefig(fname, format='eps')
os.system("open " + fname)


#! /usr/bin/env python
# -*- coding: utf8 -*-
"""
@author: ralf
Instalar:
pip install python-wordpress-xmlrpc
"""
# Librerias de python para wordpress
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods import media, posts
# LIbrerias para hacer webscraping
import requests
import bs4 as bs
import sys
try:
    # URL sistema y credenciales de acceso
    wp = Client('http://localhost/wordpress/xmlrpc.php', 'mercadodent', 'OhL#APNmpguQb22C)E')
    # URLs a obtener data
    url_txt = ['https://www.baradent.cl/categorias/endodoncia',
    'https://www.baradent.cl/categorias/endodoncia?p=2',
    'https://www.baradent.cl/categorias/endodoncia?p=3',]
    for ss in url_txt:
        #Comenzamos a obtener los datos
        categorias = ss.split("https://www.baradent.cl/categorias/")
        r = requests.get(ss)
        soup = bs.BeautifulSoup(r.content, 'html.parser')
        divs = soup.find_all("li", class_="item")
        for item in divs:
            imagen = item.div.a.img["src"]
            nombre = item.h2.a.text
            urlorigen = item.h2.a["href"]
            url_producto = [item.h2.a["href"]]
            for link in url_producto:
                rq = requests.get(link)
                lsoup = bs.BeautifulSoup(rq.content, 'html.parser')
                detallesku = lsoup.find_all("div", class_="sku")
                descripcion = lsoup.find_all("div", class_="panel")
                for psku in detallesku:
                    sku = getattr(psku.find('span', attrs={'class': 'value'}), "text")
                    break
                for pdetalle in descripcion:
                    desc = pdetalle.div.text
                    break
            precio = getattr(item.contents[9].find('span', attrs={'class': 'price'}), "text", 0).strip()
            precio = precio.replace(".","").strip().lstrip("$")
            #Insert en el sistema wordpress
            post = WordPressPost()
            post.title = nombre.strip()
            post.content = desc.strip()
            #post.thumbnail = attachment_id
            post.post_status = 'publish'
            post.terms_names = {
                'post_tag': ['Baradent'],
                'category': ['Endodoncia']
                }
            post.custom_fields = []
            post.custom_fields.append({
                '_regular_price':precio,
                '_sku': sku.strip(),
                '_product_url':url_producto,
                '_button_text':'Baradent',
                '_price':precio
            })
            wp.call(NewPost(post))

except Exception as e:
    sys.exit(e)


import wx

class DisplayPanel(wx.Panel):

    #---------------------------------------------------------------------------
    def __init__(self, parent):
        """ Constructor """
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour("WHITE")
        self.WIDTH = (parent.GetSize()[0] / 3) - 10
        self.index = 0

        self.__createSizer()
        self.__createListCtrl()

        self.__addSizerContent()

        self.SetSizer(self.main_sizer)

    #---------------------------------------------------------------------------
    def __createSizer(self):
        """ Create sizer for class """
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)

    #---------------------------------------------------------------------------
    def __createListCtrl(self):
        self.list_ctrl = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
        self.list_ctrl.InsertColumn(0, 'Tweeter Handler', width=self.WIDTH)
        self.list_ctrl.InsertColumn(1, 'Sentiment Score', width=self.WIDTH)
        self.list_ctrl.InsertColumn(2, 'Top Hash Tag', width=self.WIDTH)

    #---------------------------------------------------------------------------
    def __addSizerContent(self):
        """ Add objects to main sizer """
        self.main_sizer.Add(self.list_ctrl, 1, wx.EXPAND)

    #---------------------------------------------------------------------------
    def addToList(self, name, sentiment=None, hashtag=None):
        """ Add information to list control """
        if name == None:
            self.list_ctrl.InsertStringItem(self.index, "None")
        else:
            self.list_ctrl.InsertStringItem(self.index, name)
        if sentiment == None:
            self.list_ctrl.SetStringItem(self.index, 1, "N/A")
        else:
            self.list_ctrl.SetStringItem(self.index, 1, sentiment)

        if hashtag == None or hashtag == "None":
            self.list_ctrl.SetStringItem(self.index, 2, "N/A")
        else:
            self.list_ctrl.SetStringItem(self.index, 2, eval(hashtag)[0])
        self.index += 1


#!/usr/bin/env python3
"""Saper napisany w Pythonie i GTK+ 3 za pomocą bibliotek PyGObject i NumPy.
Użycie: ./minesweeper_gtk.py lub python3 minesweeper_gtk.py"""

#Importy
import time
import numpy
from gi.repository import Gtk, GObject

#Inicjalizacja
GRID_LABELS = {-2:"*", -1:"", 0:"", 1:"1", 2:"2", 3:"3", 4:"4", 5:"5", 6:"6", 7:"7", 8:"8"}

MAIN_WINDOW = Gtk.Window(title="Saper", default_width=640)
MAIN_WINDOW.connect("destroy", Gtk.main_quit)
CANVAS = Gtk.Box(orientation=0, spacing=10)
SIDEBAR = Gtk.Box(orientation=1)

#Definicje funkcji i klas
def mine_button_press_handler(button, event):
    """Ta funkcja obsługuje kliknięcia przycisków min wszystkimi przyciskami\
    oraz powiązane z nimi funkcje rozszerzonego odkrywania i zaznaczania."""
    check_x, check_y = button.pos_x, button.pos_y

    #Obsługa lewego kliknięcia
    if event.button == 1:
        #Jeżeli zaczynamy grę, to generujemy planszę
        if MAIN_FIELD.fields_left == 0:
            MAIN_FIELD.populate(check_x, check_y)

        #Jeżeli pole jest niezaznaczone, to odkrywamy je
        if MAIN_FIELD.view_grid[button.pos_x][button.pos_y] == -1:
            MAIN_FIELD.step(check_x, check_y)
            MAIN_FIELD.discover()

    #Obsługa środkowego kliknięcia, jeżeli wokół pola jest mina
    elif event.button == 2 and MAIN_FIELD.view_grid[button.pos_x][button.pos_y] > 0:
        #Nie wychodząc poza granice siatki odkrywamy niezaznaczone pola wokół
        for border_x in range(max(0, button.pos_x-1), min(button.pos_x+2, MAIN_FIELD.size_x)):
            for border_y in range(max(0, button.pos_y-1), min(button.pos_y+2, MAIN_FIELD.size_y)):
                if MAIN_FIELD.view_grid[border_x][border_y] == -1:
                    MAIN_FIELD.step(border_x, border_y)
                    MAIN_FIELD.discover()

    #Obsługa prawego kliknięcia
    elif event.button == 3:
        #Zamieniamy pole nieodkryte na zaznaczone lub na odwrót
        if MAIN_FIELD.view_grid[button.pos_x][button.pos_y] == -1:
            MAIN_FIELD.view_grid[button.pos_x][button.pos_y] = -2
            button.set_label(GRID_LABELS[-2])
            MAIN_FIELD.markers_placed += 1
        elif MAIN_FIELD.view_grid[button.pos_x][button.pos_y] == -2:
            MAIN_FIELD.view_grid[button.pos_x][button.pos_y] = -1
            button.set_label(GRID_LABELS[-1])
            MAIN_FIELD.markers_placed -= 1

        #Odpowiednio zmieniamy tekst ze stanem
        MAIN_FIELD.fields_label.set_label(
            "{}/{}".format(MAIN_FIELD.markers_placed, MAIN_FIELD.mines))

def spin_button_change_handler(button):
    """Ta funkcja obsługuje zmianę maksymalnej ilości min w zależności\
    od wartości pola szerokości i wysokości."""
    max_mines = (SETTINGS_DIALOG.height_spinbutton.get_value_as_int()
                 * SETTINGS_DIALOG.width_spinbutton.get_value_as_int() - 1)
    SETTINGS_DIALOG.mines_spinbutton.set_range(10, max_mines)

def settings_button_handler(button):
    """Ta funkcja obsługuje wyświetlanie okna ustawień."""
    SETTINGS_DIALOG.settings_window.show_all()

def timer_update():
    """Ta funkcja aktualizuje stoper, gdy gra się toczy."""
    #Gdy gra się nie toczy, zwrócenie False zatrzymuje licznik
    if MAIN_FIELD.fields_left == 0:
        return False

    time_delta = time.time() - MAIN_FIELD.timer_start

    #Obliczamy ilość sekund i minut, które upłynęły
    minutes_delta = int(time_delta) // 60
    seconds_delta = int(time_delta) % 60

    #Aktualizujemy tekst stopera
    MAIN_FIELD.time_label.set_label("{:0=2d}:{:0=2d}".format(minutes_delta, seconds_delta))

    #Aby licznik znów zadziałał, funkcja zwraca True
    return True

def win():
    """Ta funkcja wyswietla okno wygranej."""
    #Ustawiamy przyciski min na nieklikalne
    for item in MAIN_FIELD.grid_buttons:
        item.set_sensitive(False)

    #Wyświetlamy okno dialogowe
    dialog = Gtk.MessageDialog(MAIN_WINDOW, 0, Gtk.MessageType.INFO,
                               Gtk.ButtonsType.OK, "Wygrałeś!")
    dialog.format_secondary_text(
        "Twój czas to {}\nKliknij OK aby zmienić typ planszy lub zacząć od nowa."
        .format(MAIN_FIELD.time_label.get_label()))
    dialog.run()
    dialog.destroy()

def lose():
    """Ta funkcja wyświetla okno przegranej."""
    #Ustawiamy przyciski min na nieklikalne
    for item in MAIN_FIELD.grid_buttons:
        item.set_sensitive(False)

    #Ustawiamy ilość pozostałych pól na 0, aby stoper się zatrzymał
    MAIN_FIELD.fields_left = 0

    #Wyświetlamy okno dialogowe
    dialog = Gtk.MessageDialog(MAIN_WINDOW, 0, Gtk.MessageType.INFO,
                               Gtk.ButtonsType.OK, "Przegrałeś.")
    dialog.format_secondary_text(
        "Twój czas to {}\nKliknij OK aby zmienić typ planszy lub zacząć od nowa."
        .format(MAIN_FIELD.time_label.get_label()))
    dialog.run()
    dialog.destroy()

class BombButton(Gtk.Button):
    """Ta klasa jest rozszerzeniem klasy Gtk.Button o zmienne położenia na siatce\
    równomierne skalowanie i automatyczne dołączanie do siatki.
    (self.pos_x i self.pos_y)"""

    def __init__(self, parent, pos_x, pos_y):
        self.parent, self.pos_x, self.pos_y = parent, pos_x, pos_y

        #Stworzenie przycisku wraz z odpowiednim opisem i skalowaniem w obu kierunkach
        Gtk.Button.__init__(self, label=GRID_LABELS[-1], hexpand=True, vexpand=True)

        #Przypisanie funkcji i dopięcie przycisku do siatki
        self.connect("button-press-event", mine_button_press_handler)
        parent.button_grid.attach(self, pos_x, pos_y, 1, 1)

class MineField():
    """Ta klasa zawiera tablice określające planszę, elementy interfejsu,\
    i metody, które je zmieniają."""

    def populate(self, step_x, step_y):
        """Ta metoda generuje planszę z uwzględnieniem pierwszego kroku i uruchamia stoper."""
        self.fields_left = self.size_x * self.size_y - self.mines
        mines_to_create = self.mines

        while mines_to_create > 0:
            #Losujemy pozycję miny
            fill_x = numpy.random.randint(0, self.size_x)
            fill_y = numpy.random.randint(0, self.size_y)

            #Sprawdzamy czy nie zapełniamy pierwszego kroku lub istniejącej miny
            if self.mine_grid[fill_x][fill_y] != 1 and (fill_x, fill_y) != (step_x, step_y):
                self.mine_grid[fill_x][fill_y] = 1
                mines_to_create -= 1

        #Uruchamiamy stoper
        self.timer_start = time.time()
        GObject.timeout_add_seconds(1, timer_update)

    def step(self, step_x, step_y):
        """Ta metoda służy do odkrywania pól, zmiany opisów przycisków\
        i zaznaczania sąsiednich pól do odkrycia."""
        mines_around = 0

        #Jeśli trafiamy na minę, to przegrywamy
        if self.mine_grid[step_x][step_y] == 1:
            lose()

        #Sprawdzamy ile jest min wokół kroku, nie wychodzimy poza granice siatki
        for border_x in range(max(0, step_x-1), min(step_x+2, self.size_x)):
            for border_y in range(max(0, step_y-1), min(step_y+2, self.size_y)):
                if (border_x, border_y) != (step_x, step_y):
                    #Obliczamy ilość min wokół kroku
                    mines_around += self.mine_grid[border_x][border_y]

        #Przypisujemy ilość min do widoku
        if self.view_grid[step_x][step_y] == -1:
            self.fields_left -= 1
            self.view_grid[step_x][step_y] = mines_around
            self.grid_buttons[step_x+step_y*self.size_x].set_label(GRID_LABELS[mines_around])

        #Jeśli wokół kroku nie ma min, zaznaczamy pola do odkrycia
        if mines_around == 0:
            for border_x in range(max(0, step_x-1), min(step_x+2, self.size_x)):
                for border_y in range(max(0, step_y-1), min(step_y+2, self.size_y)):
                    if self.view_grid[border_x][border_y] == -1:
                        #Dodajemy do kolejki
                        self.queue.add((border_x, border_y))

            #Pola puste są nieklikalne
            self.grid_buttons[step_x+step_y*self.size_x].set_sensitive(False)

    def discover(self):
        """Ta metoda służy do odkrywania pól znajdujących się wokół pustych pól\
        zaznaczonych przez funkcję step."""

        #Odkrywamy kolejne pola aż kolejka się skończy
        while self.queue:
            (discover_x, discover_y) = self.queue.pop()
            self.step(discover_x, discover_y)

        #Jeśli nie ma pól do odkrycia, to wygrywamy
        if self.fields_left == 0:
            win()

    def grids_setup(self):
        """Ta metoda służy do ustawiania pustych tablic podczas inicjalizacji."""
        self.mine_grid = numpy.zeros((self.size_x, self.size_y), dtype=bool)
        self.view_grid = numpy.full((self.size_x, self.size_y), -1, dtype=int)

    def restart(self, button):
        """Ta metoda służy do restartowania gry, w tym zmiany opisów przycisków\
        i zawartości tablic."""
        self.grids_setup()
        self.markers_placed = 0
        self.fields_left = 0
        self.timer_start = 0
        self.queue.clear()

        #Ustawiamy opisy przycisków
        for item in self.grid_buttons:
            item.set_label(GRID_LABELS[-1])
            item.set_sensitive(True)

        #Ustawiamy opisy stanu gry
        self.fields_label.set_label("{}/{}".format(self.markers_placed, self.mines))
        self.time_label.set_label("00:00")

    def change_size(self, button):
        """Ta metoda służy do zmiany właściwości pola gry, w tym siatki, przycisków\
        i tablic."""
        self.size_x, self.size_y, self.mines = (
            SETTINGS_DIALOG.width_spinbutton.get_value_as_int(),
            SETTINGS_DIALOG.height_spinbutton.get_value_as_int(),
            SETTINGS_DIALOG.mines_spinbutton.get_value_as_int())
        self.grids_setup()
        self.markers_placed = 0
        self.fields_left = 0
        self.timer_start = 0
        self.queue.clear()

        #Usuwamy przyciski i referencje do nich
        for item in self.grid_buttons:
            item.destroy()
        self.grid_buttons.clear()

        #Tworzymy nowe przyciski i dołączamy je do siatki
        for pos_y in range(self.size_y):
            for pos_x in range(self.size_x):
                self.grid_buttons.append(BombButton(self, pos_x, pos_y))

        #Ustawiamy opisy stanu gry
        self.fields_label.set_label("{}/{}".format(self.markers_placed, self.mines))
        self.time_label.set_label("00:00")

        #Aktualizujemy główne okno i zamykamy okno ustawień
        MAIN_WINDOW.show_all()
        SETTINGS_DIALOG.settings_window.hide()

    def __init__(self, size_x, size_y, mines):
        self.size_x, self.size_y, self.mines = size_x, size_y, mines
        self.grids_setup()
        self.markers_placed = 0
        self.fields_left = 0
        self.timer_start = 0
        self.grid_buttons = []
        self.queue = set()

        #Ustawiamy równe wymiary kolumn i wierszy
        self.button_grid = Gtk.Grid(column_homogeneous=True, row_homogeneous=True)

        #Tworzymy przyciski
        for pos_y in range(size_y):
            for pos_x in range(size_x):
                self.grid_buttons.append(BombButton(self, pos_x, pos_y))

        #Dołączamy siatkę do głównego okna
        CANVAS.pack_start(self.button_grid, True, True, 10)

        #Tworzymy napisy
        state_labels = Gtk.Box(orientation=1, spacing=10)
        info_label = Gtk.Label(label="Stan gry:")

        #Ustawiamy opisy stanu gry
        self.fields_label = Gtk.Label(label='{}/{}'.format(self.markers_placed, self.mines))
        self.time_label = Gtk.Label(label="00:00")

        #Dołączamy przyciski do paska bocznego
        state_labels.pack_start(info_label, False, False, 0)
        state_labels.pack_start(self.fields_label, False, False, 0)
        state_labels.pack_start(self.time_label, False, False, 0)
        SIDEBAR.pack_start(state_labels, False, False, 10)

class SettingsDialog():
    """Ta klasa obsługuje okno ustawień."""

    def __init__(self):
        #Tworzymy nowe modalne okno do ustawiania właściwości planszy
        self.settings_window = Gtk.Window(title="Ustawienia", default_width=250,
                                          modal=True, transient_for=MAIN_WINDOW)
        height_spinbutton_label = Gtk.Label(label="Wysokość:")
        self.height_spinbutton = Gtk.SpinButton.new_with_range(8, 32, 1)
        width_spinbutton_label = Gtk.Label(label="Szerokość:")
        self.width_spinbutton = Gtk.SpinButton.new_with_range(8, 32, 1)
        mines_spinbutton_label = Gtk.Label(label="Ilość min:")
        self.mines_spinbutton = Gtk.SpinButton.new_with_range(10, 63, 1)
        accept_button = Gtk.Button(label="OK")

        button_container = Gtk.Box(orientation=1, spacing=4)
        button_container.pack_start(height_spinbutton_label, False, False, 4)
        button_container.pack_start(self.height_spinbutton, False, False, 4)
        button_container.pack_start(width_spinbutton_label, False, False, 4)
        button_container.pack_start(self.width_spinbutton, False, False, 4)
        button_container.pack_start(mines_spinbutton_label, False, False, 4)
        button_container.pack_start(self.mines_spinbutton, False, False, 4)
        button_container.pack_start(accept_button, False, False, 4)

        #Podpinamy funkcje do przycisków
        self.height_spinbutton.connect("value-changed", spin_button_change_handler)
        self.width_spinbutton.connect("value-changed", spin_button_change_handler)
        accept_button.connect("clicked", MAIN_FIELD.change_size)
        self.settings_window.add(button_container)

#Konstruujemy obiekty
MAIN_FIELD = MineField(16, 16, 10)
SETTINGS_DIALOG = SettingsDialog()

#Tworzymy przyciski
CONTROL_BUTTONS = Gtk.Box(orientation=1, spacing=10)
SETTINGS_BUTTON = Gtk.Button(label="Zmień ustawienia planszy")
RESTART_BUTTON = Gtk.Button(label="Zacznij od nowa")

#Dołączamy przyciski do paska bocznego
CONTROL_BUTTONS.pack_start(SETTINGS_BUTTON, False, False, 0)
CONTROL_BUTTONS.pack_start(RESTART_BUTTON, False, False, 0)
SIDEBAR.pack_end(CONTROL_BUTTONS, False, False, 10)

#Łączymy przyciski na pasku bocznym do ich funkcji
RESTART_BUTTON.connect("clicked", MAIN_FIELD.restart)
SETTINGS_BUTTON.connect("clicked", settings_button_handler)
CANVAS.pack_end(SIDEBAR, False, False, 10)

#Pokazujemy okno
MAIN_WINDOW.add(CANVAS)
MAIN_WINDOW.show_all()

Gtk.main()


from colander import null, Invalid
from deform.i18n import _
from deform.widget import AutocompleteInputWidget, SelectWidget, Widget
from deform.widget import DateTimeInputWidget as DateTimeInputWidgetBase
import json
import sys

PY3 = sys.version_info[0] == 3

if PY3:
    string_types = str
else:
    string_types = basestring  # noqa

class TypeaheadInputWidget(AutocompleteInputWidget):
    """
    Renders an ``<input type="text"/>`` widget which provides
    autocompletion via a list of values using bootstrap's typeahead plugin
    http://twitter.github.com/bootstrap/javascript.html#typeahead.

    When this option is used, the :term:`bootstrap-typeahead`
    library must be loaded into the page serving the form for
    autocompletion to have any effect.

    **Attributes/Arguments**

    size
        The size, in columns, of the text input field.  Defaults to
        ``None``, meaning that the ``size`` is not included in the
        widget output (uses browser default size).

    template
        The template name used to render the widget.  Default:
        ``typeahead_textinput``.

    readonly_template
        The template name used to render the widget in read-only mode.
        Default: ``readonly/typeahead_textinput``.

    strip
        If true, during deserialization, strip the value of leading
        and trailing whitespace (default ``True``).

    source
        A list of strings.
        Defaults to ``[]``.

    items
        The max number of items to display in the dropdown. Defaults to
        ``8``.

    """

    readonly_template = "readonly/textinput"
    size = None
    strip = True
    template = "typeahead_input"
    source = []
    items = 8

    def serialize(self, field, cstruct, readonly=False):
        if cstruct in (null, None):
            cstruct = ""
        options = dict(size=self.size, source=self.source)
        template = readonly and self.readonly_template or self.template
        return field.renderer(
            template, cstruct=cstruct, field=field, options=json.dumps(options)
        )

class DateTimeInputWidget(DateTimeInputWidgetBase):

    template = "splitted_datetimeinput"
    readonly_template = "readonly/textinput"
    requirements = ()

    def serialize(self, field, cstruct, readonly=False):
        if cstruct is null or not cstruct:
            _date = ""
            _time = ""
        else:
            if len(cstruct) == 25:  # strip timezone if it's there
                cstruct = cstruct[:-6]
            try:
                _date, _time = cstruct.split("T")
            except ValueError:
                _date, _time = cstruct.split(" ")
        template = readonly and self.readonly_template or self.template
        return field.renderer(
            template, field=field, cstruct=cstruct, date=_date, time=_time
        )

    def deserialize(self, field, pstruct):
        if pstruct is null:
            return null
        else:
            _date = pstruct["date"].strip()
            _time = pstruct["time"].strip()

            if not _date and not _time:
                return null

            if not _date:
                raise Invalid(field.schema, _("Incomplete date"), "")

            if not _time:
                _time = "00:00:00"

            result = " ".join([_date, _time])

            return result

class ChosenSingleWidget(SelectWidget):
    template = "chosen_single"

class ChosenOptGroupWidget(SelectWidget):
    template = "chosen_optgroup"

class ChosenMultipleWidget(Widget):
    template = "chosen_multiple"
    values = ()
    size = 1

    def serialize(self, field, cstruct, readonly=False):
        if cstruct in (null, None):
            cstruct = ()
        template = readonly and self.readonly_template or self.template
        return field.renderer(template, field=field, cstruct=cstruct)

    def deserialize(self, field, pstruct):
        if pstruct is null:
            return null
        if isinstance(pstruct, string_types):
            return (pstruct,)
        return tuple(pstruct)


from rest_framework.generics import (
    ListAPIView,
    CreateAPIView,
    RetrieveAPIView,
    RetrieveUpdateAPIView,
    RetrieveDestroyAPIView,
)

from rest_framework.response import Response

from rest_framework.views import APIView

from ..models import Follow
from .serializers import FollowSerializer

class FollowerListAPIView(ListAPIView):
    serializer_class = FollowSerializer

    def get_queryset(self,*arg,**kwargs):
        user = self.kwargs['user']
        queryset = Follow.objects.filter(followed__user=user).order_by('-timestamp')
        return queryset

class FollowedListAPIView(ListAPIView):
    serializer_class = FollowSerializer

    def get_queryset(self,*arg,**kwargs):
        user = self.kwargs['user']
        queryset = Follow.objects.filter(follower__user=user).order_by('-timestamp')
        return queryset

class FollowAPIView(CreateAPIView):
    queryset = Follow.objects.all()
    serializer_class = FollowSerializer

class UnfollowAPIView(APIView):
    queryset = Follow.objects.all()
    serializer_class = FollowSerializer

    def delete(self,*args,**kwrags):
        followerUser = self.kwargs['follower']
        followedUser = self.kwargs['followed']

        Follow.objects.filter(follower__user=followerUser,followed__user=followedUser).delete()
        return Response({})

class IsFollowerAPIView(APIView):
    serializer_class = FollowSerializer

    def get(self,*args,**kwargs):
        followerUser = self.kwargs['follower']
        followedUser = self.kwargs['followed']

        isFollower = Follow.objects.filter(follower__user=followerUser,followed__user=followedUser).count() >  0
        return Response({
            "isFollower": isFollower,
        })

class IsFollowedAPIView(APIView):
    serializer_class = FollowSerializer

    def get(self,*args,**kwargs):
        followerUser = self.kwargs['follower']
        followedUser = self.kwargs['followed']

        isFollowed = Follow.objects.filter(follower__user=followerUser,followed__user=followedUser).count() >  0
        return Response({
            "isFollowed": isFollowed,
        })


from django.shortcuts import render
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, HttpResponseRedirect
from datetime import datetime
from .models import Comment
from blog.models import Post

# Create your views here.

@csrf_exempt
@login_required(login_url='/auth/login')
def create_comment(request, post_id):
	args = {}
	post = Post.objects.get(pk = post_id)
	args['image'] = post.image.url
	args['name'] = post.name
	args['description'] = post.description
	args['username'] = post.owner
	if request.POST:
		text = request.POST.get('comment', '')
		if len(text) == 0:
			return render(request, 'blog/post.html', args)
		owner = request.user
		date = datetime.now()
		post = Post.objects.get(pk=post_id)
		likes = 0
		comment = Comment.objects.create(text=text, likes=likes, date=date, post=post, owner=owner)
		comment.save()
		comments = Comment.objects.all().filter(post=post)
		args['comments'] = comments
	return HttpResponseRedirect("/posts/" + str(post.id))


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

from setuptools import setup, find_packages

with open('README.md') as f:
    readme = f.read()

with open('LICENSE') as f:
    license = f.read()

setup(
    name='simple-blockchain',
    version='0.1.1',
    description='This is a simplified model of a blockchain, which is also the technology behind the well known crypto-currency Bitcoin.',
    long_description=readme,
    author='Aleksander Sadowski',
    author_email='aleksander.sadowski@alsado.de',
    url='https://github.com/alekssadowski95/simple-blockchain',
    license=license,
    packages=find_packages(exclude=('tests', 'docs')),
    classifiers = [
        'Development Status :: 4 - Beta',
        'Intended Audience :: Education',
        'Operating System :: Microsoft :: Windows :: Windows 10',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3'
    ]
)


import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.preprocessing import StandardScaler

# importing the dataset
dataset = pd.read_csv('data\Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2:3].values

# feature scaling
# sc_X = StandardScaler()
# X = sc_X.fit_transform(X)
# sc_y = StandardScaler()
# y = sc_y.fit_transform(y)

# fitting SVR to the dataset
regressor = DecisionTreeRegressor(random_state=0)
regressor.fit(X, y)

# predicting the slaray for positon 6.5
# inverse transform due to feature scaling
y_pred = regressor.predict(6.5)

# visualizing the Decision Tree result
# this is a non-linear as well as non-continuos regression model
# therefore this visualization will not work
plt.scatter(X, y, color='red')
plt.plot(X, regressor.predict(X), color='blue')
plt.title('Truth or Bluff: Decision Tree')
plt.xlabel('Position')
plt.ylabel('Salary')
plt.show()

# non-continuos regression model needs to be plotted in higher
# resolution
X_grid = np.arange(min(X), max(X), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color='red')
plt.plot(X_grid, regressor.predict(X_grid), color='blue')
plt.title('Truth or Bluff: Decision Tree')
plt.xlabel('Position')
plt.ylabel('Salary')
plt.show()