from .classes import Transform
from .classes import Element
from .constants import Position, Resize
from . import utils
from ..common import Action

import cv2
import typing as tp
import numpy as np
import matplotlib.pyplot as plt

class Compose(Action):
  def __init__(self, actions: tp.List[Action]):
    if hasattr(actions, "__iter__"):
      self.actions = actions
    else:
      self.actions = [actions]

  def __call__(self, element: Element) -> Element:
    for system in self.actions:
      element = system(element)

    return element

class ApplyToObjects(Transform):
  def __init__(self, transforms: tp.List[Transform]):
    if hasattr(transforms, "__iter__"):
      self.transforms = transforms
    else:
      self.transforms = [transforms]

  def __call__(self, element: Element) -> Element:
    for object in element.objects:
      for system in self.transforms:
        object = system(object)
    return element

class DrawElement(Transform):
  def __call__(self, element: Element) -> Element:
    image = element.image.copy()

    img = cv2.GaussianBlur(element.objects[0].image, (7, 7), 0)
    b, g, r, a = cv2.split(img)

    cimg = element.objects[0].image.copy()
    cimg[:, :, 3] = a

    for obj in element.objects:
      image = utils.overlayTransparent(
        background=image,
        overlay=obj.image[..., :-1],
        mask=obj.image[..., -1],
        x=obj.x,
        y=obj.y,
      )

    # plt.imshow(image)
    # plt.show()
    # print("Image created")

    element.fimg = image

    return element

class RandomResize(Transform):
  def __init__(self, mode:Resize = Resize.asymmetric, wmin=None, wmax=None, hmin=None, hmax=None):
    self.wmin = wmin
    self.wmax = wmax
    self.hmin = hmin
    self.hmax = hmax
    self.mode = mode

  def __call__(self, element: Element) -> Element:
    assert element, "Element cannot be None"

    wmin = self.wmin if self.wmin is not None else element.image.shape[1]
    hmin = self.hmin if self.hmin is not None else element.image.shape[0]
    wmax = self.wmax if self.wmax is not None else element.image.shape[1]
    hmax = self.hmax if self.hmax is not None else element.image.shape[0]

    if(self.mode == Resize.symmetrich):
      h = hmin if hmin == hmax is not None else np.random.randint(
          low=hmin,
          high=hmax,
        )
      w = element.image.shape[1] * (h/element.image.shape[0])
      w = int(w)
    if(self.mode == Resize.symmetricw):
      w = wmin if wmin == wmax is not None else np.random.randint(
          low=wmin,
          high=wmax,
        )
      h = element.image.shape[0] * (w/element.image.shape[1])
      h = int(h)
    else:
      w = wmin if wmin == wmax is not None else np.random.randint(
          low=wmin,
          high=wmax,
        )

      h = hmin if hmin == hmax is not None else np.random.randint(
          low=hmin,
          high=hmax,
        )

    element.image = cv2.resize(element.image, (w, h))

    return element

# Only for element with objects
class ObjectsRandomPosition(Transform):
  def __init__(self, mode:Position = Position.random, xmin=None, xmax=None, ymin=None, ymax=None):
    self.xmin = xmin
    self.xmax = xmax
    self.ymin = ymin
    self.ymax = ymax
    self.mode = mode

  def __call__(self, element: Element) -> Element:
    assert element, "Element cannot be None"

    el_h: int = element.image.shape[0]
    el_w: int = element.image.shape[1]

    if(self.mode == Position.percentage):
      xmin = (self.xmin if self.xmin <= 1 else (self.xmin%100)/100) * el_w if self.xmin is not None else 0
      xmax = (self.xmax if self.xmax <= 1 else (self.xmax%100)/100) * el_w if self.xmax is not None else el_w
      ymin = (self.ymin if self.ymin <= 1 else (self.ymin%100)/100) * el_h if self.ymin is not None else 0
      ymax = (self.ymax if self.ymax <= 1 else (self.ymax%100)/100) * el_h if self.ymax is not None else el_h
    else:
      xmin = self.xmin if self.xmin is not None else 0
      xmax = self.xmax if self.xmax is not None else el_w
      ymin = self.ymin if self.ymin is not None else 0
      ymax = self.ymax if self.ymax is not None else el_h

    lastx, lasty = 0, 0
    for obj in element.objects:
      obj_h: int = int(obj.image.shape[0])
      obj_w: int = int(obj.image.shape[1])

      while True:
        obj.x = np.random.randint(
          low=max(0, xmin- obj_w/2) ,
          high=min(xmax, el_w),
        )

        if(obj.x > lastx + obj_w/2 or obj.x < lastx - obj_w/2):
          break

      while True:
        obj.y = np.random.randint(
          low=max(0, ymin- obj_h/2) ,
          high=min(ymax, el_h),
        )

        if(obj.y > lasty + obj_h/2 or obj.y < lasty - obj_h/2):
          break

      lastx, lasty = obj.x, obj.y

    return element

class ObjectsRandomResize(Transform):
  def __init__(self, mode:Resize = Resize.asymmetric, wmin=None, wmax=None, hmin=None, hmax=None):
    self.wmin = wmin
    self.wmax = wmax
    self.hmin = hmin
    self.hmax = hmax
    self.mode = mode

  def __call__(self, element: Element) -> Element:
    assert element, "Element cannot be None"

    el_h: int = element.image.shape[0]
    el_w: int = element.image.shape[1]

    wmin = (self.wmin if self.wmin <= 1 else (self.wmin%100)/100) * el_w if self.wmin is not None else 0
    wmax = (self.wmax if self.wmax <= 1 else (self.wmax%100)/100) * el_w if self.wmax is not None else el_w
    hmin = (self.hmin if self.hmin <= 1 else (self.hmin%100)/100) * el_h if self.hmin is not None else 0
    hmax = (self.hmax if self.hmax <= 1 else (self.hmax%100)/100) * el_h if self.hmax is not None else el_h

    rr = RandomResize(mode=Resize.symmetricw, wmin=wmin, wmax=wmax, hmin=hmin, hmax=hmax)

    for obj in element.objects:
      obj = rr(obj)

    return element

class ObjectsGetBGColor(Transform):
  def __init__(self):
    pass
  def __call__(self, element: Element) -> Element:
    for obj in element.objects:
      t = self.color_transfer(element.image, obj.image)
      obj.image = t

    return element

  def color_transfer(self, source, target):
    alpha = cv2.split(target)[3]
    # convert the images from the RGB to L*ab* color space, being
    # sure to utilizing the floating point data type (note: OpenCV
    # expects floats to be 32-bit, so use that instead of 64-bit)
    source = cv2.cvtColor(source, cv2.COLOR_BGR2LAB).astype("float32")
    target = cv2.cvtColor(target, cv2.COLOR_BGR2LAB).astype("float32")

    # compute color statistics for the source and target images
    (lMeanSrc, lStdSrc, aMeanSrc, aStdSrc, bMeanSrc, bStdSrc) = self.image_stats(source)
    (lMeanTar, lStdTar, aMeanTar, aStdTar, bMeanTar, bStdTar) = self.image_stats(target)

    # subtract the means from the target image
    (l, a, b) = cv2.split(target)
    l -= lMeanTar
    a -= aMeanTar
    b -= bMeanTar

    # scale by the standard deviations
    l = (lStdTar / lStdSrc) * l
    a = (aStdTar / aStdSrc) * a
    b = (bStdTar / bStdSrc) * b

    # add in the source mean
    l += lMeanSrc
    a += aMeanSrc
    b += bMeanSrc

    # clip the pixel intensities to [0, 255] if they fall outside
    # this range
    l = np.clip(l, 0, 255)
    a = np.clip(a, 0, 255)
    b = np.clip(b, 0, 255)

    # merge the channels together and convert back to the RGB color
    # space, being sure to utilize the 8-bit unsigned integer data
    # type
    transfer = cv2.merge([l, a, b])
    transfer = cv2.cvtColor(transfer.astype("uint8"), cv2.COLOR_LAB2BGR)

    b_channel, g_channel, r_channel = cv2.split(transfer)
    transfer = cv2.merge((b_channel, g_channel, r_channel, alpha))

    # return the color transferred image
    return transfer

  def image_stats(self, image):
    # compute the mean and standard deviation of each channel
    (l, a, b) = cv2.split(image)
    (lMean, lStd) = (l.mean(), l.std())
    (aMean, aStd) = (a.mean(), a.std())
    (bMean, bStd) = (b.mean(), b.std())

    # return the color statistics
    return (lMean, lStd, aMean, aStd, bMean, bStd)


import matplotlib.pyplot as plt
import tensorflow
from sklearn.cluster import KMeans, AgglomerativeClustering, Birch, FeatureAgglomeration
from sklearn.cross_decomposition import PLSCanonical, CCA, PLSSVD, PLSRegression
from sklearn.decomposition import PCA, FastICA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.manifold import LocallyLinearEmbedding, TSNE, MDS, Isomap
from sklearn.preprocessing import StandardScaler

from helpers.visualize import plot_true_labels, subplot_components

def visualize(x, y):
    """
    https://scikit-learn.org/stable/modules/clustering.html

    :param x:
    :param y:
    :return:
    """

    # make sure data is scaled
    scaler = StandardScaler()
    x = scaler.fit_transform(x, y)

    # cross decomposition
    cross_decomposers = {
        "PLSCanonical": PLSCanonical(n_components=2),  # 2 because the 3rd component is just zero
        "CCA": CCA(n_components=2),  # 2 because the 3rd component is just zero
        "PLSSVD": PLSSVD(n_components=3),
        "PLSRegression": PLSRegression(n_components=3),
    }

    for extractor_name, extractor in cross_decomposers.items():
        x_components, y_component = extractor.fit_transform(x, tensorflow.keras.utils.to_categorical(y, 3))
        plot_true_labels(x, y, x_components, extractor_name)

    feature_extractors = {
        # Discriminant Analysis
        # https://scikit-learn.org/stable/modules/classes.html#module-sklearn.discriminant_analysis
        "LDA": LinearDiscriminantAnalysis(n_components=2),
        # decomposition
        # https://scikit-learn.org/stable/modules/classes.html#module-sklearn.decomposition
        "PCA": PCA(n_components=3),
        "FastICA": FastICA(n_components=3, random_state=41),
        # Manifold Learning
        # https://scikit-learn.org/stable/modules/classes.html#module-sklearn.manifold
        "Isomap": Isomap(n_components=3, n_jobs=-1),
        "LocallyLinearEmbedding": LocallyLinearEmbedding(n_components=3, n_jobs=-1, random_state=41),
        "MDS": MDS(n_components=3, n_jobs=-1, random_state=41),
        "TSNE": TSNE(n_components=3, n_jobs=-1, random_state=41),
        # Clustering
        # https://scikit-learn.org/stable/modules/classes.html#module-sklearn.cluster
        "FeatureAgglomeration": FeatureAgglomeration(n_clusters=3, linkage='ward')
    }

    cluster_dict = {
        "KMeans": KMeans(n_clusters=3, random_state=41),
        "Birch": Birch(threshold=0.8, n_clusters=3),
        # linkage : {'ward', 'complete', 'average', 'single'}
        "AgglomerativeClustering": AgglomerativeClustering(n_clusters=3, linkage='ward'),
    }
    for extractor_name, extractor in feature_extractors.items():
        components = extractor.fit_transform(x, y)
        plot_true_labels(x, y, components, extractor_name)

    plt.show()

    """
    for name, clusterer in cluster_dict.items():
        plot_predicted_clusters(x, y, components, clusterer, extractor_name + " - " + name)
    """

def plot_predicted_clusters(x, y, components, clusterer, title="", interactive=True):
    if interactive:
        # pip install PyQt5
        import matplotlib as mpl
        mpl.use("Qt5Agg")

    fig = plt.figure(figsize=(12, 12))
    fig.suptitle(title, fontsize=20)
    fig.set_dpi(150)

    # plot ture values
    y_pred = y.values.T
    subplot_components(components, 221, "True labels", y_pred)

    # plot cluster prediction based on x
    y_pred = clusterer.fit_predict(x)

    subplot_components(components, 222, "Predicted on whole input data", y_pred)

    # plot cluster prediction based only on the supplied components
    y_pred = clusterer.fit_predict(components)
    subplot_components(components, 212, "Predicted on only the supplied components", y_pred)


#consists of the following tree structure:
#  global axis  <-- fixed location & rotation in space
#    ring <-- rotates at one rate in z-dimension
#    debris axis <-- rotates at another rate in the z-dimension
#        asteroid 1
#        asteroid 2
#        asteroid 3
#    art asteroid <-- any debris outside the playing field for aesthetics

from enum import Enum

class DEBRIS_TYPE(Enum):
	ASTEROID_LARGE=0
	ASTEROID_MEDIUM=1

class RingAssembly:
	PRE_RENDER_SECONDS=5 #render objects that are defined between now and X seconds into the future
	POST_RENDER_SECONDS=1

	RING_ROTATION_DEGREES_PER_SECOND=-40
	DEBRIS_ROTATION_DEGREES_PER_SECOND=25

	#asset_library is an instance of the AssetLibrary with the template 3D models
	#position is the np.array of x,y,z coords in the global reference frame for hte center of the ring
	#rotation_euler is the Z-X-Y euler rotation angles in degrees, and
	#rotation_matrix is the pi3d rotation matrix for the Shape
	#definition_time is the time at which this RingAssembly is defined
	# all rotation rates are movtions are computed as a linear propagation
	# forward or backward from this timestamp in seconds
	#ring_index is the 3D model to use: None for none, 0 or more for straight or branched as defined in AssetLibrary.rings
	#ring_rotation_degrees is the rotation of the ring at the time definition_time
	#ring_rotation_rate is degrees per second rotation about the Z axis for the ring
	#debris_rotation_rate is degrees per second rotation of the cumulative debris about the Z axis
	def __init__(self,asset_library,position,rotation_euler,rotation_matrix,definition_time,
		ring_index,ring_rotation_degrees,ring_rotation_rate,debris_rotation_rate):
		#variables
		self.rotation_euler=rotation_euler
		self.rotation_matrix=rotation_matrix
		self.definition_time=definition_time
		self.ring_rotation_degrees=ring_rotation_degrees
		self.ring_rotation_rate=ring_rotation_rate
		self.debris_rotation_rate=debris_rotation_rate
		self.is_visible=False
		self.asset_library=asset_library

		#global axis
		self.global_axis=asset_library.invisible.shallow_clone()
		self.global_axis.children=[]
		self.global_axis.position(position[0],position[1],position[2])
		self.global_axis.rotateToX(rotation_euler[0])
		self.global_axis.rotateToY(rotation_euler[1])
		self.global_axis.rotateToZ(rotation_euler[2])

		#ring
		self.ring=asset_library.rings[ring_index].shallow_clone()
		self.ring.children=[]
		self.global_axis.add_child(self.ring)

		#debris axis
		self.debris_axis=None

		#debris objects
		self.debris_list=[]

	#add a piece of debris to this debris_axis
	#location is an [x,y,z] numpy.array of the debris at definition_time
	#angle is the numpy.array [x,y,z] rotation at definition_time measures in degrees
	#angular_velocity is degrees per second
	def addDebris(self,debris_type,location,angle,angular_velocity):
		if(self.debris_axis is None):
			#debris axis, declare only on first usage
			self.debris_axis=self.asset_library.invisible.shallow_clone()
			self.debris_axis.children=[]
			self.global_axis.add_child(self.debris_axis)
		model=None
		if(debris_type==DEBRIS_TYPE.ASTEROID_LARGE):
			model=self.asset_library.asteroid_large.shallow_clone()
		elif(debris_type==DEBRIS_TYPE.ASTEROID_MEDIUM):
			model=self.asset_library.asteroid_medium.shallow_clone()
		model.children=[]
		model.position(location[0],location[1],location[2])
		self.debris_list.append({"model":model,
								 "debris_type":debris_type,
								 "location":location,
								 "angle":angle,
								 "angular_velocity":angular_velocity}) #todo: add radius property
		self.debris_axis.add_child(model)

	def update(self,life_elapsed_time_seconds,light):
		min_time=self.definition_time - self.PRE_RENDER_SECONDS
		max_time=self.definition_time + self.POST_RENDER_SECONDS
		self.is_visible= min_time <= life_elapsed_time_seconds <= max_time
		#if(self.is_visible):
		seconds_offset=(life_elapsed_time_seconds-self.definition_time)
		self.ring.rotateToZ(self.ring_rotation_rate*seconds_offset+self.ring_rotation_degrees)
		if(not self.debris_axis is None):
			self.debris_axis.rotateToZ(self.debris_rotation_rate*seconds_offset)
		self.ring.set_light(light)
		for debris in self.debris_list:
			rot=debris["angle"]
			rot_rate=debris["angular_velocity"]
			model=debris["model"]
			model.rotateToX(rot_rate[0]*seconds_offset+rot[0])
			model.rotateToY(rot_rate[1]*seconds_offset+rot[1])
			model.rotateToZ(rot_rate[2]*seconds_offset+rot[2])
			model.set_light(light)

	def draw(self):
		#TODO: checks if ring is visible, otherwise does not render
		if(self.is_visible):
			self.global_axis.draw()

	#return list of x,y,z and radius for each phyisical (non-art) asteroid
	def getAsteroidPositions(self):
		pass


#!/usr/bin/env python3

# Copyright 2014 Brett Slatkin, Pearson Education Inc.
#
# Udostępniono na licencji Apache w wersji 2.0 ("Licencja").
# Tego pliku można używać jedynie zgodnie z warunkami Licencji.
# Treść Licencji znajdziesz na stronie:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# O ile obowiązujące prawo nie stanowi inaczej lub czegoś innego nie
# uzgodniono w formie pisemnej, oprogramowanie objęte Licencją jest
# dostarczane w stanie, w jakim jest (wersja "AS IS"), BEZ JAKIEJKOLWIEK
# GWARANCJI, ani wyrażonej otwarcie, ani domyślnej. Dokładne zasady
# i warunki Licencji znajdziesz w jej treści.

# Przygotowania mające na celu odtworzenie środowiska użytego w książce.
import logging
from pprint import pprint
from sys import stdout as STDOUT

# Przykład 1.
from datetime import datetime, timedelta

class Bucket(object):
    def __init__(self, period):
        self.period_delta = timedelta(seconds=period)
        self.reset_time = datetime.now()
        self.quota = 0

    def __repr__(self):
        return 'Wiadro(quota=%d)' % self.quota

bucket = Bucket(60)
print(bucket)

# Przykład 2.
def fill(bucket, amount):
    now = datetime.now()
    if now - bucket.reset_time > bucket.period_delta:
        bucket.quota = 0
        bucket.reset_time = now
    bucket.quota += amount

# Przykład 3.
def deduct(bucket, amount):
    now = datetime.now()
    if now - bucket.reset_time > bucket.period_delta:
        return False
    if bucket.quota - amount < 0:
        return False
    bucket.quota -= amount
    return True

# Przykład 4.
bucket = Bucket(60)
fill(bucket, 100)
print(bucket)

# Przykład 5.
if deduct(bucket, 99):
    print('Potrzebne 99 jednostek danych')
else:
    print('Nie ma 99 jednostek danych')
print(bucket)

# Przykład 6.
if deduct(bucket, 3):
    print('Potrzebne 3 jednostki danych')
else:
    print('Nie ma 3 jednostek danych')
print(bucket)

# Przykład 7.
class Bucket(object):
    def __init__(self, period):
        self.period_delta = timedelta(seconds=period)
        self.reset_time = datetime.now()
        self.max_quota = 0
        self.quota_consumed = 0

    def __repr__(self):
        return ('Bucket(max_quota=%d, quota_consumed=%d)' %
                (self.max_quota, self.quota_consumed))

# Przykład 8.
    @property
    def quota(self):
        return self.max_quota - self.quota_consumed

# Przykład 9.
    @quota.setter
    def quota(self, amount):
        delta = self.max_quota - amount
        if amount == 0:
            # Liczba jednostek danych jest wyzerowana dla nowego przedziału czasu.
            self.quota_consumed = 0
            self.max_quota = 0
        elif delta < 0:
            # Liczba jednostek danych przygotowana dla nowego przedziału czasu.
            assert self.quota_consumed == 0
            self.max_quota = amount
        else:
            # Liczba jednostek danych wykorzystana w przedziale czasu.
            assert self.max_quota >= self.quota_consumed
            self.quota_consumed += delta

# Przykład 10.
bucket = Bucket(60)
print('Początkowo', bucket)
fill(bucket, 100)
print('Wypełniono', bucket)

if deduct(bucket, 99):
    print('Potrzebne 99 jednostek danych')
else:
    print('Nie ma 99 jednostek danych')

print('Teraz', bucket)

if deduct(bucket, 3):
    print('Potrzebne 3 jednostki danych')
else:
    print('Nie ma 3 jednostek danych')

print('Pozostało', bucket)

Python/Python - efektywny Python/item_30.py


from collections import defaultdict

def get1stK(arr, n, k):
  if k > n:
    return -1
  cnt = defaultdict(int)
  for earr in arr:
    cnt[earr] += 1
  for earr in arr:
    if cnt[earr] == k:
      return earr
  return -1

t = int(input())
for i in range(t):
  n, k = list(map(int, input().strip().split()))
  arr = list(map(int, input().strip().split()))
  print(get1stK(arr, n, k))


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^register', views.register),
    url(r'^logout', views.logout),
    url(r'^home', views.home),
    url(r'^wow', views.wow),
    url(r'^profile', views.profile)
]


import argparse
import time
from pathlib import Path

import numpy as np
from PIL import Image

import seam_carving

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('src', type=str)
    parser.add_argument('-o', dest='dst', type=str, default='a.jpg')
    parser.add_argument('--keep', type=str, default=None)
    parser.add_argument('--drop', type=str, default=None)
    parser.add_argument('--dw', type=int, default=0)
    parser.add_argument('--dh', type=int, default=0)
    parser.add_argument('--energy', type=str, default='backward',
                        choices=['backward', 'forward'])
    parser.add_argument('--order', type=str, default='width-first',
                        choices=['width-first', 'height-first', 'optimal'])
    args = parser.parse_args()

    try:
        print('Loading source image from {}'.format(args.src))
        src = np.array(Image.open(args.src))

        drop_mask = None
        if args.drop is not None:
            print('Loading drop_mask from {}'.format(args.drop))
            drop_mask = np.array(Image.open(args.drop).convert('L'))

        keep_mask = None
        if args.keep is not None:
            print('Loading keep_mask from {}'.format(args.keep))
            keep_mask = np.array(Image.open(args.keep).convert('L'))

        print('Performing seam carving...')
        start = time.time()
        if drop_mask is not None:
            dst = seam_carving.remove_object(src, drop_mask, keep_mask)
        else:
            src_h, src_w, _ = src.shape
            dst = seam_carving.resize(src, (src_w + args.dw, src_h + args.dh),
                                      args.energy, args.order, keep_mask)
        print('Done at {:.4f} second(s)'.format(time.time() - start))

        print('Saving output image to {}'.format(args.dst))
        Path(args.dst).parent.mkdir(parents=True, exist_ok=True)
        Image.fromarray(dst).save(args.dst)
    except Exception as e:
        print(e)
        exit(1)

if __name__ == "__main__":
    main()


# !/usr/bin/env python
# encoding: utf-8
"""
:copyright (c) 2014 - 2017, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Department of Energy) and contributors. All rights reserved.  # NOQA
:author
"""

import json

from django.http import JsonResponse
from rest_framework import viewsets
from rest_framework.authentication import SessionAuthentication
from rest_framework.decorators import list_route

from seed.authentication import SEEDAuthentication
from seed.decorators import ajax_request_class, require_organization_id_class
from seed.utils.api import api_endpoint_class
from seed.utils.buildings import (
    get_columns as utils_get_columns,
)

DEFAULT_CUSTOM_COLUMNS = [
    'project_id',
    'project_building_snapshots__status_label__name',
    'address_line_1',
    'city',
    'state_province',
]

class ColumnViewSetV1(viewsets.ViewSet):
    raise_exception = True
    authentication_classes = (SessionAuthentication, SEEDAuthentication)

    @require_organization_id_class
    @api_endpoint_class
    @ajax_request_class
    def list(self, request):
        """
        Returns a JSON list of columns a user can select as his/her default

        Requires the organization_id as a query parameter.

        This was formally /get_columns
        """
        all_fields = request.query_params.get('all_fields', '')
        all_fields = True if all_fields.lower() == 'true' else False
        return JsonResponse(utils_get_columns(request.query_params['organization_id'], all_fields))

    @api_endpoint_class
    @ajax_request_class
    @list_route(methods=['GET'])
    def get_default_columns(self, request):
        """
        Get default columns for building list view.

        front end is expecting a JSON object with an array of field names

        Returns::

            {
                "columns": ["project_id", "name", "gross_floor_area"]
            }
        """
        columns = request.user.default_custom_columns

        if columns == '{}' or isinstance(columns, dict):
            columns = DEFAULT_CUSTOM_COLUMNS
        if isinstance(columns, unicode):
            # PostgreSQL 9.1 stores JSONField as unicode
            columns = json.loads(columns)

        return JsonResponse({
            'columns': columns,
        })


import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import accuracy_score
from sklearn.model_selection import permutation_test_score
from sklearn.model_selection import StratifiedKFold

def bootstrap_resample(X, n=None):
    if n == None:
        n = len(X)

    resample_i = np.floor(np.random.rand(n)*len(X)).astype(int)
    X_resample = np.array(X[resample_i])
    return X_resample

if __name__ == '__main__':
    sns.set_style('whitegrid')

    for extreme_case in ['pyro', 'chanting', 'throwing', 'banner', 'others']:
        features_df = pd.DataFrame.from_csv(
            '/media/vasiliy/01D266BF62940460/Linux/football_research/output_data/max_features.csv', sep=';')

        data_df = features_df[[extreme_case, 'posts_number', 'stands_filling', 'posts_before_game_number', 'mean_text_length',
                               'mean_likes', 'mutual_friends_graph_density', 'mutual_communities_graph_density',
                               'friends_among_visitors_graph_density']]

        pyro_df = pd.DataFrame()

        pyro_positive_df = data_df[data_df[extreme_case] == 1]
        pyro_positive_df.reset_index(drop=True, inplace=True)

        pyro_negative_df = data_df[data_df[extreme_case] == 0]
        pyro_negative_df.reset_index(drop=True, inplace=True)

        for _ in range(5):
            data_df_resampled = pd.DataFrame()
            for col in data_df.columns:
                data_df_resampled[col] = bootstrap_resample(pyro_positive_df[col], 100)
            pyro_df = pyro_df.append(data_df_resampled)

            data_df_resampled = pd.DataFrame()
            for col in data_df.columns:
                data_df_resampled[col] = bootstrap_resample(pyro_negative_df[col], 100)
            pyro_df = pyro_df.append(data_df_resampled)

        X = pyro_df.drop([extreme_case], axis=1)
        Y = pyro_df[extreme_case].get_values()

        X = X.as_matrix()

        test = SelectKBest(chi2, k=4)
        fit = test.fit(X, Y)
        X_new = fit.transform(X)

        rocs = []

        for i in range(100):
            clf = LinearRegression()

            # Y = np.random.permutation(Y)

            X_train, X_test, Y_train, Y_test = train_test_split(X, Y)

            # Y_test = np.random.permutation(Y_test)

            clf.fit(X_train, Y_train)

            y_score = clf.predict(X_test)

            # kfold = StratifiedKFold(5)
            # score, permutation_scores, p_value = permutation_test_score(clf, X, Y, scoring="roc_auc", cv=kfold, n_permutations=100, n_jobs=1)
            # print('score = {0}, permutation_scores = {1}, p_value = {2}'.format(score, permutation_scores, p_value))
            # print(accuracy_score(Y_test, y_score))
            # compute ROC

            fpr = dict()
            tpr = dict()
            roc_auc = dict()

            fpr[0], tpr[0], _ = roc_curve(Y_test, y_score)
            roc_auc[0] = auc(fpr[0], tpr[0])

            rocs.append(roc_auc[0])

            fpr["micro"], tpr["micro"], _ = roc_curve(Y_test.ravel(), y_score.ravel())
            roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

            lw = 2

        mean_roc = np.mean(rocs)
        median_roc = np.median(rocs)
        std_roc = np.std(rocs)
        # print('mean roc = {0}'.format(mean_roc))
        # print('std roc = {0}'.format(std_roc))
        plt.figure()
        sns.distplot(rocs)
        plt.title('LinearRegression - ' + extreme_case + ' mean roc = {0:.2f}, median = {1:.2f}, std = {2:.2f}'.
                  format(round(mean_roc, 2), round(median_roc, 2), round(std_roc, 2)))
        rocs.clear()
    plt.show()
        # plt.savefig('/media/vasiliy/01D266BF62940460/Linux/football_research/output_data/img/rocs/artificial_data_rocs/' + 'LinearRegression' + extreme_case + '.png')


try:
    from googlesearch import search
except ImportError:
    print ("no module named 'google' found")
from typing import Any, Text, Dict, List ## Datatypes
from rasa_sdk import Action, Tracker  ##
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction

class kek(Action): # pode ser qualquer coisa, mas por padrão utilizaremos o nome da ação

    def name(self) -> Text:
        return "action_gg" # precisa ser o mesmo nome da ação utilizada nas stories e domain file

    def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        vacinas = {
        'Astrazeneca': {'numero de doses': 2, 'eficiencia': {'1 dose':'76%', '2 dose': '81%'}, 'eficiencia contra variantes': '92%', 'intervalo entre as doses': '3 meses', 'efeitos colaterais': 'Dor no local da injeção, dor de cabeça, fadiga, dor no corpo e mal esta'},
        'Pfizer': {'numero de doses': 2, 'eficiencia': {'1 dose':'61%', '2 dose': '88%'}, 'eficiencia contra variantes': '88%', 'intervalo entre as doses': '3 meses', 'efeitos colaterais':'Dor no local da injeção, dor de cabeça, fadiga, dor no corpo e mal estar'},
        'Coronavac': {'numero de doses': 2, 'eficiencia': {'1 dose':'sem dados%', '2 dose': '63%'}, 'eficiencia contra variantes': 'entre 69% e 78%', 'intervalo entre as doses': '21 dias', 'efeitos colaterais':'Dor no local da injeção, febre, cansaço e calafrios'},
        'Janssen': {'numero de doses': 1, 'eficiencia': {'1 dose':'85%', '2 dose': 'dose única'}, 'eficiencia contra variantes': '85%', 'intervalo entre as doses': 'dose única', 'efeitos colaterais':'Dor no local da injeção, vermelhidão, dores musculares, febre, náusea, dor no peito e falta de ar'},
        'Moderna': {'numero de doses': 2, 'eficiencia': {'1 dose':'94%', '2 dose': '94%'}, 'eficiencia contra variantes': '94%', 'intervalo entre as doses': '28 dias', 'efeitos colaterais':'Dor no local injeção, febre, vermelhidão, náusea e cansaço'},
        'Sputnik': {'numero de doses': 2, 'eficiencia': {'1 dose':'sem dados', '2 dose': '92%'}, 'eficiencia contra variantes': '81%', 'intervalo entre as doses': '21 dias', 'efeitos colaterais':'Dor no local injeção, febre, vermelhidão, náusea e cansaço'}
        }
        nome = 'Astrazeneca'
        nome2 = 'Pfizer'
        nome3 = 'Coronavac'
        nome4 = 'Janssen'
        nome5 = 'Moderna'
        nome6 = 'Sputnik'
        caracteristica = 'eficiencia contra variantes'
        dispatcher.utter_message(text ='A vacina ' + nome + ' possui ' + caracteristica + ": " + str(vacinas[nome][caracteristica]))
        dispatcher.utter_message(text ='A vacina ' + nome2 + ' possui ' + caracteristica + ": " + str(vacinas[nome2][caracteristica]))
        dispatcher.utter_message(text ='A vacina ' + nome3 + ' possui ' + caracteristica + ": " + str(vacinas[nome3][caracteristica]))
        dispatcher.utter_message(text ='A vacina ' + nome4 + ' possui ' + caracteristica + ": " + str(vacinas[nome4][caracteristica]))
        dispatcher.utter_message(text ='A vacina ' + nome5 + ' possui ' + caracteristica + ": " + str(vacinas[nome5][caracteristica]))
        dispatcher.utter_message(text ='A vacina ' + nome6 + ' possui ' + caracteristica + ": " + str(vacinas[nome6][caracteristica]))
        return []

class alfa(Action):

    def name(self) -> Text:
        return "action_uma"

    def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        eficacia = tracker.get_slot('eficacia')
        if eficacia == "Pfizer":
            dispatcher.utter_message(text ='1 dose 61%, 2 dose 88%')

        if eficacia == "Coronavac":
            dispatcher.utter_message(text ='primeira dose sem dados, 2 dose 63%')

        if eficacia == "Astrazeneca":
            dispatcher.utter_message(text ='1 dose 76%, 2 dose 81%')

        if eficacia == "Janssen":
            dispatcher.utter_message(text ='85%')

        if eficacia == "Sputnik":
            dispatcher.utter_message(text ='primeira dose sem dados, 2 dose 92%')

        if eficacia == "Moderna":
            dispatcher.utter_message(text ='1 dose 94%, 2 dose 94%')

class NomeVacinasForm(FormAction):

    def name(self) -> Text:

        return "nome_vacinas_form"

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:

        return ["eficacia"]

    @staticmethod
    def eficacia_db() -> List[Text]:
        return [
            "Pfizer",
            "Coronavac",

        ]

    def validate_spec(
        self,
        value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        if value in self.eficacia_db():
            return {"eficacia": value}
        else:
            dispatcher.utter_message(template="utter_vacina_invalida")
            return {"eficacia": None}

    def submit(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text,Any],
    )   -> List[Dict]:
        return []

class ActionSearch(Action):

    def name(self) -> Text:
        return "action_search" # precisa ser o mesmo nome da ação utilizada nas stories e domain file

    def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        # podemos chamar banco de dados
        # chamar alguma API
        # fazer qualquer coisa
        # adepois podemos dar um retorno ao usuário
        idade = tracker.get_slot('idade')
        cidade = tracker.get_slot('cidade')
        dispatcher.utter_message(text= 'pessoas com '+ str(idade)+' anos serão vacinadas semana que vem na cidade ' +str(cidade))
        return []

class Google(Action): # pode ser qualquer coisa, mas por padrão utilizaremos o nome da ação

    def name(self) -> Text:
        return "search_google" # precisa ser o mesmo nome da ação utilizada nas stories e domain file

    def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        idade = tracker.get_slot('idade')
        cidade = tracker.get_slot('cidade')
        query =  "vacinacao" +str(cidade) +str(idade)
        resultado = search(query)
        dispatcher.utter_message(text= 'voce pode encontrar essa informação em: '+str(resultado[0]))
        for j in search(query):
            print(j)
        return []


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import xml.etree.ElementTree as ET
from fwunit.ip import IP, IPSet
from fwunit.srx import parse
from nose.tools import eq_
from fwunit.test.util.srx_xml import route_xml_blackhole
from fwunit.test.util.srx_xml import route_xml_11_4R6
from fwunit.test.util.srx_xml import zones_empty_xml
from fwunit.test.util.srx_xml import FakeSRX

def parse_xml(xml, elt_path=None):
    elt = parse.strip_namespaces(ET.fromstring(xml))
    if elt_path:
        elt = elt.find(elt_path)
    return elt

def test_parse_zone():
    f = FakeSRX()
    z = f.add_zone('untrust')
    f.add_address(z, 'host1', '9.0.9.1/32')
    f.add_address(z, 'host2', '9.0.9.2/32')
    f.add_address(z, 'puppet', '9.0.9.3/32')
    f.add_address_set(z, 'hosts', 'host1', 'host2')
    f.add_interface(z, 'reth0')

    elt = parse_xml(
        f.fake_show('configuration security zones'), './/security-zone')
    z = parse.Zone._from_xml(elt)
    eq_(z.interfaces, ['reth0'])
    eq_(sorted(z.addresses.keys()),
        sorted(['any', 'any-ipv4', 'any-ipv6', 'host1', 'host2', 'hosts', 'puppet']))
    eq_(z.addresses['any'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv4'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv6'], IPSet([]))
    eq_(z.addresses['host1'], IPSet([IP('9.0.9.1')]))
    eq_(z.addresses['host2'], IPSet([IP('9.0.9.2')]))
    eq_(z.addresses['puppet'], IPSet([IP('9.0.9.3')]))
    eq_(z.addresses['hosts'], IPSet([IP('9.0.9.1'), IP('9.0.9.2')]))

def test_parse_zone_no_sets():
    f = FakeSRX()
    z = f.add_zone('untrust')
    f.add_address(z, 'trustedhost', '10.0.9.2/32')
    f.add_address(z, 'dmz', '10.1.0.0/16')
    f.add_address(z, 'shadow', '10.1.99.99/32')
    f.add_interface(z, 'reth1')

    elt = parse_xml(
        f.fake_show('configuration security zones'), './/security-zone')
    z = parse.Zone._from_xml(elt)
    eq_(z.interfaces, ['reth1'])
    eq_(sorted(z.addresses.keys()),
        sorted(['any', 'any-ipv4', 'any-ipv6', 'trustedhost', 'dmz', 'shadow']))
    eq_(z.addresses['any'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv4'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv6'], IPSet([]))
    eq_(z.addresses['trustedhost'], IPSet([IP('10.0.9.2')]))
    eq_(z.addresses['dmz'], IPSet([IP('10.1.0.0/16')]))
    eq_(z.addresses['shadow'], IPSet([IP('10.1.99.99')]))

def test_parse_global_policy():
    f = FakeSRX()
    f.add_policy('global',
                 dict(sequence=1, name='global-ping', src='any', dst='any',
                      app='ping', action='permit'))
    f.add_policy('global',
                 dict(sequence=10, name='global-deny', src='any', dst='any',
                      app='any', action='deny'))

    elt = parse_xml(
        f.fake_show('security policies global'), './/security-context')
    policies = [parse.Policy._from_xml(None, None, e) for e in elt.findall('./policies/policy-information')]
    eq_(policies[0].from_zone, None)
    eq_(policies[0].to_zone, None)
    eq_(policies[0].to_zone, None)
    eq_(policies[0].enabled, True)
    eq_(policies[0].sequence, 1)
    eq_(policies[0].source_addresses, ['any'])
    eq_(policies[0].destination_addresses, ['any'])
    eq_(policies[0].applications, ['ping'])
    eq_(policies[0].action, 'permit')
    eq_(policies[0].from_zone, None)
    eq_(policies[1].to_zone, None)
    eq_(policies[1].to_zone, None)
    eq_(policies[1].enabled, True)
    eq_(policies[1].sequence, 10)
    eq_(policies[1].source_addresses, ['any'])
    eq_(policies[1].destination_addresses, ['any'])
    eq_(policies[1].applications, ['any'])
    eq_(policies[1].action, 'deny')

def test_parse_addrbook_global():
    f = FakeSRX()
    ab = f.add_addrbook('global')
    f.add_address(ab, 'host1', '9.0.9.1/32')
    f.add_address(ab, 'host2', '9.0.9.2/32')
    f.add_address_set(ab, 'hosts', 'host1', 'host2')

    elt = parse_xml(
        f.fake_show('configuration security address-book'), './/address-book')
    z = parse.AddressBook._from_xml(elt)
    eq_(z.attaches, [])
    eq_(sorted(z.addresses.keys()),
        sorted(['any', 'any-ipv4', 'any-ipv6', 'host1', 'host2', 'hosts']))
    eq_(z.addresses['any'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv4'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv6'], IPSet([]))
    eq_(z.addresses['host1'], IPSet([IP('9.0.9.1')]))
    eq_(z.addresses['host2'], IPSet([IP('9.0.9.2')]))
    eq_(z.addresses['hosts'], IPSet([IP('9.0.9.1'), IP('9.0.9.2')]))

def test_parse_addrbook_attached():
    f = FakeSRX()
    ab = f.add_addrbook('general-stuff')
    f.add_address(ab, 'trustedhost', '10.0.9.2/32')
    f.add_attach(ab, 'untrust')
    f.add_attach(ab, 'trust')

    elt = parse_xml(
        f.fake_show('configuration security address-book'), './/address-book')
    z = parse.AddressBook._from_xml(elt)
    eq_(z.attaches, ['untrust', 'trust'])
    eq_(sorted(z.addresses.keys()),
        sorted(['any', 'any-ipv4', 'any-ipv6', 'trustedhost']))
    eq_(z.addresses['any'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv4'], IPSet([IP('0.0.0.0/0')]))
    eq_(z.addresses['any-ipv6'], IPSet([]))
    eq_(z.addresses['trustedhost'], IPSet([IP('10.0.9.2')]))

def test_parse_zones_empty():
    elt = parse_xml(zones_empty_xml, './/security-zone')
    z = parse.Zone._from_xml(elt)
    eq_(z.interfaces, [])
    eq_(sorted(z.addresses.keys()), sorted(['any', 'any-ipv6', 'any-ipv4']))

def test_parse_route_blackhole():
    elt = parse_xml(route_xml_blackhole, './/rt')
    r = parse.Route._from_xml(elt)
    eq_(r.destination, IP('20.0.0.0/8'))
    eq_(r.interface, None)
    eq_(r.is_local, False)

def test_parse_route_11_4R6():
    elt = parse_xml(route_xml_11_4R6, './/rt')
    r = parse.Route._from_xml(elt)
    eq_(r.destination, IP('0.0.0.0/0'))
    eq_(r.interface, 'reth0.10')
    eq_(r.is_local, False)


###与Spark相似，tensorflow也是构建一个有向无环的工作流，通过创建常量、变量、占位量，并定义三者之间的关系，最后放入工作流执行

import numpy as np
import tensorflow as t
#唯有将常量、变量、占位量放入session执行，才会有结果，类似于spark中的transform和show，可存放超参数
s = t.Session()

##创建常量，常量一旦创建，别无法改变

t.zeros([2,2])
t.ones([2,2])
t.fill(1,[2,2])
t.range(1.0,2,3)
t.constant(43,shape=[10,10])
c = t.constant([1,2,3,4,5,6],shape=[2,3])
s.run(c)
#random_shuffle可将矩阵的行随即调换位置
t.random_shuffle(c)

#均匀分布
t.random_uniform([3,3],1,10)
#高斯分布
t.random_normal([3,3,3])
#截断高斯，即与中心相距不超过2个标准差的区域
t.truncated_normal([2, 3])

#将np.array转为tensor.array
a = np.random.rand(3,3)
at = t.convert_to_tensor(a)

##创建变量,变量在每次迭代中都会被改变，需要由初始值和初始化，可存放训练参数和每层训练后的临时结果
#设初始值
t.Variable(c)
v1 = t.Variable(at,name="at")
#初始化
v2 = t.Variable(v1)
s.run(v2.initializer)
s.run(v2)
#全局初始化，所有变量都在初始化池中，可单个初始化，也可全部初始化
init_op = t.global_variables_initializer()
s.run(init_op)

#占位量，只需指定类型和维度，无需初始化，用于存放输出值
x = t.placeholder(t.float32,[2,2])
y = t.identity(x-1)
x_vals = np.random.rand(2,2)
s.run(y,feed_dict={x:x_vals})

##矩阵操作
#定义对角矩阵
identity_matrix = t.diag([1.0, 1.0, 1.0])

A = t.truncated_normal([2, 3])
B = t.fill([2,3], 5.0)
C = t.random_uniform([3,2])
D = t.convert_to_tensor(np.array([[1., 2., 3.],[-3., -7.,
-1.],[0., 5., -2.]]))
print(s.run(identity_matrix))

#加
s.run(A+B)
#点乘
s.run(t.matmul(B,identity_matrix*2))
#转置
s.run(t.transpose(D))
#行列式
s.run(t.matrix_determinant(D))
#求逆
s.run(t.matrix_inverse(D))
#用cholesky求特征值和特征向量
s.run(t.self_adjoint_eig(D))

#除
s.run(t.div(3,4))

sr = s.run
#将int转为float后除
sr(t.truediv(3,4))
#反转类型
sr(t.floordiv(3.0,4.0))
#求余
sr(t.mod(22.0,3))
#叉乘
sr(t.cross([1.0,0.,0.],[0.,1.,0.]))
#自定义操作
def custom_polynomial(value):
    return(t.subtract(3 * t.square(value), value) + 10)
print(s.run(custom_polynomial(11)))

#激活函数
s.run(t.nn.relu([-3,0,3]))
s.run(t.nn.relu6([-3,0,3]))
s.run(t.nn.sigmoid([1.0,2.0,3]))
s.run(t.nn.tanh([1.0,2.0,3]))
s.run(t.nn.softsign([1.0,2.0,3]))
s.run(t.nn.softplus([1.0,2.0,3]))
s.run(t.nn.elu(np.array([-1,0,1],dtype=float)))

#读入iris
from sklearn import datasets
iris = datasets.load_iris()
print(len(iris.data),iris.data[1:20])

#读入mnist
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
print(len(mnist.train.images),len(mnist.test.images),len(mnist.validation.images))
mnist.train.labels[1,:]


import cv2 as cv
import os
import numpy as np

file_path = os.path.dirname(__file__)

img = cv.imread(file_path+'/rose.jpg')

hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)

h,s,v = cv.split(hsv)
cv.imshow('hue',h)
cv.imshow('saturation',s)
cv.imshow('value',v)
img_merge = cv.merge((h,s,v))
output_merged = cv.cvtColor(img_merge,cv.COLOR_HSV2BGR)
cv.imshow('merged',output_merged)

cv.waitKey(0)
cv.destroyAllWindows()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
from codecs import open

import os
# _en_set = os.environ.get('HAS_SYS_ENCODING_SET', 0)
# if _en_set == 0:
#     import sys
#     reload(sys)
#     sys.setdefaultencoding('utf-8')
import json
from helper import SUID, PathHelper
import time
import shutil

def _fill_contents(tpl_sch_dir, sch_name, ct_dict):
    sch_path = os.path.join(tpl_sch_dir, sch_name + '.json')
    with open(sch_path, 'r', encoding='utf-8') as _f:
        tpl_sch = json.load(_f)
    var_bi, var_ctt = {}, {}
    if 'extends' in tpl_sch:
        var_bi, var_ctt = _fill_contents(tpl_sch_dir, tpl_sch['extends'], ct_dict)
    if 'build_in' in tpl_sch:
        for _k, _v in tpl_sch['build_in'].items():
            var_bi[_k] = ''  # TODO: Do Something
    if 'contents' in tpl_sch:
        for _k, _v in tpl_sch['contents'].items():
            optional = _v.get('optional', False)
            if _k in ct_dict:
                var_ctt[_k] = ct_dict[_k]
            else:
                if not optional:
                    raise KeyError('Content var: %s is required from template: %s !' % (_k, sch_name))
            # TODO: Validate !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    return var_bi, var_ctt

def _update_nav(root_fs, sp_path):
    # if repo_tree is None:
    #     repo_tree = {'': [0, 'root', 'P', {'cn': {'n': '首页'}, 'en': {'n': 'index'}}, {}]}
    #     cur_dir = repo_tree
    #     _p = root_fs
    #     for _d in sp_path[: -1]:
    #         cur_dir = cur_dir[_d][4]
    #         _p = os.path.join(_p, _d)
    #         with open(os.path.join(_p, 'repo.json'), 'r') as _f:
    #             repo = json.load(_f)
    #         for idx, item in enumerate(repo['items']):
    #             name, _type = item['n'], item['t']
    #             if _type == 'P':
    #                 cur_dir[name] = [idx, name, _type, item['inf'], {}]
    # cur_dir = repo_tree
    # for _d in sp_path:
    #     cur_dir = cur_dir[_d][4]
    # _d = sp_path[-1]
    _p = os.path.join(root_fs, *sp_path)
    with open(os.path.join(_p, 'repo.json'), 'r', encoding='utf-8') as _f:
        repo = json.load(_f)
    # for idx, item in enumerate(repo['items']):
    #     name, _type = item['n'], item['t']
    #     if _type == 'P':
    #         cur_dir[name] = [idx, name, _type, item['inf'], {}]

    for item in repo['items']:
        name, _type = item['n'], item['t']
        if _type == 'P':
            sub_sp_path = sp_path + [name]
            _update_nav(root_fs, sub_sp_path)

    with open(os.path.join(_p, 'nav.json'), 'r', encoding='utf-8') as _f:
        nav = json.load(_f)

    # if nav['t'] == 'A':
    #     _path = []
    #     cur_dir = repo_tree
    #     for _d in nav['ns'][: -1]:
    #         _path.append(_d)
    #         cur_dir = cur_dir[_d][4]
    #     for _d in sp_path[len(nav['ns']) - 1: ]:
    #         _path.append(_d)
    #         cur_dir = cur_dir[_d][4]
    if nav['t'] == 'A':
        _path = []
        for _d in nav['ns']:
            _path.append(_d)
        _pp = os.path.join(root_fs, *_path)
        with open(os.path.join(_pp, 'prop.json'), 'r', encoding='utf-8') as _f:
            prop = json.load(_f)
        info = {"en": {"n": prop['inf']['en']['nm']}, "cn": {"n": prop['inf']['cn']['nm']}}
        _u_p = '/'.join(_path)
        nav_tree = {'nm': prop['pn'], 't': 'P', 'inf': info, 'ch': [], 'p': _u_p}
        next_b = nav_tree
        for _d in sp_path[len(nav['ns']) + 1:] + [None]:
            cur_b = next_b
            with open(os.path.join(_pp, 'repo.json'), 'r', encoding='utf-8') as _f:
                repo = json.load(_f)
            for item in repo['items']:
                name, _type = item['n'], item['t']
                if _type == 'P':
                    _b = {'nm': name, 't': _type, 'inf': item['inf'], 'ch': [], 'p': _u_p + '/' + name}
                    cur_b['ch'].append(_b)
                    if name == _d:
                        next_b = _b
            if _d is None:
                break
            _path.append(_d)
            _u_p += '/' + _d
            _pp = os.path.join(root_fs, *_path)
        nav['nt'] = [nav_tree]
        # print json.dumps(nav_tree)
        with open(os.path.join(_p, 'nav.json'), 'w', encoding='utf-8') as _f:
            json.dump(nav, _f, ensure_ascii=False)

# _update_nav('./data', [''])

# with open(os.path.join(fs_path, 'repo.json'), 'r') as _f:
    #     repo = json.load(_f)
    # cur_dir = {}
    # for idx, item in enumerate(repo['items']):
    #     name, _type = item['n'], item['t']
    #     if _type == 'P'
    #         cur_dir[name] = [idx, name, _type]

# _fill_contents('./templates/template_schema', 'general_view', {'body_content': '23333', 'title': 'hhhhhh', 'useless': '213213'})

def get_auth():
    # TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    return True, 'admin', 'admins'

def check_perm(need_perms):
    def perm_dec(func):
        def dec_func(cls, path, *args, **kw):
            if isinstance(path, tuple):
                path, sp_path, fs_path, path_type = path
            else:
                path, sp_path, fs_path, path_type = cls.path_h.do_all(path)
            is_login, user, group = get_auth()
            print('Method: %s for user: %s, group: %s need permission: %s:' % (str(func), user, group, need_perms))  # TODO: debug
            # TODO: permission check !!!!!!!!!!
            kw['user'], kw['group'] = user, group
            kw['inh_perm'] = 508  # TODO !!!!!!!!!!!!! inherent permission
            func(cls, (path, sp_path, fs_path, path_type), *args, **kw)
        return dec_func
    return perm_dec

class DirSite(object):
    def __init__(self, root_dir, template_dir='./templates'):
        self._root_dir = root_dir
        self.path_h = PathHelper(root_dir)
        self.tpl_sch_dir = os.path.join(template_dir, 'template_schema')

    @check_perm('w')
    def add_page(self, path_inf, contents, page_name=None, permission=None, extra_perm=None,
                 name_en='', name_cn=None, tags=None, **kw):

        par_path, par_sp_path, par_fs_path, par_path_type = path_inf
        uuid = SUID.timeUID()
        # TODO: validate page_name is plain text
        if page_name is None:
            page_name = uuid

        sp_path = par_sp_path + [page_name]
        path = '/'.join(sp_path)
        path, sp_path, fs_path, path_type = self.path_h.do_all(path)

        if path_type is not None:
            raise IOError('Path or File: %s already exists !' % fs_path)
        user = kw['user']
        group = kw['group']
        if permission is None:
            permission = kw['inh_perm']

        if extra_perm:
            # TODO:  Check extra permission !
            print("Not Implement: extra permission !")

        if tags:
            assert isinstance(tags, list)
            # TODO: Check tags
        else:
            tags = []

        page_prop = {
            "id": uuid,
            "pn": page_name,
            "pms": {"o": user, "g": group, "p": permission, "ep": []},
            "cr": user,
            "crt": int(time.time()),
            "lmt": int(time.time()),
            "tgs": tags,
            "inf": {
                "cn": {
                    "nm": name_cn if name_cn else name_en
                },
                "en": {
                    "nm": name_en
                }
            }
        }

        page_repo = {
            "id": uuid,
            "items": [],
        }

        if len(sp_path) == 1:
            nav_start = []
            nav_type = 'N'
        else:
            nav_start = sp_path[0: 2]
            nav_type = 'A'
        page_nav = {
            "id": uuid,
            "t": nav_type,   # type: A: automatic, N: None
            "ns": nav_start,  # Nav Start splited Path
            "nt": [],  # Nav Tree
        }

        os.makedirs(fs_path)
        path_type = 'D'

        with open(os.path.join(fs_path, 'prop.json'), 'w', encoding='utf-8') as _f:
            json.dump(page_prop, _f, ensure_ascii=False)
        with open(os.path.join(fs_path, 'repo.json'), 'w', encoding='utf-8') as _f:
            json.dump(page_repo, _f, ensure_ascii=False)
        with open(os.path.join(fs_path, 'nav.json'), 'w', encoding='utf-8') as _f:
            json.dump(page_nav, _f, ensure_ascii=False)

        self.edit_page((path, sp_path, fs_path, path_type), contents)

        self._add_child(path_inf, page_name, 'P', {'cn': {'n': name_cn}, 'en': {'n': name_en}})
        self.update_nav(path_inf)

    @check_perm('w')
    def edit_page(self, path_inf, contents, **kw):
        path, sp_path, fs_path, path_type = path_inf
        if path_type == 'D':
            for lang, ctt_upd in contents['contents'].items():
                page_fs_p = os.path.join(fs_path, 'page.%s.json' % lang)
                if os.path.exists(page_fs_p):
                    with open(page_fs_p, 'r', encoding='utf-8') as _f:
                        page = json.load(_f)
                    tpl_name = page['tpl']  # TODO: Can't change template
                else:
                    tpl_name = contents['template']
                    page = {
                        "tpl": tpl_name,  # template
                        'bi': {},  # build in
                        "ctt": {},  # contents
                    }
                ctt_fill = page['ctt']
                ctt_fill.update(ctt_upd)
                page['bi'], page['ctt'] = _fill_contents(self.tpl_sch_dir, tpl_name, ctt_fill)
                with open(page_fs_p, 'w', encoding='utf-8') as _f:
                    json.dump(page, _f, ensure_ascii=False)
        else:
            raise ValueError

    def _add_child(self, path_inf, name, ch_type, info, **kw):
        assert isinstance(path_inf, tuple)
        path, sp_path, fs_path, path_type = path_inf
        repo_path = os.path.join(fs_path, 'repo.json')
        with open(repo_path, 'r', encoding='utf-8') as _f:
            repo = json.load(_f)
        repo['items'].append({
            'n': name, 't': ch_type, 'inf': info,
        })
        with open(repo_path, 'w', encoding='utf-8') as _f:
            json.dump(repo, _f, ensure_ascii=False)

    @check_perm('w')
    def delete_page(self, path_inf, **kw):
        path, sp_path, fs_path, path_type = path_inf
        assert len(sp_path) > 1, 'Can not remove root directory !'
        par_sp_path = sp_path[: -1]
        par_path = '/'.join(par_sp_path)
        par_fs_path = self.path_h.fs_path(par_path)
        par_repo_fp = os.path.join(par_fs_path, 'repo.json')
        with open(par_repo_fp, 'r', encoding='utf-8') as _f:
            par_repo = json.load(_f)
        resource_name = sp_path[-1]
        for item in par_repo['items']:
            if item['n'] == resource_name:
                par_repo['items'].remove(item)
                break
        with open(par_repo_fp, 'w', encoding='utf-8') as _f:
            json.dump(par_repo, _f, ensure_ascii=False)
        if path_type == 'D':
            shutil.rmtree(fs_path)
        else:
            # TODO
            pass
        self.update_nav((par_path, par_sp_path, par_fs_path, 'D'))

    @check_perm('')
    def update_nav(self, path_inf, **kw):
        path, sp_path, fs_path, path_type = path_inf
        _update_nav(self._root_dir, sp_path)

# ss = DirSite('./data')
#
# ctt = {
#     'template': 'general_view',
#     'contents': {
#         'cn': {
#             'title': '中心简介222',
#             'body_content': '哈哈哈哈222',
#         },
#         'en': {
#             'title': 'Introduction222',
#             'body_content': 'hhhhh222',
#         }
#     }
# }
# ss.add_page('/', ctt, page_name='introduction', name_cn='中心简介', name_en='introduction')
# ss.add_page('/introduction', ctt, page_name='111', name_cn='111', name_en='111')
# ss.add_page('/introduction/111', ctt, page_name='222', name_cn='222', name_en='222')
# ss.add_page('/introduction/111/222', ctt, page_name='333', name_cn='333', name_en='333')
# ss.add_page('/introduction', ctt, page_name='com', name_cn='com', name_en='com')
#
# ss.edit_page('/introduction', ctt)


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

from django.shortcuts import render, redirect
from django.contrib import messages
from .models import User
def index(request):
    return render(request, "LoginReg/index.html")

def process(request):
    if request.method == 'POST':
        logic_id = int(request.POST['logic_id'])
        if logic_id == 1: #Register Form
            result = User.objects.register(request.POST)
            if 'err_messages' in result:
                for e in result['err_messages']:
                    messages.error(request, e)
            else:
                request.session['user_id'] = result['new_user'].id
                print('New user registered')
                return redirect('/dashboard')

        if logic_id == 2: #Login Form
            result = User.objects.login(request.POST)
            if 'err_messages' in result:
                for e in result['err_messages']:
                    messages.error(request, e)
            else:
                request.session['user_id'] = result['logged_user'].id
                return redirect('/dashboard')

    return redirect('/main')

def logout(request):
    del request.session['user_id']
    return redirect('/main')


## TOV SOLVER FOR SIMPLE POLYTROPES.
## Authors: Phil Chang, Zachariah B. Etienne, Leo Werneck

# Full documentation for this module may be found in the NRPy+ tutorial Jupyter notebook:
#  Tutorial-Start_to_Finish-BSSNCurvilinear-Setting_up_TOV_initial_data.ipynb

##############
# TOV SOLVER #
##############

# Inputs:
# * Output data file name
# * rho_baryon_central, the central density of the TOV star.
# * n, the polytropic equation of state index. n=1 models cold, degenerate neutron star matter.
# * K_Polytrope, the polytropic constant.
# * Verbose output toggle (default = True)

# Output: An initial data file (default file name = "outputTOVpolytrope.txt") that well
#         samples the (spherically symmetric) solution both inside and outside the star.
#         It is up to the initial data module to perform the 1D interpolation to generate
#         the solution at arbitrary radius. The file has the following columns:
# Column 1: Schwarzschild radius
# Column 2: rho(r), *total* mass-energy density (as opposed to baryonic rest-mass density)
# Column 3: P(r), Pressure
# Column 4: m(r), mass enclosed
# Column 5: e^{nu(r)}, g_{tt}(r)
# Column 6: e^{4 phi(r)}, conformal factor g_{rr}(r)
# Column 7: rbar(r), Isotropic radius

# rbar refers to the isotropic radius, and
# R_Schw refers to the Schwarzschild radius

# Step 1: Import needed Python/NRPy+ modules
import numpy as np
import scipy.integrate as si
import math
import sys

# Step 2: The TOV equations
def TOV_Solver(outfile = "outputTOVpolytrope.txt",
               rho_baryon_central=0.129285,
               rho_poly_tab=[],Gamma_poly_tab=[2.0], K_poly_tab0=1.0,
               verbose = True ):

    #####################################
    # Polytropic EOS lowlevel functions #
    #####################################

    # Function     : impose_continuity_on_P_cold()
    # Author(s)    : Leo Werneck
    # Description  : This function populates the array K_poly_tab
    #                by demanding that P_cold be everywhere continuous
    # Dependencies : none
    #
    # Inputs       : eos              - named tuple containing the following:
    #                  neos           - number of EOSs to be used (single polytrope = 1)
    #                  rho_poly_tab   - values of rho distinguish one EOS from the
    #                                   other (not required for a single polytrope)
    #                  Gamma_poly_tab - values of Gamma to be used within each EOS
    #                  K_poly_tab     - uninitialized, see output variable below
    #                  P_poly_tab     - uninitialized, see function
    #                                   compute_P_poly_tab() below
    #                K_poly_tab0      - value of K_poly_tab[0], for the first EOS
    #
    # Outputs      : eos.K_poly_tab   - values of K to be used within each EOS, determined
    #                                   by imposing that P_cold be everywhere continuous

    def impose_continuity_on_P_cold(eos,K_poly_tab0):

        # A piecewise polytropic EOS is given by
        # .--------------------------------------------------------------------------.
        # |      /     K_0 * rho^(Gamma_0)     ,                rho < rho_0 ;        |
        # |      |     K_1 * rho^(Gamma_1)     ,        rho_0 < rho < rho_1 ;        |
        # |      |          ...                                 ...                  |
        # | P = <      K_j * rho^(Gamma_j)     ,    rho_(j-1) < rho < rho_j ;        |
        # |      |          ...                                 ...                  |
        # |      | K_(n-2) * rho^(Gamma_(n-2)) , rho_(neos-3) < rho < rho_(neos-2) ; |
        # |      \ K_(n-1) * rho^(Gamma_(n-1)) ,                rho > rho_(neos-2) . |
        # .--------------------------------------------------------------------------.
        # Notice that the case of a single polytropic EOS corresponds to
        # the first EOS in the boxed equation above, with no condition on
        # rho. Thus we need only return K_poly_tab0.
        eos.K_poly_tab[0] = K_poly_tab0
        if eos.neos==1:
            return

        # For the case of a piecewise polytropic EOS, emanding that P_cold
        # be everywhere continuous results in the relation:
        # .-----------------------------------------------------.
        # | K_j = K_(j-1) * rho_(j-1)^( Gamma_(j-1) - Gamma_j ) |
        # .-----------------------------------------------------.
        for j in range(1,eos.neos):
            eos.K_poly_tab[j] = eos.K_poly_tab[j-1]*eos.rho_poly_tab[j-1]**(eos.Gamma_poly_tab[j-1]-eos.Gamma_poly_tab[j])

        return

    # Function     : compute_P_poly_tab()
    # Author(s)    : Leo Werneck
    # Description  : This function populates the array eos.P_poly_tab,
    #                used to distinguish which EOS we are using in the
    #                case of a piecewise polytropic EOS
    # Dependencies : none
    #
    # Inputs       : eos              - named tuple containing the following:
    #                  neos           - number of EOSs to be used (single polytrope = 1)
    #                  rho_poly_tab   - values of rho used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)
    #                  Gamma_poly_tab - values of Gamma to be used within each EOS
    #                  K_poly_tab     - value of K to be used within each EOS
    #                  P_poly_tab     - uninitialized, see output variable below
    #
    # Outputs      : eos.P_poly_tab   - values of P used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)

    def compute_P_poly_tab(eos):

        # We now compute the values of P_poly_tab that are used
        # to find the appropriate polytropic index and, thus,
        # EOS we must use.
        # First, if we have a single polytrope EOS, we need to
        # do nothing.
        if eos.neos==1:
            return

        # For the case of a piecewise polytropic EOS, we have
        # .---------------------------.
        # | P_j = K_j*rho_j^(Gamma_j) |
        # .---------------------------.
        for j in range(eos.neos-1):
            eos.P_poly_tab[j] = eos.K_poly_tab[j]*rho_poly_tab[j]**(Gamma_poly_tab[j])

        return

    # Function     : set_single_or_piecewise_polytrope_EOS_parameters()
    # Author(s)    : Leo Werneck
    # Description  : This function determine all polytropic related
    #                parameters from user input
    # Dependencies : impose_continuity_on_P_cold()
    #                compute_P_poly_tab()
    #
    # Inputs       : neos             - number of EOSs to be used (single polytrope = 1)
    #                rho_poly_tab     - values of rho distinguish one EOS from the
    #                                   other (not required for a single polytrope)
    #                Gamma_poly_tab   - values of Gamma to be used within each EOS
    #                K_poly_tab0      - value of K_poly_tab[0], for the first EOS
    #
    # Outputs      : eos              - named tuple containing the following:
    #                  neos           - number of EOSs to be used (single polytrope = 1)
    #                  rho_poly_tab   - values of rho used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)
    #                  Gamma_poly_tab - values of Gamma to be used within each EOS
    #                  K_poly_tab     - value of K to be used within each EOS
    #                  P_poly_tab     - values of P used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)

    def set_single_or_piecewise_polytrope_EOS_parameters(neos,rho_poly_tab,Gamma_poly_tab,K_poly_tab0):

        # Error check #1: Verify if the correct number of rho_poly_tab has been given by the user
        if (neos == 1):
            pass
        elif len(rho_poly_tab) != neos-1:
            print("Error: neos="+str(neos)+". Expected "+str(neos-1)+" values of rho_poly_tab, but "+str(len(rho_poly_tab))+" values were given.")
            sys.exit(1)

        # Error check #2: Verify if the correct number of Gamma_poly_tab has been given by the user
        if len(Gamma_poly_tab) != neos:
            print("Error: neos="+str(neos)+". Expected "+str(neos)+" values of Gamma_poly_tab, but "+str(len(Gamma_poly_tab))+" values were given.")
            sys.exit(2)

        # Create the arrays to store the values of K_poly_tab and eps_integ_const_tab
        K_poly_tab = [0 for i in range(neos)]
        P_poly_tab = [0 for i in range(neos-1)]

        # Create the EOS "struct" (named tuple)
        from collections import namedtuple
        eos_struct = namedtuple("eos_struct","neos rho_poly_tab Gamma_poly_tab K_poly_tab P_poly_tab")
        eos = eos_struct(neos,rho_poly_tab,Gamma_poly_tab,K_poly_tab,P_poly_tab)

        # Step 1: Determine K_poly_tab. For the details, please see the implementation
        #         of the function impose_continuity_on_P_cold() below.
        impose_continuity_on_P_cold(eos,K_poly_tab0)

        # Step 2: Determine eps_integ_const_tab. For the details, please see the
        #         implementation of the function impose_continuity_on_eps_cold() below.
        compute_P_poly_tab(eos)

        return eos

    # Function     : Polytrope_EOS__compute_P_cold_from_rhob()
    # Author(s)    : Leo Werneck
    # Description  : This function computes P_cold for a polytropic EOS
    # Dependencies : polytropic_index_from_rhob()
    #
    # Inputs       : eos              - named tuple containing the following:
    #                  neos           - number of EOSs to be used (single polytrope = 1)
    #                  rho_poly_tab   - values of rho distinguish one EOS from the
    #                                   other (not required for a single polytrope)
    #                  Gamma_poly_tab - values of Gamma to be used within each EOS
    #                  K_poly_tab     - value of K to be used within each EOS
    #                  P_poly_tab     - values of P used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)
    #                rho_baryon       - the value of rho for which we want to
    #                                   compute P_cold
    #
    # Outputs      : P_cold           - for a single or piecewise polytropic EOS

    def Polytrope_EOS__compute_P_cold_from_rhob(eos, rho_baryon):

        # Compute the polytropic index from rho_baryon
        j = polytropic_index_from_rhob(eos, rho_baryon)

        # Return the value of P_cold for a polytropic EOS
        # .--------------------------------.
        # | P_cold = K_j * rho_b^(Gamma_j) |
        # .--------------------------------.
        return eos.K_poly_tab[j]*rho_baryon**eos.Gamma_poly_tab[j]

    # Function     : Polytrope_EOS__compute_rhob_from_P_cold()
    # Author(s)    : Leo Werneck
    # Description  : This function computes rho_b for a polytropic EOS
    # Dependencies : polytropic_index_from_P()
    #
    # Inputs       : eos              - named tuple containing the following:
    #                  neos           - number of EOSs to be used (single polytrope = 1)
    #                  rho_poly_tab   - values of rho distinguish one EOS from the
    #                                   other (not required for a single polytrope)
    #                  Gamma_poly_tab - values of Gamma to be used within each EOS
    #                  K_poly_tab     - value of K to be used within each EOS
    #                  P_poly_tab     - values of P used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)
    #                P                - the value of P for which we want to
    #                                   compute rho_b
    #
    # Outputs      : rho_baryon       - for a single or piecewise polytropic EOS

    def Polytrope_EOS__compute_rhob_from_P_cold(eos,P):

        # Compute the polytropic index from P
        j = polytropic_index_from_P(eos,P)

        # Return the value of rho_b for a polytropic EOS
        # .----------------------------------.
        # | rho_b = (P_cold/K_j)^(1/Gamma_j) |
        # .----------------------------------.
        return (P/eos.K_poly_tab[j])**(1.0/eos.Gamma_poly_tab[j])

    # Function     : polytropic_index_from_rhob()
    # Author(s)    : Leo Werneck and Zach Etienne
    # Description  : This function computes P_cold for a polytropic EOS
    # Dependencies : none
    #
    # Input(s)     : eos              - named tuple containing the following:
    #                  neos           - number of EOSs to be used (single polytrope = 1)
    #                  rho_poly_tab   - values of rho distinguish one EOS from the
    #                                   other (not required for a single polytrope)
    #                  Gamma_poly_tab - values of Gamma to be used within each EOS
    #                  K_poly_tab     - value of K to be used within each EOS
    #                  P_poly_tab     - values of P used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)
    #                rho_in           - value of rho for which we compute the
    #                                   polytropic index
    #
    # Output(s)    : polytropic index computed from rho_in

    def polytropic_index_from_rhob(eos, rho_in):

        # Returns the value of the polytropic index based on rho_in
        polytropic_index = 0
        if not (eos.neos==1):
            for j in range(eos.neos-1):
                polytropic_index += (rho_in > eos.rho_poly_tab[j])

        return polytropic_index

    # Function     : polytropic_index_from_P()
    # Author(s)    : Leo Werneck and Zach Etienne
    # Description  : This function computes P_cold for a polytropic EOS
    # Dependencies : none
    #
    # Input(s)     : eos              - named tuple containing the following:
    #                  neos           - number of EOSs to be used (single polytrope = 1)
    #                  rho_poly_tab   - values of rho distinguish one EOS from the
    #                                   other (not required for a single polytrope)
    #                  Gamma_poly_tab - values of Gamma to be used within each EOS
    #                  K_poly_tab     - value of K to be used within each EOS
    #                  P_poly_tab     - values of P used to distinguish one EOS from
    #                                   the other (not required for a single polytrope)
    #                P_in             - value of P for which we compute the
    #                                   polytropic index
    #
    # Output(s)    : polytropic index computed from P_in

    def polytropic_index_from_P(eos, P_in):

        # Returns the value of the polytropic index based on P_in
        polytropic_index = 0
        if not (eos.neos==1):
            for j in range(eos.neos-1):
                polytropic_index += (P_in > eos.P_poly_tab[j])

        return polytropic_index
    def TOV_rhs(r_Schw, y) :
    # In \tilde units
    #
        P    = y[0]
        m    = y[1]
        nu   = y[2]
        rbar = y[3]

        j = polytropic_index_from_P(eos,P)
        Gamma = Gamma_poly_tab[j]
        Gam1  = Gamma-1.0

        rho_baryon = Polytrope_EOS__compute_rhob_from_P_cold(eos,P)
        rho = rho_baryon + P/Gam1 # rho is the *total* mass-energy density!

        if( r_Schw < 1e-4 or m <= 0.):
            m = 4*math.pi/3. * rho*r_Schw**3
            dPdrSchw = -(rho + P)*(4.*math.pi/3.*r_Schw*rho + 4.*math.pi*r_Schw*P)/(1.-8.*math.pi*rho*r_Schw*r_Schw)
            drbardrSchw = 1./(1. - 8.*math.pi*rho*r_Schw*r_Schw)**0.5
        else:
            dPdrSchw = -(rho + P)*(m + 4.*math.pi*r_Schw**3*P)/(r_Schw*r_Schw*(1.-2.*m/r_Schw))
            drbardrSchw = 1./(1. - 2.*m/r_Schw)**0.5*rbar/r_Schw

        dmdrSchw  =  4.*math.pi*r_Schw*r_Schw*rho
        dnudrSchw = -2./(P + rho)*dPdrSchw
        return [dPdrSchw, dmdrSchw, dnudrSchw, drbardrSchw]

    def integrateStar( eos, P, dumpData = False ):
        integrator = si.ode(TOV_rhs).set_integrator('dop853')
        y0 = [P, 0., 0., 0.]
        integrator.set_initial_value(y0,0.)
        dr_Schw = 1e-5
        P = y0[0]

        PArr      = []
        r_SchwArr = []
        mArr      = []
        nuArr     = []
        rbarArr   = []

        r_Schw = 0.

        while integrator.successful() and P > 1e-9*y0[0] :
            P, m, nu, rbar = integrator.integrate(r_Schw + dr_Schw)
            r_Schw = integrator.t

            dPdrSchw, dmdrSchw, dnudrSchw, drbardrSchw = TOV_rhs( r_Schw+dr_Schw, [P,m,nu,rbar])
            dr_Schw = 0.1*min(abs(P/dPdrSchw), abs(m/dmdrSchw))
            dr_Schw = min(dr_Schw, 1e-2)
            PArr.append(P)
            r_SchwArr.append(r_Schw)
            mArr.append(m)
            nuArr.append(nu)
            rbarArr.append(rbar)

        M = mArr[-1]
        R_Schw = r_SchwArr[-1]

        # Apply integration constant to ensure rbar is continuous across TOV surface
        for ii in range(len(rbarArr)):
            rbarArr[ii] *= 0.5*(np.sqrt(R_Schw*(R_Schw - 2.0*M)) + R_Schw - M) / rbarArr[-1]

        nuArr_np = np.array(nuArr)
        # Rescale solution to nu so that it satisfies BC: exp(nu(R))=exp(nutilde-nu(r=R)) * (1 - 2m(R)/R)
        #   Thus, nu(R) = (nutilde - nu(r=R)) + log(1 - 2*m(R)/R)
        nuArr_np = nuArr_np - nuArr_np[-1] + math.log(1.-2.*mArr[-1]/r_SchwArr[-1])

        r_SchwArrExtend_np = 10.**(np.arange(0.01,5.0,0.01))*r_SchwArr[-1]

        r_SchwArr.extend(r_SchwArrExtend_np)
        mArr.extend(r_SchwArrExtend_np*0. + M)
        PArr.extend(r_SchwArrExtend_np*0.)
        exp2phiArr_np = np.append( np.exp(nuArr_np), 1. - 2.*M/r_SchwArrExtend_np)
        nuArr.extend(np.log(1. - 2.*M/r_SchwArrExtend_np))
        rbarArr.extend( 0.5*(np.sqrt(r_SchwArrExtend_np**2 - 2.*M*r_SchwArrExtend_np) + r_SchwArrExtend_np - M) )

        # Appending to a Python array does what one would reasonably expect.
        #   Appending to a numpy array allocates space for a new array with size+1,
        #   then copies the data over... over and over... super inefficient.
        r_SchwArr_np     = np.array(r_SchwArr)
        PArr_np          = np.array(PArr)
        rho_baryonArr_np = np.array(PArr)
        for j in range(len(PArr_np)):
            # Compute rho_b from P
            rho_baryonArr_np[j] = Polytrope_EOS__compute_rhob_from_P_cold(eos,PArr_np[j])

        mArr_np               = np.array(mArr)
        rbarArr_np            = np.array(rbarArr)
        confFactor_exp4phi_np = (r_SchwArr_np/rbarArr_np)**2

        # Compute the *total* mass-energy density (as opposed to the *baryonic* mass density)
        rhoArr_np = []
        for i in range(len(rho_baryonArr_np)):
            polytropic_index = 0
            if not (eos.neos==1):
                for i in range(eos.neos-1):
                    polytropic_index += (PArr_np[j] > P_poly_tab[i])

            rhoArr_np.append(rho_baryonArr_np[i] + PArr_np[i]/(eos.Gamma_poly_tab[polytropic_index] - 1.))

        print(len(r_SchwArr_np),len(rhoArr_np),len(PArr_np),len(mArr_np),len(exp2phiArr_np))
        # Special thanks to Leonardo Werneck for pointing out this issue with zip()
        if sys.version_info[0] < 3:
            np.savetxt(outfile, zip(r_SchwArr_np,rhoArr_np,PArr_np,mArr_np,exp2phiArr_np,confFactor_exp4phi_np,rbarArr_np),
                       fmt="%.15e")
        else:
            np.savetxt(outfile, list(zip(r_SchwArr_np,rhoArr_np,PArr_np,mArr_np,exp2phiArr_np,confFactor_exp4phi_np,rbarArr_np)),
                       fmt="%.15e")

        return R_Schw, M

    # Set neos from input variables
    neos = len(Gamma_poly_tab)

    # Set polytropic quantities
    eos = set_single_or_piecewise_polytrope_EOS_parameters(neos,rho_poly_tab,Gamma_poly_tab,K_poly_tab0)

    # Set initial condition from rho_baryon_central
    P_initial_condition = Polytrope_EOS__compute_P_cold_from_rhob(eos, rho_baryon_central)

    # Integrate the initial condition
    R_Schw_TOV, M_TOV = integrateStar(eos, P_initial_condition, True)
    if verbose:
        print("Just generated a TOV star with R_Schw = " + str(R_Schw_TOV) + " , M = " + str(M_TOV) + " , M/R_Schw = "
              + str(M_TOV / R_Schw_TOV) + " .")


import json

from asyncio import get_event_loop

from aiohttp.web import View, json_response
from aiohttp_jinja2 import template

from video_relay.db import db
from video_relay.websocket import send_data_to_all

class PlayerResource(View):

    async def get(self):
        source = await db.player.find_one()
        if not source:
            source = {
                'provider': 'twitch',
                'channel_id': 'wgl_en',
            }
        return json_response(source)

class AdminResource(View):

    async def post(self):
        data = await self.request.post()
        result = await db.player.insert_one(dict(data))
        msg = json.dumps({
            'type': 'UPDATE_PLAYER_DATA',
            'payload': {
                'provider': data['provider'],
                'channel_id': data['channel_id'],
            }
        })

        loop = get_event_loop()
        loop.call_soon(send_data_to_all, msg)
        return json_response({'result': 'ok'})


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

from django.utils import timezone
import logging
from datetime import datetime, timedelta
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.shortcuts import render
from intranet import settings
from ..users.models import User
from ..schedule.views import schedule_context
from ..announcements.models import Announcement, AnnouncementRequest
from ..eighth.models import (
    EighthBlock, EighthSignup, EighthScheduledActivity
)

logger = logging.getLogger(__name__)

def gen_schedule(user, num_blocks=6, surrounding_blocks=None):
    """Generate a list of information about a block and a student's
    current activity signup.

    Returns:
        schedule
        no_signup_today

    """
    no_signup_today = None
    schedule = []

    if surrounding_blocks is None:
        surrounding_blocks = EighthBlock.objects.get_upcoming_blocks(num_blocks)

    if len(surrounding_blocks) == 0:
        return None, False

    # Use select_related to reduce query count
    signups = (EighthSignup.objects.filter(user=user,
                                           scheduled_activity__block__in=surrounding_blocks)
               .select_related("scheduled_activity",
                               "scheduled_activity__block",
                               "scheduled_activity__activity")
               .nocache())
    block_signup_map = {s.scheduled_activity.block.id: s.scheduled_activity for s in signups}

    for b in surrounding_blocks:
        current_sched_act = block_signup_map.get(b.id, None)
        if current_sched_act:
            current_signup = current_sched_act.title_with_flags
            current_signup_cancelled = current_sched_act.cancelled
            current_signup_sticky = current_sched_act.activity.sticky
        else:
            current_signup = None
            current_signup_cancelled = False
            current_signup_sticky = False

        # warning flag (red block text and signup link) if no signup today
        # cancelled flag (red activity text) if cancelled
        flags = "locked" if b.locked else "open"
        blk_today = b.is_today()
        if (blk_today and not current_signup):
            flags += " warning"
        if current_signup_cancelled:
            flags += " cancelled warning"

        if current_signup_cancelled:
            # don't duplicate this info; already caught
            current_signup = current_signup.replace(" (Cancelled)", "")

        info = {
            "id": b.id,
            "block": b,
            "block_letter": b.block_letter,
            "current_signup": current_signup,
            "current_signup_cancelled": current_signup_cancelled,
            "current_signup_sticky": current_signup_sticky,
            "locked": b.locked,
            "date": b.date,
            "flags": flags,
            "is_today": blk_today,
            "signup_time": b.signup_time,
            "signup_time_future": b.signup_time_future
        }
        schedule.append(info)

        if blk_today and not current_signup:
            no_signup_today = True

    return schedule, no_signup_today

def gen_sponsor_schedule(user, sponsor=None, num_blocks=6, surrounding_blocks=None):
    """Return a list of :class:`EighthScheduledActivity`\s in which the
    given user is sponsoring.

    Returns:
        sponsor_schedule
        no_attendance_today
    """

    no_attendance_today = None
    acts = []

    if sponsor is None:
        sponsor = user.get_eighth_sponsor()

    if surrounding_blocks is None:
        surrounding_blocks = EighthBlock.objects.get_upcoming_blocks(num_blocks).nocache()

    activities_sponsoring = (EighthScheduledActivity.objects.for_sponsor(sponsor)
                                                            .select_related("block")
                                                            .filter(block__in=surrounding_blocks)
                                                            .nocache())
    sponsoring_block_map = {}
    for sa in activities_sponsoring:
        bid = sa.block.id
        if bid in sponsoring_block_map:
            sponsoring_block_map[bid] += [sa]
        else:
            sponsoring_block_map[bid] = [sa]

    for b in surrounding_blocks:
        num_added = 0
        sponsored_for_block = sponsoring_block_map.get(b.id, [])

        for schact in sponsored_for_block:
            acts.append(schact)
            if schact.block.is_today():
                if not schact.attendance_taken and schact.block.locked:
                    no_attendance_today = True

            num_added += 1

        if num_added == 0:
            # fake an entry for a block where there is no sponsorship
            acts.append({
                "block": b,
                "id": None,
                "fake": True
            })

    logger.debug(acts)
    return acts, no_attendance_today

def find_birthdays(request):
    """Return information on user birthdays."""
    today = datetime.now().date()
    custom = False
    yr_inc = 0
    if "birthday_month" in request.GET and "birthday_day" in request.GET:
        try:
            mon = int(request.GET["birthday_month"])
            day = int(request.GET["birthday_day"])
            yr = today.year

            """ If searching a date that already happened this year, skip to the next year. """
            if mon < today.month or (mon == today.month and day < today.day):
                yr += 1
                yr_inc = 1

            real_today = today
            today = datetime(yr, mon, day).date()
            if today:
                custom = True
            else:
                today = real_today
        except Exception:
            pass

    key = "birthdays:{}".format(today)

    cached = cache.get(key)

    if cached:
        logger.debug("Birthdays on {} loaded "
                     "from cache.".format(today))
        logger.debug(cached)
        return cached
    else:
        logger.debug("Loading and caching birthday info for {}".format(today))
        tomorrow = today + timedelta(days=1)
        try:
            data = {
                "custom": custom,
                "today": {
                    "date": today,
                    "users": [{
                        "id": u.id,
                        "full_name": u.full_name,
                        "grade": {
                            "name": u.grade.name
                        },
                        "age": (u.age + yr_inc) if u.age is not None else -1
                    } if u else {} for u in User.objects.users_with_birthday(today.month, today.day)],
                    "inc": 0
                },
                "tomorrow": {
                    "date": tomorrow,
                    "users": [{
                        "id": u.id,
                        "full_name": u.full_name,
                        "grade": {
                            "name": u.grade.name
                        },
                        "age": (u.age - 1)
                    } for u in User.objects.users_with_birthday(tomorrow.month, tomorrow.day)],
                    "inc": 1
                }
            }
        except AttributeError:
            return None
        else:
            cache.set(key, data, timeout=60 * 60 * 24)
            return data

@login_required
def dashboard_view(request, show_widgets=True, show_expired=False):
    """Process and show the dashboard."""

    user = request.user

    announcements_admin = user.has_admin_permission("announcements")

    if not show_expired:
        show_expired = ("show_expired" in request.GET)

    if announcements_admin and "show_all" in request.GET:
        # Show all announcements if user has admin permissions and the
        # show_all GET argument is given.
        announcements = (Announcement.objects.all())
    else:
        # Only show announcements for groups that the user is enrolled in.
        if show_expired:
            announcements = (Announcement.objects
                             .visible_to_user(user))
        else:
            announcements = (Announcement.objects
                                         .visible_to_user(user)
                                         .filter(expiration_date__gt=timezone.now()))

    # pagination
    if "start" in request.GET:
        try:
            start_num = int(request.GET.get("start"))
        except ValueError:
            start_num = 0
    else:
        start_num = 0

    display_num = 10
    end_num = start_num + display_num
    more_announcements = ((announcements.count() - start_num) > display_num)
    try:
        announcements_sorted = announcements[start_num:end_num]
    except (ValueError, AssertionError):
        announcements_sorted = announcements[:display_num]
    else:
        announcements = announcements_sorted

    announcements = announcements.select_related("user").prefetch_related("groups", "event")

    user_hidden_announcements = (Announcement.objects.hidden_announcements(user)
                                                     .values_list("id", flat=True)).nocache()

    is_student = user.is_student
    is_teacher = user.is_teacher
    is_senior = (user.grade.number == 12) if user.grade and user.grade.number else False
    eighth_sponsor = user.get_eighth_sponsor()

    if show_widgets:
        dashboard_title = "Dashboard"
        dashboard_header = "Announcements"
    elif show_expired:
        dashboard_title = dashboard_header = "Announcement Archive"
    else:
        dashboard_title = dashboard_header = "Announcements"

    context = {
        "announcements": announcements,
        "announcements_admin": announcements_admin,
        "start_num": start_num,
        "end_num": end_num,
        "prev_page": start_num - display_num,
        "more_announcements": more_announcements,
        "hide_announcements": True,
        "user_hidden_announcements": user_hidden_announcements,
        "show_widgets": show_widgets,
        "show_expired": show_expired,
        "dashboard_title": dashboard_title,
        "dashboard_header": dashboard_header,
        "is_student": is_student,
        "is_teacher": is_teacher,
        "is_senior": is_senior
    }

    if show_widgets:
        if is_student or eighth_sponsor:
            num_blocks = 6
            surrounding_blocks = EighthBlock.objects.get_upcoming_blocks(num_blocks)

        if is_student:
            schedule, no_signup_today = gen_schedule(user, num_blocks, surrounding_blocks)
            context.update({
                "schedule": schedule,
                "no_signup_today": no_signup_today,
                "senior_graduation": settings.SENIOR_GRADUATION,
                "senior_graduation_year": settings.SENIOR_GRADUATION_YEAR,
            })

        if eighth_sponsor:
            sponsor_schedule, no_attendance_today = gen_sponsor_schedule(user, eighth_sponsor, num_blocks, surrounding_blocks)
            context.update({
                "sponsor_schedule": sponsor_schedule,
                "no_attendance_today": no_attendance_today
            })

        context.update({
            "eighth_sponsor": eighth_sponsor,
            "birthdays": find_birthdays(request),
            "sched_ctx": schedule_context(request)["sched_ctx"]
        })

    if announcements_admin:
        all_waiting = AnnouncementRequest.objects.filter(posted=None, rejected=False)
        awaiting_teacher = all_waiting.filter(teachers_approved__isnull=True)
        awaiting_approval = all_waiting.filter(teachers_approved__isnull=False)

        context.update({
            "awaiting_teacher": awaiting_teacher,
            "awaiting_approval": awaiting_approval,
        })

    return render(request, "dashboard/dashboard.html", context)